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

Subversion Repositories pci

[/] [pci/] [tags/] [asyst_3/] [rtl/] [verilog/] [pci_wb_master.v] - Blame information for rev 154

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 77 mihad
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  File name: wb_master.v                                      ////
4
////                                                              ////
5
////  This file is part of the "PCI bridge" project               ////
6
////  http://www.opencores.org/cores/pci/                         ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - Tadej Markovic, tadej@opencores.org                   ////
10
////                                                              ////
11
////  All additional information is avaliable in the README.txt   ////
12
////  file.                                                       ////
13
////                                                              ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2000 Tadej Markovic, tadej@opencores.org       ////
18
////                                                              ////
19
//// This source file may be used and distributed without         ////
20
//// restriction provided that this copyright statement is not    ////
21
//// removed from the file and that any derivative work contains  ////
22
//// the original copyright notice and the associated disclaimer. ////
23
////                                                              ////
24
//// This source file is free software; you can redistribute it   ////
25
//// and/or modify it under the terms of the GNU Lesser General   ////
26
//// Public License as published by the Free Software Foundation; ////
27
//// either version 2.1 of the License, or (at your option) any   ////
28
//// later version.                                               ////
29
////                                                              ////
30
//// This source is distributed in the hope that it will be       ////
31
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33
//// PURPOSE.  See the GNU Lesser General Public License for more ////
34
//// details.                                                     ////
35
////                                                              ////
36
//// You should have received a copy of the GNU Lesser General    ////
37
//// Public License along with this source; if not, download it   ////
38
//// from http://www.opencores.org/lgpl.shtml                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
//
42
// CVS Revision History
43
//
44
// $Log: not supported by cvs2svn $
45 124 tadejm
// Revision 1.4  2003/08/21 20:56:40  tadejm
46
// WB Master is now WISHBONE B3 compatible.
47
//
48 117 tadejm
// Revision 1.3  2003/03/14 15:31:57  mihad
49
// Entered the option to disable no response counter in wb master.
50
//
51 86 mihad
// Revision 1.2  2003/01/30 22:01:09  mihad
52
// Updated synchronization in top level fifo modules.
53
//
54 81 mihad
// Revision 1.1  2003/01/27 16:49:31  mihad
55
// Changed module and file names. Updated scripts accordingly. FIFO synchronizations changed.
56
//
57 77 mihad
// Revision 1.7  2002/12/05 12:19:23  mihad
58
// *** empty log message ***
59
//
60
// Revision 1.6  2002/10/11 14:15:29  mihad
61
// Cleaned up non-blocking assignments in combinatinal logic statements
62
//
63
// Revision 1.5  2002/03/05 11:53:47  mihad
64
// Added some testcases, removed un-needed fifo signals
65
//
66
// Revision 1.4  2002/02/19 16:32:37  mihad
67
// Modified testbench and fixed some bugs
68
//
69
// Revision 1.3  2002/02/01 15:25:13  mihad
70
// Repaired a few bugs, updated specification, added test bench files and design document
71
//
72
// Revision 1.2  2001/10/05 08:14:30  mihad
73
// Updated all files with inclusion of timescale file for simulation purposes.
74
//
75
// Revision 1.1.1.1  2001/10/02 15:33:47  mihad
76
// New project directory structure
77
//
78
//
79
 
80
`define WB_FSM_BITS 3 // number of bits needed for FSM states
81
 
82
 
83
`include "bus_commands.v"
84
`include "pci_constants.v"
85
//synopsys translate_off
86
`include "timescale.v"
87
//synopsys translate_on
88
 
89
module pci_wb_master
90
                 (  wb_clock_in,        // CLK_I
91
                    reset_in,           // RST_I
92
 
93
                    pci_tar_read_request,
94
                    pci_tar_address,
95
                    pci_tar_cmd,
96
                    pci_tar_be,
97
                    pci_tar_burst_ok,
98
                    pci_cache_line_size,
99
                    cache_lsize_not_zero,
100
                    wb_read_done_out,
101
                    w_attempt,
102
 
103
                    pcir_fifo_wenable_out,
104
                    pcir_fifo_data_out,
105
                    pcir_fifo_be_out,
106
                    pcir_fifo_control_out,
107
                    //pcir_fifo_renable_out,            for PCI Target !!!
108
                    //pcir_fifo_data_in,                for PCI Target !!!
109
                    //pcir_fifo_be_in,                  for PCI Target !!!
110
                    //pcir_fifo_control_in,             for PCI Target !!!
111
                    //pcir_fifo_flush_out,              for PCI Target !!!
112
                    //pcir_fifo_almost_empty_in,        for PCI Target !!!
113
                    //pcir_fifo_empty_in,               NOT used
114
                    //pcir_fifo_transaction_ready_in,   NOT used
115
                    //pciw_fifo_wenable_out,            for PCI Target !!!
116
                    //pciw_fifo_addr_data_out,          for PCI Target !!!   
117
                    //pciw_fifo_cbe_out,                for PCI Target !!!
118
                    //pciw_fifo_control_out,            for PCI Target !!!       
119
                    pciw_fifo_renable_out,
120
                    pciw_fifo_addr_data_in,
121
                    pciw_fifo_cbe_in,
122
                    pciw_fifo_control_in,
123
                    //pciw_fifo_flush_out,              NOT used
124
                    //pciw_fifo_almost_full_in,         for PCI Target !!!
125
                    //pciw_fifo_full_in,                for PCI Target !!!
126
                    pciw_fifo_almost_empty_in,
127
                    pciw_fifo_empty_in,
128
                    pciw_fifo_transaction_ready_in,
129
 
130
                    pci_error_sig_out,
131
                    pci_error_bc,
132
                    write_rty_cnt_exp_out,
133
                    error_source_out,
134
                    read_rty_cnt_exp_out,
135
 
136 117 tadejm
                    wb_cyc_o,
137
                    wb_stb_o,
138
                    wb_we_o,
139
                    wb_cti_o,
140
                    wb_bte_o,
141
                    wb_sel_o,
142
                    wb_adr_o,
143
                    wb_dat_i,
144
                    wb_dat_o,
145
                    wb_ack_i,
146
                    wb_rty_i,
147
                    wb_err_i
148
//                    CYC_O,
149
//                    STB_O,
150
//                    WE_O,
151
//                    SEL_O,
152
//                    ADR_O,
153
//                    MDATA_I,
154
//                    MDATA_O,
155
//                    ACK_I,
156
//                    RTY_I,
157
//                    ERR_I,
158 77 mihad
                );
159
 
160
/*----------------------------------------------------------------------------------------------------------------------
161
Various parameters needed for state machine and other stuff
162
----------------------------------------------------------------------------------------------------------------------*/
163
parameter       S_IDLE          = `WB_FSM_BITS'h0 ;
164
parameter       S_WRITE         = `WB_FSM_BITS'h1 ;
165
parameter       S_WRITE_ERR_RTY = `WB_FSM_BITS'h2 ;
166
parameter       S_READ          = `WB_FSM_BITS'h3 ;
167
parameter       S_READ_RTY      = `WB_FSM_BITS'h4 ;
168
parameter       S_TURN_ARROUND  = `WB_FSM_BITS'h5 ;
169
 
170
/*----------------------------------------------------------------------------------------------------------------------
171
System signals inputs
172
wb_clock_in - WISHBONE bus clock input
173
reset_in    - system reset input controlled by bridge's reset logic
174
----------------------------------------------------------------------------------------------------------------------*/
175
input           wb_clock_in ;
176
input           reset_in ;
177
 
