-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
283 lines (217 loc) · 6.1 KB
/
monitor.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "vr65c02.h"
#include <6502.h>
#include <peekpoke.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROMPT ">"
#define INPUT_BUFFER_SIZE 32
#define COMMAND_MAX_LENGTH 6
#define MEMDUMP_BYTES 128
typedef struct {
char name[6];
char param1[6];
char param2[6];
char desc[48];
void (*handler)();
} command_t;
void header();
void prompt();
void free_ram();
uint8_t readline(char *buffer);
void memdump(uint16_t address, uint8_t bytes);
void handler_help();
void handler_info();
void handler_peek();
void handler_poke();
void handler_dump();
void handler_demo();
void handler_time();
void command_not_found(char *command_name);
// CPU type names
static const char *cpu_names[] = {"6502", "65C02", "65816",
"4510", "65SC02", "65CE02"};
static const command_t commands[] = {
{"help", "", "", "Show this message", handler_help},
{"info", "", "", "Get system info", handler_info},
{"peek", "addr", "", "Peek at a memory address", handler_peek},
{"poke", "addr", "val", "Poke a value into memory", handler_poke},
{"dump", "addr", "", "Dump memory in hex and ASCII", handler_dump},
{"demo", "", "", "Run a hardware demo", handler_demo},
{"time", "", "", "Display the current time", handler_time},
};
static const uint8_t COMMAND_COUNT = sizeof(commands) / sizeof(command_t);
int main() {
uint8_t i;
char buffer[INPUT_BUFFER_SIZE];
char *command;
bool command_handled;
header();
while (true) {
command_handled = false;
// Present the command prompt and wait for input
prompt();
readline(buffer);
printf("\r\n");
command = strtok(buffer, " ");
// Look for the command name in the command list
for (i = 0; i < COMMAND_COUNT; i++) {
if (strncmp(command, commands[i].name, COMMAND_MAX_LENGTH) == 0) {
// Found the command, handle it
commands[i].handler();
command_handled = true;
break;
}
}
if (!command_handled) {
command_not_found(command);
}
printf("\r\n");
}
return 0;
}
void header() { printf("\r\n*** VR65C02 Monitor ***\r\n"); }
void prompt() { printf("%s ", PROMPT); }
void free_ram() { printf("%d bytes free", _heapmemavail()); }
void handler_help() {
uint8_t i = 0;
printf("Available commands:\r\n");
for (i = 0; i < COMMAND_COUNT; i++) {
printf(commands[i].name);
if (commands[i].param1[0] != '\0') {
printf(" <");
printf(commands[i].param1);
printf(">");
}
if (commands[i].param2[0] != '\0') {
printf(" <");
printf(commands[i].param2);
printf(">");
}
printf(" : ");
printf(commands[i].desc);
if (i != COMMAND_COUNT - 1) {
printf("\r\n");
}
}
}
void handler_info() {
printf("CPU: ");
printf(cpu_names[getcpu()]);
printf("\r\nRAM: ");
free_ram();
}
void handler_peek() {
char *param1 = strtok(NULL, " ");
uint16_t addr = strtol(param1, 0, 16);
printf("%02X", PEEK(addr));
}
void handler_poke() {
char *param1 = strtok(NULL, " ");
char *param2 = strtok(NULL, " ");
uint16_t addr = strtol(param1, 0, 16);
uint8_t val = strtol(param2, 0, 16);
POKE(addr, val);
printf("OK");
}
void handler_dump() {
char *param1 = strtok(NULL, " ");
uint16_t addr = strtol(param1, 0, 16);
memdump(addr, MEMDUMP_BYTES);
}
void handler_demo() {
char rx = 0;
unsigned char io = 0;
// Set all GPIO pins to output
POKE(VIA_DIR, 0xF);
while (1) {
// Watch for escape key press
if (PEEK(UART_RX_RDY) == 1) {
rx = PEEK(UART_RX);
if (rx == 0x1B) {
printf("Stopping demo.");
POKE(VIA_DIR, 0);
return;
}
}
printf("GPIO: %01X\r\n", io);
POKE(VIA, io);
io++;
}
}
uint8_t bcd2bin(uint8_t val) { return val - 6 * (val >> 4); }
void handler_time() {
uint8_t b[7] = {0};
int i = 0;
uint8_t ack = 0;
printf("Reading time...\r\n");
// Init I2C
POKE(I2C_CTRL, 1);
// Set I2C device address
POKE(I2C_ADDR, 0x68); // RTC address is 0x68
// Send "now" command
POKE(I2C_DATA, 0);
// Read ack
ack = PEEK(I2C_DATA);
// Read 7 bytes of datetime from the RTC
b[0] = bcd2bin(PEEK(I2C_DATA));
b[1] = bcd2bin(PEEK(I2C_DATA));
b[2] = bcd2bin(PEEK(I2C_DATA));
b[3] = bcd2bin(PEEK(I2C_DATA));
b[4] = bcd2bin(PEEK(I2C_DATA));
b[5] = bcd2bin(PEEK(I2C_DATA) & 0x7F);
b[6] = bcd2bin(PEEK(I2C_DATA));
printf("%04d-%02d-%02d %02d:%02d:%02d\r\n", b[6] + 2000, b[5], b[4], b[2],
b[1], b[0]);
}
void print_string_bin(char *str, uint8_t max) {
uint8_t i = 0;
while (i < max) {
if (str[i] >= 32 && str[i] < 127) {
serial_putc(str[i]);
} else {
serial_putc('.');
}
i++;
}
}
void memdump(uint16_t address, uint8_t bytes) {
uint16_t i = 0;
uint8_t b = 0;
printf("%04X ", address);
while (i < bytes) {
b = PEEK(address + i);
printf("%02X ", b);
i++;
if (i % 16 == 0 && i < bytes) {
printf(" |");
print_string_bin((char *)(address + i - 16), 16);
printf("|\r\n%04X ", address + i);
} else if (i % 8 == 0) {
serial_putc(' ');
}
}
serial_putc('|');
print_string_bin((char *)(address + i - 16), 16);
serial_putc('|');
}
void command_not_found(char *command_name) {
printf("Command not found: %s", command_name);
}
uint8_t readline(char *buffer) {
uint8_t count = 0;
uint8_t in = serial_getc();
while (in != '\n' && in != '\r') {
// Character is printable ASCII
if (in >= 0x20 && in < 0x7F) {
serial_putc(in);
buffer[count] = in;
count++;
}
in = serial_getc();
}
buffer[count] = 0;
return count;
}