Wize Electric Circuits and Electronics Textbook > Digital Logic Design

Computer-Aided Design and Simulation Tools for Digital Design

Hardware Description Languages

Once a digital design grows past a few dozen gates, drawing and checking schematics by hand stops scaling. Engineers instead describe the circuit as text, simulate that text to check its behaviour, and pass the synthesizable code to a tool that builds the gates. You meet this workflow in your first FPGA lab, usually in Verilog or VHDL.


Flow diagram in which HDL source code branches to simulation with a testbench and to synthesis, a gate-level netlist and place and route, with a feedback loop from simulation back to the source.


Describing hardware in text

A hardware description language (HDL) is a language for describing the structure and behaviour of digital circuits, so that a circuit description can be both simulated and turned into hardware. Verilog, SystemVerilog and VHDL are common examples. The basic unit is a module (an entity plus an architecture in VHDL) with named input and output ports, and a larger design instantiates smaller modules the way a board holds chips.


You can describe the same 2-to-1 multiplexer in three styles:


Style

What you write

Verilog example

Structural

gate or module instances wired together by nets

and g1 (t1, s, b); plus an inverter for s, another AND gate and an OR gate

Dataflow

continuous assignments of Boolean expressions

assign y = s ? b : a;

Behavioral

procedural blocks with if or case

always @(*) if (s) y = b; else y = a;



Simulation in event-driven time

A simulator keeps a queue of scheduled events and advances through event times instead of calculating every intervening instant. A delay such as #5 schedules an action 5 time units later, and the timescale directive sets the unit and precision, for example 1 ns and 1 ps. Verilog treats a delay on a continuous assignment as inertial: a pending output transition is cancelled if the evaluated right-hand-side value changes before that transition takes effect. An operand change that leaves the expression's value unchanged does not cancel the pending transition.


You check a module with a testbench, a separate module that is never synthesized. It drives the inputs, waits, and compares the outputs with expected values. For a combinational module with nn input bits, an exhaustive testbench applies every input combination. If each combination is held for Δt\Delta t, the vector count and the simulated time are


N=2n,Tsim=N Δt=2n ΔtN = 2^{n}, \qquad T_{\text{sim}} = N\,\Delta t = 2^{n}\,\Delta t


The stimulus loop below drives a 4-input module and holds each vector for 20 ns, so it applies 24=162^4 = 16 vectors over 16×20=320 ns16 \times 20 = 320\ \text{ns} of simulated time. Output comparisons would be added to make this a self-checking testbench.


Verilog
`timescale 1ns/1ps
module tb;
  reg  [3:0] x;
  wire       y;
  integer    i;
  dut u0 (.x(x), .y(y));
  initial begin
    for (i = 0; i < 16; i = i + 1) begin
      x = i;
      #20;
    end
    $finish;
  end
endmodule


The count doubles with every added input bit. A 32-bit adder without carry-in has 64 input bits and therefore 264≈1.84×10192^{64} \approx 1.84 \times 10^{19} combinations; at 1 ns each, that is about 584.54 years of simulated time. Wide datapaths are verified with directed tests of corner cases plus random vectors instead.

Synthesis: from code to gates

Synthesis is the translation of HDL source into a netlist of gates, flip-flops or FPGA lookup tables. It accepts only a synthesizable subset of the language. Simulation timing controls and runtime diagnostics such as $display do not create hardware; synthesis tools ignore or reject them. Support for initial blocks depends on the tool and target. Many FPGA tools accept register start-up values and memory initialization using $readmemh or $readmemb. Real timing comes from the device and is checked by static timing analysis after place and route.


Two coding rules keep simulation and hardware in agreement:

  • In a clocked always @(posedge clk) block, use nonblocking assignments (<=). Right-hand sides are evaluated when the statements execute at the edge, with updates deferred so registers can use one another's old values. A blocking assignment (=) updates at once, so later statements in the same block see the new value.
  • In a combinational always @(*) block, assign every output on every path. An output left unassigned on some path must hold its old value, and synthesis infers a latch to store it.



What to remember

  • An HDL describes concurrent hardware; synthesizable descriptions support both simulation and synthesis.
  • Structural, dataflow and behavioral styles can describe the same circuit.
  • Exhaustive testing needs 2n2^n vectors and 2n Δt2^n\,\Delta t of simulated time, so it is practical only for narrow inputs.
  • Simulation timing controls and runtime diagnostics do not implement hardware; supported FPGA initialization constructs are an exception to treating initial blocks and system tasks as simulation-only.
  • Use <= in clocked blocks, and assign every output on every path in combinational blocks.

Example: Simulating a majority-vote module in Verilog

A safety system sounds an alarm when at least two sensors output 1, indicating an alarm condition. The designer models the three-input voter with a 2 ns gate delay and writes an exhaustive testbench. The input vector is v, with v[2] driving a, v[1] driving b and v[0] driving c.


Three bits of input vector v connect to the corresponding ports of the vote3 module, which drives output y.


Verilog
`timescale 1ns/1ps
module vote3 (input a, b, c, output y);
  assign #2 y = (a & b) | (a & c) | (b & c);
