[Online Simulation] Use HDLBits for online comprehensive simulation of FPGA code and generation of timing diagrams

This article describes the use of HDLBits for online comprehensive simulation of FPGA code and generation of timing diagrams to verify your own design.
After studying this tutorial, you can easily build your own simulation by checking the “Summary of necessary simulation elements” each time!

Article directory

  • Introduction to HDLBits
  • Online simulation rule learning
    • Official template
    • :key: Summary of necessary elements for simulation
    • Web interface usage operations
  • Example demonstration to verify your own design
    • RTL writing
    • Testbench writing
    • Summarized into a .v file
    • Simulation results
    • Timing diagram export
    • Compilation error modification
  • Separate top_module simulation
    • Write top_module module
    • Compilation information and timing diagram viewing

Introduction to HDLBits

HDLBits is best known as a web version of Verilog code editing simulation verification platform. This platform is a foreign open source FPGA learning website. Enter the web page through the address “https://hdlbits.01xz.net/wiki/Main_Page” , on this web page, you can write and synthesize Verilog code, and finally can simulate waveforms to verify the correctness of the design code. The verification platform is based on Icarus Verilog (referred to as iVerilog, a well-known open source HDL simulation tool, which also has a corresponding installation version), allowing you to enjoy the fun of Verilog programming simulation anytime and anywhere just by logging in to the web page!

It is a website that is very suitable for Verilog beginners to get started quickly. It includes exercises on Verilog syntax, combinational logic, sequential logic, simulation, etc. Your code in the web page will be synthesized into a hardware circuit through the Altera Quartus synthesizer. Your integrated circuit is checked for correct functionality through functional simulation. HDLBits uses ModelSim to simultaneously simulate your code and a reference solution, then compare the output of the two.

More comprehensively, it also contains tools to help you learn the basics of computer design, including HDLBits, ASMBits, and CPUlator. Enter the web page through the address ”https://www.01xz.net/wiki/Main_Page”.

HDLBits: Problem sets and online judgments for practicing digital circuit design in Verilog;

ASMBits: Just like HDLBits, but for practicing Nios II or ARMv7 assembly language;

CPUlator: In-browser full-system MIPS, Nios II and ARMv7 emulator and debugger.

Online simulation rule learning

In addition to being used for Verilog practice, HDLBits can also be used for online comprehensive simulation of FPGA code and timing diagram generation to verify your own design, allowing you to run any simulation you want.

Click “Run a Simulation (lcarus Verilog)” under Simulation in the upper left corner of the web page.

Official template

First, we use the official template to learn how to use the simulation function.

The official template code is as follows:

module top_module ();
reg clk=0;
always #5 clk = ~clk; // Create clock with period=10
initial `probe_start; // Start the timing diagram

`probe(clk); // Probe signal "clk"

// A testbench
reg in=0;
initial begin
#10 in <= 1;
#10 in <= 0;
#20 in <= 1;
#20 in <= 0;
$display ("Hello world! The current time is (\r ps)", $time);
#50 $finish; // Quit the simulation
end

invert inst1 ( .in(in) ); // Sub-modules work too.

endmodule

module invert(input in, output out);
    assign out = ~in;

    `probe(in); // Sub-modules can also have `probe()
    `probe(out);
endmodule

Summary of necessary elements for simulation

Based on the official template, the elements required for simulation are summarized.

First of all, to simulate FPGA logic, you must have two files. One is the RTL code file, which is used to comprehensively generate the hardware circuit part; the second is the Testbench file, which is the simulation file used to verify the function of the RTL code. The two lack Not even one. Here you need to write both RTL code and Testbench in an edit box.

Secondly, “initial probe_start” on line 4, “probe(clk)” on line 6, and “probe(in)” on line 26 are an officially defined “macro”, that is, called through this “macro” For the function of “probe” probe, we don’t need to worry about how this “macro” is defined, we only need to be able to call it. Note that this is not a syntax in Verilog, it is only used for waveform drawing in web page simulation.

Therefore, the necessary elements when writing code in the code editing area are as follows:

Elements Code location
RTL code and Testbench are written in In an edit box, the order of the two is arbitrary There are two modules in line 1 and line 23, and they end with endmodule respectively
You can also< strong>Use a separate top_module for simulation The section “Single top_module simulation” at the end of this article is explained
The simulation file name must be “top_module” Line 1
initial `probe_start; Line 4, for Start the timing diagram
`probe(XX); Lines 6, 26, and 27, the signals in brackets indicate the signals that need to be drawn
#50 $finish; In line 16, be sure to set the simulation stop time. If the simulation end time is too long, you will be prompted

The following adds the usage and limitations of timing diagram drawing: each signal can only be added once. There can be up to 512 signals. Each signal can be a bus of no more than 512 bits.

Web interface usage operations

  1. Click ”Submit”. The compiled information and simulation waveforms will appear at the bottom of the current page.

  1. Click “Submit(new window)” to simulate in the new interface. The compiled information and simulation waveforms will appear in the newly opened interface.

  2. Click ”Share”, the page code will be generated into a web page, and you can share it with others.

  1. You can also put the written Testbench code and RTL code into the same local .v file, then click “Upload a source file…” under the code editing box below, and select to add the .v file in the expanded interface. Then click “Upload and simulate” to start the simulation.

Example demonstration to verify your design

Let’s take this example truth table as an example https://hdlbits.01xz.net/wiki/Truthtable1 for demonstration. Input signals x3, x2, x1; output signal f. Write Verilog code and simulate and draw timing diagrams.

Truth table

< /table>

RTL writing

The implementation details will not be elaborated here. The idea is to use the minimum term of the truth table to simplify.

//--------------RTL------------------
moduleTruthtable(
    input x3,
    input x2,
    input x1, // three inputs
    output f // one output
);

    assign f =(~x3 & amp; x2) | (x3 & amp; x1);
    
endmodule

Testbench writing

`timescale 1ns/1ns
//----------------Tesebench-----------------
module top_module (); //The simulation file name must be "top_module"
reg clk=0;
always #5 clk = ~clk; // Create clock with period=10

