Skip to main content

SystemVerilog packed struct/enum type definitions to JSON/HTML refbook generator

Project description

sv-ref

SystemVerilog packed struct/enum type definitions to JSON/HTML refbook generator.

Parses SystemVerilog source using pyslang and outputs bit-level field information for waveform debugging. The generated HTML viewer lets you paste hex values and instantly decode them into named struct fields and enum values.

Installation

Requires Python 3.11+.

# Clone and install
git clone https://github.com/HTsulfuric/sv-ref.git
cd sv-ref
uv sync

Usage

Generate

sv-ref generate <files...> [options]
Option Description
files One or more SystemVerilog source files (supports glob patterns)
-I, --include-dir Include directories (auto-discovers *.sv files)
-o, --output-dir Output directory (default: .)
-f, --filelist Filelist (.f) files to parse (repeatable)
-r, --recursive Recursively scan include directories
--json-only Only generate JSON output (skip HTML)
--html-only Only generate HTML output (skip JSON)
--version Show version and exit

Decode

Decode a hex value in the terminal without opening a browser:

sv-ref decode <refbook.json> <type_name> <hex_value>

Example:

$ sv-ref decode refbook.json packet_t ABCD
packet_t [16 bits] = 0xABCD
------------------------------------------------------------
Name                 Bits         Hex          Decoded
------------------------------------------------------------
header               [15:8]       0xAB         171
status               [7:6]        0x02         ERR
payload              [5:0]        0x0D         13

Filelist Support

Use .f files to specify source files and include directories:

# sources.f
rtl/types.sv
rtl/pkg.sv

# Include directories
+incdir+rtl/includes
+incdir+lib/common
sv-ref generate -f sources.f -o out/

Example

Given a SystemVerilog file types.sv:

package test_pkg;
    typedef enum logic [1:0] { IDLE=0, BUSY=1, ERR=2 } state_e;
    typedef struct packed {
        logic [7:0] header;
        state_e status;
        logic [5:0] payload;
    } packet_t;
endpackage

Run:

sv-ref generate types.sv -o out/

This produces two files in out/:

  • refbook.json -- machine-readable type data with bit widths and offsets
  • index.html -- self-contained HTML viewer with hex decoder

JSON Output

The generated refbook.json contains all parsed types with field-level detail:

{
  "meta": {
    "version": "0.1.0",
    "generated_at": "2026-02-07T00:00:00+00:00",
    "source_files": ["types.sv"]
  },
  "types": [
    {
      "name": "packet_t",
      "kind": "struct",
      "total_width": 16,
      "package": "test_pkg",
      "fields": [
        {
          "name": "header",
          "width": 8,
          "offset": 8,
          "field_type": { "name": "logic[7:0]", "kind": null, "signed": false }
        },
        {
          "name": "status",
          "width": 2,
          "offset": 6,
          "field_type": { "name": "state_e", "kind": "enum", "signed": false },
          "enum_members": [
            { "name": "IDLE", "value": 0 },
            { "name": "BUSY", "value": 1 },
            { "name": "ERR", "value": 2 }
          ]
        },
        {
          "name": "payload",
          "width": 6,
          "offset": 0,
          "field_type": { "name": "logic[5:0]", "kind": null, "signed": false }
        }
      ]
    }
  ]
}

HTML Viewer

The generated index.html is a self-contained single-page app (no external dependencies). Open it in any browser to:

  • Browse all parsed types in a sidebar with package grouping
  • Search types by name or package (/ to focus)
  • Paste a hex value to decode it into individual struct fields
  • See enum member names resolved automatically
  • View nested struct fields recursively
  • Click any hex or decoded value to copy to clipboard
  • Share decoded state via URL hash (#type=packet_t&hex=ABCD)
  • Toggle light/dark theme (persisted to localStorage)

Keyboard Shortcuts

Key Action
/ Focus search input
Escape Clear search, blur input
Arrow Up/Down Navigate type list
Enter Focus hex input for selected type

Supported Types

  • Packed structs (typedef struct packed)
  • Enums (typedef enum)
  • Nested packed structs
  • Signed/unsigned fields
  • Parameterized types (resolved at elaboration)
  • Multiple packages per file
  • Wide types (>53 bits) via BigInt

Types must be defined inside a package block.

Development

# Run tests
uv run pytest tests/ -v

# Lint
uv run ruff check sv_ref/ tests/

# Format
uv run ruff format sv_ref/ tests/

# Generate demo output
make demo

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sv_ref-0.1.2.tar.gz (67.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sv_ref-0.1.2-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file sv_ref-0.1.2.tar.gz.

File metadata

  • Download URL: sv_ref-0.1.2.tar.gz
  • Upload date:
  • Size: 67.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for sv_ref-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3325b779617ceef154c27a40543cf123fd8a98899c5a18e815831cb1d6ac5c60
MD5 95cb865b6f14b557ead8f00df340a363
BLAKE2b-256 b24ee8dfb21180e83026f85e333435403e26dc7fcffbedac065c678b3d6d38c5

See more details on using hashes here.

File details

Details for the file sv_ref-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: sv_ref-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for sv_ref-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77d14d73e651173a33bf7c5024fb948a7c198b92fecfa0aa4fb9bf37c58ca144
MD5 e4b980ac0a99f5e23c7fb83925ced2f4
BLAKE2b-256 78530d7bf05e13e67eace282656909ee08a802fd186db42798e091df1ddcd5db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page