Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [basext/] [commands.asm] - Blame information for rev 380

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 221 winston
;The MIT License
2
;
3
;Copyright (c) 2009 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22
 
23
; TNFS BASIC extensions
24 380 winston
.include        "zxrom.inc"
25
.include        "spectranet.inc"
26
.include        "defs.inc"
27
.include        "fcntl.inc"
28
.include        "sysvars.inc"
29
.include        "zxsysvars.inc"
30
.text
31 221 winston
;---------------------------------------------------------------------------
32
; F_tbas_mount
33
; BASIC interpreter for "mount"
34 233 winston
; Syntax: %mount mountpoint, "url"
35 380 winston
.globl F_tbas_mount
36
F_tbas_mount:
37 221 winston
        ; Syntax and runtime
38
        rst CALLBAS
39 233 winston
        defw ZX_EXPT1_NUM
40 221 winston
        cp ','                          ; comma
41
        jp nz, PARSE_ERROR
42
        rst CALLBAS
43
        defw ZX_NEXT_CHAR
44
 
45
        rst CALLBAS
46 238 winston
        defw ZX_EXPT_EXP                ; string parameter - an URL
47 221 winston
 
48
        call STATEMENT_END              ; followed by statement end
49
 
50
        ; -------- Runtime only ---------
51 233 winston
        ld hl, INTERPWKSPC              ; clear space for the
52
        ld de, INTERPWKSPC+1            ; mount argument structure
53
        ld bc, 9
54
        ld (hl), 0
55 221 winston
        ldir
56
 
57
        rst CALLBAS
58
        defw ZX_STK_FETCH               ; path string
59 233 winston
        ld hl, INTERPWKSPC+10
60 221 winston
        call F_basstrcpy                ; copy string from BASIC
61 233 winston
        ld ix, INTERPWKSPC              ; where to place the mount struct
62
        ld hl, INTERPWKSPC+10           ; location of the string to parse
63
        call F_parseurl
64 380 winston
        jr c, .badurl1
65 233 winston
 
66
        rst CALLBAS                     ; fetch the mount point
67
        defw ZX_FIND_INT2
68 221 winston
 
69 380 winston
.mount1:
70 233 winston
        ld a, c                         ; mount point in BC
71 221 winston
        call MOUNT
72
        jp c, J_tbas_error              ; display the error message
73
 
74
        jp EXIT_SUCCESS
75
 
76 380 winston
.badurl1:
77 233 winston
        ld a, EBADURL
78
        jp J_tbas_error
79
 
80 221 winston
        ; Copy a BASIC string to a C string.
81
        ; BASIC string in DE, C string (dest) in HL
82 380 winston
.globl F_basstrcpy
83
F_basstrcpy:
84 221 winston
        ld a, b                         ; end of string?
85
        or c
86 380 winston
        jr z, .terminate2
87 221 winston
        ld a, (de)
88
        ld (hl), a
89
        inc hl
90
        inc de
91
        dec bc
92
        jr F_basstrcpy
93 380 winston
.terminate2:
94 221 winston
        xor a                           ; put the null on the end
95
        ld (hl), a
96
        inc hl
97
        ret
98
 
99
;----------------------------------------------------------------------------
100
; F_tbas_umount
101
; Umount the mounted filesystem.
102 380 winston
.globl F_tbas_umount
103
F_tbas_umount:
104 238 winston
        rst CALLBAS
105
        defw ZX_EXPT1_NUM               ; 1 parameter - mount point number
106
        call STATEMENT_END
107 221 winston
 
108
        ;--------- runtime ---------
109 238 winston
        rst CALLBAS
110
        defw ZX_FIND_INT2
111
        ld a, c                         ; mount point is in BC
112 221 winston
        call UMOUNT
113
        jp c, J_tbas_error
114
        jp EXIT_SUCCESS
115
 
116
;----------------------------------------------------------------------------
117
; F_tbas_chdir
118
; Handle changing directory
119 380 winston
.globl F_tbas_chdir
120
F_tbas_chdir:
121 221 winston
        rst CALLBAS
122
        defw ZX_EXPT_EXP                ; expect a string expression
123
        call STATEMENT_END
124
 
125
        ;-------- runtime --------
126
        rst CALLBAS
127
        defw ZX_STK_FETCH               ; get the string
