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

Subversion Repositories can

[/] [can/] [tags/] [rel_11/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 161

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

powered by: WebSVN 2.1.0

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