|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: // ブランチ履歴
8:
9: #include "branchhistory.h"
10: #include "m68030core.h"
11: #include "m88100.h"
12:
13: // コンストラクタ
1.1.1.4 ! root 14: BranchHistory::BranchHistory(bool is_exhist_)
! 15: : inherited(is_exhist_ ? "ExceptionHistory" : "BranchHistory")
1.1 root 16: {
1.1.1.4 ! root 17: is_exhist = is_exhist_;
! 18:
! 19: monitor.func = (MonitorCallback_t)&BranchHistory::MonitorUpdate;
! 20: monitor.SetSize(55, 33);
! 21: if (is_exhist) {
! 22: monitor.Regist(ID_SUBWIN_EXHIST);
! 23: } else {
! 24: monitor.Regist(ID_SUBWIN_BRHIST);
! 25: }
1.1 root 26: }
27:
28: // デストラクタ
29: BranchHistory::~BranchHistory()
30: {
31: }
32:
33: // 初期化
34: void
35: BranchHistory::Clear()
36: {
37: top = 0;
38: for (int i = 0; i < countof(entry); i++) {
39: BranchEntry& e = entry[i];
40: e.count = 0;
41: e.from = 0xffffffff;
42: e.to = 0xffffffff;
43: e.inst = 0;
44: }
45: }
46:
47: // 使用中のエントリ数を取得する
48: int
49: BranchHistory::GetUsed() const
50: {
51: int i = 0;
52:
53: // top の位置から一巡するまで。有効エントリは count > 0。
54: for (; i < 256; i++) {
55: if (entry[(256 + top - i) % 256].count == 0) {
56: break;
57: }
58: }
59: return i;
60: }
61:
62: // モニタ更新
63: void
1.1.1.4 ! root 64: BranchHistory::MonitorUpdate(Monitor *, TextScreen& screen)
1.1 root 65: {
66: uint8 t;
67: int y;
68: int ydelta;
69: int row;
70:
71: // userdata は表示開始位置と各種フラグ。
72: // 下位 32bit が表示開始位置で、0 なら先頭のエントリから、1 なら先頭の
1.1.1.4 ! root 73: // 一つ次のエントリから、…を表す (表示行数は screen の高さ分)。
1.1 root 74: // BottomToTop が %1 なら並び順を下から上に変える (コンソール用)。
1.1.1.4 ! root 75: bool bottom_to_top = (screen.userdata & BottomToTop);
! 76: int pos = (int)screen.userdata;
1.1 root 77:
78: // 0 1 2 3 4 5 6
79: // 0123456789012345678901234567890123456789012345678901234567890123456789
80: // No. FromAddr Instruction ToAddr Iteration
81: // 001 U:01234567(01234567:bcnd.n) -> $01234567 x123456789
82: // 001 U:01234567(0123:trap ) -> $01234567 x123456789
83: // 002 S:01234567< 2> Bus Error
84: // 002 S:01234567< 69> MFP Timer-C
85: // 01234567890123456789012
86:
1.1.1.4 ! root 87: screen.Clear();
! 88: row = screen.GetRow();
1.1 root 89:
90: // 最初の1行は常にヘッダ
91: if (is_exhist) {
1.1.1.4 ! root 92: screen.Print(0, 0, "No. FromAddr Vec. Exception");
! 93: screen.Print(45, 0, "Iteration");
1.1 root 94: } else {
1.1.1.4 ! root 95: screen.Print(0, 0, "No. FromAddr Instruction");
! 96: screen.Print(35, 0, "ToAddr Iteration");
1.1 root 97: }
98:
99: // pos は最新を 0 とした通し番号。(スクロールしてたら開始が 0 とは限らない)
100: // t が entry[] 上の現在位置。
101: // y は表示座標。(上から表示か下から表示かが変わる)
102: t = top - pos;
103: int pos_end = pos + row - 1;
104: if (bottom_to_top == false) {
105: y = 1;
106: ydelta = 1;
107: } else {
108: y = row - 1;
109: ydelta = -1;
110: }
111: for (; pos < pos_end; pos++, y += ydelta) {
112: BranchEntry& e = entry[t--];
113: if (e.count == 0) {
114: continue;
115: }
116: std::string str = FormatEntry(e);
1.1.1.4 ! root 117: screen.Print(0, y, "%3d %s", pos, str.c_str());
1.1 root 118: if (e.count > 1) {
1.1.1.4 ! root 119: screen.Print(45, y, "x%9u", e.count);
1.1 root 120: }
121: }
122: }
123:
124:
125:
126: //
127: // m680x0 ブランチ履歴 (どこに置くのがよいか)
128: //
129:
130: // m680x0 ブランチ履歴では、通常ブランチ、例外発生、例外ベクタによる
131: // ジャンプの3つを区別する。
132: //
133: // 通常の分岐:
1.1.1.3 root 134: // from は上位31ビットが分岐元 PC、最下位ビットが S/U、
135: // to は32ビット全体が分岐先 PC、
136: // inst の上位16ビットに命令の1ワード目。
1.1 root 137: // inst の $8000 が立っていないことで区別する。
138: //
139: // 例外発生:
1.1.1.3 root 140: // from は上位31ビットが例外発生時の PC、最下位ビットが S/U、
141: // to は不問(通常 0 をセットすること)、
1.1 root 142: // inst は $00008LVV で $8000 が例外フラグ、VV がベクタ番号である。
143: // ベクタ番号が $00 だと次項(ベクタによる分岐)と区別つかなくなってしまう
144: // ため、リセット例外 (ベクタ番号0) は V=$01 で記録する。
145: // L は割り込みなら割り込みレベル。
146: // inst に $8000 が立っていて、かつ $8000 と一致しないことで区別する。
147: //
148: // 例外ベクタによる分岐 (例外処理の最後に起きる):
1.1.1.3 root 149: // from はベクタアドレス、
150: // to は分岐先 (0 も起こりえる)、
151: // inst は $00008000。
1.1 root 152: // inst が $00008000 と一致することで区別する (ビットが立っているではない)。
153:
154: // コンストラクタ
1.1.1.4 ! root 155: BranchHistory_m680x0::BranchHistory_m680x0(bool mode_)
! 156: : inherited(mode_)
1.1 root 157: {
158: }
159:
160: // デストラクタ
161: BranchHistory_m680x0::~BranchHistory_m680x0()
162: {
163: }
164:
165: // 1エントリ分の表示内容作成
166: std::string
167: BranchHistory_m680x0::FormatEntry(const BranchEntry& e)
168: {
169: // m68k では from の最下位1ビットを S/U として使う
170: std::string desc = string_format("%c:%08x",
171: (e.from & 1) ? 'S' : 'U', (e.from & ~1));
172:
173: if ((e.inst & 0x8000)) {
174: if ((e.inst & 0x7fff) != 0) {
175: // 例外発生記録
176: // 発生アドレスとベクタ(と割り込みレベル)を表示
177: uint32 vector = e.inst & 0xff;
178: if (vector == 1) {
179: vector = 0;
180: }
181: // とりあえず
182: desc += string_format("<%3d> %s", vector,
183: m68030_get_exception_name(vector));
184: // XXX TODO 割り込みレベル
185: } else {
186: // 例外ベクタフェッチしてジャンプ
187: desc += string_format("(vector fetch) -> $%08x", e.to);
188: }
189: } else {
190: // ブランチ
191: const char *mnemonic;
192: uint32 inst = e.inst >> 16;
193: if (inst == 0x4e73) {
194: mnemonic = ":rte";
195: } else if (inst == 0x4e75) {
196: mnemonic = ":rts";
197: } else if (inst == 0x4e77) {
198: mnemonic = ":rtr";
199: } else if ((inst & 0xffc0) == 0x4e80) {
200: mnemonic = ":jsr";
201: } else if ((inst & 0xffc0) == 0x4ec0) {
202: mnemonic = ":jmp";
203: } else if ((inst & 0xfff8) == 0x51c8) {
204: mnemonic = ":dbra";
205: } else if ((inst & 0xf0f8) == 0x50c8) {
206: mnemonic = ":dbcc";
207: } else if ((inst & 0xff00) == 0x6000) {
208: mnemonic = ":bra";
209: } else if ((inst & 0xff00) == 0x6100) {
210: mnemonic = ":bsr";
211: } else if ((inst & 0xf000) == 0x6000) {
212: mnemonic = ":bcc";
213: } else if ((inst & 0xfff8) == 0xf248) {
214: mnemonic = ":fdbcc";
215: } else if ((inst & 0xff80) == 0xf280) {
216: mnemonic = ":fbcc";
217: } else {
218: mnemonic = "";
219: }
220: desc += string_format("(%04x%-6s) -> $%08x", inst, mnemonic, e.to);
221: }
222: return desc;
223: }
224:
225:
226: //
227: // m88xx0 ブランチ履歴 (どこに置くのがよいか)
228: //
229:
230: // m88xx0 ブランチ履歴では、通常ブランチ、例外発生の2つを区別する。
231: // m68k は例外ベクタに書いてあるアドレスに飛ぶのでもう1種類分けてあったが、
232: // m88k は例外ベクタを直接実行するのでそれは不要。
233: //
234: // 通常の分岐:
1.1.1.3 root 235: // from は上位30ビットが分岐元 XIP、最下位ビットが S/U、
236: // to は32ビット全体が分岐先アドレス、
237: // inst は命令ワード。
1.1 root 238: //
239: // 例外発生:
1.1.1.3 root 240: // from は上位30ビットが例外発生時の XIP、最下位ビットが S/U、
241: // to は 0。
242: // inst は $fc000VVV で VVV (9ビット)がベクタ番号。
243: // 命令ワードとは衝突しない (instruction.txt 参照)。
1.1 root 244:
245: // コンストラクタ
1.1.1.4 ! root 246: BranchHistory_m88xx0::BranchHistory_m88xx0(bool mode_)
! 247: : inherited(mode_)
1.1 root 248: {
249: }
250:
251: // デストラクタ
252: BranchHistory_m88xx0::~BranchHistory_m88xx0()
253: {
254: }
255:
256: // 1エントリ分の表示内容作成
257: std::string
258: BranchHistory_m88xx0::FormatEntry(const BranchEntry& e)
259: {
260: // m88k では from の最下位1ビットを S/U として使う
261: std::string desc = string_format("%c:%08x",
262: (e.from & 1) ? 'S' : 'U', (e.from & ~1));
263:
264: if (e.inst >= 0xfc000000 && e.to == 0) {
265: // 例外発生記録
266: // 発生アドレスとベクタを表示
267: uint32 vector = e.inst & 0x1ff;
268: const char *name = m88kcpu::GetExceptionName(vector);
269: desc += string_format("<%3d> %s", vector, name ?: "");
270: if (vector == 450) {
271: // OpenBSD のシステムコール番号
272: desc += string_format("(%d)", (e.inst >> 12) & 0xfff);
273: }
274: } else if (e.inst >= 0xc0000000) {
275: // ブランチ
276: const char *mnemonic = "";
277: uint32 up4 = (e.inst >> 26) & 0xf;
278: switch (up4) {
279: case 0: mnemonic = ":br"; break;
280: case 1: mnemonic = ":br.n"; break;
281: case 2: mnemonic = ":bsr"; break;
282: case 3: mnemonic = ":bsr.n"; break;
283: case 4: mnemonic = ":bb0"; break;
284: case 5: mnemonic = ":bb0.n"; break;
1.1.1.2 root 285: case 6: mnemonic = ":bb1"; break;
1.1 root 286: case 7: mnemonic = ":bb1.n"; break;
287:
288: case 0xa: mnemonic = ":bcnd"; break;
289: case 0xb: mnemonic = ":bcnd.n"; break;
290: case 0xc: {
291: uint32 lo6 = (e.inst >> 10) & 0x3f;
292: if (lo6 == 0x34) {
293: mnemonic = ":tb0";
294: } else if (lo6 == 0x36) {
295: mnemonic = ":tb1";
296: } else if (lo6 == 0x3a) {
297: mnemonic = ":tcnd";
298: }
299: break;
300: }
301: case 0xd: {
302: uint32 lo6 = (e.inst >> 10) & 0x3f;
303: if (lo6 == 0x30) {
304: if ((e.inst & 0x1f) == 1) {
305: mnemonic = ":jmp r1";
306: } else {
307: mnemonic = ":jmp";
308: }
309: } else if (lo6 == 0x31) {
310: mnemonic = ":jmp.n";
311: } else if (lo6 == 0x32) {
312: mnemonic = ":jsr";
313: } else if (lo6 == 0x33) {
314: mnemonic = ":jsr.n";
315: } else if (lo6 == 0x3f) {
316: mnemonic = ":rte";
317: }
318: break;
319: }
320: case 0xe: mnemonic = ":tbnd"; break;
321: default:
322: break;
323: }
324: desc += string_format("(%08x%-7s) -> $%08x", e.inst, mnemonic, e.to);
325: }
326: return desc;
327: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.