`timescale 1ns / 1ps
module top(
input refclk,
input rst_n,
// output rst_o,
output [4:0] raddr1,raddr2,waddr,
output [31:0] alu_o,adder2_o,
output [31:0] pc_out,
output [31:0] wdata,rdata1,rdata2
);
wire bra;
wire [31:0] adder1_o; // ,adder2_o;
wire [31:0] pc_o; // ,pc_out;
wire [31:0] instr;
wire [32:0] imme_o;
wire [4:0] rindex1,rindex2,windex;
// wire [31:0] wdata,rdata1,rdata2;
wire [1:0] LSctrl;
wire RegWrite;
wire [1:0] ALUOp;
wire [3:0] alu_ctrl;
wire [8:0] funct9;
wire [31:0] rmux_o;
// wire [31:0] alu_o;
wire [31:0] adder2_w;
wire alu_zero;
wire [31:0] mdata;
wire [31:0] wdata_w;
wire [1:0] LSctrl_o;
wire RegWrite_o;
wire [1:0] RegWrSrc_o;
// wire rst_o;
wire reset = ~rst_n;
clk_wiz_0 CLOCK
(
.clk_out1(clk),
.clk_out1_n(clk_2),
.reset(reset),
.locked(),
.clk_in1(refclk)
);
RESET U0 (.rst_n(rst_o),.clk,.rst_in(rst_n));
Mux21 U1 (.out(pc_o),.in0(adder1_o),.in1(adder2_w),.sel(bra));
PC U2 (.out(pc_out),.clk(clk), .rst_n(rst_o), .in(pc_o));
Adder U3 (.out(adder1_o),.in0(pc_out), .in1(32'd4));
I_mem U4 (.clk(clk),.rst_n(rst_o),.instr(instr),.addr(pc_out));
// IF_ID
assign rindex1 = instr[19:15];
assign rindex2 = instr[24:20]; //ignores /I,U,UJ Type
assign windex = instr[11:7];
assign raddr1 = rindex1;
assign raddr2 = rindex2;
assign waddr = windex;
Imme_gen U5 (.clk(clk),.imme_o(imme_o),.instr(instr));
regFiles U6 (
.rdata1(rdata1),
.rdata2(rdata2),
.clk(clk),
.rst_n(rst_o),
.rindex1(raddr1),
.rindex2(raddr2),
.windex(waddr),
.wdata(wdata),
.RegWrite(RegWrite)
);
Control U7 (
.inst(instr[6:0]),
.funct(instr[14:12]),
.Branch(Branch),
.MemRead(MemRead_o),
.MemtoReg(MemtoReg),
.ALUOp(ALUOp),
.MemWrite(MemWrite_o),
.ALUSrc(ALUSrc_o),
.RegWrite(RegWrite_o),
.IsJalr(IsJalr_o),
.IsSlti(IsSlti_o),
.LSctrl(LSctrl_o),
.RegWrSrc(RegWrSrc_o)
);
assign MemRead=MemRead_o;
assign MemWrite=MemWrite_o;
assign RegWrite=RegWrite_o;
assign ALUSrc=ALUSrc_o;
assign IsJalr=IsJalr_o;
assign LSctrl=LSctrl_o;
assign IsSlti=IsSlti_o;
assign RegWrSrc=RegWrSrc_o;
//hot assign instrw={instr[30],instr[14:12]};
assign funct9={instr[6:2],instr[30],instr[14:12]};
ALU_Control U8 (.funct9(funct9), .ALUOp(ALUOp), .out(alu_ctrl));
// ID_EX
Adder U10 (.out(adder2_o),.in0(pc_out),.in1(imme_o[32:1]));
Mux21 U11 (.out(rmux_o),.in0(rdata2),.in1(imme_o[31:0]),.sel(ALUSrc));
ALU U13 (.out(alu_o),.zero(alu_zero),.in0(rdata1),.in1(rmux_o),.control(alu_ctrl));
// Mux21 U14 (.out(adder2_w),.in0(alu_o),.in1(adder2_o),.sel(IsJalr));
Mux21 U14 (.out(adder2_w),.in0(imme_o[32:1]),.in1(alu_o),.sel(IsJalr));
// EX_MEM
and U15 (bra,Branch,alu_zero);
DataMemory U16 (
.mdata(mdata),
.clk(clk),
.LSctrl(LSctrl),
.addr(alu_o),
.wdata(rdata2),
.MemWrite(MemWrite),
.MemRead(MemRead)
);
// MEM_EX
Mux21 U18 (.out(wdata_w),.in0(alu_o),.in1(mdata),.sel(MemtoReg));
Mux21 U19 (.out(wdata),.in0(wdata_w),.in1({31'b0,alu_zero}),.sel(IsSlti));
endmodule
`timescale 1ns / 1ps
module Adder (out,in0, in1);
output [31:0] out;
input [31:0] in0, in1;
reg [31:0] out;
always@(*)
begin
out = in0 + in1;
end
endmodule
`timescale 1ns / 1ps
module Mux21 (out,in0,in1,sel);
output reg [31:0] out;
input [31:0] in0, in1;
input sel;
always @(*)
begin
if (sel)
out = in1;
else
out = in0;
end
endmodule
`timescale 1ns / 1ps
// module Imme_gen (imme_o,instr);
module Imme_gen (clk,imme_o,instr);
output [32:0] imme_o;
reg [32:0] imme_o;
input clk;
input [31:0] instr;
// output [31:0] imme_o;
// reg [31:0] imme_o;
localparam R_Type =7'b0110011,
R_Type1=7'b0111011,
I_Type =7'b0000011,
I_Type1=7'b0010011,
I_Type2=7'b1100111, // jalr
I_Type3=7'b0011011, // I slti, I sli, I srai,
I_Type4=7'b0001111, //I fence
I_Type5=7'b1110011, //I ecallebreak csrw csrrs csrrc csrrwi csrrsi csrrci
S_Type =7'b0100011,
SB_Type=7'b1100011,
U_Type =7'b0110111,
U_Type2 =7'b0100111,
UJ_Type=7'b1101111;
initial
begin
imme_o[32:0]='0;
end
//always @(instr)
always @(*) // changed by pipe
begin
imme_o='0;
case(instr[6:0])
I_Type,I_Type1,I_Type2: //I-type
begin
imme_o[11:0]=instr[31:20];
end
U_Type,U_Type2: //U-type (Upper Immediate)
begin
imme_o[31:12]=instr[31:12];
end
S_Type://S-type (Store)
begin
imme_o[11:5]=instr[31:25];
imme_o[4:0]=instr[11:7];
end
SB_Type: //SB-type (Branch)
begin
imme_o[12]=instr[31];
imme_o[10:5]=instr[30:25];
imme_o[4:1]=instr[11:8];
imme_o[11]=instr[7];
imme_o[0]=1'b0;
end
UJ_Type: //UJ-type (Jump)
begin
imme_o[20]=instr[31];
imme_o[10:1]=instr[30:21];
imme_o[11]=instr[20];
imme_o[19:12]=instr[19:12];
imme_o[0]=1'b0;
end
default:
begin
end
endcase
end
endmodule
`timescale 1ns / 1ps
module Control(inst, funct, Branch, MemRead, MemtoReg, ALUOp, MemWrite, ALUSrc, RegWrite, IsJalr,IsSlti, LSctrl, RegWrSrc);
output Branch, MemRead, MemWrite, ALUSrc, RegWrite, IsJalr;
output IsSlti;
output [1:0] ALUOp, LSctrl, RegWrSrc;
output MemtoReg;
input [6:0] inst;
input [2:0] funct;
reg Branch, MemRead, MemWrite, ALUSrc, RegWrite, IsJalr;
reg [1:0]ALUOp, LSctrl, RegWrSrc;
reg MemtoReg;
reg IsSlti;
localparam R_Type =7'b0110011,
R_Type1=7'b0111011,
I_Type =7'b0000011,
I_Type1=7'b0010011,
I_Type2=7'b1100111, // jalr
I_Type3=7'b0011011, // I slti, I sli, I srai,
I_Type4=7'b0001111, //I fence
I_Type5=7'b1110011, //I ecallebreak csrw csrrs csrrc csrrwi csrrsi csrrci
S_Type =7'b0100011,
SB_Type=7'b1100011,
U_Type =7'b0110111,
U_Type2 =7'b0010111,
UJ_Type=7'b1101111;
initial MemtoReg=1'b0;
always @(*) begin
case(inst)
R_Type,R_Type1: // add,sub,sll,slt,sltu,xor,srl,sra,or,and,addw,subw,sllw,srlw,sraw
begin
Branch = 1'b0;
MemRead = 1'b0;
RegWrSrc = 2'b00;
MemWrite = 1'b0;
ALUSrc = 1'b0;
RegWrite = 1'b1;
IsJalr = 1'b0;
IsSlti = funct==3'b010;
LSctrl = 2'b11;
ALUOp = 2'b10;
MemtoReg =1'b0; // ALU의 출력이 레지스터파일의 wrdata에 연결
end
// 7'b0010011: //addi,andi,slli, srli
I_Type1,I_Type3: // addi,slli,slti,sltiu,xori,srli,ori,andi,addiw,slliw,srliw,sraiw,
begin
Branch = 1'b0;
MemRead = 1'b0;
RegWrSrc = 2'b00;
MemWrite = 1'b0;
ALUSrc = 1'b1;
RegWrite = 1'b1;
IsJalr = 1'b0;
IsSlti = funct==3'b010;
LSctrl = 2'b11;
ALUOp = 2'b00;
MemtoReg =1'b0;
end
I_Type: //lb,lw,lh,ld,lbu,lhu,lwu,
begin
Branch = 1'b0;
MemRead = 1'b1;
RegWrSrc = 2'b01;
MemWrite = 1'b0;
MemtoReg = 1'b1;
ALUSrc = 1'b1;
RegWrite = 1'b1;
IsJalr = 1'b0;
IsSlti =1'b0;
ALUOp = 2'b00;
case(funct[2:0])
3'b000: LSctrl = 2'b01;//lb
3'b010: LSctrl = 2'b00;//lw
3'b100: LSctrl = 2'b10;//lbu
default: LSctrl = 2'b11;
endcase
end
S_Type: //sw,sb,sh
begin
Branch = 1'b0;
MemRead = 1'b0;
MemWrite = 1'b1; //hot
RegWrSrc = 2'b01;
ALUSrc = 1'b1;
RegWrite = 1'b0;
IsJalr = 1'b0;
IsSlti =1'b0;
ALUOp = 2'b00;
MemtoReg =1'b0;
case(funct[2:0])
3'b000: LSctrl = 2'b01;
3'b010: LSctrl = 2'b00;
3'b100: LSctrl = 2'b10;
default: LSctrl = 2'b11;
endcase
end
SB_Type: //beq,bne,blt,bge,bltu,bgeu
begin
Branch = 1'b1;
MemRead = 1'b0;
RegWrSrc = 2'b00;
MemWrite = 1'b0;
ALUSrc = 1'b0;
RegWrite = 1'b0;
ALUOp = 2'b01;
LSctrl = 2'b11;
IsJalr = 1'b0;
IsSlti =1'b0;
MemtoReg =1'b0;
end
UJ_Type: //jal
begin
Branch = 1'b1;
MemRead = 1'b0;
RegWrSrc = 2'b10;
MemWrite = 1'b0;
ALUSrc = 1'b0;
RegWrite = 1'b1;
ALUOp = 2'b11;
LSctrl = 2'b11;
IsJalr = 1'b0;
IsSlti =1'b0;
MemtoReg =1'b0;
end
I_Type2: //jalr
begin
Branch = 1'b1;
MemRead = 1'b0;
RegWrSrc = 2'b10;
MemWrite = 1'b0;
//hot ALUSrc = 1'b1;
ALUSrc = 1'b0;
RegWrite = 1'b1;
ALUOp = 2'b11;
LSctrl = 2'b11;
IsJalr = 1'b1;
IsSlti =1'b0;
MemtoReg =1'b0;
end
default: begin
Branch = 1'b0;
MemRead = 1'b0;
RegWrSrc = 2'b00;
MemWrite = 1'b0;
ALUSrc = 1'b0;
RegWrite = 1'b0;
ALUOp = 2'b00;
LSctrl = 2'b11;
IsJalr = 1'b0;
MemtoReg =1'b0;
IsSlti =1'b0;
end
endcase
end
endmodule
`timescale 1ns / 1ps
// module ALU_Control(funct9, ALUOp, out);
module ALU_Control(clk,funct9, ALUOp, out);
output [3:0] out;
input clk;
input [8:0] funct9; // OPCODE[6:2]+FUNCT7[5]+FUNCT[2:0]
input [1:0] ALUOp;
reg[3:0] out;
// always @(*) begin // changed by pipe
always @(clk) begin
case(ALUOp[1:0])
2'b00: //I-type
begin
if(funct9[8:4]==5'b00100 && funct9[2:0]==3'b010)
out <=4'b1011;
else if (funct9[2:0] == 3'b010 | funct9[2:0] == 3'b000 | funct9[2:0] == 3'b100)
out <= 4'b0010;//lw,lb,lbu,sw,sb,addi ??slti
else if(funct9[2:0] == 3'b000) out<= 4'b0010;//addi hot
else if (funct9[2:0] == 3'b001) out <= 4'b0011;//slli
else if (funct9[2:0] == 3'b010) out <= 4'b0011;//?slti
else if (funct9[2:0] == 3'b011) out <= 4'b0011;//?sltiu
else if (funct9[2:0] == 3'b100) out <= 4'b0101;//xori
else if (funct9[3]==1'b0 && funct9[2:0] == 3'b101) out <= 4'b0100;//srli
else if (funct9[3]==1'b1 && funct9[2:0] == 3'b101) out <= 4'b0100;//srai
else if (funct9[2:0] == 3'b110) out <= 4'b0001;//ori
else if (funct9[2:0] == 3'b111) out <= 4'b0000;//andi
end
2'b01: //B-type
begin
if (funct9[2:0] == 3'b000) out <= 4'b0110;//beq
else if (funct9[2:0] == 3'b001) out <= 4'b1110;//bne
else if (funct9[2:0] == 3'b100) out <= 4'b1011;//blt
else if (funct9[2:0] == 3'b101) out <= 4'b0111;//bge
else if (funct9[2:0] == 3'b110) out <= 4'b0111;//bltu
else if (funct9[2:0] == 3'b111) out <= 4'b0111;//?bgeu
end
2'b10: //R-type
begin
if (funct9[3:0] == 4'b1000) out <= 4'b0110;//sub
else if(funct9[3:0] == 4'b0000) out<= 4'b0010;//add
else if(funct9[3:0] == 4'b1000) out<= 4'b1000;//sub
else if (funct9[2:0] == 3'b001) out <= 4'b0011;//sll
else if (funct9[3:0] == 4'b0010) out <= 4'b0110;//slt
else if (funct9[3:0] == 4'b0100) out <= 4'b0101;// R xor
else if (funct9[3:0] == 4'b0110) out <= 4'b0001;// R or
else if (funct9[3:0] == 4'b0111) out <= 4'b0000;// R and
else if (funct9[3:0] == 4'b0101) out <= 4'b0100;//srl
else if (funct9[3:0] == 4'b1101) out <= 4'b0100;//sra
end
2'b11: //J-tupe
out <= 4'b1010;//jar,jalr
endcase
end
endmodule
`timescale 1ns / 1ps
module ALU (in0, in1, control, out, zero);
output [31:0] out;
output zero;
input [31:0] in0, in1;
input [3:0] control;
reg[31:0] out;
// reg[31:0] temp_out;
reg zero;
// integer k;
always @(in0, in1, control)
begin
case(control[3:0])
4'b0000: out <= in0 & in1;
4'b0001: out <= in0 | in1;
4'b0101: out <= in0 ^ in1;
// 4'b1001: out <= in0 in1;
// 4'b1101: out <= in0 in1;
// 4'b1111: out <= in0 in1;
4'b0010,4'b1010: out <= in0 + in1;
4'b1000,4'b0110,4'b1110,4'b0111,4'b1011: out <= in0 - in1;
4'b0011: out <= in0 << in1;
4'b0100: out <= in0 >> in1;//unsigned
4'b1100: begin //signed
out <= in0>>>in1;
end
default: out <= 32'b1;
endcase
end
always @(*) begin
case(control[3:0])
4'b1010: zero = 1'b1;//jal,jalr
4'b0110: begin
if(out[31:0]==32'b0)
zero = 1'b1;//beq
else
zero =1'b0;
end
4'b1110: begin
if(out[31:0] !=32'b0)
zero = 1'b1;//bne
else
zero =1'b0;
end
4'b0111: begin
if (~out[31]) zero = 1'b1;//bge
else
zero = 1'b0;
end
4'b1011: begin
if(out[31])
zero = 1'b1;//blt
else
zero = 1'b0;
end
default: zero=1'b0;
endcase
end
endmodule
`timescale 1ns / 1ps
module DataMemory ( clk,LSctrl,addr,wdata,MemWrite,MemRead,mdata);
output reg [31:0] mdata;
input clk;
input [1:0] LSctrl; //to be added into control
input [31:0] addr, wdata;
input MemWrite, MemRead;
localparam N=32;
reg [7:0] dmem[N-1:0][0:3];
integer i;
initial
begin
for (i=0; i<N; i=i+1)
begin
dmem[i][0]='0;
dmem[i][1]='0;
dmem[i][2]='0;
dmem[i][3]='0;
end
end
always @(posedge clk) begin
mdata[7:0] = #0.1 'z;
mdata[15:8] = #0.1 'z;
mdata[23:16] = #0.1 'z;
mdata[31:24] = #0.1 'z;
if (MemWrite) begin // store
if (LSctrl == 2'b00)
{dmem[addr[31:2]][3],dmem[addr[31:2]][2],dmem[addr[31:2]][1],dmem[addr[31:2]][0]} = wdata;//sw
else if (LSctrl == 2'b01)
begin
case(addr[1:0])
2'b00: dmem[addr[31:2]][0] = wdata[7:0];//sb
2'b01: dmem[addr[31:2]][1] = wdata[7:0];//sb
2'b10: dmem[addr[31:2]][2] = wdata[7:0];//sb
2'b11: dmem[addr[31:2]][3] = wdata[7:0];//sb
endcase
end
else if (LSctrl == 2'b10)
begin
case(addr[1:0])
2'b00:begin
dmem[addr[31:2]][0] = wdata[7:0];//shw
dmem[addr[31:2]][1] = wdata[15:8];//shw
end
2'b01: begin
dmem[addr[31:2]][1] = wdata[7:0]; //shw
dmem[addr[31:2]][2] = wdata[15:8];//shw
end
2'b10: begin
dmem[addr[31:2]][2] = wdata[7:0]; //shw
dmem[addr[31:2]][3] = wdata[15:8];//shw
end
2'b10: begin
dmem[addr[31:2]][3] = wdata[7:0]; //shw
// dmem[addr[31:2][0] = 'z; //shw for compiler
dmem[addr[31:2]+1'b1][0] = wdata[15:8]; //for highlevel code compiler
end
endcase
end
end
else if (MemRead) begin //load
if (LSctrl == 2'b00)
// mdata = #0.1 {dmem[addr[31:2]][3],dmem[addr[31:2]][2],dmem[addr[31:2]][1],dmem[addr[31:2]][0]};//lw
mdata = {dmem[addr[31:2]][3],dmem[addr[31:2]][2],dmem[addr[31:2]][1],dmem[addr[31:2]][0]};//lw
else if (LSctrl == 2'b01)
begin
mdata[7:0] =#0.1 'z;
mdata[15:8] =#0.1 'z;
mdata[23:16] =#0.1 'z;
mdata[31:24] =#0.1 'z;
case(addr[1:0])
2'b00: mdata[7:0] =#0.1 dmem[addr[31:2]][0];//lb
2'b01: mdata[7:0] =#0.1 dmem[addr[31:2]][1];//lb
2'b10: mdata[7:0] =#0.1 dmem[addr[31:2]][2];//lb
2'b11: mdata[7:0] =#0.1 dmem[addr[31:2]][3];//lb
endcase
end
else if(LSctrl==2'b10)
begin
mdata[7:0] =#0.1 'z;
mdata[15:8] =#0.1 'z;
mdata[23:16] =#0.1 'z;
mdata[31:24] =#0.1 'z;
case(addr[1:0])
2'b00:begin
mdata[7:0] =#0.1 dmem[addr[31:2]][0];//shw
mdata[15:8]=#0.1 dmem[addr[31:2]][1];//shw
end
2'b01: begin
mdata[7:0] =#0.1 dmem[addr[31:2]][1]; //shw
mdata[15:8]=#0.1 dmem[addr[31:2]][2];//shw
end
2'b10: begin
mdata[7:0] =#0.1 dmem[addr[31:2]][2]; //shw
mdata[15:8] =#0.1 dmem[addr[31:2]][3];//shw
end
2'b11: begin
mdata[7:0] =#0.1 dmem[addr[31:2]][3]; //shw
// mdata[15:8] =#0.1 dmem[addr[31:2]+1'b1][0]; to add read byte operation for compiler
mdata[15:8] =#0.1 'z;//shw
end
endcase
end
else
begin
mdata[7:0] =#0.1 'z;
mdata[15:8] =#0.1 'z;
mdata[23:16] =#0.1 'z;
mdata[31:24] =#0.1 'z;
end
end
end
endmodule
`timescale 1ns / 1ps
module I_mem (clk,rst_n,instr,addr);
output reg [31:0] instr;
input clk,rst_n;
input [31:0] addr;
reg [31:0] imem[77:0];
initial begin
#200 $readmemh("instr.txt",imem);
end
reg flag_cnt=0;
// always @(*) begin
always @(posedge clk) // out pipe inserted
begin
if(rst_n==1'b0)
begin
instr = (flag_cnt==1'b0)?'1:'0;
if(flag_cnt!=1)
flag_cnt=1'b1;
else
flag_cnt=1'b1;
end
else
instr = imem[addr[31:2]];
end
endmodule
`timescale 1ns / 1ps
module I_mem2 (instr,addr);
output reg [31:0] instr;
input [31:0] addr;
reg [31:0] imem[77:0];
initial begin
imem[0] = 32'b000110010011_00000_000_00101_0010011;
imem[1] = 32'b000000000001_00000_110_00001_0010011; //Ok ORI R1,R0,1 R1=R0|11'b1
//Ok R[1]=1
imem[2] = 32'b000000000010_00001_110_00010_0010011; //Ok ORI R2,R1,2 R2=R1|11'b10
//Ok R[2]=3
imem[3] = 32'b0000000_00010_00001_010_00111_0100011; //Ok SW R1,R2,7 M[[R1]+7]=[R2] 8번지에 R2의 값쓰기(R2=3)
imem[4] = 32'b000000000110_00010_010_00011_0000011; //Ok LW R3,R2,7 [R3]=M[[R2]+6] R2+6번지의 데이터값읽어오기
imem[4] = 32'b000000000111_00001_010_00011_0000011; //Ok LW R3,R1,7 [R3]=M[[R1]+7] R1+6번지의 데이터값읽어오기
imem[5] = 32'b0000000_00010_00001_000_00111_0110011; //Ok ADD R7,R1,R2 4=1+3
// imem[] = 32'b000000000000_00000_000_00000_0000000;
// imem[6] = 32'b000000100001_00010_010_00100_0010011; // for add MUX SLTI rd R4,rs1 R2, imm(1) SLTI R4,R2,1 [R4]=[R2]<imm?1:0
imem[6] = 32'b0_000000_00001_00011_000_00000_1100011; // BEQ R3,R1 PC= PC+{imm,1'b0};
imem[7] = 32'b0100000_00001_00011_000_00011_0110011; // SUB R3,R3,R1
imem[8] = 32'b0100000_00001_00011_000_00011_0110011; // SUB R3,R3,R1
imem[9] = 32'b0_000000_00001_00011_000_00000_1100011; // BEQ R3,R1 PC= PC+{imm,1'b0};
// imem[9] = 32'b0_0000000000_0000_0_00000000_1101111; //Ok JAL PC={imm,1'b0} PC=0
// imem[9] = 32'b111111100000_00000_000_00000_1100111; //Ok JALR PC=R[rs1]
imem[10] = 32'b111111100000_00000_000_00000_1100111; //Ok JALR PC=R[rs1]
imem[11] = 32'b00000001110000110001001100110011;
imem[12] = 32'b00000000000000000000000000010011;
imem[13] = 32'b00000000000000000000000000010011;
imem[14] = 32'b00000000011000111110001110110011;
imem[15] = 32'b00000000000000000000000000010011;
imem[16] = 32'b00000000000000000000000000010011;
imem[17] = 32'b01110011001000111111111010010011;
imem[18] = 32'b00000000000000000000000000010011;
imem[19] = 32'b00000000000000000000000000010011;
imem[20] = 32'b00000000010111101101111010010011;
imem[21] = 32'b00000000000000000000000000010011;
imem[22] = 32'b00000000000000000000000000010011;
imem[23] = 32'b00000001110000111101001110110011;
imem[24] = 32'b00000000000000000000000000010011;
imem[25] = 32'b00000000000000000000000000010011;
imem[26] = 32'b00000001000000111001001110010011;
imem[27] = 32'b00000000000000000000000000010011;
imem[28] = 32'b00000000000000000000000000010011;
imem[29] = 32'b01000001110000111101001110110011;
imem[30] = 32'b00000000011000101001100001100011;
imem[31] = 32'b00000000000000000000000000010011;
imem[32] = 32'b00000000000000000000000000010011;
imem[33] = 32'b00000000000000000000001110110011;
imem[34] = 32'b00000010000000111000100001100011;
imem[35] = 32'b00000000000000000000000000010011;
imem[36] = 32'b00000000000000000000000000010011;
imem[37] = 32'b00000011110100111101001001100011;
imem[38] = 32'b00000000000000000000000000010011;
imem[39] = 32'b00000000000000000000000000010011;
imem[40] = 32'b00000000000000111000001010110011;
imem[41] = 32'b00000000000000000000000000010011;
imem[42] = 32'b00000000000000000000000000010011;
imem[43] = 32'b00000001110100111100100001100011;
imem[44] = 32'b00000000000000000000000000010011;
imem[45] = 32'b00000000000000000000000000010011;
imem[46] = 32'b00000000000000000000001010110011;
imem[47] = 32'b00000000010100111001110001100011;
imem[48] = 32'b00000000000000000000000000010011;
imem[49] = 32'b00000000000000000000000000010011;
imem[50] = 32'b00000000010100111000100001100011;
imem[51] = 32'b00000000000000000000000000010011;
imem[52] = 32'b00000000000000000000000000010011;
imem[53] = 32'b00000000000000000000001100110011;
imem[54] = 32'b00000001110100110100110001100011;
imem[55] = 32'b00000000000000000000000000010011;
imem[56] = 32'b00000000000000000000000000010011;
imem[57] = 32'b00000001110000110101100001100011;
imem[58] = 32'b00000000000000000000000000010011;
imem[59] = 32'b00000000000000000000000000010011;
imem[60] = 32'b00000000000000000000111000110011;
imem[61] = 32'b00000000000000000000111010110011;
imem[62] = 32'b00000001100000000000000011101111;
imem[63] = 32'b00000000000000000000000000010011;
imem[64] = 32'b00000000000000000000000000010011;
imem[65] = 32'b00000010011100101000100001100011;
imem[66] = 32'b00000000000000000000000000010011;
imem[67] = 32'b00000000000000000000000000010011;
imem[68] = 32'b00000000010100010010000000100011;
imem[69] = 32'b00000000011000010000001000100011;
imem[70] = 32'b00000000000000010010111010000011;
imem[71] = 32'b00000000010000010000111010000011;
imem[72] = 32'b00000000010000010100111010000011;
imem[73] = 32'b00000000000000001000000001100111;
imem[74] = 32'b00000000000000000000000000010011;
imem[75] = 32'b00000000000000000000000000010011;
imem[76] = 32'b00000000000000000000000010110011;
imem[77] = 32'b00000000000000000000001110110011;
/*page 91. A[30]=h+A[30]+l;
imem[72] = 32'b00001111_00000101_00110100_10000011;
imem[73] = 32'b00000000_10011010_10000100_10110011;
imem[74] = 32'b00000000_00010100_10000100_10010011;
imem[75] = 32'b00001110_10010101_00111000_00100011;
imem[1] = 32'b00000000_00010001_00110001_00100011;
imem[2] = 32'b00000000_00110001_00000000_10110011;
imem[3] = 32'b00000000_00110001_00000000_10110011;
imem[4] = 32'b00000000_00110001_00000000_10110011;
imem[5] = 32'b00111110_10000001_00000000_10010011;
imem[6] = 32'b00111110_10000001_00000000_10010011;
imem[7] = 32'b00111110_10000000_10110000_10000011;
*/
end
always @(*) begin
instr = imem[addr[31:2]];
end
endmodule
`timescale 1ns / 1ps
module regFiles(rdata1, rdata2,clk,rst_n,rindex1, rindex2,windex,wdata,RegWrite);
output reg [31:0] rdata1, rdata2;
input clk,rst_n;
input [4:0] rindex1, rindex2, windex;
input [31:0] wdata;
input RegWrite;
reg [31:0] x[31:0];
always @(*)
begin
rdata1 <=#0.1 x[rindex1];
rdata2 <=#0.1 x[rindex2];
end
initial x[0] =#0.1 0;
always @(posedge clk)
begin
if(!rst_n)
begin
x[0] <=#0.1 32'b0;
x[1] <=#0.1 32'b0;
x[2] <=#0.1 32'b0;
x[3] <=#0.1 32'b0;
x[4] <=#0.1 32'b0;
x[5] <=#0.1 32'b0;
x[6] <=#0.1 32'b0;
x[7] <=#0.1 32'b0;
x[8] <=#0.1 32'b0;
x[9] <=#0.1 32'b0;
x[10] <=#0.1 32'b0;
x[11] <=#0.1 32'b0;
x[12] <=#0.1 32'b0;
x[13] <=#0.1 32'b0;
x[14] <=#0.1 32'b0;
x[15] <=#0.1 32'b0;
x[16] <=#0.1 32'b0;
x[17] <=#0.1 32'b0;
x[18] <=#0.1 32'b0;
x[19] <=#0.1 32'b0;
x[20] <=#0.1 32'b0;
x[21] <=#0.1 32'b0;
x[22] <=#0.1 32'b0;
x[23] <=#0.1 32'b0;
x[24] <=#0.1 32'b0;
x[25] <=#0.1 32'b0;
x[26] <=#0.1 32'b0;
x[27] <=#0.1 32'b0;
x[28] <=#0.1 32'b0;
x[29] <=#0.1 32'b0;
x[30] <=#0.1 32'b0;
x[31] <=#0.1 32'b0;
end
else if (RegWrite)
begin
x[windex] <=#0.1 wdata;
// assert(windex!=0) x[windex] =#0.1 wdata; else $display("zero regisger Write try error!!!");
end
end
initial begin
x[0] =#0.1 32'b0;
x[1] =#0.1 32'b0;
x[2] =#0.1 32'b0;
x[3] =#0.1 32'b0;
x[4] =#0.1 32'b0;
x[5] =#0.1 32'b0;
x[6] =#0.1 32'b0;
x[7] =#0.1 32'b0;
x[8] =#0.1 32'b0;
x[9] =#0.1 32'b0;
x[10] =#0.1 32'b0;
x[11] =#0.1 32'b0;
x[12] =#0.1 32'b0;
x[13] =#0.1 32'b0;
x[14] =#0.1 32'b0;
x[15] =#0.1 32'b0;
x[16] =#0.1 32'b0;
x[17] =#0.1 32'b0;
x[18] =#0.1 32'b0;
x[19] =#0.1 32'b0;
x[20] =#0.1 32'b0;
x[21] =#0.1 32'b0;
x[22] =#0.1 32'b0;
x[23] =#0.1 32'b0;
x[24] =#0.1 32'b0;
x[25] =#0.1 32'b0;
x[26] =#0.1 32'b0;
x[27] =#0.1 32'b0;
x[28] =#0.1 32'b0;
x[29] =#0.1 32'b0;
x[30] =#0.1 32'b0;
x[31] =#0.1 32'b0;
end
endmodule
`timescale 1ns / 1ps
module PC (out,clk, rst_n, in);
output reg [31:0] out;
input clk, rst_n;
input [31:0] in;
initial
begin
out <= 0;
end
always @ (posedge clk) begin
if(~rst_n )
begin
out<=0;
end
else
out<= in;
end
endmodule
`timescale 1ns / 1ps
module RESET (rst_n,clk,rst_in);
output reg rst_n;
input clk, rst_in;
reg rst_d0,rst_d1;
always @ (posedge clk)
begin
if(!rst_in)
rst_d0<=1'b0;
else
rst_d0 <=1'b1;
rst_d1 <= rst_d0;
rst_n <= rst_d1;
end
endmodule