-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialmon.ino
200 lines (165 loc) · 3.78 KB
/
serialmon.ino
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
/* Serial monitor based on 6502 hex monitor (wozmon)
*/
#include <stdint.h>
#include <stdarg.h>
#define SERIAL_PRINTF_MAX_BUFF 256
void serialPrintf(const char *fmt, ...);
void serialPrintf(const char *fmt, ...)
{
char buff[SERIAL_PRINTF_MAX_BUFF];
va_list pargs;
va_start(pargs, fmt);
vsnprintf(buff, SERIAL_PRINTF_MAX_BUFF, fmt, pargs);
va_end(pargs);
Serial.print(buff);
}
// #define DEBUG_SLOW
#ifdef DEBUG_SLOW
// TODO(proto): make restart or infinite loop on assertion fail?
#define assert(exp) if(!(exp)) serialPrintf("%s:%d: Assertion failed\n", __FILE__, __LINE__)
#else
#define assert(...)
#endif
#define internal static
#define global_variable static
// typedef uint8_t bool8;
typedef enum Command_t {
C_PRINT,
C_PRINT_RANGE,
C_STORE,
} Command;
#define SERIAL_BUFFER_CAP 128
struct Monitor_t {
int inputBufferSize;
char inputBuffer[SERIAL_BUFFER_CAP];
};
typedef struct Monitor_t Monitor;
internal inline int parseHexDigit(const char digit)
{
if ('0' <= digit && digit <= '9') {
return digit - '0';
} else if ('a' <= digit && digit <= 'f') {
return digit - 'a' + 10;
} else if ('A' <= digit && digit <= 'F') {
return digit - 'A' + 10;
}
return -1;
}
internal inline uint8_t getData(uint16_t addr)
{
return *((int8_t *) addr);
}
internal inline void setData(uint16_t addr, uint8_t value)
{
*((uint8_t *) addr) = value;
}
internal void startMonitor(Monitor *mon);
internal void startMonitor(Monitor *mon)
{
mon->inputBufferSize = 0;
serialPrintf("\\\n");
}
internal void parseBuffer(Monitor *mon);
internal void parseBuffer(Monitor *mon)
{
int index = 0;
char c = 0;
uint16_t storeAddr = 0;
uint16_t printAddr = 0;
uint16_t number = 0;
Command command = C_PRINT;
assert(mon->inputBufferSize <= SERIAL_BUFFER_CAP);
assert(mon->inputBuffer[mon->inputBufferSize-1] == '\n');
while(index < SERIAL_BUFFER_CAP) {
assert(index < mon->inputBufferSize);
c = mon->inputBuffer[index];
if (c == '\n')
return;
// blank
if (isspace(c)) {
index++;
continue;
}
if (c == ':' || c == '=') {
//wirte command
command = C_STORE;
index++;
continue;
}
if (c == '.' || c == '-') {
//print range
command = C_PRINT_RANGE;
index++;
continue;
}
int startNumber = index;
// first number
number = 0;
while (isxdigit(c)) {
number = number << 4 | parseHexDigit(c);
c = mon->inputBuffer[++index];
}
if(index == startNumber) {
startMonitor(mon);
return;
}
// execute command
switch (command) {
case C_PRINT: {
printAddr = number;
storeAddr = number;
serialPrintf("\n%.4x:", printAddr);
serialPrintf(" %.2x", getData(printAddr));
} break;
case C_PRINT_RANGE: {
command = C_PRINT;
while(printAddr < number) {
printAddr++;
if(printAddr % 8 == 0) {
serialPrintf("\n%.4x:", printAddr);
}
serialPrintf(" %.2x", getData(printAddr));
}
} break;
case C_STORE: {
setData(storeAddr++, (uint8_t) number);
} break;
default: {
assert(0 && "unreachable");
startMonitor(mon);
return;
} break;
}
}
}
global_variable bool ledState;
global_variable Monitor monitor;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
startMonitor(&monitor);
}
void loop() {
static long int timestamp;
// NOTE(proto): volatile to try avoid optimization
// so we can change it in the monitor
static volatile uint16_t interval = 400;
if (millis() - timestamp > interval) {
digitalWrite(LED_BUILTIN, ledState);
ledState = !ledState;
timestamp = millis();
}
while(Serial.available()) {
char c = Serial.read();
if (monitor.inputBufferSize < SERIAL_BUFFER_CAP) {
monitor.inputBuffer[monitor.inputBufferSize++] = c;
if (c == '\n') {
parseBuffer(&monitor);
monitor.inputBufferSize = 0;
serialPrintf("\n");
}
} else {
startMonitor(&monitor);
}
}
}