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

Subversion Repositories pci

[/] [pci/] [tags/] [rel_6/] [rtl/] [verilog/] [pci_master32_sm.v] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 mihad
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  File name "pci_master32_sm.v"                               ////
4
////                                                              ////
5
////  This file is part of the "PCI bridge" project               ////
6
////  http://www.opencores.org/cores/pci/                         ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - Miha Dolenc (mihad@opencores.org)                     ////
10
////                                                              ////
11
////  All additional information is avaliable in the README       ////
12
////  file.                                                       ////
13
////                                                              ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2001 Miha Dolenc, mihad@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
//
46
 
47
// module includes pci master state machine and surrounding logic
48
`include "bus_commands.v"
49
`include "constants.v"
50
module PCI_MASTER32_SM
51
(
52
    // system inputs
53
    clk_in,
54
    reset_in,
55
    // arbitration
56
    pci_req_out,
57
    pci_gnt_in,
58
    // master in/outs
59
    pci_frame_in,
60
    pci_frame_out,
61
    pci_frame_out_in,
62
    pci_frame_load_out,
63
    pci_frame_en_in,
64
    pci_frame_en_out,
65
    pci_irdy_in,
66
    pci_irdy_out,
67
    pci_irdy_en_out,
68
 
69
    // target response inputs
70
    pci_trdy_in,
71
    pci_trdy_reg_in,
72
    pci_stop_in,
73
    pci_stop_reg_in,
74
    pci_devsel_in,
75
    pci_devsel_reg_in,
76
 
77
    // address, data, bus command, byte enable in/outs
78
    pci_ad_reg_in,
79
    pci_ad_out,
80
    pci_ad_en_out,
81
    pci_cbe_out,
82
    pci_cbe_en_out,
83
 
84
    // other side of state machine
85
    address_in,
86
    bc_in,
87
    data_in,
88
    data_out,
89
    be_in,
90
    req_in,
91
    rdy_in,
92
    last_in,
93
    next_data_in,
94
    next_be_in,
95
    next_last_in,
96
    load_next_out,
97
    wait_out,
98
    wtransfer_out,
99
    rtransfer_out,
100
    retry_out,
101
    werror_out,
102
    rerror_out,
103
    first_out,
104
    mabort_out,
105
    latency_tim_val_in
106
) ;
107
 
108
// system inputs
109
input   clk_in,
110
        reset_in ;
111
 
112
/*==================================================================================================================
113
PCI interface signals - bidirectional signals are divided to inputs and outputs in I/O cells instantiation
114
module. Enables are separate signals.
115
==================================================================================================================*/
116
// arbitration
117
output  pci_req_out ;
118
 
119
input   pci_gnt_in ;
120
 
121
// master in/outs
122
input   pci_frame_in ;
123
input   pci_frame_en_in ;
124
input   pci_frame_out_in ;
125
 
126
output  pci_frame_out,
127
        pci_frame_en_out ;
128
 
129
output  pci_frame_load_out ;
130
 
131
input   pci_irdy_in ;
132
output  pci_irdy_out,
133
        pci_irdy_en_out;
134
 
135
// target response inputs
136
input   pci_trdy_in,
137
        pci_trdy_reg_in,
138
        pci_stop_in,
139
        pci_stop_reg_in,
140
        pci_devsel_in,
141
        pci_devsel_reg_in ;
142
 
143
// address, data, bus command, byte enable in/outs
144
input   [31:0]  pci_ad_reg_in ;
145
output  [31:0]  pci_ad_out ;
146
 
147
reg     [31:0]  pci_ad_out ;
148
 
149
output          pci_ad_en_out ;
150
 
151
output  [3:0]   pci_cbe_out ;
152
 
153
reg     [3:0]   pci_cbe_out ;
154
 
155
output          pci_cbe_en_out ;
156
 
157
input   [31:0]  address_in ; // current request address input
158
 
159
input   [3:0]   bc_in ;      // current request bus command input
160
 