178
/*----------------------------------------------------------------------------------------------------------------------
179
Control signals from PCI Target for READS to PCIR_FIFO
180
---------------------------------------------------------------------------------------------------------------------*/
181
input           pci_tar_read_request ;      // read request from PCI Target
182
input   [31:0]  pci_tar_address ;           // address for requested read from PCI Target                   
183
input   [3:0]   pci_tar_cmd ;               // command for requested read from PCI Target                   
184
input   [3:0]   pci_tar_be ;                // byte enables for requested read from PCI Target              
185
input           pci_tar_burst_ok ;
186
input   [7:0]   pci_cache_line_size ;       // CACHE line size register value for burst length   
187
input           cache_lsize_not_zero ;
188
output          wb_read_done_out ;              // read done and PCIR_FIFO has data ready
189
output          w_attempt ;
190
 
191
reg             wb_read_done_out ;
192
reg             wb_read_done ;
193
 
194
/*----------------------------------------------------------------------------------------------------------------------
195
PCIR_FIFO control signals used for sinking data into PCIR_FIFO and status monitoring
196
---------------------------------------------------------------------------------------------------------------------*/
197
output          pcir_fifo_wenable_out ;     // PCIR_FIFO write enable output
198
output  [31:0]  pcir_fifo_data_out ;        // data output to PCIR_FIFO
199
output  [3:0]   pcir_fifo_be_out ;          // byte enable output to PCIR_FIFO
200
output  [3:0]   pcir_fifo_control_out ;     // control bus output to PCIR_FIFO
201
 
202
reg     [31:0]  pcir_fifo_data_out ;
203
reg             pcir_fifo_wenable_out ;
204
reg             pcir_fifo_wenable ;
205
reg     [3:0]   pcir_fifo_control_out ;
206
reg     [3:0]   pcir_fifo_control ;
207
 
208
/*----------------------------------------------------------------------------------------------------------------------
209
PCIW_FIFO control signals used for fetching data from PCIW_FIFO and status monitoring
210
---------------------------------------------------------------------------------------------------------------------*/
211
output          pciw_fifo_renable_out ;     // read enable for PCIW_FIFO output
212
input   [31:0]  pciw_fifo_addr_data_in ;    // address and data input from PCIW_FIFO
213
input   [3:0]   pciw_fifo_cbe_in ;          // command and byte_enables from PCIW_FIFO
214
input   [3:0]   pciw_fifo_control_in ;      // control bus input from PCIW_FIFO
215
input           pciw_fifo_almost_empty_in ; // almost empty status indicator from PCIW_FIFO
216
input           pciw_fifo_empty_in ;        // empty status indicator from PCIW_FIFO
217
input           pciw_fifo_transaction_ready_in ;    // write transaction is ready in PCIW_FIFO
218
 
219
reg             pciw_fifo_renable_out ;
220
reg             pciw_fifo_renable ;
221
 
222
/*----------------------------------------------------------------------------------------------------------------------
223
Control INPUT / OUTPUT signals for configuration space reporting registers !!!
224
---------------------------------------------------------------------------------------------------------------------*/
225
output          pci_error_sig_out ;         // When error occures (on WB bus, retry counter, etc.)
226
output  [3:0]   pci_error_bc ;              // bus command at which error occured !
227
output          write_rty_cnt_exp_out ;     // Signaling that RETRY counter has expired during write transaction!
228
output          read_rty_cnt_exp_out ;      // Signaling that RETRY counter has expired during read transaction!
229
                                            //  if error_source is '0' other side didn't respond
230
                                            //  if error_source is '1' other side RETRIED for max retry counter value
231
output          error_source_out ;          // Signaling error source - '0' other WB side signaled error OR didn't respond
232
                                            //   if '1' wridge counted max value in retry counter because of RTY responds
233
reg             pci_error_sig_out ;
234
reg             write_rty_cnt_exp_out ;
235
reg             read_rty_cnt_exp_out ;
236
reg             error_source_out ;
237
 
238
/*----------------------------------------------------------------------------------------------------------------------
239
WISHBONE bus interface signals - can be connected directly to WISHBONE bus
240
---------------------------------------------------------------------------------------------------------------------*/
241 117 tadejm
output          wb_cyc_o;         // cycle indicator output
242
output          wb_stb_o;         // strobe output - data is valid when strobe and cycle indicator are high
243
output          wb_we_o;          // write enable output - 1 - write operation, 0 - read operation
244
output  [2:0]   wb_cti_o;         // WB B3 - cycle type identifier
245
output  [1:0]   wb_bte_o;         // WB B3 - burst type 
246
output  [3:0]   wb_sel_o;         // Byte select outputs
247
output  [31:0]  wb_adr_o;         // WISHBONE address output
248
input   [31:0]  wb_dat_i;         // WISHBONE interface input data bus
249
output  [31:0]  wb_dat_o;         // WISHBONE interface output data bus
250
input           wb_ack_i;         // Acknowledge input - qualifies valid data on data output bus or received data on data input bus
251
input           wb_rty_i;         // retry input - signals from WISHBONE slave that cycle should be terminated and retried later
252
input           wb_err_i;         // Signals from WISHBONE slave that access resulted in an error
253 77 mihad
 
254 117 tadejm
reg             wb_cyc_o;
255
reg             wb_stb_o;
256
reg             wb_we_o;
257
reg     [2:0]   wb_cti_o;
258
reg     [1:0]   wb_bte_o;
259
reg     [3:0]   wb_sel_o;
260
reg     [31:0]  wb_dat_o;
261 77 mihad
 
262
 
263 117 tadejm
 
264 77 mihad
/*###########################################################################################################
265
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
266
    LOGIC, COUNTERS, STATE MACHINE and some control register bits
267
    =============================================================
268
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
269
###########################################################################################################*/
270
 
271
reg             last_data_transferred ; // signal is set by STATE MACHINE after each complete transfere !
272
 
273
// wire for write attempt - 1 when PCI Target attempt to write and PCIW_FIFO has a write transaction ready
274 117 tadejm
reg     w_attempt;
275
always@(posedge wb_clock_in or posedge reset_in)
276
begin
277
    if (reset_in)
278
        w_attempt <= #`FF_DELAY 1'b0;
279
    else
280 77 mihad
    begin
281 117 tadejm
        if (pciw_fifo_transaction_ready_in && ~pciw_fifo_empty_in)
282
            w_attempt <= #`FF_DELAY 1'b1;
283 77 mihad
        else
284 117 tadejm
            if (last_data_transferred)
285
                w_attempt <= #`FF_DELAY 1'b0;
286 77 mihad
    end
287 117 tadejm
end
288 77 mihad
 
289
// wire for read attempt - 1 when PCI Target is attempting a read and PCIR_FIFO is not full !
290
// because of transaction ordering, PCI Master must not start read untill all writes are done -> at that
291
//   moment PCIW_FIFO is empty !!! (when read is pending PCI Target will block new reads and writes)
292 81 mihad
wire r_attempt = ( pci_tar_read_request && !w_attempt && pciw_fifo_empty_in ) ;
293 77 mihad
 
294
// Signal is used for reads on WB, when there is retry!
295
reg             first_wb_data_access ;
296
 
297
reg             last_data_from_pciw_fifo ;  // signal tells when there is last data in pciw_fifo
298
reg             last_data_from_pciw_fifo_reg ;
299
reg             last_data_to_pcir_fifo ;    // signal tells when there will be last data for pcir_fifo
300
 
