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

Subversion Repositories can

[/] [can/] [tags/] [rel_9/] [bench/] [verilog/] [can_testbench.v] - Blame information for rev 29

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

Line No. Rev Author Line
1 2 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  can_testbench.v                                             ////
4
////                                                              ////
5
////                                                              ////
6 9 mohor
////  This file is part of the CAN Protocol Controller            ////
7 2 mohor
////  http://www.opencores.org/projects/can/                      ////
8
////                                                              ////
9
////                                                              ////
10
////  Author(s):                                                  ////
11
////       Igor Mohor                                             ////
12
////       igorm@opencores.org                                    ////
13
////                                                              ////
14
////                                                              ////
15 9 mohor
////  All additional information is available in the README.txt   ////
16 2 mohor
////  file.                                                       ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20 9 mohor
//// Copyright (C) 2002, 2003 Authors                             ////
21 2 mohor
////                                                              ////
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 28 mohor
//// The CAN protocol is developed by Robert Bosch GmbH and       ////
44
//// protected by patents. Anybody who wants to implement this    ////
45
//// CAN IP core on silicon has to obtain a CAN protocol license  ////
46
//// from Bosch.                                                  ////
47
////                                                              ////
48 2 mohor
//////////////////////////////////////////////////////////////////////
49
//
50
// CVS Revision History
51
//
52
// $Log: not supported by cvs2svn $
53 29 mohor
// Revision 1.20  2003/02/09 02:24:11  mohor
54
// Bosch license warning added. Error counters finished. Overload frames
55
// still need to be fixed.
56
//
57 28 mohor
// Revision 1.19  2003/02/04 17:24:33  mohor
58
// Backup.
59
//
60 26 mohor
// Revision 1.18  2003/02/04 14:34:45  mohor
61
// *** empty log message ***
62
//
63 25 mohor
// Revision 1.17  2003/01/31 01:13:31  mohor
64
// backup.
65
//
66 24 mohor
// Revision 1.16  2003/01/16 13:36:14  mohor
67
// Form error supported. When receiving messages, last bit of the end-of-frame
68
// does not generate form error. Receiver goes to the idle mode one bit sooner.
69
// (CAN specification ver 2.0, part B, page 57).
70
//
71 22 mohor
// Revision 1.15  2003/01/15 21:05:06  mohor
72
// CRC checking fixed (when bitstuff occurs at the end of a CRC sequence).
73
//
74 20 mohor
// Revision 1.14  2003/01/15 14:40:16  mohor
75
// RX state machine fixed to receive "remote request" frames correctly. No data bytes are written to fifo when such frames are received.
76
//
77 19 mohor
// Revision 1.13  2003/01/15 13:16:42  mohor
78
// When a frame with "remote request" is received, no data is stored to fifo, just the frame information (identifier, ...). Data length that is stored is the received data length and not the actual data length that is stored to fifo.
79
//
80 18 mohor
// Revision 1.12  2003/01/14 17:25:03  mohor
81
// Addresses corrected to decimal values (previously hex).
82
//
83 17 mohor
// Revision 1.11  2003/01/14 12:19:29  mohor
84
// rx_fifo is now working.
85
//
86 16 mohor
// Revision 1.10  2003/01/10 17:51:28  mohor
87
// Temporary version (backup).
88
//
89 15 mohor
// Revision 1.9  2003/01/09 21:54:39  mohor
90
// rx fifo added. Not 100 % verified, yet.
91
//
92 14 mohor
// Revision 1.8  2003/01/08 02:09:43  mohor
93
// Acceptance filter added.
94
//
95 11 mohor
// Revision 1.7  2002/12/28 04:13:53  mohor
96
// Backup version.
97
//
98 10 mohor
// Revision 1.6  2002/12/27 00:12:48  mohor
99
// Header changed, testbench improved to send a frame (crc still missing).
100
//
101 9 mohor
// Revision 1.5  2002/12/26 16:00:29  mohor
102
// Testbench define file added. Clock divider register added.
103
//
104 8 mohor
// Revision 1.4  2002/12/26 01:33:01  mohor
105
// Tripple sampling supported.
106
//
107 7 mohor
// Revision 1.3  2002/12/25 23:44:12  mohor
108
// Commented lines removed.
109
//
110 6 mohor
// Revision 1.2  2002/12/25 14:16:54  mohor
111
// Synchronization working.
112
//
113 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
114
// Initial
115 2 mohor
//
116
//
117 5 mohor
//
118 2 mohor
 
119
// synopsys translate_off
120
`include "timescale.v"
121
// synopsys translate_on
122
`include "can_defines.v"
123 8 mohor
`include "can_testbench_defines.v"
124 2 mohor
 
125
module can_testbench();
126
 
127
 
128
 
129
parameter Tp = 1;
130 8 mohor
parameter BRP = 2*(`CAN_TIMING0_BRP + 1);
131 2 mohor
 
132
 
133
 
134
reg         clk;
135
reg         rst;
136
reg   [7:0] data_in;
137
wire  [7:0] data_out;
138
reg         cs, rw;
139
reg   [7:0] addr;
140
reg         rx;
141 24 mohor
wire        tx;
142 28 mohor
wire        tx_oen;
143
wire        tx_3st;
144 24 mohor
wire        rx_and_tx;
145
 
146 2 mohor
integer     start_tb;
147 14 mohor
reg   [7:0] tmp_data;
148 24 mohor
reg         delayed_tx;
149 28 mohor
reg         tx_bypassed;
150 2 mohor
 
151 24 mohor
 
152 11 mohor
// Instantiate can_top module
153 2 mohor
can_top i_can_top
154
(
155
  .clk(clk),
156
  .rst(rst),
157
  .data_in(data_in),
158
  .data_out(data_out),
159
  .cs(cs),
160
  .rw(rw),
161
  .addr(addr),
162 24 mohor
  .rx(rx_and_tx),
163 28 mohor
  .tx(tx),
164
  .tx_oen(tx_oen)
165 2 mohor
);
166
 
