Two families: nets and variables
Verilog signals come in two families.
Nets (wire) model physical connections. A net has no memory of its own; it simply carries whatever drives it, such as an assign statement or a module output.
wire y;
assign y = a & b; // y is driven continuously
Variables (reg, integer) hold a value until something assigns a new one. They are assigned inside procedural blocks (always or initial).
reg q;
always @(posedge clk)
q <= d; // q keeps its value between clock edges
The rule of thumb
| Assigned by… | Declare as |
|---|---|
assign statement or a module output port |
wire |
inside an always or initial block |
reg |
The classic trap: reg is not a register
Despite the name, reg does not mean a flip-flop. It only means “assigned procedurally”. Whether hardware gets a flip-flop depends on how you write the block:
reg y;
always @(*) // combinational: no clock
y = a & b; // y is a reg, but synthesis builds a plain AND gate
SystemVerilog fixed this confusing name with the logic type, which you can use in both places. Many teams now write logic everywhere. In plain Verilog, follow the table above.
Four logic values
Real wires are not always a clean 0 or 1, so Verilog has four values:
| Value | Meaning |
|---|---|
0 |
logic low |
1 |
logic high |
x |
unknown: uninitialised, or two drivers fighting |
z |
high impedance: nothing is driving the wire (tri-state) |
x is your friend in simulation. A reg with no reset starts as x, and if that x spreads to your outputs, the waveform shows you immediately that something was never initialised. That’s why the testbench in Lesson 2 used !==: the normal != returns x (not “true”) when a value is unknown, which can hide errors, while !== compares x and z exactly.
Vectors: multi-bit signals
Most signals are buses. Declare a range [msb:lsb]:
wire [7:0] data; // 8-bit bus, bit 7 is the MSB
reg [15:0] count; // 16-bit variable
wire [3:0] nibble;
assign nibble = data[7:4]; // part-select: upper 4 bits
assign msb = data[7]; // bit-select: a single bit
Concatenation joins signals, and replication repeats them:
assign word = {data, data}; // 16 bits: data twice
assign padded = {4'b0000, nibble}; // zero-extend 4 bits to 8
assign sext = {{8{data[7]}}, data};// sign-extend 8 bits to 16: repeat the MSB 8 times
Writing numbers
The general form is <size>'<base><value>:
| Literal | Meaning | Value |
|---|---|---|
4'b1010 |
4-bit binary | 10 |
8'hFF |
8-bit hexadecimal | 255 |
8'd200 |
8-bit decimal | 200 |
12'o777 |
12-bit octal | 511 |
16'h0 |
16-bit zero | 0 |
8'b1010_0101 |
underscores for readability | 165 |
Always give a size. An unsized number like 'hFF or plain 255 is at least 32 bits wide, which can cause surprises in comparisons and concatenations. Also watch out for truncation: 4'hFF silently keeps only the lower 4 bits, giving 4'hF.
Parameters: named constants
Use parameter to make modules configurable instead of hard-coding widths:
module register #(parameter WIDTH = 8) (
input wire clk,
input wire [WIDTH-1:0] d,
output reg [WIDTH-1:0] q
);
always @(posedge clk)
q <= d;
endmodule
// A 32-bit version, set at instantiation:
register #(.WIDTH(32)) r32 (.clk(clk), .d(bus_in), .q(bus_out));
The same module now gives you registers of any width.
Try it yourself: write a module swap_nibbles with input [7:0] in and output [7:0] out that swaps the upper and lower 4 bits using concatenation (8'hA5 should become 8'h5A). Write a testbench that checks three values with !==.
Key takeaways
wirefor continuous connections,regfor anything assigned inalwaysorinitial.regdoes not mean flip-flop.- Verilog uses four values:
0,1,x(unknown) andz(undriven). Use===or!==in testbenches to catchx. - Vectors use
[msb:lsb]. Use part-selects, concatenation{}and replication{n{}}to manipulate them. - Always write sized literals like
8'hFF, and use parameters for reusable widths.
Next lesson: operators and continuous assignment: building arithmetic, comparison and multiplexer logic with assign.