1 |
17 |
leonardoar |
--! @file
|
2 |
20 |
leonardoar |
--! @brief Testbench for Datapath
|
3 |
17 |
leonardoar |
|
4 |
|
|
--! Use standard library and import the packages (std_logic_1164,std_logic_unsigned,std_logic_arith)
|
5 |
|
|
library IEEE;
|
6 |
|
|
use ieee.std_logic_1164.all;
|
7 |
|
|
use ieee.std_logic_unsigned.all;
|
8 |
|
|
use ieee.std_logic_arith.all;
|
9 |
|
|
|
10 |
|
|
--! Use CPU Definitions package
|
11 |
|
|
use work.pkgOpenCPU32.all;
|
12 |
|
|
|
13 |
|
|
ENTITY testDataPath IS
|
14 |
|
|
END testDataPath;
|
15 |
|
|
|
16 |
20 |
leonardoar |
--! @brief Datapath Testbench file
|
17 |
|
|
--! @details Attention to this testbench because it will give you hints on how the control circuit must work....
|
18 |
17 |
leonardoar |
--! for more information: http://vhdlguru.blogspot.com/2010/03/how-to-write-testbench.html
|
19 |
|
|
ARCHITECTURE behavior OF testDataPath IS
|
20 |
|
|
|
21 |
|
|
--! Component declaration to instantiate the Alu circuit
|
22 |
|
|
COMPONENT DataPath
|
23 |
|
|
generic (n : integer := nBits - 1); --! Generic value (Used to easily change the size of the Alu on the package)
|
24 |
|
|
Port ( inputMm : in STD_LOGIC_VECTOR (n downto 0); --! Input of Datapath from main memory
|
25 |
|
|
inputImm : in STD_LOGIC_VECTOR (n downto 0); --! Input of Datapath from imediate value (instructions...)
|
26 |
|
|
clk : in STD_LOGIC; --! Clock signal
|
27 |
|
|
outEn : in typeEnDis; --! Enable/Disable datapath output
|
28 |
|
|
aluOp : in aluOps; --! Alu operations
|
29 |
|
|
muxSel : in STD_LOGIC_VECTOR (2 downto 0); --! Select inputs from dataPath(Memory,Imediate,RegisterFile,Alu)
|
30 |
27 |
leonardoar |
muxRegFile : in STD_LOGIC_VECTOR(1 downto 0); --! Select Alu InputA (Memory,Imediate,RegFileA)
|
31 |
17 |
leonardoar |
regFileWriteAddr : in generalRegisters; --! General register write address
|
32 |
|
|
regFileWriteEn : in STD_LOGIC; --! RegisterFile write enable signal
|
33 |
|
|
regFileReadAddrA : in generalRegisters; --! General register read address (PortA)
|
34 |
|
|
regFileReadAddrB : in generalRegisters; --! General register read address (PortB)
|
35 |
|
|
regFileEnA : in STD_LOGIC; --! Enable RegisterFile PortA
|
36 |
|
|
regFileEnB : in STD_LOGIC; --! Enable RegisterFile PortB
|
37 |
|
|
outputDp : out STD_LOGIC_VECTOR (n downto 0); --! DataPath Output
|
38 |
|
|
dpFlags : out STD_LOGIC_VECTOR (n downto 0)); --! Alu Flags
|
39 |
|
|
END COMPONENT;
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
--Inputs
|
43 |
20 |
leonardoar |
signal inputMm : std_logic_vector(31 downto 0) := (others => 'U'); --! Wire to connect Test signal to component
|
44 |
|
|
signal inputImm : std_logic_vector(31 downto 0) := (others => 'U'); --! Wire to connect Test signal to component
|
45 |
17 |
leonardoar |
signal clk : std_logic := '0'; --! Wire to connect Test signal to component
|
46 |
20 |
leonardoar |
signal outEn : typeEnDis := disable; --! Wire to connect Test signal to component
|
47 |
|
|
signal aluOp : aluOps := alu_pass; --! Wire to connect Test signal to component
|
48 |
|
|
signal muxSel : std_logic_vector(2 downto 0) := (others => 'U'); --! Wire to connect Test signal to component
|
49 |
27 |
leonardoar |
signal muxRegFile : std_logic_vector(1 downto 0) := (others => 'U'); --! Wire to connect Test signal to component
|
50 |
20 |
leonardoar |
signal regFileWriteAddr : generalRegisters := r0; --! Wire to connect Test signal to component
|
51 |
17 |
leonardoar |
signal regFileWriteEn : std_logic := '0'; --! Wire to connect Test signal to component
|
52 |
20 |
leonardoar |
signal regFileReadAddrA : generalRegisters := r0; --! Wire to connect Test signal to component
|
53 |
|
|
signal regFileReadAddrB : generalRegisters := r0; --! Wire to connect Test signal to component
|
54 |
17 |
leonardoar |
signal regFileEnA : std_logic := '0'; --! Wire to connect Test signal to component
|
55 |
|
|
signal regFileEnB : std_logic := '0'; --! Wire to connect Test signal to component
|
56 |
|
|
|
57 |
|
|
--Outputs
|
58 |
|
|
signal outputDp : std_logic_vector(31 downto 0); --! Wire to connect Test signal to component
|
59 |
|
|
signal dpFlags : std_logic_vector(31 downto 0); --! Wire to connect Test signal to component
|
60 |
|
|
|
61 |
20 |
leonardoar |
-- Clock period definitions
|
62 |
|
|
constant CLK_period : time := 10 ns;
|
63 |
17 |
leonardoar |
|
64 |
|
|
BEGIN
|
65 |
|
|
|
66 |
|
|
--! Instantiate the Unit Under Test (Alu) (Doxygen bug if it's not commented!)
|
67 |
|
|
uut: DataPath PORT MAP (
|
68 |
|
|
inputMm => inputMm,
|
69 |
|
|
inputImm => inputImm,
|
70 |
|
|
clk => clk,
|
71 |
|
|
outEn => outEn,
|
72 |
|
|
aluOp => aluOp,
|
73 |
|
|
muxSel => muxSel,
|
74 |
27 |
leonardoar |
muxRegFile => muxRegFile,
|
75 |
17 |
leonardoar |
regFileWriteAddr => regFileWriteAddr,
|
76 |
|
|
regFileWriteEn => regFileWriteEn,
|
77 |
|
|
regFileReadAddrA => regFileReadAddrA,
|
78 |
|
|
regFileReadAddrB => regFileReadAddrB,
|
79 |
|
|
regFileEnA => regFileEnA,
|
80 |
|
|
regFileEnB => regFileEnB,
|
81 |
|
|
outputDp => outputDp,
|
82 |
|
|
dpFlags => dpFlags
|
83 |
|
|
);
|
84 |
20 |
leonardoar |
|
85 |
|
|
-- Clock process definitions
|
86 |
|
|
CLK_process :process
|
87 |
|
|
begin
|
88 |
|
|
CLK <= '0';
|
89 |
|
|
wait for CLK_period/2;
|
90 |
|
|
CLK <= '1';
|
91 |
|
|
wait for CLK_period/2;
|
92 |
|
|
end process;
|
93 |
17 |
leonardoar |
|
94 |
|
|
-- Stimulus process
|
95 |
|
|
stim_proc: process
|
96 |
|
|
begin
|
97 |
20 |
leonardoar |
-- MOV r0,10d ---------------------------------------------------------------------------------
|
98 |
|
|
REPORT "MOV r0,10" SEVERITY NOTE;
|
99 |
|
|
inputImm <= conv_std_logic_vector(10, nBits);
|
100 |
|
|
regFileWriteAddr <= r0;
|
101 |
|
|
aluOp <= alu_pass;
|
102 |
|
|
muxSel <= muxPos(fromImediate);
|
103 |
27 |
leonardoar |
muxRegFile <= muxRegPos(fromRegFileA);
|
104 |
20 |
leonardoar |
regFileWriteEn <= '1';
|
105 |
|
|
wait for CLK_period; -- Wait for clock cycle to latch some data to the register file
|
106 |
|
|
-- Read value in r0 to verify if is equal to 20
|
107 |
|
|
regFileWriteEn <= '0';
|
108 |
|
|
inputImm <= (others => 'U');
|
109 |
|
|
muxSel <= muxPos(fromRegFileA);
|
110 |
|
|
regFileReadAddrA <= r0; -- Read data from r0 and verify if it's 10
|
111 |
|
|
regFileEnA <= '1';
|
112 |
|
|
outEn <= enable;
|
113 |
|
|
wait for 1 ns; -- Wait for data to settle
|
114 |
|
|
assert outputDp = conv_std_logic_vector(10, nBits) report "Invalid value" severity FAILURE;
|
115 |
|
|
wait for 1 ns; -- Finish test case
|
116 |
|
|
muxSel <= (others => 'U');
|
117 |
|
|
regFileEnA <= '0';
|
118 |
|
|
outEn <= disable;
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
-- MOV r1,20d ---------------------------------------------------------------------------------
|
122 |
|
|
REPORT "MOV r1,20" SEVERITY NOTE;
|
123 |
|
|
inputImm <= conv_std_logic_vector(20, nBits);
|
124 |
|
|
regFileWriteAddr <= r1;
|
125 |
|
|
aluOp <= alu_pass;
|
126 |
|
|
muxSel <= muxPos(fromImediate);
|
127 |
27 |
leonardoar |
muxRegFile <= muxRegPos(fromRegFileA);
|
128 |
20 |
leonardoar |
regFileWriteEn <= '1';
|
129 |
|
|
wait for CLK_period; -- Wait for clock cycle to latch some data to the register file
|
130 |
|
|
-- Read value in r1 to verify if is equal to 20
|
131 |
|
|
regFileWriteEn <= '0';
|
132 |
|
|
inputImm <= (others => 'U');
|
133 |
|
|
muxSel <= muxPos(fromRegFileA);
|
134 |
|
|
regFileReadAddrA <= r1; -- Read data from r0 and verify if it's 10
|
135 |
|
|
regFileEnA <= '1';
|
136 |
|
|
outEn <= enable;
|
137 |
|
|
wait for 1 ns; -- Wait for data to settle
|
138 |
|
|
assert outputDp = conv_std_logic_vector(20, nBits) report "Invalid value" severity FAILURE;
|
139 |
|
|
wait for 1 ns; -- Finish test case
|
140 |
|
|
muxSel <= (others => 'U');
|
141 |
|
|
regFileEnA <= '0';
|
142 |
|
|
outEn <= disable;
|
143 |
|
|
|
144 |
21 |
leonardoar |
|
145 |
20 |
leonardoar |
-- MOV r2,r1 (r2 <= r1) --------------------------------------------------------------------
|
146 |
|
|
REPORT "MOV r2,r1" SEVERITY NOTE;
|
147 |
|
|
regFileReadAddrB <= r1; -- Read data from r1
|
148 |
|
|
regFileEnB <= '1';
|
149 |
|
|
regFileWriteAddr <= r2; -- Write data in r2
|
150 |
|
|
muxSel <= muxPos(fromRegFileB); -- Select the PortB output from regFile
|
151 |
27 |
leonardoar |
muxRegFile <= muxRegPos(fromRegFileA);
|
152 |
20 |
leonardoar |
regFileWriteEn <= '1';
|
153 |
|
|
wait for CLK_period; -- Wait for clock cycle to write into r2
|
154 |
|
|
-- Read value in r2 to verify if is equal to r1(20)
|
155 |
|
|
regFileWriteEn <= '0';
|
156 |
|
|
inputImm <= (others => 'U');
|
157 |
|
|
muxSel <= muxPos(fromRegFileA);
|
158 |
|
|
regFileReadAddrA <= r2; -- Read data from r0 and verify if it's 10
|
159 |
|
|
regFileEnA <= '1';
|
160 |
|
|
outEn <= enable;
|
161 |
|
|
wait for 1 ns; -- Wait for data to settle
|
162 |
|
|
assert outputDp = conv_std_logic_vector(20, nBits) report "Invalid value" severity FAILURE;
|
163 |
|
|
wait for 1 ns; -- Finish test case
|
164 |
|
|
muxSel <= (others => 'U');
|
165 |
|
|
regFileEnA <= '0';
|
166 |
|
|
outEn <= disable;
|
167 |
21 |
leonardoar |
wait for 1 ns; -- Finish test case
|
168 |
20 |
leonardoar |
|
169 |
|
|
-- ADD r2,r0 (r2 <= r2+r0)
|
170 |
|
|
REPORT "ADD r2,r0" SEVERITY NOTE;
|
171 |
|
|
regFileReadAddrA <= r2; -- Read data from r2
|
172 |
|
|
regFileEnA <= '1';
|
173 |
|
|
regFileReadAddrB <= r0; -- Read data from r0
|
174 |
|
|
regFileEnB <= '1';
|
175 |
|
|
aluOp <= alu_sum;
|
176 |
|
|
regFileWriteAddr <= r2; -- Write data in r2
|
177 |
|
|
muxSel <= muxPos(fromAlu); -- Select the Alu output
|
178 |
27 |
leonardoar |
muxRegFile <= muxRegPos(fromRegFileA);
|
179 |
20 |
leonardoar |
regFileWriteEn <= '1';
|
180 |
|
|
wait for CLK_period; -- Wait for clock cycle to write into r2
|
181 |
|
|
-- Read value in r2 to verify if is equal to 30(10+20)
|
182 |
|
|
regFileWriteEn <= '0';
|
183 |
|
|
inputImm <= (others => 'U');
|
184 |
|
|
muxSel <= muxPos(fromRegFileB); -- Must access from other Port otherwise you will need an extra cycle to change it's address
|
185 |
|
|
regFileReadAddrB <= r2; -- Read data from r0 and verify if it's 10
|
186 |
|
|
regFileEnB <= '1';
|
187 |
|
|
outEn <= enable;
|
188 |
|
|
wait for 1 ns; -- Wait for data to settle
|
189 |
|
|
assert outputDp = conv_std_logic_vector(30, nBits) report "Invalid value" severity FAILURE;
|
190 |
|
|
wait for 1 ns; -- Finish test case
|
191 |
|
|
muxSel <= (others => 'U');
|
192 |
|
|
regFileEnA <= '0';
|
193 |
21 |
leonardoar |
regFileEnB <= '0';
|
194 |
20 |
leonardoar |
outEn <= disable;
|
195 |
21 |
leonardoar |
wait for 1 ns; -- If you don't use this wait the signals will not change...! (Take care of this when implementing the ControlUnit)
|
196 |
20 |
leonardoar |
|
197 |
21 |
leonardoar |
-- ADD r3,r2,r0 (r3 <= r2+r0)
|
198 |
|
|
REPORT "ADD r3,r2,r0" SEVERITY NOTE;
|
199 |
|
|
regFileReadAddrA <= r2; -- Read data from r2
|
200 |
|
|
regFileEnA <= '1';
|
201 |
|
|
regFileReadAddrB <= r0; -- Read data from r0
|
202 |
|
|
regFileEnB <= '1';
|
203 |
|
|
aluOp <= alu_sum;
|
204 |
|
|
regFileWriteAddr <= r3; -- Write data in r2
|
205 |
|
|
muxSel <= muxPos(fromAlu); -- Select the Alu output
|
206 |
27 |
leonardoar |
muxRegFile <= muxRegPos(fromRegFileA);
|
207 |
21 |
leonardoar |
regFileWriteEn <= '1';
|
208 |
|
|
wait for CLK_period; -- Wait for clock cycle to write into r2
|
209 |
|
|
-- Read value in r2 to verify if is equal to 30(10+20)
|
210 |
|
|
regFileWriteEn <= '0';
|
211 |
|
|
inputImm <= (others => 'U');
|
212 |
|
|
muxSel <= muxPos(fromRegFileB); -- Must access from other Port otherwise you will need an extra cycle to change it's address
|
213 |
|
|
regFileReadAddrB <= r3; -- Read data from r0 and verify if it's 10
|
214 |
|
|
regFileEnB <= '1';
|
215 |
|
|
outEn <= enable;
|
216 |
|
|
wait for 1 ns; -- Wait for data to settle
|
217 |
|
|
assert outputDp = conv_std_logic_vector(40, nBits) report "Invalid value" severity FAILURE;
|
218 |
|
|
wait for 1 ns; -- Finish test case
|
219 |
|
|
muxSel <= (others => 'U');
|
220 |
|
|
regFileEnA <= '0';
|
221 |
|
|
regFileEnB <= '0';
|
222 |
|
|
outEn <= disable;
|
223 |
|
|
|
224 |
27 |
leonardoar |
-- ADD r3,2 (r2 <= r2+2)
|
225 |
|
|
REPORT "ADD r3,2" SEVERITY NOTE;
|
226 |
|
|
inputImm <= conv_std_logic_vector(2, nBits);
|
227 |
|
|
regFileReadAddrB <= r3; -- Read data from r2
|
228 |
|
|
regFileEnB <= '1';
|
229 |
|
|
regFileWriteAddr <= r3;
|
230 |
|
|
muxRegFile <= muxRegPos(fromImediate);
|
231 |
|
|
aluOp <= alu_sum;
|
232 |
|
|
muxSel <= muxPos(fromAlu); -- Select the Alu output
|
233 |
|
|
regFileWriteEn <= '1';
|
234 |
|
|
wait for CLK_period; -- Wait for clock cycle to write into r2
|
235 |
|
|
-- Read value in r2 to verify if is equal to 42(40+2)
|
236 |
|
|
regFileWriteEn <= '0';
|
237 |
|
|
inputImm <= (others => 'U');
|
238 |
|
|
muxSel <= muxPos(fromRegFileA); -- Must access from other Port otherwise you will need an extra cycle to change it's address
|
239 |
|
|
regFileReadAddrA <= r3; -- Read data from r0 and verify if it's 10
|
240 |
|
|
regFileEnA <= '1';
|
241 |
|
|
outEn <= enable;
|
242 |
|
|
wait for 1 ns; -- Wait for data to settle
|
243 |
|
|
assert outputDp = conv_std_logic_vector(42, nBits) report "Invalid value" severity FAILURE;
|
244 |
|
|
wait for 1 ns; -- Finish test case
|
245 |
|
|
muxSel <= (others => 'U');
|
246 |
|
|
regFileEnA <= '0';
|
247 |
|
|
regFileEnB <= '0';
|
248 |
|
|
outEn <= disable;
|
249 |
|
|
wait for 1 ns; -- If you don't use this wait the signals will not change...! (Take care of this when implementing the ControlUnit)
|
250 |
|
|
|
251 |
17 |
leonardoar |
|
252 |
20 |
leonardoar |
-- Finish simulation
|
253 |
|
|
assert false report "NONE. End of simulation." severity failure;
|
254 |
|
|
wait;
|
255 |
17 |
leonardoar |
end process;
|
256 |
|
|
|
257 |
|
|
END;
|