-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.asm
716 lines (603 loc) · 9.43 KB
/
monitor.asm
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
ROM_BEGIN equ 0x0000
ROM_END equ 0x1FFF
RAM_BEGIN equ 0x8000
RAM_END equ 0x9FFF
CR equ 0x0D
LF equ 0x0A
TAB equ 0x09
BS equ 0x7F
SPACE equ 0x20
XOFF equ 0x13
XON equ 0x11
;variables
rxd_buff equ 0x9E00
rxd_rptr equ 0x9F00
rxd_wptr equ 0x9F01
addr_l equ 0x9F02
addr_h equ 0x9F03
byte_buff equ 0x9F04
isxon equ 0x9F04
tmp equ 0x9F05 ;4 bytes
;io ports
UDR equ 0x00
org 0x0000
reset: ld sp, RAM_END
ld a, 0
ld (rxd_rptr), a
ld (rxd_wptr), a
ld a, XOFF
rst 0x28
;set up interrupts
im 1
ei
jp main
org 0x0018
;print routine
org 0x0020
jp print
db 'PRINT'
;uart write routine
org 0x0028
jp txd
db 'TXD', 0, 0
;uart read routine
org 0x0030
jp rxd_pop
db 'RXD', 0, 0
;interrupt service routine
org 0x0038
irq: push af
push bc
push hl
in a, (UDR)
ld b, a
ld h, high rxd_buff
ld a, (rxd_rptr)
ld l, a
ld a, (rxd_wptr)
inc a
cp l
jr z, irq_buffer_full
dec a
ld l, a
ld (hl), b
inc a
ld (rxd_wptr), a
irq_buffer_full:
pop hl
pop bc
pop af
ei
ret
;######################################
main_done:
ld hl, done_msg
rst 0x20
jr main_loop
main:
ld hl, welcome_msg
rst 0x20
main_help:
ld hl, menu_msg
rst 0x20
main_loop:
ld a, 0
rst 0x30
cp 'r'
jp z, read
cp 'w'
jp z, write
cp 'm'
jp z, modify
cp 'x'
jp z, execute
cp ':'
jp z, intelhex
cp 'h'
jr z, main_help
ld hl, invalid_msg
rst 0x20
ld b, a
ld a, 0x27 ;'
rst 0x28
ld a, b
rst 0x28
ld a, 0x27 ;'
rst 0x28
ld hl, new_line
rst 0x20
jr main_loop
;######################################
read:
ld hl, read_addr_msg
rst 0x20
call get_addr
ld hl, read_len_msg
rst 0x20
call get_byte
jr c, main_done ;user aborted
ld a, (addr_h)
ld h, a
ld a, (addr_l)
and 0xF0
ld l, a
ld a, (byte_buff)
ld d, a
ld a, 0
ld e, a
read_loop:
ld a, e
cp 0
call z, print_addr
ld a, e
cp 0
jr nz, read_no_new_line
ld a, 16
read_no_new_line:
dec a
ld e, a
ld a, (hl)
inc hl
call byte2hex
ld a, b
rst 0x28
ld a, c
rst 0x28
ld a, SPACE
rst 0x28
ld a, d
dec a
cp 0
jp z, main_done
ld d, a
jr read_loop
;######################################
write:
ld hl, write_addr_msg
rst 0x20
call get_addr
ld a, (addr_h)
ld h, a
ld a, (addr_l)
ld l, a
write_loop:
;print begin address
call print_addr
ld c, 0
push hl
call get_byte_loop ;skip msg print
pop hl
jp c, main_done ;user aborted
ld a, (byte_buff)
ld (hl), a
inc hl
jr write_loop
;######################################
modify:
ld hl, mod_addr_msg
rst 0x20
call get_addr
ld a, (addr_h)
ld h, a
ld a, (addr_l)
ld l, a
mod_loop:
;print begin address
call print_addr
ld a, (hl)
push hl
call byte2hex
ld h, b
ld l, c
ld a, b
rst 0x28
ld a, c
rst 0x28
ld c, 2
call get_byte_loop ;skip msg print
pop hl
jp c, main_done ;user aborted
ld a, (byte_buff)
ld (hl), a
inc hl
jr mod_loop
;######################################
execute:
ld hl, exe_addr_msg
rst 0x20
call get_addr
ld a, (addr_h)
ld h, a
ld a, (addr_l)
ld l, a
jp (hl)
;######################################
intelhex:
;data length
call intelhex_rxd
ld b, a
ld c, a
;address
call intelhex_rxd
ld d, a
add a, c
ld c, a
call intelhex_rxd
ld e, a
add a, c
ld c, a
;record type
call intelhex_rxd
cp 1 ;last record
jr z, intelhex_done
cp 0 ;data record
jp nz, intelhex_type_error
add a, c
ld c, a
intelhex_loop:
call intelhex_rxd
ld (de), a
inc de
add a, c
ld c, a
djnz intelhex_loop
call intelhex_rxd
add a, c
cp 0
jp nz, intelhex_checksum_error
jp main_done
intelhex_type_error:
ld hl, type_error_msg
rst 0x20
jp main_loop
intelhex_checksum_error:
ld hl, checksum_error_msg
rst 0x20
jp main_loop
intelhex_char_error:
ld hl, char_error
rst 0x20
jp main_loop
intelhex_done:
call intelhex_rxd ;flush last byte
jp main_done
intelhex_rxd:
ld a, 0
rst 0x30
call ishex
jr c, intelhex_char_error
ld h, a
ld a, 0
rst 0x30
call ishex
jr c, intelhex_char_error
ld l, a
call hex2byte
ret
;######################################
get_addr:
ld hl, address_msg
rst 0x20
ld c, 0 ;number of aquired digits
ld hl, tmp
get_addr_loop:
ld a, 0
rst 0x30
cp BS
jr z, get_addr_bs
cp CR
jr z, get_addr_enter
ld b, a
ld a, c
cp 4
jr z, get_addr_loop
ld a, b
call ishex
jr c, get_addr_loop
inc c
rst 0x28
ld (hl), a
inc hl
jr get_addr_loop
get_addr_bs:
ld a, c
cp 0
jr z, get_addr_loop
ld a, BS
rst 0x28
dec c
dec hl
jr get_addr_loop
get_addr_enter:
ld a, c
cp 4
jr nz, get_addr_loop
ld hl, tmp
ld b, (hl)
inc hl
ld c, (hl)
ld h, b
ld l, c
call hex2byte
ld (addr_h), a
ld hl, tmp+2
ld b, (hl)
inc hl
ld c, (hl)
ld h, b
ld l, c
call hex2byte
ld (addr_l), a
ld hl, new_line
rst 0x20
ret
;######################################
get_byte:
ld hl, byte_msg
rst 0x20
ld c, 0 ;number of aquired digits
get_byte_loop:
ld a, 0
rst 0x30
cp BS
jr z, get_byte_bs
cp CR
jr z, get_byte_enter
cp 'q'
jr z, get_byte_aborted
ld b, a
ld a, c
cp 2
jr z, get_byte_loop
ld a, b
call ishex
jr c, get_byte_loop
inc c
rst 0x28
ld b, a
ld a, c
cp 2
jr z, get_byte_store_low
ld h, b
jr get_byte_loop
get_byte_aborted:
ld hl, abort_msg
rst 0x20
scf
ret
get_byte_store_low
ld l, b
jr get_byte_loop
get_byte_bs:
ld a, c
cp 0
jr z, get_byte_loop
ld a, BS
rst 0x28
dec c
jr get_byte_loop
get_byte_enter:
ld a, c
cp 2
jr nz, get_byte_loop
call hex2byte
ld (byte_buff), a
ld hl, new_line
rst 0x20
scf
ccf
ret
;######################################
;general purpose routines
print_addr:
ld a, CR
rst 0x28
ld a, LF
rst 0x28
ld a, h
call byte2hex
ld a, b
rst 0x28
ld a, c
rst 0x28
ld a, l
call byte2hex
ld a, b
rst 0x28
ld a, c
rst 0x28
ld a, SPACE
rst 0x28
ret
;######################################
;converts value in a to hex
;returns 2 ascii hex character in bc (b, c) == (high, low)
byte2hex:
ld b, a
call nibble2hex
ld c, a
ld a, b
srl a
srl a
srl a
srl a
call nibble2hex
ld b, a
ret
;convers nibble to hex
;in a, out a
nibble2hex:
and 0x0F
cp 10
jr c, nibble2hex_digit
sub 10
add a, 'A'
ret
nibble2hex_digit:
add a, '0'
ret
;######################################
;checks if digit is valid hexadecimal
;sets carry if not
ishex:
cp '0'
jr c, ishex_no
cp ':'
jr nc, ishex_no_digit
scf
ccf
ret
ishex_no_digit:
cp 'A'
jr c, ishex_no
cp 'G'
jr nc, ishex_lower
scf
ccf
ret
ishex_lower
cp 'a'
jr c, ishex_no
cp 'g'
jr nc, ishex_no
scf
ccf
ret
ishex_no:
scf
ret
;######################################
;reads hexadecimal value (in HL) to byte (to A)
hex2byte:
push bc
ld a, h
call hex2nibble
sla a
sla a
sla a
sla a
ld b, a
ld a, l
call hex2nibble
or b
pop bc
ret
;reads one hexadecimal character
hex2nibble:
cp 'a'
jr nc, hex2nibble_nup
cp 'A'
jr nc, hex2nibble_up
sub '0'
ret
hex2nibble_nup:
sub 'a'
add a, 10
ret
hex2nibble_up:
sub 'A'
add a, 10
ret
;######################################
;pop received byte from the buffer
;if a == 0 wait for data if the buffer is empty
;returns received byte in a
rxd_pop:
push bc
push hl
rxd_pop_loop:
ld c, a
di ;can't allow irq to interrupt us now
ld a, (rxd_wptr)
ld l, a
ld a, (rxd_rptr)
cp l
jr z, rxd_pop_empty
ld b, a
ld a, XOFF
rst 0x28
ld a, b
ld h, high rxd_buff
ld l, a
ld a, (hl)
ei ;reenable interrupts
inc l
ld b, a
ld a, l
ld (rxd_rptr), a
ld a, b
rxd_pop_end:
pop hl
pop bc
ret
rxd_pop_empty:
ei
ld a, c
cp 0
jr nz, rxd_pop_end
ld a, XON
rst 0x28
halt ;wait for data
jr rxd_pop_loop
;######################################
;sends byte in a
txd:
out (UDR), a
ret
;######################################
;prints string till terminator (\0)
;pointer to the string in HL
print:
push af
print_loop:
ld a, (HL)
inc hl
cp 0
jr z, print_end
out (UDR), a
jr print_loop
print_end:
pop af
ret
;######################################
;constants storage
welcome_msg:
db CR, LF, 'Z80simple system monitor', CR, LF
db 'A.Kaminski 2016', CR, LF, 0
menu_msg:
db 'Available options:'
db CR, LF, TAB, 'h - this help'
db CR, LF, TAB, 'r - read memory'
db CR, LF, TAB, 'w - write memory'
db CR, LF, TAB, 'm - modify memory'
db CR, LF, TAB, 'x - execute program'
db CR, LF, TAB, ': - Intel HEX (just send line)'
db CR, LF, 0
new_line:
db CR, LF, 0
invalid_msg:
db 'Invalid choice: ', CR, LF, 0
address_msg:
db 'Input address (hex): ', 0
byte_msg:
db 'Input byte (hex): ', 0
done_msg:
db CR, LF, 'Done.', CR, LF, 0
abort_msg:
db CR, LF, 'Aborted.', CR, LF, 0
read_addr_msg:
db 'Provide start address for read', CR, LF, 0
read_len_msg:
db 'Provide number of bytes to read', CR, LF, 0
write_addr_msg:
db 'Provide start address for write', CR, LF, 0
mod_addr_msg:
db 'Provide start address for modify', CR, LF, 0
exe_addr_msg:
db 'Provice start address for execution', CR, LF, 0
type_error_msg:
db CR, LF, 'Unexpectected IntelHEX record type.', CR, LF, 0
checksum_error_msg:
db CR, LF, 'Checksum error.', CR, LF, 0
char_error:
db CR, LF, 'Received non-hex character.', CR, LF, 0