题解 | ROM的简单实现

ROM的简单实现

https://www.nowcoder.com/practice/b76fdef7ffa747909b0ea46e0d13738a

`timescale 1ns/1ns
module rom(
	input clk,
	input rst_n,
	input [7:0]addr,
	
	output [3:0]data
);
//only read memory
//1个ROM钟有8个4bit位宽的数据
reg [3:0]rom_data[7:0];//多位数组
//时序关系
//输入地址addr,输出相应的数据data
assign data = rom_data[addr];
//only read memory
//只读存储器
//保持ROM数据不变
always@(posedge clk or negedge rst_n)
if(!rst_n)begin
	//必须复位才可以使用,使得rom中存储对应的初始化数据
	rom_data[0] <= 4'd0;
	rom_data[1] <= 4'd2;
	rom_data[2] <= 4'd4;
	rom_data[3] <= 4'd6;
	rom_data[4] <= 4'd8;
	rom_data[5] <= 4'd10;
	rom_data[6] <= 4'd12;
	rom_data[7] <= 4'd14;
end
else begin
	rom_data[0] <= rom_data[0];
	rom_data[1] <= rom_data[1];
	rom_data[2] <= rom_data[2];
	rom_data[3] <= rom_data[3];
	rom_data[4] <= rom_data[4];
	rom_data[5] <= rom_data[5];
	rom_data[6] <= rom_data[6];
	rom_data[7] <= rom_data[7];
end

endmodule

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务