题解 | #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 ); reg [3:0] myrom [7:0]; always@(posedge clk or negedge rst_n)begin if(!rst_n)begin myrom[0]<=0; myrom[1]<=2; myrom[2]<=4; myrom[3]<=6; myrom[4]<=8; myrom[5]<=10; myrom[6]<=12; myrom[7]<=14; end else begin myrom[0]<=myrom[0]; myrom[1]<=myrom[1]; myrom[2]<=myrom[2]; myrom[3]<=myrom[3]; myrom[4]<=myrom[4]; myrom[5]<=myrom[5]; myrom[6]<=myrom[6]; myrom[7]<=myrom[7]; end end assign data=myrom[addr]; endmodule
实现rom要求:存储数据,根据addr可读数据。存数据方法:生成4位宽8深度(8位数组)寄存器(数组),通过always语句写入原始数值,else写入维持信号防止Latch。因为题目有隐含条件,无上升沿时addr信号改变,data信号也会改变,所以需要通过组合逻辑连接data与myrom[addr],这样实现实时的查询