add ClintBase
[shakti-core.git] / src / core / prf.bsv
1 /*
2 Copyright (c) 2013, IIT Madras
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of IIT Madras nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13 */
14 package prf;
15 import defined_types::*;
16 `include "core_parameters.bsv"
17 import Vector::*;
18 interface Ifc_prf_new;
19 method ActionValue#(RFType#(`Reg_width)) read_rs1 (Bit#(5) addr, Operand_type rs1type, Bit#(`Reg_width) data);
20 method ActionValue#(RFType#(`Reg_width)) read_rs2 (Bit#(5) addr, Operand_type rs2type, Bit#(`Reg_width) data);
21 method ActionValue#(RFType#(`Reg_width)) read_rs3 (Bit#(5) addr, Operand_type rs3type, Bit#(`Reg_width) data);
22 method ActionValue#(Tuple2#(Bit#(TLog#(`PRFDEPTH)),Bit#(4))) get_index_pid(Bit#(5) addr, Operand_type rdtype);
23 method Action update_rd (Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
24 method Action fwd_from_execution (Bit#(`Reg_width) data, Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
25 method Action fwd_from_memory (Bit#(`Reg_width) data, Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
26 method Action flush_all;
27 endinterface
28 (*conflict_free="fwd_from_execution,fwd_from_memory"*)
29 (*conflict_free="fwd_from_execution,update_rd"*)
30 (*conflict_free="update_rd,fwd_from_memory"*)
31 (*synthesize*)
32 module mkprf_new(Ifc_prf_new);
33 Reg#(RFType#(`Reg_width)) physical_rf [`PRFDEPTH];
34 Reg#(Bit#(4)) pid_rf [`PRFDEPTH];
35 Vector#(`PRFDEPTH,Reg#(Tuple2#(Bit#(5),Operand_type))) rd_rf<-replicateM(mkReg(tuple2(0,IntegerRF)));
36 Reg#(Bit#(4)) rg_pid_counter<-mkReg(0);
37 Reg#(Bit#(TLog#(`PRFDEPTH))) rg_prf_index<-mkReg(0);
38 Wire#(Bool) wr_flush<-mkDWire(False);
39 for(Integer i=0;i<`PRFDEPTH;i=i+1)begin
40 physical_rf[i]<-mkReg(tagged Absent 0);
41 pid_rf[i]<-mkReg(0);
42 end
43 rule flush_all_mapping(wr_flush);
44 for(Integer i=0;i<`PRFDEPTH;i=i+1)begin
45 physical_rf[i]<=tagged Absent 0;
46 pid_rf[i]<=0;
47 rd_rf[i]<=tuple2(0,IntegerRF);
48 end
49 endrule
50 method ActionValue#(RFType#(`Reg_width)) read_rs1 (Bit#(5) addr, Operand_type rs1type, Bit#(`Reg_width) data);
51 if(rs1type==IntegerRF && addr==0)
52 return tagged Present 0;
53 else if(rs1type==IntegerRF `ifdef spfpu || rs1type==FloatingRF `endif ) begin
54 let array_rd=readVReg(rd_rf); // convert the reg-vector to bit#(5)-vector
55 let index=findElem(tuple2(addr,rs1type),array_rd); // find the index of match
56 if(index matches tagged Valid .idx)begin // if match exists
57 `ifdef verbose $display($time,"\tPRF: READ_RS1: valid index: %d",idx); `endif
58 return physical_rf[idx];
59 end
60 else // there is no instruction in pipe updating this reg.
61 return tagged Present data;
62 end
63 else // there is no instruction in pipe updating this reg.
64 return tagged Present data;
65 endmethod
66 method ActionValue#(RFType#(`Reg_width)) read_rs2 (Bit#(5) addr, Operand_type rs2type, Bit#(`Reg_width) data);
67 if(rs2type==IntegerRF && addr==0)
68 return tagged Present 0;
69 else if(rs2type==IntegerRF `ifdef spfpu || rs2type==FloatingRF `endif ) begin
70 let array_rd=readVReg(rd_rf); // convert the reg-vector to bit#(5)-vector
71 let index=findElem(tuple2(addr,rs2type),array_rd); // find the index of match
72 if(index matches tagged Valid .idx)begin // if match exists
73 `ifdef verbose $display($time,"\tPRF: READ_RS2: valid index: %d",idx); `endif
74 return physical_rf[idx];
75 end
76 else // there is no instruction in pipe updating this reg.
77 return tagged Present data;
78 end
79 else // there is no instruction in pipe updating this reg.
80 return tagged Present data;
81 endmethod
82 method ActionValue#(RFType#(`Reg_width)) read_rs3 (Bit#(5) addr, Operand_type rs3type, Bit#(`Reg_width) data);
83 if(rs3type==IntegerRF && addr==0)
84 return tagged Present 0;
85 else if(rs3type==IntegerRF `ifdef spfpu || rs3type==FloatingRF `endif ) begin
86 let array_rd=readVReg(rd_rf); // convert the reg-vector to bit#(5)-vector
87 let index=findElem(tuple2(addr,rs3type),array_rd); // find the index of match
88 if(index matches tagged Valid .idx)begin // if match exists
89 return physical_rf[idx];
90 end
91 else // there is no instruction in pipe updating this reg.
92 return tagged Present data;
93 end
94 else // there is no instruction in pipe updating this reg.
95 return tagged Present data;
96 endmethod
97 method ActionValue#(Tuple2#(Bit#(TLog#(`PRFDEPTH)),Bit#(4))) get_index_pid(Bit#(5) addr, Operand_type rdtype);
98 let array_rd=readVReg(rd_rf);
99 let index=findElem(tuple2(addr,rdtype),array_rd); // find the index of match
100 rg_pid_counter<=rg_pid_counter+1;
101 if(rg_prf_index==`PRFDEPTH-1)
102 rg_prf_index<=0;
103 else
104 rg_prf_index<=rg_prf_index+1;
105 rd_rf[rg_prf_index]<=tuple2(addr,rdtype);
106 if(index matches tagged Valid .idx)begin
107 if(pack(idx)!=rg_prf_index)
108 rd_rf[idx]<=tuple2(0,IntegerRF);
109 `ifdef verbose $display($time,"\tPRF: Removing previously alotted index: %d",idx); `endif
110 end
111 `ifdef verbose $display($time,"\tPRF: Giving Index: %d PID: %d",rg_prf_index, rg_pid_counter); `endif
112 return tuple2(rg_prf_index,rg_pid_counter);
113 endmethod
114 method Action update_rd (Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
115 physical_rf[index]<=tagged Absent pid;
116 pid_rf[index]<=pid;
117 endmethod
118 method Action fwd_from_execution (Bit#(`Reg_width) data, Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
119 `ifdef verbose $display($time,"\tPRF: FWD from EXE Data: %h index: %d pid: %d",data,index,pid); `endif
120 physical_rf[index]<=tagged Present data;
121 endmethod
122 method Action fwd_from_memory (Bit#(`Reg_width) data, Bit#(TLog#(`PRFDEPTH)) index, Bit#(4) pid);
123 `ifdef verbose $display($time,"\tPRF: FWD from MEM Data: %h index: %d pid: %d",data,index,pid); `endif
124 physical_rf[index]<=tagged Present data;
125 endmethod
126 method Action flush_all;
127 wr_flush<=True;
128 endmethod
129
130 endmodule
131 endpackage