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 102

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

powered by: WebSVN 2.1.0

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