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

Subversion Repositories apbi2c

[/] [apbi2c/] [trunk/] [rtl/] [module_i2c.v] - Blame information for rev 21

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    TOP I2C BLOCK to I2C Core
5
////
6
////
7
////
8
//// This file is part of the APB to I2C project
9
////
10
//// http://www.opencores.org/cores/apbi2c/
11
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// apbi2c_spec IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): - Felipe Fernandes Da Costa, fefe2560@gmail.com
29 4 redbear
////              Ronal Dario Celaya ,rcelaya.dario@gmail.com
30 2 redbear
////
31
///////////////////////////////////////////////////////////////// 
32
////
33
////
34
//// Copyright (C) 2009 Authors and OPENCORES.ORG
35
////
36
////
37
////
38
//// This source file may be used and distributed without
39
////
40
//// restriction provided that this copyright statement is not
41
////
42
//// removed from the file and that any derivative work contains
43
//// the original copyright notice and the associated disclaimer.
44
////
45
////
46
//// This source file is free software; you can redistribute it
47
////
48
//// and/or modify it under the terms of the GNU Lesser General
49
////
50
//// Public License as published by the Free Software Foundation;
51
//// either version 2.1 of the License, or (at your option) any
52
////
53
//// later version.
54
////
55
////
56
////
57
//// This source is distributed in the hope that it will be
58
////
59
//// useful, but WITHOUT ANY WARRANTY; without even the implied
60
////
61
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
62
////
63
//// PURPOSE. See the GNU Lesser General Public License for more
64
//// details.
65
////
66
////
67
////
68
//// You should have received a copy of the GNU Lesser General
69
////
70
//// Public License along with this source; if not, download it
71
////
72
//// from http://www.opencores.org/lgpl.shtml
73
////
74
////
75
///////////////////////////////////////////////////////////////////
76
 
77
 
