MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

rggen:把寄存器手册变成可编译的代码

项目背景与痛点

在芯片和 FPGA 项目里,寄存器手册(Register Map)往往是最先冻结、也最容易失控的一份文档。硬件工程师照着 Excel 表格手写 RTL 的读写逻辑,软件工程师照着同一份表格手写 C 头文件,验证工程师再照着手册敲一遍 UVM RAL 模型。三份代码、一份真相,任何一次位域调整都要在三处同步修改——漏改一处,就是流片后软件写不进去、仿真对不上、调试靠猜的经典事故。

rggen/rggen 要解决的就是这个"同一份规格、N 份实现"的割裂问题。它的定位很清晰:用一个统一的寄存器对象模型作为中间表示,前端吃表格、SystemRDL 或 Ruby DSL,后端吐出 RTL、C 头文件、UVM 模型和 Markdown 文档。你改一处规格,所有产物一次性重新生成,人手不再介入。

这不是一个新概念,IP-XACT、SystemRDL 生态里早有类似尝试,但那些方案普遍绑定在昂贵的商业工具链上。rggen 的价值在于:开源、Ruby Gem 化、命令行直接跑,能塞进 CI 流水线。

核心技术揭秘

rggen 的架构是典型的输入插件 → 中间模型 → 输出插件三段式。核心 gem rggen-core 并不关心你从哪里读规格,也不关心你要生成什么语言,它只负责定义 DSL、构建并校验寄存器模型,剩下的都交给插件。

graph TD A[Excel / ODS 规格表] --> P1[rggen-spreadsheet 输入插件] B[SystemRDL 源文件] --> P2[rggen-systemrdl 输入插件] C[Ruby DSL 脚本] --> P3[rggen-core DSL 解析器] P1 --> M[寄存器对象模型 RegisterMap - Register - BitField] P2 --> M P3 --> M M --> O1[rggen-verilog 输出插件] M --> O2[rggen-c-header 输出插件] M --> O3[rggen-uvm 输出插件] M --> O4[rggen-markdown 输出插件] O1 --> R1[RTL:寄存器阵列与总线适配器] O2 --> R2[C 头文件:偏移量与位域宏] O3 --> R3[UVM RAL 模型] O4 --> R4[可评审的寄存器手册]

这个设计的精妙之处在于模型层的抽象粒度。BitField 不只记录"第 7:4 位叫什么名字",它还携带硬件访问属性(rw/ro/w1c/rc/rs)、软件访问属性、初始值、是否有存储元件、是否触发中断、是否需要写保护等语义。正因为语义完整,输出插件才有足够信息做真正的代码合成,而不是字符串替换式的模板填充。

以 Verilog 后端为例,生成的 RTL 不是一坨扁平逻辑,而是分层实例化:顶层是总线适配器(AXI4-Lite、APB、Wishbone、Avalon 等可切换),中间是 rggen_register 构成寄存器阵列,底层是 rggen_bit_field 承担每一位的实际行为——w1c 的清零逻辑、rc 的读后自清、跨时钟域的握手,全部封装在库模块里。这种"生成结构、复用原语"的策略,避免了生成代码冗长难读的通病,也让时序分析能落到具体模块上。

技术难点集中在两个地方:一是规格表达能力与实现复杂度的权衡,SystemRDL 是个庞大的标准,rggen 只实现了其中一个可用子集,凡是子集之外的结构都会显式报错而不是静默忽略;二是总线协议的适配抽象,要让同一份寄存器模型在不同总线宽度、不同字节使能语义下行为一致,适配层必须正确处理部分写、非对齐访问和读回延迟。

功能亮点与差异

第一,多输入、多输出的正交组合。你可以今天用 Excel 起步、明天迁到 SystemRDL,或者反过来把已有 SystemRDL 导出成表格给软件团队评审,模型层不变,迁移成本极低。这一点在同类开源工具里很少见。

第二,RTL 生成是可综合的真实代码,而不是仅供仿真。生成的适配器支持配置总线位宽与寄存器位宽的差异,能自动处理窄总线访问宽寄存器的情况。

第三,UVM RAL 与 RTL 同源生成。验证环境的镜像值和期望值来自与硬件完全相同的模型定义,从根本上消除了"寄存器模型和后端实现不一致"的验证盲区。

第四,生成物零第三方依赖。除总线适配器外,生成的 Verilog 不依赖任何外部 IP 或宏库,方便直接并入既有工程。

应用场景与落地建议

最适合的场景是中大型 SoC 的寄存器子系统、FPGA 加速卡的控制面、以及需要软硬件协同交付的 IP 核。寄存器数量超过几十个、软件团队和硬件团队异地协作时,收益最明显。

落地时有几点必须注意:

  • 把规格文件纳入版本控制,并把生成过程放进 CI。每次提交触发重新生成,用 git diff 检查产物变化,任何未经评审的寄存器变更都会被拦下。
  • 严禁手工修改生成产物。一旦有人为了赶进度直接改 RTL,下次重新生成就会覆掉,这类"影子修改"是最常见的翻车原因。
  • 提前核对 SystemRDL 子集覆盖范围。复杂结构体、多维数组、动态地址映射这些高级特性可能不被支持,规格设计阶段就要确认。
  • 关注生成 RTL 的面积与时序。每个位域一个原语实例,寄存器量大时门数会上去,综合后需要抽查关键路径,必要时调整原语实现。
  • 锁定工具版本。插件版本漂移会改变生成结果的代码结构,在发布分支上应固定 Gem 版本。

综合评价

优点:设计哲学正确——单一事实来源、插件化解耦、产物全覆盖;生成的 RTL 可综合、结构清晰;命令行友好,天然适合 CI;开源免费,无商业工具链绑定。

