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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber25/] [a25_wishbone.v] - Blame information for rev 31

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

Line No. Rev Author Line
1 16 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Wishbone master interface for the Amber 25 core             //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Turns memory access requests from the execute stage and     //
10 17 csantifort
//  instruction and data caches into wishbone bus cycles.       //
11
//  For 4-word read requests from either cache and swap         //
12
//  accesses ( read followed by write to the same address)      //
13
//  from the execute stage, a block transfer is done.           //
14
//  All other requests result in single word transfers.         //
15 16 csantifort
//                                                              //
16
//  Write accesses can be done in a single clock cycle on       //
17
//  the wishbone bus, is the destination allows it. The         //
18
//  next transfer will begin immediately on the                 //
19
//  next cycle on the bus. This looks like a block transfer     //
20
//  and does hold ownership of the wishbone bus, preventing     //
21
//  the other master ( the ethernet MAC) from gaining           //
22
//  ownership between those two cycles. But otherwise it would  //
23
//  be necessary to insert a wait cycle after every write,      //
24
//  slowing down the performance of the core by around 5 to     //
25
//  10%.                                                        //
26
//                                                              //
27
//  Author(s):                                                  //
28
//      - Conor Santifort, csantifort.amber@gmail.com           //
29
//                                                              //
30
//////////////////////////////////////////////////////////////////
31
//                                                              //
32
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
33
//                                                              //
34
// This source file may be used and distributed without         //
35
// restriction provided that this copyright statement is not    //
36
// removed from the file and that any derivative work contains  //
37
// the original copyright notice and the associated disclaimer. //
38
//                                                              //
39
// This source file is free software; you can redistribute it   //
40
// and/or modify it under the terms of the GNU Lesser General   //
41
// Public License as published by the Free Software Foundation; //
42
// either version 2.1 of the License, or (at your option) any   //
43
// later version.                                               //
44
//                                                              //
45
// This source is distributed in the hope that it will be       //
46
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
47
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
48
// PURPOSE.  See the GNU Lesser General Public License for more //
49
// details.                                                     //
50
//                                                              //
51
// You should have received a copy of the GNU Lesser General    //
52
// Public License along with this source; if not, download it   //
53
// from http://www.opencores.org/lgpl.shtml                     //
54
//                                                              //
55
//////////////////////////////////////////////////////////////////
56
 
57
 
58
module a25_wishbone
59
(
60
input                       i_clk,
61
 
62
    // Instruction Cache Accesses
63
input                       i_icache_req,
64
input                       i_icache_qword,
65
input       [31:0]          i_icache_address,
66
output      [31:0]          o_icache_read_data,
67
output                      o_icache_ready,
68
 
69
    // Data Cache Accesses
70
input                       i_exclusive,      // high for read part of swap access
71
input                       i_dcache_cached_req,
72
input                       i_dcache_uncached_req,
73
input                       i_dcache_qword,
74
input                       i_dcache_write,
75
input       [31:0]          i_dcache_write_data,
76
input       [3:0]           i_dcache_byte_enable,    // valid for writes only
77
input       [31:0]          i_dcache_address,
78
output      [31:0]          o_dcache_read_data,
79
output                      o_dcache_cached_ready,
80
output                      o_dcache_uncached_ready,
81
 
82
// Wishbone Bus
83
output reg  [31:0]          o_wb_adr = 'd0,
84
output reg  [3:0]           o_wb_sel = 'd0,
85
output reg                  o_wb_we  = 'd0,
86
input       [31:0]          i_wb_dat,
87
output reg  [31:0]          o_wb_dat = 'd0,
88
output reg                  o_wb_cyc = 'd0,
89
output reg                  o_wb_stb = 'd0,
90
input                       i_wb_ack,
91
input                       i_wb_err
92
 
93
);
94
 
95
 
96
localparam [3:0] WB_IDLE            = 3'd0,
97
                 WB_BURST1          = 3'd1,
98
                 WB_BURST2          = 3'd2,
99
                 WB_BURST3          = 3'd3,
100
                 WB_WAIT_ACK        = 3'd4;
101
 
102
reg     [2:0]               wishbone_st = WB_IDLE;
103
 
104
wire                        icache_read_req_c;
105
wire                        icache_read_qword_c;
106
wire    [31:0]              icache_read_addr_c;
107
wire                        dcache_read_qword_c;
108
 
