题解 | #单端口RAM#
单端口RAM
https://www.nowcoder.com/practice/a1b0c13edba14a2984e7369d232d9793
`timescale 1ns/1ns
module RAM_1port(
input clk,
input rst,
input enb,
input [6:0]addr,
input [3:0]w_data,
output wire [3:0]r_data
);
//*************code***********//
reg [3:0] ram [127:0]; //前面是位宽,后面是个数
//无语了,题目默认enb=1是写,enb=0是读
reg [3:0] rdata;
always @(negedge clk,negedge rst)begin
if (!rst) begin //I want to write a tast to finish this
reset_mem();
rdata<=0;
end
else begin
if (enb) begin
rdata<=rdata;
ram[addr]<=w_data;
end
else rdata<=ram[addr];
end
end
assign r_data=rdata;
task reset_mem;
integer i;
for (i=0;i<128;i=i+1)begin
ram[i] = 0;
end
endtask
//*************code***********//
endmodule