本篇文章使用Verilog语言编写实现指令寄存器和取指模块,含有设计代码和测试代码。
一、接口描述/程序框图
指令寄存器接口描述表:
取指模块程序框图:
二、设计代码
(1)程序计数器设计代码
见之前发的程序计数器模块
(2)指令寄存器设计代码inst_rom.v
`timescale 1ns / 1ps
//`include "define.v"
module inst_rom(
input wire ce,
input wire [`InstAddrBus]addr,
output reg [`InstBus]inst
);
reg [`InstBus] inst_mem[0:127];
initial begin
$readmemh("E:/exper/inst_rom.data",inst_mem);//此处地址改为自己的指令.data文件
end
always@(*)begin
if(ce==`CeDisable)begin
inst<=`ZeroWord;
end
else begin
inst<=inst_mem[addr[31:2]];
end
end
endmodule
(3)取值模块设计代码inst_fetch.v
`timescale 1ns / 1ps
module inst_fetch(
input wire rst,
input wire clk,
output wire [`InstBus]inst_o
);
wire [`InstAddrBus]pc;
wire ce;
pc pc0(clk,rst,pc,ce);
inst_rom inst_rom0(ce,pc,inst_o);
endmodule
(4)宏定义文件define.v
`define InstBus 31:0
`define InstAddrBus 31:0
`define CeDisable 0
`define CeEnable 1
`define ZeroWord 0
三、测试代码
inst_fetch_tb.v
`timescale 1ns / 1ps
module inst_fetch_tb();
reg rst;
reg clk;
wire [`InstBus] inst_o;
inst_fetch inst_fetch0(rst,clk,inst_o);
initial begin
clk=1;
forever #10 clk=~clk;
end
initial begin
rst=0;
#10
rst=1;
#100
rst=0;
#500
$finish;
end
endmodule
四、仿真波形图
由波形图可得,当复位信号rst无效时,在每个时钟上升沿,取指模块向指令寄存器中读入文件中的指令。
仅供学习交流,如发现错误,欢迎大家指正。