128
        ld hl, INTERPWKSPC
129
        call F_basstrcpy                ; convert to a C string
130
        ld hl, INTERPWKSPC
131
        call CHDIR
132
        jp c, J_tbas_error              ; carry set = error
133
        jp EXIT_SUCCESS
134
 
135
;---------------------------------------------------------------------------
136
; F_tbas_aload: Loads an arbitary file from the TNFS filesystem.
137
; The syntax is %aload "filename" CODE address. This allows the user
138
; to load an arbitrary file with no ZX formatting into memory.
139 380 winston
.globl F_tbas_aload
140
F_tbas_aload:
141 221 winston
        rst CALLBAS
142
        defw ZX_EXPT_EXP                ; expect a string expression
143
        cp TOKEN_CODE                   ; expect CODE
144
        jp nz, PARSE_ERROR
145
        rst CALLBAS                     ; fetch the next bit
146
        defw ZX_NEXT_CHAR               ; which must be a number
147
        rst CALLBAS
148
        defw ZX_EXPT1_NUM
149
        call STATEMENT_END              ; and then the end of statement
150
 
151
        ;------- runtime -------
152
        rst CALLBAS
153
        defw ZX_FIND_INT2               ; find the 16 bit int
154
        push bc                         ; save it
155
        rst CALLBAS
156
        defw ZX_STK_FETCH               ; get the filename
157
        ld hl, INTERPWKSPC              ; save it in workspace...
158
        call F_basstrcpy
159
 
160
        ; Now read the file into memory
161
        ld hl, INTERPWKSPC
162
        pop de                          ; retrieve address
163
        call F_tbas_readrawfile
164
        jp c, J_tbas_error
165
        jp EXIT_SUCCESS
166
 
167
;----------------------------------------------------------------------------
168
; F_tbas_load: Loads a ZX file (BASIC, CODE etc.)
169
; The syntax is as for ZX BASIC LOAD, except %load.
170
; TODO: CODE et al.
171 380 winston
.globl F_tbas_load
172
F_tbas_load:
173 221 winston
        rst CALLBAS
174
        defw ZX_EXPT_EXP                ; expect a string expression
175
        cp TOKEN_CODE                   ; Check for CODE...
176 380 winston
        jr z, .loadcode6
177 221 winston
        call STATEMENT_END              ; If not, statment end for BASIC.
178
 
179
        ;------ runtime for BASIC ------
180
        xor a                           ; type 0x00 is BASIC
181 380 winston
.loader6:
182 221 winston
        push af                         ; save type
183
        rst CALLBAS
184
        defw ZX_STK_FETCH               ; fetch the filename
185 332 winston
        ld a, b                         ; check for empty string
186
        or c
187 380 winston
        jr nz, .havefilename6
188 332 winston
        ld hl, STR_BOOTDOTZX
189
        ld de, INTERPWKSPC
190
        ld bc, STR_BOOTDOTZXLEN
191
        ldir
192 380 winston
        jr .loadbasic6
193
.havefilename6:
194 221 winston
        ld hl, INTERPWKSPC
195
        call F_basstrcpy                ; copy + convert to C string
196 380 winston
.loadbasic6:
197 221 winston
        ; Now call the loader routine with the filename in HL
198
        ld hl, INTERPWKSPC
199
        pop af                          ; get type id
200
        call F_tbas_loader
201
        jp c, J_tbas_error
202
        jp EXIT_SUCCESS
203
 
204 380 winston
.loadcode6:
205 221 winston
        ; TODO - code to a specific address.
206
        rst CALLBAS
207
        defw ZX_NEXT_CHAR
208
        call STATEMENT_END
209
 
210
        ;------ runtime for CODE with no addr -------
211
        ld a, 3                         ; type=CODE
212 380 winston
        jr .loader6                     ; get the filename then load.
213 221 winston
 
214
;----------------------------------------------------------------------------
215
; F_tbas_save: Save a ZX file (BASIC, CODE etc.)
216
; The syntax is as for ZX BASIC SAVE.
217
; TODO: CODE et al.
218 380 winston
.globl F_tbas_save
219
F_tbas_save:
220 221 winston
        rst CALLBAS
221
        defw ZX_EXPT_EXP                ; fetch the file name