78
`timescale 1ns/1ps //timescale 
79
 
80
module module_i2c#(
81
                        //THIS IS USED ONLY LIKE PARAMETER TO BEM CONFIGURABLE
82
                        parameter integer DWIDTH = 32,
83
                        parameter integer AWIDTH = 14
84
                )
85
                (
86
                //I2C INTERFACE WITH ANOTHER BLOCKS
87
                 input PCLK,
88
                 input PRESETn,
89
 
90
                //INTERFACE WITH FIFO TRANSMISSION
91
                 input fifo_tx_f_full,
92
                 input fifo_tx_f_empty,
93
                 input [DWIDTH-1:0] fifo_tx_data_out,
94
 
95
                //INTERFACE WITH FIFO RECEIVER
96
                 input fifo_rx_f_full,
97
                 input fifo_rx_f_empty,
98 6 redbear
                 output reg fifo_rx_wr_en,
99
                 output reg [DWIDTH-1:0] fifo_rx_data_in,
100 2 redbear
 
101
                //INTERFACE WITH REGISTER CONFIGURATION
102
                 input [AWIDTH-1:0] DATA_CONFIG_REG,
103 20 redbear
                 input [AWIDTH-1:0] TIMEOUT_TX,
104 2 redbear
 
105 20 redbear
                //INTERFACE TO APB AND READ FOR FIFO   
106 2 redbear
                 output reg fifo_tx_rd_en,
107 20 redbear
                 output   TX_EMPTY,
108
                 output   RX_EMPTY,
109 2 redbear
                 output ERROR,
110 20 redbear
                 output ENABLE_SDA,
111
                 output ENABLE_SCL,
112 2 redbear
 
113
                //I2C BI DIRETIONAL PORTS
114
                inout SDA,
115
                inout SCL
116
 
117
 
118
                 );
119
 
120
//THIS IS USED TO GENERATE INTERRUPTIONS
121
assign TX_EMPTY = (fifo_tx_f_empty == 1'b1)? 1'b1:1'b0;
122
assign RX_EMPTY = (fifo_rx_f_empty == 1'b1)? 1'b1:1'b0;
123
 
124
        //THIS COUNT IS USED TO CONTROL DATA ACCROSS FSM        
125 6 redbear
        reg [1:0] count_tx;
126 20 redbear
        reg [1:0] count_rx;
127 2 redbear
        //CONTROL CLOCK AND COUNTER
128
        reg [11:0] count_send_data;
129 20 redbear
        reg [11:0] count_receive_data;
130
        reg [11:0] count_timeout;
131 2 redbear
        reg BR_CLK_O;
132
        reg SDA_OUT;
133
 
134 20 redbear
        reg BR_CLK_O_RX;
135
        reg SDA_OUT_RX;
136
 
137 2 redbear
        //RESPONSE USED TO HOLD SIGNAL TO ACK OR NACK
138
        reg RESPONSE;
139
 
140 20 redbear
//    PARAMETERS USED TO STATE MACHINE
141 2 redbear
 
142 20 redbear
localparam [5:0] IDLE = 6'd0, //IDLE
143 2 redbear
 
144 20 redbear
           START = 6'd1,//START BIT
145 2 redbear
 
146 20 redbear
             CONTROLIN_1 = 6'd2, //START BYTE
147
             CONTROLIN_2 = 6'd3,
148
             CONTROLIN_3 = 6'd4,
149
             CONTROLIN_4 = 6'd5,
150
             CONTROLIN_5 = 6'd6,
151
             CONTROLIN_6 = 6'd7,
152
             CONTROLIN_7 = 6'd8,
153
             CONTROLIN_8 = 6'd9, //END FIRST BYTE
154 2 redbear
 
155 20 redbear
             RESPONSE_CIN =6'd10, //RESPONSE
156 2 redbear
 
157 20 redbear
             ADDRESS_1 = 6'd11,//START BYTE
158
             ADDRESS_2 = 6'd12,
159
             ADDRESS_3 = 6'd13,
160
             ADDRESS_4 = 6'd14,
161
             ADDRESS_5 = 6'd15,
162
             ADDRESS_6 = 6'd16,
163
             ADDRESS_7 = 6'd17,
164
             ADDRESS_8 = 6'd18,//END FIRST BYTE
165 2 redbear
 
166 20 redbear
             RESPONSE_ADDRESS =6'd19, //RESPONSE
167 2 redbear
 
168 20 redbear
             DATA0_1 = 6'd20,//START BYTE
169
             DATA0_2 = 6'd21,
170
             DATA0_3 = 6'd22,
171
             DATA0_4 = 6'd23,
172
             DATA0_5 = 6'd24,
173
             DATA0_6 = 6'd25,
174
             DATA0_7 = 6'd26,
175
             DATA0_8 = 6'd27,//END FIRST BYTE
176 2 redbear
 
177 20 redbear
             RESPONSE_DATA0_1 = 6'd28,  //RESPONSE
178 2 redbear
 
179 20 redbear
             DATA1_1 = 6'd29,//START BYTE
180
             DATA1_2 = 6'd30,
181
             DATA1_3 = 6'd31,
182
             DATA1_4 = 6'd32,
183
             DATA1_5 = 6'd33,
184
             DATA1_6 = 6'd34,
185
             DATA1_7 = 6'd35,
186
             DATA1_8 = 6'd36,//END FIRST BYTE
187 2 redbear
 
188 20 redbear
             RESPONSE_DATA1_1 = 6'd37,//RESPONSE
189 2 redbear
 
190 20 redbear
             DELAY_BYTES = 6'd38,//USED ONLY IN ACK TO DELAY BETWEEN
191
             NACK = 6'd39,//USED ONLY IN ACK TO DELAY BETWEEN BYTES
192
             STOP = 6'd40;//USED TO SEND STOP BIT
193 2 redbear
 
194
        //STATE CONTROL 
195 20 redbear
        reg [5:0] state_tx;
196
        reg [5:0] next_state_tx;
197 2 redbear
 
198
//ASSIGN REGISTERS TO BIDIRETIONAL PORTS
199 21 redbear
assign SDA = (DATA_CONFIG_REG[0] == 1'b1 & DATA_CONFIG_REG[1] == 1'b0)?SDA_OUT:1'bz;
200
assign SCL = (DATA_CONFIG_REG[0] == 1'b1 & DATA_CONFIG_REG[1] == 1'b0)?BR_CLK_O:1'bz;
201 2 redbear
 
202 4 redbear
//STANDARD ERROR
203
assign ERROR = (DATA_CONFIG_REG[0] == 1'b1 & DATA_CONFIG_REG[1] == 1'b1)?1'b1:1'b0;
204 2 redbear
 
205 20 redbear
 
206
//COMBINATIONAL BLOCK TO   
207 2 redbear
always@(*)
208
begin
209
 
210
        //THE FUN START HERE :-)
211
        //COMBINATIONAL UPDATE STATE BE CAREFUL WITH WHAT YOU MAKE HERE
212 20 redbear
        next_state_tx=state_tx;
213 2 redbear
 
214 20 redbear
        case(state_tx)//state_   IS MORE SECURE CHANGE ONLY IF YOU KNOW WHAT ARE YOU DOING 
215
        IDLE:
216 2 redbear
        begin
217
                //OBEYING SPEC
218 18 redbear
                if(DATA_CONFIG_REG[0] == 1'b0 && (fifo_tx_f_full == 1'b1 || fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b0)
219 2 redbear
                begin
220 20 redbear
                        next_state_tx   = IDLE;
221 2 redbear
                end
222 18 redbear
                else if(DATA_CONFIG_REG[0] == 1'b1 && (fifo_tx_f_full == 1'b1 || fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b1)
223 2 redbear
                begin
224 20 redbear
                        next_state_tx   = IDLE;
225 4 redbear
                end
226 20 redbear
                else if(DATA_CONFIG_REG[0] == 1'b1 && (fifo_tx_f_full == 1'b1 || fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b0 && count_timeout < TIMEOUT_TX)
227 4 redbear
                begin
228 20 redbear
                        next_state_tx   = START;
229 2 redbear
                end
230
 
231
 
232
        end
233 20 redbear
        START://THIS IS USED TOO ALL STATE MACHINES THE COUNTER_SEND_DATA
234 2 redbear
        begin
235
                if(count_send_data != DATA_CONFIG_REG[13:2])
236
                begin
237 20 redbear
                        next_state_tx   = START;
238 2 redbear
                end
239
                else
240
                begin
241 20 redbear
                        next_state_tx   = CONTROLIN_1;
242 2 redbear
                end
243
 
244
        end
245 20 redbear
        CONTROLIN_1:
246 2 redbear
        begin
247
                if(count_send_data != DATA_CONFIG_REG[13:2])
248
                begin
249 20 redbear
                        next_state_tx  = CONTROLIN_1;
250 2 redbear
                end
251
                else
252
                begin
253 20 redbear
                        next_state_tx  =  CONTROLIN_2;
254 2 redbear
                end
255
 
256
        end
257 20 redbear
        CONTROLIN_2:
258 2 redbear
        begin
259
 
260
                if(count_send_data != DATA_CONFIG_REG[13:2])
261
                begin
262 20 redbear
                        next_state_tx   = CONTROLIN_2;
263 2 redbear
                end
264
                else
265
                begin
266 20 redbear
                        next_state_tx   = CONTROLIN_3;
267 2 redbear
                end
268
 
269
        end
270 20 redbear
        CONTROLIN_3:
271 2 redbear
        begin
272
 
273
                if(count_send_data != DATA_CONFIG_REG[13:2])
274
                begin
275 20 redbear
                        next_state_tx  =  CONTROLIN_3;
276 2 redbear
                end
277
                else
278
                begin
279 20 redbear
                        next_state_tx   = CONTROLIN_4;
280 2 redbear
                end
281
        end
282 20 redbear
        CONTROLIN_4:
283 2 redbear
        begin
284
 
285
                if(count_send_data != DATA_CONFIG_REG[13:2])
286
                begin
287 20 redbear
                        next_state_tx   = CONTROLIN_4;
288 2 redbear
                end
289
                else
290
                begin
291 20 redbear
                        next_state_tx   = CONTROLIN_5;
292 2 redbear
                end
293
        end
294 20 redbear
        CONTROLIN_5:
295 2 redbear
        begin
296
 
297
                if(count_send_data != DATA_CONFIG_REG[13:2])
298
                begin
299 20 redbear
                        next_state_tx = CONTROLIN_5;
300 2 redbear
                end
301
                else
302
                begin
303 20 redbear
                        next_state_tx = CONTROLIN_6;
304 2 redbear
                end
305
        end
306 20 redbear
        CONTROLIN_6:
307 2 redbear
        begin
308
 
309
                if(count_send_data != DATA_CONFIG_REG[13:2])
310
                begin
311 20 redbear
                        next_state_tx = CONTROLIN_6;
312 2 redbear
                end
313
                else
314
                begin
315 20 redbear
                        next_state_tx = CONTROLIN_7;
316 2 redbear
                end
317
        end
318 20 redbear
        CONTROLIN_7:
319 2 redbear
        begin
320
 
321
                if(count_send_data != DATA_CONFIG_REG[13:2])
322
                begin
323 20 redbear
                        next_state_tx = CONTROLIN_7;
324 2 redbear
                end
325
                else
326
                begin
327 20 redbear
                        next_state_tx = CONTROLIN_8;
328 2 redbear
                end
329
        end
330 20 redbear
        CONTROLIN_8:
331 2 redbear
        begin
332
 
333
                if(count_send_data != DATA_CONFIG_REG[13:2])
334
                begin
335 20 redbear
                        next_state_tx  = CONTROLIN_8;
336 2 redbear
                end
337
                else
338
                begin
339 20 redbear
                        next_state_tx  =  RESPONSE_CIN;
340 2 redbear
                end
341
        end
342 20 redbear
        RESPONSE_CIN:
343 2 redbear
        begin
344
 
345
                if(count_send_data != DATA_CONFIG_REG[13:2])
346
                begin
347 20 redbear
                        next_state_tx = RESPONSE_CIN;
348 2 redbear
                end
349
                else if(RESPONSE == 1'b0)//ACK
350
                begin
351 20 redbear
                        next_state_tx = DELAY_BYTES;
352 2 redbear
                end
353
                else if(RESPONSE == 1'b1)//NACK
354
                begin
355 20 redbear
                        next_state_tx = NACK;
356 2 redbear
                end
357
 
358
        end
359
 
360
        //NOW SENDING ADDRESS
361 20 redbear
        ADDRESS_1:
362 2 redbear
        begin
363
                if(count_send_data != DATA_CONFIG_REG[13:2])
364
                begin
365 20 redbear
                        next_state_tx  = ADDRESS_1;
366 2 redbear
                end
367
                else
368
                begin
369 20 redbear
                        next_state_tx  =  ADDRESS_2;
370 2 redbear
                end
371
        end
372 20 redbear
        ADDRESS_2:
373 2 redbear
        begin
374
                if(count_send_data != DATA_CONFIG_REG[13:2])
375
                begin
376 20 redbear
                        next_state_tx = ADDRESS_2;
377 2 redbear
                end
378
                else
379
                begin
380 20 redbear
                        next_state_tx = ADDRESS_3;
381 2 redbear
                end
382
        end
383 20 redbear
        ADDRESS_3:
384 2 redbear
        begin
385
                if(count_send_data != DATA_CONFIG_REG[13:2])
386
                begin
387 20 redbear
                        next_state_tx = ADDRESS_3;
388 2 redbear
                end
389
                else
390
                begin
391 20 redbear
                        next_state_tx = ADDRESS_4;
392 2 redbear
                end
393
        end
394 20 redbear
        ADDRESS_4:
395 2 redbear
        begin
396
                if(count_send_data != DATA_CONFIG_REG[13:2])
397
                begin
398 20 redbear
                        next_state_tx = ADDRESS_4;
399 2 redbear
                end
400
                else
401
                begin
402 20 redbear
                        next_state_tx = ADDRESS_5;
403 2 redbear
                end
404
        end
405 20 redbear
        ADDRESS_5:
406 2 redbear
        begin
407
                if(count_send_data != DATA_CONFIG_REG[13:2])
408
                begin
409 20 redbear
                        next_state_tx = ADDRESS_5;
410 2 redbear
                end
411
                else
412
                begin
413 20 redbear
                        next_state_tx = ADDRESS_6;
414 2 redbear
                end
415
        end
416 20 redbear
        ADDRESS_6:
417 2 redbear
        begin
418
                if(count_send_data != DATA_CONFIG_REG[13:2])
419
                begin
420 20 redbear
                        next_state_tx = ADDRESS_6;
421 2 redbear
                end
422
                else
423
                begin
424 20 redbear
                        next_state_tx = ADDRESS_7;
425 2 redbear
                end
426
        end
427 20 redbear
        ADDRESS_7:
428 2 redbear
        begin
429
                if(count_send_data != DATA_CONFIG_REG[13:2])
430
                begin
431 20 redbear
                        next_state_tx = ADDRESS_7;
432 2 redbear
                end
433
                else
434
                begin
435 20 redbear
                        next_state_tx = ADDRESS_8;
436 2 redbear
                end
437
        end
438 20 redbear
        ADDRESS_8:
439 2 redbear
        begin
440
                if(count_send_data != DATA_CONFIG_REG[13:2])
441
                begin
442 20 redbear
                        next_state_tx = ADDRESS_8;
443 2 redbear
                end
444
                else
445
                begin
446 20 redbear
                        next_state_tx = RESPONSE_ADDRESS;
447 2 redbear
                end
448
        end
449 20 redbear
        RESPONSE_ADDRESS:
450 2 redbear
        begin
451
                if(count_send_data != DATA_CONFIG_REG[13:2])
452
                begin
453 20 redbear
                        next_state_tx = RESPONSE_ADDRESS;
454 2 redbear
                end
455
                else if(RESPONSE == 1'b0)//ACK
456
                begin
457 20 redbear
                        next_state_tx = DELAY_BYTES;
458 2 redbear
                end
459
                else if(RESPONSE == 1'b1)//NACK --> RESTART CONDITION AND BACK TO START BYTE AGAIN
460
                begin
461 20 redbear
                        next_state_tx = NACK;
462 2 redbear
                end
463
        end
464
 
465
        //data in
466 20 redbear
        DATA0_1:
467 2 redbear
        begin
468
                if(count_send_data != DATA_CONFIG_REG[13:2])
469
                begin
470 20 redbear
                        next_state_tx = DATA0_1;
471 2 redbear
                end
472
                else
473
                begin
474 20 redbear
                        next_state_tx = DATA0_2;
475 2 redbear
                end
476
        end
477 20 redbear
        DATA0_2:
478 2 redbear
        begin
479
                if(count_send_data != DATA_CONFIG_REG[13:2])
480
                begin
481 20 redbear
                        next_state_tx = DATA0_2;
482 2 redbear
                end
483
                else
484
                begin
485 20 redbear
                        next_state_tx = DATA0_3;
486 2 redbear
                end
487
        end
488 20 redbear
        DATA0_3:
489 2 redbear
        begin
490
                if(count_send_data != DATA_CONFIG_REG[13:2])
491
                begin
492 20 redbear
                        next_state_tx = DATA0_3;
493 2 redbear
                end
494
                else
495
                begin
496 20 redbear
                        next_state_tx = DATA0_4;
497 2 redbear
                end
498
        end
499 20 redbear
        DATA0_4:
500 2 redbear
        begin
501
                if(count_send_data != DATA_CONFIG_REG[13:2])
502
                begin
503 20 redbear
                        next_state_tx = DATA0_4;
504 2 redbear
                end
505
                else
506
                begin
507 20 redbear
                        next_state_tx = DATA0_5;
508 2 redbear
                end
509
        end
510 20 redbear
        DATA0_5:
511 2 redbear
        begin
512
                if(count_send_data != DATA_CONFIG_REG[13:2])
513
                begin
514 20 redbear
                        next_state_tx = DATA0_5;
515 2 redbear
                end
516
                else
517
                begin
518 20 redbear
                        next_state_tx   = DATA0_6;
519 2 redbear
                end
520
        end
521 20 redbear
        DATA0_6:
522 2 redbear
        begin
523
                if(count_send_data != DATA_CONFIG_REG[13:2])
524
                begin
525 20 redbear
                        next_state_tx  = DATA0_6;
526 2 redbear
                end
527
                else
528
                begin
529 20 redbear
                        next_state_tx  = DATA0_7;
530 2 redbear
                end
531
        end
532 20 redbear
        DATA0_7:
533 2 redbear
        begin
534
                if(count_send_data != DATA_CONFIG_REG[13:2])
535
                begin
536 20 redbear
                        next_state_tx  = DATA0_7;
537 2 redbear
                end
538
                else
539
                begin
540 20 redbear
                        next_state_tx  = DATA0_8;
541 2 redbear
                end
542
        end
543 20 redbear
        DATA0_8:
544 2 redbear
        begin
545
                if(count_send_data != DATA_CONFIG_REG[13:2])
546
                begin
547 20 redbear
                        next_state_tx  = DATA0_8;
548 2 redbear
                end
549
                else
550
                begin
551 20 redbear
                        next_state_tx  =  RESPONSE_DATA0_1;
552 2 redbear
                end
553
        end
554 20 redbear
        RESPONSE_DATA0_1:
555 2 redbear
        begin
556
                if(count_send_data != DATA_CONFIG_REG[13:2])
557
                begin
558 20 redbear
                        next_state_tx  =  RESPONSE_DATA0_1;
559 2 redbear
                end
560
                else if(RESPONSE == 1'b0)//ACK
561
                begin
562 20 redbear
                        next_state_tx  =   DELAY_BYTES;
563 2 redbear
                end
564
                else if(RESPONSE == 1'b1)//NACK
565
                begin
566 20 redbear
                        next_state_tx  =   NACK;
567 2 redbear
                end
568
        end
569
 
570
        //second byte
571 20 redbear
        DATA1_1:
572 2 redbear
        begin
573
                if(count_send_data != DATA_CONFIG_REG[13:2])
574
                begin
575 20 redbear
                        next_state_tx  = DATA1_1;
576 2 redbear
                end
577
                else
578
                begin
579 20 redbear
                        next_state_tx  = DATA1_2;
580 2 redbear
                end
581
        end
582 20 redbear
        DATA1_2:
583 2 redbear
        begin
584
                if(count_send_data != DATA_CONFIG_REG[13:2])
585
                begin
586 20 redbear
                        next_state_tx = DATA1_2;
587 2 redbear
                end
588
                else
589
                begin
590 20 redbear
                        next_state_tx = DATA1_3;
591 2 redbear
                end
592
        end
593 20 redbear
        DATA1_3:
594 2 redbear
        begin
595
                if(count_send_data != DATA_CONFIG_REG[13:2])
596
                begin
597 20 redbear
                        next_state_tx  = DATA1_3;
598 2 redbear
                end
599
                else
600
                begin
601 20 redbear
                        next_state_tx  =  DATA1_4;
602 2 redbear
                end
603
        end
604 20 redbear
        DATA1_4:
605 2 redbear
        begin
606
                if(count_send_data != DATA_CONFIG_REG[13:2])
607
                begin
608 20 redbear
                        next_state_tx  = DATA1_4;
609 2 redbear
                end
610
                else
611
                begin
612 20 redbear
                        next_state_tx  = DATA1_5;
613 2 redbear
                end
614
        end
615 20 redbear
        DATA1_5:
616 2 redbear
        begin
617
                if(count_send_data != DATA_CONFIG_REG[13:2])
618
                begin
619 20 redbear
                        next_state_tx = DATA1_5;
620 2 redbear
                end
621
                else
622
                begin
623 20 redbear
                        next_state_tx = DATA1_6;
624 2 redbear
                end
625
        end
626 20 redbear
        DATA1_6:
627 2 redbear
        begin
628
                if(count_send_data != DATA_CONFIG_REG[13:2])
629
                begin
630 20 redbear
                        next_state_tx  =  DATA1_6;
631 2 redbear
                end
632
                else
633
                begin
634 20 redbear
                        next_state_tx  =  DATA1_7;
635 2 redbear
                end
636
        end
637 20 redbear
        DATA1_7:
638 2 redbear
        begin
639
                if(count_send_data != DATA_CONFIG_REG[13:2])
640
                begin
641 20 redbear
                        next_state_tx =  DATA1_7;
642 2 redbear
                end
643
                else
644
                begin
645 20 redbear
                        next_state_tx =  DATA1_8;
646 2 redbear
                end
647
        end
648 20 redbear
        DATA1_8:
649 2 redbear
        begin
650
                if(count_send_data != DATA_CONFIG_REG[13:2])
651
                begin
652 20 redbear
                        next_state_tx = DATA1_8;
653 2 redbear
                end
654
                else
655
                begin
656 20 redbear
                        next_state_tx = RESPONSE_DATA1_1;
657 2 redbear
                end
658
        end
659 20 redbear
        RESPONSE_DATA1_1:
660 2 redbear
        begin
661
                if(count_send_data != DATA_CONFIG_REG[13:2])
662
                begin
663 20 redbear
                        next_state_tx   =  RESPONSE_DATA1_1;
664 2 redbear
                end
665
                else if(RESPONSE == 1'b0)//ACK
666
                begin
667 20 redbear
                        next_state_tx   =  DELAY_BYTES;
668 2 redbear
                end
669
                else if(RESPONSE == 1'b1)//NACK
670
                begin
671 20 redbear
                        next_state_tx   =  NACK;
672 2 redbear
                end
673
        end
674 20 redbear
        DELAY_BYTES://THIS FORM WORKS 
675 2 redbear
        begin
676
 
677
 
678
                if(count_send_data != DATA_CONFIG_REG[13:2])
679
                begin
680 20 redbear
                        next_state_tx =  DELAY_BYTES;
681 2 redbear
                end
682
                else
683
                begin
684
 
685 6 redbear
                        if(count_tx == 2'd0)
686 2 redbear
                        begin
687 20 redbear
                                next_state_tx = ADDRESS_1;
688 2 redbear
                        end
689 20 redbear
                        else if(count_tx   == 2'd1)
690 2 redbear
                        begin
691 20 redbear
                                next_state_tx = DATA0_1;
692 2 redbear
                        end
693 20 redbear
                        else if(count_tx   == 2'd2)
694 2 redbear
                        begin
695 20 redbear
                                next_state_tx = DATA1_1;
696 2 redbear
                        end
697 20 redbear
                        else if(count_tx   == 2'd3)
698 2 redbear
                        begin
699 20 redbear
                                next_state_tx = STOP;
700 2 redbear
                        end
701
 
702
                end
703
 
704
        end
705 20 redbear
        NACK://NOT TESTED YET !!!!
706 2 redbear
        begin
707
                if(count_send_data != DATA_CONFIG_REG[13:2]*2'd2)
708
                begin
709 20 redbear
                        next_state_tx  = NACK;
710 2 redbear
                end
711
                else
712
                begin
713 6 redbear
                        if(count_tx == 2'd0)
714 2 redbear
                        begin
715 20 redbear
                                next_state_tx = CONTROLIN_1;
716 2 redbear
                        end
717 6 redbear
                        else if(count_tx == 2'd1)
718 2 redbear
                        begin
719 20 redbear
                                next_state_tx = ADDRESS_1;
720 2 redbear
                        end
721 20 redbear
                        else if(count_tx  == 2'd2)
722 2 redbear
                        begin
723 20 redbear
                                next_state_tx   = DATA0_1;
724 2 redbear
                        end
725 6 redbear
                        else if(count_tx == 2'd3)
726 2 redbear
                        begin
727 20 redbear
                                next_state_tx = DATA1_1;
728 2 redbear
                        end
729
                end
730
        end
731 20 redbear
        STOP://THIS WORK
732 2 redbear
        begin
733
                if(count_send_data != DATA_CONFIG_REG[13:2])
734
                begin
735 20 redbear
                        next_state_tx = STOP;
736 2 redbear
                end
737
                else
738
                begin
739 20 redbear
                        next_state_tx = IDLE;
740 2 redbear
                end
741
        end
742
        default:
743
        begin
744 20 redbear
                next_state_tx =  IDLE;
745 2 redbear
        end
746
        endcase
747
 
748
 
749
end
750 19 redbear
 
751
 
752
 
753 20 redbear
//SEQUENTIAL   
754 2 redbear
always@(posedge PCLK)
755
begin
756
 
757
        //RESET SYNC
758
        if(!PRESETn)
759
        begin
760
                //SIGNALS MUST BE RESETED
761
                count_send_data <= 12'd0;
762 20 redbear
                state_tx   <= IDLE;
763 2 redbear
                SDA_OUT<= 1'b1;
764
                fifo_tx_rd_en <= 1'b0;
765 20 redbear
                count_tx   <= 2'd0;
766 2 redbear
                BR_CLK_O <= 1'b1;
767
                RESPONSE<= 1'b0;
768
        end
769
        else
770
        begin
771
 
772
                // SEQUENTIAL FUN START
773 20 redbear
                state_tx  <= next_state_tx;
774 2 redbear
 
775 20 redbear
                case(state_tx)
776
                IDLE:
777 2 redbear
                begin
778
 
779
                        fifo_tx_rd_en <= 1'b0;
780
 
781
 
782 18 redbear
                        if(DATA_CONFIG_REG[0] == 1'b0 && (fifo_tx_f_full == 1'b1 ||fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b0)
783 2 redbear
                        begin
784
                                count_send_data <= 12'd0;
785
                                SDA_OUT<= 1'b1;
786
                                BR_CLK_O <= 1'b1;
787
                        end
788 18 redbear
                        else if(DATA_CONFIG_REG[0] == 1'b1 && (fifo_tx_f_full == 1'b1 ||fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b0)
789 2 redbear
                        begin
790
                                count_send_data <= count_send_data + 12'd1;
791
                                SDA_OUT<=1'b0;
792 4 redbear
                        end
793 18 redbear
                        else if(DATA_CONFIG_REG[0] == 1'b1 && (fifo_tx_f_full == 1'b1 ||fifo_tx_f_empty == 1'b0) && DATA_CONFIG_REG[1] == 1'b1)
794 4 redbear
                        begin
795
                                count_send_data <= 12'd0;
796
                                SDA_OUT<= 1'b1;
797
                                BR_CLK_O <= 1'b1;
798 2 redbear
                        end
799
 
800
                end
801 20 redbear
                START:
802 2 redbear
                begin
803
 
804
                        if(count_send_data < DATA_CONFIG_REG[13:2])
805
                        begin
806
                                count_send_data <= count_send_data + 12'd1;
807
                                BR_CLK_O <= 1'b0;
808
                        end
809
                        else
810
                        begin
811 7 redbear
                                count_send_data <= 12'd0;
812 2 redbear
                        end
813
 
814
                        if(count_send_data == DATA_CONFIG_REG[13:2]- 12'd1)
815
                        begin
816 6 redbear
                                SDA_OUT<=fifo_tx_data_out[0:0];
817 20 redbear
                                count_tx   <= 2'd0;
818 2 redbear
                        end
819
 
820
                end
821 20 redbear
                CONTROLIN_1:
822 2 redbear
                begin
823
 
824
 
825
 
826
                        if(count_send_data < DATA_CONFIG_REG[13:2])
827
                        begin
828
 
829
                                count_send_data <= count_send_data + 12'd1;
830
                                SDA_OUT<=fifo_tx_data_out[0:0];
831
 
832 7 redbear
 
833
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
834 2 redbear
                                begin
835 7 redbear
                                        BR_CLK_O <= 1'b0;
836
                                end
837
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
838
                                begin
839 2 redbear
                                        BR_CLK_O <= 1'b1;
840
                                end
841 7 redbear
                                else
842 2 redbear
                                begin
843
                                        BR_CLK_O <= 1'b0;
844
                                end
845
                        end
846
                        else
847
                        begin
848
                                count_send_data <= 12'd0;
849
                                SDA_OUT<=fifo_tx_data_out[1:1];
850
                        end
851
 
852
 
853
                end
854
 
855 20 redbear
                CONTROLIN_2:
856 2 redbear
                begin
857
 
858
 
859
 
860
                        if(count_send_data < DATA_CONFIG_REG[13:2])
861
                        begin
862
                                count_send_data <= count_send_data + 12'd1;
863
                                SDA_OUT<=fifo_tx_data_out[1:1];
864
 
865 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
866 2 redbear
                                begin
867 7 redbear
                                        BR_CLK_O <= 1'b0;
868
                                end
869
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
870
                                begin
871 2 redbear
                                        BR_CLK_O <= 1'b1;
872
                                end
873 7 redbear
                                else
874 2 redbear
                                begin
875
                                        BR_CLK_O <= 1'b0;
876 7 redbear
                                end
877 2 redbear
                        end
878
                        else
879
                        begin
880
                                count_send_data <= 12'd0;
881
                                SDA_OUT<=fifo_tx_data_out[2:2];
882
                        end
883
 
884
                end
885
 
886 20 redbear
                CONTROLIN_3:
887 2 redbear
                begin
888
 
889
 
890
 
891
                        if(count_send_data < DATA_CONFIG_REG[13:2])
892
                        begin
893
                                count_send_data <= count_send_data + 12'd1;
894
                                SDA_OUT<=fifo_tx_data_out[2:2];
895
 
896 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
897 2 redbear
                                begin
898 7 redbear
                                        BR_CLK_O <= 1'b0;
899
                                end
900
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
901
                                begin
902 2 redbear
                                        BR_CLK_O <= 1'b1;
903
                                end
904 7 redbear
                                else
905 2 redbear
                                begin
906
                                        BR_CLK_O <= 1'b0;
907 7 redbear
                                end
908 2 redbear
                        end
909
                        else
910
                        begin
911
                                count_send_data <= 12'd0;
912
                                SDA_OUT<=fifo_tx_data_out[3:3];
913
                        end
914
 
915
 
916
 
917
                end
918 20 redbear
                CONTROLIN_4:
919 2 redbear
                begin
920
 
921
 
922
 
923
                        if(count_send_data < DATA_CONFIG_REG[13:2])
924
                        begin
925
                                count_send_data <= count_send_data + 12'd1;
926
                                SDA_OUT<=fifo_tx_data_out[3:3];
927
 
928 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd4)
929 2 redbear
                                begin
930 7 redbear
                                        BR_CLK_O <= 1'b0;
931
                                end
932
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
933
                                begin
934 2 redbear
                                        BR_CLK_O <= 1'b1;
935
                                end
936 7 redbear
                                else
937 2 redbear
                                begin
938
                                        BR_CLK_O <= 1'b0;
939 7 redbear
                                end
940 2 redbear
                        end
941
                        else
942
                        begin
943
                                count_send_data <= 12'd0;
944
                                SDA_OUT<=fifo_tx_data_out[4:4];
945
                        end
946
 
947
                end
948
 
949 20 redbear
                CONTROLIN_5:
950 2 redbear
                begin
951
 
952
 
953
 
954
                        if(count_send_data < DATA_CONFIG_REG[13:2])
955
                        begin
956
                                count_send_data <= count_send_data + 12'd1;
957
                                SDA_OUT<=fifo_tx_data_out[4:4];
958
 
959 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd4)
960 2 redbear
                                begin
961 7 redbear
                                        BR_CLK_O <= 1'b0;
962
                                end
963
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
964
                                begin
965 2 redbear
                                        BR_CLK_O <= 1'b1;
966
                                end
967 7 redbear
                                else
968 2 redbear
                                begin
969
                                        BR_CLK_O <= 1'b0;
970 7 redbear
                                end
971 2 redbear
                        end
972
                        else
973
                        begin
974
                                count_send_data <= 12'd0;
975
                                SDA_OUT<=fifo_tx_data_out[5:5];
976
                        end
977
 
978
                end
979
 
980
 
981 20 redbear
                CONTROLIN_6:
982 2 redbear
                begin
983
 
984
                        if(count_send_data < DATA_CONFIG_REG[13:2])
985
                        begin
986
                                count_send_data <= count_send_data + 12'd1;
987
                                SDA_OUT<=fifo_tx_data_out[5:5];
988
 
989 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
990 2 redbear
                                begin
991 7 redbear
                                        BR_CLK_O <= 1'b0;
992
                                end
993
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
994
                                begin
995 2 redbear
                                        BR_CLK_O <= 1'b1;
996
                                end
997 7 redbear
                                else
998 2 redbear
                                begin
999
                                        BR_CLK_O <= 1'b0;
1000
                                end
1001
                        end
1002
                        else
1003
                        begin
1004
                                count_send_data <= 12'd0;
1005
                                SDA_OUT<=fifo_tx_data_out[6:6];
1006
                        end
1007
 
1008
 
1009
                end
1010
 
1011 20 redbear
                CONTROLIN_7:
1012 2 redbear
                begin
1013
 
1014
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1015
                        begin
1016
                                count_send_data <= count_send_data + 12'd1;
1017
                                SDA_OUT<=fifo_tx_data_out[6:6];
1018
 
1019 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1020 2 redbear
                                begin
1021 7 redbear
                                        BR_CLK_O <= 1'b0;
1022
                                end
1023
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1024
                                begin
1025 2 redbear
                                        BR_CLK_O <= 1'b1;
1026
                                end
1027 7 redbear
                                else
1028 2 redbear
                                begin
1029
                                        BR_CLK_O <= 1'b0;
1030
                                end
1031
                        end
1032
                        else
1033
                        begin
1034
                                count_send_data <= 12'd0;
1035
                                SDA_OUT<=fifo_tx_data_out[7:7];
1036
                        end
1037
 
1038
 
1039
                end
1040 20 redbear
                CONTROLIN_8:
1041 2 redbear
                begin
1042
 
1043
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1044
                        begin
1045
                                count_send_data <= count_send_data + 12'd1;
1046
                                SDA_OUT<=fifo_tx_data_out[7:7];
1047
 
1048 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1049 2 redbear
                                begin
1050 7 redbear
                                        BR_CLK_O <= 1'b0;
1051
                                end
1052
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1053
                                begin
1054 2 redbear
                                        BR_CLK_O <= 1'b1;
1055
                                end
1056 7 redbear
                                else
1057 2 redbear
                                begin
1058
                                        BR_CLK_O <= 1'b0;
1059 7 redbear
                                end
1060 2 redbear
                        end
1061
                        else
1062
                        begin
1063
                                count_send_data <= 12'd0;
1064
                                SDA_OUT<= 1'b0;
1065
                        end
1066
 
1067
 
1068
                end
1069 20 redbear
                RESPONSE_CIN:
1070 2 redbear
                begin
1071
 
1072
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1073
                        begin
1074
                                count_send_data <= count_send_data + 12'd1;
1075
 
1076
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1077
                                RESPONSE<= SDA;
1078
 
1079 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd4)
1080 2 redbear
                                begin
1081 7 redbear
                                        BR_CLK_O <= 1'b0;
1082
                                end
1083
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1084
                                begin
1085 2 redbear
                                        BR_CLK_O <= 1'b1;
1086
                                end
1087 7 redbear
                                else
1088 2 redbear
                                begin
1089
                                        BR_CLK_O <= 1'b0;
1090 7 redbear
                                end
1091 2 redbear
                        end
1092
                        else
1093
                        begin
1094
                                count_send_data <= 12'd0;
1095
                        end
1096
 
1097
 
1098
                end
1099 20 redbear
                ADDRESS_1:
1100 2 redbear
                begin
1101
 
1102
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1103
                        begin
1104
                                count_send_data <= count_send_data + 12'd1;
1105
                                SDA_OUT<=fifo_tx_data_out[8:8];
1106
 
1107 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1108 2 redbear
                                begin
1109 7 redbear
                                        BR_CLK_O <= 1'b0;
1110
                                end
1111
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1112
                                begin
1113 2 redbear
                                        BR_CLK_O <= 1'b1;
1114
                                end
1115 7 redbear
                                else
1116 2 redbear
                                begin
1117
                                        BR_CLK_O <= 1'b0;
1118 7 redbear
                                end
1119 2 redbear
                        end
1120
                        else
1121
                        begin
1122
                                count_send_data <= 12'd0;
1123
                                SDA_OUT<=fifo_tx_data_out[9:9];
1124
                        end
1125
 
1126
                end
1127 20 redbear
                ADDRESS_2:
1128 2 redbear
                begin
1129
 
1130
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1131
                        begin
1132
                                count_send_data <= count_send_data + 12'd1;
1133
                                SDA_OUT<=fifo_tx_data_out[9:9];
1134
 
1135 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd4)
1136 2 redbear
                                begin
1137 7 redbear
                                        BR_CLK_O <= 1'b0;
1138
                                end
1139
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1140
                                begin
1141 2 redbear
                                        BR_CLK_O <= 1'b1;
1142
                                end
1143 7 redbear
                                else
1144 2 redbear
                                begin
1145
                                        BR_CLK_O <= 1'b0;
1146 7 redbear
                                end
1147 2 redbear
                        end
1148
                        else
1149
                        begin
1150
                                count_send_data <= 12'd0;
1151
                                SDA_OUT<=fifo_tx_data_out[10:10];
1152
                        end
1153
 
1154
                end
1155 20 redbear
                ADDRESS_3:
1156 2 redbear
                begin
1157
 
1158
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1159
                        begin
1160
                                count_send_data <= count_send_data + 12'd1;
1161
                                SDA_OUT<=fifo_tx_data_out[10:10];
1162
 
1163 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1164 2 redbear
                                begin
1165 7 redbear
                                        BR_CLK_O <= 1'b0;
1166
                                end
1167
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1168
                                begin
1169 2 redbear
                                        BR_CLK_O <= 1'b1;
1170
                                end
1171 7 redbear
                                else
1172 2 redbear
                                begin
1173
                                        BR_CLK_O <= 1'b0;
1174 7 redbear
                                end
1175 2 redbear
                        end
1176
                        else
1177
                        begin
1178
                                count_send_data <= 12'd0;
1179
                                SDA_OUT<=fifo_tx_data_out[11:11];
1180
                        end
1181
 
1182
                end
1183 20 redbear
                ADDRESS_4:
1184 2 redbear
                begin
1185
 
1186
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1187
                        begin
1188
                                count_send_data <= count_send_data + 12'd1;
1189
                                SDA_OUT<=fifo_tx_data_out[11:11];
1190
 
1191 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1192 2 redbear
                                begin
1193 7 redbear
                                        BR_CLK_O <= 1'b0;
1194
                                end
1195
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1196
                                begin
1197 2 redbear
                                        BR_CLK_O <= 1'b1;
1198
                                end
1199 7 redbear
                                else
1200 2 redbear
                                begin
1201
                                        BR_CLK_O <= 1'b0;
1202 7 redbear
                                end
1203 2 redbear
                        end
1204
                        else
1205
                        begin
1206
                                count_send_data <= 12'd0;
1207
                                SDA_OUT<=fifo_tx_data_out[12:12];
1208
                        end
1209
                end
1210 20 redbear
                ADDRESS_5:
1211 2 redbear
                begin
1212
 
1213
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1214
                        begin
1215 20 redbear
                                count_send_data <= count_receive_data + 12'd1;
1216 2 redbear
                                SDA_OUT<=fifo_tx_data_out[12:12];
1217
 
1218 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1219 2 redbear
                                begin
1220 7 redbear
                                        BR_CLK_O <= 1'b0;
1221
                                end
1222
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1223
                                begin
1224 2 redbear
                                        BR_CLK_O <= 1'b1;
1225
                                end
1226 7 redbear
                                else
1227 2 redbear
                                begin
1228
                                        BR_CLK_O <= 1'b0;
1229 7 redbear
                                end
1230 2 redbear
                        end
1231
                        else
1232
                        begin
1233
                                count_send_data <= 12'd0;
1234
                                SDA_OUT<=fifo_tx_data_out[13:13];
1235
                        end
1236
 
1237
 
1238
                end
1239 20 redbear
                ADDRESS_6:
1240 2 redbear
                begin
1241
 
1242
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1243
                        begin
1244
                                count_send_data <= count_send_data + 12'd1;
1245
                                SDA_OUT<=fifo_tx_data_out[13:13];
1246
 
1247 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1248 2 redbear
                                begin
1249 7 redbear
                                        BR_CLK_O <= 1'b0;
1250
                                end
1251
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1252
                                begin
1253 2 redbear
                                        BR_CLK_O <= 1'b1;
1254
                                end
1255 7 redbear
                                else
1256 2 redbear
                                begin
1257
                                        BR_CLK_O <= 1'b0;
1258
                                end
1259
                        end
1260
                        else
1261
                        begin
1262 7 redbear
                                count_send_data <= 12'd0;
1263 2 redbear
                                SDA_OUT<=fifo_tx_data_out[14:14];
1264
                        end
1265
 
1266
                end
1267 20 redbear
                ADDRESS_7:
1268 2 redbear
                begin
1269
 
1270
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1271
                        begin
1272
                                count_send_data <= count_send_data + 12'd1;
1273
                                SDA_OUT<=fifo_tx_data_out[14:14];
1274
 
1275 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1276 2 redbear
                                begin
1277 7 redbear
                                        BR_CLK_O <= 1'b0;
1278
                                end
1279
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1280
                                begin
1281 2 redbear
                                        BR_CLK_O <= 1'b1;
1282
                                end
1283 7 redbear
                                else
1284 2 redbear
                                begin
1285
                                        BR_CLK_O <= 1'b0;
1286 7 redbear
                                end
1287 2 redbear
                        end
1288
                        else
1289
                        begin
1290
                                count_send_data <= 12'd0;
1291
                                SDA_OUT<=fifo_tx_data_out[15:15];
1292
                        end
1293
 
1294
 
1295
                end
1296 20 redbear
                ADDRESS_8:
1297 2 redbear
                begin
1298
 
1299
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1300
                        begin
1301
                                count_send_data <= count_send_data + 12'd1;
1302
                                SDA_OUT<=fifo_tx_data_out[15:15];
1303
 
1304 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1305 2 redbear
                                begin
1306 7 redbear
                                        BR_CLK_O <= 1'b0;
1307
                                end
1308
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1309
                                begin
1310 2 redbear
                                        BR_CLK_O <= 1'b1;
1311
                                end
1312 7 redbear
                                else
1313 2 redbear
                                begin
1314
                                        BR_CLK_O <= 1'b0;
1315
                                end
1316
                        end
1317
                        else
1318
                        begin
1319
                                count_send_data <= 12'd0;
1320 18 redbear
                                SDA_OUT<=1'b0;
1321 2 redbear
                        end
1322
 
1323
                end
1324 20 redbear
                RESPONSE_ADDRESS:
1325 2 redbear
                begin
1326
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1327
                        begin
1328
                                count_send_data <= count_send_data + 12'd1;
1329
 
1330
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1331
                                RESPONSE<= SDA;
1332
 
1333 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1334 2 redbear
                                begin
1335 7 redbear
                                        BR_CLK_O <= 1'b0;
1336
                                end
1337
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1338
                                begin
1339 2 redbear
                                        BR_CLK_O <= 1'b1;
1340
                                end
1341 7 redbear
                                else
1342 2 redbear
                                begin
1343
                                        BR_CLK_O <= 1'b0;
1344
                                end
1345
                        end
1346
                        else
1347
                        begin
1348
                                count_send_data <= 12'd0;
1349
                        end
1350
 
1351
                end
1352 20 redbear
                DATA0_1:
1353 2 redbear
                begin
1354
 
1355
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1356
                        begin
1357
                                count_send_data <= count_send_data + 12'd1;
1358
                                SDA_OUT<=fifo_tx_data_out[16:16];
1359
 
1360 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1361 2 redbear
                                begin
1362 7 redbear
                                        BR_CLK_O <= 1'b0;
1363
                                end
1364
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1365
                                begin
1366 2 redbear
                                        BR_CLK_O <= 1'b1;
1367
                                end
1368 7 redbear
                                else
1369 2 redbear
                                begin
1370
                                        BR_CLK_O <= 1'b0;
1371
                                end
1372
                        end
1373
                        else
1374
                        begin
1375
                                count_send_data <= 12'd0;
1376
                                SDA_OUT<=fifo_tx_data_out[17:17];
1377
                        end
1378
 
1379
 
1380
                end
1381 20 redbear
                DATA0_2:
1382 2 redbear
                begin
1383
 
1384
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1385
                        begin
1386 20 redbear
                                count_send_data <= count_receive_data + 12'd1;
1387 2 redbear
                                SDA_OUT<=fifo_tx_data_out[17:17];
1388
 
1389 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1390 2 redbear
                                begin
1391 7 redbear
                                        BR_CLK_O <= 1'b0;
1392
                                end
1393
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1394
                                begin
1395 2 redbear
                                        BR_CLK_O <= 1'b1;
1396
                                end
1397 7 redbear
                                else
1398 2 redbear
                                begin
1399
                                        BR_CLK_O <= 1'b0;
1400 7 redbear
                                end
1401 2 redbear
                        end
1402
                        else
1403
                        begin
1404
                                count_send_data <= 12'd0;
1405
                                SDA_OUT<=fifo_tx_data_out[18:18];
1406
                        end
1407
 
1408
 
1409
                end
1410 20 redbear
                DATA0_3:
1411 2 redbear
                begin
1412
 
1413
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1414
                        begin
1415
                                count_send_data <= count_send_data + 12'd1;
1416
                                SDA_OUT<=fifo_tx_data_out[18:18];
1417
 
1418 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1419 2 redbear
                                begin
1420 7 redbear
                                        BR_CLK_O <= 1'b0;
1421
                                end
1422
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1423
                                begin
1424 2 redbear
                                        BR_CLK_O <= 1'b1;
1425
                                end
1426 7 redbear
                                else
1427 2 redbear
                                begin
1428
                                        BR_CLK_O <= 1'b0;
1429 7 redbear
                                end
1430 2 redbear
                        end
1431
                        else
1432
                        begin
1433
                                count_send_data <= 12'd0;
1434
                                SDA_OUT<=fifo_tx_data_out[19:19];
1435
                        end
1436
 
1437
                end
1438 20 redbear
                DATA0_4:
1439 2 redbear
                begin
1440
 
1441
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1442
                        begin
1443
                                count_send_data <= count_send_data + 12'd1;
1444
                                SDA_OUT<=fifo_tx_data_out[19:19];
1445
 
1446 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1447 2 redbear
                                begin
1448 7 redbear
                                        BR_CLK_O <= 1'b0;
1449
                                end
1450
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1451
                                begin
1452 2 redbear
                                        BR_CLK_O <= 1'b1;
1453
                                end
1454 7 redbear
                                else
1455 2 redbear
                                begin
1456
                                        BR_CLK_O <= 1'b0;
1457 7 redbear
                                end
1458 2 redbear
                        end
1459
                        else
1460
                        begin
1461
                                count_send_data <= 12'd0;
1462
                                SDA_OUT<=fifo_tx_data_out[20:20];
1463
                        end
1464
 
1465
                end
1466 20 redbear
                DATA0_5:
1467 2 redbear
                begin
1468
 
1469
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1470
                        begin
1471
                                count_send_data <= count_send_data + 12'd1;
1472
                                SDA_OUT<=fifo_tx_data_out[20:20];
1473
 
1474 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1475 2 redbear
                                begin
1476 7 redbear
                                        BR_CLK_O <= 1'b0;
1477
                                end
1478
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1479
                                begin
1480 2 redbear
                                        BR_CLK_O <= 1'b1;
1481
                                end
1482 7 redbear
                                else
1483 2 redbear
                                begin
1484
                                        BR_CLK_O <= 1'b0;
1485
                                end
1486
                        end
1487
                        else
1488
                        begin
1489
                                count_send_data <= 12'd0;
1490
                                SDA_OUT<=fifo_tx_data_out[21:21];
1491
                        end
1492
 
1493
                end
1494 20 redbear
                DATA0_6:
1495 2 redbear
                begin
1496
 
1497
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1498
                        begin
1499
                                count_send_data <= count_send_data + 12'd1;
1500
                                SDA_OUT<=fifo_tx_data_out[21:21];
1501
 
1502 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1503 2 redbear
                                begin
1504 7 redbear
                                        BR_CLK_O <= 1'b0;
1505
                                end
1506
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1507
                                begin
1508 2 redbear
                                        BR_CLK_O <= 1'b1;
1509
                                end
1510 7 redbear
                                else
1511 2 redbear
                                begin
1512
                                        BR_CLK_O <= 1'b0;
1513
                                end
1514
                        end
1515
                        else
1516
                        begin
1517
                                count_send_data <= 12'd0;
1518
                                SDA_OUT<=fifo_tx_data_out[22:22];
1519
                        end
1520
 
1521
                end
1522 20 redbear
                DATA0_7:
1523 2 redbear
                begin
1524
 
1525
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1526
                        begin
1527
                                count_send_data <= count_send_data + 12'd1;
1528
                                SDA_OUT<=fifo_tx_data_out[22:22];
1529
 
1530 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1531 2 redbear
                                begin
1532 7 redbear
                                        BR_CLK_O <= 1'b0;
1533
                                end
1534
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1535
                                begin
1536 2 redbear
                                        BR_CLK_O <= 1'b1;
1537
                                end
1538 7 redbear
                                else
1539 2 redbear
                                begin
1540
                                        BR_CLK_O <= 1'b0;
1541 7 redbear
                                end
1542 2 redbear
                        end
1543
                        else
1544
                        begin
1545
                                count_send_data <= 12'd0;
1546
                                SDA_OUT<=fifo_tx_data_out[23:23];
1547
                        end
1548
 
1549
                end
1550 20 redbear
                DATA0_8:
1551 2 redbear
                begin
1552
 
1553
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1554
                        begin
1555
                                count_send_data <= count_send_data + 12'd1;
1556
                                SDA_OUT<=fifo_tx_data_out[23:23];
1557
 
1558 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1559 2 redbear
                                begin
1560 7 redbear
                                        BR_CLK_O <= 1'b0;
1561
                                end
1562
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1563
                                begin
1564 2 redbear
                                        BR_CLK_O <= 1'b1;
1565
                                end
1566 7 redbear
                                else
1567 2 redbear
                                begin
1568
                                        BR_CLK_O <= 1'b0;
1569
                                end
1570
 
1571
                        end
1572
                        else
1573
                        begin
1574
                                count_send_data <= 12'd0;
1575 18 redbear
                                SDA_OUT<=1'b0;
1576 2 redbear
                        end
1577
 
1578
                end
1579 20 redbear
                RESPONSE_DATA0_1:
1580 2 redbear
                begin
1581
 
1582
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1583
                        begin
1584
                                count_send_data <= count_send_data + 12'd1;
1585
 
1586
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1587
                                RESPONSE<= SDA;
1588
 
1589 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1590 2 redbear
                                begin
1591 7 redbear
                                        BR_CLK_O <= 1'b0;
1592
                                end
1593
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1594
                                begin
1595 2 redbear
                                        BR_CLK_O <= 1'b1;
1596
                                end
1597 7 redbear
                                else
1598 2 redbear
                                begin
1599
                                        BR_CLK_O <= 1'b0;
1600 7 redbear
                                end
1601 2 redbear
                        end
1602
                        else
1603
                        begin
1604
                                count_send_data <= 12'd0;
1605
                        end
1606
 
1607
                end
1608 20 redbear
                DATA1_1:
1609 2 redbear
                begin
1610
 
1611
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1612
                        begin
1613
                                count_send_data <= count_send_data + 12'd1;
1614
                                SDA_OUT<=fifo_tx_data_out[24:24];
1615
 
1616 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1617 2 redbear
                                begin
1618 7 redbear
                                        BR_CLK_O <= 1'b0;
1619
                                end
1620
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1621
                                begin
1622 2 redbear
                                        BR_CLK_O <= 1'b1;
1623
                                end
1624 7 redbear
                                else
1625 2 redbear
                                begin
1626
                                        BR_CLK_O <= 1'b0;
1627 7 redbear
                                end
1628 2 redbear
                        end
1629
                        else
1630
                        begin
1631
                                count_send_data <= 12'd0;
1632
                                SDA_OUT<=fifo_tx_data_out[25:25];
1633
 
1634
                        end
1635
 
1636
 
1637
                end
1638 20 redbear
                DATA1_2:
1639 2 redbear
                begin
1640
 
1641
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1642
                        begin
1643
                                count_send_data <= count_send_data + 12'd1;
1644
                                SDA_OUT<=fifo_tx_data_out[25:25];
1645
 
1646 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1647 2 redbear
                                begin
1648 7 redbear
                                        BR_CLK_O <= 1'b0;
1649
                                end
1650
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1651
                                begin
1652 2 redbear
                                        BR_CLK_O <= 1'b1;
1653
                                end
1654 7 redbear
                                else
1655 2 redbear
                                begin
1656
                                        BR_CLK_O <= 1'b0;
1657 7 redbear
                                end
1658 2 redbear
                        end
1659
                        else
1660
                        begin
1661
                                count_send_data <= 12'd0;
1662
                                SDA_OUT<=fifo_tx_data_out[26:26];
1663
                        end
1664
 
1665
                end
1666 20 redbear
                DATA1_3:
1667 2 redbear
                begin
1668
 
1669
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1670
                        begin
1671
                                count_send_data <= count_send_data + 12'd1;
1672
                                SDA_OUT<=fifo_tx_data_out[26:26];
1673
 
1674 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd4)
1675 2 redbear
                                begin
1676 7 redbear
                                        BR_CLK_O <= 1'b0;
1677
                                end
1678
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1679
                                begin
1680 2 redbear
                                        BR_CLK_O <= 1'b1;
1681
                                end
1682 7 redbear
                                else
1683 2 redbear
                                begin
1684
                                        BR_CLK_O <= 1'b0;
1685
                                end
1686
 
1687
                        end
1688
                        else
1689
                        begin
1690
                                count_send_data <= 12'd0;
1691
                                SDA_OUT<=fifo_tx_data_out[27:27];
1692
                        end
1693
 
1694
                end
1695 20 redbear
                DATA1_4:
1696 2 redbear
                begin
1697
 
1698
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1699
                        begin
1700
                                count_send_data <= count_send_data + 12'd1;
1701
                                SDA_OUT<=fifo_tx_data_out[27:27];
1702
 
1703 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1704 2 redbear
                                begin
1705 7 redbear
                                        BR_CLK_O <= 1'b0;
1706
                                end
1707
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1708
                                begin
1709 2 redbear
                                        BR_CLK_O <= 1'b1;
1710
                                end
1711 7 redbear
                                else
1712 2 redbear
                                begin
1713
                                        BR_CLK_O <= 1'b0;
1714 7 redbear
                                end
1715 2 redbear
 
1716
                        end
1717
                        else
1718
                        begin
1719
                                count_send_data <= 12'd0;
1720
                                SDA_OUT<=fifo_tx_data_out[28:28];
1721
                        end
1722
 
1723
                end
1724 20 redbear
                DATA1_5:
1725 2 redbear
                begin
1726
 
1727
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1728
                        begin
1729
                                count_send_data <= count_send_data + 12'd1;
1730
                                SDA_OUT<=fifo_tx_data_out[28:28];
1731
 
1732 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1733 2 redbear
                                begin
1734 7 redbear
                                        BR_CLK_O <= 1'b0;
1735
                                end
1736
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1737
                                begin
1738 2 redbear
                                        BR_CLK_O <= 1'b1;
1739
                                end
1740 7 redbear
                                else
1741 2 redbear
                                begin
1742
                                        BR_CLK_O <= 1'b0;
1743
                                end
1744
 
1745
                        end
1746
                        else
1747
                        begin
1748
                                count_send_data <= 12'd0;
1749
                                SDA_OUT<=fifo_tx_data_out[29:29];
1750
                        end
1751
 
1752
                end
1753 20 redbear
                DATA1_6:
1754 2 redbear
                begin
1755
 
1756
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1757
                        begin
1758
                                count_send_data <= count_send_data + 12'd1;
1759
                                SDA_OUT<=fifo_tx_data_out[29:29];
1760
 
1761 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1762 2 redbear
                                begin
1763 7 redbear
                                        BR_CLK_O <= 1'b0;
1764
                                end
1765
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1766
                                begin
1767 2 redbear
                                        BR_CLK_O <= 1'b1;
1768
                                end
1769 7 redbear
                                else
1770 2 redbear
                                begin
1771
                                        BR_CLK_O <= 1'b0;
1772
                                end
1773
 
1774
                        end
1775
                        else
1776
                        begin
1777
                                count_send_data <= 12'd0;
1778
                                SDA_OUT<=fifo_tx_data_out[30:30];
1779
                        end
1780
 
1781
                end
1782 20 redbear
                DATA1_7:
1783 2 redbear
                begin
1784
 
1785
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1786
                        begin
1787
                                count_send_data <= count_send_data + 12'd1;
1788
                                SDA_OUT<=fifo_tx_data_out[30:30];
1789
 
1790 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1791 2 redbear
                                begin
1792 7 redbear
                                        BR_CLK_O <= 1'b0;
1793
                                end
1794
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1795
                                begin
1796 2 redbear
                                        BR_CLK_O <= 1'b1;
1797
                                end
1798 7 redbear
                                else
1799 2 redbear
                                begin
1800
                                        BR_CLK_O <= 1'b0;
1801
                                end
1802
 
1803
                        end
1804
                        else
1805
                        begin
1806
                                count_send_data <= 12'd0;
1807
                                SDA_OUT<=fifo_tx_data_out[31:31];
1808
                        end
1809
 
1810
 
1811
                end
1812 20 redbear
                DATA1_8:
1813 2 redbear
                begin
1814
 
1815
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1816
                        begin
1817
                                count_send_data <= count_send_data + 12'd1;
1818
                                SDA_OUT<=fifo_tx_data_out[31:31];
1819
 
1820 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1821 2 redbear
                                begin
1822 7 redbear
                                        BR_CLK_O <= 1'b0;
1823
                                end
1824
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1825
                                begin
1826 2 redbear
                                        BR_CLK_O <= 1'b1;
1827
                                end
1828 7 redbear
                                else
1829 2 redbear
                                begin
1830
                                        BR_CLK_O <= 1'b0;
1831
                                end
1832
 
1833
                        end
1834
                        else
1835
                        begin
1836
                                count_send_data <= 12'd0;
1837 18 redbear
                                SDA_OUT<=1'b0;
1838 2 redbear
                        end
1839
 
1840
                end
1841 20 redbear
                RESPONSE_DATA1_1:
1842 2 redbear
                begin
1843 20 redbear
                        //fifo_  _rd_en <= 1'b1;
1844 2 redbear
 
1845
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1846
                        begin
1847
                                count_send_data <= count_send_data + 12'd1;
1848
 
1849
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1850
                                RESPONSE<= SDA;
1851
 
1852 7 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd4)
1853 2 redbear
                                begin
1854 7 redbear
                                        BR_CLK_O <= 1'b0;
1855
                                end
1856
                                else if(count_send_data >= DATA_CONFIG_REG[13:2]/12'd4 && count_send_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
1857
                                begin
1858 2 redbear
                                        BR_CLK_O <= 1'b1;
1859
                                end
1860 7 redbear
                                else
1861 2 redbear
                                begin
1862
                                        BR_CLK_O <= 1'b0;
1863 7 redbear
                                end
1864 2 redbear
                        end
1865
                        else
1866
                        begin
1867
                                count_send_data <= 12'd0;
1868
                                fifo_tx_rd_en <= 1'b1;
1869
                        end
1870
 
1871
                end
1872 20 redbear
                DELAY_BYTES:
1873 2 redbear
                begin
1874
 
1875
                        fifo_tx_rd_en <= 1'b0;
1876
 
1877
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1878
                        begin
1879
 
1880
                                count_send_data <= count_send_data + 12'd1;
1881
                                BR_CLK_O <= 1'b0;
1882
                                SDA_OUT<=1'b0;
1883
                        end
1884
                        else
1885
                        begin
1886
 
1887
 
1888 6 redbear
                                if(count_tx == 2'd0)
1889 2 redbear
                                begin
1890 6 redbear
                                        count_tx <= count_tx + 2'd1;
1891 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[8:8];
1892
                                end
1893 20 redbear
                                else if(count_tx   == 2'd1)
1894 2 redbear
                                begin
1895 6 redbear
                                        count_tx <= count_tx + 2'd1;
1896 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[16:16];
1897
                                end
1898 6 redbear
                                else if(count_tx == 2'd2)
1899 2 redbear
                                begin
1900 6 redbear
                                        count_tx <= count_tx + 2'd1;
1901 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[24:24];
1902
                                end
1903 6 redbear
                                else if(count_tx == 2'd3)
1904 2 redbear
                                begin
1905 6 redbear
                                        count_tx <= 2'd0;
1906 2 redbear
                                end
1907
 
1908
                                count_send_data <= 12'd0;
1909
 
1910
                        end
1911
 
1912
                end
1913
                //THIS BLOCK MUST BE CHECKED WITH CARE
1914 20 redbear
                NACK:// MORE A RESTART 
1915 2 redbear
                begin
1916
                        fifo_tx_rd_en <= 1'b0;
1917
 
1918
                        if(count_send_data < DATA_CONFIG_REG[13:2]*2'd2)
1919
                        begin
1920
                                count_send_data <= count_send_data + 12'd1;
1921
 
1922 20 redbear
                                if(count_receive_data < DATA_CONFIG_REG[13:2]/12'd2)
1923 2 redbear
                                begin
1924
                                        SDA_OUT<=1'b0;
1925
                                end
1926
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1927
                                begin
1928
                                        SDA_OUT<=1'b1;
1929
                                end
1930
                                else if(count_send_data  == DATA_CONFIG_REG[13:2]*2'd2)
1931
                                begin
1932
                                        SDA_OUT<=1'b0;
1933
                                end
1934
 
1935 6 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1936 2 redbear
                                begin
1937
                                        BR_CLK_O <= 1'b1;
1938
                                end
1939
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1940
                                begin
1941
                                        BR_CLK_O <= 1'b0;
1942
                                end
1943
                                else if(count_send_data < DATA_CONFIG_REG[13:2]*2'd2)
1944
                                begin
1945
                                        BR_CLK_O <= 1'b1;
1946
                                end
1947
 
1948
                        end
1949
                        else
1950
                        begin
1951
                                count_send_data <= 12'd0;
1952
 
1953 6 redbear
                                if(count_tx == 2'd0)
1954 2 redbear
                                begin
1955 6 redbear
                                        count_tx <= 2'd0;
1956 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[0:0];
1957
                                end
1958 6 redbear
                                else if(count_tx == 2'd1)
1959 2 redbear
                                begin
1960 6 redbear
                                        count_tx <= 2'd1;
1961 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[8:8];
1962
                                end
1963 6 redbear
                                else if(count_tx == 2'd2)
1964 2 redbear
                                begin
1965 6 redbear
                                        count_tx <= 2'd2;
1966 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[16:16];
1967
                                end
1968 6 redbear
                                else if(count_tx == 2'd3)
1969 2 redbear
                                begin
1970 6 redbear
                                        count_tx <= 2'd3;
1971 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[24:24];
1972
                                end
1973
 
1974
 
1975
                        end
1976
                end
1977 20 redbear
                STOP:
1978 2 redbear
                begin
1979 7 redbear
 
1980
                        BR_CLK_O <= 1'b1;
1981
 
1982 2 redbear
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1983
                        begin
1984 20 redbear
                                count_send_data <= count_receive_data + 12'd1;
1985 2 redbear
 
1986
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2-12'd2)
1987
                                begin
1988
                                        SDA_OUT<=1'b0;
1989
                                end
1990
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1991
                                begin
1992
                                        SDA_OUT<=1'b1;
1993
                                end
1994
                        end
1995
                        else
1996
                        begin
1997
                                count_send_data <= 12'd0;
1998
                        end
1999
                end
2000
                default:
2001
                begin
2002
                        fifo_tx_rd_en <= 1'b0;
2003
                        count_send_data <= 12'd4095;
2004
                end
2005
                endcase
2006
 
2007
        end
2008
 
2009
 
2010
end
2011
 
2012 20 redbear
 
2013
        //STATE CONTROL 
2014
        reg [5:0] state_rx;
2015
        reg [5:0] next_state_rx;
2016
 
2017
assign ENABLE_SDA = (state_rx ==  RESPONSE_CIN||
2018
                     state_rx ==  RESPONSE_ADDRESS||
2019
                     state_rx == RESPONSE_DATA0_1||
2020
                     state_rx == RESPONSE_DATA1_1)?1'b1:
2021
                    (state_tx ==  RESPONSE_CIN||
2022
                     state_tx ==  RESPONSE_ADDRESS||
2023
                     state_tx == RESPONSE_DATA0_1||
2024
                     state_tx == RESPONSE_DATA1_1)?1'b0:1'b1;
2025
 
2026
 
2027
assign ENABLE_SCL = (state_rx ==  RESPONSE_CIN||
2028
                     state_rx ==  RESPONSE_ADDRESS||
2029
                     state_rx == RESPONSE_DATA0_1||
2030
                     state_rx == RESPONSE_DATA1_1)?1'b1:
2031
                    (state_tx ==  RESPONSE_CIN||
2032
                     state_tx ==  RESPONSE_ADDRESS||
2033
                     state_tx == RESPONSE_DATA0_1||
2034
                     state_tx == RESPONSE_DATA1_1)?1'b1:1'b0;
2035
 
2036
 
2037
//COMBINATIONAL BLOCK TO RX
2038
always@(*)
2039
begin
2040
 
2041
        //THE FUN START HERE :-)
2042
        //COMBINATIONAL UPDATE STATE BE CAREFUL WITH WHAT YOU MAKE HERE
2043
        next_state_rx = state_rx;
2044
 
2045
        case(state_rx)//state_rx IS MORE SECURE CHANGE ONLY IF YOU KNOW WHAT ARE YOU DOING 
2046
        IDLE:
2047
        begin
2048
                //OBEYING SPEC
2049
                if(DATA_CONFIG_REG[0] == 1'b0 && DATA_CONFIG_REG[1] == 1'b0)
2050
                begin
2051
                        next_state_rx =   IDLE;
2052
                end
2053
                else if(DATA_CONFIG_REG[0] == 1'b1 && DATA_CONFIG_REG[1] == 1'b1)
2054
                begin
2055
                        next_state_rx =   IDLE;
2056
                end
2057
                else if(DATA_CONFIG_REG[0] == 1'b0 && DATA_CONFIG_REG[1] == 1'b1 && SDA_OUT_RX == 1'b0 && BR_CLK_O_RX == 1'b0)
2058
                begin
2059
                        next_state_rx =   START;
2060
                end
2061
 
2062
 
2063
        end
2064
        START://THIS IS USED TOO ALL STATE MACHINES THE COUNTER_SEND_DATA
2065
        begin
2066
 
2067
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2068
                begin
2069
                        next_state_rx =   START;
2070
                end
2071
                else if(fifo_rx_data_in[0] == 1'b0 && fifo_rx_data_in[1] == 1'b0)
2072
                begin
2073
                        next_state_rx =   CONTROLIN_1;
2074
                end
2075
                else
2076
                begin
2077
                        next_state_rx =   IDLE;
2078
                end
2079
 
2080
        end
2081
          CONTROLIN_1:
2082
        begin
2083
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2084
                begin
2085
                        next_state_rx =   CONTROLIN_1;
2086
                end
2087
                else
2088
                begin
2089
                        next_state_rx =   CONTROLIN_2;
2090
                end
2091
 
2092
        end
2093
          CONTROLIN_2:
2094
        begin
2095
 
2096
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2097
                begin
2098
                        next_state_rx =   CONTROLIN_2;
2099
                end
2100
                else
2101
                begin
2102
                        next_state_rx =   CONTROLIN_3;
2103
                end
2104
 
2105
        end
2106
          CONTROLIN_3:
2107
        begin
2108
 
2109
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2110
                begin
2111
                        next_state_rx =   CONTROLIN_3;
2112
                end
2113
                else
2114
                begin
2115
                        next_state_rx =   CONTROLIN_4;
2116
                end
2117
        end
2118
          CONTROLIN_4:
2119
        begin
2120
 
2121
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2122
                begin
2123
                        next_state_rx =   CONTROLIN_4;
2124
                end
2125
                else
2126
                begin
2127
                        next_state_rx =   CONTROLIN_5;
2128
                end
2129
        end
2130
          CONTROLIN_5:
2131
        begin
2132
 
2133
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2134
                begin
2135
                        next_state_rx =   CONTROLIN_5;
2136
                end
2137
                else
2138
                begin
2139
                        next_state_rx =   CONTROLIN_6;
2140
                end
2141
        end
2142
          CONTROLIN_6:
2143
        begin
2144
 
2145
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2146
                begin
2147
                        next_state_rx =   CONTROLIN_6;
2148
                end
2149
                else
2150
                begin
2151
                        next_state_rx =   CONTROLIN_7;
2152
                end
2153
        end
2154
          CONTROLIN_7:
2155
        begin
2156
 
2157
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2158
                begin
2159
                        next_state_rx =   CONTROLIN_7;
2160
                end
2161
                else
2162
                begin
2163
                        next_state_rx =   CONTROLIN_8;
2164
                end
2165
        end
2166
          CONTROLIN_8:
2167
        begin
2168
 
2169
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2170
                begin
2171
                        next_state_rx =   CONTROLIN_8;
2172
                end
2173
                else
2174
                begin
2175
                        next_state_rx =   RESPONSE_CIN;
2176
                end
2177
        end
2178
          RESPONSE_CIN:
2179
        begin
2180
 
2181
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2182
                begin
2183
                        next_state_rx =   RESPONSE_CIN;
2184
                end
2185
                else
2186
                begin
2187
                        next_state_rx =   ADDRESS_1;
2188
                end
2189
 
2190
        end
2191
        //NOW SENDING ADDRESS
2192
          ADDRESS_1:
2193
        begin
2194
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2195
                begin
2196
                        next_state_rx =   ADDRESS_1;
2197
                end
2198
                else
2199
                begin
2200
                        next_state_rx =   ADDRESS_2;
2201
                end
2202
        end
2203
          ADDRESS_2:
2204
        begin
2205
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2206
                begin
2207
                        next_state_rx =   ADDRESS_2;
2208
                end
2209
                else
2210
                begin
2211
                        next_state_rx =   ADDRESS_3;
2212
                end
2213
        end
2214
          ADDRESS_3:
2215
        begin
2216
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2217
                begin
2218
                        next_state_rx =   ADDRESS_3;
2219
                end
2220
                else
2221
                begin
2222
                        next_state_rx =   ADDRESS_4;
2223
                end
2224
        end
2225
          ADDRESS_4:
2226
        begin
2227
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2228
                begin
2229
                        next_state_rx =   ADDRESS_4;
2230
                end
2231
                else
2232
                begin
2233
                        next_state_rx =   ADDRESS_5;
2234
                end
2235
        end
2236
          ADDRESS_5:
2237
        begin
2238
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2239
                begin
2240
                        next_state_rx =   ADDRESS_5;
2241
                end
2242
                else
2243
                begin
2244
                        next_state_rx =   ADDRESS_6;
2245
                end
2246
        end
2247
          ADDRESS_6:
2248
        begin
2249
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2250
                begin
2251
                        next_state_rx =   ADDRESS_6;
2252
                end
2253
                else
2254
                begin
2255
                        next_state_rx =   ADDRESS_7;
2256
                end
2257
        end
2258
          ADDRESS_7:
2259
        begin
2260
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2261
                begin
2262
                        next_state_rx =   ADDRESS_7;
2263
                end
2264
                else
2265
                begin
2266
                        next_state_rx =   ADDRESS_8;
2267
                end
2268
        end
2269
          ADDRESS_8:
2270
        begin
2271
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2272
                begin
2273
                        next_state_rx =   ADDRESS_8;
2274
                end
2275
                else
2276
                begin
2277
                        next_state_rx =   RESPONSE_ADDRESS;
2278
                end
2279
        end
2280
          RESPONSE_ADDRESS:
2281
        begin
2282
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2283
                begin
2284
                        next_state_rx =   RESPONSE_ADDRESS;
2285
                end
2286
                else
2287
                begin
2288
                        next_state_rx =   DATA0_1;
2289
                end
2290
        end
2291
        //data in
2292
          DATA0_1:
2293
        begin
2294
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2295
                begin
2296
                        next_state_rx =   DATA0_1;
2297
                end
2298
                else
2299
                begin
2300
                        next_state_rx =   DATA0_2;
2301
                end
2302
        end
2303
          DATA0_2:
2304
        begin
2305
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2306
                begin
2307
                        next_state_rx =   DATA0_2;
2308
                end
2309
                else
2310
                begin
2311
                        next_state_rx =   DATA0_3;
2312
                end
2313
        end
2314
          DATA0_3:
2315
        begin
2316
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2317
                begin
2318
                        next_state_rx =   DATA0_3;
2319
                end
2320
                else
2321
                begin
2322
                        next_state_rx =   DATA0_4;
2323
                end
2324
        end
2325
          DATA0_4:
2326
        begin
2327
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2328
                begin
2329
                        next_state_rx =   DATA0_4;
2330
                end
2331
                else
2332
                begin
2333
                        next_state_rx =   DATA0_5;
2334
                end
2335
        end
2336
          DATA0_5:
2337
        begin
2338
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2339
                begin
2340
                        next_state_rx =   DATA0_5;
2341
                end
2342
                else
2343
                begin
2344
                        next_state_rx =   DATA0_6;
2345
                end
2346
        end
2347
          DATA0_6:
2348
        begin
2349
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2350
                begin
2351
                        next_state_rx =   DATA0_6;
2352
                end
2353
                else
2354
                begin
2355
                        next_state_rx =   DATA0_7;
2356
                end
2357
        end
2358
          DATA0_7:
2359
        begin
2360
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2361
                begin
2362
                        next_state_rx =   DATA0_7;
2363
                end
2364
                else
2365
                begin
2366
                        next_state_rx =   DATA0_8;
2367
                end
2368
        end
2369
          DATA0_8:
2370
        begin
2371
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2372
                begin
2373
                        next_state_rx =   DATA0_8;
2374
                end
2375
                else
2376
                begin
2377
                        next_state_rx =   RESPONSE_DATA0_1;
2378
                end
2379
        end
2380
          RESPONSE_DATA0_1:
2381
        begin
2382
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2383
                begin
2384
                        next_state_rx =   RESPONSE_DATA0_1;
2385
                end
2386
                else
2387
                begin
2388
                        next_state_rx =   DATA1_1;
2389
                end
2390
        end
2391
 
2392
        //second byte
2393
          DATA1_1:
2394
        begin
2395
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2396
                begin
2397
                        next_state_rx =   DATA1_1;
2398
                end
2399
                else
2400
                begin
2401
                        next_state_rx =   DATA1_2;
2402
                end
2403
        end
2404
          DATA1_2:
2405
        begin
2406
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2407
                begin
2408
                        next_state_rx =   DATA1_2;
2409
                end
2410
                else
2411
                begin
2412
                        next_state_rx =   DATA1_3;
2413
                end
2414
        end
2415
          DATA1_3:
2416
        begin
2417
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2418
                begin
2419
                        next_state_rx =   DATA1_3;
2420
                end
2421
                else
2422
                begin
2423
                        next_state_rx =   DATA1_4;
2424
                end
2425
        end
2426
          DATA1_4:
2427
        begin
2428
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2429
                begin
2430
                        next_state_rx =   DATA1_4;
2431
                end
2432
                else
2433
                begin
2434
                        next_state_rx =   DATA1_5;
2435
                end
2436
        end
2437
          DATA1_5:
2438
        begin
2439
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2440
                begin
2441
                        next_state_rx =   DATA1_5;
2442
                end
2443
                else
2444
                begin
2445
                        next_state_rx =   DATA1_6;
2446
                end
2447
        end
2448
          DATA1_6:
2449
        begin
2450
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2451
                begin
2452
                        next_state_rx =   DATA1_6;
2453
                end
2454
                else
2455
                begin
2456
                        next_state_rx =   DATA1_7;
2457
                end
2458
        end
2459
          DATA1_7:
2460
        begin
2461
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2462
                begin
2463
                        next_state_rx =   DATA1_7;
2464
                end
2465
                else
2466
                begin
2467
                        next_state_rx =   DATA1_8;
2468
                end
2469
        end
2470
          DATA1_8:
2471
        begin
2472
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2473
                begin
2474
                        next_state_rx =   DATA1_8;
2475
                end
2476
                else
2477
                begin
2478
                        next_state_rx =   RESPONSE_DATA1_1;
2479
                end
2480
        end
2481
          RESPONSE_DATA1_1:
2482
        begin
2483
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2484
                begin
2485
                        next_state_rx =   RESPONSE_DATA1_1;
2486
                end
2487
                else
2488
                begin
2489
                        next_state_rx =   DELAY_BYTES;
2490
                end
2491
 
2492
        end
2493
          DELAY_BYTES://THIS FORM WORKS 
2494
        begin
2495
 
2496
 
2497
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2498
                begin
2499
                        next_state_rx =   DELAY_BYTES;
2500
                end
2501
                else
2502
                begin
2503
 
2504
                        if(count_rx == 2'd0)
2505
                        begin
2506
                                next_state_rx =   ADDRESS_1;
2507
                        end
2508
                        else if(count_rx == 2'd1)
2509
                        begin
2510
                                next_state_rx =   DATA0_1;
2511
                        end
2512
                        else if(count_rx == 2'd2)
2513
                        begin
2514
                                next_state_rx =   DATA1_1;
2515
                        end
2516
                        else if(count_rx == 2'd3)
2517
                        begin
2518
                                next_state_rx =   STOP;
2519
                        end
2520
 
2521
                end
2522
 
2523
        end
2524
          STOP://THIS WORK
2525
        begin
2526
                if(  count_receive_data != DATA_CONFIG_REG[13:2])
2527
                begin
2528
                        next_state_rx =   STOP;
2529
                end
2530
                else
2531
                begin
2532
                        next_state_rx =   IDLE;
2533
                end
2534
        end
2535
        default:
2536
        begin
2537
                        next_state_rx =   IDLE;
2538
        end
2539
        endcase
2540
 
2541
 
2542
end
2543
 
2544
 
2545
 
2546
//SEQUENTIAL   
2547
always@(posedge PCLK)
2548
begin
2549
 
2550
        //RESET SYNC
2551
        if(!PRESETn)
2552
        begin
2553
                //SIGNALS MUST BE RESETED
2554
                  count_receive_data <= 12'd0;
2555
                state_rx <=   IDLE;
2556
                SDA_OUT_RX<= 1'b0;
2557
                fifo_rx_wr_en <= 1'b0;
2558
                count_rx <= 2'd0;
2559
                BR_CLK_O_RX <= 1'b0;
2560
        end
2561
        else
2562
        begin
2563
 
2564
                // SEQUENTIAL FUN START
2565
                state_rx <= next_state_rx;
2566
 
2567
                case(state_rx)
2568
                  IDLE:
2569
                begin
2570
 
2571
                        SDA_OUT_RX<= SDA;
2572
                        BR_CLK_O_RX<=SCL;
2573
 
2574
                        if(((fifo_rx_f_full == 1'b0 && fifo_rx_f_empty == 1'b0) || (fifo_rx_f_full == 1'b0 && fifo_rx_f_empty == 1'b1)) && DATA_CONFIG_REG[1] == 1'b1)
2575
                        begin
2576
 
2577
                                  count_receive_data <=   count_receive_data + 12'd1;
2578
                        end
2579
                        else
2580
                        begin
2581
                                  count_receive_data <=   count_receive_data;
2582
                        end
2583
 
2584
                end
2585
                  START:
2586
                begin
2587
 
2588
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2589
                        begin
2590
                                  count_receive_data <=   count_receive_data + 12'd1;
2591
                        end
2592
                        else
2593
                        begin
2594
                                  count_receive_data <= 12'd0;
2595
                        end
2596
 
2597
                        if(  count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2598
                        begin
2599
                                fifo_rx_data_in[0]<= SDA;
2600
                                fifo_rx_data_in[1]<= SCL;
2601
                        end
2602
 
2603
                end
2604
                  CONTROLIN_1:
2605
                begin
2606
 
2607
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2608
                        begin
2609
                                  count_receive_data <=   count_receive_data + 12'd1;
2610
                        end
2611
                        else
2612
                        begin
2613
                                  count_receive_data <= 12'd0;
2614
                        end
2615
 
2616
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2617
                        begin
2618
                                fifo_rx_data_in[0]<= SDA;
2619
                        end
2620
 
2621
                end
2622
                  CONTROLIN_2:
2623
                begin
2624
 
2625
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2626
                        begin
2627
                                  count_receive_data <=   count_receive_data + 12'd1;
2628
                        end
2629
                        else
2630
                        begin
2631
                                  count_receive_data <= 12'd0;
2632
                        end
2633
 
2634
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2635
                        begin
2636
                                fifo_rx_data_in[1]<= SDA;
2637
                        end
2638
 
2639
                end
2640
                  CONTROLIN_3:
2641
                begin
2642
 
2643
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2644
                        begin
2645
                                  count_receive_data <=   count_receive_data + 12'd1;
2646
                        end
2647
                        else
2648
                        begin
2649
                                  count_receive_data <= 12'd0;
2650
                        end
2651
 
2652
 
2653
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2654
                        begin
2655
                                fifo_rx_data_in[2]<= SDA;
2656
                        end
2657
 
2658
 
2659
                end
2660
                  CONTROLIN_4:
2661
                begin
2662
 
2663
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2664
                        begin
2665
                                  count_receive_data <=   count_receive_data + 12'd1;
2666
                        end
2667
                        else
2668
                        begin
2669
                                  count_receive_data <= 12'd0;
2670
                        end
2671
 
2672
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2673
                        begin
2674
                                fifo_rx_data_in[3]<= SDA;
2675
                        end
2676
 
2677
                end
2678
                  CONTROLIN_5:
2679
                begin
2680
 
2681
 
2682
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2683
                        begin
2684
                                  count_receive_data <=   count_receive_data + 12'd1;
2685
                        end
2686
                        else
2687
                        begin
2688
                                  count_receive_data <= 12'd0;
2689
                        end
2690
 
2691
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2692
                        begin
2693
                                        fifo_rx_data_in[4]<= SDA;
2694
                        end
2695
 
2696
 
2697
                end
2698
                  CONTROLIN_6:
2699
                begin
2700
                                if(  count_receive_data < DATA_CONFIG_REG[13:2])
2701
                                begin
2702
                                          count_receive_data <=   count_receive_data + 12'd1;
2703
                                end
2704
                                else
2705
                                begin
2706
                                          count_receive_data <= 12'd0;
2707
                                end
2708
 
2709
                                if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2710
                                begin
2711
                                        fifo_rx_data_in[5]<= SDA;
2712
                                end
2713
                end
2714
 
2715
                  CONTROLIN_7:
2716
                begin
2717
 
2718
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2719
                        begin
2720
                                  count_receive_data <=   count_receive_data + 12'd1;
2721
                        end
2722
                        else
2723
                        begin
2724
                                  count_receive_data <= 12'd0;
2725
                        end
2726
 
2727
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2728
                        begin
2729
                                fifo_rx_data_in[6]<= SDA;
2730
                        end
2731
                end
2732
                  CONTROLIN_8:
2733
                begin
2734
 
2735
 
2736
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2737
                        begin
2738
                                  count_receive_data <=   count_receive_data + 12'd1;
2739
                        end
2740
                        else
2741
                        begin
2742
                                  count_receive_data <= 12'd0;
2743
                        end
2744
 
2745
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2746
                        begin
2747
                                fifo_rx_data_in[7]<= SDA;
2748
                        end
2749
 
2750
 
2751
 
2752
                end
2753
                  RESPONSE_CIN:
2754
                begin
2755
 
2756
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2757
                        begin
2758
                                  count_receive_data <=   count_receive_data + 12'd1;
2759
                        end
2760
                        else
2761
                        begin
2762
                                  count_receive_data <= 12'd0;
2763
                        end
2764
 
2765
                end
2766
                  ADDRESS_1:
2767
                begin
2768
 
2769
 
2770
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2771
                        begin
2772
                                  count_receive_data <=   count_receive_data + 12'd1;
2773
                        end
2774
                        else
2775
                        begin
2776
                                  count_receive_data <= 12'd0;
2777
                        end
2778
 
2779
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2780
                        begin
2781
                                fifo_rx_data_in[8]<= SDA;
2782
                        end
2783
 
2784
 
2785
                end
2786
                  ADDRESS_2:
2787
                begin
2788
 
2789
 
2790
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2791
                        begin
2792
                                  count_receive_data <=   count_receive_data + 12'd1;
2793
                        end
2794
                        else
2795
                        begin
2796
                                  count_receive_data <= 12'd0;
2797
                        end
2798
 
2799
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2800
                        begin
2801
                                fifo_rx_data_in[9]<= SDA;
2802
                        end
2803
 
2804
 
2805
                end
2806
                  ADDRESS_3:
2807
                begin
2808
 
2809
 
2810
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2811
                        begin
2812
                                  count_receive_data <=   count_receive_data + 12'd1;
2813
                        end
2814
                        else
2815
                        begin
2816
                                  count_receive_data <= 12'd0;
2817
                        end
2818
 
2819
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2820
                        begin
2821
                                fifo_rx_data_in[10]<= SDA;
2822
                        end
2823
 
2824
 
2825
 
2826
                end
2827
                  ADDRESS_4:
2828
                begin
2829
 
2830
 
2831
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2832
                        begin
2833
                                  count_receive_data <=   count_receive_data + 12'd1;
2834
                        end
2835
                        else
2836
                        begin
2837
                                  count_receive_data <= 12'd0;
2838
                        end
2839
 
2840
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2841
                        begin
2842
                                fifo_rx_data_in[11]<= SDA;
2843
                        end
2844
 
2845
                end
2846
                  ADDRESS_5:
2847
                begin
2848
 
2849
 
2850
 
2851
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2852
                        begin
2853
                                  count_receive_data <=   count_receive_data + 12'd1;
2854
                        end
2855
                        else
2856
                        begin
2857
                                  count_receive_data <= 12'd0;
2858
                        end
2859
 
2860
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2861
                        begin
2862
                                fifo_rx_data_in[12]<= SDA;
2863
                        end
2864
 
2865
 
2866
                end
2867
                  ADDRESS_6:
2868
                begin
2869
 
2870
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2871
                        begin
2872
                                  count_receive_data <=   count_receive_data + 12'd1;
2873
                        end
2874
                        else
2875
                        begin
2876
                                  count_receive_data <= 12'd0;
2877
                        end
2878
 
2879
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2880
                        begin
2881
                                fifo_rx_data_in[13]<= SDA;
2882
                        end
2883
 
2884
                end
2885
                  ADDRESS_7:
2886
                begin
2887
 
2888
 
2889
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2890
                        begin
2891
                                  count_receive_data <=   count_receive_data + 12'd1;
2892
                        end
2893
                        else
2894
                        begin
2895
                                  count_receive_data <= 12'd0;
2896
                        end
2897
 
2898
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2899
                        begin
2900
                                fifo_rx_data_in[14]<= SDA;
2901
                        end
2902
 
2903
                end
2904
                  ADDRESS_8:
2905
                begin
2906
 
2907
 
2908
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2909
                        begin
2910
                                  count_receive_data <=   count_receive_data + 12'd1;
2911
                        end
2912
                        else
2913
                        begin
2914
                                  count_receive_data <= 12'd0;
2915
                        end
2916
 
2917
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2918
                        begin
2919
                                fifo_rx_data_in[15]<= SDA;
2920
                        end
2921
 
2922
 
2923
                end
2924
                  RESPONSE_ADDRESS:
2925
                begin
2926
 
2927
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2928
                        begin
2929
                                  count_receive_data <=   count_receive_data + 12'd1;
2930
                        end
2931
                        else
2932
                        begin
2933
                                  count_receive_data <= 12'd0;
2934
                        end
2935
 
2936
 
2937
                end
2938
                  DATA0_1:
2939
                begin
2940
 
2941
 
2942
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2943
                        begin
2944
                                  count_receive_data <=   count_receive_data + 12'd1;
2945
                        end
2946
                        else
2947
                        begin
2948
                                  count_receive_data <= 12'd0;
2949
                        end
2950
 
2951
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2952
                        begin
2953
                                fifo_rx_data_in[16]<= SDA;
2954
                        end
2955
 
2956
 
2957
 
2958
                end
2959
                  DATA0_2:
2960
                begin
2961
 
2962
 
2963
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2964
                        begin
2965
                                  count_receive_data <=   count_receive_data + 12'd1;
2966
                        end
2967
                        else
2968
                        begin
2969
                                  count_receive_data <= 12'd0;
2970
                        end
2971
 
2972
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2973
                        begin
2974
                                fifo_rx_data_in[17]<= SDA;
2975
                        end
2976
 
2977
 
2978
                end
2979
                  DATA0_3:
2980
                begin
2981
 
2982
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
2983
                        begin
2984
                                  count_receive_data <=   count_receive_data + 12'd1;
2985
                        end
2986
                        else
2987
                        begin
2988
                                  count_receive_data <= 12'd0;
2989
                        end
2990
 
2991
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
2992
                        begin
2993
                                fifo_rx_data_in[18]<= SDA;
2994
                        end
2995
 
2996
                end
2997
                  DATA0_4:
2998
                begin
2999
 
3000
 
3001
 
3002
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3003
                        begin
3004
                                  count_receive_data <=   count_receive_data + 12'd1;
3005
                        end
3006
                        else
3007
                        begin
3008
                                  count_receive_data <= 12'd0;
3009
                        end
3010
 
3011
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3012
                        begin
3013
                                fifo_rx_data_in[19]<= SDA;
3014
                        end
3015
 
3016
                end
3017
                  DATA0_5:
3018
                begin
3019
 
3020
 
3021
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3022
                        begin
3023
                                  count_receive_data <=   count_receive_data + 12'd1;
3024
                        end
3025
                        else
3026
                        begin
3027
                                  count_receive_data <= 12'd0;
3028
                        end
3029
 
3030
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3031
                        begin
3032
                                fifo_rx_data_in[20]<= SDA;
3033
                        end
3034
 
3035
 
3036
                end
3037
                  DATA0_6:
3038
                begin
3039
 
3040
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3041
                        begin
3042
                                  count_receive_data <=   count_receive_data + 12'd1;
3043
                        end
3044
                        else
3045
                        begin
3046
                                  count_receive_data <= 12'd0;
3047
                        end
3048
 
3049
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3050
                        begin
3051
                                fifo_rx_data_in[21]<= SDA;
3052
                        end
3053
 
3054
                end
3055
                  DATA0_7:
3056
                begin
3057
 
3058
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3059
                        begin
3060
                                  count_receive_data <=   count_receive_data + 12'd1;
3061
                        end
3062
                        else
3063
                        begin
3064
                                  count_receive_data <= 12'd0;
3065
                        end
3066
 
3067
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3068
                        begin
3069
                                fifo_rx_data_in[22]<= SDA;
3070
                        end
3071
 
3072
                end
3073
                  DATA0_8:
3074
                begin
3075
 
3076
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3077
                        begin
3078
                                  count_receive_data <=   count_receive_data + 12'd1;
3079
                        end
3080
                        else
3081
                        begin
3082
                                  count_receive_data <= 12'd0;
3083
                        end
3084
 
3085
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3086
                        begin
3087
                                fifo_rx_data_in[23]<= SDA;
3088
                        end
3089
 
3090
                end
3091
                  RESPONSE_DATA0_1:
3092
                begin
3093
 
3094
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3095
                        begin
3096
                                  count_receive_data <=   count_receive_data + 12'd1;
3097
                        end
3098
                        else
3099
                        begin
3100
                                  count_receive_data <= 12'd0;
3101
                        end
3102
 
3103
                end
3104
                  DATA1_1:
3105
                begin
3106
 
3107
 
3108
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3109
                        begin
3110
                                  count_receive_data <=   count_receive_data + 12'd1;
3111
                        end
3112
                        else
3113
                        begin
3114
                                  count_receive_data <= 12'd0;
3115
                        end
3116
 
3117
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3118
                        begin
3119
                                fifo_rx_data_in[24]<= SDA;
3120
                        end
3121
 
3122
                end
3123
                  DATA1_2:
3124
                begin
3125
 
3126
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3127
                        begin
3128
                                  count_receive_data <=   count_receive_data + 12'd1;
3129
                        end
3130
                        else
3131
                        begin
3132
                                  count_receive_data <= 12'd0;
3133
                        end
3134
 
3135
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3136
                        begin
3137
                                fifo_rx_data_in[25]<= SDA;
3138
                        end
3139
 
3140
 
3141
                end
3142
                  DATA1_3:
3143
                begin
3144
 
3145
 
3146
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3147
                        begin
3148
                                  count_receive_data <=   count_receive_data + 12'd1;
3149
                        end
3150
                        else
3151
                        begin
3152
                                  count_receive_data <= 12'd0;
3153
                        end
3154
 
3155
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3156
                        begin
3157
                                fifo_rx_data_in[26]<= SDA;
3158
                        end
3159
 
3160
 
3161
                end
3162
                  DATA1_4:
3163
                begin
3164
 
3165
 
3166
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3167
                        begin
3168
                                  count_receive_data <=   count_receive_data + 12'd1;
3169
                        end
3170
                        else
3171
                        begin
3172
                                  count_receive_data <= 12'd0;
3173
                        end
3174
 
3175
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3176
                        begin
3177
                                fifo_rx_data_in[27]<= SDA;
3178
                        end
3179
 
3180
 
3181
                end
3182
                  DATA1_5:
3183
                begin
3184
 
3185
 
3186
 
3187
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3188
                        begin
3189
                                  count_receive_data <=   count_receive_data + 12'd1;
3190
                        end
3191
                        else
3192
                        begin
3193
                                  count_receive_data <= 12'd0;
3194
                        end
3195
 
3196
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3197
                        begin
3198
                                fifo_rx_data_in[28]<= SDA;
3199
                        end
3200
 
3201
 
3202
                end
3203
                  DATA1_6:
3204
                begin
3205
 
3206
 
3207
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3208
                        begin
3209
                                  count_receive_data <=   count_receive_data + 12'd1;
3210
                        end
3211
                        else
3212
                        begin
3213
                                  count_receive_data <= 12'd0;
3214
                        end
3215
 
3216
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3217
                        begin
3218
                                fifo_rx_data_in[29]<= SDA;
3219
                        end
3220
 
3221
 
3222
 
3223
                end
3224
                  DATA1_7:
3225
                begin
3226
 
3227
 
3228
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3229
                        begin
3230
                                  count_receive_data <=   count_receive_data + 12'd1;
3231
                        end
3232
                        else
3233
                        begin
3234
                                  count_receive_data <= 12'd0;
3235
                        end
3236
 
3237
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3238
                        begin
3239
                                fifo_rx_data_in[30]<= SDA;
3240
                        end
3241
 
3242
 
3243
 
3244
                end
3245
                  DATA1_8:
3246
                begin
3247
 
3248
 
3249
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3250
                        begin
3251
                                  count_receive_data <=   count_receive_data + 12'd1;
3252
                        end
3253
                        else
3254
                        begin
3255
                                  count_receive_data <= 12'd0;
3256
                        end
3257
 
3258
                        if(SCL == 1'b1 &&   count_receive_data >= DATA_CONFIG_REG[13:2]/12'd4 &&   count_receive_data < (DATA_CONFIG_REG[13:2]-(DATA_CONFIG_REG[13:2]/12'd4))-12'd1)
3259
                        begin
3260
                                fifo_rx_data_in[31]<= SDA;
3261
                        end
3262
 
3263
                end
3264
                  RESPONSE_DATA1_1:
3265
                begin
3266
 
3267
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3268
                        begin
3269
                                  count_receive_data <=   count_receive_data + 12'd1;
3270
                        end
3271
                        else
3272
                        begin
3273
                                  count_receive_data <= 12'd0;
3274
                        end
3275
                        //fifo_  _rd_en <= 1'b1;
3276
 
3277
                end
3278
                  DELAY_BYTES:
3279
                begin
3280
 
3281
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3282
                        begin
3283
                                  count_receive_data <=   count_receive_data + 12'd1;
3284
                        end
3285
                        else
3286
                        begin
3287
                                  count_receive_data <= 12'd0;
3288
                                fifo_rx_wr_en <= 1'b1;
3289
                        end
3290
 
3291
 
3292
                end
3293
                  STOP:
3294
                begin
3295
                        if(  count_receive_data < DATA_CONFIG_REG[13:2])
3296
                        begin
3297
                                  count_receive_data <=   count_receive_data + 12'd1;
3298
                        end
3299
                        else
3300
                        begin
3301
                                  count_receive_data <= 12'd0;
3302
                        end
3303
                        fifo_rx_wr_en <= 1'b0;
3304
                end
3305
                default:
3306
                begin
3307
                        fifo_rx_wr_en <= 1'b0;
3308
                          count_receive_data <= 12'd4095;
3309
                end
3310
                endcase
3311
 
3312
        end
3313
 
3314
 
3315
end
3316
 
3317
//USED ONLY TO COUNTER TIME
3318
always@(posedge PCLK)
3319
begin
3320
 
3321
        //RESET SYNC
3322
        if(!PRESETn)
3323
        begin
3324
                count_timeout <= 12'd0;
3325
        end
3326
        else
3327
        begin
3328
                if(count_timeout <= TIMEOUT_TX)
3329
                begin
3330
                        if(SDA == 1'b0 && SCL == 1'b0)
3331
                        count_timeout <= count_timeout + 12'd1;
3332
                end
3333
                else
3334
                begin
3335
                        count_timeout <= 12'd0;
3336
                end
3337
 
3338
        end
3339
 
3340
end
3341
 
3342
 
3343 19 redbear
endmodule
3344 2 redbear
 

powered by: WebSVN 2.1.0

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