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 25

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
//////////////////////////////////////////////////////////////////////
44
//
45
// CVS Revision History
46
//
47
// $Log: not supported by cvs2svn $
48 25 mohor
// Revision 1.15  2003/01/31 01:13:37  mohor
49
// backup.
50
//
51 24 mohor
// Revision 1.14  2003/01/16 13:36:19  mohor
52
// Form error supported. When receiving messages, last bit of the end-of-frame
53
// does not generate form error. Receiver goes to the idle mode one bit sooner.
54
// (CAN specification ver 2.0, part B, page 57).
55
//
56 22 mohor
// Revision 1.13  2003/01/15 21:59:45  mohor
57
// Data is stored to fifo at the end of ack stage.
58
//
59 21 mohor
// Revision 1.12  2003/01/15 21:05:11  mohor
60
// CRC checking fixed (when bitstuff occurs at the end of a CRC sequence).
61
//
62 20 mohor
// Revision 1.11  2003/01/15 14:40:23  mohor
63
// RX state machine fixed to receive "remote request" frames correctly. No data bytes are written to fifo when such frames are received.
64
//
65 19 mohor
// Revision 1.10  2003/01/15 13:16:47  mohor
66
// When a frame with "remote request" is received, no data is stored to fifo, just the frame information (identifier, ...). Data length that is stored is the received data length and not the actual data length that is stored to fifo.
67
//
68 18 mohor
// Revision 1.9  2003/01/14 12:19:35  mohor
69
// rx_fifo is now working.
70
//
71 16 mohor
// Revision 1.8  2003/01/10 17:51:33  mohor
72
// Temporary version (backup).
73
//
74 15 mohor
// Revision 1.7  2003/01/09 21:54:45  mohor
75
// rx fifo added. Not 100 % verified, yet.
76
//
77 14 mohor
// Revision 1.6  2003/01/09 14:46:58  mohor
78
// Temporary files (backup).
79
//
80 13 mohor
// Revision 1.5  2003/01/08 13:30:31  mohor
81
// Temp version.
82
//
83 12 mohor
// Revision 1.4  2003/01/08 02:10:53  mohor
84
// Acceptance filter added.
85
//
86 11 mohor
// Revision 1.3  2002/12/28 04:13:23  mohor
87
// Backup version.
88
//
89 10 mohor
// Revision 1.2  2002/12/27 00:12:52  mohor
90
// Header changed, testbench improved to send a frame (crc still missing).
91
//
92 9 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
93
// Initial
94 2 mohor
//
95
//
96 9 mohor
//
97 2 mohor
 
98
// synopsys translate_off
99
`include "timescale.v"
100
// synopsys translate_on
101
`include "can_defines.v"
102
 
103
module can_bsp
104
(
105
  clk,
106 10 mohor
  rst,
107
 
108
  sample_point,
109
  sampled_bit,
110
  sampled_bit_q,
111 24 mohor
  tx_point,
112 11 mohor
  hard_sync,
113
 
114 14 mohor
  addr,
115
  data_out,
116
 
117
 
118 11 mohor
  /* Mode register */
119 10 mohor
  reset_mode,
120 11 mohor
  acceptance_filter_mode,
121
 
122 15 mohor
  /* Command register */
123
  release_buffer,
124 25 mohor
  tx_request,
125 15 mohor
 
126
  /* Clock Divider register */
127 11 mohor
  extended_mode,
128
 
129
  rx_idle,
130 24 mohor
  transmitting,
131 11 mohor
 
132
  /* This section is for BASIC and EXTENDED mode */
133
  /* Acceptance code register */
134
  acceptance_code_0,
135
 
136
  /* Acceptance mask register */
137
  acceptance_mask_0,
138
  /* End: This section is for BASIC and EXTENDED mode */
139 10 mohor
 
140 11 mohor
  /* This section is for EXTENDED mode */
141
  /* Acceptance code register */
142
  acceptance_code_1,
143
  acceptance_code_2,
144
  acceptance_code_3,
145 10 mohor
 
146 11 mohor
  /* Acceptance mask register */
147
  acceptance_mask_1,
148
  acceptance_mask_2,
149 18 mohor
  acceptance_mask_3,
150 11 mohor
  /* End: This section is for EXTENDED mode */
151 10 mohor
 
152 18 mohor
  /* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
153
  tx_data_0,
154
  tx_data_1,
155
  tx_data_2,
156
  tx_data_3,
157
  tx_data_4,
158
  tx_data_5,
159
  tx_data_6,
160
  tx_data_7,
161
  tx_data_8,
162
  tx_data_9,
163
  tx_data_10,
164
  tx_data_11,
165 24 mohor
  tx_data_12,
166 18 mohor
  /* End: Tx data registers */
167 24 mohor
 
168
  /* Tx signal */
169
  tx
170 18 mohor
 
171 2 mohor
);
172
 
173
parameter Tp = 1;
174
 
175 10 mohor
input         clk;
176
input         rst;
177
input         sample_point;
178
input         sampled_bit;
179
input         sampled_bit_q;
180 24 mohor
input         tx_point;
181 11 mohor
input         hard_sync;
182 14 mohor
input   [7:0] addr;
183
output  [7:0] data_out;
184 11 mohor
 
185 14 mohor
 
186 10 mohor
input         reset_mode;
187 11 mohor
input         acceptance_filter_mode;
188
input         extended_mode;
189 2 mohor
 
190 15 mohor
/* Command register */
191
input         release_buffer;
192 25 mohor
input         tx_request;
193 11 mohor
 
194 10 mohor
output        rx_idle;
195 24 mohor
output        transmitting;
196 2 mohor
 
197 11 mohor
/* This section is for BASIC and EXTENDED mode */
198
/* Acceptance code register */
199
input   [7:0] acceptance_code_0;
200
 
201
/* Acceptance mask register */
202
input   [7:0] acceptance_mask_0;
203
 
204
/* End: This section is for BASIC and EXTENDED mode */
205
 
206
 
207
/* This section is for EXTENDED mode */
208
/* Acceptance code register */
209
input   [7:0] acceptance_code_1;
210
input   [7:0] acceptance_code_2;
211
input   [7:0] acceptance_code_3;
212
 
213
/* Acceptance mask register */
214
input   [7:0] acceptance_mask_1;
215
input   [7:0] acceptance_mask_2;
216
input   [7:0] acceptance_mask_3;
217
/* End: This section is for EXTENDED mode */
218
 
