VexHDL Procedural and Control Flow Guide
This document covers VexHDL's procedural execution blocks (processes) and control structures, detailing conditional branching, pattern matching, loops, and simulation-time concurrency.
1. Process Blocks
Hardware behavior is described inside process blocks. VexHDL categorizes processes based on their sensitivity list and timing characteristics.
1.1 Combinational Process (comb)
- Re-evaluated by the simulator when a read signal changes.
- Only blocking assignments (
=) are allowed inside this block. - Blocking assignments define evaluation order within a process; zero-time simulation may still require delta-cycle settling. Physical gates have propagation delay.
let comb is_zero: Bool;
comb {
if data_bus == 0 {
is_zero = true;
} else {
is_zero = false;
}
}1.2 Sequential Process (on)
- Triggered only on specified edges of clock or reset signals.
- Uses non-blocking assignments (
<-).
on(PosEdge(clk), PosEdge(rst)) {
if rst {
r_out <- 0;
} else {
r_out <- next_val;
}
}1.3 Latch Process (latch)
- Synthesizes transparent latches.
latch {
if gate_enable {
latch_out = d_in;
}
// Note: missing else branch infers memory/latch state.
}1.4 Simulation Initial Process (initial)
- Runs exactly once at time step 0 during simulation.
- Used to drive initial inputs or sequences in testbenches. Ignored during synthesis.
initial {
clk = false;
rst = true;
Sim.delay(10);
rst = false;
}2. Conditional Statements
2.1 if-else
Standard conditional structure:
if (sel == 2'b01) {
out = a;
} else if (sel == 2'b10) {
out = b;
} else {
out = c;
}2.2 case Statements (Normal, casez, casex)
VexHDL supports three variants of the case statement, supporting don't-care comparison matching:
case(Normal): Strict bitwise comparison of0,1,X, andZvalues.casez: Treats high-impedanceZbits (or?) in the patterns as don't-care values.casex: Treats bothXandZbits in the patterns as don't-care values.
// CaseZ example: matching priority encoder
casez (encoded_input) {
4'b1??? => out = 3;
4'b01?? => out = 2;
4'b001? => out = 1;
4'b0001 => out = 0;
default => out = 0;
}A wildcard match can conceal an unknown or undriven control signal. Use an explicit invalid-input check when X must expose a design error.
2.3 match Statements (Pattern Matching)
VexHDL supports Rust-like pattern matching for struct fields, enums, and values:
- Exhaustive Variant Checks: Variants are scoped with
Enum::Variant. - Wildcards:
_matches all remaining paths.
next_state = current_state; // default avoids retaining an unintended comb value
match (current_state) {
State::IDLE => {
if start { next_state = State::INIT; }
}
State::INIT => {
next_state = State::RUN;
}
_ => {
next_state = State::IDLE;
}
}
// Classify a nonnegative temperature with explicit, contiguous bounds.
if temperature <= 50 {
fan_speed = 0;
} else if temperature <= 100 {
fan_speed = 1;
} else if temperature <= 150 {
fan_speed = 2;
} else {
fan_speed = 3;
}3. Loops
Loops are unrolled during compilation/synthesis or run dynamically during simulation.
3.1 for Loops
Used for structured iteration.
for i in 0..8 {
out_vector[i] = in_vector[7 - i]; // Reverse bit-order
}3.2 repeat(count) Loops
Repeats a statement block count times.
repeat(8) {
clk = !clk;
Sim.delay(5);
}3.3 forever Loops
Runs an infinite loop. Primarily used inside clock generation threads.
forever {
clk = !clk;
Sim.delay(10);
}4. Simulation Concurrency & Synchronization
VexHDL provides advanced statements for managing time-synchronization and thread forks in simulation:
fork ... join: Fork-join blocks run multiple statement blocks concurrently as parallel threads during simulation.vexhdlfork { // Thread 1: Wait and print Sim.delay(50); Sim.print("Thread 1 completed!"); } { // Thread 2: Clock stimulus repeat(5) { clk = !clk; Sim.delay(10); } Sim.print("Thread 2 completed!"); } joinwait(condition): Halts thread execution until a condition expression evaluates totrue.vexhdlwait(ready_flag == true); // Continue execution once flag is set