161
input   [31:0]  data_in ;    // current dataphase data input
162
 
163
output  [31:0]  data_out ;    // for read operations - current request data output
164
 
165
reg     [31:0]  data_out ;
166
 
167
input   [3:0]   be_in ;      // current dataphase byte enable inputs
168
 
169
input           req_in ;     // initiator cycle is requested
170
input           rdy_in ;     // requestor indicates that data is ready to be sent for write transaction and ready to
171
                            // be received on read transaction
172
input           last_in ;    // last dataphase in current transaction indicator
173
 
174
// status outputs
175
output wait_out,            // wait indicates to the backend that dataphases are not in progress on PCI bus
176
       wtransfer_out,       // on any rising clock edge that this status is 1, data is transferred - heavy constraints here
177
       rtransfer_out,       // registered transfer indicator - when 1 indicates that data was transfered on previous clock cycle
178
       retry_out,           // retry status output - when target signals a retry
179
       werror_out,          // error output - when 1 indicates that error (target abort) occured on current dataphase - heavy constraints
180
       rerror_out,          // registered error output - when 1 indicates that error was signalled by a target on previous clock cycle
181
       first_out ,          // indicates whether or not any data was transfered in current transaction
182
       mabort_out;          // master abort indicator
183
 
184
reg wait_out ;
185
 
186
// latency timer value input - state machine starts latency timer whenever it starts a transaction and last is not
187
// asserted ( meaning burst transfer ). 
188
input [7:0] latency_tim_val_in ;
189
 
190
// next data, byte enable and last inputs
191
input [31:0] next_data_in ;
192
input [3:0]  next_be_in ;
193
input        next_last_in ;
194
 
195
// clock enable for data output flip-flops - whenever data is transfered, sm loads next data to those flip flops
196
output       load_next_out ;
197
 
198
// parameters - states - one hot
199
// idle state
200
parameter S_IDLE            = 4'h1 ;
201
 
202
// address state
203
parameter S_ADDRESS         = 4'h2 ;
204
 
205
// transfer state - dataphases
206
parameter S_TRANSFER        = 4'h4 ;
207
 
208
// turn arround state
209
parameter S_TA_END          = 4'h8 ;
210
 
211
// change state - clock enable for sm state register
212
wire change_state ;
213
// next state for state machine
214
reg [4:0] next_state ;
215
// SM state register
216
reg [4:0] cur_state ;
217
 
218
// variables for indicating which state state machine is in
219
// this variables are used to reduce logic levels in case of heavily constrained PCI signals
220
reg sm_idle            ;
221
reg sm_address         ;
222
reg sm_data_phases     ;
223
reg sm_turn_arround    ;
224
 
225
// state machine register control logic with clock enable
226
always@(posedge reset_in or posedge clk_in)
227
begin
228
    if (reset_in)
229
        cur_state <= #`FF_DELAY S_IDLE ;
230
    else
231
    if ( change_state )
232
        cur_state <= #`FF_DELAY next_state ;
233
end
234
 
235
// parameters - data selector - ad and bc lines switch between address/data and bus command/byte enable respectively
236
parameter SEL_ADDR_BC      = 2'b01 ;
237
parameter SEL_DATA_BE      = 2'b00 ;
238
parameter SEL_NEXT_DATA_BE = 2'b11 ;
239
 
240
reg [1:0] wdata_selector ;
241
 
242
wire u_dont_have_pci_bus = pci_gnt_in || ~pci_frame_in || ~pci_irdy_in ;    // pci master can't start a transaction when GNT is deasserted ( 1 ) or
243
                                                                            // bus is not in idle state ( FRAME and IRDY both 1 )
244
wire u_have_pci_bus      = ~pci_gnt_in && pci_frame_in && pci_irdy_in ;
245
 
