Skip to content

§ 2.5. Flags

ALUFlagUnit<W> derives result flags and qualifies arithmetic and shift status. Flags are combinational outputs, not stored condition codes.

Component Description Structural Behavioral
ALUFlagUnit<W> Zero/sign detection and enabled carry/overflow Yes Yes

W is 8, 16 or 32. The fidelity columns hold at every width: each unit has a structural and a compact form at 8, 16 and 32 bits. The C++ references take W as a template parameter. Construction and selection describes construction.

2.5.1. ALUFlagUnit<W>

IO pins

Pin Direction Width Description
RESULT Input W Final selected result
ARITHMETIC, SHIFT Input 1 each Arithmetic/shift flag enables
ARITH_CARRY, ARITH_OVERFLOW Input 1 each Arithmetic source flags
SHIFT_CARRY Input 1 Shifter source carry
ZERO, NEGATIVE Output 1 each Result zero and binary sign
CARRY, OVERFLOW Output 1 each Enabled source flags

Behavior

For binary inputs, ZERO=[RESULT==0], NEGATIVE=RESULT[W−1], CARRY=(ARITHMETIC AND ARITH_CARRY) OR (SHIFT AND SHIFT_CARRY), and OVERFLOW=ARITHMETIC AND ARITH_OVERFLOW.

Both enables low clear carry/overflow. Both high OR the carry sources; there is no priority. The complete ALU drives mutually exclusive enables. Comparison/min/max operations clear these enables. Persistent condition codes require a separate register.

C++ example

This binary reference is parameterized by W = 8, 16 or 32. Inputs must fit their documented pin widths. It omits propagation delays and X/Z values.

#include <cstdint>

struct FlagsResult {
    bool ZERO;
    bool NEGATIVE;
    bool CARRY;
    bool OVERFLOW;
};

template<unsigned W>
FlagsResult flags_ref(
    std::uint32_t result, bool arithmetic, bool shift,
    bool arith_carry, bool arith_overflow, bool shift_carry) {
    return {
        result == 0,
        (result & WordReference<W>::sign) != 0,
        (arithmetic && arith_carry) || (shift && shift_carry),
        arithmetic && arith_overflow,
    };
}

Unknown inputs

Zero detection masks unknowns when any result bit is known one. Sign passes through OR with zero, so either X or Z in RESULT[W−1] produces NEGATIVE=X. Low enables mask unknown source flags; all remaining cases follow four-state AND/OR gates.

Implementation

Structural implementation

ZERO_DETECT is ZeroDetect<W>. RESULT_SPLIT exposes the sign; SIGN ORs it with zero. Two AND gates mask carry sources, an OR combines them, and another AND qualifies overflow. There is no flag storage or clock.

Behavioral implementation

The compact form, available at W = 8, 16 and 32, computes the settled outputs of these gates in one step. It keeps the gates' Boolean factoring, so X and Z inputs give the same outputs as the structural circuit, and it runs on delays proven on that circuit (delay model).

Examples

All 128 recorded rows of ALUFlagUnitWidth8Test, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.

Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.

