Skip to content

Digital Logic: Dataflow Modeling and FPGA Verification

Objectives

  • Master Verilog dataflow modeling using the continuous assignment statement assign.
  • Learn to write a simple testbench for simulation and verification.
  • Understand the difference between dataflow modeling and structural modeling with gate-level primitives.
  • Master FPGA pin assignment and the on-board verification flow.

1. Dataflow Modeling

1.1 What Is Dataflow Modeling?

Dataflow modeling describes the logical relationship between a circuit's inputs and outputs using continuous assignment statements (assign). It abstracts away the underlying gate-level structure and describes functionality directly with Boolean expressions or operators, making it suitable for combinational logic.

1.2 Continuous Assignment with assign

  • Syntax: assign wire_signal = expression;
  • Characteristics:
  • The assignment target must be of type wire, not reg.
  • Whenever any variable on the right-hand side changes, the left-hand side is updated immediately, modeling the continuous response of combinational logic.
  • Multiple assign statements execute concurrently, regardless of their order in the source file.

1.3 Common Operators

Operators Commonly Used in Dataflow Modeling

Operator category Symbols Example
Bitwise logic ~, &, |, ^ y = a & b;
Logical !, &&, || y = a && b;
Arithmetic +, -, *, / sum = a + b;
Relational >, <, >=, <= gt = a > b;
Equality ==, != eq = a == b;
Conditional (ternary) ? : y = sel ? a : b;
Concatenation {} {cout, sum} = a + b;

Bitwise Versus Logical Operators

  • Logical operators (!, &&, ||) always produce an effective output width of one bit (0 or 1). They are commonly used in conditions such as if and while.
  • Bitwise operators (~, &, |) operate independently on each bit. Their output width matches the operand width, so they are commonly used in combinational datapath logic.
a b a | b a || b a & b a && b ~a !a
2'b01 2'b10 2'b11 1'b1 2'b00 1'b1 2'b10 1'b0
2'b01 2'b11 2'b11 1'b1 2'b01 1'b1 2'b10 1'b0
2'b00 2'b01 2'b01 1'b1 2'b00 1'b0 2'b11 1'b1

1.4 Dataflow Example: Three-Input Majority Voter

Design a three-input majority voter whose output is 1 when at least two of its three inputs are 1.

79006421268

The logic expression is F = ab + ac + bc. The design is:

// 3-input majority voter (数据流描述)
module majority_3 (
    input  a, b, c,
    output out
);
    assign out = (a & b) | (a & c) | (b & c);
endmodule

Note the operator-precedence order: () > ~ > & > |.

For example, the expression F = a(b‘+c) is written in Verilog as:

assign F = a & (~b | c);

2. Writing a Testbench for Simulation

2.1 Basic Testbench Structure

A testbench is a simulation environment for testing a design module. It has no input or output ports, instantiates the design under test, and generates stimulus signals.

  • Module definition: A testbench is a top-level module with no input or output ports.
  • Signal declarations: Declare inputs to the design under test as type reg so that the testbench can generate stimuli. Declare outputs from the design under test as type wire so that the responses can be monitored.
  • Instantiate the design under test: Instantiate the module with a suitable instance name, such as DUT, and connect signals declared in the testbench to the corresponding DUT ports.
  • Generate stimuli: Generate test vectors in an initial or always block, often using loops and delays. This laboratory uses only initial.
  • Monitor results: Print output using system tasks such as $display and $monitor, or inspect the waveform.

2.2 Testbench Template

The following is a testbench template for the majority_3 module:

`timescale 1ns/1ps

module tb_majority_3();
    // 声明信号(与待测模块端口对应)
    reg a, b, c;
    wire out;

    // 实例化被测模块
    majority_3 uut (
        .a(a),
        .b(b),
        .c(c),
        .out(out)
    );

    // 产生激励
    initial begin
        // 在这里给输入赋值
    end

    // 监控输出(可选)
    initial begin
        $monitor(...);
    end
endmodule

2.3 Common Ways to Generate Stimuli

  • Direct assignment: Use # delays to change inputs sequentially.
initial begin
    a = 0; b = 0; c = 0; 
    #10;
    a = 0; b = 0; c = 1; 
    #10;
    a = 0; b = 1; c = 0; 
    #10;
  // ... 覆盖所有组合
    $finish;
end
  • Automatically enumerate values with a loop (appropriate for a small number of variables): Verilog provides several loop statements. Three common forms are shown below.

1. for loop (most common)

integer i;
initial begin
    for (i = 0; i < 8; i = i + 1) begin
        {a, b, c} = i;  // 拼接赋值,自动遍历 000~111
        #10;
    end
    $finish;
end

2. while loop

integer i;
initial begin
    i = 0;
    while (i < 8) begin
        {a, b, c} = i;
        #10;
        i = i + 1;
    end
    $finish;
end

3. repeat loop (a fixed number of iterations)

integer i;
initial begin
    i = 0;
    repeat(8) begin
        {a, b, c} = i;
        #10;
        i = i + 1;
    end
    $finish;
end

Note: repeat executes a fixed number of times and is usually used when no loop variable is needed. If counting is required, an additional variable is still necessary.

2.4 Common System Tasks (Simulation Only)

  • $display("format string", variable1, variable2, ...);
    Prints information during simulation, similarly to C's printf. It prints once each time it executes.
  • $monitor("format string", variable1, variable2, ...);
    Continuously monitors variables and prints a line whenever any of them changes. It is usually placed in a separate initial block.
  • $finish;
    Ends the simulation, preventing an excessively long default simulation from making the waveform difficult to inspect.

Example:

initial begin
    $monitor("time=%t a=%b b=%b c=%b out=%b", $time, a, b, c, out);
end

2.5 Complete Testbench Example for majority_3

`timescale 1ns/1ps