246
// decode count enable - counter that counts cycles passed since address phase
247
wire        sm_decode_count_enable = sm_data_phases ;                                                               // counter is enabled when master wants to transfer 
248
wire        decode_count_enable    = sm_decode_count_enable && pci_trdy_in && pci_stop_in && pci_devsel_in ;        // and target is not responding
249
wire        decode_count_load      = ~decode_count_enable ;
250
reg [2:0]   decode_count ;
251
 
252
wire decode_to = ~( decode_count[2] || decode_count[1]) ;
253
 
254
always@(posedge reset_in or posedge clk_in)
255
begin
256
    if ( reset_in )
257
        // initial value of counter is 4
258
        decode_count <= #`FF_DELAY 3'h4 ;
259
    else
260
    if ( decode_count_load )
261
        decode_count <= #`FF_DELAY 3'h4 ;
262
    else
263
    if ( decode_count_enable )
264
        decode_count <= #`FF_DELAY decode_count - 1'b1 ;
265
end
266
 
267
// Bus commands LSbit indicates whether operation is a read or a write
268
wire do_write = bc_in[0] ;
269
 
270
// latency timer
271
reg [7:0]   latency_timer ;
272
 
273
 
274
wire latency_timer_enable = sm_data_phases ;
275
wire latency_timer_load   = ~sm_address && ~sm_data_phases ;
276
wire latency_timer_exp    = ~(
277
                               (latency_timer[7] || latency_timer[6] || latency_timer[5] || latency_timer[4]) ||
278
                               (latency_timer[3] || latency_timer[2] || latency_timer_load)
279
                             ) ;
280
 
281
// flip flop for registering latency timer timeout
282
reg         latency_time_out ;
283
always@(posedge clk_in or posedge reset_in)
284
begin
285
    if (reset_in)
286
        latency_time_out <= #`FF_DELAY 1'b0 ;
287
    else
288
        latency_time_out <= #`FF_DELAY latency_timer_exp ;
289
end
290
 
291
always@(posedge clk_in or posedge reset_in)
292
begin
293
    if (reset_in)
294
        latency_timer <= #`FF_DELAY 8'hFF ;
295
    else
296
    if ( latency_timer_load )
297
        latency_timer <= #`FF_DELAY latency_tim_val_in ;
298
    else
299
    if ( latency_timer_enable && ~latency_time_out)         // latency timer counts down until it expires - then it stops
300
        latency_timer <= #`FF_DELAY latency_timer - 1'b1 ;
301
end
302
 
303
// master abort indicators - when decode time out occurres and still no target response is received
304
wire do_master_abort = decode_to && pci_trdy_in && pci_stop_in && pci_devsel_in ;
305
reg mabort1 ;
306
always@(posedge reset_in or posedge clk_in)
307
begin
308
    if (reset_in)
309
        mabort1 <= #`FF_DELAY 1'b0 ;
310
    else
311
        mabort1 <= #`FF_DELAY do_master_abort ;
312
end
313
 
314
reg mabort2 ;
315
always@(posedge reset_in or posedge clk_in)
316
begin
317
    if ( reset_in )
318
        mabort2 <= #`FF_DELAY 1'b0 ;
319
    else
320
        mabort2 <= #`FF_DELAY mabort1 ;
321
end
322
 
323
// master abort is only asserted for one clock cycle
324
assign mabort_out = mabort1 && ~mabort2 ;
325
 
326
// register indicating when master should do timeout termination (latency timer expires)
327
reg timeout ;
328
always@(posedge reset_in or posedge clk_in)
329
begin
330
    if (reset_in)
331
        timeout <= #`FF_DELAY 1'b0 ;
332
    else
333
        timeout <= #`FF_DELAY (latency_time_out && ~pci_frame_out_in && pci_gnt_in || timeout ) && ~wait_out ;
334
end
335
 
336
wire timeout_termination = sm_turn_arround && timeout && pci_stop_reg_in ;
337
 
