FPGA TUTORIALS

What is the Testbench? FPGA Simulation, TCL – Tool Command Language

Before reading this article, you should read the article about HDLs: (LINK HERE )

The Github folder for this article: (LINK HERE)

+ One of the main uses of an HDL is that of simulating a new design to ensure that the design is correct. To simulate a design, we need to set the design’s inputs to certain values, and then check that the design’s output values are what we expect them to be. A system that sets input values and checks output values is known as a testbench.

+ Creating test benches for your FPGA design is a critical step for any FPGA design project. The VHDL, Verilog test benches are used for the simulation and verification of FPGA designs.

+ The Testbench does not contain any external ports, just internal signals that connect the two instantiated components. Testbench automatically generates inputs and checks the output with the provided inputs.

+ FPGA Software provides HDL Simulators, you can look up information here: List of HDL Simulators.

The testbench has two main components:

  • The first being the Design Under Test (DUT) or the Unit Under Test (UUT), the Design Under Verification (DUV).
  • The second the stimulus driver.

A testbench is used to verify the functionality of a design.
A testbench is at the highest level in the hierarchy of the design (The top-level design). The testbench instantiates the design under test (DUT). The testbench provides the necessary input stimulus to the DUT and examines the output from the DUT.

=> The stimulus driver drives inputs into the DUT.

There are two ways to create input stimulus:

  • Using an interactive waveform editor (easy).
  • Using a testbench (a bit harder).

=> The DUT responds to the input signals and produces output results.

=> Finally, a compare function within the testbench compares the results from the DUT against those known good results and reports any discrepancies.

Let’s learn about Programming languages for TestBench through examples.
We will learn
how to write TestBench for programs with AND Gate, so you can grasp the problem as quickly as possible. If you start learning about TestBench for complex designs, it will be harder for you to approach and understand the problem.

=> Attention: VHDL code of AND gate, you read in this link about HDLs.

We will write a VHDL testbench for this AND gate.

3.1.1./ Write VHDL testbench and run on Quartus, ModelSim

  1. -- Vhdl Test Bench template for design : ANDGATE
  2. --
  3. -- Simulation tool : ModelSim-Altera (VHDL)
  4. --
  5. LIBRARY ieee;
  6. USE ieee.std_logic_1164.all;
  7. ENTITY ANDGATE_vhd_tst IS
  8. END ANDGATE_vhd_tst;
  9. ARCHITECTURE ANDGATE_arch OF ANDGATE_vhd_tst IS
  10. -- constants
  11. -- signals
  12. SIGNAL I1 : STD_LOGIC;
  13. SIGNAL I2 : STD_LOGIC;
  14. SIGNAL O : STD_LOGIC;
  15. COMPONENT ANDGATE
  16. PORT (
  17. I1 : IN STD_LOGIC;
  18. I2 : IN STD_LOGIC;
  19. O : OUT STD_LOGIC
  20. );
  21. END COMPONENT;
  22. BEGIN
  23. dut : ANDGATE
  24. PORT MAP (
  25. -- list connections between master ports and signals
  26. I1 => I1,
  27. I2 => I2,
  28. O => O
  29. );
  30. init : PROCESS
  31. -- variable declarations
  32. BEGIN
  33. -- code that executes only once
  34. I1 <= '0';
  35. I2 <= '0';
  36. wait for 1 ps;
  37. I1 <= '0';
  38. I2 <= '1';
  39. wait for 1 ps;
  40. I1 <= '1';
  41. I2 <= '0';
  42. wait for 1 ps;
  43. I1 <= '1';
  44. I2 <= '1';
  45. wait for 1 ps;
  46. WAIT;
  47. END PROCESS init;
  48. always : PROCESS
  49. -- optional sensitivity list
  50. -- ( )
  51. -- variable declarations
  52. BEGIN
  53. -- code executes for every event on sensitivity list
  54. WAIT;
  55. END PROCESS always;
  56. END ANDGATE_arch;

