26

There are different data types in SystemVerilog that can be used like the following:

reg   [31:0] data;
logic [31:0] data;
bit   [31:0] data;

How do the three of them differ?

toolic
  • 57,801
  • 17
  • 75
  • 117
e19293001
  • 2,783
  • 9
  • 42
  • 54

7 Answers7

30

reg and wire were the original types. Wires are constantly assigned and regs are evaluated at particular points, the advantage here is for the simulator to make optimisations.

wire w_data;
assign w_data = y;

// Same function as above using reg
reg r_data;
always @* 
  r_data = y ;

A common mistake when learning Verilog is to assume the a reg type implies a register in hardware. The earlier optimisation for the simulator can be done through the context of its usage.

This introduces logic which can be used in place of wire and reg.

logic  w_data;
assign w_data = y;

// Same function as above using reg
logic r_data;
always @* 
  r_data = y ;

The type bit and byte have also been created that can only hold 2 states 0 or 1 no x or z. byte implies bit [7:0]. Using these types offers a small speed improvement but I would recommend not using them in RTL as your verification may miss uninitialized values or critical resets.

The usage of bit and byte would be more common in testbench components, but can lead to issues in case of having to drive x's to stimulate data corruption and recovery.


Update

At the time of writing I was under the impression that logic could not be used for tristate, I am unable to find the original paper that I based this on. Until further updates, comments or edits, I revoke my assertion that logic can not be used to create tri-state lines.


The tri type has been added, for explicitly defining a tri-state line. It is based on the properties of a wire, logic is based on the properties of a reg.

tri t_data;
assign t_data = (drive) ? y : 1'bz ;

If you no longer have to support backwards compatibility Verilog then I would recommend switching to using logic and tri. Using logic aids re-factoring and and tri reflects the design intent of a tristate line.

Morgan
  • 19,934
  • 8
  • 58
  • 84
  • Look at [this other answer](http://stackoverflow.com/questions/33580513/how-do-i-implement-a-parametrizable-mux-in-systemverilog) to see one case where logic cannot be used in the same place as wire. Look at the comments. – user4061565 Nov 10 '15 at 15:14
7
  • The choice of the name reg turned out to be a mistake, because the existence of registers is instead inferred based on how assignments are performed. Due to this, use of reg is essentially deprecated in favor of logic, which is actually the same type.

  • logic is a 1-bit, 4-state data type

  • bit is a 1-bit, 2-state data type which may simulate faster than logic
  • If a logic is also declared as a wire, it has the additional capability of supporting multiple drivers. Note that by default wire is equivalent to wire logic.
  • In general, the "nets" (such as wire and tri) are most suitable for designing communication buses.

Practically speaking, for RTL it usually doesn't matter whether you declare with reg, or logic, or wire. However, if you have to make an explicit declaration of a 4-state type (as opposed to when you don't), you should typically choose logic since that is what is intended by the language.


Related articles:

Community
  • 1
  • 1
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
2

As I'm unable to add a comment I've to write what looks like a new answer but isn't. Sigh!

@e19293001, @Morgan, logic defines a 4-state variable unlike bit, and hence a logic variable can be used to store 1'bz so the following code is valid and compiles:

logic t_data;
assign t_data = (drive) ? y : 1'bz ;

But I agree that to reflect the design intent tri should be used instead of logic in these cases (although I must say I don't see people using tri instead of logic/wire too often).

Paddu
  • 246
  • 2
  • 6
  • I am aware that logic holds 4 values (0,1,z,x) but was sure there was an issue with using it for tri-state. I can not find the paper or section in the LRM which says this. I think you might be correct, logic could be used to drive tri state. – Morgan Apr 14 '14 at 13:53
0

reg and logic are exactly the same. These data types appear inside the always or initial blocks and store values i.e. always @(a) b <= a;, the reg b gets evaluated only when 'a' changes but otherwise it simply stores the value it has been assigned last.

wire are just simply connections and need to continuously driven. I agree that they can behave identical as @Morgan mentioned, but they can be imagined as a piece of hard wire, the value of which changes only the value at the other end or the source changes.

0

How do the three of them differ?

There is no difference between logic and reg.

The difference between bit and the other two is that bit is 2-state, whereas logic/reg are 4-state.

Refer to IEEE Std 1800-2017, section 6.11.2, 2-state (two-value) and 4-state (four-value) data types:

logic and reg denote the same type.

Also, section 6.3.1 Logic values:

The SystemVerilog value set consists of the following four basic values:
0 —represents a logic zero or a false condition
1 —represents a logic one or a true condition
x —represents an unknown logic value
z —represents a high-impedance state

Several SystemVerilog data types are 4-state types, which can store all four logic values. All bits of 4-state vectors can be independently set to one of the four basic values. Some SystemVerilog data types are 2-state, and only store 0 or 1 values in each bit of a vector.

toolic
  • 57,801
  • 17
  • 75
  • 117
-1

Logic data type doesn't permit multiple driver. The last assignment wins in case of multiple assignment .Reg/Wire data type give X if multiple driver try to drive them with different value. Logic data type simply assign the last assignment value.

-1

The "Reg" data type is used in procedural assignment, whereas the "Logic" data type can be used anywhere.

Lalita
  • 1
  • This answer is incorrect, as this [simulation on edaplayground proves](https://www.edaplayground.com/x/cagK). Also, Reg and Logic must not be capitalized. – toolic Jan 08 '23 at 12:34