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 28

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

powered by: WebSVN 2.1.0

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