--- nono/m680x0/m68030mmu.cpp 2026/04/29 17:04:31 1.1.1.2 +++ nono/m680x0/m68030mmu.cpp 2026/04/29 17:05:22 1.1.1.9 @@ -1,16 +1,12 @@ // // nono -// Copyright (C) 2017 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" -#include "mystring.h" -#include "m68030core.h" -#include "m68030bus.h" -#include "m68030ea.h" +#include "mpu680x0.h" #include "m68030mmu.h" -#include "bus.h" -#include "mpu.h" +#include "mainbus.h" #define TIA (0) #define TIB (1) @@ -29,8 +25,8 @@ // ディスクリプタ #define MMU_DESC_S (0x00000100) #define MMU_DESC_CI (0x00000040) -#define MMU_DESC_M (0x00000010) -#define MMU_DESC_U (0x00000008) +#define MMU_DESC_M (0x00000010) +#define MMU_DESC_U (0x00000008) #define MMU_DESC_WP (0x00000004) #define MMU_DESC_DT (0x00000003) @@ -46,63 +42,60 @@ enum ATCtype { // デバッグ表示 // -#define IF_DEBUG if (gMPU->loglevel > 0) - // デバッグ表示のため値の表示に必要な桁数を返す #define NIBBLE(x) ( (x) == 0 ? 1 : ((x) + 3) / 4 ) -// インデント付き fmt, ap を出力 -static void viprintf(int indent, const char *fmt, va_list) __printflike(2, 0); -static void -viprintf(int indent, const char *fmt, va_list ap) -{ - static const char *indentstr = " "; // 長さは適当 - char buf[1024]; - - vsnprintf(buf, sizeof(buf), fmt, ap); - cpulog(3, "%s%s", &indentstr[strlen(indentstr) - indent], buf); -} - -// IF_DEBUG 中だと分かってるところではこっちを使う -static void IPRINTF(int indent, const char *fmt, ...)__printflike(2, 3); -static void -IPRINTF(int indent, const char *fmt, ...) +// MMU デバッグモードなら ATC エントリ a を表示 +#define FILL_PRINT(fc, a) do { \ + if (__predict_false(loglevel >= 3)) { \ + cpu->putlogf(3, [&] { return (a)->fill_print(fc); }); \ + } \ +} while (0) + +// インデント付き string_format +static std::string indent_format(int indent, const char *fmt, ...) + __printflike(2, 3); +static std::string +indent_format(int indent, const char *fmt, ...) { va_list ap; + char *buf; va_start(ap, fmt); - viprintf(indent, fmt, ap); + vasprintf(&buf, fmt, ap); va_end(ap); -} -// MMU デバッグモードならインデント付き fmt... を出力 -#define DPRINTF(indent, ...) do { \ - IF_DEBUG \ - IPRINTF(indent, __VA_ARGS__); \ -} while(0) + std::string rv(indent, ' '); + rv += buf; + free(buf); -// MMU デバッグモードなら ATC エントリ a を表示 -#define FILL_PRINT(fc, a) do { \ - IF_DEBUG \ - (a)->fill_print(fc); \ -} while (0) + return rv; +} +#define MMULOG(lv, indent, arg...) do { \ + if (__predict_false(loglevel >= (lv))) { \ + cpu->putlogf(lv, [&, lam_func = __func__] { \ + (void)lam_func; \ + return indent_format(indent, arg); \ + }); \ + } \ +} while (0) // テーブルサーチ操作クラス -class m68030MMU +class m68030MMU final { public: - m68030MMU(m68kcpu *); - virtual ~m68030MMU() { } + m68030MMU(MPU680x0Device *, int loglevel_); + ~m68030MMU(); // サーチを実行 - void search(); + void search(int level); // ATC にエントリを作成 void make_atc(m68030ATCLine *); // ATC にエントリを作成 (デバッガアクセス用) - uint64 make_atc_debugger(); + busaddr make_atc_debugger() const; private: // リミットチェック @@ -112,13 +105,10 @@ class m68030MMU bool fetch_desc(uint32, int); // MMU による物理メモリ読み込み - uint64 phys_read_32(uint32 addr); + busdata phys_read_32(uint32 addr); // MMU による物理メモリ書き込み - uint64 phys_write_32(uint32 addr, uint32 data); - - // デバッガ読み込み - uint64 phys_peek_32(uint32 addr); + busdata phys_write_32(uint32 addr, uint32 data); // ディスクリプタを表示用に (デバッグ用) std::string sprintf_desc(); @@ -127,34 +117,38 @@ class m68030MMU const char *dtname(uint32 dt); public: - ATCtype m_type; // 状態 - int m_s; // S ビット - int m_wp; // WP ビット - int m_m; // ページディスクリプタの M が立っているか - int m_l; // サーチ中にリミットを越えた - bool m_berr; // テーブルサーチ中にバスエラー - bool m_ptest; // PTEST[RW] 命令中なら真 - bool m_debugger; // デバッガからのアクセスなら true にする + ATCtype m_type {}; // 状態 + bool m_s {}; // S ビット + bool m_wp {}; // WP ビット + bool m_ci {}; // CI ビット + bool m_m {}; // ページディスクリプタの M が立っているか + bool m_lim {}; // サーチ中にリミットを越えた + bool m_berr {}; // テーブルサーチ中にバスエラー + bool m_ptest {}; // PTEST[RW] 命令中なら真 + bool m_debugger {}; // デバッガからのアクセスなら true にする + int x {}; // 段数 TIA〜TID, FCL + uint32 m_lastaddr {}; // 最後にアクセスしたディスクリプタアドレス(PTEST) private: - int x; // 段数 TIA〜TID, FCL - int m_lv[MAX_LV]; - uint32 m_laddr; - int m_fc; - uint32 m_desc1; // ディスクリプタ - uint32 m_desc2; // ロングディスクリプタの後半部 - int m_descsize; // ディスクリプタサイズ (4 or 8) - int m_lastsize; // 前段のディスクリプタサイズ + int m_lv[MAX_LV] {}; + uint32 m_laddr {}; + int m_fc {}; + uint32 m_desc1 {}; // ディスクリプタ + uint32 m_desc2 {}; // ロングディスクリプタの後半部 + int m_descsize {}; // ディスクリプタサイズ (4 or 8) + int m_lastsize {}; // 前段のディスクリプタサイズ // デバッグ用文字列 static const char * const m_lvname[MAX_LV]; - m68kcpu *cpu; + int loglevel {}; + + MPU680x0Device *cpu; }; // 現在の TC:E、TT:E の状態に応じてアドレス変換の有無を切り替える。 // mmu_enable ならアドレス変換ありのバスアクセスを使う。see m68030bus.cpp void -m68kcpu::mmu_enable_changed() +MPU680x0Device::mmu_enable_changed() { bool enable; @@ -165,11 +159,11 @@ m68kcpu::mmu_enable_changed() if (mmu_enable && !enable) { // 無効にする mmu_enable = false; - cpulog(1, "MMU アドレス変換停止"); + putlog(1, "MMU Translation Disabled"); } else if (!mmu_enable && enable) { // 有効にする mmu_enable = true; - cpulog(1, "MMU アドレス変換開始"); + putlog(1, "MMU Translation Enabled"); } } @@ -178,22 +172,30 @@ m68kcpu::mmu_enable_changed() // レジスタクラス // -void -m68kcpu::SetSRP(uint32 h, uint32 l) +bool +MPU680x0Device::SetSRP(uint32 h, uint32 l) { + if ((h & m68030RP::DT_MASK) == 0) { + return false; + } reg.srp.h = h & m68030RP::H_MASK; reg.srp.l = l & m68030RP::L_MASK; + return true; } -void -m68kcpu::SetCRP(uint32 h, uint32 l) +bool +MPU680x0Device::SetCRP(uint32 h, uint32 l) { + if ((h & m68030RP::DT_MASK) == 0) { + return false; + } reg.crp.h = h & m68030RP::H_MASK; reg.crp.l = l & m68030RP::L_MASK; + return true; } void -m68kcpu::SetTT(int n, uint32 data) +MPU680x0Device::SetTT(int n, uint32 data) { uint32 lbase; uint32 lmask; @@ -233,27 +235,28 @@ m68kcpu::SetTT(int n, uint32 data) // ため base 側にビットを立てておくと必ず不一致になることを利用して、 // E ビットの評価も一度に行う。 // ・RW ビットは SSW の RW と比較するので、SSW_RW のビット位置を使う - // (0x00000040 の位置)。 - // また TT::RW は Write が %1 だが SSW_RW は Read が %1 なので反転。 + // (0x00000040 の位置)。TT:RW も SSW_RW も Read が %1。 // ・RWM が %1 なら R/W は無視、RWM が %0 なら R/W の一致も検査するので // RWM が %0 の時は RW のマスクビットを立てておく。 + // ・FC は busaddr の配置に合わせて1ビット左シフトしたところを使う。 // ・FCMask は %1 が無視を表しているので NOT しておく。 // - // target 00000000 00000000 00000000 0R000FFF : R はアクセス R/~W + // target 00000000 00000000 00000000 0R00FFFS : R はアクセス R/~W // ^ : F はアクセス FC - // base 00000000 00000000 E0000000 0R000FFF : R は期待する R/~W + // : S は FC2 のコピー + // base 00000000 00000000 E0000000 0R00FFF0 : R は期待する R/~W // & : F は期待する FC // : E は ~TT:E - // mask 00000000 00000000 10000000 0M000CCC : M は R/~W マスク + // mask 00000000 00000000 10000000 0M00CCC0 : M は R/~W マスク // : C は FC マスク - sbase = ~data & m68030TT::E; - sbase |= (~data & m68030TT::RW) >> 3; - sbase |= ( data & m68030TT::FCBASE_MASK) >> 4; + sbase = ~data & m68030TT::E; + sbase |= (data & m68030TT::RW) >> 3; + sbase |= (data & m68030TT::FCBASE_MASK) >> 3; smask = m68030TT::E; if ((data & m68030TT::RWM) == 0) { - smask |= SSW_RW; + smask |= M68K::SSW_RW; } - smask |= ~data & m68030TT::FCMASK_MASK; + smask |= (~data & m68030TT::FCMASK_MASK) << 1; mmu_tt[n].base = lbase | (((uint64)sbase) << 32); mmu_tt[n].mask = lmask | (((uint64)smask) << 32); @@ -273,21 +276,22 @@ m68kcpu::SetTT(int n, uint32 data) mmu_enable_changed(); } -// アクセスがこの TT と一致するか調べる。 -// ssw_laddr は上位32ビットが cpu->bus.ssw、下位32ビットが論理アドレス -// (下24ビットは呼び出し側でマスクしなくてよい)。 -// 一致すれば true を返す。 +// アクセスがこの TT とマッチするか調べる。 +// laddr の上位ワードはそのまま使える。 +// 下位32ビットが論理アドレス (下24ビットは呼び出し側でマスクしなくてよい)。 +// この TT が有効でアドレスがマッチすれば true を返す。 +// (TT:E は演算に織り込んである、このすぐ上の SetTT() のコメント参照) bool -m68030TT::Match(uint64 ssw_laddr) +m68030TT::Match(busaddr laddr) const { - bool rv = (((ssw_laddr ^ base) & mask) == 0); + bool rv = (((laddr.Get() ^ base) & mask) == 0); return rv; } // 成功すれば true、MMU 構成例外が起きる場合は false を返す。 // 呼び出し側で例外を起こすこと。 bool -m68kcpu::SetTC(uint32 data) +MPU680x0Device::SetTC(uint32 data) { // マスクしてレジスタイメージにセット reg.tc = data & m68030TC::MASK; @@ -300,7 +304,7 @@ m68kcpu::SetTC(uint32 data) if (mmu_tc.Check() == false) { // エラーなら E をクリアして MMU 構成例外。 // (例外処理自体は呼び出し側で行う) - mmu_tc.e = 0; + mmu_tc.e = false; mmu_enable_changed(); return false; } @@ -391,7 +395,7 @@ m68030TC::MakeMask() } void -m68kcpu::SetMMUSR(uint16 data) +MPU680x0Device::SetMMUSR(uint16 data) { reg.mmusr = data & m68030MMUSR::MASK; } @@ -417,6 +421,11 @@ m68030ATCTable::m68030ATCTable() #endif } +// デストラクタ +m68030ATCTable::~m68030ATCTable() +{ +} + #if !defined(ATC_SINGLE) // a のエントリを先頭にする。 m68030ATCLine * @@ -439,9 +448,29 @@ m68030ATCTable::MoveHead(m68030ATCLine * // // コンストラクタ -m68030ATC::m68030ATC(m68kcpu *parent) +m68030ATC::m68030ATC(MPU680x0Device *cpu_) +{ + cpu = cpu_; + + for (auto& table : tables) { + table.cpu = cpu; + } +} + +// デストラクタ +m68030ATC::~m68030ATC() { - cpu = parent; +} + +// ログレベルの設定。 +// これは Object のオーバーライドではないが、同じ使い方をする。 +void +m68030ATC::SetLogLevel(int loglevel_) +{ + loglevel = loglevel_; + for (auto& table : tables) { + table.loglevel = loglevel_; + } } #if defined(ATC_SINGLE) @@ -540,15 +569,15 @@ m68030ATCTable::move_second(m68030ATCLin #endif // ATC_SINGLE // fill_{fetch,read,write}() が各アクセス用の ATC サーチ。 -// 論理アドレス fc:addr が ATC にあればそのエントリを返す。 -// なければ ATC を更新してそのエントリを返す。 +// あらかじめ論理ページにマスクされたアドレス fc:addr が ATC にあれば +// そのエントリを返す。なければ ATC を更新してそのエントリを返す。 // fill_peek() はデバッグ用なので一切状態を更新せず結果だけ返す。 const m68030ATCLine& -m68030ATC::fill_fetch() +m68030ATC::fill_fetch(busaddr addr_) { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); + uint32 laddr = addr_.Addr(); + uint fc = addr_.GetFC(); m68030ATCTable *table; m68030ATCLine *a; int i; @@ -607,8 +636,8 @@ m68030ATC::fill_fetch() #endif // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); + m68030MMU mmu(cpu, loglevel); + mmu.search(7); mmu.make_atc(a); FILL_PRINT(fc, a); @@ -616,10 +645,10 @@ m68030ATC::fill_fetch() } const m68030ATCLine& -m68030ATC::fill_read() +m68030ATC::fill_read(busaddr addr_) { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); + uint32 laddr = addr_.Addr(); + uint fc = addr_.GetFC(); m68030ATCTable *table; m68030ATCLine *a; int i; @@ -678,8 +707,8 @@ m68030ATC::fill_read() #endif // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); + m68030MMU mmu(cpu, loglevel); + mmu.search(7); mmu.make_atc(a); FILL_PRINT(fc, a); @@ -687,10 +716,10 @@ m68030ATC::fill_read() } const m68030ATCLine& -m68030ATC::fill_write() +m68030ATC::fill_write(busaddr addr_) { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); + uint32 laddr = addr_.Addr(); + uint fc = addr_.GetFC(); m68030ATCTable *table; m68030ATCLine *a; int i; @@ -705,8 +734,8 @@ m68030ATC::fill_write() // テーブルサーチを実行する。 if (!a->IsWProtect() && !a->IsModified()) { FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); + MMULOG(3, 1, "%s: write access but M is not set, " + "so invalidate it", lam_func); a->Invalidate(); goto search; } @@ -722,8 +751,8 @@ m68030ATC::fill_write() // テーブルサーチを実行する。 if (!a->IsWProtect() && !a->IsModified()) { FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); + MMULOG(3, 1, "%s: write access but M is not set, " + "so invalidate it", lam_func); a->Invalidate(); break; } @@ -753,8 +782,8 @@ m68030ATC::fill_write() // テーブルサーチを実行する。 if (!a->IsWProtect() && !a->IsModified()) { FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); + MMULOG(3, 1, "%s: write access but M is not set, " + "so invalidate it", lam_func); a->Invalidate(); break; } @@ -781,8 +810,8 @@ m68030ATC::fill_write() #endif // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); + m68030MMU mmu(cpu, loglevel); + mmu.search(7); mmu.make_atc(a); FILL_PRINT(fc, a); @@ -790,14 +819,13 @@ m68030ATC::fill_write() } // ピークアクセス用に ATC を検索する。 -// ATC になければ (uint64)-1 を返す。ここでは疑似テーブルサーチは行わない。 +// ATC になければ NULL を返す。ここでは疑似テーブルサーチは行わない。 // laddr は論理ページアドレスにマスクしておくこと。 -// 戻り値はいずれの場合も物理ページアドレスか、バスエラーなら (uint64)-1。 -uint64 +const m68030ATCLine * m68030ATC::fill_peek(uint32 laddr, uint fc) { m68030ATCTable *table; - m68030ATCLine *a; + const m68030ATCLine *a; #if !defined(ATC_SINGLE) // キャッシュとマッチすればそれを返す @@ -806,10 +834,9 @@ m68030ATC::fill_peek(uint32 laddr, uint a = &table->line[i]; if (a->GetTag() == laddr) { // ヒットした - uint32 paddr = a->GetPAddr(); - DPRINTF(1, "fill_peek: match %d:$%08x -> $%08x", - fc, laddr, paddr); - return paddr; + MMULOG(3, 1, "%s: match %d:$%08x -> $%08x", lam_func, + fc, laddr, (uint32)a->GetPAddr()); + return a; } } #else @@ -818,37 +845,38 @@ m68030ATC::fill_peek(uint32 laddr, uint for (a = table->head; a; a = a->next) { if ((a->GetTag() == laddr) && ((a->fc & 4) == fc)) { // ヒットした - uint32 paddr = a->GetPAddr(); - DPRINTF(1, "fill_peek: match %d:$%08x -> $%08x", - a->fc, laddr, paddr); - return paddr; + MMULOG(3, 1, "%s: match %d:$%08x -> $%08x", lam_func, + a->fc, laddr, (uint32)a->GetPAddr()); + return a; } } #endif - return (uint64)-1; + return NULL; } // デバッグ表示。 -// ATC_SINGLE なら fc は ATCLine に含まれているため、引数はダミーで -// 変数名が衝突するため仮引数名も変えてある。なんだかなあ。 -#if defined(ATC_SINGLE) -void -m68030ATCLine::fill_print(uint dummyfc) -#else -void -m68030ATCLine::fill_print(uint fc) -#endif +// ATC_SINGLE なら fc は this に含まれている。 +// !ATC_SINGLE なら引数で渡される。 +// XXX print といいつつ string を返す +std::string +m68030ATCLine::fill_print(uint fc_) { +#if !defined(ATC_SINGLE) + uint fc = fc_; +#endif uint32 laddr = GetLAddr(); if (!IsBusError()) { + uint32 ci = IsCInhibit(); uint32 wp = IsWProtect(); uint32 mod = IsModified(); - IPRINTF(1, "ATC_fill: match %d:$%08x -> $%08x WP=%d M=%d", - fc, laddr, GetPAddr(), wp, mod); + return indent_format(1, + "ATC_fill: match %d:$%08x -> $%08x CI=%d WP=%d M=%d", + fc, laddr, GetPAddr(), ci, wp, mod); } else { - IPRINTF(1, "ATC_fill: match %d:$%08x -> BusErr", + return indent_format(1, + "ATC_fill: match %d:$%08x -> BusErr", fc, laddr); } } @@ -868,7 +896,7 @@ m68030ATCTable::Lookup() for (int i = 0; i < countof(line); i++) { m68030ATCLine& a = line[i]; if (a.IsInvalid()) { - DPRINTF(1, "ATC_lookup got free entry [%d]", i); + MMULOG(3, 1, "ATC.Lookup got free entry [%d]", i); return &a; } if (a.age < minage) { @@ -877,7 +905,7 @@ m68030ATCTable::Lookup() } } // 空きがなければ、最古のエントリを差し出す - DPRINTF(1, "ATC_lookup got oldest entry [%d]", minidx); + MMULOG(3, 1, "ATC.Lookup got oldest entry [%d]", minidx); return &line[minidx]; } #else @@ -891,7 +919,7 @@ m68030ATCTable::lookup() for (int i = 0; i < LINES; i++) { a = &line[i]; if (a->IsInvalid()) { - DPRINTF(1, "ATC_lookup got free entry [%d]", i); + MMULOG(3, 1, "ATC.Lookup got free entry [%d]", i); goto found; } } @@ -901,7 +929,7 @@ m68030ATCTable::lookup() while (a->next) { a = a->next; } - DPRINTF(1, "ATC_lookup got oldest entry"); + MMULOG(3, 1, "ATC.Lookup got oldest entry"); found: return a; @@ -1015,40 +1043,32 @@ m68030ATCTable::flush(uint32 fc, uint32 // // コンストラクタ -m68030MMU::m68030MMU(m68kcpu *parent) +m68030MMU::m68030MMU(MPU680x0Device *cpu_, int loglevel_) { - cpu = parent; + cpu = cpu_; + loglevel = loglevel_; m_type = INVALID; - m_s = 0; - m_wp = 0; - m_m = 0; - m_l = 0; - m_berr = false; - m_debugger = false; - m_ptest = false; - x = -1; - memset(m_lv, 0, sizeof(m_lv)); - m_laddr = 0; m_fc = -1; - m_desc1 = 0; - m_desc2 = 0; - m_descsize = 0; - m_lastsize = 0; +} + +// デストラクタ +m68030MMU::~m68030MMU() +{ } // テーブルサーチのメイン部分。 // m_type および ATC 作成に必要な情報を埋めてから帰る。 // ATC は帰った先で作成する。 void -m68030MMU::search() +m68030MMU::search(int level) { const char *rpname; int dt; bool is_super; - m_laddr = cpu->bus.laddr; + m_laddr = cpu->bus.laddr.Addr(); m_fc = cpu->bus.GetFC(); is_super = (m_fc & 0x04); @@ -1073,10 +1093,10 @@ m68030MMU::search() } m_lv[FCL] = m_fc; - IF_DEBUG { + cpu->putlogf(3, [&] { std::string buf; - buf += string_format("search: laddr=$%08x ->", m_laddr); + buf = indent_format(2, "search: laddr=$%08x ->", m_laddr); if (cpu->mmu_tc.is > 0) buf += string_format(" IS=%d", cpu->mmu_tc.is); if (cpu->mmu_tc.fcl) @@ -1088,12 +1108,10 @@ m68030MMU::search() NIBBLE(cpu->mmu_tc.tix[i]), m_lv[i]); } } - IPRINTF(2, "%s", buf.c_str()); - } + return buf; + }); - // サーチ時の初期化処理。図9-26 - m_wp = 0; - m_s = 0; + // この辺に図9-26 の初期化だがコンストラクタで初期化済み。 // ここから図9-25。 @@ -1110,8 +1128,13 @@ m68030MMU::search() // ルートポインタの DT をチェック dt = GET_DT(m_desc1); - DPRINTF(2, "search: %s dt=%s", rpname, dtname(dt)); + MMULOG(3, 2, "%s: %s dt=%s", lam_func, rpname, dtname(dt)); switch (dt) { + case DT_INVALID: + // 無効は設定できないはずなので、ここに来ないはず + PANIC("%s.DT==Invalid", rpname); + break; + case DT_PAGE: // ページディスクリプタ m_type = EARLY; return; @@ -1127,7 +1150,10 @@ m68030MMU::search() // 必要ならファンクションコード・ルックアップ if (cpu->mmu_tc.fcl) { x = FCL; - DPRINTF(2, "search: %s tableaddr=$%08x idx=$%x", + // たぶん検索レベルを1つ消費するはず。未確認。 + level--; + + MMULOG(3, 2, "%s: %s tableaddr=$%08x idx=$%x", lam_func, m_lvname[x], m_desc2, m_lv[x]); // ディスクリプタをフェッチ @@ -1156,12 +1182,11 @@ m68030MMU::search() // TIx のテーブルサーチに入る。図9-25 の下半分。 - x = TIA; - for (;;) { + for (x = TIA; x < level; x++) { // リミットチェック if (m_lastsize == 8 && limitcheck() == false) { m_type = INVALID; - m_l = 1; + m_lim = true; return; } @@ -1169,7 +1194,7 @@ m68030MMU::search() uint32 tableaddr; tableaddr = (m_lastsize == 4) ? m_desc1 : m_desc2; tableaddr &= 0xfffffff0; - DPRINTF(2, "search: %s tableaddr=$%08x idx=$%x", + MMULOG(3, 2, "%s: %s tableaddr=$%08x idx=$%x", lam_func, m_lvname[x], tableaddr, m_lv[x]); // ディスクリプタをフェッチ @@ -1217,11 +1242,12 @@ m68030MMU::search() return; } // 次段のサーチを繰り返す - x++; - continue; + break; } } - __unreachable(); + + // レベル上限にひっかかった場合、x は過ぎているので一つ戻しておく。 + x--; } // リミットチェックを実行。図9-28。 @@ -1229,14 +1255,19 @@ m68030MMU::search() bool m68030MMU::limitcheck() const { - int lu = (m_desc1 & 0x80000000); - uint16 limit = (m_desc1 >> 16) & 0x7fff; + uint16 limit = (m_desc1 >> 16) & 0x7fff; - if (lu != 0 && m_lv[x] < limit) { - return false; - } - if (lu == 0 && m_lv[x] > limit) { - return false; + // 最上位ビットが L/U + if ((int32)m_desc1 < 0) { + if (m_lv[x] < limit) { + MMULOG(3, 2, "%s: $%04x < $%04x", lam_func, m_lv[x], limit); + return false; + } + } else { + if (m_lv[x] > limit) { + MMULOG(3, 2, "%s: $%04x > $%04x", lam_func, m_lv[x], limit); + return false; + } } return true; } @@ -1264,7 +1295,7 @@ m68030MMU::fetch_desc(uint32 tableaddr, // インダイレクトなら idx = 0 でコールしてあるので、 // tableaddr がそのまま PA になるという寸法。 descaddr = tableaddr + idx * m_descsize; - DPRINTF(2, "fetch_desc: descsize=%d descaddr=$%08x", + MMULOG(3, 2, "%s: descsize=%d descaddr=$%08x", lam_func, m_descsize, descaddr); // バスエラー発生フラグをクリアしておく @@ -1274,42 +1305,44 @@ m68030MMU::fetch_desc(uint32 tableaddr, if (m_debugger) { // デバッガアクセス uint64 data; - data = phys_peek_32(descaddr); + data = cpu->mainbus->Peek32(descaddr); if ((int64)data < 0) { goto buserr; } m_desc1 = data; if (m_descsize == 8) { - data = phys_peek_32(descaddr + 4); + data = cpu->mainbus->Peek32(descaddr + 4); if ((int64)data < 0) { goto buserr; } m_desc2 = data; } } else { - uint64 data; + busdata bd; - data = phys_read_32(descaddr); - if ((int64)data < 0) { + bd = phys_read_32(descaddr); + if (bd.IsBusErr()) { goto buserr; } - m_desc1 = data; + m_desc1 = bd.Data(); if (m_descsize == 8) { - data = phys_read_32(descaddr + 4); - if ((int64)data < 0) { + bd = phys_read_32(descaddr + 4); + if (bd.IsBusErr()) { goto buserr; } - m_desc2 = data; + m_desc2 = bd.Data(); } + + m_lastaddr = descaddr; } // ディスクリプタタイプをチェック // DT は必ず最初のロングワードの下位2ビット。 do_write = false; dt = GET_DT(m_desc1); - DPRINTF(2, "fetch_desc: desc=%s dt=%s", + MMULOG(3, 2, "%s: desc=%s dt=%s", lam_func, sprintf_desc().c_str(), dtname(dt)); switch (dt) { case DT_INVALID: // 無効 @@ -1326,7 +1359,7 @@ m68030MMU::fetch_desc(uint32 tableaddr, // 図にはないけど、ATC 更新のためと PTEST のために // ページディスクリプタの M ビットを記録。 if ((m_desc1 & MMU_DESC_M)) - m_m = 1; + m_m = true; break; case DT_VALID4: // 有効4バイト @@ -1343,21 +1376,20 @@ m68030MMU::fetch_desc(uint32 tableaddr, // ライトライクルの実行 if (do_write) { - int64 berr = phys_write_32(descaddr, m_desc1); - if (berr < 0) { + busdata bd = phys_write_32(descaddr, m_desc1); + if (bd.IsBusErr()) { goto buserr; } // バス動作正常終了 - DPRINTF(2, "fetch_desc: desc=%s updated", sprintf_desc().c_str()); + MMULOG(3, 2, "%s: desc=%s updated", lam_func, sprintf_desc().c_str()); } - // ステータス更新 - // XXX Cache Inhibit ビットは未実装なので、ページディスクリプタと - // 有効4/8バイトのケースは同じでよくなる。 + // ステータス更新 (OR していく) // XXX 実際には CI はページディスクリプタにしかないけど - m_wp |= (m_desc1 & MMU_DESC_WP) ? 1 : 0; - if (m_descsize == 8) { - m_s |= (m_desc1 & MMU_DESC_S) ? 1 : 0; + m_wp |= (m_desc1 & MMU_DESC_WP); + m_ci |= (m_desc1 & MMU_DESC_CI); + if (__predict_false(m_descsize == 8)) { + m_s |= (m_desc1 & MMU_DESC_S); } return true; @@ -1381,19 +1413,19 @@ m68030MMU::make_atc(m68030ATCLine *a) switch (m_type) { case INVALID: // 無効 a->data |= m68030ATCLine::BUSERROR; - DPRINTF(2, "make_atc: type=INVALID -> BusErr"); + MMULOG(3, 2, "%s: type=INVALID -> BusErr", lam_func); return; case NORMAL: // ノーマル case INDIRECT: // 間接 paddr = (m_descsize == 4) ? m_desc1 : m_desc2; - DPRINTF(2, "make_atc: type=NORMAL paddr=$%08x", paddr & 0xffffff00); + MMULOG(3, 2, "%s: type=NORMAL paddr=$%08x", lam_func, + paddr & 0xffffff00); break; case EARLY: // アーリーターミネーション paddr = (m_descsize == 4) ? m_desc1 : m_desc2; - buf = string_format("make_atc: type=EARLY paddr=$%08x", - paddr & 0xffffff00); + buf = string_format("type=EARLY paddr=$%08x", paddr & 0xffffff00); // 残ってるビットも足して返す for (int i = x + 1; i <= TID; i++) { @@ -1402,16 +1434,17 @@ m68030MMU::make_atc(m68030ATCLine *a) buf += string_format(" +%s=%08x", m_lvname[i], paddr); } } - DPRINTF(2, "%s", buf.c_str()); + MMULOG(3, 2, "%s: %s", lam_func, buf.c_str()); break; default: - __unreachable(); + PANIC("corrupted m_type=%d", (int)m_type); } // PS の部分は反映しない a->paddr = paddr & cpu->mmu_tc.lmask; - //if (m_ci) a->data |= m68030ATCLine::CINHIBIT; + if (m_ci) + a->data |= m68030ATCLine::CINHIBIT; if (m_wp) a->data |= m68030ATCLine::WPROTECT; if (m_m) @@ -1419,16 +1452,16 @@ m68030MMU::make_atc(m68030ATCLine *a) } // make_atc() と同様にテーブルサーチの結果から物理ページアドレスを返す。 -// バスエラーになるケースなら (uint64)-1 を返す。 +// バスエラーになるケースなら BusErr を返す。 // デバッガアクセスなので、VM に一切干渉しない。 -uint64 -m68030MMU::make_atc_debugger() +busaddr +m68030MMU::make_atc_debugger() const { uint64 paddr; switch (m_type) { case INVALID: // 無効 - return (uint64)-1; + paddr = busaddr::BusErr; break; case NORMAL: // ノーマル @@ -1447,78 +1480,63 @@ m68030MMU::make_atc_debugger() } } break; + default: - __unreachable(); + PANIC("corrupted m_type=%d", (int)m_type); } + + paddr |= busaddr::TableSearched; return paddr; } // 物理メモリからの読み込み。 // 正常に読み込めれば 32bit の値。バスエラーなら (uint64)-1 を返す。 -uint64 +busdata m68030MMU::phys_read_32(uint32 addr) { m68kbus backup; - uint64 data; + busdata bd; // バス情報を一旦退避 backup = cpu->bus; // 物理バスアクセス - cpu->bus.ssw = SSW_BUS_R | FC_SD; - data = vm_phys_read_32(addr); - if ((int64)data < 0) - return data; + cpu->bus.laddr.Set(addr, busaddr::Read | busaddr::S); + cpu->bus.paddr = cpu->bus.laddr; + bd = cpu->mainbus->Read32(cpu->bus.paddr); + if (__predict_false(bd.IsBusErr())) { + cpu->bus.size = M68K::SSW_SIZE_LONG; + return bd; + } // バス情報を復元 cpu->bus = backup; - return data; + return bd; } // 物理メモリへの書き込み。 // 正常に書き込めれば 0、バスエラーなら (uint64)-1 を返す。 -uint64 +busdata m68030MMU::phys_write_32(uint32 addr, uint32 data) { m68kbus backup; - uint64 berr; + busdata bd; // バス情報を一旦退避 backup = cpu->bus; // 物理バスアクセス - cpu->bus.ssw = SSW_BUS_W | FC_SD; - berr = vm_phys_write_32(addr, data); - if ((int64)berr < 0) - return berr; + cpu->bus.laddr.Set(addr, busaddr::Write | busaddr::S); + cpu->bus.paddr = cpu->bus.laddr; + bd = cpu->mainbus->Write32(cpu->bus.paddr, data); + if (__predict_false(bd.IsBusErr())) { + cpu->bus.size = M68K::SSW_SIZE_LONG; + return bd; + } // バス情報を復元 cpu->bus = backup; - return 0; -} - -// デバッガによるメモリ読み込み -uint64 -m68030MMU::phys_peek_32(uint32 addr) -{ - uint64 peekdata; - uint32 data; - - // 1バイト目はバスエラーを検査。 - peekdata = vm_phys_peek_8(addr); - if ((int64)peekdata < 0) { - // バスエラー - return -1; - } - data = peekdata << 24; - - // ここはロングワード境界でアクセスしてるはずなので、 - // 先頭バイト以外のバスエラーチェックは不要。 - data |= vm_phys_peek_8(addr + 1) << 16; - data |= vm_phys_peek_8(addr + 2) << 8; - data |= vm_phys_peek_8(addr + 3); - - return data; + return bd; } // ディスクリプタ表示用の文字列を返す。 @@ -1554,7 +1572,7 @@ m68030MMU::dtname(uint32 dt) static char buf[32]; switch (dt) { case DT_INVALID: return "invalid"; - case DT_PAGE: return "page"; + case DT_PAGE: return "page"; case DT_VALID4: return "valid4"; case DT_VALID8: return "valid8"; } @@ -1575,11 +1593,11 @@ const char * const m68030MMU::m_lvname[M // アドレス変換 // -// 論理アドレスが TT0/TT1 どちらかと一致すれば true を返す。 +// 論理アドレスが TT0/TT1 どちらかと一致すれば (bus.ci を更新して) true を返す。 // 一致しなければ false を返す。 // 高速化のため mmu_tt_quick による評価は呼び出し側で行なっている。 -static bool -m68030_check_tt(m68kcpu *cpu) +bool +MPU680x0Device::CheckTT() { bool ci; int match; @@ -1587,23 +1605,21 @@ m68030_check_tt(m68kcpu *cpu) match = 0; ci = false; for (int i = 0; i < 2; i++) { - m68030TT& tt = cpu->mmu_tt[i]; - if (tt.Match(cpu->bus.ssw_laddr)) { + const m68030TT& tt = mmu_tt[i]; + if (tt.Match(bus.laddr)) { // 一致した match += i + 1; ci |= tt.ci != 0; - DPRINTF(1, "check_tt: TT%d match", i); + putlog(3, " %s: TT%d match", __func__, i); } } - // CI は計算はするけど現状未実装なので放置 - (void)ci; - // 一致したら PA <- LA if (match != 0) { - DPRINTF(0, "check_tt: result %08x (match TT%s)", - cpu->bus.laddr, + putlog(3, "%s: result %08x (match TT%s)", __func__, + (uint32)bus.laddr, (match == 3) ? "0 and TT1" : ((match == 1) ? "0" : "1")); + bus.ci = ci; return true; } return false; @@ -1612,364 +1628,383 @@ m68030_check_tt(m68kcpu *cpu) // フェッチ用のアドレス変換を行う。 // 変換できれば true、バスエラーなら false を返す。 bool -m68030_mmu_translate_fetch(m68kcpu *cpu) +MPU680x0Device::TranslateFetch() { + busaddr baddr = bus.laddr; + uint32 laddr = baddr.Addr(); + // 状態表示 - DPRINTF(0, "translate_fetch: enter %c:$%08x pc=$%08x", - (cpu->bus.GetFC() & 0x04) ? 'S' : 'U', - cpu->bus.laddr, - RegPPC); + putlog(3, "%s: enter %c:$%08x pc=$%08x", __func__, + (baddr.IsSuper() ? 'S' : 'U'), + laddr, + ppc); // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { + if (__predict_false(mmu_tt_quick[laddr >> 24] != 0)) { + if (CheckTT()) { return true; } } // ATC からアドレスを取得 - if (cpu->mmu_tc.e) { - const m68030ATCLine& a = cpu->atc.fill_fetch(); - if (a.IsBusError()) { - DPRINTF(0, "translate_fetch: result BusErr (ATC:B)"); + if (__predict_true(mmu_tc.e)) { + baddr &= mmu_tc.lmask; + const m68030ATCLine& a = atc.fill_fetch(baddr); + if (__predict_false(a.IsBusError())) { + putlog(3, "%s: result BusErr (ATC:B)", __func__); return false; } - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; + bus.paddr.SetAddr(a.GetPAddr() | (laddr & mmu_tc.pgmask)); + bus.ci = a.data & m68030ATCLine::CINHIBIT; } - DPRINTF(0, "translate_fetch: result $%08x", cpu->bus.paddr); + putlog(3, "%s: result $%08x", __func__, bus.paddr.Addr()); return true; } // リード用のアドレス変換を行う。 // 変換できれば true、バスエラーなら false を返す。 bool -m68030_mmu_translate_read(m68kcpu *cpu) +MPU680x0Device::TranslateRead() { + busaddr baddr = bus.laddr; + uint32 laddr = baddr.Addr(); + // 状態表示 - DPRINTF(0, "translate_read: enter %c:$%08x pc=%c:$%08x", - (cpu->bus.GetFC() & 0x04) ? 'S' : 'U', - cpu->bus.laddr, - (cpu->reg.s) ? 'S' : 'U', - RegPPC); + putlog(3, "%s: enter %c:$%08x pc=%c:$%08x", __func__, + (baddr.IsSuper() ? 'S' : 'U'), + laddr, + IsSuper() ? 'S' : 'U', + ppc); // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { + if (__predict_false(mmu_tt_quick[laddr >> 24] != 0)) { + if (CheckTT()) { return true; } } // ATC からアドレスを取得 - if (cpu->mmu_tc.e) { - const m68030ATCLine& a = cpu->atc.fill_read(); - if (a.IsBusError()) { - DPRINTF(0, "translate_read: result BusErr (ATC:B)"); + if (__predict_true(mmu_tc.e)) { + baddr &= mmu_tc.lmask; + const m68030ATCLine& a = atc.fill_read(baddr); + if (__predict_false(a.IsBusError())) { + putlog(3, "%s: result BusErr (ATC:B)", __func__); return false; } - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; + bus.paddr.SetAddr(a.GetPAddr() | (laddr & mmu_tc.pgmask)); + bus.ci = a.data & m68030ATCLine::CINHIBIT; } - DPRINTF(0, "translate_read: result $%08x", cpu->bus.paddr); + putlog(3, "%s: result $%08x", __func__, bus.paddr.Addr()); return true; } // ライト用のアドレス変換を行う。 // 変換できれば true、バスエラーなら false を返す。 bool -m68030_mmu_translate_write(m68kcpu *cpu) +MPU680x0Device::TranslateWrite() { + busaddr baddr = bus.laddr; + uint32 laddr = baddr.Addr(); + // 状態表示 - DPRINTF(0, "translate_write: enter %c:$%08x pc=%c:$%08x", - (cpu->bus.GetFC() & 0x04) ? 'S' : 'U', - cpu->bus.laddr, - (cpu->reg.s) ? 'S' : 'U', - RegPPC); + putlog(3, "%s: enter %c:$%08x pc=%c:$%08x", __func__, + (baddr.IsSuper() ? 'S' : 'U'), + laddr, + IsSuper() ? 'S' : 'U', + ppc); // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { + if (__predict_false(mmu_tt_quick[laddr >> 24] != 0)) { + if (CheckTT()) { return true; } } // ATC からアドレスを取得 - if (cpu->mmu_tc.e) { - const m68030ATCLine& a = cpu->atc.fill_write(); - if (a.IsBusError()) { - DPRINTF(0, "translate_write: result BusErr (ATC:B)"); + if (__predict_true(mmu_tc.e)) { + baddr &= mmu_tc.lmask; + const m68030ATCLine& a = atc.fill_write(baddr); + if (__predict_false(a.IsBusError())) { + putlog(3, "%s: result BusErr (ATC:B)", __func__); return false; } - if (a.IsWProtect()) { + if (__predict_false(a.IsWProtect())) { // Write アクセスで WP が立ってたらバスエラー - DPRINTF(0, "translate_write: result Buserr (ATC:WP)"); + putlog(3, "%s: result Buserr (ATC:WP)", __func__); return false; } - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; + bus.paddr.SetAddr(a.GetPAddr() | (laddr & mmu_tc.pgmask)); + bus.ci = a.data & m68030ATCLine::CINHIBIT; } - DPRINTF(0, "translate_write: result $%08x", cpu->bus.paddr); + putlog(3, "%s: result $%08x", __func__, bus.paddr.Addr()); return true; } // ピーク用のアドレス変換を行う。 -// ATC にエントリが見付からない場合、do_search が true ならテーブル -// サーチまで行う。false ならテーブルサーチせずに帰る。 -// 変換できれば対応する物理アドレスを、バスエラーなら (uint64)-1 を返す。 -uint64 -m68030_mmu_translate_peek(m68kcpu *cpu, uint32 laddr, uint fc, bool do_search) +// 変換できれば対応する物理アドレスを、バスエラーなら BusErr を返す。 +// テーブルサーチを行ったら TableSearched ビットを立てる。 +busaddr +MPU680x0Device::TranslatePeek(busaddr baddr) { - uint64 paddr; + busaddr paddr; + uint32 laddr = baddr.Addr(); // 状態表示 - DPRINTF(0, "translate_peek: enter %c:$%08x", - (fc & 0x04) ? 'S' : 'U', + putlog(3, "%s: enter %c:$%08x", __func__, + (baddr.IsSuper() ? 'S' : 'U'), laddr); // まず TT をチェック paddr = laddr; - for (int i = 0; i < 2; i++) { - m68030TT& tt = cpu->mmu_tt[i]; - uint64 ssw = (uint64)(SSW_BUS_R | fc) << 32; - if (tt.Match(ssw | laddr)) { - // 一致した。 - // CI はここでは使わないため、一致したことだけわかればよい - DPRINTF(0, "translate_peek: result $%08x (match tt)", - (uint32)paddr); - return paddr; + if (__predict_false(mmu_tt_quick[laddr >> 24] != 0)) { + for (int i = 0; i < 2; i++) { + m68030TT& tt = mmu_tt[i]; + if (tt.Match(baddr)) { + // 一致した。 + // CI はここでは使わないため、一致したことだけわかればよい + putlog(3, "%s: result $%08x (match tt)", + __func__, (uint32)paddr); + return paddr; + } } } // 次に TC (ATC と MMU テーブルサーチ)。 - if (cpu->mmu_tc.e) { + if (mmu_tc.e) { // ATC からこっそりアドレスを取得 - paddr = cpu->atc.fill_peek(laddr & cpu->mmu_tc.lmask, fc); - if ((int64)paddr >= 0) { - paddr |= laddr & cpu->mmu_tc.pgmask; - DPRINTF(0, "translate_peek: result $%08x (match atc)", - (uint32)paddr); + const m68030ATCLine *a; + a = atc.fill_peek(laddr & mmu_tc.lmask, baddr.GetFC()); + if (a != NULL) { + paddr = a->GetPAddr(); + paddr |= laddr & mmu_tc.pgmask; + putlog(3, "%s: result $%08x (match atc)", + __func__, (uint32)paddr); return paddr; } // 見付からなかったのでこっそりテーブルサーチ。 - if (do_search) { - // mmu.search() はさすがに cpu->bus を使うので事前に退避。 - m68kbus backup = cpu->bus; - - cpu->bus.laddr = laddr; - cpu->bus.ssw = fc; - m68030MMU mmu(cpu); - mmu.m_debugger = true; - mmu.search(); - paddr = mmu.make_atc_debugger(); - // リストア - cpu->bus = backup; - - if ((int64)paddr >= 0) { - paddr |= laddr & cpu->mmu_tc.pgmask; - DPRINTF(0, "translate_peek: result $%08x (table search)", - (uint32)paddr); - return paddr; - } - } + + // mmu.search() はさすがに bus を使うので事前に退避。 + m68kbus backup = bus; + + bus.laddr = baddr; + m68030MMU mmu(this, loglevel); + mmu.m_debugger = true; + mmu.search(7); + paddr = mmu.make_atc_debugger(); + // リストア + bus = backup; + + if (paddr.IsBusErr()) { + // TC が有効だがヒットしなかったらバスエラー + putlog(3, "%s: result BusErr", __func__); + } else { + paddr |= laddr & mmu_tc.pgmask; + putlog(3, "%s: result $%08x (table search)", + __func__, paddr.Addr()); + } + return paddr; } - DPRINTF(0, "translate_peek: result BusErr"); - return (uint64)-1; + // TT が無効かまたは有効でもヒットせず、 + // かつ TC も無効だったら (こっちは「有効でヒットせず」は含まない)、 + // 論理アドレスをそのまま返す + putlog(3, "%s: result $%08x (no translation)", __func__, laddr); + return laddr; } // // 命令 // -// MMU 命令からの呼び出し用。 -// XXX 命令によってこの中のいずれかの EA に対応していないものがあれば調査。 -// XXX クロック未調査。 -static inline uint32 -cea_mmu(m68kcpu *cpu) -{ - uint mm = eamode(RegIR); - uint rr = eanum(RegIR); - - switch (mm) { - case 0: // Dn - case 1: // An - break; - - case 2: // (An) - return cea_anin(cpu, rr); - - case 3: // (An)+ - case 4: // -(An) - break; - - case 5: // d16(An) - return cea_andi(cpu, rr); - case 6: // (An,IX) - return cea_anix(cpu, rr); - case 7: - switch (rr) { - case 0: // Abs.W - return cea_absw(cpu); - case 1: // Abs.L - return cea_absl(cpu); - default: - break; - } - break; - } - // 不当命令 - throw M68K_EXCEP_ILLEGAL; -} - -// RegIR2 の下位5ビットから FC を返す。 +// ir2 の下位5ビットから FC を返す。 // 不当パターンなら C++ の例外をスローする。 -static uint32 -get_fc(m68kcpu *cpu) +uint32 +MPU680x0Device::get_fc() { - uint fcfield = RegIR2 & 0x1f; + uint fcfield = ir2 & 0x1f; if (fcfield == 0) { - return RegSFC; + return reg.GetSFC(); } else if (fcfield == 1) { - return RegDFC; + return reg.GetDFC(); } else if (fcfield < 0x08) { // break; } else if (fcfield < 0x10) { - return RegD(fcfield & 7) & 7; + return reg.D[fcfield & 7] & 7; } else if (fcfield < 0x18) { return fcfield & 7; } // 不当命令 - throw M68K_EXCEP_ILLEGAL; + throw M68K::EXCEP_ILLEGAL; } // MMU 不当命令 (2ワード目で確定) void -mmu_op_illg2(m68kcpu *cpu) +MPU680x0Device::mmu_op_illg2() { - cpulog(1, "MMU 不当命令 %04x %04x", RegIR, RegIR2); - m68030_exception(cpu, M68K_EXCEP_FLINE); + putlog(1, "MMU illegal instruction %04x %04x", ir, ir2); + Exception(M68K::EXCEP_FLINE); } // PFLUSH fc,#mask 命令 (EA を持たない方) void -mmu_op_pflush(m68kcpu *cpu) +MPU680x0Device::mmu_op_pflush() { uint32 mask; uint32 fc; - mask = (RegIR2 >> 5) & 7; - fc = get_fc(cpu); - cpu->atc.flush(fc, mask); + mask = (ir2 >> 5) & 7; + fc = get_fc(); + atc.flush(fc, mask); } // PFLUSH fc,#mask,ea 命令 void -mmu_op_pflush_ea(m68kcpu *cpu) +MPU680x0Device::mmu_op_pflush_ea() { uint32 ea; uint32 mask; uint32 fc; - ea = cea_mmu(cpu); - mask = (RegIR2 >> 5) & 7; - fc = get_fc(cpu); - cpu->atc.flush(fc, mask, ea); + ea = cea_copro(); + mask = (ir2 >> 5) & 7; + fc = get_fc(); + atc.flush(fc, mask, ea); } // PLOADR/PLOADW 命令 void -mmu_op_pload(m68kcpu *cpu) +MPU680x0Device::mmu_op_pload() { uint32 ea; uint32 fc; - if ((RegIR2 & 0x01e0) != 0) { - mmu_op_illg2(cpu); + if ((ir2 & 0x01e0) != 0) { + mmu_op_illg2(); return; } - ea = cea_mmu(cpu); - fc = get_fc(cpu); + ea = cea_copro(); + fc = get_fc(); // 実効アドレスを論理アドレスとする ATC を作成。 // ってこれでいいのか? - cpu->bus.laddr = ea; - if ((RegIR2 & 0x0200)) { - cpu->bus.ssw = SSW_BUS_R | fc; - cpu->atc.fill_read(); + if ((ir2 & 0x0200)) { + busaddr addr(ea, busaddr::FC(fc) | busaddr::R); + atc.fill_read(addr); } else { - cpu->bus.ssw = SSW_BUS_W | fc; - cpu->atc.fill_write(); + busaddr addr(ea, busaddr::FC(fc) | busaddr::W); + atc.fill_write(addr); } } // PTESTR/PTESTW 命令 -// XXX てきとー void -mmu_op_ptest(m68kcpu *cpu) +MPU680x0Device::mmu_op_ptest() { - uint level = (RegIR2 >> 10) & 7; - uint rw = (RegIR2 & 0x0200) ? SSW_BUS_R : SSW_BUS_W; + uint level = (ir2 >> 10) & 7; + busaddr rw = (ir2 & 0x0200) ? busaddr::R : busaddr::W; + uint rn = (ir2 >> 5) & 0xf; uint32 ea; uint32 fc; uint16 mmusr; - ea = cea_mmu(cpu); - fc = get_fc(cpu); + ea = cea_copro(); + fc = get_fc(); + mmusr = 0; if (level == 0) { - cpulog(0, "未実装 PTEST %04x %04x", RegIR, RegIR2); - return; - } - if ((RegIR2 & 0x0100)) { - cpulog(0, "未実装 PTEST %04x %04x", RegIR, RegIR2); - return; - } + if (rn != 0) { + // レベル0で An 指定なら Fライン例外 + mmu_op_illg2(); + return; + } - // テーブルサーチを実行 - cpu->bus.laddr = ea; - cpu->bus.ssw = rw | fc; - m68030MMU table(cpu); - table.m_ptest = true; - table.search(); - // 結果を MMUSR に格納 - mmusr = 0; - if (level == 0) { - if (table.m_type == INVALID) { - mmusr |= m68030MMUSR::B; + // laddr は IS と PS のところを落としたもの。 + // ssw_laddr のアドレスは IS を落とさないもの (PSは不問)。 + busaddr ssw_laddr(ea, busaddr::FC(fc) | rw); + uint32 laddr = ea & mmu_tc.lmask; + + const m68030ATCLine *a = atc.fill_peek(laddr, fc); + if (a) { + // ATC に見付かった。Modified の状態を返す。 + if (a->IsBusError()) { + mmusr |= m68030MMUSR::B; + } else if (a->IsWProtect()) { + mmusr |= m68030MMUSR::W; + } else if (a->IsModified()) { + mmusr |= m68030MMUSR::M; + } + } else { + // ATC に見付からなかった。Invalid を立てる。 + mmusr = m68030MMUSR::I; + } + + // どちらにしても TT を評価。 + for (int i = 0; i < 2; i++) { + m68030TT& tt = mmu_tt[i]; + if (tt.Match(ssw_laddr)) { + mmusr |= m68030MMUSR::T; + } } } else { + // レベル 1-7 ならテーブルサーチを実行。 + + bus.laddr.Set(ea, busaddr::FC(fc) | rw); + m68030MMU table(this, loglevel); + table.m_ptest = true; + table.search(level); + if (table.m_berr) { mmusr |= m68030MMUSR::B; } + if (table.m_lim) { + // XXX not tested + mmusr |= m68030MMUSR::L; + } + if (table.m_s && (fc & 4)) { + // XXX not tested + mmusr |= m68030MMUSR::S; + } + if (table.m_wp) { + mmusr |= m68030MMUSR::W; + } + if (table.m_type == INVALID) { + mmusr |= m68030MMUSR::I; + } + if (table.m_m) { + mmusr |= m68030MMUSR::M; + } + // N を求める。 + // もともとそういう風には出来ていないので逆算している。 + int n; + if (mmu_tc.fcl) { + // たぶんファンクションテーブルで1段消費する? (未確認) + if (table.x == FCL) { + n = 1; + } else { + n = table.x + 2; + } + } else { + n = table.x + 1; + } + mmusr |= n; + + // アドレスレジスタ指定があれば、最後のディスクリプタアドレスを返す。 + // rn は有効なら 8-15。 + if (rn != 0) { + // XXX 途中でバスエラーが起きるケースは未実装 + if (table.m_berr == false) { + reg.R[rn] = table.m_lastaddr; + } + } } - if (table.m_l) { - mmusr |= m68030MMUSR::L; - } - if (table.m_s && (fc & 4)) { - mmusr |= m68030MMUSR::S; - } - if (table.m_wp) { - mmusr |= m68030MMUSR::W; - } - if (table.m_type == INVALID) { - mmusr |= m68030MMUSR::I; - } - if (table.m_m) { - mmusr |= m68030MMUSR::M; - } - // XXX N 未実装 - mmusr |= 1; - cpu->SetMMUSR(mmusr); + SetMMUSR(mmusr); }