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

Subversion Repositories pci

[/] [pci/] [tags/] [rel_3/] [rtl/] [verilog/] [wb_master.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 2 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 6 mihad
// Revision 1.1.1.1  2001/10/02 15:33:47  mihad
46
// New project directory structure
47 2 mihad
//
48 6 mihad
//
49 2 mihad
 
50
`define FSM_BITS 3 // number of bits needed for FSM states
51
 
52
 
53
`include "bus_commands.v"
54
`include "constants.v"
55 6 mihad
`include "timescale.v"
56 2 mihad
 
57
module WB_MASTER (  wb_clock_in,                // CLK_I
58
                    reset_in,                   // RST_I
59
 
60
                    pci_tar_read_request,
61
                                        pci_tar_address,
62
                                        pci_tar_cmd,
63
                                        pci_tar_be,
64
                                        pci_tar_prefetch_en,
65
                                        pci_cache_line_size,
66
                                        wb_read_done,
67
 
68
                                        pcir_fifo_wenable_out,
69
                                        pcir_fifo_data_out,
70
                                        pcir_fifo_be_out,
71
                                        pcir_fifo_control_out,
72
                                        //pcir_fifo_renable_out,                        for PCI Target !!!
73
                                        //pcir_fifo_data_in,                            for PCI Target !!!
74
                                        //pcir_fifo_be_in,                                      for PCI Target !!!
75
                                        //pcir_fifo_control_in,                         for PCI Target !!!
76
                                        //pcir_fifo_flush_out,                          for PCI Target !!!
77
                                        pcir_fifo_almost_full_in,
78
                                        pcir_fifo_full_in,
79
                                        //pcir_fifo_almost_empty_in,            for PCI Target !!!
80
                                        //pcir_fifo_empty_in,                           NOT used
81
                                        //pcir_fifo_transaction_ready_in,       NOT used
82
                                        //pciw_fifo_wenable_out,                        for PCI Target !!!
83
                                        //pciw_fifo_addr_data_out,                      for PCI Target !!!   
84
                                        //pciw_fifo_cbe_out,                            for PCI Target !!!
85
                                        //pciw_fifo_control_out,                        for PCI Target !!!       
86
                                        pciw_fifo_renable_out,
87
                                        pciw_fifo_addr_data_in,
88
                                        pciw_fifo_cbe_in,
89
                                        pciw_fifo_control_in,
90
                                        //pciw_fifo_flush_out,                          NOT used
91
                                        //pciw_fifo_almost_full_in,             for PCI Target !!!
92
                                        //pciw_fifo_full_in,                            for PCI Target !!!
93
                                        pciw_fifo_almost_empty_in,
94
                                        pciw_fifo_empty_in,
95
                                        pciw_fifo_transaction_ready_in,
96
 
97
                                        pci_error_sig_set_in,
98
                                        pci_error_sig_out,
99
                                        pci_error_bc,
100
                                        write_rty_cnt_exp_out,
101
                                        read_rty_cnt_exp_out,
102
 
103
                    CYC_O,
104
                    STB_O,
105
                    WE_O,
106
                    SEL_O,
107
                    ADR_O,
108
                    MDATA_I,
109
                    MDATA_O,
110
                    ACK_I,
111
                    RTY_I,
112
                    ERR_I,
113
                    CAB_O
114
                );
115
 
116
/*----------------------------------------------------------------------------------------------------------------------
117
Various parameters needed for state machine and other stuff
118
----------------------------------------------------------------------------------------------------------------------*/
119
parameter               S_IDLE                  = `FSM_BITS'h0 ;
120
parameter               S_WRITE                 = `FSM_BITS'h1 ;
121
parameter               S_WRITE_ERR_RTY = `FSM_BITS'h2 ;
122
parameter               S_READ                  = `FSM_BITS'h3 ;
123
parameter               S_READ_RTY              = `FSM_BITS'h4 ;
124
parameter               S_TURN_ARROUND  = `FSM_BITS'h5 ;
125
 
126
/*----------------------------------------------------------------------------------------------------------------------
127
System signals inputs
128
wb_clock_in - WISHBONE bus clock input
129
reset_in    - system reset input controlled by bridge's reset logic
130
----------------------------------------------------------------------------------------------------------------------*/
131
input                   wb_clock_in ;
132
input                   reset_in ;
133
 
134
/*----------------------------------------------------------------------------------------------------------------------
135
Control signals from PCI Target for READS to PCIR_FIFO
136
---------------------------------------------------------------------------------------------------------------------*/
137
input                   pci_tar_read_request ;          // read request from PCI Target
138
input   [31:0]   pci_tar_address ;                       // address for requested read from PCI Target                                   
139
input   [3:0]    pci_tar_cmd ;                           // command for requested read from PCI Target                                   
140
input   [3:0]    pci_tar_be ;                            // byte enables for requested read from PCI Target              
141
input                   pci_tar_prefetch_en ;           // pre-fetch enable for requested read from PCI Target          
142
input   [7:0]    pci_cache_line_size ;           // CACHE line size register value for burst length              
143
output                  wb_read_done ;                          // read done and PCIR_FIFO has data ready
144
 
145
reg                             wb_read_done ;
146
 
147
/*----------------------------------------------------------------------------------------------------------------------
148
PCIR_FIFO control signals used for sinking data into PCIR_FIFO and status monitoring
149
---------------------------------------------------------------------------------------------------------------------*/
150
output                  pcir_fifo_wenable_out ;         // PCIR_FIFO write enable output
151
output  [31:0]   pcir_fifo_data_out ;            // data output to PCIR_FIFO
152
output  [3:0]    pcir_fifo_be_out ;                      // byte enable output to PCIR_FIFO
153
output  [3:0]    pcir_fifo_control_out ;         // control bus output to PCIR_FIFO
154
input                   pcir_fifo_almost_full_in ;      // almost full status indicator from PCIR_FIFO
155
input                   pcir_fifo_full_in ;                     // full status indicator from PCIR_FIFO
156
 
157
reg                             pcir_fifo_wenable_out ;
158
reg             [3:0]    pcir_fifo_control_out ;
159
 
160
/*----------------------------------------------------------------------------------------------------------------------
161
PCIW_FIFO control signals used for fetching data from PCIW_FIFO and status monitoring
162
---------------------------------------------------------------------------------------------------------------------*/
163
output                  pciw_fifo_renable_out ;         // read enable for PCIW_FIFO output
164
input   [31:0]   pciw_fifo_addr_data_in ;        // address and data input from PCIW_FIFO
165
input   [3:0]    pciw_fifo_cbe_in ;                      // command and byte_enables from PCIW_FIFO
166
input   [3:0]    pciw_fifo_control_in ;          // control bus input from PCIW_FIFO
167
input                   pciw_fifo_almost_empty_in ;     // almost empty status indicator from PCIW_FIFO
168
input                   pciw_fifo_empty_in ;            // empty status indicator from PCIW_FIFO
169
input                   pciw_fifo_transaction_ready_in ;        // write transaction is ready in PCIW_FIFO
170
 
171
reg                             pciw_fifo_renable_out ;
172
 
173
/*----------------------------------------------------------------------------------------------------------------------
174
Control INPUT / OUTPUT signals for configuration space reporting registers !!!
175
---------------------------------------------------------------------------------------------------------------------*/
176
input                   pci_error_sig_set_in ;          // When error signaled bit in PCI_ERR_CS register is set
177
output                  pci_error_sig_out ;                     // When error on WB bus occures
178
output  [3:0]   pci_error_bc ;                           // bus command at which error occured !
179
output                  write_rty_cnt_exp_out ;         // Signaling that RETRY counter has expired during write transaction!
180
output                  read_rty_cnt_exp_out ;          // Signaling that RETRY counter has expired during read transaction!
181
 
182
reg                             pci_error_sig_out ;
183
reg                             write_rty_cnt_exp_out ;
184
reg                             read_rty_cnt_exp_out ;
185
 
186
/*----------------------------------------------------------------------------------------------------------------------
187
WISHBONE bus interface signals - can be connected directly to WISHBONE bus
188
---------------------------------------------------------------------------------------------------------------------*/
189
output          CYC_O ;                 // cycle indicator output
190
output          STB_O ;                 // strobe output - data is valid when strobe and cycle indicator are high
191
output          WE_O  ;                 // write enable output - 1 - write operation, 0 - read operation
192
output  [3:0]   SEL_O ;                  // Byte select outputs
193
output  [31:0]  ADR_O ;                  // WISHBONE address output
194
input   [31:0]  MDATA_I ;                // WISHBONE slave interface input data bus
195
output  [31:0]  MDATA_O ;                // WISHBONE slave interface output data bus
196
input           ACK_I ;                 // Acknowledge input - qualifies valid data on data output bus or received data on data input bus
197
input           RTY_I ;                 // retry input - signals from WISHBONE slave that cycle should be terminated and retried later
198
input           ERR_I ;                 // Signals from WISHBONE slave that access resulted in an error
199
output          CAB_O ;                 // consecutive address burst output - indicated that master will do a serial address transfer in current cycle
200
 
201
reg             [3:0]   SEL_O ;
202
 
203
 
204
/*###########################################################################################################
205
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
206
        LOGIC, COUNTERS, STATE MACHINE and some control register bits
207
        =============================================================
208
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
209
###########################################################################################################*/
210
 
211
// wire for write attempt - 1 when PCI Target attempt to write and PCIW_FIFO has a write transaction ready
212
wire w_attempt = ( pciw_fifo_transaction_ready_in && ~pciw_fifo_empty_in ) ;
213
 
214
// wire for read attempt - 1 when PCI Target is attempting a read and PCIR_FIFO is not full !
215
// because of transaction ordering, PCI Master must not start read untill all writes are done -> at that
216
//   moment PCIW_FIFO is empty !!! (when read is pending PCI Target will block new reads and writes)
217
wire r_attempt = ( pci_tar_read_request && ~pcir_fifo_full_in && pciw_fifo_empty_in ) ;
218
 
219
// Signal is used for reads on WB, when there is retry!
220
reg                             first_wb_data_access ;
221
 
222
reg                             last_data_from_pciw_fifo ;      // signal tells when there is last data in pciw_fifo
223
reg                             last_data_to_pcir_fifo ;        // signal tells when there will be last data for pcir_fifo
224
 
225
// Logic used in State Machine logic implemented out of State Machine because of less delay!
226
always@(pciw_fifo_control_in or pciw_fifo_almost_empty_in)
227
begin
228
        //      (pciw_fifo_control_in == `LAST) is the same as (pciw_fifo_control_in[0] == 1'b1)
229
        if (pciw_fifo_control_in[0] || pciw_fifo_almost_empty_in) // if last data is going to be transfered
230
                last_data_from_pciw_fifo <= 1'b1 ; // signal for last data from PCIW_FIFO
231
        else
232
                last_data_from_pciw_fifo <= 1'b0 ;
233
end
234
 
235
wire    [7:0]    cache_line_wire ; // wire assigned directly from cache_line register !!!
236
// Logic used in State Machine logic implemented out of State Machine because of less delay!
237
//   definition of signal telling, when there is last data written into FIFO
238
always@(pci_tar_cmd or pci_tar_prefetch_en or cache_line_wire or pcir_fifo_almost_full_in)
239
begin
240
        case ({pci_tar_cmd, pci_tar_prefetch_en})
241
        {`BC_IO_READ, 1'b0},
242
        {`BC_IO_READ, 1'b1},
243
        {`BC_MEM_READ, 1'b0} :
244
        begin   // when single cycle
245
                last_data_to_pcir_fifo <= 1'b1 ;
246
        end
247
        {`BC_MEM_READ, 1'b1},
248
        {`BC_MEM_READ_LN, 1'b0},
249
        {`BC_MEM_READ_LN, 1'b1},
250
        {`BC_MEM_READ_MUL, 1'b0} :
251
        begin   // when burst cycle
252
                if ((cache_line_wire == 8'h1) || pcir_fifo_almost_full_in)
253
                        last_data_to_pcir_fifo <= 1'b1 ;
254
                else
255
                        last_data_to_pcir_fifo <= 1'b0 ;
256
        end
257
        {`BC_MEM_READ_MUL, 1'b1} :
258
        begin   // when burst cycle
259
                if (pcir_fifo_almost_full_in)
260
                        last_data_to_pcir_fifo <= 1'b1 ;
261
                else
262
                        last_data_to_pcir_fifo <= 1'b0 ;
263
        end
264
        default :
265
        begin
266
                last_data_to_pcir_fifo <= 1'b0 ;
267
        end
268
        endcase
269
end
270
 
271
reg             [3:0]    wb_no_response_cnt ;
272
reg             [3:0]    wb_response_value ;
273
reg                             wait_for_wb_response ;
274
reg                             set_retry ; // 
275
 
276
// internal WB no response retry generator counter!
277
always@(posedge reset_in or posedge wb_clock_in)
278
begin
279
        if (reset_in)
280
                wb_no_response_cnt <= #`FF_DELAY 4'h0 ;
281
        else
282
                wb_no_response_cnt <= #`FF_DELAY wb_response_value ;
283
end
284
// internal WB no response retry generator logic
285
always@(wait_for_wb_response or wb_no_response_cnt or ACK_I or ERR_I or RTY_I)
286
begin
287
        if (wb_no_response_cnt == 4'h8) // when there isn't response for 8 clocks, set internal retry
288
        begin
289
                wb_response_value <= 4'h0 ;
290
                set_retry <= 1'b1 ;
291
        end
292
        else
293
        begin
294
                if (ACK_I || ERR_I || RTY_I) // if device responded, then ZERO must be written into counter
295
                        wb_response_value <= 4'h0 ;
296
                else
297
                if (wait_for_wb_response)
298
                        wb_response_value <= wb_no_response_cnt + 1 ; // count clocks when no response
299
                else
300
                        wb_response_value <= wb_no_response_cnt ;
301
                set_retry <= 1'b0 ;
302
        end
303
end
304
 
305
wire    retry = RTY_I || set_retry ; // retry signal - logic OR function between RTY_I and internal WB no response retry!
306
reg             [7:0]    rty_counter ; // output from retry counter
307
reg             [7:0]    rty_counter_in ; // input value - output value + 1 OR output value
308
reg                             rty_counter_max_value ; // signal tells when retry counter riches maximum value!
309
reg                             reset_rty_cnt ; // signal for asynchronous reset of retry counter after each complete transfere
310
reg                             last_data_transferred ; // signal is set by STATE MACHINE after each complete transfere !
311
 
312
// sinchronous signal after each transfere and asynchronous signal 'reset_rty_cnt' after reset  
313
//   for reseting the retry counter
314
always@(posedge reset_in or posedge wb_clock_in)
315
begin
316
        if (reset_in)
317
                reset_rty_cnt <= 1'b1 ; // asynchronous set when reset signal is active
318
        else
319
                reset_rty_cnt <= ACK_I || ERR_I ; // synchronous set after completed transfere
320
end
321
 
322
// clock enable for retry counter - when there is sinchronous reset OR retry
323
wire                    rty_cnt_clk_en = reset_rty_cnt || retry ;
324
// data written into retry counter - ZEROS when sinchronous reset OR incremented value when retry
325
wire    [7:0]    rty_cnt_data = reset_rty_cnt ? 8'h00 : rty_counter_in ;
326
// Retry counter register control
327
always@(posedge reset_in or posedge wb_clock_in)
328
begin
329
        if (reset_in)
330
                rty_counter <= #`FF_DELAY 8'h00 ;
331
        else
332
        begin
333
                if (rty_cnt_clk_en)
334
                        rty_counter <= #`FF_DELAY rty_cnt_data ; // count-up or hold value depending on retry counter logic
335
        end
336
end
337
// Retry counter logic
338
always@(rty_counter or rty_counter_in)
339
begin
340
        if(rty_counter == `WB_RTY_CNT_MAX) // stop counting
341
        begin
342
        rty_counter_in <= rty_counter ;
343
        rty_counter_max_value <= 1'b1 ;
344
        end
345
        else
346
        begin
347
        rty_counter_in <= rty_counter_in + 1'b1 ; // count up
348
        rty_counter_max_value <= 1'b0 ;
349
        end
350
end
351
 
352
reg             [7:0]    cache_line ; // output from cache line down-counter
353
reg             [7:0]    cache_line_in ; // input to cache line counter
354
reg                             cache_line_into_cnt ; // control signal for loading cache line size to counter
355
reg                             cache_line_count ; // control signal for count enable
356
reg                             cache_line_reg_used ; // if pci_cache_line_size is ZERO then all PCIR_FIFO is read
357
 
358
assign  cache_line_wire = cache_line ;
359
// cache line size down-counter register control
360
always@(posedge wb_clock_in or posedge reset_in)
361
begin
362
        if (reset_in) // reset counter
363
                cache_line <= #`FF_DELAY 8'h00 ;
364
        else
365
                cache_line <= #`FF_DELAY cache_line_in ; // count down or hold value depending on cache line counter logic
366
end
367
// cache line size down-counter logic
368
always@(cache_line_into_cnt or cache_line_count or pci_cache_line_size or cache_line)
369
begin
370
        if (cache_line_into_cnt) // load cache line size into counter
371
        begin
372
                if (pci_cache_line_size == 8'h0)
373
                        cache_line_in = 8'h01 ;
374
                else
375
                        cache_line_in = pci_cache_line_size ;
376
        end
377
        else
378
        if (cache_line_count)
379
        begin
380
                cache_line_in = cache_line - 1'h1 ; // count down
381
        end
382
        else
383
        begin
384
                cache_line_in = cache_line ;
385
        end
386
end
387
 
388
 
389
reg             [31:0]   addr_cnt_out ;  // output value from address counter to WB ADDRESS output
390
reg             [31:0]   addr_cnt_in ;   // input address value to address counter
391
reg                             addr_into_cnt ; // control signal for loading starting address into counter
392
reg                             addr_count ; // control signal for count enable
393
reg             [3:0]    bc_register ; // used when error occures during writes!
394
 
395
// wb address counter register control
396
always@(posedge wb_clock_in or posedge reset_in)
397
begin
398
        if (reset_in) // reset counter
399
        begin
400
                addr_cnt_out <= #`FF_DELAY 32'h0000_0000 ;
401
                bc_register  <= #`FF_DELAY 4'h0 ;
402
        end
403
        else
404
        begin
405
                addr_cnt_out <= #`FF_DELAY addr_cnt_in ; // count up or hold value depending on cache line counter logic
406
                if (addr_into_cnt)
407
                        bc_register  <= #`FF_DELAY pciw_fifo_cbe_in ;
408
        end
409
end
410
// wb address counter logic
411
always@(addr_into_cnt or r_attempt or addr_count or pciw_fifo_addr_data_in or pci_tar_address or addr_cnt_out)
412
begin
413
        if (addr_into_cnt) // load starting address into counter
414
        begin
415
                if (r_attempt)
416
                begin
417
                        addr_cnt_in = pci_tar_address ; // if read request, then load read addresss from PCI Target
418
                end
419
                else
420
                begin
421
                        addr_cnt_in = pciw_fifo_addr_data_in ; // if not read request, then load write address from PCIW_FIFO
422
                end
423
        end
424
        else
425
        if (addr_count)
426
        begin
427
                addr_cnt_in = addr_cnt_out + 3'h4 ; // count up for 32-bit alligned address 
428
        end
429
        else
430
        begin
431
                addr_cnt_in = addr_cnt_out ;
432
        end
433
end
434
 
435
reg wb_stb_o ; // Internal signal for driwing STB_O on WB bus
436
reg wb_we_o ; // Internal signal for driwing WE_O on WB bus
437
reg wb_cyc_o ; // Internal signal for driwing CYC_O on WB bus and for enableing burst signal generation
438
 
439
reg     retried ; // Signal is output value from FF and is set for one clock period after retried_d is set
440
reg     retried_d ; // Signal is set whenever cycle is retried and is input to FF for delaying -> used in S_IDLE state
441
 
442
reg     first_data_is_burst ; // Signal is set in S_WRITE or S_READ states, when data transfere is burst!
443
reg     burst_transfer ; // This signal is set when data transfere is burst and is reset with RESET or last data transfered
444
 
445
// FFs output signals tell, when there is first data out from FIFO (for BURST checking)
446
//   and for delaying retried signal
447
always@(posedge wb_clock_in or posedge reset_in)
448
begin
449
        if (reset_in) // reset signals
450
        begin
451
                retried <= #`FF_DELAY 1'b0 ;
452
        end
453
        else
454
        begin
455
                retried <= #`FF_DELAY retried_d ; // delaying retried signal  
456
        end
457
end
458
 
459
// Determinig if first data is a part of BURST or just a single transfere!
460
always@(addr_into_cnt or r_attempt or pci_tar_cmd or pci_tar_prefetch_en or pciw_fifo_control_in)
461
begin
462
        if (addr_into_cnt)
463
        begin
464
                if (r_attempt)
465
                begin
466
                        if (((pci_tar_cmd == `BC_MEM_READ) && pci_tar_prefetch_en) ||
467
                                (pci_tar_cmd == `BC_MEM_READ_LN) ||
468
                                (pci_tar_cmd == `BC_MEM_READ_MUL))
469
                                first_data_is_burst <= 1'b1 ;
470
                        else
471
                                first_data_is_burst <= 1'b0 ;
472
                end
473
                else
474
                begin
475
                        // When first data is not LAST, then there will be burst transfere
476
                        if (pciw_fifo_control_in[`BURST_BIT]) // (pciw_fifo_control_in != `LAST)
477
                                first_data_is_burst <= 1'b1 ;
478
                        else
479
                                first_data_is_burst <= 1'b0 ;
480
                end
481
        end
482
        else
483
                first_data_is_burst <= 1'b0 ;
484
end
485
 
486
// clock enable for burst_transfere signal when last data is transfered OR if first data is burst
487
wire burst_transfer_clk_en = last_data_transferred || first_data_is_burst ;
488
// data written into burst_transfere register is ZERO when last data is transfered OR ONE when first data is burst
489
wire burst_transfer_data = last_data_transferred ? 1'b0 : 1'b1 ;
490
// FF for seting and reseting burst_transfere signal
491
always@(posedge wb_clock_in or posedge reset_in)
492
begin
493
        if (reset_in)
494
                burst_transfer <= #`FF_DELAY 1'b0 ;
495
        else
496
        begin
497
                if (burst_transfer_clk_en)
498
                        burst_transfer <= #`FF_DELAY burst_transfer_data ;
499
        end
500
end
501
 
502
reg [(`FSM_BITS - 1):0]  c_state ; //current state register
503
reg [(`FSM_BITS - 1):0]  n_state ; //next state input to current state register
504
 
505
// state machine register control
506
always@(posedge wb_clock_in or posedge reset_in)
507
begin
508
    if (reset_in) // reset state machine ti S_IDLE state
509
        c_state <= #`FF_DELAY S_IDLE ;
510
    else
511
        c_state <= #`FF_DELAY n_state ;
512
end
513
 
514
// state machine logic
515
always@(c_state or
516
                ACK_I or
517
                RTY_I or
518
                ERR_I or
519
                w_attempt or
520
                r_attempt or
521
                pci_error_sig_set_in or
522
                retried or
523
                pciw_fifo_control_in or
524
                pciw_fifo_almost_empty_in or
525
                pci_tar_cmd or
526
                pci_tar_prefetch_en or
527
                cache_line or
528
                rty_counter or
529
                pci_tar_read_request or
530
                pcir_fifo_almost_full_in or
531
                rty_counter_max_value or
532
                last_data_to_pcir_fifo or
533
                first_wb_data_access or
534
                last_data_from_pciw_fifo
535
                )
536
begin
537
    case (c_state)
538
    S_IDLE:
539
        begin
540
                // Default values for signals not used in this state
541
                pcir_fifo_wenable_out <= 1'b0 ;
542
                pcir_fifo_control_out <= 4'h0 ;
543
                addr_count <= 1'b0 ;
544
                cache_line_count <= 1'b0 ;
545
                pci_error_sig_out <= 1'b0 ;
546
                retried_d <= 1'b0 ;
547
                last_data_transferred <= 1'b0 ;
548
                wb_read_done <= 1'b0 ;
549
                        write_rty_cnt_exp_out <= 1'b0 ;
550
                        read_rty_cnt_exp_out <= 1'b0 ;
551
                        wait_for_wb_response <= 1'b0 ;
552
                        // If ERROR SIGNALED bit in pci_error_cs register is set, then state machine must be frozen
553
                        case (pci_error_sig_set_in)
554
                        1'b1 :
555
                begin
556
                        pciw_fifo_renable_out <= 1'b0 ;
557
                        addr_into_cnt <= 1'b0 ;
558
                        cache_line_into_cnt <= 1'b0 ;
559
                        n_state <= S_IDLE ;
560
                end
561
                default :
562
                begin
563
                        case ({w_attempt, r_attempt, retried})
564
                        3'b101 : // Write request for PCIW_FIFO to WB bus transaction
565
                        begin    // If there was retry, the same transaction must be initiated
566
                                pciw_fifo_renable_out <= 1'b0 ; // the same data
567
                                addr_into_cnt <= 1'b0 ; // the same address
568
                                cache_line_into_cnt <= 1'b0 ; // no need for cache line when there is write
569
                                n_state <= S_WRITE ;
570
                        end
571
                        3'b100 : // Write request for PCIW_FIFO to WB bus transaction
572
                                begin    // If there is new transaction
573
                                pciw_fifo_renable_out <= 1'b1 ; // first location is address (in FIFO), next will be data
574
                                addr_into_cnt <= 1'b1 ; // address must be latched into address counter
575
                                cache_line_into_cnt <= 1'b0 ; // no need for cache line when there is write
576
                                n_state <= S_WRITE ;
577
                        end
578
                        3'b011 : // Read request from PCI Target for WB bus to PCIR_FIFO transaction
579
                                begin    // If there was retry, the same transaction must be initiated
580
                        addr_into_cnt <= 1'b0 ; // the same address
581
                        cache_line_into_cnt <= 1'b0 ; // cache line counter must not be changed for retried read
582
                                        pciw_fifo_renable_out <= 1'b0 ; // don't read from FIFO, when read transaction from WB to FIFO
583
                                n_state <= S_READ ;
584
                                end
585
                        3'b010 : // Read request from PCI Target for WB bus to PCIR_FIFO transaction
586
                                begin    // If there is new transaction
587
                        addr_into_cnt <= 1'b1 ; // address must be latched into counter from separate request bus
588
                        cache_line_into_cnt <= 1'b1 ; // cache line size must be latched into its counter
589
                                        pciw_fifo_renable_out <= 1'b0 ; // don't read from FIFO, when read transaction from WB to FIFO
590
                                n_state <= S_READ ;
591
                    end
592
                        default : // stay in IDLE state
593
                                begin
594
                                pciw_fifo_renable_out <= 1'b0 ;
595
                                addr_into_cnt <= 1'b0 ;
596
                                cache_line_into_cnt <= 1'b0 ;
597
                                n_state <= S_IDLE ;
598
                                end
599
                        endcase
600
                        end
601
                        endcase
602
                        wb_stb_o <= 1'b0 ;
603
                        wb_we_o <= 1'b0 ;
604
                        wb_cyc_o <= 1'b0 ;
605
                end
606
        S_WRITE: // WRITE from PCIW_FIFO to WB bus
607
                begin
608
                // Default values for signals not used in this state
609
                pcir_fifo_wenable_out <= 1'b0 ;
610
                pcir_fifo_control_out <= 4'h0 ;
611
                addr_into_cnt <= 1'b0 ;
612
                cache_line_into_cnt <= 1'b0 ;
613
                cache_line_count <= 1'b0 ;
614
                wb_read_done <= 1'b0 ;
615
                        read_rty_cnt_exp_out <= 1'b0 ;
616
                        case ({ACK_I, ERR_I, RTY_I})
617
                        3'b100 : // If writting of one data is acknowledged
618
                begin
619
                        pciw_fifo_renable_out <= 1'b1 ; // prepare next value (address when new trans., data when burst tran.)
620
                        addr_count <= 1'b1 ; // prepare next address if there will be burst
621
                                pci_error_sig_out <= 1'b0 ; // there was no error
622
                                retried_d <= 1'b0 ; // there was no retry
623
                                write_rty_cnt_exp_out <= 1'b0 ; // there was no retry
624
                                wait_for_wb_response <= 1'b0 ;
625
                        if (last_data_from_pciw_fifo) // if last data was transfered
626
                                begin
627
                                n_state <= S_IDLE ;
628
                                last_data_transferred <= 1'b1 ; // signal for last data transfered
629
                                end
630
                        else
631
                                begin
632
                                n_state <= S_WRITE ;
633
                                last_data_transferred <= 1'b0 ;
634
                                end
635
                end
636
                        3'b010 : // If writting of one data is terminated with ERROR
637
                begin
638
                        pciw_fifo_renable_out <= 1'b1 ; // prepare next value (address when new trans., data when cleaning FIFO)
639
                        addr_count <= 1'b0 ; // no need for new address
640
                        retried_d <= 1'b0 ; // there was no retry
641
                        last_data_transferred <= 1'b1 ; // signal for last data transfered
642
                        pci_error_sig_out <= 1'b1 ; // segnal for error reporting
643
                                write_rty_cnt_exp_out <= 1'b0 ; // there was no retry
644
                                wait_for_wb_response <= 1'b0 ;
645
                                if (last_data_from_pciw_fifo) // if last data was transfered
646
                                        n_state <= S_IDLE ; // go to S_IDLE for new transfere
647
                                else // if there wasn't last data of transfere
648
                                        n_state <= S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
649
                end
650
                        3'b001 : // If writting of one data is retried
651
                        begin
652
                                pciw_fifo_renable_out <= 1'b0 ;
653
                                addr_count <= 1'b0 ;
654
                                pci_error_sig_out <= 1'b0 ;
655
                                last_data_transferred <= 1'b0 ;
656
                                retried_d <= 1'b1 ; // there was a retry
657
                                wait_for_wb_response <= 1'b0 ;
658
                                if(rty_counter_max_value) // If retry counter reached maximum allowed value
659
                                begin
660
                                        n_state <= S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
661
                                        write_rty_cnt_exp_out <= 1'b1 ; // signal for reporting write counter expired
662
                                end
663
                                else
664
                                begin
665
                                        n_state <= S_IDLE ; // go to S_IDLE state for retrying the transaction
666
                                        write_rty_cnt_exp_out <= 1'b0 ; // retry counter hasn't expired yet
667
                                end
668
                        end
669
                        default :
670
                        begin
671
                                pciw_fifo_renable_out <= 1'b0 ;
672
                                addr_count <= 1'b0 ;
673
                                last_data_transferred <= 1'b0 ;
674
                                retried_d <= 1'b0 ;
675
                                wait_for_wb_response <= 1'b1 ; // wait for WB device to response (after 8 clocks RTY CNT is incremented)
676
                                if(rty_counter_max_value) // when no WB response and RTY CNT reached maximum allowed value 
677
                                begin
678
                                        n_state <= S_WRITE_ERR_RTY ; // go here to clean this write transaction from PCIW_FIFO
679
                                        write_rty_cnt_exp_out <= 1'b1 ; // signal for reporting write counter expired
680
                                pci_error_sig_out <= 1'b1 ; // signal for error reporting
681
                                end
682
                                else
683
                                begin
684
                                        n_state <= S_WRITE ; // stay in S_WRITE state to wait WB to response
685
                                        write_rty_cnt_exp_out <= 1'b0 ; // retry counter hasn't expired yet
686
                                        pci_error_sig_out <= 1'b0 ;
687
                                end
688
                        end
689
                        endcase
690
                        wb_stb_o <= 1'b1 ;
691
                        wb_we_o <= 1'b1 ;
692
                        wb_cyc_o <= 1'b1 ;
693
                end
694
        S_WRITE_ERR_RTY: // Clean current write transaction from PCIW_FIFO if ERROR or Retry counter expired occures
695
                begin
696
                        pciw_fifo_renable_out <= 1'b1 ; // put out next data (untill last data or FIFO empty)
697
                        last_data_transferred <= 1'b1 ; // after exiting this state, negedge of this signal is used
698
                        // Default values for signals not used in this state
699
                pcir_fifo_wenable_out <= 1'b0 ;
700
                pcir_fifo_control_out <= 4'h0 ;
701
                addr_into_cnt <= 1'b0 ;
702
                cache_line_into_cnt <= 1'b0 ;
703
                cache_line_count <= 1'b0 ;
704
                addr_count <= 1'b0 ;
705
                        pci_error_sig_out <= 1'b0 ;
706
                        retried_d <= 1'b0 ;
707
                        wb_read_done <= 1'b0 ;
708
                        write_rty_cnt_exp_out <= 1'b0 ;
709
                        read_rty_cnt_exp_out <= 1'b0 ;
710
                        wait_for_wb_response <= 1'b0 ;
711
                        // If last data is cleaned out from PCIW_FIFO
712
                        if (last_data_from_pciw_fifo)
713
                                n_state <= S_IDLE ;
714
                        else
715
                                n_state <= S_WRITE_ERR_RTY ; // Clean until last data is cleaned out from FIFO
716
                        wb_stb_o <= 1'b0 ;
717
                        wb_we_o <= 1'b0 ;
718
                        wb_cyc_o <= 1'b0 ;
719
                end
720
        S_READ: // READ from WB bus to PCIR_FIFO
721
                begin
722
                        // Default values for signals not used in this state
723
                pciw_fifo_renable_out <= 1'b0 ;
724
                addr_into_cnt <= 1'b0 ;
725
                cache_line_into_cnt <= 1'b0 ;
726
                pci_error_sig_out <= 1'b0 ;
727
                        write_rty_cnt_exp_out <= 1'b0 ;
728
                        case ({ACK_I, ERR_I, RTY_I})
729
                        3'b100 : // If reading of one data is acknowledged
730
                begin
731
                        pcir_fifo_wenable_out <= 1'b1 ; // enable writting data into PCIR_FIFO
732
                        addr_count <= 1'b1 ; // prepare next address if there will be burst
733
                                cache_line_count <= 1'b1 ; // decrease counter value for cache line size
734
                        retried_d <= 1'b0 ; // there was no retry
735
                                read_rty_cnt_exp_out <= 1'b0 ; // there was no retry
736
                                wait_for_wb_response <= 1'b0 ;
737
                                // if last data was transfered
738
                        if (last_data_to_pcir_fifo)
739
                        begin
740
                                pcir_fifo_control_out <= `LAST ; // control code in FIFO must indicate LAST data transfered
741
                                last_data_transferred <= 1'b1 ; // signal for last data transfered
742
                                wb_read_done <= 1'b1 ; // signal last data of read transaction for PCI Target
743
                                n_state <= S_TURN_ARROUND ;
744
                        end
745
                        else // if not last data transfered
746
                        begin
747
                                        pcir_fifo_control_out <= 4'h0 ; // ZERO for control code
748
                                        last_data_transferred <= 1'b0 ; // not last data transfered
749
                                        wb_read_done <= 1'b0 ; // read is not done yet
750
                                        n_state <= S_READ ;
751
                                end
752
                end
753
                3'b010 : // If reading of one data is terminated with ERROR
754
                begin
755
                        pcir_fifo_wenable_out <= 1'b1 ; // enable for writting to FIFO data with ERROR
756
                        addr_count <= 1'b0 ; // no need for new address
757
                                pcir_fifo_control_out <= `DATA_ERROR ; // control code in FIFO must indicate the DATA with ERROR
758
                                last_data_transferred <= 1'b1 ; // signal for last data transfered
759
                                wb_read_done <= 1'b1 ; // signal last data of read transaction for PCI Target
760
                                cache_line_count <= 1'b0 ; // no need for cache line, when error occures
761
                                n_state <= S_TURN_ARROUND ;
762
                                retried_d <= 1'b0 ; // there was no retry
763
                                wait_for_wb_response <= 1'b0 ;
764
                                read_rty_cnt_exp_out <= 1'b0 ; // there was no retry
765
                end
766
                        3'b001 : // If reading of one data is retried
767
                begin
768
                                pcir_fifo_wenable_out <= 1'b0 ;
769
                                pcir_fifo_control_out <= 4'h0 ;
770
                                addr_count <= 1'b0 ;
771
                                cache_line_count <= 1'b0 ;
772
                                wait_for_wb_response <= 1'b0 ;
773
                                case ({first_wb_data_access, rty_counter_max_value})
774
                                2'b10 :
775
                                begin
776
                                        n_state <= S_IDLE ; // go to S_IDLE state for retrying the transaction
777
                                        read_rty_cnt_exp_out <= 1'b0 ; // retry counter hasn't expired yet   
778
                                        last_data_transferred <= 1'b0 ;
779
                                        wb_read_done <= 1'b0 ;
780
                                        retried_d <= 1'b1 ; // there was a retry
781
                                end
782
                                2'b11 :
783
                                begin
784
                                        n_state <= S_READ_RTY ; // go here to wait for PCI Target to remove read request
785
                                        read_rty_cnt_exp_out <= 1'b1 ; // signal for reporting read counter expired  
786
                                        last_data_transferred <= 1'b0 ;
787
                                        wb_read_done <= 1'b0 ;
788
                                        retried_d <= 1'b1 ; // there was a retry
789
                                end
790
                                default :
791
                                begin
792
                                        n_state <= S_TURN_ARROUND ; // go to S_TURN_ARROUND state 
793
                                        read_rty_cnt_exp_out <= 1'b0 ; // retry counter hasn't expired  
794
                                        last_data_transferred <= 1'b1 ;
795
                                        wb_read_done <= 1'b1 ;
796
                                        retried_d <= 1'b0 ; // retry must not be retried, since there is not a first data
797
                                end
798
                                endcase
799
                        end
800
                default :
801
                begin
802
                                addr_count <= 1'b0 ;
803
                                cache_line_count <= 1'b0 ;
804
                                read_rty_cnt_exp_out <= 1'b0 ;
805
                                retried_d <= 1'b0 ;
806
                                wait_for_wb_response <= 1'b1 ; // wait for WB device to response (after 8 clocks RTY CNT is incremented)
807
                                if(rty_counter_max_value) // when no WB response and RTY CNT reached maximum allowed value 
808
                                begin
809
                                        n_state <= S_TURN_ARROUND ; // go here to stop read request
810
                                        pcir_fifo_wenable_out <= 1'b1 ;
811
                                        pcir_fifo_control_out <= `DATA_ERROR ;
812
                                        last_data_transferred <= 1'b1 ;
813
                                        wb_read_done <= 1'b1 ;
814
                                end
815
                                else
816
                                begin
817
                                        n_state <= S_READ ; // stay in S_READ state to wait WB to response
818
                                        pcir_fifo_wenable_out <= 1'b0 ;
819
                                        pcir_fifo_control_out <= 4'h0 ;
820
                                        last_data_transferred <= 1'b0 ;
821
                                        wb_read_done <= 1'b0 ;
822
                                end
823
                        end
824
                        endcase
825
                        wb_stb_o <= 1'b1 ;
826
                        wb_we_o <= 1'b0 ;
827
                        wb_cyc_o <= 1'b1 ;
828
                end
829
        S_READ_RTY: // Wait for PCI Target to remove read request, when retry counter reaches maximum value!
830
                begin
831
                        // Default values for signals not used in this state
832
                        pciw_fifo_renable_out <= 1'b0 ;
833
                pcir_fifo_wenable_out <= 1'b0 ;
834
                pcir_fifo_control_out <= 4'h0 ;
835
                addr_into_cnt <= 1'b0 ;
836
                cache_line_into_cnt <= 1'b0 ;
837
                cache_line_count <= 1'b0 ;
838
                addr_count <= 1'b0 ;
839
                        pci_error_sig_out <= 1'b0 ;
840
                        retried_d <= 1'b0 ;
841
                        wb_read_done <= 1'b0 ;
842
                        write_rty_cnt_exp_out <= 1'b0 ;
843
                        read_rty_cnt_exp_out <= 1'b0 ;
844
                        wait_for_wb_response <= 1'b0 ;
845
                        // wait for PCI Target to remove read request
846
                        if (pci_tar_read_request)
847
                        begin
848
                                n_state <= S_READ_RTY ; // stay in this state until read request is removed
849
                                last_data_transferred <= 1'b0 ;
850
                        end
851
                        else // when read request is removed
852
                        begin
853
                                n_state <= S_IDLE ;
854
                                last_data_transferred <= 1'b1 ; // when read request is removed, there is "last" data
855
                        end
856
                        wb_stb_o <= 1'b0 ;
857
                        wb_we_o <= 1'b0 ;
858
                        wb_cyc_o <= 1'b0 ;
859
                end
860
        S_TURN_ARROUND: // Turn arround cycle after writting to PCIR_FIFO (for correct data when reading from PCIW_FIFO) 
861
                begin
862
                        // Default values for signals not used in this state
863
                        pciw_fifo_renable_out <= 1'b0 ;
864
                pcir_fifo_wenable_out <= 1'b0 ;
865
                pcir_fifo_control_out <= 4'h0 ;
866
                addr_into_cnt <= 1'b0 ;
867
                cache_line_into_cnt <= 1'b0 ;
868
                cache_line_count <= 1'b0 ;
869
                addr_count <= 1'b0 ;
870
                        pci_error_sig_out <= 1'b0 ;
871
                        retried_d <= 1'b0 ;
872
                        last_data_transferred <= 1'b1 ;
873
                        wb_read_done <= 1'b0 ;
874
                        write_rty_cnt_exp_out <= 1'b0 ;
875
                        read_rty_cnt_exp_out <= 1'b0 ;
876
                        wait_for_wb_response <= 1'b0 ;
877
                        n_state <= S_IDLE ;
878
                        wb_stb_o <= 1'b0 ;
879
                        wb_we_o <= 1'b0 ;
880
                        wb_cyc_o <= 1'b0 ;
881
                end
882
        default :
883
                begin
884
                        // Default values for signals not used in this state
885
                        pciw_fifo_renable_out <= 1'b0 ;
886
                pcir_fifo_wenable_out <= 1'b0 ;
887
                pcir_fifo_control_out <= 4'h0 ;
888
                addr_into_cnt <= 1'b0 ;
889
                cache_line_into_cnt <= 1'b0 ;
890
                cache_line_count <= 1'b0 ;
891
                addr_count <= 1'b0 ;
892
                        pci_error_sig_out <= 1'b0 ;
893
                        retried_d <= 1'b0 ;
894
                        last_data_transferred <= 1'b0 ;
895
                        wb_read_done <= 1'b0 ;
896
                        write_rty_cnt_exp_out <= 1'b0 ;
897
                        read_rty_cnt_exp_out <= 1'b0 ;
898
                        wait_for_wb_response <= 1'b0 ;
899
                        n_state <= S_IDLE ;
900
                        wb_stb_o <= 1'b0 ;
901
                        wb_we_o <= 1'b0 ;
902
                        wb_cyc_o <= 1'b0 ;
903
                end
904
        endcase
905
end
906
 
907
// Signal for retry monitor in state machine when there is read and first (or single) data access
908
wire ack_rty_response = ACK_I || RTY_I ;
909
 
910
// clock enable for first_wb_data_access signal when NO WB cycle present OR there was acknowledge or retry termination
911
wire wb_access_clk_en = ~wb_cyc_o || ack_rty_response ;
912
// data written into first_wb_data_access signal register is ONE when no WB cycle present OR ZERO durin WB cycle (when
913
//   acknowledge or retry termination occures)
914
wire wb_access_data = ~wb_cyc_o ? 1'b1 : 1'b0 ;
915
// Signal first_wb_data_access is set when no WB cycle present till end of first data access of WB cycle on WB bus
916
always@(posedge wb_clock_in or posedge reset_in)
917
begin
918
        if (reset_in)
919
                first_wb_data_access = 1'b1 ;
920
        else
921
        begin
922
                if (wb_access_clk_en)
923
                        first_wb_data_access = wb_access_data ;
924
        end
925
end
926
 
927
 
928
// Signals to FIFO
929
assign  pcir_fifo_be_out = pci_tar_be ;
930
assign  pcir_fifo_data_out = MDATA_I ;
931
 
932
// OUTPUT signals
933
assign  pci_error_bc = bc_register ;
934
 
935
assign  STB_O = wb_stb_o ;
936
assign  WE_O = wb_we_o ;
937
assign  CYC_O = wb_cyc_o ;
938
assign  CAB_O = wb_cyc_o & burst_transfer ;
939
assign  ADR_O = addr_cnt_out ;
940
assign  MDATA_O = pciw_fifo_addr_data_in ;
941
 
942
always@(pciw_fifo_cbe_in or pci_tar_be or wb_we_o)
943
begin
944
        if (wb_we_o)
945
                SEL_O = ~pciw_fifo_cbe_in ;
946
        else
947
                SEL_O = ~pci_tar_be ;
948
end
949
 
950
 
951
endmodule
952
 

powered by: WebSVN 2.1.0

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