167 28 mohor
assign tx_3st = tx_oen? 1'bz : tx;
168 2 mohor
 
169 24 mohor
 
170 2 mohor
// Generate clock signal 24 MHz
171
initial
172
begin
173
  clk=0;
174
  forever #20 clk = ~clk;
175
end
176
 
177
initial
178
begin
179
  start_tb = 0;
180
  data_in = 'hz;
181
  cs = 0;
182
  rw = 'hz;
183
  addr = 'hz;
184
  rx = 1;
185
  rst = 1;
186
  #200 rst = 0;
187 16 mohor
  #200 initialize_fifo;
188 2 mohor
  #200 start_tb = 1;
189 28 mohor
  tx_bypassed = 0;
190 2 mohor
end
191
 
192
 
193 28 mohor
 
194
 
195 24 mohor
// Generating delayed tx signal (CAN transciever delay)
196
always
197
begin
198 28 mohor
  wait (tx_3st);
199 24 mohor
  repeat (4*BRP) @ (posedge clk);   // 4 time quants delay
200 28 mohor
  #1 delayed_tx = tx_3st;
201
  wait (~tx_3st);
202 24 mohor
  repeat (4*BRP) @ (posedge clk);   // 4 time quants delay
203 28 mohor
  #1 delayed_tx = tx_3st;
204 24 mohor
end
205
 
206 28 mohor
//assign rx_and_tx = rx & delayed_tx;   FIX ME !!!
207
assign rx_and_tx = rx & (delayed_tx | tx_bypassed);
208 24 mohor
 
209
 
210 2 mohor
// Main testbench
211
initial
212
begin
213
  wait(start_tb);
214
 
215 11 mohor
  // Set bus timing register 0
216 17 mohor
  write_register(8'd6, {`CAN_TIMING0_SJW, `CAN_TIMING0_BRP});
217 8 mohor
 
218 11 mohor
  // Set bus timing register 1
219 17 mohor
  write_register(8'd7, {`CAN_TIMING1_SAM, `CAN_TIMING1_TSEG2, `CAN_TIMING1_TSEG1});
220 11 mohor
 
221 14 mohor
 
222
 
223 11 mohor
  // Set Clock Divider register
224 17 mohor
  write_register(8'd31, {`CAN_CLOCK_DIVIDER_MODE, 7'h0});    // Setting the normal mode (not extended)
225 16 mohor
 
226 11 mohor
  // Set Acceptance Code and Acceptance Mask registers (their address differs for basic and extended mode
227
  if(`CAN_CLOCK_DIVIDER_MODE)   // Extended mode
228
    begin
229
      // Set Acceptance Code and Acceptance Mask registers
230 17 mohor
      write_register(8'd16, 8'ha6); // acceptance code 0
231
      write_register(8'd17, 8'hb0); // acceptance code 1
232
      write_register(8'd18, 8'h12); // acceptance code 2
233 20 mohor
      write_register(8'd19, 8'h30); // acceptance code 3
234 17 mohor
      write_register(8'd20, 8'h0); // acceptance mask 0
235
      write_register(8'd21, 8'h0); // acceptance mask 1
236 20 mohor
      write_register(8'd22, 8'h00); // acceptance mask 2
237
      write_register(8'd23, 8'h00); // acceptance mask 3
238 11 mohor
    end
239
  else
240
    begin
241
      // Set Acceptance Code and Acceptance Mask registers
242 17 mohor
//      write_register(8'd4, 8'ha6); // acceptance code
243 25 mohor
      write_register(8'd4, 8'he8); // acceptance code
244
      write_register(8'd5, 8'h0f); // acceptance mask
245 11 mohor
    end
246 2 mohor
 
247
  #10;
248
  repeat (1000) @ (posedge clk);
249
 
250 11 mohor
  // Switch-off reset mode
251 17 mohor
  write_register(8'd0, {7'h0, ~(`CAN_MODE_RESET)});
252 10 mohor
 
253
  repeat (BRP) @ (posedge clk);   // At least BRP clocks needed before bus goes to dominant level. Otherwise 1 quant difference is possible
254
                                  // This difference is resynchronized later.
255 24 mohor
 
256
  // After exiting the reset mode
257 11 mohor
  repeat (7) send_bit(1);         // Sending EOF
258 24 mohor
  repeat (3) send_bit(1);         // Sending Interframe
259 10 mohor
 
260 9 mohor
//  test_synchronization;
261 10 mohor
 
262
 
263
 
