Skip to main content

Flags

Description

The ALU supports four different flags: Sign Flag (SF), Zero Flag (ZF), Overflow Flag (OF), and Carry Flag (CF).
To perform 16-bit operations (e.g., adding two addresses) in the microarchitecture without overwriting the CF, the ALU implements a shadow carry flag (SCF). A control line (USE_SHADOW_CARRY) decides whether to use the CF or the SCF for the current operation. Not all operations should overwrite all flags. For example, moving a value from a register to the ACCU should not change any flags. To reflect this, the flags register allows to update no flags, all flags, or just the carry flag in the flags register. Additionally, the flags register can be written to the bus and set "raw" (SET_FLAGS_RAW control line), which will not calculate the flags but just use the least significant 4 bits from the shifter output directly.

Sign Flag

The sign flag (SF) is calculated by taking the most significant bit of the shifter output.

Zero Flag

The zero flag (ZF) is calculated by checking whether the output of the shifter is 0. If all bits of the shifter output equal 0, the zero flag is set. Otherwise, it is not set. This is implemented by first NOR-ing pairs of bits from the shifter output and then AND-ing all results.

Overflow Flag

Similar to the CF, the overflow flag (OF) is calculated by taking the overflow output of the ALU Hi chip.

Carry Flag

The carry flag (CF) is calculated by taking the carry output of the ALU HI chip (the ALU chip that calculates the most significant 4 bit) if the shifter is disabled. This implements what one would expect for most operations. If the shifter is enabled, the carry flag is set from the bit that is shifted out (the least significant bit of the input operand).

Shadow Carry Flag

An implementation detail of the arithmetic unit.

Implementation in Hardware

The flags are stored in a register (74-377 flagsRegister). To select whether flags should be set "raw" or from the calculated results, we use a 4-bit mux (74-157 rawMux). As it should be allowed to set just the carry flag, while leaving all other flags in-tact, we use another 4-bit mux (74-157 justCarryMmux). Selection of the correct carry (SCF or CF) is also implemented with a 4-bit mux (74-157 shadowMux). It handles the selection for both, reading and writing the correct carry.

To calculate the zero-flag, we use a bunch of NOR-gates (74-02 zeroFlagNOR) and AND-gates (74-08 zeroFlagAND). For bigger than 8-bit operations (e.g. 16-bit addition), it can be useful to set the ZF only if it is currently set. This is implemented by AND-ing the calculated zero flag with the last zero flag, or 1 if this is not desired. The selection between last zero flag and one is done by OR-ing (74-32 zeroFlagOR) the last zero flag with an active-low control line (ZERO_FLAG_AND). To allow the flags to be sent to the bus, there is a bus-transceiver (74-245 flagTransceiver).

Schematic

ALU flags schematic

Hint

The AND gate (74-08 clockAnd) is important to mention. This allows us to synchronously update all the internal registers (flags, latch, accu) and avoid race conditions.