-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon.cpp
227 lines (184 loc) · 8.2 KB
/
mon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Simple Intel 8008 machine language monitor
//
// Copyright 2024 Christian Bauer
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#include "mon.h"
#include <Arduino.h>
static uint16_t getArg(String & args)
{
String arg;
int spaceIdx = args.indexOf(' ');
if (spaceIdx >= 0) {
arg = args.substring(0, spaceIdx);
args = args.substring(spaceIdx);
args.trim();
} else {
arg = args;
args = "";
}
return strtoul(arg.c_str(), 0, 16);
}
static char char2print(uint8_t c)
{
return (c >= 0x20 && c <= 0x7e) ? c : '.';
}
static void dumpMem(String & args, const uint8_t * mem)
{
if (args.length() == 0) {
Serial.println("Missing argument");
return;
}
uint16_t startAdr = getArg(args);
uint16_t endAdr;
const unsigned bytesPerLine = 16;
if (args.length() > 0) {
endAdr = getArg(args);
} else {
endAdr = startAdr + 8 * bytesPerLine - 1;
}
startAdr &= 0x3FFF;
endAdr &= 0x3FFF;
for (uint16_t adr = startAdr; adr <= endAdr; adr += bytesPerLine) {
char buf[256];
char ascii[bytesPerLine + 1];
int l = 0;
l += sprintf(buf + l, "%04x:", adr & 0x3FFF);
for (unsigned i = 0; i < bytesPerLine; ++i) {
uint8_t c = mem[(adr + i) & 0x3FFF];
l += sprintf(buf + l, " %02x", c);
ascii[i] = char2print(c);
}
ascii[bytesPerLine] = '\0';
l += sprintf(buf + l, " '%s'", ascii);
Serial.println(buf);
}
}
static const char * opcodeText[256] = {
// x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF
"HLT", "HLT", "RLC", "RFC", "ADI", "RST 0", "LAI", "RET", "INB", "DCB", "RRC", "RFZ", "ACI", "RST 1", "LBI", "RET", // 00..0F
"INC", "DCC", "RAL", "RFN", "SUI", "RST 2", "LCI", "RET", "IND", "DCD", "RAR", "RFP", "SBI", "RST 3", "LDI", "RET", // 10..1F
"INE", "DCE", "???", "RTC", "NDI", "RST 4", "LEI", "RET", "INH", "DCH", "???", "RTZ", "XRI", "RST 5", "LHI", "RET", // 20..2F
"INL", "DCL", "???", "RTN", "ORI", "RST 6", "LLI", "RET", "???", "???", "???", "RTP", "CPI", "RST 7", "LMI", "RET", // 30..3F
"JFC", "INP 0", "CFC", "INP 1", "JMP", "INP 2", "CAL", "INP 3", "JFZ", "INP 4", "CFZ", "INP 5", "JMP", "INP 6", "CAL", "INP 7", // 40..4F
"JFN", "OUT 8", "CFN", "OUT 9", "JMP", "OUT A", "CAL", "OUT B", "JFP", "OUT C", "CFP", "OUT D", "JMP", "OUT E", "CAL", "OUT F", // 50..5F
"JTC", "OUT 10", "CTC", "OUT 11", "JMP", "OUT 12", "CAL", "OUT 13", "JTZ", "OUT 14", "CTZ", "OUT 15", "JMP", "OUT 16", "CAL", "OUT 17", // 60..6F
"JTN", "OUT 18", "CTN", "OUT 19", "JMP", "OUT 1A", "CAL", "OUT 1B", "JTP", "OUT 1C", "CTP", "OUT 1D", "JMP", "OUT 1E", "CAL", "OUT 1F", // 70..7F
"ADA", "ADB", "ADC", "ADD", "ADE", "ADH", "ADL", "ADM", "ACA", "ACB", "ACC", "ACD", "ACE", "ACH", "ACL", "ACM", // 80..8F
"SUA", "SUB", "SUC", "SUD", "SUE", "SUH", "SUL", "SUM", "SBA", "SBB", "SBC", "SBD", "SBE", "SBH", "SBL", "SBM", // 90..9F
"NDA", "NDB", "NDC", "NDD", "NDE", "NDH", "NDL", "NDM", "XRA", "XRB", "XRC", "XRD", "XRE", "XRH", "XRL", "XRM", // A0..AF
"ORA", "ORB", "ORC", "ORD", "ORE", "ORH", "ORL", "ORM", "CPA", "CPB", "CPC", "CPD", "CPE", "CPH", "CPL", "CPM", // B0..BF
"NOP", "LAB", "LAC", "LAD", "LAE", "LAH", "LAL", "LAM", "LBA", "LBB", "LBC", "LBD", "LBE", "LBH", "LBL", "LBM", // C0..CF
"LCA", "LCB", "LCC", "LCD", "LCE", "LCH", "LCL", "LCM", "LDA", "LDB", "LDC", "LDD", "LDE", "LDH", "LDL", "LDM", // D0..DF
"LEA", "LEB", "LEC", "LED", "LEE", "LEH", "LEL", "LEM", "LHA", "LHB", "LHC", "LHD", "LHE", "LHH", "LHL", "LHM", // E0..EF
"LLA", "LLB", "LLC", "LLD", "LLE", "LLH", "LLL", "LLM", "LMA", "LMB", "LMC", "LMD", "LME", "LMH", "LML", "HLT", // F0..FF
};
static const uint8_t opcodeLen[256] = {
// x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF
1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, // 00..0F
1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, // 10..1F
1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, // 20..2F
1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, // 30..3F
3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, // 40..4F
3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, // 50..5F
3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, // 60..6F
3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, // 70..7F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80..8F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 90..9F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A0..AF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B0..BF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C0..CF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D0..DF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E0..EF
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // F0..FF
};
static void disassemble(String & args, const uint8_t * mem)
{
if (args.length() == 0) {
Serial.println("Missing argument");
return;
}
uint16_t startAdr = getArg(args);
uint16_t endAdr;
if (args.length() > 0) {
endAdr = getArg(args);
} else {
endAdr = startAdr + 32;
}
startAdr &= 0x3FFF;
endAdr &= 0x3FFF;
for (uint16_t adr = startAdr; adr <= endAdr; ) {
uint8_t op = mem[adr & 0x3FFF];
uint8_t len = opcodeLen[op];
char buf[256];
int l = 0;
l += sprintf(buf + l, "%04x:", adr & 0x3FFF);
for (unsigned i = 0; i < len; ++i) {
l += sprintf(buf + l, " %02x", mem[(adr + i) & 0x3FFF]);
}
for (unsigned i = len; i < 3; ++i) {
l += sprintf(buf + l, " ");
}
l += sprintf(buf + l, " %s", opcodeText[op]);
if (len == 2) {
l += sprintf(buf + l, " %02x", mem[(adr + 1) & 0x3FFF]);
} else if (len == 3) {
l += sprintf(buf + l, " %02x%02x", mem[(adr + 2) & 0x3FFF] & 0x3F, mem[(adr + 1) & 0x3FFF]);
}
Serial.println(buf);
adr += len;
}
}
void monitor(uint8_t * mem)
{
bool done = false;
while (! done) {
// Print prompt
Serial.print(".");
// Read and parse command line
String input;
while (! Serial.available()) { }
input = Serial.readStringUntil('\n');
input.trim();
Serial.println(input);
if (input.length() == 0) {
continue;
}
String cmd;
String args;
int spaceIdx = input.indexOf(' ');
if (spaceIdx > 0) {
cmd = input.substring(0, spaceIdx);
args = input.substring(spaceIdx);
args.trim();
} else {
cmd = input;
}
if (cmd.equals("h") || cmd.equals("??")) {
Serial.println("h Show list of commands");
Serial.println("g Run 8008 program");
Serial.println("m xxxx [yyyy] Dump memory");
Serial.println("d xxxx [yyyy] Disassemble");
} else if (cmd.equals("g")) {
done = true;
} else if (cmd.equals("m")) {
dumpMem(args, mem);
} else if (cmd.equals("d")) {
disassemble(args, mem);
} else {
Serial.println("Unknown command");
}
}
}