--- nono/m88xx0/m88100.h 2026/04/29 17:04:40 1.1.1.4 +++ nono/m88xx0/m88100.h 2026/04/29 17:05:05 1.1.1.11 @@ -19,6 +19,7 @@ #define CPU_REQ_PROMPT (0x00000008) // デバッガプロンプト #define CPU_REQ_RELEASE (0x00000010) // 命令後に CPU 実行中断 #define CPU_REQ_INTR (0x00000020) // 命令後に割り込みチェック +#define CPU_REQ_RESET (0x00000040) // リセット例外 // m88100 レジスタイメージを保持する構造体。 // この構造体はコピーや比較をするので、ポインタとかは置かないこと。 @@ -70,6 +71,123 @@ struct m88100reg static const uint32 DM_D1 = DM_SIZE_W | DM_DOUB1; static const uint32 DM_D2 = DM_SIZE_W; + // FPECR(fcr0) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------+---------------+---------------+-+-+-+-+-+-+-+-+ + // | |I|U|P|O|D|U|O|I| + // | 0 |O|N|V|P|Z|F|V|N| + // +---------------+---------------+---------------+-+-+-+-+-+-+-+-+ + static const uint32 FPECR_MASK = 0x000000ff; // 有効ビットマスク + static const uint32 FPECR_FIOV = 0x00000080; // FP Integer Overflow + static const uint32 FPECR_FUNIMP= 0x00000040; // FP Unimplemented + static const uint32 FPECR_FPRV = 0x00000020; // FP Privilege Violation + static const uint32 FPECR_FROP = 0x00000010; // FP Reserved Operand + static const uint32 FPECR_FDVZ = 0x00000008; // FP Divice-by-Zero + static const uint32 FPECR_FUNF = 0x00000004; // FP Underflow + static const uint32 FPECR_FOVF = 0x00000002; // FP Overflow + static const uint32 FPECR_FINX = 0x00000001; // FP Inexact + + // FPHS1(fcr1) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +-+---------------------+---------------------------------------+ + // |S| Exponent | High order 20 bits of Mantissa | + // +-+---------------------+---------------------------------------+ + + // FPLS1(fcr2) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------------------------------------------------------+ + // | Low order bits of Mantissa or Integer Operand | + // +---------------------------------------------------------------+ + + // FPHS2(fcr3) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +-+---------------------+---------------------------------------+ + // |S| Exponent | High order 20 bits of Mantissa | + // +-+---------------------+---------------------------------------+ + + // FPLS2(fcr4) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------------------------------------------------------+ + // | Low order bits of Mantissa or Integer Operand | + // +---------------------------------------------------------------+ + + // FPPT(fcr5) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------+---------------+---------+---+---+---+---------+ + // | 0 | OPCODE |T1 |T2 |TD | DestReg | + // +---------------+---------------+---------+---+---+---+---------+ + static const uint32 FPPT_MASK = 0x0000ffff; // 有効ビットマスク + static const uint32 FPPT_OPCODE = 0x0000f800; // Opcode + static const uint32 FPPT_T1 = 0x00000600; // Source1 Operand Size + static const uint32 FPPT_T2 = 0x00000180; // Source2 Operand Size + static const uint32 FPPT_TD = 0x00000060; // Destination Size + static const uint32 FPPT_DEST = 0x0000001f; // Destination Register No. + + // FPRH(fcr6) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +-----+-+-+-+-+-+-----+---------+-------------+-----------------+ + // |S|RM |G|R|S|A| 0 | High Order Bits of Mantissa | + // +-----+-+-+-+-+-+-----+---------+-------------+-----------------+ + static const uint32 FPRH_MASK = 0xfe1fffff; // 有効ビットマスク + static const uint32 FPRH_SIGN = 0x80000000; // Result Sign + static const uint32 FPRH_RNDMODE= 0x60000000; // Rounding Mode for Res. + static const uint32 FPRH_GUARD = 0x10000000; // Guard Bit for Result + static const uint32 FPRH_ROUND = 0x08000000; // Round Bit for Result + static const uint32 FPRH_STICKY = 0x04000000; // Sticky Bit for Result + static const uint32 FPRH_ADDONE = 0x02000000; // Add One + static const uint32 FPRH_MANT = 0x001fffff; // Mantissa(High) + static const uint32 FPRH_1 = 0x00100000; // 隠し実数部の 1 + + // FPRL(fcr7) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------------------------------------------------------+ + // | Low Order Bits of Mantissa | + // +---------------------------------------------------------------+ + + // FPIT(fcr8) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +-----------------------+-------+---------+-+-+-+-+-+-+---------+ + // | | | |D|E|E|E|E|E| | + // | RESEXP (12bits) | 0 | OPCODE |S|V|Z|U|O|N| DestReg | + // +-----------------------+-------+---------+-+-+-+-+-+-+---------+ + static const uint32 FPIT_MASK = 0xfff0ffff; // 有効ビットマスク + static const uint32 FPIT_RESEXP = 0xfff00000; // Result Exponent + static const uint32 FPIT_OPCODE = 0x0000f800; // Opcode + static const uint32 FPIT_DESTSIZ= 0x00000400; // Destination Size + static const uint32 FPIT_EFINV = 0x00000200; // Enable Invalid Op. + static const uint32 FPIT_EFDVZ = 0x00000100; // Enable Divide-by-Zero + static const uint32 FPIT_EFUNF = 0x00000080; // Enable Underflow + static const uint32 FPIT_EFOVF = 0x00000040; // Enable Overflow + static const uint32 FPIT_EFINX = 0x00000020; // Enable Inexact + static const uint32 FPIT_DEST = 0x0000001f; // Destination Register No. + + // FPSR(fcr62) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------+---------------+---------------+-----+-+-+-+-+-+ + // | |A|A|A|A|A| + // | 0 |V|D|U|O|I| + // +---------------+---------------+---------------+-----+-+-+-+-+-+ + static const uint32 FPSR_MASK = 0x0000001f; // 有効ビットマスク + static const uint32 FPSR_AFINV = 0x00000010; // Acc. Invalid Op. Flag + static const uint32 FPSR_AFDVZ = 0x00000008; // Acc. Divide-by-Zero Flag + static const uint32 FPSR_AFUNF = 0x00000004; // Acc. Underflow Flag + static const uint32 FPSR_AFOVF = 0x00000002; // Acc. Overflow Flag + static const uint32 FPSR_AFINX = 0x00000001; // Acc. Inexact Flag + + // FPCR(fcr63) + // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + // +---------------+---------------+---+-----------+-----+-+-+-+-+-+ + // | | | |E|E|E|E|E| + // | 0 |RM | |V|D|U|O|I| + // +---------------+---------------+---+-----------+-----+-+-+-+-+-+ + static const uint32 FPCR_MASK = 0x0000c01f; // 有効ビットマスク + static const uint32 FPCR_RM = 0x0000c000; // Rounding Mask + static const uint32 FPCR_EFINV = 0x00000010; // Enable Invalid Operation + static const uint32 FPCR_EFDVZ = 0x00000008; // Enable Divide-by-Zero + static const uint32 FPCR_EFUNF = 0x00000004; // Enable Underflow + static const uint32 FPCR_EFOVF = 0x00000002; // Enable Overflow + static const uint32 FPCR_EFINX = 0x00000001; // Enable Inexact + // 頻繁にアクセスしそうな変数はオフセットの若いほうに持ってきておく。 // 実行中の命令 @@ -129,6 +247,9 @@ struct m88100reg bool IsSuper() const { return (psr & PSR_SUPER) != 0; } bool IsUser() const { return (psr & PSR_SUPER) == 0; } + // 割り込みが許可なら true + bool IsIntrEnable() const { return (psr & PSR_IND) == 0; } + // SFU1(FPU)が有効なら true bool IsFPUEnable() const { return (psr & PSR_SFD1) == 0; } @@ -145,6 +266,7 @@ struct m88100reg psr |= ((cy & 1) << 28); } + static const uint32 fcr_mask[11]; static const char * const sipname[3]; static const char * const dmt_en_str[16]; }; @@ -166,11 +288,18 @@ class m88kcpu : public m88100reg m88kcpu(); virtual ~m88kcpu(); - void Reset(uint32 reset_vector); - uint32 Run(uint64 request); + void PowerOn(); + void RequestReset(); + + uint32 Run(uint32 delta); void Release(); - void Interrupt(); + // 仮想時刻を返す + uint64 GetVTime() const { return total_vtime + Cycle2Vtime(used_cycle); } + + // 割り込み信号線が変化したことの通知を受ける。 + // level は 0 か 1。 + void Interrupt(int level); // PID.VERSION を設定する (初期化時に呼ぶ) void SetVersion(uint32 version); @@ -196,12 +325,12 @@ class m88kcpu : public m88100reg return (OpIsBusErr(op) ? ~op : op) & DELAYSLOT; } - // 積算実行サイクル数を取得 - uint64 GetTotalCycle() const { return total_cycle; } - // サイクルを加算 (m88200 から使う) - // 今は単純に加算しているだけで正確ではない - void AddCycle(uint64 cycle) { total_cycle += cycle; } + // 今は単純に加算しているだけでオーバーラップなどは考慮してない + void AddCycle(int32 wait) { used_cycle += wait; } + + // MPU クロックを設定 + void SetClockSpeed(uint32 clock_khz_); std::atomic atomic_reqflag {}; // リクエストフラグ @@ -210,14 +339,27 @@ class m88kcpu : public m88100reg m88200 cmmu[2] {}; // ブランチ履歴 (例外履歴を含む) - BranchHistory_m88xx0 brhist {}; + BranchHistory_m88xx0 brhist { BranchHistory::BrHist }; // 例外履歴のみ - BranchHistory_m88xx0 exhist {}; + BranchHistory_m88xx0 exhist { BranchHistory::ExHistOnly }; + + // 32bit 命令コードからディスパッチ用の 12bit に変換。 + static uint32 op32_to_12(uint32 op) { + return ((op >> 20) & 0x0fc0) | ((op >> 10) & 0x003f); + } // 例外名を返す static const char *GetExceptionName(int vector); + // 疑似 STOP 状態に入る機能を有効にする場合 true + bool pseudo_stop_enable {}; + + // DOS call エミュレーションコールバックを設定する + void SetFLineCallback(bool (*callback)(m88kcpu *, void *), void *arg); + private: + void Reset(); + uint64 fetch() { nip = fip; opF = cmmu[0].load_32(fip); @@ -239,13 +381,33 @@ class m88kcpu : public m88100reg // CMMU に S/U 信号を出す cmmu[0].SetSuper(IsSuper()); cmmu[1].SetSuper(IsSuper()); + // 割り込み許可なら割り込みチェック + if (IsIntrEnable()) { + atomic_reqflag |= CPU_REQ_INTR; + } } + // FPxS1, FPxS2 レジスタを設定 + void SetFPxS(uint32& h, uint32& l, double src, uint32 t); + void SetFPS1(double src, uint32 t1) { SetFPxS(fphs1, fpls1, src, t1); } + void SetFPS2(double src, uint32 t2) { SetFPxS(fphs2, fpls2, src, t2); } + void SetFPS1(uint32 src) { fpls1 = src; } + void SetFPS2(uint32 src) { fpls2 = src; } + // FP Result を FPIT, FPRH, FPRL レジスタに設定 + void SetFPRx(double src); + void Exception(int vec); void ExceptionCore(int vec, ExceptionKind cause); - void ReadDataException(uint32 addr, uint32 flag); - void WriteDataException(uint32 addr, uint32 data, uint32 flag); - void XmemDataException(uint32 addr, uint32 data, uint32 flag); + uint32 Calcdmt(uint32 flag); + void ReadDataException32(uint32 addr, uint32 flag); + void ReadDataException64(uint32 addr, uint32 flag); + void WriteDataException32(uint32 addr, uint32 flag); + void WriteDataException64(uint32 addr, uint32 flag); + void XmemDataException(uint32 addr, uint32 flag); + void FPPreciseException(uint32 cause, double s2); + void FPPreciseException(uint32 cause, double s1, double s2); + void FPPreciseException(uint32 cause); + void FPImpreciseException(uint32 cause); // ブランチ処理用 void EnterBranch(uint32 toaddr); @@ -257,6 +419,8 @@ class m88kcpu : public m88100reg bool ldst_usr(uint32& usr); uint32 ldst_scale(uint32 size); bool ldst_align(uint32 size, uint32& addr); + // 浮動小数点数命令用の処理 + void ops_int(int mode); #define OP_PROTO(name) void __CONCAT(op_,name)() #include "m88100ops.h" @@ -268,14 +432,56 @@ OP_PROTO(illegal); } void fpu_unimpl(); - // 32bit 命令コードからディスパッチ用の 12bit に変換。 - static uint32 op32_to_12(uint32 op) { - return ((op >> 20) & 0x0fc0) | ((op >> 10) & 0x003f); + // 仮想時刻 [nsec] + uint64 total_vtime {}; + + // サイクル数を仮想時間 [nsec] に変換 + uint64 Cycle2Vtime(int32 cycle) const { + return (int64)cycle * c2v; + } + + // 仮想時間 [nsec] をサイクル数に変換 + uint32 Vtime2Cycle(uint32 vtime) const { + return ((uint64)vtime * v2c_factor) >> v2c_shift; + } + + // 今のところサイクルを正確に実装するのは無理なので + // 適当に全部積み上げてある。 + uint32 used_cycle {}; // 現在のターンでこれまでに消費したサイクル数 + uint32 goal_cycle {}; // 現在のターンで消費すべきサイクル数 + uint32 clock_khz {}; // MPU クロック [kHz] + uint32 c2v {}; // サイクル数を nsec に変換する際の係数 + uint64 v2c_factor {}; // nsec をサイクル数に変換する際の乗算係数 + uint32 v2c_shift {}; // nsec をサイクル数に変換する際のシフト係数 + + // ペンディング中の割り込みレベル + int intr_pending {}; + + // float,double と整数値の相互変換 + static float u2f(uint32 u) { + float v; + memcpy(&v, &u, sizeof(v)); + return v; + } + static double u2d(uint64 u) { + double v; + memcpy(&v, &u, sizeof(v)); + return v; + } + static uint32 f2u(float v) { + uint32 u; + memcpy(&u, &v, sizeof(u)); + return u; + } + static uint64 d2u(double v) { + uint64 u; + memcpy(&u, &v, sizeof(u)); + return u; } - // 積算実行サイクル数 - // 今のところ正確に実装するのは無理なので、適当に全部積み上げてある。 - uint64 total_cycle {}; + // DOS call エミュレーション + bool (*fline_callback)(m88kcpu *, void *) = NULL; + void *fline_arg {}; // 例外名 static const char * const exception_names[];