Digital Logic: Verilog Basics and Structural Modeling
Objectives
- Master the basic syntax of Verilog HDL, including identifiers, number representations, and keywords.
- Master the structure of a Verilog module and its port declarations.
- Understand when to use the basic data types
wireandreg. - Master structural modeling using gate-level primitives.
- Become familiar with the basic structure of a testbench and run simulations with simple stimuli.
1. Verilog Syntax Fundamentals
1.1 Overview
- Verilog is case-sensitive.
- Its basic syntax is derived from the C programming language.
1.2 Comments and Punctuation
- Single-line comments: Use
//. - Multi-line comments: Use
/* ... */. - Separators and terminators: Separate list elements with commas
,and terminate statements with semicolons;.
1.3 Identifiers
- Identifiers name objects such as registers, functions, and modules so that they can be referenced in code.
- An identifier must begin with a letter or underscore (
a-zA-Z_). - Subsequent characters may include letters, digits, underscores, and dollar signs (
a-zA-Z0-9_$). - An identifier may be up to 1024 characters long.
- Scalar examples:
A,CO,my_clk,a_1. - Vector examples:
sel[0:2],f0[0:5],ACC[31:0].
1.4 Number Representations
- Syntax:
<size>'<radix><value>. - Radix:
d(decimal),b(binary),o(octal), andh(hexadecimal). - Binary value states:
0,1,X/x(unknown), andZ/z(high-impedance/open-circuit state). - Examples:
6'hCA= 6‘b0010106'hA= 6'b0010106'b1= 6'b0000014'b0101= 4'b01018'bxxxx= 8'b0000xxxx'hF= 32'b00000000000000000000000000001111 (no size is specified, so the default is 32 bits)0= 32'b00000000000000000000000000000000 (neither size nor radix is specified, so the default is a 32-bit decimal value)
1.5 Keywords
- Keywords are reserved identifiers used to define language constructs and are always lowercase.
- Common keywords include
module,endmodule,input,output,wire,tri,reg,assign,initial,always,begin, andend. - Gate-level primitive keywords include
and,nand,or,nor,xor,xnor,not, andbuf.
2. Modules and Data Types
2.1 Modules and Ports
- Modules are the fundamental building blocks of a Verilog design.
- A design hierarchy can be created by instantiating modules within other modules.
- The module name should match the file name.
- Ports allow a module to communicate with its external environment. Except for the top-level module, every module in a hierarchy has ports.
// 方式一
module myCircuit (sw, led);
input [23:0] sw;
output [23:0] led;
assign led = sw;
endmodule
// 方式二 (可维护性更高)
module myCircuit (
input [23:0] sw,
output [23:0] led
);
assign led = sw;
endmodule
2.2 Data Types (wire and reg)
Verilog has two main categories of data types:
- Nets: Represent structural connections between components.
wire: A standard interconnect with no special resolution behavior; its default value is 'X'.tri: A net that is pulled up or down when it is not driven.- Registers: Represent variables used to store data.
reg: A register retains the last value assigned to it until another assignment changes that value; its default value is 'Z'.
3. Structural Modeling with Gate-Level Primitives
Gate-level primitives are basic logic gates built into the Verilog language.
- Basic gate primitives:
buf,not,and,or,nand,nor,xor, andxnor. - Syntax:
gate_operator instance_identifier (output, input_1, input_2, ...). When modeling with primitives, the output is always the first entry in the port list. For example:
and A1 (F, A, B); // 逻辑等价于: F = AB
or O1 (w, a, b, c); // 逻辑等价于: w = a+b+c
or O2 (x, b, c, d, e); // 逻辑等价于: x = b+c+d+e
- Module design example:
// 示例 F = AB + C
module test(
input A, input B, input C, output F
);
wire and1_or1;
and and1(and1_or1, A, B);
or or1(F, and1_or1, C);
endmodule
4. Simulation with a Testbench
4.1 Testbench Fundamentals
- A testbench is an HDL description that supplies stimuli to the circuit under test.
- It is a virtual platform used to simulate and verify inputs and outputs before the circuit is operated in a physical environment.
- Inputs supplied to the circuit under test are declared with the
regkeyword, while signals receiving outputs from the circuit under test are declared with thewirekeyword. - The module under test must be instantiated (for example, with the instance name
circuit1, ordut. Every module instance must have a unique instance name.
module my_hw_tb();
reg [23:0] sw_sim = 24'h00_0000;
wire [23:0] led_sim;
// 实例化单元,完成连接
my_circuit circuit1(.sw(sw_sim), .led(led_sim));
// 在此处添加激励信号...
endmodule
4.2 Delays
- Timescale syntax:
`timescale 1ns/1ps. - The first value specifies the unit used to measure delays (1 ns). The second specifies the precision (values are rounded to 0.001 ns, or 1 ps).
- If no timescale is specified, the simulator may display unitless values or use a tool-specific default time unit, commonly 1 ns.
- Propagation delay: Use
#; for example, under a1ns/1pstimescale,#10represents a delay of 10 ns. - Note that this delay is a simulated delay used only by the simulator.
4.3 Generating Stimuli
initialblock: Executes once at the beginning of a simulation and is therefore particularly useful in a testbench. If severalinitialblocks are present, they all execute concurrently when the simulation begins.
initial begin
A = 1'b0; B = 1'b0;
#10 A = 1'b1;
#20 A = 1'b0; B = 1'b1;
end

alwaysblock: Unlike aninitialblock, analwaysblock repeats indefinitely.
always begin
#10 B = ~B;
end

5. Design Flow and RTL Analysis
5.1 Review of the Complete Design Flow
The standard flow for FPGA verification in Vivado is:
- RTL Design: Write the Verilog code.
- Simulation: Write a simulation file and verify the logic function.
- Synthesis: Run synthesis.
- IO Constraint: Assign hardware pins.
- Implementation: Run implementation.
- Generate Bitstream: Generate a bitstream and verify the design on the board.
This laboratory session practices Steps 1 and 2.
5.2 RTL Analysis
- RTL analysis generates a pre-synthesis schematic that you can compare with your gate-level design.
- Procedure: Under
RTL ANALYSISin the Flow Navigator, expandOpen Elaborated Design, then clickSchematic. - This step is optional, but it can help you verify that the design matches your expectations.

6. Laboratory Exercises
Exercise 1: Gate-Level Design and Simulation
-
Create a Vivado project named
Project_2. -
Create a Verilog design file and save it as
structural_hw.v. -
Use Verilog gate-level primitives and a structural design to implement the following Boolean function:
A=x'y'z+x'yz+xy'

-
Create a simulation file and save it as
structural_hw_tb.v. Instantiate the top-level module (dut), add stimuli, run the simulation, and compare the resulting waveform with the truth table. -
Run RTL Analysis.
Reference design code:
module structural_hw (
input x,
input y,
input z,
output out
);
wire notx, noty, notz;
wire out1, out2, out3;
not nx(notx, x);
// Complete your code
endmodule
Reference simulation code:
module structural_hw_tb();
reg tb_x;
reg tb_y;
reg tb_z;
wire tb_out;
structural_hw dut (
// Complete your code
);
// create stimulis
// Complete your code
endmodule
Reference waveform:

Truth table:
| x | y | z | A |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 |
Exercise 2 (Optional): Logic Optimization and Comparison
- Modify your design to implement another Boolean function:
B=xy'+x'z
-
Inspect the simulation waveform and RTL schematic. Does this circuit have the same function as the previous circuit?
-
Compare the hardware complexity of the two designs. Which implementation is more economical?