Single Port RAM Fix
Single Port RAM Fix
Your single-port RAM Verilog code is logically structured but has a few areas for improvement:
Corrected Code:
verilog
module single_port_ram (
input [7:0] data,
input [5:0] addr,
input we, clock,
output reg [7:0] q
);
endmodule
verilog
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/8
reg [5:0] addr;
reg we;
reg clock;
wire [7:0] q;
initial clock = 1;
always #5 clock = ~clock; // Clock Toggle Every 5 Time Units
initial begin
$dumpfile("wave_single_port_ram.vcd"); // Added .vcd Extension
$dumpvars(0, single_port_ram_tb);
end
initial begin
$monitor(" time=%3t | data=%h | addr=%d | we=%b | clock=%b | q=%h ", $time, data, addr,
end
initial begin
// Write Cycle
we = 1;
data = 8'h01; addr = 6'd0; #10;
data = 8'h02; addr = 6'd1; #10;
data = 8'h03; addr = 6'd2; #10;
// Read Cycle
we = 0;
#5; addr = 6'd0; #10; // Small Delay Added
#5; addr = 6'd1; #10;
#5; addr = 6'd2; #10;
$finish;
end
endmodule
When you run the simulation, the RAM should behave as follows:
0 1 0 0x01 -
10 1 1 0x02 -
20 1 2 0x03 -
30 0 0 - 0x01
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/8
Time (ns) `we` `addr` `data` (Written) `q` (Read Data)
40 0 1 - 0x02
50 0 2 - 0x03
verilog
module dual_port_ram (
input clk, reset,
// PORT 0
input [5:0] address_0,
inout [7:0] data_0,
input cs_0, we_0, oe_0,
// PORT 1
input [5:0] address_1,
inout [7:0] data_1,
input cs_1, we_1, oe_1
);
assign data_0 = (cs_0 && oe_0 && !we_0) ? data_out_0 : 8'bz; // Output only when OE is enab
assign data_1 = (cs_1 && oe_1 && !we_1) ? data_out_1 : 8'bz;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/8
if (we_0 || we_1) begin
for (i = 0; i < 64; i = i + 1)
ram[i] <= 8'b0; // Clear memory on reset if write enabled
end
end
else begin
// PORT 0 Operation
if (cs_0) begin
if (we_0)
ram[address_0] <= data_0; // Write Operation
else if (oe_0)
data_out_0 <= ram[address_0]; // Read Operation
end
// PORT 1 Operation
if (cs_1) begin
if (we_1)
ram[address_1] <= data_1; // Write Operation
else if (oe_1)
data_out_1 <= ram[address_1]; // Read Operation
end
end
end
endmodule
Testbench: dual_port_ram_tb
verilog
module dual_port_ram_tb;
dual_port_ram dut (
.clk(clk),
.reset(reset),
.address_0(address_0),
.data_0(data_0),
.cs_0(cs_0),
.we_0(we_0),
.oe_0(oe_0),
.address_1(address_1),
.data_1(data_1),
.cs_1(cs_1),
.we_1(we_1),
.oe_1(oe_1)
);
initial clk = 0;
always #5 clk = ~clk;
initial begin
$dumpfile("dual_port_ram.vcd");
$dumpvars(0, dual_port_ram_tb);
reset = 1;
cs_0 = 0; we_0 = 0; oe_0 = 0;
cs_1 = 0; we_1 = 0; oe_1 = 0;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/8
#10 reset = 0; // De-assert reset
$finish;
end
endmodule
40 0 5 0 1 - 0xCC
40 1 10 0 1 - 0x33
Key Enhancements
This RTL Design is ready for synthesis and FPGA implementation. 🚀 Let me know if you need
any modifications!
Here is the Verilog RTL design for a Synchronous Dual-Port RAM with Separate Read/Write
Ports that meets your specifications.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/8
Key Features Implemented
verilog
module dual_port_ram (
input clk, reset,
input [5:0] address_rd, address_wr,
input [7:0] data_wr,
output reg [7:0] data_rd,
input cs, we, re
);
// Read Operation
if (re)
data_rd <= ram[address_rd];
end
end
end
endmodule
Testbench: dual_port_ram_tb
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/8
verilog
module dual_port_ram_tb;
dual_port_ram dut (
.clk(clk),
.reset(reset),
.address_rd(address_rd),
.address_wr(address_wr),
.data_wr(data_wr),
.data_rd(data_rd),
.cs(cs),
.we(we),
.re(re)
);
initial clk = 0;
always #5 clk = ~clk; // Clock Toggle Every 5 Time Units
initial begin
$dumpfile("dual_port_ram.vcd");
$dumpvars(0, dual_port_ram_tb);
reset = 1; cs = 0; we = 0; re = 0;
#10 reset = 0; // De-assert reset
$finish;
end
endmodule
10 1 0 5 0xAA - -
20 0 1 - - 5 0xAA
30 1 1 10 0x55 5 0xAA
40 0 1 - - 10 0x55
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/8
Key Enhancements
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/8