openMSX
Dasm.cc
Go to the documentation of this file.
1#include "Dasm.hh"
2#include "DasmTables.hh"
3#include "MSXCPUInterface.hh"
4#include "narrow.hh"
5#include "strCat.hh"
6
7namespace openmsx {
8
9static constexpr char sign(unsigned char a)
10{
11 return (a & 128) ? '-' : '+';
12}
13
14static constexpr int abs(unsigned char a)
15{
16 return (a & 128) ? (256 - a) : a;
17}
18
19unsigned dasm(const MSXCPUInterface& interface, word pc, std::span<byte, 4> buf,
20 std::string& dest, EmuTime::param time)
21{
22 const char* r = nullptr;
23
24 buf[0] = interface.peekMem(pc, time);
25 auto [s, i] = [&]() -> std::pair<const char*, unsigned> {
26 switch (buf[0]) {
27 case 0xCB:
28 buf[1] = interface.peekMem(pc + 1, time);
29 return {mnemonic_cb[buf[1]], 2};
30 case 0xED:
31 buf[1] = interface.peekMem(pc + 1, time);
32 return {mnemonic_ed[buf[1]], 2};
33 case 0xDD:
34 case 0xFD:
35 r = (buf[0] == 0xDD) ? "ix" : "iy";
36 buf[1] = interface.peekMem(pc + 1, time);
37 if (buf[1] != 0xcb) {
38 return {mnemonic_xx[buf[1]], 2};
39 } else {
40 buf[2] = interface.peekMem(pc + 2, time);
41 buf[3] = interface.peekMem(pc + 3, time);
42 return {mnemonic_xx_cb[buf[3]], 4};
43 }
44 default:
45 return {mnemonic_main[buf[0]], 1};
46 }
47 }();
48
49 for (int j = 0; s[j]; ++j) {
50 switch (s[j]) {
51 case 'B':
52 buf[i] = interface.peekMem(narrow_cast<word>(pc + i), time);
53 strAppend(dest, '#', hex_string<2>(
54 static_cast<uint16_t>(buf[i])));
55 i += 1;
56 break;
57 case 'R':
58 buf[i] = interface.peekMem(narrow_cast<word>(pc + i), time);
59 strAppend(dest, '#', hex_string<4>(
60 pc + 2 + static_cast<int8_t>(buf[i])));
61 i += 1;
62 break;
63 case 'W':
64 buf[i + 0] = interface.peekMem(narrow_cast<word>(pc + i + 0), time);
65 buf[i + 1] = interface.peekMem(narrow_cast<word>(pc + i + 1), time);
66 strAppend(dest, '#', hex_string<4>(buf[i] + buf[i + 1] * 256));
67 i += 2;
68 break;
69 case 'X':
70 buf[i] = interface.peekMem(narrow_cast<word>(pc + i), time);
71 strAppend(dest, '(', r, sign(buf[i]), '#',
72 hex_string<2>(abs(buf[i])), ')');
73 i += 1;
74 break;
75 case 'Y':
76 strAppend(dest, r, sign(buf[2]), '#', hex_string<2>(abs(buf[2])));
77 break;
78 case 'I':
79 dest += r;
80 break;
81 case '!':
82 dest = strCat("db #ED,#", hex_string<2>(buf[1]),
83 " ");
84 return 2;
85 case '@':
86 dest = strCat("db #", hex_string<2>(buf[0]),
87 " ");
88 return 1;
89 case '#':
90 dest = strCat("db #", hex_string<2>(buf[0]),
91 ",#CB,#", hex_string<2>(buf[2]),
92 ' ');
93 return 2;
94 case ' ': {
95 dest.resize(7, ' ');
96 break;
97 }
98 default:
99 dest += s[j];
100 break;
101 }
102 }
103 dest.resize(19, ' ');
104 return i;
105}
106
107} // namespace openmsx
byte peekMem(word address, EmuTime::param time) const
Peek memory location.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
const std::array< const char *, 256 > mnemonic_xx
Definition: DasmTables.cc:113
unsigned dasm(const MSXCPUInterface &interface, word pc, std::span< byte, 4 > buf, std::string &dest, EmuTime::param time)
Disassemble.
Definition: Dasm.cc:19
const std::array< const char *, 256 > mnemonic_ed
Definition: DasmTables.cc:77
const std::array< const char *, 256 > mnemonic_xx_cb
Definition: DasmTables.cc:5
const std::array< const char *, 256 > mnemonic_cb
Definition: DasmTables.cc:41
const std::array< const char *, 256 > mnemonic_main
Definition: DasmTables.cc:149
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
std::string strCat(Ts &&...ts)
Definition: strCat.hh:542
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:620