In: Electrical Engineering
-- VHDL CODE
-- Code your design here
library IEEE;
use IEEE.std_logic_1164.all;
entity register_4bit is
port ( CLK: in std_logic;
D : in std_logic ;
RST : in std_logic;
Q : out std_logic
);
end register_4bit;
architecture structural of register_4bit is
--d flipflop componenet declartion
component dff is
port ( CLK: in std_logic;
D : in std_logic;
RST : in std_logic;
Q : out std_logic );
end component;
signal Q_reg : std_logic_vector(3 downto 0) ;
begin
--instantiate D fliplfops
U1: dff port map ( CLK,D,RST,Q_reg(3));
U2: dff port map ( CLK,Q_reg(3),RST,Q_reg(2));
U3: dff port map ( CLK,Q_reg(2),RST,Q_reg(1));
U4: dff port map ( CLK,Q_reg(1),RST,Q_reg(0));
Q<= Q_reg(0);
end structural;
-- d flilfop verilog code
library IEEE;
use IEEE.std_logic_1164.all;
entity dff is
port ( CLK: in std_logic;
D : in std_logic;
RST : in std_logic;
Q : out std_logic );
end dff;
architecture rtl of dff is
begin
process(CLK,RST)
begin
if (RST) then
Q<='0';
elsif (rising_edge(CLK)) then
Q<=D;
end if;
end process;
end rtl;
-- Code your testbench here
library IEEE;
use IEEE.std_logic_1164.all;
entity test is
end test;
architecture behavioral of test is
component register_4bit is
port ( CLK: in std_logic;
D : in std_logic ;
RST : in std_logic;
Q : out std_logic
);
end component;
signal CLK,D,RST: std_logic;
signal Q : std_logic;
begin
--innstantiate DUT
DUT: register_4bit port map ( CLK => CLK , D => D , RST =>
RST , Q => Q );
--clocl generation
process
begin
CLK <= '0';
wait for 10 ns;
CLK <= '1';
wait for 10 ns;
end process;
--input stimulus
process
begin
RST <= '1'; D <= '0';
wait for 10 ns;
RST <= '0'; D <= '1';
wait for 10 ns;
RST <= '0'; D <= '0';
wait for 10 ns;
RST <= '0'; D <= '1';
wait for 10 ns;
RST <= '0'; D <= '1';
wait for 10 ns;
RST <= '0'; D <= '0';
wait for 10 ns;
RST <= '0'; D <= '1';
wait;
end process;
end behavioral;
-- simulated waveform
Explanation