Path: blob/master/libsnes/bsnes/gameboy/cpu/core/registers.hpp
2 views
enum {1A, F, AF,2B, C, BC,3D, E, DE,4H, L, HL,5SP, PC,6};78enum {9ZF, NF, HF, CF,10};1112//register base class13//the idea here is to have all registers derive from a single base class.14//this allows construction of opcodes that can take any register as input or output,15//despite the fact that behind-the-scenes, special handling is done for eg: F, AF, HL, etc.16//registers can also be chained together: eg af = 0x0000 writes both a and f.17struct Register {18virtual operator unsigned() const = 0;19virtual unsigned operator=(unsigned x) = 0;20Register& operator=(const Register &x) { operator=((unsigned)x); return *this; }2122unsigned operator++(int) { unsigned r = *this; operator=(*this + 1); return r; }23unsigned operator--(int) { unsigned r = *this; operator=(*this - 1); return r; }24unsigned operator++() { return operator=(*this + 1); }25unsigned operator--() { return operator=(*this - 1); }2627unsigned operator |=(unsigned x) { return operator=(*this | x); }28unsigned operator ^=(unsigned x) { return operator=(*this ^ x); }29unsigned operator &=(unsigned x) { return operator=(*this & x); }3031unsigned operator<<=(unsigned x) { return operator=(*this << x); }32unsigned operator>>=(unsigned x) { return operator=(*this >> x); }3334unsigned operator +=(unsigned x) { return operator=(*this + x); }35unsigned operator -=(unsigned x) { return operator=(*this - x); }36unsigned operator *=(unsigned x) { return operator=(*this * x); }37unsigned operator /=(unsigned x) { return operator=(*this / x); }38unsigned operator %=(unsigned x) { return operator=(*this % x); }39};4041struct Register8 : Register {42uint8 data;43operator unsigned() const { return data; }44unsigned operator=(unsigned x) { return data = x; }45};4647struct RegisterF : Register {48bool z, n, h, c;49operator unsigned() const { return (z << 7) | (n << 6) | (h << 5) | (c << 4); }50unsigned operator=(unsigned x) { z = x & 0x80; n = x & 0x40; h = x & 0x20; c = x & 0x10; return *this; }51bool& operator[](unsigned r) {52static bool* table[] = { &z, &n, &h, &c };53return *table[r];54}55};5657struct Register16 : Register {58uint16 data;59operator unsigned() const { return data; }60unsigned operator=(unsigned x) { return data = x; }61};6263struct RegisterAF : Register {64Register8 &hi;65RegisterF &lo;66operator unsigned() const { return (hi << 8) | (lo << 0); }67unsigned operator=(unsigned x) { hi = x >> 8; lo = x >> 0; return *this; }68RegisterAF(Register8 &hi, RegisterF &lo) : hi(hi), lo(lo) {}69};7071struct RegisterW : Register {72Register8 &hi, &lo;73operator unsigned() const { return (hi << 8) | (lo << 0); }74unsigned operator=(unsigned x) { hi = x >> 8; lo = x >> 0; return *this; }75RegisterW(Register8 &hi, Register8 &lo) : hi(hi), lo(lo) {}76};7778struct Registers {79Register8 a;80RegisterF f;81RegisterAF af;82Register8 b;83Register8 c;84RegisterW bc;85Register8 d;86Register8 e;87RegisterW de;88Register8 h;89Register8 l;90RegisterW hl;91Register16 sp;92Register16 pc;9394Register& operator[](unsigned r) {95static Register* table[] = { &a, &f, &af, &b, &c, &bc, &d, &e, &de, &h, &l, &hl, &sp, &pc };96return *table[r];97}9899Registers() : af(a, f), bc(b, c), de(d, e), hl(h, l) {}100} r;101102103