1
|
---------------------------------------------------------------------------------------------------
|
2
|
-- Filename : usbrx_toplevel.vhd
|
3
|
-- Project : OsmoSDR FPGA Firmware Testbench
|
4
|
-- Purpose : Toplevel Stimulus
|
5
|
---------------------------------------------------------------------------------------------------
|
6
|
|
7
|
-----------------------------------------------------------------------------------
|
8
|
-- Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
|
9
|
-- written by Matthias Kleffel --
|
10
|
-- --
|
11
|
-- This program is free software; you can redistribute it and/or modify --
|
12
|
-- it under the terms of the GNU General Public License as published by --
|
13
|
-- the Free Software Foundation as version 3 of the License, or --
|
14
|
-- --
|
15
|
-- This program is distributed in the hope that it will be useful, --
|
16
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
17
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
18
|
-- GNU General Public License V3 for more details. --
|
19
|
-- --
|
20
|
-- You should have received a copy of the GNU General Public License --
|
21
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>. --
|
22
|
-----------------------------------------------------------------------------------
|
23
|
|
24
|
library ieee;
|
25
|
use ieee.std_logic_1164.all;
|
26
|
use ieee.std_logic_misc.all;
|
27
|
use ieee.numeric_std.all;
|
28
|
library work;
|
29
|
use work.all;
|
30
|
use work.mt_toolbox.all;
|
31
|
|
32
|
entity tb_usbrx is
|
33
|
end tb_usbrx;
|
34
|
|
35
|
architecture rtl of tb_usbrx is
|
36
|
|
37
|
-- common
|
38
|
signal clk_in_pclk : std_logic := '1';
|
39
|
|
40
|
-- special control
|
41
|
signal dings : std_logic;
|
42
|
signal dingsrst : std_logic;
|
43
|
|
44
|
-- ADC interface
|
45
|
signal adc_cs : std_logic;
|
46
|
signal adc_sck : std_logic;
|
47
|
signal adc_sd1 : std_logic;
|
48
|
signal adc_sd2 : std_logic;
|
49
|
|
50
|
-- control SPI
|
51
|
signal ctl_int : std_logic;
|
52
|
signal ctl_cs : std_logic;
|
53
|
signal ctl_sck : std_logic;
|
54
|
signal ctl_mosi : std_logic;
|
55
|
signal ctl_miso : std_logic;
|
56
|
|
57
|
-- data SPIs
|
58
|
signal rx_clk : std_logic;
|
59
|
signal rx_syn : std_logic;
|
60
|
signal rx_dat : std_logic;
|
61
|
|
62
|
-- data SPIs
|
63
|
signal tx_clk : std_logic := '1';
|
64
|
signal tx_syn : std_logic;
|
65
|
signal tx_dat : std_logic;
|
66
|
|
67
|
-- gain PWMs
|
68
|
signal gain0 : std_logic;
|
69
|
signal gain1 : std_logic;
|
70
|
|
71
|
-- GPS
|
72
|
signal gps_1pps : std_logic;
|
73
|
signal gps_10k : std_logic;
|
74
|
|
75
|
-- gpios
|
76
|
signal gpio : std_logic_vector(9 downto 0);
|
77
|
|
78
|
-- virtual GNDs/VCCs
|
79
|
signal vgnd : std_logic_vector(11 downto 0);
|
80
|
signal vcc33 : std_logic_vector(11 downto 0);
|
81
|
signal vcc12 : std_logic_vector(4 downto 0);
|
82
|
|
83
|
begin
|
84
|
|
85
|
-- generate clocks
|
86
|
clk_in_pclk <= not clk_in_pclk after 500 ns / 30.0;
|
87
|
tx_clk <= '0'; --not tx_clk after 500 ns / 24.0;
|
88
|
|
89
|
-- special control
|
90
|
dings <= '0';
|
91
|
dingsrst <= '1'; --, '0' after 100 us, '1' after 200 us;
|
92
|
|
93
|
-- data SPIs
|
94
|
tx_syn <= '0';
|
95
|
tx_dat <= '0';
|
96
|
|
97
|
-- GPS
|
98
|
-- gps_1pps <= '0';
|
99
|
gps_10k <= '0';
|
100
|
|
101
|
-- gpios
|
102
|
gpio <= (others=>'H');
|
103
|
|
104
|
-- generate pps signal
|
105
|
-- (set every millisecond instead of every second
|
106
|
-- to speed to simulation time)
|
107
|
process
|
108
|
variable cnt : natural;
|
109
|
begin
|
110
|
gps_1pps <= '0';
|
111
|
|
112
|
cnt := 1;
|
113
|
loop
|
114
|
wait for (cnt * 1 ms) - now;
|
115
|
gps_1pps <= '1';
|
116
|
wait for 1us;
|
117
|
gps_1pps <= '0';
|
118
|
cnt := cnt+1;
|
119
|
end loop;
|
120
|
|
121
|
wait;
|
122
|
end process;
|
123
|
|
124
|
-- dummy ADC model
|
125
|
process
|
126
|
-- constant word1 : unsigned(15 downto 0) := "0010000000000001";
|
127
|
-- constant word2 : unsigned(15 downto 0) := "0001111111111110";
|
128
|
-- constant word1 : unsigned(15 downto 0) := "0001111111111111";
|
129
|
-- constant word2 : unsigned(15 downto 0) := "0001111111111111";
|
130
|
variable word1 : unsigned(15 downto 0) := "0011010111001101";
|
131
|
variable word2 : unsigned(15 downto 0) := "0001001111000101";
|
132
|
variable sreg1 : unsigned(15 downto 0);
|
133
|
variable sreg2 : unsigned(15 downto 0);
|
134
|
|
135
|
variable cnt : natural;
|
136
|
|
137
|
begin
|
138
|
adc_sd1 <= 'Z';
|
139
|
adc_sd2 <= 'Z';
|
140
|
|
141
|
cnt := 0;
|
142
|
loop
|
143
|
wait until falling_edge(adc_cs);
|
144
|
|
145
|
word1 := to_unsigned(8192 + cnt, 16);
|
146
|
word2 := to_unsigned(8192 - cnt, 16);
|
147
|
cnt := (cnt + 1) mod 8192;
|
148
|
|
149
|
sreg1 := word1;
|
150
|
sreg2 := word2;
|
151
|
|
152
|
adc_sd1 <= transport 'X', sreg1(15) after 5ns;
|
153
|
adc_sd2 <= transport 'X', sreg2(15) after 5ns;
|
154
|
sreg1 := shift_left(sreg1,1);
|
155
|
sreg2 := shift_left(sreg2,1);
|
156
|
|
157
|
il: loop
|
158
|
wait until rising_edge(adc_cs) or falling_edge(adc_sck);
|
159
|
exit when rising_edge(adc_cs);
|
160
|
|
161
|
adc_sd1 <= transport 'X', sreg1(15) after 11.0ns;
|
162
|
adc_sd2 <= transport 'X', sreg2(15) after 11.0ns;
|
163
|
sreg1 := shift_left(sreg1,1);
|
164
|
sreg2 := shift_left(sreg2,1);
|
165
|
end loop;
|
166
|
|
167
|
adc_sd1 <= transport 'X', 'Z' after 9.5ns;
|
168
|
adc_sd2 <= transport 'X', 'Z' after 9.5ns;
|
169
|
|
170
|
end loop;
|
171
|
|
172
|
end process;
|
173
|
|
174
|
-- SPI interface
|
175
|
process
|
176
|
|
177
|
-- write cycle
|
178
|
procedure spi_write(addr: in integer; data: in slv32_t) is
|
179
|
variable sreg : std_logic_vector(39 downto 0);
|
180
|
begin
|
181
|
-- assemble message
|
182
|
sreg(39) := '0';
|
183
|
sreg(38 downto 32) := std_logic_vector(to_unsigned(addr,7));
|
184
|
sreg(31 downto 0) := data;
|
185
|
|
186
|
-- assert CS
|
187
|
ctl_sck <= '1';
|
188
|
ctl_mosi <= '1';
|
189
|
ctl_cs <= '0';
|
190
|
wait for 250ns;
|
191
|
|
192
|
-- clock out data
|
193
|
for i in 39 downto 0 loop
|
194
|
ctl_sck <= '0';
|
195
|
ctl_mosi <= sreg(i);
|
196
|
wait for 250ns;
|
197
|
ctl_sck <= '1';
|
198
|
wait for 250ns;
|
199
|
end loop;
|
200
|
|
201
|
-- deassert CS
|
202
|
wait for 250ns;
|
203
|
ctl_cs <= '1';
|
204
|
wait for 250ns;
|
205
|
end procedure spi_write;
|
206
|
|
207
|
-- write cycle
|
208
|
procedure spi_writem(addr,count: in integer; data: in slv32_array_t) is
|
209
|
variable sreg : std_logic_vector(31 downto 0);
|
210
|
begin
|
211
|
|
212
|
-- assert CS
|
213
|
ctl_sck <= '1';
|
214
|
ctl_mosi <= '1';
|
215
|
ctl_cs <= '0';
|
216
|
wait for 250ns;
|
217
|
|
218
|
-- write command
|
219
|
sreg(7) := '0';
|
220
|
sreg(6 downto 0) := std_logic_vector(to_unsigned(addr,7));
|
221
|
for i in 7 downto 0 loop
|
222
|
ctl_sck <= '0';
|
223
|
ctl_mosi <= sreg(i);
|
224
|
wait for 250ns;
|
225
|
ctl_sck <= '1';
|
226
|
wait for 250ns;
|
227
|
end loop;
|
228
|
|
229
|
--write data
|
230
|
for j in 0 to count-1 loop
|
231
|
sreg := data(j);
|
232
|
for i in 31 downto 0 loop
|
233
|
ctl_sck <= '0';
|
234
|
ctl_mosi <= sreg(i);
|
235
|
wait for 250ns;
|
236
|
ctl_sck <= '1';
|
237
|
wait for 250ns;
|
238
|
end loop;
|
239
|
end loop;
|
240
|
|
241
|
-- deassert CS
|
242
|
wait for 250ns;
|
243
|
ctl_cs <= '1';
|
244
|
wait for 250ns;
|
245
|
end procedure spi_writem;
|
246
|
|
247
|
-- read cycle
|
248
|
procedure spi_read(addr,count: in integer; data: out slv32_array_t) is
|
249
|
variable sreg : std_logic_vector(7 downto 0);
|
250
|
begin
|
251
|
|
252
|
-- assemble message
|
253
|
sreg(7) := '1';
|
254
|
sreg(6 downto 0) := std_logic_vector(to_unsigned(addr,7));
|
255
|
|
256
|
-- assert CS
|
257
|
ctl_sck <= '1';
|
258
|
ctl_mosi <= '1';
|
259
|
ctl_cs <= '0';
|
260
|
wait for 250ns;
|
261
|
|
262
|
-- clock out command
|
263
|
for i in 7 downto 0 loop
|
264
|
ctl_sck <= '0';
|
265
|
ctl_mosi <= sreg(i);
|
266
|
wait for 250ns;
|
267
|
ctl_sck <= '1';
|
268
|
wait for 250ns;
|
269
|
end loop;
|
270
|
|
271
|
wait for 50us;
|
272
|
|
273
|
-- read data
|
274
|
for j in 0 to count-1 loop
|
275
|
for i in 31 downto 0 loop
|
276
|
ctl_sck <= '0';
|
277
|
wait for 250ns;
|
278
|
data(j)(i) := ctl_miso;
|
279
|
ctl_sck <= '1';
|
280
|
wait for 250ns;
|
281
|
|
282
|
if i=24 or i=16 or i=8 then
|
283
|
wait for 50us;
|
284
|
end if;
|
285
|
|
286
|
end loop;
|
287
|
end loop;
|
288
|
|
289
|
-- deassert CS
|
290
|
wait for 250ns;
|
291
|
ctl_cs <= '1';
|
292
|
wait for 250ns;
|
293
|
end procedure spi_read;
|
294
|
|
295
|
variable temp : slv32_array_t(0 to 5);
|
296
|
|
297
|
begin
|
298
|
ctl_cs <= '1';
|
299
|
ctl_sck <= '1';
|
300
|
ctl_mosi <= '1';
|
301
|
|
302
|
wait for 30us;
|
303
|
|
304
|
-- spi_write(4,x"00000001");
|
305
|
|
306
|
-- spi_read(0,1,temp);
|
307
|
-- spi_read(0,1,temp);
|
308
|
-- wait;
|
309
|
--
|
310
|
-- temp(0) := x"00000000";
|
311
|
-- temp(1) := x"12345678";
|
312
|
-- temp(2) := x"9ABCDEF0";
|
313
|
-- temp(3) := x"11233435";
|
314
|
-- temp(4) := x"23652662";
|
315
|
-- temp(5) := x"98735773";
|
316
|
-- spi_writem(0,6,temp);
|
317
|
--
|
318
|
-- temp := (others=>x"00000000");
|
319
|
-- spi_read(0,6,temp);
|
320
|
|
321
|
wait;
|
322
|
end process;
|
323
|
|
324
|
-- unit under test
|
325
|
uut: entity usbrx_toplevel
|
326
|
port map (
|
327
|
-- common
|
328
|
clk_in_pclk => clk_in_pclk,
|
329
|
|
330
|
-- special control
|
331
|
dings => dings,
|
332
|
dingsrst => dingsrst,
|
333
|
|
334
|
-- ADC interface
|
335
|
adc_cs => adc_cs,
|
336
|
adc_sck => adc_sck,
|
337
|
adc_sd1 => adc_sd1,
|
338
|
adc_sd2 => adc_sd2,
|
339
|
|
340
|
-- control SPI
|
341
|
ctl_int => ctl_int,
|
342
|
ctl_cs => ctl_cs,
|
343
|
ctl_sck => ctl_sck,
|
344
|
ctl_mosi => ctl_mosi,
|
345
|
ctl_miso => ctl_miso,
|
346
|
|
347
|
-- data SPIs
|
348
|
rx_clk => rx_clk,
|
349
|
rx_syn => rx_syn,
|
350
|
rx_dat => rx_dat,
|
351
|
|
352
|
-- data SPIs
|
353
|
tx_clk => tx_clk,
|
354
|
tx_syn => tx_syn,
|
355
|
tx_dat => tx_dat,
|
356
|
|
357
|
-- gain PWMs
|
358
|
gain0 => gain0,
|
359
|
gain1 => gain1,
|
360
|
|
361
|
-- GPS
|
362
|
gps_1pps => gps_1pps,
|
363
|
gps_10k => gps_10k,
|
364
|
|
365
|
-- gpios
|
366
|
gpio => gpio,
|
367
|
|
368
|
-- virtual GNDs/VCCs
|
369
|
vgnd => vgnd,
|
370
|
vcc33 => vcc33,
|
371
|
vcc12 => vcc12
|
372
|
);
|
373
|
|
374
|
end rtl;
|
375
|
|
376
|
|