1 |
2 |
mikej |
--
|
2 |
|
|
-- Risc5x
|
3 |
|
|
-- www.OpenCores.Org - November 2001
|
4 |
|
|
--
|
5 |
|
|
--
|
6 |
|
|
-- This library is free software; you can distribute it and/or modify it
|
7 |
|
|
-- under the terms of the GNU Lesser General Public License as published
|
8 |
|
|
-- by the Free Software Foundation; either version 2.1 of the License, or
|
9 |
|
|
-- (at your option) any later version.
|
10 |
|
|
--
|
11 |
|
|
-- This library is distributed in the hope that it will be useful, but
|
12 |
|
|
-- WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
14 |
|
|
-- See the GNU Lesser General Public License for more details.
|
15 |
|
|
--
|
16 |
|
|
-- A RISC CPU core.
|
17 |
|
|
--
|
18 |
|
|
-- (c) Mike Johnson 2001. All Rights Reserved.
|
19 |
|
|
-- mikej@opencores.org for support or any other issues.
|
20 |
|
|
--
|
21 |
|
|
-- Revision list
|
22 |
|
|
--
|
23 |
|
|
-- version 1.0 initial opencores release
|
24 |
|
|
--
|
25 |
|
|
-- NOTE THIS JUST A TOP LEVEL TEST BENCH
|
26 |
|
|
|
27 |
|
|
use work.pkg_risc5x.all;
|
28 |
|
|
use std.textio.ALL;
|
29 |
|
|
library ieee;
|
30 |
|
|
use ieee.std_logic_1164.all;
|
31 |
|
|
use ieee.std_logic_arith.all;
|
32 |
|
|
use ieee.std_logic_unsigned.all;
|
33 |
|
|
|
34 |
|
|
entity cpu_tb is
|
35 |
|
|
end;
|
36 |
|
|
|
37 |
|
|
architecture Sim of cpu_tb is
|
38 |
|
|
signal clk : std_logic;
|
39 |
|
|
signal reset : std_logic := '1';
|
40 |
|
|
|
41 |
|
|
signal paddr : std_logic_vector(10 downto 0);
|
42 |
|
|
signal pdata : std_logic_vector(11 downto 0) := (others => '0');
|
43 |
|
|
|
44 |
|
|
signal porta_in : std_logic_vector(7 downto 0);
|
45 |
|
|
signal porta_out : std_logic_vector(7 downto 0);
|
46 |
|
|
signal porta_oe_l : std_logic_vector(7 downto 0);
|
47 |
|
|
|
48 |
|
|
signal portb_in : std_logic_vector(7 downto 0);
|
49 |
|
|
signal portb_out : std_logic_vector(7 downto 0);
|
50 |
|
|
signal portb_oe_l : std_logic_vector(7 downto 0);
|
51 |
|
|
|
52 |
|
|
signal portc_in : std_logic_vector(7 downto 0);
|
53 |
|
|
signal portc_out : std_logic_vector(7 downto 0);
|
54 |
|
|
signal portc_oe_l : std_logic_vector(7 downto 0);
|
55 |
|
|
|
56 |
|
|
signal porta_io : std_logic_vector(7 downto 0) := (others => 'H');
|
57 |
|
|
signal portb_io : std_logic_vector(7 downto 0) := (others => 'H');
|
58 |
|
|
signal portc_io : std_logic_vector(7 downto 0) := (others => 'H');
|
59 |
|
|
|
60 |
|
|
signal debug_w : std_logic_vector(7 downto 0);
|
61 |
|
|
signal debug_pc : std_logic_vector(10 downto 0);
|
62 |
|
|
signal debug_inst : std_logic_vector(11 downto 0);
|
63 |
|
|
signal debug_status : std_logic_vector(7 downto 0);
|
64 |
|
|
signal test_addr, test_val : integer;
|
65 |
|
|
signal inst_string : string(8 downto 1);
|
66 |
|
|
signal pc_t1 : std_logic_vector(10 downto 0);
|
67 |
|
|
|
68 |
|
|
signal cnt : std_logic_vector(7 downto 0) := (others => '0');
|
69 |
|
|
constant cDelay : time := 5 ns;
|
70 |
|
|
constant ClkPeriod : time := 20 ns;
|
71 |
|
|
constant filename : string := "JUMPTEST.HEX";
|
72 |
|
|
constant nwords : integer := 2 ** 11;
|
73 |
|
|
|
74 |
|
|
type ram_type is array (0 to nwords-1) of std_logic_vector(11 downto 0);
|
75 |
|
|
shared variable ram :ram_type := (others => (others => '0'));
|
76 |
|
|
|
77 |
|
|
component CPU is
|
78 |
|
|
port (
|
79 |
|
|
PADDR : out std_logic_vector(10 downto 0);
|
80 |
|
|
PDATA : in std_logic_vector(11 downto 0);
|
81 |
|
|
|
82 |
|
|
PORTA_IN : in std_logic_vector(7 downto 0);
|
83 |
|
|
PORTA_OUT : out std_logic_vector(7 downto 0);
|
84 |
|
|
PORTA_OE_L : out std_logic_vector(7 downto 0);
|
85 |
|
|
|
86 |
|
|
PORTB_IN : in std_logic_vector(7 downto 0);
|
87 |
|
|
PORTB_OUT : out std_logic_vector(7 downto 0);
|
88 |
|
|
PORTB_OE_L : out std_logic_vector(7 downto 0);
|
89 |
|
|
|
90 |
|
|
PORTC_IN : in std_logic_vector(7 downto 0);
|
91 |
|
|
PORTC_OUT : out std_logic_vector(7 downto 0);
|
92 |
|
|
PORTC_OE_L : out std_logic_vector(7 downto 0);
|
93 |
|
|
|
94 |
|
|
DEBUG_W : out std_logic_vector(7 downto 0);
|
95 |
|
|
DEBUG_PC : out std_logic_vector(10 downto 0);
|
96 |
|
|
DEBUG_INST : out std_logic_vector(11 downto 0);
|
97 |
|
|
DEBUG_STATUS : out std_logic_vector(7 downto 0);
|
98 |
|
|
|
99 |
|
|
RESET : in std_logic;
|
100 |
|
|
CLK : in std_logic
|
101 |
|
|
);
|
102 |
|
|
end component;
|
103 |
|
|
|
104 |
|
|
begin
|
105 |
|
|
u0 : CPU
|
106 |
|
|
port map (
|
107 |
|
|
PADDR => paddr,
|
108 |
|
|
PDATA => pdata,
|
109 |
|
|
|
110 |
|
|
PORTA_IN => porta_in,
|
111 |
|
|
PORTA_OUT => porta_out,
|
112 |
|
|
PORTA_OE_L => porta_oe_l,
|
113 |
|
|
|
114 |
|
|
PORTB_IN => portb_in,
|
115 |
|
|
PORTB_OUT => portb_out,
|
116 |
|
|
PORTB_OE_L => portb_oe_l,
|
117 |
|
|
|
118 |
|
|
PORTC_IN => portc_in,
|
119 |
|
|
PORTC_OUT => portc_out,
|
120 |
|
|
PORTC_OE_L => portc_oe_l,
|
121 |
|
|
|
122 |
|
|
DEBUG_W => debug_w,
|
123 |
|
|
DEBUG_PC => debug_pc,
|
124 |
|
|
DEBUG_INST => debug_inst,
|
125 |
|
|
DEBUG_STATUS => debug_status,
|
126 |
|
|
|
127 |
|
|
RESET => reset,
|
128 |
|
|
CLK => clk
|
129 |
|
|
);
|
130 |
|
|
|
131 |
|
|
p_drive_ports_out_comb : process(porta_out,porta_oe_l,portb_out,portb_oe_l,portc_out,portc_oe_l)
|
132 |
|
|
begin
|
133 |
|
|
for i in 0 to 7 loop
|
134 |
|
|
if (porta_oe_l(i) = '0') then
|
135 |
|
|
porta_io(i) <= porta_out(i);
|
136 |
|
|
else
|
137 |
|
|
porta_io(i) <= 'Z';
|
138 |
|
|
end if;
|
139 |
|
|
|
140 |
|
|
if (portb_oe_l(i) = '0') then
|
141 |
|
|
portb_io(i) <= portb_out(i);
|
142 |
|
|
else
|
143 |
|
|
portb_io(i) <= 'Z';
|
144 |
|
|
end if;
|
145 |
|
|
|
146 |
|
|
if (portc_oe_l(i) = '0') then
|
147 |
|
|
portc_io(i) <= portc_out(i);
|
148 |
|
|
else
|
149 |
|
|
portc_io(i) <= 'Z';
|
150 |
|
|
end if;
|
151 |
|
|
end loop;
|
152 |
|
|
|
153 |
|
|
end process;
|
154 |
|
|
|
155 |
|
|
p_pullup : process(porta_io,portb_io,portc_io)
|
156 |
|
|
begin
|
157 |
|
|
-- stop unknowns in simulation
|
158 |
|
|
porta_io <= (others => 'H');
|
159 |
|
|
portb_io <= (others => 'H');
|
160 |
|
|
portc_io <= (others => 'H');
|
161 |
|
|
end process;
|
162 |
|
|
|
163 |
|
|
p_drive_ports_in_comb : process(porta_io,portb_io,portc_io)
|
164 |
|
|
begin
|
165 |
|
|
porta_in <= porta_io;
|
166 |
|
|
portb_in <= portb_io;
|
167 |
|
|
portc_in <= portc_io;
|
168 |
|
|
end process;
|
169 |
|
|
|
170 |
|
|
p_clks : process
|
171 |
|
|
begin
|
172 |
|
|
CLK <= '0';
|
173 |
|
|
wait for ClkPeriod / 2;
|
174 |
|
|
CLK <= '1';
|
175 |
|
|
wait for ClkPeriod / 2;
|
176 |
|
|
end process;
|
177 |
|
|
|
178 |
|
|
p_prom : process (RESET,CLK)
|
179 |
|
|
begin
|
180 |
|
|
if (RESET = '1') then
|
181 |
|
|
pdata <= (others=>'0');
|
182 |
|
|
elsif CLK'event and (CLK ='1') then
|
183 |
|
|
pdata <= ram(slv_to_integer(paddr));
|
184 |
|
|
end if;
|
185 |
|
|
end process;
|
186 |
|
|
|
187 |
|
|
p_pc : process
|
188 |
|
|
begin
|
189 |
|
|
wait until CLK'event and (CLK = '1');
|
190 |
|
|
pc_t1 <= debug_pc;
|
191 |
|
|
end process;
|
192 |
|
|
|
193 |
|
|
p_drive_a : process
|
194 |
|
|
begin
|
195 |
|
|
porta_io <= x"02";
|
196 |
|
|
wait for ClkPeriod * 50;
|
197 |
|
|
porta_io <= x"03";
|
198 |
|
|
wait for ClkPeriod * 50;
|
199 |
|
|
end process;
|
200 |
|
|
|
201 |
|
|
p_cpu_top : process
|
202 |
|
|
begin
|
203 |
|
|
reset <= '1';
|
204 |
|
|
wait until CLK'event and CLK = '1';
|
205 |
|
|
reset <= '0' after 100 ns;
|
206 |
|
|
wait;
|
207 |
|
|
end process;
|
208 |
|
|
|
209 |
|
|
p_readhex : process
|
210 |
|
|
function digit_value(c : character) return integer is
|
211 |
|
|
begin
|
212 |
|
|
if (c >= '0') and (c <= '9') then
|
213 |
|
|
return (character'pos(c) - character'pos('0'));
|
214 |
|
|
elsif (c >= 'a') and (c <= 'f') THEN
|
215 |
|
|
return (character'pos(c) - character'pos('a') + 10);
|
216 |
|
|
elsif (c >= 'A') and (c <= 'F') THEN
|
217 |
|
|
return (character'pos(c) - character'pos('A') + 10);
|
218 |
|
|
else
|
219 |
|
|
assert false report "ERROR IN HEX FILE !!" severity note;
|
220 |
|
|
return 999;
|
221 |
|
|
end if;
|
222 |
|
|
end;
|
223 |
|
|
|
224 |
|
|
file hex_file : TEXT open read_mode is filename;
|
225 |
|
|
variable l : line;
|
226 |
|
|
variable val, pos : integer := 0;
|
227 |
|
|
variable numbytes,addr,ltype : integer := 0;
|
228 |
|
|
variable ram_data : std_logic_vector(11 downto 0);
|
229 |
|
|
begin
|
230 |
|
|
assert false report "Loading hex file" & filename severity note;
|
231 |
|
|
while not endfile (hex_file) loop
|
232 |
|
|
readline (hex_file, l);
|
233 |
|
|
if l'left > l'right then next; end if; -- ignore blanks
|
234 |
|
|
--hex file format
|
235 |
|
|
--BBAAAATT HH CC
|
236 |
|
|
|
237 |
|
|
--BB number of HH's, AAAA addr, TT 00 - data (ignore others), CC - checksum
|
238 |
|
|
--skip any spaces or :'s
|
239 |
|
|
pos := l'low;
|
240 |
|
|
for i in l'low TO l'high loop
|
241 |
|
|
case l(i) IS
|
242 |
|
|
when ' ' | ':' | ht => pos := i + 1;
|
243 |
|
|
when others => exit;
|
244 |
|
|
end case;
|
245 |
|
|
end loop;
|
246 |
|
|
|
247 |
|
|
numbytes := digit_value(l(pos)) * 16;
|
248 |
|
|
numbytes := numbytes + digit_value(l(pos + 1));
|
249 |
|
|
|
250 |
|
|
addr := digit_value(l(pos+2)) * 16 * 16 * 16;
|
251 |
|
|
addr := addr + digit_value(l(pos + 3)) * 16 * 16;
|
252 |
|
|
addr := addr + digit_value(l(pos + 4)) * 16;
|
253 |
|
|
addr := addr + digit_value(l(pos + 5)) ;
|
254 |
|
|
addr := addr /2; -- word address
|
255 |
|
|
ltype := digit_value(l(pos+6)) * 16;
|
256 |
|
|
ltype := ltype + digit_value(l(pos+7));
|
257 |
|
|
|
258 |
|
|
if not (ltype = 0) then next; end if;
|
259 |
|
|
pos := pos + 8;
|
260 |
|
|
for i in 1 to (numbytes/2) loop
|
261 |
|
|
val := digit_value(l(pos)) * 16;
|
262 |
|
|
val := val + digit_value(l(pos + 1));
|
263 |
|
|
val := val + digit_value(l(pos + 2)) * 16 * 16 * 16;
|
264 |
|
|
val := val + digit_value(l(pos + 3)) * 16 * 16;
|
265 |
|
|
test_val <= val;
|
266 |
|
|
test_addr <= addr;
|
267 |
|
|
if (addr > nwords-1) then assert false report "ADDRESS TOO BIG !!";
|
268 |
|
|
report "(have you included the configuration bits ??)"
|
269 |
|
|
severity failure; exit;
|
270 |
|
|
end if;
|
271 |
|
|
if (val > (2**12)-1) then assert false report "DATA TOO BIG !!"
|
272 |
|
|
severity failure; exit;
|
273 |
|
|
end if;
|
274 |
|
|
-- wait for 10 ns; -- debug
|
275 |
|
|
ram_data := integer_to_slv(val,12);
|
276 |
|
|
ram(addr) := ram_data;
|
277 |
|
|
addr := addr + 1;
|
278 |
|
|
pos := pos + 4;
|
279 |
|
|
end loop;
|
280 |
|
|
end loop;
|
281 |
|
|
assert false report "Load hex done" severity note;
|
282 |
|
|
wait;
|
283 |
|
|
end process;
|
284 |
|
|
|
285 |
|
|
p_debug_comb : process(DEBUG_INST)
|
286 |
|
|
begin
|
287 |
|
|
inst_string <= "-XXXXXX-";
|
288 |
|
|
if DEBUG_INST(11 downto 0) = "000000000000" then inst_string <= "NOP "; end if;
|
289 |
|
|
if DEBUG_INST(11 downto 5) = "0000001" then inst_string <= "MOVWF "; end if;
|
290 |
|
|
if DEBUG_INST(11 downto 0) = "000001000000" then inst_string <= "CLRW "; end if;
|
291 |
|
|
if DEBUG_INST(11 downto 5) = "0000011" then inst_string <= "CLRF "; end if;
|
292 |
|
|
if DEBUG_INST(11 downto 6) = "000010" then inst_string <= "SUBWF "; end if;
|
293 |
|
|
if DEBUG_INST(11 downto 6) = "000011" then inst_string <= "DECF "; end if;
|
294 |
|
|
if DEBUG_INST(11 downto 6) = "000100" then inst_string <= "IORWF "; end if;
|
295 |
|
|
if DEBUG_INST(11 downto 6) = "000101" then inst_string <= "ANDWF "; end if;
|
296 |
|
|
if DEBUG_INST(11 downto 6) = "000110" then inst_string <= "XORWF "; end if;
|
297 |
|
|
if DEBUG_INST(11 downto 6) = "000111" then inst_string <= "ADDWF "; end if;
|
298 |
|
|
if DEBUG_INST(11 downto 6) = "001000" then inst_string <= "MOVF "; end if;
|
299 |
|
|
if DEBUG_INST(11 downto 6) = "001001" then inst_string <= "COMF "; end if;
|
300 |
|
|
if DEBUG_INST(11 downto 6) = "001010" then inst_string <= "INCF "; end if;
|
301 |
|
|
if DEBUG_INST(11 downto 6) = "001011" then inst_string <= "DECFSZ "; end if;
|
302 |
|
|
if DEBUG_INST(11 downto 6) = "001100" then inst_string <= "RRF "; end if;
|
303 |
|
|
if DEBUG_INST(11 downto 6) = "001101" then inst_string <= "RLF "; end if;
|
304 |
|
|
if DEBUG_INST(11 downto 6) = "001110" then inst_string <= "SWAPF "; end if;
|
305 |
|
|
if DEBUG_INST(11 downto 6) = "001111" then inst_string <= "INCFSZ "; end if;
|
306 |
|
|
|
307 |
|
|
-- *** Bit-Oriented File Register Operations
|
308 |
|
|
if DEBUG_INST(11 downto 8) = "0100" then inst_string <= "BCF "; end if;
|
309 |
|
|
if DEBUG_INST(11 downto 8) = "0101" then inst_string <= "BSF "; end if;
|
310 |
|
|
if DEBUG_INST(11 downto 8) = "0110" then inst_string <= "BTFSC "; end if;
|
311 |
|
|
if DEBUG_INST(11 downto 8) = "0111" then inst_string <= "BTFSS "; end if;
|
312 |
|
|
|
313 |
|
|
-- *** Literal and Control Operations
|
314 |
|
|
if DEBUG_INST(11 downto 0) = "000000000010" then inst_string <= "OPTION "; end if;
|
315 |
|
|
if DEBUG_INST(11 downto 0) = "000000000011" then inst_string <= "SLEEP "; end if;
|
316 |
|
|
if DEBUG_INST(11 downto 0) = "000000000100" then inst_string <= "CLRWDT "; end if;
|
317 |
|
|
if DEBUG_INST(11 downto 0) = "000000000101" then inst_string <= "TRIS "; end if;
|
318 |
|
|
if DEBUG_INST(11 downto 0) = "000000000110" then inst_string <= "TRIS "; end if;
|
319 |
|
|
if DEBUG_INST(11 downto 0) = "000000000111" then inst_string <= "TRIS "; end if;
|
320 |
|
|
if DEBUG_INST(11 downto 8) = "1000" then inst_string <= "RETLW "; end if;
|
321 |
|
|
if DEBUG_INST(11 downto 8) = "1001" then inst_string <= "CALL "; end if;
|
322 |
|
|
if DEBUG_INST(11 downto 9) = "101" then inst_string <= "GOTO "; end if;
|
323 |
|
|
if DEBUG_INST(11 downto 8) = "1100" then inst_string <= "MOVLW "; end if;
|
324 |
|
|
if DEBUG_INST(11 downto 8) = "1101" then inst_string <= "IORLW "; end if;
|
325 |
|
|
if DEBUG_INST(11 downto 8) = "1110" then inst_string <= "ANDLW "; end if;
|
326 |
|
|
if DEBUG_INST(11 downto 8) = "1111" then inst_string <= "XORLW "; end if;
|
327 |
|
|
end process;
|
328 |
|
|
|
329 |
|
|
end Sim;
|
330 |
|
|
|