264 11 mohor
  if(`CAN_CLOCK_DIVIDER_MODE)   // Extended mode
265
    begin
266 20 mohor
//      test_empty_fifo_ext;    // test currently switched off
267
      test_full_fifo_ext;     // test currently switched on
268
//      send_frame_ext;         // test currently switched off
269 11 mohor
    end
270
  else
271
    begin
272 18 mohor
//      test_empty_fifo;    // test currently switched off
273 24 mohor
//      test_full_fifo;     // test currently switched off
274 28 mohor
//      send_frame;         // test currently switched off
275
      manual_frame;         // test currently switched on
276 16 mohor
    end
277 15 mohor
 
278
 
279 16 mohor
  $display("CAN Testbench finished !");
280
  $stop;
281
end
282 5 mohor
 
283 14 mohor
 
284 28 mohor
task manual_frame;    // Testbench sends a frame
285
  begin
286 29 mohor
/*
287
    begin
288 14 mohor
 
289 28 mohor
      $display("\n\nTestbench sends a frame bit by bit");
290
      send_bit(0);  // SOF
291
      send_bit(1);  // ID
292
      send_bit(1);  // ID
293
      send_bit(1);  // ID
294
      send_bit(0);  // ID
295
      send_bit(1);  // ID
296
      send_bit(0);  // ID
297
      send_bit(0);  // ID
298
      send_bit(0);  // ID
299
      send_bit(1);  // ID
300
      send_bit(0);  // ID
301
      send_bit(1);  // ID
302
      send_bit(1);  // RTR
303
      send_bit(0);  // IDE
304
      send_bit(0);  // r0
305
      send_bit(0);  // DLC
306
      send_bit(1);  // DLC
307
      send_bit(0);  // DLC
308
      send_bit(0);  // DLC
309
      send_bit(1);  // CRC
310
      send_bit(0);  // CRC
311
      send_bit(0);  // CRC
312
      send_bit(0);  // CRC
313
      send_bit(1);  // CRC
314
      send_bit(0);  // CRC
315
      send_bit(1);  // CRC
316
      send_bit(1);  // CRC
317
      send_bit(0);  // CRC
318
      send_bit(1);  // CRC
319
      send_bit(0);  // CRC
320
      send_bit(1);  // CRC
321
      send_bit(1);  // CRC
322
      send_bit(0);  // CRC
323
      send_bit(0);  // CRC          // error
324
      send_bit(1);  // CRC DELIM
325
      send_bit(0);  // ACK
326
      send_bit(1);  // ACK DELIM
327
      send_bit(0);  // EOF        // error comes here
328
      send_bit(0);  // EOF        // error comes here
329
 
330
//tx_bypassed=1;
331
      send_bit(0);  // EOF        // error comes here
332
//tx_bypassed=0;
333
 
334
      send_bit(0);  // EOF        // error comes here
335
      send_bit(0);  // EOF        // error comes here
336
      send_bit(0);  // EOF        // error comes here
337
      send_bit(1);  // EOF        // delimiter
338
      send_bit(1);  // INTER      // delimiter
339
      send_bit(1);  // INTER      // delimiter
340
      send_bit(1);  // INTER      // delimiter
341
      send_bit(1);  // IDLE       // delimiter
342
      send_bit(1);  // IDLE       // delimiter
343
      send_bit(1);  // IDLE       // delimiter
344
      send_bit(0);  // IDLE       // delimiter
345
      send_bit(1);  // IDLE       // delimiter
346
      send_bit(1);  // IDLE       // delimiter
347
      send_bit(1);  // IDLE       // delimiter
348
      send_bit(1);  // IDLE       // delimiter
349
      send_bit(1);  // IDLE       // delimiter
350
      send_bit(1);  // IDLE       // delimiter
351
      send_bit(1);  // IDLE
352
      send_bit(1);  // IDLE
353
      send_bit(1);  // IDLE
354
 
355
    end
356 29 mohor
*/
357 28 mohor
// tx_bypassed=1;
358
 
359
 
360 29 mohor
    write_register(8'd10, 8'he8); // Writing ID[10:3] = 0xe8
361
    write_register(8'd11, 8'hb7); // Writing ID[2:0] = 0x5, rtr = 1, length = 7
362
    write_register(8'd12, 8'h00); // data byte 1
363
    write_register(8'd13, 8'h00); // data byte 2
364
    write_register(8'd14, 8'h00); // data byte 3
365
    write_register(8'd15, 8'h00); // data byte 4
366
    write_register(8'd16, 8'h00); // data byte 5
367
    write_register(8'd17, 8'h00); // data byte 6
368
    write_register(8'd18, 8'h00); // data byte 7
369
    write_register(8'd19, 8'h00); // data byte 8
370
 
371 28 mohor
    fork
372
      begin
373
        tx_request;
374
      end
375
 
376
      begin
377
        #520;
378
        send_bit(0);  // SOF
379
        send_bit(1);  // ID
380
        send_bit(1);  // ID
381
        send_bit(1);  // ID
382
        send_bit(0);  // ID
383
        send_bit(1);  // ID
384
        send_bit(0);  // ID
385
        send_bit(0);  // ID
386
        send_bit(0);  // ID
387
        send_bit(1);  // ID
388
        send_bit(0);  // ID
389
        send_bit(1);  // ID
390
        send_bit(1);  // RTR
391
        send_bit(0);  // IDE
392
        send_bit(0);  // r0
393
        send_bit(0);  // DLC
394
        send_bit(1);  // DLC
395
        send_bit(1);  // DLC
396
        send_bit(1);  // DLC
397
        send_bit(1);  // CRC
398
        send_bit(0);  // CRC
399
        send_bit(0);  // CRC
400
        send_bit(1);  // CRC
401
        send_bit(1);  // CRC
402
        send_bit(1);  // CRC
403
        send_bit(0);  // CRC
404
        send_bit(1);  // CRC
405
        send_bit(0);  // CRC
406
        send_bit(0);  // CRC
407
        send_bit(1);  // CRC
408
        send_bit(1);  // CRC
409
        send_bit(1);  // CRC
410
        send_bit(1);  // CRC
411
        send_bit(1);  // CRC
412
        send_bit(1);  // CRC DELIM
413
        send_bit(0);  // ACK
414
        send_bit(1);  // ACK DELIM
415
        send_bit(1);  // EOF
416
        send_bit(1);  // EOF
417
        send_bit(1);  // EOF
418
        send_bit(1);  // EOF
419
        send_bit(1);  // EOF
420
        send_bit(1);  // EOF
421
        send_bit(1);  // EOF
422
//  tx_bypassed=1;
423 29 mohor
        send_bit(0);  // INTER
424
        send_bit(1);  // INTER    overload
425
        send_bit(1);  // INTER    overload
426
        send_bit(1);  // INTER    overload
427
        send_bit(1);  // INTER    overload
428
        send_bit(1);  // INTER    overload
429
        send_bit(1);  // INTER    overload
430
        send_bit(1);  // INTER    overload delim
431
        send_bit(1);  // INTER    overload delim
432
        send_bit(1);  // INTER    overload delim
433
        send_bit(1);  // INTER    overload delim
434
        send_bit(1);  // INTER    overload delim
435
        send_bit(1);  // INTER    overload delim
436
        send_bit(1);  // INTER    overload delim
437
        send_bit(1);  // INTER    overload delim
438 28 mohor
        send_bit(1);  // INTER
439 29 mohor
        send_bit(0);  // IDLE
440
        send_bit(1);  // INTER    overload
441
        send_bit(1);  // INTER    overload
442
        send_bit(1);  // INTER    overload
443
        send_bit(1);  // INTER    overload
444
        send_bit(1);  // INTER    overload
445
        send_bit(1);  // INTER    overload
446
        send_bit(0);  // INTER    waiting for recessive
447
        send_bit(0);  // INTER    waiting for recessive
448
        send_bit(0);  // INTER    waiting for recessive
449
        send_bit(0);  // INTER    waiting for recessive
450
        send_bit(1);  // INTER    overload delim
451
        send_bit(1);  // INTER    overload delim
452
        send_bit(1);  // INTER    overload delim
453
        send_bit(1);  // INTER    overload delim
454
        send_bit(1);  // INTER    overload delim
455
        send_bit(1);  // INTER    overload delim
456
        send_bit(1);  // INTER    overload delim
457
        send_bit(1);  // INTER    overload delim
458 28 mohor
        send_bit(1);  // IDLE
459
        send_bit(1);  // IDLE
460
        send_bit(1);  // IDLE
461
 
462
      end
463
 
464
 
465
    join
466
 
467
    read_receive_buffer;
468
    release_rx_buffer;
469
 
470
 
471
 
472
 
473
 
474
    read_receive_buffer;
475
    release_rx_buffer;
476
    read_receive_buffer;
477
 
478
    #200000;
479
 
480
  end
481
endtask
482
 
483
 
484
 
485 18 mohor
task send_frame;    // CAN IP core sends frames
486
  begin
487 14 mohor
 
488 18 mohor
    if(`CAN_CLOCK_DIVIDER_MODE)   // Extended mode
