§ 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-0 | 4 | 0 |
0 |
0 |
0x00 |
0 |
0 |
0 |
0 |
0 |
1 |
#1row-1 | 9 | 0 |
0 |
0 |
0x01 |
0 |
0 |
0 |
0 |
0 |
0 |
#2row-2 | 13 | 0 |
0 |
0 |
0x80 |
0 |
0 |
0 |
1 |
0 |
0 |
#3row-3 | 17 | 0 |
0 |
0 |
0xFF |
0 |
0 |
0 |
1 |
0 |
0 |
#4row-4 | 22 | 1 |
0 |
0 |
0x00 |
0 |
0 |
0 |
0 |
0 |
1 |
#5row-5 | 27 | 1 |
0 |
0 |
0x01 |
0 |
0 |
0 |
0 |
0 |
0 |
#6row-6 | 31 | 1 |
0 |
0 |
0x80 |
0 |
0 |
0 |
1 |
0 |
0 |
#7row-7 | 35 | 1 |
0 |
0 |
0xFF |
0 |
0 |
0 |
1 |
0 |
0 |
#8row-8 | 40 | 0 |
0 |
0 |
0x00 |
1 |
0 |
0 |
0 |
0 |
1 |
#9row-9 | 45 | 0 |
0 |
0 |
0x01 |
1 |
0 |
0 |
0 |
0 |
0 |
#10row-10 | 49 | 0 |
0 |
0 |
0x80 |
1 |
0 |
0 |
1 |
0 |
0 |
#11row-11 | 53 | 0 |
0 |
0 |
0xFF |
1 |
0 |
0 |
1 |
0 |
0 |
#12row-12 | 58 | 1 |
0 |
0 |
0x00 |
1 |
0 |
0 |
0 |
0 |
1 |
#13row-13 | 63 | 1 |
0 |
0 |
0x01 |
1 |
0 |
0 |
0 |
0 |
0 |
#14row-14 | 67 | 1 |
0 |
0 |
0x80 |
1 |
0 |
0 |
1 |
0 |
0 |
#15row-15 | 71 | 1 |
0 |
0 |
0xFF |
1 |
0 |
0 |
1 |
0 |
0 |
#16row-16 | 76 | 0 |
1 |
0 |
0x00 |
0 |
0 |
0 |
0 |
0 |
1 |
#17row-17 | 81 | 0 |
1 |
0 |
0x01 |
0 |
0 |
0 |
0 |
0 |
0 |
#18row-18 | 85 | 0 |
1 |
0 |
0x80 |
0 |
0 |
0 |
1 |
0 |
0 |
#19row-19 | 89 | 0 |
1 |
0 |
0xFF |
0 |
0 |
0 |
1 |
0 |
0 |
#20row-20 | 94 | 1 |
1 |
0 |
0x00 |
0 |
0 |
1 |
0 |
0 |
1 |
#21row-21 | 99 | 1 |
1 |
0 |
0x01 |
0 |
0 |
1 |
0 |
0 |
0 |
#22row-22 | 103 | 1 |
1 |
0 |
0x80 |
0 |
0 |
1 |
1 |
0 |
0 |
#23row-23 | 107 | 1 |
1 |
0 |
0xFF |
0 |
0 |
1 |
1 |
0 |
0 |
#24row-24 | 112 | 0 |
1 |
0 |
0x00 |
1 |
0 |
0 |
0 |
0 |
1 |
#25row-25 | 117 | 0 |
1 |
0 |
0x01 |
1 |
0 |
0 |
0 |
0 |
0 |
#26row-26 | 121 | 0 |
1 |
0 |
0x80 |
1 |
0 |
0 |
1 |
0 |
0 |
#27row-27 | 125 | 0 |
1 |
0 |
0xFF |
1 |
0 |
0 |
1 |
0 |
0 |
#28row-28 | 130 | 1 |
1 |
0 |
0x00 |
1 |
0 |
1 |
0 |
0 |
1 |
#29row-29 | 135 | 1 |
1 |
0 |
0x01 |
1 |
0 |
1 |
0 |
0 |
0 |
#30row-30 | 139 | 1 |
1 |
0 |
0x80 |
1 |
0 |
1 |
1 |
0 |
0 |
#31row-31 | 143 | 1 |
1 |
0 |
0xFF |
1 |
0 |
1 |
1 |
0 |
0 |
#32row-32 | 148 | 0 |
0 |
1 |
0x00 |
0 |
0 |
0 |
0 |
0 |
1 |
#33row-33 | 153 | 0 |
0 |
1 |
0x01 |
0 |
0 |
0 |
0 |
0 |
0 |
#34row-34 | 157 | 0 |
0 |
1 |
0x80 |
0 |
0 |
0 |
1 |
0 |
0 |
#35row-35 | 161 | 0 |
0 |
1 |
0xFF |
0 |
0 |
0 |
1 |
0 |
0 |
#36row-36 | 166 | 1 |
0 |
1 |
0x00 |
0 |
0 |
0 |
0 |
1 |
1 |
#37row-37 | 171 | 1 |
0 |
1 |
0x01 |
0 |
0 |
0 |
0 |
1 |
0 |
#38row-38 | 175 | 1 |
0 |
1 |
0x80 |
0 |
0 |
0 |
1 |
1 |
0 |
#39row-39 | 179 | 1 |
0 |
1 |
0xFF |
0 |
0 |
0 |
1 |
1 |
0 |
#40row-40 | 184 | 0 |
0 |
1 |
0x00 |
1 |
0 |
0 |
0 |
0 |
1 |
#41row-41 | 189 | 0 |
0 |
1 |
0x01 |
1 |
0 |
0 |
0 |
0 |
0 |
#42row-42 | 193 | 0 |
0 |
1 |
0x80 |
1 |
0 |
0 |
1 |
0 |
0 |
#43row-43 | 197 | 0 |
0 |
1 |
0xFF |
1 |
0 |
0 |
1 |
0 |
0 |
#44row-44 | 202 | 1 |
0 |
1 |
0x00 |
1 |
0 |
0 |
0 |
1 |
1 |
#45row-45 | 207 | 1 |
0 |
1 |
0x01 |
1 |
0 |
0 |
0 |
1 |
0 |
#46row-46 | 211 | 1 |
0 |
1 |
0x80 |
1 |
0 |
0 |
1 |
1 |
0 |
#47row-47 | 215 | 1 |
0 |
1 |
0xFF |
1 |
0 |
0 |
1 |
1 |
0 |
#48row-48 | 220 | 0 |
1 |
1 |
0x00 |
0 |
0 |
0 |
0 |
0 |
1 |
#49row-49 | 225 | 0 |
1 |
1 |
0x01 |
0 |
0 |
0 |
0 |
0 |
0 |
#50row-50 | 229 | 0 |
1 |
1 |
0x80 |
0 |
0 |
0 |
1 |
0 |
0 |
#51row-51 | 233 | 0 |
1 |
1 |
0xFF |
0 |
0 |
0 |
1 |
0 |
0 |
#52row-52 | 238 | 1 |
1 |
1 |
0x00 |
0 |
0 |
1 |
0 |
1 |
1 |
#53row-53 | 243 | 1 |
1 |
1 |
0x01 |
0 |
0 |
1 |
0 |
1 |
0 |
#54row-54 | 247 | 1 |
1 |
1 |
0x80 |
0 |
0 |
1 |
1 |
1 |
0 |
#55row-55 | 251 | 1 |
1 |
1 |
0xFF |
0 |
0 |
1 |
1 |
1 |
0 |
#56row-56 | 256 | 0 |
1 |
1 |
0x00 |
1 |
0 |
0 |
0 |
0 |
1 |
#57row-57 | 261 | 0 |
1 |
1 |
0x01 |
1 |
0 |
0 |
0 |
0 |
0 |
#58row-58 | 265 | 0 |
1 |
1 |
0x80 |
1 |
0 |
0 |
1 |
0 |
0 |
#59row-59 | 269 | 0 |
1 |
1 |
0xFF |
1 |
0 |
0 |
1 |
0 |
0 |
#60row-60 | 274 | 1 |
1 |
1 |
0x00 |
1 |
0 |
1 |
0 |
1 |
1 |
#61row-61 | 279 | 1 |
1 |
1 |
0x01 |
1 |
0 |
1 |
0 |
1 |
0 |
#62row-62 | 283 | 1 |
1 |
1 |
0x80 |
1 |
0 |
1 |
1 |
1 |
0 |
#63row-63 | 287 | 1 |
1 |
1 |
0xFF |
1 |
0 |
1 |
1 |
1 |
0 |
#64row-64 | 292 | 0 |
0 |
0 |
0x00 |
0 |
1 |
0 |
0 |
0 |
1 |
#65row-65 | 297 | 0 |
0 |
0 |
0x01 |
0 |
1 |
0 |
0 |
0 |
0 |
#66row-66 | 301 | 0 |
0 |
0 |
0x80 |
0 |
1 |
0 |
1 |
0 |
0 |
#67row-67 | 305 | 0 |
0 |
0 |
0xFF |
0 |
1 |
0 |
1 |
0 |
0 |
#68row-68 | 310 | 1 |
0 |
0 |
0x00 |
0 |
1 |
0 |
0 |
0 |
1 |
#69row-69 | 315 | 1 |
0 |
0 |
0x01 |
0 |
1 |
0 |
0 |
0 |
0 |
#70row-70 | 319 | 1 |
0 |
0 |
0x80 |
0 |
1 |
0 |
1 |
0 |
0 |
#71row-71 | 323 | 1 |
0 |
0 |
0xFF |
0 |
1 |
0 |
1 |
0 |
0 |
#72row-72 | 328 | 0 |
0 |
0 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#73row-73 | 333 | 0 |
0 |
0 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#74row-74 | 337 | 0 |
0 |
0 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#75row-75 | 341 | 0 |
0 |
0 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#76row-76 | 346 | 1 |
0 |
0 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#77row-77 | 351 | 1 |
0 |
0 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#78row-78 | 355 | 1 |
0 |
0 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#79row-79 | 359 | 1 |
0 |
0 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#80row-80 | 364 | 0 |
1 |
0 |
0x00 |
0 |
1 |
0 |
0 |
0 |
1 |
#81row-81 | 369 | 0 |
1 |
0 |
0x01 |
0 |
1 |
0 |
0 |
0 |
0 |
#82row-82 | 373 | 0 |
1 |
0 |
0x80 |
0 |
1 |
0 |
1 |
0 |
0 |
#83row-83 | 377 | 0 |
1 |
0 |
0xFF |
0 |
1 |
0 |
1 |
0 |
0 |
#84row-84 | 382 | 1 |
1 |
0 |
0x00 |
0 |
1 |
1 |
0 |
0 |
1 |
#85row-85 | 387 | 1 |
1 |
0 |
0x01 |
0 |
1 |
1 |
0 |
0 |
0 |
#86row-86 | 391 | 1 |
1 |
0 |
0x80 |
0 |
1 |
1 |
1 |
0 |
0 |
#87row-87 | 395 | 1 |
1 |
0 |
0xFF |
0 |
1 |
1 |
1 |
0 |
0 |
#88row-88 | 400 | 0 |
1 |
0 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#89row-89 | 405 | 0 |
1 |
0 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#90row-90 | 409 | 0 |
1 |
0 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#91row-91 | 413 | 0 |
1 |
0 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#92row-92 | 418 | 1 |
1 |
0 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#93row-93 | 423 | 1 |
1 |
0 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#94row-94 | 427 | 1 |
1 |
0 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#95row-95 | 431 | 1 |
1 |
0 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#96row-96 | 436 | 0 |
0 |
1 |
0x00 |
0 |
1 |
0 |
0 |
0 |
1 |
#97row-97 | 441 | 0 |
0 |
1 |
0x01 |
0 |
1 |
0 |
0 |
0 |
0 |
#98row-98 | 445 | 0 |
0 |
1 |
0x80 |
0 |
1 |
0 |
1 |
0 |
0 |
#99row-99 | 449 | 0 |
0 |
1 |
0xFF |
0 |
1 |
0 |
1 |
0 |
0 |
#100row-100 | 454 | 1 |
0 |
1 |
0x00 |
0 |
1 |
0 |
0 |
1 |
1 |
#101row-101 | 459 | 1 |
0 |
1 |
0x01 |
0 |
1 |
0 |
0 |
1 |
0 |
#102row-102 | 463 | 1 |
0 |
1 |
0x80 |
0 |
1 |
0 |
1 |
1 |
0 |
#103row-103 | 467 | 1 |
0 |
1 |
0xFF |
0 |
1 |
0 |
1 |
1 |
0 |
#104row-104 | 472 | 0 |
0 |
1 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#105row-105 | 477 | 0 |
0 |
1 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#106row-106 | 481 | 0 |
0 |
1 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#107row-107 | 485 | 0 |
0 |
1 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#108row-108 | 490 | 1 |
0 |
1 |
0x00 |
1 |
1 |
1 |
0 |
1 |
1 |
#109row-109 | 495 | 1 |
0 |
1 |
0x01 |
1 |
1 |
1 |
0 |
1 |
0 |
#110row-110 | 499 | 1 |
0 |
1 |
0x80 |
1 |
1 |
1 |
1 |
1 |
0 |
#111row-111 | 503 | 1 |
0 |
1 |
0xFF |
1 |
1 |
1 |
1 |
1 |
0 |
#112row-112 | 508 | 0 |
1 |
1 |
0x00 |
0 |
1 |
0 |
0 |
0 |
1 |
#113row-113 | 513 | 0 |
1 |
1 |
0x01 |
0 |
1 |
0 |
0 |
0 |
0 |
#114row-114 | 517 | 0 |
1 |
1 |
0x80 |
0 |
1 |
0 |
1 |
0 |
0 |
#115row-115 | 521 | 0 |
1 |
1 |
0xFF |
0 |
1 |
0 |
1 |
0 |
0 |
#116row-116 | 526 | 1 |
1 |
1 |
0x00 |
0 |
1 |
1 |
0 |
1 |
1 |
#117row-117 | 531 | 1 |
1 |
1 |
0x01 |
0 |
1 |
1 |
0 |
1 |
0 |
#118row-118 | 535 | 1 |
1 |
1 |
0x80 |
0 |
1 |
1 |
1 |
1 |
0 |
#119row-119 | 539 | 1 |
1 |
1 |
0xFF |
0 |
1 |
1 |
1 |
1 |
0 |
#120row-120 | 544 | 0 |
1 |
1 |
0x00 |
1 |
1 |
1 |
0 |
0 |
1 |
#121row-121 | 549 | 0 |
1 |
1 |
0x01 |
1 |
1 |
1 |
0 |
0 |
0 |
#122row-122 | 553 | 0 |
1 |
1 |
0x80 |
1 |
1 |
1 |
1 |
0 |
0 |
#123row-123 | 557 | 0 |
1 |
1 |
0xFF |
1 |
1 |
1 |
1 |
0 |
0 |
#124row-124 | 562 | 1 |
1 |
1 |
0x00 |
1 |
1 |
1 |
0 |
1 |
1 |
#125row-125 | 567 | 1 |
1 |
1 |
0x01 |
1 |
1 |
1 |
0 |
1 |
0 |
#126row-126 | 571 | 1 |
1 |
1 |
0x80 |
1 |
1 |
1 |
1 |
1 |
0 |
#127row-127 | 575 | 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.