Implementing JSON encoder and decoder in pure SystemVerilog

Posted on 07 Jul 2024 in Verification • 2 min read

Why JSON? Because it's simple enough and well-formalized, making it easy to load and dump. It's supported everywhere, and it's still human-readable. Overall, it's the perfect "the great equalizer" if we want to pass data, configurations, metrics, traces, transactions, and other stuff between simulation and the outside world through IO.

Overview

So here it is on GitHub. And documention is on GitHub Pages.

No UVM, DPI, or external dependencies - just pure SV. It has almost full support for the specification (I didn't support Unicode, sorry about that). And there's even some SV-specific features.

You can read JSON, dump it, inspect, modify, and manually create an object tree. You can even implement a special interface class in any of your own classes and gain the ability to dump that class into JSON. There is a transparent mechanism for signaling decoding errors. In short, everything you'd expect from normal languages.

Development and testing

I used SVUnit and Verilator. Tests include both my own and those using the external JSONTestSuite set.

Working with Verilator is not really easy - you often end up debugging Verilator itself instead of your logic. As a result, a dozen bugs were filed for Verilator (btw, most of them fixed), and its compatibility with SVUnit was even improved. In the end, almost 20% of the changelog for version 5.024 was reported by me. "It ain't much, but it's honest work.jpg" Anyway, let that sink in - Verilator already works decently with fairly abstract OOP code! So, regardless of everything, the positive side effect of slightly improving the open-source tools already justifies the whole idea 100%.

Documentation

I really like AsciiDoc. From the very beginning, I looked for ways to set up GitHub Pages with it. Fortunately, I found the Antora project, which seems to be one of a kind. All other site and documentation generators either don't support AsciiDoc or only do so with extra plugins and workarounds. However, Antora's default UI has some drawbacks (for example, no SV syntax highlighting in the documentation), but everything is fixable.

Automation

Everything mentioned above has been "put on paper" using GitHub Actions, and it just "goes brrr" on new PRs and pushes to the master branch. By the way, besides Verilator, I also set up ModelSim (the version from Altera) - this is the only commercial simulator you can download via a direct link and run without a separate license (though, I don't really know how legal that action is).

In the end, with just a bit more work, the infrastructure will be almost on par with what's available out-of-the-box in major programming languages, with no difficulties.

Where to start?

Documenation homepage is a perfect place.