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