🧩 What is a Module in Verilog? #
A module is the fundamental building block in Verilog. It represents a self-contained hardware block — such as a gate, a flip-flop, a counter, or even an entire processor.
📦 Module Structure #
Modules promote hierarchical design, reuse, and testability.
🧱 Example Module #
module and_gate (
input wire a,
input wire b,
output wire y
);
assign y = a & b;
endmodule
🔌 Module Port Types #
Ports define how a module communicates with the outside world.
Keyword | Direction | Description |
---|---|---|
input |
Into module | Read-only inside |
output |
Out of module | Can be wire or reg |
inout |
Bidirectional | Used for shared buses |
🧱 Module Instantiation Styles #
🔹 Positional Mapping #
and_gate u1 (a, b, y);
⚠️ Port order must match definition exactly.
🔹 Named Mapping ✅ (Recommended) #
and_gate u1 (
.a(in1),
.b(in2),
.y(out)
);
🔁 Multiple Instances #
and_gate u1 (.a(a1), .b(b1), .y(y1));
and_gate u2 (.a(a2), .b(b2), .y(y2));
🧪 Continuous Assignment: assign
#
The assign
keyword is used for combinational logic on wire
-type signals.
assign y = a & b;
- Continuous: updates automatically when inputs change
- Can only be used with
wire
🔁 Procedural Logic: always
Block
#
Used for logic that reacts to events (like clocks or conditions).
✅ Syntax #
always @(sensitivity_list) begin
// statements
end
⚙️ always @(*)
→ Combinational Logic
#
always @(*) begin
case (sel)
2'b00: y = a;
2'b01: y = b;
default: y = 0;
endcase
end
- Use blocking assignment (
=
)
⏱️ always @(posedge clk)
→ Sequential Logic
#
always @(posedge clk or posedge rst) begin
if (rst)
q <= 0;
else
q <= d;
end
- Use non-blocking assignment (
<=
) - Models flip-flops and registers
🧬 Bitwise Operators #
Operate on each bit individually:
Operator | Meaning | Example |
---|---|---|
& |
AND | y = a & b; |
| |
OR | y = a | b; |
^ |
XOR | y = a ^ b; |
~ |
NOT (bitwise) | y = ~a; |
~& |
NAND | y = ~(a & b); |
~| |
NOR | y = ~(a | b);\ |
Don’t confuse with logical operators (
&&
,||
) which are single-bit.
🔗 Concatenation & Replication #
🔹 Concatenation #
assign bus = {addr, data}; // Combines two 8-bit signals into one 16-bit bus
🔹 Replication #
assign all_ones = {8{1'b1}}; // 8-bit vector of all 1s
📊 Summary Table #
Construct | Use Case | Assignment Type | Typical Signal |
---|---|---|---|
assign |
Continuous logic | = |
wire |
always @(*) |
Combinational logic | = (blocking) |
reg |
always @(posedge clk) |
Sequential logic | <= (non-blocking) |
reg |
🧠 Best Practices #
- ✅ Always use named port mapping
- 🧾 Use meaningful instance names:
AND1
,stage0
, etc. - 🎯 Follow consistent port direction and grouping
- ⚠️ Avoid mixing
=
and<=
inside the samealways
block