Checkpointzero-based number · timeline label Timesimulator units ARITHMETICInput · 1 bit ARITH_CARRYInput · 1 bit ARITH_OVERFLOWInput · 1 bit RESULTInput · 8 bit SHIFTInput · 1 bit SHIFT_CARRYInput · 1 bit CARRYExpected output · 1 bit NEGATIVEExpected output · 1 bit OVERFLOWExpected output · 1 bit ZEROExpected output · 1 bit
#0row-04 0 0 0 0x00 0 0 0 0 0 1
#1row-19 0 0 0 0x01 0 0 0 0 0 0
#2row-213 0 0 0 0x80 0 0 0 1 0 0
#3row-317 0 0 0 0xFF 0 0 0 1 0 0
#4row-422 1 0 0 0x00 0 0 0 0 0 1
#5row-527 1 0 0 0x01 0 0 0 0 0 0
#6row-631 1 0 0 0x80 0 0 0 1 0 0
#7row-735 1 0 0 0xFF 0 0 0 1 0 0
#8row-840 0 0 0 0x00 1 0 0 0 0 1
#9row-945 0 0 0 0x01 1 0 0 0 0 0
#10row-1049 0 0 0 0x80 1 0 0 1 0 0
#11row-1153 0 0 0 0xFF 1 0 0 1 0 0
#12row-1258 1 0 0 0x00 1 0 0 0 0 1
#13row-1363 1 0 0 0x01 1 0 0 0 0 0
#14row-1467 1 0 0 0x80 1 0 0 1 0 0
#15row-1571 1 0 0 0xFF 1 0 0 1 0 0
#16row-1676 0 1 0 0x00 0 0 0 0 0 1
#17row-1781 0 1 0 0x01 0 0 0 0 0 0
#18row-1885 0 1 0 0x80 0 0 0 1 0 0
#19row-1989 0 1 0 0xFF 0 0 0 1 0 0
#20row-2094 1 1 0 0x00 0 0 1 0 0 1
#21row-2199 1 1 0 0x01 0 0 1 0 0 0
#22row-22103 1 1 0 0x80 0 0 1 1 0 0
#23row-23107 1 1 0 0xFF 0 0 1 1 0 0
#24row-24112 0 1 0 0x00 1 0 0 0 0 1
#25row-25117 0 1 0 0x01 1 0 0 0 0 0
#26row-26121 0 1 0 0x80 1 0 0 1 0 0
#27row-27125 0 1 0 0xFF 1 0 0 1 0 0
#28row-28130 1 1 0 0x00 1 0 1 0 0 1
#29row-29135 1 1 0 0x01 1 0 1 0 0 0
#30row-30139 1 1 0 0x80 1 0 1 1 0 0
#31row-31143 1 1 0 0xFF 1 0 1 1 0 0
#32row-32148 0 0 1 0x00 0 0 0 0 0 1
#33row-33153 0 0 1 0x01 0 0 0 0 0 0
#34row-34157 0 0 1 0x80 0 0 0 1 0 0
#35row-35161 0 0 1 0xFF 0 0 0 1 0 0
#36row-36166 1 0 1 0x00 0 0 0 0 1 1
#37row-37171 1 0 1 0x01 0 0 0 0 1 0
#38row-38175 1 0 1 0x80 0 0 0 1 1 0
#39row-39179 1 0 1 0xFF 0 0 0 1 1 0
#40row-40184 0 0 1 0x00 1 0 0 0 0 1
#41row-41189 0 0 1 0x01 1 0 0 0 0 0
#42row-42193 0 0 1 0x80 1 0 0 1 0 0
#43row-43197 0 0 1 0xFF 1 0 0 1 0 0
#44row-44202 1 0 1 0x00 1 0 0 0 1 1
#45row-45207 1 0 1 0x01 1 0 0 0 1 0
#46row-46211 1 0 1 0x80 1 0 0 1 1 0
#47row-47215 1 0 1 0xFF 1 0 0 1 1 0
#48row-48220 0 1 1 0x00 0 0 0 0 0 1
#49row-49225 0 1 1 0x01 0 0 0 0 0 0
#50row-50229 0 1 1 0x80 0 0 0 1 0 0
#51row-51233 0 1 1 0xFF 0 0 0 1 0 0
#52row-52238 1 1 1 0x00 0 0 1 0 1 1
#53row-53243 1 1 1 0x01 0 0 1 0 1 0
#54row-54247 1 1 1 0x80 0 0 1 1 1 0
#55row-55251 1 1 1 0xFF 0 0 1 1 1 0
#56row-56256 0 1 1 0x00 1 0 0 0 0 1
#57row-57261 0 1 1 0x01 1 0 0 0 0 0
#58row-58265 0 1 1 0x80 1 0 0 1 0 0
#59row-59269 0 1 1 0xFF 1 0 0 1 0 0
#60row-60274 1 1 1 0x00 1 0 1 0 1 1
#61row-61279 1 1 1 0x01 1 0 1 0 1 0
#62row-62283 1 1 1 0x80 1 0 1 1 1 0
#63row-63287 1 1 1 0xFF 1 0 1 1 1 0
#64row-64292 0 0 0 0x00 0 1 0 0 0 1
#65row-65297 0 0 0 0x01 0 1 0 0 0 0
#66row-66301 0 0 0 0x80 0 1 0 1 0 0
#67row-67305 0 0 0 0xFF 0 1 0 1 0 0
#68row-68310 1 0 0 0x00 0 1 0 0 0 1
#69row-69315 1 0 0 0x01 0 1 0 0 0 0
#70row-70319 1 0 0 0x80 0 1 0 1 0 0
#71row-71323 1 0 0 0xFF 0 1 0 1 0 0
#72row-72328 0 0 0 0x00 1 1 1 0 0 1
#73row-73333 0 0 0 0x01 1 1 1 0 0 0
#74row-74337 0 0 0 0x80 1 1 1 1 0 0
#75row-75341 0 0 0 0xFF 1 1 1 1 0 0
#76row-76346 1 0 0 0x00 1 1 1 0 0 1
#77row-77351 1 0 0 0x01 1 1 1 0 0 0
#78row-78355 1 0 0 0x80 1 1 1 1 0 0
#79row-79359 1 0 0 0xFF 1 1 1 1 0 0
#80row-80364 0 1 0 0x00 0 1 0 0 0 1
#81row-81369 0 1 0 0x01 0 1 0 0 0 0
#82row-82373 0 1 0 0x80 0 1 0 1 0 0
#83row-83377 0 1 0 0xFF 0 1 0 1 0 0
#84row-84382 1 1 0 0x00 0 1 1 0 0 1
#85row-85387 1 1 0 0x01 0 1 1 0 0 0
#86row-86391 1 1 0 0x80 0 1 1 1 0 0
#87row-87395 1 1 0 0xFF 0 1 1 1 0 0
#88row-88400 0 1 0 0x00 1 1 1 0 0 1
#89row-89405 0 1 0 0x01 1 1 1 0 0 0
#90row-90409 0 1 0 0x80 1 1 1 1 0 0
#91row-91413 0 1 0 0xFF 1 1 1 1 0 0
#92row-92418 1 1 0 0x00 1 1 1 0 0 1
#93row-93423 1 1 0 0x01 1 1 1 0 0 0
#94row-94427 1 1 0 0x80 1 1 1 1 0 0
#95row-95431 1 1 0 0xFF 1 1 1 1 0 0
#96row-96436 0 0 1 0x00 0 1 0 0 0 1
#97row-97441 0 0 1 0x01 0 1 0 0 0 0
#98row-98445 0 0 1 0x80 0 1 0 1 0 0
#99row-99449 0 0 1 0xFF 0 1 0 1 0 0
#100row-100454 1 0 1 0x00 0 1 0 0 1 1
#101row-101459 1 0 1 0x01 0 1 0 0 1 0
#102row-102463 1 0 1 0x80 0 1 0 1 1 0
#103row-103467 1 0 1 0xFF 0 1 0 1 1 0
#104row-104472 0 0 1 0x00 1 1 1 0 0 1
#105row-105477 0 0 1 0x01 1 1 1 0 0 0
#106row-106481 0 0 1 0x80 1 1 1 1 0 0
#107row-107485 0 0 1 0xFF 1 1 1 1 0 0
#108row-108490 1 0 1 0x00 1 1 1 0 1 1
#109row-109495 1 0 1 0x01 1 1 1 0 1 0
#110row-110499 1 0 1 0x80 1 1 1 1 1 0
#111row-111503 1 0 1 0xFF 1 1 1 1 1 0
#112row-112508 0 1 1 0x00 0 1 0 0 0 1
#113row-113513 0 1 1 0x01 0 1 0 0 0 0
#114row-114517 0 1 1 0x80 0 1 0 1 0 0
#115row-115521 0 1 1 0xFF 0 1 0 1 0 0
#116row-116526 1 1 1 0x00 0 1 1 0 1 1
#117row-117531 1 1 1 0x01 0 1 1 0 1 0
#118row-118535 1 1 1 0x80 0 1 1 1 1 0
#119row-119539 1 1 1 0xFF 0 1 1 1 1 0
#120row-120544 0 1 1 0x00 1 1 1 0 0 1
#121row-121549 0 1 1 0x01 1 1 1 0 0 0
#122row-122553 0 1 1 0x80 1 1 1 1 0 0
#123row-123557 0 1 1 0xFF 1 1 1 1 0 0
#124row-124562 1 1 1 0x00 1 1 1 0 1 1
#125row-125567 1 1 1 0x01 1 1 1 0 1 0
#126row-126571 1 1 1 0x80 1 1 1 1 1 0
#127row-127575 1 1 1 0xFF 1 1 1 1 1 0

Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.

This recording instantiates W = 8; the C++ reference accepts every supported W. Width/configuration coverage is listed in the tests below.

Rows with both ARITHMETIC = 1 and SHIFT = 1 demonstrate the standalone both-enables-high behavior, not a valid complete-ALU operation group.

Embedded simulation

The alu-flag-unit-w8 scenario runs ALUFlagUnitWidth8Test: 128 recorded binary cases, covering all 32 combinations of the five single-bit inputs with each of four result words (0x00, 0x01, 0x80, 0xFF). All appear in the table above. The source flags are driven by the test; upstream arithmetic and shifting are not part of this standalone circuit.

Open ALUFlagUnit<8> in the simulator

Source and tests

Test Coverage
ALUFlagUnitWidth8Test / flagsRows() 128 recorded binary cases: 32 control/source-flag combinations × four result words. Checks all four outputs, including both enables high; this is the embedded scenario.
ALUContractTest Check flags through the complete ALU, with its actual result selector and group enables. Contract cases include unknown result data and unknown carry for a zero-distance rotation.

The 128-case scenario is exhaustive over the five binary single-bit inputs for those four result words, not over all 256 words or four-state combinations.