Vectors in more detial¶
Declaring Vectors¶
Vectors must be declared:
Verilog | |
---|---|
type
specifies the datatype of the vector. This is usually wire
or reg
. If you are declaring a input or output port, the type can additionally include the port type (e.g., input
or output
) as well. Some examples:
The endianness (or, informally, "direction") of a vector is whether the the least significant bit has a lower index (little-endian, e.g., [3:0]) or a higher index (big-endian, e.g., [0:3]). In Verilog, once a vector is declared with a particular endianness, it must always be used the same way. e.g., writing vec[0:3]
when vec
is declared wire [3:0] vec;
is illegal. Being consistent with endianness is good practice, as weird bugs occur if vectors of different endianness are assigned or used together.
- 向量的方向性需要保持一致
Implicit nets¶
Implicit nets are often a source of hard-to-detect bugs. In Verilog, net-type signals can be implicitly created by an assign
statement or by attaching something undeclared to a module port. Implicit nets are always one-bit wires and causes bugs if you had intended to use a vector. Disabling creation of implicit nets can be done using the `default_nettype none
directive.
Example:
Adding `default_nettype none
would make the second line of code an error, which makes the bug more visible.
Unpacked vs. Packed Arrays¶
You may have noticed that in declarations, the vector indices are written before the vector name. This declares the "packed" dimensions of the array, where the bits are "packed" together into a blob (this is relevant in a simulator, but not in hardware). The unpacked dimensions are declared after the name. They are generally used to declare memory arrays. Since ECE253 didn't cover memory arrays, we have not used packed arrays in this course. See http://www.asic-world.com/systemverilog/data_types10.html for more details.
Example:
Accessing Vector Elements: Part-Select¶
Accessing an entire vector is done using the vector name. For example:
Verilog | |
---|---|
takes the entire 4-bit vector a and assigns it to the entire 8-bit vector w (declarations are taken from above). If the lengths of the right and left sides don't match, it is zero-extended or truncated as appropriate.
The part-select operator can be used to access a portion of a vector:
Practice¶
Problem statement¶
Build a combinational circuit that splits an input half-word (16 bits, [15:0] ) into lower [7:0] and upper [15:8] bytes.
Verilog | |
---|---|