338
// frame control logic
339
// frame is forced to 0 (active) when state machine is in idle state, since only possible next state is address state which always drives frame active
340
wire force_frame = ~sm_idle ;
341
// slow signal for frame calculated from various registers in the core
342
wire slow_frame  = last_in || timeout || (next_last_in && sm_data_phases) || mabort1 ;
343
// critical timing frame logic in separate module - some combinations of target signals force frame to inactive state immediately after sampled asserted
344
// (STOP)
345
FRAME_CRIT frame_iob_feed
346
(
347
    .pci_frame_out      (pci_frame_out),
348
    .force_frame_in     (force_frame),
349
    .slow_frame_in      (slow_frame),
350
    .pci_stop_in        (pci_stop_in)
351
) ;
352
 
353
// frame IOB flip flop's clock enable signal
354
// slow clock enable - calculated from internal - non critical paths
355
wire frame_load_slow = sm_idle || sm_address || mabort1 ;
356
 
357
// critical clock enable for frame IOB in separate module - target response signals actually allow frame value change - critical timing
358
FRAME_LOAD_CRIT frame_iob_ce
359
(
360
    .pci_frame_load_out (pci_frame_load_out),
361
    .sm_data_phases_in  (sm_data_phases),
362
    .frame_load_slow_in (frame_load_slow),
363
    .pci_trdy_in        (pci_trdy_in),
364
    .pci_stop_in        (pci_stop_in)
365
) ;
366
 
367
// IRDY driving
368
// non critical path for IRDY calculation
369
wire irdy_slow = pci_frame_out_in && mabort1 || mabort2 ;
370
 
371
// critical path in separate module
372
IRDY_OUT_CRIT irdy_iob_feed
373
(
374
    .pci_irdy_out       (pci_irdy_out),
375
    .irdy_slow_in       (irdy_slow),
376
    .pci_frame_out_in   (pci_frame_out_in),
377
    .pci_trdy_in        (pci_trdy_in),
378
    .pci_stop_in        (pci_stop_in)
379
) ;
380
 
381
// transfer FF indicator - when first transfer occurs it is set to 1 so backend can distinguish between disconnects and retries.
382
wire sm_transfer = sm_data_phases ;
383
reg transfer ;
384
 
385
wire transfer_input = sm_transfer && (~(pci_trdy_in || pci_devsel_in) || transfer) ;
386
 
387
always@(posedge clk_in or posedge reset_in)
388
begin
389
    if (reset_in)
390
        transfer <= #`FF_DELAY 1'b0 ;
391
    else
392
        transfer <= #`FF_DELAY transfer_input ;
393
end
394
 
395
assign first_out = ~transfer ;
396
 
397
// fast transfer status output - it's only negated target ready, since wait indicator qualifies valid transfer
398
assign wtransfer_out = ~pci_trdy_in ;
399
 
400
// registered transfer status output - calculated from registered target response inputs
401
assign rtransfer_out = ~(pci_trdy_reg_in || pci_devsel_reg_in) ;
402
 
403
// current error status - calculated directly from target signals and therefore critical
404
assign werror_out    = (~pci_stop_in && pci_devsel_in) ;
405
 
406
// registered error status - calculated from registered target response inputs
407
assign rerror_out    = (~pci_stop_reg_in && pci_devsel_reg_in) ;
408
 
409
// retry is signalled to backend depending on registered target response or when latency timer expires
410
assign retry_out = timeout_termination || (~pci_stop_reg_in && ~pci_devsel_reg_in) ;
411
 
412
// AD output flip flops' clock enable
413
// new data is loaded to AD outputs whenever state machine is idle, bus was granted and bus is in idle state or
414
// when address phase is about to be finished
415
wire load_force = (sm_idle && u_have_pci_bus) || (sm_address && do_write) ;
416
 
417
// next data loading is allowed when state machine is in transfer state and operation is a write
418
wire load_allow = sm_data_phases && do_write ;
419
 
