本篇文章使用Verilog語言編寫實現(xiàn)帶有優(yōu)先級的83譯碼器,含有設計代碼和測試代碼。
一、
寄存器堆regfile模塊實現(xiàn)了32個32位通用寄存器??梢酝瑫r進行兩個寄存器的讀操作和一個寄存器的寫操作。寫操作是同步寫,寫使能信號(we)為1時有效,為0時無效;讀操作可以在任意時刻進行讀操作。
(1)當復位信號有效(rst為1)時,讀數(shù)據(jù)(rdata1和rdata2)為0
(2)否則當復位信號無效(rst為0)時,當讀地址為0,讀數(shù)據(jù)為0
(3)否則當讀寫地址相等,且讀寫使能都有效的時候,讀數(shù)據(jù)為寫數(shù)據(jù)
(4)否則當讀使能有效時,讀數(shù)據(jù)為寄存器堆中存儲數(shù)據(jù)
(5)其余情況,讀數(shù)據(jù)為0
接口描述表如下:
接口名 | 寬度 | 輸入/輸出 |
---|---|---|
rst | 1 | 輸入 |
clk | 1 | 輸入 |
waddr | 5 | 輸入 |
wdata | 32 | 輸入 |
we | 1 | 輸入 |
raddr1 | 5 | 輸入 |
re1 | 1 | 輸入 |
rdata2 | 32 | 輸出 |
raddr2 | 5 | 輸入 |
re2 | 1 | 輸入 |
rdata2 | 32 | 輸出 |
二、宏定義文件
將宏定義文件(此處為define.v)右鍵設置為global include文件,即可在其他文件中進行引用。
`define RegAddrBus 4:0
`define RegDataBus 31:0
`define RegNum 31:0
`define RstEnable 1
`define RstDisable 0
`define ZeroWord 0
`define ReadEnable 1
`define WriteEnable 1
三、設計代碼
`timescale 1ns / 1ps
`include "define.v"
module regfile(
input wire clk,
input wire rst,
input wire [`RegAddrBus] waddr,
input wire [`RegDataBus] wdata,
input wire we,
input wire [`RegAddrBus] raddr1,
input wire re1,
output reg [`RegDataBus] rdata1,
input wire [`RegAddrBus] raddr2,
input wire re2,
output reg [`RegDataBus] rdata2
);
reg [`RegDataBus] reg1[`RegNum];
always@(*)begin
//復位信號為0,寫信號為1,當前地址不為0時,數(shù)據(jù)寫入寄存器
if(rst==`RstDisable)begin
if((we==`WriteEnable)&&(waddr!=0))begin
reg1[waddr]<=wdata;
end
end
end
//讀rdata1
always@(*)begin
if(rst==`RstEnable)begin
//復位信號有效時,讀數(shù)據(jù)為0
rdata1<=`ZeroWord;
end
//復位信號無效時
//讀信號為1,讀地址為0時,讀數(shù)據(jù)為0
else if((re1==`ReadEnable)&&(raddr1==0))begin
rdata1<=`ZeroWord;
end
//讀信號為1,寫信號為1,讀地址=寫地址
else if((re1==`ReadEnable)&&(we==`WriteEnable)&&(raddr1==waddr))begin
rdata1<=wdata;
end
else if(re1==`ReadEnable) begin
rdata1<=reg1[raddr1];
end
//其余情況下,讀數(shù)據(jù)為0
else begin
rdata1<=`ZeroWord;
end
end
//讀rdata2
always@(*)begin
if(rst==`RstEnable)begin
rdata2<=`ZeroWord;
end
else if((re2==`ReadEnable)&&(raddr2==0))begin
rdata2<=`ZeroWord;
end
else if((re2==`ReadEnable)&&(we==`WriteEnable)&&(raddr2==waddr))begin
rdata2<=wdata;
end
else if(re2==`ReadEnable) begin
rdata2<=reg1[raddr2];
end
else begin
rdata2<=`ZeroWord;
end
end
endmodule
四、測試代碼
`timescale 1ns / 1ps
module regfile_tb();
reg clk;
reg rst;
reg [`RegAddrBus] waddr;
reg [`RegDataBus] wdata;
reg we;
reg [`RegAddrBus] raddr1;
reg re1;
wire [`RegDataBus] rdata1;
reg [`RegAddrBus] raddr2;
reg re2;
wire [`RegDataBus] rdata2;
regfile regfile0(rst,clk,waddr,wdata,we,raddr1,re1,rdata1,raddr2,re2,rdata2);
integer i;
initial begin
clk=1;
forever begin
#10 clk=~clk;
end
end
//若把寫和讀放在兩個initial中,存在并列的問題
initial begin
rst=0;
#10
rst=1;
#30
rst=0;
#30
//寫使能有效,寫數(shù)據(jù)有效
we=1;
wdata=32'h1234;
for(i=0;i<=31;i=i+1)begin
waddr=i;
wdata=wdata+32'h1111;
#30;
end
//寫無效,讀有效
we=0;
re1=1;
re2=1;
for(i=0;i<=31;i=i+1)begin
raddr1=i;
raddr2=31-i;
#30;
end
//讀寫地址相等
we=1;
wdata=32'h5678;
for(i=0;i<=31;i=i+1)begin
waddr=i;
raddr1=i;
raddr2=i;
#30;
wdata=wdata+32'h0101;
end
//恢復0
re1=0;
re2=0;
we=0;
#100 $finish;
end
endmodule
五、仿真波形圖
文章來源:http://www.zghlxwxcb.cn/news/detail-475002.html
僅供學習交流,如發(fā)現(xiàn)錯誤,歡迎大家指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-475002.html
到了這里,關(guān)于Vivado MIPS寄存器堆(含測試代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!