Skip to content

Commit be7ca5a

Browse files
BUAA Computer_Organizations Projects
0 parents  commit be7ca5a

File tree

215 files changed

+20028
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

215 files changed

+20028
-0
lines changed

p3/P3.docx

31.2 KB
Binary file not shown.

p3/P3.pdf

249 KB
Binary file not shown.

p3/p3/p3_final.circ

+5,132
Large diffs are not rendered by default.

p4/P4/ALU.v

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
`timescale 1ns / 1ps
2+
3+
module ALU(a,b,aluop,aluout);
4+
input [31:0]a,b;
5+
input [1:0] aluop;
6+
output [31:0] aluout;
7+
8+
wire [31:0] tmp[3:0];
9+
assign tmp[1]=a+b;
10+
assign tmp[2]=a-b;
11+
assign tmp[3]=a|b;
12+
assign aluout=tmp[aluop];
13+
14+
endmodule

p4/P4/CMP.v

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
`timescale 1ns / 1ps
2+
3+
module CMP(a,b,zero32,gtz32,gez32,ltz32,lez32);
4+
input [31:0]a,b;
5+
output [31:0] zero32,gtz32,gez32,ltz32,lez32;
6+
7+
assign zero32=(a==b)?32'h1:32'h0;
8+
9+
assign gtz32=($signed(a)>$signed(b))?32'b1:32'b0;
10+
assign gez32=($signed(a)>=$signed(b))?32'b1:32'b0;
11+
assign ltz32=($signed(a)<$signed(b))?32'b1:32'b0;
12+
assign lez32=($signed(a)<=$signed(b))?32'b1:32'b0;
13+
14+
endmodule

