1706
|
1 *************************************************************************
|
|
2 *
|
|
3 * Buffered input routines
|
|
4 *
|
|
5 * I_Open - Opens file. X -> filename, A has permissions
|
|
6 * I_Read - Read Y bytes from file into [X]
|
|
7 * I_GetByte - Returns one byte from file in A.
|
|
8 * I_Close - Closes file.
|
|
9 * I_Size - returns file size, truncated to $FFFF
|
|
10 *
|
|
11 * Terminal output routines
|
|
12 *
|
|
13 * O_Write - Uses I$WritLn to output null-terminated string.
|
|
14 *
|
|
15 * Note: as with OS9 I$ system calls, all calls return Carry clear if
|
|
16 * no error. If there is an error, Carry is set, and the error code is in B
|
|
17 *
|
|
18 * Global var:
|
|
19 * FilePos: 2-byte total number of bytes returned by I_Read or I_GetByte
|
|
20 *
|
|
21 *************************************************************************
|
|
22
|
|
23 ifp1
|
1777
|
24 use os9defs.a
|
1706
|
25 endc
|
|
26
|
|
27 StdIn equ 0
|
|
28 StdOut equ 1
|
|
29 StdErr equ 2
|
|
30
|
|
31 psect View_IO_a,0,0,0,0,0
|
|
32
|
|
33
|
|
34 vsect dp
|
|
35 FilePos: rmb 2 Current File position
|
|
36 Buffsize rmb 2 Size of buffer area
|
|
37 Buffer rmb 2 Start of buffer
|
|
38 Buffstart rmb 2 Start of data in buffer
|
|
39 Buffend rmb 2 End of data in buffer
|
|
40 Path rmb 1 Path number of file
|
|
41 errorno rmb 1 Last error number.
|
|
42 endsect
|
|
43
|
|
44 *
|
|
45 * Open file, initialize buffering
|
|
46 *
|
|
47 I_Open:
|
|
48 pshs x,a,b
|
|
49 clr errorno
|
|
50 ldd <MemSiz Use as much memory as is free.
|
|
51 cmpd <MaxBuff But in no case use more than MaxBuff space.
|
|
52 blo IO1
|
|
53 ldd <MaxBuff
|
|
54 IO1
|
|
55 std Buffsize
|
|
56 ldx <MemTop Buffer starts at MemTop
|
|
57 stx Buffer
|
|
58 stx Buffend
|
|
59 stx Buffstart
|
|
60 leax d,x
|
|
61 stx <MemTop Move MemTop up
|
|
62 pshs d
|
|
63 ldd <MemSiz Decrease MemSiz accordingly
|
|
64 subd ,s++
|
|
65 std <MemSiz
|
|
66 puls x,a,b
|
|
67 cmpx #0000
|
|
68 bne IO2
|
|
69 lda #StdIn
|
|
70 bra IO3
|
|
71 IO2
|
|
72 os9 I$Open
|
|
73 bcs IOexit
|
|
74 IO3
|
|
75 sta Path
|
|
76 IOexit
|
|
77 rts
|
|
78
|
|
79 *
|
|
80 * Return Y bytes from input file, to [X]
|
|
81 *
|
|
82 I_Read:
|
|
83 cmpy #0
|
|
84 beq _I_R_quit
|
|
85 tst errorno If there's an error from the last operation,
|
|
86 beq _I_Read return that as an error code.
|
|
87 ldb errorno
|
|
88 orcc #Carry Set carry.
|
|
89 _I_R_quit
|
|
90 rts
|
|
91
|
|
92 _I_Read
|
|
93 pshs a,b,x,u
|
|
94 pshs x Extra copy of initial pointer, for final calculation
|
|
95 IR_Again
|
|
96 ldu Buffstart Get start of data
|
|
97 IR_Loop
|
|
98 cmpu Buffend Is this the end?
|
|
99 bne IR_go
|
|
100 bsr fillBuff Yes, fill buffer from file
|
|
101 bcc IR_Again
|
|
102 stb errorno store error if any
|
|
103 andcc #^Carry clear carry
|
|
104 bra IR_exit return
|
|
105 IR_go
|
|
106 lda ,u+ move one byte
|
|
107 sta ,x+
|
|
108 leay -1,y Does this fill the request?
|
|
109 bne IR_Loop No, keep going.
|
|
110 andcc #^Carry
|
|
111 IR_exit
|
|
112 pshs cc Save condition codes, especially Carry
|
|
113 stu Buffstart Save new start of buffer
|
|
114 tfr x,d Calculate number of bytes copied.
|
|
115 subd 1,s
|
|
116 tfr d,y Y has number of bytes copied
|
|
117 addd FilePos Update FilePos counter.
|
|
118 std FilePos
|
|
119 puls cc Restore CC
|
|
120 puls d Clean up stack.
|
|
121 puls a,b,x,u,pc
|
|
122
|
|
123
|
|
124 *
|
|
125 * Return a single byte from input file, return in A
|
|
126 *
|
|
127 I_GetByte:
|
|
128 tst errorno If there's an error from the last operation,
|
|
129 beq _I_GetByte return that as an error code.
|
|
130 ldb errorno
|
|
131 orcc #Carry Set carry.
|
|
132 rts
|
|
133 _I_GetByte
|
|
134 pshs x,b
|
|
135 ldx Buffstart Get start of data
|
|
136 cmpx Buffend Is this the end?
|
|
137 bne IG_go
|
|
138 bsr fillBuff Yes, fill buffer from file
|
|
139 bcc IG_go
|
|
140 stb errorno store error if any
|
|
141 andcc #^Carry clear carry
|
|
142 bra IG_exit return
|
|
143 IG_go
|
|
144 ldx Buffstart
|
|
145 lda ,x+ move one byte
|
|
146 stx Buffstart
|
|
147 ldx FilePos
|
|
148 leax 1,x
|
|
149 stx FilePos
|
|
150 andcc #^Carry
|
|
151 IG_exit
|
|
152 puls b,x,pc
|
|
153
|
|
154 fillBuff
|
|
155 pshs a,x,y
|
|
156 lda Path
|
|
157 ldy Buffsize Maximum number of characters we can buffer
|
|
158 ldx Buffer Location of buffer
|
|
159 stx Buffstart First char in buffer
|
|
160 os9 I$Read
|
|
161 bcs fillexit
|
|
162 pshs b
|
|
163 tfr y,d
|
|
164 leax d,x
|
|
165 stx Buffend Set end of buffer
|
|
166 puls b
|
|
167 andcc #^Carry
|
|
168 fillexit
|
|
169 puls a,x,y,pc
|
|
170
|
|
171 *
|
|
172 * Close input file
|
|
173 *
|
|
174 I_Close:
|
|
175 pshs a
|
|
176 lda Path
|
|
177 os9 I$Close
|
|
178 puls a,pc
|
|
179
|
|
180 *
|
|
181 * Return size of file in D (truncated to $ffff)
|
|
182 *
|
|
183 I_Size:
|
|
184 pshs x,u
|
|
185 lda Path
|
|
186 ldb #SS.Size
|
|
187 os9 I$GetStt
|
|
188 bcs ISexit
|
|
189 tfr u,d
|
|
190 cmpx #0000 Is top 16 bits = 0000 ??
|
|
191 beq ISexit
|
|
192 ldd #$ffff No, return file size = $ffff
|
|
193 ISexit
|
|
194 puls x,u,pc
|
|
195
|
|
196 *
|
|
197 * Input: X points to null-terminated string, possibly containing embedded
|
|
198 * CR characters.
|
|
199 * String is output to StdOut, with full line editing a la I$WritLn
|
|
200 *
|
|
201 O_Write:
|
|
202 pshs a,x,y
|
|
203 pshs x
|
|
204 ldy #0
|
|
205 lda ,x+
|
|
206 beq OWexit
|
|
207 OWloop
|
|
208 leay 1,y
|
|
209 lda ,x+
|
|
210 bne OWloop
|
|
211 ldx ,s
|
|
212
|
|
213 OWloop1
|
|
214 sty ,s
|
|
215 lda #StdOut
|
|
216 os9 I$WritLn
|
|
217 bcs OWexit
|
|
218 pshs y
|
|
219 ldd ,s
|
|
220 leax d,x
|
|
221 ldd 2,s
|
|
222 subd ,s++
|
|
223 tfr d,y
|
|
224 bne OWloop1
|
|
225
|
|
226 OWokay
|
|
227 andcc #^Carry
|
|
228 OWexit
|
|
229 puls x
|
|
230 puls a,x,y,pc
|
|
231
|
|
232 endsect
|