需要注意的点:
1.题目要求的是双端口RAM,执行的读写操作互不干扰,因此需要写两个always块
2.采用for循环语句对RAM进行初始化,不能写成RAM[addr] <= 'b0的形式
总体代码
`timescale 1ns/1ns module ram_mod( input clk, input rst_n, input write_en, input [7:0]write_addr, input [3:0]write_data, input read_en, input [7:0]read_addr, output reg [3:0]read_data ); reg [3:0] RAM [0:7]; integer i; //写操作 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin for(i=0;i<8;i=i+1) RAM[i] <= 4'b0000; end else if(write_en)begin RAM[write_addr] <= write_data; end end //读操作 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin read_data <= 4'b0; end else if(read_en)begin read_data <= RAM[read_addr]; end end endmodule