109
wire                        dcache_req_c;
110
wire                        write_req_c;
111
wire                        dcache_cached_rreq_c;
112
wire                        dcache_cached_wreq_c;
113
wire                        dcache_uncached_rreq_c;
114
wire                        dcache_uncached_wreq_c;
115
 
116
wire                        dcache_cached_rreq_in;
117
wire                        dcache_cached_wreq_in;
118
wire                        dcache_uncached_rreq_in;
119
wire                        dcache_uncached_wreq_in;
120
 
121
reg                         dcache_cached_rreq_r   = 'd0;
122
reg                         dcache_cached_wreq_r   = 'd0;
123
reg                         dcache_uncached_rreq_r = 'd0;
124
reg                         dcache_uncached_wreq_r = 'd0;
125
 
126
wire                        dcache_cached_wready;
127
wire                        dcache_uncached_wready;
128
wire                        dcache_cached_rready;
129
wire                        dcache_uncached_rready;
130
 
131
wire                        start_access;
132
wire    [3:0]               byte_enable;
133
reg                         exclusive_access = 'd0;
134
wire                        read_ack;
135
wire                        wait_write_ack;
136
reg                         icache_read_req_r  = 'd0;
137
reg                         icache_read_qword_r  = 'd0;
138
reg     [31:0]              icache_read_addr_r  = 'd0;
139
reg                         dcache_read_qword_r  = 'd0;
140
wire                        icache_read_req_in;
141
wire                        icache_read_ready;
142
reg                         servicing_dcache_cached_read_r = 'd0;
143
reg                         servicing_dcache_uncached_read_r = 'd0;
144 30 csantifort
reg                         servicing_dcache_cached_write_r = 'd0;
145
reg                         servicing_dcache_uncached_write_r = 'd0;
146 16 csantifort
reg                         servicing_icache_r = 'd0;
147 30 csantifort
wire                        buffer_write;
148
reg                         buffer_write_r = 'd0;
149
reg     [31:0]              buffer_write_data_r;
150
reg     [31:0]              buffer_write_address_r;
151
reg     [3:0]               buffer_write_be_r;
152
wire                        write_ack;
153 16 csantifort
 
154 30 csantifort
 
155 16 csantifort
assign read_ack                 = !o_wb_we && i_wb_ack;
156
 
157
assign dcache_cached_rready     = dcache_cached_rreq_r   && servicing_dcache_cached_read_r   && read_ack;
158
assign dcache_uncached_rready   = dcache_uncached_rreq_r && servicing_dcache_uncached_read_r && read_ack;
159
 
160 30 csantifort
assign dcache_cached_wready     = (dcache_cached_wreq_c && wishbone_st == WB_IDLE && !dcache_cached_rreq_c)  ||
161
                                  (servicing_dcache_cached_write_r && i_wb_ack  && wishbone_st == WB_WAIT_ACK && !dcache_cached_rreq_c);
162
assign dcache_uncached_wready   = (dcache_uncached_wreq_c && wishbone_st == WB_IDLE && !dcache_uncached_rreq_c) ||
163
                                  (servicing_dcache_uncached_write_r && i_wb_ack  && wishbone_st == WB_WAIT_ACK && !dcache_uncached_rreq_c);
164 16 csantifort
 
165
assign o_dcache_cached_ready    = dcache_cached_rready   || dcache_cached_wready;
166
assign o_dcache_uncached_ready  = dcache_uncached_rready || dcache_uncached_wready;
167
assign o_dcache_read_data       = i_wb_dat;
168
 
169
assign icache_read_ready        = servicing_icache_r && read_ack;
170
assign o_icache_ready           = icache_read_ready;
171
assign o_icache_read_data       = i_wb_dat;
172
 
173
assign dcache_cached_rreq_in    = i_dcache_cached_req   && !i_dcache_write;
174
assign dcache_cached_wreq_in    = i_dcache_cached_req   &&  i_dcache_write;
175
assign dcache_uncached_rreq_in  = i_dcache_uncached_req && !i_dcache_write;
176
assign dcache_uncached_wreq_in  = i_dcache_uncached_req &&  i_dcache_write;
177
assign icache_read_req_in       = i_icache_req && !o_icache_ready;
178
 
