题目
实现一个深度为8,位宽为4bit的ROM,数据初始化为0,2,4,6,8,10,12,14。可以通过输入地址addr,输出相应的数据data。该模块的接口信号图如下:
信号 | 类型 | 输入/输出 | 位宽 |
---|---|---|---|
clk | wire | Intput | 1 |
rst_n | wire | Intput | 1 |
addr | wire | Intput | 8 |
data | wire | Output | 4 |
其中,输入的8比特位宽的地址和输出的4比特位宽的数据均为无符号数。
答案
需要注意的是,题目中说数据初始化为0,2,4,6,8,10,12,14,是对前8个地址初始化为该数值,即第0~7个地址。
`timescale 1ns/1ns
module rom(
input clk,
input rst_n,
input [7:0]addr,
output [3:0]data
);
reg [3:0] rom_data [7:0];
always @(posedge clk or negedge rst_n)
if(!rst_n) begin
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] <= 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
assign data = rom_data[addr];
endmodule