222
        cp TOKEN_CODE                   ; for CODE
223 380 winston
        jr z, .savecode7
224 221 winston
        cp TOKEN_SCREEN                 ; for SCREEN$
225 380 winston
        jr z, .savescreen7
226 248 winston
        cp TOKEN_LINE                   ; then for LINE
227 380 winston
        jr z, .savebasline7
228 221 winston
 
229
        call STATEMENT_END              ; a basic BASIC save.
230
 
231
        ;------- runtime for simple BASIC save -------
232 250 winston
        rst CALLBAS
233
        defw ZX_STK_FETCH               ; Fetch the file name
234
        push de                         ; save the filename
235
        push bc
236 248 winston
        xor a
237
        call F_tbas_mktapheader
238
        ld hl, 0xFFFF                   ; set param1 to >32767
239
        ld (INTERPWKSPC+OFFSET_PARAM1), hl
240 380 winston
        jr .makebasicblock7
241 221 winston
 
242
        ; Deal with SAVE "filename" CODE
243 380 winston
.savecode7:
244 221 winston
        rst CALLBAS
245
        defw ZX_NEXT2_NUM               ; check for 2 numbers
246
        call STATEMENT_END              ; then end of command.
247
 
248
        ; Runtime
249
        rst CALLBAS
250
        defw ZX_FIND_INT2               ; Get the length
251
        push bc                         ; into BC and save it
252
        rst CALLBAS
253
        defw ZX_FIND_INT2               ; and the start
254
        push bc                         ; and save that, too
255 380 winston
.savecodemain7:
256 221 winston
        rst CALLBAS
257
        defw ZX_STK_FETCH               ; and get the filename
258
        ld a, 3                         ; type = 3 - CODE
259
        call F_tbas_mktapheader         ; create the header template
260
        pop hl                          ; retrieve the start address
261
        ld (INTERPWKSPC+OFFSET_PARAM1), hl      ; and put it in the header
262
        pop hl                          ; and the length
263
        ld (INTERPWKSPC+OFFSET_LENGTH), hl      ; and put it in the header
264
        call F_tbas_writefile           ; finally write it out
265
        jp c, J_tbas_error
266
        jp EXIT_SUCCESS
267
 
268 380 winston
.savescreen7:
269 221 winston
        rst CALLBAS
270
        defw ZX_NEXT_CHAR               ; advance to the end of the line
271
        call STATEMENT_END
272
 
273
        ; Runtime
274
        ld hl, 6912                     ; Put the length of a SCREEN$
275
        push hl                         ; on the stack
276
        ld hl, 16384                    ; followed by the start address
277
        push hl
278 380 winston
        jr .savecodemain7               ; and do as for CODE
279 248 winston
 
280 380 winston
.savebasline7:
281 248 winston
        rst CALLBAS
282
        defw ZX_NEXT_CHAR
283
        rst CALLBAS
284
        defw ZX_EXPT1_NUM               ; 1 number - the line number
285
        call STATEMENT_END
286 221 winston
 
287 248 winston
        ; Runtime for save "x" LINE y
288
        rst CALLBAS
289
        defw ZX_FIND_INT2               ; Fetch the number
290 250 winston
        ld (v_bcsave), bc
291 248 winston
        rst CALLBAS
292
        defw ZX_STK_FETCH               ; Fetch the file name
293 250 winston
        push de
294
        push bc
295
        xor a                           ; type = 0
296
        call F_tbas_mktapheader         ; Create the header
297
        ld hl, (v_bcsave)               ; get LINE parameter
298
        ld (INTERPWKSPC+OFFSET_PARAM1), hl ; Put it into parameter 1
299 380 winston
.makebasicblock7:
300 248 winston
 
301
        ; Fill in the header, length and length without vars
302
        ld hl, (ZX_E_LINE)              ; get the length of the BASIC prog
303
        ld de, (ZX_PROG)                ; by calculating it
304
        scf
305
        sbc hl, de
306
        ld (INTERPWKSPC+OFFSET_LENGTH), hl      ; prog + vars
307
        ld hl, (ZX_VARS)                ; now save the length - vars
308
        sbc hl, de                      ; calculate it...
309
        ld (INTERPWKSPC+OFFSET_PARAM2), hl
310 250 winston
        pop bc                          ; retrieve filename
