-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-monitor.z80
376 lines (306 loc) · 7.57 KB
/
simple-monitor.z80
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
;;
;; Simple monitor program.
;;
;; Accept strings from STDIN, and execute them.
;;
;; Built-in commands
;;
;; [c]all xxxx -> Call the routine at XXXX
;;
;; [d]ump -> Dump 16 bytes of RAM at a given address.
;;
;; [i]nput -> Enter bytes
;;
;; Other input will generate an "ERR" message, and be ignored.
;;
org 0
;;
;; Ensure we have a stack-pointer setup, with some room.
;;
ld sp, stack_start
;;
;; Entry-point to the monitor.
;;
;; Read text into into `input_buffer`, appending to the buffer until a newline
;; is seen, then invoke `process` to handle the input.
;;
monitor:
;; show the prompt.
ld a, '>'
out (1),a
ld hl, input_buffer
monitor_input_loop:
;; overwrite an previous input
ld (hl), '\n'
;; read and store the new character
in a,(1)
ld (hl),a
;; was it a newline? If so process.
cp '\n'
jr z, process_input_line
;; Otherwise loop round for more input.
inc hl
jr monitor_input_loop
;;
;; process_input_line is called when the monitor has received a complete
;; newline-terminated line of text.
;;
;; We process the contents by looking for commands we understand, if we see
;; input we don't recognize we show a message and return, otherwise we invoke
;; the appropriate handler.
;;
;; C => CALL
;; D => DUMP
;; I => INPUT
;;
process_input_line:
ld hl, input_buffer
ld a, (hl)
;; C == CALL
cp 'c'
jr z, call_handler
cp 'C'
jr z, call_handler
;; D == DUMP
cp 'd'
jr z, dump_handler
cp 'D'
jr z, dump_handler
;; I == INPUT
cp 'i'
jr z, input_handler
cp 'I'
jr z, input_handler
;;
;; Unknown command: show a message and restart our monitor
;;
;; We just show "ERR" which is simple, and saves bytes compared to
;; outputting a longer message and using a print-string routine.
;;
ld a, 'E'
out (1),a
ld a, 'R'
out (1), a
out (1), a
ld a, '\n'
out (1),a
jr monitor
;;
;; Call is invoked with the address to call
;;
;; For example "C0003" will call the routine at 0x0003
;;
call_handler:
;; Our input-buffer will start with [cC], so we start looking at the
;; next character.
ld hl, input_buffer+1
;; Read the address to call into BC
call read_16_bit_ascii_number
;; We'll be making a call, so we need to have the return
;; address on the stack so that when the call'd routine ends
;; execution goes somewhere sane.
;;
;; We'll want to re-load the monitor, so we'll store the
;; entry point on the stack
;;
ld hl,monitor
push hl
;; Now we jump, indirectly, to the address in the BC register.
push bc
ret
;;
;; Dump 16 bytes from the current dump_address
;;
;; We're called with either "D" to keep going where we left off or
;; "D1234" if we should start at the given offset.
;;
dump_handler:
;; Our input-buffer will start with [dD], so we start looking at the
;; next character.
ld hl, input_buffer+1
;; Look at the next input-byte. If empty then no address.
ld a, (hl)
cp '\n'
jr z, dump_handler_no_number
;; OK we expect an (ASCII) address following HL - read it into BC.
call read_16_bit_ascii_number
ld (dump_address), bc
dump_handler_no_number:
;; The address we start from
ld hl, (dump_address)
;; show the address
call output_16_bit_number
;; Loop to print the next 16 bytes at that address.
ld b, 16
dump_byte:
;; show a space
ld a, ' '
out (1), a
;; show the memory-contents.
ld c, (hl)
call output_8_bit_number
inc hl
djnz dump_byte
;; all done
ld a, '\n'
out (1),a
;; store our updated/final address.
ld (dump_address), hl
jmp_monitor:
jr monitor
;;
;; Input handler allows code to be assembled at a given address
;;
;; Usage is:
;;
;; I01234 01 02 03 04 0f
;;
;; i.e. "I<address> byte1 byte2 .. byteN"
;;
;; If there is no address keep going from the last time, which means this
;; works as you expect:
;;
;; I1000 01 03
;; I 03 04 0F
;;
input_handler:
;; Our input-buffer will start with [iI], so we start looking at the
;; next character.
ld hl, input_buffer+1
;; Look at the next input-byte. If it is a space then no address was
;; given, so we keep appending bytes to the address set previously.
ld a, (hl)
cp ' '
jr z, input_handler_no_address
;; OK we expect an (ASCII) address following HL - Read it into BC.
call read_16_bit_ascii_number
ld (input_address), bc
input_handler_no_address:
;; HL contains the a string. Get the next byte
ld a,(hl)
inc hl
;; space? skip
cp ' '
jr z, input_handler_no_address
;; newline? If so we're done
cp '\n'
jr z, jmp_monitor
;; OK then we have a two-digit number
dec hl
call read_8_bit_ascii_number
;; store the byte in RAM
ld bc, (input_address)
ld (bc), a
;; bump to the next address
inc bc
ld (input_address), bc
;; continue
jr input_handler_no_address
;;
;; Convert a 4-digit ASCII number, pointed to by HL to a number.
;; Return that number in BC.
;;
read_16_bit_ascii_number:
;; HL is a pointer to a four-char string
;; This is read as a 16 bit hex number
;; The number is stored in BC
call read_8_bit_ascii_number
ld b, a
call read_8_bit_ascii_number
ld c, a
ret
;;
;; Read the two-digit HEX number from HL, and convert to an integer
;; stored in the A-register.
;;
;; HL will be incremented twice.
;;
read_8_bit_ascii_number:
ld a, (hl)
;; is it lower-case? If so upper-case it.
cp 'a'
jr c, read_8_bit_ascii_number_uc
cp 'z'
jr nc, read_8_bit_ascii_number_uc
sub a, 32
read_8_bit_ascii_number_uc:
call read_8_bit_ascii_number_hex
add a, a
add a, a
add a, a
add a, a
ld d, a
inc hl
ld a, (hl)
;; upper-case the second character too, if necessary.
cp 'a'
jr c, conty
cp 'z'
jr nc, conty
sub a, 32
conty:
call read_8_bit_ascii_number_hex
or d
inc hl
ret
read_8_bit_ascii_number_hex:
sub a, '0'
cp 10
ret c
sub a,'A'-'0'-10
ret
;;
;; Display the 16-bit number stored in HL in hex.
;;
output_16_bit_number:
ld c,h
call output_8_bit_number
ld c,l
call output_8_bit_number
ret
;;
;; Display the 8-bit number stored in C in hex.
;;
output_8_bit_number:
ld a,c
rra
rra
rra
rra
call Conv
ld a,c
Conv:
and $0F
add a,$90
daa
adc a,$40
daa
out (1),a
ret
;;;;;;;;
;;;;;;;; RAM stuff
;;;;;;;;
;;
;; Here we store some values.
;;
;; DUMP: We track of the address from which we're dumping.
dump_address:
db 0,0
;; INPUT: Keep track of the address to which we next write.
input_address:
db 0,0
;; We don't nest calls too deeply ..
stack_end:
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
db 0, 0
stack_start:
;; Command-line input buffer.
input_buffer: