t27.aiРусский

From a spec to Verilog

You will learn

How a t27 function becomes logic, and how a clocked function becomes registers.

Verilog is the text a synthesizer reads to build a circuit. t27c writes it from the spec. A function named on_clock becomes an always block on the clock edge, and every module var becomes a register with a reset. Open clocked_counter.t27 in the player: an 8-bit counter in a few lines, and the Verilog it turns into. Its header records what yosys built from it.

Try it

Open clocked_counter.t27 in the player and find the always block in its Verilog tab.

Open the interactive lesson →

hello_world.t27 to Verilog, in a terminal inside the post
hello_world.t27 to Verilog, in a terminal inside the post ↗

A terminal in your browser running the t27 compiler as WebAssembly: t27c gen verilog prints the Verilog it emits for hello_world.t27, and t27c test runs the spec's tests.

specs/ternary/clocked_counter.t27

module ClockedCounter;
// #1764: the sequential-state primitive for the spec-first ternary datapath.
//
// Until now `gen-verilog` was combinational-only: it emitted no
// `always @(posedge clk)` and module-level `var` state was never registered.
// A function named `on_clock` is the opt-in clocked process — it lowers to an
// `always @(posedge clk or negedge rst_n)` that registers every scalar
// module-level `var`: on `!rst_n` each `var` takes its declared init value, and
// while `en` is asserted the body runs with nonblocking (`<=`) assignments.
//
// This `count` register is the minimal proof of that mechanism: reset,
// en-gating (freeze) and per-cycle advance. It is the building block a streamed
// ternary MAC needs to accumulate across cycles (the Phase-2 MVP gate).
//
// Because an `on_clock` fn is present, each scalar module `var` (here `count`)
// is exposed as an `output reg` data port — so the registered state is
// observable and the design SYNTHESIZES to real flip-flops (yosys synth_xilinx:
// 8 FDCE + 2 CARRY4). Without an output port a synthesizer dead-code-eliminates
// the whole design to zero cells. This is the first spec-first design that maps
// to non-zero real hardware.
//
// Specs without an `on_clock` fn are unaffected — their generated Verilog is
// byte-identical to before this feature.

var count : u8 = 0

fn on_clock() {
    count = count + 1
}

test "on_clock_increments" {
    assert_eq(count, 0);
    on_clock();
    assert_eq(count, 1);
    on_clock();
    assert_eq(count, 2);
}

Open the lesson's spec in the player ↗

All lessons