Digital System Design
Behavioral vs Structural Modeling
The same circuit can be written in Verilog in several ways. You can describe the gates and wires that make it up, write the Boolean equations, or describe what it should do and let the synthesis tool choose the gates. These styles are called structural, dataflow and behavioral modeling. Choosing the right style makes a design easier to read, easier to check and easier to reuse.
Learning objectives
By the end of this lesson you will be able to:
- Distinguish structural, dataflow and behavioral descriptions.
- Write the same full adder in all three styles.
- Use always blocks with if and case statements for combinational logic.
- Avoid the accidental latch caused by an incomplete description.
- Decide which style suits a given task.
The three modeling styles
| Style | What you write | Typical constructs |
|---|---|---|
| Structural | Components and how they are connected | Gate primitives, module instances, wires |
| Dataflow | How outputs depend on inputs through equations | assign with operators |
| Behavioral | What the circuit does, step by step | always blocks, if, case, loops |
A structural description is like a schematic in text form. A behavioral description is closer to a specification. The synthesis tool turns all three into gates, so the choice is about clarity and control, not about whether the result is "real" hardware.
Worked example 1: A full adder, structural
A full adder adds bits a, b and a carry-in. It can be built from two half adders and an OR gate. The truth table of a half adder is sum = a XOR b and carry = a AND b.
module half_adder (input a, input b, output s, output c);
xor g1 (s, a, b);
and g2 (c, a, b);
endmodule
module full_adder (input a, input b, input cin,
output sum, output cout);
wire s1, c1, c2;
half_adder ha1 (.a(a), .b(b), .s(s1), .c(c1));
half_adder ha2 (.a(s1), .b(cin), .s(sum), .c(c2));
or g3 (cout, c1, c2);
endmodule
Read it like a wiring diagram. The first half adder adds a and b. The second adds that partial sum to cin. The carry-out is 1 if either half adder produced a carry.
Gate count: each half adder has 1 XOR and 1 AND, so two of them give 2 XOR and 2 AND, plus 1 OR. The total is 5 gates.
Worked example 2: The same adder, dataflow
module full_adder_df (input a, input b, input cin,
output sum, output cout);
assign sum = a ^ b ^ cin;
assign cout = (a & b) | (cin & (a ^ b));
endmodule
The equations are sum = a XOR b XOR cin and cout = ab + cin(a XOR b). Check all eight input rows to confirm they are correct:
| a | b | cin | cout | sum |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
Each row satisfies a + b + cin = 2 x cout + sum. For example, row 1 1 1 gives 3 = 2 x 1 + 1. The carry can also be written as ab + bcin + a cin, which has the same truth table. You can verify equivalences like this with the truth table generator.
Worked example 3: The same adder, behavioral
module full_adder_bh (input a, input b, input cin,
output reg sum, output reg cout);
always @(*) begin
{cout, sum} = a + b + cin;
end
endmodule
This version states the intent: add three one-bit values and split the 2-bit result into carry and sum. The @(*) sensitivity list means the block re-evaluates whenever any input changes, which describes combinational logic. Outputs assigned inside an always block must be declared reg, but here they synthesize to plain gates, not flip-flops.
For this tiny circuit all three versions produce equivalent logic. The behavioral version is shortest, the structural one gives you the most control over exactly which gates are used, and the dataflow version sits in between.
Behavioral constructs: if and case
Behavioral modeling shines when logic is easier to say than to draw. A 4-to-1 multiplexer is a good case:
module mux4 (input [3:0] d, input [1:0] sel, output reg y);
always @(*) begin
case (sel)
2'b00: y = d[0];
2'b01: y = d[1];
2'b10: y = d[2];
2'b11: y = d[3];
endcase
end
endmodule
The structural alternative needs three 2-to-1 multiplexers: two in the first stage selected by sel[0], and one in the second stage selected by sel[1]. Written as gates, each 2-to-1 mux needs 4 gates (one NOT, two AND, one OR), so the structural version uses 3 x 4 = 12 gates. The case statement hides that detail and lets the tool decide.
Blocking and non-blocking assignments
Use the right assignment symbol for the right block type:
- Combinational always block: use blocking assignments
=. Statements are evaluated in order. - Clocked always block: use non-blocking assignments
<=. All registers update together on the clock edge.
Mixing the two in the same block is a common source of simulation and synthesis mismatches.
Avoiding accidental latches
In a combinational always block, every output must be given a value on every path. If a path leaves an output unassigned, the tool assumes the output must remember its old value and builds a latch.
// Bug: y is not assigned when en is 0, so a latch is created
always @(*) begin
if (en) y = d;
end
// Fix: assign y on every path
always @(*) begin
if (en) y = d;
else y = 1'b0;
end
The same problem appears when a case statement does not cover every value and has no default branch. Add a default, or assign a default value before the case.
Choosing a style
- Use structural to connect larger modules into a system, or when you must control the exact gates.
- Use dataflow for short equations and simple combinational logic.
- Use behavioral for state machines, counters, decoders and anything described by conditions.
- Mix them freely. Most real designs use structural code at the top level and behavioral code inside the modules.
Common mistakes
- Incomplete sensitivity lists. Listing only some inputs, such as
always @(a), makes simulation ignore changes to the others. Use@(*). - Missing default in case. This creates a latch on unlisted values.
- Assigning a wire inside an always block. Anything assigned in always must be
reg. - Using
<=in combinational logic or=in clocked logic. Follow the rule above. - Multiple drivers. Never drive the same signal from two assign statements or two always blocks.
Practice problems
Problem 1. How many gates are in the structural full adder above, and how many would a 4-bit ripple-carry adder built from four of them use?
Answer: 5 gates per full adder, so 4 x 5 = 20 gates.
Problem 2. Which style does assign y = s ? b : a; use?
Answer: Dataflow modeling, because it is a continuous assignment of an equation.
Problem 3. A 4-to-1 multiplexer is built structurally from 2-to-1 multiplexers. How many 2-to-1 multiplexers are needed?
Answer: 3. Two select between pairs of inputs, and one selects between their outputs.
Problem 4. Why does always @(*) if (en) y = d; create a latch?
Answer: Because y is not assigned when en is 0, so the circuit must hold its previous value. Add an else branch or a default assignment to fix it.
Frequently asked questions
Is behavioral code slower in hardware than structural code? Not necessarily. Synthesis tools optimise both. A clear behavioral description often produces results as good as hand-built gates.
Can I mix styles in one module? Yes. A module can contain assign statements, always blocks and instances together, as long as each signal has a single driver.
What is the difference between behavioral and RTL? RTL, register-transfer level, is the synthesizable style of behavioral code that describes data moving between registers each clock cycle. Behavioral can also include untimed or simulation-only code.
Next lesson: continue with RAM and ROM Structures to learn how the memories that hold data are organized.