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 44

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

powered by: WebSVN 2.1.0

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