-
Notifications
You must be signed in to change notification settings - Fork 56
/
testbench.vhd
325 lines (285 loc) · 8.83 KB
/
testbench.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
-------------------------------------------------------
--! @file
--! @brief RISCV Testbench
-- This testbench simulates a core with a
-- generic IO hardware and a Timer
--
-------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.decoder_types.all;
entity core_main_testbench is
generic(
--! Num of 32-bits memory words
IMEMORY_WORDS : integer := 1024; --!= 4K (1024 * 4) bytes
DMEMORY_WORDS : integer := 1024 --!= 2k (512 * 2) bytes
);
port(
----------- SEG7 ------------
HEX0 : out std_logic_vector(7 downto 0);
HEX1 : out std_logic_vector(7 downto 0);
HEX2 : out std_logic_vector(7 downto 0);
HEX3 : out std_logic_vector(7 downto 0);
HEX4 : out std_logic_vector(7 downto 0);
HEX5 : out std_logic_vector(7 downto 0);
----------- SW ------------
SW: in std_logic_vector(9 downto 0);
LEDR: out std_logic_vector(9 downto 0);
---------- ARDUINO IO -----
ARDUINO_IO: inout std_logic_vector(15 downto 0)
);
end entity core_main_testbench;
architecture RTL of core_main_testbench is
-- Clocks and reset
signal clk : std_logic;
signal clk_32x : std_logic;
signal rst : std_logic;
-- Instruction bus and instruction memory
signal address : std_logic_vector(9 downto 0);
signal iaddress : unsigned(15 downto 0);
signal idata : std_logic_vector(31 downto 0);
-- Data bus
signal daddress : unsigned(31 downto 0);
signal ddata_r : std_logic_vector(31 downto 0);
signal ddata_w : std_logic_vector(31 downto 0);
signal dmask : std_logic_vector(3 downto 0);
signal dcsel : std_logic_vector(1 downto 0);
signal d_we : std_logic := '0';
signal ddata_r_mem : std_logic_vector(31 downto 0);
signal d_rd : std_logic;
signal d_sig : std_logic;
-- Modelsim debug signals
signal cpu_state : cpu_state_t;
signal debugString : string(1 to 40) := (others => '0');
-- I/O signals
signal interrupts : std_logic_vector(31 downto 0);
signal ddata_r_gpio : std_logic_vector(31 downto 0);
signal gpio_input : std_logic_vector(31 downto 0);
signal gpio_output : std_logic_vector(31 downto 0);
signal ddata_r_timer : std_logic_vector(31 downto 0);
signal timer_interrupt : std_logic_vector(5 downto 0);
signal ddata_r_periph : std_logic_vector(31 downto 0);
signal ddata_r_stepmot : std_logic_vector(31 downto 0);
signal ddata_r_sdram : std_logic_vector(31 downto 0);
signal gpio_interrupts : std_logic_vector(6 downto 0);
signal ddata_r_segments : std_logic_vector(31 downto 0);
signal ddata_r_uart : std_logic_vector(31 downto 0);
signal ddata_r_adc : std_logic_vector(31 downto 0);
signal ddata_r_i2c : std_logic_vector(31 downto 0);
signal ddata_r_dif_fil : std_logic_vector(31 downto 0);
signal ddata_r_lcd : std_logic_vector(31 downto 0);
signal ddata_r_nn_accelerator : std_logic_vector(31 downto 0);
signal ddata_r_fir_fil : std_logic_vector(31 downto 0);
signal ddata_r_spwm : std_logic_vector(31 downto 0);
signal ddata_r_crc : std_logic_vector(31 downto 0);
signal ddata_r_key : std_logic_vector(31 downto 0);
signal ddata_r_accelerometer : std_logic_vector(31 downto 0);
-- Timer
signal ifcap : std_logic;
begin
clock_driver : process
constant period : time := 1000 ns;
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process clock_driver;
--! Division unit clock
clock_driver_32x : process
constant period : time := 20 ns;
begin
clk_32x <= '0';
wait for period / 2;
clk_32x <= '1';
wait for period / 2;
end process clock_driver_32x;
reset : process is
begin
rst <= '1';
wait for 150 ns;
rst <= '0';
wait;
end process reset;
-- Connect gpio data to output hardware
LEDR <= gpio_output(9 downto 0);
-- Connect input hardware to gpio data
gpio_test: process
begin
gpio_input <= (others => '0');
wait for 6 ms;
-- Generate a input pulse (External IRQ 0 or pooling)
gpio_input(0) <= '1';
wait for 1 us;
gpio_input(0) <= '0';
-- Generate a input pulse (External IRQ 1 or pooling)
wait for 200 us;
gpio_input(1) <= '1';
wait for 1 us;
gpio_input(1) <= '0';
wait;
end process;
-- IMem shoud be read from instruction and data buses
-- Not enough RAM ports for instruction bus, data bus and in-circuit programming
instr_mux: entity work.instructionbusmux
port map(
d_rd => d_rd,
dcsel => dcsel,
daddress => daddress,
iaddress => iaddress,
address => address
);
-- 32-bits x 1024 words quartus RAM (dual port: portA -> riscV, portB -> In-System Mem Editor
iram_quartus_inst : entity work.iram_quartus
port map(
address => address(9 downto 0),
byteena => "1111",
clock => clk,
data => (others => '0'),
wren => '0',
q => idata
);
-- dmemory_address <= daddress;
-- Data Memory RAM
dmem : entity work.dmemory
generic map(
MEMORY_WORDS => DMEMORY_WORDS
)
port map(
rst => rst,
clk => clk,
data => ddata_w,
address => daddress,
we => d_we,
signal_ext => d_sig,
csel => dcsel(0),
dmask => dmask,
q => ddata_r_mem
);
-- Adress space mux ((check sections.ld) -> Data chip select:
-- 0x00000 -> Instruction memory
-- 0x20000 -> Data memory
-- 0x40000 -> Input/Output generic address space
-- 0x60000 -> SDRAM address space
data_bus_mux: entity work.databusmux
port map(
dcsel => dcsel,
idata => idata,
ddata_r_mem => ddata_r_mem,
ddata_r_periph => ddata_r_periph,
ddata_r_sdram => ddata_r_sdram,
ddata_r => ddata_r
);
io_data_bus_mux: entity work.iodatabusmux
port map(
daddress => daddress,
ddata_r_gpio => ddata_r_gpio,
ddata_r_segments => ddata_r_segments,
ddata_r_uart => ddata_r_uart,
ddata_r_adc => ddata_r_adc,
ddata_r_i2c => ddata_r_i2c,
ddata_r_timer => ddata_r_timer,
ddata_r_dif_fil => ddata_r_dif_fil,
ddata_r_stepmot => ddata_r_stepmot,
ddata_r_lcd => ddata_r_lcd,
ddata_r_nn_accelerator => ddata_r_nn_accelerator,
ddata_r_fir_fil => ddata_r_fir_fil,
ddata_r_spwm => ddata_r_spwm,
ddata_r_crc => ddata_r_crc,
ddata_r_key => ddata_r_key,
ddata_r_accelerometer => ddata_r_accelerometer,
ddata_r_periph => ddata_r_periph
);
-- Softcore instatiation
myRiscv : entity work.core
port map(
clk => clk,
rst => rst,
clk_32x => clk_32x,
iaddress => iaddress,
idata => idata,
daddress => daddress,
ddata_r => ddata_r,
ddata_w => ddata_w,
d_we => d_we,
d_rd => d_rd,
d_sig => d_sig,
dcsel => dcsel,
dmask => dmask,
interrupts=>interrupts,
state => cpu_state
);
-- Group IRQ signals.
irq_signals: process(timer_interrupt,gpio_interrupts)
begin
interrupts <= (others => '0');
interrupts(24 downto 18) <= gpio_interrupts(6 downto 0);
interrupts(30 downto 25) <= timer_interrupt;
end process;
-- Timer instantiation
timer : entity work.Timer
generic map(
prescaler_size => 16,
compare_size => 32
)
port map(
clock => clk,
reset => rst,
daddress => daddress,
ddata_w => ddata_w,
ddata_r => ddata_r_timer,
d_we => d_we,
d_rd => d_rd,
dcsel => dcsel,
dmask => dmask,
timer_interrupt => timer_interrupt,
ifcap => ifcap
);
-- Generic GPIO module instantiation
generic_gpio: entity work.gpio
port map(
clk => clk,
rst => rst,
daddress => daddress,
ddata_w => ddata_w,
ddata_r => ddata_r_gpio,
d_we => d_we,
d_rd => d_rd,
dcsel => dcsel,
dmask => dmask,
input => gpio_input,
output => gpio_output,
gpio_interrupts => gpio_interrupts
);
generic_displays : entity work.led_displays
port map(
clk => clk,
rst => rst,
daddress => daddress,
ddata_w => ddata_w,
ddata_r => ddata_r_segments,
d_we => d_we,
d_rd => d_rd,
dcsel => dcsel,
dmask => dmask,
hex0 => HEX0,
hex1 => HEX1,
hex2 => HEX2,
hex3 => HEX3,
hex4 => HEX4,
hex5 => HEX5,
hex6 => open,
hex7 => open
);
-- FileOutput DEBUG
debug : entity work.trace_debug
generic map(
MEMORY_WORDS => IMEMORY_WORDS
)
port map(
pc => iaddress,
data => idata,
inst => debugString
);
end architecture RTL;