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 48

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

powered by: WebSVN 2.1.0

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