489
      begin
490
 
491
        // Writing TX frame information + identifier + data
492
        write_register(8'd16, 8'h12);
493
        write_register(8'd17, 8'h34);
494
        write_register(8'd18, 8'h56);
495
        write_register(8'd19, 8'h78);
496
        write_register(8'd20, 8'h9a);
497
        write_register(8'd21, 8'hbc);
498
        write_register(8'd22, 8'hde);
499
        write_register(8'd23, 8'hf0);
500
        write_register(8'd24, 8'h0f);
501
        write_register(8'd25, 8'hed);
502
        write_register(8'd26, 8'hcb);
503
        write_register(8'd27, 8'ha9);
504
        write_register(8'd28, 8'h87);
505
      end
506
    else
507
      begin
508 25 mohor
        write_register(8'd10, 8'hea); // Writing ID[10:3] = 0xea
509 28 mohor
        write_register(8'd11, 8'h28); // Writing ID[2:0] = 0x1, rtr = 0, length = 8
510 18 mohor
        write_register(8'd12, 8'h56); // data byte 1
511
        write_register(8'd13, 8'h78); // data byte 2
512
        write_register(8'd14, 8'h9a); // data byte 3
513
        write_register(8'd15, 8'hbc); // data byte 4
514
        write_register(8'd16, 8'hde); // data byte 5
515
        write_register(8'd17, 8'hf0); // data byte 6
516
        write_register(8'd18, 8'h0f); // data byte 7
517
        write_register(8'd19, 8'hed); // data byte 8
518
      end
519
 
520 25 mohor
 
521
    fork
522
      begin
523
        $display("\n\nStart receiving data from CAN bus");
524
        receive_frame(0, 0, {26'h00000e8, 3'h1}, 4'h0, 15'h2372); // mode, rtr, id, length, crc
525
        receive_frame(0, 0, {26'h00000e8, 3'h1}, 4'h1, 15'h30bb); // mode, rtr, id, length, crc
526
        receive_frame(0, 0, {26'h00000e8, 3'h1}, 4'h2, 15'h2da1); // mode, rtr, id, length, crc
527
        receive_frame(0, 0, {26'h00000ee, 3'h1}, 4'h0, 15'h6cea); // mode, rtr, id, length, crc
528
        receive_frame(0, 0, {26'h00000ee, 3'h1}, 4'h1, 15'h00c5); // mode, rtr, id, length, crc
529
        receive_frame(0, 0, {26'h00000ee, 3'h1}, 4'h2, 15'h7b4a); // mode, rtr, id, length, crc
530 28 mohor
 
531 25 mohor
      end
532
 
533
      begin
534
        tx_request;
535
      end
536
 
537
      begin
538
        // Transmitting acknowledge
539
        wait (can_testbench.i_can_top.i_can_bsp.tx_state & can_testbench.i_can_top.i_can_bsp.rx_ack);
540 28 mohor
        #1 rx = 0;
541 25 mohor
        wait (can_testbench.i_can_top.i_can_bsp.rx_ack_lim);
542 28 mohor
        #1 rx = 1;
543 25 mohor
      end
544
 
545
    join
546
 
547
    read_receive_buffer;
548
    release_rx_buffer;
549
    release_rx_buffer;
550
    read_receive_buffer;
551
    release_rx_buffer;
552
    read_receive_buffer;
553
    release_rx_buffer;
554
    read_receive_buffer;
555
    release_rx_buffer;
556
    read_receive_buffer;
557
 
558
    #200000;
559
 
560
    read_receive_buffer;
561
 
562 18 mohor
  end
563
endtask
564
 
565
 
566
 
567 16 mohor
task test_empty_fifo;
568
  begin
