# Flags and undocumented behaviour The judge here is WSCPU (FluBBa's WSCpuTest v0.7.5 source at `245b12c`, release v0.7.1 run in this phase): a ROM that computes each instruction's result and flags for many inputs and compares them with its own expected values, printing "Ok" on a real WonderSwan and stopping at the first difference on an emulator (its README). Its expected values are its author's reading of hardware, so it is tier 3 (a community test that passes on real hardware), not a capture. The repository has no licence: its rules below are restated as facts, never copied, and the ROM is run, not redistributed. ## Rules WSCPU states (summarised) - **Undefined flags are always written.** "The flags marked as Undefined in the manual are always modified by the instructions": no undefined flag keeps its old value. - **Logic ops** (AND, OR, XOR, TEST): AF, CF, OF cleared; PF, SF, ZF from the result. - **INC/DEC**: CF kept; the rest as ADD/SUB 1. - **NEG**: exactly SUB with a destination of 0 (so NEG 0 leaves OF and AF clear). - **Rotates** (ROL, ROR, RCL, RCR): AF, PF, SF, ZF kept; OF from the result's top bits; a count that is 0 after masking with 1Fh leaves CF unchanged. - **Shifts** (SHL, SHR, SAR): AF cleared; PF, SF, ZF from the result; the count masked to 5 bits; a masked count of 0 leaves CF unchanged. - **Unsigned multiply** (MULU): AF, PF, SF cleared; CF = OF = high half non-zero; **ZF always set on SPHINX/SPHINX2 (Color, Crystal) and always cleared on ASWAN (mono)**: the one difference between the SoCs the test knows of. - **Signed multiply** (MUL): AF, PF, SF cleared; ZF set; CF = OF = result does not fit. - **Unsigned divide**: AF, PF, SF cleared; for 16/8, CF and OF are left from the last multiplication; ZF set when the remainder is zero and bit 0 of the quotient is set; on a divide exception ZF follows a formula of the dividend and divisor that the README gives, and AW (and DW for 32/16) is not written. - **Signed divide** 16/8: 8000h / 00h gives 0081h, not an exception. - **Undefined opcodes**: mostly 1-byte NOPs; D8-DF (FPO1) 2-byte NOPs (WSTIME adds 0Fh and FFh /7 to the NOPs). ## Machine rows this creates | Behaviour | ASWAN (WS, PCV2) | SPHINX, SPHINX2 (WSC, SC) | Evidence | | --- | --- | --- | --- | | ZF after MULU | cleared | set | WSCPU README (TEST ROM, verdicts on the author's units) | The core takes the SoC as a constructor argument (`V30MZ(soc="aswan" | "sphinx")`) for this row; the board passes it from its machine table. ## What the PUM says Section 1.3: undefined flags may differ from the V30HL and V30MX, especially after multiply and divide; the decimal adjust instructions CVTDB/CVTBD correct the second byte (the 80186's AAD/AAM base byte is honoured). The V20/V30's removed instructions give "an undefined result"; WSCPU and WSTIME say they are NOPs on hardware (C3: the core follows the tests and tags it TEST ROM over DOC). ## MAME 0.285 fails WSCPU Run in this phase (mame-oracle.md): after "Test All", the last screen shows the SUB and CMP byte groups passed (earlier groups scrolled away) and the run stopped at **NEG bytes, input 00h**: expected flags F246h, MAME's FA56h, a difference of 0810h (OF and AF set where the test expects them clear). So MAME's v30mz flags are not a judge, and a lockstep against MAME must compare flags only where WSCPU agrees with MAME, or not at all (validation.md). ## Unknowns - U4: whether the ASWAN/SPHINX MULU difference is the only SoC difference in the CPU. - U5: undefined-flag results for instruction forms WSCPU does not cover (BCD adjusts with unusual inputs, string instructions interrupted mid-repeat, BOUND). - U6: the value of FLAGS bit 15 (MD): reset leaves F002h, so bits 12-15 read as 1.