1 | ibrary IEEE;
|
2 | use IEEE.STD_LOGIC_1164.ALL;
|
3 | use IEEE.STD_LOGIC_ARITH.ALL;
|
4 | use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
5 | entity alu_32 is
|
6 | port ( clk,reset : in std_logic;
|
7 | sel : in std_logic_vector(2 downto 0);
|
8 | a,b : in std_logic_vector(31 downto 0);
|
9 | y : out std_logic_vector(31 downto 0) );
|
10 | end alu_32;
|
11 |
|
12 | architecture Behavioral of alu_32 is
|
13 | --signal a : std_logic_vector(31 downto 0) ;
|
14 | --signal a,b : std_logic_vector(31 downto 0);
|
15 | signal count1,count2 : std_logic_vector (1 downto 0);
|
16 | signal atemp, btemp ,atemp1, btemp1: std_logic_vector(15 downto 0);
|
17 | signal y1,y2 :std_logic_vector(31 downto 0);
|
18 | begin
|
19 | process(clk)
|
20 | begin
|
21 | if (reset='1')then
|
22 | y<="00000000000000000000000000000000";
|
23 | end if;
|
24 | if (rising_edge(clk)) then
|
25 | if count1 ="11" then
|
26 | count1 <= "00";
|
27 | else
|
28 | count1 <= count1 + 1;
|
29 | end if;
|
30 | end if;
|
31 | if count1 = "00" then
|
32 | atemp <= y1(31 downto 16);
|
33 | elsif
|
34 | count1 = "01" then
|
35 | btemp <= y1(15 downto 0);
|
36 | elsif
|
37 | count1 ="10" then
|
38 | a<= atemp & btemp;
|
39 | end if;
|
40 |
|
41 | if count2 = "00" then
|
42 | atemp1 <= y2(31 downto 16);
|
43 | elsif
|
44 | count2 = "01" then
|
45 | btemp1 <= y2(15 downto 0);
|
46 | elsif
|
47 | count2 ="10" then
|
48 | b<= atemp1 & btemp1;
|
49 | end if;
|
50 | end process;
|
51 | PROCESS (sel)
|
52 | begin
|
53 | case sel is
|
54 | when "001" => y <= (a - b) ;
|
55 | when "010" => y <= not (a) ;
|
56 | when "011" => y <= (a xor b) ;
|
57 | when "100" => y <= (a + b) ;
|
58 | when "101" => y <=(a and b) ;
|
59 | when "110" => y <= (a or b) ;
|
60 | when others => y <= (a nand b) ;
|
61 | End case ;
|
62 | end process ;
|
63 | end Behavioral;
|
64 | ----------------------------------------------------------------
|
65 | TESTBENCH
|
66 |
|
67 |
|
68 | LIBRARY ieee;
|
69 | USE ieee.std_logic_1164.ALL;
|
70 | USE ieee.std_logic_unsigned.all;
|
71 | USE ieee.numeric_std.ALL;
|
72 |
|
73 | ENTITY test32bit_vhd IS
|
74 | END test32bit_vhd;
|
75 |
|
76 | ARCHITECTURE behavior OF test32bit_vhd IS
|
77 |
|
78 | -- Component Declaration for the Unit Under Test (UUT)
|
79 | COMPONENT alu32
|
80 | PORT(
|
81 | clk ,reset: IN std_logic;
|
82 | sel : IN std_logic_vector(2 downto 0);
|
83 | a : INOUT std_logic_vector(31 downto 0);
|
84 | b : INOUT std_logic_vector(31 downto 0);
|
85 | y : OUT std_logic_vector(31 downto 0)
|
86 | );
|
87 | END COMPONENT;
|
88 |
|
89 | --Inputs
|
90 | SIGNAL clk : std_logic := '0';
|
91 | SIGNAL reset : std_logic := '0';
|
92 | SIGNAL sel : std_logic_vector(2 downto 0) := (others=>'0');
|
93 |
|
94 | --BiDirs
|
95 | SIGNAL a : std_logic_vector(31 downto 0);
|
96 | SIGNAL b : std_logic_vector(31 downto 0);
|
97 |
|
98 | --Outputs
|
99 | SIGNAL y : std_logic_vector(31 downto 0);
|
100 | constant clkperiod : time := 100 ns;
|
101 |
|
102 | BEGIN
|
103 |
|
104 | -- Instantiate the Unit Under Test (UUT)
|
105 | uut: alu32 PORT MAP(
|
106 | clk => clk,
|
107 | reset => reset,
|
108 | sel => sel,
|
109 | a => a,
|
110 | b => b,
|
111 | y => y
|
112 | );
|
113 |
|
114 | clk_process : process
|
115 |
|
116 |
|
117 | BEGIN
|
118 |
|
119 | clk <= '0';
|
120 | wait for clkperiod/2 ;
|
121 | clk <= '1';
|
122 | wait for clkperiod/2;
|
123 | end process;
|
124 |
|
125 | stimproces : process
|
126 | begin
|
127 | wait for 100 ns;
|
128 | wait for 50 ns;
|
129 |
|
130 | reset <= '1';
|
131 |
|
132 |
|
133 | wait for 100 ns;
|
134 | reset <= '0' ;
|
135 |
|
136 | wait for 100 ns;
|
137 | a <= "01011101010100111110000101100010";
|
138 | b <= "00111100101011111100111001100111";
|
139 | --enable <= '1';
|
140 |
|
141 | sel <= "001";
|
142 |
|
143 | wait for 200 ns;
|
144 | sel <= "010";
|
145 |
|
146 | wait for 200 ns;
|
147 | wait; -- will wait forever
|
148 | END PROCESS;
|
149 |
|
150 | END;
|