t27.aiEnglish

Посчитайте ячейки

Вы узнаете

Как посчитать, что построил синтезатор: LUT, триггеры, DSP, блочную память.

После синтеза yosys может напечатать построенное в JSON: сколько LUT, триггеров, DSP и блоков памяти. Эти числа — честная мера дизайна до размещения и трассировки. Вставьте stat -json из yosys в карточку или откройте настоящий пример и получите карточку и бейдж для README, которые прямо говорят, что это подсчёт синтеза.

Попробуйте

Откройте настоящий пример и прочитайте на карточке число LUT и FF.

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

Stat card: your yosys stat -json as a card and a README badge
Stat card: your yosys stat -json as a card and a README badge ↗

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

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

Все уроки