Count the cells
You will learn
How to count what a synthesizer built: LUTs, flip-flops, DSPs, block RAM.
After synthesis, yosys can print what it built as JSON: how many LUTs, flip-flops, DSPs and block RAMs. Those counts are an honest measure of a design before place and route. Paste a stat -json from yosys into the stat card, or open the real sample, and get a card and a README badge that say plainly that they are a synthesis count.
Try it
Open the real sample, then read the LUT and FF counts on the card.

Paste yosys stat -json: LUT, FF, DSP and BRAM counts, the top module and the yosys version on a 1200x630 card plus a README badge SVG. Read in your browser.
specs/tutorial/04_control_flow.t27
// SPDX-License-Identifier: Apache-2.0
; 04 — Control flow
; Lesson 4. if/else as a statement and as an expression, while, for over a
; range, and break/continue.
; .
; One rule worth learning before you need it: `switch` belongs in lesson 06,
; and only in its expression form. The statement form does not survive
; compilation, and it does not tell you so.
; phi^2 + 1/phi^2 = 3 | TRINITY
module tutorial-04-control-flow;
pub const TRIT_NEG : i8 = -1;
pub const TRIT_ZERO : i8 = 0;
pub const TRIT_POS : i8 = 1;
// ============================================================================
// if / else
// ============================================================================
pub fn sign_of(value: i32) i8 {
if (value > 0) {
return TRIT_POS;
} else {
if (value < 0) {
return TRIT_NEG;
}
}
return TRIT_ZERO;
}
// ============================================================================
// if as an expression
//
// `if (cond) a else b` produces a value. For a two-way choice this reads
// better than four lines of statement, and it is the form to reach for first.
// ============================================================================
pub fn larger(a: i32, b: i32) i32 {
return if (a > b) a else b;
}
pub fn clamp_to_trit(value: i32) i8 {
const high = if (value > 1) 1 else value;
const low = if (high < -1) -1 else high;
return @as(i8, @intCast(low));
}
// ============================================================================
// while
//
// Note the width: the counter is i32. Lesson 05 explains why a narrower
// counter needs one extra piece of syntax here.
// ============================================================================
pub fn count_to(limit: i32) i32 {
var i : i32 = 0;
while (i < limit) {
i = i + 1;
}
return i;
}
pub fn sum_to(limit: i32) i32 {
var total : i32 = 0;
var i : i32 = 0;
while (i < limit) {
total = total + i;
i = i + 1;
}
return total;
}
// ============================================================================
// while with a continue expression
//
// while (cond) : (step) { body }
//
// The step runs after each pass. It keeps the loop variable's advance next to
// the condition instead of buried at the end of the body -- and it has one
// more property, which lesson 05 gets to.
// ============================================================================
pub fn sum_with_step(limit: u8) u8 {
var total : u8 = 0;
var i : u8 = 0;
while (i < limit) : (i += 1) {
total = total + i;
}
return total;
}
// ============================================================================
// for over a range
//
// for (LOW..HIGH) |name| { ... }
//
// The bound is exclusive: 0..4 visits 0, 1, 2, 3.
// ============================================================================
pub fn sum_range() u8 {
var total : u8 = 0;
for (0..4) |i| {
total = total + i;
}
return total;
}
// ============================================================================
// break and continue
// ============================================================================
pub fn first_multiple(limit: i32, factor: i32) i32 {
var i : i32 = 1;
var found : i32 = 0;
while (i < limit) {
i = i + 1;
if (i % factor != 0) {
continue;
}
found = i;
break;
}
return found;
}
// ============================================================================
// Tests
// ============================================================================
test "sign_of returns one of the three trits" {
try std.testing.expectEqual(@as(i8, TRIT_POS), sign_of(42));
try std.testing.expectEqual(@as(i8, TRIT_NEG), sign_of(-42));
try std.testing.expectEqual(@as(i8, TRIT_ZERO), sign_of(0));
}
test "larger picks the greater of two" {
try std.testing.expectEqual(@as(i32, 9), larger(9, 3));
try std.testing.expectEqual(@as(i32, 9), larger(3, 9));
}
test "the two summing loops agree" {
try std.testing.expectEqual(@as(i32, 6), sum_to(4));
try std.testing.expectEqual(@as(u8, 6), sum_with_step(4));
try std.testing.expectEqual(@as(u8, 6), sum_range());
}
test "clamp_to_trit saturates at both rails" {
try std.testing.expectEqual(@as(i8, TRIT_POS), clamp_to_trit(99));
try std.testing.expectEqual(@as(i8, TRIT_NEG), clamp_to_trit(-99));
try std.testing.expectEqual(@as(i8, TRIT_ZERO), clamp_to_trit(0));
}
All lessons
Module 1 · The chip
What an FPGA is, which chip we use, and how its pins meet the board.
Module 2 · Numbers in hardware
Bits, trits and number formats, and what arithmetic costs in logic.
Module 3 · Your t27 program
Write a spec, test it, and see why compiling is not the same as being right.
Module 4 · Inside t27c
How the compiler reads a spec and what it writes, including the native t27b.
Module 5 · From spec to hardware
The Verilog t27c writes, the cells it becomes, and what one LUT does.
Module 6 · Reading synthesis
What yosys reports about your design, and which warnings matter.
Module 7 · Place, route, timing
Where the cells land on the die, and whether the clock is met.
Module 8 · The bitstream
How a routed design becomes the bits the chip loads, with no Vivado.
Module 9 · On the board
Ask the chip who it is, load the bits, and check them.