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

Subversion Repositories can

[/] [can/] [tags/] [rel_16/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 112

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

powered by: WebSVN 2.1.0

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