t27.aiEnglish

От спеки к Verilog

Вы узнаете

Как функция t27 становится логикой, а тактируемая функция — регистрами.

Verilog — это текст, по которому синтезатор строит схему. t27c пишет его из спеки. Функция с именем on_clock становится блоком always по фронту такта, а каждая переменная модуля — регистром со сбросом. Откройте clocked_counter.t27 в плеере: 8-битный счётчик в несколько строк и Verilog, в который он превращается. В его заголовке записано, что из него построил yosys.

Попробуйте

Откройте clocked_counter.t27 в плеере и найдите блок always на вкладке Verilog.

Открыть интерактивный урок →

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);
}

Открыть спеку урока в плеере ↗

Все уроки