I originally wrote this article for issue #2 of Hardware Thrashers, but unfortunately that issue didn’t come to fruition. So I’m cross posting it here.
Ok so the truth is I’m a software guy invading this zine. I dabbled in digital design in college about ten years ago, but when I recently fired up ModelSim and Quartus again I was shocked because the tools looked ancient compared to what I became used to in software. So I asked the famed question: making an EDA toolchain from scratch couldn’t be that hard, could it?
You’re probably smart enough to know that yes, it’s extremely challenging. But I embarked on the journey anyway, and here’s what I learned.
At that point in time I was studying compilers, and I had a eureka moment during my studies. It turns out that an HDL like VHDL is actually NOT that different from a regular programming language. This might come as obvious to some, but to me HDLs were this bespoke thing with weird syntax so surely their execution model was totally different. Well actually… It’s pretty similar to a regular programming language.
When you compile a programming language to machine code, it goes through the following process

I have intentionally omitted steps from the backend process because they are out of scope for this article (I didn’t implement them). But suffice it to say that’s where things like Code Generation and Code Optimization live. The output of the backend is native machine code. The output of the front end of a compiler is an IR (Intermediate Representation).
The reason I dialed in on the front end is because it’s technically all you need to interpret a programming language. An interpreter directly executes your program, statement by statement, without producing a separate executable like a compiler does. Interpreters are easier to write and they’re a great way to verify your language’s semantics.
It turns out that to build a hardware simulator that performs behavioral simulation, you can start by building an interpreter for the language you want to simulate. That’s essentially what I did. It’s called Nexsim, an interpreter for VHDL.
Lexing
The front end of a compiler has a series of stages and the output of it is an intermediate representation. The input of an interpreter then is that intermediate representation. How do we get our VHDL code into an interpretable state? We follow the diagram!
You begin by Lexing your VHDL code. Lexical Analysis is the process of turning raw text, in this case your VHDL program, into tokens. A token is the smallest meaningful unit that the Lexer produces from raw source code. Tokens can look different in many implementations of compilers but the way I structured mine is as follows:
pub struct Token {
pub kind: TokenKind,
pub line: usize,
pub column: usize,
}
Lines and columns are included for nice error messages if the user makes a mistake. Let’s look at an example to see what this looks like when you tokenize VHDL.
inputs : in std_logic_vector(3 downto 0);
If you input this into the Lexer, it produces something like this
[
Token { kind: Identifier("inputs"), line: 1, column: 1 },
Token { kind: Colon, line: 1, column: 8 },
Token { kind: In, line: 1, column: 10 },
Token { kind: Identifier("std_logic_vector"), line: 1, column: 14 },
Token { kind: LeftParen, line: 1, column: 30 },
Token { kind: Integer("3"), line: 1, column: 31 },
Token { kind: DownTo, line: 1, column: 33 },
Token { kind: Integer("0"), line: 1, column: 40 },
Token { kind: RightParen, line: 1, column: 41 },
Token { kind: Semicolon, line: 1, column: 42 },
]
So the output of the Lexical Analysis stage is an array of tokens. You’ll notice the kind field indicates the “type” of token it is. You end up using the type marker in the parsing stage to help you build what’s called an Abstract Syntax Tree (AST).
Parsing
The second stage is called Syntax Analysis, otherwise known as parsing. Parsing is turning a flat sequence of tokens into a structured representation that represents the program’s grammatical structure. In the earlier diagram you saw that the output of Parsing is an IR. Oftentimes the IR is an AST.
There are two concepts here, one is the grammatical structure of a program, and the other is the AST. Let’s start with the first. Programming languages are of course different than spoken languages, but they do have one interesting similarity. Both contain grammar rules. A bunch of plain text that follows the rules of the VHDL grammar is, in fact, VHDL code. In compiler theory, the way that you typically represent the grammar of a language is through a technique called CFG (Context Free Grammar). CFG is a notation that is used to describe a rule in the grammar.
An example of this would be
signal_assignment_statement ::= identifier "<=" expression ";"
This CFG excerpt says “a signal assignment looks like this”. Some identifier on the left hand side of a <= character, followed by an expression, followed by a semicolon. Whitespace and comments are stripped during the tokenization step.
Parser generators exist that can produce a parser automatically from a grammar, but I went with a hand written recursive descent parser for educational purposes. In a recursive descent parser, you write a function for each rule in the grammar, and these functions call each other recursively as they consume tokens.
Here’s an example from my parser
// signal_assignment ::= identifier '<=' expression ';'
fn signal_assignment(&mut self) -> Result<ConcurrentStatement, ParseError> {
let target = self.identifier()?;
self.expect(TokenKind::LessEquals)?;
let expression = self.expression()?;
self.expect(TokenKind::Semicolon)?;
Ok(ConcurrentStatement::SignalAssignment { target, expression })
}
Notice how the function reads almost like the CFG rule itself. Each part of the grammar, the identifier, the <= operator, the expression, and the semicolon maps directly to a line of code. The expression() call will itself descend into more parsing functions, handling operators and so on. This is the “recursive descent” in action.
Here is an example of an AST for the following VHDL code
result <= a and b;

