OpenCores
URL https://opencores.org/ocsvn/spi/spi/trunk

Subversion Repositories spi

[/] [spi/] [tags/] [rel_3/] [bench/] [verilog/] [tb_spi_top.v] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 simons
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  tb_spi_top.v                                                ////
4
////                                                              ////
5
////  This file is part of the SPI IP core project                ////
6
////  http://www.opencores.org/projects/spi/                      ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - Simon Srot (simons@opencores.org)                     ////
10
////                                                              ////
11
////  Based on:                                                   ////
12
////      - i2c/bench/verilog/tst_bench_top.v                     ////
13
////        Copyright (C) 2001 Richard Herveille                  ////
14
////                                                              ////
15
////  All additional information is avaliable in the Readme.txt   ////
16
////  file.                                                       ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2002 Authors                                   ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE.  See the GNU Lesser General Public License for more ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44
 
45
`include "timescale.v"
46
 
47
module tb_spi_top();
48
 
49
  reg         clk;
50
  reg         rst;
51
  wire [31:0] adr;
52
  wire [31:0] dat_i, dat_o;
53
  wire        we;
54
  wire  [3:0] sel;
55
  wire        stb;
56
  wire        cyc;
57
  wire        ack;
58
  wire        err;
59
  wire        int;
60
 
61
  wire  [7:0] ss;
62
  wire        sclk;
63
  wire        mosi;
64
  wire        miso;
65
 
66
  reg  [31:0] q;
67
 
68 7 simons
  parameter SPI_RX_L   = 5'h0;
69
  parameter SPI_RX_H   = 5'h4;
70
  parameter SPI_TX_L   = 5'h0;
71
  parameter SPI_TX_H   = 5'h4;
72
  parameter SPI_CTRL   = 5'h8;
73
  parameter SPI_DEVIDE = 5'hc;
74
  parameter SPI_SS     = 5'h10;
75 2 simons
 
76
  // Generate clock
77
  always #5 clk = ~clk;
78
 
79
  // Wishbone master model
80
  wb_master_model #(32, 32) i_wb_master (
81
    .clk(clk), .rst(rst),
82
    .adr(adr), .din(dat_i), .dout(dat_o),
83
    .cyc(cyc), .stb(stb), .we(we), .sel(sel), .ack(ack), .err(err), .rty(1'b0)
84
  );
85
 
86
  // SPI master core
87
  spi_top i_spi_top (
88
    .wb_clk_i(clk), .wb_rst_i(rst),
89
    .wb_adr_i(adr[4:0]), .wb_dat_i(dat_o), .wb_dat_o(dat_i),
90
    .wb_sel_i(sel), .wb_we_i(we), .wb_stb_i(stb),
91
    .wb_cyc_i(cyc), .wb_ack_o(ack), .wb_err_o(err), .wb_int_o(int),
92
    .ss_pad_o(ss), .sclk_pad_o(sclk), .mosi_pad_o(mosi), .miso_pad_i(miso)
93
  );
94
 
95
  // SPI slave model
96
  spi_slave_model i_spi_slave (
97
    .rst(rst), .ss(ss[0]), .sclk(sclk), .mosi(mosi), .miso(miso)
98
  );
99
 
100
  initial
101
    begin
102
      $display("\nstatus: %t Testbench started\n\n", $time);
103
 
104
      $dumpfile("bench.vcd");
105
      $dumpvars(1, tb_spi_top);
106
      $dumpvars(1, tb_spi_top.i_spi_slave);
107
 
108
      // Initial values
109
      clk = 0;
110
 
111
      i_spi_slave.rx_negedge = 1'b0;
112
      i_spi_slave.tx_negedge = 1'b0;
113
 
114
      // Reset system
115
      rst = 1'b0; // negate reset
116
      #2;
117
      rst = 1'b1; // assert reset
118
      repeat(20) @(posedge clk);
119
      rst = 1'b0; // negate reset
120
 
121
      $display("status: %t done reset", $time);
122
 
123
      @(posedge clk);
124
 
125
      // Program core
126
      i_wb_master.wb_write(0, SPI_DEVIDE, 32'h05); // set devider register
127 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'h5a);   // set tx register to 0x5a
128 2 simons
      i_wb_master.wb_write(0, SPI_CTRL, 32'h40);   // set 8 bit transfer
129
      i_wb_master.wb_write(0, SPI_SS, 32'h01);     // set ss 0
130
 
131
      $display("status: %t programmed registers", $time);
132
 
133
      i_wb_master.wb_cmp(0, SPI_DEVIDE, 32'h05);   // verify devider register
134 7 simons
      i_wb_master.wb_cmp(0, SPI_TX_L, 32'h5a);     // verify tx register
135 2 simons
      i_wb_master.wb_cmp(0, SPI_CTRL, 32'h40);     // verify tx register
136
      i_wb_master.wb_cmp(0, SPI_SS, 32'h01);       // verify ss register
137
 
138
      $display("status: %t verified registers", $time);
139
 
140
      i_spi_slave.rx_negedge = 1'b1;
141
      i_wb_master.wb_write(0, SPI_CTRL, 32'h41);   // set 8 bit transfer, start transfer
142
 
143
      $display("status: %t generate transfer:  8 bit (0x0000005a), msb first, tx posedge, rx negedge", $time);
144
 
145
      // Check bsy bit
146
      i_wb_master.wb_read(0, SPI_CTRL, q);
147
      while (q[0])
148
        i_wb_master.wb_read(1, SPI_CTRL, q);
149
 
150
      if (i_spi_slave.data == 32'h5a)
151
        $display("status: %t transfer completed: 0x0000005a == 0x%x                          ok", $time, i_spi_slave.data);
152
      else
153
        $display("status: %t transfer completed: 0x0000005a != 0x%x                          nok", $time, i_spi_slave.data);
154
 
155
      i_spi_slave.rx_negedge = 1'b0;
156 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'ha5);
157 2 simons
      i_wb_master.wb_write(0, SPI_CTRL, 32'h44);   // set 8 bit transfer, tx negedge
158
      i_wb_master.wb_write(0, SPI_CTRL, 32'h45);   // set 8 bit transfer, tx negedge, start transfer
159
 
160
      $display("status: %t generate transfer:  8 bit (0x0000005a), msb first, tx negedge, rx posedge", $time);
161
 
162
      // Check bsy bit
163
      i_wb_master.wb_read(0, SPI_CTRL, q);
164
      while (q[0])
165
        i_wb_master.wb_read(1, SPI_CTRL, q);
166
 
167
      if (i_spi_slave.data == 32'h5aa5)
168
        $display("status: %t transfer completed: 0x00005aa5 == 0x%x                          ok", $time, i_spi_slave.data);
169
      else
170
        $display("status: %t transfer completed: 0x00005aa5 != 0x%x                          nok", $time, i_spi_slave.data);
171
 
172
      i_spi_slave.rx_negedge = 1'b0;
173 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'h5aa5);
174
      i_wb_master.wb_write(0, SPI_CTRL, 32'h284);   // set 16 bit transfer, tx negedge, lsb
175
      i_wb_master.wb_write(0, SPI_CTRL, 32'h285);   // set 16 bit transfer, tx negedge, start transfer
176 2 simons
 
177
      $display("status: %t generate transfer: 16 bit (0x00005aa5), lsb first, tx negedge, rx posedge", $time);
178
 
179
      // Check bsy bit
180
      i_wb_master.wb_read(0, SPI_CTRL, q);
181
      while (q[0])
182
        i_wb_master.wb_read(1, SPI_CTRL, q);
183
 
184
 
185
      if (i_spi_slave.data == 32'h5aa5a55a)
186
        $display("status: %t transfer completed: 0x5aa5a55a == 0x%x                          ok", $time, i_spi_slave.data);
187
      else
188
        $display("status: %t transfer completed: 0x5aa5a55a != 0x%x                          nok", $time, i_spi_slave.data);
189
 
190
      i_spi_slave.rx_negedge = 1'b0;
191
      i_spi_slave.tx_negedge = 1'b1;
192 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'h55);
193
      i_wb_master.wb_write(0, SPI_CTRL, 32'h244);   // set 8 bit transfer, tx negedge, lsb
194
      i_wb_master.wb_write(0, SPI_CTRL, 32'h245);   // set 8 bit transfer, tx negedge, start transfer
195 2 simons
 
196
      $display("status: %t generate transfer:  8 bit (0x000000a5), lsb first, tx negedge, rx posedge", $time);
197
 
198
      // Check bsy bit
199
      i_wb_master.wb_read(0, SPI_CTRL, q);
200
      while (q[0])
201
        i_wb_master.wb_read(1, SPI_CTRL, q);
202
 
203 7 simons
      i_wb_master.wb_read(1, SPI_RX_L, q);
204 2 simons
 
205 7 simons
      if (i_spi_slave.data == 32'ha5a55aaa && q == 32'h0000005a)
206 2 simons
        $display("status: %t transfer completed: 0xa5a55aaa == 0x%x 0x0000005a == 0x%x ok", $time, i_spi_slave.data, q);
207
      else if (i_spi_slave.data == 32'ha5a55aaa)
208
        $display("status: %t transfer completed: 0xa5a55aaa == 0x%x 0x0000005a != 0x%x nok", $time, i_spi_slave.data, q);
209
      else if (q == 32'h0000005a)
210
        $display("status: %t transfer completed: 0xa5a55aaa != 0x%x 0x0000005a == 0x%x nok", $time, i_spi_slave.data, q);
211
      else
212
        $display("status: %t transfer completed: 0xa5a55aaa != 0x%x 0x0000005a != 0x%x nok", $time, i_spi_slave.data, q);
213
 
214
      i_spi_slave.rx_negedge = 1'b1;
215
      i_spi_slave.tx_negedge = 1'b0;
216 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'haa);
217
      i_wb_master.wb_write(0, SPI_CTRL, 32'h242);   // set 8 bit transfer, rx negedge, lsb
218
      i_wb_master.wb_write(0, SPI_CTRL, 32'h243);   // set 8 bit transfer, rx negedge, start transfer
219 2 simons
 
220
      $display("status: %t generate transfer:  8 bit (0x000000aa), lsb first, tx posedge, rx negedge", $time);
221
 
222
      // Check bsy bit
223
      i_wb_master.wb_read(0, SPI_CTRL, q);
224
      while (q[0])
225
        i_wb_master.wb_read(1, SPI_CTRL, q);
226
 
227 7 simons
      i_wb_master.wb_read(1, SPI_RX_L, q);
228 2 simons
 
229
      if (i_spi_slave.data == 32'ha55aaa55 && q == 32'h000000a5)
230
        $display("status: %t transfer completed: 0xa55aaa55 == 0x%x 0x000000a5 == 0x%x ok", $time, i_spi_slave.data, q);
231
      else if (i_spi_slave.data == 32'ha55aaa55)
232
        $display("status: %t transfer completed: 0xa55aaa55 == 0x%x 0x000000a5 != 0x%x nok", $time, i_spi_slave.data, q);
233
      else if (q == 32'h000000a5)
234
        $display("status: %t transfer completed: 0xa55aaa55 != 0x%x 0x000000a5 == 0x%x nok", $time, i_spi_slave.data, q);
235
      else
236
        $display("status: %t transfer completed: 0xa55aaa55 != 0x%x 0x000000a5 != 0x%x nok", $time, i_spi_slave.data, q);
237
 
238
      i_spi_slave.rx_negedge = 1'b1;
239
      i_spi_slave.tx_negedge = 1'b0;
240 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'haa55);
241 2 simons
      i_wb_master.wb_write(0, SPI_CTRL, 32'h82);   // set 16 bit transfer, rx negedge
242
      i_wb_master.wb_write(0, SPI_CTRL, 32'h83);   // set 16 bit transfer, rx negedge, start transfer
243
 
244
      $display("status: %t generate transfer:  8 bit (0x0000aa55), msb first, tx posedge, rx negedge", $time);
245
 
246
      // Check bsy bit
247
      i_wb_master.wb_read(0, SPI_CTRL, q);
248
      while (q[0])
249
        i_wb_master.wb_read(1, SPI_CTRL, q);
250
 
251 7 simons
      i_wb_master.wb_read(1, SPI_RX_L, q);
252 2 simons
 
253
      if (i_spi_slave.data == 32'haa55aa55 && q == 32'h0000a55a)
254
        $display("status: %t transfer completed: 0xaa55aa55 == 0x%x 0x0000a55a == 0x%x ok", $time, i_spi_slave.data, q);
255
      else if (i_spi_slave.data == 32'haa55aa55)
256
        $display("status: %t transfer completed: 0xaa55aa55 == 0x%x 0x0000a55a != 0x%x nok", $time, i_spi_slave.data, q);
257
      else if (q == 32'h0000a55a)
258
        $display("status: %t transfer completed: 0xaa55aa55 != 0x%x 0x0000a55a == 0x%x nok", $time, i_spi_slave.data, q);
259
      else
260
        $display("status: %t transfer completed: 0xaa55aa55 != 0x%x 0x0000a55a != 0x%x nok", $time, i_spi_slave.data, q);
261
 
262
      i_spi_slave.rx_negedge = 1'b1;
263
      i_spi_slave.tx_negedge = 1'b1;
264 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'haa55a5a5);
265
      i_wb_master.wb_write(0, SPI_CTRL, 32'h500);   // set 32 bit transfer, ie
266
      i_wb_master.wb_write(0, SPI_CTRL, 32'h501);   // set 32 bit transfer, start transfer
267 2 simons
 
268
      $display("status: %t generate transfer: 32 bit (0xaa55a5a5), msb first, tx negedge, rx negedge", $time);
269
 
270
      // Check interrupt signal
271
      while (!int)
272
        @(posedge clk);
273
 
274 7 simons
      i_wb_master.wb_read(1, SPI_RX_L, q);
275 2 simons
 
276
      @(posedge clk);
277
      if (int)
278
        $display("status: %t transfer completed: interrupt still active                            nok", $time, i_spi_slave.data, q);
279
 
280
      if (i_spi_slave.data == 32'haa55a5a5 && q == 32'h552ad52a)
281
        $display("status: %t transfer completed: 0xaa55a5a5 == 0x%x 0x552ad52a == 0x%x ok", $time, i_spi_slave.data, q);
282
      else if (i_spi_slave.data == 32'haa55a5a5)
283
        $display("status: %t transfer completed: 0xaa55a5a5 == 0x%x 0x552ad52a != 0x%x nok", $time, i_spi_slave.data, q);
284
      else if (q == 32'h552ad52a)
285
        $display("status: %t transfer completed: 0xaa55a5a5 != 0x%x 0x552ad52a == 0x%x nok", $time, i_spi_slave.data, q);
286
      else
287
        $display("status: %t transfer completed: 0xaa55a5a5 != 0x%x 0x552ad52a != 0x%x nok", $time, i_spi_slave.data, q);
288
 
289
      i_spi_slave.rx_negedge = 1'b0;
290
      i_spi_slave.tx_negedge = 1'b0;
291 7 simons
      i_wb_master.wb_write(0, SPI_CTRL, 32'h706);   // set 32 bit transfer, ie, lsb, rx negedge, tx negedge
292
      i_wb_master.wb_write(0, SPI_CTRL, 32'h707);   // set 32 bit transfer, start transfer
293 2 simons
 
294
      $display("status: %t generate transfer: 32 bit (0xaa55a5a5), msb first, tx negedge, rx negedge", $time);
295
 
296
      // Check interrupt signal
297
      while (!int)
298
        @(posedge clk);
299
 
300 7 simons
      i_wb_master.wb_read(1, SPI_RX_L, q);
301 2 simons
 
302
      @(posedge clk);
303
      if (int)
304
        $display("status: %t transfer completed: interrupt still active                            nok", $time, i_spi_slave.data, q);
305
 
306
      if (i_spi_slave.data == 32'h54ab54aa && q == 32'ha5a5aa55)
307
        $display("status: %t transfer completed: 0x54ab54aa == 0x%x 0xa5a5aa55 == 0x%x ok", $time, i_spi_slave.data, q);
308
      else if (i_spi_slave.data == 32'h54ab54aa)
309
        $display("status: %t transfer completed: 0x54ab54aa == 0x%x 0xa5a5aa55 != 0x%x nok", $time, i_spi_slave.data, q);
310
      else if (q == 32'ha5a5aa55)
311
        $display("status: %t transfer completed: 0x54ab54aa != 0x%x 0xa5a5aa55 == 0x%x nok", $time, i_spi_slave.data, q);
312
      else
313
        $display("status: %t transfer completed: 0x54ab54aa != 0x%x 0xa5a5aa55 != 0x%x nok", $time, i_spi_slave.data, q);
314
 
315 7 simons
      i_wb_master.wb_write(0, SPI_TX_L, 32'h01234567);
316
      i_wb_master.wb_write(0, SPI_TX_H, 32'h89abcdef);
317
      i_wb_master.wb_write(0, SPI_CTRL, 32'h606);   // set 64 bit transfer, ie, lsb, rx negedge, tx negedge
318
      i_wb_master.wb_write(0, SPI_CTRL, 32'h607);   // set 64 bit transfer, start transfer
319
 
320
      $display("status: %t generate transfer: 64 bit (0x0123456789abcdef), msb first, tx negedge, rx negedge", $time);
321
 
322
      // Check interrupt signal
323
      while (!int)
324
        @(posedge clk);
325
 
326
      i_wb_master.wb_read(1, SPI_RX_H, q);
327
 
328
      @(posedge clk);
329
      if (int)
330
        $display("status: %t transfer completed: interrupt still active                            nok", $time, i_spi_slave.data, q);
331
 
332
      if (i_spi_slave.data == 32'hf7b3d591 && q == 32'h01234567)
333
        $display("status: %t transfer completed: 0xf7b3d591 == 0x%x 0x01234567 == 0x%x ok", $time, i_spi_slave.data, q);
334
      else if (i_spi_slave.data == 32'hf7b3d591)
335
        $display("status: %t transfer completed: 0xf7b3d591 == 0x%x 0x01234567 != 0x%x nok", $time, i_spi_slave.data, q);
336
      else if (q == 32'hf7b3d591)
337
        $display("status: %t transfer completed: 0xf7b3d591 != 0x%x 0x01234567 == 0x%x nok", $time, i_spi_slave.data, q);
338
      else
339
        $display("status: %t transfer completed: 0xf7b3d591 != 0x%x 0x01234567 != 0x%x nok", $time, i_spi_slave.data, q);
340
 
341 2 simons
      $display("\n\nstatus: %t Testbench done", $time);
342
 
343
      #25000; // wait 25us
344
 
345
      $stop;
346
    end
347
 
348
endmodule
349
 
350
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.