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 93

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

powered by: WebSVN 2.1.0

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