219 24 mohor
/* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
220 18 mohor
input   [7:0] tx_data_0;
221
input   [7:0] tx_data_1;
222
input   [7:0] tx_data_2;
223
input   [7:0] tx_data_3;
224
input   [7:0] tx_data_4;
225
input   [7:0] tx_data_5;
226
input   [7:0] tx_data_6;
227
input   [7:0] tx_data_7;
228
input   [7:0] tx_data_8;
229
input   [7:0] tx_data_9;
230
input   [7:0] tx_data_10;
231
input   [7:0] tx_data_11;
232
input   [7:0] tx_data_12;
233 24 mohor
/* End: Tx data registers */
234 11 mohor
 
235 24 mohor
/* Tx signal */
236
output        tx;
237 11 mohor
 
238 10 mohor
reg           reset_mode_q;
239
reg     [5:0] bit_cnt;
240 2 mohor
 
241 10 mohor
reg     [3:0] data_len;
242
reg    [28:0] id;
243
reg     [2:0] bit_stuff_cnt;
244 25 mohor
reg     [2:0] bit_stuff_cnt_tx;
245 10 mohor
reg           stuff_error;
246 25 mohor
reg           tx_point_q;
247 10 mohor
 
248
wire          bit_de_stuff;
249 25 mohor
wire          bit_de_stuff_tx;
250 10 mohor
 
251
 
252
/* Rx state machine */
253
wire          go_rx_idle;
254
wire          go_rx_id1;
255
wire          go_rx_rtr1;
256
wire          go_rx_ide;
257
wire          go_rx_id2;
258
wire          go_rx_rtr2;
259
wire          go_rx_r1;
260
wire          go_rx_r0;
261
wire          go_rx_dlc;
262
wire          go_rx_data;
263
wire          go_rx_crc;
264 11 mohor
wire          go_rx_crc_lim;
265 10 mohor
wire          go_rx_ack;
266 11 mohor
wire          go_rx_ack_lim;
267 10 mohor
wire          go_rx_eof;
268 24 mohor
wire          go_error_frame;
269
wire          go_overload_frame;
270
wire          go_rx_inter;
271 10 mohor
 
272 11 mohor
wire          go_crc_enable;
273
wire          rst_crc_enable;
274
 
275
wire          bit_de_stuff_set;
276
wire          bit_de_stuff_reset;
277
 
278 25 mohor
wire          go_early_tx;
279
wire          go_tx;
280
 
281 10 mohor
reg           rx_idle;
282
reg           rx_id1;
283
reg           rx_rtr1;
284
reg           rx_ide;
285
reg           rx_id2;
286
reg           rx_rtr2;
287
reg           rx_r1;
288
reg           rx_r0;
289
reg           rx_dlc;
290
reg           rx_data;
291
reg           rx_crc;
292 11 mohor
reg           rx_crc_lim;
293 10 mohor
reg           rx_ack;
294 11 mohor
reg           rx_ack_lim;
295 10 mohor
reg           rx_eof;
296 24 mohor
reg           rx_inter;
297 10 mohor
 
298 19 mohor
reg           rtr1;
299
reg           ide;
300
reg           rtr2;
301
reg    [14:0] crc_in;
302
 
303 24 mohor
reg     [7:0] tmp_data;
304
reg     [7:0] tmp_fifo [0:7];
305
reg           write_data_to_tmp_fifo;
306
reg     [2:0] byte_cnt;
307
reg           bit_stuff_cnt_en;
308 11 mohor
reg           crc_enable;
309
 
310 10 mohor
reg     [2:0] eof_cnt;
311 11 mohor
wire   [14:0] calculated_crc;
312 25 mohor
wire   [15:0] r_calculated_crc;
313 19 mohor
wire          remote_rq;
314 22 mohor
wire    [3:0] limited_data_len;
315 24 mohor
wire          form_error;
316 22 mohor
wire          set_form_error;
317 24 mohor
reg           transmitting;
318 10 mohor
 
319 24 mohor
reg           error_frame;
320
reg           enable_error_cnt2;
321
reg     [2:0] error_cnt1;
322
reg     [2:0] error_cnt2;
323
reg           tx;
324
reg           crc_error;
325
 
326 25 mohor
reg           priority_lost;
327
reg           tx_q;
328
 
329 24 mohor
wire          error_frame_ended;
330
wire          bit_error = 0; // FIX ME !!!
331 25 mohor
wire          acknowledge_error;
332
reg           need_to_tx; // When the CAN core has something to transmit and a dominant bit is sampled at the third bit
333
                          // of intermission, it starts reading the identifier (and transmitting its own). // FIX ME !!!
334 24 mohor
wire          overload_needed = 0;  // When receiver is busy, it needs to send overload frame. Only 2 overload frames are allowed to
335
                                    // be send in a row. Counter?   FIX ME
336
 
337 25 mohor
wire          id_ok;        // If received ID matches ID set in registers
338
wire          no_byte0;     // There is no byte 0 (RTR bit set to 1 or DLC field equal to 0). Signal used for acceptance filter.
339
wire          no_byte1;     // There is no byte 1 (RTR bit set to 1 or DLC field equal to 1). Signal used for acceptance filter.
340
reg     [3:0] data_cnt;     // Counting the data bytes that are written to FIFO
341
reg     [2:0] header_cnt;   // Counting header length
342
reg           wr_fifo;      // Write data and header to 64-byte fifo
343
reg     [7:0] data_for_fifo;// Multiplexed data that is stored to 64-byte fifo
344
 
345
reg     [5:0] tx_pointer;
346
reg           tx_bit;
347
reg           tx_state;
348
reg           finish_msg;
349
 
350
wire    [2:0] header_len;
351
wire          storing_header;
352
wire    [3:0] limited_data_len_minus1;
353
wire          reset_wr_fifo;
354
wire          no_error;
355
 
356
 
