1、0 OpCode,0 Funct, output regRegDst, output regALUSrc, output regRegWrite, output regMemWrite, output regMemRead, output regMemtoReg, output reg Branch, output reg Jump, output reg 3:0 ALUControl );reg 1:0 ALUOp;always (OpCode)begin case(OpCode) /R type 6b000000: begin RegDst=1;ALUSrc=0;RegWrite=1;Me
2、mWrite=0;MemRead=0;MemtoReg=0; Branch=0; ALUOp=2b10; Jump=0; end /beqb000100: RegDst=1bx;RegWrite=0;MemtoReg=1 Branch=1;b01; /lwb100011: RegDst=0;ALUSrc=1;MemRead=1;MemtoReg=1;b00; /swb101011:MemWrite=1; /Jumpb000010: ALUSrc=0; MemtoReg=0; RegWrite=0; MemRead=0; MemWrite=0; Branch=0; ALUOp=2 Jump=1;
3、endcaseend always (ALUOp or Funct)casex(ALUOp,Funct) 8b00xxxxxx: ALUControl=4b0010;b01xxxxxx:b0110;b1xxx0000:b1xxx0010:b1xxx0100:b0000;b1xxx0101:b0001;b1xxx1010:b0111; default: endcaseendmodulemodule ALU( input 31:0 SrcA,0 SrcB, input 3:0 ALUCtr, output Zero, output reg 31:0 ALUResassign Zero=(ALURe
4、s=1b0);always (SrcA or SrcB or ALUCtr)case(ALUCtr)4b0000: ALURes=SrcA&SrcB; /ANDb0001: ALURes=SrcA | SrcB; /ORb0010: ALURes=SrcA + SrcB; /addb0110: ALURes=SrcA - SrcB; /substractb0111: ALURes=SrcASrcB ? 1:0; /set on less thanb1100: ALURes=(SrcA | SrcB); /NORdefault ALURes=32h0;end四、仿真测试1.代码module Ct
5、r_tb; / Inputs reg 5:0 OpCode;0 Funct; / Outputs wire RegDst; wire ALUSrc; wire RegWrite; wire MemWrite; wire MemRead; wire MemtoReg; wire Branch; wire Jump; wire 3:0 ALUControl; / Instantiate the Unit Under Test (UUT) Ctruut ( .OpCode(OpCode), .Funct(Funct), .RegDst(RegDst), .ALUSrc(ALUSrc), .RegWr
6、ite(RegWrite), .MemWrite(MemWrite), .MemRead(MemRead), .MemtoReg(MemtoReg), .Branch(Branch), .Jump(Jump), .ALUControl(ALUControl) initial begin / R-type Add OpCode=6b000000; Funct=6b100000; / R-type Subtract #10;b100010; / Lwb100011;bxxxxxx; / Swb101011; / Beqb000100; / R-type ANDb100100; / R-type O
7、Rb100101; / R-type set on less thanb101010;/ Jumpb000010;module ALU_tb; reg 31:0 SrcA;0 SrcB; reg 3:0 ALUCtr; wire Zero; wire 31:0 ALURes; ALU uut ( .SrcA(SrcA), .SrcB(SrcB), .ALUCtr(ALUCtr), .Zero(Zero), .ALURes(ALURes) / AND SrcA =32hf0f0ffff; SrcB =32h0000f0f0; ALUCtr =0; / OR ALUCtr =4 / Add / S
8、ubtract / set on less than /NORb1100; /Other situation b1111;2.仿真截图五、实验总结Lab03: MIPS处理器部件实现B本实验旨在使读者实现MIPS处理器的部件Data memory, Instruction memory和Registers 三大存储器件。理解CPU的寄存器和内存,使用Verilog语言设计存储器件,使用ISim进行行为仿真。本实验旨在使读者掌握MIPS处理器中内存和寄存器的设计。在本实验中,利用Verilog HDL语言描述硬件逻辑实现和仿真内存和寄存器。实验由以下几个部分组成:1Instruction mem
9、ory的实现2Data Memory的实现3Register的实现4有符号扩展的实现本实验主要实现三大存储单元。处理器指令运行过程可以包括取指令、指令译码、执行、内存操作、寄存器回写,这些操作会对三种存储设备进行读或者写,但是不会同时对同一存储设备进行读写。所以为了实现单周期的MIPS,做这样一个设计,Instruction Memory用组合逻辑实现,完成类似于ROM的功能,仅作读操作;而Data memory 和Register的读操作用组合逻辑实现,而写操作用时序逻辑来实现。图2. MIPS存储设备Data memory是用来存储运行完成的数据,或者初始化的数据。其中用于控制Data m
10、emory的读写信号,可以由一个信号来控制,高低电平控制读写,分别用两个信号来控制读写(一)Instruction memory的实现module Instruction_memory(0 ImemRdAddr,0 Instructionreg 31:0 InstMem 0:255;/memory space for storing instructions/initial the instruction and data memoryinitial $readmemh(instruction, InstMem,8h0);always (ImemRdAddr) Instruction =Ins
11、tMemImemRdAddr;(二)Data Memory的实现module Data_memory( input Clk,0 DmemAddr, output 31:0 DmemRdData, input DmemWrite,0 DmemWrData0 DataMem 0:Data, DataMem,10always (posedgeClk) if(DmemWrite=1b1) DataMemDmemAddr=DmemWrData;assign DmemRdData =(DmemWrite=1b0)? DataMemDmemAddr:(三)Register 的实现module registe
12、r( input 4:0 RegARdAddr,0 RegBRdAddr,0 RegWrAddr,0 RegWrData, input RegWrite,0 RegARdData,0 RegBRdData0 regFile0:31;register, regFile, 32/write on falling clock edge always (negedgeClk) if(RegWrite=1 regFileRegWrAddr=RegWrData; assign RegARdData=(RegARdAddr !=0)?regFileRegARdAddr: assign RegBRdData=
13、(RegBRdAddr !regFileRegBRdAddr:module Instruction_memory_tb;0 ImemRdAddr;0 Instruction; Instruction_memoryuut ( .ImemRdAddr(ImemRdAddr), .Instruction(Instruction)reg 7:0 index; / Initialize Inputs ImemRdAddr = 0; index=0; $readmemh( / Wait 10 ns for global reset to finish for(index=0;index=255;index
14、=index+1) begin ImemRdAddr=index; end / Add stimulus heremodule Data_memory_tb; regClk;0 DmemAddr; regDmemWrite;0 DmemWrData;0 DmemRdData; Data_memoryuut ( .Clk(Clk), .DmemAddr(DmemAddr), .DmemRdData(DmemRdData), .DmemWrite(DmemWrite), .DmemWrData(DmemWrData) reg 7: Clk = 0; DmemAddr = 0; DmemWrite
15、= 1; DmemWrData = 0; /write data into memory for(index=0;=7; DmemAddr DmemWrData /read data from memory DmemWrite=0; /Clock Generatoralways #2 Clk=!Clk;module register_tb; reg 4:0 RegARdAddr;0 RegBRdAddr;0 RegWrAddr;0 RegWrData; regRegWrite;0 RegARdData;0 RegBRdData; register uut ( .RegARdAddr(RegARdAddr), .RegBRdAddr(RegBRdAddr), .RegWrAddr(RegWrAddr), .RegWrData(RegWrData), .RegARdData(RegARdData), .RegBRdData(RegBRdData) RegARdAddr = 0; RegBRdAddr = 0; RegWrAddr = 0; RegWrData = 0; RegWrite = 1; /write data into memory RegWrAddr RegWrData=6; RegARdAddr RegBRdAddr五、 实验总结
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1