Skip to content

Commit 8bd1851

Browse files
authored
Add files via upload
1 parent c72f4cc commit 8bd1851

17 files changed

+1418
-0
lines changed

ALU.v

+201
Large diffs are not rendered by default.

CU.v

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
// Company:
4+
// Engineer:
5+
//
6+
// Create Date: 2019/12/23 16:47:49
7+
// Design Name:
8+
// Module Name: CU
9+
// Project Name:
10+
// Target Devices:
11+
// Tool Versions:
12+
// Description:
13+
//
14+
// Dependencies:
15+
//
16+
// Revision:
17+
// Revision 0.01 - File Created
18+
// Additional Comments:
19+
//
20+
//////////////////////////////////////////////////////////////////////////////////
21+
22+
23+
module CU(Inst,Func,ID_B_code,RegDst,Se,RegWrite,ALUXSrc,ALUYSrc,ALUControl,MemWrite,PCSrc,MemtoReg,load_option,save_option,usigned,c_adventure);
24+
input [5:0]Func;
25+
input [31:0]Inst;
26+
input c_adventure,ID_B_code;
27+
output RegDst,Se,RegWrite,ALUXSrc,ALUYSrc,MemWrite,MemtoReg,usigned;
28+
output [2:0]PCSrc,load_option;
29+
output [1:0]save_option;
30+
output [3:0]ALUControl;
31+
32+
wire R_type=~Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & ~Inst[27] & ~Inst[26];
33+
wire I_lb = Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & ~Inst[27] & ~Inst[26];
34+
wire I_lbu = Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & ~Inst[27] & ~Inst[26];
35+
wire I_lh = Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & ~Inst[27] & Inst[26];
36+
wire I_lhu = Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & ~Inst[27] & Inst[26];
37+
wire I_lw = Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & Inst[27] & Inst[26];
38+
wire I_sb = Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & ~Inst[27] & ~Inst[26];
39+
wire I_sh = Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & ~Inst[27] & Inst[26];
40+
wire I_sw = Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & Inst[27] & Inst[26];
41+
wire I_add = R_type & Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & ~Func[1] & ~Func[0];
42+
wire I_addu = R_type & Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & ~Func[1] & Func[0];
43+
wire I_sub = R_type & Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & Func[1] & ~Func[0];
44+
wire I_subu = R_type & Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & Func[1] & Func[0];
45+
wire I_sll = R_type & ~Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & ~Func[1] & ~Func[0] & (Inst!=0);
46+
wire I_srl = R_type & ~Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & Func[1] & ~Func[0];
47+
wire I_sra = R_type & ~Func[5] & ~Func[4] & ~Func[3] & ~Func[2] & Func[1] & Func[0];
48+
wire I_sllv = R_type & ~Func[5] & ~Func[4] & ~Func[3] & Func[2] & ~Func[1] & ~Func[0] & (Inst!=0);
49+
wire I_srlv = R_type & ~Func[5] & ~Func[4] & ~Func[3] & Func[2] & Func[1] & ~Func[0];
50+
wire I_srav = R_type & ~Func[5] & ~Func[4] & ~Func[3] & Func[2] & Func[1] & Func[0];
51+
wire I_and = R_type & Func[5] & ~Func[4] & ~Func[3] & Func[2] & ~Func[1] & ~Func[0];
52+
wire I_or = R_type & Func[5] & ~Func[4] & ~Func[3] & Func[2] & ~Func[1] & Func[0];
53+
wire I_xor = R_type & Func[5] & ~Func[4] & ~Func[3] & Func[2] & Func[1] & ~Func[0];
54+
wire I_nor = R_type & Func[5] & ~Func[4] & ~Func[3] & Func[2] & Func[1] & Func[0];
55+
wire I_addi = ~Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & ~Inst[27] & ~Inst[26];
56+
wire I_addiu = ~Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & ~Inst[27] & Inst[26];
57+
wire I_andi = ~Inst[31] & ~Inst[30] & Inst[29] & Inst[28] & ~Inst[27] & ~Inst[26];
58+
wire I_ori = ~Inst[31] & ~Inst[30] & Inst[29] & Inst[28] & ~Inst[27] & Inst[26];
59+
wire I_xori = ~Inst[31] & ~Inst[30] & Inst[29] & Inst[28] & Inst[27] & ~Inst[26];
60+
wire I_lui = ~Inst[31] & ~Inst[30] & Inst[29] & Inst[28] & Inst[27] & Inst[26];
61+
wire I_slt = R_type & Func[5] & ~Func[4] & Func[3] & ~Func[2] & Func[1] & ~Func[0];
62+
wire I_slti = ~Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & Inst[27] & ~Inst[26];
63+
wire I_sltiu = ~Inst[31] & ~Inst[30] & Inst[29] & ~Inst[28] & Inst[27] & Inst[26];
64+
wire I_sltu = R_type & Func[5] & ~Func[4] & Func[3] & ~Func[2] & Func[1] & Func[0];
65+
wire I_beq = ~Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & ~Inst[27] & ~Inst[26];
66+
wire I_bne = ~Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & ~Inst[27] & Inst[26];
67+
wire I_blez = ~Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & Inst[27] & ~Inst[26];
68+
wire I_bgtz = ~Inst[31] & ~Inst[30] & ~Inst[29] & Inst[28] & Inst[27] & Inst[26];
69+
wire I_bltz = ~Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & ~Inst[27] & Inst[26] & ~ID_B_code;
70+
wire I_bgez = ~Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & ~Inst[27] & Inst[26] & ID_B_code;
71+
wire I_j = ~Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & Inst[27] & ~Inst[26];
72+
wire I_jal = ~Inst[31] & ~Inst[30] & ~Inst[29] & ~Inst[28] & Inst[27] & Inst[26];
73+
wire I_jalr = R_type & ~Func[5] & ~Func[4] & Func[3] & ~Func[2] & ~Func[1] & Func[0];
74+
wire I_jr = R_type & ~Func[5] & ~Func[4] & Func[3] & ~Func[2] & ~Func[1] & ~Func[0];
75+
76+
assign RegDst = I_lb | I_lbu | I_lh | I_lhu | I_lw | I_addi | I_addiu | I_andi | I_ori | I_xori | I_lui | I_slti | I_sltiu;
77+
assign Se = I_lb | I_lbu | I_lh | I_lhu | I_lw | I_sb | I_sh | I_sw | I_addi | I_addiu | I_slti | I_sltiu | I_beq | I_bne | I_blez | I_bgtz | I_bltz | I_bgez;
78+
assign RegWrite = I_lb | I_lbu | I_lh | I_lhu | I_lw | I_add | I_addu | I_sub | I_subu | I_sll | I_srl | I_sra | I_sllv | I_srlv | I_srav | I_and | I_or | I_xor | I_nor | I_addi | I_addiu | I_andi | I_ori | I_xori | I_lui | I_slt | I_slti | I_sltiu | I_sltu;
79+
assign ALUXSrc = I_sll | I_srl | I_sra;
80+
assign ALUYSrc = I_add | I_addu | I_sub | I_subu | I_and | I_or | I_xor | I_nor | I_slt | I_sltu | I_beq | I_bne | I_j | I_jal | I_jalr | I_jr | I_sll | I_srl | I_sra | I_sllv | I_srlv | I_srav;
81+
assign ALUControl[0] =I_sub | I_subu | I_sll | I_sllv | I_or | I_nor | I_ori | I_slt | I_slti | I_sltiu | I_sltu | I_beq | I_bne | I_bgtz | I_bgez;
82+
assign ALUControl[1] =I_sll | I_sllv | I_and | I_or | I_andi | I_ori | I_lui | I_blez | I_bgtz | I_sra | I_srav;
83+
assign ALUControl[2] =I_sll | I_sllv | I_xor | I_nor | I_xori | I_lui | I_bltz | I_bgez | I_sra | I_srav;
84+
assign ALUControl[3] =I_srl | I_sra | I_srlv | I_srav | I_slt | I_slti | I_sltiu | I_sltu | I_blez | I_bgtz | I_bltz | I_bgez | I_sll | I_sllv;
85+
assign MemWrite = I_sb | I_sh | I_sw;
86+
assign PCSrc[0] = (I_blez & c_adventure) | (I_bgtz & c_adventure)| (I_bltz & c_adventure)| (I_bgez & c_adventure)| I_jal | I_jalr | (I_beq & c_adventure) | (I_bne & ~c_adventure);
87+
assign PCSrc[1] = I_j | I_jal;
88+
assign PCSrc[2] = I_jalr | I_jr;
89+
assign MemtoReg = I_lb | I_lbu | I_lh | I_lhu | I_lw;
90+
assign load_option[0] = I_lb | I_lbu | I_lh | I_lhu;
91+
assign load_option[1] = I_lh | I_lhu;
92+
assign load_option[2] = I_lh | I_lb;
93+
assign save_option[0] = I_sb;
94+
assign save_option[1] = I_sh;
95+
assign usigned = I_lbu | I_lhu | I_addu | I_subu | I_addiu | I_sltiu | I_sltu;
96+
97+
98+
endmodule
99+

