generate-for: a Loop in Space, Not in Time

The intro to level 37: how one line of a loop describes eight identical flip-flops — and why they still all fire together, in a single tick.

Experiment: all 8 bits on one edge

Eight lamps are a data byte; the WRITE toggle is the shared clock of all eight flip-flops in the register. One edge stores the whole byte: turn on the 4 and 1 lamps, press WRITE, and the register shows 5. Change the inputs — the register holds the stored value until a new press.

Predict: how many ticks for eight flip-flops

A generate loop describes 8 D flip-flops sharing the clk input. One tick stores all 8 bits: eight is the number of copies of the circuit, not the number of ticks. The order in the description affects only the copies' names, never their timing.

The rule: draw 8 copies side by side

generate-for is not "do it eight times in a row" but "draw eight copies side by side": the loop unrolls into 8 independent flip-flops before the circuit ever runs. Three required elements: genvar i — the blueprint's counter, begin : bit_cell — the clone label (the copies get the names bit_cell[0].u … bit_cell[7].u), and the substitution in[i], which at i = 3 wires up bit 3.

Where it is used

On this level you build an 8-bit register out of D flip-flops with a generate loop. The same trick then multiplies any repeated node: counter digits, ALU bits, memory rows — everywhere one and the same circuit stands in a long row.

Take the intro in the game · Level 37 guide · All intros