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

Subversion Repositories can

[/] [can/] [tags/] [rel_22/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 45

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

powered by: WebSVN 2.1.0

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