|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 venom 于 2011-9-29 15:44 编辑
大家好,下面是我写的一个简单的可逆计数器,以及测试程序,用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;
|
|