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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v7/] [rtl/] [twoway/] [FT64_fetchbuf.v] - Diff between revs 60 and 61

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 60 Rev 61
// ============================================================================
// ============================================================================
//        __
//        __
//   \\__/ o\    (C) 2017-2018  Robert Finch, Waterloo
//   \\__/ o\    (C) 2017-2018  Robert Finch, Waterloo
//    \  __ /    All rights reserved.
//    \  __ /    All rights reserved.
//     \/_//     robfinch<remove>@finitron.ca
//     \/_//     robfinch<remove>@finitron.ca
//       ||
//       ||
//
//
//      FT64_fetchbuf.v
//      FT64_fetchbuf.v
//
//
// This source file is free software: you can redistribute it and/or modify 
// This source file is free software: you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation, either version 3 of the License, or     
// by the Free Software Foundation, either version 3 of the License, or     
// (at your option) any later version.                                      
// (at your option) any later version.                                      
//                                                                          
//                                                                          
// This source file is distributed in the hope that it will be useful,      
// This source file is distributed in the hope that it will be useful,      
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
// GNU General Public License for more details.                             
// GNU General Public License for more details.                             
//                                                                          
//                                                                          
// You should have received a copy of the GNU General Public License        
// You should have received a copy of the GNU General Public License        
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
//
//
// ============================================================================
// ============================================================================
//
//
`include "FT64_config.vh"
`include "FT64_config.vh"
`include "FT64_defines.vh"
`include "FT64_defines.vh"
 
 
// FETCH
// FETCH
//
//
// fetch exactly two instructions from memory into the fetch buffer
// fetch exactly two instructions from memory into the fetch buffer
// unless either one of the buffers is still full, in which case we
// unless either one of the buffers is still full, in which case we
// do nothing (kinda like alpha approach)
// do nothing (kinda like alpha approach)
// Like to turn this into an independent module at some point.
// Like to turn this into an independent module at some point.
//
//
module FT64_fetchbuf(rst, clk4x, clk, fcu_clk,
module FT64_fetchbuf(rst, clk4x, clk, fcu_clk,
        cs_i, cyc_i, stb_i, ack_o, we_i, adr_i, dat_i,
        cs_i, cyc_i, stb_i, ack_o, we_i, adr_i, dat_i,
        cmpgrp,
        cmpgrp,
        freezePC, thread_en,
        freezePC, thread_en,
        regLR,
        regLR,
    insn0, insn1, phit,
    insn0, insn1, phit,
    threadx,
    threadx,
    branchmiss, misspc, branchmiss_thrd, predict_taken0, predict_taken1,
    branchmiss, misspc, branchmiss_thrd, predict_taken0, predict_taken1,
    predict_takenA, predict_takenB, predict_takenC, predict_takenD,
    predict_takenA, predict_takenB, predict_takenC, predict_takenD,
    queued1, queued2, queuedNop,
    queued1, queued2, queuedNop,
    pc0, pc1, fetchbuf, fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v,
    pc0, pc1, fetchbuf, fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v,
    fetchbufA_instr, fetchbufA_pc,
    fetchbufA_instr, fetchbufA_pc,
    fetchbufB_instr, fetchbufB_pc,
    fetchbufB_instr, fetchbufB_pc,
    fetchbufC_instr, fetchbufC_pc,
    fetchbufC_instr, fetchbufC_pc,
    fetchbufD_instr, fetchbufD_pc,
    fetchbufD_instr, fetchbufD_pc,
    fetchbuf0_instr, fetchbuf1_instr, fetchbuf0_insln, fetchbuf1_insln,
    fetchbuf0_instr, fetchbuf1_instr, fetchbuf0_insln, fetchbuf1_insln,
    fetchbuf0_thrd, fetchbuf1_thrd,
    fetchbuf0_thrd, fetchbuf1_thrd,
    fetchbuf0_pc, fetchbuf1_pc,
    fetchbuf0_pc, fetchbuf1_pc,
    fetchbuf0_v, fetchbuf1_v,
    fetchbuf0_v, fetchbuf1_v,
    codebuf0, codebuf1,
    codebuf0, codebuf1,
    btgtA, btgtB, btgtC, btgtD,
    btgtA, btgtB, btgtC, btgtD,
    nop_fetchbuf,
    nop_fetchbuf,
    take_branch0, take_branch1,
    take_branch0, take_branch1,
    stompedRets,
    stompedRets,
 
    pred_on,
    panic
    panic
);
);
parameter AMSB = `AMSB;
parameter AMSB = `AMSB;
parameter RSTPC = 64'hFFFC0100;
parameter RSTPC = 64'hFFFC0100;
parameter TRUE = 1'b1;
parameter TRUE = 1'b1;
parameter FALSE = 1'b0;
parameter FALSE = 1'b0;
input rst;
input rst;
input clk4x;
input clk4x;
input clk;
input clk;
input fcu_clk;
input fcu_clk;
input cs_i;
input cs_i;
input cyc_i;
input cyc_i;
input stb_i;
input stb_i;
output ack_o;
output ack_o;
input we_i;
input we_i;
input [15:0] adr_i;
input [15:0] adr_i;
input [47:0] dat_i;
input [47:0] dat_i;
input [2:0] cmpgrp;
input [2:0] cmpgrp;
input freezePC;
input freezePC;
input thread_en;
input thread_en;
input [4:0] regLR;
input [4:0] regLR;
input [47:0] insn0;
input [47:0] insn0;
input [47:0] insn1;
input [47:0] insn1;
input phit;
input phit;
output threadx;
output threadx;
input branchmiss;
input branchmiss;
input [AMSB:0] misspc;
input [AMSB:0] misspc;
input branchmiss_thrd;
input branchmiss_thrd;
output predict_taken0;
output predict_taken0;
output predict_taken1;
output predict_taken1;
input predict_takenA;
input predict_takenA;
input predict_takenB;
input predict_takenB;
input predict_takenC;
input predict_takenC;
input predict_takenD;
input predict_takenD;
input queued1;
input queued1;
input queued2;
input queued2;
input queuedNop;
input queuedNop;
output reg [AMSB:0] pc0;
output reg [AMSB:0] pc0;
output reg [AMSB:0] pc1;
output reg [AMSB:0] pc1;
output reg fetchbuf;
output reg fetchbuf;
output reg fetchbufA_v;
output reg fetchbufA_v;
output reg fetchbufB_v;
output reg fetchbufB_v;
output reg fetchbufC_v;
output reg fetchbufC_v;
output reg fetchbufD_v;
output reg fetchbufD_v;
output fetchbuf0_thrd;
output fetchbuf0_thrd;
output fetchbuf1_thrd;
output fetchbuf1_thrd;
output reg [47:0] fetchbufA_instr;
output reg [47:0] fetchbufA_instr;
output reg [47:0] fetchbufB_instr;
output reg [47:0] fetchbufB_instr;
output reg [47:0] fetchbufC_instr;
output reg [47:0] fetchbufC_instr;
output reg [47:0] fetchbufD_instr;
output reg [47:0] fetchbufD_instr;
output reg [AMSB:0] fetchbufA_pc;
output reg [AMSB:0] fetchbufA_pc;
output reg [AMSB:0] fetchbufB_pc;
output reg [AMSB:0] fetchbufB_pc;
output reg [AMSB:0] fetchbufC_pc;
output reg [AMSB:0] fetchbufC_pc;
output reg [AMSB:0] fetchbufD_pc;
output reg [AMSB:0] fetchbufD_pc;
output [47:0] fetchbuf0_instr;
output [47:0] fetchbuf0_instr;
output [47:0] fetchbuf1_instr;
output [47:0] fetchbuf1_instr;
output [AMSB:0] fetchbuf0_pc;
output [AMSB:0] fetchbuf0_pc;
output [AMSB:0] fetchbuf1_pc;
output [AMSB:0] fetchbuf1_pc;
output [2:0] fetchbuf0_insln;
output [2:0] fetchbuf0_insln;
output [2:0] fetchbuf1_insln;
output [2:0] fetchbuf1_insln;
output fetchbuf0_v;
output fetchbuf0_v;
output fetchbuf1_v;
output fetchbuf1_v;
input [47:0] codebuf0;
input [47:0] codebuf0;
input [47:0] codebuf1;
input [47:0] codebuf1;
input [AMSB:0] btgtA;
input [AMSB:0] btgtA;
input [AMSB:0] btgtB;
input [AMSB:0] btgtB;
input [AMSB:0] btgtC;
input [AMSB:0] btgtC;
input [AMSB:0] btgtD;
input [AMSB:0] btgtD;
input [3:0] nop_fetchbuf;
input [3:0] nop_fetchbuf;
output take_branch0;
output take_branch0;
output take_branch1;
output take_branch1;
input [3:0] stompedRets;
input [3:0] stompedRets;
 
