题解 | #ROM的简单实现#

ROM的简单实现

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

`timescale 1ns/1ns
module rom(
	input clk,
	input rst_n,
	input [7:0]addr,
	
	output 	reg	[3:0]	data
);

reg		[3:0]	rom[7:0];

always@(posedge clk or negedge rst_n)	//题目给的答案是组合逻辑或者双边沿采样才能做到
	if(!rst_n)
		data <= 4'd0;
	else if(addr < 8'd8)
		data <= rom[addr[2:0]];
	else
		data <= 4'd0;

always@(posedge	clk or negedge rst_n)
	if(!rst_n)
		begin
			rom[0] <= 4'd0;
			rom[1] <= 4'd2;
			rom[2] <= 4'd4;
			rom[3] <= 4'd6;
			rom[4] <= 4'd8;
			rom[5] <= 4'd10;
			rom[6] <= 4'd12;
			rom[7] <= 4'd14;
		end

			
endmodule

全部评论

相关推荐

点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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