3.1.2./ Write VHDL testbench and run on Vivado

  1. library IEEE;
  2. use IEEE.std_logic_1164.all;
  3. use std.env.finish;
  4. -- creat VHDL testbench entity has no input or output
  5. entity and_vhdl_tb is
  6. end entity and_vhdl_tb;
  7. -- describe the architecture of VHDL testbench entity
  8. architecture behave of and_vhdl_tb is
  9. signal r_I1: std_logic;
  10. signal r_I2: std_logic;
  11. signal w_O: std_logic;
  12. begin
  13. -- instantiate the uut, using and_vhdl entity in and_vhdl_tb architecture
  14. -- the Working Library: contains the compiled version of your design. The contents of a working library change every time you compile your design.
  15. -- the default working library is named "work"
  16. uut : entity work.and_vhdl
  17. port map(
  18. I1 => r_I1,
  19. I2 => r_I2,
  20. O => w_O
  21. );
  22. -- start at the begining of the simulation
  23. process is
  24. begin
  25. r_I1 <= '0';
  26. r_I2 <= '0';
  27. wait for 5ps;
  28. r_I1 <= '0';
  29. r_I2 <= '1';
  30. wait for 5ps;
  31. r_I1 <= '1';
  32. r_I2 <= '0';
  33. wait for 5ps;
  34. r_I1 <= '1';
  35. r_I2 <= '1';
  36. wait for 5ps;
  37. --finish;
  38. wait;
  39. end process;
  40. end behave;

=> Attention: Verilog code of AND gate, you read in this link about HDLs.

We will write a Verilog testbench for this AND gate.

3.2.1./ Write Verilog testbench and run on Quartus, ModelSim

  1. // Verilog Test Bench template for design : and_verrilog
  2. //
  3. // Simulation tool : ModelSim-Altera (Verilog)
  4. //
  5. `timescale 1 ps/ 1 ps
  6. module and_verrilog_vlg_tst();
  7. // constants
  8. // general purpose registers
  9. reg eachvec;
  10. // test vector input registers
  11. reg a;
  12. reg b;
  13. // wires
  14. wire c;
  15. // assign statements (if any)
  16. and_verrilog i1 (
  17. // port map - connection between master ports and signals/registers
  18. .a(a),
  19. .b(b),
  20. .c(c)
  21. );
  22. initial
  23. begin
  24. // code that executes only once
  25. // insert code here --> begin
  26. a = 0;
  27. b = 0;
  28. // --> end
  29. $display("Running testbench");
  30. end
  31. always
  32. // optional sensitivity list
  33. // @(event1 or event2 or .... eventn)
  34. begin
  35. // code executes for every event on sensitivity list
  36. // insert code here --> begin
  37. $dumpfile("and_verilog_test.vcd"); $dumpvars;
  38. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  39. a <= 1'b0;
  40. b <= 1'b0;
  41. #5;
  42. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  43. a <= 1'b0;
  44. b <= 1'b1;
  45. #5;
  46. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  47. a <= 1'b1;
  48. b <= 1'b0;
  49. #5;
  50. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  51. a <= 1'b1;
  52. b <= 1'b1;
  53. #5;
  54. $display("Finish testbench");
  55. @eachvec;
  56. // --> end
  57. end
  58. endmodule

3.2.2./ Write Verilog testbench and run on Vivado

  1. // Verilog Test Bench template for design : and_verrilog
  2. //
  3. // Simulation tool : Xilinx Vivado (Verilog)
  4. //
  5. `timescale 1 ps/ 1 ps
  6. module and_verrilog_vlg_tst();
  7. // test vector input registers
  8. reg a;
  9. reg b;
  10. // wires
  11. wire c;
  12. // assign statements (if any)
  13. and_verrilog i1 (
  14. // port map - connection between master ports and signals/registers
  15. .a(a),
  16. .b(b),
  17. .c(c)
  18. );
  19. initial
  20. begin
  21. // code that executes only once
  22. // insert code here --> begin
  23. a = 0;
  24. b = 0;
  25. // --> end
  26. $display("Running testbench");
  27. end
  28. always
  29. // optional sensitivity list
  30. // @(event1 or event2 or .... eventn)
  31. begin
  32. // code executes for every event on sensitivity list
  33. // insert code here --> begin
  34. $dumpfile("and_verilog_test.vcd"); $dumpvars;
  35. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  36. a <= 1'b0;
  37. b <= 1'b0;
  38. #5;
  39. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  40. a <= 1'b0;
  41. b <= 1'b1;
  42. #5;
  43. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  44. a <= 1'b1;
  45. b <= 1'b0;
  46. #5;
  47. $monitor("Time= %3d : a= %d,b= %d, c=%d \n",$time,a,b,c);
  48. a <= 1'b1;
  49. b <= 1'b1;
  50. #5;
  51. $display("Finish testbench");
  52. // --> end
  53. end
  54. endmodule

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Telegram
Tin nhắn
Skype
Zalo
Tin nhắn
Telegram
Skype
Zalo