module tb_majority_3();
    reg a, b, c;
    wire out;
    integer i;

    // 实例化被测模块
    majority_3 uut (
        .a(a),
        .b(b),
        .c(c),
        .out(out)
    );

    initial begin
        $monitor("time=%3d: a=%b b=%b c=%b => out=%b", $time, a, b, c, out);
        for (i = 0; i < 8; i = i + 1) begin
            {a, b, c} = i;
            #10;
        end
        $finish;
    end
endmodule

3. Dataflow Modeling Versus Structural Modeling```verilog

3.1 Review: Structural Modeling with Primitive Gates

Verilog provides built-in basic gate primitives such as and, or, and xor. Instantiating and connecting these gates is called structural modeling.

Example: Implementing the majority voter with gate primitives

module majority_3_gate (
    input a, b, c,
    output out
);
    wire w1, w2, w3;
    and (w1, a, b);
    and (w2, a, c);
    and (w3, b, c);
    or  (out, w1, w2, w3);
endmodule

3.2 Comparing Coding Styles

The following table compares the two styles using the majority voter as an example:

Dataflow modeling Structural modeling with primitives
Direct expression Basic gate instantiation
verilog
assign out = (a & b) | (a & c) | (b & c);
verilog
wire w1, w2, w3;and (w1, a, b);
and (w2, a, c);
and (w3, b, c);
or (out, w1, w2, w3);
Intermediate signals in steps (same result) Gate order may be changed (same result)
verilog
wire and_ab, and_ac, and_bc;
assign and_ab = a & b;
assign and_ac = a & c;
assign and_bc = b & c;
assign out = and_ab | and_ac | and_bc;
verilog
wire w1, w2, w3;
or (out, w1, w2, w3);
and (w1, a, b);
and (w2, a, c);
and (w3, b, c);

3.3 Comparison

Characteristic Dataflow modeling (assign) Structural modeling (gate primitives)
Abstraction level Higher; close to a logic expression Lower; close to transistor/gate-level connections
Conciseness Concise; often a single expression Verbose; requires several gate instances
Readability Good; directly expresses the function Lower; requires interpreting gate-level connections
Design efficiency High; suitable for complex combinational logic Low; suitable only for simple or instructional examples
Synthesis result Optimized by the synthesis tool Closely follows specified gates; controllable but inefficient
Typical use Combinational logic in practical designs Instructional demonstrations and special custom gate structures

Conclusion: Practical designs use dataflow or behavioral modeling almost exclusively; behavioral modeling is introduced later. Gate-level primitives are mainly used for instruction or specialized custom requirements.


4. Synthesis

4.1 What Is Synthesis?

Synthesis converts hardware description language (HDL) code into a concrete logic circuit, such as a gate-level netlist. Vivado's synthesis tool analyzes Verilog code such as assign statements, infers the corresponding logic gates and connections, and produces a netlist mapped to FPGA resources such as lookup tables (LUTs) and flip-flops (FFs).

Purposes of synthesis:

  • Verify that the code is accepted by the synthesis tool and can produce actual hardware.
  • Inspect the synthesized schematic to understand the hardware corresponding to the code.
  • Analyze resource utilization and estimated timing, which are beyond the scope of this course at present.

4.2 How Do I View the Synthesized Schematic?

  1. Click Run Synthesis.
  2. After synthesis completes successfully, click Open Synthesized Design.
  3. In the Schematic view, inspect the circuit made up of logic gates, LUTs, and flip-flops.
  4. Zoom in to inspect how input signals pass through logic elements to produce outputs.
  5. Combinational logic is usually implemented with LUTs (lookup tables).

image-20260308105212858

How Do I View a Module's Truth Table or Logic Expression?

  • Click a LUT cell in the schematic, usually labeled LUT2, LUT3, and so on. Its LUT equation appears in the properties window. The LUT equation is the logic expression inferred by Vivado during synthesis. It may differ from the original assign statement but will be functionally equivalent.

image-20260308104706741

By examining the schematic and LUT properties, you can verify that the synthesis result matches your expectations and develop a deeper understanding of how Verilog code maps onto FPGA hardware.

image-20260308104441503

Note: The synthesized schematic represents logic mapping, not the final placement and routing. The circuit after placement and routing is more complex, but its logical function remains unchanged.

5. Pin Assignment and On-Board Implementation in Vivado

Note: Run synthesis before assigning pins.

4.1 Writing Pin Constraints

Method 1: Use the Vivado GUI (I/O Planning)

  1. Click Run Synthesis.
  2. After synthesis completes, click Open Synthesized Design.
  3. Click I/O Planning in Vivado's top toolbar, or select Layout → I/O Planning.
  4. The I/O Ports window lists all top-level ports.
  5. In the Package Pin column, double-click the cell for a port and enter its physical pin identifier.
  6. In the I/O Std column, select LVCMOS33, according to the board voltage.
  7. Save the constraints with File → Save Constraints. Select or create an XDC file to store the current assignments.
  8. You can also generate the constraint file by clicking Write XDC in the toolbar.

Tip: Consult the development board schematic or official documentation for pin identifiers. Documentation for the EGO1 board is available under References on the course Blackboard site. If a pin identifier is unavailable after you enter it, confirm that the correct FPGA part was selected when the project was created.

image-20260308103938122

Method 2: Write the XDC File Manually (Review)

Add a new constraint file, xxx.xdc, to the project, then enter and edit the following content. The order of the constraints does not affect their behavior.

Example XDC file: Replace each question mark with the correct pin for the board.

## 输入引脚 (拨码开关) 
set_property PACKAGE_PIN ?? [get_ports {a}]
set_property IOSTANDARD LVCMOS33 [get_ports {a}]

set_property PACKAGE_PIN ?? [get_ports {b}]
set_property IOSTANDARD LVCMOS33 [get_ports {b}]

set_property PACKAGE_PIN ?? [get_ports {c}]
set_property IOSTANDARD LVCMOS33 [get_ports {c}]

## 输出引脚 (LED)
set_property PACKAGE_PIN ?? [get_ports {out}]
set_property IOSTANDARD LVCMOS33 [get_ports {out}]

Reference: Consult the EGO1 board manual or the pin labels printed next to the peripherals. If a pin identifier is not listed in the graphical interface, confirm that the project uses the correct FPGA part.

image-20260609120819104

image-20260609120748493

4.2 Implementation and Bitstream Generation

  • Click Run Implementation to perform placement and routing. After implementation completes, you can inspect the timing report.
  • Click Generate Bitstream to generate the .bit file. Vivado displays a completion dialog when generation succeeds.
  • You may also click Generate Bitstream directly. Vivado then runs Synthesis, Implementation, and Generate Bitstream automatically without requiring intervention between stages.

4.3 Programming the FPGA Development Board

  1. Connect and power on the board, ensuring that the programming cable, such as USB-JTAG, is connected.
  2. Click Open Hardware Manager → Auto Connect. Vivado automatically detects the board.
  3. Select Program Device in the Hardware window.
  4. Confirm that the bitstream path in the dialog is correct, then click Program.
  5. After programming completes, toggle the switches and verify that the LED follows the majority-voter logic.

5. Exercises

Exercise: Implement a Full Adder with Dataflow Modeling (assign)

  • Module name: FullAdder

  • Inputs: a, b, cin

  • Outputs: s, cout

  • Function: As shown in the circuit diagram.

  • Requirement: Use assign statements and operators.

Full Adder Circuit – How it Works

Step 1: Write the Design

  • Derive the expressions for cout and s.
  • Implement the design with assign statements.

Step 2: Write and Verify the Testbench

  • Draw the circuit's truth table on paper.
  • Compare it with the simulation result. You may either compare the log produced by $monitor or inspect the waveform directly.

Step 3: Synthesize

  • Click Run Synthesis in Vivado and wait for synthesis to complete. If an error occurs, correct the code according to the diagnostic message.

Step 4: Assign Pins

  • Add pin constraints to the Exercise 1 design. For example, connect a/b/cin to three slide switches and s/cout to LEDs.
  • Create the constraints either by editing an XDC file or by using the I/O Planning graphical interface.

Step 5: Generate the Bitstream and Verify on the Board

  • Click Generate Bitstream, program the development board with the resulting bitstream, and verify the design's function.

Appendix: Frequently Asked Questions

  • Q: Can the left-hand side of an assign statement be type reg?
    A: No. It must be wire or a related net type such as tri.
  • Q: Why is the simulation waveform a straight line?
    A: Check whether the testbench contains $finish or whether the simulation interval is too long. Use Zoom Fit and Zoom In to display a suitable portion of the waveform.
  • Q: Why is the LED off or unresponsive after the board is programmed?
    A: Check the pin constraints, confirm that programming succeeded, and verify that the development board is powered on.
  • Q: Why can I not find the ports in I/O Planning?
    A: You must run Synthesis and open the synthesized design before the list of I/O ports is available.