@@ -194,10 +194,119 @@ def scan_text(text: str, source_file: str = "") -> list[schema.Memory]:
194194 return out
195195
196196
197+ def parse_param_int (val ) -> int :
198+ """Parse a integer parameter from Yosys JSON (handles ints and binary strings)."""
199+ if isinstance (val , int ):
200+ return val
201+ if isinstance (val , str ):
202+ if val .startswith ("0x" ):
203+ return int (val , 16 )
204+ try :
205+ return int (val , 2 )
206+ except ValueError :
207+ return int (val )
208+ return int (val )
209+
210+
211+ def scan_yosys_json (data : dict | str | Path ) -> list [schema .Memory ]:
212+ """Extract memory modules from a Yosys netlist JSON output ($mem_v2 cells)."""
213+ if isinstance (data , (str , Path )):
214+ p = Path (data )
215+ if not p .is_file ():
216+ return []
217+ import json
218+ data = json .loads (p .read_text ())
219+
220+ out : list [schema .Memory ] = []
221+ modules = data .get ("modules" , {})
222+
223+ for mod_name , mod_info in modules .items ():
224+ clean_mod_name = mod_name [1 :] if mod_name .startswith ("\\ " ) else mod_name
225+ cells = mod_info .get ("cells" , {})
226+ for cell_name , cell_info in cells .items ():
227+ cell_type = cell_info .get ("type" , "" )
228+ if cell_type not in ("$mem_v2" , "$mem" ):
229+ continue
230+
231+ params = cell_info .get ("parameters" , {})
232+ conn = cell_info .get ("connections" , {})
233+
234+ size = parse_param_int (params .get ("SIZE" , 0 ))
235+ width = parse_param_int (params .get ("WIDTH" , 0 ))
236+ rd_ports = parse_param_int (params .get ("RD_PORTS" , 0 ))
237+ wr_ports = parse_param_int (params .get ("WR_PORTS" , 0 ))
238+
239+ if size == 0 or width == 0 :
240+ continue
241+
242+ addr_w = (size - 1 ).bit_length () if size > 1 else 1
243+
244+ # Clock net tracking & single-clock verification across ports
245+ rd_clks = conn .get ("RD_CLK" , [])
246+ wr_clks = conn .get ("WR_CLK" , [])
247+ all_clk_bits = [c for c in rd_clks + wr_clks if str (c ) not in ("0" , "1" , "x" , "z" )]
248+ single_clock = (len (set (all_clk_bits )) <= 1 )
249+
250+ # Write enable mask lanes
251+ wr_en_bits = conn .get ("WR_EN" , [])
252+ mask_lanes = 0
253+ if wr_ports > 0 and len (wr_en_bits ) > wr_ports :
254+ mask_lanes = len (wr_en_bits ) // wr_ports
255+
256+ pins : list [schema .Pin ] = []
257+
258+ for i in range (rd_ports ):
259+ port_id = f"R{ i } "
260+ pins .extend ([
261+ schema .Pin (name = f"{ port_id } _clk" , direction = "input" , width = 1 , port_id = port_id , function = "clk" ),
262+ schema .Pin (name = f"{ port_id } _addr" , direction = "input" , width = addr_w , port_id = port_id , function = "addr" ),
263+ schema .Pin (name = f"{ port_id } _en" , direction = "input" , width = 1 , port_id = port_id , function = "en" ),
264+ schema .Pin (name = f"{ port_id } _data" , direction = "output" , width = width , port_id = port_id , function = "data_out" ),
265+ ])
266+
267+ for i in range (wr_ports ):
268+ port_id = f"W{ i } "
269+ pins .extend ([
270+ schema .Pin (name = f"{ port_id } _clk" , direction = "input" , width = 1 , port_id = port_id , function = "clk" ),
271+ schema .Pin (name = f"{ port_id } _addr" , direction = "input" , width = addr_w , port_id = port_id , function = "addr" ),
272+ schema .Pin (name = f"{ port_id } _en" , direction = "input" , width = 1 , port_id = port_id , function = "en" ),
273+ schema .Pin (name = f"{ port_id } _data" , direction = "input" , width = width , port_id = port_id , function = "data_in" ),
274+ ])
275+ if mask_lanes > 0 :
276+ pins .append (
277+ schema .Pin (name = f"{ port_id } _mask" , direction = "input" , width = mask_lanes , port_id = port_id , function = "mask" )
278+ )
279+
280+ clean_cell = cell_name [1 :] if cell_name .startswith ("\\ " ) else cell_name
281+ mem_name = clean_mod_name if len (cells ) == 1 else clean_cell
282+
283+ mem = schema .Memory (
284+ name = mem_name ,
285+ rows = size ,
286+ bits = width ,
287+ addr_w = addr_w ,
288+ read_ports = rd_ports ,
289+ write_ports = wr_ports ,
290+ rw_ports = 0 ,
291+ mask_lanes = mask_lanes ,
292+ pins = pins ,
293+ behavioral_model = {"module" : clean_mod_name },
294+ reason = f"Yosys inferred $mem_v2 ({ 'single-clock' if single_clock else 'multi-clock' } )" ,
295+ )
296+ out .append (mem )
297+
298+ return out
299+
300+
197301def scan_files (paths : list [Path ]) -> list [schema .Memory ]:
198- """Scan Verilog files; later definitions of a name win."""
302+ """Scan Verilog files or Yosys JSON files ; later definitions of a name win."""
199303 found : dict [str , schema .Memory ] = {}
200304 for path in paths :
201- for memory in scan_text (path .read_text (errors = "replace" ), str (path )):
202- found [memory .name ] = memory
305+ if path .suffix == ".json" :
306+ for memory in scan_yosys_json (path ):
307+ found [memory .name ] = memory
308+ else :
309+ for memory in scan_text (path .read_text (errors = "replace" ), str (path )):
310+ found [memory .name ] = memory
203311 return list (found .values ())
312+
0 commit comments