179
assign dcache_cached_rreq_c     = ( dcache_cached_rreq_in   || dcache_cached_rreq_r   ) && !(servicing_dcache_cached_read_r   && read_ack);
180
assign dcache_uncached_rreq_c   = ( dcache_uncached_rreq_in || dcache_uncached_rreq_r ) && !(servicing_dcache_uncached_read_r && read_ack);
181
 
182
assign dcache_read_qword_c      = ( i_dcache_qword       || dcache_read_qword_r ) && !(servicing_dcache_cached_read_r && read_ack);
183
 
184
assign icache_read_req_c        = ( icache_read_req_in   || icache_read_req_r   ) && !(servicing_icache_r && read_ack);
185
assign icache_read_qword_c      = ( i_icache_qword       || icache_read_qword_r ) && !(servicing_icache_r && read_ack);
186
assign icache_read_addr_c       = i_icache_req ?  i_icache_address : icache_read_addr_r;
187
 
188
assign dcache_req_c             = dcache_cached_rreq_c || dcache_cached_wreq_c || dcache_uncached_rreq_c || dcache_uncached_wreq_c;
189
assign write_req_c              = dcache_cached_wreq_c || dcache_uncached_wreq_c;
190
 
191
assign start_access             = !wait_write_ack && (dcache_req_c || icache_read_req_c);
192
 
193
// For writes the byte enable is always 4'hf
194
assign byte_enable              = write_req_c ? i_dcache_byte_enable : 4'hf;
195
 
196
assign dcache_cached_wreq_c     = dcache_cached_wreq_in   || dcache_cached_wreq_r;
197
assign dcache_uncached_wreq_c   = dcache_uncached_wreq_in || dcache_uncached_wreq_r;
198
 
199
 
200
// ======================================
201
// Register Accesses
202
// ======================================
203
 
204 30 csantifort
assign buffer_write   = dcache_cached_wreq_in  || dcache_uncached_wreq_in;
205
assign write_buffered = dcache_uncached_wreq_r || dcache_uncached_wreq_r;
206 16 csantifort
 
207
 
208
 
209
always @( posedge i_clk )
210
    begin
211
    icache_read_req_r   <= icache_read_req_in  || icache_read_req_c;
212
    icache_read_qword_r <= i_icache_qword      || icache_read_qword_c;
213
    if ( i_icache_req ) icache_read_addr_r  <= i_icache_address;
214
 
215
    dcache_read_qword_r    <= i_dcache_qword      || dcache_read_qword_c;
216 30 csantifort
 
217
    // Buffer Write requests
218
    case (wishbone_st)
219
        WB_WAIT_ACK :
220
            begin
221
            if (servicing_dcache_uncached_write_r && i_wb_ack && !buffer_write)
222
                dcache_uncached_wreq_r <= 1'd0;
223
            else
224
                dcache_uncached_wreq_r <= dcache_uncached_wreq_c;
225
 
226
            if (servicing_dcache_cached_write_r && i_wb_ack && !buffer_write)
227
                dcache_cached_wreq_r <= 1'd0;
228
            else
229
                dcache_cached_wreq_r <= dcache_cached_wreq_c;
230
            end
231
 
232
        WB_IDLE:
233
            begin
234
            if (dcache_uncached_wreq_c && o_wb_stb && !i_wb_ack)
235
                dcache_uncached_wreq_r <= 1'd1;
236
            else
237
                dcache_uncached_wreq_r <= 1'd0;
238
 
239
            if (dcache_cached_wreq_c && o_wb_stb && !i_wb_ack)
240
                dcache_cached_wreq_r <= 1'd1;
241
            else
242
                dcache_cached_wreq_r <= 1'd0;
243
            end
244
 
245
        default:
246
            begin
247
            dcache_uncached_wreq_r <= dcache_uncached_wreq_c;
248
            dcache_cached_wreq_r   <= dcache_cached_wreq_c;
249
            end
250
    endcase
251
 
252
 
253
    // A buffer to hold a second write while on is in progress
254
    if ( buffer_write )
255 16 csantifort
        begin
256 30 csantifort
        buffer_write_data_r      <= i_dcache_write_data;
257
        buffer_write_address_r   <= i_dcache_address;
258
        buffer_write_be_r        <= i_dcache_byte_enable;
259 16 csantifort
        end
260
 
261
 
262
    // The flag can be set during any state but only cleared during WB_IDLE or WB_WAIT_ACK
