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 75

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

powered by: WebSVN 2.1.0

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