缺点:Ruby 技术栈在硬件团队里普及度低,二次开发插件需要额外学习成本;SystemRDL 支持是子集而非全集,遇到标准之外的写法只能绕路;文档以英文为主且示例分散在各插件仓库里,新手上手曲线偏陡;社区规模小,遇到冷门问题基本只能靠读源码。

一句话结论:如果你的项目正被"三份寄存器实现互相对不上"折磨,rggen 是目前开源方案里投入产出比最高的一档;如果你只需要生成一个几十行的 C 头文件,那它属于杀鸡用牛刀。

关于

Code generation tool for control and status registers

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
589
价值分
293
活跃状态
活跃

语言占比

Ruby

Release

README

RgGen

Gem Version Docker Pulls Homebrew Formula Downloads

CI Maintainability codecov Discord

ko-fi

RgGen

RgGen is a code generation tool for ASIC/IP/FPGA/RTL engineers. It will automatically generate source code related to control and status registers (CSR), e.g. SytemVerilog RTL, UVM register model (UVM RAL/uvm_reg), C header file, Wiki documents, from human readable register map specifications.

RgGen has following features:

  • Generate source files related to CSR from register map specifications
    • RTL module
    • UVM register model (UVM RAL/uvm_reg)
    • C header file
    • Register map documents written in Markdown
  • Register map specifications can be written in human readable format
    • Ruby with APIs to describe register map information
    • YAML
    • JSON
    • TOML
    • Spreadsheet (XLSX, ODS, CSV)
    • SystemRDL
  • Plugin feature
    • Allow you to customize RgGen for your environment
      • Add your own special bit field types
      • Add your own host bus protocol
  • Browser-based Web UI is available at https://rggen.github.io/rggen-webui/

Installation

Ruby

RgGen is written in the Ruby programing language and its required version is 3.2 or later. You need to install any of these versions of Ruby before installing RgGen tool. To install Ruby, see this page.

Installation Command

RgGen depends on following sub components and other Ruby libraries.

To install RgGen and the dependencies, use the command below:

$ gem install rggen

RgGen and dependencies will be installed on your system root.

If you want to install them on other location, you need to specify install path and set GEM_PATH and PATH environment variables:

$ gem install --install-dir /path/to/your/install/directory rggen
$ export GEM_PATH=/path/to/your/install/directory
$ export PATH=$GEM_PATH/bin:$PATH

You would get the following error message duaring installation if you have the old RgGen (version < 0.9).

ERROR:  Error installing rggen:
        "rggen" from rggen-core conflicts with installed executable from rggen

To resolve the above error, there are three solutions. See this page

Docker Image

The rggen-docker is a Docker image to simplify installation and use of RgGen. You can also execute RgGen by using this image:

$ docker run -ti --rm -v ${PWD}:/work --user $(id -u):$(id -g) rggendev/rggen-docker:latest -c config.yml -o out block_0.yml

See the rggen-docker repository for further details.

Homebrew Installation

On macOS or Linux, if Homebrew is installed, you can install RgGen with this command:

brew install rggen

This will automatically install Ruby if needed, and will provide RgGen itself as well as the VHDL, Verilog, and Veryl plugins.

Web UI

A browser-based interface is available at https://rggen.github.io/rggen-webui/. No installation required — RgGen runs entirely in the browser.

  • Define register blocks, registers, and bit fields in an interactive table UI
  • Generate RTL (SystemVerilog, Verilog, Veryl, VHDL), UVM RAL (uvm_reg), C header, and Markdown documents
  • Download all outputs as a ZIP archive
  • Export and import register map definitions as YAML files

Usage

See Wiki documents.

Plugin

RgGen has plugin feature to allow your cusomization. See this Wiki document for futher detals.

Supported Tools

Following EDA tools can accept the generated source files.

  • Simulation tools
    • Synopsys VCS
    • Cadence Xcelium
    • Altair DSim
    • AMD Vivado Simulator
    • Verilator
      • Need -Wno-unoptflat switch for Verilog RTL
    • Icarus Verilog
      • Verilog RTL only
  • Synthesis tools
    • Synopsys Design Compiler
    • Altera Quartus
    • AMD Vivado
    • Yosys
      • Verilog RTL

Example

You can get sample configuration file and register map specification from the rggen-sample repository. This register map specification is for a UART IP.

You can try to use RgGen by uisng these example files. Hit command below:

$ rggen -c config.yml -o out uart_csr.yml
  • -c: Specify path to your configuration file
  • -o: Specify path to the directory where generated files will be written to

Then, generated files will be written to the out directory.

If you want to generate Verilog RTL, Veryl RTL and VHDL RTL then you need to instll optional plugins listed below.

$ gem install rggen-verilog
$ gem install rggen-veryl
$ gem install rggen-vhdl

In addition, you need to tell RgGen to use these plugins by using the --plugin option switch:

$ rggen -c config.yml --plugin rggen-verilog --plugin rggen-veryl --plugin rggen-vhdl uart_csr.yml

RgGen will generate following source files from the uart_csr.yml register map specification:

Contributing

See Contributing Guide.

Publications

  • Taichi Ishitani. RgGen: An Open-Source CSR Automation Tool. September 2026. In Design & Verification Conference & Exhibition (DVCon) Japan 2026.

Contact

Feedbacks, bug reports, questions and etc. are wellcome! You can post them by using following ways:

See Also

Copyright © 2019-2026 Taichi Ishitani. RgGen is licensed under the MIT License, see LICENSE for futher detils.

Code of Conduct

Everyone interacting in the RgGen project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

评论

暂无评论