FU.v

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
// Company:
4+
// Engineer:
5+
//
6+
// Create Date: 2020/01/13 17:40:15
7+
// Design Name:
8+
// Module Name: FU
9+
// Project Name:
10+
// Target Devices:
11+
// Tool Versions:
12+
// Description:
13+
//
14+
// Dependencies:
15+
//
16+
// Revision:
17+
// Revision 0.01 - File Created
18+
// Additional Comments:
19+
//
20+
//////////////////////////////////////////////////////////////////////////////////
21+
22+
23+
module FU(E_RegWrite,E_WriteReg,E_MemtoReg,M_RegWrite,M_WriteReg,M_MemtoReg,ID_rs,ID_rt,ID_FwdA,ID_FwdB,ID_Op,ID_func,c_adventure,stall,stallstall);
24+
input [4:0] E_WriteReg,M_WriteReg,ID_rs,ID_rt;
25+
input E_RegWrite,M_RegWrite,E_MemtoReg,M_MemtoReg,c_adventure;
26+
input [5:0] ID_Op,ID_func;
27+
output reg [1:0] ID_FwdA,ID_FwdB;
28+
output stall,stallstall;
29+
30+
always@(E_WriteReg,M_WriteReg,E_RegWrite,M_RegWrite,ID_rs,ID_rt)begin
31+
ID_FwdA=2'b00;
32+
if((ID_rs==E_WriteReg)&(E_WriteReg!=0)&(E_RegWrite==1))begin
33+
ID_FwdA=2'b01;
34+
end
35+
else begin
36+
if((ID_rs==M_WriteReg)&(M_WriteReg!=0)&(M_RegWrite==1))begin
37+
ID_FwdA=2'b10;
38+
end
39+
end
40+
end
41+
42+
always@(E_WriteReg,M_WriteReg,E_RegWrite,M_RegWrite,ID_rs,ID_rt)begin
43+
ID_FwdB=2'b00;
44+
if((ID_rt==E_WriteReg)&(E_WriteReg!=0)&(E_RegWrite==1))begin
45+
ID_FwdB=2'b01;
46+
end
47+
else begin
48+
if((ID_rt==M_WriteReg)&(M_WriteReg!=0)&(M_RegWrite==1))begin
49+
ID_FwdB=2'b10;
50+
end
51+
end
52+
end
53+
54+
wire ID_beq=~ID_Op[5]&~ID_Op[4]&~ID_Op[3]&ID_Op[2]&~ID_Op[1]&~ID_Op[0];
55+
wire ID_bne=~ID_Op[5]&~ID_Op[4]&~ID_Op[3]&ID_Op[2]&~ID_Op[1]&ID_Op[0];
56+
wire ID_jalr=~ID_Op[5]&~ID_Op[4]&~ID_Op[3]&~ID_Op[2]&~ID_Op[1]&~ID_Op[0]&~ID_func[5]&~ID_func[4]&ID_func[3]&~ID_func[2]&~ID_func[1]&ID_func[0];
57+
58+
//lw+add add+beq(jalr) lw+x+beq
59+
assign stall=(((ID_rs==E_WriteReg)|(ID_rt==E_WriteReg))&(E_MemtoReg==1)&(E_WriteReg!=0)&(E_RegWrite==1))|((ID_beq | ID_bne | ID_jalr)&((ID_rs==E_WriteReg)|(ID_rt==E_WriteReg))&(E_WriteReg!=0)&(E_RegWrite==1))|(((ID_rs==M_WriteReg)|(ID_rt==M_WriteReg))&(M_MemtoReg==1)&(M_WriteReg!=0)&(M_RegWrite==1)&ID_beq);
60+
//lw+beq
61+
assign stallstall=((ID_rs==E_WriteReg)|(ID_rt==E_WriteReg))&(E_MemtoReg==1)&(E_WriteReg!=0)&(E_RegWrite==1)&ID_beq;
62+
63+
endmodule

