Тесты — часть спеки
Вы узнаете
Почему каждая спека t27 несёт свои тесты и что показывает временная диаграмма.
В t27 спека не закончена без своих утверждений: блоки test, инварианты и бенчи живут в том же файле, что и проверяемый ими код, а спеку без них компилятор отвергает. Тест можно запустить и как вентили в симуляторе. Диаграммы ниже — настоящая симуляция Verilog, сгенерированного из спеки троичного умножения с накоплением, на тестовых векторах этой же спеки.
Попробуйте
Приблизьте один такт и прочитайте значения на шинах в отметке pass.

Verilog generated from a ternary MAC spec, driven by the spec's own test vectors under Icarus Verilog: clock, buses, pass marks. Drop a .vcd and it draws yours.
specs/tutorial/07_tests_invariants_benches.t27
// SPDX-License-Identifier: Apache-2.0
; 07 — Tests, invariants and benches
; Lesson 7, and the reason this language exists rather than a header file.
; A spec carries its own claims: examples that must hold, properties that must
; always hold, and the operations worth measuring. All three are emitted into
; every target, so the same claim is checked in Zig, C and Rust alike.
; phi^2 + 1/phi^2 = 3 | TRINITY
module tutorial-07-claims;
pub const TRIT_NEG : i8 = -1;
pub const TRIT_ZERO : i8 = 0;
pub const TRIT_POS : i8 = 1;
pub const TRINITY : i8 = 3;
pub fn trit_add(a: i8, b: i8) i8 {
const sum : i32 = a + b;
const high = if (sum > 1) 1 else sum;
const low = if (high < -1) -1 else high;
return @as(i8, @intCast(low));
}
pub fn trit_mul(a: i8, b: i8) i8 {
return a * b;
}
// ============================================================================
// test — a concrete example
//
// test "name" { ... }
//
// Use it for the cases a reader would ask about: the boundaries, the identity,
// the one that used to be wrong.
// ============================================================================
test "trit_add saturates rather than wrapping" {
try std.testing.expectEqual(@as(i8, TRIT_POS), trit_add(TRIT_POS, TRIT_POS));
try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_add(TRIT_NEG, TRIT_NEG));
}
test "zero is the additive identity" {
try std.testing.expectEqual(@as(i8, TRIT_POS), trit_add(TRIT_POS, TRIT_ZERO));
try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_add(TRIT_ZERO, TRIT_NEG));
}
test "one is the multiplicative identity and zero absorbs" {
try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_mul(TRIT_NEG, TRIT_POS));
try std.testing.expectEqual(@as(i8, TRIT_ZERO), trit_mul(TRIT_ZERO, TRIT_NEG));
}
// ============================================================================
// invariant — a property, not an example
//
// invariant name
// assert EXPRESSION
//
// This is the part a test cannot express. A test says "for these inputs, this
// output"; an invariant says "this holds, full stop". Reach for it whenever a
// claim has no particular input attached to it.
// ============================================================================
invariant trit_set_has_three_states
assert TRIT_POS - TRIT_NEG + 1 == TRINITY
invariant trits_are_ordered
assert TRIT_NEG < TRIT_ZERO
invariant zero_sits_between_the_rails
assert TRIT_ZERO < TRIT_POS
// ============================================================================
// bench — what is worth measuring
//
// bench "name" { ... }
//
// A bench names an operation whose cost matters. It is a declaration of intent
// as much as a measurement: it says this is the hot path.
// ============================================================================
bench "trit_add" {
_ = trit_add(TRIT_POS, TRIT_NEG);
}
bench "trit_mul" {
_ = trit_mul(TRIT_POS, TRIT_NEG);
}
Все уроки
Модуль 1 · Чип
Что такое FPGA, какой чип у нас и как его выводы встречаются с платой.
Модуль 2 · Числа в железе
Биты, триты и форматы чисел, и сколько стоит арифметика в логике.
Модуль 3 · Ваша программа на t27
Напишите спеку, проверьте её и увидьте, почему «компилируется» не значит «верно».
Модуль 4 · Внутри t27c
Как компилятор читает спеку и что он пишет, включая нативный t27b.
Модуль 5 · От спеки к железу
Verilog, который пишет t27c, ячейки, в которые он превращается, и что делает одна LUT.
Модуль 6 · Читаем синтез
Что yosys сообщает о вашем дизайне и какие предупреждения важны.
Модуль 7 · Размещение, трассировка, тайминг
Где ячейки оказываются на кристалле и успевает ли тактовая частота.
Модуль 8 · Битстрим
Как оттрассированный дизайн становится битами, которые загружает чип, без Vivado.
Модуль 9 · На плате
Спросите чип, кто он, загрузите биты и проверьте их.