357 24 mohor
assign go_rx_idle     =                   sample_point &  sampled_bit & rx_inter & (bit_cnt == 2);  // Look the following line for TX
358
//assign go_rx_id1      =                   sample_point &  (~sampled_bit) & (rx_idle | rx_inter & (bit_cnt == 2) & need_to_tx);
359
assign go_rx_id1      =                   sample_point &  (~sampled_bit) & (rx_idle | rx_inter & (bit_cnt == 2));
360 11 mohor
assign go_rx_rtr1     = (~bit_de_stuff) & sample_point &  rx_id1  & (bit_cnt == 10);
361
assign go_rx_ide      = (~bit_de_stuff) & sample_point &  rx_rtr1;
362
assign go_rx_id2      = (~bit_de_stuff) & sample_point &  rx_ide  &   sampled_bit;
363
assign go_rx_rtr2     = (~bit_de_stuff) & sample_point &  rx_id2  & (bit_cnt == 17);
364
assign go_rx_r1       = (~bit_de_stuff) & sample_point &  rx_rtr2;
365
assign go_rx_r0       = (~bit_de_stuff) & sample_point & (rx_ide  & (~sampled_bit) | rx_r1);
366
assign go_rx_dlc      = (~bit_de_stuff) & sample_point &  rx_r0;
367 19 mohor
assign go_rx_data     = (~bit_de_stuff) & sample_point &  rx_dlc  & (bit_cnt == 3) &  (sampled_bit   |   (|data_len[2:0])) & (~remote_rq);
368
assign go_rx_crc      = (~bit_de_stuff) & sample_point & (rx_dlc  & (bit_cnt == 3) & ((~sampled_bit) & (~(|data_len[2:0])) | remote_rq) |
369
                                                          rx_data & (bit_cnt == ((limited_data_len<<3) - 1'b1)));
370 20 mohor
assign go_rx_crc_lim  = (~bit_de_stuff) & sample_point &  rx_crc  & (bit_cnt == 14);
371 11 mohor
assign go_rx_ack      =                   sample_point &  rx_crc_lim;
372
assign go_rx_ack_lim  =                   sample_point &  rx_ack;
373
assign go_rx_eof      =                   sample_point &  rx_ack_lim  | (~reset_mode) & reset_mode_q;
374 24 mohor
assign go_rx_inter    =                 ((sample_point &  rx_eof  & (eof_cnt == 6)) | error_frame_ended) & (~overload_needed);
375 10 mohor
 
376 24 mohor
assign go_error_frame = form_error | stuff_error | bit_error | acknowledge_error | (crc_error & go_rx_eof);
377
assign error_frame_ended = (error_cnt2 == 7) & tx_point;
378
 
379
assign go_overload_frame = ((sample_point &  rx_eof  & (eof_cnt == 6)) | error_frame_ended) & overload_needed |
380
                             sample_point & (~sampled_bit) & rx_inter & ((bit_cnt == 0) | (bit_cnt == 1))     |
381
                             sample_point & (~sampled_bit) & (error_cnt2 == 7)
382
                            ;
383
 
384 25 mohor
 
385
assign go_crc_enable  = hard_sync | go_tx;
386 11 mohor
assign rst_crc_enable = go_rx_crc;
387 10 mohor
 
388 11 mohor
assign bit_de_stuff_set   = go_rx_id1;
389 24 mohor
assign bit_de_stuff_reset = go_rx_crc_lim | reset_mode | go_error_frame;
390 11 mohor
 
391 19 mohor
assign remote_rq = ((~ide) & rtr1) | (ide & rtr2);
392
assign limited_data_len = (data_len < 8)? data_len : 4'h8;
393 11 mohor
 
394 25 mohor
assign acknowledge_error = rx_ack & sample_point & sampled_bit & tx_state;
395 19 mohor
 
396 25 mohor
 
397
 
398 10 mohor
// Rx idle state
399
always @ (posedge clk or posedge rst)
400
begin
401
  if (rst)
402 24 mohor
    rx_idle <= 1'b0;
403
  else if (reset_mode | go_rx_id1 | error_frame)
404 10 mohor
    rx_idle <=#Tp 1'b0;
405
  else if (go_rx_idle)
406
    rx_idle <=#Tp 1'b1;
407
end
408
 
409
 
410
// Rx id1 state
411
always @ (posedge clk or posedge rst)
412
begin
413
  if (rst)
414
    rx_id1 <= 1'b0;
415 24 mohor
  else if (reset_mode | go_rx_rtr1 | error_frame)
416 10 mohor
    rx_id1 <=#Tp 1'b0;
417
  else if (go_rx_id1)
418
    rx_id1 <=#Tp 1'b1;
419
end
420
 
421
 
422
// Rx rtr1 state
423
always @ (posedge clk or posedge rst)
424
begin
425
  if (rst)
426
    rx_rtr1 <= 1'b0;
427 24 mohor
  else if (reset_mode | go_rx_ide | error_frame)
428 10 mohor
    rx_rtr1 <=#Tp 1'b0;
429
  else if (go_rx_rtr1)
430
    rx_rtr1 <=#Tp 1'b1;
431
end
432
 
433
 
434
// Rx ide state
435
always @ (posedge clk or posedge rst)
436
begin
437
  if (rst)
438
    rx_ide <= 1'b0;
439 24 mohor
  else if (reset_mode | go_rx_r0 | go_rx_id2 | error_frame)
440 10 mohor
    rx_ide <=#Tp 1'b0;
441
  else if (go_rx_ide)
442
    rx_ide <=#Tp 1'b1;
443
end
444
 
445
 
446
// Rx id2 state
447
always @ (posedge clk or posedge rst)
448
begin
449
  if (rst)
450
    rx_id2 <= 1'b0;
451 24 mohor
  else if (reset_mode | go_rx_rtr2 | error_frame)
452 10 mohor
    rx_id2 <=#Tp 1'b0;
453
  else if (go_rx_id2)
454
    rx_id2 <=#Tp 1'b1;
455
end
456
 
457
 
458
// Rx rtr2 state
459
always @ (posedge clk or posedge rst)
460
begin
461
  if (rst)
462
    rx_rtr2 <= 1'b0;
463 24 mohor
  else if (reset_mode | go_rx_r1 | error_frame)
464 10 mohor
    rx_rtr2 <=#Tp 1'b0;
465
  else if (go_rx_rtr2)
466
    rx_rtr2 <=#Tp 1'b1;
467
end
468
 
469
 
470
// Rx r0 state
471
always @ (posedge clk or posedge rst)
472
begin
473
  if (rst)
474
    rx_r1 <= 1'b0;
475 24 mohor
  else if (reset_mode | go_rx_r0 | error_frame)
476 10 mohor
    rx_r1 <=#Tp 1'b0;
477
  else if (go_rx_r1)
478
    rx_r1 <=#Tp 1'b1;
479
end
480
 
481
 
482
// Rx r0 state
483
always @ (posedge clk or posedge rst)
484
begin
485
  if (rst)
486
    rx_r0 <= 1'b0;
487 24 mohor
  else if (reset_mode | go_rx_dlc | error_frame)
488 10 mohor
    rx_r0 <=#Tp 1'b0;
489
  else if (go_rx_r0)
490
    rx_r0 <=#Tp 1'b1;
491
end
492
 
493
 
494
// Rx dlc state
495
always @ (posedge clk or posedge rst)
496
begin
497
  if (rst)
498
    rx_dlc <= 1'b0;
499 24 mohor
  else if (reset_mode | go_rx_data | go_rx_crc | error_frame)
500 10 mohor
    rx_dlc <=#Tp 1'b0;
501
  else if (go_rx_dlc)
502
    rx_dlc <=#Tp 1'b1;
503
end
504
 
505
 
506
// Rx data state
507
always @ (posedge clk or posedge rst)
508
begin
509
  if (rst)
510
    rx_data <= 1'b0;
511 24 mohor
  else if (reset_mode | go_rx_crc | error_frame)
512 10 mohor
    rx_data <=#Tp 1'b0;
513
  else if (go_rx_data)
514
    rx_data <=#Tp 1'b1;
515
end
516
 
517
 
518
// Rx crc state
519
always @ (posedge clk or posedge rst)
520
begin
521
  if (rst)
522
    rx_crc <= 1'b0;
523 24 mohor
  else if (reset_mode | go_rx_crc_lim | error_frame)
524 10 mohor
    rx_crc <=#Tp 1'b0;
525
  else if (go_rx_crc)
526
    rx_crc <=#Tp 1'b1;
527
end
528
 
529
 
530 11 mohor
// Rx crc delimiter state
531
always @ (posedge clk or posedge rst)
532
begin
533
  if (rst)
534
    rx_crc_lim <= 1'b0;
535 24 mohor
  else if (reset_mode | go_rx_ack | error_frame)
536 11 mohor
    rx_crc_lim <=#Tp 1'b0;
537
  else if (go_rx_crc_lim)
538
    rx_crc_lim <=#Tp 1'b1;
539
end
540
 
541
 
542 10 mohor
// Rx ack state
543
always @ (posedge clk or posedge rst)
544
begin
545
  if (rst)
546
    rx_ack <= 1'b0;
547 24 mohor
  else if (reset_mode | go_rx_ack_lim | error_frame)
548 10 mohor
    rx_ack <=#Tp 1'b0;
549
  else if (go_rx_ack)
550
    rx_ack <=#Tp 1'b1;
551
end
552
 
553
 
554 11 mohor
// Rx ack delimiter state
555
always @ (posedge clk or posedge rst)
556
begin
557
  if (rst)
558
    rx_ack_lim <= 1'b0;
559 24 mohor
  else if (reset_mode | go_rx_eof | error_frame)
560 11 mohor
    rx_ack_lim <=#Tp 1'b0;
561
  else if (go_rx_ack_lim)
562
    rx_ack_lim <=#Tp 1'b1;
563
end
564
 
565
 
566 10 mohor
// Rx eof state
567
always @ (posedge clk or posedge rst)
568
begin
569
  if (rst)
570
    rx_eof <= 1'b0;
571 24 mohor
  else if (go_rx_inter | error_frame)
572 10 mohor
    rx_eof <=#Tp 1'b0;
573
  else if (go_rx_eof)
574
    rx_eof <=#Tp 1'b1;
575
end
576
 
577
 
578 24 mohor
 
579
// Interframe space
580
always @ (posedge clk or posedge rst)
581
begin
582
  if (rst)
583
    rx_inter <= 1'b0;
584
  else if (go_rx_idle | go_rx_id1 | go_overload_frame | go_error_frame)
585
    rx_inter <=#Tp 1'b0;
586
  else if (go_rx_inter)
587
    rx_inter <=#Tp 1'b1;
588
end
589
 
590
 
591 10 mohor
// ID register
592
always @ (posedge clk or posedge rst)
593
begin
594
  if (rst)
595
    id <= 0;
596 11 mohor
  else if (sample_point & (rx_id1 | rx_id2) & (~bit_de_stuff))
597 10 mohor
    id <=#Tp {id[27:0], sampled_bit};
598
end
599
 
600 11 mohor
 
601
// rtr1 bit
602
always @ (posedge clk or posedge rst)
603
begin
604
  if (rst)
605
    rtr1 <= 0;
606
  else if (sample_point & rx_rtr1 & (~bit_de_stuff))
607
    rtr1 <=#Tp sampled_bit;
608
end
609
 
610
 
611
// rtr2 bit
612
always @ (posedge clk or posedge rst)
613
begin
614
  if (rst)
615
    rtr2 <= 0;
616
  else if (sample_point & rx_rtr2 & (~bit_de_stuff))
617
    rtr2 <=#Tp sampled_bit;
618
end
619
 
620
 
621
// ide bit
622
always @ (posedge clk or posedge rst)
623
begin
624
  if (rst)
625
    ide <= 0;
626
  else if (sample_point & rx_ide & (~bit_de_stuff))
627
    ide <=#Tp sampled_bit;
628
end
629
 
630
 
631 10 mohor
// Data length
632
always @ (posedge clk or posedge rst)
633
begin
634
  if (rst)
635
    data_len <= 0;
636
  else if (sample_point & rx_dlc & (~bit_de_stuff))
637
    data_len <=#Tp {data_len[2:0], sampled_bit};
638
end
639
 
640
 
641 11 mohor
// Data
642
always @ (posedge clk or posedge rst)
643
begin
644
  if (rst)
645
    tmp_data <= 0;
646
  else if (sample_point & rx_data & (~bit_de_stuff))
647
    tmp_data <=#Tp {tmp_data[6:0], sampled_bit};
648
end
649
 
650
 
651
always @ (posedge clk or posedge rst)
652
begin
653
  if (rst)
654
    write_data_to_tmp_fifo <= 0;
655
  else if (sample_point & rx_data & (~bit_de_stuff) & (&bit_cnt[2:0]))
656
    write_data_to_tmp_fifo <=#Tp 1'b1;
657
  else
658
    write_data_to_tmp_fifo <=#Tp 0;
659
end
660
 
661
 
662
always @ (posedge clk or posedge rst)
663
begin
664
  if (rst)
665
    byte_cnt <= 0;
666
  else if (write_data_to_tmp_fifo)
667
    byte_cnt <=#Tp byte_cnt + 1;
668
  else if (sample_point & go_rx_crc_lim)
669
    byte_cnt <=#Tp 0;
670
end
671
 
672
 
673 24 mohor
always @ (posedge clk)
674 11 mohor
begin
675
  if (write_data_to_tmp_fifo)
676
    tmp_fifo[byte_cnt] <=#Tp tmp_data;
677
end
678
 
679
 
680
 
681
// CRC
682
always @ (posedge clk or posedge rst)
683
begin
684
  if (rst)
685
    crc_in <= 0;
686
  else if (sample_point & rx_crc & (~bit_de_stuff))
687
    crc_in <=#Tp {crc_in[13:0], sampled_bit};
688
end
689
 
690
 
691 10 mohor
// bit_cnt
692
always @ (posedge clk or posedge rst)
693
begin
694
  if (rst)
695
    bit_cnt <= 0;
696 24 mohor
  else if (go_rx_id1 | go_rx_id2 | go_rx_dlc | go_rx_data | go_rx_crc |
697
           go_rx_ack | go_rx_eof | go_rx_inter | go_error_frame | go_overload_frame)
698 10 mohor
    bit_cnt <=#Tp 0;
699 11 mohor
  else if (sample_point & (~bit_de_stuff))
700 10 mohor
    bit_cnt <=#Tp bit_cnt + 1'b1;
701
end
702
 
703
 
704
// eof_cnt
705
always @ (posedge clk or posedge rst)
706
begin
707
  if (rst)
708
    eof_cnt <= 0;
709
  else if (sample_point)
710
    begin
711
      if (rx_eof & sampled_bit)
712
        eof_cnt <=#Tp eof_cnt + 1'b1;
713
      else
714
        eof_cnt <=#Tp 0;
715
    end
716
end
717
 
718
 
719 11 mohor
// Enabling bit de-stuffing
720
always @ (posedge clk or posedge rst)
721
begin
722
  if (rst)
723
    bit_stuff_cnt_en <= 1'b0;
724
  else if (bit_de_stuff_set)
725
    bit_stuff_cnt_en <=#Tp 1'b1;
726
  else if (bit_de_stuff_reset)
727
    bit_stuff_cnt_en <=#Tp 1'b0;
728
end
729 10 mohor
 
730 24 mohor
 
731 10 mohor
// bit_stuff_cnt
732
always @ (posedge clk or posedge rst)
733
begin
734
  if (rst)
735
    bit_stuff_cnt <= 1;
736 15 mohor
  else if (bit_de_stuff_reset)
737
    bit_stuff_cnt <=#Tp 1;
738 11 mohor
  else if (sample_point & bit_stuff_cnt_en)
739 10 mohor
    begin
740
      if (bit_stuff_cnt == 5)
741
        bit_stuff_cnt <=#Tp 1;
742
      else if (sampled_bit == sampled_bit_q)
743
        bit_stuff_cnt <=#Tp bit_stuff_cnt + 1'b1;
744
      else
745
        bit_stuff_cnt <=#Tp 1;
746
    end
747
end
748
 
749
 
750 25 mohor
// bit_stuff_cnt_tx
751
always @ (posedge clk or posedge rst)
752
begin
753
  if (rst)
754
    bit_stuff_cnt_tx <= 1;
755
  else if (bit_de_stuff_reset)
756
    bit_stuff_cnt_tx <=#Tp 1;
757
  else if (tx_point_q & bit_stuff_cnt_en)
758
    begin
759
      if (bit_stuff_cnt_tx == 5)
760
        bit_stuff_cnt_tx <=#Tp 1;
761
      else if (tx == tx_q)
762
        bit_stuff_cnt_tx <=#Tp bit_stuff_cnt_tx + 1'b1;
763
      else
764
        bit_stuff_cnt_tx <=#Tp 1;
765
    end
766
end
767
 
768
 
769 10 mohor
assign bit_de_stuff = bit_stuff_cnt == 5;
770 25 mohor
assign bit_de_stuff_tx = bit_stuff_cnt_tx == 5;
771 10 mohor
 
772
 
773 24 mohor
 
774 10 mohor
// stuff_error
775
always @ (posedge clk or posedge rst)
776
begin
777
  if (rst)
778
    stuff_error <= 0;
779 24 mohor
  else if (reset_mode | go_rx_idle | error_frame)     // Stuff error might reset itself
780
    stuff_error <=#Tp 0;
781
  else if (sample_point & bit_stuff_cnt_en & bit_de_stuff & (sampled_bit == sampled_bit_q))
782 10 mohor
    stuff_error <=#Tp 1'b1;
783
end
784
 
785
 
786
// Generating delayed reset_mode signal
787
always @ (posedge clk)
788
begin
789
  reset_mode_q <=#Tp reset_mode;
790
end
791
 
792
 
793 11 mohor
 
794
always @ (posedge clk or posedge rst)
795
begin
796
  if (rst)
797
    crc_enable <= 1'b0;
798
  else if (go_crc_enable)
799
    crc_enable <=#Tp 1'b1;
800
  else if (reset_mode | rst_crc_enable)
801
    crc_enable <=#Tp 1'b0;
802
end
803
 
804
 
805
// CRC error generation
806
always @ (posedge clk or posedge rst)
807
begin
808
  if (rst)
809
    crc_error <= 0;
810 12 mohor
  else if (go_rx_ack)
811 11 mohor
    crc_error <=#Tp crc_in != calculated_crc;
812 25 mohor
  else if (reset_mode | go_rx_idle | error_frame)
813 11 mohor
    crc_error <=#Tp 0;
814
end
815
 
816
 
817 22 mohor
// Conditions for form error
818 24 mohor
assign     form_error = sample_point & ( (~bit_de_stuff) & rx_ide     &   sampled_bit & (~rtr1) |
819 22 mohor
                                                           rx_crc_lim & (~sampled_bit)          |
820
                                                           rx_ack_lim & (~sampled_bit)          |
821
                                                           rx_eof     & (~sampled_bit)
822
                                       );
823 11 mohor
 
824 22 mohor
 
825 11 mohor
// Instantiation of the RX CRC module
826
can_crc i_can_crc_rx
827
(
828
  .clk(clk),
829 25 mohor
  .data(sampled_bit),
830 11 mohor
  .enable(crc_enable & sample_point & (~bit_de_stuff)),
831
  .initialize(rx_eof),
832
  .crc(calculated_crc)
833
);
834
 
835
 
836
 
837
 
838 16 mohor
assign no_byte0 = rtr1 | (data_len<1);
839
assign no_byte1 = rtr1 | (data_len<2);
840 11 mohor
 
841
can_acf i_can_acf
842
(
843
  .clk(clk),
844
  .rst(rst),
845
 
846
  .id(id),
847
 
848
  /* Mode register */
849
  .reset_mode(reset_mode),
850
  .acceptance_filter_mode(acceptance_filter_mode),
851
 
852 12 mohor
  // Clock Divider register
853 11 mohor
  .extended_mode(extended_mode),
854
 
855
  /* This section is for BASIC and EXTENDED mode */
856
  /* Acceptance code register */
857
  .acceptance_code_0(acceptance_code_0),
858
 
859
  /* Acceptance mask register */
860
  .acceptance_mask_0(acceptance_mask_0),
861
  /* End: This section is for BASIC and EXTENDED mode */
862
 
863
  /* This section is for EXTENDED mode */
864
  /* Acceptance code register */
865
  .acceptance_code_1(acceptance_code_1),
866
  .acceptance_code_2(acceptance_code_2),
867
  .acceptance_code_3(acceptance_code_3),
868
 
869
  /* Acceptance mask register */
870
  .acceptance_mask_1(acceptance_mask_1),
871
  .acceptance_mask_2(acceptance_mask_2),
872
  .acceptance_mask_3(acceptance_mask_3),
873
  /* End: This section is for EXTENDED mode */
874
 
875
  .go_rx_crc_lim(go_rx_crc_lim),
876
  .go_rx_idle(go_rx_idle),
877
 
878
  .data0(tmp_fifo[0]),
879
  .data1(tmp_fifo[1]),
880
  .rtr1(rtr1),
881
  .rtr2(rtr2),
882
  .ide(ide),
883 16 mohor
  .no_byte0(no_byte0),
884
  .no_byte1(no_byte1),
885 11 mohor
 
886
  .id_ok(id_ok)
887
 
888
);
889
 
890
 
891
 
892
 
893 16 mohor
assign header_len[2:0] = extended_mode ? (ide? (3'h5) : (3'h3)) : 3'h2;
894 13 mohor
assign storing_header = header_cnt < header_len;
895 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
896 25 mohor
assign reset_wr_fifo = (data_cnt == (limited_data_len_minus1 + header_len)) | reset_mode;
897 24 mohor
assign no_error = (~crc_error) & (~form_error) & (~stuff_error);
898 12 mohor
 
899 24 mohor
 
900
 
901 13 mohor
// Write enable signal for 64-byte rx fifo
902
always @ (posedge clk or posedge rst)
903
begin
904
  if (rst)
905
    wr_fifo <= 1'b0;
906 16 mohor
  else if (reset_wr_fifo)
907
    wr_fifo <=#Tp 1'b0;
908 25 mohor
  else if (go_rx_inter & id_ok & (~error_frame_ended))    // FIX ME !!! Look following line
909
//  else if (go_rx_inter & id_ok & (~error_frame_ended) & (~tx_state))  FIX ME !!! This line is the correct one. The above line is for easier debugging only.
910 13 mohor
    wr_fifo <=#Tp 1'b1;
911
end
912 12 mohor
 
913
 
914 13 mohor
// Header counter. Header length depends on the mode of operation and frame format.
915 12 mohor
always @ (posedge clk or posedge rst)
916
begin
917
  if (rst)
918 13 mohor
    header_cnt <= 0;
919 16 mohor
  else if (reset_wr_fifo)
920
    header_cnt <=#Tp 0;
921
  else if (wr_fifo & storing_header)
922 13 mohor
    header_cnt <=#Tp header_cnt + 1;
923 12 mohor
end
924
 
925
 
926 13 mohor
// Data counter. Length of the data is limited to 8 bytes.
927 12 mohor
always @ (posedge clk or posedge rst)
928
begin
929
  if (rst)
930 13 mohor
    data_cnt <= 0;
931 16 mohor
  else if (reset_wr_fifo)
932
    data_cnt <=#Tp 0;
933
  else if (wr_fifo)
934 13 mohor
    data_cnt <=#Tp data_cnt + 1;
935 12 mohor
end
936
 
937
 
938 13 mohor
// Multiplexing data that is stored to 64-byte fifo depends on the mode of operation and frame format
939 24 mohor
always @ (extended_mode or ide or data_cnt or header_cnt or  header_len or
940
          storing_header or id or rtr1 or rtr2 or data_len or
941 13 mohor
          tmp_fifo[0] or tmp_fifo[2] or tmp_fifo[4] or tmp_fifo[6] or
942
          tmp_fifo[1] or tmp_fifo[3] or tmp_fifo[5] or tmp_fifo[7])
943 12 mohor
begin
944 13 mohor
  if (storing_header)
945 12 mohor
    begin
946 13 mohor
      if (extended_mode)      // extended mode
947 12 mohor
        begin
948 13 mohor
          if (ide)              // extended format
949
            begin
950 24 mohor
              case (header_cnt) // synthesis parallel_case 
951
                3'h0  : data_for_fifo <= {1'b1, rtr2, 2'h0, data_len};
952
                3'h1  : data_for_fifo <= id[28:21];
953
                3'h2  : data_for_fifo <= id[20:13];
954
                3'h3  : data_for_fifo <= id[12:5];
955
                3'h4  : data_for_fifo <= {id[4:0], 3'h0};
956
                default: data_for_fifo <= 0;
957 13 mohor
              endcase
958
            end
959
          else                  // standard format
960
            begin
961 24 mohor
              case (header_cnt) // synthesis parallel_case 
962
                3'h0  : data_for_fifo <= {1'b0, rtr1, 2'h0, data_len};
963
                3'h1  : data_for_fifo <= id[10:3];
964
                3'h2  : data_for_fifo <= {id[2:0], 5'h0};
965
                default: data_for_fifo <= 0;
966 13 mohor
              endcase
967
            end
968 12 mohor
        end
969 13 mohor
      else                    // normal mode
970 12 mohor
        begin
971 24 mohor
          case (header_cnt) // synthesis parallel_case 
972
            3'h0  : data_for_fifo <= id[10:3];
973
            3'h1  : data_for_fifo <= {id[2:0], rtr1, data_len};
974
            default: data_for_fifo <= 0;
975 12 mohor
          endcase
976
        end
977
    end
978 13 mohor
  else
979 16 mohor
    data_for_fifo <= tmp_fifo[data_cnt-header_len];
980 12 mohor
end
981
 
982
 
983
 
984
 
985
// Instantiation of the RX fifo module
986 13 mohor
can_fifo i_can_fifo
987 12 mohor
(
988
  .clk(clk),
989
  .rst(rst),
990
 
991 13 mohor
  .wr(wr_fifo),
992 12 mohor
 
993 13 mohor
  .data_in(data_for_fifo),
994 14 mohor
  .addr(addr),
995
  .data_out(data_out),
996 12 mohor
 
997
  .reset_mode(reset_mode),
998 15 mohor
  .release_buffer(release_buffer),
999 14 mohor
  .extended_mode(extended_mode)
1000 12 mohor
 
1001
 
1002
);
1003
 
1004
 
1005
 
1006 24 mohor
// transmitting signals that core is a transmitter. No synchronization is done meanwhile.
1007
always @ (posedge clk or posedge rst)
1008
begin
1009
  if (rst)
1010
    transmitting <= 1'b0;
1011 25 mohor
  else if (go_rx_idle | reset_mode | priority_lost)
1012 24 mohor
    transmitting <=#Tp 1'b0;
1013 25 mohor
  else if (~no_error | go_tx)
1014 24 mohor
    transmitting <=#Tp 1'b1;
1015
end
1016 12 mohor
 
1017
 
1018
 
1019 24 mohor
// Transmitting error frame. The same counters are used for sending overload frame, too.
1020
always @ (posedge clk or posedge rst)
1021
begin
1022
  if (rst)
1023
    error_frame <= 1'b0;
1024
  else if (reset_mode | error_frame_ended)
1025
    error_frame <=#Tp 1'b0;
1026
  else if (go_error_frame | go_overload_frame)
1027
    error_frame <=#Tp 1'b1;
1028
end
1029 12 mohor
 
1030 13 mohor
 
1031 24 mohor
always @ (posedge clk or posedge rst)
1032
begin
1033
  if (rst)
1034
    error_cnt1 <= 1'b0;
1035
  else if (reset_mode | error_frame_ended)
1036
    error_cnt1 <=#Tp 1'b0;
1037
  else if (error_frame & tx_point & (error_cnt1 < 6))
1038
    error_cnt1 <=#Tp error_cnt1 + 1'b1;
1039
end
1040
 
1041
 
1042
always @ (posedge clk or posedge rst)
1043
begin
1044
  if (rst)
1045
    enable_error_cnt2 <= 1'b0;
1046
  else if (reset_mode | error_frame_ended)
1047
    enable_error_cnt2 <=#Tp 1'b0;
1048
  else if (sample_point & sampled_bit & (error_cnt1 == 6))
1049
    enable_error_cnt2 <=#Tp 1'b1;
1050
end
1051
 
1052
 
1053
 
1054
always @ (posedge clk or posedge rst)
1055
begin
1056
  if (rst)
1057
    error_cnt2 <= 1'b0;
1058
  else if (reset_mode | error_frame_ended)
1059
    error_cnt2 <=#Tp 1'b0;
1060
  else if (enable_error_cnt2 & tx_point)
1061
    error_cnt2 <=#Tp error_cnt2 + 1'b1;
1062
end
1063
 
1064
 
1065 25 mohor
wire node_error_passive = 1;    // FIX ME
1066 24 mohor
 
1067 25 mohor
wire [18:0] basic_chain;
1068
wire [63:0] basic_chain_data;
1069
wire [18:0] extended_chain_std;
1070
wire [38:0] extended_chain_ext;
1071
wire [63:0] extended_chain_data;
1072
 
1073
wire        rst_tx_pointer;
1074
 
1075
wire  [7:0] r_tx_data_0;
1076
wire  [7:0] r_tx_data_1;
1077
wire  [7:0] r_tx_data_2;
1078
wire  [7:0] r_tx_data_3;
1079
wire  [7:0] r_tx_data_4;
1080
wire  [7:0] r_tx_data_5;
1081
wire  [7:0] r_tx_data_6;
1082
wire  [7:0] r_tx_data_7;
1083
wire  [7:0] r_tx_data_8;
1084
wire  [7:0] r_tx_data_9;
1085
wire  [7:0] r_tx_data_10;
1086
wire  [7:0] r_tx_data_11;
1087
wire  [7:0] r_tx_data_12;
1088
 
1089
 
1090
 
1091 24 mohor
always @ (posedge clk or posedge rst)
1092
begin
1093
  if (rst)
1094
    tx <= 1'b1;
1095 25 mohor
  else if (reset_mode | error_frame_ended)                                      // Reset
1096 24 mohor
    tx <=#Tp 1'b1;
1097 25 mohor
  else if (tx_point)
1098 24 mohor
    begin
1099 25 mohor
      if (tx_state)                                                             // Transmitting message
1100
        tx <=#Tp ((~bit_de_stuff_tx) & tx_bit) | (bit_de_stuff_tx & (~tx_q));
1101
      else if ((~tx_state) & rx_ack & no_error)                                 // Acknowledge
1102
        tx <=#Tp 1'b0;
1103
      else if (error_frame)                                                     // Transmitting error frame
1104 24 mohor
        begin
1105 25 mohor
          if (error_cnt1 < 6)
1106
            begin
1107
              if (node_error_passive)
1108
                tx <=#Tp 1'b1;
1109
              else
1110
                tx <=#Tp 1'b0;
1111
            end
1112
          else if (error_cnt2 < 7)
1113 24 mohor
            tx <=#Tp 1'b1;
1114
        end
1115 25 mohor
      else
1116 24 mohor
        tx <=#Tp 1'b1;
1117
    end
1118
end
1119
 
1120
 
1121 25 mohor
always @ (posedge clk)
1122
begin
1123
  if (tx_point)
1124
    tx_q <=#Tp tx;
1125
end
1126 24 mohor
 
1127
 
1128 25 mohor
/* Delayed tx point */
1129
always @ (posedge clk)
1130
begin
1131
  tx_point_q <=#Tp tx_point;
1132
end
1133 24 mohor
 
1134
 
1135 25 mohor
/* Changing bit order from [7:0] to [0:7] */
1136
can_ibo i_ibo_tx_data_0  (.di(tx_data_0),  .do(r_tx_data_0));
1137
can_ibo i_ibo_tx_data_1  (.di(tx_data_1),  .do(r_tx_data_1));
1138
can_ibo i_ibo_tx_data_2  (.di(tx_data_2),  .do(r_tx_data_2));
1139
can_ibo i_ibo_tx_data_3  (.di(tx_data_3),  .do(r_tx_data_3));
1140
can_ibo i_ibo_tx_data_4  (.di(tx_data_4),  .do(r_tx_data_4));
1141
can_ibo i_ibo_tx_data_5  (.di(tx_data_5),  .do(r_tx_data_5));
1142
can_ibo i_ibo_tx_data_6  (.di(tx_data_6),  .do(r_tx_data_6));
1143
can_ibo i_ibo_tx_data_7  (.di(tx_data_7),  .do(r_tx_data_7));
1144
can_ibo i_ibo_tx_data_8  (.di(tx_data_8),  .do(r_tx_data_8));
1145
can_ibo i_ibo_tx_data_9  (.di(tx_data_9),  .do(r_tx_data_9));
1146
can_ibo i_ibo_tx_data_10 (.di(tx_data_10), .do(r_tx_data_10));
1147
can_ibo i_ibo_tx_data_11 (.di(tx_data_11), .do(r_tx_data_11));
1148
can_ibo i_ibo_tx_data_12 (.di(tx_data_12), .do(r_tx_data_12));
1149 24 mohor
 
1150 25 mohor
/* Changing bit order from [14:0] to [0:14] */
1151
can_ibo i_calculated_crc0 (.di(calculated_crc[14:7]), .do(r_calculated_crc[7:0]));
1152
can_ibo i_calculated_crc1 (.di({calculated_crc[6:0], 1'b0}), .do(r_calculated_crc[15:8]));
1153 24 mohor
 
1154
 
1155 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};
1156
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};
1157
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};
1158
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};
1159
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};
1160
 