MUX.v

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
// Company:
4+
// Engineer:
5+
//
6+
// Create Date: 2019/12/23 16:47:49
7+
// Design Name:
8+
// Module Name: MUX
9+
// Project Name:
10+
// Target Devices:
11+
// Tool Versions:
12+
// Description:
13+
//
14+
// Dependencies:
15+
//
16+
// Revision:
17+
// Revision 0.01 - File Created
18+
// Additional Comments:
19+
//
20+
//////////////////////////////////////////////////////////////////////////////////
21+
22+
23+
//决定nextAddr,分别为pc+4,b,j,jr
24+
module MUX4X32_addr (PCAdd4, B, J, Jr, PCSrc, nextAddr);
25+
input [31:0] PCAdd4, B, J, Jr;
26+
input [2:0] PCSrc;
27+
output [31:0] nextAddr;
28+
29+
function [31:0] select;
30+
input [31:0] PCAdd4, B, J, Jr;
31+
input [2:0] PCSrc;
32+
case(PCSrc)
33+
3'b000: select = PCAdd4;
34+
3'b001: select = B;
35+
3'b010: select = J;
36+
3'b011: select = J;
37+
3'b100: select = Jr;
38+
3'b101: select = Jr;
39+
endcase
40+
endfunction
41+
42+
assign nextAddr = select (PCAdd4, B, J, Jr, PCSrc);
43+
endmodule
44+
45+
//选择写的地址,rd还是rt,1为rt
46+
module MUX2X5(rd,rt,RegDst,Y);
47+
input [4:0] rd,rt;
48+
input RegDst;
49+
output [4:0] Y;
50+
51+
function [4:0] select;
52+
input [4:0] rd,rt;
53+
input RegDst;
54+
case(RegDst)
55+
1:select=rt;
56+
0:select=rd;
57+
endcase
58+
endfunction
59+
assign Y=select(rd,rt,RegDst);
60+
endmodule
61+
62+
//旁路选择器,从Q,EX/MEM,MEM/WB,0中选一个
63+
module MUX3X32 (Q, EX_MEM, MEM_WB, S, Y);
64+
input [31:0] Q, EX_MEM, MEM_WB;
65+
input [1:0] S;
66+
output [31:0] Y;
67+
68+
function [31:0] select;
69+
input [31:0] Q, EX_MEM, MEM_WB;
70+
input [1:0] S;
71+
case(S)
72+
2'b00: select = Q;
73+
2'b01: select = EX_MEM;
74+
2'b10: select = MEM_WB;
75+
endcase
76+
endfunction
77+
assign Y = select (Q, EX_MEM, MEM_WB, S);
78+
endmodule
79+
80+
//选择ALU x端
81+
//选择ALU y端的来源,是来自于扩展为32位的立即数,还是Qb,forward啥的,0的时候为扩展
82+
//选择写回寄存器堆的数据来源,是从dm里面来还是从alu里面来
83+
module MUX2X32(EXT,Qb_FORWARD,S,Y);
84+
input [31:0] EXT,Qb_FORWARD;
85+
input S;
86+
output [31:0] Y;
87+
88+
function [31:0] select;
89+
input [31:0] EXT,Qb_FORWARD;
90+
input S;
91+
case(S)
92+
0:select=EXT;
93+
1:select=Qb_FORWARD;
94+
endcase
95+
endfunction
96+
assign Y=select(EXT,Qb_FORWARD,S);
97+
endmodule
98+
99+
//ID阶段的旁路选择器
100+
module MUX2X32_forward(ID_Q,ALU_OUT,Fwd,Y);
101+
input [31:0] ID_Q,ALU_OUT;
102+
input [1:0] Fwd;
103+
output [31:0] Y;
104+
105+
function [31:0] select;
106+
input [31:0] ID_Q,ALU_OUT;
107+
input [1:0] Fwd;
108+
case(Fwd)
109+
2'b00: select = ID_Q;
110+
2'b01: select = ALU_OUT;
111+
endcase
112+
endfunction
113+
114+
assign Y = select (ID_Q,ALU_OUT,Fwd);
115+
endmodule
116+
117+
//选择load的数据
118+
module MUX5X32(lb,lbu,lh,lhu,lw,load_option,ext_Dout);
119+
input [31:0] lb,lbu,lh,lhu,lw;
120+
input [2:0]load_option;
121+
output [31:0] ext_Dout;
122+
123+
function [31:0] select;
124+
input [31:0]lb,lbu,lh,lhu,lw;
125+
input [2:0]load_option;
126+
case(load_option)
127+
3'b000:select=lw;
128+
3'b101:select=lb;
129+
3'b001:select=lbu;
130+
3'b111:select=lh;
131+
3'b011:select=lhu;
132+
endcase
133+
endfunction
134+
assign ext_Dout=select(lb,lbu,lh,lhu,lw,load_option);
135+
endmodule

