--- nono/vm/mpu680x0.cpp 2026/04/29 17:05:17 1.1.1.15 +++ nono/vm/mpu680x0.cpp 2026/04/29 17:05:28 1.1.1.17 @@ -12,13 +12,13 @@ #include "config.h" #include "debugger.h" #include "interrupt.h" +#include "m68030cache.h" #include "mainapp.h" +#include "mainbus.h" #include "scheduler.h" #include "syncer.h" #include "uimessage.h" #include "vectortable.h" -#include "vm.h" -#include static Syncer *gSyncer; @@ -33,25 +33,27 @@ MPU680x0Device::MPU680x0Device() SetCRP(0x00000001, 0); // FPU fpu_init(); + // キャッシュ + icache.reset(new m68030Cache()); + dcache.reset(new m68030Cache()); - // 割り込みイベントコールバック設定 - intr_event.func = ToEventCallback(&MPU680x0Device::InterruptCallback); - intr_event.Regist(GetName() + " Interrupt"); + // 例外カウンタ + excep_counter.resize(256); - // レジスタモニター + // レジスタモニター (サイズは Init で確定する) reg_monitor.func = ToMonitorCallback(&MPU680x0Device::MonitorUpdateReg); - reg_monitor.SetSize(79, 16); reg_monitor.Regist(ID_MONITOR_MPUREG); // ATC モニター atc_monitor.func = ToMonitorCallback(&MPU680x0Device::MonitorUpdateATC); -#if !defined(ATC_SINGLE) - atc_monitor.SetSize(131, m68030ATCTable::LINES + 3); -#else - atc_monitor.SetSize(31, 24); -#endif + atc_monitor.SetSize(114, m68030ATCTable::LINES + 6); atc_monitor.Regist(ID_MONITOR_MPUATC); + // キャッシュモニター + cache_monitor.func = ToMonitorCallback(&MPU680x0Device::MonitorUpdateCache); + cache_monitor.SetSize(48, 20 * 2 + 1); + cache_monitor.Regist(ID_MONITOR_MPUCACHE); + SetTrace(false); } @@ -60,6 +62,14 @@ MPU680x0Device::~MPU680x0Device() { } +void +MPU680x0Device::SetLogLevel(int loglevel_) +{ + inherited::SetLogLevel(loglevel_); + + atc.SetLogLevel(loglevel_); +} + // 初期化 bool MPU680x0Device::Init() @@ -101,8 +111,16 @@ MPU680x0Device::Init() has_fpu = 1; } + // レジスタモニタに FPU を載せるか。 + reg_monitor.SetSize(78, 10 + (has_fpu ? 9 : 0)); + vectortable = GetVectorTable(); + // 割り込みイベントコールバック設定 + intr_event.func = ToEventCallback(&MPU680x0Device::InterruptCallback); + intr_event.SetName(GetName() + " Interrupt"); + scheduler->RegistEvent(intr_event); + return true; } @@ -197,9 +215,7 @@ MPU680x0Device::ExecFull(Event& ev) intr_pending = false; // 前の命令実行後となるここで割り込みを起動 - int cycle = ExceptionInterrupt(); - - ev.time = cycle * clock_nsec; + ev.time = ExceptionInterrupt(); scheduler->StartEvent(ev); } else { if (cpu_state == CPU_STATE_NORMAL) { @@ -268,19 +284,19 @@ MPU680x0Device::Interrupt(int level) void MPU680x0Device::InterruptCallback(Event& ev) { - int level = ev.code; - int mask = reg.intr_mask; + uint level = ev.code; + uint mask = reg.intr_mask; // 信号線が 2 クロック期間安定していること云々は省略 - // ホールトなら何もしない + // 割り込みを起こすかどうかに関わらずここで更新。 + intr_level = level; + + // ホールトならここまで。 if (cpu_state == CPU_STATE_HALT) { return; } - // 割り込みを起こすかどうかに関わらずここで更新 - intr_level = level; - // 割り込み例外を起動するのは // o レベルがマスクより高い // o マスクに関係なく、レベルが下位から 7 に変化した場合 @@ -347,7 +363,8 @@ MPU680x0Device::SetFLineCallback(bool (* void MPU680x0Device::ops_reset() { - gVM->ResetByMPU(); + auto mainbus = GetMainbusDevice(); + mainbus->ResetByMPU(); } // ホールト状態 @@ -363,70 +380,6 @@ MPU680x0Device::Halt() UIMessage::Post(UIMessage::HALT); } -// MPU からのアクセスをエミュレート -uint64 -MPU680x0Device::Read8(uint32 addr) -{ - try { - return read_8(addr); - } catch (...) { - return (uint64)-1; - } -} - -uint64 -MPU680x0Device::Read16(uint32 addr) -{ - try { - return read_16(addr); - } catch (...) { - return (uint64)-1; - } -} - -uint64 -MPU680x0Device::Read32(uint32 addr) -{ - try { - return read_32(addr); - } catch (...) { - return (uint64)-1; - } -} - -uint64 -MPU680x0Device::Write8(uint32 addr, uint32 data) -{ - try { - write_8(addr, data); - return 0; - } catch (...) { - return (uint64)-1; - } -} - -uint64 -MPU680x0Device::Write16(uint32 addr, uint32 data) -{ - try { - write_16(addr, data); - return 0; - } catch (...) { - return (uint64)-1; - } -} - -uint64 -MPU680x0Device::Write32(uint32 addr, uint32 data) -{ - try { - write_32(addr, data); - return 0; - } catch (...) { - return (uint64)-1; - } -} - // モニター更新 (レジスタウィンドウ) void MPU680x0Device::MonitorUpdateReg(Monitor *, TextScreen& screen) @@ -447,7 +400,7 @@ MPU680x0Device::MonitorUpdateReg(Monitor // データレジスタ、アドレスレジスタ for (int i = 0; i < 4; i++) { - screen.Print(0, i, "D%d:%08x D%d:%08x A%d:%08x A%d:%08x", + screen.Print(0, i, "D%u:%08x D%u:%08x A%u:%08x A%u:%08x", i + 0, tmp.R[i], i + 4, tmp.R[i + 4], i + 0, tmp.R[i + 8], @@ -465,76 +418,99 @@ MPU680x0Device::MonitorUpdateReg(Monitor (tmp.ccr.IsV()) ? 'V' : '-', (tmp.ccr.IsC()) ? 'C' : '-'); - // VBR - screen.Print(x, 1, "VBR:%08x", tmp.vbr); - // *SP uint ms = (tmp.s ? 2 : 0) | (tmp.m ? 1 : 0); // T1 T0 S M if (ms == 3) { // Supervisor (Master) mode (A7=MSP, USP/ISP) - screen.Print(x, 2, TA::Disable, "USP:%08x", tmp.usp); - screen.Print(x, 3, TA::Disable, "ISP:%08x", tmp.isp); + screen.Print(x, 1, TA::Disable, "USP:%08x", tmp.usp); + screen.Print(x, 2, TA::Disable, "ISP:%08x", tmp.isp); } else if (ms == 2) { // Supervisor (Interrupt) mode (A7=ISP, USP/MSP) - screen.Print(x, 2, TA::Disable, "USP:%08x", tmp.usp); - screen.Print(x, 3, TA::Disable, "MSP:%08x", tmp.msp); + screen.Print(x, 1, TA::Disable, "USP:%08x", tmp.usp); + screen.Print(x, 2, TA::Disable, "MSP:%08x", tmp.msp); } else { // User mode (A7=USP, ISP/MSP) - screen.Print(x, 2, TA::Disable, "ISP:%08x", tmp.isp); - screen.Print(x, 3, TA::Disable, "MSP:%08x", tmp.msp); + screen.Print(x, 1, TA::Disable, "ISP:%08x", tmp.isp); + screen.Print(x, 2, TA::Disable, "MSP:%08x", tmp.msp); } // 6列目 - // PC: 01234567 - // SFC:1 DFC:1 - // CACR:01234567 - // CAAR:01234567 + // PC: 01234567 + // SFC:1 DFC:1 + // VBR:01234567 x = 66; - screen.Print(x, 0, "PC: %08x", tmp_ppc); - screen.Print(x, 1, "SFC:%d DFC:%d", tmp.sfc, tmp.dfc); - screen.Print(x, 2, "CACR:%08x", tmp.cacr); - screen.Print(x, 3, "CAAR:%08x", tmp.caar); + screen.Print(x, 0, "PC: %08x", tmp_ppc); + screen.Print(x, 1, "SFC:%u DFC:%u", tmp.GetSFC(), tmp.GetDFC()); + screen.Print(x, 2, "VBR:%08x", tmp.vbr); - // XXX どこに置くのがいいか switch (tmp_state) { case CPU_STATE_NORMAL: - screen.Puts(25, 4, "State: Running"); + screen.Puts(50, 3, "State: Running"); break; case CPU_STATE_STOP: - screen.Puts(25, 4, "State: STOP instruction"); + screen.Puts(50, 3, "State: STOP instruction"); break; case CPU_STATE_HALT: - screen.Puts(25, 4, TA::On, "State: Double Bus Fault"); + screen.Puts(50, 3, TA::On, "State: Double Bus Fault"); break; default: - screen.Print(25, 4, TA::On, "corrupted cpu_state=%d", tmp_state); + screen.Print(50, 3, TA::On, "corrupted cpu_state=%u", tmp_state); break; } + // Cache + y = 4; + screen.Puts(0, y++, ""); + screen.Print(0, y, "CACR:%08x(", tmp.cacr); + screen.Puts(14, y, TA::OnOff(tmp.cacr & M68K::CACR_WA), "WA"); + screen.Puts(17, y, TA::OnOff(tmp.cacr & M68K::CACR_DBE), "DBE"); + screen.Puts(21, y, "- -"); + screen.Puts(25, y, TA::OnOff(tmp.cacr & M68K::CACR_FD), "FD"); + screen.Puts(28, y, TA::OnOff(tmp.cacr & M68K::CACR_ED), "ED"); + screen.Puts(31, y, "000"); + screen.Puts(35, y, TA::OnOff(tmp.cacr & M68K::CACR_IBE), "IBE"); + screen.Puts(39, y, "- -"); + screen.Puts(43, y, TA::OnOff(tmp.cacr & M68K::CACR_FI), "FI"); + screen.Puts(46, y, TA::OnOff(tmp.cacr & M68K::CACR_EI), "EI"); + screen.Putc(48, y, ')'); + screen.Print(50, y, "CAAR:%08x", tmp.caar); + y++; + // MMU x = 0; - y = 4; screen.Puts(x, y++, ""); screen.Print(x, y, "SRP:%08x_%08x", tmp.srp.h, tmp.srp.l); screen.Print(x, y + 1, "CRP:%08x_%08x", tmp.crp.h, tmp.crp.l); - x += 23; + x += 25; for (int i = 0; i < 2; i++) { uint32 tt = tmp.tt[i]; - screen.Print(x, y + i, "TT%d:%08x(%c%c%c)", + screen.Print(x, y + i, + "TT%u:%08x(%c%c%c Addr=$%02x Mask=$%02x FC=%u FCMask=%u)", i, tt, (tt & m68030TT::E) ? 'E' : '-', (tt & m68030TT::CI) ? 'C' : '-', - (tt & m68030TT::RWM) ? '-' : ((tt & m68030TT::RW) ? 'R' : 'W')); - } - x += 19; - screen.Print(x, y, "TC:%08x(%c%c%c)", + (tt & m68030TT::RWM) ? '-' : ((tt & m68030TT::RW) ? 'R' : 'W'), + (tt >> 24), + (tt >> 16) & 0xff, + (tt >> 4) & 0x07, + (tt ) & 0x07); + } + y += 2; + screen.Print(0, y, + "TC:%08x(%c%c%c IS=$%x TIA-D=%x:%x:%x:%x PS=$%x)", tmp.tc, (tmp.tc & m68030TC::TC_E) ? 'E' : '-', (tmp.tc & m68030TC::TC_SRE) ? 'S' : '-', - (tmp.tc & m68030TC::TC_FCL) ? 'F' : '-'); - y++; - screen.Print(x, y, "MMUSR: %04x(", tmp.mmusr); - x += 12; + (tmp.tc & m68030TC::TC_FCL) ? 'F' : '-', + (tmp.tc >> 16) & 0xf, + (tmp.tc >> 12) & 0xf, + (tmp.tc >> 8) & 0xf, + (tmp.tc >> 4) & 0xf, + (tmp.tc ) & 0xf, + (tmp.tc >> 20) & 0xf); + x = 49; + screen.Print(x, y, "MMUSR:%04x(", tmp.mmusr); + x += 11; screen.Putc(x++, y, TA::OnOff(tmp.mmusr & m68030MMUSR::B) | 'B'); screen.Putc(x++, y, TA::OnOff(tmp.mmusr & m68030MMUSR::L) | 'L'); screen.Putc(x++, y, TA::OnOff(tmp.mmusr & m68030MMUSR::S) | 'S'); @@ -545,14 +521,25 @@ MPU680x0Device::MonitorUpdateReg(Monitor screen.Puts(x, y, "- -"); x += 3; screen.Putc(x++, y, TA::OnOff(tmp.mmusr & m68030MMUSR::T) | 'T'); - screen.Print(x, y, "---N=%d)", tmp.mmusr & m68030MMUSR::N); + screen.Print(x, y, "---N=%u)", tmp.mmusr & m68030MMUSR::N); + y++; // FPU + if (has_fpu != 0) { + MonitorUpdateFPU(screen, y, tmp); + } +} + +// モニタ (FPU) +void +MPU680x0Device::MonitorUpdateFPU(TextScreen& screen, int y, m68kreg& tmp) +{ + int x; + x = 0; - y = 7; screen.Puts(x, y++, ""); for (int i = 0; i < 8; i++) { - screen.Print(x, y + i, "FP%d:%04x_%08x_%08x (%-20s)", + screen.Print(x, y + i, "FP%u:%04x_%08x_%08x (%-20s)", i, tmp.fpframe.fpf_regs[i * 3 + 0] >> 16, tmp.fpframe.fpf_regs[i * 3 + 1], @@ -614,12 +601,23 @@ MPU680x0Device::MonitorUpdateReg(Monitor screen.Print(x, y++, "FPIAR:%08x", tmp.fpframe.fpf_fpiar); } +static int +sort_atc(const void *a, const void *b) +{ + const uint32 *ua = (const uint32 *)a; + const uint32 *ub = (const uint32 *)b; + return *ub - *ua; +} + + // モニター更新 (ATC) void MPU680x0Device::MonitorUpdateATC(Monitor *, TextScreen& screen) { m68030ATCTable *table; -#if !defined(ATC_SINGLE) + + screen.Clear(); + struct { int fc; const char *name; @@ -630,98 +628,201 @@ MPU680x0Device::MonitorUpdateATC(Monitor { 6, "Super/Program" }, }; - screen.Clear(); - - // 行数は左端にだけ - for (int i = 0; i < m68030ATCTable::LINES; i++) { - screen.Print(0, i + 2, "%02d:", i); - } - - for (int t = 0; t < countof(fclist); t++) { - int x = t * 32 + 4; + for (uint t = 0; t < countof(fclist); t++) { + int x = t * 29; int fc = fclist[t].fc; const char *name = fclist[t].name; - double r; - int i; - table = &atc.tables[fc]; - screen.Print(x, 0, "FC=%d %s", fc, name); - screen.Puts(x, 1, "LAddr PAddr Flag Hit% Age"); - // 先に統計用の母数を計算 - uint64 total = 0; - for (i = 0; i < countof(table->hit); i++) { - total += table->hit[i]; + table = atc.fctables[fc]; + screen.Print(x, 0, "FC=%u %s", fc, name); + screen.Puts(x, 1, "No. LAddr PAddr Flag"); + + std::array sbuf; + for (uint i = 0; i < m68030ATCTable::LINES; i++) { + sbuf[i * 2 + 0] = table->line[i].age; + sbuf[i * 2 + 1] = i; } - total += table->hit_head; + qsort(&sbuf[0], m68030ATCTable::LINES, sizeof(uint32) * 2, sort_atc); // エントリ表示 - for (i = 0; i < countof(table->line); i++) { - m68030ATCLine& a = table->line[i]; + for (uint i = 0; i < m68030ATCTable::LINES; i++) { + int idx = sbuf[i * 2 + 1]; + m68030ATCLine& a = table->line[idx]; TA attr; if (a.IsInvalid()) { attr = TA::Disable; - } else if (table->head == &a) { - attr = TA::Em; } else { attr = TA::Normal; } - screen.Print(x, i + 2, attr, "%08x %08x %c%c%c", + screen.Print(x, i + 2, attr, "[%02x] %08x %08x %c%c%c%c", + idx, a.GetLAddr(), a.GetPAddr(), a.IsBusError() ? 'B' : '-', + a.IsCInhibit() ? 'C' : '-', a.IsWProtect() ? 'P' : '-', a.IsModified() ? 'M' : '-'); } - // ヘッドのヒット率 - screen.Puts(x + 0, i + 2, "head"); - r = (double)table->hit_head * 100 / total; - if (!std::isnan(r)) { - screen.Print(x + 5, i + 2, "%4.1f%%", r); - } - // 配列のヒット率とミス率 - screen.Puts(x + 17, i + 2, "miss"); - for (i = 0; i < countof(table->hit); i++) { - r = (double)(table->hit[i] * 100) / total; - if (!std::isnan(r)) { - screen.Print(x + 22, i + 2, "%4.1f%%", r); + + // ヒット率とミス率。 + uint64 total = 0; + uint64 tthit = 0; + uint64 miss = 0; + bool tt_once_hit; + + { + std::unique_lock lock(table->atchist_mtx); + for (int i = 0; i < table->atchist.Length(); i++) { + const auto& s = table->atchist.Peek(i); + total += s.total; + tthit += s.tthit; + miss += s.miss; } + tt_once_hit = table->atchist_tt_once_hit; } - // カウンタ - // 分かりやすさのため最大値からの差(距離)を表示 - for (i = 0; i < countof(table->line); i++) { - m68030ATCLine& a = table->line[i]; - if (!a.IsInvalid()) { - screen.Print(x + 27, i + 2, "%4d", table->head->age - a.age); + + int y = m68030ATCTable::LINES + 2; + screen.Puts(x + 1, y, "TT Match"); + if (tt_once_hit) { + screen.Print(x + 10, y, "%5.1f%%", (double)(tthit * 100) / total); + } else { + screen.Puts(x + 12, y, "0"); + } + y++; + + screen.Puts(x + 1, y, "ATC Match"); + if (total == 0) { + screen.Puts(x + 12, y, "0"); + } else { + screen.Print(x + 10, y, "%5.1f%%", + (double)((total - miss - tthit) * 100) / total); + } + y++; + + screen.Puts(x + 1, y, "ATC Miss"); + if (miss == 0) { + screen.Puts(x + 12, y, "0"); + } else { + screen.Print(x + 10, y, "%5.1f%%", (double)(miss * 100) / total); + } + y++; + } + + screen.Print(0, m68030ATCTable::LINES + 5, + "Note: The real 68030 ATC is single FC-mixed 22-entries. " + "This ATC with FC-separated %u-entries is by nono.", + m68030ATCTable::LINES); +} + +// 統計情報を更新 (Syncer から呼ばれる) +void +MPU680x0Device::CalcStat() +{ + // ATC のヒット率。 + for (auto& table : atc.tables) { + // ここはロックなしでやってしまう + m68030ATCStat s; + s.total = table->atcstat.total; + s.tthit = table->atcstat.tthit; + s.miss = table->atcstat.miss; + table->atcstat.total = 0; + table->atcstat.tthit = 0; + table->atcstat.miss = 0; + + // こっちはロックする + { + std::unique_lock lock(table->atchist_mtx); + + table->atchist.EnqueueForce(s); + if (__predict_false(s.tthit != 0)) { + table->atchist_tt_once_hit = true; } } } -#else - m68030ATCLine *a; - int i; - table = &atc.tables[0]; + // キャッシュのヒット率。 + icache->CalcStat(); + dcache->CalcStat(); +} + +static char +icache_fc2str(const m68030CacheLine& l) +{ + return l.IsSuper() ? 'S' : 'U'; +} + +static char +dcache_fc2str(const m68030CacheLine& l) +{ + return "0U234S67"[l.GetFC()]; +} + +// モニタ更新 (キャッシュ) +void +MPU680x0Device::MonitorUpdateCache(Monitor *, TextScreen& screen) +{ + int y; + screen.Clear(); - screen.Puts(0, 0, "No. FC LAddr PAddr Flag"); - for (a = table->head, i = 0; a; a = a->next, i++) { - screen.Print(0, i + 1, "%02d:", i); - if (!a->IsInvalid()) { - char dp; - switch (a->fc & 3) { - case 1: dp = 'D'; break; - case 2: dp = 'P'; break; - default: dp = '?'; break; + // + // 01234567890123456789012345678901234567890123456789 + // Tag Address Data + // S.012345'00: 00000000 00000000 -------- 00000000 + + y = 0; + screen.Puts(0, y++, ""); + y = MonitorUpdateCache1(screen, y, icache.get(), icache_fc2str); + + y++; + screen.Puts(0, y++, ""); + MonitorUpdateCache1(screen, y, dcache.get(), dcache_fc2str); +} + +// モニタ更新 (キャッシュ片方の下請け) +int +MPU680x0Device::MonitorUpdateCache1(TextScreen& screen, int y, + m68030Cache *cache, char (*fc2str)(const m68030CacheLine&)) +{ + screen.Puts(0, y, "Tag Address Data[0]"); + screen.Puts(13 + 1 * 9, y, "[1]"); + screen.Puts(13 + 2 * 9, y, "[2]"); + screen.Puts(13 + 3 * 9, y, "[3]"); + y++; + + TA attr = cache->enable ? TA::Normal : TA::Disable; + for (int idx = 0; idx < cache->line.size(); idx++) { + auto& l = cache->line[idx]; + screen.Print(0, y, attr, "%c.%06x'%x0:", + fc2str(l), (l.TagAddr() >> 8), idx); + + for (int i = 0; i < 4; i++) { + if (l.valid[i]) { + screen.Print(13 + 9 * i, y, attr, "%08x", l.data[i]); + } else { + screen.Puts(13 + 9 * i, y, TA::Disable, "--------"); } - screen.Print(4, i + 1, "%d(%c%c):%08x %08x %c%c%c", - a->fc, - (a->fc & 4) ? 'S' : 'U', - dp, - a->GetLAddr(), a->GetPAddr(), - a->IsBusError() ? 'B' : '-', - a->IsWProtect() ? 'W' : '-', - a->IsModified() ? 'M' : '-'); } + y++; } -#endif + + // 統計。 + float hit = 0.0; + { + std::unique_lock lock(cache->hist_mtx); + int len = cache->hist.Length(); + if (len != 0) { + for (int i = 0; i < cache->hist.Length(); i++) { + const auto& s = cache->hist.Peek(i); + hit += s.hit; + } + hit /= len; + } + } + + y++; + screen.Print(1, y++, attr, "Cache Read Hit %5.1f%%", hit); + + return y; }