namespace Spice86.Core.Emulator.CPU;
using Spice86.Shared.Utils;
using System.Collections.Frozen;
using System.Text;
/// Handles the CPU flag register.
public class Flags {
private static readonly IDictionary BitOnOffPerModel = new Dictionary {
// This is for the zet project tests
[CpuModel.ZET_86] = new(bitsAlwaysOn: [1], bitsAlwaysOff: [3, 5, 15]),
// Real CPUs
[CpuModel.INTEL_8086] = new(bitsAlwaysOn: [1, 12, 13, 14, 15], bitsAlwaysOff: [3, 5]),
// Since we dont handle IO privilege (12 / 13) and nested task (14), let's put them as always off
[CpuModel.INTEL_80286] = new(bitsAlwaysOn: [1], bitsAlwaysOff: [3, 5, 12, 13, 14, 15]),
// IOPL (12-13) and NT (14) are real, software-visible bits on the 386 (privilege checks and
// task-switch nesting both depend on them) so they are no longer forced off here.
[CpuModel.INTEL_80386] = new(bitsAlwaysOn: [1, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], bitsAlwaysOff: [3, 5, 15])
}.ToFrozenDictionary();
private record BitsOnOff {
public BitsOnOff(List bitsAlwaysOn, List bitsAlwaysOff) {
BitsAlwaysOn = BitMaskUtils.BitMaskFromBitList(bitsAlwaysOn);
BitsAlwaysOff = ~BitMaskUtils.BitMaskFromBitList(bitsAlwaysOff);
}
///
/// Or that into the register
///
public uint BitsAlwaysOn { get; }
///
/// And that into the register
///
public uint BitsAlwaysOff { get; }
}
///
/// The carry flag bitmask
///
public const ushort Carry = 0b00000000_00000001; //0
///
/// The parity flag bitmask
///
public const ushort Parity = 0b00000000_00000100; //2
///
/// The auxiliary flag bitmask
///
public const ushort Auxiliary = 0b00000000_00010000; //4
///
/// The zero flag bitmask
///
public const ushort Zero = 0b00000000_01000000; //6
///
/// The sign flag bitmask
///
public const ushort Sign = 0b00000000_10000000; //7
///
/// The trap flag bitmask
///
public const ushort Trap = 0b00000001_00000000; //8
///
/// The interrupt flag bitmask
///
public const ushort Interrupt = 0b00000010_00000000; //9
///
/// The direction flag bitmask
///
public const ushort Direction = 0b00000100_00000000; //10
///
/// The overflow flag bitmask
///
public const ushort Overflow = 0b00001000_00000000; //11
///
/// The I/O privilege level field bitmask (EFLAGS bits 12-13). A 2-bit field, not a single boolean
/// flag; use to read/write the numeric level.
///
public const uint IoPrivilegeLevelMask = 0b0011_0000_0000_0000; //12-13
///
/// The nested-task flag bitmask (EFLAGS bit 14). Set on entry to a task via a `CALL`/`INT` to a
/// task gate, so `IRET` knows to return via the back-link instead of a normal stack pop.
///
public const uint NestedTask = 0x0000_4000; //14
///
/// The virtual-8086 mode flag bitmask (EFLAGS bit 17). Only meaningful once protected mode is
/// enabled; toggled to enter/leave V86 mode via IRET or a task switch.
///
public const uint Virtual8086Mode = 0x0002_0000; //17
///
/// rflag mask to OR with flags, useful to compare with values emulated by DOSBox.
///
private uint _orFlagMask;
private uint _andFlagMask;
///
/// Initialises a new instance.
///
public Flags(CpuModel cpuModel) {
CpuModel = cpuModel;
FlagRegister = 0;
}
public CpuModel CpuModel {
get;
set {
field = value;
BitsOnOff bitsOnOff = BitOnOffPerModel[CpuModel];
_orFlagMask = bitsOnOff.BitsAlwaysOn;
_andFlagMask = bitsOnOff.BitsAlwaysOff;
}
}
///
/// Gets the value of a particular flag
///
/// The bitmask to apply on the flags register.
/// The value of a particular flag, as a boolean.
public bool GetFlag(ushort mask) {
return (FlagRegister & mask) == mask;
}
///
/// Sets the value of a particular flag
///
/// The bitmask to access a particular flag in the flags register.
/// The boolean value of the flag.
public void SetFlag(ushort mask, bool value) {
if (value) {
FlagRegister |= mask;
} else {
FlagRegister &= ~(uint)mask;
}
}
///
/// Gets the value of a particular flag outside the low 16 bits of the register (e.g. VM).
///
/// The bitmask to apply on the flags register.
/// The value of a particular flag, as a boolean.
public bool GetFlag(uint mask) {
return (FlagRegister & mask) == mask;
}
///
/// Sets the value of a particular flag outside the low 16 bits of the register (e.g. VM).
///
/// The bitmask to access a particular flag in the flags register.
/// The boolean value of the flag.
public void SetFlag(uint mask, bool value) {
if (value) {
FlagRegister |= mask;
} else {
FlagRegister &= ~mask;
}
}
///
/// Gets the 16-bit value of the flags register.
///
public ushort FlagRegister16 { get => (ushort)FlagRegister; set => FlagRegister = (FlagRegister & 0xFFFF0000) | (uint)value; }
///
/// Gets the 32-bit value of the flags register.
///
public uint FlagRegister {
get;
set => field = (value | _orFlagMask) & _andFlagMask;
}
///
public override bool Equals(object? obj) {
if (obj == this) {
return true;
}
if (obj is not Flags other) {
return false;
}
return FlagRegister == other.FlagRegister;
}
///
public override int GetHashCode() {
return (int)FlagRegister;
}
///
public override string ToString() {
return DumpFlags(FlagRegister);
}
///
/// Returns a string representation of all flags in the flags register.
///
/// The value of the flags register.
/// A string representation of all flags in the flags register.
public static string DumpFlags(uint flags) {
StringBuilder res = new StringBuilder();
res.Append(GetFlag(flags, Overflow, 'O'));
res.Append(GetFlag(flags, Direction, 'D'));
res.Append(GetFlag(flags, Interrupt, 'I'));
res.Append(GetFlag(flags, Trap, 'T'));
res.Append(GetFlag(flags, Sign, 'S'));
res.Append(GetFlag(flags, Zero, 'Z'));
res.Append(GetFlag(flags, Auxiliary, 'A'));
res.Append(GetFlag(flags, Parity, 'P'));
res.Append(GetFlag(flags, Carry, 'C'));
return res.ToString();
}
private static char GetFlag(uint flags, int mask, char representation) {
if ((flags & mask) == 0) {
return ' ';
}
return representation;
}
}