ROM.v代码
这个模块设计的关键是在复位信号中执行初始化代码,读取指定位置的HEX文件中的数据初始化rom,然后在其他时钟沿时刻输出地址所指的数据。
//read hex file to initial ROM or RAM
module ROM(
input clk,
input rst_n,
input[15:0] addr,
output reg[7:0] q
);
parameter filename="F:/project/cpu/code/ModelSim/04_ROMInitTest/src/ROM.hex";
reg[ 7:0] char_1st;
reg[15:0] address; // Rom address
reg[ 7:0] len; // bytes of one line in the hex file
reg[ 7:0] dat;
reg[7:0] sum; // intel hex file verification
reg[640:1] errstr;
reg[7:0] rom[0:4095];
reg CanRead;
integer i,fp,code;
always@(posedge clk)
if(!rst_n)begin
char_1st = 0;
address =0;
len =0;
dat =0;
sum =0;
CanRead =1;
fp=$fopen(filename,"r");
if