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

Subversion Repositories can

[/] [can/] [tags/] [rel_6/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 26

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

powered by: WebSVN 2.1.0

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