MIPS Branch Target Buffer动态分支预测(BTB) Oracle实战优化:INSERT ALL关键字的应用 Linux中MySQL数据库的使用②-----数据的基本操作 理论+实验——MySQL备份与恢复 MySQL常用数据库函数 MySQL 备份与恢复(完全备份恢复--增量备份恢复+案例演示) MySQL之基础总结部分 Oracle实战优化:递归+分析函数+OLAP函数的应用 Linux中MySQL数据库的使用③-----编码和基本数据类型 理论+实验:MySQL备份与恢复(完整备份、增量备份) “数”聚永川 “智”引未来——永川区大数据智能化产业发展强劲 从一款防疫App感受新加坡大数据智能化气息 “数”聚永川“智”引未来——永川区大数据智能化产业发展强劲 从连接量变到数据质变 物联网将二次爆发 重磅发布!猎芯半导体首创全球最小支持5G物联网的多模多频射频PA芯片 从精准授信到助企惠民,江苏银行物联网金融派上大用场 Python Selenium UI自动化_WebDriver元素_8大定位方式+总结(持续更新完善) Python中的继承、抽象基类和接口 Datawhale学习笔记【阿里云天池 金融风控-贷款违约预测】task1 赛题理解 Pytorch - torchvision计算机视觉工具库 linux 重点笔记 Ubuntu18.04安装ROS Melodic(一路到站型) 小甲鱼笔记:数据结构——线性表(一)线性表的顺序存储结构,线性表顺序存储结构的增,删,插入元素操作 实战比特币脚本编程(1) JAVA WEB DAY 01_Tomcat & Servlet Java基础算法之堆排序(Heap Sort) synchronized批量重偏向与批量撤销 终于等到了!阿里P8历时九个月整理,Java面试宝典,核心知识点笔记在此 “数字心脏”动态解析消费密码,国家级消费市场大数据联合实验室在上海先行先试 全世界运行着大约230亿台物联网设备,安全问题如何解? 物联网产业园&thinkplus解决方案中心国学讲座如期而至 都是程序员,凭什么他能站在鄙视链的顶端? 猛男必看!去小红书做程序员是种什么体验 drozer提示[Errno 2] No such file or directory 【STM32】NB-iOT BC35-G模块 AT指令应用设计指导(附代码) 【北京迅为】i.MX6ULL终结者编译LED汇编程序 Linux系统读写网卡PHY寄存器工具 洛谷:P1226 【模板】快速幂||取余运算(分治,数学) 【2020顶会KDD】AutoST:面向时空预测的高效神经网络学习模型 C/C++实现并查集disjoint_set的模板(带路径压缩优化) 实现一个百万级推送服务,除了它,还有谁 “健康守护者”——STM32标准库和HAL库的比较 程序员被公司辞退12天后,前领导要求回公司讲清代码,结果懵了 RTTR实现C++反射(1)集成rttr库 lotus node 远程运行 CCF历年4,5题收录 N卡 RTX3070/RTX3080/RTX3090挖矿算力推测 最高算力86MH 理论+实验:MySQL索引、事物与存储引擎 PostgreSQL JOIN 多表查询 TP框架实现Excel批量导入数据库数据
您的位置:首页 >运维 >

MIPS Branch Target Buffer动态分支预测(BTB)

MIPS代码:

