我有一个由 D 触发器和多路复用器制成的 4 位计数器。它向上计数到 1111,然后向下计数到 0000。我的设计是结构性的。虽然我不知道如何使启用和负载同步。这是我的尝试:
entity counter4Bit is
Port ( clock : in STD_LOGIC;
reset : in STD_LOGIC;
load : in STD_LOGIC;
enable : in STD_LOGIC;
ud : in STD_LOGIC;
counterOut : out STD_LOGIC_VECTOR (3 downto 0));
end counter4Bit;
architecture Behavioral of counter4Bit is
Component MUX
Port ( sel : in STD_LOGIC_VECTOR (1 downto 0);
a : in STD_LOGIC;
b : in STD_LOGIC;
c : in STD_LOGIC;
d : in STD_LOGIC;
f : out STD_LOGIC);
end component;
Component D_FlipFlop
Port ( D : in STD_LOGIC;
Resetn : in STD_LOGIC;
Clock : in STD_LOGIC;
Q : out STD_LOGIC);
end component;
signal w: std_logic_vector(3 downto 0);
signal h: std_logic_vector(3 downto 0);
signal q0,q1,q2,q3 :std_logic;
signal nq0,nq1,nq2,nq3 :std_logic;
begin
FF0 : D_FlipFlop
port map( D => w(0),
Resetn => reset,
Clock => clock,
Q => q0);
FF1 : D_FlipFlop
port map( D => w(1),
Resetn => reset,
Clock => clock,
Q => q1);
FF2 : D_FlipFlop
port map( D => w(2),
Resetn => reset,
Clock => clock,
Q => q2);
FF3 : D_FlipFlop
port map( D => w(3),
Resetn => reset,
Clock => clock,
Q => q3);
MUX0 : MUX
port map( sel(0) => h(0),
sel(1) => load,
a => q0,
b => nq0,
c => '1',
d => '1',
f => w(0) );
MUX1 : MUX
port map( sel(0) => h(1),
sel(1) => load,
a => q1,
b => nq1,
c => '1',
d => '1',
f => w(1) );
MUX2 : MUX
port map( sel(0) => h(2),
sel(1) => load,
a => q2,
b => nq2,
c => '0',
d => '0',
f => w(2) );
MUX3 : MUX
port map( sel(0) => h(3),
sel(1) => load,
a => q3,
b => nq3,
c => '0',
d => '0',
f => w(3) );
h(0) <= (ud and enable) or (enable and (not ud) );
nq0 <= not q0;
h(1) <= (ud and enable and q0) or (enable and (not ud) and nq0) ;
nq1 <= not q1;
h(2) <= (ud and enable and q0 and q1 ) or (enable and (not ud) and nq1 and nq0);
nq2 <= not q2;
h(3) <= (ud and enable and q0 and q1 and q2 ) or (enable and (not ud) and nq1 and nq2 and nq0);
nq3 <= not q3;
counterOut(0) <= q0;
counterOut(1) <= q1;
counterOut(2) <= q2;
counterOut(3) <= q3;
end Behavioral;