In my previous blog post, I investigated a method for replicating a single-port RAM into a multiport RAM using an LVT.
https://msyksphinz-en.hatenablog.com/entry/2023/12/07/040000msyksphinz-en.hatenablog.com
In the method using an LVT (Live Value Table), for each RAM index we prepare an LVT entry that indicates which bank RAM holds the latest value. This method is powerful and convenient, but the more ports there are, the wider the LVT has to be.
For example, a fairly high-performance-oriented CPU needs an outrageous number of ports, such as 12 read ports and 10 write ports. Also, considering the number of physical registers, we have to manage a RAM with something like 256 entries, and if we manage this purely with an LVT, the number of FFs required for the LVT can be calculated as follows.
256 * $clog2(10) = 1024 FF
On top of that, the logic for updating this LVT also becomes quite demanding. A naive implementation would result in FFs with 10 write ports, and the surrounding logic circuits would also become quite large.
always_ff @ (posedge i_clk, negedge i_reset_n) begin if (!i_reset_n) begin for (int i = 0; i < WORDS; i++) r_lvt[i] <= 'h0; end else begin for (int i = 0; i < WR_PORTS; i++) begin if (i_wr[i]) begin r_lvt[i_wr_addr[i]] <= i; end end end end // always_ff @ (posedge i_clk, negedge i_reset_n)
So I looked into the XOR multiport RAM, a method that eliminates even the LVT. This method can remove the LVT, but it requires additional RAMs.
The basic idea is as follows. Suppose, for example, that there are two write ports, and that they use RAM1 and RAM2, respectively. Suppose that VALUE1 and VALUE2 have been written to some index A in each of them.
Now suppose that NEW is written through write port 0. In this case, we write to RAM1 a value modified using the value in RAM2, as follows.
RAM1 New Value = NEW ^ VALUE2
Then, when reading, we XOR the value of RAM1 (NEW ^ VALUE2) with the value of RAM2 (VALUE2) and read out the result. This gives NEW ^ VALUE2 ^ VALUE2 = NEW, so the value NEW can be read out.

However, there is a problem with this approach. When a read and a write occur at the same time, the write also needs a read port in order to read out the existing value. Naively, we would need to add one more port for this, but there is a way to avoid that: we can simply add new RAMs dedicated to that purpose. Since these RAMs store the data written by the ports other than the port itself, each write port needs (number of write ports - 1) of them.

As a result, the total number of RAMs required is as follows.
Number of RAMs needed for writing = number of write ports * (number of write ports - 1) Number of RAMs needed for reading = number of write ports * number of read ports Total number of RAMs needed = number of RAMs needed for writing + number of RAMs needed for reading = number of write ports * (number of write ports - 1 + number of read ports)
Comparing the number of RAMs needed with the LVT approach for the case above, with 12 read ports and 10 write ports, we get:
- LVT approach = 10 * 12= 120
- XOR approach = 10 * (10-1+12) =10 * 21 = 210
So the number increases significantly. Still, since this completely removes the LVT, which is a critical path, it should be useful depending on where it is used.
This is an English translation of the original Japanese post: マルチポートRAMを使用するためのXOR Multiport RAMの実装