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

Subversion Repositories can

[/] [can/] [tags/] [complete_1/] [bench/] [verilog/] [can_testbench.v] - Blame information for rev 34

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

powered by: WebSVN 2.1.0

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