p4/P4/DATAPATH.v

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
`timescale 1ns / 1ps
2+
`include "head.v"
3+
module DATAPATH(clk,clr,
4+
branch,regwrite,memwrite,npcop,extop,aluop,alusrc,hf,bt,
5+
regdst,memtoreg,
6+
isbeq,isbgtz,isbgez,isbltz,isblez,
7+
issll,issra,issrl,v,
8+
instr);
9+
input clk,clr,branch,regwrite,memwrite,isbeq,alusrc,hf,bt,isbgtz,isbgez,isbltz,isblez,issra,issrl,issll,v;
10+
input [1:0] npcop,extop,aluop,regdst;
11+
input [2:0] memtoreg;
12+
output[31:0] instr;
13+
//==================mux=====================================
14+
wire [4:0]regaddr;
15+
wire [31:0] regdata,alubdata;
16+
//==================wire declearation=======================
17+
wire [31:0]pc4;
18+
wire [31:0] rd1,rd2,extout,zero32,npcout,gtz32,gez32,ltz32,lez32,shiftout;
19+
wire [31:0] aluout;
20+
wire [31:0] dmout;
21+
wire branchlogic;
22+
//===================instr fetch============================
23+
IM im(.clk(clk),.clr(clr),.branch(branchlogic),.npc(npcout),.instr(instr),.pc4(pc4));
24+
assign branchlogic=(branch&isbeq&zero32[0])|(branch&isbgtz&gtz32[0])|(branch&isbgez&gez32[0])|(branch&isbltz&ltz32[0])
25+
|(branch&isblez&lez32[0])|(branch&~isbeq&~isbgtz&~isbgtz&~isbgez&~isbltz&~isblez);
26+
//=====================decode===============================
27+
wire [4:0] rs,rd,rt,shamt;
28+
wire [5:0] op,funct;
29+
wire [15:0] imm;
30+
assign op=instr[31:26];
31+
assign funct=instr[5:0];
32+
assign shamt=instr[10:6];
33+
assign rd=instr[15:11];
34+
assign rt=instr[20:16];
35+
assign rs=instr[25:21];
36+
assign imm=instr[15:0];
37+
GPR gpr(.clk(clk),.clr(clr),.we(regwrite),.a1(rs),.a2(rt),.a3(regaddr),.wd(regdata),.rd1(rd1),.rd2(rd2));
38+
EXT ext(.imm(imm),.extop(extop),.extout(extout));
39+
CMP cmp(.a(rd1),.b(rd2),.zero32(zero32),.gtz32(gtz32),.gez32(gez32),.ltz32(ltz32),.lez32(lez32));
40+
NPC npc(.pc4(pc4),.extout(extout),.instr(instr),.rd1(rd1),.npcop(npcop),.npcout(npcout));
41+
shift sft(rd2,rd1,shamt,issll,issrl,issra,v,shiftout);
42+
43+
//========================execute===============================
44+
ALU alu(.a(rd1),.b(alubdata),.aluop(aluop),.aluout(aluout));
45+
assign alubdata=(alusrc==`alusrc_rd2)?rd2:extout;
46+
//======================memory==================================
47+
DM dm(.clk(clk),.clr(clr),.we(memwrite),.addr(aluout),.wd(rd2),.dmout(dmout),.hf(hf),.bt(bt));
48+
//========================writeback===================================
49+
assign regaddr= (regdst==`regdst_rd)?rd:
50+
(regdst==`regdst_rt)?rt:
51+
5'd31;
52+
53+
54+
55+
assign regdata= (memtoreg==`mtr_slti)?($signed(rd1)<$signed(extout))?32'b1:32'b0:
56+
(memtoreg==`mtr_shift)?shiftout:
57+
(memtoreg==`mtr_slt)?(($signed(rd1)<$signed(rd2))?32'b1:32'b0):
58+
(memtoreg==`mtr_alu)?aluout:
59+
(memtoreg==`mtr_dm)?dmout:
60+
(memtoreg==`mtr_ext)?extout:
61+
pc4;
62+
//================================================================================
63+
64+
65+
66+
67+
68+
endmodule

p4/P4/DM.v

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
`timescale 1ns / 1ps
2+
3+
module DM(clk,clr,we,addr,wd,dmout,hf,bt);
4+
input clk,clr,we,hf,bt;
5+
input [31:0] addr,wd;
6+
output [31:0]dmout;
7+
8+
reg [31:0] dmmem[1023:0];
9+
integer i;
10+
wire [31:0] word,wdin;
11+
wire [31:0] hfword[1:0],hfin[1:0];
12+
wire [31:0]btword[3:0],btin[3:0];
13+
14+
15+
16+
assign word=dmmem[addr[11:2]];
17+
assign hfword[0]={16'b0,word[15:0]};
18+
assign hfword[1]={16'b0,word[31:16]};
19+
assign btword [0]={24'b0,word[7:0]};
20+
assign btword [1]={24'b0,word[15:8]};
21+
assign btword [2]={24'b0,word[23:16]};
22+
assign btword [3]={24'b0,word[31:24]};
23+
24+
25+
assign hfin[0]={word[31:16],wd[15:0]};
26+
assign hfin[1]={wd[15:0],word[15:0]};
27+
assign btin[0]={word[31:24],word[23:16],word[15:8],wd[7:0]};
28+
assign btin[1]={word[31:24],word[23:16],wd[7:0],word[7:0]};
29+
assign btin[2]={word[31:24],wd[7:0],word[15:8],word[7:0]};
30+
assign btin[3]={wd[7:0],word[23:16],word[15:8],word[7:0]};
31+
32+
assign wdin=(hf==1)?hfin[addr[1]]:
33+
(bt==1)?btin[addr[1:0]]:
34+
wd;
35+
36+
initial begin
37+
for(i=0;i<1024;i=i+1)
38+
dmmem[i]<=0;
39+
end
40+
41+
42+
always@(posedge clk)
43+
begin
44+
if(clr)
45+
begin
46+
for(i=0;i<1024;i=i+1)
47+
dmmem[i]<=0;
48+
end
49+
else
50+
begin
51+
if(we)
52+
begin
53+
dmmem[addr[11:2]]<=wdin;
54+
$display("@%h: *%h <= %h",im.pc, {addr[31:2],2'b00},wdin);//xian fang zheli
55+
end
56+
end
57+
end
58+
59+
60+
assign dmout=(hf==1)?hfword[addr[1]]:
61+
(bt==1)?btword[addr[1:0]]:
62+
word;
63+
endmodule

p4/P4/EXT.v

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
`timescale 1ns / 1ps
2+
3+
module EXT(imm,extop,extout);
4+
input [15:0]imm;
5+
input [1:0] extop;
6+
output [31:0] extout;
7+
8+
wire [31:0]tmp[3:0];
9+
assign tmp[0]={16'b0,imm};
10+
assign tmp[1]={{16{imm[15]}},imm};
11+
assign tmp[2]={imm,16'b0};
12+
assign extout=tmp[extop];
13+
14+
endmodule

p4/P4/FUCK.zip

6.38 KB
Binary file not shown.

p4/P4/GPR.v

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
`timescale 1ns / 1ps
2+
3+
module GPR(clk,clr,we,a1,a2,a3,wd,rd1,rd2);
4+
input clk,clr,we;
5+
input [31:0] wd;
6+
input [4:0] a1,a2,a3;
7+
output [31:0] rd1,rd2;
8+
reg[31:0] regmem[31:0];
9+
10+
integer i;
11+
initial begin
12+
for(i=0;i<32;i=i+1)
13+
regmem[i]<=0;
14+
end
15+
16+
always@(posedge clk)
17+
begin
18+
if(clr)
19+
begin
20+
for(i=0;i<31;i=i+1)
21+
regmem[i]<=0;
22+
end
23+
else
24+
begin
25+
if(we==1&&a3!=0)
26+
begin
27+
regmem[a3]<=wd;
28+
$display("@%h: $%d <= %h", im.pc,a3,wd);//output display,xian fang zhe li
29+
end
30+
end
31+
end
32+
33+
assign rd1=regmem[a1];
34+
assign rd2=regmem[a2];
35+
36+
endmodule

p4/P4/IM.v

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
`timescale 1ns / 1ps
2+
3+
module IM(clk,clr,branch,npc,instr,pc4);
4+
input clk,clr,branch;
5+
input [31:0] npc;
6+
output [31:0] instr,pc4;
7+
8+
reg [31:0]pc;
9+
reg [31:0] inmem[1023:0];
10+
integer i;
11+
12+
wire [31:0]pointer;
13+
initial begin
14+
for(i=0;i<1024;i=i+1)
15+
inmem[i]=0;
16+
pc=32'h0003000;
17+
$readmemh("code.txt",inmem,0,1023);
18+
end
19+
20+
21+
22+
always @(posedge clk)
23+
begin
24+
if(clr)
25+
begin
26+
for(i=0;i<1024;i=i+1)
27+
inmem[i]=0;
28+
$readmemh("code.txt",inmem,0,1023);
29+
pc<=32'h00003000;
30+
end
31+
else
32+
begin
33+
if(branch==1)
34+
pc<=npc;
35+
else
36+
pc<=pc+4;
37+
end
38+
end
39+
40+
assign pc4=pc+4;
41+
assign pointer=pc-32'h00003000;
42+
assign instr=inmem[pointer[31:2]];
43+
44+
endmodule

p4/P4/NPC.v

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
`timescale 1ns / 1ps
2+
3+
module NPC(pc4,extout,instr,rd1,npcop,npcout);
4+
input [31:0] pc4,extout,instr,rd1;
5+
input [1:0] npcop;
6+
output [31:0] npcout;
7+
8+
wire [31:0]tmp[3:0];
9+
wire [31:0]offset;
10+
assign offset={extout[29:0],2'b00};
11+
assign tmp[0]=pc4+offset;
12+
assign tmp[1]={pc4[31:28],instr[25:0],2'b00};
13+
assign tmp[2]=rd1;
14+
assign npcout=tmp[npcop];
15+
endmodule

0 commit comments

Comments
 (0)