實驗內容
1. 掌握MIPS R型指令的資料通路設計,掌握指令流和資料流的控制方法
2. 掌握完整的單周期CPU頂層模塊的設計方法
3. 實作MIPS R型指令的功能
解決方法
1. 分析MIPS R型指令的特點,OP均為000000b,可作為判斷R型指令的依據
2. 兩個源運算元分別在rs和rt欄位所指定的暫存器中,而目的運算元則是在rd欄位所指定的暫存器
3. 分析資料通路是從指令存盤器取出的指令經過初級譯碼,將分解出的源暫存器rs、rt直接與暫存器堆的兩個讀埠A和B的暫存器地址連接,而將目的暫存器rd欄位與暫存器堆的寫埠地址相連,暫存器讀出的A口資料和B口資料則直接連接到ALU的輸入端A和B,ALU計算后的結果則送入暫存器堆的寫資料埠
4. 分析時序,首先是取指令,然后進行指令譯碼,最后將運算結果送到目的暫存器,PC自增,指向下一條指令
5. 實驗八中除譯碼模塊,其他模塊都已經前幾個實驗中完成,只需要將其連接,然后在譯碼模塊中通過OP判斷是否為R型,且將funct翻譯成ALU的控制信號ALU_OP以及指定ALU的運算功能,由于此譯碼模塊較為簡單,我就在頂層模塊實作了
6. 具體用到的實驗是多功能ALU設計實驗、暫存器堆設計實驗、取指令與指令譯碼實驗
7. 代碼實作:
頂層模塊
module CPU(clk,rst,op_code,funct,rs_addr,rt_addr,rd_addr,shamt,OF,ZF,F,ALU_OP,Inst_code);
input clk,rst;
output [31:0]Inst_code;
output [5:0]op_code,funct;
output [4:0]rs_addr,rt_addr,rd_addr,shamt;
output [31:0]F;
output OF,ZF;
output reg [2:0]ALU_OP;
reg Write_Reg;
wire [31:0]R_Data_A,R_Data_B;
PC pc1(clk,rst,Inst_code);
assign op_code = Inst_code[31:26];
assign rs_addr = Inst_code[25:21];
assign rt_addr = Inst_code[20:16];
assign rd_addr = Inst_code[15:11];
assign shamt = Inst_code[10:6];
assign funct = Inst_code[5:0];
Fourth_experiment_first F1(rs_addr,rt_addr,Write_Reg,R_Data_A,R_Data_B,rst,~clk,rd_addr,F);
Third_experiment_first T1(OF,ZF,ALU_OP,R_Data_A,R_Data_B,F);
always@(*)
begin
Write_Reg<=0;
ALU_OP=0;
if(op_code==0)
Write_Reg<=1;
begin
case(funct)
6'b100000:ALU_OP=3'b100;
6'b100010:ALU_OP=3'b101;
6'b100100:ALU_OP=3'b000;
6'b100101:ALU_OP=3'b001;
6'b100110:ALU_OP=3'b010;
6'b100111:ALU_OP=3'b011;
6'b101011:ALU_OP=3'b110;
6'b000100:ALU_OP=3'b111;
endcase
end
end
endmodule
PC(取指令模塊)
module PC(clk,rst,Inst_code);
input clk,rst;
wire [31:0]PC_new;
reg[31:0]PC;
initial
PC = 32'h0000_0000;
output [31:0]Inst_code;
assign PC_new = PC +4;
Inst_Rom your_instance_name(
.clka(clk), // input clka
.addra(PC[7:2]), // input [5 : 0] addra
.douta(Inst_code) // output [31 : 0] douta
);
always@(posedge clk or posedge rst)
begin
if(rst)
begin PC <= 32'B0000_0000; end
else
begin PC <= PC_new; end
end
endmodule
Fourth_experiment_first模塊(暫存器堆模塊)
module Fourth_experiment_first(R_Addr_A,R_Addr_B,Write_Reg,R_Data_A,R_Data_B,Reset,Clk,W_Addr,W_Data);
input [4:0]R_Addr_A,R_Addr_B,W_Addr;
input Write_Reg,Reset,Clk;
input[31:0] W_Data;
output [31:0] R_Data_A,R_Data_B;
reg [31:0] REG_Files[0:31];
integer i=0;
initial
for(i=0;i<32;i=i+1) REG_Files[i]<=0;
always @ (posedge Clk or posedge Reset)
begin
if(Reset)
begin
for(i=0;i<=31;i=i+1)
REG_Files[i]<=0;
end
else
begin
if(Write_Reg)
REG_Files[W_Addr]<=W_Data;
end
end
assign R_Data_A = REG_Files[R_Addr_A];
assign R_Data_B = REG_Files[R_Addr_B];
endmodule
Third_experiment_first(ALU模塊)
module Third_experiment_first(OF,ZF,ALU_OP,A,B,F);
input [2:0]ALU_OP;
input [31:0]A,B;
output reg[31:0]F;
reg C32;
output reg OF;
output reg ZF;
always @(ALU_OP or A or B)
begin
OF = 0;
C32 = 0;
case(ALU_OP)
3'b000:F<=A&B;
3'b001:F<=A|B;
3'b010:F<=A^B;
3'b011:F<=A~^B;
3'b100:{C32,F}<=A+B;
3'b101:{C32,F}<=A-B;
3'b110:begin if(A<B) F<=32'h0000_0001;else F<=32'h0000_0000;end
3'b111:begin F<=B<<A;end
endcase
if(F==32'h0000_0000)
ZF<=1;
else
ZF<=0;
if(ALU_OP == 3'b100 || ALU_OP == 3'b101)
OF<=C32^F[31]^A[31]^B[31];
else
OF <=0;
end
endmodule
MIPS32.coe
memory_initialization_radix=16;
memory_initialization_vector=00000827 0001102b 00421820 00622020 00832820 00a33020 00463804 00a64820 01264004
00284826 01215020 01075822 00e86022 012c6824 012c7025 00c77825 00c78027 00e38820 02289004 02239804 00f3a004
0281a820 0255b025 0296b820 0296c022 02d4c822 0241d026 02d4d82b 0354e02b 02c2e820 0282f022 017af820;
測驗模塊
module test;
// Inputs
reg clk;
reg rst;
// Outputs
wire [5:0] op_code;
wire [5:0] funct;
wire [4:0] rs_addr;
wire [4:0] rt_addr;
wire [4:0] rd_addr;
wire [4:0] shamt;
wire OF;
wire ZF;
wire [31:0] F;
wire [2:0] ALU_OP;
wire [31:0] Inst_code;
CPU uut (
.clk(clk),
.rst(rst),
.op_code(op_code),
.funct(funct),
.rs_addr(rs_addr),
.rt_addr(rt_addr),
.rd_addr(rd_addr),
.shamt(shamt),
.OF(OF),
.ZF(ZF),
.F(F),
.ALU_OP(ALU_OP),
.Inst_code(Inst_code)
);
always #20 clk = ~clk;
initial begin
clk = 0;
rst = 1;
#2;
rst = 0;
end
endmodule
友情提示:開始不拉高rst,第一條指令取不到噢,解釋可以看實驗七,頂上有鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/240050.html
標籤:其他
上一篇:Linux[CentOS 7]下搭建hadoop偽分布式
下一篇:淺談功分器