301
// Logic used in State Machine logic implemented out of State Machine because of less delay!
302 117 tadejm
always@(posedge wb_clock_in or posedge reset_in)
303 77 mihad
begin
304 117 tadejm
    if (reset_in)
305
        last_data_from_pciw_fifo <= #`FF_DELAY 1'b0 ;
306 77 mihad
    else
307 117 tadejm
    begin
308
        if ((pciw_fifo_renable_out) &&
309
            (pciw_fifo_control_in[`LAST_CTRL_BIT] || pciw_fifo_almost_empty_in)) // if last data is going to be transfered
310
            last_data_from_pciw_fifo <= #`FF_DELAY 1'b1 ; // signal for last data from PCIW_FIFO
311
        else
312
            last_data_from_pciw_fifo <= #`FF_DELAY 1'b0 ;
313
    end
314 77 mihad
end
315
 
316
    reg read_count_load;
317
    reg read_count_enable;
318
 
319
    reg [(`PCIR_ADDR_LENGTH - 1):0] max_read_count ;
320
    always@(pci_cache_line_size or cache_lsize_not_zero or pci_tar_cmd)
321
    begin
322
        if (cache_lsize_not_zero)
323
            if ( (pci_cache_line_size >= `PCIR_DEPTH) || (~pci_tar_cmd[1] && ~pci_tar_cmd[0]) )
324
                // If cache line size is larger than FIFO or BC_MEM_READ_MUL command is performed!
325
                max_read_count = `PCIR_DEPTH - 1'b1;
326
            else
327
                max_read_count = pci_cache_line_size ;
328
        else
329
            max_read_count = 1'b1;
330
    end
331
 
332
    reg [(`PCIR_ADDR_LENGTH - 1):0] read_count ;
333
 
334
    // cache line bound indicator - it signals when data for one complete cacheline was read
335
    wire read_bound_comb = ~|( { read_count[(`PCIR_ADDR_LENGTH - 1):2], read_count[0] } ) ;
336
 
337
    reg  read_bound ;
338
    always@(posedge wb_clock_in or posedge reset_in)
339
    begin
340
        if ( reset_in )
341
            read_bound <= #`FF_DELAY 1'b0 ;
342
        else if (read_count_load)
343
            read_bound <= #`FF_DELAY 1'b0 ;
344
        else if ( read_count_enable )
345
            read_bound <= #`FF_DELAY read_bound_comb ;
346
    end
347
 
348
    // down counter with load
349
    always@(posedge reset_in or posedge wb_clock_in)
350
    begin
351
        if (reset_in)
352
            read_count <= #`FF_DELAY 0 ;
353
        else
354
        if (read_count_load)
355
            read_count <= #`FF_DELAY max_read_count ;
356
        else
357
        if (read_count_enable)
358
            read_count <= #`FF_DELAY read_count - 1'b1 ;
359
    end
360
 
361
// Logic used in State Machine logic implemented out of State Machine because of less delay!
362
//   definition of signal telling, when there is last data written into FIFO
363
always@(pci_tar_cmd or pci_tar_burst_ok or read_bound)
364
begin
365
    // burst is OK for reads when there is ((MEM_READ_LN or MEM_READ_MUL) and AD[1:0]==2'b00) OR
366
    //   (MEM_READ and Prefetchable_IMAGE and AD[1:0]==2'b00) -> pci_tar_burst_ok
367
    case ({pci_tar_cmd, pci_tar_burst_ok})
368
    {`BC_MEM_READ, 1'b1},
369
    {`BC_MEM_READ_LN, 1'b1} :
370
    begin   // when burst cycle
371
        if (read_bound)
372
            last_data_to_pcir_fifo = 1'b1 ;
373
        else
374
            last_data_to_pcir_fifo = 1'b0 ;
375
    end
376
    {`BC_MEM_READ_MUL, 1'b1} :
377
    begin   // when burst cycle
378
        if (read_bound)
379
            last_data_to_pcir_fifo = 1'b1 ;
380
        else
381
            last_data_to_pcir_fifo = 1'b0 ;
382
    end
383
    default :
384
    // {`BC_IO_READ, 1'b0},
385
    // {`BC_IO_READ, 1'b1},
386
    // {`BC_MEM_READ, 1'b0},
387
    // {`BC_MEM_READ_LN, 1'b0},
388
    // {`BC_MEM_READ_MUL, 1'b0}:
389
    begin   // when single cycle
390
        last_data_to_pcir_fifo = 1'b1 ;
391
    end
392
    endcase
393
end
394
 
395 86 mihad
reg             wait_for_wb_response ;
396
 
397
`ifdef PCI_WBM_NO_RESPONSE_CNT_DISABLE
398
wire set_retry = 1'b0 ;
399
 
400
`else
401 77 mihad
reg     [3:0]   wb_no_response_cnt ;
402
reg     [3:0]   wb_response_value ;
403
reg             set_retry ; // 
404
 
405
// internal WB no response retry generator counter!
406
always@(posedge reset_in or posedge wb_clock_in)
407
begin
408
    if (reset_in)
409
        wb_no_response_cnt <= #`FF_DELAY 4'h0 ;
410
    else
411
        wb_no_response_cnt <= #`FF_DELAY wb_response_value ;
412
end
413
// internal WB no response retry generator logic
414
always@(wait_for_wb_response or wb_no_response_cnt)
415
begin
416
    if (wb_no_response_cnt == 4'h8) // when there isn't response for 8 clocks, set internal retry
417
    begin
418
        wb_response_value = 4'h0 ;
419
        set_retry = 1'b1 ;
420
    end
421
    else
422
    begin
423
        if (wait_for_wb_response)
424
            wb_response_value = wb_no_response_cnt + 1'h1 ; // count clocks when no response
425
        else
426
            wb_response_value = 4'h0 ;
427
        set_retry = 1'b0 ;
428
    end
429
end
430 86 mihad
`endif
431 77 mihad
 
432 117 tadejm
wire    retry = wb_rty_i || set_retry ; // retry signal - logic OR function between wb_rty_i and internal WB no response retry!
433 77 mihad
reg     [7:0]   rty_counter ; // output from retry counter
434
reg     [7:0]   rty_counter_in ; // input value - output value + 1 OR output value
435
reg             rty_counter_almost_max_value ; // signal tells when retry counter riches maximum value - 1!
436
reg             reset_rty_cnt ; // signal for asynchronous reset of retry counter after each complete transfere
437
 
438
// sinchronous signal after each transfere and asynchronous signal 'reset_rty_cnt' after reset  
439
//   for reseting the retry counter
440
always@(posedge reset_in or posedge wb_clock_in)
441
begin
442
    if (reset_in)
443
        reset_rty_cnt <= #`FF_DELAY 1'b1 ; // asynchronous set when reset signal is active
444
    else
445 117 tadejm
        reset_rty_cnt <= #`FF_DELAY wb_ack_i || wb_err_i || last_data_transferred ; // synchronous set after completed transfere
446 77 mihad
end
447
 
448
// Retry counter register control
449
always@(posedge reset_in or posedge wb_clock_in)
450
begin
451
    if (reset_in)
452
        rty_counter <= #`FF_DELAY 8'h00 ;
453
    else
454
    begin
455
        if (reset_rty_cnt)
456
            rty_counter <= #`FF_DELAY 8'h00 ;
457
        else if (retry)
458
            rty_counter <= #`FF_DELAY rty_counter_in ;
459
    end
460
end
461
// Retry counter logic
462
always@(rty_counter)
463
begin
464
    if(rty_counter == `WB_RTY_CNT_MAX - 1'b1) // stop counting
465
    begin
466
        rty_counter_in = rty_counter ;
467
        rty_counter_almost_max_value = 1'b1 ;
468
    end
469
    else
470
    begin
471
        rty_counter_in = rty_counter + 1'b1 ; // count up
472
        rty_counter_almost_max_value = 1'b0 ;
473
    end
474
end
475
 
476
reg     [31:0]  addr_cnt_out ;  // output value from address counter to WB ADDRESS output
477
reg     [31:0]  addr_cnt_in ;   // input address value to address counter
478
reg             addr_into_cnt ; // control signal for loading starting address into counter
479 117 tadejm
reg             addr_into_cnt_reg ;
480 77 mihad
reg             addr_count ; // control signal for count enable
481
reg     [3:0]   bc_register ; // used when error occures during writes!
482
 
483
// wb address counter register control
484
always@(posedge wb_clock_in or posedge reset_in)
485
begin
486
    if (reset_in) // reset counter
487
    begin
488
        addr_cnt_out <= #`FF_DELAY 32'h0000_0000 ;
489
        bc_register  <= #`FF_DELAY 4'h0 ;
490 117 tadejm
        addr_into_cnt_reg <= #`FF_DELAY 1'b0;
491 77 mihad
    end
492
    else
493
    begin
494
        addr_cnt_out <= #`FF_DELAY addr_cnt_in ; // count up or hold value depending on cache line counter logic
495 117 tadejm
        addr_into_cnt_reg <= #`FF_DELAY addr_into_cnt;
496 77 mihad
        if (addr_into_cnt)
497
            bc_register  <= #`FF_DELAY pciw_fifo_cbe_in ;
498
    end
499
end
500
 
501
// when '1', the bus command is IO command - not supported commands are checked in pci_decoder modules
502
wire    io_memory_bus_command = !pci_tar_cmd[3] && !pci_tar_cmd[2] ;
503
 
504
// wb address counter logic
505
always@(addr_into_cnt or r_attempt or addr_count or pciw_fifo_addr_data_in or pci_tar_address or addr_cnt_out or
506
        io_memory_bus_command)
507
begin
508
    if (addr_into_cnt) // load starting address into counter
509
    begin
510
        if (r_attempt)
511
        begin // if read request, then load read addresss from PCI Target
512
            addr_cnt_in = {pci_tar_address[31:2], pci_tar_address[1] && io_memory_bus_command,
513
                                                  pci_tar_address[0] && io_memory_bus_command} ;
514
        end
515
        else
516
        begin // if not read request, then load write address from PCIW_FIFO
517
            addr_cnt_in = pciw_fifo_addr_data_in[31:0] ;
518
        end
519
    end
520
    else
521
    if (addr_count)
522
    begin
523
        addr_cnt_in = addr_cnt_out + 3'h4 ; // count up for 32-bit alligned address 
524
    end
525
    else
526
    begin
527
        addr_cnt_in = addr_cnt_out ;
528
    end
529
end
530
 
531
reg retried ; // Signal is output value from FF and is set for one clock period after retried_d is set
532
reg retried_d ; // Signal is set whenever cycle is retried and is input to FF for delaying -> used in S_IDLE state
533
reg retried_write;
534
reg rty_i_delayed; // Dignal used for determinig the source of retry!
535
 
536
reg     first_data_is_burst ; // Signal is set in S_WRITE or S_READ states, when data transfere is burst!
537
reg     first_data_is_burst_reg ;
538
wire    burst_transfer ; // This signal is set when data transfere is burst and is reset with RESET or last data transfered
539 117 tadejm
reg     burst_chopped; // This signal is set when WB_SEL_O is changed during burst write transaction
540
reg     burst_chopped_delayed;
541 77 mihad
 
542
// FFs output signals tell, when there is first data out from FIFO (for BURST checking)
543
//   and for delaying retried signal
544
always@(posedge wb_clock_in or posedge reset_in)
545
begin
546
    if (reset_in) // reset signals
547
    begin
548
        retried <= #`FF_DELAY 1'b0 ;
549
        retried_write <= #`FF_DELAY 1'b0 ;
550
        rty_i_delayed <= #`FF_DELAY 1'B0 ;
551
    end
552
    else
553
    begin
554
        retried <= #`FF_DELAY retried_d ; // delaying retried signal  
555
        retried_write <= #`FF_DELAY retried ;
556 117 tadejm
        rty_i_delayed <= #`FF_DELAY wb_rty_i ;
557 77 mihad
    end
558
end
559
 
560
// Determinig if first data is a part of BURST or just a single transfere!
561
always@(addr_into_cnt or r_attempt or pci_tar_burst_ok or max_read_count or
562
        pciw_fifo_control_in or pciw_fifo_empty_in)
563
begin
564
    if (addr_into_cnt)
565
    begin
566
        if (r_attempt)
567
        begin
568
                // burst is OK for reads when there is ((MEM_READ_LN or MEM_READ_MUL) and AD[1:0]==2'b00) OR
569
                //   (MEM_READ and Prefetchable_IMAGE and AD[1:0]==2'b00) -> pci_tar_burst_ok
570
            if  (pci_tar_burst_ok && (max_read_count != 8'h1))
571
                first_data_is_burst = 1'b1 ;
572
            else
573
                first_data_is_burst = 1'b0 ;
574
        end
575
        else
576
        begin
577
            first_data_is_burst = 1'b0 ;
578
        end
579
    end
580
    else
581 117 tadejm
        first_data_is_burst = pciw_fifo_control_in[`BURST_BIT] && ~pciw_fifo_empty_in &&
582
                              ~pciw_fifo_control_in[`LAST_CTRL_BIT] /*&& ~pciw_fifo_control_in[`DATA_ERROR_CTRL_BIT]*/;
583 77 mihad
end
584
 
585
// FF for seting and reseting burst_transfer signal
586
always@(posedge wb_clock_in or posedge reset_in)
587
begin
588
    if (reset_in)
589 117 tadejm
    begin
590
        burst_chopped         <= #`FF_DELAY 1'b0;
591
        burst_chopped_delayed <= #`FF_DELAY 1'b0;
592 77 mihad
        first_data_is_burst_reg <= #`FF_DELAY 1'b0 ;
593 117 tadejm
    end
594 77 mihad
    else
595
    begin
596 117 tadejm
        if (pciw_fifo_transaction_ready_in)
597
        begin
598
            if (pciw_fifo_control_in[`DATA_ERROR_CTRL_BIT])
599
                burst_chopped     <= #`FF_DELAY 1'b1;
600
            else if (wb_ack_i || wb_err_i || wb_rty_i)
601
                burst_chopped     <= #`FF_DELAY 1'b0;
602
        end
603
        else
604
            burst_chopped         <= #`FF_DELAY 1'b0;
605
        burst_chopped_delayed <= #`FF_DELAY burst_chopped;
606 77 mihad
        if (last_data_transferred || first_data_is_burst)
607
            first_data_is_burst_reg <= #`FF_DELAY ~last_data_transferred ;
608
    end
609
end
610 117 tadejm
assign  burst_transfer = first_data_is_burst || first_data_is_burst_reg ;
611 77 mihad
 
612
reg [(`WB_FSM_BITS - 1):0]  c_state ; //current state register
613
reg [(`WB_FSM_BITS - 1):0]  n_state ; //next state input to current state register
614
 
615 117 tadejm
//##################################
616
// WISHBONE B3 master state machine
617
//##################################
618
 
619
// state machine register control and registered outputs (without wb_adr_o counter)
620 77 mihad
always@(posedge wb_clock_in or posedge reset_in)
621
begin
622 117 tadejm
    if (reset_in) // reset state machine to S_IDLE state
623
    begin
624
        c_state  <= #`FF_DELAY  S_IDLE;
625
        wb_cyc_o <= #`FF_DELAY  1'b0;
626
        wb_stb_o <= #`FF_DELAY  1'b0;
627
        wb_we_o  <= #`FF_DELAY  1'b0;
628
        wb_cti_o <= #`FF_DELAY  3'h2;
629
        wb_bte_o <= #`FF_DELAY  2'h0;
630
        wb_sel_o <= #`FF_DELAY  4'h0;
631
        wb_dat_o <= #`FF_DELAY 32'h0;
632
        pcir_fifo_data_out <= #`FF_DELAY 32'h0;
633
        pcir_fifo_control_out <= #`FF_DELAY 4'h0;
634
        pcir_fifo_wenable_out <= #`FF_DELAY 1'b0;
635
    end
636 77 mihad
    else
637 117 tadejm
    begin
638
        c_state  <= #`FF_DELAY  n_state;
639
        wb_bte_o <= #`FF_DELAY  2'h0;
640
        case (n_state) // synthesis parallel_case full_case
641
        S_WRITE:
642
        begin
643
            wb_cyc_o <= #`FF_DELAY  ~addr_into_cnt;
644
            wb_stb_o <= #`FF_DELAY  ~addr_into_cnt;
645
            wb_we_o  <= #`FF_DELAY  ~addr_into_cnt;
646
            // if '1' then next burst BE is not equat to current one => burst is chopped into singles
647
            // OR if last data is going to be transfered
648
            if ((wb_stb_o && wb_ack_i) || addr_into_cnt_reg || (~wb_cyc_o && (retried || burst_chopped_delayed)))
649
            begin
650
                if (burst_transfer && ~pciw_fifo_control_in[`DATA_ERROR_CTRL_BIT] &&
651
                   ~(pciw_fifo_renable_out && (pciw_fifo_control_in[`LAST_CTRL_BIT] || pciw_fifo_almost_empty_in)))
652
                    wb_cti_o <= #`FF_DELAY  3'h2;
653
                else
654
                    wb_cti_o <= #`FF_DELAY  3'h7;
655
            end
656
            if ((pciw_fifo_renable_out && ~addr_into_cnt) || addr_into_cnt_reg)
657
            begin
658
                wb_sel_o <= #`FF_DELAY  ~pciw_fifo_cbe_in;
659
                wb_dat_o <= #`FF_DELAY  pciw_fifo_addr_data_in;
660
            end
661
        end
662
        S_WRITE_ERR_RTY:
663
        begin
664
            wb_cyc_o <= #`FF_DELAY  1'b0;
665
            wb_stb_o <= #`FF_DELAY  1'b0;
666
            wb_we_o  <= #`FF_DELAY  1'b0;
667
            wb_cti_o <= #`FF_DELAY  3'h2;
668
            // stay the same as previous
669
            //wb_sel_o <= #`FF_DELAY  4'h0;
670
            //wb_dat_o <= #`FF_DELAY 32'h0;
671
        end
672
        S_READ:
673
        begin
674
            wb_cyc_o <= #`FF_DELAY  ~addr_into_cnt;
675
            wb_stb_o <= #`FF_DELAY  ~addr_into_cnt;
676
            wb_we_o  <= #`FF_DELAY  1'b0;
677
            if ((wb_stb_o && wb_ack_i) || addr_into_cnt_reg || (~wb_cyc_o && retried))
678
            begin
679
                if (burst_transfer && ~read_bound_comb)
680
                    wb_cti_o <= #`FF_DELAY  3'h2;
681
                else
682
                    wb_cti_o <= #`FF_DELAY  3'h7;
683
            end
684
            if (burst_transfer)
685
                wb_sel_o <= #`FF_DELAY  4'hF;
686
            else
687
                wb_sel_o <= #`FF_DELAY  ~pci_tar_be;
688
            // no need to change att all
689
            //wb_dat_o <= #`FF_DELAY 32'h0;
690
        end
691
        S_READ_RTY:
692
        begin
693
            wb_cyc_o <= #`FF_DELAY  1'b0;
694
            wb_stb_o <= #`FF_DELAY  1'b0;
695
            wb_we_o  <= #`FF_DELAY  1'b0;
696
            wb_cti_o <= #`FF_DELAY  3'h2;
697
            // no need to change att all
698
            //wb_sel_o <= #`FF_DELAY  4'h0;
699
            //wb_dat_o <= #`FF_DELAY 32'h0;
700
        end
701
        S_TURN_ARROUND:
702
        begin
703
            wb_cyc_o <= #`FF_DELAY  1'b0;
704
            wb_stb_o <= #`FF_DELAY  1'b0;
705
            wb_we_o  <= #`FF_DELAY  1'b0;
706
            wb_cti_o <= #`FF_DELAY  3'h2;
707
            // no need to change att all
708
            //wb_sel_o <= #`FF_DELAY  4'h0;
709
            //wb_dat_o <= #`FF_DELAY 32'h0;
710
        end
711
        default: // S_IDLE:
712
        begin
713
            wb_cyc_o <= #`FF_DELAY  1'b0;
714
            wb_stb_o <= #`FF_DELAY  1'b0;
715
            wb_we_o  <= #`FF_DELAY  1'b0;
716
            wb_cti_o <= #`FF_DELAY  3'h2;
717
            // no need to change att all
718
            //wb_sel_o <= #`FF_DELAY  4'h0;
719
            //wb_dat_o <= #`FF_DELAY 32'h0;
720
        end
721
        endcase
722
        pcir_fifo_data_out <= #`FF_DELAY  wb_dat_i;
723
        pcir_fifo_control_out <= #`FF_DELAY pcir_fifo_control ;
724
        pcir_fifo_wenable_out <= #`FF_DELAY pcir_fifo_wenable ;
725
    end
726 77 mihad
end
727
 
728 117 tadejm
assign  wb_adr_o = addr_cnt_out ;
729
 
730 77 mihad
// state machine logic
731
always@(c_state or
732 117 tadejm
        wb_ack_i or
733
        wb_rty_i or
734
        wb_err_i or
735 77 mihad
        w_attempt or
736
        r_attempt or
737
        retried or
738 117 tadejm
        burst_chopped or
739
        burst_chopped_delayed or
740 77 mihad
        rty_i_delayed or
741
        pci_tar_read_request or
742
        rty_counter_almost_max_value or
743
        set_retry or
744
        last_data_to_pcir_fifo or
745
        first_wb_data_access or
746 124 tadejm
        pciw_fifo_control_in or
747
        pciw_fifo_empty_in or
748
        burst_transfer or
749 77 mihad
        last_data_from_pciw_fifo_reg
750
        )
751
begin
752
    case (c_state)
753
    S_IDLE:
754
        begin
755
            // Default values for signals not used in this state
756
            pcir_fifo_wenable = 1'b0 ;
757
            pcir_fifo_control = 4'h0 ;
758
            addr_count = 1'b0 ;
759
            read_count_enable = 1'b0 ;
760
            pci_error_sig_out = 1'b0 ;
761
            error_source_out = 1'b0 ;
762
            retried_d = 1'b0 ;
763
            last_data_transferred = 1'b0 ;
764
            wb_read_done = 1'b0 ;
765
            wait_for_wb_response = 1'b0 ;
766
            write_rty_cnt_exp_out = 1'b0 ;
767
            pci_error_sig_out = 1'b0 ;
768
            read_rty_cnt_exp_out = 1'b0 ;
769
            case ({w_attempt, r_attempt, retried})
770
            3'b101 : // Write request for PCIW_FIFO to WB bus transaction
771
            begin    // If there was retry, the same transaction must be initiated
772
                pciw_fifo_renable = 1'b0 ; // the same data
773
                addr_into_cnt = 1'b0 ; // the same address
774
                read_count_load = 1'b0 ; // no need for cache line when there is write
775
                n_state = S_WRITE ;
776
            end
777
            3'b100 : // Write request for PCIW_FIFO to WB bus transaction
778
            begin    // If there is new transaction
779 117 tadejm
                if (burst_chopped_delayed)
780
                begin
781
                  addr_into_cnt = 1'b0 ; // address must not be latched into address counter
782
                  pciw_fifo_renable = 1'b1 ; // first location is address (in FIFO), next will be data
783
                end
784
                else
785
                begin
786
                  if (pciw_fifo_control_in[`ADDR_CTRL_BIT])
787
                      addr_into_cnt = 1'b1 ; // address must be latched into address counter
788
                  else
789
                      addr_into_cnt = 1'b0 ;
790
                  pciw_fifo_renable = 1'b1 ; // first location is address (in FIFO), next will be data
791
                end
792 77 mihad
                read_count_load = 1'b0 ; // no need for cache line when there is write
793
                n_state = S_WRITE ;
794
            end
795
            3'b011 : // Read request from PCI Target for WB bus to PCIR_FIFO transaction
796
            begin    // If there was retry, the same transaction must be initiated
797
                addr_into_cnt = 1'b0 ; // the same address
798
                read_count_load = 1'b0 ; // cache line counter must not be changed for retried read
799
                pciw_fifo_renable = 1'b0 ; // don't read from FIFO, when read transaction from WB to FIFO
800
                n_state = S_READ ;
801
            end
802
            3'b010 : // Read request from PCI Target for WB bus to PCIR_FIFO transaction
803
            begin    // If there is new transaction
804
                addr_into_cnt = 1'b1 ; // address must be latched into counter from separate request bus
805
                read_count_load = 1'b1 ; // cache line size must be latched into its counter
806
                pciw_fifo_renable = 1'b0 ; // don't read from FIFO, when read transaction from WB to FIFO
807
                n_state = S_READ ;
808
            end
809
            default : // stay in IDLE state
810
            begin
811
                pciw_fifo_renable = 1'b0 ;
812
                addr_into_cnt = 1'b0 ;
813
                read_count_load = 1'b0 ;
814
                n_state = S_IDLE ;
815
            end
816
            endcase
817
        end
818
    S_WRITE: // WRITE from PCIW_FIFO to WB bus
819
        begin
820
            // Default values for signals not used in this state
821
            pcir_fifo_wenable = 1'b0 ;
822
            pcir_fifo_control = 4'h0 ;
823
            addr_into_cnt = 1'b0 ;
824
            read_count_load = 1'b0 ;
825
            read_count_enable = 1'b0 ;
826
            wb_read_done = 1'b0 ;
827
            read_rty_cnt_exp_out = 1'b0 ;
828 117 tadejm
            case ({wb_ack_i, wb_err_i, wb_rty_i})
829 77 mihad
            3'b100 : // If writting of one data is acknowledged
830
            begin
831
                addr_count = 1'b1 ; // prepare next address if there will be burst
832 117 tadejm
                retried_d = 1'b0 ; // there was no retry
833 77 mihad
                pci_error_sig_out = 1'b0 ; // there was no error
834
                error_source_out = 1'b0 ;
835
                write_rty_cnt_exp_out = 1'b0 ; // there was no retry
836
                wait_for_wb_response = 1'b0 ;
837 117 tadejm
                // if last data was transfered !
838
                if (last_data_from_pciw_fifo_reg)
839 77 mihad
                begin
840 117 tadejm
                    n_state = S_TURN_ARROUND;
841
                    if (~pciw_fifo_empty_in)
842
                        pciw_fifo_renable = 1'b0 ; // prepare next value (address when new trans., data when burst tran.)
843
                    else
844
                        pciw_fifo_renable = 1'b0 ;
845 77 mihad
                    last_data_transferred = 1'b1 ; // signal for last data transfered
846
                end
847 117 tadejm
                // next burst data has different byte enables !
848
                else if (burst_transfer && burst_chopped)
849
                begin
850
                    n_state = S_IDLE ;
851
                    pciw_fifo_renable = 1'b0 ; // next value (address when new trans., data when burst tran.)
852
                    last_data_transferred = 1'b0 ;
853
                end
854 77 mihad
                else
855
                begin
856
                    n_state = S_WRITE ;
857 117 tadejm
                    pciw_fifo_renable = 1'b1 ; // prepare next value (address when new trans., data when burst tran.)
858 77 mihad
                    last_data_transferred = 1'b0 ;
859
                end
860
            end
861
            3'b010 : // If writting of one data is terminated with ERROR
862
            begin
863 117 tadejm
                if (~pciw_fifo_empty_in)
864
                    pciw_fifo_renable = 1'b1 ; // prepare next value (address when new trans., data when cleaning FIFO)
865
                else
866
                    pciw_fifo_renable = 1'b0 ;
867 77 mihad
                addr_count = 1'b0 ; // no need for new address
868
                retried_d = 1'b0 ; // there was no retry
869
                last_data_transferred = 1'b1 ; // signal for last data transfered
870
                pci_error_sig_out = 1'b1 ; // segnal for error reporting
871
                error_source_out = 1'b0 ; // error source from other side of WB bus
872
                write_rty_cnt_exp_out = 1'b0 ; // there was no retry
873
                wait_for_wb_response = 1'b0 ;
874
                if (last_data_from_pciw_fifo_reg) // if last data was transfered
875 117 tadejm
                    n_state = S_TURN_ARROUND ; // go to S_TURN_ARROUND for new transfere
876 77 mihad
                else // if there wasn't last data of transfere
877
                    n_state = S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
878
            end
879
            3'b001 : // If writting of one data is retried
880
            begin
881
                addr_count = 1'b0 ;
882
                last_data_transferred = 1'b0 ;
883
                retried_d = 1'b1 ; // there was a retry
884
                wait_for_wb_response = 1'b0 ;
885
                if(rty_counter_almost_max_value) // If retry counter reached maximum allowed value
886
                begin
887 117 tadejm
                    if (last_data_from_pciw_fifo_reg) // if last data was transfered
888 77 mihad
                        pciw_fifo_renable = 1'b0 ;
889 117 tadejm
                    else // if there wasn't last data of transfere
890
                        pciw_fifo_renable = 1'b1 ;
891
                    n_state = S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
892 77 mihad
                    write_rty_cnt_exp_out = 1'b1 ; // signal for reporting write counter expired
893
                    pci_error_sig_out = 1'b1 ;
894
                    error_source_out = 1'b1 ; // error ocuerd because of retry counter
895
                end
896
                else
897
                begin
898 117 tadejm
                    pciw_fifo_renable = 1'b0 ;
899 77 mihad
                    n_state = S_IDLE ; // go to S_IDLE state for retrying the transaction
900
                    write_rty_cnt_exp_out = 1'b0 ; // retry counter hasn't expired yet
901
                    pci_error_sig_out = 1'b0 ;
902
                    error_source_out = 1'b0 ;
903
                end
904
            end
905
            default :
906
            begin
907
                addr_count = 1'b0 ;
908
                last_data_transferred = 1'b0 ;
909
                wait_for_wb_response = 1'b1 ; // wait for WB device to response (after 8 clocks RTY CNT is incremented)
910
                error_source_out = 1'b0 ; // if error ocures, error source is from other WB bus side
911
                if((rty_counter_almost_max_value)&&(set_retry)) // when no WB response and RTY CNT reached maximum allowed value 
912
                begin
913
                    retried_d = 1'b1 ;
914 117 tadejm
                    if (last_data_from_pciw_fifo_reg) // if last data was transfered
915 77 mihad
                        pciw_fifo_renable = 1'b0 ;
916 117 tadejm
                    else // if there wasn't last data of transfere
917
                        pciw_fifo_renable = 1'b1 ;
918
                    n_state = S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
919 77 mihad
                    write_rty_cnt_exp_out = 1'b1 ; // signal for reporting write counter expired
920
                    pci_error_sig_out = 1'b1 ; // signal for error reporting
921
                end
922
                else
923
                begin
924 117 tadejm
                    pciw_fifo_renable = 1'b0 ;
925 77 mihad
                    retried_d = 1'b0 ;
926
                    n_state = S_WRITE ; // stay in S_WRITE state to wait WB to response
927
                    write_rty_cnt_exp_out = 1'b0 ; // retry counter hasn't expired yet
928
                    pci_error_sig_out = 1'b0 ;
929
                end
930
            end
931
            endcase
932
        end
933
    S_WRITE_ERR_RTY: // Clean current write transaction from PCIW_FIFO if ERROR or Retry counter expired occures
934
        begin
935
            pciw_fifo_renable = !last_data_from_pciw_fifo_reg ; // put out next data (untill last data or FIFO empty)
936
            last_data_transferred = 1'b1 ; // after exiting this state, negedge of this signal is used
937
            // Default values for signals not used in this state
938
            pcir_fifo_wenable = 1'b0 ;
939
            pcir_fifo_control = 4'h0 ;
940
            addr_into_cnt = 1'b0 ;
941
            read_count_load = 1'b0 ;
942
            read_count_enable = 1'b0 ;
943
            addr_count = 1'b0 ;
944
            pci_error_sig_out = 1'b0 ;
945
            error_source_out = 1'b0 ;
946
            retried_d = 1'b0 ;
947
            wb_read_done = 1'b0 ;
948
            write_rty_cnt_exp_out = 1'b0 ;
949
            read_rty_cnt_exp_out = 1'b0 ;
950
            wait_for_wb_response = 1'b0 ;
951
            // If last data is cleaned out from PCIW_FIFO
952
            if (last_data_from_pciw_fifo_reg)
953
                n_state = S_IDLE ;
954
            else
955
                n_state = S_WRITE_ERR_RTY ; // Clean until last data is cleaned out from FIFO
956
        end
957
    S_READ: // READ from WB bus to PCIR_FIFO
958
        begin
959
            // Default values for signals not used in this state
960
            pciw_fifo_renable = 1'b0 ;
961
            addr_into_cnt = 1'b0 ;
962
            read_count_load = 1'b0 ;
963
            pci_error_sig_out = 1'b0 ;
964
            error_source_out = 1'b0 ;
965
            write_rty_cnt_exp_out = 1'b0 ;
966 117 tadejm
            case ({wb_ack_i, wb_err_i, wb_rty_i})
967 77 mihad
            3'b100 : // If reading of one data is acknowledged
968
            begin
969
                pcir_fifo_wenable = 1'b1 ; // enable writting data into PCIR_FIFO
970
                addr_count = 1'b1 ; // prepare next address if there will be burst
971
                read_count_enable = 1'b1 ; // decrease counter value for cache line size
972
                retried_d = 1'b0 ; // there was no retry
973
                read_rty_cnt_exp_out = 1'b0 ; // there was no retry
974
                wait_for_wb_response = 1'b0 ;
975
                // if last data was transfered
976
                if (last_data_to_pcir_fifo)
977
                begin
978
                    pcir_fifo_control[`LAST_CTRL_BIT]       = 1'b1 ; // FIFO must indicate LAST data transfered
979
                    pcir_fifo_control[`DATA_ERROR_CTRL_BIT] = 1'b0 ;
980
                    pcir_fifo_control[`UNUSED_CTRL_BIT]     = 1'b0 ;
981
                    pcir_fifo_control[`ADDR_CTRL_BIT]       = 1'b0 ;
982
                    last_data_transferred = 1'b1 ; // signal for last data transfered
983
                    wb_read_done = 1'b1 ; // signal last data of read transaction for PCI Target
984
                    n_state = S_TURN_ARROUND ;
985
                end
986
                else // if not last data transfered
987
                begin
988
                    pcir_fifo_control = 4'h0 ; // ZERO for control code
989
                    last_data_transferred = 1'b0 ; // not last data transfered
990
                    wb_read_done = 1'b0 ; // read is not done yet
991
                    n_state = S_READ ;
992
                end
993
            end
994
            3'b010 : // If reading of one data is terminated with ERROR
995
            begin
996
                pcir_fifo_wenable = 1'b1 ; // enable for writting to FIFO data with ERROR
997
                addr_count = 1'b0 ; // no need for new address
998
                pcir_fifo_control[`LAST_CTRL_BIT]       = 1'b0 ;
999
                pcir_fifo_control[`DATA_ERROR_CTRL_BIT] = 1'b1 ; // FIFO must indicate the DATA with ERROR
1000
                pcir_fifo_control[`UNUSED_CTRL_BIT]     = 1'b0 ;
1001
                pcir_fifo_control[`ADDR_CTRL_BIT]       = 1'b0 ;
1002
                last_data_transferred = 1'b1 ; // signal for last data transfered
1003
                wb_read_done = 1'b1 ; // signal last data of read transaction for PCI Target
1004
                read_count_enable = 1'b0 ; // no need for cache line, when error occures
1005
                n_state = S_TURN_ARROUND ;
1006
                retried_d = 1'b0 ; // there was no retry
1007
                wait_for_wb_response = 1'b0 ;
1008
                read_rty_cnt_exp_out = 1'b0 ; // there was no retry
1009
            end
1010
            3'b001 : // If reading of one data is retried
1011
            begin
1012
                pcir_fifo_wenable = 1'b0 ;
1013
                pcir_fifo_control = 4'h0 ;
1014
                addr_count = 1'b0 ;
1015
                read_count_enable = 1'b0 ;
1016
                wait_for_wb_response = 1'b0 ;
1017
                case ({first_wb_data_access, rty_counter_almost_max_value})
1018
                2'b10 :
1019
                begin  // if first data of the cycle (CYC_O) is retried - after each retry CYC_O goes inactive 
1020
                    n_state = S_IDLE ; // go to S_IDLE state for retrying the transaction
1021
                    read_rty_cnt_exp_out = 1'b0 ; // retry counter hasn't expired yet   
1022
                    last_data_transferred = 1'b0 ;
1023
                    wb_read_done = 1'b0 ;
1024
                    retried_d = 1'b1 ; // there was a retry
1025
                end
1026
                2'b11 :
1027
                begin  // if retry counter reached maximum value
1028
                    n_state = S_READ_RTY ; // go here to wait for PCI Target to remove read request
1029
                    read_rty_cnt_exp_out = 1'b1 ; // signal for reporting read counter expired  
1030
                    last_data_transferred = 1'b0 ;
1031
                    wb_read_done = 1'b0 ;
1032
                    retried_d = 1'b1 ; // there was a retry
1033
                end
1034
                default : // if retry occures after at least 1 data was transferred without breaking cycle (CYC_O inactive)
1035
                begin     // then PCI device will retry access!
1036
                    n_state = S_TURN_ARROUND ; // go to S_TURN_ARROUND state 
1037
                    read_rty_cnt_exp_out = 1'b0 ; // retry counter hasn't expired  
1038
                    last_data_transferred = 1'b1 ;
1039
                    wb_read_done = 1'b1 ;
1040
                    retried_d = 1'b0 ; // retry must not be retried, since there is not a first data
1041
                end
1042
                endcase
1043
            end
1044
            default :
1045
            begin
1046
                addr_count = 1'b0 ;
1047
                read_count_enable = 1'b0 ;
1048
                read_rty_cnt_exp_out = 1'b0 ;
1049
                wait_for_wb_response = 1'b1 ; // wait for WB device to response (after 8 clocks RTY CNT is incremented)
1050
                if((rty_counter_almost_max_value)&&(set_retry)) // when no WB response and RTY CNT reached maximum allowed value 
1051
                begin
1052
                    retried_d = 1'b1 ;
1053
                    n_state = S_TURN_ARROUND ; // go here to stop read request
1054
                    pcir_fifo_wenable = 1'b1 ;
1055
                    pcir_fifo_control[`LAST_CTRL_BIT]       = 1'b0 ;
1056
                    pcir_fifo_control[`DATA_ERROR_CTRL_BIT] = 1'b1 ; // FIFO must indicate the DATA with ERROR
1057
                    pcir_fifo_control[`UNUSED_CTRL_BIT]     = 1'b0 ;
1058
                    pcir_fifo_control[`ADDR_CTRL_BIT]       = 1'b0 ;
1059
                    last_data_transferred = 1'b1 ;
1060
                    wb_read_done = 1'b1 ;
1061
                end
1062
                else
1063
                begin
1064
                    retried_d = 1'b0 ;
1065
                    n_state = S_READ ; // stay in S_READ state to wait WB to response
1066
                    pcir_fifo_wenable = 1'b0 ;
1067
                    pcir_fifo_control = 4'h0 ;
1068
                    last_data_transferred = 1'b0 ;
1069
                    wb_read_done = 1'b0 ;
1070
                end
1071
            end
1072
            endcase
1073
        end
1074
    S_READ_RTY: // Wait for PCI Target to remove read request, when retry counter reaches maximum value!
1075
        begin
1076
            // Default values for signals not used in this state
1077
            pciw_fifo_renable = 1'b0 ;
1078
            pcir_fifo_wenable = 1'b0 ;
1079
            pcir_fifo_control = 4'h0 ;
1080
            addr_into_cnt = 1'b0 ;
1081
            read_count_load = 1'b0 ;
1082
            read_count_enable = 1'b0 ;
1083
            addr_count = 1'b0 ;
1084
            pci_error_sig_out = 1'b0 ;
1085
            error_source_out = 1'b0 ;
1086
            retried_d = 1'b0 ;
1087
            wb_read_done = 1'b0 ;
1088
            write_rty_cnt_exp_out = 1'b0 ;
1089
            read_rty_cnt_exp_out = 1'b0 ;
1090
            wait_for_wb_response = 1'b0 ;
1091
            // wait for PCI Target to remove read request
1092
            if (pci_tar_read_request)
1093
            begin
1094
                n_state = S_READ_RTY ; // stay in this state until read request is removed
1095
                last_data_transferred = 1'b0 ;
1096
            end
1097
            else // when read request is removed
1098
            begin
1099
                n_state = S_IDLE ;
1100
                last_data_transferred = 1'b1 ; // when read request is removed, there is "last" data
1101
            end
1102 117 tadejm
        end
1103
    // Turn arround cycle after writting to PCIR_FIFO (for correct data when reading from PCIW_FIFO)  
1104
    default: // S_TURN_ARROUND: 
1105 77 mihad
        begin
1106
            // Default values for signals not used in this state
1107
            pciw_fifo_renable = 1'b0 ;
1108
            pcir_fifo_wenable = 1'b0 ;
1109
            pcir_fifo_control = 4'h0 ;
1110
            addr_into_cnt = 1'b0 ;
1111
            read_count_load = 1'b0 ;
1112
            read_count_enable = 1'b0 ;
1113
            addr_count = 1'b0 ;
1114
            pci_error_sig_out = 1'b0 ;
1115
            error_source_out = 1'b0 ;
1116
            retried_d = 1'b0 ;
1117
            last_data_transferred = 1'b1 ;
1118
            wb_read_done = 1'b0 ;
1119
            write_rty_cnt_exp_out = 1'b0 ;
1120
            read_rty_cnt_exp_out = 1'b0 ;
1121
            wait_for_wb_response = 1'b0 ;
1122
            n_state = S_IDLE ;
1123
        end
1124
    endcase
1125
end
1126
 
1127
// Signal for retry monitor in state machine when there is read and first (or single) data access
1128 117 tadejm
wire ack_rty_response = wb_ack_i || wb_rty_i ;
1129 77 mihad
 
1130
// Signal first_wb_data_access is set when no WB cycle present till end of first data access of WB cycle on WB bus
1131
always@(posedge wb_clock_in or posedge reset_in)
1132
begin
1133
    if (reset_in)
1134
        first_wb_data_access = 1'b1 ;
1135
    else
1136
    begin
1137
        if (~wb_cyc_o)
1138
            first_wb_data_access = 1'b1 ;
1139
        else if (ack_rty_response)
1140
            first_wb_data_access = 1'b0 ;
1141
    end
1142
end
1143
 
1144
// Signals to FIFO
1145
assign  pcir_fifo_be_out = 4'hf ; // pci_tar_be ;
1146
 
1147 117 tadejm
// Signals to Conf. space
1148 77 mihad
assign  pci_error_bc = bc_register ;
1149
 
1150
 
1151 117 tadejm
always@(posedge wb_clock_in or posedge reset_in)
1152
begin
1153
    if (reset_in)
1154
        wb_read_done_out <= #`FF_DELAY 1'b0 ;
1155
    else
1156
        wb_read_done_out <= #`FF_DELAY wb_read_done ;
1157
end
1158 77 mihad
 
1159 117 tadejm
always@(pciw_fifo_renable or addr_into_cnt_reg or pciw_fifo_control_in or pciw_fifo_empty_in)
1160
begin
1161
    pciw_fifo_renable_out =    pciw_fifo_renable || addr_into_cnt_reg ;
1162
    last_data_from_pciw_fifo_reg <= #`FF_DELAY pciw_fifo_control_in[`ADDR_CTRL_BIT] || pciw_fifo_empty_in ;
1163
end
1164 77 mihad
 
1165
 
1166
endmodule
1167
 

powered by: WebSVN 2.1.0

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