module dual_port_ram_example #(
parameter DATA_WIDTH = 8,
parameter ADDR_WIDTH = 4,
parameter RAM_DEPTH = 16
) (
input clk,
input rst,
input enable_a,
input [ADDR_WIDTH-1:0] addr_a,
input [DATA_WIDTH-1:0] data_in_a,
output reg [DATA_WIDTH-1:0] data_out_a,
input enable_b,
input [ADDR_WIDTH-1:0] addr_b,
output reg [DATA_WIDTH-1:0] data_out_b
);
reg [DATA_WIDTH-1:0] ram [0:RAM_DEPTH-1];
// Port A
always @(posedge clk) begin
if (rst) begin
data_out_a <= 0;
end else if (enable_a) begin
data_out_a <= ram[addr_a];
end
if (enable_a && !enable_b) begin // Write to Port A
ram[addr_a] <= data_in_a;
end
end
// Port B
always @(posedge clk) begin
if (rst) begin
data_out_b <= 0;
end else if (enable_b) begin
data_out_b <= ram[addr_b];
end
if (enable_b && !enable_a) begin // Write to Port B
ram[addr_b] <= data_in_a; // Using data_in_a for simplicity. In a real design, you'd have a separate data input for Port B.
end
end
endmodule
Explanation:
Parameters:
DATA_WIDTH: The width of the data being stored (e.g., 8 bits).
ADDR_WIDTH: The width of the address bus (e.g., 4 bits).
RAM_DEPTH: The number of memory locations (e.g., 16).
Inputs/Outputs:
clk: Clock signal.
rst: Reset signal.
enable_a, enable_b: Enable signals for each port. These control when writes are allowed.
addr_a, addr_b: Address inputs for Port A and Port B.
data_in_a: Data input for Port A (used for writing).
data_out_a, data_out_b: Data output for Port A and Port B, respectively.
Memory Array:
reg [DATA_WIDTH-1:0] ram [0:RAM_DEPTH-1];: Declares a memory array to store the data.
Port A Logic:
always @(posedge clk): This block executes on the rising edge of the clock.
if (rst): Resets the output to 0.
if (enable_a): If enable_a is high, the output data_out_a is assigned the value of the memory location ram[addr_a].
if (enable_a && !enable_b): If enable_a is high andenable_b is low, the memory location ram[addr_a] is written with the value of data_in_a. This ensures that only one port can write at a time.
Port B Logic:
Similar logic to Port A, but for Port B. The if (enable_b && !enable_a) condition ensures that Port B can only write when Port A is not writing.
How to Use:
Instantiate the module: In your top-level design, instantiate this dual_port_ram_example module.
Connect signals: Connect the clock, reset, enable, address, and data signals to the appropriate pins.
Control the ports: Use the enable_a and enable_b signals to control which port is active.
Write data: When enable_a is high, provide data to data_in_a to write to Port A. When enable_b is high, provide data to data_in_a to write to Port B.
Read data: When enable_a is high, data_out_a will contain the data stored at the address specified by addr_a. When enable_b is high, data_out_b will contain the data stored at the address specified by addr_b.
Important Considerations:
Write Conflicts: This example uses a simple mechanism to prevent write conflicts. In a real design, you might need more sophisticated arbitration logic to handle more complex scenarios.
Data Input for Port B: In this example, both ports use the same data_in_a input. In a real design, you would typically have separate data inputs for each port.
BRAM Configuration: This Verilog code implements the dual-port RAM functionality. To actually use this in an Artix-7 FPGA, you would need to configure it as a dual-port Block RAM using the Xilinx IP Catalog. The IP Catalog will handle the hardware implementation of the dual-port RAM. The Verilog code provides the logical behavior.
Write Mode: The behavior when both ports try to write to the same address is undefined in this example. You'll need to choose a write mode (WRITE_FIRST, READ_FIRST, NO_CHANGE) when configuring the BRAM in the IP Catalog.
This minimal example provides a basic understanding of how dual-port RAM can be implemented in Verilog. For more complex designs, you may need to add additional features such as write arbitration, data masking, and error handling.