Semantic Analysis
Semantic Analysis is a step to see if the AST produced by the parsing step conforms to the semantics of the language. Let’s start with an example.
entity test is
port (
input : in std_logic;
output : out std_logic
);
end test;
architecture simple of test is
begin
output <= mystery_signal; -- ???
end simple;
According to the grammar of the VHDL language this is actually correct. The problem is that mystery_signal is nowhere to be found. We used an undeclared signal! Semantic Analysis checks and reports to the user if they made a mistake.
Semantic Analysis also decorates the AST from the parser with resolved information, most notably, types.
Take for example this signal declaration
signal data : std_logic_vector(7 downto 0);
After parsing it looks like this
RawSignalDeclaration {
identifiers: ["data"],
raw_type: RawType {
name: "std_logic_vector",
range: Some(VectorRange { left: 7, right: 0, direction: DownTo }),
},
initial_value: None,
}
At parse time we don’t KNOW that the std_logic_vector type is
- A valid VHDL type
- Whether the user imported the correct library to use it
- Its computed width, which is part of the type
After Semantic Analysis, we have a decorated version
SignalDeclaration {
identifiers: ["data"],
vhdl_type: VhdlType::StdLogicVector {
size: 8,
left: 7,
right: 0,
direction: DownTo,
},
initial_value: None,
}
We are now CERTAIN, the user meant the std_logic_vector type from the ieee library std_logic_1164 package, and we verified they used it correctly.
The result the semantic analyzer produces is a decorated AST.
Elaboration
Awesome! We made it to simulation. Elaboration sits in an interesting spot.
The frontend work (lexing, parsing, semantic analysis) is done and you have a valid, type checked AST. In a traditional compiler, you’d now move to the backend for optimization, code generation, and emission of an executable.
However we’re not compiling to machine code in this case. Elaboration is really the first step of simulation. It’s where you go from “here’s a description of hardware” to “here’s a runnable model of that hardware.”
The process described up until now suggests that there is one AST but, when you simulate digital designs you tend to work across multiple files. In a tool like ModelSim, you upload multiple files and click compile. This process, at least how I did it, turns each file into an AST. The elaborator then takes all your designs and produces a flat simulation model that we can run against the simulation algorithm. It loads the simulator with a runtime model.
Here is what elaboration produces for a simple mux
entity mux_2x1 is
port (
in0, in1 : in std_logic;
sel : in std_logic;
output : out std_logic
);
end mux_2x1;
ElaboratedDesign {
signals: {
SignalId(0) → Signal { type: StdLogic, value: 'U' },
SignalId(1) → Signal { type: StdLogic, value: 'U' },
SignalId(2) → Signal { type: StdLogic, value: 'U' },
SignalId(3) → Signal { type: StdLogic, value: 'U' },
},
signal_name_to_id: {
"mux_2x1.in0" → SignalId(0),
"mux_2x1.in1" → SignalId(1),
"mux_2x1.sel" → SignalId(2),
"mux_2x1.output" → SignalId(3),
},
sensitivity_map: {
SignalId(0) → [ProcessId(0)], // in0 triggers the process
SignalId(1) → [ProcessId(0)], // in1 triggers the process
SignalId(2) → [ProcessId(0)], // sel triggers the process
},
...
}
Notice a few things.
- First, signals get hierarchical names like
mux_2x1.in0. This becomes important when you instantiate components inside other components and need to keep signal names unique. - Second, the
sensitivity_maplinks signals to the processes that care about them. Whenselchanges, the simulator knows exactly which process to wake up.
Elaboration also handles component instantiation. If your testbench instantiates a mux_2x1, elaboration recursively elaborates that child entity, creates its signals with a nested path like tb.uut.sel, and wires up port bindings so data flows correctly between parent and child.
The output of elaboration is everything the simulator needs to actually run: concrete signals with values, processes ready to execute, and a map of what depends on what.
Simulation
Finally we reached simulation! This is a great spot to link back to our earlier discussion on the similarities of compilers. When we run the simulation what we are really doing is something similar to what an interpreter does. An interpreter takes the AST and executes it directly. In this case we are taking a slightly different IR, the elaborated design, and executing directly against it!
Now we actually run the simulation. The simulator takes the elaborated design and executes it over time, tracking how signals change in response to events using a signal table. A master record of every signal active in the simulation and its current value.
VHDL simulation is event driven. Rather than evaluating everything on every tick, we only do work when something changes. The core data structure is an event queue implemented as a priority queue ordered by time. The three types of events that trigger activity in the algorithm are
- Signal Changes
- Time Advancements
- Process Wakeups
The algorithm looks like this

Let’s trace through a simple example. Say we have a testbench that toggles a clock:
process
begin
clk <= '1';
wait for 10 ns;
clk <= '0';
wait for 10 ns;
end process;
At time 0, the process runs and queues a SignalChange event: set clk to '1'. It then hits wait for 10 ns, suspends itself, and queues a ProcessWakeup event at time 10 ns.
The simulator processes the signal change, updates clk, and checks the sensitivity map: “who cares about clk?” Any process with clk in its sensitivity list gets executed. Any concurrent assignment that reads clk gets re-evaluated.
At time 10 ns, the ProcessWakeup event fires. The process resumes, queues a SignalChange to set clk to '0', and queues another ProcessWakeup at 20 ns. This continues until we hit our simulation time limit.
The key insight is that processes don’t run continuously, they run in response to events, then suspend. A process with a sensitivity list like process(a, b) only wakes when a or b changes. A process with wait statements explicitly controls when it resumes by queuing future ProcessWakeup events.
The Result
There’s a lot more detail to it than what I covered here but I wanted to cover the full lifecycle from source code to evaluation. Additionally MANY more optimizations are made in professional tools. They actually implement compilers to run native machine code, use specialized file formats, and etc.
In Nexsim, the product has an engine exactly like the one described here, wired up to web and desktop apps. The apps implement a waveform viewer so we can view the results of the simulation. Much like apps like GTKWave and Surfer.

So as of now, I have source code to behavioral simulation. There’s much more to tackle, not to mention synthesis.
If you want to try it out, feel free. But be warned! This is pre pre alpha software :) nexsim.dev