1161
always @ (extended_mode or rx_data or tx_pointer or extended_chain_data or rx_crc or r_calculated_crc or
1162
          r_tx_data_0   or extended_chain_ext or extended_chain_std or basic_chain_data or basic_chain or
1163
          finish_msg)
1164
begin
1165
  if (extended_mode)
1166
    begin
1167
      if (rx_data)  // data stage
1168
        tx_bit = extended_chain_data[tx_pointer];
1169
      else if (rx_crc)
1170
        tx_bit = r_calculated_crc[tx_pointer];
1171
      else if (finish_msg)
1172
        tx_bit = 1'b1;
1173
      else
1174
        begin
1175
          if (r_tx_data_0[0])    // Extended frame
1176
            tx_bit = extended_chain_ext[tx_pointer];
1177
          else
1178
            tx_bit = extended_chain_std[tx_pointer];
1179
        end
1180
    end
1181
  else  // Basic mode
1182
    begin
1183
      if (rx_data)  // data stage
1184
        tx_bit = basic_chain_data[tx_pointer];
1185
      else if (rx_crc)
1186
        tx_bit = r_calculated_crc[tx_pointer];
1187
      else if (finish_msg)
1188
        tx_bit = 1'b1;
1189
      else
1190
        tx_bit = basic_chain[tx_pointer];
