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