|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
5: //
6:
1.1.1.5 root 7: //
1.1 root 8: // ブランチ履歴
1.1.1.5 root 9: //
1.1 root 10:
11: #include "branchhistory.h"
1.1.1.10 root 12: #include "m680x0disasm.h"
1.1 root 13: #include "m88100.h"
1.1.1.10 root 14: #include "monitor.h"
1.1.1.7 root 15: #include "mpu64180.h"
1.1.1.5 root 16: #include "vectortable.h"
1.1 root 17:
18: // コンストラクタ
1.1.1.9 root 19: BranchHistory::BranchHistory(uint objid_)
1.1.1.7 root 20: : inherited(objid_)
1.1 root 21: {
1.1.1.7 root 22: int monitor_id;
23:
24: switch (GetId()) {
25: case OBJ_MPU_BRHIST:
26: is_exhist = false;
27: monitor_id = ID_SUBWIN_BRHIST;
28: break;
29: case OBJ_MPU_EXHIST:
30: is_exhist = true;
31: monitor_id = ID_SUBWIN_EXHIST;
32: break;
33: case OBJ_XP_BRHIST:
34: is_exhist = false;
35: monitor_id = ID_SUBWIN_XPBRHIST;
36: break;
37: case OBJ_XP_EXHIST:
38: is_exhist = true;
39: monitor_id = ID_SUBWIN_XPEXHIST;
40: break;
41: default:
42: PANIC("unknown id %s", GetIdStr());
43: }
1.1.1.4 root 44:
1.1.1.5 root 45: // ログは不要
46: ClearAlias();
47:
1.1.1.7 root 48: vectortable = GetVectorTable();
49:
1.1.1.10 root 50: monitor = gMonitorManager->Regist(monitor_id, this);
51: monitor->func = ToMonitorCallback(&BranchHistory::MonitorUpdate);
52: monitor->SetSize(55, 1 + 32); // ヘッダ1行とエントリ数
53: monitor->SetMaxHeight(1 + 256);
1.1.1.7 root 54:
55: // hd64180 はこれを上書きする
56: x_count = 45;
1.1 root 57: }
58:
59: // デストラクタ
60: BranchHistory::~BranchHistory()
61: {
62: }
63:
64: // 初期化
65: void
66: BranchHistory::Clear()
67: {
68: top = 0;
69: for (int i = 0; i < countof(entry); i++) {
70: BranchEntry& e = entry[i];
71: e.count = 0;
72: e.from = 0xffffffff;
73: e.to = 0xffffffff;
1.1.1.11 root 74: e.info = 0;
1.1 root 75: }
76: }
77:
78: // 使用中のエントリ数を取得する
79: int
80: BranchHistory::GetUsed() const
81: {
82: int i = 0;
83:
84: // top の位置から一巡するまで。有効エントリは count > 0。
85: for (; i < 256; i++) {
86: if (entry[(256 + top - i) % 256].count == 0) {
87: break;
88: }
89: }
90: return i;
91: }
92:
93: // モニタ更新
94: void
1.1.1.4 root 95: BranchHistory::MonitorUpdate(Monitor *, TextScreen& screen)
1.1 root 96: {
97: uint8 t;
98: int y;
99: int ydelta;
100: int row;
101:
102: // userdata は表示開始位置と各種フラグ。
103: // 下位 32bit が表示開始位置で、0 なら先頭のエントリから、1 なら先頭の
1.1.1.4 root 104: // 一つ次のエントリから、…を表す (表示行数は screen の高さ分)。
1.1 root 105: // BottomToTop が %1 なら並び順を下から上に変える (コンソール用)。
1.1.1.4 root 106: bool bottom_to_top = (screen.userdata & BottomToTop);
107: int pos = (int)screen.userdata;
1.1 root 108:
109: // 0 1 2 3 4 5 6
110: // 0123456789012345678901234567890123456789012345678901234567890123456789
111: // No. FromAddr Instruction ToAddr Iteration
112: // 001 U:01234567(01234567:bcnd.n) -> $01234567 x123456789
113: // 001 U:01234567(0123:trap ) -> $01234567 x123456789
114: // 002 S:01234567< 2> Bus Error
115: // 002 S:01234567< 69> MFP Timer-C
116: // 01234567890123456789012
117:
1.1.1.4 root 118: screen.Clear();
119: row = screen.GetRow();
1.1 root 120:
121: // 最初の1行は常にヘッダ
1.1.1.7 root 122: std::string header = FormatHeader();
123: screen.Puts(0, 0, header.c_str());
1.1 root 124:
125: // pos は最新を 0 とした通し番号。(スクロールしてたら開始が 0 とは限らない)
126: // t が entry[] 上の現在位置。
127: // y は表示座標。(上から表示か下から表示かが変わる)
128: t = top - pos;
129: int pos_end = pos + row - 1;
130: if (bottom_to_top == false) {
131: y = 1;
132: ydelta = 1;
133: } else {
134: y = row - 1;
135: ydelta = -1;
136: }
137: for (; pos < pos_end; pos++, y += ydelta) {
138: BranchEntry& e = entry[t--];
139: if (e.count == 0) {
140: continue;
141: }
142: std::string str = FormatEntry(e);
1.1.1.9 root 143: screen.Print(0, y, "%3u %s", pos, str.c_str());
1.1 root 144: if (e.count > 1) {
1.1.1.7 root 145: screen.Print(x_count, y, "x%9u", e.count);
1.1 root 146: }
147: }
148: }
149:
1.1.1.7 root 150: // ヘッダ文字列を返す。(m68k, m88k 共通)
151: std::string
152: BranchHistory::FormatHeader() const
153: {
154: if (is_exhist) {
155: // 012345678901234567890123456789012345678901234567890123456789
156: return "No. FromAddr Vec. Exception Iteration";
157: } else {
158: // 012345678901234567890123456789012345678901234567890123456789
159: return "No. FromAddr Instruction ToAddr Iteration";
160: }
161: }
1.1 root 162:
163:
164: //
1.1.1.11 root 165: // m680x0 ブランチ履歴
1.1 root 166: //
167:
1.1.1.8 root 168: // m680x0 ブランチ履歴では、通常ブランチ、例外発生 (IOCS コールと
169: // それ以外)、例外ベクタによるジャンプの4つを区別する。
1.1 root 170: //
1.1.1.8 root 171: // 1. 通常の分岐:
172: // from は上位31ビットが分岐元 PC、最下位ビットが S/U。
173: // to は32ビット全体が分岐先 PC。
1.1.1.11 root 174: // info は上位2ビットが %00 (Normal)、下位16ビットに分岐元命令の1ワード目。
1.1.1.8 root 175: //
1.1.1.11 root 176: // 3 2 1 0
177: // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
178: // +-+-+-----------+---------------+---------------+---------------+
179: // |0 0| 0 | Instruction Word |
180: // +-+-+-----------+---------------+---------------+---------------+
1.1.1.8 root 181: //
1.1.1.11 root 182: // 2. 例外発生 (IOCS/DOS コール以外の場合):
1.1.1.8 root 183: // from は上位31ビットが例外発生時の PC、最下位ビットが S/U。
184: // to は不問だが 0 にすること。
1.1.1.11 root 185: // info は上位2ビットが %11 (Exception)、最下位バイトがベクタ番号 (0-255)。
186: // 例外が TRAP#15 だが IOCS コールっぽくない場合と、
187: // 例外が F ライン例外だが DOS コールっぽくない場合もこちら。
188: //
189: // +-+-+-----------+---------------+---------------+---------------+
190: // |1 1| 0 | 0 | Vector No. |
191: // +-+-+-----------+---------------+---------------+---------------+
1.1.1.8 root 192: //
1.1.1.11 root 193: // 3. 例外発生 (IOCS/DOS コールの場合):
1.1.1.8 root 194: // from, to は同じ。
1.1.1.11 root 195: // info は上位2ビットが %10 (ServiceCall)。
196: // IOCS コールなら、b8 が %0 で b7-b0 に IOCS コール番号。
197: // DOS コールなら、b8 が %1 で b7-b0 に DOS コール番号。
198: //
199: // +-+-+-----------+---------------+---------------+---------------+
200: // |1 0| 0 | 0 |0| IOCS Call No. |
201: // +-+-+-----------+---------------+---------------+---------------+
202: // +-+-+-----------+---------------+---------------+---------------+
203: // |1 0| 0 | - |1| DOS Call No. |
204: // +-+-+-----------+---------------+---------------+---------------+
1.1 root 205: //
1.1.1.8 root 206: // 4. 例外ベクタによる分岐 (例外処理の最後に起きる):
1.1.1.3 root 207: // from はベクタアドレス、
208: // to は分岐先 (0 も起こりえる)、
1.1.1.11 root 209: // info は上位2ビットが %01 (VectorJump)。
1.1.1.8 root 210: //
1.1.1.11 root 211: // +-+-+-----------+---------------+---------------+---------------+
212: // |0 1| 0 |
213: // +-+-+-----------+---------------+---------------+---------------+
1.1 root 214:
215: // コンストラクタ
1.1.1.9 root 216: BranchHistory_m680x0::BranchHistory_m680x0(uint objid_)
1.1.1.7 root 217: : inherited(objid_)
1.1 root 218: {
219: }
220:
221: // デストラクタ
222: BranchHistory_m680x0::~BranchHistory_m680x0()
223: {
224: }
225:
226: // 1エントリ分の表示内容作成
227: std::string
228: BranchHistory_m680x0::FormatEntry(const BranchEntry& e)
229: {
230: // m68k では from の最下位1ビットを S/U として使う
231: std::string desc = string_format("%c:%08x",
232: (e.from & 1) ? 'S' : 'U', (e.from & ~1));
233:
1.1.1.11 root 234: switch (e.info >> 30) {
1.1.1.8 root 235: case 0: // 通常分岐
236: {
1.1 root 237: const char *mnemonic;
1.1.1.11 root 238: uint32 inst = e.info & 0xffff;
1.1 root 239: if (inst == 0x4e73) {
240: mnemonic = ":rte";
241: } else if (inst == 0x4e75) {
242: mnemonic = ":rts";
243: } else if (inst == 0x4e77) {
244: mnemonic = ":rtr";
245: } else if ((inst & 0xffc0) == 0x4e80) {
246: mnemonic = ":jsr";
247: } else if ((inst & 0xffc0) == 0x4ec0) {
248: mnemonic = ":jmp";
249: } else if ((inst & 0xfff8) == 0x51c8) {
250: mnemonic = ":dbra";
251: } else if ((inst & 0xf0f8) == 0x50c8) {
252: mnemonic = ":dbcc";
253: } else if ((inst & 0xff00) == 0x6000) {
254: mnemonic = ":bra";
255: } else if ((inst & 0xff00) == 0x6100) {
256: mnemonic = ":bsr";
257: } else if ((inst & 0xf000) == 0x6000) {
258: mnemonic = ":bcc";
259: } else if ((inst & 0xfff8) == 0xf248) {
260: mnemonic = ":fdbcc";
261: } else if ((inst & 0xff80) == 0xf280) {
262: mnemonic = ":fbcc";
263: } else {
264: mnemonic = "";
265: }
266: desc += string_format("(%04x%-6s) -> $%08x", inst, mnemonic, e.to);
1.1.1.8 root 267: break;
268: }
269:
270: case 1: // 例外ベクタフェッチによるジャンプ
271: // 例外ベクタフェッチしてジャンプ
272: desc += string_format("(vector fetch) -> $%08x", e.to);
273: break;
274:
1.1.1.11 root 275: case 2: // 例外発生 (IOCS/DOS コール)
1.1.1.8 root 276: {
1.1.1.11 root 277: const char *call;
278: const char *name;
279: uint vector;
280: uint num = e.info & 0xff;
281: if ((e.info & 0x0100) == 0) {
282: call = "IOCS";
283: name = m680x0disasm::GetIOCSName(num);
284: vector = M68K::EXCEP_TRAP15;
285: } else {
286: call = "DOS";
287: name = m680x0disasm::GetDOSName(num);
288: vector = M68K::EXCEP_FLINE;
289: }
1.1.1.8 root 290: if (name != NULL) {
1.1.1.11 root 291: desc += string_format("<%3u> %s %s", vector, call, name);
1.1.1.8 root 292: } else {
1.1.1.11 root 293: desc += string_format("<%3u> %s $%02x", vector, call, num);
1.1.1.8 root 294: }
295: break;
296: }
297:
1.1.1.11 root 298: case 3: // 例外発生 (IOCS/DOS コール以外)
1.1.1.8 root 299: {
1.1.1.11 root 300: uint32 vector = e.info & 0xff;
1.1.1.8 root 301: const char *name = vectortable->GetExceptionName(vector);
1.1.1.9 root 302: desc += string_format("<%3u> %s", vector, name ?: "");
1.1.1.8 root 303: break;
304: }
305:
306: default:
307: __unreachable();
1.1 root 308: }
309: return desc;
310: }
311:
312:
313: //
1.1.1.11 root 314: // m88xx0 ブランチ履歴
1.1 root 315: //
316:
317: // m88xx0 ブランチ履歴では、通常ブランチ、例外発生の2つを区別する。
318: // m68k は例外ベクタに書いてあるアドレスに飛ぶのでもう1種類分けてあったが、
319: // m88k は例外ベクタを直接実行するのでそれは不要。
320: //
321: // 通常の分岐:
1.1.1.3 root 322: // from は上位30ビットが分岐元 XIP、最下位ビットが S/U、
323: // to は32ビット全体が分岐先アドレス、
1.1.1.11 root 324: // info は命令ワード。
1.1 root 325: //
326: // 例外発生:
1.1.1.3 root 327: // from は上位30ビットが例外発生時の XIP、最下位ビットが S/U、
1.1.1.12! root 328: // to は不問。
1.1.1.11 root 329: // info は、9 ビットのベクタ番号と、ベクタが OpenBSD/m88k システムコール
330: // の場合はシステムコール番号を含む。
331: // 3 2 1 0
332: // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
333: // +---------------+---------------+-------+-----+-+---------------+
334: // |1 1 0 0 0 0 0 0| System Call No. |0 0 0| Vector Number |
335: // +---------------+---------------+-------+-----+-+---------------+
1.1.1.3 root 336: // 命令ワードとは衝突しない (instruction.txt 参照)。
1.1 root 337:
338: // コンストラクタ
1.1.1.9 root 339: BranchHistory_m88xx0::BranchHistory_m88xx0(uint objid_)
1.1.1.7 root 340: : inherited(objid_)
1.1 root 341: {
342: }
343:
344: // デストラクタ
345: BranchHistory_m88xx0::~BranchHistory_m88xx0()
346: {
347: }
348:
349: // 1エントリ分の表示内容作成
350: std::string
351: BranchHistory_m88xx0::FormatEntry(const BranchEntry& e)
352: {
353: // m88k では from の最下位1ビットを S/U として使う
354: std::string desc = string_format("%c:%08x",
355: (e.from & 1) ? 'S' : 'U', (e.from & ~1));
356:
1.1.1.12! root 357: if (e.info >= 0xfc000000) {
1.1 root 358: // 例外発生記録
359: // 発生アドレスとベクタを表示
1.1.1.11 root 360: uint32 vector = e.info & 0x1ff;
1.1.1.7 root 361: const char *name = vectortable->GetExceptionName(vector);
1.1.1.9 root 362: desc += string_format("<%3u> %s", vector, name ?: "");
1.1.1.7 root 363: if (vector == 128 || vector == 450) {
364: // 450 なら OpenBSD のシステムコール番号を追加。
365: // 非公式 NetBSD/luna88k は(今の所?) 128 を使ってるようだ。
1.1.1.11 root 366: desc += string_format("(%u)", (e.info >> 12) & 0xfff);
1.1 root 367: }
1.1.1.11 root 368: } else if (e.info >= 0xc0000000) {
1.1 root 369: // ブランチ
370: const char *mnemonic = "";
1.1.1.11 root 371: uint32 up4 = (e.info >> 26) & 0xf;
1.1 root 372: switch (up4) {
373: case 0: mnemonic = ":br"; break;
374: case 1: mnemonic = ":br.n"; break;
375: case 2: mnemonic = ":bsr"; break;
376: case 3: mnemonic = ":bsr.n"; break;
377: case 4: mnemonic = ":bb0"; break;
378: case 5: mnemonic = ":bb0.n"; break;
1.1.1.2 root 379: case 6: mnemonic = ":bb1"; break;
1.1 root 380: case 7: mnemonic = ":bb1.n"; break;
381:
382: case 0xa: mnemonic = ":bcnd"; break;
383: case 0xb: mnemonic = ":bcnd.n"; break;
384: case 0xc: {
1.1.1.11 root 385: uint32 lo6 = (e.info >> 10) & 0x3f;
1.1 root 386: if (lo6 == 0x34) {
387: mnemonic = ":tb0";
388: } else if (lo6 == 0x36) {
389: mnemonic = ":tb1";
390: } else if (lo6 == 0x3a) {
391: mnemonic = ":tcnd";
392: }
393: break;
394: }
395: case 0xd: {
1.1.1.11 root 396: uint32 lo6 = (e.info >> 10) & 0x3f;
1.1 root 397: if (lo6 == 0x30) {
1.1.1.11 root 398: if ((e.info & 0x1f) == 1) {
1.1 root 399: mnemonic = ":jmp r1";
400: } else {
401: mnemonic = ":jmp";
402: }
403: } else if (lo6 == 0x31) {
404: mnemonic = ":jmp.n";
405: } else if (lo6 == 0x32) {
406: mnemonic = ":jsr";
407: } else if (lo6 == 0x33) {
408: mnemonic = ":jsr.n";
409: } else if (lo6 == 0x3f) {
410: mnemonic = ":rte";
411: }
412: break;
413: }
414: case 0xe: mnemonic = ":tbnd"; break;
415: default:
416: break;
417: }
1.1.1.11 root 418: desc += string_format("(%08x%-7s) -> $%08x", e.info, mnemonic, e.to);
1.1 root 419: }
420: return desc;
421: }
1.1.1.7 root 422:
423:
424: //
425: // XP ブランチ履歴
426: //
427:
428: // XP ブランチ履歴では、通常ブランチ、例外発生、例外ベクタによるジャンプの
429: // 3つを区別する。
430: //
431: // 通常の分岐
432: // from は下位 16 ビットが分岐元 PC
433: // to は下位 16 ビットが分岐先 PC
1.1.1.11 root 434: // info の最上位ビットが %0 で区別する。
435: // info の下位 16 ビットに命令を上詰めで。例えば
1.1.1.7 root 436: // CALL (1バイト命令 CD nn nn) なら $0000'cd00、
437: // RETI (2バイト命令 ED 4D) なら $0000'ed4d。
438: //
439: // 例外発生
440: // from は下位 16 ビットが例外発生時の PC
441: // to は $ffffffff ならこのエントリはベクタ方式の例外発生時側を示す、
442: // そうでなければダイレクト方式で下位 16 ビットが分岐先 PC
1.1.1.11 root 443: // info の最上位ビットが %1 で info が $ffffffff でなければ
1.1.1.7 root 444: // 下位に優先度(0-15)。
445: //
446: // 例外ベクタジャンプの場合
447: // from はベクタアドレス
448: // to は分岐先 PC、
1.1.1.11 root 449: // info が $ffffffff で区別する。
1.1.1.7 root 450:
451: // コンストラクタ
1.1.1.9 root 452: BranchHistory_hd64180::BranchHistory_hd64180(uint objid_)
1.1.1.7 root 453: : inherited(objid_)
454: {
1.1.1.10 root 455: monitor->SetSize(48, 1 + 32); // ヘッダ1行とエントリ数
1.1.1.7 root 456: x_count = 38;
457: }
458:
459: // デストラクタ
460: BranchHistory_hd64180::~BranchHistory_hd64180()
461: {
462: }
463:
464: // ヘッダ文字列を返す。
465: std::string
466: BranchHistory_hd64180::FormatHeader() const
467: {
468: if (is_exhist) {
469: // 01234567890123456789012345678901234567890123456789
470: return "No. From Exception Iteration";
471: } else {
472: // 01234567890123456789012345678901234567890123456789
473: return "No. From Instruction ToAddr Iteration";
474: }
475: }
476:
477: // 1エントリ分の表示内容作成
478: std::string
479: BranchHistory_hd64180::FormatEntry(const BranchEntry& e)
480: {
481: // 0 1 2 3 4 5
482: // 012345678901234567890123456789012345678901234567890
483: // No. From Instruction ToAddr Iteration
484: // 001 0123(0000:CALL NZ) -> $0234 x123456899
485: // No. From Exception
486: // 002 0123<00> Timer Overflow
487: // 003 0038(vector fetch) -> $0000
488:
489: std::string desc = strhex(e.from, 4);
490:
1.1.1.11 root 491: if (e.info == 0xffffffff) {
1.1.1.7 root 492: // 例外ベクタフェッチしてジャンプ
493: desc += string_format("(vector fetch) -> $%04x", e.to);
1.1.1.11 root 494: } else if ((int32)e.info < 0) {
1.1.1.7 root 495: // 例外発生
1.1.1.11 root 496: uint32 vector = e.info & 0xff;
1.1.1.9 root 497: desc += string_format("<%2u>%-15s",
1.1.1.7 root 498: vector, MPU64180Device::InterruptName[vector]);
499: // 例外履歴には宛先は出さないほうに統一。
500: // ブランチ履歴でも、ベクタ方式なら宛先不要。
501: if (is_exhist == false && e.to != -1) {
502: desc += string_format(" -> $%04x", e.to);
503: }
504: } else {
505: // ブランチ
506: std::string mnemonic;
1.1.1.11 root 507: uint32 inst = e.info & 0xffff;
1.1.1.7 root 508: if (inst == 0x1000) {
509: mnemonic = ":DJNZ";
510: } else if (inst == 0x1800) {
511: mnemonic = ":JR";
512: } else if (inst == 0xc300) {
513: mnemonic = ":JP";
514: } else if (inst == 0xc900) {
515: mnemonic = ":RET";
516: } else if (inst == 0xcd00) {
517: mnemonic = ":CALL";
518: } else if (inst == 0xe900) {
519: mnemonic = ":JP (HL)";
520: } else if (inst == 0xed45) {
521: mnemonic = ":RETN";
522: } else if (inst == 0xed4d) {
523: mnemonic = ":RETI";
524: } else if (inst == 0xdde9) {
525: mnemonic = ":JP (IX)";
526: } else if (inst == 0xfde9) {
527: mnemonic = ":JP (IY)";
528: } else if ((inst & 0xe700) == 0x2000) {
529: mnemonic = ":JR " + fstr((inst >> 11) & 3);
530: } else if ((inst & 0xc700) == 0xc000) {
531: mnemonic = ":RET " + fstr((inst >> 11) & 7);
532: } else if ((inst & 0xc700) == 0xc200) {
533: mnemonic = ":JP " + fstr((inst >> 11) & 7);
534: } else if ((inst & 0xc700) == 0xc400) {
535: mnemonic = ":CALL " + fstr((inst >> 11) & 7);
536: } else if ((inst & 0xc700) == 0xc700) {
537: mnemonic = string_format(":RST %02xH", (inst >> 8) & 0x38);
538: }
539: desc += string_format("(%04x%-8s) -> $%04x",
540: inst, mnemonic.c_str(), e.to);
541: }
542: return desc;
543: }
544:
545: /*static*/ std::string
546: BranchHistory_hd64180::fstr(int f)
547: {
548: static const char fff[] =
549: "NZ\0\0"
550: "Z\0\0\0"
551: "NC\0\0"
552: "C\0\0\0"
553: "PO\0\0"
554: "PE\0\0"
555: "P\0\0\0"
556: "M";
557:
558: return &fff[f * 4];
559: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.