--- nono/m88xx0/m88100ops.cpp 2026/04/29 17:04:41 1.1.1.4 +++ nono/m88xx0/m88100ops.cpp 2026/04/29 17:05:05 1.1.1.11 @@ -5,6 +5,8 @@ // #include "m88100acc.h" +#include +#include // 命令のフィールド取り出し #define FLD_D m88100opf_D(opX) @@ -30,16 +32,51 @@ #define rS1 (r[FLD_S1]) #define rS2 (r[FLD_S2]) // ダブルワードの2ワード目 -#define rD2 (r[(FLD_D + 1) & 0x1f]) +#define FLD_D2 ((FLD_D + 1) & 0x1f) +#define rD2 (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 t(サイズ)が原因で Reserved Operand になった時の S1, S2 はどうなる? +#define FP_GET(v, t, n) \ +do { \ + if (t == 0) { \ + v = u2f(r[n]); \ + } else if (t == 1) { \ + v = u2d(((uint64)r[n] << 32) | r[(n + 1) & 31]); \ + } else { \ + FPPreciseException(FPECR_FROP); \ + return; \ + } \ +} while (0) + +#define FP_SET(t, n, v) \ +do { \ + if (t == 0) { \ + r[n] = f2u(v); \ + } else if (t == 1) { \ + uint64 tmp = d2u(v); \ + r[n] = tmp >> 32; \ + if (__predict_true(n != 31)) \ + r[n + 1] = (uint32)tmp; \ + } else { \ + FPPreciseException(FPECR_FROP); \ + return; \ + } \ +} while (0) // ブランチ命令入り口 inline void m88kcpu::EnterBranch(uint32 toaddr) { + // ジャンプ先アドレスの下位2bitはマスクされる + toaddr &= ~3; // ブランチヒストリに登録しつつ、 // ブランチヒストリからブランチ状況を取得 - auto e = brhist.AddEntry(xip, toaddr, opX); + auto e = brhist.AddEntry(xip | (IsSuper() ? 1 : 0), toaddr, opX); fip = toaddr; // STOP 検出。 @@ -66,19 +103,28 @@ m88kcpu::EnterBranch(uint32 toaddr) // 新規ブランチ // 初期化 nop_counter = 0; - } else { + } else if (pseudo_stop_enable) { + // 疑似 STOP 状態有効のときに、 // e.count == 2 のときだけ STOP の検証をすれば良い。 // d はブランチ間の命令数 uint32 d = (xip - toaddr) / sizeof(uint32); // 遅延ブランチなら 1 命令余分に実行するはずなので 1 足す - if (opX & (1 << 26)) { + if ((opX & (1 << 26)) != 0) { d++; } // ブランチ間の命令がすべて NOP なら STOP ということにする。 if (nop_counter == d) { atomic_reqflag |= CPU_REQ_STOP; } + + // OpenBSD 6.6 では + // L1: tb1 #1 r0, #0xff + // ld r13, r21, #0x168 + // bcnd.n eq0, r13, L1 + // or r2, r25, #0x5ea0 + // の命令列が使用されている。 + // これについては tb1 で実装してある。 } // STOP ではなかったら、あとは放置していい } @@ -128,7 +174,7 @@ m88kcpu::ldst_usr(uint32& usr) inline uint32 m88kcpu::ldst_scale(uint32 size) { - if (opX & (1 << 9)) { + if ((opX & (1 << 9)) != 0) { return rS1 + rS2 * size; } else { return rS1 + rS2; @@ -141,7 +187,7 @@ m88kcpu::ldst_scale(uint32 size) inline bool m88kcpu::ldst_align(uint32 size, uint32& addr) { - if (addr & (size - 1)) { + if ((addr & (size - 1)) != 0) { if (IsMXM()) { addr &= ~(size - 1); } else { @@ -237,7 +283,7 @@ static inline int acc_cnd(uint32 r) { int rv; - if (r & 0x80000000) { + if ((r & 0x80000000) != 0) { if (r == 0x80000000) { rv = 0x8; } else { @@ -266,6 +312,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 @@ -277,12 +333,12 @@ OP_DEF(xmem_bu_imm) addr = rS1 + IMM16; LDST_ALIGN(1); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; - tmp = cmmu[1].xmem_8(addr, rD); + tmp = cmmu[1].xmem_8(addr, rD & 0xff); if ((int64)tmp < 0) { - XmemDataException(addr, rD, DM_BU | DM_LOCK); + XmemDataException(addr, DM_BU | DM_LOCK); return; } if (FLD_D == 0) @@ -299,12 +355,12 @@ OP_DEF(xmem_w_imm) addr = rS1 + IMM16; LDST_ALIGN(4); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; tmp = cmmu[1].xmem_32(addr, rD); if ((int64)tmp < 0) { - XmemDataException(addr, rD, DM_W | DM_LOCK); + XmemDataException(addr, DM_W | DM_LOCK); return; } if (FLD_D == 0) @@ -321,12 +377,12 @@ OP_DEF(ld_hu_imm) addr = rS1 + IMM16; LDST_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; data = cmmu[1].load_16(addr); if ((int64)data < 0) { - ReadDataException(addr, DM_HU); + ReadDataException32(addr, DM_HU); return; } if (FLD_D == 0) @@ -343,12 +399,12 @@ OP_DEF(ld_bu_imm) addr = rS1 + IMM16; LDST_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; data = cmmu[1].load_8(addr); if ((int64)data < 0) { - ReadDataException(addr, DM_BU); + ReadDataException32(addr, DM_BU); return; } if (FLD_D == 0) @@ -365,18 +421,18 @@ OP_DEF(ld_d_imm) addr = rS1 + IMM16; LDST_ALIGN(8); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; data1 = cmmu[1].load_32(addr); if ((int64)data1 < 0) { - ReadDataException(addr, DM_D1); + ReadDataException64(addr, DM_D1); return; } addr += 4; data2 = cmmu[1].load_32(addr); if ((int64)data2 < 0) { - ReadDataException(addr, DM_D2); + ReadDataException64(addr, DM_D2); return; } @@ -394,12 +450,12 @@ OP_DEF(ld_w_imm) addr = rS1 + IMM16; LDST_ALIGN(4); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; data = cmmu[1].load_32(addr); if ((int64)data < 0) { - ReadDataException(addr, DM_W); + ReadDataException32(addr, DM_W); return; } if (FLD_D == 0) @@ -416,12 +472,12 @@ OP_DEF(ld_h_imm) addr = rS1 + IMM16; LDST_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; data = cmmu[1].load_16(addr); if ((int64)data < 0) { - ReadDataException(addr, DM_H); + ReadDataException32(addr, DM_H); return; } if (FLD_D == 0) @@ -438,12 +494,12 @@ OP_DEF(ld_b_imm) addr = rS1 + IMM16; LDST_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; data = cmmu[1].load_8(addr); if ((int64)data < 0) { - ReadDataException(addr, DM_B); + ReadDataException32(addr, DM_B); return; } if (FLD_D == 0) @@ -460,18 +516,18 @@ OP_DEF(st_d_imm) addr = rS1 + IMM16; LDST_ALIGN(8); - total_cycle += 1 + 4; // Table.7-3 + AddCycle(1 + 4); // Table.7-3 lastaddr = addr; rv = cmmu[1].store_32(addr, rD); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_D1); + WriteDataException64(addr, DM_D1); return; } addr += 4; rv = cmmu[1].store_32(addr, rD2); if ((int64)rv < 0) { - WriteDataException(addr, rD2, DM_D2); + WriteDataException64(addr, DM_D2); return; } } @@ -485,12 +541,12 @@ OP_DEF(st_w_imm) addr = rS1 + IMM16; LDST_ALIGN(4); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; rv = cmmu[1].store_32(addr, rD); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_W); + WriteDataException32(addr, DM_W); return; } } @@ -504,12 +560,12 @@ OP_DEF(st_h_imm) addr = rS1 + IMM16; LDST_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_16(addr, rD); + rv = cmmu[1].store_16(addr, rD & 0xffff); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_H); + WriteDataException32(addr, DM_H); return; } } @@ -523,12 +579,12 @@ OP_DEF(st_b_imm) addr = rS1 + IMM16; LDST_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; - rv = cmmu[1].store_8(addr, rD); + rv = cmmu[1].store_8(addr, rD & 0xff); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_B); + WriteDataException32(addr, DM_B); return; } } @@ -653,7 +709,7 @@ OP_DEF(divu_imm) return; } - total_cycle += 38; // Table.7-6 (ただしざっくり) + AddCycle(38); // Table.7-6 (ただしざっくり) if (IMM16 == 0) { Exception(M88K_EXCEP_INT_DIV); @@ -672,7 +728,7 @@ OP_DEF(mul_imm) return; } - total_cycle += 4; // Table.7-6 (ただしざっくり) + AddCycle(4); // Table.7-6 (ただしざっくり) if (FLD_D == 0) return; @@ -715,7 +771,7 @@ OP_DEF(div_imm) return; } - total_cycle += 38; // Table.7-6 (ただしざっくり) + AddCycle(38); // Table.7-6 (ただしざっくり) if ((int32)rS1 < 0 || IMM16 == 0) { Exception(M88K_EXCEP_INT_DIV); @@ -742,8 +798,9 @@ OP_DEF(ldcr) return; } if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 compatible + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 Exception(M88K_EXCEP_UNIMPL_OP); return; } @@ -757,30 +814,31 @@ OP_DEF(fldcr) { int n = FLD_CR; - if (IsUser()) { - // fcr62, 63 はユーザモードでアクセスできる - // それ以外はスーパバイザ用。 - if (n < 62) { - Exception(M88K_EXCEP_PRIV); - return; - } - } - - if (8 < n && n < 62) { - // 未実装 FCR - // 88100 undocumented, - // 88110 compatible - Exception(M88K_EXCEP_UNIMPL_OP); - 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 - // 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 = fcr[n] & fcr_mask[n]; } // 100000_zzzzzSSSSS_10000n_nnnnnsssss @@ -791,8 +849,9 @@ OP_DEF(stcr) return; } if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 unimpl_op + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 Exception(M88K_EXCEP_UNIMPL_OP); return; } @@ -807,27 +866,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; - } + fcr[n] = rS1 & fcr_mask[n]; - // fcr は 62,63 の配置に細工がしてある - if (n >= 62) { - n = n - 62 + 9; + // FPCR が変更されたら、ホストの丸めモードも更新 + if (n == 10) { + uint32 rm = (fpcr >> 14) & 3; + std::fesetround(rm_to_round[rm]); } - fcr[n] = rS1; } // 100000_DDDDDSSSSS_11000n_nnnnnsssss @@ -840,8 +906,9 @@ OP_DEF(xcr) return; } if (FLD_CR > countof(cr)) { - // 88100 undocumented, - // 88110 unimpl_op + // 制御レジスタ番号が範囲外の時は + // 88100 では undocumented、 + // 88110 では Unimplemented Opcode 例外。 Exception(M88K_EXCEP_UNIMPL_OP); return; } @@ -857,93 +924,239 @@ OP_DEF(xcr) // 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 = fcr[n] & fcr_mask[n]; + 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; + + exchange: + uint32 tmp = rS1; + rD = fcr[n] & fcr_mask[n]; + fcr[n] = tmp & fcr_mask[n]; + r[0] = 0; } // 100001_DDDDDSSSSS_00000n_nnnnnsssss OP_DEF(fmul) { - OP_FUNC(unimpl); + // 暫定 + double s1; + double s2; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_GET(s1, FP_T1, FLD_S1); + FP_GET(s2, FP_T2, FLD_S2); + FP_SET(FP_TD, FLD_D, s1 * s2); } // 100001_DDDDD00000_001000_000nnsssss OP_DEF(flt) { - OP_FUNC(unimpl); + // 暫定 + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_SET(FP_TD, FLD_D, (double)(int32)rS2); } + // 100001_DDDDDSSSSS_00101n_nnnnnsssss OP_DEF(fadd) { - OP_FUNC(unimpl); + // 暫定 + double s1; + double s2; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_GET(s1, FP_T1, FLD_S1); + FP_GET(s2, FP_T2, FLD_S2); + FP_SET(FP_TD, FLD_D, s1 + s2); } // 100001_DDDDDSSSSS_00110n_nnnnnsssss OP_DEF(fsub) { - OP_FUNC(unimpl); + // 暫定 + double s1; + double s2; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_GET(s1, FP_T1, FLD_S1); + FP_GET(s2, FP_T2, FLD_S2); + FP_SET(FP_TD, FLD_D, s1 - s2); } // 100001_DDDDDSSSSS_00111n_nnnnnsssss OP_DEF(fcmp) { - OP_FUNC(unimpl); + double s1; + double s2; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_GET(s1, FP_T1, FLD_S1); + FP_GET(s2, FP_T2, FLD_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; +} + +// int, nint, trnc 命令の共通部 +void +m88kcpu::ops_int(int rndmode) +{ + double src; + double res; + int exponent; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + 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(); + + 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) { - OP_FUNC(unimpl); + // 現在のモードのまま (ちょっと無駄だけど…) + ops_int(std::fegetround()); } // 100001_DDDDD00000_010100_0nn00sssss OP_DEF(nint) { - OP_FUNC(unimpl); + ops_int(FE_TONEAREST); } // 100001_DDDDD00000_010110_0nn00sssss OP_DEF(trnc) { - OP_FUNC(unimpl); + ops_int(FE_TOWARDZERO); } // 100001_DDDDDSSSSS_01110n_nnnnnsssss OP_DEF(fdiv) { - OP_FUNC(unimpl); + // 暫定 + double s1; + double s2; + + if (FLD_D == 0) { + FPPreciseException(FPECR_FUNIMP); + return; + } + + FP_GET(s1, FP_T1, FLD_S1); + FP_GET(s2, FP_T2, FLD_S2); + FP_SET(FP_TD, FLD_D, s1 / s2); } // 110000_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(br) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(xip + D26); ExitBranch(); } @@ -951,7 +1164,7 @@ OP_DEF(br) // 110001_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(br_n) { - total_cycle += 0; // Table.7-5 + AddCycle(0); // Table.7-5 EnterBranch(xip + D26); ExitDelayBranch(); } @@ -959,7 +1172,7 @@ OP_DEF(br_n) // 110010_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(bsr) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(xip + D26); r[1] = nip; ExitBranch(); @@ -968,7 +1181,7 @@ OP_DEF(bsr) // 110011_nnnnnnnnnn_nnnnnn_nnnnnnnnnn OP_DEF(bsr_n) { - total_cycle += 0; // Table.7-5 + AddCycle(0); // Table.7-5 EnterBranch(xip + D26); r[1] = nip + 4; ExitDelayBranch(); @@ -978,7 +1191,7 @@ OP_DEF(bsr_n) OP_DEF(bb0) { if ((rS1 & (1 << B5)) == 0) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(xip + D16); ExitBranch(); } @@ -997,7 +1210,7 @@ OP_DEF(bb0_n) OP_DEF(bb1) { if ((rS1 & (1 << B5)) != 0) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(xip + D16); ExitBranch(); } @@ -1015,8 +1228,8 @@ OP_DEF(bb1_n) // 111010_MMMMMSSSSS_nnnnnn_nnnnnnnnnn OP_DEF(bcnd) { - if (acc_cnd(rS1) & M5) { - total_cycle += 1; // Table.7-5 + if ((acc_cnd(rS1) & M5) != 0) { + AddCycle(1); // Table.7-5 EnterBranch(xip + D16); ExitBranch(); } @@ -1025,7 +1238,7 @@ OP_DEF(bcnd) // 111011_MMMMMSSSSS_nnnnnn_nnnnnnnnnn OP_DEF(bcnd_n) { - if (acc_cnd(rS1) & M5) { + if ((acc_cnd(rS1) & M5) != 0) { EnterBranch(xip + D16); ExitDelayBranch(); } @@ -1120,6 +1333,31 @@ OP_DEF(tb1) } if ((rS1 & (1 << B5)) != 0) { ExceptionCore(VEC9, ExceptionKind::TRAP); + } else if (pseudo_stop_enable) { + + // OpenBSD の STOP エミュレーション。 + // OpenBSD 6.6 では + // L1: tb1 #1 r0, #0xff + // ld r13, r21, #0x168 + // bcnd.n eq0, r13, L1 + // or r2, r25, #0x5ea0 + // の命令列が使用されている。 + // tb1 は volatile 変数をアクセスするための同期命令として + // gcc により出力されている。スーパバイザが volatile を + // 観測するために同じアドレスの tb1 に直接ブランチで 3 回飛び込んで + // きたら、それは割り込みか別プロセッサか I/O による変更を待っている + // はずなので STOP と判断してみる。 + // o. 3回、という数字に確たる理由はない。 + // o. 念の為、16バイト(4命令)後方以内からのブランチに限定しておく。 + + if (IsSuper() && VEC9 == 0xff) { + const auto& e = brhist.entry[brhist.top]; + if (e.count == 3 + && e.to == xip + && e.from - e.to < 16) { + atomic_reqflag |= CPU_REQ_STOP; + } + } } } @@ -1130,7 +1368,7 @@ OP_DEF(tcnd) Exception(M88K_EXCEP_PRIV); return; } - if (acc_cnd(rS1) & M5) { + if ((acc_cnd(rS1) & M5) != 0) { ExceptionCore(VEC9, ExceptionKind::TRAP); } } @@ -1144,16 +1382,16 @@ OP_DEF(xmem_bu) LDST_USR_SCALE_ALIGN(1); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; // 例外安全のために都度 S/U ビットを制御 if (__predict_false(usr)) cmmu[1].SetSuper(false); - tmp = cmmu[1].xmem_8(addr, rD); + tmp = cmmu[1].xmem_8(addr, rD & 0xff); if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)tmp < 0) { - XmemDataException(addr, rD, DM_BU | DM_LOCK | usr); + XmemDataException(addr, DM_BU | DM_LOCK | usr); return; } if (FLD_D == 0) @@ -1170,7 +1408,7 @@ OP_DEF(xmem_w) LDST_USR_SCALE_ALIGN(4); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; @@ -1179,7 +1417,7 @@ OP_DEF(xmem_w) tmp = cmmu[1].xmem_32(addr, rD); if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)tmp < 0) { - XmemDataException(addr, rD, DM_W | DM_LOCK | usr); + XmemDataException(addr, DM_W | DM_LOCK | usr); return; } @@ -1197,7 +1435,7 @@ OP_DEF(ld_hu) LDST_USR_SCALE_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1206,7 +1444,7 @@ OP_DEF(ld_hu) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data < 0) { - ReadDataException(addr, DM_HU | usr); + ReadDataException32(addr, DM_HU | usr); return; } if (FLD_D == 0) @@ -1223,7 +1461,7 @@ OP_DEF(ld_bu) LDST_USR_SCALE_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1232,7 +1470,7 @@ OP_DEF(ld_bu) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data < 0) { - ReadDataException(addr, DM_BU | usr); + ReadDataException32(addr, DM_BU | usr); return; } if (FLD_D == 0) @@ -1249,7 +1487,7 @@ OP_DEF(ld_d) LDST_USR_SCALE_ALIGN(8); - total_cycle += 4; // Table.7-3 + AddCycle(4); // Table.7-3 lastaddr = addr; @@ -1259,7 +1497,7 @@ OP_DEF(ld_d) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data1 < 0) { - ReadDataException(addr, DM_D1 | usr); + ReadDataException64(addr, DM_D1 | usr); return; } addr += 4; @@ -1269,7 +1507,7 @@ OP_DEF(ld_d) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data2 < 0) { - ReadDataException(addr, DM_D2 | usr); + ReadDataException64(addr, DM_D2 | usr); return; } rD = data1; @@ -1286,7 +1524,7 @@ OP_DEF(ld_w) LDST_USR_SCALE_ALIGN(4); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1295,7 +1533,7 @@ OP_DEF(ld_w) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data < 0) { - ReadDataException(addr, DM_W | usr); + ReadDataException32(addr, DM_W | usr); return; } if (FLD_D == 0) @@ -1312,7 +1550,7 @@ OP_DEF(ld_h) LDST_USR_SCALE_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1321,7 +1559,7 @@ OP_DEF(ld_h) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data < 0) { - ReadDataException(addr, DM_H | usr); + ReadDataException32(addr, DM_H | usr); return; } if (FLD_D == 0) @@ -1338,7 +1576,7 @@ OP_DEF(ld_b) LDST_USR_SCALE_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1347,7 +1585,7 @@ OP_DEF(ld_b) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)data < 0) { - ReadDataException(addr, DM_B | usr); + ReadDataException32(addr, DM_B | usr); return; } if (FLD_D == 0) @@ -1364,7 +1602,7 @@ OP_DEF(st_d) LDST_USR_SCALE_ALIGN(8); - total_cycle += 1 + 4; // Table.7-3 + AddCycle(1 + 4); // Table.7-3 lastaddr = addr; @@ -1373,7 +1611,7 @@ OP_DEF(st_d) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_D1 | usr); + WriteDataException64(addr, DM_D1 | usr); return; } @@ -1384,7 +1622,7 @@ OP_DEF(st_d) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)rv < 0) { - WriteDataException(addr, rD2, DM_D2 | usr); + WriteDataException64(addr, DM_D2 | usr); return; } } @@ -1398,7 +1636,7 @@ OP_DEF(st_w) LDST_USR_SCALE_ALIGN(4); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; @@ -1407,7 +1645,7 @@ OP_DEF(st_w) if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_W | usr); + WriteDataException32(addr, DM_W | usr); return; } } @@ -1421,16 +1659,16 @@ OP_DEF(st_h) LDST_USR_SCALE_ALIGN(2); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_16(addr, rD); + rv = cmmu[1].store_16(addr, rD & 0xffff); if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_H | usr); + WriteDataException32(addr, DM_H | usr); return; } } @@ -1444,16 +1682,16 @@ OP_DEF(st_b) LDST_USR_SCALE_ALIGN(1); - total_cycle += 3; // Table.7-3 + AddCycle(3); // Table.7-3 lastaddr = addr; if (__predict_false(usr)) cmmu[1].SetSuper(false); - rv = cmmu[1].store_8(addr, rD); + rv = cmmu[1].store_8(addr, rD & 0xff); if (__predict_false(usr)) cmmu[1].SetSuper(true); if ((int64)rv < 0) { - WriteDataException(addr, rD, DM_B | usr); + WriteDataException32(addr, DM_B | usr); return; } } @@ -1578,8 +1816,7 @@ OP_DEF(addu) // 111101_DDDDDSSSSS_011001_nn000sssss OP_DEF(subu) { - // sub 命令では CY ビットは Z80 とかと逆で、0 ならボロー発生。 - // 88100 のマニュアルには書いてない。88110 のマニュアルにある。 + // sub* 命令の CY ビットはボローが発生*していない*時セット。 // 1 の補数をとって足す。 uint64 res = (uint64)rS1 + (~rS2); @@ -1604,7 +1841,7 @@ OP_DEF(divu) return; } - total_cycle += 38; // Table.7-6 (ただしざっくり) + AddCycle(38); // Table.7-6 (ただしざっくり) if (rS2 == 0) { Exception(M88K_EXCEP_INT_DIV); @@ -1623,7 +1860,7 @@ OP_DEF(mul) return; } - total_cycle += 4; // Table.7-6 (ただしざっくり) + AddCycle(4); // Table.7-6 (ただしざっくり) if (FLD_D == 0) return; @@ -1637,15 +1874,15 @@ OP_DEF(add) if (IsCI) { res += GetCY(); } - // XXX: オーバーフローが起きたときにキャリーが更新されるかどうか - // ドキュメントされてない気がする - if (IsCO) { - SetCY(res >> 32); - } if (isovf_add(rS1, rS2, res)) { Exception(M88K_EXCEP_INT_OVF); return; } + + // rD, Cy の更新は例外が起きなかった時だけ (p.6-19) + if (IsCO) { + SetCY(res >> 32); + } if (FLD_D == 0) return; rD = res; @@ -1654,8 +1891,7 @@ OP_DEF(add) // 111101_DDDDDSSSSS_011101_nn000sssss OP_DEF(sub) { - // sub 命令では CY ビットは Z80 とかと逆で、0 ならボロー発生。 - // 88100 のマニュアルには書いてない。88110 のマニュアルにある。 + // sub* 命令の CY ビットはボローが発生*していない*時セット。 // 1 の補数をとって足す。 uint64 res = (uint64)rS1 + (~rS2); @@ -1664,15 +1900,15 @@ OP_DEF(sub) } else { res += 1; } - // XXX: オーバーフローが起きたときにキャリーが更新されるかどうか - // ドキュメントされてない気がする - if (IsCO) { - SetCY(res >> 32); - } if (isovf_sub(rS1, rS2, res)) { Exception(M88K_EXCEP_INT_OVF); return; } + + // rD, Cy の更新は例外が起きなかった時だけ (p.6-19) + if (IsCO) { + SetCY(res >> 32); + } if (FLD_D == 0) return; rD = res; @@ -1686,7 +1922,7 @@ OP_DEF(div) return; } - total_cycle += 38; // Table.7-6 (ただしざっくり) + AddCycle(38); // Table.7-6 (ただしざっくり) if ((int32)rS1 < 0 || (int32)rS2 <= 0) { Exception(M88K_EXCEP_INT_DIV); @@ -1779,7 +2015,7 @@ OP_DEF(rot_2) // 111101_zzzzzzzzzz_110000_00000sssss OP_DEF(jmp) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(rS2); ExitBranch(); } @@ -1787,7 +2023,7 @@ OP_DEF(jmp) // 111101_zzzzzzzzzz_110001_00000sssss OP_DEF(jmp_n) { - total_cycle += 0; // Table.7-5 + AddCycle(0); // Table.7-5 EnterBranch(rS2); ExitDelayBranch(); } @@ -1795,7 +2031,7 @@ OP_DEF(jmp_n) // 111101_zzzzzzzzzz_110010_00000sssss OP_DEF(jsr) { - total_cycle += 1; // Table.7-5 + AddCycle(1); // Table.7-5 EnterBranch(rS2); r[1] = nip; ExitBranch(); @@ -1804,7 +2040,7 @@ OP_DEF(jsr) // 111101_zzzzzzzzzz_110011_00000sssss OP_DEF(jsr_n) { - total_cycle += 0; // Table.7-5 + AddCycle(0); // Table.7-5 EnterBranch(rS2); r[1] = nip + 4; ExitDelayBranch(); @@ -1856,7 +2092,7 @@ OP_DEF(ff0) // 111101_zzzzzzzzzz_111111_0000000000 OP_DEF(rte) { - total_cycle += 2; // Table.7-5 + AddCycle(2); // Table.7-5 SetPSR(epsr); @@ -1869,7 +2105,7 @@ OP_DEF(rte) // CPU は sxip は評価しない。snip は 0 なので、無効として nop 扱い。 // (実際はクロックが消費されるかも?) // sfip が VALID なので、そこから実行を再開する。 - if (snip & SIP_V) { + if ((snip & SIP_V)) { EnterBranch(snip & SIP_MASK); ExitBranch(); fip = sfip & SIP_MASK; @@ -1902,17 +2138,28 @@ OP_DEF(tbnd_2) } } -// 111111_zzzzzzzzzz_000011_zzzzzzzzzz -OP_DEF(getc) +// 111111_DDDDDSSSSS_000001_nnnnnnnnnn +OP_DEF(doscall) { - printf("getc inst\n"); - int c = getchar(); - r[2] = c; -} + // Human68k の DOS call エミュレーションのような何か。 + // 独自ニーモニックでは doscall rD, rS1, #nn と表記することにする。 + // + // Human68k の DOS コール #nn 番を実行し、戻り値を rD に書き込む。 + // 戻り値のない DOS コールでは rD に 0 を指定すること。 + // 引数は rS1 以降に一つずつ置く。m68k でのワードやバイト値の場合は + // 下位詰めするだけで上位ビットのクリア等は不要。 + // 処理するこちら側が必要なビット幅だけを取り出すこと。 + // + // また、ここでのコンソールは VM 内のコンソールではなくホストの標準入出力。 + // + // コールバックがセットされていて特権状態の時のみ発動する。 + // それ以外では現物同様不当命令扱い。 -// 111111_zzzzzzzzzz_000100_zzzzzzzzzz -OP_DEF(putc) -{ - // r3 を表示 - putchar(r[3]); + if (fline_callback && IsSuper()) { + if (fline_callback(this, fline_arg)) { + return; + } + } + + OP_FUNC(illegal); }