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

Subversion Repositories can

[/] [can/] [tags/] [rel_22/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 39

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

powered by: WebSVN 2.1.0

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