|
| 1 | +// Copyright 2023 ETH Zurich and University of Bologna. |
| 2 | +// Solderpad Hardware License, Version 0.51, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: SHL-0.51 |
| 4 | + |
| 5 | +// Author: Michael Rogenmoser <michaero@iis.ee.ethz.ch> |
| 6 | + |
| 7 | +module obi_sim_mem import obi_pkg::*; #( |
| 8 | + parameter obi_pkg::obi_cfg_t ObiCfg = obi_pkg::ObiDefaultConfig, |
| 9 | + parameter type obi_req_t = logic, |
| 10 | + parameter type obi_rsp_t = logic, |
| 11 | + parameter type obi_r_chan_t = logic, |
| 12 | + parameter bit WarnUninitialized = 1'b0, |
| 13 | + parameter bit ClearErrOnAccess = 1'b0, |
| 14 | + parameter time ApplDelay = 0ps, |
| 15 | + parameter time AcqDelay = 0ps |
| 16 | +) ( |
| 17 | + input logic clk_i, |
| 18 | + input logic rst_ni, |
| 19 | + input obi_req_t obi_req_i, |
| 20 | + output obi_rsp_t obi_rsp_o, |
| 21 | + |
| 22 | + output logic mon_valid_o, |
| 23 | + output logic mon_we_o, |
| 24 | + output logic [ ObiCfg.AddrWidth-1:0] mon_addr_o, |
| 25 | + output logic [ ObiCfg.DataWidth-1:0] mon_wdata_o, |
| 26 | + output logic [ObiCfg.DataWidth/8-1:0] mon_be_o, |
| 27 | + output logic [ ObiCfg.IdWidth-1:0] mon_id_o |
| 28 | +); |
| 29 | + if (ObiCfg.OptionalCfg.UseAtop) $error("Please use an ATOP resolver before sim mem."); |
| 30 | + if (ObiCfg.Integrity) $error("Integrity not supported"); |
| 31 | + if (ObiCfg.OptionalCfg.UseProt) $warning("Prot not checked!"); |
| 32 | + if (ObiCfg.OptionalCfg.UseMemtype) $warning("Memtype not checked!"); |
| 33 | + |
| 34 | + typedef logic [ObiCfg.AddrWidth-1:0] addr_t; |
| 35 | + |
| 36 | + obi_r_chan_t rsp_queue[$]; |
| 37 | + |
| 38 | + logic [7:0] mem[addr_t]; |
| 39 | + logic rsp_ready; |
| 40 | + |
| 41 | + if (ObiCfg.UseRReady) begin : gen_rready |
| 42 | + assign rsp_ready = obi_req_i.rready; |
| 43 | + end else begin : gen_no_rready |
| 44 | + assign rsp_ready = 1'b1; |
| 45 | + end |
| 46 | + |
| 47 | + logic mon_valid; |
| 48 | + logic mon_we; |
| 49 | + logic [ObiCfg.AddrWidth-1:0] mon_addr; |
| 50 | + logic [ObiCfg.DataWidth-1:0] mon_wdata; |
| 51 | + logic [ObiCfg.DataWidth/8-1:0] mon_be; |
| 52 | + logic [ObiCfg.IdWidth-1:0] mon_id; |
| 53 | + |
| 54 | + assign mon_we = obi_req_i.a.we; |
| 55 | + assign mon_addr = obi_req_i.a.addr; |
| 56 | + assign mon_wdata = obi_req_i.a.wdata; |
| 57 | + assign mon_be = obi_req_i.a.be; |
| 58 | + assign mon_id = obi_req_i.a.aid; |
| 59 | + |
| 60 | + initial begin |
| 61 | + fork |
| 62 | + // Request Handling |
| 63 | + forever begin |
| 64 | + // Start cycle |
| 65 | + @(posedge clk_i); |
| 66 | + #(ApplDelay); |
| 67 | + // Indicate ready |
| 68 | + obi_rsp_o.gnt = 1'b1; |
| 69 | + mon_valid = 1'b0; |
| 70 | + // End of cycle |
| 71 | + #(AcqDelay-ApplDelay); |
| 72 | + // If requesting |
| 73 | + if (obi_req_i.req) begin |
| 74 | + mon_valid = 1'b1; |
| 75 | + if (obi_req_i.a.we) begin |
| 76 | + automatic obi_r_chan_t write_rsp; |
| 77 | + // write memory |
| 78 | + for (int i = 0; i < ObiCfg.DataWidth/8; i++) begin |
| 79 | + if (obi_req_i.a.be[i]) begin |
| 80 | + mem[obi_req_i.a.addr+i] = obi_req_i.a.wdata[8*i+:8]; |
| 81 | + end |
| 82 | + end |
| 83 | + // write response |
| 84 | + write_rsp = 'x; |
| 85 | + write_rsp.rid = obi_req_i.a.aid; |
| 86 | + write_rsp.err = 1'b0; |
| 87 | + write_rsp.r_optional = '0; |
| 88 | + rsp_queue.push_back(write_rsp); |
| 89 | + end else begin |
| 90 | + // read response |
| 91 | + automatic obi_r_chan_t read_rsp = 'x; |
| 92 | + if (!mem.exists(obi_req_i.a.addr)) begin |
| 93 | + if (WarnUninitialized) begin |
| 94 | + $warning("%t - Access to non-initialized address at 0x%016x by ID 0x%x.", |
| 95 | + $realtime, obi_req_i.a.addr, obi_req_i.a.aid); |
| 96 | + end |
| 97 | + end else begin |
| 98 | + for (int i = 0; i < ObiCfg.DataWidth/8; i++) begin |
| 99 | + read_rsp.rdata[8*i+:8] = mem[obi_req_i.a.addr+i]; |
| 100 | + end |
| 101 | + end |
| 102 | + read_rsp.rid = obi_req_i.a.aid; |
| 103 | + read_rsp.err = 1'b0; |
| 104 | + read_rsp.r_optional = '0; |
| 105 | + rsp_queue.push_back(read_rsp); |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + // Response Handling |
| 110 | + forever begin |
| 111 | + // Start cycle |
| 112 | + @(posedge clk_i); |
| 113 | + #(ApplDelay); |
| 114 | + obi_rsp_o.rvalid = 1'b0; |
| 115 | + if (rsp_queue.size() != 0) begin |
| 116 | + obi_rsp_o.r = rsp_queue[0]; |
| 117 | + obi_rsp_o.rvalid = 1'b1; |
| 118 | + // End of cycle |
| 119 | + #(AcqDelay-ApplDelay); |
| 120 | + if (rsp_ready) begin |
| 121 | + void'(rsp_queue.pop_front()); |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + join |
| 126 | + end |
| 127 | + |
| 128 | + initial begin |
| 129 | + mon_valid_o = '0; |
| 130 | + mon_we_o = '0; |
| 131 | + mon_addr_o = '0; |
| 132 | + mon_wdata_o = '0; |
| 133 | + mon_be_o = '0; |
| 134 | + mon_id_o = '0; |
| 135 | + wait (rst_ni); |
| 136 | + forever begin |
| 137 | + @(posedge clk_i); |
| 138 | + mon_valid_o <= #(ApplDelay) mon_valid; |
| 139 | + mon_we_o <= #(ApplDelay) mon_we; |
| 140 | + mon_addr_o <= #(ApplDelay) mon_addr; |
| 141 | + mon_wdata_o <= #(ApplDelay) mon_wdata; |
| 142 | + mon_be_o <= #(ApplDelay) mon_be; |
| 143 | + mon_id_o <= #(ApplDelay) mon_id; |
| 144 | + end |
| 145 | + end |
| 146 | +endmodule |
| 147 | + |
| 148 | +`include "obi/typedef.svh" |
| 149 | +`include "obi/assign.svh" |
| 150 | + |
| 151 | +module obi_sim_mem_intf import obi_pkg::*; #( |
| 152 | + parameter obi_pkg::obi_cfg_t ObiCfg = obi_pkg::ObiDefaultConfig, |
| 153 | + parameter bit WarnUninitialized = 1'b0, |
| 154 | + parameter bit ClearErrOnAccess = 1'b0, |
| 155 | + parameter time ApplDelay = 0ps, |
| 156 | + parameter time AcqDelay = 0ps |
| 157 | +) ( |
| 158 | + input logic clk_i, |
| 159 | + input logic rst_ni, |
| 160 | + OBI_BUS.Subordinate obi_sbr, |
| 161 | + |
| 162 | + output logic mon_valid_o, |
| 163 | + output logic mon_we_o, |
| 164 | + output logic [ ObiCfg.AddrWidth-1:0] mon_addr_o, |
| 165 | + output logic [ ObiCfg.DataWidth-1:0] mon_wdata_o, |
| 166 | + output logic [ObiCfg.DataWidth/8-1:0] mon_be_o, |
| 167 | + output logic [ ObiCfg.IdWidth-1:0] mon_id_o |
| 168 | +); |
| 169 | + |
| 170 | + `OBI_TYPEDEF_ALL(obi, ObiCfg) |
| 171 | + |
| 172 | + obi_req_t obi_req; |
| 173 | + obi_rsp_t obi_rsp; |
| 174 | + |
| 175 | + `OBI_ASSIGN_TO_REQ(obi_req, obi_sbr, ObiCfg) |
| 176 | + `OBI_ASSIGN_FROM_RSP(obi_sbr, obi_rsp, ObiCfg) |
| 177 | + |
| 178 | + obi_sim_mem #( |
| 179 | + .ObiCfg (ObiCfg), |
| 180 | + .obi_req_t (obi_req_t), |
| 181 | + .obi_rsp_t (obi_rsp_t), |
| 182 | + .obi_r_chan_t (obi_r_chan_t), |
| 183 | + .WarnUninitialized(WarnUninitialized), |
| 184 | + .ClearErrOnAccess (ClearErrOnAccess), |
| 185 | + .ApplDelay (ApplDelay), |
| 186 | + .AcqDelay (AcqDelay) |
| 187 | + ) i_obi_sim_mem ( |
| 188 | + .clk_i, |
| 189 | + .rst_ni, |
| 190 | + .obi_req_i(obi_req), |
| 191 | + .obi_rsp_o(obi_rsp), |
| 192 | + |
| 193 | + .mon_valid_o, |
| 194 | + .mon_we_o, |
| 195 | + .mon_addr_o, |
| 196 | + .mon_wdata_o, |
| 197 | + .mon_be_o, |
| 198 | + .mon_id_o |
| 199 | + ); |
| 200 | + |
| 201 | +endmodule |
0 commit comments