311 248 winston
        pop de
312
        call F_tbas_writefile           ; Write it out.
313
        jp c, J_tbas_error
314
        jp EXIT_SUCCESS
315
 
316 221 winston
;----------------------------------------------------------------------------
317
; F_tbas_ls
318
; List a directory
319
; Two forms - either %cat or %cat "directory"
320 380 winston
.globl F_tbas_ls
321
F_tbas_ls:
322 221 winston
        cp 0x0d
323 380 winston
        jr z, .noargs8
324 221 winston
        cp ':'
325 380 winston
        jr z, .noargs8
326 221 winston
 
327
        ; we have an argument supplied.
328
        rst CALLBAS
329
        defw ZX_EXPT_EXP                ; expect a string
330
        call STATEMENT_END
331
 
332
        ; --------- runtime ----------
333
        rst CALLBAS
334
        defw ZX_STK_FETCH               ; get the directory arg
335
        ld hl, INTERPWKSPC
336
        call F_basstrcpy                ; convert it to a C string
337
        ld hl, INTERPWKSPC
338 314 winston
        jp F_listdir
339 221 winston
 
340 380 winston
.noargs8:
341 221 winston
        call STATEMENT_END
342
 
343
        ; --------- runtime ----------
344
        ld a, '.'
345
        ld (INTERPWKSPC), a             ; default directory is CWD
346
        xor a
347
        ld (INTERPWKSPC+1), a
348
        ld hl, INTERPWKSPC
349 380 winston
.makecat8:
350 314 winston
        jp F_listdir
351 221 winston
 
352 222 winston
;---------------------------------------------------------------------------
353
; F_tbas_tapein
354
; Handle the %tapein command, which takes a filename as a parameter.
355 380 winston
.globl F_tbas_tapein
356
F_tbas_tapein:
357 222 winston
        rst CALLBAS
358
        defw ZX_EXPT_EXP                ; expect a string expression
359
        call STATEMENT_END
360
 
361
        ;-------- runtime --------
362
        rst CALLBAS
363
        defw ZX_STK_FETCH               ; get the string
364
        call F_settrap
365
        jp c, J_tbas_error              ; carry set = error
366
        jp EXIT_SUCCESS
367
 
368 221 winston
;----------------------------------------------------------------------------
369 225 winston
; F_tbas_info
370
; Handle the %info command
371 380 winston
.globl F_tbas_info
372
F_tbas_info:
373 225 winston
        rst CALLBAS
374
        defw ZX_EXPT_EXP                ; expect a string
375
        call STATEMENT_END
376
 
377
        ;-------- runtime ----------
378
        rst CALLBAS
379
        defw ZX_STK_FETCH
380
        call F_showfileinfo             ; Try to open the file and show
381
        jp c, J_tbas_error              ; the information.
382
        jp EXIT_SUCCESS
383 238 winston
 
384
;----------------------------------------------------------------------------
385
; F_tbas_fs
386
; Sets the current filesystem.
387 380 winston
.globl F_tbas_fs
388
F_tbas_fs:
389 238 winston
        rst CALLBAS
390
        defw ZX_EXPT1_NUM               ; expect one number - the FS number
391
        call STATEMENT_END
392
 
393
        ;-------- runtime ----------
394
        rst CALLBAS
395
        defw ZX_FIND_INT2               ; get the fs number
396
        ld a, c                         ; get it from BC
397
        call SETMOUNTPOINT
398
        jp nc, EXIT_SUCCESS             ; No carry = FS change OK
399
        ld a, EBADFS
400
        jp J_tbas_error
401 255 winston
 
402
;----------------------------------------------------------------------------
403
; F_loadsnap
404
; Loads a snapshot file.
405 380 winston
.globl F_loadsnap
406
F_loadsnap:
407 255 winston
        rst CALLBAS
408
        defw ZX_EXPT_EXP                ; filename string
409
        call STATEMENT_END
410
 
411
        ;---------- runtime -----------
412
        rst CALLBAS
413
        defw ZX_STK_FETCH               ; get the filename
414 262 winston
        ld hl, INTERPWKSPC+256
415 255 winston
        call F_basstrcpy                ; copy filename as a C string
416 284 winston
        ld de, INTERPWKSPC+256
