| 1 |
#ifndef CPUREGS_HH |
| 2 |
#define CPUREGS_HH |
| 3 |
|
| 4 |
#include "serialize_meta.hh" |
| 5 |
|
| 6 |
#include <bit> |
| 7 |
#include <cassert> |
| 8 |
#include <cstdint> |
| 9 |
|
| 10 |
namespace openmsx { |
| 11 |
|
| 12 |
template<std::endian> struct z80regPair_8bit; |
| 13 |
template<> struct z80regPair_8bit<std::endian::little> { uint8_t l, h; }; |
| 14 |
template<> struct z80regPair_8bit<std::endian::big > { uint8_t h, l; }; |
| 15 |
union z80regPair { |
| 16 |
z80regPair_8bit<std::endian::native> b; |
| 17 |
uint16_t w; |
| 18 |
}; |
| 19 |
|
| 20 |
class CPURegs |
| 21 |
{ |
| 22 |
public: |
| 23 |
explicit CPURegs(bool r800) : Rmask(r800 ? 0xff : 0x7f) {} |
| 24 |
[[nodiscard]] uint8_t getA() const { return AF_.b.h; } |
| 25 |
[[nodiscard]] uint8_t getF() const { return AF_.b.l; } |
| 26 |
[[nodiscard]] uint8_t getB() const { return BC_.b.h; } |
| 27 |
[[nodiscard]] uint8_t getC() const { return BC_.b.l; } |
| 28 |
[[nodiscard]] uint8_t getD() const { return DE_.b.h; } |
| 29 |
[[nodiscard]] uint8_t getE() const { return DE_.b.l; } |
| 30 |
[[nodiscard]] uint8_t getH() const { return HL_.b.h; } |
| 31 |
[[nodiscard]] uint8_t getL() const { return HL_.b.l; } |
| 32 |
[[nodiscard]] uint8_t getA2() const { return AF2_.b.h; } |
| 33 |
[[nodiscard]] uint8_t getF2() const { return AF2_.b.l; } |
| 34 |
[[nodiscard]] uint8_t getB2() const { return BC2_.b.h; } |
| 35 |
[[nodiscard]] uint8_t getC2() const { return BC2_.b.l; } |
| 36 |
[[nodiscard]] uint8_t getD2() const { return DE2_.b.h; } |
| 37 |
[[nodiscard]] uint8_t getE2() const { return DE2_.b.l; } |
| 38 |
[[nodiscard]] uint8_t getH2() const { return HL2_.b.h; } |
| 39 |
[[nodiscard]] uint8_t getL2() const { return HL2_.b.l; } |
| 40 |
[[nodiscard]] uint8_t getIXh() const { return IX_.b.h; } |
| 41 |
[[nodiscard]] uint8_t getIXl() const { return IX_.b.l; } |
| 42 |
[[nodiscard]] uint8_t getIYh() const { return IY_.b.h; } |
| 43 |
[[nodiscard]] uint8_t getIYl() const { return IY_.b.l; } |
| 44 |
[[nodiscard]] uint8_t getPCh() const { return PC_.b.h; } |
| 45 |
[[nodiscard]] uint8_t getPCl() const { return PC_.b.l; } |
| 46 |
[[nodiscard]] uint8_t getSPh() const { return SP_.b.h; } |
| 47 |
[[nodiscard]] uint8_t getSPl() const { return SP_.b.l; } |
| 48 |
|
| 49 |
[[nodiscard]] uint16_t getAF() const { return AF_.w; } |
| 50 |
[[nodiscard]] uint16_t getBC() const { return BC_.w; } |
| 51 |
[[nodiscard]] uint16_t getDE() const { return DE_.w; } |
| 52 |
[[nodiscard]] uint16_t getHL() const { return HL_.w; } |
| 53 |
[[nodiscard]] uint16_t getAF2() const { return AF2_.w; } |
| 54 |
[[nodiscard]] uint16_t getBC2() const { return BC2_.w; } |
| 55 |
[[nodiscard]] uint16_t getDE2() const { return DE2_.w; } |
| 56 |
[[nodiscard]] uint16_t getHL2() const { return HL2_.w; } |
| 57 |
[[nodiscard]] uint16_t getIX() const { return IX_.w; } |
| 58 |
[[nodiscard]] uint16_t getIY() const { return IY_.w; } |
| 59 |
[[nodiscard]] uint16_t getPC() const { return PC_.w; } |
| 60 |
[[nodiscard]] uint16_t getSP() const { return SP_.w; } |
| 61 |
|
| 62 |
[[nodiscard]] uint8_t getIM() const { return IM_; } |
| 63 |
[[nodiscard]] uint8_t getI() const { return I_; } |
| 64 |
[[nodiscard]] uint8_t getR() const { return (R_ & Rmask) | (R2_ & ~Rmask); } |
| 65 |
[[nodiscard]] bool getIFF1() const { return IFF1_; } |
| 66 |
[[nodiscard]] bool getIFF2() const { return IFF2_; } |
| 67 |
[[nodiscard]] uint8_t getHALT() const { return HALT_; } |
| 68 |
|
| 69 |
void setA(uint8_t x) { AF_.b.h = x; } |
| 70 |
void setF(uint8_t x) { AF_.b.l = x; } |
| 71 |
void setB(uint8_t x) { BC_.b.h = x; } |
| 72 |
void setC(uint8_t x) { BC_.b.l = x; } |
| 73 |
void setD(uint8_t x) { DE_.b.h = x; } |
| 74 |
void setE(uint8_t x) { DE_.b.l = x; } |
| 75 |
void setH(uint8_t x) { HL_.b.h = x; } |
| 76 |
void setL(uint8_t x) { HL_.b.l = x; } |
| 77 |
void setA2(uint8_t x) { AF2_.b.h = x; } |
| 78 |
void setF2(uint8_t x) { AF2_.b.l = x; } |
| 79 |
void setB2(uint8_t x) { BC2_.b.h = x; } |
| 80 |
void setC2(uint8_t x) { BC2_.b.l = x; } |
| 81 |
void setD2(uint8_t x) { DE2_.b.h = x; } |
| 82 |
void setE2(uint8_t x) { DE2_.b.l = x; } |
| 83 |
void setH2(uint8_t x) { HL2_.b.h = x; } |
| 84 |
void setL2(uint8_t x) { HL2_.b.l = x; } |
| 85 |
void setIXh(uint8_t x) { IX_.b.h = x; } |
| 86 |
void setIXl(uint8_t x) { IX_.b.l = x; } |
| 87 |
void setIYh(uint8_t x) { IY_.b.h = x; } |
| 88 |
void setIYl(uint8_t x) { IY_.b.l = x; } |
| 89 |
void setPCh(uint8_t x) { PC_.b.h = x; } |
| 90 |
void setPCl(uint8_t x) { PC_.b.l = x; } |
| 91 |
void setSPh(uint8_t x) { SP_.b.h = x; } |
| 92 |
void setSPl(uint8_t x) { SP_.b.l = x; } |
| 93 |
|
| 94 |
void setAF(uint16_t x) { AF_.w = x; } |
| 95 |
void setBC(uint16_t x) { BC_.w = x; } |
| 96 |
void setDE(uint16_t x) { DE_.w = x; } |
| 97 |
void setHL(uint16_t x) { HL_.w = x; } |
| 98 |
void setAF2(uint16_t x) { AF2_.w = x; } |
| 99 |
void setBC2(uint16_t x) { BC2_.w = x; } |
| 100 |
void setDE2(uint16_t x) { DE2_.w = x; } |
| 101 |
void setHL2(uint16_t x) { HL2_.w = x; } |
| 102 |
void setIX(uint16_t x) { IX_.w = x; } |
| 103 |
void setIY(uint16_t x) { IY_.w = x; } |
| 104 |
void setPC(uint16_t x) { PC_.w = x; } |
| 105 |
void setSP(uint16_t x) { SP_.w = x; } |
| 106 |
|
| 107 |
void setIM(uint8_t x) { IM_ = x; } |
| 108 |
void setI(uint8_t x) { I_ = x; } |
| 109 |
void setR(uint8_t x) { R_ = x; R2_ = x; } |
| 110 |
void setIFF1(bool x) { IFF1_ = x; } |
| 111 |
void setIFF2(bool x) { IFF2_ = x; } |
| 112 |
void setHALT(bool x) { HALT_ = (HALT_ & ~1) | (x ? 1 : 0); } |
| 113 |
void setExtHALT(bool x) { HALT_ = (HALT_ & ~2) | (x ? 2 : 0); } |
| 114 |
|
| 115 |
void incR(uint8_t x) { R_ += x; } |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
void setCurrentEI() { |
| 142 |
prev_ |= 1; |
| 143 |
} |
| 144 |
|
| 145 |
void setCurrentLDAI() { |
| 146 |
prev_ |= 2; |
| 147 |
} |
| 148 |
|
| 149 |
void setCurrentCall() { |
| 150 |
prev_ |= 4; |
| 151 |
} |
| 152 |
|
| 153 |
void setCurrentPopRet() { |
| 154 |
prev_ |= 8; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
[[nodiscard]] bool prevWasEI() const { |
| 159 |
return (prev_ & (1 << 8)) != 0; |
| 160 |
} |
| 161 |
|
| 162 |
[[nodiscard]] bool prevWasLDAI() const { |
| 163 |
return (prev_ & (2 << 8)) != 0; |
| 164 |
} |
| 165 |
|
| 166 |
[[nodiscard]] bool prev2WasCall() const { |
| 167 |
return (prev_ & (4 << (2 * 8))) != 0; |
| 168 |
} |
| 169 |
|
| 170 |
[[nodiscard]] bool prevWasPopRet() const { |
| 171 |
return (prev_ & (8 << 8)) != 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
void endInstruction() { |
| 177 |
prev_ <<= 8; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void clearPrevious() { |
| 182 |
prev_ = 0; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void checkNoCurrentFlags() const { |
| 188 |
|
| 189 |
|
| 190 |
assert((prev_ & 0xF7) == 0); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
template<typename Archive> |
| 195 |
void serialize(Archive& ar, unsigned version); |
| 196 |
|
| 197 |
private: |
| 198 |
z80regPair PC_; |
| 199 |
z80regPair AF_, BC_, DE_, HL_; |
| 200 |
z80regPair AF2_, BC2_, DE2_, HL2_; |
| 201 |
z80regPair IX_, IY_, SP_; |
| 202 |
bool IFF1_, IFF2_; |
| 203 |
uint8_t HALT_ = 0; |
| 204 |
uint8_t IM_, I_; |
| 205 |
uint8_t R_, R2_; |
| 206 |
uint8_t Rmask; |
| 207 |
unsigned prev_; |
| 208 |
}; |
| 209 |
SERIALIZE_CLASS_VERSION(CPURegs, 3); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#if 0 |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
#endif |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
#endif |