|
21 | 21 |
|
22 | 22 | from __future__ import annotations |
23 | 23 |
|
| 24 | +import json |
24 | 25 | import re |
25 | 26 | from pathlib import Path |
26 | 27 |
|
@@ -194,10 +195,177 @@ def scan_text(text: str, source_file: str = "") -> list[schema.Memory]: |
194 | 195 | return out |
195 | 196 |
|
196 | 197 |
|
| 198 | +def parse_param_int(val) -> int: |
| 199 | + """Parse a integer parameter from Yosys JSON (handles ints and binary strings).""" |
| 200 | + if isinstance(val, int): |
| 201 | + return val |
| 202 | + if isinstance(val, str): |
| 203 | + if val.startswith("0x"): |
| 204 | + return int(val, 16) |
| 205 | + try: |
| 206 | + return int(val, 2) |
| 207 | + except ValueError: |
| 208 | + return int(val) |
| 209 | + return int(val) |
| 210 | + |
| 211 | + |
| 212 | +def scan_yosys_json(data: dict | str | Path) -> list[schema.Memory]: |
| 213 | + """Extract memory modules from a Yosys netlist JSON output ($mem_v2 cells).""" |
| 214 | + if isinstance(data, (str, Path)): |
| 215 | + p = Path(data) |
| 216 | + if not p.is_file(): |
| 217 | + return [] |
| 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 = [ |
| 248 | + c for c in rd_clks + wr_clks if str(c) not in ("0", "1", "x", "z") |
| 249 | + ] |
| 250 | + single_clock = len(set(all_clk_bits)) <= 1 |
| 251 | + |
| 252 | + # Write enable mask lanes |
| 253 | + wr_en_bits = conn.get("WR_EN", []) |
| 254 | + mask_lanes = 0 |
| 255 | + if wr_ports > 0 and len(wr_en_bits) > wr_ports: |
| 256 | + mask_lanes = len(wr_en_bits) // wr_ports |
| 257 | + |
| 258 | + pins: list[schema.Pin] = [] |
| 259 | + |
| 260 | + for i in range(rd_ports): |
| 261 | + port_id = f"R{i}" |
| 262 | + pins.extend( |
| 263 | + [ |
| 264 | + schema.Pin( |
| 265 | + name=f"{port_id}_clk", |
| 266 | + direction="input", |
| 267 | + width=1, |
| 268 | + port_id=port_id, |
| 269 | + function="clk", |
| 270 | + ), |
| 271 | + schema.Pin( |
| 272 | + name=f"{port_id}_addr", |
| 273 | + direction="input", |
| 274 | + width=addr_w, |
| 275 | + port_id=port_id, |
| 276 | + function="addr", |
| 277 | + ), |
| 278 | + schema.Pin( |
| 279 | + name=f"{port_id}_en", |
| 280 | + direction="input", |
| 281 | + width=1, |
| 282 | + port_id=port_id, |
| 283 | + function="en", |
| 284 | + ), |
| 285 | + schema.Pin( |
| 286 | + name=f"{port_id}_data", |
| 287 | + direction="output", |
| 288 | + width=width, |
| 289 | + port_id=port_id, |
| 290 | + function="data_out", |
| 291 | + ), |
| 292 | + ] |
| 293 | + ) |
| 294 | + |
| 295 | + for i in range(wr_ports): |
| 296 | + port_id = f"W{i}" |
| 297 | + pins.extend( |
| 298 | + [ |
| 299 | + schema.Pin( |
| 300 | + name=f"{port_id}_clk", |
| 301 | + direction="input", |
| 302 | + width=1, |
| 303 | + port_id=port_id, |
| 304 | + function="clk", |
| 305 | + ), |
| 306 | + schema.Pin( |
| 307 | + name=f"{port_id}_addr", |
| 308 | + direction="input", |
| 309 | + width=addr_w, |
| 310 | + port_id=port_id, |
| 311 | + function="addr", |
| 312 | + ), |
| 313 | + schema.Pin( |
| 314 | + name=f"{port_id}_en", |
| 315 | + direction="input", |
| 316 | + width=1, |
| 317 | + port_id=port_id, |
| 318 | + function="en", |
| 319 | + ), |
| 320 | + schema.Pin( |
| 321 | + name=f"{port_id}_data", |
| 322 | + direction="input", |
| 323 | + width=width, |
| 324 | + port_id=port_id, |
| 325 | + function="data_in", |
| 326 | + ), |
| 327 | + ] |
| 328 | + ) |
| 329 | + if mask_lanes > 0: |
| 330 | + pins.append( |
| 331 | + schema.Pin( |
| 332 | + name=f"{port_id}_mask", |
| 333 | + direction="input", |
| 334 | + width=mask_lanes, |
| 335 | + port_id=port_id, |
| 336 | + function="mask", |
| 337 | + ) |
| 338 | + ) |
| 339 | + |
| 340 | + clean_cell = cell_name[1:] if cell_name.startswith("\\") else cell_name |
| 341 | + mem_name = clean_mod_name if len(cells) == 1 else clean_cell |
| 342 | + |
| 343 | + mem = schema.Memory( |
| 344 | + name=mem_name, |
| 345 | + rows=size, |
| 346 | + bits=width, |
| 347 | + addr_w=addr_w, |
| 348 | + read_ports=rd_ports, |
| 349 | + write_ports=wr_ports, |
| 350 | + rw_ports=0, |
| 351 | + mask_lanes=mask_lanes, |
| 352 | + pins=pins, |
| 353 | + behavioral_model={"module": clean_mod_name}, |
| 354 | + reason=f"Yosys inferred $mem_v2 ({'single-clock' if single_clock else 'multi-clock'})", |
| 355 | + ) |
| 356 | + out.append(mem) |
| 357 | + |
| 358 | + return out |
| 359 | + |
| 360 | + |
197 | 361 | def scan_files(paths: list[Path]) -> list[schema.Memory]: |
198 | | - """Scan Verilog files; later definitions of a name win.""" |
| 362 | + """Scan Verilog files or Yosys JSON files; later definitions of a name win.""" |
199 | 363 | found: dict[str, schema.Memory] = {} |
200 | 364 | for path in paths: |
201 | | - for memory in scan_text(path.read_text(errors="replace"), str(path)): |
202 | | - found[memory.name] = memory |
| 365 | + if path.suffix == ".json": |
| 366 | + for memory in scan_yosys_json(path): |
| 367 | + found[memory.name] = memory |
| 368 | + else: |
| 369 | + for memory in scan_text(path.read_text(errors="replace"), str(path)): |
| 370 | + found[memory.name] = memory |
203 | 371 | return list(found.values()) |
0 commit comments