VHDL语言编写一个一位10进制可逆计数器

用VHDL语言编写一个一位10进制可逆计数器,其中,sl=0时,加计数;sl=1时,减计数;clr=0时,计数器清零

是用BCD码表示十进制吗?可以每四位分开看。

比如BCD码q(11 downto 0)可以表示0到999,前四位是个位,中四位是十位,后四位是百位。不知道对于溢出的有什么要求,我设成溢出后不做任何运算。

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity add_sub is
port(
clk : in std_logic;
clr : in std_logic;
sl : in std_logic;
q : out std_logic_vector(11 downto 0));

end add_sub;

architecture add_sub_arc of add_sub is

signal cnt : std_logic_vector(11 downto 0);

begin

process(clk,clr,cnt)
begin

if clr = '0' then

cnt <= (others => '0');

elsif clk = '1' and clk'event then

if sl = '0' then -- adder

if cnt /= "100110011001" then

if cnt(3 downto 0) = "1001" then

cnt(3 downto 0) <= (others => '0'); -- units
cnt(7 downto 4) <= cnt(7 downto 4) + '1'; -- tens

else

cnt(3 downto 0) <= cnt(3 downto 0) + '1'; -- units

end if;

if cnt(7 downto 4) = "1001" then -- tens

cnt(7 downto 4) <= (others => '0'); -- tens
cnt(11 downto 8) <= cnt(11 downto 8) + '1'; -- hundreds

end if;

else

cnt <= cnt;

end if;

else -- substractor

if cnt /= "000000000000" 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;

if cnt(7 downto 4) = "0000" then

cnt(7 downto 4) <= "1001";
cnt(11 downto 8) <= cnt (11 downto 8) - '1';

end if;

end if;

end if;

q <= cnt;

end if;

end process;

end add_sub_arc;
温馨提示:答案为网友推荐,仅供参考