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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [twoway/] [FT64_fetchbuf.v] - Blame information for rev 49

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

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2017-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      FT64_fetchbuf.v
9
//
10
// This source file is free software: you can redistribute it and/or modify 
11
// it under the terms of the GNU Lesser General Public License as published 
12
// by the Free Software Foundation, either version 3 of the License, or     
13
// (at your option) any later version.                                      
14
//                                                                          
15
// This source file is distributed in the hope that it will be useful,      
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
18
// GNU General Public License for more details.                             
19
//                                                                          
20
// You should have received a copy of the GNU General Public License        
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
22
//
23
// ============================================================================
24
//
25 49 robfinch
`include "FT64_config.vh"
26 48 robfinch
`include "FT64_defines.vh"
27
 
28
// FETCH
29
//
30
// fetch exactly two instructions from memory into the fetch buffer
31
// unless either one of the buffers is still full, in which case we
32
// do nothing (kinda like alpha approach)
33
// Like to turn this into an independent module at some point.
34
//
35 49 robfinch
module FT64_fetchbuf(rst, clk4x, clk, fcu_clk,
36 48 robfinch
        cs_i, cyc_i, stb_i, ack_o, we_i, adr_i, dat_i,
37
        hirq, thread_en,
38
        regLR,
39
    insn0, insn1, phit,
40
    threadx,
41
    branchmiss, misspc, branchmiss_thrd, predict_taken0, predict_taken1,
42
    predict_takenA, predict_takenB, predict_takenC, predict_takenD,
43
    queued1, queued2, queuedNop,
44
    pc0, pc1, fetchbuf, fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v,
45
    fetchbufA_instr, fetchbufA_pc,
46
    fetchbufB_instr, fetchbufB_pc,
47
    fetchbufC_instr, fetchbufC_pc,
48
    fetchbufD_instr, fetchbufD_pc,
49
    fetchbuf0_instr, fetchbuf1_instr, fetchbuf0_insln, fetchbuf1_insln,
50
    fetchbuf0_thrd, fetchbuf1_thrd,
51
    fetchbuf0_pc, fetchbuf1_pc,
52
    fetchbuf0_v, fetchbuf1_v,
53
    codebuf0, codebuf1,
54
    btgtA, btgtB, btgtC, btgtD,
55
    nop_fetchbuf,
56
    take_branch0, take_branch1,
57 49 robfinch
    stompedRets,
58
    panic
59 48 robfinch
);
60 49 robfinch
parameter AMSB = `AMSB;
61 48 robfinch
parameter RSTPC = 32'hFFFC0100;
62
parameter TRUE = 1'b1;
63
parameter FALSE = 1'b0;
64
input rst;
65
input clk4x;
66
input clk;
67 49 robfinch
input fcu_clk;
68 48 robfinch
input cs_i;
69
input cyc_i;
70
input stb_i;
71
output ack_o;
72
input we_i;
73
input [15:0] adr_i;
74
input [31:0] dat_i;
75
input hirq;
76
input thread_en;
77
input [4:0] regLR;
78
input [47:0] insn0;
79
input [47:0] insn1;
80
input phit;
81
output threadx;
82
input branchmiss;
83
input [AMSB:0] misspc;
84
input branchmiss_thrd;
85
output predict_taken0;
86
output predict_taken1;
87
input predict_takenA;
88
input predict_takenB;
89
input predict_takenC;
90
input predict_takenD;
91
input queued1;
92
input queued2;
93
input queuedNop;
94
output reg [AMSB:0] pc0;
95
output reg [AMSB:0] pc1;
96
output reg fetchbuf;
97
output reg fetchbufA_v;
98
output reg fetchbufB_v;
99
output reg fetchbufC_v;
100
output reg fetchbufD_v;
101
output fetchbuf0_thrd;
102
output fetchbuf1_thrd;
103
output reg [47:0] fetchbufA_instr;
104
output reg [47:0] fetchbufB_instr;
105
output reg [47:0] fetchbufC_instr;
106
output reg [47:0] fetchbufD_instr;
107
output reg [AMSB:0] fetchbufA_pc;
108
output reg [AMSB:0] fetchbufB_pc;
109
output reg [AMSB:0] fetchbufC_pc;
110
output reg [AMSB:0] fetchbufD_pc;
111
output [47:0] fetchbuf0_instr;
112
output [47:0] fetchbuf1_instr;
113
output [AMSB:0] fetchbuf0_pc;
114
output [AMSB:0] fetchbuf1_pc;
115
output reg [3:0] fetchbuf0_insln;
116
output reg [3:0] fetchbuf1_insln;
117
output fetchbuf0_v;
118
output fetchbuf1_v;
119
input [47:0] codebuf0;
120
input [47:0] codebuf1;
121
input [AMSB:0] btgtA;
122
input [AMSB:0] btgtB;
123
input [AMSB:0] btgtC;
124
input [AMSB:0] btgtD;
125
input [3:0] nop_fetchbuf;
126
output take_branch0;
127
output take_branch1;
128
input [3:0] stompedRets;
129 49 robfinch
output reg [3:0] panic;
130 48 robfinch
integer n;
131
 
132
//`include "FT64_decode.vh"
133
 
