--- nono/m680x0/m68030mmu.cpp 2026/04/29 17:04:54 1.1.1.5 +++ nono/m680x0/m68030mmu.cpp 2026/04/29 17:05:40 1.1.1.13 @@ -4,12 +4,9 @@ // Licensed under nono-license.txt // -#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) @@ -45,63 +42,53 @@ 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, ...) +// インデント付き 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); + + std::string rv(indent, ' '); + rv += buf; + free(buf); + + return rv; } -// MMU デバッグモードならインデント付き fmt... を出力 -#define DPRINTF(indent, ...) do { \ - IF_DEBUG \ - IPRINTF(indent, __VA_ARGS__); \ -} while(0) - -// MMU デバッグモードなら ATC エントリ a を表示 -#define FILL_PRINT(fc, a) do { \ - IF_DEBUG \ - (a)->fill_print(fc); \ +#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 final { public: - m68030MMU(m68kcpu *); - ~m68030MMU() { } + m68030MMU(MPU68030Device *, int loglevel_); + ~m68030MMU(); // サーチを実行 - void search(); + void search(int level, busaddr laddr); // ATC にエントリを作成 void make_atc(m68030ATCLine *); // ATC にエントリを作成 (デバッガアクセス用) - uint64 make_atc_debugger(); + busaddr make_atc_debugger() const; private: // リミットチェック @@ -110,15 +97,6 @@ class m68030MMU final // ディスクリプタを取得 bool fetch_desc(uint32, int); - // MMU による物理メモリ読み込み - uint64 phys_read_32(uint32 addr); - - // MMU による物理メモリ書き込み - uint64 phys_write_32(uint32 addr, uint32 data); - - // デバッガ読み込み - uint64 phys_peek_32(uint32 addr); - // ディスクリプタを表示用に (デバッグ用) std::string sprintf_desc(); @@ -126,34 +104,36 @@ class m68030MMU final 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_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 {}; + + MPU68030Device *cpu; }; // 現在の TC:E、TT:E の状態に応じてアドレス変換の有無を切り替える。 // mmu_enable ならアドレス変換ありのバスアクセスを使う。see m68030bus.cpp void -m68kcpu::mmu_enable_changed() +MPU68030Device::mmu_enable_changed() { bool enable; @@ -164,11 +144,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"); } } @@ -177,28 +157,59 @@ m68kcpu::mmu_enable_changed() // レジスタクラス // -void -m68kcpu::SetSRP(uint32 h, uint32 l) +bool +MPU68030Device::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 +MPU68030Device::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) +MPU68030Device::SetTT(int n, uint32 data) { uint32 lbase; uint32 lmask; uint32 sbase; uint32 smask; + // ハッシュから TT#n の成分を一旦クリアする。 + if (mmu_tt[n].e) { + // 変更前の TT#n にマッチして、現在の TT#(1-n) にマッチしない + // アドレスブロックに対応する 4096 エントリをクリア。 + for (int fc = 1; fc <= 6; fc++) { + auto *table = atc.fctables[fc]; + if (table) { + busaddr laddr = busaddr(0) | busaddr::FC(fc); + for (int i = 0; i < 256; i++) { + // XXX R/W は? + if (mmu_tt[n].Match(laddr) && + mmu_tt[1 - n].Match(laddr) == false) + { + uint32 baseidx = i << (24 - 12); + memset(&table->hash[baseidx], + m68030ATCTable::HASH_NONE, 4096); + } + laddr += 0x0100'0000; + } + } + } + } + // マスクしてレジスタイメージにセット reg.tt[n] = data & m68030TT::MASK; @@ -232,40 +243,50 @@ 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 |= M68030::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); - // 論理アドレスとEnableビットだけの早見表を作成。 - // TT0 が一致するなら bit0 をセット、TT1 が一致するなら bit1 をセット - // しておく。 - for (int i = 0; i < countof(mmu_tt_quick); i++) { - uint32 laddr = ((uint32)i) << 24; - if (mmu_tt[n].e && ((laddr ^ lbase) & lmask) == 0) { - mmu_tt_quick[i] |= (1 << n); - } else { - mmu_tt_quick[i] &= ~(1 << n); + // 更新後にこの TT が有効ならハッシュも更新。 + if (mmu_tt[n].e) { + // この TT#n にマッチするアドレスブロックに対応する 4096 エントリを + // TT 印にする。 + for (uint fc = 1; fc <= 6; fc++) { + auto *table = atc.fctables[fc]; + if (table) { + busaddr laddr = busaddr(0) | busaddr::FC(fc); + for (uint i = 0; i < 256; i++) { + // XXX R/W は? + if (mmu_tt[n].Match(laddr)) { + uint32 baseidx = i << (24 - 12); + memset(&table->hash[baseidx], + m68030ATCTable::HASH_TT, 4096); + } + laddr += 0x0100'0000; + } + } } } @@ -273,21 +294,21 @@ m68kcpu::SetTT(int n, uint32 data) } // アクセスがこの TT とマッチするか調べる。 -// ssw_laddr は上位32ビットが cpu->bus.ssw、下位32ビットが論理アドレス -// (下24ビットは呼び出し側でマスクしなくてよい)。 +// 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) +MPU68030Device::SetTC(uint32 data) { // マスクしてレジスタイメージにセット reg.tc = data & m68030TC::MASK; @@ -300,7 +321,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; } @@ -309,10 +330,15 @@ m68kcpu::SetTC(uint32 data) // マスクをあらかじめ用意しておく mmu_tc.MakeMask(); + // ATC でも PS を覚えておく必要がある。 + for (int i = 0; i < countof(atc.tables); i++) { + auto *table = atc.tables[i].get(); + table->ps4k = mmu_tc.ps - 12; + } + // TC:E が変更されていればスイッチ更新 mmu_enable_changed(); return true; - } // TC レジスタへの書き込み値を m68030TC クラスにセットする。 @@ -380,7 +406,7 @@ m68030TC::MakeMask() // +----------------+--------------+--------------+ // shift[TIA]=20 shift[TIB]=10 // mask[TIA]=0xfff mask[TIB]=0x3ff - int sh = ps; + uint sh = ps; for (int i = TID; i >= TIA; i--) { if (tix[i] > 0) { shift[i] = sh; @@ -391,623 +417,353 @@ m68030TC::MakeMask() } void -m68kcpu::SetMMUSR(uint16 data) +MPU68030Device::SetMMUSR(uint16 data) { reg.mmusr = data & m68030MMUSR::MASK; } - -// -// ATC テーブル -// - -// コンストラクタ -m68030ATCTable::m68030ATCTable() -{ -#if !defined(ATC_SINGLE) - for (int i = 0; i < countof(line); i++) { - line[i].Invalidate(); - } - head = &line[0]; -#else - for (int i = 0; i < countof(line); i++) { - line[i].Invalidate(); - insert_tail(&line[i]); - } -#endif -} - -#if !defined(ATC_SINGLE) -// a のエントリを先頭にする。 -m68030ATCLine * -m68030ATCTable::MoveHead(m68030ATCLine *a) +// リセット例外の CPU 固有部分。 +void +MPU68030Device::ResetMMU() { - // head を向け直す - head = a; - - // 更新 - head->age = counter++; - - // そのエントリを返す - return head; + SetTC(GetTC() & ~m68030TC::TC_E); + SetTT(0, GetTT(0) & ~m68030TT::E); + SetTT(1, GetTT(1) & ~m68030TT::E); } -#endif // // ATC // +// 今となっては独立したクラスである必要はなくなっているが、これを +// MPU68030Device にマージするとパフォーマンスが下がるので維持してある。 +// // コンストラクタ -m68030ATC::m68030ATC(m68kcpu *parent) +m68030ATC::m68030ATC(MPU68030Device *cpu_) { - cpu = parent; + cpu = cpu_; + + tables[0].reset(new m68030ATCTable(cpu, 1)); + tables[1].reset(new m68030ATCTable(cpu, 2)); + tables[2].reset(new m68030ATCTable(cpu, 5)); + tables[3].reset(new m68030ATCTable(cpu, 6)); + + fctables[1] = tables[0].get(); + fctables[2] = tables[1].get(); + fctables[5] = tables[2].get(); + fctables[6] = tables[3].get(); } -#if defined(ATC_SINGLE) -// item をリストの先頭に挿入。 -void -m68030ATCTable::insert_head(m68030ATCLine *item) +// デストラクタ +m68030ATC::~m68030ATC() { - item->prev = NULL; - item->next = head; - if (head) { - head->prev = item; - } - head = item; } -// item をリストの先頭から2番目に挿入。 +// ログレベルの設定。 +// これは Object のオーバーライドではないが、同じ使い方をする。 void -m68030ATCTable::insert_second(m68030ATCLine *item) +m68030ATC::SetLogLevel(int loglevel_) { - item->prev = head; - if (head) { - item->next = head->next; - if (head->next) { - head->next->prev = item; - } - head->next = item; - } else { - item->next = NULL; + loglevel = loglevel_; + for (int i = 0; i < countof(tables); i++) { + auto *table = tables[i].get(); + table->loglevel = loglevel_; } } -// item をリストの末尾に追加。 +// ATC をすべてフラッシュする。命令からコールされる void -m68030ATCTable::insert_tail(m68030ATCLine *item) +m68030ATC::flush() { - item->next = NULL; - if (head == NULL) { - item->prev = NULL; - head = item; - } else { - m68030ATCLine *a = head; - while (a->next) - a = a->next; - a->next = item; - item->prev = a; + for (int i = 0; i < countof(tables); i++) { + auto *table = tables[i].get(); + table->Flush(); } } -// item をリストから外す +// ATC の fc, mask が一致するエントリをフラッシュする。 +// 命令からコールされる。 void -m68030ATCTable::remove(m68030ATCLine *item) +m68030ATC::flush(uint32 fc, uint32 mask) { - if (item->prev) - item->prev->next = item->next; - if (item->next) - item->next->prev = item->prev; - if (item == head) - head = item->next; + for (uint i = 1; i <= 6; i++) { + if (((i ^ fc) & mask) == 0) { + // FC が一致したテーブルを全フラッシュ + auto *table = fctables[i]; + if (table) { + table->Flush(); + } + } + } } -// item をリストの先頭に移す。 -// fill_fetch() から呼ばれるもので、リストは空でなく、item は先頭から -// 3番目以内ではない、という前提で高速化。 +// ATC の fc, mask, addr が一致するエントリをフラッシュする。 +// 命令からコールされる。 void -m68030ATCTable::move_head(m68030ATCLine *item) +m68030ATC::flush(uint32 fc, uint32 mask, uint32 addr) { - // 先頭でない前提の remove - item->prev->next = item->next; - if (item->next) - item->next->prev = item->prev; - - // 先頭がある前提の insert_head - item->prev = NULL; - item->next = head; - head->prev = item; - head = item; + for (uint i = 1; i <= 6; i++) { + if (((i ^ fc) & mask) == 0) { + // FC が一致したテーブルから addr が一致したエントリをフラッシュ + auto *table = fctables[i]; + if (table) { + table->Flush(addr); + } + } + } } -// item を先頭から2番目に移す。 -// fill_{read,write} から呼ばれるもので、リストは空でなく、item は先頭から -// 3番目以内ではない、という前提で高速化。 -void -m68030ATCTable::move_second(m68030ATCLine *item) -{ - // 先頭でない前提の remove - item->prev->next = item->next; - if (item->next) - item->next->prev = item->prev; - // 先頭2つがある前提の insert_second - item->prev = head; - item->next = head->next; - head->next->prev = item; - head->next = item; -} -#endif // ATC_SINGLE +// +// ATC テーブル +// -// fill_{fetch,read,write}() が各アクセス用の ATC サーチ。 -// 論理アドレス fc:addr が ATC にあればそのエントリを返す。 -// なければ ATC を更新してそのエントリを返す。 -// fill_peek() はデバッグ用なので一切状態を更新せず結果だけ返す。 +// アドレス変換の本来のフローはこんな感じだが、 +// +// if (TT*.E || TC.E) { +// if (TT*.E が有効) { +// laddr,RW が TT[01] とマッチしたら、paddr = laddr,CI +// } +// if (TC.E が有効) { +// ATC とマッチしたら、paddr = atc[n].paddr,CI +// そうでなければ、 paddr = テーブルサーチ +// } +// } else { +// paddr = laddr (アドレス変換オフ) +// } +// +// ハッシュで一度に引く。 +// switch (hash[laddr>>12]) { +// case HASH_TT: +// laddr,RW が TT[01] とマッチしたら、paddr = laddr,CI +// case HASH_ATC: +// paddr = atc[n].paddr,CI +// case HASH_NONE: +// TC.E なら paddr = テーブルサーチ +// そうでなければ、paddr = laddr (アドレス変換オフ) +// case HASH_SUB: +// PS が 4KB 未満だとハッシュの1エントリに2つ以上が混在するので、 +// この場合は諦めて ATC を線形探索。通常起きないので性能無視。 +// } + +// ATC テーブルは本来は 1つ(FC混合) x 22 ラインだが、 +// 高速化のため FC別(4テーブル) x 22 ラインのモードを用意する。 +// FC 混合版ではテーブルは tables[0] のみで、FC(1,0,2) は busaddr の +// 34-32 ビット部分として laddr と一緒に検索キーにする。 +// FC 分離版ではテーブルは fctables[fc] で検索キーは laddr(下位32ビット部分)。 +// +// ハッシュなのでライン間の順序関係はないが、LRU で古いのを捨てる時や +// PS<4K の時に前から探索する時のために、age を使った順序管理をする。 +// ラインを使用するたびに age に ++latest_age を代入しておくことで、 +// 順序が必要な時には age の大きい順にすれば最近使った順になる。 +// 32 ビット符号なし整数が折り返すと事故るがたぶんそれより高い頻度で +// フラッシュが起きるんじゃないかな… -const m68030ATCLine& -m68030ATC::fill_fetch() +// コンストラクタ +m68030ATCTable::m68030ATCTable(MPU68030Device *cpu_, uint fc_) { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); - m68030ATCTable *table; - m68030ATCLine *a; - int i; - -#if !defined(ATC_SINGLE) - table = &tables[fc]; - // キャッシュとマッチすればそれを返す - a = table->head; - if (a->GetTag() == laddr) { - FILL_PRINT(fc, a); - table->hit_head++; - return *a; - } - for (i = 0; i < countof(table->line); i++) { - a = &table->line[i]; - if (a->GetTag() == laddr) { - // ヒットしたので先頭に移動。fetch は常に入れ替え - a = table->MoveHead(a); - - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; - } - } + cpu = cpu_; + fc = fc_; - // 見付からなかったので、古いエントリを1つ更新。 - a = table->MoveHead(table->Lookup()); - table->hit[m68030ATCTable::LINES]++; - // タグはこっちで初期化 - a->tag = laddr; -#else - table = &tables[0]; - // キャッシュとマッチすればそれを返す - a = table->head; - for (i = 0; a; a = a->next, i++) { - if (a->GetTag() == laddr && a->fc == fc) { - // ヒットしたので先頭に移動。 - if (i > 2) { - table->move_head(a); - } - - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; - } - } - - // 見付からなかったので、古いエントリを1つ更新。 - a = table->lookup(); - table->remove(a); - table->insert_head(a); - table->hit[m68030ATCTable::LINES]++; - // タグはこっちで初期化 - a->tag = laddr; - a->fc = fc; -#endif - - // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); - mmu.make_atc(a); + std::fill(hash.begin(), hash.end(), HASH_NONE); - FILL_PRINT(fc, a); - return *a; + latest_age = 0; + Flush(); } -const m68030ATCLine& -m68030ATC::fill_read() +// デストラクタ +m68030ATCTable::~m68030ATCTable() { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); - m68030ATCTable *table; - m68030ATCLine *a; - int i; - -#if !defined(ATC_SINGLE) - table = &tables[fc]; - // キャッシュとマッチすればそれを返す - a = table->head; - if (a->GetTag() == laddr) { - FILL_PRINT(fc, a); - table->hit_head++; - return *a; - } - for (i = 0; i < countof(table->line); i++) { - a = &table->line[i]; - if (a->GetTag() == laddr) { - // ヒットしたので先頭に移動。 - a = table->MoveHead(a); - - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; - } - } - - // 見付からなかったので、古いエントリを1つ更新 - a = table->MoveHead(table->Lookup()); - table->hit[m68030ATCTable::LINES]++; - // タグはこっちで初期化 - a->tag = laddr; -#else - table = &tables[0]; - // キャッシュとマッチすればそれを返す - a = table->head; - for (i = 0; a; a = a->next, i++) { - if ((a->GetTag() == laddr) && (a->fc == fc)) { - // ヒットしたので先頭から2番目に移動 - if (i > 2) { - table->move_second(a); - } +} - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; - } +// この ATC テーブルのエントリをすべてフラッシュする。 +void +m68030ATCTable::Flush() +{ + for (int i = 0; i < LINES; i++) { + Invalidate(&line[i]); + line[i].age = 0; } - - // 見付からなかったので、古いエントリを1つ更新 - a = table->lookup(); - table->remove(a); - table->insert_head(a); - table->hit[m68030ATCTable::LINES]++; - // タグはこっちで初期化 - a->tag = laddr; - a->fc = fc; -#endif - - // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); - mmu.make_atc(a); - - FILL_PRINT(fc, a); - return *a; + latest_age = 0; } -const m68030ATCLine& -m68030ATC::fill_write() +// この ATC テーブルから addr が一致するエントリをすべてフラッシュする。 +void +m68030ATCTable::Flush(uint32 addr) { - uint32 laddr = cpu->bus.laddr & cpu->mmu_tc.lmask; - uint fc = cpu->bus.GetFC(); - m68030ATCTable *table; - m68030ATCLine *a; - int i; - -#if !defined(ATC_SINGLE) - table = &tables[fc]; - // キャッシュとマッチすればそれを返す - a = table->head; - if (a->GetTag() == laddr) { - // ライトアクセスなので、WP でなく Modified == 0 なら - // Modified を立てるため、このエントリを破棄して改めて - // テーブルサーチを実行する。 - if (!a->IsWProtect() && !a->IsModified()) { - FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); - a->Invalidate(); - goto search; - } - FILL_PRINT(fc, a); - table->hit_head++; - return *a; - } - for (i = 0; i < countof(table->line); i++) { - a = &table->line[i]; - if (a->GetTag() == laddr) { - // ライトアクセスなので、WP でなく Modified == 0 なら - // Modified を立てるため、このエントリを破棄して改めて - // テーブルサーチを実行する。 - if (!a->IsWProtect() && !a->IsModified()) { - FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); - a->Invalidate(); - break; - } - // ヒットしたので先頭に移動。 - a = table->MoveHead(a); - - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; - } - } - search:; - - // 見付からなかったので、古いエントリを1つ更新 - a = table->MoveHead(table->Lookup()); - table->hit[m68030ATCTable::LINES]++; - // タグはこっちで初期化 - a->tag = laddr; -#else - table = &tables[0]; - // キャッシュとマッチすればそれを返す - a = table->head; - for (i = 0; a; a = a->next, i++) { - if ((a->GetTag() == laddr) && (a->fc == fc)) { - // ライトアクセスなので、WP でなく Modified == 0 なら - // Modified を立てるため、このエントリを破棄して改めて - // テーブルサーチを実行する。 - if (!a->IsWProtect() && !a->IsModified()) { - FILL_PRINT(fc, a); - DPRINTF(1, "ATC_fill: write access but M is not set, " - "so invalidate it"); - a->Invalidate(); - break; - } - - // ヒットしたので、先頭から2番目に移動 - if (i > 2) { - table->move_second(a); - } - - FILL_PRINT(fc, a); - table->hit[i]++; - return *a; + for (int i = 0; i < LINES; i++) { + m68030ATCLine *a = &line[i]; + if (a->GetTag() == addr) { + Invalidate(a); } } - - // 見付からなかったので、古いエントリを1つ更新 - a = table->lookup(); - table->remove(a); - table->insert_head(a); - // 論理部分(タグ部分)を初期化 - a->tag = laddr; - a->fc = fc; - table->hit[m68030ATCTable::LINES]++; -#endif - - // テーブルサーチして結果を ATC に入れる - m68030MMU mmu(cpu); - mmu.search(); - mmu.make_atc(a); - - FILL_PRINT(fc, a); - return *a; } -// ピークアクセス用に ATC を検索する。 -// ATC になければ (uint64)-1 を返す。ここでは疑似テーブルサーチは行わない。 -// laddr は論理ページアドレスにマスクしておくこと。 -// 戻り値はいずれの場合も物理ページアドレスか、バスエラーなら (uint64)-1。 -uint64 -m68030ATC::fill_peek(uint32 laddr, uint fc) +// この要素を先頭に移動。 +inline void +m68030ATCTable::MoveHead(m68030ATCLine *a) { - m68030ATCTable *table; - m68030ATCLine *a; + a->age = ++latest_age; -#if !defined(ATC_SINGLE) - // キャッシュとマッチすればそれを返す - table = &tables[fc]; - for (int i = 0; i < countof(table->line); i++) { - 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; - } - } -#else - // キャッシュとマッチすればそれを返す - table = &tables[0]; - 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; + if (__predict_false(a->age == 0)) { + // latest_age が1周したので、一旦全部リセット。 + for (int i = 0; i < LINES; i++) { + line[i].age = 0; } + a->age = ++latest_age; } -#endif - - return (uint64)-1; } -// デバッグ表示。 -// ATC_SINGLE なら fc は ATCLine に含まれているため、引数はダミーで -// 変数名が衝突するため仮引数名も変えてある。なんだかなあ。 -#if defined(ATC_SINGLE) -void -m68030ATCLine::fill_print(uint dummyfc) -#else -void -m68030ATCLine::fill_print(uint fc) -#endif -{ - uint32 laddr = GetLAddr(); - - if (!IsBusError()) { - uint32 wp = IsWProtect(); - uint32 mod = IsModified(); - IPRINTF(1, "ATC_fill: match %d:$%08x -> $%08x WP=%d M=%d", - fc, laddr, GetPAddr(), wp, mod); - } else { - IPRINTF(1, "ATC_fill: match %d:$%08x -> BusErr", - fc, laddr); - } -} - -#if !defined(ATC_SINGLE) -// 更新用にエントリを1つ差し出す。 +// このテーブルのうち最も古い要素を差し出す。 m68030ATCLine * -m68030ATCTable::Lookup() +m68030ATCTable::RemoveTail() { - uint32 minage; - int minidx; + m68030ATCLine *a = NULL; + uint32 min = (uint32)~0; + uint32 minidx = 0; - // 空きエントリを探しながら、最古エントリを探しておく。 - // 最古エントリは age が最も小さいもの。 - minage = (uint32)-1; - minidx = 0; - for (int i = 0; i < countof(line); i++) { - m68030ATCLine& a = line[i]; - if (a.IsInvalid()) { - DPRINTF(1, "ATC_lookup got free entry [%d]", i); - return &a; + // 最後に使われたの (あるいは空き) を探す。 + for (int i = 0; i < LINES; i++) { + if (line[i].IsInvalid()) { + return &line[i]; } - if (a.age < minage) { - minage = a.age; + if (line[i].age < min) { + min = line[i].age; minidx = i; } } - // 空きがなければ、最古のエントリを差し出す - DPRINTF(1, "ATC_lookup got oldest entry [%d]", minidx); - return &line[minidx]; -} -#else -// 更新用にエントリを1つ差し出す。 -m68030ATCLine * -m68030ATCTable::lookup() -{ - m68030ATCLine *a; - - // 空きエントリを探す - for (int i = 0; i < LINES; i++) { - a = &line[i]; - if (a->IsInvalid()) { - DPRINTF(1, "ATC_lookup got free entry [%d]", i); - goto found; - } + if (a == NULL) { + a = &line[minidx]; } - // 空きがなければ、リストの末尾が最古なのでこれを差し出す - a = head; - while (a->next) { - a = a->next; - } - DPRINTF(1, "ATC_lookup got oldest entry"); + // ハッシュから削除。 + Invalidate(a); - found: return a; } -#endif -// ATC をすべてフラッシュする。命令からコールされる +// a からハッシュを作成する。 void -m68030ATC::flush() +m68030ATCTable::MakeHash(const m68030ATCLine *a) { - for (int i = 0; i < countof(tables); i++) { - tables[i].flush(); + uint32 addr4k = a->GetLAddr() >> 12; + int idx = a - &line[0]; + + if (__predict_true(ps4k == 0)) { + hash[addr4k] = idx; + } else if (ps4k > 0) { + for (int i = 0; i < (1 << ps4k); i++) { + hash[addr4k + i] = idx; + } + } else { + hash[addr4k] = HASH_SUB; } } -// ATC の fc, mask が一致するエントリをフラッシュする。 -// 命令からコールされる。 +// ATC a を無効にする。すでに無効なら何もしない。 void -m68030ATC::flush(uint32 fc, uint32 mask) +m68030ATCTable::Invalidate(m68030ATCLine *a) { -#if !defined(ATC_SINGLE) - for (int i = 0; i < countof(tables); i++) { - if (((i ^ fc) & mask) == 0) { - // FC が一致したテーブルを全フラッシュ - tables[i].flush(); - } - } -#else - for (int i = 0; i < 8; i++) { - if (((i ^ fc) & mask) == 0) { - // この FC をテーブルからフラッシュ - tables[0].flush_fc(i); + if (a->IsValid()) { + // 有効ならハッシュから削除。 + if (__predict_true(ps4k == 0)) { + uint32 addr4k = a->GetLAddr() >> 12; + hash[addr4k] = HASH_NONE; + } else if (ps4k > 0) { + // PS>4KB + uint32 addr4k = a->GetLAddr() >> 12; + for (int i = 0; i < (1U << ps4k); i++) { + hash[addr4k + i] = HASH_NONE; + } + } else { + // PS<4KB、自分が抜けることでこの枠が空くかどうか + InvalidateSub(a); } + + a->Invalidate(); } -#endif } -// ATC の fc, mask, addr が一致するエントリをフラッシュする。 -// 命令からコールされる。 +// PS<4KB の場合に aa が抜けることでこの addr4k の枠が空けばハッシュから削除。 void -m68030ATC::flush(uint32 fc, uint32 mask, uint32 addr) +m68030ATCTable::InvalidateSub(const m68030ATCLine *aa) { -#if !defined(ATC_SINGLE) - for (int i = 0; i < countof(tables); i++) { - if (((i ^ fc) & mask) == 0) { - // FC が一致したテーブルから addr が一致したエントリをフラッシュ - tables[i].flush(addr); + const uint32 MASK_4K = 0xfffff000 | m68030ATCLine::INVALID; + uint32 target_laddr = aa->GetTag() & MASK_4K; + bool in_use = false; + + for (int i = 0; i < LINES; i++) { + auto *a = &line[i]; + // 自分自身は今から削除予定なので除く。 + if (a == aa) { + continue; } - } -#else - for (int i = 0; i < 8; i++) { - if (((i ^ fc) & mask) == 0) { - // FC と addr が一致したエントリをフラッシュ - tables[0].flush(i, addr); + // それ以外で有効なものがあるか。 + if ((a->GetTag() & MASK_4K) == target_laddr) { + in_use = true; + break; } } -#endif -} -// この ATC テーブルのエントリをすべてフラッシュする。 -void -m68030ATCTable::flush() -{ - for (int i = 0; i < countof(line); i++) { - m68030ATCLine& a = line[i]; - a.Invalidate(); + // 誰もいなくなれば削除。 + if (in_use == false) { + hash[target_laddr >> 12] = HASH_NONE; } } -#if !defined(ATC_SINGLE) -// addr が一致するエントリをすべてフラッシュ -void -m68030ATCTable::flush(uint32 addr) +// laddr を ATC から探す。見つからなければ NULL を返す。 +m68030ATCLine * +m68030ATCTable::FindSub(const busaddr laddr) { - for (int i = 0; i < countof(line); i++) { - m68030ATCLine& a = line[i]; - if (a.GetLAddr() == addr) { - a.Invalidate(); + uint32 addr = laddr.Addr() & cpu->mmu_tc.lmask; + + // 同じエントリはないはずなので、順序通りに探さなくてもいいはず。 + for (int i = 0; i < LINES; i++) { + auto *a = &line[i]; + if (a->GetTag() == addr) { + return a; } } + return NULL; } -#else -// FC が一致するエントリをすべてフラッシュする -void -m68030ATCTable::flush_fc(uint32 fc) + +// テーブルサーチを行う。 +// テーブルという用語がぶつかっているので MMU Search にしてある。 +m68030ATCLine * +m68030ATCTable::MMUSearch(busaddr laddr) { - for (int i = 0; i < countof(line); i++) { - m68030ATCLine& a = line[i]; - if (a.fc == fc) { - a.Invalidate(); - } - } + m68030ATCLine *a = RemoveTail(); + a->tag = laddr.Addr() & cpu->mmu_tc.lmask; + + m68030MMU mmu(cpu, loglevel); + mmu.search(7, laddr); + mmu.make_atc(a); + + MakeHash(a); + return a; } -// FC と addr が一致するエントリをすべてフラッシュ -void -m68030ATCTable::flush(uint32 fc, uint32 addr) +// デバッグ表示。 +std::string +m68030ATCTable::LineToStr(const m68030ATCLine *a) const { - for (int i = 0; i < countof(line); i++) { - m68030ATCLine& a = line[i]; - if (a.fc == fc && a.GetLAddr() == addr) { - a.Invalidate(); - } + uint32 laddr = a->GetLAddr(); + + if (!a->IsBusError()) { + uint32 paddr = a->GetPAddr(); + uint32 ci = a->IsCInhibit(); + uint32 wp = a->IsWProtect(); + uint32 mod = a->IsModified(); + return indent_format(1, + "ATC: match %u:$%08x -> $%08x CI=%u WP=%u M=%u", + fc, laddr, paddr, ci, wp, mod); + } else { + return indent_format(1, + "ATC: match %u:$%08x -> BusErr", + fc, laddr); } } -#endif // @@ -1015,43 +771,36 @@ m68030ATCTable::flush(uint32 fc, uint32 // // コンストラクタ -m68030MMU::m68030MMU(m68kcpu *parent) +m68030MMU::m68030MMU(MPU68030Device *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, busaddr laddr) { const char *rpname; + uint32 addr; + uint32 fc; int dt; bool is_super; - m_laddr = cpu->bus.laddr; - m_fc = cpu->bus.GetFC(); + addr = laddr.Addr(); + fc = laddr.GetFC(); - is_super = (m_fc & 0x04); + is_super = (fc & 0x04); // アドレスを (IS,) TIA .. TID に分解。 // @@ -1068,37 +817,35 @@ m68030MMU::search() for (int i = TID; i >= TIA; i--) { if (cpu->mmu_tc.tix[i] > 0) { uint32 mask = cpu->mmu_tc.mask[i]; - m_lv[i] = (m_laddr >> cpu->mmu_tc.shift[i]) & mask; + m_lv[i] = (addr >> cpu->mmu_tc.shift[i]) & mask; } } - m_lv[FCL] = m_fc; + m_lv[FCL] = fc; - IF_DEBUG { + cpu->putlogf(3, [&] { std::string buf; - buf += string_format("search: laddr=$%08x ->", m_laddr); + buf = indent_format(2, "search: laddr=$%08x ->", addr); if (cpu->mmu_tc.is > 0) - buf += string_format(" IS=%d", cpu->mmu_tc.is); + buf += string_format(" IS=%u", cpu->mmu_tc.is); if (cpu->mmu_tc.fcl) - buf += string_format(" FC=%d", m_lv[FCL]); + buf += string_format(" FC=%u", m_lv[FCL]); for (int i = TIA; i <= TID; i++) { if (cpu->mmu_tc.tix[i] > 0) { - buf += string_format(" %s=%d($%0*x)", + buf += string_format(" %s=%u($%0*x)", m_lvname[i], cpu->mmu_tc.tix[i], 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。 // 使用するルートポインタを決定 - if (cpu->mmu_tc.sre && is_super) { + if (__predict_true(cpu->mmu_tc.sre) && is_super) { rpname = "SRP"; m_desc1 = cpu->GetSRPh(); m_desc2 = cpu->GetSRPl(); @@ -1110,24 +857,29 @@ m68030MMU::search() // ルートポインタの DT をチェック dt = GET_DT(m_desc1); - DPRINTF(2, "search: %s dt=%s", rpname, dtname(dt)); - switch (dt) { - case DT_PAGE: // ページディスクリプタ + MMULOG(3, 2, "%s: %s dt=%s", lam_func, rpname, dtname(dt)); + if (__predict_true(dt == DT_VALID4)) { + // 有効 4 バイト。基本的にはこれしか使われないので優先。 + m_descsize = 4; + m_lastsize = 8; + } else if (dt == DT_VALID8) { // 有効 (8バイト) + m_descsize = 8; + m_lastsize = 8; + } else if (dt == DT_PAGE) { // ページディスクリプタ m_type = EARLY; return; - - case DT_VALID4: // 有効(4バイト) - case DT_VALID8: // 有効(8バイト) - // descsize は DT=$2 なら 4、DT=$3 なら 8 になる - m_descsize = 1 << dt; - m_lastsize = 8; - break; + } else { + // 無効は設定できないはずなので、ここには来ないはず。 + PANIC("%s.DT==Invalid", rpname); } // 必要ならファンクションコード・ルックアップ - if (cpu->mmu_tc.fcl) { + if (__predict_false(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 +908,11 @@ m68030MMU::search() // TIx のテーブルサーチに入る。図9-25 の下半分。 - x = TIA; - for (;;) { + for (x = TIA; x < level; x++) { // リミットチェック - if (m_lastsize == 8 && limitcheck() == false) { + if (__predict_false(m_lastsize == 8) && limitcheck() == false) { m_type = INVALID; - m_l = 1; + m_lim = true; return; } @@ -1169,7 +920,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]); // ディスクリプタをフェッチ @@ -1201,7 +952,7 @@ m68030MMU::search() m_lastsize = m_descsize; m_descsize = 1 << dt; - if (x == TID || cpu->mmu_tc.tix[x + 1] == 0) { + if (__predict_false(x == TID || cpu->mmu_tc.tix[x + 1] == 0)) { // 間接 m_type = INDIRECT; @@ -1217,11 +968,12 @@ m68030MMU::search() return; } // 次段のサーチを繰り返す - x++; - continue; + break; } } - __unreachable(); + + // レベル上限にひっかかった場合、x は過ぎているので一つ戻しておく。 + x--; } // リミットチェックを実行。図9-28。 @@ -1229,14 +981,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,52 +1021,54 @@ 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=%u descaddr=$%08x", lam_func, m_descsize, descaddr); // バスエラー発生フラグをクリアしておく m_berr = false; // ディスクリプタを取得 - if (m_debugger) { + if (__predict_false(m_debugger)) { // デバッガアクセス uint64 data; - data = phys_peek_32(descaddr); + data = cpu->mainbus->Peek4(descaddr); if ((int64)data < 0) { goto buserr; } m_desc1 = data; if (m_descsize == 8) { - data = phys_peek_32(descaddr + 4); + data = cpu->mainbus->Peek4(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 = cpu->read_phys_4(descaddr); + if (__predict_false(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) { + if (__predict_false(m_descsize == 8)) { + bd = cpu->read_phys_4(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 +1085,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バイト @@ -1334,30 +1093,32 @@ m68030MMU::fetch_desc(uint32 tableaddr, // U ビットを更新 UPDATE_DESC(MMU_DESC_U); break; + + default: + __unreachable(); } // デバッガ自身がアドレス変換を実施している時は書き込みは行わない - if (m_debugger) { + if (__predict_false(m_debugger)) { do_write = false; } // ライトライクルの実行 if (do_write) { - int64 berr = phys_write_32(descaddr, m_desc1); - if (berr < 0) { + busdata bd = cpu->write_phys_4(descaddr, m_desc1); + if (__predict_false(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 +1142,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 +1163,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=%u", (uint)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,106 +1181,49 @@ m68030MMU::make_atc(m68030ATCLine *a) } // make_atc() と同様にテーブルサーチの結果から物理ページアドレスを返す。 -// バスエラーになるケースなら (uint64)-1 を返す。 +// バスエラーになるケースなら BusErr を返す。 // デバッガアクセスなので、VM に一切干渉しない。 -uint64 -m68030MMU::make_atc_debugger() +busaddr +m68030MMU::make_atc_debugger() const { - uint64 paddr; + busaddr paddr; switch (m_type) { case INVALID: // 無効 - return (uint64)-1; + paddr = BusAddr::BusErr; break; case NORMAL: // ノーマル case INDIRECT: // 間接 - paddr = (m_descsize == 4) ? m_desc1 : m_desc2; - paddr &= 0xffffff00; + { + uint32 descaddr; + descaddr = (m_descsize == 4) ? m_desc1 : m_desc2; + descaddr &= 0xffffff00; + paddr = busaddr(descaddr); break; + } case EARLY: // アーリーターミネーション - paddr = (m_descsize == 4) ? m_desc1 : m_desc2; - paddr &= 0xffffff00; + { + uint32 descaddr; + descaddr = (m_descsize == 4) ? m_desc1 : m_desc2; + descaddr &= 0xffffff00; // 残ってるビットも足して返す for (int i = x + 1; i <= TID; i++) { if (cpu->mmu_tc.tix[i] > 0) { - paddr |= m_lv[i] << cpu->mmu_tc.shift[i]; + descaddr |= m_lv[i] << cpu->mmu_tc.shift[i]; } } + paddr = busaddr(descaddr); break; - default: - __unreachable(); - } - return paddr; -} - -// 物理メモリからの読み込み。 -// 正常に読み込めれば 32bit の値。バスエラーなら (uint64)-1 を返す。 -uint64 -m68030MMU::phys_read_32(uint32 addr) -{ - m68kbus backup; - uint64 data; - - // バス情報を一旦退避 - 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 = backup; - return data; -} - -// 物理メモリへの書き込み。 -// 正常に書き込めれば 0、バスエラーなら (uint64)-1 を返す。 -uint64 -m68030MMU::phys_write_32(uint32 addr, uint32 data) -{ - m68kbus backup; - uint64 berr; - - // バス情報を一旦退避 - 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 = 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; + default: + PANIC("corrupted m_type=%u", (uint)m_type); } - 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; + paddr |= BusAddr::TableSearched; + return paddr; } // ディスクリプタ表示用の文字列を返す。 @@ -1558,7 +1263,7 @@ m68030MMU::dtname(uint32 dt) case DT_VALID4: return "valid4"; case DT_VALID8: return "valid8"; } - sprintf(buf, "(invalid DT %d)", dt); + snprintf(buf, sizeof(buf), "(invalid DT %u)", dt); return buf; } @@ -1575,11 +1280,10 @@ 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 +MPU68030Device::CheckTT() { bool ci; int match; @@ -1587,398 +1291,476 @@ 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%u 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__, + bus.laddr.Addr(), (match == 3) ? "0 and TT1" : ((match == 1) ? "0" : "1")); + bus.ci = ci; return true; } return false; } -// フェッチ用のアドレス変換を行う。 +// リード/フェッチ用のアドレス変換を行う。 // 変換できれば true、バスエラーなら false を返す。 bool -m68030_mmu_translate_fetch(m68kcpu *cpu) +MPU68030Device::TranslateRead() { + m68030ATCLine *a; + busaddr& laddr = bus.laddr; + uint32 addr = laddr.Addr(); + uint32 fc = laddr.GetFC(); + // 状態表示 - DPRINTF(0, "translate_fetch: enter %c:$%08x pc=$%08x", - (cpu->bus.GetFC() & 0x04) ? 'S' : 'U', - cpu->bus.laddr, - RegPPC); - - // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { + putlog(3, "%s: enter %c:$%08x pc=%c:$%08x", __func__, + (laddr.IsSuper() ? 'S' : 'U'), + addr, + IsSuper() ? 'S' : 'U', + ppc); + + m68030ATCTable *table = atc.fctables[fc]; + uint8 n = table->hash[addr >> 12]; + if (__predict_true((int8)n >= 0)) { + // ATC #(n) + table->atcstat.total++; + a = &table->line[n]; + } else if (n == m68030ATCTable::HASH_NONE) { + if (__predict_true(mmu_tc.e)) { + // TC.E 有効で対応なしなら、テーブルサーチ。 + table->atcstat.total++; + table->atcstat.miss++; + a = table->MMUSearch(laddr); + } else { + // TC.E 無効。(total もカウントしない) 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)"); - return false; + } else if (n == m68030ATCTable::HASH_TT) { + // TT。CheckTT で外れたかどうかはカウントしない。ほぼ 0 なので。 + table->atcstat.total++; + table->atcstat.tthit++; + return CheckTT(); + } else { + // PS<4KB でマッチしたので再検査。 + table->atcstat.total++; + a = table->FindSub(laddr); + if (a == NULL) { + table->atcstat.miss++; + a = table->MMUSearch(laddr); } - - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; } - DPRINTF(0, "translate_fetch: result $%08x", cpu->bus.paddr); + // ATC a でマッチした + table->MoveHead(a); + putlog(3, "%s", table->LineToStr(a).c_str()); + if (__predict_false(a->IsBusError())) { + return false; + } + bus.paddr.ChangeAddr(a->GetPAddr() | (addr & mmu_tc.pgmask)); + bus.ci = a->IsCInhibit(); return true; } -// リード用のアドレス変換を行う。 +// ライト用のアドレス変換を行う。 // 変換できれば true、バスエラーなら false を返す。 bool -m68030_mmu_translate_read(m68kcpu *cpu) +MPU68030Device::TranslateWrite() { + m68030ATCLine *a; + busaddr& laddr = bus.laddr; + uint32 addr = laddr.Addr(); + uint32 fc = laddr.GetFC(); + // 状態表示 - 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); - - // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { + putlog(3, "%s: enter %c:$%08x pc=%c:$%08x", __func__, + (laddr.IsSuper() ? 'S' : 'U'), + addr, + IsSuper() ? 'S' : 'U', + ppc); + + m68030ATCTable *table = atc.fctables[fc]; + uint8 n = table->hash[addr >> 12]; + if (__predict_true((int8)n >= 0)) { + // ATC #(n) + table->atcstat.total++; + a = &table->line[n]; + } else if (n == m68030ATCTable::HASH_NONE) { + if (__predict_true(mmu_tc.e)) { + // TC.E 有効で対応なしなら、テーブルサーチ。 + table->atcstat.total++; + goto tablesearch; + } else { + // TC.E 無効。(total もカウントしない) 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)"); - return false; + } else if (n == m68030ATCTable::HASH_TT) { + // TT。CheckTT で外れたかどうかはカウントしない。ほぼ 0 なので。 + table->atcstat.total++; + table->atcstat.tthit++; + return CheckTT(); + } else { + // PS<4KB でマッチしたので再検査。 + table->atcstat.total++; + a = table->FindSub(laddr); + if (a == NULL) { + goto tablesearch; } - - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; } - DPRINTF(0, "translate_read: result $%08x", cpu->bus.paddr); - return true; -} - -// ライト用のアドレス変換を行う。 -// 変換できれば true、バスエラーなら false を返す。 -bool -m68030_mmu_translate_write(m68kcpu *cpu) -{ - // 状態表示 - 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); - - // まず TT をチェック - if (__predict_false(cpu->mmu_tt_quick[cpu->bus.laddr >> 24] != 0)) { - if (m68030_check_tt(cpu)) { - return true; - } + // ライトアクセスなので、WP でなく Modified == 0 なら + // Modified を立てるため、このエントリを破棄して改めて + // テーブルサーチを実行する。 + if (a->IsWProtect() == false && a->IsModified() == false) { + table->Invalidate(a); + tablesearch: + table->atcstat.miss++; + a = table->MMUSearch(laddr); } - // ATC からアドレスを取得 - if (cpu->mmu_tc.e) { - const m68030ATCLine& a = cpu->atc.fill_write(); - if (a.IsBusError()) { - DPRINTF(0, "translate_write: result BusErr (ATC:B)"); - return false; - } - if (a.IsWProtect()) { - // Write アクセスで WP が立ってたらバスエラー - DPRINTF(0, "translate_write: result Buserr (ATC:WP)"); - return false; - } + // ATC a でマッチした + table->MoveHead(a); - cpu->bus.paddr = a.GetPAddr(); - cpu->bus.paddr |= cpu->bus.laddr & cpu->mmu_tc.pgmask; - //cpu->bus.ci = a->ci; + if (__predict_false(a->IsBusError())) { + return false; + } + if (__predict_false(a->IsWProtect())) { + // Write アクセスで WP が立ってたらバスエラー + putlog(3, "%s: result Buserr (ATC:WP)", __func__); + return false; } - DPRINTF(0, "translate_write: result $%08x", cpu->bus.paddr); + putlog(3, "%s", table->LineToStr(a).c_str()); + bus.paddr.ChangeAddr(a->GetPAddr() | (addr & mmu_tc.pgmask)); + bus.ci = a->IsCInhibit(); return true; } // ピーク用のアドレス変換を行う。 -// ATC にエントリが見付からない場合、do_search が true ならテーブル -// サーチまで行う。false ならテーブルサーチせずに帰る。 -// 変換できれば対応する物理アドレスを、バスエラーなら (uint64)-1 を返す。 -uint64 -m68030_mmu_translate_peek(m68kcpu *cpu, uint32 laddr, uint fc, bool do_search) -{ - uint64 paddr; +// 変換できれば対応する物理アドレスを、バスエラーなら BusErr を返す。 +// テーブルサーチを行ったら TableSearched ビットを立てる。 +busaddr +MPU68030Device::TranslatePeek(busaddr laddr) +{ + const m68030ATCLine *a; + busaddr paddr; + uint32 addr = laddr.Addr(); + uint32 fc = laddr.GetFC(); // 状態表示 - DPRINTF(0, "translate_peek: enter %c:$%08x", - (fc & 0x04) ? 'S' : 'U', - laddr); - - // まず TT をチェック - paddr = laddr; - if (__predict_false(cpu->mmu_tt_quick[laddr >> 24] != 0)) { - uint64 ssw_laddr = ((uint64)(SSW_BUS_R | fc) << 32) | laddr; - for (int i = 0; i < 2; i++) { - m68030TT& tt = cpu->mmu_tt[i]; - if (tt.Match(ssw_laddr)) { - // 一致した。 - // CI はここでは使わないため、一致したことだけわかればよい - DPRINTF(0, "translate_peek: result $%08x (match tt)", - (uint32)paddr); - return paddr; - } + putlog(5, "%s: enter %c:$%08x", __func__, + (laddr.IsSuper() ? 'S' : 'U'), + addr); + + m68030ATCTable *table = atc.fctables[fc]; + uint8 n = table->hash[addr >> 12]; + // PS<4KB のケースを先に解決する。 + if (__predict_false(n == m68030ATCTable::HASH_SUB)) { + a = table->FindSub(laddr); + if (a) { + n = a - &table->line[0]; + } else { + n = m68030ATCTable::HASH_NONE; } } + if (__predict_true((int8)n >= 0)) { + // ATC #(n) + a = &table->line[n]; + paddr = busaddr(a->GetPAddr()); + paddr |= addr & mmu_tc.pgmask; + putlog(5, "%s: result $%08x (match atc)", __func__, paddr.Addr()); + return paddr; + } else if (n == m68030ATCTable::HASH_NONE) { + if (__predict_true(mmu_tc.e)) { + // TC.E 有効で対応なしなら、こっそりテーブルサーチ。 - // 次に TC (ATC と MMU テーブルサーチ)。 - if (cpu->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); - return paddr; - } + // mmu.search() はさすがに bus を使うので事前に退避。 + m68kbus backup = bus; - // 見付からなかったのでこっそりテーブルサーチ。 - if (do_search) { - // mmu.search() はさすがに cpu->bus を使うので事前に退避。 - m68kbus backup = cpu->bus; - - cpu->bus.laddr = laddr; - cpu->bus.ssw = fc; - m68030MMU mmu(cpu); + m68030MMU mmu(this, loglevel); mmu.m_debugger = true; - mmu.search(); + mmu.search(7, laddr); paddr = mmu.make_atc_debugger(); // リストア - cpu->bus = backup; + bus = backup; - if ((int64)paddr >= 0) { - paddr |= laddr & cpu->mmu_tc.pgmask; - DPRINTF(0, "translate_peek: result $%08x (table search)", - (uint32)paddr); + if (paddr.IsBusErr()) { + // TC が有効だがヒットしなかったらバスエラー + putlog(5, "%s: result BusErr", __func__); + } else { + paddr |= addr & mmu_tc.pgmask; + putlog(5, "%s: result $%08x (table search)", + __func__, paddr.Addr()); + } + return paddr; + } else { + // TC.E 無効 + paddr = laddr; + putlog(5, "%s: result $%08x (no translation)", + __func__, paddr.Addr()); + return paddr; + } + } else { + // TT をこっそりチェック。 + for (int i = 0; i < 2; i++) { + m68030TT& tt = mmu_tt[i]; + if (tt.Match(laddr)) { + // 一致した。 + // CI はここでは使わないため、一致したことだけわかればよい + putlog(5, "%s: result $%08x (match tt)", + __func__, paddr.Addr()); return paddr; } } - - // TC が有効だがヒットしなかったらバスエラー - DPRINTF(0, "translate_peek: result BusErr"); - return (uint64)-1; + return BusAddr::BusErr; } - - // TT が無効かまたは有効でもヒットせず、 - // かつ TC も無効だったら (こっちは「有効でヒットせず」は含まない)、 - // 論理アドレスをそのまま返す - DPRINTF(0, "translate_peek: result $%08x (no translation)", 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 +MPU68030Device::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ワード目で確定) -static void -mmu_op_illg2(m68kcpu *cpu) +void +MPU68030Device::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) +MPU68030Device::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) +MPU68030Device::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(); + ea &= mmu_tc.lmask; + mask = (ir2 >> 5) & 7; + fc = get_fc(); + atc.flush(fc, mask, ea); } // PLOADR/PLOADW 命令 void -mmu_op_pload(m68kcpu *cpu) +MPU68030Device::mmu_op_pload() { uint32 ea; uint32 fc; + busaddr laddr; - 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)) { + laddr = busaddr(ea) | busaddr::FC(fc) | BusAddr::R; } else { - cpu->bus.ssw = SSW_BUS_W | fc; - cpu->atc.fill_write(); + laddr = busaddr(ea) | busaddr::FC(fc) | BusAddr::W; + } + + m68030ATCTable *table = atc.fctables[fc]; + m68030ATCLine *a; + uint8 n = table->hash[laddr.Addr() >> 12]; + // PS<4KB のケースを先に解決する。 + if (__predict_false(n == m68030ATCTable::HASH_SUB)) { + a = table->FindSub(laddr); + if (a) { + n = a - &table->line[0]; + } else { + n = m68030ATCTable::HASH_NONE; + } + } + if ((int8)n >= 0) { + // すでにエントリがある場合 + if (laddr.IsWrite()) { + // 命令が ploadw でエントリが WProtect==0 && Modified==0 なら、 + // 一回破棄して書き込み用に作り直す。 + a = &table->line[n]; + if (a->IsWProtect() == false && a->IsModified() == false) { + table->Invalidate(a); + table->MMUSearch(laddr); + } + } + } else if (n == m68030ATCTable::HASH_NONE) { + if (__predict_true(mmu_tc.e)) { + // TC.E 有効で対応なしなら、テーブルサーチを行って + // ATC を作成。 + table->MMUSearch(laddr); + } else { + // TC.E 無効なら何もしない? + } + } else if (n == m68030ATCTable::HASH_TT) { + // TT の領域なら何もしてはいけない。 } } // PTESTR/PTESTW 命令 -// XXX てきとー void -mmu_op_ptest(m68kcpu *cpu) +MPU68030Device::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; + } + CYCLE(22); - // テーブルサーチを実行 - 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 laddr = busaddr(ea) | busaddr::FC(fc) | rw; + uint32 addr = ea & mmu_tc.lmask; + + m68030ATCTable *table = atc.fctables[fc]; + m68030ATCLine *a = NULL; + uint8 n = table->hash[addr >> 12]; + // PS<4KB のケースを先に解決する。 + if (__predict_false(n == m68030ATCTable::HASH_SUB)) { + a = table->FindSub(laddr); + if (a) { + n = a - &table->line[0]; + } else { + n = m68030ATCTable::HASH_NONE; + } + } + if (__predict_true((int8)n >= 0)) { + // ATC #(n) で見付かった。 + a = &table->line[n]; + } else if (n == m68030ATCTable::HASH_NONE) { + // ATC に見付からなかった。Invalid を立てる。 + mmusr = m68030MMUSR::I; + } else if (n == m68030ATCTable::HASH_TT) { + for (int i = 0; i < 2; i++) { + m68030TT& tt = mmu_tt[i]; + if (tt.Match(laddr)) { + mmusr |= m68030MMUSR::T; + } + } + } + + if (a) { + // 見付かれば Modified 等の状態を返す。 + if (a->IsBusError()) { + mmusr |= m68030MMUSR::B; + } else if (a->IsWProtect()) { + mmusr |= m68030MMUSR::W; + } else if (a->IsModified()) { + mmusr |= m68030MMUSR::M; + } } } else { - if (table.m_berr) { + // レベル 1-7 ならテーブルサーチを実行。 + CYCLE(88); + + m68030MMU mmu(this, loglevel); + mmu.m_ptest = true; + mmu.search(level, busaddr(ea) | busaddr::FC(fc) | rw); + + if (mmu.m_berr) { mmusr |= m68030MMUSR::B; } + if (mmu.m_lim) { + // XXX not tested + mmusr |= m68030MMUSR::L; + } + if (mmu.m_s && (fc & 4)) { + // XXX not tested + mmusr |= m68030MMUSR::S; + } + if (mmu.m_wp) { + mmusr |= m68030MMUSR::W; + } + if (mmu.m_type == INVALID) { + mmusr |= m68030MMUSR::I; + } + if (mmu.m_m) { + mmusr |= m68030MMUSR::M; + } + // N を求める。 + // もともとそういう風には出来ていないので逆算している。 + int n; + if (mmu_tc.fcl) { + // たぶんファンクションテーブルで1段消費する? (未確認) + if (mmu.x == FCL) { + n = 1; + } else { + n = mmu.x + 2; + } + } else { + n = mmu.x + 1; + } + mmusr |= n; + + // アドレスレジスタ指定があれば、最後のディスクリプタアドレスを返す。 + // rn は有効なら 8-15。 + if (rn != 0) { + // XXX 途中でバスエラーが起きるケースは未実装 + if (mmu.m_berr == false) { + reg.R[rn] = mmu.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); }