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 100

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

powered by: WebSVN 2.1.0

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