endmodule

module tb;
  reg  [2:0] v;
  wire       y;
  integer    i;
  vote3 dut (.a(v[2]), .b(v[1]), .c(v[0]), .y(y));
  initial begin
    for (i = 0; i < 8; i = i + 1) begin
      v = i;
      #10;
    end
    $finish;
  end
endmodule


a) How many input vectors does the testbench apply, and at what simulation time does the simulation stop?


The module has n=3n = 3 input bits, so an exhaustive test needs


N=23=8N = 2^{3} = 8


vectors, which matches the loop running for i=0i = 0 to 77. Vector ii is applied at t=10it = 10i ns, so the last vector (i=7i = 7) is applied at ________ ns and held for 10 ns. The loop then exits and the simulation stops at


Tsim=N Δt=8×10 ns=80 nsT_{\text{sim}} = N\,\Delta t = 8 \times 10\ \text{ns} = 80\ \text{ns}


The testbench applies 8 vectors and stops at 80 ns.


b) A colleague deletes integer i and rewrites the loop as for (v = 0; v < 8; v = v + 1) #10;. Explain why this simulation never stops.


The register v is declared [2:0], so it holds only ________ bits: values 0 to 7. When v is 7, the update computes 7+1=8=100027 + 1 = 8 = 1000_2. Assigning that to a 3-bit register keeps only the low three bits, 0002000_2, so v wraps to 0.


The loop condition v < 8 is therefore true for every value v can hold, and the loop never exits. Simulated time keeps advancing by 80 ns per pass through the eight vectors, and the code after the loop is never reached.


The fix is a loop counter wide enough to reach the exit value: an integer counter as in the original, or a [3:0] register that can hold 8.


c) For the original testbench, list every simulation time between 0 and 80 ns at which y changes, and give its new value.


First find the logical output for each vector, then allow for the 2 ns delay. The output is 1 when at least two of aa, bb, cc are 1.

  • At 0 ns, vector 000 is applied; its majority output is 0, valid by 2 ns.
  • At 10 ns, vector 001 is applied; its majority output is ________, valid by 12 ns.
  • At 20 ns, vector 010 is applied; its majority output is 0, valid by 22 ns.
  • At 30 ns, vector 011 is applied; its majority output is ________, valid by 32 ns.
  • At 40 ns, vector 100 is applied; its majority output is 0, valid by 42 ns.
  • At 50 ns, vector 101 is applied; its majority output is 1, valid by 52 ns.
  • At 60 ns, vector 110 is applied; its majority output is 1, valid by 62 ns.
  • At 70 ns, vector 111 is applied; its majority output is 1, valid by 72 ns.

At initialization, the inputs and continuous-assignment driver are unknown; y becomes 0 at 2 ns. A vector whose majority output matches the current output causes no transition. The complete list of changes is therefore:

  • 2 ns: x to 0.
  • 32 ns: 0 to 1.
  • 42 ns: 1 to 0.
  • 52 ns: 0 to 1.

After 52 ns, vectors 110 and 111 both give 1, so y stays at 1 until the simulation stops at 80 ns. Every input change is 10 ns apart, longer than the 2 ns delay, so no pending output change is cancelled.

A designer writes the following line in a Verilog module that will be synthesized for an FPGA:


Verilog
assign #3 y = a & b;


What does a typical FPGA synthesis tool do with the #3?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A team is organizing the files produced and used while developing a digital controller. Match each artifact to the description of its role.

Match each term
A.

A saved record of signal values against simulated time.

B.

A specification of required clock periods or interface timing used during implementation and analysis.

C.

A simulation environment that applies inputs and can check the resulting outputs.

D.

A structural description of circuit instances and the connections between them.

Netlist

Waveform trace

Timing constraint

Testbench

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A Verilog testbench uses a time unit of 1 ns. It first holds the design in reset for 15 ns. It then applies every possible combination of a combinational module's 5 input bits, one after another, holding each combination for 8 ns. The simulation ends immediately after the last combination's 8 ns interval.


At what simulation time does the simulation end? Give your answer in ns, to the nearest whole ns.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice