]> git.defcon.no Git - z80cpm/blob - rf1.asm
An experiment in reading files on CP/M
[z80cpm] / rf1.asm
1 ; rf1.asm - Read File experiment #1
2 ; ASM dialect is for crasm/z80, producing Intel HEX.
3 ;
4 ; Attempting to understand file read operations on CP/M2
5 ; Reference material:
6 ; http://www.cpm.z80.de/manuals/archive/cpm22htm/ch5.htm
7 ; The default FCB can be used, it's location is at 05CH
8 ; The default DMA buffer can be used, it's located at 080H
9 ; When CCP jumps to 0100H, it's already taken the two
10 ; first parameters to the command, and placed them in
11 ; the default FCB area. More on this later in the code
12 ; CCP has also placed the parameter string in the default
13 ; DMA buffer area (080H). Not used in this experiment.
14
15 ; crasm macros.. Z80 CPU type, iHEX output, readable listing pages, listing-toggles..
16 CPU Z80
17 OUTPUT HEX
18 PAGE 24,120
19 LIST ON
20 CLIST ON
21 ILIST ON
22 MLIST ON
23
24 ; BDOS call constants..
25 conout EQU 2 ; Console output, put 1 char from E to Console
26 strout EQU 9 ; String output, place string at (DE) terminated by $ to Console
27 openf EQU 15 ; Open file, using the FCB at address (DE)
28 reads EQU 20 ; Read sequential, get next DMABUF from file at FCB in (DE)
29 closef EQU 16 ; Close file, using hte FCB at (DE)
30 bdos EQU 5 ; Call jump-address for all BDOS calls.
31
32 ; Standard location constants..
33 sfcb EQU 0x5c ; The Standard/default FCB
34 fcbcr EQU sfcb+32 ; The CR field of the FCB
35 sdma EQU 0x80 ; The Standard/default DMA Buffer
36
37 ; Other fun constants...
38 eof EQU 0x1a ; CP/M EOF character, 0x1Ah/26d
39
40 ; Origin at TPA Start, 0x100
41 * = 0x100
42
43 ; Start by printing out the filename currently in the default FCB.
44 ; CCP will take 'command file.sfx file1.sfx" and fill the default
45 ; FCB appropriately for file.sfx, plus the needed content for a
46 ; second FCB started at 0x6c (default fcb + 10h)
47 ; The Drive index is the first byte, and the filename is 8+3 bytes
48 LD HL, sfcb + 1 ; Load HL with address of Standard FCB + 1
49 LD D, 8+3 ; Prepare a counter for 8+3 characters for a full filename
50 START LD E, (HL) ; Load content of address in HL into E
51 LD C, conout ; Prepare a Console Character Out BDOS call
52 PUSH HL ; BDOS call clobbers HL, push to stack
53 CALL bdos ; Go to BDOS for conout call
54 POP HL ; Restore HL after BDOS call
55 INC HL ; Next byte
56 DEC D ; .. until all needed characters..
57 JR NZ, START
58
59 ; --- START OF FILE OPEN
60 ; To open file for sequential access, the 'cr' field of the FCB needs
61 ; to be zeroed
62 LD HL, fcbcr ; Set the HL register pair to the CR field address
63 LD (HL), 0 ; Set to zero
64
65 LD DE, sfcb ; Prepare DE with address of FCB
66 LD C, openf ; Set up for Open File BDOS call
67 CALL bdos ; GO!
68
69 CP 255 ; Compare A register to check for error
70 JR Z, ERROPF ; If Zero set, A == 255, and we have an error
71 ; --- END OF FILE OPEN
72
73 LD DE, okmsg ; Compare did not set Zero flag, so we can
74 LD C, strout ; use String Output to say that the file is opened
75 CALL bdos
76
77 ; --- ACTUAL DATA REA
78 RREC LD DE, sfcb ; Load DE with address of FCB
79 LD C, reads ; and perform a sequential file read
80 CALL bdos ; BDOS will look at the FCB and retrieve the appropriate block
81
82 CP 0 ; Check for errors on read. If we see anything other than
83 JR NZ, ERRRDD ; a zero, we have something wonky on the read.
84 ; --- DMA buffer now contains 128 bytes from file
85
86 ; --- Fluff of code to read through file
87 LD D, 128 ; Prepare for counting through 128 bytes record...
88 LD HL, sdma ; Using the HL as a pointer to positions on DMA buffer
89 PRTBYTE LD E, (HL) ; Load content of address in HL into E
90
91 ; This bit works for text, will be wonky if the file is binary
92 ; But, it seems that if a record is not full, it's supposed to get
93 ; filled with EOF characters until end...
94 LD A, eof ; Check for EOF character
95 CP E ; If cound, we're at the end of file
96 JR Z, DONE ; .. so jump to done
97
98 LD C, conout ; Prepare a Console Character Out BDOS call
99 PUSH HL ; BDOS call clobbers HL, push to stack
100 CALL bdos ; Go to BDOS for conout call
101 POP HL ; Restore HL after BDOS call
102 INC HL ; Next byte
103 DEC D ; .. until all needed characters..
104 JR NZ, PRTBYTE
105
106 ; If we've gotten here, we have completed one 128 byte set, and
107 ; haven't seen any EOF characters yet. So, try to get a new one.
108 JP RREC ; Jump to read next record from file.
109
110
111 DONE LD DE, donemsg ; Do a quick status at the end of completion...
112 LD C, strout
113 CALL bdos
114 JP 0 ; Do a jump to BOOT, causing a boot to CCP
115
116 ERROPF LD DE, erropn
117 JP ERROR
118 ERRRDD LD DE, errrd
119 JP ERROR
120
121 ERROR LD C, strout
122 CALL bdos
123 JP 0 ; Do a jump to BOOT, causing a boot to CCP
124
125 erropn ASC "\r\n---> ERROR opening file\r\n$"
126 errrd ASC "\r\n---> ERROR reading file\r\n$"
127 okmsg ASC "\r\n---> Opened file\r\n$"
128 donemsg ASC "\r\n---> Completed reading file.\r\n$"
129