Early alpha
A modern hardware description language that compiles to plain Verilog
KHDL checks integer ranges, clock domains and register resets every time you compile. Interfaces and generics replace copied port lists and code generators. The output is plain Verilog 2005, so it works with the simulation, synthesis and lint tools you already use.
The language and compiler are still changing. Build instructions are in the repository.
pub module Pwm(period: positive) ports(
clk: in clock,
sync(clk) {
rst: in bool,
duty: in uint(0..=period),
pin: out bool,
}
) {
clocked(clk, sync rst) {
reg phase: uint(0..period) = 0;
reg wire pin = false;
pin = phase < duty;
if (phase == period - 1) {
phase = 0;
} else {
phase = phase + 1;
}
}
}
What KHDL does differently
Each of these is checked or generated by the compiler, on every build.
Integers know their range
Integer types are ranges: uint(8) holds 0 to 255, int(-8..=7) holds −8 to 7.
Arithmetic results get the range they actually need, and every assignment is checked.
count + 1 has type int(1..257), so storing it in a uint(8) is a
compile error instead of a silent wrap-around.
The compiler also follows your control flow. In the else branch below,
count can't be 255, so count + 1 fits. If you want wrap-around, write it:
(count + 1) % 256.
if,
count + 1 can be 256.
pub module Counter ports(
clk: in clock,
sync(clk) {
rst: in bool,
count: out uint(8),
}
) {
clocked(clk, sync rst) {
reg wire count = 0;
if (count == 255) {
count = 0;
} else {
count = count + 1;
}
}
}
Without the if, the compiler reports
error: type mismatch
--> counter_overflow.kh:10:17
|
5 | count: out uint(8),
| ------- target type set here
...
10 | count = count + 1;
| ----- ^^^^^^^^^ source value with type `int(1..257)` does not fit
| |
| target requires type `int(0..256)`
The same arithmetic in Verilog
In Verilog, the width and signedness of an expression come from its context. With
a = 7 and b = -2, this adder outputs −11: one operand is unsigned, so
b is zero-extended to 01110 (14), and the 5-bit sum 10101 is
read back as a signed number. That's what the standard prescribes, so tools accept it.
(Example from Jan Decaluwe.)
module MixedAdd(
input wire [2:0] a,
input wire signed [3:0] b,
output wire signed [4:0] result
);
assign result = a + b; // 7 + -2 = -11
endmodule
pub module MixedAdd ports(
a: in async int(0..=7),
b: in async int(-8..=7),
result: out async int(-8..=14),
) {
comb {
result = a + b;
}
}
In KHDL the ranges are the types, and 7 + -2 is 5.
Clock domains are part of the type
Every port and signal has a domain: sync(clk) for signals that belong to a clock,
async for those that don't. The compiler checks every assignment and connection, so an
accidental clock-domain crossing is a compile error, not something a separate CDC tool reports
weeks later.
Crossing on purpose is explicit, with unsafe_value_with_domain, so every crossing is easy
to find in review.
request in the
clk_b process crosses domains, and the compiler reports it.
pub module Crossing ports(
clk_a: in clock,
clk_b: in clock,
sync(clk_a) {
request: in bool,
}
sync(clk_b) {
rst_b: in bool,
seen: out bool,
}
) {
clocked(clk_b, sync rst_b) {
reg wire seen = false;
if (request) {
seen = true;
}
}
}
error: invalid domain crossing: different clock
--> crossing.kh:14:13
|
12 | clocked(clk_b, sync rst_b) {
| ------------------- target domain is sync(clk_b)
13 | reg wire seen = false;
14 | if (request) {
| ^^^^^^^
| |
| invalid domain crossing here
| source domain is sync(clk_a)
|
= note: crossing due to signal read in clocked block
= help: to intentionally cross domains, use `unsafe_value_with_domain` or `unsafe_bool_to_clock`
Code runs top to bottom, registers are declared
There is one assignment operator, and every statement sees the result of the statements before it,
like in software. Registers are declared with reg, registered outputs with
reg wire, and each needs a reset value or an explicit undef. The compiler
generates Verilog that simulates and synthesizes the same way.
That makes logic with several dependent steps easy to write. This module adds 5 to a stream of values, with ready/valid handshakes on both sides and a skid register, so it can still take a value every cycle. Each step only has to make sense given the steps before it, so you can check it like software instead of reasoning through every combination of states and inputs.
pub module AddFive ports(
clk: in clock,
sync(clk) {
rst: in bool,
input_data: in uint(8),
input_valid: in bool,
input_ready: out bool,
output_data: out uint(8),
output_valid: out bool,
output_ready: in bool,
}
) {
clocked(clk, sync rst) {
reg skid_data: uint(8) = undef;
reg skid_valid: bool = false;
reg wire input_ready = true;
reg wire output_data = undef;
reg wire output_valid = false;
// input handshake: store the new value
if (input_valid && input_ready) {
skid_data = input_data;
skid_valid = true;
}
// output handshake: the output has been taken
if (output_valid && output_ready) {
output_valid = false;
}
// add 5 and move to the output when it's free
if (skid_valid && !output_valid) {
output_data = (skid_data + 5) % 256;
output_valid = true;
skid_valid = false;
}
// ready for new input if skid is empty
input_ready = !skid_valid;
}
}
The same module in VHDL
VHDL variables also update immediately, so the steps can be written the same way. But variables only exist inside a process, and the ports have to be driven by signals. So every register is declared twice, copied into a variable at the start of the process, and copied back at the end.
architecture rtl of add_five is
signal r_skid_data : unsigned(7 downto 0);
signal r_skid_valid : std_logic;
signal r_output_data : unsigned(7 downto 0);
signal r_output_valid : std_logic;
begin
input_ready <= not r_skid_valid;
output_data <= r_output_data;
output_valid <= r_output_valid;
process(clk)
variable v_skid_data : unsigned(7 downto 0);
variable v_skid_valid : std_logic;
variable v_output_data : unsigned(7 downto 0);
variable v_output_valid : std_logic;
begin
if rising_edge(clk) then
if rst = '1' then
r_skid_valid <= '0';
r_output_valid <= '0';
else
-- copy registers into variables
v_skid_data := r_skid_data;
v_skid_valid := r_skid_valid;
v_output_data := r_output_data;
v_output_valid := r_output_valid;
-- input handshake
if input_valid = '1' and v_skid_valid = '0' then
v_skid_data := input_data;
v_skid_valid := '1';
end if;
-- output handshake
if output_ready = '1' then
v_output_valid := '0';
end if;
-- processing
if v_skid_valid = '1' and v_output_valid = '0' then
v_output_data := v_skid_data + 5;
v_output_valid := '1';
v_skid_valid := '0';
end if;
-- copy variables back into registers
r_skid_data <= v_skid_data;
r_skid_valid <= v_skid_valid;
r_output_data <= v_output_data;
r_output_valid <= v_output_valid;
end if;
end if;
end process;
end rtl;
Blocking and nonblocking assignments in Verilog
Verilog has blocking (=) and nonblocking (<=) assignments. Use the
wrong one, and the result can depend on which always block the simulator happens to
run first. In this two-stage pipeline, the first block uses = where both assignments
should have been <=, and two open-source simulators we tried disagree on its
output.
module RacyPipeline(input wire clk, rst, data, output reg result);
reg stage;
always @(posedge clk) begin
if (rst) stage = 0; // should have been <=
else stage = data; // should have been <=
end
always @(posedge clk) begin
if (rst) result <= 0;
else result <= stage;
end
endmodule
Interfaces instead of port lists
An interface groups the signals of a bus, and a view gives the direction of each signal
for one side. A module takes the whole bus as one port, directions flip between producer and consumer,
and connecting two modules takes one line.
Interfaces can be generic too: Stream(T) carries any payload type, from a byte to a
struct.
data and valid go forward, ready comes back. The
views set these directions, so each connection is a single name in the code.
interface Stream(T: type) {
data: T,
valid: bool,
ready: bool,
view Source { data: out, valid: out, ready: in }
view Sink { data: in, valid: in, ready: out }
}
pub module TwoStages ports(
clk: in clock,
sync(clk) {
rst: in bool,
input: interface Stream(uint(8)).Sink,
output: interface Stream(uint(8)).Source,
}
) {
wire link: interface Stream(uint(8));
instance Register(uint(8)) ports(clk, rst, input, output=link);
instance Register(uint(8)) ports(clk, rst, input=link, output);
}
Generics are ordinary code
Parameters can be numbers, types, functions or modules, and elaboration is regular code. A
for loop in the port list creates numbered ports, with names built from format strings.
Types can depend on parameters too: total is exactly wide enough for the sum of
inputs bytes.
sum has no declared type: its range follows each assignment, so every adder in the chain
is one bit wider than the last. In Verilog and VHDL, a port list is fixed, so a variable number of
inputs needs an array port or a script that generates the source.
inputs. Each adder is exactly as wide as its result
needs to be.
pub module Sum(inputs: positive) ports(
async {
for (i in 0..inputs) {
ident("value_{i}"): in uint(8),
}
total: out uint(0..=255 * inputs),
}
) {
comb {
var sum = 0;
for (i in 0..inputs) {
sum = sum + ident("value_{i}");
}
total = sum;
}
}
Fits into your existing flow
Plain Verilog output
A simple subset of Verilog 2005 that any EDA tool reads. Existing IP can be instantiated as external modules.
Clear error messages
Diagnostics point at the problem and at the declaration that caused it, like the ones above.
Editor support
The compiler includes a language server, so editors show the same errors as you type. There's a VS Code extension.
Python bindings
Elaborate modules, call functions and simulate with Verilator from Python, with values converted automatically.
Formatter
The compiler comes with a formatter, so every project can use one consistent code style.
Fast
The compiler is multithreaded down to individual items and elaborated modules, so large projects compile quickly.
Try it
The web demo runs the compiler in your browser and shows the generated Verilog as you type.