Verilog Tutorial Ando KI Spring 2009
Copyright © by Ando KiVerilog tutorial ( 2 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example Example design Simulation Synthesis Gate-level simulation Appendix: Run adder with GUI
Copyright © by Ando KiVerilog tutorial ( 3 ) Standard FPGA-based design flow
Copyright © by Ando KiVerilog tutorial ( 4 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 5 ) Hello world Try a simple example to display text on the screen. GUI based Step 1: coding using text editor, e.g., vi or vim. Step 2: compilation Step 3: simulation Command based Step 1: coding using text editor, e.g., vi or vim. Step 2: compilation Step 3: simulation
Copyright © by Ando KiVerilog tutorial ( 6 ) Hello world: coding Make a directory say ex_hello Go to the directory Create a Verilog file hello.v module top; initial $display(Hello world!); endmodule Initial construct is executed at the start of simulation. initial_construct ::= initial statement Display task puts information on the terminal. display_task ::= $display ( list_of_arguments ); Top-level module contains whole design, which does not have port. module and endmodule specifies a block. DIY
Copyright © by Ando KiVerilog tutorial ( 7 ) Create a new project Invoke ModelSim File New Project Specify Project Name and Project Location DIY
Copyright © by Ando KiVerilog tutorial ( 8 ) Add existing file Add the Verilog design file DIY
Copyright © by Ando KiVerilog tutorial ( 9 ) Compile DIY
Copyright © by Ando KiVerilog tutorial ( 10 ) Compile DIY
Copyright © by Ando KiVerilog tutorial ( 11 ) Compile DIY
Copyright © by Ando KiVerilog tutorial ( 12 ) Simulation DIY
Copyright © by Ando KiVerilog tutorial ( 13 ) Quit There should be hello.mpf, which is ModelSim project file. DIY
Copyright © by Ando KiVerilog tutorial ( 14 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 15 ) Command based simulation vlib specifies where compiled library locates. vlog compiles Verilog code. vsim simulate the design with a given command. You can use any script language such as MS-DOS, shell and so on. vlib work vlog hello.v vsim -c -do "run -all; quit" work.top The vlib command creates a design library. vlib [options] The vlog command compiles Verilog source code into a specified working library (or to the work library by default). vlog [options] The vsim command is used to invoke the VSIM simulator. vsim [-c] [-do ].
Copyright © by Ando KiVerilog tutorial ( 16 ) Command based simulation result DIY
Copyright © by Ando KiVerilog tutorial ( 17 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 18 ) Module declaration Module is the basic design unit. Module must be declared. prepare Module can be instantiated. use A module definition shall be enclosed between the keywords module and endmodule. Module syntax module module_name ( list of ports ); // in, out, inout port declarations // signal/wire/reg declarations // data variable declarations // sub-module instantiation and connection // functional blocks: initial, always, function, task endmodule module ModF (A, B, C); input A; inout [7:0] B; output [7:0] C; // declarations // description of f endmodule ModF f f A B C
Copyright © by Ando KiVerilog tutorial ( 19 ) Module instantiation module ModG (AA, BB, CC); input AA; inout [7:0] BB; output [7:0] CC; wire a; wire [7:0] b; // description of g ModG Umodg (.A(a),.B(b),.C(CC)); endmodule ModG g g AA BB CC f f a b ModF f f A B C instantiation Port connection Instance name Module name
Copyright © by Ando KiVerilog tutorial ( 20 ) Connection through port Port connection rules Input port must be net (i.e. wire). Output port can be either net or reg. Inout port must be net. input output inout reg or net net reg or net
Copyright © by Ando KiVerilog tutorial ( 21 ) Test-bench A test-bench is a layer of code that is created to apply input patterns (stimulus) to the DUT (design under test) and to determine whether the DUT produces the outputs expected. A test-vector is a set of values for all the expected input ports (stimuli) and expected values for the output ports of a module under test. A test-bench that is created to apply inputs, sample the outputs of the DUT, and compare the outputs with the expected (golden) results is called a self- checking test-bench.
Copyright © by Ando KiVerilog tutorial ( 22 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example What is adder Directory structure Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 23 ) What is adder An adder is a digital circuit that performs addition of numbers. The half adder adds two one-bit binary numbers (A and B). The output is the sum of the two bits (S) and the carry (C). The full-adder circuit adds three one-bit binary numbers (C, A and B) and outputs two one-bit binary numbers, a sum (S) and a carry (C). Multi-bit adder Ripple carry adder Carry look-ahead adders Picture has been adopted from Wikipedia (
Copyright © by Ando KiVerilog tutorial ( 24 ) Directory structure adder design sim syn Verilog source code folder HDL simulation folder Synthesis folder Directory structure sim.gate Gate-level simulation folder incite5000 iNSPIRE folder for specific FPGA board sim.fpga FPGA-based simulation folder (emulation)
Copyright © by Ando KiVerilog tutorial ( 25 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example What is adder Directory structure Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 26 ) Example design See the design directory top.v full_adder.v half_adder_gate.v half_adder_rtl.v stimulus.v full_adder_ref.v checker.v
Copyright © by Ando KiVerilog tutorial ( 27 ) Full adder (1/3) module full_adder(sum,cout,in1,in2,cin,clk,resetb); output sum, cout; input in1, in2, cin; input clk, resetb; wire sum, cout; reg rin1, rin2, rcin; wire s1, c1, s2, c2; (posedge clk or negedge resetb) begin if (resetb==1'b0) begin rin1 <= 1'b0; rin2 <= 1'b0; rcin <= 1'b0; end else begin rin1 <= in1; rin2 <= in2; rcin <= cin; end half_adder_gate ha1 (.S(s1),.C(c1),.A(rin1),.B(rin2)); half_adder_rtl ha2 (.S(s2),.C(c2),.A(s1),.B(rcin)); assign sum = s2; assign cout = c1|c2; endmodule
Copyright © by Ando KiVerilog tutorial ( 28 ) Full adder (2/3) module full_adder(sum,cout,in1,in2,cin,clk,resetb); output sum, cout; input in1, in2, cin; input clk, resetb; wire sum, cout; reg rin1, rin2, rcin; wire s1, c1; wire s2, c2; (posedge clk or negedge resetb) begin if (resetb==1'b0) begin rin1 <= 1'b0; rin2 <= 1'b0; rcin <= 1'b0; end else begin rin1 <= in1; rin2 <= in2; rcin <= cin; end half_adder_gate ha1 (.S(s1),.C(c1),.A(rin1),.B(rin2)); half_adder_rtl ha2 (.S(s2),.C(c2),.A(s1),.B(rcin)); assign sum = s2; assign cout = c1|c2; endmodule module name ports port directions internal signals sub module instantiation internal design description
Copyright © by Ando KiVerilog tutorial ( 29 ) Full adder (3/3) module full_adder(sum,cout,in1,in2,cin,clk,resetb); output sum, cout; input in1, in2, cin; input clk, resetb; wire sum, cout; reg rin1, rin2, rcin; wire s1, c1; wire s2, c2; (posedge clk or negedge resetb) begin if (resetb==1'b0) begin rin1 <= 1'b0; rin2 <= 1'b0; rcin <= 1'b0; end else begin rin1 <= in1; rin2 <= in2; rcin <= cin; end half_adder_gate ha1 (.S(s1),.C(c1),.A(rin1),.B(rin2)); half_adder_rtl ha2 (.S(s2),.C(c2),.A(s1),.B(rcin)); assign sum = s2; assign cout = c1|c2; endmodule Verilog data types: net and variable net: wire variable: reg, integer, real... Sub-module instantiation with named port connection; refer to positional pot connection. Continuous assignment places values to nets whenever the value of the right-hand side changes. This always block runs when the following condition occurs. positive edge of clk negative edge of resetb Procedural assignment places values to variables within always, initial and so on. ==: equality check operator <=: non-blocking assignment operator |: bit-wize OR operator
Copyright © by Ando KiVerilog tutorial ( 30 ) Half adder structural model (gate level) module half_adder_gate (S, C, A, B); output S, C; input A, B; and UAND (C, A, B); xor UXOR (S, A, B); endmodule A B S C Structural model: instantiation of primitives and modules.
Copyright © by Ando KiVerilog tutorial ( 31 ) Half adder data-flow model (RTL) module half_adder_rtl (S, C, A, B); output S, C; input A, B; wire S, C; assign C = A & B; assign S = A ^ B; endmodule A B S C Data-flow model: continuous assignments.
Copyright © by Ando KiVerilog tutorial ( 32 ) Test-bench: stimulus module stimulus(out1,out2,out3,clk,resetb); output out1,out2,out3; input clk,resetb; reg out1,out2,out3; initial begin out1 <=0; out2 <=0; out3 <=0; wait (resetb==1'b0); wait (posedge clk); out1=1; out2=0; (posedge clk); out1=0; out2=1; (posedge clk); out1=1; out2=1; (posedge clk); out1=0; out2=0; (posedge clk); out1=1; out2=0; (posedge clk); out1=0; out2=1; (posedge clk); out1=1; out2=1; (posedge clk); repeat (posedge clk); $finish; end endmodule <=: non-blocking assignment operator Initial construct is executed at the start of simulation. =: blocking assignment operator repeat: Executes a statement a fixed number of times. wait: It blocks until the condition becomes waits until the specified event occurs. The finish system task simply makes the simulator exit and pass control back to the host computer operating system.
Copyright © by Ando KiVerilog tutorial ( 33 ) Test-bench: checker module checker(in1,in2,cin,sum,cout,sumr,coutr,clk,resetb); input in1,in2,cin,sum,cout,sumr,coutr,clk,resetb; (clk) begin if ({cout,sum}=={coutr,sumr}) $display($time,,"correct"); else $display($time,,"error result=%b expect=%b", {cout, sum}, {coutr,sumr}); end endmodule Time system function returns an integer that is a 64-bit time, scaled to the timescale unit of the module that invoked it. time_function ::= $time ; Display task puts information on the terminal. display_task ::= $display ( list_of_arguments );
Copyright © by Ando KiVerilog tutorial ( 34 ) Test-bench: full_adder_ref module full_adder_ref(sum,cout,in1,in2,cin,clk,resetb); output sum, cout; input in1, in2, cin; input clk, resetb; wire sum, cout; reg rin1, rin2, rcin; wire s1, c1; wire s2, c2; (posedge clk or negedge resetb) begin if (resetb==1'b0) begin rin1 <= 1'b0; rin2 <= 1'b0; rcin <= 1'b0; end else begin rin1 <= in1; rin2 <= in2; rcin <= cin; end assign {cout, sum} = rin1+rin2+rcin; endmodule assign: Continuous assignment places values to nets whenever the value of the right-hand side changes. Concatenation operator ({, }) combines two or more in order to form wider bits.
Copyright © by Ando KiVerilog tutorial ( 35 ) Test-bench: top module top; wire sum, cout, in1, in2, cin; reg clk; full_adder fa (.sum(sum),.cout(cout),.in1(in1),.in2(in2),.cin(cin)); stimulus st (.out1(in1),.out2(in2),.out3(cin),.clk(clk)); checker ck (.in1(in1),.in2(in2),.cin(cin),.sum(sum),.cout(cout),.clk(clk)); initial begin clk = 0; forever #5 clk = ~clk; end initial begin $dumpfile("wave.vcd"); $dumpvars(1) end endmodule Dumpfile task specify the name of the VCD file. dumpfile_task ::= $dumpfile( filename ) ; Dumpvars task puts information on the terminal. dumpvars_task ::= $dumpvars ( level, [ list_of_mod_or_var ); Initial construct is executed at the start of simulation. initial_construct ::= initial statement Forever statement continuously executes a statemet. forever_statement ::= forever statement Top-level module contains whole design, which does not have port.
Copyright © by Ando KiVerilog tutorial ( 36 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example What is adder Directory structure Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 37 ) Command based simulation See sim directory vlib work vlog../design/top.v vlog../design/full_adder.v vlog../design/half_adder_gate.v vlog../design/half_adder_rtl.v vlog../design/stimulus.v vlog../design/full_adder_ref.v vlog../design/checker.v vsim -c -do "run -all; quit" work.top DIY
Copyright © by Ando KiVerilog tutorial ( 38 ) Simulation result with VCD Run GTKwave to see wave form DIY
Copyright © by Ando KiVerilog tutorial ( 39 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example What is adder Directory structure Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 40 ) Synthesis with command-line See the syn directory set top=full_adder xst -ifn xst_option.xst -ofn %top%.log ngc2edif -bd angle -w %top%.ngc %top%.edif netgen –w –ofmt verilog %top%.ngc %top%_gate.v Windows batch command file RunMe.bat set -tmpdir. run -ifmt MIXED -top $top -p $device -ifn $design -vlgincdir $inc_dir -ofn $top -ofmt NGC …… -iobuf NO -fsm_encoding User …… -iob auto xst_option.xst verilog xst../design/full_adder..v verilog xst../design/half_adder_gate.v verilog xst../design/half_adder_rtl.v xst_list.xst Important notice: iNCITE does not need IO pads. FSM encoding can be changed without this option Generate gate-level design DIY
Copyright © by Ando KiVerilog tutorial ( 41 ) Contents Design flow overview Hello world GUI based Command based Module Declaration Instantiation Port Test-bench Adder example What is adder Directory structure Example design Simulation Synthesis Gate-level simulation
Copyright © by Ando KiVerilog tutorial ( 42 ) Command based gate-level simulation See sim.gate directory vlib work vlog../design/top.v vlog../design/stimulus.v vlog../design/checker.v vlog../syn/full_adder_gate.v^ +libext+.v^ -y %XILINX%/verilog/src/simprims^ -y %XILINX%/verilog/src/unisims vsim -c -do "run -all; quit" work.top work.glbl Generated gate-level design while logic synthesis Gate-level library for Xilinx DIY
Copyright © by Ando KiVerilog tutorial ( 43 ) Invoking ModelSim from start menu
Copyright © by Ando KiVerilog tutorial ( 44 ) File->New->Project
Copyright © by Ando KiVerilog tutorial ( 45 ) Specify project name and location
Copyright © by Ando KiVerilog tutorial ( 46 ) File->Add to Project->Existing File
Copyright © by Ando KiVerilog tutorial ( 47 ) Add files
Copyright © by Ando KiVerilog tutorial ( 48 ) After adding files
Copyright © by Ando KiVerilog tutorial ( 49 ) Compile->Compile All
Copyright © by Ando KiVerilog tutorial ( 50 ) After compilation
Copyright © by Ando KiVerilog tutorial ( 51 ) View->Debug Windows->Wave
Copyright © by Ando KiVerilog tutorial ( 52 ) After adding wave window
Copyright © by Ando KiVerilog tutorial ( 53 ) Select Library tab
Copyright © by Ando KiVerilog tutorial ( 54 ) Run simulation with top-level
Copyright © by Ando KiVerilog tutorial ( 55 ) After simulation
Copyright © by Ando KiVerilog tutorial ( 56 ) Selecting signals to be view
Copyright © by Ando KiVerilog tutorial ( 57 ) After selection
Copyright © by Ando KiVerilog tutorial ( 58 ) Run-All
Copyright © by Ando KiVerilog tutorial ( 59 ) Home work AccumulatorMAC (multiply-accumulate) a a + b * c