0
0
Verilogprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Single-port RAM design
Start
Check write_enable?
NoRead data at address
|Yes
Write data at address
Output data
End
The RAM checks if write is enabled; if yes, it writes data to the address, else it reads data from the address and outputs it.
Execution Sample
Verilog
module single_port_ram(
  input clk,
  input we,
  input [3:0] addr,
  input [7:0] din,
  output reg [7:0] dout
);
  reg [7:0] ram [0:15];
  always @(posedge clk) begin
    if (we) begin
      ram[addr] <= din;
      dout <= din;
    end else begin
      dout <= ram[addr];
    end
  end
endmodule
This code defines a 16x8-bit single-port RAM with synchronous write and read on clock edge.
Execution Table
Stepclk edgewrite_enable (we)addrdinActiondout (output)
1rising1410101010Write 10101010 at addr 4; Read addr 410101010
2rising04XXXXXXXXNo write; Read addr 410101010
3rising1211110000Write 11110000 at addr 2; Read addr 211110000
4rising02XXXXXXXXNo write; Read addr 211110000
5rising04XXXXXXXXNo write; Read addr 410101010
💡 Simulation ends after 5 clock cycles demonstrating write and read operations.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
ram[4]undefined1010101010101010101010101010101010101010
ram[2]undefinedundefinedundefined111100001111000011110000
doutundefined1010101010101010111100001111000010101010
Key Moments - 3 Insights
Why does dout update even when write_enable (we) is 0?
Because the RAM reads the data at the given address every clock edge regardless of write_enable, as shown in rows 2, 4, and 5 of the execution_table.
What happens if write_enable is 1 and we write to an address?
The data at that address is updated with din on the clock edge, and dout outputs the new data immediately, as seen in rows 1 and 3.
Why is dout updated after writing and reading in the same clock cycle?
Because the design reads the RAM at the same clock edge after writing, so dout reflects the latest data at the address, demonstrated in the execution_table steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the value of dout?
A11110000
BXXXXXXXX
C10101010
D00000000
💡 Hint
Check the 'dout (output)' column at step 2 in the execution_table.
At which step does the RAM write 11110000 to address 2?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for the write operation to address 2 in the execution_table.
If write_enable (we) is always 0, what will dout show after multiple clock cycles?
AIt will show data at the current address without changes.
BIt will always be undefined.
CIt will show the last written data.
DIt will show random data.
💡 Hint
Refer to variable_tracker and execution_table rows where we=0 and observe dout values.
Concept Snapshot
Single-port RAM design in Verilog:
- Uses one port for read/write.
- On clock edge, if write_enable is 1, write data to address.
- Always read data at address and output it.
- Synchronous operation with clk.
- RAM size and data width defined by reg array.
Full Transcript
This visual execution traces a single-port RAM design in Verilog. The RAM has one port used for both reading and writing. On each rising clock edge, the design checks if write_enable is high. If yes, it writes the input data to the specified address. Regardless of writing, it reads the data at the address and outputs it. The execution table shows five clock cycles with different write_enable and addresses. The variable tracker shows how RAM contents and output change over time. Key moments clarify why output updates even when not writing and how writing affects RAM. The quiz tests understanding of output values and write timing. This design is synchronous and simple, suitable for beginners learning RAM behavior in hardware description languages.