// A testbench
reg x1,x2,x3;
\t
initial begin
x3 <= 0 ;
x2 <= 0 ;
x1 <= 0 ;
\t\t
#10
x3 <= 0 ;
x2 <= 0 ;
x1 <= 1;
\t\t
#10
x3 <= 0 ;
x2 <= 1;
x1 <= 0 ;
\t\t
#10
x3 <= 0 ;
x2 <= 1;
x1 <= 1;
\t\t
#10
x3 <= 1;
x2 <= 0 ;
x1 <= 0 ;
\t\t
#10
x3 <= 1;
x2 <= 0 ;
x1 <= 1;
\t\t
#10
x3 <= 1;
x2 <= 1;
x1 <= 0 ;
\t\t
#10
x3 <= 1;
x2 <= 1;
x1 <= 1;
\t\t
#10 $finish; // Quit the simulation
end

Truthtable inst1 (
.x3(x3),
.x2(x2),
.x1(x1)
); // Sub-modules work too.
\t
endmodule

Summary into a .v file

//--------------RTL------------------
moduleTruthtable(
    input x3,
    input x2,
    input x1, // three inputs
    output f // one output
);

    assign f =(~x3 & amp; x2) | (x3 & amp; x1);
    `probe(f);
endmodule

`timescale 1ns/1ns
//----------------Tesebench-----------------
module top_module (); //The simulation file name must be "top_module"
reg clk=0;
always #5 clk = ~clk; // Create clock with period=10
initial `probe_start; // Start the timing diagram

`probe(clk); // Probe signal "clk"

// A testbench
reg x1,x2,x3;
\t
initial begin
x3 <= 0 ;
x2 <= 0 ;
x1 <= 0 ;
\t\t
#10
x3 <= 0 ;
x2 <= 0 ;
x1 <= 1;
\t\t
#10
x3 <= 0 ;
x2 <= 1;
x1 <= 0 ;
\t\t
#10
x3 <= 0 ;
x2 <= 1;
x1 <= 1;
\t\t
#10
x3 <= 1;
x2 <= 0 ;
x1 <= 0 ;
\t\t
#10
x3 <= 1;
x2 <= 0 ;
x1 <= 1;
\t\t
#10
x3 <= 1;
x2 <= 1;
x1 <= 0 ;
\t\t
#10
x3 <= 1;
x2 <= 1;
x1 <= 1;
\t\t
#10 $finish; // Quit the simulation
end

Truthtable inst1 (
.x3(x3),
.x2(x2),
.x1(x1)
); // Sub-modules work too.
\t
`probe(x3);
`probe(x2);
`probe(x1);

endmodule

Simulation results

Get compilation information and waveform graphs.

Export timing diagram

Right-click the mouse in the timing diagram display box and you can choose to save it in PNG format or SVG format for export. There seems to be a problem with the PNG format export, so you can use SVG export.

Compilation error modification

If there is an error in compilation, you can modify it according to the prompts.

Separate top_module simulation

Write top_module module

Use modules to implement and verify division and modulo arithmetic rules in Verilog.

`timescale 1ns/1ns
module top_module;
  reg [7:0] a, b;
  reg [7:0] quotient, remainder;
  initial `probe_start;
    
  initial begin
    a = 46;
    b = 16;
     $display("a = %d, b = %d, quotient = %d, remainder = %d", a, b, quotient, remainder);
    #10
    a = 35;
    b = 6;
     $display("a = %d, b = %d, quotient = %d, remainder = %d", a, b, quotient, remainder);
   #10
    a = 27;
    b = 9;
     $display("a = %d, b = %d, quotient = %d, remainder = %d", a, b, quotient, remainder);
   #30 $finish;
  end

    //Draw a picture
    `probe(a);
    `probe(b);
    `probe(quotient);
    `probe(remainder);
   
    //assignment
    assign quotient = a / b; // perform integer division
    assign remainder = a % b; // perform modular operation
    
endmodule

View compilation information and timing diagram

Get the compilation information and corresponding timing waveforms. It should be noted that the summary of waveforms is not necessary, and it is also allowed if the `probe macro part is removed.

Running Icarus Verilog simulator...
a = 46, b = 16, quotient = 2, remainder = 14
VCD info: dumping is suppressed.
a = 35, b = 6, quotient = 5, remainder = 5
a = 27, b = 9, quotient = 3, remainder = 0
Hint: Total mismatched samples is 0 out of 0 samples

Simulation finished at 50000 ps
Mismatches: 0 in 0 samples

The figure displays the values in hexadecimal:

After removing the `probe macro, only compilation information is displayed without waveforms:

The above is all the content of using Hdlbits to perform online comprehensive simulation of FPGA code and generation of timing diagrams. I hope it will be helpful to you!

syntaxbug.com © 2021 All Rights Reserved.
Row Inputs Outputs
number x3 x2 x1 f
0 0 0 0 0
1 0 0 1 0
2 0 1 0 1
3 0 1 1 1
4 1 0 0 0
5 1 0 1 1
6 1 1 0 0
7 1 1 1 1