1191
    end
1192
end
1193
 
1194
 
1195
assign rst_tx_pointer = ((~bit_de_stuff_tx) & tx_point & (~rx_data) &   extended_mode  & tx_pointer == 38                      ) |   // arbitration + control for extended format
1196
                        ((~bit_de_stuff_tx) & tx_point & (~rx_data) & (~extended_mode) & tx_pointer == 18                      ) |   // arbitration + control for standard format
1197
                        ((~bit_de_stuff_tx) & tx_point &   rx_data  &   extended_mode  & tx_pointer == (8 * tx_data_0[3:0] - 1)) |   // data
1198
                        ((~bit_de_stuff_tx) & tx_point &   rx_data  & (~extended_mode) & tx_pointer == (8 * tx_data_1[3:0] - 1)) |   // data
1199
                        (                     tx_point &   rx_crc_lim                                                          ) |   // crc
1200
                        (go_rx_idle                                                                                            ) |   // at the end
1201
                        (reset_mode                                                                                            ) |
1202
                        (error_frame                                                                                           ) ;   // FIX ME (not sure this is ok)
1203
 
1204
always @ (posedge clk or posedge rst)
1205
begin
1206
  if (rst)
1207
    tx_pointer <= 'h0;
1208
  else if (rst_tx_pointer)
