verilog-basics-for-lowlevel

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Verilog Basics for Low-Level Engineers

面向底层工程师的 Verilog 基础

Purpose

目的

Give firmware and kernel engineers enough Verilog/SystemVerilog literacy to read RTL: modules, clocks/resets, combinational vs sequential logic, bus interfaces, and why hardware behavior explains driver bugs — not a replacement for HDL design courses.
让 firmware 和 kernel 工程师具备足够的 Verilog/SystemVerilog 素养,以便阅读 RTL:理解 modules、clocks/resets、组合逻辑与时序逻辑、bus interfaces,以及 hardware 行为为何能够解释 driver bugs;本 skill 不能替代 HDL design 课程。

When to Use

何时使用

  • Reference manual unclear — RTL clarifies register behavior
  • Understanding CDC (clock domain crossing) bugs
  • Correlating DMA/AXI transactions with driver ordering
  • Reviewing SoC block diagram with hardware team
  • Reference manual 含义不清——通过 RTL 厘清 register 行为
  • 理解 CDC(clock domain crossing)bugs
  • 将 DMA/AXI transactions 与 driver ordering 关联起来
  • 与 hardware 团队一起审查 SoC block diagram

Workflow

工作流程

1. Module structure

1. Module 结构

verilog
module uart_tx (
    input  wire       clk,
    input  wire       rst_n,   /* active-low async reset */
    input  wire       start,
    input  wire [7:0] data,
    output reg        busy
);
    /* sequential logic */
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n)
            busy <= 1'b0;
        else if (start)
            busy <= 1'b1;
        /* ... */
    end
endmodule
wire
= combinational/network;
reg
in
always
block = flip-flop unless combinational
always @(*)
.
verilog
module uart_tx (
    input  wire       clk,
    input  wire       rst_n,   /* active-low async reset */
    input  wire       start,
    input  wire [7:0] data,
    output reg        busy
);
    /* sequential logic */
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n)
            busy <= 1'b0;
        else if (start)
            busy <= 1'b1;
        /* ... */
    end
endmodule
wire
= 组合逻辑/网络;在
always
block 中使用
reg
= flip-flop,除非是组合逻辑
always @(*)
。

2. Synthesizable subset (what firmware folks need)

2. 可综合子集(firmware 工程师所需内容)

ConstructMeaning
posedge clk
Registered update
assign x = a & b
Combinational
case
/
if
in
always @(*)
Mux logic
Parameters
#(.WIDTH(32))
Configurable width
Avoid
#delay
in synthesizable RTL — simulation only.
构造含义
posedge clk
寄存器式更新
assign x = a & b
组合逻辑
always @(*)
中的
case
/
if
Mux 逻辑
参数
#(.WIDTH(32))
可配置位宽
避免在可综合 RTL 中使用
#delay
——它仅用于 simulation。

3. Reset discipline

3. Reset 规范

  • Async assert, sync deassert common (
    rst_sync_n
    )
  • Firmware must wait post-reset setup times — see RM reset chapter
  • Multiple reset domains → peripheral may need explicit soft reset bit
  • 异步 assert、同步 deassert 很常见(
    rst_sync_n
    )
  • Firmware 必须等待 reset 后的 setup times——参见 RM 的 reset 章节
  • 存在多个 reset domains → peripheral 可能需要显式的 soft reset bit

4. Bus protocols (reading SoC diagrams)

4. Bus protocols(阅读 SoC diagrams)

BusTypical use
APBSlow peripherals, simple reg interface
AHBHigher throughput on-chip
AXIDMA, modern SoCs — bursts, channels
Linux
regmap
MMIO maps to APB/AXI slave decode in RTL address map.
总线典型用途
APB慢速 peripherals、简单的 reg interface
AHB芯片内更高吞吐量
AXIDMA、现代 SoC——bursts、channels
在 Linux 中,
regmap
MMIO 会映射到 RTL address map 中的 APB/AXI slave decode。

5. Clock domains

5. Clock domains

Signals crossing
clk_a
→
clk_b
need synchronizers (2+ FFs). Metastability causes intermittent firmware bugs — not fixed in software alone.
从
clk_a
→
clk_b
跨越的 signals 需要 synchronizer(2 个或更多 FFs)。Metastability 会导致间歇性 firmware bugs——无法仅通过 software 修复。

6. Simulation vs silicon

6. Simulation 与 silicon

bash
undefined
bash
undefined

Typical open-source sim (conceptual)

Typical open-source sim (conceptual)

iverilog -o sim.vvp design.v tb.v vvp sim.vvp

QEMU/peripheral models may not match RTL edge cases.
iverilog -o sim.vvp design.v tb.v vvp sim.vvp

QEMU/peripheral models 可能无法与 RTL edge cases 完全匹配。

7. Agent usage

7. Agent 使用方式

/verilog-basics-for-lowlevel Explain this APB register block and when STATUS bit updates relative to WRITE
/verilog-basics-for-lowlevel Explain this APB register block and when STATUS bit updates relative to WRITE

Common Problems

常见问题

SymptomCauseFix
Bit toggles oncePulse in RTLPoll latch / clear-on-read in driver
Random corruptionCDCHardware synchronizer; don't hack delays
Read stale dataBus bridge bufferFollow RM ordering / barrier
IRQ stuckLevel vs pulse in RTLMatch handler ACK sequence
Verilog vs VHDLMixed SoC docsFocus on interface signals table
症状原因修复方法
Bit 只翻转一次RTL 中的 Pulse在 driver 中轮询 latch / 使用 clear-on-read
随机数据损坏CDC使用 hardware synchronizer;不要用 delay 变通
读取到 stale dataBus bridge buffer遵循 RM ordering / barrier
IRQ 卡住RTL 中为 level 而非 pulse匹配 handler 的 ACK sequence
Verilog 与 VHDL 混用混合的 SoC docs聚焦 interface signals 表

Related Skills

相关 Skills

  • skills/baremetal/mmio-and-bit-manipulation
    — register access from C
  • skills/baremetal/peripherals-from-datasheet
    — RM ↔ RTL
  • skills/baremetal/datasheet-and-refmanual-reading
    — doc navigation
  • skills/kernel-dev/device-tree
    — hardware integration in Linux
  • skills/qemu/protocol-analysis
    — validate bus timing
  • skills/baremetal/mmio-and-bit-manipulation
    ——从 C 访问 register
  • skills/baremetal/peripherals-from-datasheet
    ——RM ↔ RTL
  • skills/baremetal/datasheet-and-refmanual-reading
    ——文档导航
  • skills/kernel-dev/device-tree
    ——Linux 中的 hardware integration
  • skills/qemu/protocol-analysis
    ——验证 bus timing