fengb/fundude

CPU reference

Closed this issue · 1 comments

fengb commented

Currently, we save a CPU reference by adhoc pointer arithmetic:

This is currently only done in the disassembly layer, but switching to function dispatch will require more references. This can easily become super unwieldy and confusing, especially when translating to Zig.

Theoretical replacement

Fixed array buffers. This forces offsets to be consistent, at the cost of some slight API discomfort (until we move to Zig?)

const Reg16: enum(u3) {
  AF = 0,
  BC = 1,
  DE = 2,
  HL = 3,
  SP = 4,
  PC = 5,
};

const Reg8: enum(u3) {
  F = 0,
  A = 1,
  C = 2,
  B = 3,
  E = 4,
  D = 5,
  L = 6,
  H = 7,
};

const Flags: packed struct {
  _pad: u4,
  C: bool,
  H: bool,
  N: bool,
  Z: bool,
};

const Cpu: packed union {
  r16: [6]u16, // candidate for enum array: https://github.com/ziglang/zig/issues/793
  r8: [8]u8,

  // Is this even a good idea? lol
  magic: packed struct {
    flags: Flags,
  }
};
fengb commented

Closed via ee736f7

Code isn't as nice as I hoped, but the output is just as performant and it's one step closer to unified instruction decoding: #12