569 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h3, 15'h7bcb); // mode, rtr, id, length, crc
570
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h7, 15'h085c); // mode, rtr, id, length, crc
571 15 mohor
 
572 16 mohor
    read_receive_buffer;
573
    fifo_info;
574 15 mohor
 
575 16 mohor
    release_rx_buffer;
576
    $display("\n\n");
577
    read_receive_buffer;
578
    fifo_info;
579 15 mohor
 
580 16 mohor
    release_rx_buffer;
581
    $display("\n\n");
582
    read_receive_buffer;
583
    fifo_info;
584 15 mohor
 
585 16 mohor
    release_rx_buffer;
586
    $display("\n\n");
587
    read_receive_buffer;
588
    fifo_info;
589 15 mohor
 
590 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
591 15 mohor
 
592 16 mohor
    $display("\n\n");
593
    read_receive_buffer;
594
    fifo_info;
595 2 mohor
 
596 16 mohor
    release_rx_buffer;
597
    $display("\n\n");
598
    read_receive_buffer;
599
    fifo_info;
600 2 mohor
 
601 16 mohor
    release_rx_buffer;
602
    $display("\n\n");
603
    read_receive_buffer;
604
    fifo_info;
605
  end
606
endtask
607 2 mohor
 
608
 
609 16 mohor
 
610 20 mohor
task test_empty_fifo_ext;
611
  begin