134
function IsBranch;
135
input [47:0] isn;
136
casex(isn[`INSTRUCTION_OP])
137
`Bcc:   IsBranch = TRUE;
138
`BBc:   IsBranch = TRUE;
139
`BEQI:  IsBranch = TRUE;
140
`BCHK:  IsBranch = TRUE;
141
default: IsBranch = FALSE;
142
endcase
143
endfunction
144
 
145
function IsJmp;
146
input [47:0] isn;
147
IsJmp = isn[`INSTRUCTION_OP]==`JMP;
148
endfunction
149
 
150
function IsCall;
151
input [47:0] isn;
152
IsCall = isn[`INSTRUCTION_OP]==`CALL;
153
endfunction
154
 
155
function IsRet;
156
input [47:0] isn;
157
IsRet = isn[`INSTRUCTION_OP]==`RET;
158
endfunction
159
 
160
function IsRTI;
161
input [47:0] isn;
162
IsRTI = isn[`INSTRUCTION_OP]==`R2 && isn[`INSTRUCTION_S2]==`RTI;
163
endfunction
164
 
165
function [3:0] fnInsLength;
166
input [47:0] ins;
167
case(ins[7:6])
168
2'd0:   fnInsLength = 4'd4;
169
2'd1:   fnInsLength = 4'd6;
170
default:        fnInsLength = 4'd2;
171
endcase
172
endfunction
173
 
174
 
175
wire [47:0] xinsn0;
176
wire [47:0] xinsn1;
177
 
178
FT64_iexpander ux1
179
(
180
        .cinstr(insn0[15:0]),
181
        .expand(xinsn0)
182
);
183
FT64_iexpander ux2
184
(
185
        .cinstr(insn1[15:0]),
186
        .expand(xinsn1)
187
);
188
 
189
 
190
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
191
// Table of decompressed instructions.
192
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
193
assign ack_o = cs_i & cyc_i & stb_i;
194
reg [47:0] DecompressTable [0:1023];
195
always @(posedge clk)
196
        if (cs_i & cyc_i & stb_i & we_i)
197
                DecompressTable[adr_i[11:2]] <= dat_i;
198
wire [47:0] expand0 = DecompressTable[insn0[15:6]];
199
wire [47:0] expand1 = DecompressTable[insn1[15:6]];
200
 
201
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
202
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
203
 
204
reg thread;
205
reg stompedRet;
206
reg ret0Counted, ret1Counted;
207
wire [AMSB:0] retpc0, retpc1;
208
 
209
reg did_branchback0;
210
reg did_branchback1;
211
 
212
assign predict_taken0 = (fetchbuf==1'b0) ? predict_takenA : predict_takenC;
213
assign predict_taken1 = (fetchbuf==1'b0) ? predict_takenB : predict_takenD;
214
 
215
reg [AMSB:0] branch_pcA;
216
reg [AMSB:0] branch_pcB;
217
reg [AMSB:0] branch_pcC;
218
reg [AMSB:0] branch_pcD;
219
 
220
always @*
221
case(fetchbufA_instr[`INSTRUCTION_OP])
222
`RET:           branch_pcA = retpc0;
223
`JMP,`CALL: branch_pcA = fetchbufA_instr[6] ? {fetchbufA_instr[39:8],1'b0} : {fetchbufA_pc[31:25],fetchbufA_instr[31:8],1'b0};
224
`R2:            branch_pcA = btgtA;     // RTI
225
`BRK,`JAL:      branch_pcA = btgtA;
226
default:        branch_pcA = fetchbufA_pc + {{20{fetchbufA_instr[31]}},fetchbufA_instr[31:21],1'b0} + fnInsLength(fetchbufA_instr);
227
endcase
228
 
229
always @*
230
case(fetchbufB_instr[`INSTRUCTION_OP])
231
`RET:           branch_pcB = retpc1;
232
`JMP,`CALL: branch_pcB = fetchbufB_instr[6] ? {fetchbufB_instr[39:8],1'b0} : {fetchbufB_pc[31:25],fetchbufB_instr[31:8],1'b0};
233
`R2:            branch_pcB = btgtB;     // RTI
234
`BRK,`JAL:      branch_pcB = btgtB;
235
default:        branch_pcB = fetchbufB_pc + {{20{fetchbufB_instr[31]}},fetchbufB_instr[31:21],1'b0} + fnInsLength(fetchbufB_instr);
236
endcase
237
 
238
always @*
239
case(fetchbufC_instr[`INSTRUCTION_OP])
240
`RET:           branch_pcC = retpc0;
241
`JMP,`CALL: branch_pcC = fetchbufC_instr[6] ? {fetchbufC_instr[39:8],1'b0} : {fetchbufC_pc[31:25],fetchbufC_instr[31:8],1'b0};
242
`R2:            branch_pcC = btgtC;     // RTI
243
`BRK,`JAL:      branch_pcC = btgtC;
244
default:        branch_pcC = fetchbufC_pc + {{20{fetchbufC_instr[31]}},fetchbufC_instr[31:21],1'b0} + fnInsLength(fetchbufC_instr);
245
endcase
246
 
247
always @*
248
case(fetchbufD_instr[`INSTRUCTION_OP])
249
`RET:           branch_pcD = retpc1;
250
`JMP,`CALL: branch_pcD = fetchbufD_instr[6] ? {fetchbufD_instr[39:8],1'b0} : {fetchbufD_pc[31:25],fetchbufD_instr[31:8],1'b0};
251
`R2:            branch_pcD = btgtD;     // RTI
252
`BRK,`JAL:      branch_pcD = btgtD;
253
default:        branch_pcD = fetchbufD_pc + {{20{fetchbufD_instr[31]}},fetchbufD_instr[31:21],1'b0} + fnInsLength(fetchbufD_instr);
254
endcase
255
 
256
wire take_branchA = ({fetchbufA_v, IsBranch(fetchbufA_instr), predict_takenA}  == {`VAL, `TRUE, `TRUE}) ||
257
                        ((IsRet(fetchbufA_instr)||IsJmp(fetchbufA_instr)||IsCall(fetchbufA_instr)||
258
                        IsRTI(fetchbufA_instr)|| fetchbufA_instr[`INSTRUCTION_OP]==`BRK || fetchbufA_instr[`INSTRUCTION_OP]==`JAL) &&
259
                        fetchbufA_v);
260
wire take_branchB = ({fetchbufB_v, IsBranch(fetchbufB_instr), predict_takenB}  == {`VAL, `TRUE, `TRUE}) ||
261
                        ((IsRet(fetchbufB_instr)|IsJmp(fetchbufB_instr)|IsCall(fetchbufB_instr) ||
262
                        IsRTI(fetchbufB_instr)|| fetchbufB_instr[`INSTRUCTION_OP]==`BRK || fetchbufB_instr[`INSTRUCTION_OP]==`JAL) &&
263
                        fetchbufB_v);
264
wire take_branchC = ({fetchbufC_v, IsBranch(fetchbufC_instr), predict_takenC}  == {`VAL, `TRUE, `TRUE}) ||
265
                        ((IsRet(fetchbufC_instr)|IsJmp(fetchbufC_instr)|IsCall(fetchbufC_instr) ||
266
                        IsRTI(fetchbufC_instr)|| fetchbufC_instr[`INSTRUCTION_OP]==`BRK || fetchbufC_instr[`INSTRUCTION_OP]==`JAL) &&
267
                        fetchbufC_v);
268
wire take_branchD = ({fetchbufD_v, IsBranch(fetchbufD_instr), predict_takenD}  == {`VAL, `TRUE, `TRUE}) ||
269
                        ((IsRet(fetchbufD_instr)|IsJmp(fetchbufD_instr)|IsCall(fetchbufD_instr) ||
270
                        IsRTI(fetchbufD_instr)|| fetchbufD_instr[`INSTRUCTION_OP]==`BRK || fetchbufD_instr[`INSTRUCTION_OP]==`JAL) &&
271
                        fetchbufD_v);
272
 
273
assign take_branch0 = fetchbuf==1'b0 ? take_branchA : take_branchC;
274
assign take_branch1 = fetchbuf==1'b0 ? take_branchB : take_branchD;
275
wire take_branch = take_branch0 || take_branch1;
276
/*
277
always @*
278
begin
279
        pc0 <= thread_en ? (fetchbuf ? pc0b : pc0a) : pc0a;
280
        pc1 <= thread_en ? (fetchbuf ? pc1b : pc1a) : pc1a;
281
end
282
*/
283
assign threadx = fetchbuf;
284
 
285 49 robfinch
`ifdef FCU_ENH
286 48 robfinch
FT64_RSB #(AMSB) ursb1
287
(
288
        .rst(rst),
289 49 robfinch
        .clk(fcu_clk),
290 48 robfinch
        .regLR(regLR),
291
        .queued1(queued1),
292
        .queued2(queued2),
293
        .fetchbuf0_v(fetchbuf0_v),
294
        .fetchbuf0_pc(fetchbuf0_pc),
295
        .fetchbuf0_instr(fetchbuf0_instr),
296
        .fetchbuf1_v(fetchbuf1_v),
297
        .fetchbuf1_pc(fetchbuf1_pc),
298
        .fetchbuf1_instr(fetchbuf1_instr),
299
        .stompedRets(stompedRets),
300
        .stompedRet(stompedRet),
301
        .pc(retpc0)
302
);
303
 
304
FT64_RSB #(AMSB) ursb2
305
(
306
        .rst(rst),
307 49 robfinch
        .clk(fcu_clk),
308 48 robfinch
        .regLR(regLR),
309
        .queued1(queued1),
310
        .queued2(1'b0),
311
        .fetchbuf0_v(fetchbuf1_v),
312
        .fetchbuf0_pc(fetchbuf1_pc),
313
        .fetchbuf0_instr(fetchbuf1_instr),
314
        .fetchbuf1_v(1'b0),
315
        .fetchbuf1_pc(32'h00000000),
316
        .fetchbuf1_instr(`NOP_INSN),
317
        .stompedRets(stompedRets[3:1]),
318
        .stompedRet(stompedRet),
319
        .pc(retpc1)
320
);
321 49 robfinch
`else
322
assign retpc0 = RSTPC;
323
assign retpc1 = RSTPC;
324
`endif
325 48 robfinch
 
326
wire peclk, neclk;
327
edge_det ued1 (.rst(rst), .clk(clk4x), .ce(1'b1), .i(clk), .pe(peclk), .ne(neclk), .ee());
328
 
329
always @(posedge clk)
330
if (rst) begin
331
        pc0 <= RSTPC;
332
`ifdef SUPPORT_SMT
333
  pc1 <= RSTPC;
334
`endif
335
        fetchbufA_v <= 0;
336
        fetchbufB_v <= 0;
337
        fetchbufC_v <= 0;
338
        fetchbufD_v <= 0;
339
        fetchbuf <= 0;
340 49 robfinch
        panic <= `PANIC_NONE;
341 48 robfinch
end
342
else begin
343
 
344
        did_branchback0 <= take_branch0;
345
        did_branchback1 <= take_branch1;
346
 
347
        stompedRet = FALSE;
348
 
349
        begin
350
 
351
        // On a branch miss with threading enabled all fectch buffers are
352
        // invalidated even though the data in the fetch buffer would be valid
353
        // for the thread that isn't in a branchmiss state. This is done to
354
        // keep things simple. For the thread that doesn't miss the current
355
        // data for the fetch buffer needs to be retrieved again, so the pc
356
        // for that thread is assigned the current fetchbuf pc.
357
        // For the thread that misses the pc is simply assigned the misspc.
358
        if (branchmiss) begin
359 49 robfinch
                $display("***********");
360
                $display("Branch miss");
361
                $display("***********");
362 48 robfinch
                if (branchmiss_thrd) begin
363
                        pc0 <= fetchbuf0_pc;
364
`ifdef SUPPORT_SMT
365
                        pc1 <= misspc;
366
`endif
367
                end
368
                else begin
369
                        pc0 <= misspc;
370
`ifdef SUPPORT_SMT
371
                        pc1 <= fetchbuf1_pc;
372
`endif
373
                end
374
                fetchbufA_v <= `INV;
375
                fetchbufB_v <= `INV;
376
                fetchbufC_v <= `INV;
377
                fetchbufD_v <= `INV;
378
                fetchbuf <= 1'b0;
379
             $display("********************");
380
             $display("********************");
381
             $display("********************");
382
             $display("Branch miss");
383
             $display("misspc=%h", misspc);
384
             $display("********************");
385
             $display("********************");
386
             $display("********************");
387
        end
388
        // Some of the testing for valid branch conditions has been removed. In real
389
        // hardware it isn't needed, and just increases the size of the core. It's
390
        // assumed that the hardware is working.
391
        // The risk is an error will occur during simulation and go missed.
392
        else if (take_branch) begin
393
 
394
            // update the fetchbuf valid bits as well as fetchbuf itself
395
            // ... this must be based on which things are backwards branches, how many things
396
            // will get enqueued (0, 1, or 2), and how old the instructions are
397
            if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v})
398
 
399
                4'b0000 : ;     // do nothing
400
//              4'b0001 : panic <= `PANIC_INVALIDFBSTATE;
401
//              4'b0010 : panic <= `PANIC_INVALIDFBSTATE;
402
//              4'b0011 : panic <= `PANIC_INVALIDFBSTATE;       // this looks like it might be screwy fetchbuf logic
403
 
404
                // because the first instruction has been enqueued, 
405
                // we must have noted this in the previous cycle.
406
                // therefore, pc0 and pc1 have to have been set appropriately ... so do a regular fetch
407
                // this looks like the following:
408
                //   cycle 0 - fetched a INSTR+BEQ, with fbB holding a branchback
409
                //   cycle 1 - enqueued fbA, stomped on fbB, stalled fetch + updated pc0/pc1
410
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufB_v appropriately
411
                4'b0100 :
412
                    begin
413
                            FetchCD();
414
                             fetchbufB_v <= !(queued1|queuedNop);       // if it can be queued, it will
415
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
416
                        end
417
 
418
                // Can occur with threading enabled
419
                4'b0101:
420
                        fetchbufB_v <= !(queued1|queuedNop);
421
 
422
//              4'b0101 : panic <= `PANIC_INVALIDFBSTATE;
423
//              4'b0110 : panic <= `PANIC_INVALIDFBSTATE;
424
 
425
                // this looks like the following:
426
                //   cycle 0 - fetched an INSTR+BEQ, with fbB holding a branchback
427
                //   cycle 1 - enqueued fbA, but not fbB, recognized branchback in fbB, stalled fetch + updated pc0/pc1
428
                //   cycle 2 - still could not enqueue fbB, but fetched from backwards target
429
                //   cycle 3 - where we are now ... update fetchbufB_v appropriately
430
                //
431
                // however -- if there are backwards branches in the latter two slots, it is more complex.
432
                // simple solution: leave it alone and wait until we are through with the first two slots.
433
                4'b0111 :
434
                        begin
435
                            fetchbufB_v <= !(queued1|queuedNop);        // if it can be queued, it will
436
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
437
                        end
438
 
439
                // this looks like the following:
440
                //   cycle 0 - fetched a BEQ+INSTR, with fbA holding a branchback
441
                //   cycle 1 - stomped on fbB, but could not enqueue fbA, stalled fetch + updated pc0/pc1
442
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufA_v appropriately
443
                4'b1000 :
444
                        begin
445
                            FetchCD();
446
                             fetchbufA_v <= !(queued1|queuedNop);       // if it can be queued, it will
447
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
448
                        end
449
 
450
//              4'b1001 : panic <= `PANIC_INVALIDFBSTATE;
451
//              4'b1010 : panic <= `PANIC_INVALIDFBSTATE;
452
 
453
                // this looks like the following:
454
                //   cycle 0 - fetched a BEQ+INSTR, with fbA holding a branchback
455
                //   cycle 1 - stomped on fbB, but could not enqueue fbA, stalled fetch + updated pc0/pc1
456
                //   cycle 2 - still could not enqueue fbA, but fetched from backwards target
457
                //   cycle 3 - where we are now ... set fetchbufA_v appropriately
458
                //
459
                // however -- if there are backwards branches in the latter two slots, it is more complex.
460
                // simple solution: leave it alone and wait until we are through with the first two slots.
461
                4'b1011 :
462
                        begin
463
                             fetchbufA_v <=!(queued1|queuedNop);        // if it can be queued, it will
464
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
465
                        end
466
 
467
                // if fbB has the branchback, can't immediately tell which of the following scenarios it is:
468
                //   cycle 0 - fetched a pair of instructions, one or both of which is a branchback
469
                //   cycle 1 - where we are now.  stomp, enqueue, and update pc0/pc1
470
                // or
471
                //   cycle 0 - fetched a INSTR+BEQ, with fbB holding a branchback
472
                //   cycle 1 - could not enqueue fbA or fbB, stalled fetch + updated pc0/pc1
473
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufX_v appropriately
474
                // if fbA has the branchback, then it is scenario 1.
475
                // if fbB has it: if pc0 == fbB_pc, then it is the former scenario, else it is the latter
476
                4'b1100 : begin
477
`ifdef SUPPORT_SMT
478
                                if (take_branchA && take_branchB) begin
479
                                        pc0 <= branch_pcA;
480
                                        pc1 <= branch_pcB;
481
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
482
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
483
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
484
                                end
485
                                else
486
`endif
487
                                if (take_branchA) begin
488
                                        pc0 <= branch_pcA;
489
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
490
`ifdef SUPPORT_SMT
491
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
492
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
493
`else
494
                                        fetchbufB_v <= `INV;
495
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
496
`endif
497
                                end
498
`ifdef SUPPORT_SMT
499
                                else if (take_branchB) begin
500
                                        pc1 <= branch_pcB;
501
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
502
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
503
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
504
                                end
505
`else
506
                                else begin
507
                                        if (did_branchback0) begin
508
                                                FetchCD();
509
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
510
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
511
`ifdef SUPPORT_SMT
512
                                                if ((queued2|queuedNop))   fetchbuf <= 1'b1;
513
`else
514
                                                fetchbuf <= fetchbuf + ((queued2|queuedNop));
515
`endif
516
                                        end
517
                                        else begin
518
                                                pc0 <= branch_pcB;
519
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
520
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
521
                                                if ((queued2|queuedNop))   fetchbuf <= 1'b1;
522
                                        end
523
                                end
524
`endif
525
                    end
526
 
527
//              4'b1101 : panic <= `PANIC_INVALIDFBSTATE;
528
//              4'b1110 : panic <= `PANIC_INVALIDFBSTATE;
529
 
530
                // this looks like the following:
531
                //   cycle 0 - fetched an INSTR+BEQ, with fbB holding a branchback
532
                //   cycle 1 - enqueued neither fbA nor fbB, recognized branchback in fbB, stalled fetch + updated pc0/pc1
533
                //   cycle 2 - still could not enqueue fbB, but fetched from backwards target
534
                //   cycle 3 - where we are now ... update fetchbufX_v appropriately
535
                //
536
                // however -- if there are backwards branches in the latter two slots, it is more complex.
537
                // simple solution: leave it alone and wait until we are through with the first two slots.
538
                4'b1111 :
539
                        begin
540
                             fetchbufA_v <= !(queued1|queuedNop);       // if it can be queued, it will
541
                             fetchbufB_v <= !(queued2|queuedNop);       // if it can be queued, it will
542
                              fetchbuf <= fetchbuf + (queued2|queuedNop);
543
                        end
544
        default:    ;
545
            endcase
546
            else case ({fetchbufC_v, fetchbufD_v, fetchbufA_v, fetchbufB_v})
547
 
548
                4'b0000 : ; // do nothing
549
//              4'b0001 : panic <= `PANIC_INVALIDFBSTATE;
550
//              4'b0010 : panic <= `PANIC_INVALIDFBSTATE;
551
//              4'b0011 : panic <= `PANIC_INVALIDFBSTATE;       // this looks like it might be screwy fetchbuf logic
552
 
553
                // because the first instruction has been enqueued, 
554
                // we must have noted this in the previous cycle.
555
                // therefore, pc0 and pc1 have to have been set appropriately ... so do a regular fetch
556
                // this looks like the following:
557
                //   cycle 0 - fetched a INSTR+BEQ, with fbD holding a branchback
558
                //   cycle 1 - enqueued fbC, stomped on fbD, stalled fetch + updated pc0/pc1
559
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufB_v appropriately
560
                4'b0100 :
561
                        begin
562
                            FetchAB();
563
                             fetchbufD_v <= !(queued1|queuedNop);       // if it can be queued, it will
564
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
565
                        end
566
 
567
                4'b0101:
568
                        fetchbufD_v <= !(queued1|queuedNop);
569
 
570
//              4'b0101 : panic <= `PANIC_INVALIDFBSTATE;
571
//              4'b0110 : panic <= `PANIC_INVALIDFBSTATE;
572
 
573
                // this looks like the following:
574
                //   cycle 0 - fetched an INSTR+BEQ, with fbD holding a branchback
575
                //   cycle 1 - enqueued fbC, but not fbD, recognized branchback in fbD, stalled fetch + updated pc0/pc1
576
                //   cycle 2 - still could not enqueue fbD, but fetched from backwards target
577
                //   cycle 3 - where we are now ... update fetchbufD_v appropriately
578
                //
579
                // however -- if there are backwards branches in the latter two slots, it is more complex.
580
                // simple solution: leave it alone and wait until we are through with the first two slots.
581
                4'b0111 :
582
                        begin
583
                             fetchbufD_v <= !(queued1|queuedNop);       // if it can be queued, it will
584
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
585
                        end
586
 
587
                // this looks like the following:
588
                //   cycle 0 - fetched a BEQ+INSTR, with fbC holding a branchback
589
                //   cycle 1 - stomped on fbD, but could not enqueue fbC, stalled fetch + updated pc0/pc1
590
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufC_v appropriately
591
                4'b1000 :
592
                        begin
593
                            FetchAB();
594
                             fetchbufC_v <= !(queued1|queuedNop);       // if it can be queued, it will
595
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
596
                        end
597
 
598
//              4'b1001 : panic <= `PANIC_INVALIDFBSTATE;
599
//              4'b1010 : panic <= `PANIC_INVALIDFBSTATE;
600
 
601
                // this looks like the following:
602
                //   cycle 0 - fetched a BEQ+INSTR, with fbC holding a branchback
603
                //   cycle 1 - stomped on fbD, but could not enqueue fbC, stalled fetch + updated pc0/pc1
604
                //   cycle 2 - still could not enqueue fbC, but fetched from backwards target
605
                //   cycle 3 - where we are now ... set fetchbufC_v appropriately
606
                //
607
                // however -- if there are backwards branches in the latter two slots, it is more complex.
608
                // simple solution: leave it alone and wait until we are through with the first two slots.
609
                4'b1011 :
610
                        begin
611
                             fetchbufC_v <= !(queued1|queuedNop);       // if it can be queued, it will
612
                              fetchbuf <= fetchbuf + (queued1|queuedNop);
613
                        end
614
 
615
                // if fbD has the branchback, can't immediately tell which of the following scenarios it is:
616
                //   cycle 0 - fetched a pair of instructions, one or both of which is a branchback
617
                //   cycle 1 - where we are now.  stomp, enqueue, and update pc0/pc1
618
                // or
619
                //   cycle 0 - fetched a INSTR+BEQ, with fbD holding a branchback
620
                //   cycle 1 - could not enqueue fbC or fbD, stalled fetch + updated pc0/pc1
621
                //   cycle 2 - where we are now ... fetch the two instructions & update fetchbufX_v appropriately
622
                // if fbC has the branchback, then it is scenario 1.
623
                // if fbD has it: if pc0 == fbB_pc, then it is the former scenario, else it is the latter
624
                4'b1100 : begin
625
`ifdef SUPPORT_SMT
626
                                if (take_branchC && take_branchD) begin
627
                                        pc0 <= branch_pcC;
628
                                        pc1 <= branch_pcD;
629
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
630
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
631
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
632
                                end
633
                                else
634
`endif
635
                                if (take_branchC) begin
636
                                        pc0 <= branch_pcC;
637
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
638
`ifdef SUPPORT_SMT
639
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
640
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
641
`else
642
                                        fetchbufD_v <= `INV;
643
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
644
`endif
645
                                end
646
`ifdef SUPPORT_SMT
647
                                else if (take_branchD) begin
648
                                        pc1 <= branch_pcD;
649
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
650
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
651
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
652
                                end
653
`else
654
                                else begin
655
                                        if (did_branchback1) begin
656
                                                FetchAB();
657
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
658
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
659
`ifdef SUPPORT_SMT
660
                                                if ((queued2|queuedNop))   fetchbuf <= 1'b1;
661
`else
662
                                                fetchbuf <= fetchbuf + ((queued2|queuedNop));
663
`endif
664
                                        end
665
                                        else begin
666
                                                pc0 <= branch_pcD;
667
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
668
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
669
                                                if ((queued2|queuedNop))   fetchbuf <= 1'b1;
670
                                        end
671
                                end
672
`endif
673
                        end
674
 
675
//              4'b1101 : panic <= `PANIC_INVALIDFBSTATE;
676
//              4'b1110 : panic <= `PANIC_INVALIDFBSTATE;
677
 
678
                // this looks like the following:
679
                //   cycle 0 - fetched an INSTR+BEQ, with fbD holding a branchback
680
                //   cycle 1 - enqueued neither fbC nor fbD, recognized branchback in fbD, stalled fetch + updated pc0/pc1
681
                //   cycle 2 - still could not enqueue fbD, but fetched from backwards target
682
                //   cycle 3 - where we are now ... update fetchbufX_v appropriately
683
                //
684
                // however -- if there are backwards branches in the latter two slots, it is more complex.
685
                // simple solution: leave it alone and wait until we are through with the first two slots.
686
                4'b1111 :
687
                        begin
688
                             fetchbufC_v <= !(queued1|queuedNop);       // if it can be queued, it will
689
                             fetchbufD_v <= !(queued2|queuedNop);       // if it can be queued, it will
690
                              fetchbuf <= fetchbuf + (queued2|queuedNop);
691
                        end
692
            default:   ;
693
            endcase
694
 
695
        end // if branchback
696
 
697
        else begin      // there is no branchback in the system
698
            //
699
            // update fetchbufX_v and fetchbuf ... relatively simple, as
700
            // there are no backwards branches in the mix
701
            if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, (queued1|queuedNop), (queued2|queuedNop)})
702
                4'b00_00 : ;    // do nothing
703
//              4'b00_01 : panic <= `PANIC_INVALIDIQSTATE;
704
                4'b00_10 : ;    // do nothing
705
                4'b00_11 : ;    // do nothing
706
                4'b01_00 : ;    // do nothing
707
//              4'b01_01 : panic <= `PANIC_INVALIDIQSTATE;
708
 
709
                4'b01_10,
710
                4'b01_11 : begin        // enqueue fbB and flip fetchbuf
711
                         fetchbufB_v <= `INV;
712
                          fetchbuf <= ~fetchbuf;
713
                    end
714
 
715
                4'b10_00 : ;    // do nothing
716
//              4'b10_01 : panic <= `PANIC_INVALIDIQSTATE;
717
 
718
                4'b10_10,
719
                4'b10_11 : begin        // enqueue fbA and flip fetchbuf
720
                         fetchbufA_v <= `INV;
721
                          fetchbuf <= ~fetchbuf;
722
                    end
723
 
724
                4'b11_00 : ;    // do nothing
725
//              4'b11_01 : panic <= `PANIC_INVALIDIQSTATE;
726
 
727
                4'b11_10 : begin        // enqueue fbA but leave fetchbuf
728
                         fetchbufA_v <= `INV;
729
                    end
730
 
731
                4'b11_11 : begin        // enqueue both and flip fetchbuf
732
                         fetchbufA_v <= `INV;
733
                         fetchbufB_v <= `INV;
734
                          fetchbuf <= ~fetchbuf;
735
                    end
736 49 robfinch
                default:  panic <= `PANIC_INVALIDIQSTATE;
737 48 robfinch
            endcase
738
            else case ({fetchbufC_v, fetchbufD_v, (queued1|queuedNop), (queued2|queuedNop)})
739
                4'b00_00 : ;    // do nothing
740
//              4'b00_01 : panic <= `PANIC_INVALIDIQSTATE;
741
                4'b00_10 : ;    // do nothing
742
                4'b00_11 : ;    // do nothing
743
                4'b01_00 : ;    // do nothing
744
//              4'b01_01 : panic <= `PANIC_INVALIDIQSTATE;
745
 
746
                4'b01_10,
747
                4'b01_11 : begin        // enqueue fbD and flip fetchbuf
748
                         fetchbufD_v <= `INV;
749
                          fetchbuf <= ~fetchbuf;
750
                    end
751
 
752
                4'b10_00 : ;    // do nothing
753
//              4'b10_01 : panic <= `PANIC_INVALIDIQSTATE;
754
 
755
                4'b10_10,
756
                4'b10_11 : begin        // enqueue fbC and flip fetchbuf
757
                         fetchbufC_v <= `INV;
758
                          fetchbuf <= ~fetchbuf;
759
                    end
760
 
761
                4'b11_00 : ;    // do nothing
762
//              4'b11_01 : panic <= `PANIC_INVALIDIQSTATE;
763
 
764
                4'b11_10 : begin        // enqueue fbC but leave fetchbuf
765
                         fetchbufC_v <= `INV;
766
                    end
767
 
768
                4'b11_11 : begin        // enqueue both and flip fetchbuf
769
                         fetchbufC_v <= `INV;
770
                         fetchbufD_v <= `INV;
771
                          fetchbuf <= ~fetchbuf;
772
                    end
773 49 robfinch
                default:  panic <= `PANIC_INVALIDIQSTATE;
774 48 robfinch
            endcase
775
            //
776
            // get data iff the fetch buffers are empty
777
            //
778
            if (fetchbufA_v == `INV && fetchbufB_v == `INV) begin
779
                FetchAB();
780
                // fetchbuf steering logic correction
781
                if (fetchbufC_v==`INV && fetchbufD_v==`INV && phit)
782
                      fetchbuf <= 1'b0;
783
            end
784
            else if (fetchbufC_v == `INV && fetchbufD_v == `INV)
785
                    FetchCD();
786
        end
787
    //
788
    // get data iff the fetch buffers are empty
789
    //
790
    if (fetchbufA_v == `INV && fetchbufB_v == `INV && fetchbufC_v==`INV && fetchbufD_v==`INV) begin
791
        FetchAB();
792
         fetchbuf <= 1'b0;
793
    end
794
        end
795
 
796
        // The fetchbuffer is invalidated at the end of a vector instruction
797
        // queue.
798
        if (nop_fetchbuf[0])  fetchbufA_v <= `INV;
799
        if (nop_fetchbuf[1])  fetchbufB_v <= `INV;
800
        if (nop_fetchbuf[2])  fetchbufC_v <= `INV;
801
        if (nop_fetchbuf[3])  fetchbufD_v <= `INV;
802
end
803
 
804
assign fetchbuf0_instr = (fetchbuf == 1'b0) ? fetchbufA_instr : fetchbufC_instr;
805
assign fetchbuf0_v     = (fetchbuf == 1'b0) ? fetchbufA_v     : fetchbufC_v    ;
806
assign fetchbuf0_pc    = (fetchbuf == 1'b0) ? fetchbufA_pc    : fetchbufC_pc   ;
807
assign fetchbuf1_instr = (fetchbuf == 1'b0) ? fetchbufB_instr : fetchbufD_instr;
808
assign fetchbuf1_v     = (fetchbuf == 1'b0) ? fetchbufB_v     : fetchbufD_v    ;
809
assign fetchbuf1_pc    = (fetchbuf == 1'b0) ? fetchbufB_pc    : fetchbufD_pc   ;
810
assign fetchbuf0_thrd  = 1'b0;
811
`ifdef SUPPORT_SMT
812
assign fetchbuf1_thrd  = 1'b1;
813
`else
814
assign fetchbuf1_thrd  = 1'b0;
815
`endif
816
 
817
`ifndef SUPPORT_SMT
818
always @*
819
        pc1 <= pc0 + fetchbuf0_insln;
820
`endif
821
 
822
always @*
823
begin
824
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
825
                fetchbuf0_insln <= fnInsLength(codebuf0);
826
        else
827
                fetchbuf0_insln <= fnInsLength(insn0);
828
end
829
 
830
always @*
831
begin
832
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
833
                fetchbuf1_insln <= fnInsLength(codebuf1);
834
        else
835
                fetchbuf1_insln <= fnInsLength(insn1);
836
end
837
 
838
reg [47:0] cinsn0, cinsn1;
839
 
840
always @*
841
begin
842
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
843
                cinsn0 <= codebuf0;
844
        else if (insn0[7])
845
                cinsn0 <= xinsn0;
846
        else
847
                cinsn0 <= insn0;
848
end
849
 
850
always @*
851
begin
852
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
853
                cinsn1 <= codebuf1;
854
        else if (insn1[7])
855
                cinsn1 <= xinsn1;
856
        else
857
                cinsn1 <= insn1;
858
end
859
 
860
task FetchA;
861
begin
862
        fetchbufA_instr <= cinsn0;
863
        fetchbufA_v <= `VAL;
864
        fetchbufA_pc <= pc0;
865
        if (phit && ~hirq)
866
`ifdef SUPPORT_SMT
867
                pc0 <= pc0 + fetchbuf0_insln;
868
`else
869 49 robfinch
        if (`WAYS > 1)
870 48 robfinch
                pc0 <= pc0 + fetchbuf0_insln + fetchbuf1_insln;
871 49 robfinch
        else
872
                pc0 <= pc0 + fetchbuf0_insln;
873 48 robfinch
`endif
874
end
875
endtask
876
 
877
task FetchB;
878
begin
879
        fetchbufB_instr <= cinsn1;
880 49 robfinch
        fetchbufB_v <= `WAYS > 1;
881
        fetchbufB_pc <= pc1;
882 48 robfinch
`ifdef SUPPORT_SMT
883
        if (phit)
884
                pc1 <= pc1 + fetchbuf1_insln;
885
`endif
886
end
887
endtask
888
 
889
 
890
task FetchAB;
891
begin
892
        FetchA();
893
        FetchB();
894
end
895
endtask
896
 
897
task FetchC;
898
begin
899
        fetchbufC_instr <= cinsn0;
900
        fetchbufC_v <= `VAL;
901
        fetchbufC_pc <= pc0;
902
        if (phit && ~hirq)
903
`ifdef SUPPORT_SMT
904
                pc0 <= pc0 + fetchbuf0_insln;
905
`else
906 49 robfinch
        if (`WAYS > 1)
907 48 robfinch
                pc0 <= pc0 + fetchbuf0_insln + fetchbuf1_insln;
908 49 robfinch
        else
909
                pc0 <= pc0 + fetchbuf0_insln;
910 48 robfinch
`endif
911
end
912
endtask
913
 
914
task FetchD;
915
begin
916
        fetchbufD_instr <= cinsn1;
917 49 robfinch
        fetchbufD_v <= `WAYS > 1;
918
        fetchbufD_pc <= pc1;
919 48 robfinch
`ifdef SUPPORT_SMT
920
        if (phit)
921
                pc1 <= pc1 + fetchbuf1_insln;
922
`endif
923
end
924
endtask
925
 
926
task FetchCD;
927
begin
928
        FetchC();
929
        FetchD();
930
end
931
endtask
932
 
933
endmodule
934
 

powered by: WebSVN 2.1.0

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