杭州电子科技大学计算机组成实验:代码和解析
顶层模块(需要数据输入在板子上操作):
module RegisterFile(Addr,Write_Reg,Opt,Clk,Reset,A_B,LED);
input [1:0]Opt;
input [4:0]Addr;
input Write_Reg,Clk,Reset,A_B;
output reg [7:0]LED;
wire [31:0]R_Data_A,R_Data_B;
reg [4:0]R_Addr_A,R_Addr_B;
reg [31:0]W_Data;
initial
LED <= 0;
Fourth_experiment_first F1(R_Addr_A,R_Addr_B,Write_Reg,R_Data_A,R_Data_B,Reset,Clk,Addr,W_Data);
always@(Addr or Write_Reg or Opt or A_B or R_Data_A or R_Data_B)
begin
if(Write_Reg)
begin
case(Opt)
2'b00: begin W_Data=32'h000f_000f; end
2'b01: begin W_Data=32'h0f0f_0f00; end
2'b10: begin W_Data=32'hf0f0_f0f0; end
2'b11: begin W_Data=32'hffff_ffff; end
endcase
end
else
if(A_B)
begin
R_Addr_A=Addr;
case(Opt)
2'b00: LED=R_Data_A[7:0];
2'b01: LED=R_Data_A[15:8];
2'b10: LED=R_Data_A[23:16];
2'b11: LED=R_Data_A[31:24];
endcase
end
else
begin
R_Addr_B=Addr;
case(Opt)
2'b00: LED=R_Data_B[7:0];
2'b01: LED=R_Data_B[15:8];
2'b10: LED=R_Data_B[23:16];
2'b11: LED=R_Data_B[31:24];
endcase
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;
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
测试模块
module Fourth_experiment_test;
// Inputs
reg [4:0] Addr;
reg Write_Reg;
reg [1:0] Opt;
reg Clk;
reg Reset;
reg A_B;
// Outputs
wire [7:0] LED;
RegisterFile uut (
.Addr(Addr),
.Write_Reg(Write_Reg),
.Opt(Opt),
.Clk(Clk),
.Reset(Reset),
.A_B(A_B),
.LED(LED)
);
always #20 Clk = ~Clk;
initial begin
Addr = 5'b00001;
Write_Reg = 1;
Opt = 0;
Clk = 1;
Reset = 0;
A_B = 0;
#100;
Addr = 5'b00001;
Write_Reg = 0;
Opt = 0;
A_B = 0;
end
endmodule