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

Subversion Repositories can

[/] [can/] [trunk/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 107

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

powered by: WebSVN 2.1.0

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