PC.v

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
// Company:
4+
// Engineer:
5+
//
6+
// Create Date: 2019/12/23 16:47:49
7+
// Design Name:
8+
// Module Name: PC
9+
// Project Name:
10+
// Target Devices:
11+
// Tool Versions:
12+
// Description:
13+
//
14+
// Dependencies:
15+
//
16+
// Revision:
17+
// Revision 0.01 - File Created
18+
// Additional Comments:
19+
//
20+
//////////////////////////////////////////////////////////////////////////////////
21+
22+
23+
module PC(Result,Clk,Reset,Address,stall,stallstall); //En=1可以写,stall=0没有发生lw数据冒险
24+
input Clk;//时钟
25+
input Reset;//是否重置地址。0-初始化PC,否则接受新地址
26+
input[31:0] Result;
27+
input stall,stallstall;
28+
output reg[31:0] Address;
29+
wire En;
30+
31+
assign En=(~stall)&(~stallstall);
32+
33+
initial begin
34+
Address <= 32'h00003000;
35+
end
36+
37+
always @(posedge Clk or negedge Reset)
38+
begin
39+
if(En==1)
40+
begin
41+
if (!Reset) //如果为0则初始化PC,否则接受新地址
42+
begin
43+
Address <= 32'h000003000;
44+
end
45+
else
46+
begin
47+
Address = Result;
48+
end
49+
end
50+
end
51+
endmodule
52+

0 commit comments

Comments
 (0)