|
| 1 | +# vim: ts=4 sw=4 et |
| 2 | + |
| 3 | +record CpmFCB is |
| 4 | + dr: uint8; |
| 5 | + f: uint8[11]; |
| 6 | + ex: uint8; |
| 7 | + s1: uint8; |
| 8 | + s2: uint8; |
| 9 | + rc: uint8; |
| 10 | + d: uint32[4]; # aligns also CpmFCB to even address |
| 11 | + cr: uint8; |
| 12 | + r: uint8[3]; |
| 13 | +end record; |
| 14 | + |
| 15 | +record FCB is |
| 16 | + bufferptr: uint8; # byte just read |
| 17 | + dirty: uint8; |
| 18 | + cpm: CpmFCB; |
| 19 | + buffer: uint8[128]; |
| 20 | +end record; |
| 21 | + |
| 22 | +sub fcb_set_record(fcb: [FCB], recordno: uint32) is |
| 23 | + recordno := recordno & 0x3ffff; |
| 24 | + fcb.cpm.r[0] := ((recordno >> 16) & 0x3) as uint8; |
| 25 | + fcb.cpm.r[1] := ((recordno >> 8) & 0xff) as uint8; |
| 26 | + fcb.cpm.r[2] := (recordno & 0xff) as uint8; |
| 27 | +end sub; |
| 28 | + |
| 29 | +sub fcb_get_record(fcb: [FCB]): (recordno: uint32) is |
| 30 | + var r0 := (fcb.cpm.r[0] as uint32) << 16; |
| 31 | + var r1 := (fcb.cpm.r[1] as uint32) << 8; |
| 32 | + var r2 := fcb.cpm.r[2] as uint32; |
| 33 | + recordno := (r0 + r1 + r2) & 0x3ffff; |
| 34 | +end sub; |
| 35 | + |
| 36 | +sub file_i_init(fcb: [FCB], filename: [uint8]) is |
| 37 | + sub fill(dest: [uint8], src: [uint8], len: uint8): (srcout: [uint8]) is |
| 38 | + loop |
| 39 | + var c := [src]; |
| 40 | + if (c < 32) or (c == '.') then |
| 41 | + c := ' '; |
| 42 | + elseif (c == '*') then |
| 43 | + c := '?'; |
| 44 | + else |
| 45 | + src := src + 1; |
| 46 | + end if; |
| 47 | + if (c >= 'a') and (c <= 'z') then |
| 48 | + c := c - ('a' - 'A'); |
| 49 | + end if; |
| 50 | + [dest] := c; |
| 51 | + dest := dest + 1; |
| 52 | + |
| 53 | + len := len - 1; |
| 54 | + if len == 0 then |
| 55 | + break; |
| 56 | + end if; |
| 57 | + end loop; |
| 58 | + srcout := src; |
| 59 | + end sub; |
| 60 | + |
| 61 | + MemSet(fcb as [uint8], 0, @bytesof FCB); |
| 62 | + MemSet(&fcb.cpm.f[0] as [uint8], ' ', 11); |
| 63 | + filename := fill(&fcb.cpm.f[0], filename, 8); |
| 64 | + |
| 65 | + var c: uint8; |
| 66 | + loop |
| 67 | + c := [filename]; |
| 68 | + if (c < 32) or (c == '.') then |
| 69 | + break; |
| 70 | + end if; |
| 71 | + filename := filename + 1; |
| 72 | + end loop; |
| 73 | + |
| 74 | + if c == '.' then |
| 75 | + filename := fill(&fcb.cpm.f[8], filename+1, 3); |
| 76 | + end if; |
| 77 | + |
| 78 | + fcb_set_record(fcb, 0x3ffff); |
| 79 | + fcb.bufferptr := 127; |
| 80 | +end sub; |
| 81 | + |
| 82 | +sub fcb_i_gbpb(fcb: [FCB], c: uint16) is |
| 83 | + var cpmfcb := &fcb.cpm; |
| 84 | + var dma := &fcb.buffer[0]; |
| 85 | + |
| 86 | + @asm "move.w #26, %d0"; # BDOS set dma |
| 87 | + @asm "move.l (", dma, "), %d1"; |
| 88 | + @asm "trap #2"; |
| 89 | + |
| 90 | + @asm "move.w (", c, "), %d0"; # BDOS call defined in c |
| 91 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 92 | + @asm "trap #2"; |
| 93 | +end sub; |
| 94 | + |
| 95 | +sub fcb_i_blockin(fcb: [FCB]) is |
| 96 | + MemSet(&fcb.buffer[0], 0, 128); |
| 97 | + fcb_i_gbpb(fcb, 33); # READ RANDOM |
| 98 | + fcb.dirty := 0; |
| 99 | +end sub; |
| 100 | + |
| 101 | +sub fcb_i_blockout(fcb: [FCB]) is |
| 102 | + if fcb.dirty != 0 then |
| 103 | + fcb_i_gbpb(fcb, 34); # WRITE RANDOM |
| 104 | + fcb.dirty := 0; |
| 105 | + end if; |
| 106 | +end sub; |
| 107 | + |
| 108 | +sub fcb_i_changeblock(fcb: [FCB], newblock: uint32) is |
| 109 | + if newblock != fcb_get_record(fcb) then |
| 110 | + fcb_i_blockout(fcb); |
| 111 | + fcb_set_record(fcb, newblock); |
| 112 | + fcb_i_blockin(fcb); |
| 113 | + end if; |
| 114 | +end sub; |
| 115 | + |
| 116 | +sub fcb_convert_return_code_to_error(rc: uint8): (errno: uint8) is |
| 117 | + if rc == 0xff then |
| 118 | + errno := 1; |
| 119 | + else |
| 120 | + errno := 0; |
| 121 | + end if; |
| 122 | +end sub; |
| 123 | + |
| 124 | +sub FCBOpenIn(fcb: [FCB], filename: [uint8]): (errno: uint8) is |
| 125 | + file_i_init(fcb, filename); |
| 126 | + var cpmfcb := &fcb.cpm; |
| 127 | + |
| 128 | + @asm "move.w #15, %d0"; # BDOS open file |
| 129 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 130 | + @asm "trap #2"; |
| 131 | + @asm "move.b %d0, (", errno, ")"; |
| 132 | + errno := fcb_convert_return_code_to_error(errno); |
| 133 | +end sub; |
| 134 | + |
| 135 | +sub FCBOpenUp(fcb: [FCB], filename: [uint8]): (errno: uint8) is |
| 136 | + (errno) := FCBOpenIn(fcb, filename); |
| 137 | +end sub; |
| 138 | + |
| 139 | +sub FCBOpenOut(fcb: [FCB], filename: [uint8]): (errno: uint8) is |
| 140 | + file_i_init(fcb, filename); |
| 141 | + var cpmfcb := &fcb.cpm; |
| 142 | + |
| 143 | + @asm "move.w #19, %d0"; # BDOS delete file |
| 144 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 145 | + @asm "trap #2"; |
| 146 | + |
| 147 | + @asm "move.w #22, %d0"; # BDOS make file |
| 148 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 149 | + @asm "trap #2"; |
| 150 | + @asm "move.b %d0, (", errno, ")"; |
| 151 | + errno := fcb_convert_return_code_to_error(errno); |
| 152 | +end sub; |
| 153 | + |
| 154 | +sub FCBClose(fcb: [FCB]): (errno: uint8) is |
| 155 | + fcb_i_blockout(fcb); |
| 156 | + var cpmfcb := &fcb.cpm; |
| 157 | + |
| 158 | + @asm "move.w #16, %d0"; # BDOS close file |
| 159 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 160 | + @asm "trap #2"; |
| 161 | + @asm "move.b %d0, (", errno, ")"; |
| 162 | + errno := fcb_convert_return_code_to_error(errno); |
| 163 | +end sub; |
| 164 | + |
| 165 | +sub FCBSeek(fcb: [FCB], pos: uint32) is |
| 166 | + pos := pos - 1; # seek to *previous* character |
| 167 | + var newblock := (pos >> 7) as uint32; |
| 168 | + var newptr := (pos as uint8) & 127; |
| 169 | + fcb_i_changeblock(fcb, newblock); |
| 170 | + fcb.bufferptr := newptr; |
| 171 | +end sub; |
| 172 | + |
| 173 | +sub FCBPos(fcb: [FCB]): (pos: uint32) is |
| 174 | + pos := ((fcb_get_record(fcb) << 7) | (fcb.bufferptr as uint32)) + 1; |
| 175 | +end sub; |
| 176 | + |
| 177 | +sub FCBExt(fcb: [FCB]): (len: uint32) is |
| 178 | + var oldblock := fcb_get_record(fcb); |
| 179 | + var cpmfcb := &fcb.cpm; |
| 180 | + |
| 181 | + @asm "move.w #16, %d0"; # BDOS close file |
| 182 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 183 | + @asm "trap #2"; |
| 184 | + |
| 185 | + |
| 186 | + @asm "move.w #35, %d0"; # BDOS compute file size |
| 187 | + @asm "move.l (", cpmfcb, "), %d1"; |
| 188 | + @asm "trap #2"; |
| 189 | + |
| 190 | + len := fcb_get_record(fcb) << 7; |
| 191 | + fcb_set_record(fcb, oldblock); |
| 192 | +end sub; |
| 193 | + |
| 194 | +sub fcb_i_nextchar(fcb: [FCB]) is |
| 195 | + fcb.bufferptr := fcb.bufferptr + 1; |
| 196 | + if fcb.bufferptr == 128 then |
| 197 | + fcb_i_changeblock(fcb, fcb_get_record(fcb) + 1); |
| 198 | + fcb.bufferptr := 0; |
| 199 | + end if; |
| 200 | +end sub; |
| 201 | + |
| 202 | +sub FCBGetChar(fcb: [FCB]): (c: uint8) is |
| 203 | + fcb_i_nextchar(fcb); |
| 204 | + c := fcb.buffer[fcb.bufferptr]; |
| 205 | +end sub; |
| 206 | + |
| 207 | +sub FCBPutChar(fcb: [FCB], c: uint8) is |
| 208 | + fcb_i_nextchar(fcb); |
| 209 | + fcb.buffer[fcb.bufferptr] := c; |
| 210 | + fcb.dirty := 1; |
| 211 | +end sub; |
| 212 | + |
| 213 | +include "common-file.coh"; |
| 214 | + |
0 commit comments