-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
read.z80
176 lines (147 loc) · 3.36 KB
/
read.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
;; Read sequential records from a file.
;;
;; Given a filename open it, and read 256 records to it - testing that the contents
;; match what we expect.
;;
;; A record of 0x00
;; A record of 0x01
;; ..
;; A record of 0xFF
;;
;;
FCB1: EQU 0x5C
DMA: EQU 0x80
DMA_LEN: EQU 128
BDOS_ENTRY_POINT: EQU 5
BDOS_OUTPUT_STRING: EQU 9
BDOS_READ_FILE: EQU 20
BDOS_OPEN_FILE: EQU 15
BDOS_CLOSE_FILE: EQU 16
; Simple macro to push all (important) registers.
;
MACRO PUSH_ALL
push af
push bc
push de
push hl
ENDM
;
; Simple macro to pop all (important) registers.
;
MACRO POP_ALL
pop hl
pop de
pop bc
pop af
ENDM
; }}
ORG 0x0100
;; The FCB will be populated with the pattern/first argument,
;; if the first character of that region is a space-character
;; then we've got nothing to search for.
ld a, (FCB1 + 1)
cp 0x20 ; 0x20 = 32 == SPACE
jp nz, got_argument ; Not a space, so we can proceed
;;
;; No argument, so show the error and exit
;;
ld de, usage_message
ld c, BDOS_OUTPUT_STRING
call BDOS_ENTRY_POINT
exit_fn:
LD C,0x00
CALL BDOS_ENTRY_POINT
got_argument:
;; Open the file
LD DE, FCB1
LD C, BDOS_OPEN_FILE
CALL BDOS_ENTRY_POINT
;; Did that succeed?
cp 00
jr z, open_ok
LD DE, OPEN_FAILED
ld c, BDOS_OUTPUT_STRING
call BDOS_ENTRY_POINT
jr exit_fn
open_ok:
;; Right here we loop from 0x00 - 0xFF starting at 0x00
ld a, 0x00
read_record:
PUSH AF
; read a record
LD C, BDOS_READ_FILE
LD DE, FCB1
CALL BDOS_ENTRY_POINT
POP AF
; Does this record contain the current record number?
LD HL, DMA
LD b,(HL)
CP B
JR NZ, RECORD_FAILED
INC A
cp 0
jr nz, read_record
;; Close the file
LD DE,FCB1
LD C, BDOS_CLOSE_FILE
CALL BDOS_ENTRY_POINT
;; Exit
jr exit_fn
RECORD_FAILED:
push af
ld de, FAILURE
ld c, BDOS_OUTPUT_STRING
call BDOS_ENTRY_POINT
pop af
call show_a_register
ld c, 0x02
ld e, 0x0a
call 0x0005
ld c, 0x02
ld e, 0x0d
call 0x0005
; now dump the memory
ld b, 128
ld hl, DMA
show_mem:
push bc
ld a,(hl)
call show_a_register
ld c, 0x02
ld e, " "
call 0x0005
pop bc
djnz show_mem
jr exit_fn
DispHL:
ld c,-10
call Num1
ld c,-1
Num1: ld a,'0'-1
Num2: inc a
add hl,bc
jr c,Num2
sbc hl,bc
PUSH_ALL
ld e, a
ld c, 0x02
call 0x0005
POP_ALL
ret
show_a_register:
ld h,0
ld l,a
call DispHL
ret
usage_message:
db "Usage: READ FILENAME.EXT"
;; note fall-through here :)
newline:
db 0xa, 0xd, "$"
FAILURE:
db "Unexpected value reading file at record $"
OPEN_FAILED:
db "opening the file failed.", 0x0a, 0x0d, "$"
RECORD:
DB 0x00
END