420
// actual loading during data phases is done by monitoring critical target response signals - separate module
421
MAS_LOAD_NEXT_CRIT ad_iob_ce
422
(
423
    .load_next_out      (load_next_out),
424
    .load_force_in      (load_force),
425
    .load_allow_in      (load_allow),
426
    .pci_trdy_in        (pci_trdy_in)
427
) ;
428
 
429
// request for a bus is issued anytime when backend is requesting a transaction and state machine is in idle state
430
assign pci_req_out = ~(req_in && sm_idle) ;
431
 
432
// change state signal is actually clock enable for state register
433
// Non critical path for state change enable:
434
// state is always changed when:
435
// - address phase is finishing
436
// - state machine is in turn arround state
437
// - state machine is in transfer state and master abort termination is in progress
438
 
439
wire ch_state_slow = sm_address || sm_turn_arround || sm_data_phases && ( pci_frame_out_in && mabort1 || mabort2 ) ;
440
 
441
// a bit more critical change state enable is calculated with GNT signal
442
wire ch_state_med  = ch_state_slow || sm_idle && u_have_pci_bus && req_in && rdy_in ;
443
 
444
// most critical change state enable - calculated from target response signals
445
MAS_CH_STATE_CRIT state_machine_ce
446
(
447
    .change_state_out   (change_state),
448
    .ch_state_med_in    (ch_state_med),
449
    .sm_data_phases_in  (sm_data_phases),
450
    .pci_trdy_in        (pci_trdy_in),
451
    .pci_stop_in        (pci_stop_in)
452
) ;
453
 
454
// ad enable driving
455
// also divided in several categories - from less critical to most critical in separate module
456
wire ad_en_slowest  = do_write && (sm_address || sm_data_phases && ~pci_frame_out_in) ;
457
wire ad_en_on_grant = sm_idle && pci_frame_in && pci_irdy_in || sm_turn_arround ;
458
wire ad_en_slow     = ad_en_on_grant && ~pci_gnt_in || ad_en_slowest ;
459
wire ad_en_keep     = sm_data_phases && do_write && (pci_frame_out_in && ~mabort1 && ~mabort2) ;
460
 
461
// critical timing ad enable - calculated from target response inputs
462
MAS_AD_EN_CRIT ad_iob_oe_feed
463
(
464
    .pci_ad_en_out      (pci_ad_en_out),
465
    .ad_en_slow_in      (ad_en_slow),
466
    .ad_en_keep_in      (ad_en_keep),
467
    .pci_stop_in        (pci_stop_in),
468
    .pci_trdy_in        (pci_trdy_in)
469
) ;
470
 
471
// cbe enable driving
472
wire cbe_en_on_grant = sm_idle && pci_frame_in && pci_irdy_in || sm_turn_arround ;
473
wire cbe_en_slow     = cbe_en_on_grant && ~pci_gnt_in || sm_address || sm_data_phases && ~pci_frame_out_in ;
474
wire cbe_en_keep     = sm_data_phases && pci_frame_out_in && ~mabort1 && ~mabort2 ;
475
 
476
// most critical cbe enable in separate module - calculated with most critical target inputs
477
CBE_EN_CRIT cbe_iob_feed
478
(
479
    .pci_cbe_en_out     (pci_cbe_en_out),
480
    .cbe_en_slow_in     (cbe_en_slow),
481
    .cbe_en_keep_in     (cbe_en_keep),
482
    .pci_stop_in        (pci_stop_in),
483
    .pci_trdy_in        (pci_trdy_in)
484
 
485
) ;
486
 
487
// IRDY enable is equal to FRAME enable delayed for one clock
488
assign pci_irdy_en_out   = pci_frame_en_in ;
489
 
490
// frame enable driving - sometimes it's calculated from non critical paths
491
wire frame_en_slow = (sm_idle && u_have_pci_bus && req_in && rdy_in) || sm_address || (sm_data_phases && ~pci_frame_out_in) ;
492
wire frame_en_keep = sm_data_phases && pci_frame_out_in && ~mabort1 && ~mabort2 ;
493
 
