实验四多周期CPU与存储器实验.docx
《实验四多周期CPU与存储器实验.docx》由会员分享,可在线阅读,更多相关《实验四多周期CPU与存储器实验.docx(18页珍藏版)》请在冰豆网上搜索。
![实验四多周期CPU与存储器实验.docx](https://file1.bdocx.com/fileroot1/2023-1/31/eddbaca2-be8f-42a3-b92b-58ab55b27bf6/eddbaca2-be8f-42a3-b92b-58ab55b27bf61.gif)
实验四多周期CPU与存储器实验
湘 潭 大 学 实 验 报 告
课程名称 计算机原理与设计 实验名称__________多周期CPU与存储器实验_______________ 页数
专业 计算机科学与技术 班级________2_____________ 同组者姓名
组别 学号 2015962138 姓名 庄振南 实验日期___2016.11.02____________
一、实验目的
1、深入理解MIPS—CPU指令系统的功能和工作原理;
2、掌握多周期CPU的工作原理和逻辑功能实现;
3、熟练掌握用VerilogHDL语言设计多周期存储器的方法;
4、熟练掌握对多周期存储器的仿真实验验证和硬件测试两种调试方法;
5、通过对多周期CPU的运行情况进行观察和分析,进一步加深理解。
二、实验要求
1、深入理解MIPS—CPU指令系统的功能和工作原理;
2、掌握多周期CPU的工作原理和逻辑功能实现;
3、熟练掌握用VerilogHDL语言设计多周期存储器的方法;
三、实验原理
实现上述原理框图根据功能将其分划分为控制单元(cunit)、执行单元(eunit)、指令单元(iunit)以及存储单元(munit)四大模块。
(1).控制单元(cunit)是多周期微处理器的核心控制微处理器取指令、指令译码和指令执行等工作。
主要由指令译码器控制器(outputscontrol)、算术逻辑运算控制器(ALUcontrol)两
个子模块组成。
(2).执行单元(eunit)主要由寄存器堆(registers)和算术逻辑单元(ALU)两个子模块组成。
其中寄存器是微处理器最基本的元素MIPS系统的寄存器堆由32个32位寄存器组成而ALU则是微处理器的主要功能部件执行加、减、比较等算术运算和与、或、或非、异或等逻辑运算。
指令单元(iunit)的作用是决定下一条指令的地址PC值。
(3).存储单元(munit)由存储器(memory)、指令寄存器(instructionregister)和存储数据寄存
器(memorydataregister)组成。
四、实验内容
1、设计一个32位MIPS多周期CPU具体的要求如下:
至少运行下列的6类32条MIPS指令。
(1)算术逻辑指令and、sub、addi
(2)逻辑运算指令and、0r、xor、andi、ori、xori
(3)位移指令sll、srl、sra
(4)条件分支指令beq、bne、
(5)无条件跳转指令j、jr
(6)数据传送指令lw、sw
2.设计一个存储器
五、实验环境与设备
电脑,电箱。
六、实验代码设计(含符号说明)
寄存器元件代码:
moduleregfile(rna,rnb,d,wn,we,clk,clrn,qa,qb);
input[4:
0]rna,rnb,wn;
input[31:
0]d;
inputwe,clk,clrn;
output[31:
0]qa,qb;
reg[31:
0]register[1:
31];//r1-r31
assignqa=(rna==0)?
0:
register[rna];//read
assignqb=(rnb==0)?
0:
register[rnb];//read
always@(posedgeclkornegedgeclrn)begin
if(clrn==0)begin//reset
integeri;
for(i=1;i<32;i=i+1)
register[i]<=0;
end
elsebegin
if((wn!
=0)&&(we==1))//write
register[wn]<=d;
end
end
endmodule
32位四选一选择器:
modulemux4x32(a0,a1,a2,a3,s,y);
input[31:
0]a0,a1,a2,a3;
input[1:
0]s;
output[31:
0]y;
function[31:
0]select;
input[31:
0]a0,a1,a2,a3;
input[1:
0]s;
case(s)
2'b00:
select=a0;
2'b01:
select=a1;
2'b10:
select=a2;
2'b11:
select=a3;
endcase
endfunction
assigny=select(a0,a1,a2,a3,s);
endmodule
5位二选一选择器:
modulemux2x5(a0,a1,s,y);
input[4:
0]a0,a1;
inputs;
output[4:
0]y;
assigny=s?
a1:
a0;
endmodule
32位二选一选择器:
modulemux2x32(a0,a1,s,y);
input[31:
0]a0,a1;
inputs;
output[31:
0]y;
assigny=s?
a1:
a0;
endmodule
存储器元件:
modulemcmem(clk,dataout,datain,addr,we,inclk,outclk);
input[31:
0]datain;
input[31:
0]addr;
inputclk,we,inclk,outclk;
output[31:
0]dataout;
wirewrite_enable=we&~clk;
lpm_ram_dqram(.data(datain),.address(addr[7:
2]),.we(write_enable),.inclock(inclk),.outclock(outclk),.q(dataout));
defparamram.lpm_width=32;
defparamram.lpm_widthad=6;
defparamram.lpm_indata="registered";
defparamram.lpm_outdata="registered";
defparamram.lpm_file="mcmem.mif";
defparamram.lpm_address_control="registered";
endmodule
控制部件:
modulemccu(op,func,z,clock,resetn,wpc,wir,wmem,wreg,iord,regrt,m2reg,aluc,shift,alusrca,alusrcb,pcsource,jal,sext,state);
input[5:
0]op,func;
inputz,clock,resetn;
outputregwpc,wir,wmem,wreg,iord,regrt,m2reg;
outputreg[3:
0]aluc;
outputreg[1:
0]alusrcb,pcsource;
outputregshift,alusrca,jal,sext;
outputreg[2:
0]state;
reg[2:
0]next_state;
parameter[2:
0]sif=3'b000,//IFstate
sid=3'b001,//IDstate
sexe=3'b010,//EXEstate
smem=3'b011,//MEMstate
swb=3'b100;//WBstate
wirer_type,i_add,i_sub,i_and,i_or,i_xor,i_sll,i_srl,i_sra,i_jr;
wirei_addi,i_andi,i_ori,i_xori,i_lw,i_sw,i_beq,i_bne,i_lui,i_j,i_jal;
and(r_type,~op[5],~op[4],~op[3],~op[2],~op[1],~op[0]);
and(i_add,r_type,func[5],~func[4],~func[3],~func[2],~func[1],~func[0]);
and(i_sub,r_type,func[5],~func[4],~func[3],~func[2],func[1],~func[0]);
and(i_and,r_type,func[5],~func[4],~func[3],func[2],~func[1],~func[0]);
and(i_or,r_type,func[5],~func[4],~func[3],func[2],~func[1],func[0]);
and(i_xor,r_type,func[5],~func[4],~func[3],func[2],func[1],~func[0]);
and(i_sll,r_type,~func[5],~func[4],~func[3],~func[2],~func[1],~func[0]);
and(i_srl,r_type,~func[5],~func[4],~func[3],~func[2],func[1],~func[0]);
and(i_sra,r_type,~func[5],~func[4],~func[3],~func[2],func[1],func[0]);
and(i_jr,r_type,~func[5],~func[4],func[3],~func[2],~func[1],~func[0]);
and(i_addi,~op[5],~op[4],op[3],~op[2],~op[1],~op[0]);
and(i_andi,~op[5],~op[4],op[3],op[2],~op[1],~op[0]);
and(i_ori,~op[5],~op[4],op[3],op[2],~op[1],op[0]);
and(i_xori,~op[5],~op[4],op[3],op[2],op[1],~op[0]);
and(i_lw,op[5],~op[4],~op[3],~op[2],op[1],op[0]);
and(i_sw,op[5],~op[4],op[3],~op[2],op[1],op[0]);
and(i_beq,~op[5],~op[4],~op[3],op[2],~op[1],op[0]);
and(i_bne,~op[5],~op[4],~op[3],op[2],~op[1],op[0]);
and(i_lui,~op[5],~op[4],op[3],op[2],op[1],op[0]);
and(i_j,~op[5],~op[4],~op[3],~op[2],op[1],~op[0]);
and(i_jal,~op[5],~op[4],~op[3],~op[2],op[1],op[0]);
wirei_shift;
or(i_shift,i_sll,i_srl,i_sra);
always@*begin//controlsignals'dfaultoutputs:
wpc=0;//donotwritepc
wir=0;//donotwriteir
wmem=0;//donotwritememory
wreg=0;//donotwriteregisterfile
iord=0;//selectpcasmemoryaddress
aluc=4'bx000;//ALUoperation:
add
alusrca=0;//ALUinputa:
regaorsa
alusrcb=2'h0;//ALUinputb:
regb
regrt=0;//regdestno:
rd
m2reg=0;//selectregc
shift=0;//selectrega
pcsource=2'h0;//selectaluoutput
jal=0;//notajal
sext=1;//signextend
case(state)
//-----------------------------------------------------IF:
sif:
begin//IFstate
wpc=1;//writepc
wir=1;//writeIR
alusrca=1;//PC
alusrcb=2'h1;//4
next_state=sid;//nextstate:
ID
end
//-----------------------------------------------------ID:
sid:
begin//IDstate
if(i_j)begin//jinstruction
pcsource=2'h3;//jumpaddress
wpc=1;//writePC
next_state=sif;//nextstate:
IF
end
elseif(i_jal)begin//jalinstruction
pcsource=2'h3;//jumpaddress
wpc=1;//writePC
jal=1;//regno=31
wreg=1;//savePC+4
next_state=sif;//nextstate:
IF
end
elseif(i_jr)begin//jrinstruction
pcsource=2'h2;//jumpregister
wpc=1;//writePC
next_state=sif;//nextstate:
IF
end
elsebegin//otherinstruction
aluc=4'bx000;//add
alusrca=1;//PC
alusrcb=2'h3;//branchoffset
next_state=sexe;//nextstate:
EXE
end
end
//-----------------------------------------------------EXE:
sexe:
begin//EXEstate
aluc[3]=i_sra;
aluc[2]=i_sub|i_or|i_srl|i_sra|i_ori|i_lui;
aluc[1]=i_xor|i_sll|i_srl|i_sra|i_xori|i_beq|i_bne|i_lui;
aluc[0]=i_and|i_or|i_sll|i_srl|i_sra|i_andi|i_ori;
if(i_beq||i_bne)begin//beqorbneinstruction
pcsource=2'h1;//branchaddress
wpc=i_beq&z|i_bne&~z;//writePC
next_state=sif;//nextstate:
IF
end
elsebegin//otherinstruction
if(i_lw||i_sw)begin//lworswinstruction
alusrcb=2'h2;//selectoffset
next_state=smem;//nextstate:
MEM
end
elsebegin//otherinstruction
if(i_shift)
shift=1;//shiftinstruction
if(i_addi||i_andi||i_ori||i_xori||i_lui)
alusrcb=2'h2;//selectimmediate
if(i_andi||i_ori||i_xori)
sext=0;//0-extend
next_state=swb;//nextstate:
WB
end
end
end
//-----------------------------------------------------MEM:
smem:
begin//MEMstate
iord=1;//memoryaddress=C
if(i_lw)begin
next_state=swb;//nextstate:
WB
end
elsebegin//store
wmem=1;//writememory
next_state=sif;//nextstate:
IF
end
end
//-----------------------------------------------------WB:
swb:
begin//WBstate
if(i_lw)
m2reg=1;//selectmemorydata
if(i_lw||i_addi||i_andi||i_ori||i_xori||i_lui)
regrt=1;//regdestno:
rt
wreg=1;//writeregisterfile
next_state=sif;//nextstate:
IF
end
//-----------------------------------------------------END
default:
begin
next_state=sif;//defaultstate
end
endcase
end
always@(posedgeclockornegedgeresetn)begin//stateregisters
if(resetn==0)begin
state<=sif;
end
elsebegin
state<=next_state;
end
end
endmodule
32位带使能端触发器:
moduledffe32(d,clk,clrn,e,q);
input[31:
0]d;
inputclk,clrn,e;
output[31:
0]q;
reg[31:
0]q;
always@(negedgeclrnorposedgeclk)
if(clrn==0)begin
q<=0;
end
elsebegin
if(e==1)
q<=d;
end
endmodule
32位触发器:
moduledff32(d,clk,clrn,q);
input[31:
0]d;
inputclk,clrn;
output[31:
0]q;
reg[31:
0]q;
always@(negedgeclrnorposedgeclk)
if(clrn==0)begin
q<=0;
end
elsebegin
q<=d;
end
endmodule
ALU计算部件:
modulealu(a,b,aluc,r,z);
input[31:
0]a,b;
input[3:
0]aluc;
output[31:
0]r;
outputz;
assignr=cal(a,b,aluc);
assignz=~|r;
function[31:
0]cal;