测试台和代码验证(VHDL)

测试台和代码验证(VHDL),vhdl,Vhdl,我是VHDL新手。我写了一个减量计数器的代码,在这个代码中,计数器从数组中选取整数并将其向下计数到零,然后递增检查输出。我想让你们验证一下代码在逻辑上是否正确。 如果是,那么如何使用测试台对其进行测试 entity counter is port(clk : in bit; check : out integer); end counter; architecture imp of counter is type my_array is array(n

我是VHDL新手。我写了一个减量计数器的代码,在这个代码中,计数器从数组中选取整数并将其向下计数到零,然后递增检查输出。我想让你们验证一下代码在逻辑上是否正确。 如果是,那么如何使用测试台对其进行测试

entity counter is
   port(clk   : in bit;
        check : out integer);
end counter;     

architecture imp of counter is
   type my_array is array(natural range<>) of integer;
   constant set:my_array(1 to 5):= (2,4,6,8,10);--array of 5 integers
   signal count:integer:=set(1); --initiating count with integer at first location of array 
   signal t : integer;
begin
   process(clk)
      variable i : integer:= 1;--to be used to indicate the locations of array 
   begin
      if (clk='1' and clk'event) and (count>0) then
         count<=count-1;
      elsif (clk='1' and clk'event) and (i<5) then 
         i:=i+1;
         count<= set(i);
         t<=t+1;
      end if;
   end process;
   check<=t;
end imp;
实体计数器不可用
端口(时钟:以位为单位;
签出:整数);
末端计数器;
计数器的结构重要性是
类型my_array是整数的数组(自然范围);
常数集:my_数组(1到5):=(2,4,6,8,10)--5整数数组
信号计数:整数:=设置(1)--在数组的第一个位置使用整数初始化计数
信号t:整数;
开始
过程(clk)
变量i:整数:=1--用于指示阵列的位置
开始
如果(clk='1'和clk'事件)和(计数>0),则

计数代码正常,但需要将初始化值设置为
t
进行模拟

请参阅以了解如何使用VHDL编写测试台

这里有一个简单的例子:

entity counter_tb is
end;

architecture arch of counter_tb is

   component counter is
      port (clk   : in bit;
            check : out integer);
   end component;

   signal clk: bit;
   signal check: integer;
begin

   UUT: counter
      port map (clk => clk, check => check);

   clk_proc: process
   begin
      clk <= '0';
      wait for 50 ns;
      clk <= '1';
      wait for 50 ns;
   end process; 

end arch; 
实体计数器是
结束;
计数器的体系结构是
组件计数器是
端口(时钟:以位为单位;
签出:整数);
端部元件;
信号时钟:位;
信号检查:整数;
开始
UUT:计数器
端口映射(clk=>clk,check=>check);
clk_过程:过程
开始

clk您的代码编译正确,但我们需要更详细的说明它应该做什么来确保它是正确的。正如@tsukuyo所说,您还需要为第10行中的信号“t”指定一个初始值

修复后,这里有一个测试台,可以测试电路并自动检查输出值。在测试台上检查输出值是很重要的,因为这样你就不需要每次修改代码时都盯着波形看了。由于测试台是自我检查的,因此当出现问题时,它会自动告诉您:

use std.textio.all;                                                      
use std.env.all;                                                         

entity counter_tb is                                                     
end;                                                                     

architecture testbench of counter_tb is                                  
   signal clk: bit;                                                      
   signal check: integer;                                                

begin                                                                    
   -- instantiate the unit under test                                    
   uut: entity work.counter port map(clk => clk, check => check);

   -- generate a clock pulse with a 20 ns period                         
   clk <= not clk after 10 ns;                                           

   run_tests: process is                                                 
      -- declare an array with all expected output values                
      type integer_vector is array(natural range<>) of integer;          
      constant EXPECTED_RESULTS: integer_vector := (                     
         0, 0, 0,                                                        
         1, 1, 1, 1, 1,                                                  
         2, 2, 2, 2, 2, 2, 2,                                            
         3, 3, 3, 3, 3, 3, 3, 3, 3,                                      
         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4                                 
      );                                                                 
      variable log_line: line;                                           

   begin                                                                 
      -- loop through all expected values and ensure that                
      -- they match the actual output value                              
      for i in EXPECTED_RESULTS'range loop                               
         wait until rising_edge(clk);                                    
         write(log_line,                                                 
            "i: " & to_string(i) &                                       
            ", check: " & to_string(check) &                             
            ", expected: " & to_string(EXPECTED_RESULTS(i))              
         );                                                              
         writeline(output, log_line);                                    
         assert check = EXPECTED_RESULTS(i);                             
      end loop;                                                          

      report "End of simulation. All tests passed.";                    
      finish;                                                            
   end process;                                                          

end;       
注意:要使用Modelsim运行上述模拟,请键入:

vlib work
vcom -2008 *.vhd
vsim -c counter_tb(testbench) -do "run -all; quit"

这很简单,也很有帮助
vlib work
vcom -2008 *.vhd
vsim -c counter_tb(testbench) -do "run -all; quit"