1+ // Copyright 2021 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+ module tc_sram_simwrapper # (
6+ parameter int unsigned NumWords = 32'd1024 , // Number of Words in data array
7+ parameter int unsigned DataWidth = 32'd128 , // Data signal width
8+ parameter int unsigned ByteWidth = 32'd8 , // Width of a data byte
9+ parameter int unsigned NumPorts = 32'd2 , // Number of read and write ports
10+ parameter int unsigned Latency = 32'd1 , // Latency when the read data is available
11+ parameter SimInit = " none" , // Simulation initialization
12+ parameter bit PrintSimCfg = 1'b0 , // Print configuration
13+ parameter ImplKey = " none" , // Reference to specific implementation
14+ // DEPENDENT PARAMETERS, DO NOT OVERWRITE!
15+ parameter int unsigned AddrWidth = (NumWords > 32'd1 ) ? $clog2(NumWords) : 32'd1 ,
16+ parameter int unsigned BeWidth = (DataWidth + ByteWidth - 32'd1 ) / ByteWidth, // ceil_div
17+ parameter type addr_t = logic [AddrWidth- 1 : 0 ],
18+ parameter type data_t = logic [DataWidth- 1 : 0 ],
19+ parameter type be_t = logic [BeWidth- 1 : 0 ]
20+ ) (
21+ input logic clk_i, // Clock
22+ input logic rst_ni, // Asynchronous reset active low
23+ // input ports
24+ input logic [NumPorts- 1 : 0 ] req_i, // request
25+ input logic [NumPorts- 1 : 0 ] we_i, // write enable
26+ input addr_t [NumPorts- 1 : 0 ] addr_i, // request address
27+ input data_t [NumPorts- 1 : 0 ] wdata_i, // write data
28+ input be_t [NumPorts- 1 : 0 ] be_i, // write byte enable
29+ // output ports
30+ output data_t [NumPorts- 1 : 0 ] rdata_o // read data
31+ );
32+
33+
34+ tc_sram # (
35+ .DataWidth (DataWidth ),
36+ .NumWords (NumWords ),
37+ .NumPorts (NumPorts ),
38+ .SimInit (SimInit )
39+ ) i_sram (
40+ .clk_i (clk_i ),
41+ .rst_ni (rst_ni ),
42+ .req_i (req_i ),
43+ .we_i (we_i ),
44+ .addr_i (addr_i ),
45+ .wdata_i (wdata_i ),
46+ .be_i (be_i ),
47+ .rdata_o (rdata_o )
48+ );
49+
50+
51+ /* *
52+ * Memory loader for simulation
53+ *
54+ * Include this file in a memory primitive to load a memory array from
55+ * simulation.
56+ *
57+ * Requirements:
58+ * - A memory array named `sram`.
59+ * - A parameter `DataWidth` giving the memory width (word size) in bit.
60+ * - A parameter `NumWords` giving the memory depth in words.
61+ */
62+
63+ `ifndef SYNTHESIS
64+ // Task for loading 'sram' with SystemVerilog system task $readmemh()
65+ export " DPI-C" task simutil_memload ;
66+
67+ task simutil_memload ;
68+ input string file;
69+ $readmemh (file, i_sram.sram);
70+ endtask
71+
72+ // Function for setting a specific element in |sram|
73+ // Returns 1 (true) for success, 0 (false) for errors.
74+ export " DPI-C" function simutil_set_mem ;
75+
76+ function int simutil_set_mem (input int index, input bit [1023 : 0 ] val);
77+
78+ // Function will only work for memories <= 1024 bits
79+ if (DataWidth > 1024 ) begin
80+ return 0 ;
81+ end
82+
83+ if (index >= NumWords) begin
84+ return 0 ;
85+ end
86+
87+ i_sram.sram[index] = val[DataWidth- 1 : 0 ];
88+ return 1 ;
89+ endfunction
90+
91+ // Function for getting a specific element in |sram|
92+ export " DPI-C" function simutil_get_mem ;
93+
94+ function int simutil_get_mem (input int index, output bit [1023 : 0 ] val);
95+
96+ // Function will only work for memories <= 1024 bits
97+ if (DataWidth > 1024 ) begin
98+ return 0 ;
99+ end
100+
101+ if (index >= NumWords) begin
102+ return 0 ;
103+ end
104+
105+ val = 0 ;
106+ val[DataWidth- 1 : 0 ] = i_sram.sram[index];
107+ return 1 ;
108+ endfunction
109+ `endif
110+
111+ endmodule
0 commit comments