1209
    tx_pointer <=#Tp 'h0;
1210
  else if (go_early_tx | (tx_point & tx_state & (~bit_de_stuff_tx)))
1211
    tx_pointer <=#Tp tx_pointer + 1'b1;
1212
end
1213
 
1214
 
1215
wire rst_need_to_tx = go_rx_inter & (~error_frame & (~priority_lost));    // FIX ME !!! When there is no error until the end-of-frame, tx is ok (finished).
1216
always @ (posedge clk or posedge rst)
1217
begin
1218
  if (rst)
1219
    need_to_tx <= 1'b0;
1220
  else if (rst_need_to_tx)
1221
    need_to_tx <=#Tp 'h0;
1222
  else if (tx_request)
1223
    need_to_tx <=#Tp 1'b1;
1224
end
1225
 
1226
 
1227
 
1228
assign go_early_tx      = need_to_tx & (~tx_state) & sample_point & (~sampled_bit) & (rx_idle | rx_inter & (bit_cnt == 2));
1229
assign go_tx            = need_to_tx & (~tx_state) & sample_point & (go_early_tx | sampled_bit & rx_idle);
1230
 
1231
 
1232
// Tx state
1233
always @ (posedge clk or posedge rst)
1234
begin
1235
  if (rst)
1236
    tx_state <= 1'b0;
1237
  else if (go_rx_inter | error_frame | priority_lost)
1238
    tx_state <=#Tp 1'b0;
1239
  else if (go_tx)
1240
    tx_state <=#Tp 1'b1;
1241
end
1242
 
1243
 
1244
 
1245
always @ (posedge clk or posedge rst)
1246
begin
1247
  if (rst)
1248
    finish_msg <= 1'b0;
1249
  else if (go_rx_idle | error_frame | reset_mode)
1250
    finish_msg <=#Tp 1'b0;
1251
  else if (go_rx_crc_lim)
1252
    finish_msg <=#Tp 1'b1;
1253
end
1254
 
1255
 
1256
 
1257
always @ (posedge clk or posedge rst)
1258
begin
1259
  if (rst)
1260
    priority_lost <= 1'b0;
1261
  else if (go_rx_idle | error_frame | reset_mode)
1262
    priority_lost <=#Tp 1'b0;
1263
  else if (tx_state & sample_point & tx & (rx_id1 | rx_rtr1 | rx_id2 | rx_ide | rx_id2 | rx_rtr2))
1264
    priority_lost <=#Tp (~sampled_bit);
1265
end
1266
 
1267
 
1268
 
1269 2 mohor
endmodule

powered by: WebSVN 2.1.0

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