VHDL程序仿真求教
时间:10-02
整理:3721RD
点击:
大家好,下面是我写的一个简单的可逆计数器,以及测试程序,用modelsim仿真一直出问题,不知道原因在何处,希望大家给予指正,谢谢!
BCD码q(7 downto 0)可以表示0到99,前四位是个位,后四位是十位。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity counter is
port(
clk : in std_logic;
reset : in std_logic;
sl : in std_logic;
q : out std_logic_vector(7 downto 0));
end counter;
architecture counter_arc of counter is
signal cnt : std_logic_vector(7 downto 0);
begin
process(clk,reset,cnt)
begin
if reset = '1' then
cnt <= (others => '0');
elsif clk = '1' and clk'event then
if sl = '0' then -- 加计数
if cnt /= "10011001" then
if cnt(3 downto 0) = "1001" then
cnt(3 downto 0) <= (others => '0'); -- 个位
cnt(7 downto 4) <= cnt(7 downto 4) + '1'; -- 十位
end if;
else
cnt <= cnt;
end if;
else -- 减计数
if cnt /= "00000000" then
if cnt(3 downto 0) = "0000" then
cnt(3 downto 0) <= "1001"; -- 个位
cnt(7 downto 4) <= cnt (7 downto 4) - '1'; -- 十位
else
cnt(3 downto 0) <= cnt (3 downto 0) - '1';
end if;
end if;
end if;
q <= cnt;
end if;
end process;
end counter_arc;
--test
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter_tb is
end counter_tb;
architecture behave of counter_tb is
component counter
port( clk : in std_logic;
reset : in std_logic;
sl : in std_logic;
q : out std_logic_vector(7 downto 0));
end component;
signal clk : std_logic := '0';
signal reset : std_logic := '0';
signal sl : std_logic := '0';
signal q : std_logic_vector(7 downto 0);
constant clk_period :time :=20 ms;
begin
U2: counter port map(clk => clk,
reset => reset,
sl => sl,
q => q
);
clk_gen:process
begin
clk<='1';
wait for clk_period/2;
clk<='0';
wait for clk_period/2;
end process;
tb1:process
begin
reset<='0';
wait for 1000 ms;
reset<='1';
wait for 1000 ms;
end process;
tb2:process
begin
sl<= '0';
wait for 1000 ms;
sl<= '1' ;
wait for 1000 ms;
end process;
end behave;
好难看得懂,有注释吗
好歹说说怎么不对了或者贴个波形图出来
1. process(clk,reset,cnt) 应该是 process(clk,reset)
2. sl='0'的逻辑应该有问题,cnt走不了。你自己查不出问题,是因为不写注释,自己都不知道逻辑会怎样。
reset也有问题,里面是=1复位,外面一开始是0,后面改成1,数据都没有初始化。
还是没弄好,仿真图在上面,q需要赋初值嘛?
就像4楼说的, sl=0的时候,如果cnt = "00000000",你没有做任何操作。估计就卡在这里了
q没reset值,这个习惯不好。
复位完是逻辑问题,sl=0 and cnt=0时cnt的值不会变。
