题解 | #单端口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 ); reg [3:0] mem [127:0]; //单端口RAM的读写使能enb-------enb=1时,对寄存器进行写操作;enb=0时,对寄存器进行读操作; // 数据量太多,考虑使用generate... for...语句 // 写入RAM genvar i; generate for(i=0;i<128;i=i+1) begin : ram_w always@(posedge clk or negedge rst) begin if(!rst) mem[i] <= 0; else if(enb) mem[addr] <= w_data ; else mem[addr] <=mem[addr]; end end endgenerate //读RAM assign r_data = (~enb)? mem[addr]:4'd0; endmodule