612
    receive_frame(1, 0, 29'h14d60246, 4'h3, 15'h5262); // mode, rtr, id, length, crc
613
    receive_frame(1, 0, 29'h14d60246, 4'h7, 15'h1730); // mode, rtr, id, length, crc
614
 
615
    read_receive_buffer;
616
    fifo_info;
617
 
618
    release_rx_buffer;
619
    $display("\n\n");
620
    read_receive_buffer;
621
    fifo_info;
622
 
623
    release_rx_buffer;
624
    $display("\n\n");
625
    read_receive_buffer;
626
    fifo_info;
627
 
628
    release_rx_buffer;
629
    $display("\n\n");
630
    read_receive_buffer;
631
    fifo_info;
632
 
633
    receive_frame(1, 0, 29'h14d60246, 4'h8, 15'h2f7a); // mode, rtr, id, length, crc
634
 
635
    $display("\n\n");
636
    read_receive_buffer;
637
    fifo_info;
638
 
639
    release_rx_buffer;
640
    $display("\n\n");
641
    read_receive_buffer;
642
    fifo_info;
643
 
644
    release_rx_buffer;
645
    $display("\n\n");
646
    read_receive_buffer;
647
    fifo_info;
648
  end
649
endtask
650
 
651
 
652
 
653 16 mohor
task test_full_fifo;
654
  begin
655
    release_rx_buffer;
656
    $display("\n\n");
657
    read_receive_buffer;
658
    fifo_info;
659
 
660 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h0, 15'h4edd); // mode, rtr, id, length, crc
661 16 mohor
    read_receive_buffer;
662
    fifo_info;
663 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h1, 15'h1ccf); // mode, rtr, id, length, crc
664 16 mohor
    read_receive_buffer;
665
    fifo_info;
666 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h2, 15'h73f4); // mode, rtr, id, length, crc
667 16 mohor
    fifo_info;
668
    read_receive_buffer;
669 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h3, 15'h7bcb); // mode, rtr, id, length, crc
670 16 mohor
    fifo_info;
671 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h4, 15'h37da); // mode, rtr, id, length, crc
672 16 mohor
    fifo_info;
673 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h5, 15'h7e15); // mode, rtr, id, length, crc
674 16 mohor
    fifo_info;
675 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h6, 15'h39cf); // mode, rtr, id, length, crc
676 16 mohor
    fifo_info;
677 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h7, 15'h085c); // mode, rtr, id, length, crc
678 16 mohor
    fifo_info;
679 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
680 16 mohor
    fifo_info;
681 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
682 16 mohor
    fifo_info;
683 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
684 16 mohor
    fifo_info;
685 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
686 16 mohor
    fifo_info;
687 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
688 16 mohor
    fifo_info;
689
    read_overrun_info(0, 15);
690
 
691
    release_rx_buffer;
692
    release_rx_buffer;
693
    release_rx_buffer;
694
    read_receive_buffer;
695
    fifo_info;
696 18 mohor
    receive_frame(0, 0, {26'h0000008, 3'h1}, 4'h8, 15'h57a0); // mode, rtr, id, length, crc
697 16 mohor
    fifo_info;
698
    read_overrun_info(0, 15);
699
    $display("\n\n");
700
 
701
    release_rx_buffer;
702
    read_receive_buffer;
703
    fifo_info;
704
 
705
    release_rx_buffer;
706
    read_receive_buffer;
707
    fifo_info;
708
 
709
    release_rx_buffer;
710
    read_receive_buffer;
711
    fifo_info;
712
 
713
    release_rx_buffer;
714
    read_receive_buffer;
715
    fifo_info;
716
 
717
    release_rx_buffer;
718
    read_receive_buffer;
719
    fifo_info;
720
 
721
    release_rx_buffer;
722
    read_receive_buffer;
723
    fifo_info;
724
 
725
    release_rx_buffer;
726
    read_receive_buffer;
727
    fifo_info;
728
 
729
    release_rx_buffer;
730
    read_receive_buffer;
731
    fifo_info;
732
 
733
    release_rx_buffer;
734
    read_receive_buffer;
735
    fifo_info;
736
 
737
    release_rx_buffer;
738
    read_receive_buffer;
739
    fifo_info;
740
 
741
    release_rx_buffer;
742
    read_receive_buffer;
743
    fifo_info;
744
 
745
    release_rx_buffer;
746
    read_receive_buffer;
747
    fifo_info;
748
 
749
    release_rx_buffer;
750
    read_receive_buffer;
751
    fifo_info;
752
 
753
  end
754
endtask
755
 
756
 
757
 
758 20 mohor
task test_full_fifo_ext;
759
  begin
760
    release_rx_buffer;
761
    $display("\n\n");
762
    read_receive_buffer;
763
    fifo_info;
764
 
765
    receive_frame(1, 0, 29'h14d60246, 4'h0, 15'h6f54); // mode, rtr, id, length, crc
766
    read_receive_buffer;
767
    fifo_info;
768
    receive_frame(1, 0, 29'h14d60246, 4'h1, 15'h6d38); // mode, rtr, id, length, crc
769
    read_receive_buffer;
770
    fifo_info;
771
    receive_frame(1, 0, 29'h14d60246, 4'h2, 15'h053e); // mode, rtr, id, length, crc
772
    fifo_info;
773
    read_receive_buffer;
774
    receive_frame(1, 0, 29'h14d60246, 4'h3, 15'h5262); // mode, rtr, id, length, crc
775
    fifo_info;
776
    receive_frame(1, 0, 29'h14d60246, 4'h4, 15'h4bba); // mode, rtr, id, length, crc
777
    fifo_info;
778
    receive_frame(1, 0, 29'h14d60246, 4'h5, 15'h4d7d); // mode, rtr, id, length, crc
779
    fifo_info;
780
    receive_frame(1, 0, 29'h14d60246, 4'h6, 15'h6f40); // mode, rtr, id, length, crc
781
    fifo_info;
782
    receive_frame(1, 0, 29'h14d60246, 4'h7, 15'h1730); // mode, rtr, id, length, crc
783
    fifo_info;
784
    read_overrun_info(0, 10);
785
 
786
    release_rx_buffer;
787
    release_rx_buffer;
788
    fifo_info;
789
    receive_frame(1, 0, 29'h14d60246, 4'h8, 15'h2f7a); // mode, rtr, id, length, crc
790
    fifo_info;
791
    read_overrun_info(0, 15);
792
    $display("\n\n");
793
 
794
    release_rx_buffer;
795
    read_receive_buffer;
796
    fifo_info;
797
 
798
    release_rx_buffer;
799
    read_receive_buffer;
800
    fifo_info;
801
 
802
    release_rx_buffer;
803
    read_receive_buffer;
804
    fifo_info;
805
 
806
    release_rx_buffer;
807
    read_receive_buffer;
808
    fifo_info;
809
 
810
    release_rx_buffer;
811
    read_receive_buffer;
812
    fifo_info;
813
 
814
    release_rx_buffer;
815
    read_receive_buffer;
816
    fifo_info;
817
 
818
    release_rx_buffer;
819
    read_receive_buffer;
820
    fifo_info;
821
 
822
  end
823
endtask
824
 
825
 
826
 
827 16 mohor
task initialize_fifo;
828
  integer i;
829
  begin
830
    for (i=0; i<32; i=i+1)
831
      begin
832
        can_testbench.i_can_top.i_can_bsp.i_can_fifo.length_info[i] = 0;
833
        can_testbench.i_can_top.i_can_bsp.i_can_fifo.overrun_info[i] = 0;
834
      end
835
 
836
    for (i=0; i<64; i=i+1)
837
      begin
838
        can_testbench.i_can_top.i_can_bsp.i_can_fifo.fifo[i] = 0;
839
      end
840
 
841
    $display("(%0t) Fifo initialized", $time);
842
  end
843
endtask
844
 
845
 
846
task read_overrun_info;
847
  input [4:0] start_addr;
848
  input [4:0] end_addr;
849
  integer i;
850
  begin
851
    for (i=start_addr; i<=end_addr; i=i+1)
852
      begin
853
        $display("len[0x%0x]=0x%0x", i, can_testbench.i_can_top.i_can_bsp.i_can_fifo.length_info[i]);
854
        $display("overrun[0x%0x]=0x%0x\n", i, can_testbench.i_can_top.i_can_bsp.i_can_fifo.overrun_info[i]);
855
      end
856
  end
857
endtask
858
 
859
 
860 24 mohor
task fifo_info;   // Displaying how many packets and how many bytes are in fifo. Not working when wr_info_pointer is smaller than rd_info_pointer.
861 16 mohor
  begin
862 24 mohor
      $display("(%0t) Currently %0d bytes in fifo (%0d packets)", $time, can_testbench.i_can_top.i_can_bsp.i_can_fifo.fifo_cnt,
863
      (can_testbench.i_can_top.i_can_bsp.i_can_fifo.wr_info_pointer - can_testbench.i_can_top.i_can_bsp.i_can_fifo.rd_info_pointer));
864 16 mohor
end
865
endtask
866
 
867
 
868 14 mohor
task read_register;
869
  input [7:0] reg_addr;
870
 
871
  begin
872
    @ (posedge clk);
873
    #1;
874
    addr = reg_addr;
875
    cs = 1;
876
    rw = 1;
877
    @ (posedge clk);
878 17 mohor
    $display("(%0t) Reading register [%0d] = 0x%0x", $time, addr, data_out);
879 14 mohor
    #1;
880
    addr = 'hz;
881
    cs = 0;
882
    rw = 'hz;
883
  end
884
endtask
885
 
886
 
887 2 mohor
task write_register;
888
  input [7:0] reg_addr;
889
  input [7:0] reg_data;
890
 
891
  begin
892
    @ (posedge clk);
893
    #1;
894
    addr = reg_addr;
895
    data_in = reg_data;
896
    cs = 1;
897
    rw = 0;
898
    @ (posedge clk);
899
    #1;
900
    addr = 'hz;
901
    data_in = 'hz;
902
    cs = 0;
903
    rw = 'hz;
904
  end
905
endtask
906 7 mohor
 
907
 
908 15 mohor
task read_receive_buffer;
909
  integer i;
910
  begin
911
    if(`CAN_CLOCK_DIVIDER_MODE)   // Extended mode
912
      begin
913 17 mohor
        for (i=8'd16; i<=8'd28; i=i+1)
914 15 mohor
          read_register(i);
915 16 mohor
        if (can_testbench.i_can_top.i_can_bsp.i_can_fifo.overrun_info[can_testbench.i_can_top.i_can_bsp.i_can_fifo.rd_info_pointer])
916
          $display("\nWARNING: This packet was received with overrun.");
917 15 mohor
      end
918
    else
919
      begin
920 17 mohor
        for (i=8'd20; i<=8'd29; i=i+1)
921 15 mohor
          read_register(i);
922 16 mohor
        if (can_testbench.i_can_top.i_can_bsp.i_can_fifo.overrun_info[can_testbench.i_can_top.i_can_bsp.i_can_fifo.rd_info_pointer])
923
          $display("\nWARNING: This packet was received with overrun.");
924 15 mohor
      end
925
  end
926
endtask
927
 
928
 
929
task release_rx_buffer;
930
  begin
931 17 mohor
    write_register(8'd1, 8'h4);
932 16 mohor
    $display("(%0t) Rx buffer released.", $time);
933
    repeat (2) @ (posedge clk);   // Time to decrement all the counters
934 15 mohor
  end
935
endtask
936
 
937
 
938 25 mohor
task tx_request;
939
  begin
940
    write_register(8'd1, 8'h1);
941
    $display("(%0t) Tx requested.", $time);
942
    repeat (2) @ (posedge clk);   // Time to decrement all the counters, etc.
943
  end
944
endtask
945
 
946
 
947 8 mohor
task test_synchronization;
948
  begin
949
    // Hard synchronization
950
    #1 rx=0;
951
    repeat (2*BRP) @ (posedge clk);
952
    repeat (8*BRP) @ (posedge clk);
953
    #1 rx=1;
954
    repeat (10*BRP) @ (posedge clk);
955
 
956
    // Resynchronization on time
957
    #1 rx=0;
958
    repeat (10*BRP) @ (posedge clk);
959
    #1 rx=1;
960
    repeat (10*BRP) @ (posedge clk);
961
 
962
    // Resynchronization late
963
    repeat (BRP) @ (posedge clk);
964
    repeat (BRP) @ (posedge clk);
965
    #1 rx=0;
966
    repeat (10*BRP) @ (posedge clk);
967
    #1 rx=1;
968
 
969
    // Resynchronization early
970
    repeat (8*BRP) @ (posedge clk);   // two frames too early
971
    #1 rx=0;
972
    repeat (10*BRP) @ (posedge clk);
973
    #1 rx=1;
974
    repeat (10*BRP) @ (posedge clk);
975
  end
976
endtask
977 7 mohor
 
978 8 mohor
 
979 9 mohor
task send_bit;
980
  input bit;
981
  integer cnt;
982
  begin
983
    #1 rx=bit;
984
    repeat ((`CAN_TIMING1_TSEG1 + `CAN_TIMING1_TSEG2 + 3)*BRP) @ (posedge clk);
985
  end
986
endtask
987
 
988
 
989 18 mohor
task receive_frame;           // CAN IP core receives frames
990 8 mohor
  input mode;
991 11 mohor
  input remote_trans_req;
992 9 mohor
  input [28:0] id;
993
  input  [3:0] length;
994 11 mohor
  input [14:0] crc;
995 25 mohor
 
996 15 mohor
  reg [117:0] data;
997 16 mohor
  reg         previous_bit;
998 25 mohor
  reg         stuff;
999
  reg         tmp;
1000
  reg         arbitration_lost;
1001
  integer     pointer;
1002
  integer     cnt;
1003
  integer     total_bits;
1004
  integer     stuff_cnt;
1005 24 mohor
 
1006 15 mohor
  begin
1007 9 mohor
 
1008 16 mohor
    stuff_cnt = 1;
1009
    stuff = 0;
1010 15 mohor
 
1011
    if(mode)          // Extended format
1012 20 mohor
      data = {id[28:18], 1'b1, 1'b1, id[17:0], remote_trans_req, 2'h0, length};
1013 15 mohor
    else              // Standard format
1014
      data = {id[10:0], remote_trans_req, 1'b0, 1'b0, length};
1015
 
1016 19 mohor
    if (~remote_trans_req)
1017 15 mohor
      begin
1018 19 mohor
        if(length)    // Send data if length is > 0
1019
          begin
1020
            for (cnt=1; cnt<=(2*length); cnt=cnt+1)  // data   (we are sending nibbles)
1021
              data = {data[113:0], cnt[3:0]};
1022
          end
1023 15 mohor
      end
1024
 
1025
    // Adding CRC
1026
    data = {data[104:0], crc[14:0]};
1027
 
1028
 
1029
    // Calculating pointer that points to the bit that will be send
1030 19 mohor
    if (remote_trans_req)
1031
      begin
1032
        if(mode)          // Extended format
1033 20 mohor
          pointer = 52;
1034 19 mohor
        else              // Standard format
1035
          pointer = 32;
1036
      end
1037
    else
1038
      begin
1039
        if(mode)          // Extended format
1040 20 mohor
          pointer = 52 + 8 * length;
1041 19 mohor
        else              // Standard format
1042
          pointer = 32 + 8 * length;
1043
      end
1044 15 mohor
 
1045
    // This is how many bits we need to shift
1046
    total_bits = pointer;
1047
 
1048 24 mohor
    // Waiting until previous msg is finished before sending another one
1049 25 mohor
    wait (~can_testbench.i_can_top.i_can_bsp.error_frame & ~can_testbench.i_can_top.i_can_bsp.rx_inter & ~can_testbench.i_can_top.i_can_bsp.tx_state);
1050
    arbitration_lost = 0;
1051 15 mohor
 
1052 9 mohor
    send_bit(0);                        // SOF
1053 16 mohor
    previous_bit = 0;
1054 9 mohor
 
1055 25 mohor
    fork
1056 9 mohor
 
1057 25 mohor
    begin
1058
      while (~arbitration_lost)
1059
        begin
1060
          for (cnt=0; cnt<=total_bits; cnt=cnt+1)
1061
            begin
1062
              if (stuff_cnt == 5)
1063
                begin
1064
                  stuff_cnt = 1;
1065
                  total_bits = total_bits + 1;
1066
                  stuff = 1;
1067
                  tmp = ~data[pointer+1];
1068
                  send_bit(~data[pointer+1]);
1069
                  previous_bit = ~data[pointer+1];
1070
                end
1071
              else
1072
                begin
1073
                  if (data[pointer] == previous_bit)
1074
                    stuff_cnt <= stuff_cnt + 1;
1075
                  else
1076
                    stuff_cnt <= 1;
1077
 
1078
                  stuff = 0;
1079
                  tmp = data[pointer];
1080
                  send_bit(data[pointer]);
1081
                  previous_bit = data[pointer];
1082
                  pointer = pointer - 1;
1083
                end
1084
              if (arbitration_lost)
1085
                cnt=total_bits+1;         // Exit the for loop
1086
            end
1087
            arbitration_lost = 1; // At the end we exit the while loop
1088
 
1089
            // Nothing send after the data (just recessive bit)
1090
            repeat (13) send_bit(1);         // CRC delimiter + ack + ack delimiter + EOF + intermission= 1 + 1 + 1 + 7 + 3
1091
        end
1092
    end
1093
 
1094
    begin
1095
      while (~arbitration_lost)
1096
        begin
1097
          #1 wait (can_testbench.i_can_top.sample_point);
1098
          if (mode)
1099
            begin
1100
              if (cnt<32 & tmp & (~rx_and_tx))
1101
                begin
1102
                  arbitration_lost = 1;
1103
                  rx = 1;       // Only recessive is send from now on.
1104
                end
1105
            end
1106
          else
1107
            begin
1108
              if (cnt<12 & tmp & (~rx_and_tx))
1109
                begin
1110
                  arbitration_lost = 1;
1111
                  rx = 1;       // Only recessive is send from now on.
1112
                end
1113
            end
1114
        end
1115
    end
1116
 
1117
    join
1118
 
1119 8 mohor
  end
1120
endtask
1121
 
1122
 
1123 16 mohor
 
1124 11 mohor
// State machine monitor (btl)
1125 7 mohor
always @ (posedge clk)
1126
begin
1127
  if(can_testbench.i_can_top.i_can_btl.go_sync & can_testbench.i_can_top.i_can_btl.go_seg1 | can_testbench.i_can_top.i_can_btl.go_sync & can_testbench.i_can_top.i_can_btl.go_seg2 |
1128
     can_testbench.i_can_top.i_can_btl.go_seg1 & can_testbench.i_can_top.i_can_btl.go_seg2)
1129 10 mohor
    begin
1130
      $display("(%0t) ERROR multiple go_sync, go_seg1 or go_seg2 occurance\n\n", $time);
1131
      #1000;
1132
      $stop;
1133
    end
1134 7 mohor
 
1135
  if(can_testbench.i_can_top.i_can_btl.sync & can_testbench.i_can_top.i_can_btl.seg1 | can_testbench.i_can_top.i_can_btl.sync & can_testbench.i_can_top.i_can_btl.seg2 |
1136
     can_testbench.i_can_top.i_can_btl.seg1 & can_testbench.i_can_top.i_can_btl.seg2)
1137 10 mohor
    begin
1138
      $display("(%0t) ERROR multiple sync, seg1 or seg2 occurance\n\n", $time);
1139
      #1000;
1140
      $stop;
1141
    end
1142 7 mohor
end
1143
 
1144 10 mohor
/* stuff_error monitor (bsp)
1145
always @ (posedge clk)
1146
begin
1147
  if(can_testbench.i_can_top.i_can_bsp.stuff_error)
1148
    begin
1149
      $display("\n\n(%0t) Stuff error occured in can_bsp.v file\n\n", $time);
1150
      $stop;                                      After everything is finished add another condition (something like & (~idle)) and enable stop
1151
    end
1152
end
1153
*/
1154
 
1155 18 mohor
//
1156 16 mohor
// CRC monitor (used until proper CRC generation is used in testbench
1157
always @ (posedge clk)
1158
begin
1159 28 mohor
  if (can_testbench.i_can_top.i_can_bsp.crc_err)
1160 16 mohor
    $display("Calculated crc = 0x%0x, crc_in = 0x%0x", can_testbench.i_can_top.i_can_bsp.calculated_crc, can_testbench.i_can_top.i_can_bsp.crc_in);
1161
end
1162 18 mohor
//
1163 10 mohor
 
1164 16 mohor
 
1165
 
1166
 
1167
/*
1168
// overrun monitor
1169
always @ (posedge clk)
1170
begin
1171
  if (can_testbench.i_can_top.i_can_bsp.i_can_fifo.wr & can_testbench.i_can_top.i_can_bsp.i_can_fifo.fifo_full)
1172
    $display("(%0t)overrun", $time);
1173
end
1174
*/
1175
 
1176
 
1177 22 mohor
// form error monitor
1178
always @ (posedge clk)
1179
begin
1180 28 mohor
  if (can_testbench.i_can_top.i_can_bsp.form_err)
1181 22 mohor
    $display("\n\n(%0t) ERROR: form_error\n\n", $time);
1182
end
1183
//
1184
 
1185
 
1186 2 mohor
endmodule
1187 16 mohor
 

powered by: WebSVN 2.1.0

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