|
| 1 | +// Copyright © 2019-2023 |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +`include "VX_platform.vh" |
| 15 | + |
| 16 | +`define RAM_WRITE_WREN for (integer i = 0; i < WRENW; ++i) begin \ |
| 17 | + if (wren[i]) begin \ |
| 18 | + ram[waddr][i * WSELW +: WSELW] <= wdata[i * WSELW +: WSELW]; \ |
| 19 | + end \ |
| 20 | + end |
| 21 | + |
| 22 | +`define RAM_INITIALIZATION \ |
| 23 | + if (INIT_ENABLE != 0) begin : g_init \ |
| 24 | + if (INIT_FILE != "") begin : g_file \ |
| 25 | + initial $readmemh(INIT_FILE, ram); \ |
| 26 | + end else begin : g_value \ |
| 27 | + initial begin \ |
| 28 | + for (integer i = 0; i < SIZE; ++i) begin : g_i \ |
| 29 | + ram[i] = INIT_VALUE; \ |
| 30 | + end \ |
| 31 | + end \ |
| 32 | + end \ |
| 33 | + end |
| 34 | + |
| 35 | +`define RAM_BYPASS(__d) \ |
| 36 | + reg [DATAW-1:0] bypass_data_r; \ |
| 37 | + reg bypass_valid_r; \ |
| 38 | + always @(posedge clk) begin \ |
| 39 | + bypass_valid_r <= read_s && write && (raddr_s == waddr); \ |
| 40 | + bypass_data_r <= wdata; \ |
| 41 | + end \ |
| 42 | + assign __d = bypass_valid_r ? bypass_data_r : rdata_r |
| 43 | + |
| 44 | +`TRACING_OFF |
| 45 | +module VX_async_ram_patch #( |
| 46 | + parameter DATAW = 1, |
| 47 | + parameter SIZE = 1, |
| 48 | + parameter WRENW = 1, |
| 49 | + parameter DUAL_PORT = 0, |
| 50 | + parameter INIT_ENABLE = 0, |
| 51 | + parameter INIT_FILE = "", |
| 52 | + parameter [DATAW-1:0] INIT_VALUE = 0, |
| 53 | + parameter ADDRW = `LOG2UP(SIZE) |
| 54 | +) ( |
| 55 | + input wire clk, |
| 56 | + input wire reset, |
| 57 | + input wire read, |
| 58 | + input wire write, |
| 59 | + input wire [WRENW-1:0] wren, |
| 60 | + input wire [ADDRW-1:0] waddr, |
| 61 | + input wire [DATAW-1:0] wdata, |
| 62 | + input wire [ADDRW-1:0] raddr, |
| 63 | + output wire [DATAW-1:0] rdata |
| 64 | +); |
| 65 | + localparam WSELW = DATAW / WRENW; |
| 66 | + |
| 67 | + `UNUSED_VAR (reset) |
| 68 | + |
| 69 | + (* keep = "true" *) wire [ADDRW-1:0] raddr_w, raddr_s; |
| 70 | + (* keep = "true" *) wire read_s, is_raddr_reg; |
| 71 | + |
| 72 | + assign raddr_w = raddr; |
| 73 | + |
| 74 | + VX_placeholder #( |
| 75 | + .I (ADDRW), |
| 76 | + .O (ADDRW + 1 + 1) |
| 77 | + ) placeholder ( |
| 78 | + .in (raddr_w), |
| 79 | + .out ({raddr_s, read_s, is_raddr_reg}) |
| 80 | + ); |
| 81 | + |
| 82 | + // synchroneous ram |
| 83 | + |
| 84 | + wire [DATAW-1:0] rdata_s; |
| 85 | + |
| 86 | + if (WRENW != 1) begin : g_wren_sync_ram |
| 87 | + `USE_BLOCK_BRAM reg [DATAW-1:0] ram [0:SIZE-1]; |
| 88 | + reg [DATAW-1:0] rdata_r; |
| 89 | + `RAM_INITIALIZATION |
| 90 | + always @(posedge clk) begin |
| 91 | + if (read_s || write) begin |
| 92 | + if (write) begin |
| 93 | + `RAM_WRITE_WREN |
| 94 | + end |
| 95 | + rdata_r <= ram[raddr_s]; |
| 96 | + end |
| 97 | + end |
| 98 | + `RAM_BYPASS(rdata_s); |
| 99 | + end else begin : g_no_wren_sync_ram |
| 100 | + `USE_BLOCK_BRAM reg [DATAW-1:0] ram [0:SIZE-1]; |
| 101 | + reg [DATAW-1:0] rdata_r; |
| 102 | + `RAM_INITIALIZATION |
| 103 | + `UNUSED_VAR (wren) |
| 104 | + always @(posedge clk) begin |
| 105 | + if (read_s || write) begin |
| 106 | + if (write) begin |
| 107 | + ram[waddr] <= wdata; |
| 108 | + end |
| 109 | + rdata_r <= ram[raddr_s]; |
| 110 | + end |
| 111 | + end |
| 112 | + `RAM_BYPASS(rdata_s); |
| 113 | + end |
| 114 | + |
| 115 | + // asynchronous ram (fallback) |
| 116 | + |
| 117 | + wire [DATAW-1:0] rdata_a; |
| 118 | + |
| 119 | + if (DUAL_PORT != 0) begin : g_dp_async_ram |
| 120 | + reg [DATAW-1:0] ram [0:SIZE-1]; |
| 121 | + `RAM_INITIALIZATION |
| 122 | + if (WRENW != 1) begin : g_wren |
| 123 | + always @(posedge clk) begin |
| 124 | + if (write) begin |
| 125 | + `RAM_WRITE_WREN |
| 126 | + end |
| 127 | + end |
| 128 | + end else begin : g_no_wren |
| 129 | + always @(posedge clk) begin |
| 130 | + if (write) begin |
| 131 | + ram[waddr] <= wdata; |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + assign rdata_a = ram[raddr]; |
| 136 | + end else begin : g_sp_async_ram |
| 137 | + reg [DATAW-1:0] ram [0:SIZE-1]; |
| 138 | + `RAM_INITIALIZATION |
| 139 | + if (WRENW != 1) begin : g_wren |
| 140 | + always @(posedge clk) begin |
| 141 | + if (write) begin |
| 142 | + `RAM_WRITE_WREN |
| 143 | + end |
| 144 | + end |
| 145 | + end else begin : g_no_wren |
| 146 | + always @(posedge clk) begin |
| 147 | + if (write) begin |
| 148 | + ram[waddr] <= wdata; |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + assign rdata_a = ram[waddr]; |
| 153 | + end |
| 154 | + |
| 155 | + assign rdata = is_raddr_reg ? rdata_s : rdata_a; |
| 156 | + |
| 157 | +endmodule |
| 158 | +`TRACING_ON |
0 commit comments