`timescale 1ns / 1ps//// Company: // Engineer: // // Create Date: 2020/07/23 15:31:35// Design Name: // Module Name: MIPS// Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision:// Revision 0.01 - File Created// Additional Comments:// //module MIPS(input clk,input rst);reg [31:0] RegFile [0:31];reg [31:0] IMEM [0:255];reg [31:0] DMEM [0:255];integer i;initialbeginfor(i=0;i<256;i=i+1)DMEM[i]=i;endinitialbeginfor(i=0;i<32;i=i+1)RegFile[i]<=i;endinitialbeginIMEM[0]=32'h00240820;IMEM[4]=32'h01024022;IMEM[8]=32'h1100fffd;IMEM[12]=32'h00c72820;IMEM[16]=32'h014b4820;IMEM[20]=32'h01ae6020;IMEM[24]=32'h02117820;endwire IF2ID_Write;wire PCWrite;wire ID2EXE_Stall;wire [31:0]ALUSrcA;wire [31:0]ALUSrcB;wire [1:0]ForwardA;wire [1:0]ForwardB;wire [4:0]IF2ID_rs;wire [4:0]IF2ID_rt;wire [31:0]MEM2WB_Data;wire FlushIF;wire FlushID;wire FlushEXE;assign FlushIF=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0; //若预测错误,则flush之前的3条指令assign FlushID=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0;assign FlushEXE=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0;reg wr_req;reg [31:0]wr_PC;reg [31:0]wr_predicted_PC;reg wr_predicted_state_bit;reg [31:0]rd_PC;wire rd_predicted_PC;wire rd_predicted;reg IF2ID_predicted;reg ID2EXE_predicted;reg EXE2MEM_predicted;//更新BTBalways@(*)if(EXE2MEM_Branch)if(EXE2MEM_ALUOut==1'b0&&EXE2MEM_predicted==1'b1) //预测taken但不takenbeginwr_req=1'b1;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;endelse if(EXE2MEM_ALUOut==1'b1&&EXE2MEM_predicted==1'b0)//预测不taken但takenbeginwr_req=1'b1;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b1;endelsebeginwr_req=1'b0;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;endelsebeginwr_req=1'b0;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;end//传递rd_predicted,即预测结果,以与之后的实际结果相比较always@(posedge clk,posedge rst)if(rst)beginIF2ID_predicted<=1'b0;ID2EXE_predicted<=1'b0;EXE2MEM_predicted<=1'b0;endelsebeginIF2ID_predicted<=rd_predicted;ID2EXE_predicted<=IF2ID_predicted;EXE2MEM_predicted<=ID2EXE_predicted;end//IFreg [31:0] PC;reg [31:0] IF2ID_NPC;reg [31:0] IF2ID_IR;//IDreg [4:0]ID2EXE_rs;//Rs寄存器号 约定大写为数据,小写为寄存器�?reg [4:0]ID2EXE_rt;//Rt寄存器号reg [4:0]ID2EXE_rd;//Rd寄存器号reg [31:0]ID2EXE_Rs; //Rs寄存器内�?reg [31:0]ID2EXE_Rt; //Rt寄存器内�?reg [31:0]ID2EXE_Imm;//符号扩展后的立即�?reg [31:0]ID2EXE_NPC;reg ID2EXE_RegDst;reg ID2EXE_ALUSrc;reg ID2EXE_MemRead;reg ID2EXE_MemWrite;reg ID2EXE_Branch;reg ID2EXE_MemtoReg;reg ID2EXE_RegWrite;reg [1:0] ID2EXE_ALUOp;//EXEreg [31:0]EXE2MEM_ALUOut;reg [31:0]EXE2MEM_BranchPC;reg [31:0]EXE2MEM_Rt;reg [4:0]EXE2MEM_RegRd;//WB阶段写寄存器�?reg EXE2MEM_MemRead;reg EXE2MEM_MemWrite;reg EXE2MEM_Branch;reg EXE2MEM_MemtoReg;reg EXE2MEM_RegWrite;reg [31:0]EXE2MEM_NPC;//MEMreg [31:0]MEM2WB_LDM;reg [31:0]MEM2WB_ALUOut;reg [4:0]MEM2WB_RegRd;reg MEM2WB_RegWrite;reg MEM2WB_MemtoReg;/****************************************************************************************/assign IF2ID_rs=IF2ID_IR[25:21];assign IF2ID_rt=IF2ID_IR[20:16];assign MEM2WB_Data=(MEM2WB_MemtoReg)?MEM2WB_LDM:MEM2WB_ALUOut;//IFBTB V(.clk(clk),.rst(rst),.rd_PC(PC),.rd_predicted(rd_predicted),.rd_predicted_PC(rd_predicted_PC),.wr_req(wr_req),.wr_PC(wr_PC),.wr_predicted_PC(wr_predicted_PC),.wr_predicted_state_bit(wr_predicted_state_bit));always@(posedge clk,posedge rst)beginif(rst)beginPC<=32'd0;IF2ID_NPC<=32'd0;IF2ID_IR<=32'd0;endelsebeginif(PCWrite)beginif(EXE2MEM_Branch)if(EXE2MEM_predicted==1'b1&&EXE2MEM_ALUOut==1'b0) //预测taken但实际没tabkePC<=EXE2MEM_NPC;else if(EXE2MEM_predicted==1'b0&&EXE2MEM_ALUOut==1'b1)//预测not taken但实际takenPC<=EXE2MEM_BranchPC;else if(rd_predicted)PC<=rd_predicted_PC;elsePC<=PC+32'd4;else if(rd_predicted)PC<=rd_predicted_PC;elsePC<=PC+32'd4;endelsePC<=PC;if(FlushIF)beginIF2ID_NPC<=32'd0;IF2ID_IR<=32'd0;endelse if(IF2ID_Write)beginIF2ID_NPC<=PC+4;IF2ID_IR<=IMEM[PC];endelsebeginIF2ID_NPC<=IF2ID_NPC;IF2ID_IR<=IF2ID_IR;endendend//IDHAZARD_DETECTION U1(.ID2EXE_MemRead(ID2EXE_MemRead),.IF2ID_rs(IF2ID_rs),.IF2ID_rt(IF2ID_rt),.ID2EXE_rt(ID2EXE_rt),.PCWrite(PCWrite),.ID2EXE_Stall(ID2EXE_Stall),.IF2ID_Write(IF2ID_Write));always@(posedge clk,posedge rst)if(rst)beginID2EXE_RegDst<=1'b0;ID2EXE_ALUSrc<=1'b0;ID2EXE_ALUOp<=2'b00;ID2EXE_MemRead<=1'b0;ID2EXE_MemWrite<=1'b0;ID2EXE_Branch<=1'b0;ID2EXE_RegWrite<=1'b0;ID2EXE_MemtoReg<=1'b0;endelse if(ID2EXE_Stall||FlushID)beginID2EXE_RegDst<=1'b0;ID2EXE_ALUSrc<=1'b0;ID2EXE_ALUOp<=2'b00;ID2EXE_MemRead<=1'b0;ID2EXE_MemWrite<=1'b0;ID2EXE_Branch<=1'b0;ID2EXE_RegWrite<=1'b0;ID2EXE_MemtoReg<=1'b0;endelsecase(IF2ID_IR[31:26])6'b000000:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b100100010;end6'b100011:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b011110000;end6'b101011:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b010001000;end6'b000100:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b000000101;enddefault:{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b000000000;endcasealways@(posedge clk,posedge rst)if(rst)beginID2EXE_rs<=5'd0;ID2EXE_rt<=5'd0;ID2EXE_rd<=5'd0;ID2EXE_Rs<=32'd0;ID2EXE_Rt<=32'd0;ID2EXE_Imm<=32'd0;ID2EXE_NPC<=32'd0;endelse if(FlushID)beginID2EXE_rs<=5'd0;ID2EXE_rt<=5'd0;ID2EXE_rd<=5'd0;ID2EXE_Rs<=32'd0;ID2EXE_Rt<=32'd0;ID2EXE_Imm<=32'd0;ID2EXE_NPC<=32'd0;endelsebeginID2EXE_rs<=IF2ID_IR[25:21];ID2EXE_rt<=IF2ID_IR[20:16];ID2EXE_rd<=IF2ID_IR[15:11];ID2EXE_Rs<=RegFile[IF2ID_IR[25:21]];ID2EXE_Rt<=RegFile[IF2ID_IR[20:16]];ID2EXE_Imm<={{16{IF2ID_IR[15]}},IF2ID_IR[15:0]};ID2EXE_NPC<=IF2ID_NPC;end//EXEFORWARDING U2(.ID2EXE_rs(ID2EXE_rs),.ID2EXE_rt(ID2EXE_rt),.EXE2MEM_RegRd(EXE2MEM_RegRd),.MEM2WB_RegRd(MEM2WB_RegRd),.EXE2MEM_RegWrite(EXE2MEM_RegWrite),.MEM2WB_RegWrite(MEM2WB_RegWrite),.ForwardA(ForwardA),.ForwardB(ForwardB));//ALUSrcA三�?�一mux3_1 U3(.a(ID2EXE_Rs),.b(EXE2MEM_ALUOut),.c(MEM2WB_Data),.select(ForwardA),.out(ALUSrcA));//ALUSrcBmux3_1 U4(.a(ID2EXE_Rt),.b(EXE2MEM_ALUOut),.c(MEM2WB_Data),.select(ForwardB),.out(ALUSrcB));always@(posedge clk,posedge rst)if(rst)beginEXE2MEM_MemRead<=1'b0;EXE2MEM_MemWrite<=1'b0;EXE2MEM_Branch<=1'b0;EXE2MEM_MemtoReg<=1'b0;EXE2MEM_RegWrite<=1'b0;endelse if(FlushEXE)beginEXE2MEM_MemRead<=1'b0;EXE2MEM_MemWrite<=1'b0;EXE2MEM_Branch<=1'b0;EXE2MEM_MemtoReg<=1'b0;EXE2MEM_RegWrite<=1'b0;endelsebeginEXE2MEM_MemRead<=ID2EXE_MemRead;EXE2MEM_MemWrite<=ID2EXE_MemWrite;EXE2MEM_Branch<=ID2EXE_Branch;EXE2MEM_MemtoReg<=ID2EXE_MemtoReg;EXE2MEM_RegWrite<=ID2EXE_RegWrite;endalways@(posedge clk,posedge rst)if(rst)beginEXE2MEM_ALUOut<=32'd0;EXE2MEM_RegRd<=5'd0;EXE2MEM_Rt<=32'd0;EXE2MEM_BranchPC<=32'd0;EXE2MEM_NPC<=32'd0;endelsebeginEXE2MEM_RegRd<=(ID2EXE_RegDst)?ID2EXE_rd:ID2EXE_rt;EXE2MEM_Rt<=ALUSrcB; //ID2EXE_Rt or forwarded from EXE2MEM or forwarded from MEM2WBEXE2MEM_BranchPC<=ID2EXE_NPC+(ID2EXE_Imm<<2);EXE2MEM_NPC<=ID2EXE_NPC;case(ID2EXE_ALUOp)2'b00:EXE2MEM_ALUOut<=ALUSrcA+ID2EXE_Imm;//lw or sw2'b01:EXE2MEM_ALUOut<=(ALUSrcA!=ALUSrcB)?1:0;2'b10:case (ID2EXE_Imm[5:0])6'b100000:EXE2MEM_ALUOut<=ALUSrcA+ALUSrcB;6'b100010:EXE2MEM_ALUOut<=ALUSrcA-ALUSrcB;6'b100100:EXE2MEM_ALUOut<=ALUSrcA&ALUSrcB;6'b100101:EXE2MEM_ALUOut<=ALUSrcA|ALUSrcB;6'b101010:EXE2MEM_ALUOut<=(ALUSrcA<ALUSrcB)?1:0;default: EXE2MEM_ALUOut<=EXE2MEM_ALUOut;endcasedefault:EXE2MEM_ALUOut<=EXE2MEM_ALUOut;endcaseend//MEMalways@(posedge clk,posedge rst)if(rst)beginMEM2WB_RegWrite<=1'b0;MEM2WB_MemtoReg<=1'b0;endelsebeginMEM2WB_RegWrite<=EXE2MEM_RegWrite;MEM2WB_MemtoReg<=EXE2MEM_MemtoReg;endalways@(posedge clk,posedge rst)if(rst)beginMEM2WB_LDM<=32'd0;MEM2WB_ALUOut<=32'd0;MEM2WB_RegRd<=5'd0;endelsebeginMEM2WB_ALUOut<=EXE2MEM_ALUOut;MEM2WB_RegRd<=EXE2MEM_RegRd;if(EXE2MEM_MemRead)MEM2WB_LDM<=DMEM[EXE2MEM_ALUOut];else if(EXE2MEM_MemWrite)DMEM[EXE2MEM_ALUOut]<=EXE2MEM_Rt;end//WBalways@(negedge clk,posedge rst)if(rst);else if(MEM2WB_RegWrite)if(MEM2WB_MemtoReg)RegFile[MEM2WB_RegRd]<=MEM2WB_LDM;elseRegFile[MEM2WB_RegRd]<=MEM2WB_ALUOut;endmodule

BTB代码:

`timescale 1ns / 1ps//// Company: // Engineer: // // Create Date: 2020/09/11 19:02:52// Design Name: // Module Name: BTB// Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision:// Revision 0.01 - File Created// Additional Comments:// //module BTB(inputclk, rst,input [31:0] rd_PC,//输入PCoutput reg rd_predicted,//对外输出的信号, 表示rd_PC是跳转指令,此时rd_predicted_PC是有效数据output reg [31:0] rd_predicted_PC,//从buffer中得到的预测PCinput wr_req,//写请求信号input [31:0] wr_PC,//要写入的分支PCinput [31:0] wr_predicted_PC,//要写入的预测PCinput wr_predicted_state_bit//要写入的预测状态位);parameter BUFFER_ADDR_LEN=4;parameter BUFFER_SIZE=1<<BUFFER_ADDR_LEN;parameter TAG_LEN=32-BUFFER_ADDR_LEN-2;reg [TAG_LEN-1:0] tag_pc [0:BUFFER_SIZE-1]; reg [31:0] predicted_pc [0:BUFFER_SIZE-1];reg predicted_state [0:BUFFER_SIZE-1];wire [TAG_LEN-1:0] rd_pc_tag;wire [BUFFER_ADDR_LEN-1:0] rd_pc_index;wire [1:0] rd_pc_offset;wire [TAG_LEN-1:0] wr_pc_tag;wire [BUFFER_ADDR_LEN-1:0] wr_pc_index;wire [1:0] wr_pc_offset;assign {rd_pc_tag,rd_pc_index,rd_pc_offset}=rd_PC;assign {wr_pc_tag,wr_pc_index,wr_pc_offset}=wr_PC;always@(*)if(tag_pc[rd_pc_index]==rd_pc_tag&&predicted_state[rd_pc_index]==1'b1)rd_predicted=1'b1;elserd_predicted=1'b0;always@(*) rd_predicted_PC=predicted_pc[rd_pc_index];//write buffer,updateinteger i;always@(posedge clk,posedge rst)if(rst)beginfor(i=0;i<BUFFER_SIZE;i=i+1)begintag_pc[i]<=0;predicted_pc[i]<=0;predicted_state[i]<=0;endendelse if(wr_req)begintag_pc[wr_pc_index]<=wr_pc_tag;predicted_pc[wr_pc_index]<=wr_predicted_PC;predicted_state[wr_pc_index]<=wr_predicted_state_bit;endendmodule