263
    if ( dcache_cached_rreq_r )
264
        begin
265
        if ( wishbone_st == WB_IDLE || wishbone_st == WB_WAIT_ACK )
266
            dcache_cached_rreq_r <= dcache_cached_rreq_c && !o_dcache_cached_ready;
267
        end
268
    else
269 30 csantifort
        dcache_cached_rreq_r <= dcache_cached_rreq_c && (!o_dcache_cached_ready || dcache_cached_wreq_r);
270
 
271 16 csantifort
    if ( dcache_uncached_rreq_r )
272
        begin
273
        if ( wishbone_st == WB_IDLE || wishbone_st == WB_WAIT_ACK )
274
            dcache_uncached_rreq_r <= dcache_uncached_rreq_c && !o_dcache_uncached_ready;
275
        end
276
    else
277 30 csantifort
        dcache_uncached_rreq_r <= dcache_uncached_rreq_c && (!o_dcache_uncached_ready || dcache_uncached_wreq_r);
278 16 csantifort
    end
279
 
280
assign wait_write_ack = o_wb_stb && o_wb_we && !i_wb_ack;
281 30 csantifort
assign write_ack      = o_wb_stb && o_wb_we && i_wb_ack;
282 16 csantifort
 
283
 
284
always @( posedge i_clk )
285
    case ( wishbone_st )
286
        WB_IDLE :
287 30 csantifort
            begin
288 16 csantifort
            if ( start_access )
289
                begin
290
                o_wb_stb            <= 1'd1;
291
                o_wb_cyc            <= 1'd1;
292
                o_wb_sel            <= byte_enable;
293 30 csantifort
                o_wb_dat            <= write_buffered ? buffer_write_data_r : i_dcache_write_data;
294 16 csantifort
                end
295
            else if ( !wait_write_ack )
296
                begin
297
                o_wb_stb            <= 1'd0;
298
 
299
                // Hold cyc high after an exclusive access
300
                // to hold ownership of the wishbone bus
301
                o_wb_cyc            <= exclusive_access;
302
                end
303
 
304
            if ( wait_write_ack )
305
                begin
306
                // still waiting for last (write) access to complete
307 30 csantifort
                wishbone_st                         <= WB_WAIT_ACK;
308
                servicing_dcache_cached_read_r      <= dcache_cached_rreq_c;
309
                servicing_dcache_uncached_read_r    <= dcache_uncached_rreq_c;
310
                servicing_dcache_cached_write_r     <= dcache_cached_wreq_c;
311
                servicing_dcache_uncached_write_r   <= dcache_uncached_wreq_c;
312 16 csantifort
                end
313
            // dcache accesses have priority over icache     
314
            else if ( dcache_cached_rreq_c || dcache_uncached_rreq_c )
315
                begin
316
                if ( dcache_cached_rreq_c )
317
                    servicing_dcache_cached_read_r <= 1'd1;
318
                else if ( dcache_uncached_rreq_c )
319
                    servicing_dcache_uncached_read_r <= 1'd1;
320
 
321
                if ( dcache_read_qword_c )
322
                    wishbone_st         <= WB_BURST1;
323
                else
324
                    wishbone_st         <= WB_WAIT_ACK;
325
                exclusive_access    <= i_exclusive;
326
                end
327
           // The core does not currently issue exclusive write requests
328
           // but there's no reason why this might not be added some
329
           // time in the future so allow for it here
330
            else if ( write_req_c )
331
                begin
332
                exclusive_access            <= i_exclusive;
333
                end
334
            // do a burst of 4 read to fill a cache line                   
335
            else if ( icache_read_req_c && icache_read_qword_c )
336
                begin
337
                wishbone_st                 <= WB_BURST1;
338
                exclusive_access            <= 1'd0;
339
                servicing_icache_r          <= 1'd1;
340
                end
341
            // single word read request from fetch stage                   
342
            else if ( icache_read_req_c )
343
                begin
344
                wishbone_st                 <= WB_WAIT_ACK;
345
                exclusive_access            <= 1'd0;
346
                servicing_icache_r          <= 1'd1;
347
                end
348
 
349
 
350
            if ( start_access )
351
                begin
352
                if ( dcache_req_c )
353
                    begin
354
                    o_wb_we              <= write_req_c;
355
                    // only update these on new wb access to make debug easier