417
        ld hl, 0xFB01                   ; Module ID = 0xFB, call ID = 0x01
418
        rst MODULECALL_NOPAGE
419 255 winston
        jp J_tbas_error                 ; If we get here, an error occurred
420 288 winston
 
421 225 winston
;----------------------------------------------------------------------------
422 288 winston
; F_tbas_mv
423
; Moves (renames) a file.
424 380 winston
.globl F_tbas_mv
425
F_tbas_mv:
426 288 winston
        rst CALLBAS
427
        defw ZX_EXPT_EXP                ; source filename
428
        cp ','                          ; and a comma
429
        jp nz, PARSE_ERROR
430
        rst CALLBAS
431
        defw ZX_NEXT_CHAR               ; advance past ,
432
 
433
        rst CALLBAS
434
        defw ZX_EXPT_EXP                ; destination filename
435
        call STATEMENT_END              ; then the end of statement
436
 
437
        ;------- runtime ---------
438
        rst CALLBAS
439
        defw ZX_STK_FETCH               ; destination filename
440
        ld hl, INTERPWKSPC+256
441
        call F_basstrcpy                ; copy to workspace as C string
442
        rst CALLBAS
443
        defw ZX_STK_FETCH               ; source filename
444
        ld hl, INTERPWKSPC
445
        call F_basstrcpy                ; copy to workspace
446
        ld hl, INTERPWKSPC              ; source and dest filename pointers
447
        ld de, INTERPWKSPC+256
448
        call RENAME
449
        jp nc, EXIT_SUCCESS
450
        jp J_tbas_error
451
 
452
;---------------------------------------------------------------------------
453
; F_tbas_rm: Removes a file
454 380 winston
.globl F_tbas_rm
455
F_tbas_rm:
456 288 winston
        rst CALLBAS
457
        defw ZX_EXPT_EXP                ; file to remove
458
        call STATEMENT_END
459
 
460
        ;-------- runtime ---------
461
        rst CALLBAS
462
        defw ZX_STK_FETCH
463
        ld hl, INTERPWKSPC
464
        call F_basstrcpy
465
        ld hl, INTERPWKSPC
466
        call UNLINK                     ; remove the file
467
        jp nc, EXIT_SUCCESS
468
        jp J_tbas_error
469
 
470
;----------------------------------------------------------------------------
471 221 winston
; F_tbas_zxprint
472
; Prints a C string to the current ZX channel
473
; HL = pointer to string
474 380 winston
.globl F_tbas_zxprint
475
F_tbas_zxprint:
476 221 winston
        ld a, (hl)
477
        and a
478
        ret z
479
        rst CALLBAS
480
        defw 0x10
481
        inc hl
482
        jr F_tbas_zxprint
483
 
484
;----------------------------------------------------------------------------
485
; handle errors and return control to BASIC.
486
; A=tnfs error number
487 380 winston
.globl J_tbas_error
488
J_tbas_error:
489 221 winston
        push af                         ; save error number
490
        call F_geterrstr                ; get the error string
491
        pop af
492
        jp REPORTERR                    ; exit to BASIC with error string
493
 
494
;----------------------------------------------------------------------------
495
; F_tnfs_geterrstr
496
; Enter with A=error number
497
; Exits with HL=pointer to null terminated error string
498 380 winston
.globl F_geterrstr
499
F_geterrstr:
500 255 winston
        ld h, 0xFC              ; ID of messages module
501
        ld l, a                 ; Set the message ID to fetch
502
        ld de, 0x3000           ; buffer
503
        xor a
504
        ld (de), a              ; initialize with a NULL
505
        rst MODULECALL_NOPAGE
506
        ld hl, 0x3000           ; address of the message
507
        ret
508 380 winston
.data
509
.globl STR_BOOTDOTZX
510
.globl STR_BOOTDOTZXLEN
511
STR_proto:      defb    "tnfs",0
512
STR_BOOTDOTZX:  defb    "boot.zx",0
513
STR_BOOTDOTZXEND:
514 221 winston
 
515 380 winston
STR_BOOTDOTZXLEN equ    STR_BOOTDOTZXEND-STR_BOOTDOTZX
516 221 winston
 
517 380 winston
EBADURL:                equ     0x28
518
EBADFS:         equ     0x29
519 238 winston