其余模块代码见链接 之前的MIPS代码有问题,在更新PC时,若预测错误,则PC更新为新的跳转地址的优先级应该高于因load-use数据冒险而产生的阻塞(Stall),现代码更改如下:

`timescale 1ns / 1ps//// Company: // Engineer: // // Create Date: 2020/07/23 15:31:35// Design Name: // Module Name: MIPS// Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision:// Revision 0.01 - File Created// Additional Comments:// //module MIPS(input clk,input rst);reg [31:0] RegFile [0:31];reg [31:0] IMEM [0:255];reg [31:0] DMEM [0:255];integer i;initialbeginfor(i=0;i<256;i=i+1)DMEM[i]=i;endinitialbeginfor(i=0;i<32;i=i+1)RegFile[i]<=i;endinitialbeginIMEM[0]=32'h8c810004;IMEM[4]=32'h00211820;IMEM[8]=32'h00c23022;IMEM[12]=32'h10c0fffc;IMEM[16]=32'h012a4020;IMEM[20]=32'h01ac5822;IMEM[24]=32'h01f07020;endwire IF2ID_Write;wire PCWrite;wire ID2EXE_Stall;wire [31:0]ALUSrcA;wire [31:0]ALUSrcB;wire [1:0]ForwardA;wire [1:0]ForwardB;wire [4:0]IF2ID_rs;wire [4:0]IF2ID_rt;wire [31:0]MEM2WB_Data;wire FlushIF;wire FlushID;wire FlushEXE;assign FlushIF=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0; //若预测错误,则flush之前的3条指令assign FlushID=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0;assign FlushEXE=(EXE2MEM_Branch==1'b1&&(EXE2MEM_ALUOut!=EXE2MEM_predicted))?1'b1:1'b0;reg wr_req;reg [31:0]wr_PC;reg [31:0]wr_predicted_PC;reg wr_predicted_state_bit;reg [31:0]rd_PC;wire rd_predicted_PC;wire rd_predicted;reg IF2ID_predicted;reg ID2EXE_predicted;reg EXE2MEM_predicted;//更新BTBalways@(*)if(EXE2MEM_Branch)if(EXE2MEM_ALUOut==1'b0&&EXE2MEM_predicted==1'b1) //预测taken但不takenbeginwr_req=1'b1;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;endelse if(EXE2MEM_ALUOut==1'b1&&EXE2MEM_predicted==1'b0)//预测不taken但takenbeginwr_req=1'b1;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b1;endelsebeginwr_req=1'b0;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;endelsebeginwr_req=1'b0;wr_PC=EXE2MEM_NPC-32'd4;wr_predicted_PC=EXE2MEM_BranchPC;wr_predicted_state_bit=1'b0;end//传递rd_predicted,即预测结果,以与之后的实际结果相比较always@(posedge clk,posedge rst)if(rst)beginIF2ID_predicted<=1'b0;ID2EXE_predicted<=1'b0;EXE2MEM_predicted<=1'b0;endelsebeginIF2ID_predicted<=rd_predicted;ID2EXE_predicted<=IF2ID_predicted;EXE2MEM_predicted<=ID2EXE_predicted;end//IFreg [31:0] PC;reg [31:0] IF2ID_NPC;reg [31:0] IF2ID_IR;//IDreg [4:0]ID2EXE_rs;//Rs寄存器号 约定大写为数据,小写为寄存器�?reg [4:0]ID2EXE_rt;//Rt寄存器号reg [4:0]ID2EXE_rd;//Rd寄存器号reg [31:0]ID2EXE_Rs; //Rs寄存器内�?reg [31:0]ID2EXE_Rt; //Rt寄存器内�?reg [31:0]ID2EXE_Imm;//符号扩展后的立即�?reg [31:0]ID2EXE_NPC;reg ID2EXE_RegDst;reg ID2EXE_ALUSrc;reg ID2EXE_MemRead;reg ID2EXE_MemWrite;reg ID2EXE_Branch;reg ID2EXE_MemtoReg;reg ID2EXE_RegWrite;reg [1:0] ID2EXE_ALUOp;//EXEreg [31:0]EXE2MEM_ALUOut;reg [31:0]EXE2MEM_BranchPC;reg [31:0]EXE2MEM_Rt;reg [4:0]EXE2MEM_RegRd;//WB阶段写寄存器�?reg EXE2MEM_MemRead;reg EXE2MEM_MemWrite;reg EXE2MEM_Branch;reg EXE2MEM_MemtoReg;reg EXE2MEM_RegWrite;reg [31:0]EXE2MEM_NPC;//MEMreg [31:0]MEM2WB_LDM;reg [31:0]MEM2WB_ALUOut;reg [4:0]MEM2WB_RegRd;reg MEM2WB_RegWrite;reg MEM2WB_MemtoReg;/****************************************************************************************/assign IF2ID_rs=IF2ID_IR[25:21];assign IF2ID_rt=IF2ID_IR[20:16];assign MEM2WB_Data=(MEM2WB_MemtoReg)?MEM2WB_LDM:MEM2WB_ALUOut;//IFBTB V(.clk(clk),.rst(rst),.rd_PC(PC),.rd_predicted(rd_predicted),.rd_predicted_PC(rd_predicted_PC),.wr_req(wr_req),.wr_PC(wr_PC),.wr_predicted_PC(wr_predicted_PC),.wr_predicted_state_bit(wr_predicted_state_bit));always@(posedge clk,posedge rst)beginif(rst)beginPC<=32'd0;IF2ID_NPC<=32'd0;IF2ID_IR<=32'd0;endelsebeginif(EXE2MEM_Branch)if(EXE2MEM_predicted==1'b1&&EXE2MEM_ALUOut==1'b0) //预测taken但实际没tabkePC<=EXE2MEM_NPC;else if(EXE2MEM_predicted==1'b0&&EXE2MEM_ALUOut==1'b1)//预测not taken但实际takenPC<=EXE2MEM_BranchPC;else if(PCWrite==1'b0)PC<=PC;else if(rd_predicted)PC<=rd_predicted_PC;elsePC<=PC+32'd4;else if(PCWrite==1'b0)PC<=PC;else if(rd_predicted)PC<=rd_predicted_PC;elsePC<=PC+32'd4;//if(FlushIF)beginIF2ID_NPC<=32'd0;IF2ID_IR<=32'd0;endelse if(IF2ID_Write)beginIF2ID_NPC<=PC+4;IF2ID_IR<=IMEM[PC];endelsebeginIF2ID_NPC<=IF2ID_NPC;IF2ID_IR<=IF2ID_IR;endendend//IDHAZARD_DETECTION U1(.ID2EXE_MemRead(ID2EXE_MemRead),.IF2ID_rs(IF2ID_rs),.IF2ID_rt(IF2ID_rt),.ID2EXE_rt(ID2EXE_rt),.PCWrite(PCWrite),.ID2EXE_Stall(ID2EXE_Stall),.IF2ID_Write(IF2ID_Write));always@(posedge clk,posedge rst)if(rst)beginID2EXE_RegDst<=1'b0;ID2EXE_ALUSrc<=1'b0;ID2EXE_ALUOp<=2'b00;ID2EXE_MemRead<=1'b0;ID2EXE_MemWrite<=1'b0;ID2EXE_Branch<=1'b0;ID2EXE_RegWrite<=1'b0;ID2EXE_MemtoReg<=1'b0;endelse if(ID2EXE_Stall||FlushID)beginID2EXE_RegDst<=1'b0;ID2EXE_ALUSrc<=1'b0;ID2EXE_ALUOp<=2'b00;ID2EXE_MemRead<=1'b0;ID2EXE_MemWrite<=1'b0;ID2EXE_Branch<=1'b0;ID2EXE_RegWrite<=1'b0;ID2EXE_MemtoReg<=1'b0;endelsecase(IF2ID_IR[31:26])6'b000000:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b100100010;end6'b100011:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b011110000;end6'b101011:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b010001000;end6'b000100:begin{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b000000101;enddefault:{ID2EXE_RegDst,ID2EXE_ALUSrc,ID2EXE_MemtoReg,ID2EXE_RegWrite,ID2EXE_MemRead,ID2EXE_MemWrite,ID2EXE_Branch,ID2EXE_ALUOp}<=9'b000000000;endcasealways@(posedge clk,posedge rst)if(rst)beginID2EXE_rs<=5'd0;ID2EXE_rt<=5'd0;ID2EXE_rd<=5'd0;ID2EXE_Rs<=32'd0;ID2EXE_Rt<=32'd0;ID2EXE_Imm<=32'd0;ID2EXE_NPC<=32'd0;endelse if(FlushID)beginID2EXE_rs<=5'd0;ID2EXE_rt<=5'd0;ID2EXE_rd<=5'd0;ID2EXE_Rs<=32'd0;ID2EXE_Rt<=32'd0;ID2EXE_Imm<=32'd0;ID2EXE_NPC<=32'd0;endelsebeginID2EXE_rs<=IF2ID_IR[25:21];ID2EXE_rt<=IF2ID_IR[20:16];ID2EXE_rd<=IF2ID_IR[15:11];ID2EXE_Rs<=RegFile[IF2ID_IR[25:21]];ID2EXE_Rt<=RegFile[IF2ID_IR[20:16]];ID2EXE_Imm<={{16{IF2ID_IR[15]}},IF2ID_IR[15:0]};ID2EXE_NPC<=IF2ID_NPC;end//EXEFORWARDING U2(.ID2EXE_rs(ID2EXE_rs),.ID2EXE_rt(ID2EXE_rt),.EXE2MEM_RegRd(EXE2MEM_RegRd),.MEM2WB_RegRd(MEM2WB_RegRd),.EXE2MEM_RegWrite(EXE2MEM_RegWrite),.MEM2WB_RegWrite(MEM2WB_RegWrite),.ForwardA(ForwardA),.ForwardB(ForwardB));//ALUSrcA三�?�一mux3_1 U3(.a(ID2EXE_Rs),.b(EXE2MEM_ALUOut),.c(MEM2WB_Data),.select(ForwardA),.out(ALUSrcA));//ALUSrcBmux3_1 U4(.a(ID2EXE_Rt),.b(EXE2MEM_ALUOut),.c(MEM2WB_Data),.select(ForwardB),.out(ALUSrcB));always@(posedge clk,posedge rst)if(rst)beginEXE2MEM_MemRead<=1'b0;EXE2MEM_MemWrite<=1'b0;EXE2MEM_Branch<=1'b0;EXE2MEM_MemtoReg<=1'b0;EXE2MEM_RegWrite<=1'b0;endelse if(FlushEXE)beginEXE2MEM_MemRead<=1'b0;EXE2MEM_MemWrite<=1'b0;EXE2MEM_Branch<=1'b0;EXE2MEM_MemtoReg<=1'b0;EXE2MEM_RegWrite<=1'b0;endelsebeginEXE2MEM_MemRead<=ID2EXE_MemRead;EXE2MEM_MemWrite<=ID2EXE_MemWrite;EXE2MEM_Branch<=ID2EXE_Branch;EXE2MEM_MemtoReg<=ID2EXE_MemtoReg;EXE2MEM_RegWrite<=ID2EXE_RegWrite;endalways@(posedge clk,posedge rst)if(rst)beginEXE2MEM_ALUOut<=32'd0;EXE2MEM_RegRd<=5'd0;EXE2MEM_Rt<=32'd0;EXE2MEM_BranchPC<=32'd0;EXE2MEM_NPC<=32'd0;endelsebeginEXE2MEM_RegRd<=(ID2EXE_RegDst)?ID2EXE_rd:ID2EXE_rt;EXE2MEM_Rt<=ALUSrcB; //ID2EXE_Rt or forwarded from EXE2MEM or forwarded from MEM2WBEXE2MEM_BranchPC<=ID2EXE_NPC+(ID2EXE_Imm<<2);EXE2MEM_NPC<=ID2EXE_NPC;case(ID2EXE_ALUOp)2'b00:EXE2MEM_ALUOut<=ALUSrcA+ID2EXE_Imm;//lw or sw2'b01:EXE2MEM_ALUOut<=(ALUSrcA!=ALUSrcB)?1:0;2'b10:case (ID2EXE_Imm[5:0])6'b100000:EXE2MEM_ALUOut<=ALUSrcA+ALUSrcB;6'b100010:EXE2MEM_ALUOut<=ALUSrcA-ALUSrcB;6'b100100:EXE2MEM_ALUOut<=ALUSrcA&ALUSrcB;6'b100101:EXE2MEM_ALUOut<=ALUSrcA|ALUSrcB;6'b101010:EXE2MEM_ALUOut<=(ALUSrcA<ALUSrcB)?1:0;default: EXE2MEM_ALUOut<=EXE2MEM_ALUOut;endcasedefault:EXE2MEM_ALUOut<=EXE2MEM_ALUOut;endcaseend//MEMalways@(posedge clk,posedge rst)if(rst)beginMEM2WB_RegWrite<=1'b0;MEM2WB_MemtoReg<=1'b0;endelsebeginMEM2WB_RegWrite<=EXE2MEM_RegWrite;MEM2WB_MemtoReg<=EXE2MEM_MemtoReg;endalways@(posedge clk,posedge rst)if(rst)beginMEM2WB_LDM<=32'd0;MEM2WB_ALUOut<=32'd0;MEM2WB_RegRd<=5'd0;endelsebeginMEM2WB_ALUOut<=EXE2MEM_ALUOut;MEM2WB_RegRd<=EXE2MEM_RegRd;if(EXE2MEM_MemRead)MEM2WB_LDM<=DMEM[EXE2MEM_ALUOut];else if(EXE2MEM_MemWrite)DMEM[EXE2MEM_ALUOut]<=EXE2MEM_Rt;end//WBalways@(negedge clk,posedge rst)if(rst);else if(MEM2WB_RegWrite)if(MEM2WB_MemtoReg)RegFile[MEM2WB_RegRd]<=MEM2WB_LDM;elseRegFile[MEM2WB_RegRd]<=MEM2WB_ALUOut;endmodule

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。