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

Subversion Repositories can

[/] [can/] [tags/] [rel_6/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 161

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  can_bsp.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 90 mohor
// Revision 1.32  2003/06/17 14:28:32  mohor
54
// Form error was detected when stuff bit occured at the end of crc.
55
//
56 80 mohor
// Revision 1.31  2003/06/16 14:31:29  tadejm
57
// Bit stuffing corrected when stuffing comes at the end of the crc.
58
//
59 79 tadejm
// Revision 1.30  2003/06/16 13:57:58  mohor
60
// tx_point generated one clk earlier. rx_i registered. Data corrected when
61
// using extended mode.
62
//
63 78 mohor
// Revision 1.29  2003/06/11 14:21:35  mohor
64
// When switching to tx, sync stage is overjumped.
65
//
66 75 mohor
// Revision 1.28  2003/03/01 22:53:33  mohor
67
// Actel APA ram supported.
68
//
69 48 mohor
// Revision 1.27  2003/02/20 00:26:02  mohor
70
// When a dominant bit was detected at the third bit of the intermission and
71
// node had a message to transmit, bit_stuff error could occur. Fixed.
72
//
73 45 mohor
// Revision 1.26  2003/02/19 23:21:54  mohor
74
// When bit error occured while active error flag was transmitted, counter was
75
// not incremented.
76
//
77 44 mohor
// Revision 1.25  2003/02/19 14:44:03  mohor
78
// CAN core finished. Host interface added. Registers finished.
79
// Synchronization to the wishbone finished.
80
//
81 39 mohor
// Revision 1.24  2003/02/18 00:10:15  mohor
82
// Most of the registers added. Registers "arbitration lost capture", "error code
83
// capture" + few more still need to be added.
84
//
85 36 mohor
// Revision 1.23  2003/02/14 20:17:01  mohor
86
// Several registers added. Not finished, yet.
87
//
88 35 mohor
// Revision 1.22  2003/02/12 14:23:59  mohor
89
// abort_tx added. Bit destuff fixed.
90
//
91 32 mohor
// Revision 1.21  2003/02/11 00:56:06  mohor
92
// Wishbone interface added.
93
//
94 31 mohor
// Revision 1.20  2003/02/10 16:02:11  mohor
95
// CAN is working according to the specification. WB interface and more
96
// registers (status, IRQ, ...) needs to be added.
97
//
98 30 mohor
// Revision 1.19  2003/02/09 18:40:29  mohor
99
// Overload fixed. Hard synchronization also enabled at the last bit of
100
// interframe.
101
//
102 29 mohor
// Revision 1.18  2003/02/09 02:24:33  mohor
103
// Bosch license warning added. Error counters finished. Overload frames
104
// still need to be fixed.
105
//
106 28 mohor
// Revision 1.17  2003/02/04 17:24:41  mohor
107
// Backup.
108
//
109 26 mohor
// Revision 1.16  2003/02/04 14:34:52  mohor
110
// *** empty log message ***
111
//
112 25 mohor
// Revision 1.15  2003/01/31 01:13:37  mohor
113
// backup.
114
//
115 24 mohor
// Revision 1.14  2003/01/16 13:36:19  mohor
116
// Form error supported. When receiving messages, last bit of the end-of-frame
117
// does not generate form error. Receiver goes to the idle mode one bit sooner.
118
// (CAN specification ver 2.0, part B, page 57).
119
//
120 22 mohor
// Revision 1.13  2003/01/15 21:59:45  mohor
121
// Data is stored to fifo at the end of ack stage.
122
//
123 21 mohor
// Revision 1.12  2003/01/15 21:05:11  mohor
124
// CRC checking fixed (when bitstuff occurs at the end of a CRC sequence).
125
//
126 20 mohor
// Revision 1.11  2003/01/15 14:40:23  mohor
127 31 mohor
// RX state machine fixed to receive "remote request" frames correctly.
128
// No data bytes are written to fifo when such frames are received.
129 20 mohor
//
130 19 mohor
// Revision 1.10  2003/01/15 13:16:47  mohor
131 31 mohor
// When a frame with "remote request" is received, no data is stored to
132
// fifo, just the frame information (identifier, ...). Data length that
133
// is stored is the received data length and not the actual data length
134
// that is stored to fifo.
135 19 mohor
//
136 18 mohor
// Revision 1.9  2003/01/14 12:19:35  mohor
137
// rx_fifo is now working.
138
//
139 16 mohor
// Revision 1.8  2003/01/10 17:51:33  mohor
140
// Temporary version (backup).
141
//
142 15 mohor
// Revision 1.7  2003/01/09 21:54:45  mohor
143
// rx fifo added. Not 100 % verified, yet.
144
//
145 14 mohor
// Revision 1.6  2003/01/09 14:46:58  mohor
146
// Temporary files (backup).
147
//
148 13 mohor
// Revision 1.5  2003/01/08 13:30:31  mohor
149
// Temp version.
150
//
151 12 mohor
// Revision 1.4  2003/01/08 02:10:53  mohor
152
// Acceptance filter added.
153
//
154 11 mohor
// Revision 1.3  2002/12/28 04:13:23  mohor
155
// Backup version.
156
//
157 10 mohor
// Revision 1.2  2002/12/27 00:12:52  mohor
158
// Header changed, testbench improved to send a frame (crc still missing).
159
//
160 9 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
161
// Initial
162 2 mohor
//
163
//
164 9 mohor
//
165 2 mohor
 
166
// synopsys translate_off
167
`include "timescale.v"
168
// synopsys translate_on
169
`include "can_defines.v"
170
 
171
module can_bsp
172
(
173
  clk,
174 10 mohor
  rst,
175
 
176
  sample_point,
177
  sampled_bit,
178
  sampled_bit_q,
179 24 mohor
  tx_point,
180 11 mohor
  hard_sync,
181
 
182 14 mohor
  addr,
183 35 mohor
  data_in,
184 14 mohor
  data_out,
185 48 mohor
  fifo_selected,
186 35 mohor
 
187 14 mohor
 
188
 
189 11 mohor
  /* Mode register */
190 10 mohor
  reset_mode,
191 36 mohor
  listen_only_mode,
192 11 mohor
  acceptance_filter_mode,
193 36 mohor
  self_test_mode,
194 11 mohor
 
195 15 mohor
  /* Command register */
196
  release_buffer,
197 25 mohor
  tx_request,
198 32 mohor
  abort_tx,
199 36 mohor
  self_rx_request,
200
  single_shot_transmission,
201 15 mohor
 
202 39 mohor
  /* Arbitration Lost Capture Register */
203
  read_arbitration_lost_capture_reg,
204
 
205
  /* Error Code Capture Register */
206
  read_error_code_capture_reg,
207
  error_capture_code,
208
 
209 35 mohor
  /* Error Warning Limit register */
210
  error_warning_limit,
211
 
212
  /* Rx Error Counter register */
213
  we_rx_err_cnt,
214
 
215
  /* Tx Error Counter register */
216
  we_tx_err_cnt,
217
 
218 15 mohor
  /* Clock Divider register */
219 11 mohor
  extended_mode,
220
 
221
  rx_idle,
222 24 mohor
  transmitting,
223 29 mohor
  last_bit_of_inter,
224 35 mohor
  set_reset_mode,
225
  node_bus_off,
226
  error_status,
227
  rx_err_cnt,
228
  tx_err_cnt,
229
  transmit_status,
230
  receive_status,
231
  tx_successful,
232
  need_to_tx,
233
  overrun,
234
  info_empty,
235 39 mohor
  set_bus_error_irq,
236
  set_arbitration_lost_irq,
237
  arbitration_lost_capture,
238 36 mohor
  node_error_passive,
239
  node_error_active,
240 39 mohor
  rx_message_counter,
241 11 mohor
 
242 35 mohor
 
243 36 mohor
 
244 11 mohor
  /* This section is for BASIC and EXTENDED mode */
245
  /* Acceptance code register */
246
  acceptance_code_0,
247
 
248
  /* Acceptance mask register */
249
  acceptance_mask_0,
250
  /* End: This section is for BASIC and EXTENDED mode */
251 10 mohor
 
252 11 mohor
  /* This section is for EXTENDED mode */
253
  /* Acceptance code register */
254
  acceptance_code_1,
255
  acceptance_code_2,
256
  acceptance_code_3,
257 10 mohor
 
258 11 mohor
  /* Acceptance mask register */
259
  acceptance_mask_1,
260
  acceptance_mask_2,
261 18 mohor
  acceptance_mask_3,
262 11 mohor
  /* End: This section is for EXTENDED mode */
263 10 mohor
 
264 18 mohor
  /* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
265
  tx_data_0,
266
  tx_data_1,
267
  tx_data_2,
268
  tx_data_3,
269
  tx_data_4,
270
  tx_data_5,
271
  tx_data_6,
272
  tx_data_7,
273
  tx_data_8,
274
  tx_data_9,
275
  tx_data_10,
276
  tx_data_11,
277 24 mohor
  tx_data_12,
278 18 mohor
  /* End: Tx data registers */
279 24 mohor
 
280
  /* Tx signal */
281 28 mohor
  tx,
282
  tx_oen
283 18 mohor
 
284 2 mohor
);
285
 
286
parameter Tp = 1;
287
 
288 10 mohor
input         clk;
289
input         rst;
290
input         sample_point;
291
input         sampled_bit;
292
input         sampled_bit_q;
293 24 mohor
input         tx_point;
294 11 mohor
input         hard_sync;
295 14 mohor
input   [7:0] addr;
296 35 mohor
input   [7:0] data_in;
297 14 mohor
output  [7:0] data_out;
298 48 mohor
input         fifo_selected;
299 11 mohor
 
300 14 mohor
 
301 10 mohor
input         reset_mode;
302 36 mohor
input         listen_only_mode;
303 11 mohor
input         acceptance_filter_mode;
304
input         extended_mode;
305 36 mohor
input         self_test_mode;
306 2 mohor
 
307 35 mohor
 
308 15 mohor
/* Command register */
309
input         release_buffer;
310 25 mohor
input         tx_request;
311 32 mohor
input         abort_tx;
312 36 mohor
input         self_rx_request;
313
input         single_shot_transmission;
314 11 mohor
 
315 39 mohor
/* Arbitration Lost Capture Register */
316
input         read_arbitration_lost_capture_reg;
317
 
318
/* Error Code Capture Register */
319
input         read_error_code_capture_reg;
320
output  [7:0] error_capture_code;
321
 
322 35 mohor
/* Error Warning Limit register */
323
input   [7:0] error_warning_limit;
324
 
325
/* Rx Error Counter register */
326
input         we_rx_err_cnt;
327
 
328
/* Tx Error Counter register */
329
input         we_tx_err_cnt;
330
 
331 10 mohor
output        rx_idle;
332 24 mohor
output        transmitting;
333 29 mohor
output        last_bit_of_inter;
334 35 mohor
output        set_reset_mode;
335
output        node_bus_off;
336
output        error_status;
337
output  [8:0] rx_err_cnt;
338
output  [8:0] tx_err_cnt;
339
output        transmit_status;
340
output        receive_status;
341
output        tx_successful;
342
output        need_to_tx;
343
output        overrun;
344
output        info_empty;
345 39 mohor
output        set_bus_error_irq;
346
output        set_arbitration_lost_irq;
347
output  [4:0] arbitration_lost_capture;
348 36 mohor
output        node_error_passive;
349
output        node_error_active;
350 39 mohor
output  [6:0] rx_message_counter;
351 2 mohor
 
352 29 mohor
 
353 11 mohor
/* This section is for BASIC and EXTENDED mode */
354
/* Acceptance code register */
355
input   [7:0] acceptance_code_0;
356
 
357
/* Acceptance mask register */
358
input   [7:0] acceptance_mask_0;
359
 
360
/* End: This section is for BASIC and EXTENDED mode */
361
 
362
 
363
/* This section is for EXTENDED mode */
364
/* Acceptance code register */
365
input   [7:0] acceptance_code_1;
366
input   [7:0] acceptance_code_2;
367
input   [7:0] acceptance_code_3;
368
 
369
/* Acceptance mask register */
370
input   [7:0] acceptance_mask_1;
371
input   [7:0] acceptance_mask_2;
372
input   [7:0] acceptance_mask_3;
373
/* End: This section is for EXTENDED mode */
374
 
375 24 mohor
/* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
376 18 mohor
input   [7:0] tx_data_0;
377
input   [7:0] tx_data_1;
378
input   [7:0] tx_data_2;
379
input   [7:0] tx_data_3;
380
input   [7:0] tx_data_4;
381
input   [7:0] tx_data_5;
382
input   [7:0] tx_data_6;
383
input   [7:0] tx_data_7;
384
input   [7:0] tx_data_8;
385
input   [7:0] tx_data_9;
386
input   [7:0] tx_data_10;
387
input   [7:0] tx_data_11;
388
input   [7:0] tx_data_12;
389 24 mohor
/* End: Tx data registers */
390 11 mohor
 
391 24 mohor
/* Tx signal */
392
output        tx;
393 28 mohor
output        tx_oen;
394 11 mohor
 
395 10 mohor
reg           reset_mode_q;
396
reg     [5:0] bit_cnt;
397 2 mohor
 
398 10 mohor
reg     [3:0] data_len;
399
reg    [28:0] id;
400
reg     [2:0] bit_stuff_cnt;
401 25 mohor
reg     [2:0] bit_stuff_cnt_tx;
402
reg           tx_point_q;
403 10 mohor
 
404
reg           rx_idle;
405
reg           rx_id1;
406
reg           rx_rtr1;
407
reg           rx_ide;
408
reg           rx_id2;
409
reg           rx_rtr2;
410
reg           rx_r1;
411
reg           rx_r0;
412
reg           rx_dlc;
413
reg           rx_data;
414
reg           rx_crc;
415 11 mohor
reg           rx_crc_lim;
416 10 mohor
reg           rx_ack;
417 11 mohor
reg           rx_ack_lim;
418 10 mohor
reg           rx_eof;
419 24 mohor
reg           rx_inter;
420 45 mohor
reg           go_early_tx_latched;
421 10 mohor
 
422 19 mohor
reg           rtr1;
423
reg           ide;
424
reg           rtr2;
425
reg    [14:0] crc_in;
426
 
427 24 mohor
reg     [7:0] tmp_data;
428
reg     [7:0] tmp_fifo [0:7];
429
reg           write_data_to_tmp_fifo;
430
reg     [2:0] byte_cnt;
431
reg           bit_stuff_cnt_en;
432 35 mohor
reg           bit_stuff_cnt_tx_en;
433 11 mohor
reg           crc_enable;
434
 
435 10 mohor
reg     [2:0] eof_cnt;
436 28 mohor
reg     [2:0] passive_cnt;
437
 
438 24 mohor
reg           transmitting;
439 10 mohor
 
440 24 mohor
reg           error_frame;
441 28 mohor
reg           error_frame_q;
442 24 mohor
reg           enable_error_cnt2;
443
reg     [2:0] error_cnt1;
444
reg     [2:0] error_cnt2;
445 29 mohor
reg     [2:0] delayed_dominant_cnt;
446
reg           enable_overload_cnt2;
447 30 mohor
reg           overload_frame;
448
reg           overload_frame_blocked;
449 29 mohor
reg     [2:0] overload_cnt1;
450
reg     [2:0] overload_cnt2;
451 24 mohor
reg           tx;
452 28 mohor
reg           crc_err;
453 24 mohor
 
454 39 mohor
reg           arbitration_lost;
455
reg           arbitration_lost_q;
456
reg     [4:0] arbitration_lost_capture;
457
reg           arbitration_cnt_en;
458
reg           arbitration_blocked;
459 25 mohor
reg           tx_q;
460
 
461 28 mohor
reg           need_to_tx;   // When the CAN core has something to transmit and a dominant bit is sampled at the third bit
462 25 mohor
reg     [3:0] data_cnt;     // Counting the data bytes that are written to FIFO
463
reg     [2:0] header_cnt;   // Counting header length
464
reg           wr_fifo;      // Write data and header to 64-byte fifo
465
reg     [7:0] data_for_fifo;// Multiplexed data that is stored to 64-byte fifo
466
 
467
reg     [5:0] tx_pointer;
468
reg           tx_bit;
469
reg           tx_state;
470 28 mohor
reg           transmitter;
471 25 mohor
reg           finish_msg;
472
 
473 35 mohor
reg     [8:0] rx_err_cnt;
474
reg     [8:0] tx_err_cnt;
475 28 mohor
reg           rx_err_cnt_blocked;
476 35 mohor
reg     [3:0] bus_free_cnt;
477
reg           bus_free_cnt_en;
478
reg           bus_free;
479
reg           waiting_for_bus_free;
480 28 mohor
 
481
reg           node_error_passive;
482
reg           node_bus_off;
483 35 mohor
reg           node_bus_off_q;
484 28 mohor
reg           ack_err_latched;
485
reg           bit_err_latched;
486
reg           stuff_err_latched;
487
reg           form_err_latched;
488
reg           rule3_exc1_1;
489
reg           rule3_exc1_2;
490
reg           rule3_exc2;
491
reg           suspend;
492
reg           susp_cnt_en;
493
reg     [2:0] susp_cnt;
494
reg           error_flag_over_blocked;
495
 
496 39 mohor
reg     [7:0] error_capture_code;
497
reg     [7:6] error_capture_code_type;
498
reg           error_capture_code_blocked;
499
 
500
wire    [4:0] error_capture_code_segment;
501
wire          error_capture_code_direction;
502
 
503 28 mohor
wire          bit_de_stuff;
504
wire          bit_de_stuff_tx;
505
 
506 44 mohor
wire          rule5;
507 28 mohor
 
508
/* Rx state machine */
509
wire          go_rx_idle;
510
wire          go_rx_id1;
511
wire          go_rx_rtr1;
512
wire          go_rx_ide;
513
wire          go_rx_id2;
514
wire          go_rx_rtr2;
515
wire          go_rx_r1;
516
wire          go_rx_r0;
517
wire          go_rx_dlc;
518
wire          go_rx_data;
519
wire          go_rx_crc;
520
wire          go_rx_crc_lim;
521
wire          go_rx_ack;
522
wire          go_rx_ack_lim;
523
wire          go_rx_eof;
524
wire          go_overload_frame;
525
wire          go_rx_inter;
526 39 mohor
wire          go_error_frame;
527 28 mohor
 
528
wire          go_crc_enable;
529
wire          rst_crc_enable;
530
 
531
wire          bit_de_stuff_set;
532
wire          bit_de_stuff_reset;
533
 
534
wire          go_early_tx;
535
wire          go_tx;
536
 
537
wire   [14:0] calculated_crc;
538
wire   [15:0] r_calculated_crc;
539
wire          remote_rq;
540
wire    [3:0] limited_data_len;
541
wire          form_err;
542
 
543
wire          error_frame_ended;
544
wire          overload_frame_ended;
545
wire          bit_err;
546
wire          ack_err;
547
wire          stuff_err;
548 29 mohor
                                    // of intermission, it starts reading the identifier (and transmitting its own).
549
wire          overload_needed = 0;  // When receiver is busy, it needs to send overload frame. Only 2 overload frames are allowed to
550 36 mohor
                                    // be send in a row. This is not implemented because host can not send an overload request.
551 28 mohor
 
552 29 mohor
wire          id_ok;                // If received ID matches ID set in registers
553
wire          no_byte0;             // There is no byte 0 (RTR bit set to 1 or DLC field equal to 0). Signal used for acceptance filter.
554
wire          no_byte1;             // There is no byte 1 (RTR bit set to 1 or DLC field equal to 1). Signal used for acceptance filter.
555 28 mohor
 
556 25 mohor
wire    [2:0] header_len;
557
wire          storing_header;
558
wire    [3:0] limited_data_len_minus1;
559
wire          reset_wr_fifo;
560 28 mohor
wire          err;
561 25 mohor
 
562 28 mohor
wire          arbitration_field;
563 25 mohor
 
564 28 mohor
wire   [18:0] basic_chain;
565
wire   [63:0] basic_chain_data;
566
wire   [18:0] extended_chain_std;
567
wire   [38:0] extended_chain_ext;
568 78 mohor
wire   [63:0] extended_chain_data_std;
569
wire   [63:0] extended_chain_data_ext;
570 28 mohor
 
571
wire          rst_tx_pointer;
572
 
573
wire    [7:0] r_tx_data_0;
574
wire    [7:0] r_tx_data_1;
575
wire    [7:0] r_tx_data_2;
576
wire    [7:0] r_tx_data_3;
577
wire    [7:0] r_tx_data_4;
578
wire    [7:0] r_tx_data_5;
579
wire    [7:0] r_tx_data_6;
580
wire    [7:0] r_tx_data_7;
581
wire    [7:0] r_tx_data_8;
582
wire    [7:0] r_tx_data_9;
583
wire    [7:0] r_tx_data_10;
584
wire    [7:0] r_tx_data_11;
585
wire    [7:0] r_tx_data_12;
586
 
587
wire          send_ack;
588
wire          bit_err_exc1;
589
wire          bit_err_exc2;
590
wire          bit_err_exc3;
591
wire          bit_err_exc4;
592 30 mohor
wire          bit_err_exc5;
593 28 mohor
wire          error_flag_over;
594 29 mohor
wire          overload_flag_over;
595 28 mohor
 
596
 
597 35 mohor
assign go_rx_idle     =                   sample_point &  sampled_bit & last_bit_of_inter | bus_free & (~node_bus_off);
598 29 mohor
assign go_rx_id1      =                   sample_point &  (~sampled_bit) & (rx_idle | last_bit_of_inter);
599 11 mohor
assign go_rx_rtr1     = (~bit_de_stuff) & sample_point &  rx_id1  & (bit_cnt == 10);
600
assign go_rx_ide      = (~bit_de_stuff) & sample_point &  rx_rtr1;
601
assign go_rx_id2      = (~bit_de_stuff) & sample_point &  rx_ide  &   sampled_bit;
602
assign go_rx_rtr2     = (~bit_de_stuff) & sample_point &  rx_id2  & (bit_cnt == 17);
603
assign go_rx_r1       = (~bit_de_stuff) & sample_point &  rx_rtr2;
604
assign go_rx_r0       = (~bit_de_stuff) & sample_point & (rx_ide  & (~sampled_bit) | rx_r1);
605
assign go_rx_dlc      = (~bit_de_stuff) & sample_point &  rx_r0;
606 19 mohor
assign go_rx_data     = (~bit_de_stuff) & sample_point &  rx_dlc  & (bit_cnt == 3) &  (sampled_bit   |   (|data_len[2:0])) & (~remote_rq);
607
assign go_rx_crc      = (~bit_de_stuff) & sample_point & (rx_dlc  & (bit_cnt == 3) & ((~sampled_bit) & (~(|data_len[2:0])) | remote_rq) |
608
                                                          rx_data & (bit_cnt == ((limited_data_len<<3) - 1'b1)));
609 20 mohor
assign go_rx_crc_lim  = (~bit_de_stuff) & sample_point &  rx_crc  & (bit_cnt == 14);
610 79 tadejm
assign go_rx_ack      = (~bit_de_stuff) & sample_point &  rx_crc_lim;
611 11 mohor
assign go_rx_ack_lim  =                   sample_point &  rx_ack;
612 35 mohor
assign go_rx_eof      =                   sample_point &  rx_ack_lim;
613 30 mohor
assign go_rx_inter    =                 ((sample_point &  rx_eof  & (eof_cnt == 6)) | error_frame_ended | overload_frame_ended) & (~overload_needed);
614 10 mohor
 
615 28 mohor
assign go_error_frame = (form_err | stuff_err | bit_err | ack_err | (crc_err & go_rx_eof));
616 24 mohor
assign error_frame_ended = (error_cnt2 == 7) & tx_point;
617 29 mohor
assign overload_frame_ended = (overload_cnt2 == 7) & tx_point;
618 24 mohor
 
619 30 mohor
assign go_overload_frame = (   ((sample_point &  rx_eof  & (eof_cnt == 6)) | error_frame_ended | overload_frame_ended) & overload_needed |
620
                               sample_point & (~sampled_bit) & rx_inter & (bit_cnt < 2)                                                  |
621
                               sample_point & (~sampled_bit) & ((error_cnt2 == 7) | (overload_cnt2 == 7))
622
                           )
623
                           & (~overload_frame_blocked)
624
                           ;
625 24 mohor
 
626 25 mohor
 
627
assign go_crc_enable  = hard_sync | go_tx;
628 11 mohor
assign rst_crc_enable = go_rx_crc;
629 10 mohor
 
630 32 mohor
assign bit_de_stuff_set   = go_rx_id1 & (~go_error_frame);
631 79 tadejm
assign bit_de_stuff_reset = go_rx_ack | reset_mode | go_error_frame | go_overload_frame;
632 11 mohor
 
633 19 mohor
assign remote_rq = ((~ide) & rtr1) | (ide & rtr2);
634
assign limited_data_len = (data_len < 8)? data_len : 4'h8;
635 11 mohor
 
636 36 mohor
assign ack_err = rx_ack & sample_point & sampled_bit & tx_state & (~self_test_mode);
637 39 mohor
assign bit_err = (tx_state | error_frame | overload_frame | rx_ack) & sample_point & (tx != sampled_bit) & (~bit_err_exc1) & (~bit_err_exc2) & (~bit_err_exc3) & (~bit_err_exc4) & (~bit_err_exc5);
638 28 mohor
assign bit_err_exc1 = tx_state & arbitration_field & tx;
639
assign bit_err_exc2 = rx_ack & tx;
640
assign bit_err_exc3 = error_frame & node_error_passive & (error_cnt1 < 7);
641 29 mohor
assign bit_err_exc4 = (error_frame & (error_cnt1 == 7) & (~enable_error_cnt2)) | (overload_frame & (overload_cnt1 == 7) & (~enable_overload_cnt2));
642 30 mohor
assign bit_err_exc5 = (error_frame & (error_cnt2 == 7)) | (overload_frame & (overload_cnt2 == 7));
643 19 mohor
 
644 28 mohor
assign arbitration_field = rx_id1 | rx_rtr1 | rx_ide | rx_id2 | rx_rtr2;
645 25 mohor
 
646 29 mohor
assign last_bit_of_inter = rx_inter & (bit_cnt == 2);
647 25 mohor
 
648 26 mohor
 
649 10 mohor
// Rx idle state
650
always @ (posedge clk or posedge rst)
651
begin
652
  if (rst)
653 24 mohor
    rx_idle <= 1'b0;
654
  else if (reset_mode | go_rx_id1 | error_frame)
655 10 mohor
    rx_idle <=#Tp 1'b0;
656
  else if (go_rx_idle)
657
    rx_idle <=#Tp 1'b1;
658
end
659
 
660
 
661
// Rx id1 state
662
always @ (posedge clk or posedge rst)
663
begin
664
  if (rst)
665
    rx_id1 <= 1'b0;
666 24 mohor
  else if (reset_mode | go_rx_rtr1 | error_frame)
667 10 mohor
    rx_id1 <=#Tp 1'b0;
668
  else if (go_rx_id1)
669
    rx_id1 <=#Tp 1'b1;
670
end
671
 
672
 
673
// Rx rtr1 state
674
always @ (posedge clk or posedge rst)
675
begin
676
  if (rst)
677
    rx_rtr1 <= 1'b0;
678 24 mohor
  else if (reset_mode | go_rx_ide | error_frame)
679 10 mohor
    rx_rtr1 <=#Tp 1'b0;
680
  else if (go_rx_rtr1)
681
    rx_rtr1 <=#Tp 1'b1;
682
end
683
 
684
 
685
// Rx ide state
686
always @ (posedge clk or posedge rst)
687
begin
688
  if (rst)
689
    rx_ide <= 1'b0;
690 24 mohor
  else if (reset_mode | go_rx_r0 | go_rx_id2 | error_frame)
691 10 mohor
    rx_ide <=#Tp 1'b0;
692
  else if (go_rx_ide)
693
    rx_ide <=#Tp 1'b1;
694
end
695
 
696
 
697
// Rx id2 state
698
always @ (posedge clk or posedge rst)
699
begin
700
  if (rst)
701
    rx_id2 <= 1'b0;
702 24 mohor
  else if (reset_mode | go_rx_rtr2 | error_frame)
703 10 mohor
    rx_id2 <=#Tp 1'b0;
704
  else if (go_rx_id2)
705
    rx_id2 <=#Tp 1'b1;
706
end
707
 
708
 
709
// Rx rtr2 state
710
always @ (posedge clk or posedge rst)
711
begin
712
  if (rst)
713
    rx_rtr2 <= 1'b0;
714 24 mohor
  else if (reset_mode | go_rx_r1 | error_frame)
715 10 mohor
    rx_rtr2 <=#Tp 1'b0;
716
  else if (go_rx_rtr2)
717
    rx_rtr2 <=#Tp 1'b1;
718
end
719
 
720
 
721
// Rx r0 state
722
always @ (posedge clk or posedge rst)
723
begin
724
  if (rst)
725
    rx_r1 <= 1'b0;
726 24 mohor
  else if (reset_mode | go_rx_r0 | error_frame)
727 10 mohor
    rx_r1 <=#Tp 1'b0;
728
  else if (go_rx_r1)
729
    rx_r1 <=#Tp 1'b1;
730
end
731
 
732
 
733
// Rx r0 state
734
always @ (posedge clk or posedge rst)
735
begin
736
  if (rst)
737
    rx_r0 <= 1'b0;
738 24 mohor
  else if (reset_mode | go_rx_dlc | error_frame)
739 10 mohor
    rx_r0 <=#Tp 1'b0;
740
  else if (go_rx_r0)
741
    rx_r0 <=#Tp 1'b1;
742
end
743
 
744
 
745
// Rx dlc state
746
always @ (posedge clk or posedge rst)
747
begin
748
  if (rst)
749
    rx_dlc <= 1'b0;
750 24 mohor
  else if (reset_mode | go_rx_data | go_rx_crc | error_frame)
751 10 mohor
    rx_dlc <=#Tp 1'b0;
752
  else if (go_rx_dlc)
753
    rx_dlc <=#Tp 1'b1;
754
end
755
 
756
 
757
// Rx data state
758
always @ (posedge clk or posedge rst)
759
begin
760
  if (rst)
761
    rx_data <= 1'b0;
762 24 mohor
  else if (reset_mode | go_rx_crc | error_frame)
763 10 mohor
    rx_data <=#Tp 1'b0;
764
  else if (go_rx_data)
765
    rx_data <=#Tp 1'b1;
766
end
767
 
768
 
769
// Rx crc state
770
always @ (posedge clk or posedge rst)
771
begin
772
  if (rst)
773
    rx_crc <= 1'b0;
774 24 mohor
  else if (reset_mode | go_rx_crc_lim | error_frame)
775 10 mohor
    rx_crc <=#Tp 1'b0;
776
  else if (go_rx_crc)
777
    rx_crc <=#Tp 1'b1;
778
end
779
 
780
 
781 11 mohor
// Rx crc delimiter state
782
always @ (posedge clk or posedge rst)
783
begin
784
  if (rst)
785
    rx_crc_lim <= 1'b0;
786 24 mohor
  else if (reset_mode | go_rx_ack | error_frame)
787 11 mohor
    rx_crc_lim <=#Tp 1'b0;
788
  else if (go_rx_crc_lim)
789
    rx_crc_lim <=#Tp 1'b1;
790
end
791
 
792
 
793 10 mohor
// Rx ack state
794
always @ (posedge clk or posedge rst)
795
begin
796
  if (rst)
797
    rx_ack <= 1'b0;
798 24 mohor
  else if (reset_mode | go_rx_ack_lim | error_frame)
799 10 mohor
    rx_ack <=#Tp 1'b0;
800
  else if (go_rx_ack)
801
    rx_ack <=#Tp 1'b1;
802
end
803
 
804
 
805 11 mohor
// Rx ack delimiter state
806
always @ (posedge clk or posedge rst)
807
begin
808
  if (rst)
809
    rx_ack_lim <= 1'b0;
810 24 mohor
  else if (reset_mode | go_rx_eof | error_frame)
811 11 mohor
    rx_ack_lim <=#Tp 1'b0;
812
  else if (go_rx_ack_lim)
813
    rx_ack_lim <=#Tp 1'b1;
814
end
815
 
816
 
817 10 mohor
// Rx eof state
818
always @ (posedge clk or posedge rst)
819
begin
820
  if (rst)
821
    rx_eof <= 1'b0;
822 30 mohor
  else if (go_rx_inter | error_frame | go_overload_frame)
823 10 mohor
    rx_eof <=#Tp 1'b0;
824
  else if (go_rx_eof)
825
    rx_eof <=#Tp 1'b1;
826
end
827
 
828
 
829 24 mohor
 
830
// Interframe space
831
always @ (posedge clk or posedge rst)
832
begin
833
  if (rst)
834
    rx_inter <= 1'b0;
835 35 mohor
  else if (reset_mode | go_rx_idle | go_rx_id1 | go_overload_frame | go_error_frame)
836 24 mohor
    rx_inter <=#Tp 1'b0;
837
  else if (go_rx_inter)
838
    rx_inter <=#Tp 1'b1;
839
end
840
 
841
 
842 10 mohor
// ID register
843
always @ (posedge clk or posedge rst)
844
begin
845
  if (rst)
846
    id <= 0;
847 11 mohor
  else if (sample_point & (rx_id1 | rx_id2) & (~bit_de_stuff))
848 10 mohor
    id <=#Tp {id[27:0], sampled_bit};
849
end
850
 
851 11 mohor
 
852
// rtr1 bit
853
always @ (posedge clk or posedge rst)
854
begin
855
  if (rst)
856
    rtr1 <= 0;
857
  else if (sample_point & rx_rtr1 & (~bit_de_stuff))
858
    rtr1 <=#Tp sampled_bit;
859
end
860
 
861
 
862
// rtr2 bit
863
always @ (posedge clk or posedge rst)
864
begin
865
  if (rst)
866
    rtr2 <= 0;
867
  else if (sample_point & rx_rtr2 & (~bit_de_stuff))
868
    rtr2 <=#Tp sampled_bit;
869
end
870
 
871
 
872
// ide bit
873
always @ (posedge clk or posedge rst)
874
begin
875
  if (rst)
876
    ide <= 0;
877
  else if (sample_point & rx_ide & (~bit_de_stuff))
878
    ide <=#Tp sampled_bit;
879
end
880
 
881
 
882 10 mohor
// Data length
883
always @ (posedge clk or posedge rst)
884
begin
885
  if (rst)
886
    data_len <= 0;
887
  else if (sample_point & rx_dlc & (~bit_de_stuff))
888
    data_len <=#Tp {data_len[2:0], sampled_bit};
889
end
890
 
891
 
892 11 mohor
// Data
893
always @ (posedge clk or posedge rst)
894
begin
895
  if (rst)
896
    tmp_data <= 0;
897
  else if (sample_point & rx_data & (~bit_de_stuff))
898
    tmp_data <=#Tp {tmp_data[6:0], sampled_bit};
899
end
900
 
901
 
902
always @ (posedge clk or posedge rst)
903
begin
904
  if (rst)
905
    write_data_to_tmp_fifo <= 0;
906
  else if (sample_point & rx_data & (~bit_de_stuff) & (&bit_cnt[2:0]))
907
    write_data_to_tmp_fifo <=#Tp 1'b1;
908
  else
909
    write_data_to_tmp_fifo <=#Tp 0;
910
end
911
 
912
 
913
always @ (posedge clk or posedge rst)
914
begin
915
  if (rst)
916
    byte_cnt <= 0;
917 36 mohor
  else if (write_data_to_tmp_fifo)
918 11 mohor
    byte_cnt <=#Tp byte_cnt + 1;
919 36 mohor
  else if (reset_mode | (sample_point & go_rx_crc_lim))
920 11 mohor
    byte_cnt <=#Tp 0;
921
end
922
 
923
 
924 24 mohor
always @ (posedge clk)
925 11 mohor
begin
926
  if (write_data_to_tmp_fifo)
927
    tmp_fifo[byte_cnt] <=#Tp tmp_data;
928
end
929
 
930
 
931
 
932
// CRC
933
always @ (posedge clk or posedge rst)
934
begin
935
  if (rst)
936
    crc_in <= 0;
937
  else if (sample_point & rx_crc & (~bit_de_stuff))
938
    crc_in <=#Tp {crc_in[13:0], sampled_bit};
939
end
940
 
941
 
942 10 mohor
// bit_cnt
943
always @ (posedge clk or posedge rst)
944
begin
945
  if (rst)
946
    bit_cnt <= 0;
947 24 mohor
  else if (go_rx_id1 | go_rx_id2 | go_rx_dlc | go_rx_data | go_rx_crc |
948
           go_rx_ack | go_rx_eof | go_rx_inter | go_error_frame | go_overload_frame)
949 10 mohor
    bit_cnt <=#Tp 0;
950 11 mohor
  else if (sample_point & (~bit_de_stuff))
951 10 mohor
    bit_cnt <=#Tp bit_cnt + 1'b1;
952
end
953
 
954
 
955
// eof_cnt
956
always @ (posedge clk or posedge rst)
957
begin
958
  if (rst)
959
    eof_cnt <= 0;
960
  else if (sample_point)
961
    begin
962 35 mohor
      if (reset_mode | go_rx_inter | go_error_frame | go_overload_frame)
963 28 mohor
        eof_cnt <=#Tp 0;
964
      else if (rx_eof)
965 10 mohor
        eof_cnt <=#Tp eof_cnt + 1'b1;
966
    end
967
end
968
 
969
 
970 11 mohor
// Enabling bit de-stuffing
971
always @ (posedge clk or posedge rst)
972
begin
973
  if (rst)
974
    bit_stuff_cnt_en <= 1'b0;
975
  else if (bit_de_stuff_set)
976
    bit_stuff_cnt_en <=#Tp 1'b1;
977
  else if (bit_de_stuff_reset)
978
    bit_stuff_cnt_en <=#Tp 1'b0;
979
end
980 10 mohor
 
981 24 mohor
 
982 10 mohor
// bit_stuff_cnt
983
always @ (posedge clk or posedge rst)
984
begin
985
  if (rst)
986
    bit_stuff_cnt <= 1;
987 15 mohor
  else if (bit_de_stuff_reset)
988
    bit_stuff_cnt <=#Tp 1;
989 11 mohor
  else if (sample_point & bit_stuff_cnt_en)
990 10 mohor
    begin
991
      if (bit_stuff_cnt == 5)
992
        bit_stuff_cnt <=#Tp 1;
993
      else if (sampled_bit == sampled_bit_q)
994
        bit_stuff_cnt <=#Tp bit_stuff_cnt + 1'b1;
995
      else
996
        bit_stuff_cnt <=#Tp 1;
997
    end
998
end
999
 
1000
 
1001 35 mohor
// Enabling bit de-stuffing for tx
1002
always @ (posedge clk or posedge rst)
1003
begin
1004
  if (rst)
1005
    bit_stuff_cnt_tx_en <= 1'b0;
1006
  else if (bit_de_stuff_set & transmitting)
1007
    bit_stuff_cnt_tx_en <=#Tp 1'b1;
1008
  else if (bit_de_stuff_reset)
1009
    bit_stuff_cnt_tx_en <=#Tp 1'b0;
1010
end
1011
 
1012
 
1013 25 mohor
// bit_stuff_cnt_tx
1014
always @ (posedge clk or posedge rst)
1015
begin
1016
  if (rst)
1017
    bit_stuff_cnt_tx <= 1;
1018
  else if (bit_de_stuff_reset)
1019
    bit_stuff_cnt_tx <=#Tp 1;
1020
  else if (tx_point_q & bit_stuff_cnt_en)
1021
    begin
1022
      if (bit_stuff_cnt_tx == 5)
1023
        bit_stuff_cnt_tx <=#Tp 1;
1024
      else if (tx == tx_q)
1025
        bit_stuff_cnt_tx <=#Tp bit_stuff_cnt_tx + 1'b1;
1026
      else
1027
        bit_stuff_cnt_tx <=#Tp 1;
1028
    end
1029
end
1030
 
1031
 
1032 10 mohor
assign bit_de_stuff = bit_stuff_cnt == 5;
1033 25 mohor
assign bit_de_stuff_tx = bit_stuff_cnt_tx == 5;
1034 10 mohor
 
1035
 
1036 24 mohor
 
1037 28 mohor
// stuff_err
1038
assign stuff_err = sample_point & bit_stuff_cnt_en & bit_de_stuff & (sampled_bit == sampled_bit_q);
1039 10 mohor
 
1040
 
1041 28 mohor
 
1042 35 mohor
// Generating delayed signals
1043 10 mohor
always @ (posedge clk)
1044
begin
1045
  reset_mode_q <=#Tp reset_mode;
1046 35 mohor
  node_bus_off_q <=#Tp node_bus_off;
1047 10 mohor
end
1048
 
1049
 
1050 11 mohor
 
1051
always @ (posedge clk or posedge rst)
1052
begin
1053
  if (rst)
1054
    crc_enable <= 1'b0;
1055
  else if (go_crc_enable)
1056
    crc_enable <=#Tp 1'b1;
1057
  else if (reset_mode | rst_crc_enable)
1058
    crc_enable <=#Tp 1'b0;
1059
end
1060
 
1061
 
1062
// CRC error generation
1063
always @ (posedge clk or posedge rst)
1064
begin
1065
  if (rst)
1066 28 mohor
    crc_err <= 1'b0;
1067 12 mohor
  else if (go_rx_ack)
1068 28 mohor
    crc_err <=#Tp crc_in != calculated_crc;
1069
  else if (reset_mode | error_frame_ended)
1070
    crc_err <=#Tp 1'b0;
1071 11 mohor
end
1072
 
1073
 
1074 22 mohor
// Conditions for form error
1075 28 mohor
assign form_err = sample_point & ( ((~bit_de_stuff) & rx_ide     &   sampled_bit & (~rtr1)      ) |
1076 80 mohor
                                   ((~bit_de_stuff) & rx_crc_lim & (~sampled_bit)               ) |
1077 28 mohor
                                   (                  rx_ack_lim & (~sampled_bit)               ) |
1078
                                   ((eof_cnt < 6)   & rx_eof     & (~sampled_bit) & (~tx_state) ) |
1079
                                   (                & rx_eof     & (~sampled_bit) &   tx_state  )
1080
                                 );
1081 11 mohor
 
1082 22 mohor
 
1083 28 mohor
always @ (posedge clk or posedge rst)
1084
begin
1085
  if (rst)
1086
    ack_err_latched <= 1'b0;
1087 30 mohor
  else if (reset_mode | error_frame_ended | go_overload_frame)
1088 28 mohor
    ack_err_latched <=#Tp 1'b0;
1089
  else if (ack_err)
1090
    ack_err_latched <=#Tp 1'b1;
1091
end
1092
 
1093
 
1094
always @ (posedge clk or posedge rst)
1095
begin
1096
  if (rst)
1097
    bit_err_latched <= 1'b0;
1098 30 mohor
  else if (reset_mode | error_frame_ended | go_overload_frame)
1099 28 mohor
    bit_err_latched <=#Tp 1'b0;
1100
  else if (bit_err)
1101
    bit_err_latched <=#Tp 1'b1;
1102
end
1103
 
1104
 
1105 44 mohor
 
1106 28 mohor
// Rule 5 (Fault confinement).
1107 44 mohor
assign rule5 = (~node_error_passive) & bit_err &  (error_frame    & (error_cnt1    < 7) |
1108
                                                   overload_frame & (overload_cnt1 < 7) );
1109 28 mohor
 
1110
// Rule 3 exception 1 - first part (Fault confinement).
1111
always @ (posedge clk or posedge rst)
1112
begin
1113
  if (rst)
1114
    rule3_exc1_1 <= 1'b0;
1115
  else if (reset_mode | error_flag_over | rule3_exc1_2)
1116
    rule3_exc1_1 <=#Tp 1'b0;
1117
  else if (transmitter & node_error_passive & ack_err)
1118
    rule3_exc1_1 <=#Tp 1'b1;
1119
end
1120
 
1121
 
1122
// Rule 3 exception 1 - second part (Fault confinement).
1123
always @ (posedge clk or posedge rst)
1124
begin
1125
  if (rst)
1126
    rule3_exc1_2 <= 1'b0;
1127
  else if (reset_mode | error_flag_over)
1128
    rule3_exc1_2 <=#Tp 1'b0;
1129
  else if (rule3_exc1_1)
1130
    rule3_exc1_2 <=#Tp 1'b1;
1131
  else if ((error_cnt1 < 7) & sample_point & (~sampled_bit))
1132
    rule3_exc1_2 <=#Tp 1'b0;
1133
end
1134
 
1135
 
1136
// Rule 3 exception 2 (Fault confinement).
1137
always @ (posedge clk or posedge rst)
1138
begin
1139
  if (rst)
1140
    rule3_exc2 <= 1'b0;
1141
  else if (reset_mode | error_flag_over)
1142
    rule3_exc2 <=#Tp 1'b0;
1143
  else if (transmitter & stuff_err & arbitration_field & sample_point & tx & (~sampled_bit))
1144
    rule3_exc2 <=#Tp 1'b1;
1145
end
1146
 
1147
 
1148
 
1149
always @ (posedge clk or posedge rst)
1150
begin
1151
  if (rst)
1152
    stuff_err_latched <= 1'b0;
1153 30 mohor
  else if (reset_mode | error_frame_ended | go_overload_frame)
1154 28 mohor
    stuff_err_latched <=#Tp 1'b0;
1155
  else if (stuff_err)
1156
    stuff_err_latched <=#Tp 1'b1;
1157
end
1158
 
1159
 
1160
 
1161
always @ (posedge clk or posedge rst)
1162
begin
1163
  if (rst)
1164
    form_err_latched <= 1'b0;
1165 30 mohor
  else if (reset_mode | error_frame_ended | go_overload_frame)
1166 28 mohor
    form_err_latched <=#Tp 1'b0;
1167
  else if (form_err)
1168
    form_err_latched <=#Tp 1'b1;
1169
end
1170
 
1171
 
1172
 
1173 11 mohor
// Instantiation of the RX CRC module
1174
can_crc i_can_crc_rx
1175
(
1176
  .clk(clk),
1177 25 mohor
  .data(sampled_bit),
1178 11 mohor
  .enable(crc_enable & sample_point & (~bit_de_stuff)),
1179 35 mohor
  .initialize(go_crc_enable),
1180 11 mohor
  .crc(calculated_crc)
1181
);
1182
 
1183
 
1184
 
1185
 
1186 16 mohor
assign no_byte0 = rtr1 | (data_len<1);
1187
assign no_byte1 = rtr1 | (data_len<2);
1188 11 mohor
 
1189
can_acf i_can_acf
1190
(
1191
  .clk(clk),
1192
  .rst(rst),
1193
 
1194
  .id(id),
1195
 
1196
  /* Mode register */
1197
  .reset_mode(reset_mode),
1198
  .acceptance_filter_mode(acceptance_filter_mode),
1199
 
1200 12 mohor
  // Clock Divider register
1201 11 mohor
  .extended_mode(extended_mode),
1202
 
1203
  /* This section is for BASIC and EXTENDED mode */
1204
  /* Acceptance code register */
1205
  .acceptance_code_0(acceptance_code_0),
1206
 
1207
  /* Acceptance mask register */
1208
  .acceptance_mask_0(acceptance_mask_0),
1209
  /* End: This section is for BASIC and EXTENDED mode */
1210
 
1211
  /* This section is for EXTENDED mode */
1212
  /* Acceptance code register */
1213
  .acceptance_code_1(acceptance_code_1),
1214
  .acceptance_code_2(acceptance_code_2),
1215
  .acceptance_code_3(acceptance_code_3),
1216
 
1217
  /* Acceptance mask register */
1218
  .acceptance_mask_1(acceptance_mask_1),
1219
  .acceptance_mask_2(acceptance_mask_2),
1220
  .acceptance_mask_3(acceptance_mask_3),
1221
  /* End: This section is for EXTENDED mode */
1222
 
1223
  .go_rx_crc_lim(go_rx_crc_lim),
1224 29 mohor
  .go_rx_inter(go_rx_inter),
1225 30 mohor
  .go_error_frame(go_error_frame),
1226 11 mohor
 
1227
  .data0(tmp_fifo[0]),
1228
  .data1(tmp_fifo[1]),
1229
  .rtr1(rtr1),
1230
  .rtr2(rtr2),
1231
  .ide(ide),
1232 16 mohor
  .no_byte0(no_byte0),
1233
  .no_byte1(no_byte1),
1234 11 mohor
 
1235
  .id_ok(id_ok)
1236
 
1237
);
1238
 
1239
 
1240
 
1241
 
1242 16 mohor
assign header_len[2:0] = extended_mode ? (ide? (3'h5) : (3'h3)) : 3'h2;
1243 13 mohor
assign storing_header = header_cnt < header_len;
1244 19 mohor
assign limited_data_len_minus1[3:0] = remote_rq? 4'hf : ((data_len < 8)? (data_len -1'b1) : 4'h7);   // - 1 because counter counts from 0
1245 25 mohor
assign reset_wr_fifo = (data_cnt == (limited_data_len_minus1 + header_len)) | reset_mode;
1246 12 mohor
 
1247 28 mohor
assign err = form_err | stuff_err | bit_err | ack_err | form_err_latched | stuff_err_latched | bit_err_latched | ack_err_latched | crc_err;
1248 24 mohor
 
1249
 
1250 28 mohor
 
1251 13 mohor
// Write enable signal for 64-byte rx fifo
1252
always @ (posedge clk or posedge rst)
1253
begin
1254
  if (rst)
1255
    wr_fifo <= 1'b0;
1256 16 mohor
  else if (reset_wr_fifo)
1257
    wr_fifo <=#Tp 1'b0;
1258 36 mohor
  else if (go_rx_inter & id_ok & (~error_frame_ended) & ((~tx_state) | self_rx_request))
1259 13 mohor
    wr_fifo <=#Tp 1'b1;
1260
end
1261 12 mohor
 
1262
 
1263 13 mohor
// Header counter. Header length depends on the mode of operation and frame format.
1264 12 mohor
always @ (posedge clk or posedge rst)
1265
begin
1266
  if (rst)
1267 13 mohor
    header_cnt <= 0;
1268 16 mohor
  else if (reset_wr_fifo)
1269
    header_cnt <=#Tp 0;
1270
  else if (wr_fifo & storing_header)
1271 13 mohor
    header_cnt <=#Tp header_cnt + 1;
1272 12 mohor
end
1273
 
1274
 
1275 13 mohor
// Data counter. Length of the data is limited to 8 bytes.
1276 12 mohor
always @ (posedge clk or posedge rst)
1277
begin
1278
  if (rst)
1279 13 mohor
    data_cnt <= 0;
1280 16 mohor
  else if (reset_wr_fifo)
1281
    data_cnt <=#Tp 0;
1282
  else if (wr_fifo)
1283 13 mohor
    data_cnt <=#Tp data_cnt + 1;
1284 12 mohor
end
1285
 
1286
 
1287 13 mohor
// Multiplexing data that is stored to 64-byte fifo depends on the mode of operation and frame format
1288 24 mohor
always @ (extended_mode or ide or data_cnt or header_cnt or  header_len or
1289
          storing_header or id or rtr1 or rtr2 or data_len or
1290 13 mohor
          tmp_fifo[0] or tmp_fifo[2] or tmp_fifo[4] or tmp_fifo[6] or
1291
          tmp_fifo[1] or tmp_fifo[3] or tmp_fifo[5] or tmp_fifo[7])
1292 12 mohor
begin
1293 13 mohor
  if (storing_header)
1294 12 mohor
    begin
1295 13 mohor
      if (extended_mode)      // extended mode
1296 12 mohor
        begin
1297 13 mohor
          if (ide)              // extended format
1298
            begin
1299 90 mohor
              case (header_cnt) /* synthesis full_case synthesis parallel_case */
1300 24 mohor
                3'h0  : data_for_fifo <= {1'b1, rtr2, 2'h0, data_len};
1301
                3'h1  : data_for_fifo <= id[28:21];
1302
                3'h2  : data_for_fifo <= id[20:13];
1303
                3'h3  : data_for_fifo <= id[12:5];
1304
                3'h4  : data_for_fifo <= {id[4:0], 3'h0};
1305
                default: data_for_fifo <= 0;
1306 13 mohor
              endcase
1307
            end
1308
          else                  // standard format
1309
            begin
1310 90 mohor
              case (header_cnt) /* synthesis full_case synthesis parallel_case */
1311 24 mohor
                3'h0  : data_for_fifo <= {1'b0, rtr1, 2'h0, data_len};
1312
                3'h1  : data_for_fifo <= id[10:3];
1313
                3'h2  : data_for_fifo <= {id[2:0], 5'h0};
1314
                default: data_for_fifo <= 0;
1315 13 mohor
              endcase
1316
            end
1317 12 mohor
        end
1318 13 mohor
      else                    // normal mode
1319 12 mohor
        begin
1320 90 mohor
          case (header_cnt) /* synthesis full_case synthesis parallel_case */
1321 24 mohor
            3'h0  : data_for_fifo <= id[10:3];
1322
            3'h1  : data_for_fifo <= {id[2:0], rtr1, data_len};
1323
            default: data_for_fifo <= 0;
1324 12 mohor
          endcase
1325
        end
1326
    end
1327 13 mohor
  else
1328 16 mohor
    data_for_fifo <= tmp_fifo[data_cnt-header_len];
1329 12 mohor
end
1330
 
1331
 
1332
 
1333
 
1334
// Instantiation of the RX fifo module
1335 13 mohor
can_fifo i_can_fifo
1336 12 mohor
(
1337
  .clk(clk),
1338
  .rst(rst),
1339
 
1340 13 mohor
  .wr(wr_fifo),
1341 12 mohor
 
1342 13 mohor
  .data_in(data_for_fifo),
1343 14 mohor
  .addr(addr),
1344
  .data_out(data_out),
1345 48 mohor
  .fifo_selected(fifo_selected),
1346 12 mohor
 
1347
  .reset_mode(reset_mode),
1348 15 mohor
  .release_buffer(release_buffer),
1349 35 mohor
  .extended_mode(extended_mode),
1350
  .overrun(overrun),
1351 39 mohor
  .info_empty(info_empty),
1352
  .info_cnt(rx_message_counter)
1353 12 mohor
);
1354
 
1355
 
1356 29 mohor
// Transmitting error frame.
1357 24 mohor
always @ (posedge clk or posedge rst)
1358
begin
1359
  if (rst)
1360
    error_frame <= 1'b0;
1361 30 mohor
  else if (reset_mode | error_frame_ended | go_overload_frame)
1362 24 mohor
    error_frame <=#Tp 1'b0;
1363 28 mohor
  else if (go_error_frame)
1364 24 mohor
    error_frame <=#Tp 1'b1;
1365
end
1366 12 mohor
 
1367 13 mohor
 
1368 28 mohor
always @ (posedge clk)
1369
begin
1370
  if (sample_point)
1371
    error_frame_q <=#Tp error_frame;
1372
end
1373
 
1374
 
1375 24 mohor
always @ (posedge clk or posedge rst)
1376
begin
1377
  if (rst)
1378
    error_cnt1 <= 1'b0;
1379 30 mohor
  else if (reset_mode | error_frame_ended | go_error_frame | go_overload_frame)
1380 24 mohor
    error_cnt1 <=#Tp 1'b0;
1381 29 mohor
  else if (error_frame & tx_point & (error_cnt1 < 7))
1382 24 mohor
    error_cnt1 <=#Tp error_cnt1 + 1'b1;
1383
end
1384
 
1385
 
1386 28 mohor
 
1387 30 mohor
assign error_flag_over = ((~node_error_passive) & sample_point & (error_cnt1 == 7) | node_error_passive  & sample_point & (passive_cnt == 5)) & (~enable_error_cnt2);
1388 28 mohor
 
1389
 
1390 24 mohor
always @ (posedge clk or posedge rst)
1391
begin
1392
  if (rst)
1393 28 mohor
    error_flag_over_blocked <= 1'b0;
1394 30 mohor
  else if (reset_mode | error_frame_ended | go_error_frame | go_overload_frame)
1395 28 mohor
    error_flag_over_blocked <=#Tp 1'b0;
1396
  else if (error_flag_over)
1397
    error_flag_over_blocked <=#Tp 1'b1;
1398
end
1399
 
1400
 
1401
 
1402
always @ (posedge clk or posedge rst)
1403
begin
1404
  if (rst)
1405 24 mohor
    enable_error_cnt2 <= 1'b0;
1406 30 mohor
  else if (reset_mode | error_frame_ended | go_error_frame | go_overload_frame)
1407 24 mohor
    enable_error_cnt2 <=#Tp 1'b0;
1408 30 mohor
  else if (error_frame & (error_flag_over & sampled_bit))
1409 24 mohor
    enable_error_cnt2 <=#Tp 1'b1;
1410
end
1411
 
1412
 
1413
always @ (posedge clk or posedge rst)
1414
begin
1415
  if (rst)
1416 28 mohor
    error_cnt2 <= 0;
1417 30 mohor
  else if (reset_mode | error_frame_ended | go_error_frame | go_overload_frame)
1418 28 mohor
    error_cnt2 <=#Tp 0;
1419 24 mohor
  else if (enable_error_cnt2 & tx_point)
1420
    error_cnt2 <=#Tp error_cnt2 + 1'b1;
1421
end
1422
 
1423
 
1424 28 mohor
always @ (posedge clk or posedge rst)
1425
begin
1426
  if (rst)
1427 29 mohor
    delayed_dominant_cnt <= 0;
1428
  else if (reset_mode | enable_error_cnt2 | go_error_frame | enable_overload_cnt2 | go_overload_frame)
1429
    delayed_dominant_cnt <=#Tp 0;
1430
  else if (sample_point & (~sampled_bit) & ((error_cnt1 == 7) | (overload_cnt1 == 7)))
1431
    delayed_dominant_cnt <=#Tp delayed_dominant_cnt + 1'b1;
1432 28 mohor
end
1433 24 mohor
 
1434 25 mohor
 
1435 28 mohor
// passive_cnt
1436
always @ (posedge clk or posedge rst)
1437
begin
1438
  if (rst)
1439
    passive_cnt <= 0;
1440 30 mohor
  else if (reset_mode | error_frame_ended | go_error_frame | go_overload_frame)
1441 28 mohor
    passive_cnt <=#Tp 0;
1442
  else if (sample_point & (passive_cnt < 5))
1443
    begin
1444
      if (error_frame_q & (~enable_error_cnt2) & (sampled_bit == sampled_bit_q))
1445
        passive_cnt <=#Tp passive_cnt + 1'b1;
1446
      else
1447
        passive_cnt <=#Tp 0;
1448
    end
1449
end
1450 25 mohor
 
1451
 
1452 29 mohor
 
1453
// Transmitting overload frame.
1454
always @ (posedge clk or posedge rst)
1455
begin
1456
  if (rst)
1457
    overload_frame <= 1'b0;
1458
  else if (reset_mode | overload_frame_ended | go_error_frame)
1459
    overload_frame <=#Tp 1'b0;
1460
  else if (go_overload_frame)
1461
    overload_frame <=#Tp 1'b1;
1462
end
1463
 
1464
 
1465
always @ (posedge clk or posedge rst)
1466
begin
1467
  if (rst)
1468
    overload_cnt1 <= 1'b0;
1469 30 mohor
  else if (reset_mode | overload_frame_ended | go_error_frame | go_overload_frame)
1470 29 mohor
    overload_cnt1 <=#Tp 1'b0;
1471
  else if (overload_frame & tx_point & (overload_cnt1 < 7))
1472
    overload_cnt1 <=#Tp overload_cnt1 + 1'b1;
1473
end
1474
 
1475
 
1476 30 mohor
assign overload_flag_over = sample_point & (overload_cnt1 == 7) & (~enable_overload_cnt2);
1477 29 mohor
 
1478
 
1479
always @ (posedge clk or posedge rst)
1480
begin
1481
  if (rst)
1482
    enable_overload_cnt2 <= 1'b0;
1483 30 mohor
  else if (reset_mode | overload_frame_ended | go_error_frame | go_overload_frame)
1484 29 mohor
    enable_overload_cnt2 <=#Tp 1'b0;
1485 30 mohor
  else if (overload_frame & (overload_flag_over & sampled_bit))
1486 29 mohor
    enable_overload_cnt2 <=#Tp 1'b1;
1487
end
1488
 
1489
 
1490
always @ (posedge clk or posedge rst)
1491
begin
1492
  if (rst)
1493
    overload_cnt2 <= 0;
1494 30 mohor
  else if (reset_mode | overload_frame_ended | go_error_frame | go_overload_frame)
1495 29 mohor
    overload_cnt2 <=#Tp 0;
1496
  else if (enable_overload_cnt2 & tx_point)
1497
    overload_cnt2 <=#Tp overload_cnt2 + 1'b1;
1498
end
1499
 
1500
 
1501 30 mohor
always @ (posedge clk or posedge rst)
1502
begin
1503
  if (rst)
1504
    overload_frame_blocked <= 0;
1505
  else if (reset_mode | go_error_frame | go_rx_id1)
1506
    overload_frame_blocked <=#Tp 0;
1507
  else if (go_overload_frame & overload_frame)            // This is a second sequential overload
1508
    overload_frame_blocked <=#Tp 1'b1;
1509
end
1510
 
1511
 
1512 36 mohor
assign send_ack = (~tx_state) & rx_ack & (~err) & (~listen_only_mode);
1513 25 mohor
 
1514
 
1515 24 mohor
always @ (posedge clk or posedge rst)
1516
begin
1517
  if (rst)
1518
    tx <= 1'b1;
1519 30 mohor
  else if (reset_mode)                                                          // Reset
1520 24 mohor
    tx <=#Tp 1'b1;
1521 25 mohor
  else if (tx_point)
1522 24 mohor
    begin
1523 25 mohor
      if (tx_state)                                                             // Transmitting message
1524
        tx <=#Tp ((~bit_de_stuff_tx) & tx_bit) | (bit_de_stuff_tx & (~tx_q));
1525 28 mohor
      else if (send_ack)                                                        // Acknowledge
1526 25 mohor
        tx <=#Tp 1'b0;
1527 30 mohor
      else if (overload_frame)                                                  // Transmitting overload frame
1528
        begin
1529
          if (overload_cnt1 < 6)
1530
            tx <=#Tp 1'b0;
1531
          else
1532
            tx <=#Tp 1'b1;
1533
        end
1534 29 mohor
      else if (error_frame)                                                     // Transmitting error frame
1535 24 mohor
        begin
1536 25 mohor
          if (error_cnt1 < 6)
1537
            begin
1538 29 mohor
              if (node_error_passive)
1539 25 mohor
                tx <=#Tp 1'b1;
1540
              else
1541
                tx <=#Tp 1'b0;
1542
            end
1543 29 mohor
          else
1544 24 mohor
            tx <=#Tp 1'b1;
1545
        end
1546 25 mohor
      else
1547 24 mohor
        tx <=#Tp 1'b1;
1548
    end
1549
end
1550
 
1551
 
1552 45 mohor
 
1553 25 mohor
always @ (posedge clk)
1554
begin
1555
  if (tx_point)
1556 45 mohor
    tx_q <=#Tp tx & (~go_early_tx_latched);
1557 25 mohor
end
1558 24 mohor
 
1559
 
1560 25 mohor
/* Delayed tx point */
1561
always @ (posedge clk)
1562
begin
1563
  tx_point_q <=#Tp tx_point;
1564
end
1565 24 mohor
 
1566
 
1567 25 mohor
/* Changing bit order from [7:0] to [0:7] */
1568
can_ibo i_ibo_tx_data_0  (.di(tx_data_0),  .do(r_tx_data_0));
1569
can_ibo i_ibo_tx_data_1  (.di(tx_data_1),  .do(r_tx_data_1));
1570
can_ibo i_ibo_tx_data_2  (.di(tx_data_2),  .do(r_tx_data_2));
1571
can_ibo i_ibo_tx_data_3  (.di(tx_data_3),  .do(r_tx_data_3));
1572
can_ibo i_ibo_tx_data_4  (.di(tx_data_4),  .do(r_tx_data_4));
1573
can_ibo i_ibo_tx_data_5  (.di(tx_data_5),  .do(r_tx_data_5));
1574
can_ibo i_ibo_tx_data_6  (.di(tx_data_6),  .do(r_tx_data_6));
1575
can_ibo i_ibo_tx_data_7  (.di(tx_data_7),  .do(r_tx_data_7));
1576
can_ibo i_ibo_tx_data_8  (.di(tx_data_8),  .do(r_tx_data_8));
1577
can_ibo i_ibo_tx_data_9  (.di(tx_data_9),  .do(r_tx_data_9));
1578
can_ibo i_ibo_tx_data_10 (.di(tx_data_10), .do(r_tx_data_10));
1579
can_ibo i_ibo_tx_data_11 (.di(tx_data_11), .do(r_tx_data_11));
1580
can_ibo i_ibo_tx_data_12 (.di(tx_data_12), .do(r_tx_data_12));
1581 24 mohor
 
1582 25 mohor
/* Changing bit order from [14:0] to [0:14] */
1583
can_ibo i_calculated_crc0 (.di(calculated_crc[14:7]), .do(r_calculated_crc[7:0]));
1584
can_ibo i_calculated_crc1 (.di({calculated_crc[6:0], 1'b0}), .do(r_calculated_crc[15:8]));
1585 24 mohor
 
1586
 
1587 25 mohor
assign basic_chain = {r_tx_data_1[7:4], 2'h0, r_tx_data_1[3:0], r_tx_data_0[7:0], 1'b0};
1588
assign basic_chain_data = {r_tx_data_9, r_tx_data_8, r_tx_data_7, r_tx_data_6, r_tx_data_5, r_tx_data_4, r_tx_data_3, r_tx_data_2};
1589
assign extended_chain_std = {r_tx_data_0[7:4], 2'h0, r_tx_data_0[1], r_tx_data_2[2:0], r_tx_data_1[7:0], 1'b0};
1590
assign extended_chain_ext = {r_tx_data_0[7:4], 2'h0, r_tx_data_0[1], r_tx_data_4[4:0], r_tx_data_3[7:0], r_tx_data_2[7:3], 1'b1, 1'b1, r_tx_data_2[2:0], r_tx_data_1[7:0], 1'b0};
1591 78 mohor
assign extended_chain_data_std = {r_tx_data_10, r_tx_data_9, r_tx_data_8, r_tx_data_7, r_tx_data_6, r_tx_data_5, r_tx_data_4, r_tx_data_3};
1592
assign extended_chain_data_ext = {r_tx_data_12, r_tx_data_11, r_tx_data_10, r_tx_data_9, r_tx_data_8, r_tx_data_7, r_tx_data_6, r_tx_data_5};
1593 25 mohor
 
1594 78 mohor
always @ (extended_mode or rx_data or tx_pointer or extended_chain_data_std or extended_chain_data_ext or rx_crc or r_calculated_crc or
1595 25 mohor
          r_tx_data_0   or extended_chain_ext or extended_chain_std or basic_chain_data or basic_chain or
1596
          finish_msg)
1597
begin
1598
  if (extended_mode)
1599
    begin
1600
      if (rx_data)  // data stage
1601 78 mohor
        if (r_tx_data_0[0])    // Extended frame
1602
          tx_bit = extended_chain_data_ext[tx_pointer];
1603
        else
1604
          tx_bit = extended_chain_data_std[tx_pointer];
1605 25 mohor
      else if (rx_crc)
1606
        tx_bit = r_calculated_crc[tx_pointer];
1607
      else if (finish_msg)
1608
        tx_bit = 1'b1;
1609
      else
1610
        begin
1611
          if (r_tx_data_0[0])    // Extended frame
1612
            tx_bit = extended_chain_ext[tx_pointer];
1613
          else
1614
            tx_bit = extended_chain_std[tx_pointer];
1615
        end
1616
    end
1617
  else  // Basic mode
1618
    begin
1619
      if (rx_data)  // data stage
1620
        tx_bit = basic_chain_data[tx_pointer];
1621
      else if (rx_crc)
1622
        tx_bit = r_calculated_crc[tx_pointer];
1623
      else if (finish_msg)
1624
        tx_bit = 1'b1;
1625
      else
1626
        tx_bit = basic_chain[tx_pointer];
1627
    end
1628
end
1629
 
1630 36 mohor
assign rst_tx_pointer = ((~bit_de_stuff_tx) & tx_point & (~rx_data) &   extended_mode  &   r_tx_data_0[0]   & tx_pointer == 38                      ) |   // arbitration + control for extended format
1631
                        ((~bit_de_stuff_tx) & tx_point & (~rx_data) &   extended_mode  & (~r_tx_data_0[0])  & tx_pointer == 18                      ) |   // arbitration + control for extended format
1632
                        ((~bit_de_stuff_tx) & tx_point & (~rx_data) & (~extended_mode)                      & tx_pointer == 18                      ) |   // arbitration + control for standard format
1633
                        ((~bit_de_stuff_tx) & tx_point &   rx_data  &   extended_mode                       & tx_pointer == (8 * tx_data_0[3:0] - 1)) |   // data
1634
                        ((~bit_de_stuff_tx) & tx_point &   rx_data  & (~extended_mode)                      & tx_pointer == (8 * tx_data_1[3:0] - 1)) |   // data
1635
                        (                     tx_point &   rx_crc_lim                                                                               ) |   // crc
1636
                        (go_rx_idle                                                                                                                 ) |   // at the end
1637
                        (reset_mode                                                                                                                 ) |
1638 44 mohor
                        (overload_frame                                                                                                             ) |
1639 36 mohor
                        (error_frame                                                                                                                ) ;
1640 25 mohor
 
1641
always @ (posedge clk or posedge rst)
1642
begin
1643
  if (rst)
1644
    tx_pointer <= 'h0;
1645
  else if (rst_tx_pointer)
1646
    tx_pointer <=#Tp 'h0;
1647
  else if (go_early_tx | (tx_point & tx_state & (~bit_de_stuff_tx)))
1648
    tx_pointer <=#Tp tx_pointer + 1'b1;
1649
end
1650
 
1651
 
1652 39 mohor
assign tx_successful = transmitter & go_rx_inter & ((~error_frame_ended) & (~overload_frame_ended) & (~arbitration_lost) | single_shot_transmission);
1653 28 mohor
 
1654
 
1655 25 mohor
always @ (posedge clk or posedge rst)
1656
begin
1657
  if (rst)
1658
    need_to_tx <= 1'b0;
1659 35 mohor
  else if (tx_successful | reset_mode | (abort_tx & (~transmitting)))
1660 28 mohor
    need_to_tx <=#Tp 1'h0;
1661 32 mohor
  else if (tx_request & sample_point)
1662 25 mohor
    need_to_tx <=#Tp 1'b1;
1663
end
1664
 
1665
 
1666
 
1667 36 mohor
assign go_early_tx = (~listen_only_mode) & need_to_tx & (~tx_state) & (~suspend) & sample_point & (~sampled_bit) & (rx_idle | last_bit_of_inter);
1668
assign go_tx       = (~listen_only_mode) & need_to_tx & (~tx_state) & (~suspend) & (go_early_tx | rx_idle);
1669 25 mohor
 
1670
 
1671 45 mohor
// go_early_tx latched (for proper bit_de_stuff generation)
1672
always @ (posedge clk or posedge rst)
1673
begin
1674
  if (rst)
1675
    go_early_tx_latched <= 1'b0;
1676
  else if (tx_point_q)
1677
    go_early_tx_latched <=#Tp 1'b0;
1678
  else if (go_early_tx)
1679
    go_early_tx_latched <=#Tp 1'b1;
1680
end
1681
 
1682
 
1683
 
1684 25 mohor
// Tx state
1685
always @ (posedge clk or posedge rst)
1686
begin
1687
  if (rst)
1688
    tx_state <= 1'b0;
1689 39 mohor
  else if (reset_mode | go_rx_inter | error_frame | arbitration_lost)
1690 25 mohor
    tx_state <=#Tp 1'b0;
1691
  else if (go_tx)
1692
    tx_state <=#Tp 1'b1;
1693
end
1694
 
1695
 
1696
 
1697 28 mohor
// Node is a transmitter
1698 25 mohor
always @ (posedge clk or posedge rst)
1699
begin
1700
  if (rst)
1701 28 mohor
    transmitter <= 1'b0;
1702
  else if (go_tx)
1703
    transmitter <=#Tp 1'b1;
1704 35 mohor
  else if (reset_mode | go_rx_inter)
1705 28 mohor
    transmitter <=#Tp 1'b0;
1706
end
1707
 
1708
 
1709
 
1710
// Signal "transmitting" signals that the core is a transmitting (message, error frame or overload frame). No synchronization is done meanwhile.
1711
// Node might be both transmitter or receiver (sending error or overload frame)
1712
always @ (posedge clk or posedge rst)
1713
begin
1714
  if (rst)
1715 26 mohor
    transmitting <= 1'b0;
1716 28 mohor
  else if (go_error_frame | go_overload_frame | go_tx)
1717
    transmitting <=#Tp 1'b1;
1718 39 mohor
  else if (reset_mode | go_rx_idle | (go_rx_id1 & (~tx_state)) | (arbitration_lost & tx_state))
1719 26 mohor
    transmitting <=#Tp 1'b0;
1720
end
1721
 
1722
 
1723 28 mohor
always @ (posedge clk or posedge rst)
1724
begin
1725
  if (rst)
1726
    suspend <= 0;
1727
  else if (reset_mode | (sample_point & (susp_cnt == 7)))
1728
    suspend <=#Tp 0;
1729
  else if (go_rx_inter & transmitter & node_error_passive)
1730
    suspend <=#Tp 1'b1;
1731
end
1732 26 mohor
 
1733 28 mohor
 
1734 26 mohor
always @ (posedge clk or posedge rst)
1735
begin
1736
  if (rst)
1737 28 mohor
    susp_cnt_en <= 0;
1738
  else if (reset_mode | (sample_point & (susp_cnt == 7)))
1739
    susp_cnt_en <=#Tp 0;
1740 29 mohor
  else if (suspend & sample_point & last_bit_of_inter)
1741 28 mohor
    susp_cnt_en <=#Tp 1'b1;
1742
end
1743
 
1744
 
1745
always @ (posedge clk or posedge rst)
1746
begin
1747
  if (rst)
1748
    susp_cnt <= 0;
1749
  else if (reset_mode | (sample_point & (susp_cnt == 7)))
1750
    susp_cnt <=#Tp 0;
1751
  else if (susp_cnt_en & sample_point)
1752
    susp_cnt <=#Tp susp_cnt + 1'b1;
1753
end
1754
 
1755
 
1756
 
1757
 
1758
always @ (posedge clk or posedge rst)
1759
begin
1760
  if (rst)
1761 25 mohor
    finish_msg <= 1'b0;
1762 28 mohor
  else if (go_rx_idle | go_rx_id1 | error_frame | reset_mode)
1763 25 mohor
    finish_msg <=#Tp 1'b0;
1764
  else if (go_rx_crc_lim)
1765
    finish_msg <=#Tp 1'b1;
1766
end
1767
 
1768
 
1769
always @ (posedge clk or posedge rst)
1770
begin
1771
  if (rst)
1772 39 mohor
    arbitration_lost <= 1'b0;
1773 25 mohor
  else if (go_rx_idle | error_frame | reset_mode)
1774 39 mohor
    arbitration_lost <=#Tp 1'b0;
1775 28 mohor
  else if (tx_state & sample_point & tx & arbitration_field)
1776 39 mohor
    arbitration_lost <=#Tp (~sampled_bit);
1777 25 mohor
end
1778
 
1779
 
1780 39 mohor
always @ (posedge clk)
1781
begin
1782
  arbitration_lost_q <=#Tp arbitration_lost;
1783
end
1784 25 mohor
 
1785 39 mohor
 
1786
assign set_arbitration_lost_irq = arbitration_lost & (~arbitration_lost_q) & (~arbitration_blocked);
1787
 
1788
 
1789 28 mohor
always @ (posedge clk or posedge rst)
1790
begin
1791
  if (rst)
1792 39 mohor
    arbitration_cnt_en <= 1'b0;
1793
  else if (arbitration_blocked)
1794
    arbitration_cnt_en <=#Tp 1'b0;
1795
  else if (rx_id1 & sample_point & (~arbitration_blocked))
1796
    arbitration_cnt_en <=#Tp 1'b1;
1797
end
1798
 
1799
 
1800
 
1801
always @ (posedge clk or posedge rst)
1802
begin
1803
  if (rst)
1804
    arbitration_blocked <= 1'b0;
1805
  else if (read_arbitration_lost_capture_reg)
1806
    arbitration_blocked <=#Tp 1'b0;
1807
  else if (set_arbitration_lost_irq)
1808
    arbitration_blocked <=#Tp 1'b1;
1809
end
1810
 
1811
 
1812
always @ (posedge clk or posedge rst)
1813
begin
1814
  if (rst)
1815
    arbitration_lost_capture <= 5'h0;
1816
  else if (read_arbitration_lost_capture_reg)
1817
    arbitration_lost_capture <=#Tp 5'h0;
1818
  else if (sample_point & (~arbitration_blocked) & arbitration_cnt_en & (~bit_de_stuff))
1819
    arbitration_lost_capture <=#Tp arbitration_lost_capture + 1'b1;
1820
end
1821
 
1822
 
1823
 
1824
always @ (posedge clk or posedge rst)
1825
begin
1826
  if (rst)
1827 28 mohor
    rx_err_cnt <= 'h0;
1828 35 mohor
  else if (we_rx_err_cnt & (~node_bus_off))
1829
    rx_err_cnt <=#Tp {1'b0, data_in};
1830
  else if (set_reset_mode)
1831 28 mohor
    rx_err_cnt <=#Tp 'h0;
1832
  else
1833
    begin
1834 36 mohor
      if (~listen_only_mode)
1835 28 mohor
        begin
1836 36 mohor
          if ((~transmitter) & go_rx_ack_lim & (~err) & (rx_err_cnt > 0))
1837
            begin
1838
              if (rx_err_cnt > 127)
1839
                rx_err_cnt <=#Tp 127;
1840
              else
1841
                rx_err_cnt <=#Tp rx_err_cnt - 1'b1;
1842
            end
1843
          else if ((rx_err_cnt < 248) & (~transmitter))   // 248 + 8 = 256
1844
            begin
1845 44 mohor
              if (go_error_frame & (~rule5))                                                                            // 1  (rule 5 is just the opposite then rule 1 exception
1846 36 mohor
                rx_err_cnt <=#Tp rx_err_cnt + 1'b1;
1847
              else if ( (error_frame & sample_point & (~sampled_bit) & (error_cnt1 == 7) & (~rx_err_cnt_blocked)  ) |   // 2
1848 44 mohor
                        (go_error_frame & rule5                                                                   ) |   // 5
1849 36 mohor
                        (error_frame & sample_point & (~sampled_bit) & (delayed_dominant_cnt == 7)                )     // 6
1850
                      )
1851
                rx_err_cnt <=#Tp rx_err_cnt + 4'h8;
1852
            end
1853 30 mohor
        end
1854 28 mohor
    end
1855
end
1856
 
1857
 
1858
always @ (posedge clk or posedge rst)
1859
begin
1860
  if (rst)
1861
    tx_err_cnt <= 'h0;
1862 35 mohor
  else if (we_tx_err_cnt)
1863
    tx_err_cnt <=#Tp {1'b0, data_in};
1864 28 mohor
  else
1865
    begin
1866 35 mohor
      if (set_reset_mode)
1867
        tx_err_cnt <=#Tp 127;
1868
      else if ((tx_err_cnt > 0) & (tx_successful | bus_free))
1869 30 mohor
        tx_err_cnt <=#Tp tx_err_cnt - 1'h1;
1870 35 mohor
      else if (transmitter)
1871 28 mohor
        begin
1872 29 mohor
          if ( (sample_point & (~sampled_bit) & (delayed_dominant_cnt == 7)                     ) |       // 6
1873 44 mohor
               (go_error_frame & rule5                                                          ) |       // 4  (rule 5 is the same as rule 4)
1874 28 mohor
               (error_flag_over & (~error_flag_over_blocked) & (~rule3_exc1_2) & (~rule3_exc2)  )         // 3
1875
             )
1876
            tx_err_cnt <=#Tp tx_err_cnt + 4'h8;
1877
        end
1878
    end
1879
end
1880
 
1881
 
1882
 
1883
always @ (posedge clk or posedge rst)
1884
begin
1885
  if (rst)
1886
    rx_err_cnt_blocked <= 1'b0;
1887
  else if (reset_mode | error_frame_ended)
1888
    rx_err_cnt_blocked <=#Tp 1'b0;
1889
  else if (sample_point & (error_cnt1 == 7))
1890
    rx_err_cnt_blocked <=#Tp 1'b1;
1891
end
1892
 
1893
 
1894
 
1895
always @ (posedge clk or posedge rst)
1896
begin
1897
  if (rst)
1898
    node_error_passive <= 1'b0;
1899 35 mohor
  else if ((rx_err_cnt < 128) & (tx_err_cnt < 128) & error_frame_ended)
1900 28 mohor
    node_error_passive <=#Tp 1'b0;
1901 44 mohor
  else if (((rx_err_cnt >= 128) | (tx_err_cnt >= 128)) & (error_frame_ended | go_error_frame | (~reset_mode) & reset_mode_q) & (~node_bus_off))
1902 28 mohor
    node_error_passive <=#Tp 1'b1;
1903
end
1904
 
1905
 
1906 36 mohor
assign node_error_active = ~(node_error_passive | node_bus_off);
1907
 
1908
 
1909 28 mohor
always @ (posedge clk or posedge rst)
1910
begin
1911
  if (rst)
1912
    node_bus_off <= 1'b0;
1913 35 mohor
  else if ((rx_err_cnt == 0) & (tx_err_cnt == 0) & (~reset_mode) | (we_tx_err_cnt & (data_in < 255)))
1914 28 mohor
    node_bus_off <=#Tp 1'b0;
1915 35 mohor
  else if ((tx_err_cnt >= 256) | (we_tx_err_cnt & (data_in == 255)))
1916 28 mohor
    node_bus_off <=#Tp 1'b1;
1917
end
1918
 
1919
 
1920 35 mohor
 
1921 28 mohor
always @ (posedge clk or posedge rst)
1922
begin
1923
  if (rst)
1924 35 mohor
    bus_free_cnt <= 0;
1925
  else if (reset_mode)
1926
    bus_free_cnt <=#Tp 0;
1927 30 mohor
  else if (sample_point)
1928
    begin
1929 35 mohor
      if (sampled_bit & bus_free_cnt_en & (bus_free_cnt < 10))
1930
        bus_free_cnt <=#Tp bus_free_cnt + 1'b1;
1931 30 mohor
      else
1932 35 mohor
        bus_free_cnt <=#Tp 0;
1933 30 mohor
    end
1934 28 mohor
end
1935
 
1936
 
1937 35 mohor
always @ (posedge clk or posedge rst)
1938
begin
1939
  if (rst)
1940
    bus_free_cnt_en <= 1'b0;
1941
  else if ((~reset_mode) & reset_mode_q | node_bus_off_q & (~reset_mode))
1942
    bus_free_cnt_en <=#Tp 1'b1;
1943
  else if (sample_point &  (bus_free_cnt==10) & (~node_bus_off))
1944
    bus_free_cnt_en <=#Tp 1'b0;
1945
end
1946 28 mohor
 
1947
 
1948 35 mohor
always @ (posedge clk or posedge rst)
1949
begin
1950
  if (rst)
1951
    bus_free <= 1'b0;
1952
  else if (sample_point & sampled_bit & (bus_free_cnt==10))
1953
    bus_free <=#Tp 1'b1;
1954
  else
1955
    bus_free <=#Tp 1'b0;
1956
end
1957
 
1958
 
1959
always @ (posedge clk or posedge rst)
1960
begin
1961
  if (rst)
1962
    waiting_for_bus_free <= 1'b1;
1963
  else if (bus_free & (~node_bus_off))
1964
    waiting_for_bus_free <=#Tp 1'b0;
1965
  else if ((~reset_mode) & reset_mode_q | node_bus_off_q & (~reset_mode))
1966
    waiting_for_bus_free <=#Tp 1'b1;
1967
end
1968
 
1969
 
1970 28 mohor
assign tx_oen = node_bus_off;
1971
 
1972 35 mohor
assign set_reset_mode = node_bus_off & (~node_bus_off_q);
1973
assign error_status = (~reset_mode) & extended_mode? ((rx_err_cnt >= error_warning_limit) | (tx_err_cnt >= error_warning_limit))    :
1974
                                                     ((rx_err_cnt >= 96) | (tx_err_cnt >= 96))                                      ;
1975 28 mohor
 
1976 35 mohor
assign transmit_status = transmitting                 | (extended_mode & waiting_for_bus_free);
1977
assign receive_status  = (~rx_idle) & (~transmitting) | (extended_mode & waiting_for_bus_free);
1978
 
1979 39 mohor
 
1980
/* Error code capture register */
1981
always @ (posedge clk or posedge rst)
1982
begin
1983
  if (rst)
1984
    error_capture_code <= 8'h0;
1985
  else if (read_error_code_capture_reg)
1986
    error_capture_code <=#Tp 8'h0;
1987
  else if (set_bus_error_irq)
1988
    error_capture_code <=#Tp {error_capture_code_type[7:6], error_capture_code_direction, error_capture_code_segment[4:0]};
1989
end
1990
 
1991
 
1992
 
1993
assign error_capture_code_segment[0] = rx_idle | rx_ide | (rx_id2 & (bit_cnt<13)) | rx_r1 | rx_r0 | rx_dlc | rx_ack | rx_ack_lim | error_frame & node_error_active;
1994
assign error_capture_code_segment[1] = rx_idle | rx_id1 | rx_id2 | rx_dlc | rx_data | rx_ack_lim | rx_eof | rx_inter | error_frame & node_error_passive;
1995
assign error_capture_code_segment[2] = (rx_id1 & (bit_cnt>7)) | rx_rtr1 | rx_ide | rx_id2 | rx_rtr2 | rx_r1 | error_frame & node_error_passive | overload_frame;
1996
assign error_capture_code_segment[3] = (rx_id2 & (bit_cnt>4)) | rx_rtr2 | rx_r1 | rx_r0 | rx_dlc | rx_data | rx_crc | rx_crc_lim | rx_ack | rx_ack_lim | rx_eof | overload_frame;
1997
assign error_capture_code_segment[4] = rx_crc_lim | rx_ack | rx_ack_lim | rx_eof | rx_inter | error_frame | overload_frame;
1998
assign error_capture_code_direction  = ~transmitting;
1999
 
2000
 
2001
always @ (bit_err or form_err or stuff_err)
2002
begin
2003
  if (bit_err)
2004
    error_capture_code_type[7:6] <= 2'b00;
2005
  else if (form_err)
2006
    error_capture_code_type[7:6] <= 2'b01;
2007
  else if (stuff_err)
2008
    error_capture_code_type[7:6] <= 2'b10;
2009
  else
2010
    error_capture_code_type[7:6] <= 2'b11;
2011
end
2012
 
2013
 
2014
assign set_bus_error_irq = go_error_frame & (~error_capture_code_blocked);
2015
 
2016
 
2017
always @ (posedge clk or posedge rst)
2018
begin
2019
  if (rst)
2020
    error_capture_code_blocked <= 1'b0;
2021
  else if (read_error_code_capture_reg)
2022
    error_capture_code_blocked <=#Tp 1'b0;
2023
  else if (set_bus_error_irq)
2024
    error_capture_code_blocked <=#Tp 1'b1;
2025
end
2026
 
2027
 
2028 2 mohor
endmodule

powered by: WebSVN 2.1.0

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