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 110

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

powered by: WebSVN 2.1.0

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