494
// most critical frame enable - calculated from heavily constrained target inputs in separate module
495
FRAME_EN_CRIT frame_iob_en_feed
496
(
497
    .pci_frame_en_out   (pci_frame_en_out),
498
    .frame_en_slow_in   (frame_en_slow),
499
    .frame_en_keep_in   (frame_en_keep),
500
    .pci_stop_in        (pci_stop_in),
501
    .pci_trdy_in        (pci_trdy_in)
502
) ;
503
 
504
// state machine next state definitions
505
always@(
506
    cur_state or
507
    do_write or
508
    pci_frame_out_in
509
)
510
begin
511
    // default values for state machine outputs
512
    wait_out                = 1'b1 ;
513
    wdata_selector          = SEL_ADDR_BC ;
514
    sm_idle                 = 1'b0 ;
515
    sm_address              = 1'b0 ;
516
    sm_data_phases          = 1'b0 ;
517
    sm_turn_arround         = 1'b0 ;
518
 
519
    case ( cur_state )
520
 
521
        S_IDLE: begin
522
                    // indicate the state
523
                    sm_idle      = 1'b1 ;
524
                    // assign next state - only possible is address - if state machine is supposed to stay in idle state
525
                    // outside signals disable the clock
526
                    next_state   = S_ADDRESS ;
527
                end
528
 
529
        S_ADDRESS:  begin
530
                        // indicate the state
531
                        sm_address  = 1'b1 ;
532
                        // select appropriate data/be for outputs
533
                        wdata_selector = SEL_DATA_BE ;
534
                        // only possible next state is transfer state
535
                        next_state = S_TRANSFER ;
536
                    end
537
 
538
        S_TRANSFER: begin
539
                        // during transfers wait indicator is inactive - all status signals are now valid
540
                        wait_out               = 1'b0 ;
541
                        // indicate the state
542
                        sm_data_phases         = 1'b1 ;
543
                        // select appropriate data/be for outputs
544
                        wdata_selector = SEL_NEXT_DATA_BE ;
545
                        if ( pci_frame_out_in )
546
                        begin
547
                            // when frame is inactive next state will be turn arround
548
                            next_state = S_TA_END ;
549
                        end
550
                        else
551
                            // while frame is active state cannot be anything else then transfer
552
                            next_state = S_TRANSFER ;
553
                    end
554
 
555
        S_TA_END:   begin
556
                        // wait is still inactive because of registered statuses
557
                        wait_out = 1'b0 ;
558
                        // indicate the state
559
                        sm_turn_arround = 1'b1 ;
560
                        // next state is always idle
561
                        next_state = S_IDLE ;
562
                    end
563
        default:    next_state = S_IDLE ;
564
    endcase
565
end
566
 
567
// ad and cbe lines multiplexer for write data
568
always@(wdata_selector or address_in or bc_in or data_in or be_in or next_data_in or next_be_in)
569
begin
570
    case ( wdata_selector )
571
        SEL_ADDR_BC:    begin
572
                            pci_ad_out  = address_in ;
573
                            pci_cbe_out = bc_in ;
574
                        end
575
 
576
        SEL_DATA_BE:    begin
577
                            pci_ad_out  = data_in ;
578
                            pci_cbe_out = be_in ;
579
                        end
580
        SEL_NEXT_DATA_BE,
581
        2'b10:              begin
582
                                pci_ad_out  = next_data_in ;
583
                                pci_cbe_out = next_be_in ;
584
                            end
585
    endcase
586
end
587
 
588
// data output mux for reads
589
always@(mabort_out or pci_ad_reg_in)
590
begin
591
    if ( mabort_out )
592
        data_out = 32'hFFFF_FFFF ;
593
    else
594
        data_out = pci_ad_reg_in ;
595
end
596
endmodule

powered by: WebSVN 2.1.0

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