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

Subversion Repositories can

[/] [can/] [tags/] [rel_16/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 95

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

powered by: WebSVN 2.1.0

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