0
0
Verilogprogramming~10 mins

Dual-port RAM design in Verilog - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dual-port RAM design
Start
Receive write address, data, write enable
If write enable is 1?
NoSkip write
|Yes
Write data to memory at write address
Receive read address
Read data from memory at read address
Output read data
Repeat for next clock cycle
The dual-port RAM allows simultaneous read and write operations on separate ports controlled by clock cycles.
Execution Sample
Verilog
module dual_port_ram(
  input clk,
  input we,
  input [3:0] write_addr,
  input [7:0] write_data,
  input [3:0] read_addr,
  output reg [7:0] read_data
);

  reg [7:0] ram [0:15];

  always @(posedge clk) begin
    if (we) begin
      ram[write_addr] <= write_data;
    end
    read_data <= ram[read_addr];
  end

endmodule
This code defines a dual-port RAM with separate read and write addresses and data lines.
Execution Table
Stepclkwewrite_addrwrite_dataread_addrMemory Stateread_dataAction
1000000000000000000[All zeros]00000000Initial state, no write, read outputs 0
2110010101010100000[Addr 2=10101010]00000000Write data 0xAA at address 2, read address 0 outputs 0
3000010101010100010[Addr 2=10101010]10101010No write, read from address 2 outputs 0xAA
4110001111100000010[Addr 1=11110000, Addr 2=10101010]10101010Write 0xF0 at address 1, read address 2 outputs 0xAA
5000001111100000001[Addr 1=11110000, Addr 2=10101010]11110000No write, read from address 1 outputs 0xF0
6100000000000000011[Addr 1=11110000, Addr 2=10101010]00000000No write, read from address 3 outputs 0
7End------Simulation ends
💡 Simulation ends after several clock cycles demonstrating read and write operations.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
clk010101End
we010100-
write_addr000000100010000100010000-
write_data000000001010101010101010111100001111000000000000-
read_addr000000000010001000010011-
read_data000000000000000010101010101010101111000000000000-
Memory[All zeros][Addr 2=10101010][Addr 2=10101010][Addr 1=11110000, Addr 2=10101010][Addr 1=11110000, Addr 2=10101010][Addr 1=11110000, Addr 2=10101010]-
Key Moments - 3 Insights
Why does the read_data output the old value immediately after a write?
Because the read happens in the same clock cycle but before the write updates memory, so read_data shows the previous value as seen in steps 2 and 3.
Can the write and read addresses be the same at the same time?
Yes, but the read will output the old data before the write updates memory in that clock cycle, as shown in step 4 and 5.
What happens if write enable (we) is 0?
No write occurs; memory stays unchanged and read outputs data from the read address, as in steps 1, 3, 5, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what is the value of read_data?
A10101010
B00000000
C11110000
DUndefined
💡 Hint
Check the 'read_data' column at step 3 in the execution_table.
At which step does the memory first get written with 11110000?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Memory State' column to see when Addr 1 changes.
If write enable (we) was always 0, what would happen to memory?
AMemory would update every clock cycle
BMemory would only update on read
CMemory would never change from initial zeros
DMemory would randomly change
💡 Hint
Refer to the 'we' column and memory changes in the execution_table.
Concept Snapshot
Dual-port RAM allows simultaneous read and write.
Write port uses write address, data, and write enable.
Read port uses read address to output data.
Writes happen on clock edge if enabled.
Reads output data from memory at read address.
Read and write ports operate independently.
Full Transcript
This visual execution trace shows how a dual-port RAM works in Verilog. The RAM has separate ports for reading and writing. On each clock cycle, if write enable is high, data is written to the memory at the write address. Simultaneously, data is read from the memory at the read address and output. The trace shows step-by-step how the clock, write enable, addresses, and data change, and how memory updates and read data outputs respond. Key moments clarify why read data shows old values immediately after writes and how write enable controls memory updates. The quiz tests understanding of memory state and outputs at different steps. The snapshot summarizes the dual-port RAM behavior and usage.