input pred_on;
output reg [3:0] panic;
output reg [3:0] panic;
integer n;
integer n;
 
 
//`include "FT64_decode.vh"
//`include "FT64_decode.vh"
 
 
function IsBranch;
function IsBranch;
input [47:0] isn;
input [47:0] isn;
casex(isn[`INSTRUCTION_OP])
casex(isn[`INSTRUCTION_OP])
`Bcc:   IsBranch = TRUE;
`Bcc:   IsBranch = TRUE;
`BBc:   IsBranch = TRUE;
`BBc:   IsBranch = TRUE;
`BEQI:  IsBranch = TRUE;
`BEQI:  IsBranch = TRUE;
`BCHK:  IsBranch = TRUE;
`BCHK:  IsBranch = TRUE;
default: IsBranch = FALSE;
default: IsBranch = FALSE;
endcase
endcase
endfunction
endfunction
 
 
function IsJAL;
function IsJAL;
input [47:0] isn;
input [47:0] isn;
IsJAL = isn[`INSTRUCTION_OP]==`JAL;
IsJAL = isn[`INSTRUCTION_OP]==`JAL;
endfunction
endfunction
 
 
function IsJmp;
function IsJmp;
input [47:0] isn;
input [47:0] isn;
IsJmp = isn[`INSTRUCTION_OP]==`JMP;
IsJmp = isn[`INSTRUCTION_OP]==`JMP;
endfunction
endfunction
 
 
function IsCall;
function IsCall;
input [47:0] isn;
input [47:0] isn;
IsCall = isn[`INSTRUCTION_OP]==`CALL;
IsCall = isn[`INSTRUCTION_OP]==`CALL;
endfunction
endfunction
 
 
function IsRet;
function IsRet;
input [47:0] isn;
input [47:0] isn;
IsRet = isn[`INSTRUCTION_OP]==`RET;
IsRet = isn[`INSTRUCTION_OP]==`RET;
endfunction
endfunction
 
 
function IsBrk;
function IsBrk;
input [47:0] isn;
input [47:0] isn;
IsBrk = isn[`INSTRUCTION_OP]==`BRK;
IsBrk = isn[`INSTRUCTION_OP]==`BRK;
endfunction
endfunction
 
 
function IsRTI;
function IsRTI;
input [47:0] isn;
input [47:0] isn;
IsRTI = isn[`INSTRUCTION_OP]==`R2 && isn[`INSTRUCTION_S2]==`RTI;
IsRTI = isn[`INSTRUCTION_OP]==`R2 && isn[`INSTRUCTION_S2]==`RTI;
endfunction
endfunction
 
 
 
 
function [2:0] fnInsLength;
function [2:0] fnInsLength;
input [47:0] ins;
input [47:0] ins;
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
if (ins[`INSTRUCTION_OP]==`CMPRSSD)
if (ins[`INSTRUCTION_OP]==`CMPRSSD)
        fnInsLength = 3'd2;
        fnInsLength = 3'd2;
else
else
`endif
`endif
        case(ins[7:6])
        case(ins[7:6])
        2'd0:   fnInsLength = 3'd4;
        2'd0:   fnInsLength = 3'd4|pred_on;
        2'd1:   fnInsLength = 3'd6;
        2'd1:   fnInsLength = 3'd6|pred_on;
        default:        fnInsLength = 3'd2;
        default:        fnInsLength = 3'd2|pred_on;
        endcase
        endcase
endfunction
endfunction
 
 
wire [2:0] fetchbufA_inslen;
wire [2:0] fetchbufA_inslen;
wire [2:0] fetchbufB_inslen;
wire [2:0] fetchbufB_inslen;
wire [2:0] fetchbufC_inslen;
wire [2:0] fetchbufC_inslen;
wire [2:0] fetchbufD_inslen;
wire [2:0] fetchbufD_inslen;
FT64_InsLength uilA (fetchbufA_instr, fetchbufA_inslen);
FT64_InsLength uilA (fetchbufA_instr, fetchbufA_inslen, pred_on);
FT64_InsLength uilB (fetchbufB_instr, fetchbufB_inslen);
FT64_InsLength uilB (fetchbufB_instr, fetchbufB_inslen, pred_on);
FT64_InsLength uilC (fetchbufC_instr, fetchbufC_inslen);
FT64_InsLength uilC (fetchbufC_instr, fetchbufC_inslen, pred_on);
FT64_InsLength uilD (fetchbufD_instr, fetchbufD_inslen);
FT64_InsLength uilD (fetchbufD_instr, fetchbufD_inslen, pred_on);
 
 
wire [47:0] xinsn0;
wire [47:0] xinsn0;
wire [47:0] xinsn1;
wire [47:0] xinsn1;
 
 
FT64_iexpander ux1
FT64_iexpander ux1
(
(
        .cinstr(insn0[15:0]),
        .cinstr(insn0[15:0]),
        .expand(xinsn0)
        .expand(xinsn0)
);
);
FT64_iexpander ux2
FT64_iexpander ux2
(
(
        .cinstr(insn1[15:0]),
        .cinstr(insn1[15:0]),
        .expand(xinsn1)
        .expand(xinsn1)
);
);
 
 
 
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Table of decompressed instructions.
// Table of decompressed instructions.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
assign ack_o = cs_i & cyc_i & stb_i;
assign ack_o = cs_i & cyc_i & stb_i;
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
reg [47:0] DecompressTable [0:2047];
reg [47:0] DecompressTable [0:2047];
always @(posedge clk)
always @(posedge clk)
        if (cs_i & cyc_i & stb_i & we_i)
        if (cs_i & cyc_i & stb_i & we_i)
                DecompressTable[adr_i[12:3]] <= dat_i[47:0];
                DecompressTable[adr_i[12:3]] <= dat_i[47:0];
wire [47:0] expand0 = DecompressTable[{cmpgrp,insn0[15:8]}];
wire [47:0] expand0 = DecompressTable[{cmpgrp,insn0[15:8]}];
wire [47:0] expand1 = DecompressTable[{cmpgrp,insn1[15:8]}];
wire [47:0] expand1 = DecompressTable[{cmpgrp,insn1[15:8]}];
`endif
`endif
 
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
reg thread;
reg thread;
reg stompedRet;
reg stompedRet;
reg ret0Counted, ret1Counted;
reg ret0Counted, ret1Counted;
wire [AMSB:0] retpc0, retpc1;
wire [AMSB:0] retpc0, retpc1;
 
 
reg did_branchback0;
reg did_branchback0;
reg did_branchback1;
reg did_branchback1;
 
 
assign predict_taken0 = (fetchbuf==1'b0) ? predict_takenA : predict_takenC;
assign predict_taken0 = (fetchbuf==1'b0) ? predict_takenA : predict_takenC;
assign predict_taken1 = (fetchbuf==1'b0) ? predict_takenB : predict_takenD;
assign predict_taken1 = (fetchbuf==1'b0) ? predict_takenB : predict_takenD;
 
 
reg [AMSB:0] branch_pcA;
reg [AMSB:0] branch_pcA;
reg [AMSB:0] branch_pcB;
reg [AMSB:0] branch_pcB;
reg [AMSB:0] branch_pcC;
reg [AMSB:0] branch_pcC;
reg [AMSB:0] branch_pcD;
reg [AMSB:0] branch_pcD;
 
 
always @*
always @*
case(fetchbufA_instr[`INSTRUCTION_OP])
case(fetchbufA_instr[`INSTRUCTION_OP])
`RET:           branch_pcA = retpc0;
`RET:           branch_pcA = retpc0;
`JMP,`CALL: branch_pcA = fetchbufA_instr[6] ? {fetchbufA_instr[39:8],1'b0} : {fetchbufA_pc[31:25],fetchbufA_instr[31:8],1'b0};
`JMP,`CALL: branch_pcA = fetchbufA_instr[6] ? {fetchbufA_instr[39:8],1'b0} : {fetchbufA_pc[31:25],fetchbufA_instr[31:8],1'b0};
`R2:            branch_pcA = btgtA;     // RTI
`R2:            branch_pcA = btgtA;     // RTI
`BRK,`JAL:      branch_pcA = btgtA;
`BRK,`JAL:      branch_pcA = btgtA;
default:
default:
        begin
        begin
        branch_pcA[31:8] = fetchbufA_pc[31:8] +
        branch_pcA[31:8] = fetchbufA_pc[31:8] +
                (fetchbufA_instr[7:6]==2'b01 ? {{4{fetchbufA_instr[47]}},fetchbufA_instr[47:28]} : {{20{fetchbufA_instr[31]}},fetchbufA_instr[31:28]});
                (fetchbufA_instr[7:6]==2'b01 ? {{4{fetchbufA_instr[47]}},fetchbufA_instr[47:28]} : {{20{fetchbufA_instr[31]}},fetchbufA_instr[31:28]});
        branch_pcA[7:0] = {fetchbufA_instr[27:23],fetchbufA_instr[17:16],1'b0};
        branch_pcA[7:0] = {fetchbufA_instr[27:23],fetchbufA_instr[17:16],1'b0};
        branch_pcA[63:32] = fetchbufA_pc[63:32];
        branch_pcA[63:32] = fetchbufA_pc[63:32];
        end
        end
endcase
endcase
 
 
always @*
always @*
case(fetchbufB_instr[`INSTRUCTION_OP])
case(fetchbufB_instr[`INSTRUCTION_OP])
`RET:           branch_pcB = retpc1;
`RET:           branch_pcB = retpc1;
`JMP,`CALL: branch_pcB = fetchbufB_instr[6] ? {fetchbufB_instr[39:8],1'b0} : {fetchbufB_pc[31:25],fetchbufB_instr[31:8],1'b0};
`JMP,`CALL: branch_pcB = fetchbufB_instr[6] ? {fetchbufB_instr[39:8],1'b0} : {fetchbufB_pc[31:25],fetchbufB_instr[31:8],1'b0};
`R2:            branch_pcB = btgtB;     // RTI
`R2:            branch_pcB = btgtB;     // RTI
`BRK,`JAL:      branch_pcB = btgtB;
`BRK,`JAL:      branch_pcB = btgtB;
default:
default:
        begin
        begin
        branch_pcB[31:8] = fetchbufB_pc[31:8] +
        branch_pcB[31:8] = fetchbufB_pc[31:8] +
                (fetchbufB_instr[7:6]==2'b01 ? {{4{fetchbufB_instr[47]}},fetchbufB_instr[47:28]} : {{20{fetchbufB_instr[31]}},fetchbufB_instr[31:28]});
                (fetchbufB_instr[7:6]==2'b01 ? {{4{fetchbufB_instr[47]}},fetchbufB_instr[47:28]} : {{20{fetchbufB_instr[31]}},fetchbufB_instr[31:28]});
        branch_pcB[7:0] = {fetchbufB_instr[27:23],fetchbufB_instr[17:16],1'b0};
        branch_pcB[7:0] = {fetchbufB_instr[27:23],fetchbufB_instr[17:16],1'b0};
        branch_pcB[63:32] = fetchbufB_pc[63:32];
        branch_pcB[63:32] = fetchbufB_pc[63:32];
        end
        end
endcase
endcase
 
 
always @*
always @*
case(fetchbufC_instr[`INSTRUCTION_OP])
case(fetchbufC_instr[`INSTRUCTION_OP])
`RET:           branch_pcC = retpc0;
`RET:           branch_pcC = retpc0;
`JMP,`CALL: branch_pcC = fetchbufC_instr[6] ? {fetchbufC_instr[39:8],1'b0} : {fetchbufC_pc[31:25],fetchbufC_instr[31:8],1'b0};
`JMP,`CALL: branch_pcC = fetchbufC_instr[6] ? {fetchbufC_instr[39:8],1'b0} : {fetchbufC_pc[31:25],fetchbufC_instr[31:8],1'b0};
`R2:            branch_pcC = btgtC;     // RTI
`R2:            branch_pcC = btgtC;     // RTI
`BRK,`JAL:      branch_pcC = btgtC;
`BRK,`JAL:      branch_pcC = btgtC;
default:
default:
        begin
        begin
        branch_pcC[31:8] = fetchbufC_pc[31:8] +
        branch_pcC[31:8] = fetchbufC_pc[31:8] +
                (fetchbufC_instr[7:6]==2'b01 ? {{4{fetchbufC_instr[47]}},fetchbufC_instr[47:28]} : {{20{fetchbufC_instr[31]}},fetchbufC_instr[31:28]});
                (fetchbufC_instr[7:6]==2'b01 ? {{4{fetchbufC_instr[47]}},fetchbufC_instr[47:28]} : {{20{fetchbufC_instr[31]}},fetchbufC_instr[31:28]});
        branch_pcC[7:0] = {fetchbufC_instr[27:23],fetchbufC_instr[17:16],1'b0};
        branch_pcC[7:0] = {fetchbufC_instr[27:23],fetchbufC_instr[17:16],1'b0};
        branch_pcC[63:32] = fetchbufC_pc[63:32];
        branch_pcC[63:32] = fetchbufC_pc[63:32];
        end
        end
endcase
endcase
 
 
always @*
always @*
case(fetchbufD_instr[`INSTRUCTION_OP])
case(fetchbufD_instr[`INSTRUCTION_OP])
`RET:           branch_pcD = retpc1;
`RET:           branch_pcD = retpc1;
`JMP,`CALL: branch_pcD = fetchbufD_instr[6] ? {fetchbufD_instr[39:8],1'b0} : {fetchbufD_pc[31:25],fetchbufD_instr[31:8],1'b0};
`JMP,`CALL: branch_pcD = fetchbufD_instr[6] ? {fetchbufD_instr[39:8],1'b0} : {fetchbufD_pc[31:25],fetchbufD_instr[31:8],1'b0};
`R2:            branch_pcD = btgtD;     // RTI
`R2:            branch_pcD = btgtD;     // RTI
`BRK,`JAL:      branch_pcD = btgtD;
`BRK,`JAL:      branch_pcD = btgtD;
default:
default:
        begin
        begin
        branch_pcD[31:8] = fetchbufD_pc[31:8] +
        branch_pcD[31:8] = fetchbufD_pc[31:8] +
                (fetchbufD_instr[7:6]==2'b01 ? {{4{fetchbufD_instr[47]}},fetchbufD_instr[47:28]} : {{20{fetchbufD_instr[31]}},fetchbufD_instr[31:28]});
                (fetchbufD_instr[7:6]==2'b01 ? {{4{fetchbufD_instr[47]}},fetchbufD_instr[47:28]} : {{20{fetchbufD_instr[31]}},fetchbufD_instr[31:28]});
        branch_pcD[7:0] = {fetchbufD_instr[27:23],fetchbufD_instr[17:16],1'b0};
        branch_pcD[7:0] = {fetchbufD_instr[27:23],fetchbufD_instr[17:16],1'b0};
        branch_pcD[63:32] = fetchbufD_pc[63:32];
        branch_pcD[63:32] = fetchbufD_pc[63:32];
        end
        end
endcase
endcase
 
 
wire take_branchA = ({fetchbufA_v, IsBranch(fetchbufA_instr), predict_takenA}  == {`VAL, `TRUE, `TRUE}) ||
wire take_branchA = ({fetchbufA_v, IsBranch(fetchbufA_instr), predict_takenA}  == {`VAL, `TRUE, `TRUE}) ||
                        ((IsRet(fetchbufA_instr)||IsJmp(fetchbufA_instr)||IsCall(fetchbufA_instr)||
                        ((IsRet(fetchbufA_instr)||IsJmp(fetchbufA_instr)||IsCall(fetchbufA_instr)||
                        IsRTI(fetchbufA_instr)|| IsBrk(fetchbufA_instr) || IsJAL(fetchbufA_instr)) &&
                        IsRTI(fetchbufA_instr)|| IsBrk(fetchbufA_instr) || IsJAL(fetchbufA_instr)) &&
                        fetchbufA_v);
                        fetchbufA_v);
wire take_branchB = ({fetchbufB_v, IsBranch(fetchbufB_instr), predict_takenB}  == {`VAL, `TRUE, `TRUE}) ||
wire take_branchB = ({fetchbufB_v, IsBranch(fetchbufB_instr), predict_takenB}  == {`VAL, `TRUE, `TRUE}) ||
                        ((IsRet(fetchbufB_instr)|IsJmp(fetchbufB_instr)|IsCall(fetchbufB_instr) ||
                        ((IsRet(fetchbufB_instr)|IsJmp(fetchbufB_instr)|IsCall(fetchbufB_instr) ||
                        IsRTI(fetchbufB_instr)|| IsBrk(fetchbufB_instr) || IsJAL(fetchbufB_instr)) &&
                        IsRTI(fetchbufB_instr)|| IsBrk(fetchbufB_instr) || IsJAL(fetchbufB_instr)) &&
                        fetchbufB_v);
                        fetchbufB_v);
wire take_branchC = ({fetchbufC_v, IsBranch(fetchbufC_instr), predict_takenC}  == {`VAL, `TRUE, `TRUE}) ||
wire take_branchC = ({fetchbufC_v, IsBranch(fetchbufC_instr), predict_takenC}  == {`VAL, `TRUE, `TRUE}) ||
                        ((IsRet(fetchbufC_instr)|IsJmp(fetchbufC_instr)|IsCall(fetchbufC_instr) ||
                        ((IsRet(fetchbufC_instr)|IsJmp(fetchbufC_instr)|IsCall(fetchbufC_instr) ||
                        IsRTI(fetchbufC_instr)|| IsBrk(fetchbufC_instr) || IsJAL(fetchbufC_instr)) &&
                        IsRTI(fetchbufC_instr)|| IsBrk(fetchbufC_instr) || IsJAL(fetchbufC_instr)) &&
                        fetchbufC_v);
                        fetchbufC_v);
wire take_branchD = ({fetchbufD_v, IsBranch(fetchbufD_instr), predict_takenD}  == {`VAL, `TRUE, `TRUE}) ||
wire take_branchD = ({fetchbufD_v, IsBranch(fetchbufD_instr), predict_takenD}  == {`VAL, `TRUE, `TRUE}) ||
                        ((IsRet(fetchbufD_instr)|IsJmp(fetchbufD_instr)|IsCall(fetchbufD_instr) ||
                        ((IsRet(fetchbufD_instr)|IsJmp(fetchbufD_instr)|IsCall(fetchbufD_instr) ||
                        IsRTI(fetchbufD_instr)|| IsBrk(fetchbufD_instr) || IsJAL(fetchbufD_instr)) &&
                        IsRTI(fetchbufD_instr)|| IsBrk(fetchbufD_instr) || IsJAL(fetchbufD_instr)) &&
                        fetchbufD_v);
                        fetchbufD_v);
 
 
assign take_branch0 = fetchbuf==1'b0 ? take_branchA : take_branchC;
assign take_branch0 = fetchbuf==1'b0 ? take_branchA : take_branchC;
assign take_branch1 = fetchbuf==1'b0 ? take_branchB : take_branchD;
assign take_branch1 = fetchbuf==1'b0 ? take_branchB : take_branchD;
wire take_branch = take_branch0 || take_branch1;
wire take_branch = take_branch0 || take_branch1;
/*
/*
always @*
always @*
begin
begin
        pc0 <= thread_en ? (fetchbuf ? pc0b : pc0a) : pc0a;
        pc0 <= thread_en ? (fetchbuf ? pc0b : pc0a) : pc0a;
        pc1 <= thread_en ? (fetchbuf ? pc1b : pc1a) : pc1a;
        pc1 <= thread_en ? (fetchbuf ? pc1b : pc1a) : pc1a;
end
end
*/
*/
assign threadx = fetchbuf;
assign threadx = fetchbuf;
 
 
`ifdef FCU_ENH
`ifdef FCU_ENH
FT64_RSB #(AMSB) ursb1
FT64_RSB #(AMSB) ursb1
(
(
        .rst(rst),
        .rst(rst),
        .clk(fcu_clk),
        .clk(fcu_clk),
        .regLR(regLR),
        .regLR(regLR),
        .queued1(queued1),
        .queued1(queued1),
        .queued2(queued2),
        .queued2(queued2),
        .fetchbuf0_v(fetchbuf0_v),
        .fetchbuf0_v(fetchbuf0_v),
        .fetchbuf0_pc(fetchbuf0_pc),
        .fetchbuf0_pc(fetchbuf0_pc),
        .fetchbuf0_instr(fetchbuf0_instr),
        .fetchbuf0_instr(fetchbuf0_instr),
        .fetchbuf1_v(fetchbuf1_v),
        .fetchbuf1_v(fetchbuf1_v),
        .fetchbuf1_pc(fetchbuf1_pc),
        .fetchbuf1_pc(fetchbuf1_pc),
        .fetchbuf1_instr(fetchbuf1_instr),
        .fetchbuf1_instr(fetchbuf1_instr),
        .stompedRets(stompedRets),
        .stompedRets(stompedRets),
        .stompedRet(stompedRet),
        .stompedRet(stompedRet),
        .pc(retpc0)
        .pc(retpc0)
);
);
 
 
FT64_RSB #(AMSB) ursb2
FT64_RSB #(AMSB) ursb2
(
(
        .rst(rst),
        .rst(rst),
        .clk(fcu_clk),
        .clk(fcu_clk),
        .regLR(regLR),
        .regLR(regLR),
        .queued1(queued1),
        .queued1(queued1),
        .queued2(1'b0),
        .queued2(1'b0),
        .fetchbuf0_v(fetchbuf1_v),
        .fetchbuf0_v(fetchbuf1_v),
        .fetchbuf0_pc(fetchbuf1_pc),
        .fetchbuf0_pc(fetchbuf1_pc),
        .fetchbuf0_instr(fetchbuf1_instr),
        .fetchbuf0_instr(fetchbuf1_instr),
        .fetchbuf1_v(1'b0),
        .fetchbuf1_v(1'b0),
        .fetchbuf1_pc(32'h00000000),
        .fetchbuf1_pc(32'h00000000),
        .fetchbuf1_instr(`NOP_INSN),
        .fetchbuf1_instr(`NOP_INSN),
        .stompedRets(stompedRets[3:1]),
        .stompedRets(stompedRets[3:1]),
        .stompedRet(stompedRet),
        .stompedRet(stompedRet),
        .pc(retpc1)
        .pc(retpc1)
);
);
`else
`else
assign retpc0 = RSTPC;
assign retpc0 = RSTPC;
assign retpc1 = RSTPC;
assign retpc1 = RSTPC;
`endif
`endif
 
 
wire peclk, neclk;
wire peclk, neclk;
edge_det ued1 (.rst(rst), .clk(clk4x), .ce(1'b1), .i(clk), .pe(peclk), .ne(neclk), .ee());
edge_det ued1 (.rst(rst), .clk(clk4x), .ce(1'b1), .i(clk), .pe(peclk), .ne(neclk), .ee());
 
 
always @(posedge clk)
always @(posedge clk)
if (rst) begin
if (rst) begin
        pc0 <= RSTPC;
        pc0 <= RSTPC;
  pc1 <= RSTPC;
  pc1 <= RSTPC;
        fetchbufA_v <= 0;
        fetchbufA_v <= 0;
        fetchbufB_v <= 0;
        fetchbufB_v <= 0;
        fetchbufC_v <= 0;
        fetchbufC_v <= 0;
        fetchbufD_v <= 0;
        fetchbufD_v <= 0;
        fetchbuf <= 0;
        fetchbuf <= 0;
        panic <= `PANIC_NONE;
        panic <= `PANIC_NONE;
end
end
else begin
else begin
 
 
        did_branchback0 <= take_branch0;
        did_branchback0 <= take_branch0;
        did_branchback1 <= take_branch1;
        did_branchback1 <= take_branch1;
 
 
        stompedRet = FALSE;
        stompedRet = FALSE;
 
 
        begin
        begin
 
 
        // On a branch miss with threading enabled all fectch buffers are
        // On a branch miss with threading enabled all fectch buffers are
        // invalidated even though the data in the fetch buffer would be valid
        // invalidated even though the data in the fetch buffer would be valid
        // for the thread that isn't in a branchmiss state. This is done to
        // for the thread that isn't in a branchmiss state. This is done to
        // keep things simple. For the thread that doesn't miss the current
        // keep things simple. For the thread that doesn't miss the current
        // data for the fetch buffer needs to be retrieved again, so the pc
        // data for the fetch buffer needs to be retrieved again, so the pc
        // for that thread is assigned the current fetchbuf pc.
        // for that thread is assigned the current fetchbuf pc.
        // For the thread that misses the pc is simply assigned the misspc.
        // For the thread that misses the pc is simply assigned the misspc.
        if (branchmiss) begin
        if (branchmiss) begin
                $display("***********");
                $display("***********");
                $display("Branch miss");
                $display("Branch miss");
                $display("***********");
                $display("***********");
                if (branchmiss_thrd) begin
                if (branchmiss_thrd) begin
                        pc1 <= misspc;
                        pc1 <= misspc;
                        fetchbufB_v <= `INV;
                        fetchbufB_v <= `INV;
                        fetchbufD_v <= `INV;
                        fetchbufD_v <= `INV;
                end
                end
                else begin
                else begin
                        pc0 <= misspc;
                        pc0 <= misspc;
                        if (thread_en) begin
                        if (thread_en) begin
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                        end
                        end
                        else begin
                        else begin
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                fetchbufB_v <= `INV;
                                fetchbufB_v <= `INV;
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                                fetchbufD_v <= `INV;
                                fetchbufD_v <= `INV;
                                fetchbuf <= 1'b0;
                                fetchbuf <= 1'b0;
                        end
                        end
                end
                end
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
             $display("Branch miss");
             $display("Branch miss");
             $display("misspc=%h", misspc);
             $display("misspc=%h", misspc);
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
             $display("********************");
        end
        end
        else if (take_branch) begin
        else if (take_branch) begin
 
 
    // update the fetchbuf valid bits as well as fetchbuf itself
    // update the fetchbuf valid bits as well as fetchbuf itself
    // ... this must be based on which things are backwards branches, how many things
    // ... this must be based on which things are backwards branches, how many things
    // will get enqueued (0, 1, or 2), and how old the instructions are
    // will get enqueued (0, 1, or 2), and how old the instructions are
    if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v})
    if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, fetchbufC_v, fetchbufD_v})
 
 
                4'b0000: ;      // do nothing
                4'b0000: ;      // do nothing
                4'b0001: if (thread_en) FetchC();
                4'b0001: if (thread_en) FetchC();
                4'b0010: if (thread_en) FetchD();
                4'b0010: if (thread_en) FetchD();
                4'b0011: ;
                4'b0011: ;
                4'b0100 :
                4'b0100 :
            begin
            begin
                    if (thread_en) begin
                    if (thread_en) begin
                        FetchC();
                        FetchC();
                        pc1 <= branch_pcB;
                        pc1 <= branch_pcB;
                    end
                    end
                    else
                    else
                        pc0 <= branch_pcB;
                        pc0 <= branch_pcB;
                    fetchbufB_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufB_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b0101:
                4'b0101:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        FetchC();
                                        FetchC();
                                end
                                end
                                else
                                else
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                fetchbufD_v <= `INV;
                                fetchbufD_v <= `INV;
                                fetchbufB_v <= !(queued1|queuedNop);
                                fetchbufB_v <= !(queued1|queuedNop);
                        end
                        end
                4'b0110:
                4'b0110:
                        begin
                        begin
                                if (thread_en)
                                if (thread_en)
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                else begin
                                else begin
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                        fetchbufC_v <= `INV;
                                        fetchbufC_v <= `INV;
                                end
                                end
                                fetchbufB_v <= !(queued1|queuedNop);
                                fetchbufB_v <= !(queued1|queuedNop);
                        end
                        end
                4'b0111:
                4'b0111:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                end
                                end
                                else begin
                                else begin
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                        fetchbufC_v <= `INV;
                                        fetchbufC_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                end
                                end
                          fetchbufB_v <= !(queued1|queuedNop);  // if it can be queued, it will
                          fetchbufB_v <= !(queued1|queuedNop);  // if it can be queued, it will
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1000 :
                4'b1000 :
                        begin
                        begin
                                if (thread_en) FetchD();
                                if (thread_en) FetchD();
                    pc0 <= branch_pcA;
                    pc0 <= branch_pcA;
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1001:
                4'b1001:
                        begin
                        begin
                                pc0 <= branch_pcA;
                                pc0 <= branch_pcA;
                                if (!thread_en)
                                if (!thread_en)
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1010:
                4'b1010:
                        begin
                        begin
                                pc0 <= branch_pcA;
                                pc0 <= branch_pcA;
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                                if (thread_en) FetchD();
                                if (thread_en) FetchD();
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufA_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1011:
                4'b1011:
                        begin
                        begin
                                pc0 <= branch_pcA;
                                pc0 <= branch_pcA;
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                                if (!thread_en)
                                if (!thread_en)
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                fetchbufA_v <=!(queued1|queuedNop);     // if it can be queued, it will
                                fetchbufA_v <=!(queued1|queuedNop);     // if it can be queued, it will
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1100:
                4'b1100:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchA && take_branchB) begin
                                if (take_branchA && take_branchB) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchA) begin
                                else if (take_branchA) begin
                                        FetchD();
                                        FetchD();
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        FetchC();
                                        FetchC();
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                if (take_branchA) begin
                                if (take_branchA) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1101:
                4'b1101:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchA && take_branchB) begin
                                if (take_branchA && take_branchB) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchA) begin
                                else if (take_branchA) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        FetchC();
                                        FetchC();
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                fetchbufD_v <= `INV;
                                fetchbufD_v <= `INV;
                                if (take_branchA) begin
                                if (take_branchA) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1110:
                4'b1110:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchA && take_branchB) begin
                                if (take_branchA && take_branchB) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufC_v <= `INV;
                                        fetchbufC_v <= `INV;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchA) begin
                                else if (take_branchA) begin
                                        FetchD();
                                        FetchD();
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufC_v <= `INV;
                                        fetchbufC_v <= `INV;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        pc1 <= branch_pcB;
                                        pc1 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                                if (take_branchA) begin
                                if (take_branchA) begin
                                        pc0 <= branch_pcA;
                                        pc0 <= branch_pcA;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchB) begin
                                else if (take_branchB) begin
                                        pc0 <= branch_pcB;
                                        pc0 <= branch_pcB;
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1111:
                4'b1111:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        if (take_branchA & take_branchB) begin
                                        if (take_branchA & take_branchB) begin
                                                pc0 <= branch_pcA;
                                                pc0 <= branch_pcA;
                                                pc1 <= branch_pcB;
                                                pc1 <= branch_pcB;
                                                fetchbufC_v <= `INV;
                                                fetchbufC_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                        else if (take_branchA) begin
                                        else if (take_branchA) begin
                                                pc0 <= branch_pcA;
                                                pc0 <= branch_pcA;
                                                fetchbufC_v <= `INV;
                                                fetchbufC_v <= `INV;
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                        else if (take_branchB) begin
                                        else if (take_branchB) begin
                                                pc1 <= branch_pcB;
                                                pc1 <= branch_pcB;
                                                fetchbufD_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                end
                                end
                                else begin
                                else begin
                                        if (take_branchA) begin
                                        if (take_branchA) begin
                                                pc0 <= branch_pcA;
                                                pc0 <= branch_pcA;
                                                fetchbufB_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufC_v <= `INV;
                                                fetchbufC_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                        end
                                        end
                                        else if (take_branchB) begin
                                        else if (take_branchB) begin
                                                pc0 <= branch_pcB;
                                                pc0 <= branch_pcB;
                                                fetchbufC_v <= `INV;
                                                fetchbufC_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufA_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufB_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                end
                                end
                        end
                        end
    default:    ;
    default:    ;
          endcase
          endcase
          else case ({fetchbufC_v, fetchbufD_v, fetchbufA_v, fetchbufB_v})
          else case ({fetchbufC_v, fetchbufD_v, fetchbufA_v, fetchbufB_v})
 
 
                4'b0000: ;      // do nothing
                4'b0000: ;      // do nothing
                4'b0001: if (thread_en) FetchA();
                4'b0001: if (thread_en) FetchA();
                4'b0010: if (thread_en) FetchB();
                4'b0010: if (thread_en) FetchB();
                4'b0011: ;
                4'b0011: ;
                4'b0100 :
                4'b0100 :
            begin
            begin
                    if (thread_en) begin
                    if (thread_en) begin
                        FetchA();
                        FetchA();
                        pc1 <= branch_pcD;
                        pc1 <= branch_pcD;
                    end
                    end
                    else
                    else
                        pc0 <= branch_pcD;
                        pc0 <= branch_pcD;
                    fetchbufD_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufD_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b0101:
                4'b0101:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        FetchA();
                                        FetchA();
                                end
                                end
                                else
                                else
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                fetchbufB_v <= `INV;
                                fetchbufB_v <= `INV;
                                fetchbufD_v <= !(queued1|queuedNop);
                                fetchbufD_v <= !(queued1|queuedNop);
                        end
                        end
                4'b0110:
                4'b0110:
                        begin
                        begin
                                if (thread_en)
                                if (thread_en)
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                else begin
                                else begin
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                        fetchbufA_v <= `INV;
                                        fetchbufA_v <= `INV;
                                end
                                end
                                fetchbufD_v <= !(queued1|queuedNop);
                                fetchbufD_v <= !(queued1|queuedNop);
                        end
                        end
                4'b0111:
                4'b0111:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                end
                                end
                                else begin
                                else begin
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                        fetchbufA_v <= `INV;
                                        fetchbufA_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                end
                                end
                          fetchbufD_v <= !(queued1|queuedNop);  // if it can be queued, it will
                          fetchbufD_v <= !(queued1|queuedNop);  // if it can be queued, it will
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1000 :
                4'b1000 :
                        begin
                        begin
                                if (thread_en) FetchB();
                                if (thread_en) FetchB();
                    pc0 <= branch_pcC;
                    pc0 <= branch_pcC;
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1001:
                4'b1001:
                        begin
                        begin
                                pc0 <= branch_pcC;
                                pc0 <= branch_pcC;
                                if (!thread_en)
                                if (!thread_en)
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1010:
                4'b1010:
                        begin
                        begin
                                pc0 <= branch_pcC;
                                pc0 <= branch_pcC;
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                if (thread_en) FetchB();
                                if (thread_en) FetchB();
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbufC_v <= !(queued1|queuedNop);        // if it can be queued, it will
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                    fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1011:
                4'b1011:
                        begin
                        begin
                                pc0 <= branch_pcC;
                                pc0 <= branch_pcC;
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                if (!thread_en)
                                if (!thread_en)
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                fetchbufC_v <=!(queued1|queuedNop);     // if it can be queued, it will
                                fetchbufC_v <=!(queued1|queuedNop);     // if it can be queued, it will
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                        end
                        end
                4'b1100:
                4'b1100:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchC && take_branchD) begin
                                if (take_branchC && take_branchD) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchC) begin
                                else if (take_branchC) begin
                                        FetchB();
                                        FetchB();
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchD) begin
                                else if (take_branchD) begin
                                        FetchA();
                                        FetchA();
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                if (take_branchC) begin
                                if (take_branchC) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchD) begin
                                else if (take_branchD) begin
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1101:
                4'b1101:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchC && take_branchD) begin
                                if (take_branchC && take_branchD) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchC) begin
                                else if (take_branchC) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchC) begin
                                else if (take_branchC) begin
                                        FetchA();
                                        FetchA();
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufB_v <= `INV;
                                        fetchbufB_v <= `INV;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                fetchbufB_v <= `INV;
                                fetchbufB_v <= `INV;
                                if (take_branchC) begin
                                if (take_branchC) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchD) begin
                                else if (take_branchD) begin
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1110:
                4'b1110:
                        if (thread_en) begin
                        if (thread_en) begin
                                if (take_branchC && take_branchD) begin
                                if (take_branchC && take_branchD) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufA_v <= `INV;
                                        fetchbufA_v <= `INV;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchC) begin
                                else if (take_branchC) begin
                                        FetchB();
                                        FetchB();
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufA_v <= `INV;
                                        fetchbufA_v <= `INV;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchD) begin
                                else if (take_branchD) begin
                                        pc1 <= branch_pcD;
                                        pc1 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                        end
                        end
                        else begin
                        else begin
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                if (take_branchC) begin
                                if (take_branchC) begin
                                        pc0 <= branch_pcC;
                                        pc0 <= branch_pcC;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= `INV;
                                        fetchbufD_v <= `INV;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued1|queuedNop))   fetchbuf <= 1'b1;
                                end
                                end
                                else if (take_branchD) begin
                                else if (take_branchD) begin
                                        pc0 <= branch_pcD;
                                        pc0 <= branch_pcD;
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                                        if ((queued2|queuedNop))   fetchbuf <= 1'b1;
                    end
                    end
                    // else hardware error
                    // else hardware error
                  end
                  end
                4'b1111:
                4'b1111:
                        begin
                        begin
                                if (thread_en) begin
                                if (thread_en) begin
                                        if (take_branchC & take_branchD) begin
                                        if (take_branchC & take_branchD) begin
                                                pc0 <= branch_pcC;
                                                pc0 <= branch_pcC;
                                                pc1 <= branch_pcD;
                                                pc1 <= branch_pcD;
                                                fetchbufA_v <= `INV;
                                                fetchbufA_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                        else if (take_branchC) begin
                                        else if (take_branchC) begin
                                                pc0 <= branch_pcD;
                                                pc0 <= branch_pcD;
                                                fetchbufA_v <= `INV;
                                                fetchbufA_v <= `INV;
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                        else if (take_branchD) begin
                                        else if (take_branchD) begin
                                                pc1 <= branch_pcD;
                                                pc1 <= branch_pcD;
                                                fetchbufB_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                end
                                end
                                else begin
                                else begin
                                        if (take_branchC) begin
                                        if (take_branchC) begin
                                                pc0 <= branch_pcC;
                                                pc0 <= branch_pcC;
                                                fetchbufD_v <= `INV;
                                                fetchbufD_v <= `INV;
                                                fetchbufA_v <= `INV;
                                                fetchbufA_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                                fetchbuf <= fetchbuf + (queued1|queuedNop);
                                        end
                                        end
                                        else if (take_branchD) begin
                                        else if (take_branchD) begin
                                                pc0 <= branch_pcD;
                                                pc0 <= branch_pcD;
                                                fetchbufA_v <= `INV;
                                                fetchbufA_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufB_v <= `INV;
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufC_v <= !(queued1|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbufD_v <= !(queued2|queuedNop);    // if it can be queued, it will
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                                fetchbuf <= fetchbuf + (queued2|queuedNop);
                                        end
                                        end
                                end
                                end
                        end
                        end
    default:    ;
    default:    ;
          endcase
          endcase
        end // if branchback
        end // if branchback
 
 
        else begin      // there is no branchback in the system
        else begin      // there is no branchback in the system
            //
            //
            // update fetchbufX_v and fetchbuf ... relatively simple, as
            // update fetchbufX_v and fetchbuf ... relatively simple, as
            // there are no backwards branches in the mix
            // there are no backwards branches in the mix
    if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, (queued1|queuedNop), (queued2|queuedNop)})
    if (fetchbuf == 1'b0) case ({fetchbufA_v, fetchbufB_v, (queued1|queuedNop), (queued2|queuedNop)})
                4'b00_00 : ;    // do nothing
                4'b00_00 : ;    // do nothing
                4'b00_01:       ;
                4'b00_01:       ;
                4'b00_10:       ;
                4'b00_10:       ;
                4'b00_11:       ;
                4'b00_11:       ;
                4'b01_00: ;     // do nothing
                4'b01_00: ;     // do nothing
                4'b01_01:       ;
                4'b01_01:       ;
                4'b01_10,
                4'b01_10,
                4'b01_11:
                4'b01_11:
                        begin   // enqueue fbB and flip fetchbuf
                        begin   // enqueue fbB and flip fetchbuf
                                fetchbufB_v <= `INV;
                                fetchbufB_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                4'b10_00: ;     // do nothing
                4'b10_00: ;     // do nothing
                4'b10_01: ;
                4'b10_01: ;
                4'b10_10,
                4'b10_10,
                4'b10_11:
                4'b10_11:
                        begin   // enqueue fbA and flip fetchbuf
                        begin   // enqueue fbA and flip fetchbuf
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                4'b11_00: ;     // do nothing
                4'b11_00: ;     // do nothing
                4'b11_01: ;
                4'b11_01: ;
                4'b11_10:
                4'b11_10:
                        begin   // enqueue fbA but leave fetchbuf
                        begin   // enqueue fbA but leave fetchbuf
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                  end
                  end
                4'b11_11:
                4'b11_11:
                        begin   // enqueue both and flip fetchbuf
                        begin   // enqueue both and flip fetchbuf
                                fetchbufA_v <= `INV;
                                fetchbufA_v <= `INV;
                                fetchbufB_v <= `INV;
                                fetchbufB_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                default:  panic <= `PANIC_INVALIDIQSTATE;
                default:  panic <= `PANIC_INVALIDIQSTATE;
    endcase
    endcase
    else case ({fetchbufC_v, fetchbufD_v, (queued1|queuedNop), (queued2|queuedNop)})
    else case ({fetchbufC_v, fetchbufD_v, (queued1|queuedNop), (queued2|queuedNop)})
                4'b00_00 : ;    // do nothing
                4'b00_00 : ;    // do nothing
                4'b00_01: ;
                4'b00_01: ;
                4'b00_10 : ;    // do nothing
                4'b00_10 : ;    // do nothing
                4'b00_11 : ;    // do nothing
                4'b00_11 : ;    // do nothing
                4'b01_00 : ;    // do nothing
                4'b01_00 : ;    // do nothing
                4'b01_01 : ;
                4'b01_01 : ;
                4'b01_10,
                4'b01_10,
                4'b01_11 :
                4'b01_11 :
                        begin   // enqueue fbD and flip fetchbuf
                        begin   // enqueue fbD and flip fetchbuf
                                fetchbufD_v <= `INV;
                                fetchbufD_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                4'b10_00 : ;    // do nothing
                4'b10_00 : ;    // do nothing
                4'b10_01: ;
                4'b10_01: ;
                4'b10_10,
                4'b10_10,
                4'b10_11:
                4'b10_11:
                        begin   // enqueue fbC and flip fetchbuf
                        begin   // enqueue fbC and flip fetchbuf
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                4'b11_00 : ;    // do nothing
                4'b11_00 : ;    // do nothing
                4'b11_01: ;
                4'b11_01: ;
                4'b11_10:
                4'b11_10:
                        begin   // enqueue fbC but leave fetchbuf
                        begin   // enqueue fbC but leave fetchbuf
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                  end
                  end
                4'b11_11:
                4'b11_11:
                        begin   // enqueue both and flip fetchbuf
                        begin   // enqueue both and flip fetchbuf
                                fetchbufC_v <= `INV;
                                fetchbufC_v <= `INV;
                                fetchbufD_v <= `INV;
                                fetchbufD_v <= `INV;
                          fetchbuf <= ~fetchbuf;
                          fetchbuf <= ~fetchbuf;
                  end
                  end
                default:  panic <= `PANIC_INVALIDIQSTATE;
                default:  panic <= `PANIC_INVALIDIQSTATE;
          endcase
          endcase
    //
    //
    // get data iff the fetch buffers are empty
    // get data iff the fetch buffers are empty
    //
    //
    if (fetchbufA_v == `INV && fetchbufB_v == `INV) begin
    if (fetchbufA_v == `INV && fetchbufB_v == `INV) begin
      FetchAB();
      FetchAB();
      // fetchbuf steering logic correction
      // fetchbuf steering logic correction
      if (fetchbufC_v==`INV && fetchbufD_v==`INV && phit)
      if (fetchbufC_v==`INV && fetchbufD_v==`INV && phit)
        fetchbuf <= 1'b0;
        fetchbuf <= 1'b0;
    end
    end
    else if (fetchbufC_v == `INV && fetchbufD_v == `INV)
    else if (fetchbufC_v == `INV && fetchbufD_v == `INV)
            FetchCD();
            FetchCD();
        end
        end
    //
    //
    // get data iff the fetch buffers are empty
    // get data iff the fetch buffers are empty
    //
    //
    if (fetchbufA_v == `INV && fetchbufB_v == `INV && fetchbufC_v==`INV && fetchbufD_v==`INV) begin
    if (fetchbufA_v == `INV && fetchbufB_v == `INV && fetchbufC_v==`INV && fetchbufD_v==`INV) begin
                        FetchAB();
                        FetchAB();
                        fetchbuf <= 1'b0;
                        fetchbuf <= 1'b0;
    end
    end
        end
        end
 
 
        // The fetchbuffer is invalidated at the end of a vector instruction
        // The fetchbuffer is invalidated at the end of a vector instruction
        // queue.
        // queue.
        if (nop_fetchbuf[0])  fetchbufA_v <= `INV;
        if (nop_fetchbuf[0])  fetchbufA_v <= `INV;
        if (nop_fetchbuf[1])  fetchbufB_v <= `INV;
        if (nop_fetchbuf[1])  fetchbufB_v <= `INV;
        if (nop_fetchbuf[2])  fetchbufC_v <= `INV;
        if (nop_fetchbuf[2])  fetchbufC_v <= `INV;
        if (nop_fetchbuf[3])  fetchbufD_v <= `INV;
        if (nop_fetchbuf[3])  fetchbufD_v <= `INV;
end
end
 
 
assign fetchbuf0_instr = (fetchbuf == 1'b0) ? fetchbufA_instr : fetchbufC_instr;
assign fetchbuf0_instr = (fetchbuf == 1'b0) ? fetchbufA_instr : fetchbufC_instr;
assign fetchbuf0_insln = (fetchbuf == 1'b0) ? fetchbufA_inslen: fetchbufC_inslen;
assign fetchbuf0_insln = (fetchbuf == 1'b0) ? fetchbufA_inslen: fetchbufC_inslen;
assign fetchbuf0_v     = (fetchbuf == 1'b0) ? fetchbufA_v     : fetchbufC_v    ;
assign fetchbuf0_v     = (fetchbuf == 1'b0) ? fetchbufA_v     : fetchbufC_v    ;
assign fetchbuf0_pc    = (fetchbuf == 1'b0) ? fetchbufA_pc    : fetchbufC_pc   ;
assign fetchbuf0_pc    = (fetchbuf == 1'b0) ? fetchbufA_pc    : fetchbufC_pc   ;
assign fetchbuf1_instr = (fetchbuf == 1'b0) ? fetchbufB_instr : fetchbufD_instr;
assign fetchbuf1_instr = (fetchbuf == 1'b0) ? fetchbufB_instr : fetchbufD_instr;
assign fetchbuf1_insln = (fetchbuf == 1'b0) ? fetchbufB_inslen: fetchbufD_inslen;
assign fetchbuf1_insln = (fetchbuf == 1'b0) ? fetchbufB_inslen: fetchbufD_inslen;
assign fetchbuf1_v     = (fetchbuf == 1'b0) ? fetchbufB_v     : fetchbufD_v    ;
assign fetchbuf1_v     = (fetchbuf == 1'b0) ? fetchbufB_v     : fetchbufD_v    ;
assign fetchbuf1_pc    = (fetchbuf == 1'b0) ? fetchbufB_pc    : fetchbufD_pc   ;
assign fetchbuf1_pc    = (fetchbuf == 1'b0) ? fetchbufB_pc    : fetchbufD_pc   ;
assign fetchbuf0_thrd  = 1'b0;
assign fetchbuf0_thrd  = 1'b0;
assign fetchbuf1_thrd  = thread_en;
assign fetchbuf1_thrd  = thread_en;
 
 
reg [2:0] insln0, insln1;
reg [2:0] insln0, insln1;
always @*
always @*
begin
begin
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
        if (insn0[5:0]==`CMPRSSD)
        if (insn0[5:0]==`CMPRSSD)
                insln0 <= 3'd2;
                insln0 <= 3'd2;
        else
        else
`endif
`endif
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
                insln0 <= fnInsLength(codebuf0);
                insln0 <= fnInsLength(codebuf0);
        else
        else
                insln0 <= fnInsLength(insn0);
                insln0 <= fnInsLength(insn0);
end
end
 
 
always @*
always @*
begin
begin
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
        if (insn1[5:0]==`CMPRSSD)
        if (insn1[5:0]==`CMPRSSD)
                insln1 <= 3'd2;
                insln1 <= 3'd2;
        else
        else
`endif
`endif
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
                insln1 <= fnInsLength(codebuf1);
                insln1 <= fnInsLength(codebuf1);
        else
        else
                insln1 <= fnInsLength(insn1);
                insln1 <= fnInsLength(insn1);
end
end
 
 
reg [47:0] cinsn0, cinsn1;
reg [47:0] cinsn0, cinsn1;
 
 
always @*
always @*
begin
begin
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
        if (insn0[5:0]==`CMPRSSD)
        if (insn0[5:0]==`CMPRSSD)
                cinsn0 <= expand0;
                cinsn0 <= expand0;
        else
        else
`endif
`endif
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
        if (insn0[7:6]==2'b00 && insn0[`INSTRUCTION_OP]==`EXEC)
                cinsn0 <= codebuf0;
                cinsn0 <= codebuf0;
        else if (insn0[7])
        else if (insn0[7])
                cinsn0 <= xinsn0;
                cinsn0 <= xinsn0;
        else
        else
                cinsn0 <= insn0;
                cinsn0 <= insn0;
end
end
 
 
always @*
always @*
begin
begin
`ifdef SUPPORT_DCI
`ifdef SUPPORT_DCI
        if (insn1[5:0]==`CMPRSSD)
        if (insn1[5:0]==`CMPRSSD)
                cinsn1 <= expand1;
                cinsn1 <= expand1;
        else
        else
`endif
`endif
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
        if (insn1[7:6]==2'b00 && insn1[`INSTRUCTION_OP]==`EXEC)
                cinsn1 <= codebuf1;
                cinsn1 <= codebuf1;
        else if (insn1[7])
        else if (insn1[7])
                cinsn1 <= xinsn1;
                cinsn1 <= xinsn1;
        else
        else
                cinsn1 <= insn1;
                cinsn1 <= insn1;
end
end
 
 
task FetchA;
task FetchA;
begin
begin
        fetchbufA_instr <= cinsn0;
        fetchbufA_instr <= cinsn0;
        fetchbufA_v <= `VAL;
        fetchbufA_v <= `VAL;
        fetchbufA_pc <= pc0;
        fetchbufA_pc <= pc0;
        if (phit && ~freezePC) begin
        if (phit && ~freezePC) begin
                if (thread_en)
                if (thread_en)
                        pc0 <= pc0 + insln0;
                        pc0 <= pc0 + insln0;
                else if (`WAYS > 1)
                else if (`WAYS > 1)
                        pc0 <= pc0 + insln0 + insln1;
                        pc0 <= pc0 + insln0 + insln1;
                else
                else
                        pc0 <= pc0 + insln0;
                        pc0 <= pc0 + insln0;
        end
        end
end
end
endtask
endtask
 
 
task FetchB;
task FetchB;
begin
begin
        fetchbufB_instr <= cinsn1;
        fetchbufB_instr <= cinsn1;
        fetchbufB_v <= `WAYS > 1;
        fetchbufB_v <= `WAYS > 1;
        if (thread_en)
        if (thread_en)
                fetchbufB_pc <= pc1;
                fetchbufB_pc <= pc1;
        else
        else
                fetchbufB_pc <= pc0 + insln0;
                fetchbufB_pc <= pc0 + insln0;
        if (phit & thread_en)
        if (phit & thread_en)
                pc1 <= pc1 + insln1;
                pc1 <= pc1 + insln1;
end
end
endtask
endtask
 
 
 
 
task FetchAB;
task FetchAB;
begin
begin
        FetchA();
        FetchA();
        FetchB();
        FetchB();
end
end
endtask
endtask
 
 
task FetchC;
task FetchC;
begin
begin
        fetchbufC_instr <= cinsn0;
        fetchbufC_instr <= cinsn0;
        fetchbufC_v <= `VAL;
        fetchbufC_v <= `VAL;
        fetchbufC_pc <= pc0;
        fetchbufC_pc <= pc0;
        if (phit && ~freezePC) begin
        if (phit && ~freezePC) begin
                if (thread_en)
                if (thread_en)
                        pc0 <= pc0 + insln0;
                        pc0 <= pc0 + insln0;
                else if (`WAYS > 1)
                else if (`WAYS > 1)
                        pc0 <= pc0 + insln0 + insln1;
                        pc0 <= pc0 + insln0 + insln1;
                else
                else
                        pc0 <= pc0 + insln0;
                        pc0 <= pc0 + insln0;
        end
        end
end
end
endtask
endtask
 
 
task FetchD;
task FetchD;
begin
begin
        fetchbufD_instr <= cinsn1;
        fetchbufD_instr <= cinsn1;
        fetchbufD_v <= `WAYS > 1;
        fetchbufD_v <= `WAYS > 1;
        if (thread_en)
        if (thread_en)
                fetchbufD_pc <= pc1;
                fetchbufD_pc <= pc1;
        else
        else
                fetchbufD_pc <= pc0 + insln0;
                fetchbufD_pc <= pc0 + insln0;
        if (phit & thread_en)
        if (phit & thread_en)
                pc1 <= pc1 + insln1;
                pc1 <= pc1 + insln1;
end
end
endtask
endtask
 
 
task FetchCD;
task FetchCD;
begin
begin
        FetchC();
        FetchC();
        FetchD();
        FetchD();
end
end
endtask
endtask
 
 
endmodule
endmodule
 
 
 
 

powered by: WebSVN 2.1.0

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