356
                    o_wb_adr[31:2]       <= i_dcache_address[31:2];
357
                    o_wb_adr[1:0]        <= byte_enable == 4'b0001 ? 2'd0 :
358
                                            byte_enable == 4'b0010 ? 2'd1 :
359
                                            byte_enable == 4'b0100 ? 2'd2 :
360
                                            byte_enable == 4'b1000 ? 2'd3 :
361
 
362
                                            byte_enable == 4'b0011 ? 2'd0 :
363
                                            byte_enable == 4'b1100 ? 2'd2 :
364
 
365
                                                                     2'd0 ;
366
                    end
367
                else
368
                    begin
369
                    o_wb_we              <= 1'd0;
370
                    o_wb_adr[31:0]       <= {icache_read_addr_c[31:2], 2'd0};
371
                    end
372
                end
373
            end
374
 
375
 
376
        // Read burst, wait for first ack
377
        WB_BURST1:
378
            if ( i_wb_ack )
379
                begin
380
                // burst of 4 that wraps
381
                o_wb_adr[3:2]   <= o_wb_adr[3:2] + 1'd1;
382
                wishbone_st     <= WB_BURST2;
383
                end
384
 
385
 
386
        // Read burst, wait for second ack
387
        WB_BURST2:
388
            if ( i_wb_ack )
389
                begin
390
                // burst of 4 that wraps
391
                o_wb_adr[3:2]   <= o_wb_adr[3:2] + 1'd1;
392
                wishbone_st     <= WB_BURST3;
393
                end
394
 
395
 
396
        // Read burst, wait for third ack
397
        WB_BURST3:
398
            if ( i_wb_ack )
399
                begin
400
                // burst of 4 that wraps
401
                o_wb_adr[3:2]   <= o_wb_adr[3:2] + 1'd1;
402
                wishbone_st     <= WB_WAIT_ACK;
403
                end
404
 
405
 
406
        // Wait for the wishbone ack to be asserted
407
        WB_WAIT_ACK:
408
            if ( i_wb_ack )
409 30 csantifort
                begin
410
                servicing_dcache_cached_read_r      <= 1'd0;
411
                servicing_dcache_uncached_read_r    <= 1'd0;
412
                servicing_dcache_cached_write_r     <= 1'd0;
413
                servicing_dcache_uncached_write_r   <= 1'd0;
414
                servicing_icache_r                  <= 1'd0;
415
 
416 16 csantifort
                // Another write that was acked and needs to be sent before returning to IDLE ?
417 30 csantifort
                if ( write_buffered )
418 16 csantifort
                    begin
419 30 csantifort
                    wishbone_st                         <= WB_IDLE;
420 16 csantifort
                    o_wb_stb                            <= 1'd1;
421
                    o_wb_cyc                            <= exclusive_access;
422 30 csantifort
                    o_wb_sel                            <= buffer_write_be_r;
423 16 csantifort
                    o_wb_we                             <= 1'd1;
424 30 csantifort
                    o_wb_adr[31:0]                      <= buffer_write_address_r;
425
                    o_wb_dat                            <= buffer_write_data_r;
426 16 csantifort
                    end
427
                else
428
                    begin
429
                    wishbone_st                         <= WB_IDLE;
430
                    o_wb_stb                            <= 1'd0;
431
                    o_wb_cyc                            <= exclusive_access;
432
                    o_wb_we                             <= 1'd0;
433
                    end
434 30 csantifort
                end
435 16 csantifort
 
436
    endcase
437
 
438
// ========================================================
439
// Debug Wishbone bus - not synthesizable
440
// ========================================================
441
//synopsys translate_off
442
wire    [(14*8)-1:0]   xWB_STATE;
443
 
444
 
445
assign xWB_STATE  = wishbone_st == WB_IDLE       ? "WB_IDLE"       :
446
                    wishbone_st == WB_BURST1     ? "WB_BURST1"     :
447
                    wishbone_st == WB_BURST2     ? "WB_BURST2"     :
448
                    wishbone_st == WB_BURST3     ? "WB_BURST3"     :
449
                    wishbone_st == WB_WAIT_ACK   ? "WB_WAIT_ACK"   :
450
                                                   "UNKNOWN"       ;
451
 
452
//synopsys translate_on
453
 
454
endmodule
455
 

powered by: WebSVN 2.1.0

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