--- nono/m88xx0/m88100ops.cpp 2026/04/29 17:04:57 1.1.1.9 +++ nono/m88xx0/m88100ops.cpp 2026/04/29 17:05:26 1.1.1.15 @@ -5,86 +5,51 @@ // #include "m88100acc.h" +#include "bitops.h" #include #include // 命令のフィールド取り出し -#define FLD_D m88100opf_D(opX) -#define FLD_S1 m88100opf_S1(opX) -#define FLD_S2 m88100opf_S2(opX) -#define FLD_CR m88100opf_CR(opX) -#define B5 m88100opf_B5(opX) -#define M5 m88100opf_M5(opX) -#define W5 m88100opf_W5(opX) -#define O5 m88100opf_O5(opX) -#define IMM16 m88100opf_IMM16(opX) -#define VEC9 m88100opf_VEC9(opX) +#define FLD_D m88100opf_D(reg.opX) +#define FLD_S1 m88100opf_S1(reg.opX) +#define FLD_S2 m88100opf_S2(reg.opX) +#define FLD_CR m88100opf_CR(reg.opX) +#define B5 m88100opf_B5(reg.opX) +#define M5 m88100opf_M5(reg.opX) +#define W5 m88100opf_W5(reg.opX) +#define O5 m88100opf_O5(reg.opX) +#define IMM16 m88100opf_IMM16(reg.opX) +#define VEC9 m88100opf_VEC9(reg.opX) // 符号拡張・シフト済みのディスプレースメント // XXX: (負数の右シフト)GCC only -#define D16 (((int32)(int16)opX) << 2) -#define D26 (((int32)(opX << 6) >> 4)) -#define IsCO ((opX >> 8) & 1) -#define IsCI ((opX >> 9) & 1) +#define D16 (((int32)(int16)reg.opX) << 2) +#define D26 (((int32)(reg.opX << 6) >> 4)) +#define IsCO ((reg.opX >> 8) & 1) +#define IsCI ((reg.opX >> 9) & 1) // レジスタアクセス -#define rD (r[FLD_D]) -#define rS1 (r[FLD_S1]) -#define rS2 (r[FLD_S2]) +#define rD (reg.r[FLD_D]) +#define rS1 (reg.r[FLD_S1]) +#define rS2 (reg.r[FLD_S2]) // ダブルワードの2ワード目 #define FLD_D2 ((FLD_D + 1) & 0x1f) -#define rD2 (r[FLD_D2]) +#define rD2 (reg.r[FLD_D2]) // FP フィールド -#define FP_T1 m88100opf_FP_T1(opX) -#define FP_T2 m88100opf_FP_T2(opX) -#define FP_TD m88100opf_FP_TD(opX) - -// XXX: 暫定 -static float -u2f(uint32 u) -{ - float v; - memcpy(&v, &u, 4); - return v; -} - -static double -u2d(uint64 u) -{ - double v; - memcpy(&v, &u, 8); - return v; -} - -static uint32 -f2u(float v) -{ - uint32 u; - memcpy(&u, &v, 4); - return u; -} - -static uint64 -d2u(double v) -{ - uint64 u; - memcpy(&u, &v, 8); - return u; -} +#define FP_T1 m88100opf_FP_T1(reg.opX) +#define FP_T2 m88100opf_FP_T2(reg.opX) +#define FP_TD m88100opf_FP_TD(reg.opX) +// XXX t(サイズ)が原因で Reserved Operand になった時の S1, S2 はどうなる? #define FP_GET(v, t, n) \ do { \ if (t == 0) { \ - v = u2f(r[n]); \ + v = u2f(reg.r[n]); \ } else if (t == 1) { \ - if (n == 31) { \ - FPException(M88K_FPEXCEP_FUNIMP); \ - return; \ - } \ - v = u2d(((uint64)r[n] << 32) | r[n + 1]); \ + v = u2d(((uint64)reg.r[n] << 32) | reg.r[(n + 1) & 31]); \ } else { \ - FPException(M88K_FPEXCEP_FROP); \ + FPPreciseException(FPECR_FROP); \ return; \ } \ } while (0) @@ -92,31 +57,28 @@ do { \ #define FP_SET(t, n, v) \ do { \ if (t == 0) { \ - r[n] = f2u(v); \ + reg.r[n] = f2u(v); \ } else if (t == 1) { \ - if (n == 31) { \ - FPException(M88K_FPEXCEP_FUNIMP); \ - return; \ - } \ uint64 tmp = d2u(v); \ - r[n] = tmp >> 32; \ - r[n + 1] = (uint32)tmp; \ + reg.r[n] = tmp >> 32; \ + if (__predict_true(n != 31)) \ + reg.r[n + 1] = (uint32)tmp; \ } else { \ - FPException(M88K_FPEXCEP_FROP); \ + FPPreciseException(FPECR_FROP); \ return; \ } \ } while (0) -// ブランチ命令入り口 -inline void -m88kcpu::EnterBranch(uint32 toaddr) +// ブランチ命令 +void +MPU88xx0Device::DoBranch(uint32 toaddr, bool next_exec) { // ジャンプ先アドレスの下位2bitはマスクされる toaddr &= ~3; // ブランチヒストリに登録しつつ、 // ブランチヒストリからブランチ状況を取得 - auto e = brhist.AddEntry(xip | (IsSuper() ? 1 : 0), toaddr, opX); - fip = toaddr; + auto e = brhist.AddEntry(reg.xip | (IsSuper() ? 1 : 0), toaddr, reg.opX); + reg.fip = toaddr; // STOP 検出。 // m88k には STOP 状態 (何もせず割り込みだけ待つ状態。m68k の STOP 命令 @@ -147,14 +109,14 @@ m88kcpu::EnterBranch(uint32 toaddr) // e.count == 2 のときだけ STOP の検証をすれば良い。 // d はブランチ間の命令数 - uint32 d = (xip - toaddr) / sizeof(uint32); + uint32 d = (reg.xip - toaddr) / sizeof(uint32); // 遅延ブランチなら 1 命令余分に実行するはずなので 1 足す - if ((opX & (1 << 26)) != 0) { + if (next_exec) { d++; } // ブランチ間の命令がすべて NOP なら STOP ということにする。 if (nop_counter == d) { - atomic_reqflag |= CPU_REQ_STOP; + ChangeState(CPU_STATE_STOP); } // OpenBSD 6.6 では @@ -167,37 +129,12 @@ m88kcpu::EnterBranch(uint32 toaddr) } // STOP ではなかったら、あとは放置していい } -} - -// 通常(即時)ブランチ出口 -inline void -m88kcpu::ExitBranch() -{ - // re-fetch branched instruction - fetch(); -} -// 遅延ブランチ出口 -// 速度を稼ぐため、ビット反転だけでこれを区別しているので、 -// 同じ op 中で複数回呼び出さないこと。 -inline void -m88kcpu::ExitDelayBranch() -{ - opF ^= DELAYSLOT; -} - -// ロードストアユニットの usr の処理。 -// 正常なら true を返す (ので呼び出し側は実行継続)、 -// 例外が起きたら処理して false を返すので呼び出し側は即リターンすること。 -inline bool -m88kcpu::ldst_usr(uint32& usr) -{ - usr = (opX & (1 << 8)) ? DM_USR : 0; - if (IsUser() && usr) { - Exception(M88K_EXCEP_PRIV); - return false; + if (next_exec == false) { + // 通常(即時)ブランチ出口 + // re-fetch branched instruction + fetch(); } - return true; } // MEMO: ロードストアでの usr の処理で、CMMU に対して @@ -211,43 +148,39 @@ m88kcpu::ldst_usr(uint32& usr) // ロードストアユニットの scale の処理。addr を返す。 inline uint32 -m88kcpu::ldst_scale(uint32 size) +MPU88xx0Device::ldst_scale(uint32 size) { - if ((opX & (1 << 9)) != 0) { + if ((reg.opX & (1 << 9)) != 0) { return rS1 + rS2 * size; } else { return rS1 + rS2; } } -// ロードストアユニットの align の処理。 -// 正常なら true を返す (ので呼び出し側は実行継続)、 -// 例外が起きたら処理して false を返すので呼び出し側は即リターンすること。 -inline bool -m88kcpu::ldst_align(uint32 size, uint32& addr) -{ - if ((addr & (size - 1)) != 0) { - if (IsMXM()) { - addr &= ~(size - 1); - } else { - Exception(M88K_EXCEP_MISALIGNED); - return false; - } - } - return true; -} - -// ロードストアユニットの usr と scale の処理 (呼び出し用マクロ) +// ロードストアユニットの usr と scale の処理。 +// 正常な場合は usr と addr を更新する。 +// 例外の場合は例外を処理してこのマクロが展開してある関数から return する。 #define LDST_USR_SCALE(size) do { \ - if (ldst_usr(usr) == false) \ + usr = (reg.opX & (1U << 8)) ? DM_USR : 0; \ + if (IsUser() && usr) { \ + Exception(EXCEP_PRIV); \ return; \ + } \ addr = ldst_scale(size); \ } while (0) -// ロードストアユニットの align の処理 (呼び出し用マクロ) +// ロードストアユニットの align の処理。 +// 正常な場合は addr を更新する可能性がある。 +// 例外の場合は例外を処理してこのマクロが展開されてる関数から return する。 #define LDST_ALIGN(size) do { \ - if (ldst_align(size, addr) == false) \ - return; \ + if ((addr & (size - 1)) != 0) { \ + if (IsMXM()) { \ + addr &= ~(size - 1); \ + } else { \ + Exception(EXCEP_MISALIGNED); \ + return; \ + } \ + } \ } while (0) // ロードストアユニットのまとめたもの @@ -256,6 +189,18 @@ m88kcpu::ldst_align(uint32 size, uint32& LDST_ALIGN(size); \ } while (0) +// .usr 開始。 +#define ENTER_USR() do { \ + if (__predict_false(usr)) \ + cmmu[1]->SetSuper(false); \ +} while (0) + +// .usr 終了。 +#define LEAVE_USR() do { \ + if (__predict_false(usr)) \ + cmmu[1]->SetSuper(true); \ +} while (0) + // 幅規制付き算術左シフト static inline uint32 ASL(uint32 a, int n) @@ -273,7 +218,7 @@ ASR(uint32 a, int n) { if (n <= 0) return a; - if (n >= 32) + if (n >= 31) return (int32)a < 0 ? -1 : 0; // XXX: 負数の右シフト return (int32)a >> n; @@ -290,20 +235,6 @@ LSR(uint32 a, int n) return a >> n; } - -// 右ローテート -static inline uint32 -ROR(uint32 a, int n) -{ - // TODO: 各種コンパイラごとにビルトインがあったりなかったりするらしい - n &= 31; - if (n == 0) { - return a; - } else { - return (a >> n) | (a << (32 - n)); - } -} - // add でのオーバーフロー条件 static inline bool isovf_add(uint32 s1, uint32 s2, uint32 res) @@ -318,10 +249,10 @@ isovf_sub(uint32 s1, uint32 s2, uint32 r } // bcnd のやつ -static inline int +static inline uint acc_cnd(uint32 r) { - int rv; + uint rv; if ((r & 0x80000000) != 0) { if (r == 0x80000000) { rv = 0x8; @@ -351,6 +282,16 @@ acc_bf(int w, int ofs) return m << ofs; } +// m88100 FPCR の RndMode (0..3) から の丸めモード値への変換。 +// 大抵ここに渡す前に 3 で AND しているはずなので範囲外は考慮しない。 +static int rm_to_round[4] = { + FE_TONEAREST, + FE_TOWARDZERO, + FE_DOWNWARD, + FE_UPWARD, +}; + + // ここから命令定義 // 000000_DDDDDSSSSS_nnnnnn_nnnnnnnnnn @@ -365,7 +306,7 @@ OP_DEF(xmem_bu_imm) AddCycle(4); // Table.7-3 lastaddr = addr; - tmp = cmmu[1].xmem_8(addr, rD & 0xff); + tmp = cmmu[1]->xmem_1(addr, rD & 0xff); if ((int64)tmp < 0) { XmemDataException(addr, DM_BU | DM_LOCK); return; @@ -387,7 +328,7 @@ OP_DEF(xmem_w_imm) AddCycle(4); // Table.7-3 lastaddr = addr; - tmp = cmmu[1].xmem_32(addr, rD); + tmp = cmmu[1]->xmem_4(addr, rD); if ((int64)tmp < 0) { XmemDataException(addr, DM_W | DM_LOCK); return; @@ -409,7 +350,7 @@ OP_DEF(ld_hu_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - data = cmmu[1].load_16(addr); + data = cmmu[1]->load_2(addr); if ((int64)data < 0) { ReadDataException32(addr, DM_HU); return; @@ -431,7 +372,7 @@ OP_DEF(ld_bu_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - data = cmmu[1].load_8(addr); + data = cmmu[1]->load_1(addr); if ((int64)data < 0) { ReadDataException32(addr, DM_BU); return; @@ -453,13 +394,13 @@ OP_DEF(ld_d_imm) AddCycle(4); // Table.7-3 lastaddr = addr; - data1 = cmmu[1].load_32(addr); + data1 = cmmu[1]->load_4(addr); if ((int64)data1 < 0) { ReadDataException64(addr, DM_D1); return; } addr += 4; - data2 = cmmu[1].load_32(addr); + data2 = cmmu[1]->load_4(addr); if ((int64)data2 < 0) { ReadDataException64(addr, DM_D2); return; @@ -467,7 +408,7 @@ OP_DEF(ld_d_imm) rD = data1; rD2 = data2; - r[0] = 0; + reg.r[0] = 0; } // 000101_DDDDDSSSSS_nnnnnn_nnnnnnnnnn @@ -482,7 +423,7 @@ OP_DEF(ld_w_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - data = cmmu[1].load_32(addr); + data = cmmu[1]->load_4(addr); if ((int64)data < 0) { ReadDataException32(addr, DM_W); return; @@ -504,7 +445,7 @@ OP_DEF(ld_h_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - data = cmmu[1].load_16(addr); + data = cmmu[1]->load_2(addr); if ((int64)data < 0) { ReadDataException32(addr, DM_H); return; @@ -526,7 +467,7 @@ OP_DEF(ld_b_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - data = cmmu[1].load_8(addr); + data = cmmu[1]->load_1(addr); if ((int64)data < 0) { ReadDataException32(addr, DM_B); return; @@ -548,13 +489,13 @@ OP_DEF(st_d_imm) AddCycle(1 + 4); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_32(addr, rD); + rv = cmmu[1]->store_4(addr, rD); if ((int64)rv < 0) { WriteDataException64(addr, DM_D1); return; } addr += 4; - rv = cmmu[1].store_32(addr, rD2); + rv = cmmu[1]->store_4(addr, rD2); if ((int64)rv < 0) { WriteDataException64(addr, DM_D2); return; @@ -573,7 +514,7 @@ OP_DEF(st_w_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_32(addr, rD); + rv = cmmu[1]->store_4(addr, rD); if ((int64)rv < 0) { WriteDataException32(addr, DM_W); return; @@ -592,7 +533,7 @@ OP_DEF(st_h_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_16(addr, rD & 0xffff); + rv = cmmu[1]->store_2(addr, rD & 0xffff); if ((int64)rv < 0) { WriteDataException32(addr, DM_H); return; @@ -611,7 +552,7 @@ OP_DEF(st_b_imm) AddCycle(3); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_8(addr, rD & 0xff); + rv = cmmu[1]->store_1(addr, rD & 0xff); if ((int64)rv < 0) { WriteDataException32(addr, DM_B); return; @@ -741,7 +682,7 @@ OP_DEF(divu_imm) AddCycle(38); // Table.7-6 (ただしざっくり) if (IMM16 == 0) { - Exception(M88K_EXCEP_INT_DIV); + Exception(EXCEP_INT_DIV); return; } if (FLD_D == 0) @@ -770,7 +711,7 @@ OP_DEF(add_imm) uint32 imm = IMM16; uint32 res = rS1 + imm; if (isovf_add(rS1, imm, res)) { - Exception(M88K_EXCEP_INT_OVF); + Exception(EXCEP_INT_OVF); return; } if (FLD_D == 0) @@ -784,7 +725,7 @@ OP_DEF(sub_imm) uint32 imm = IMM16; uint32 res = rS1 - imm; if (isovf_sub(rS1, imm, res)) { - Exception(M88K_EXCEP_INT_OVF); + Exception(EXCEP_INT_OVF); return; } if (FLD_D == 0) @@ -803,7 +744,7 @@ OP_DEF(div_imm) AddCycle(38); // Table.7-6 (ただしざっくり) if ((int32)rS1 < 0 || IMM16 == 0) { - Exception(M88K_EXCEP_INT_DIV); + Exception(EXCEP_INT_DIV); return; } if (FLD_D == 0) @@ -823,18 +764,19 @@ OP_DEF(cmp_imm) OP_DEF(ldcr) { if (IsUser()) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } - if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 compatible - Exception(M88K_EXCEP_UNIMPL_OP); + if (FLD_CR > countof(reg.cr)) { + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 + Exception(EXCEP_UNIMPL_OP); return; } if (FLD_D == 0) return; - rD = cr[FLD_CR]; + rD = reg.cr[FLD_CR]; } // 100000_DDDDDzzzzz_01001n_nnnnnzzzzz @@ -842,46 +784,48 @@ OP_DEF(fldcr) { int n = FLD_CR; - if (IsUser()) { - // fcr62, 63 はユーザモードでアクセスできる - // それ以外はスーパバイザ用。 - if (n < 62) { - Exception(M88K_EXCEP_PRIV); - return; - } - } + // 88100 88110(Only for reference) + // Super User Super User + // ----- ----- ----- ----- + // FCR0 read FPRV read FPRV + // FCR1..8 read FPRV FUNIMP FPRV + // FCR9..61 read0 FPRV FUNIMP FPRV + // FCR62,63 read read read read - if (8 < n && n < 62) { - // 未実装 FCR - // 88100 undocumented, - // 88110 compatible - Exception(M88K_EXCEP_UNIMPL_OP); - return; - } - - // fcr は 62,63 の配置に細工がしてある if (n >= 62) { n = n - 62 + 9; + } else { + if (IsUser()) { + FPPreciseException(FPECR_FPRV); + return; + } + if (n > 8) { + rD = 0; + return; + } } if (FLD_D == 0) return; - rD = fcr[n]; + + // XXX 内部がバグってなければ読み出す時にマスクする必要はないが、一応 + rD = reg.fcr[n] & reg.fcr_mask[n]; } // 100000_zzzzzSSSSS_10000n_nnnnnsssss OP_DEF(stcr) { if (IsUser()) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } - if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 unimpl_op - Exception(M88K_EXCEP_UNIMPL_OP); + if (FLD_CR > countof(reg.cr)) { + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 + Exception(EXCEP_UNIMPL_OP); return; } - cr[FLD_CR] = rS1; + reg.cr[FLD_CR] = rS1; if (FLD_CR == 1) { SetPSR(); } @@ -892,27 +836,34 @@ OP_DEF(fstcr) { int n = FLD_CR; - if (IsUser()) { - // fcr62, 63 はユーザモードでアクセスできる - // それ以外はスーパバイザ用。 - if (n < 62) { - Exception(M88K_EXCEP_PRIV); + // 88100 88110(Only for reference) + // Super User Super User + // ----- ----- ----- ----- + // FCR0 write FPRV write FPRV + // FCR1..8 noop FPRV FUNIMP FPRV + // FCR9..61 noop FPRV FUNIMP FPRV + // FCR62,63 write write write write + + if (n >= 62) { + n = n - 62 + 9; + } else { + if (IsUser()) { + FPPreciseException(FPECR_FPRV); + return; + } + // FCR1..8(読み込み専用レジスタ)、9..61(未実装レジスタ) ともに noop + if (n > 0) { return; } } - if (8 < n && n < 62) { - // 未実装 FCR - // 88100 null op - // 88110 fpu-unimpl - return; - } + reg.fcr[n] = rS1 & reg.fcr_mask[n]; - // fcr は 62,63 の配置に細工がしてある - if (n >= 62) { - n = n - 62 + 9; + // FPCR が変更されたら、ホストの丸めモードも更新 + if (n == 10) { + uint32 rm = (reg.fpcr >> 14) & 3; + std::fesetround(rm_to_round[rm]); } - fcr[n] = rS1; } // 100000_DDDDDSSSSS_11000n_nnnnnsssss @@ -921,54 +872,64 @@ OP_DEF(xcr) uint32 tmp; if (IsUser()) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } - if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 unimpl_op - Exception(M88K_EXCEP_UNIMPL_OP); + if (FLD_CR > countof(reg.cr)) { + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 + Exception(EXCEP_UNIMPL_OP); return; } tmp = rS1; - rD = cr[FLD_CR]; - cr[FLD_CR] = tmp; + rD = reg.cr[FLD_CR]; + reg.cr[FLD_CR] = tmp; if (FLD_CR == 1) { SetPSR(); } - r[0] = 0; + reg.r[0] = 0; } // 100000_DDDDDSSSSS_11001n_nnnnnsssss OP_DEF(fxcr) { - uint32 tmp; - int n = FLD_CR; + uint n = FLD_CR; - if (IsUser()) { - // fcr62, 63 はユーザモードでアクセスできる - // それ以外はスーパバイザ用。 - if (n < 62) { - Exception(M88K_EXCEP_PRIV); - return; - } - } + // 88100 88110(Only for reference) + // Super User Super User + // ----- ----- ----- ----- + // FCR0 xchg FPRV xchg FPRV + // FCR1..8 read FPRV FUNIMP FPRV + // FCR9..61 read0 FPRV FUNIMP FPRV + // FCR62,63 xchg xchg xchg xchg - if (8 < n && n < 62) { - // 未実装 FCR - // 88100 null op - // 88110 fpu-unimpl + if (IsUser() && n < 62) { + FPPreciseException(FPECR_FPRV); return; } - // fcr は 62,63 の配置に細工がしてある - if (n >= 62) { + if (n == 0) { + goto exchange; + } else if (__predict_false(n < 9)) { + // FCR1..8 は読み込み専用レジスタなので、読み出す側のみ + rD = reg.fcr[n] & reg.fcr_mask[n]; + reg.r[0] = 0; + return; + } else if (__predict_false(n < 62)) { + // FCR9..61 は未実装レジスタなので、0 が読み出せるのみ + rD = 0; + return; + } else { n = n - 62 + 9; } - tmp = rS1; - rD = fcr[n]; - fcr[n] = tmp; - r[0] = 0; + + exchange: + uint32 tmp = rS1; + rD = reg.fcr[n] & reg.fcr_mask[n]; + reg.fcr[n] = tmp & reg.fcr_mask[n]; + + reg.r[0] = 0; } // 100001_DDDDDSSSSS_00000n_nnnnnsssss @@ -979,7 +940,7 @@ OP_DEF(fmul) double s2; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } @@ -993,11 +954,11 @@ OP_DEF(flt) { // 暫定 if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } - FP_SET(FP_TD, FLD_D, (double)rS2); + FP_SET(FP_TD, FLD_D, (double)(int32)rS2); } @@ -1009,7 +970,7 @@ OP_DEF(fadd) double s2; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } @@ -1026,7 +987,7 @@ OP_DEF(fsub) double s2; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } @@ -1038,80 +999,111 @@ OP_DEF(fsub) // 100001_DDDDDSSSSS_00111n_nnnnnsssss OP_DEF(fcmp) { - // 暫定 double s1; double s2; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } FP_GET(s1, FP_T1, FLD_S1); FP_GET(s2, FP_T2, FLD_S2); - rD = acc_fcmp(s1, s2); + + // XXX テストが出来てないので Inf と非正規化数は未対応 + // 非正規化数もここのはず + if (std::isnan(s1) || std::isnan(s2) || + std::isinf(s1) || std::isinf(s2) ) + { + FPPreciseException(FPECR_FROP, s1, s2); + return; + } + + // + // 31 .. 12 11 10 9 8 7 6 5 4 3 2 1 0 + // +---- ---+----+----+----+----+----+----+----+----+----+----+----+----+ + // | 0 | ob | in | ib | ou | ge | lt | le | gt | ne | eq | cp | nc | + // +---- ---+----+----+----+----+----+----+----+----+----+----+----+----+ + + uint32 res = 0; + if (s2 >= 0) { + if (s1 <= 0 || s2 <= s1) res |= 0x0800; // ob + if (0 < s1 && s1 < s2) res |= 0x0400; // in + if (0 <= s1 && s1 <= s2) res |= 0x0200; // ib + if (s1 < 0 || s2 < s1) res |= 0x0100; // ou + } + if (s1 >= s2) res |= 0x0080; // ge + if (s1 < s2) res |= 0x0040; // lt + if (s1 <= s2) res |= 0x0020; // le + if (s1 > s2) res |= 0x0010; // gt + if (s1 != s2) res |= 0x0008; // ne + if (s1 == s2) res |= 0x0004; // eq + // cp; if and only if the two operands are comparable + if (!std::isnan(s1) && !std::isnan(s2)) { + res |= 0x0002; + } + // nc; if, and only if, the two operands are not comparable + if (std::isnan(s1) || std::isnan(s2)) { + res |= 0x0001; + } + + rD = res; } -// 100001_DDDDD00000_010010_0nn00sssss -OP_DEF(int) -{ - // 暫定 - double s2; +// int, nint, trnc 命令の共通部 +void +MPU88xx0Device::ops_int(int rndmode) +{ + double src; + double res; + int exponent; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } - FP_GET(s2, FP_T2, FLD_S2); + if (FP_T2 == 0) { + float f; + FP_GET(f, FP_T2, FLD_S2); + src = f; + } else { + FP_GET(src, FP_T2, FLD_S2); + } + + int saved_rndmode = std::fegetround(); - int mode = (fpcr >> 14) & 3; - switch (mode) { - case 0: - std::fesetround(FE_TONEAREST); - rD = (uint32)(int32)std::nearbyint(s2); - break; - case 1: - rD = (uint32)(int32)std::trunc(s2); - break; - case 2: - rD = (uint32)(int32)std::floor(s2); - break; - case 3: - rD = (uint32)(int32)std::ceil(s2); - break; + std::fesetround(rndmode); + res = std::nearbyint(src); + std::fesetround(saved_rndmode); + + exponent = 0; + std::frexp(res, &exponent); + if (std::isinf(res) || std::isnan(res) || exponent >= 30) { + // 整数で表現出来ない(かも知れない)ので例外 + FPPreciseException(FPECR_FIOV, src); + return; } + rD = (uint32)(int32)res; +} + +// 100001_DDDDD00000_010010_0nn00sssss +OP_DEF(int) +{ + // 現在のモードのまま (ちょっと無駄だけど…) + ops_int(std::fegetround()); } // 100001_DDDDD00000_010100_0nn00sssss OP_DEF(nint) { - // 暫定 - double s2; - - if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); - return; - } - - FP_GET(s2, FP_T2, FLD_S2); - std::fesetround(FE_TONEAREST); - rD = (uint32)(int32)std::nearbyint(s2); + ops_int(FE_TONEAREST); } // 100001_DDDDD00000_010110_0nn00sssss OP_DEF(trnc) { - // 暫定 - double s2; - - if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); - return; - } - - FP_GET(s2, FP_T2, FLD_S2); - rD = (uint32)(int32)std::trunc(s2); + ops_int(FE_TOWARDZERO); } // 100001_DDDDDSSSSS_01110n_nnnnnsssss @@ -1122,7 +1114,7 @@ OP_DEF(fdiv) double s2; if (FLD_D == 0) { - FPException(M88K_FPEXCEP_FUNIMP); + FPPreciseException(FPECR_FUNIMP); return; } @@ -1135,34 +1127,30 @@ OP_DEF(fdiv) OP_DEF(br) { AddCycle(1); // Table.7-5 - EnterBranch(xip + D26); - ExitBranch(); + DoBranch(reg.xip + D26, false); } // 110001_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(br_n) { AddCycle(0); // Table.7-5 - EnterBranch(xip + D26); - ExitDelayBranch(); + DoBranch(reg.xip + D26, true); } // 110010_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(bsr) { AddCycle(1); // Table.7-5 - EnterBranch(xip + D26); - r[1] = nip; - ExitBranch(); + reg.r[1] = reg.nip; + DoBranch(reg.xip + D26, false); } // 110011_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(bsr_n) { AddCycle(0); // Table.7-5 - EnterBranch(xip + D26); - r[1] = nip + 4; - ExitDelayBranch(); + reg.r[1] = reg.nip + 4; + DoBranch(reg.xip + D26, true); } // 110100_nnnnnSSSSS_nnnnnn_nnnnnnnnnn @@ -1170,8 +1158,7 @@ OP_DEF(bb0) { if ((rS1 & (1 << B5)) == 0) { AddCycle(1); // Table.7-5 - EnterBranch(xip + D16); - ExitBranch(); + DoBranch(reg.xip + D16, false); } } @@ -1179,8 +1166,7 @@ OP_DEF(bb0) OP_DEF(bb0_n) { if ((rS1 & (1 << B5)) == 0) { - EnterBranch(xip + D16); - ExitDelayBranch(); + DoBranch(reg.xip + D16, true); } } @@ -1189,8 +1175,7 @@ OP_DEF(bb1) { if ((rS1 & (1 << B5)) != 0) { AddCycle(1); // Table.7-5 - EnterBranch(xip + D16); - ExitBranch(); + DoBranch(reg.xip + D16, false); } } @@ -1198,8 +1183,7 @@ OP_DEF(bb1) OP_DEF(bb1_n) { if ((rS1 & (1 << B5)) != 0) { - EnterBranch(xip + D16); - ExitDelayBranch(); + DoBranch(reg.xip + D16, true); } } @@ -1208,8 +1192,7 @@ OP_DEF(bcnd) { if ((acc_cnd(rS1) & M5) != 0) { AddCycle(1); // Table.7-5 - EnterBranch(xip + D16); - ExitBranch(); + DoBranch(reg.xip + D16, false); } } @@ -1217,8 +1200,7 @@ OP_DEF(bcnd) OP_DEF(bcnd_n) { if ((acc_cnd(rS1) & M5) != 0) { - EnterBranch(xip + D16); - ExitDelayBranch(); + DoBranch(reg.xip + D16, true); } } @@ -1287,14 +1269,14 @@ OP_DEF(rot_1) { if (FLD_D == 0) return; - rD = ROR(rS1, O5); + rD = ROR32(rS1, O5); } // 111100_bbbbbSSSSS_110100_0vvvvvvvvv OP_DEF(tb0) { if (IsUser() && VEC9 < 128) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } if ((rS1 & (1 << B5)) == 0) { @@ -1306,7 +1288,7 @@ OP_DEF(tb0) OP_DEF(tb1) { if (IsUser() && VEC9 < 128) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } if ((rS1 & (1 << B5)) != 0) { @@ -1331,9 +1313,9 @@ OP_DEF(tb1) if (IsSuper() && VEC9 == 0xff) { const auto& e = brhist.entry[brhist.top]; if (e.count == 3 - && e.to == xip + && e.to == reg.xip && e.from - e.to < 16) { - atomic_reqflag |= CPU_REQ_STOP; + ChangeState(CPU_STATE_STOP); } } } @@ -1343,7 +1325,7 @@ OP_DEF(tb1) OP_DEF(tcnd) { if (IsUser() && VEC9 < 128) { - Exception(M88K_EXCEP_PRIV); + Exception(EXCEP_PRIV); return; } if ((acc_cnd(rS1) & M5) != 0) { @@ -1365,9 +1347,10 @@ OP_DEF(xmem_bu) lastaddr = addr; // 例外安全のために都度 S/U ビットを制御 - if (__predict_false(usr)) cmmu[1].SetSuper(false); - tmp = cmmu[1].xmem_8(addr, rD & 0xff); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + tmp = cmmu[1]->xmem_1(addr, rD & 0xff); + LEAVE_USR(); + if ((int64)tmp < 0) { XmemDataException(addr, DM_BU | DM_LOCK | usr); return; @@ -1391,9 +1374,10 @@ OP_DEF(xmem_w) lastaddr = addr; // 例外安全のために都度 S/U ビットを制御 - if (__predict_false(usr)) cmmu[1].SetSuper(false); - tmp = cmmu[1].xmem_32(addr, rD); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + tmp = cmmu[1]->xmem_4(addr, rD); + LEAVE_USR(); + if ((int64)tmp < 0) { XmemDataException(addr, DM_W | DM_LOCK | usr); return; @@ -1417,9 +1401,9 @@ OP_DEF(ld_hu) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data = cmmu[1].load_16(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data = cmmu[1]->load_2(addr); + LEAVE_USR(); if ((int64)data < 0) { ReadDataException32(addr, DM_HU | usr); @@ -1443,9 +1427,9 @@ OP_DEF(ld_bu) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data = cmmu[1].load_8(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data = cmmu[1]->load_1(addr); + LEAVE_USR(); if ((int64)data < 0) { ReadDataException32(addr, DM_BU | usr); @@ -1470,9 +1454,9 @@ OP_DEF(ld_d) lastaddr = addr; // 例外安全のために都度 S/U ビットを制御 - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data1 = cmmu[1].load_32(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data1 = cmmu[1]->load_4(addr); + LEAVE_USR(); if ((int64)data1 < 0) { ReadDataException64(addr, DM_D1 | usr); @@ -1480,9 +1464,9 @@ OP_DEF(ld_d) } addr += 4; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data2 = cmmu[1].load_32(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data2 = cmmu[1]->load_4(addr); + LEAVE_USR(); if ((int64)data2 < 0) { ReadDataException64(addr, DM_D2 | usr); @@ -1490,7 +1474,7 @@ OP_DEF(ld_d) } rD = data1; rD2 = data2; - r[0] = 0; + reg.r[0] = 0; } // 111101_DDDDDSSSSS_000101_xU000sssss @@ -1506,9 +1490,9 @@ OP_DEF(ld_w) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data = cmmu[1].load_32(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data = cmmu[1]->load_4(addr); + LEAVE_USR(); if ((int64)data < 0) { ReadDataException32(addr, DM_W | usr); @@ -1532,9 +1516,9 @@ OP_DEF(ld_h) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data = cmmu[1].load_16(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data = cmmu[1]->load_2(addr); + LEAVE_USR(); if ((int64)data < 0) { ReadDataException32(addr, DM_H | usr); @@ -1558,9 +1542,9 @@ OP_DEF(ld_b) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - data = cmmu[1].load_8(addr); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + data = cmmu[1]->load_1(addr); + LEAVE_USR(); if ((int64)data < 0) { ReadDataException32(addr, DM_B | usr); @@ -1584,9 +1568,9 @@ OP_DEF(st_d) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_32(addr, rD); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + rv = cmmu[1]->store_4(addr, rD); + LEAVE_USR(); if ((int64)rv < 0) { WriteDataException64(addr, DM_D1 | usr); @@ -1595,9 +1579,9 @@ OP_DEF(st_d) addr += 4; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_32(addr, rD2); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + rv = cmmu[1]->store_4(addr, rD2); + LEAVE_USR(); if ((int64)rv < 0) { WriteDataException64(addr, DM_D2 | usr); @@ -1618,9 +1602,9 @@ OP_DEF(st_w) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_32(addr, rD); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + rv = cmmu[1]->store_4(addr, rD); + LEAVE_USR(); if ((int64)rv < 0) { WriteDataException32(addr, DM_W | usr); @@ -1641,9 +1625,9 @@ OP_DEF(st_h) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_16(addr, rD & 0xffff); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + rv = cmmu[1]->store_2(addr, rD & 0xffff); + LEAVE_USR(); if ((int64)rv < 0) { WriteDataException32(addr, DM_H | usr); @@ -1664,9 +1648,9 @@ OP_DEF(st_b) lastaddr = addr; - if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_8(addr, rD & 0xff); - if (__predict_false(usr)) cmmu[1].SetSuper(true); + ENTER_USR(); + rv = cmmu[1]->store_1(addr, rD & 0xff); + LEAVE_USR(); if ((int64)rv < 0) { WriteDataException32(addr, DM_B | usr); @@ -1822,7 +1806,7 @@ OP_DEF(divu) AddCycle(38); // Table.7-6 (ただしざっくり) if (rS2 == 0) { - Exception(M88K_EXCEP_INT_DIV); + Exception(EXCEP_INT_DIV); return; } if (FLD_D == 0) @@ -1853,7 +1837,7 @@ OP_DEF(add) res += GetCY(); } if (isovf_add(rS1, rS2, res)) { - Exception(M88K_EXCEP_INT_OVF); + Exception(EXCEP_INT_OVF); return; } @@ -1879,7 +1863,7 @@ OP_DEF(sub) res += 1; } if (isovf_sub(rS1, rS2, res)) { - Exception(M88K_EXCEP_INT_OVF); + Exception(EXCEP_INT_OVF); return; } @@ -1903,7 +1887,7 @@ OP_DEF(div) AddCycle(38); // Table.7-6 (ただしざっくり) if ((int32)rS1 < 0 || (int32)rS2 <= 0) { - Exception(M88K_EXCEP_INT_DIV); + Exception(EXCEP_INT_DIV); return; } if (FLD_D == 0) @@ -1987,41 +1971,39 @@ OP_DEF(rot_2) if (FLD_D == 0) return; int o = (rS2 ) & 0x1f; - rD = ROR(rS1, o); + rD = ROR32(rS1, o); } // 111101_zzzzzzzzzz_110000_00000sssss OP_DEF(jmp) { AddCycle(1); // Table.7-5 - EnterBranch(rS2); - ExitBranch(); + DoBranch(rS2, false); } // 111101_zzzzzzzzzz_110001_00000sssss OP_DEF(jmp_n) { AddCycle(0); // Table.7-5 - EnterBranch(rS2); - ExitDelayBranch(); + DoBranch(rS2, true); } // 111101_zzzzzzzzzz_110010_00000sssss OP_DEF(jsr) { AddCycle(1); // Table.7-5 - EnterBranch(rS2); - r[1] = nip; - ExitBranch(); + uint32 toaddr = rS2; + reg.r[1] = reg.nip; + DoBranch(toaddr, false); } // 111101_zzzzzzzzzz_110011_00000sssss OP_DEF(jsr_n) { AddCycle(0); // Table.7-5 - EnterBranch(rS2); - r[1] = nip + 4; - ExitDelayBranch(); + uint32 toaddr = rS2; + reg.r[1] = reg.nip + 4; + DoBranch(toaddr, true); } // 111101_DDDDDzzzzz_111010_00000sssss @@ -2072,10 +2054,10 @@ OP_DEF(rte) { AddCycle(2); // Table.7-5 - SetPSR(epsr); + SetPSR(reg.epsr); - // LUNA88K の ROM1.2 は sxip, snip, sfip を制御してくる。 - // ROM1.2 の DAE ハンドラは、 + // LUNA-88K の PROM 1.20 は sxip, snip, sfip を制御してくる。 + // PROM 1.20 の DAE ハンドラは、 // sxip = INVALID // snip = 0 (INVALID) // sfip = original sxip @@ -2083,20 +2065,11 @@ OP_DEF(rte) // CPU は sxip は評価しない。snip は 0 なので、無効として nop 扱い。 // (実際はクロックが消費されるかも?) // sfip が VALID なので、そこから実行を再開する。 - if ((snip & SIP_V)) { - EnterBranch(snip & SIP_MASK); - ExitBranch(); - fip = sfip & SIP_MASK; + if ((reg.snip & SIP_V)) { + DoBranch(reg.snip & SIP_MASK, false); + reg.fip = reg.sfip & SIP_MASK; } else { - EnterBranch(sfip & SIP_MASK); - ExitBranch(); - } - - // ディレイスロットにいた命令かどうかをやんわり復元する。 - // 88110 ではこの情報が E?IP に保持されているが - // 88100 にはない。 - if (nip + 4 != fip) { - ExitDelayBranch(); + DoBranch(reg.sfip & SIP_MASK, false); } } @@ -2104,7 +2077,7 @@ OP_DEF(rte) OP_DEF(tbnd_1) { if (rS1 > rS2) { - Exception(M88K_EXCEP_BOUNDS); + Exception(EXCEP_BOUNDS); } } @@ -2112,7 +2085,7 @@ OP_DEF(tbnd_1) OP_DEF(tbnd_2) { if (rS1 > IMM16) { - Exception(M88K_EXCEP_BOUNDS); + Exception(EXCEP_BOUNDS); } }