-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_tb.vhd
executable file
·418 lines (383 loc) · 9.25 KB
/
processor_tb.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std.textio.all;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity processor_tb is
generic (
infile : string := "sample.asc";
addrfile : string := "sample.lst";
resultfile : string := "result_sample.txt"
);
end processor_tb;
architecture arch_processor_tb of processor_tb is
component processor
port (
clk : in std_logic;
rst : in std_logic;
run : in std_logic;
wen : in std_logic;
addr : in std_logic_vector(31 downto 0);
din : in std_logic_vector(31 downto 0);
dout : out std_logic_vector(31 downto 0);
fin : out std_logic;
PCout : out std_logic_vector(31 downto 0);
regaddr : in std_logic_vector(4 downto 0);
regdout : out std_logic_vector(31 downto 0)
);
end component;
signal clk : std_logic;
signal rst : std_logic;
signal run : std_logic;
signal wen : std_logic;
signal addr : std_logic_vector(31 downto 0);
signal din : std_logic_vector(31 downto 0);
signal dout : std_logic_vector(31 downto 0);
signal fin : std_logic;
signal PCout : std_logic_vector(31 downto 0);
signal regaddr : std_logic_vector(4 downto 0);
signal regdout : std_logic_vector(31 downto 0);
signal hexcode : std_logic_vector(3 downto 0);
signal endaddr : std_logic_vector(31 downto 0);
signal dout_buf : std_logic_vector(31 downto 0);
begin
P: processor
port map (
clk => clk,
rst => rst,
run => run,
wen => wen,
addr => addr,
din => din,
dout => dout,
fin => fin,
PCout => PCout,
regaddr => regaddr,
regdout => regdout
);
process
begin
clk <= '0';
wait for 50 ns;
clk <= '1';
wait for 50 ns;
end process;
process
procedure hex2stdvec(signal dcode : out std_logic_vector(3 downto 0);
variable c : in character;
variable good : out boolean) is
begin
if c = 'F' or c = 'f' then
good := true;
dcode <= "1111";
elsif c = 'E' or c = 'e' then
good := true;
dcode <= "1110";
elsif c = 'D' or c = 'd' then
good := true;
dcode <= "1101";
elsif c = 'C' or c = 'c' then
good := true;
dcode <= "1100";
elsif c = 'B' or c = 'b' then
good := true;
dcode <= "1011";
elsif c = 'A' or c = 'a' then
good := true;
dcode <= "1010";
elsif c = '9' then
good := true;
dcode <= "1001";
elsif c = '8' then
good := true;
dcode <= "1000";
elsif c = '7' then
good := true;
dcode <= "0111";
elsif c = '6' then
good := true;
dcode <= "0110";
elsif c = '5' then
good := true;
dcode <= "0101";
elsif c = '4' then
good := true;
dcode <= "0100";
elsif c = '3' then
good := true;
dcode <= "0011";
elsif c = '2' then
good := true;
dcode <= "0010";
elsif c = '1' then
good := true;
dcode <= "0001";
elsif c = '0' then
good := true;
dcode <= "0000";
else
good := false;
dcode <= "XXXX";
end if;
end hex2stdvec;
procedure stdvec2hex(variable c : out character;
signal din : in std_logic_vector(3 downto 0);
variable good : out boolean) is
begin
if din = "1111" then
good := true;
c := 'F';
elsif din = "1110" then
good := true;
c := 'E';
elsif din = "1101" then
good := true;
c := 'D';
elsif din = "1100" then
good := true;
c := 'C';
elsif din = "1011" then
good := true;
c := 'B';
elsif din = "1010" then
good := true;
c := 'A';
elsif din = "1001" then
good := true;
c := '9';
elsif din = "1000" then
good := true;
c := '8';
elsif din = "0111" then
good := true;
c := '7';
elsif din = "0110" then
good := true;
c := '6';
elsif din = "0101" then
good := true;
c := '5';
elsif din = "0100" then
good := true;
c := '4';
elsif din = "0011" then
good := true;
c := '3';
elsif din = "0010" then
good := true;
c := '2';
elsif din = "0001" then
good := true;
c := '1';
elsif din = "0000" then
good := true;
c := '0';
else
good := false;
c := 'X';
end if;
end stdvec2hex;
file ifile : text open read_mode is infile;
file afile : text open read_mode is addrfile;
file rfile : text open write_mode is resultfile;
variable l, message : line;
variable c : character;
variable i : natural;
variable count : natural;
variable good : boolean;
variable correct : boolean;
begin
rst <= '1';
run <= '0';
wen <= '0';
addr <= (others => '0');
din <= (others => '0');
regaddr <= (others => '0');
hexcode <= (others => '0');
dout_buf <= (others => '0');
wait for 130 ns;
rst <= '0';
write(message, string'("Loading Program into Memory"));
writeline(output, message);
writeline(output, message);
while (not endfile(ifile)) loop
readline(ifile, l);
wen <= '0';
din <= "00000000000000000000000000000000";
correct := true;
wait for 8 ns;
for i in 0 to 7 loop
read(l, c, good);
if good = true then
hex2stdvec(hexcode, c, good);
wait for 1 ns;
if good = true then
din <= din(27 downto 0) & hexcode;
else
correct := false;
end if;
wait for 1 ns;
else
correct := false;
wait for 2 ns;
end if;
end loop;
while not(c = '#') and good = true loop
read(l, c, good);
if good = false then
correct := false;
end if;
end loop;
read(l, c, good);
if good = true then
addr <= "00000000000000000000000000000000";
for i in 0 to 7 loop
read(l, c, good);
if good = true then
hex2stdvec(hexcode, c, good);
wait for 1 ns;
if good = true then
addr <= addr(27 downto 0) & hexcode;
else
correct := false;
end if;
wait for 1 ns;
else
correct := false;
wait for 2 ns;
end if;
end loop;
else
correct := false;
end if;
wait for 60 ns;
if correct = true then
wen <= '1';
wait for 100 ns;
end if;
end loop;
wen <= '0';
wait for 100 ns;
run <= '1';
wait for 100 ns;
run <= '0';
while not(fin = '1') loop
wait for 100 ns;
end loop;
write(message, string'("Saving Memory Content"));
writeline(output, message);
writeline(output, message);
l := null;
write(l, string'("Memory Value"));
writeline(rfile, l);
while not(endfile(afile)) loop
readline(afile, l);
addr <= "00000000000000000000000000000000";
wait for 4 ns;
for i in 0 to 7 loop
read(l, c, good);
if good = true then
hex2stdvec(hexcode, c, good);
wait for 1 ns;
if good = true then
addr <= addr(27 downto 0) & hexcode;
end if;
wait for 1 ns;
else
wait for 2 ns;
end if;
end loop;
read(l, c, good);
endaddr <= "00000000000000000000000000000000";
wait for 4 ns;
for i in 0 to 7 loop
read(l, c, good);
if good = true then
hex2stdvec(hexcode, c, good);
wait for 1 ns;
if good = true then
endaddr <= endaddr(27 downto 0) & hexcode;
end if;
wait for 1 ns;
else
wait for 2 ns;
end if;
end loop;
endaddr <= endaddr + 4;
wait for 60 ns;
while not(addr = endaddr) loop
l := null;
dout_buf <= dout;
wait for 2 ns;
for i in 0 to 7 loop
stdvec2hex(c, dout_buf(31 downto 28), good);
write(l, c);
dout_buf <= dout_buf(27 downto 0) & "0000";
wait for 1 ns;
end loop;
c := HT;
write(l, c);
dout_buf <= addr;
wait for 2 ns;
for i in 0 to 7 loop
stdvec2hex(c, dout_buf(31 downto 28), good);
write(l, c);
dout_buf <= dout_buf(27 downto 0) & "0000";
wait for 1 ns;
end loop;
writeline(rfile, l);
addr <= addr + 4;
wait for 80 ns;
end loop;
end loop;
write(message, string'("Saving Register Content"));
writeline(output, message);
writeline(output, message);
l := null;
writeline(rfile, l);
l := null;
write(l, string'("Register Value"));
writeline(rfile, l);
regaddr <= "00000";
count := 0;
wait for 90 ns;
while not(count = 32) loop
l := null;
dout_buf <= regdout;
wait for 2 ns;
for i in 0 to 7 loop
stdvec2hex(c, dout_buf(31 downto 28), good);
write(l, c);
dout_buf <= dout_buf(27 downto 0) & "0000";
wait for 1 ns;
end loop;
c := HT;
write(l, c);
write(l, count);
writeline(rfile, l);
regaddr <= regaddr + 1;
count := count + 1;
wait for 90 ns;
end loop;
l := null;
dout_buf <= PCout;
wait for 2 ns;
for i in 0 to 7 loop
stdvec2hex(c, dout_buf(31 downto 28), good);
write(l, c);
dout_buf <= dout_buf(27 downto 0) & "0000";
wait for 1 ns;
end loop;
c := HT;
write(l, c);
write(l, string'("PC"));
writeline(rfile, l);
wait for 90 ns;
write(message, string'("Writing Result Complete"));
writeline(output, message);
writeline(output, message);
while fin = '1' loop
wait for 100 ns;
end loop;
end process;
end arch_processor_tb;