kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 1 | .area _CODE |
| 2 | .globl _get_file_length |
| 3 | .globl _load_file |
| 4 | ;;---------------------------------------------------------------------------- |
| 5 | ;; get length of file on disc. Assumption file has a AMSDOS header |
| 6 | ;; |
| 7 | ;; int get_file_length(const char *filename); |
| 8 | |
| 9 | _get_file_length:: |
| 10 | ld hl,#2 |
| 11 | add hl,sp |
| 12 | ld a,(hl) |
| 13 | inc hl |
| 14 | ld h,(hl) |
| 15 | ld l,a |
| 16 | |
Adrien Destugues | dd007ce | 2020-12-17 21:49:04 +0100 | [diff] [blame^] | 17 | push ix |
| 18 | |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 19 | ;; HL = address of null terminated string |
| 20 | call count_string_length |
| 21 | ld de,#0x0c000 ;; points to unused 2k buffer |
| 22 | call 0x0bc77 ;; cas in open |
| 23 | push bc ;; BC = length of file |
| 24 | call 0x0bc7d ;; cas in abandon |
| 25 | pop hl |
Adrien Destugues | dd007ce | 2020-12-17 21:49:04 +0100 | [diff] [blame^] | 26 | |
| 27 | pop ix |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 28 | ret |
| 29 | |
| 30 | ;;--------------------------------------------------------------------- |
| 31 | |
| 32 | count_string_length: |
| 33 | push hl |
| 34 | ld b,#0 |
| 35 | csl: ld a,(hl) |
| 36 | or a |
| 37 | jr z,csl2 |
| 38 | inc hl |
| 39 | inc b |
| 40 | jr csl |
| 41 | csl2: |
| 42 | pop hl |
| 43 | ret |
| 44 | |
| 45 | ;;--------------------------------------------------------------------------- |
| 46 | ;; void load_file(const char *filename, void *addr) |
| 47 | |
| 48 | _load_file:: |
| 49 | ld hl,#5 |
| 50 | add hl,sp |
| 51 | ld d,(hl) |
| 52 | dec hl |
| 53 | ld e,(hl) |
| 54 | dec hl |
| 55 | |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 56 | ld a,(hl) |
| 57 | dec hl |
| 58 | ld l,(hl) |
| 59 | ld h,a |
| 60 | |
Adrien Destugues | dd007ce | 2020-12-17 21:49:04 +0100 | [diff] [blame^] | 61 | push ix |
| 62 | |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 63 | call count_string_length |
Adrien Destugues | dd007ce | 2020-12-17 21:49:04 +0100 | [diff] [blame^] | 64 | push de |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 65 | ld de,#0x0c000 |
| 66 | call 0x0bc77 ;; cas in open |
| 67 | pop hl ;; load address |
| 68 | call 0x0bc83 ;; cas in direct |
| 69 | call 0x0bc7a ;; cas in close |
Adrien Destugues | dd007ce | 2020-12-17 21:49:04 +0100 | [diff] [blame^] | 70 | |
| 71 | pop ix |
| 72 | |
kthacker | 6de6775 | 2006-04-17 15:02:26 +0000 | [diff] [blame] | 73 | ret |
| 74 | |
| 75 | ;; void relocate(void *addr,void *base) |
| 76 | |
| 77 | ;; IX = address of relocate data |
| 78 | _relocate:: |
| 79 | ld hl,#5 |
| 80 | add hl,sp |
| 81 | push ix |
| 82 | ld b,(hl) ;; base address |
| 83 | dec hl |
| 84 | ld c,(hl) |
| 85 | dec hl |
| 86 | ld a,(hl) |
| 87 | .db #0x0dd |
| 88 | ld h,a |
| 89 | dec hl |
| 90 | ld a,(hl) |
| 91 | .db #0x0dd |
| 92 | ld l,a ;; IX is offset of table from start of loaded file |
| 93 | add ix,bc ;; relocate IX to give absolute address of table. |
| 94 | |
| 95 | push bc |
| 96 | pop hl |
| 97 | call relocate_16bit |
| 98 | push bc |
| 99 | pop hl |
| 100 | call relocate_8bitl ;; lower byte |
| 101 | push bc |
| 102 | pop hl |
| 103 | call relocate_8bith ;; upper byte |
| 104 | pop ix |
| 105 | ret |
| 106 | |
| 107 | ;;-------------------------------------------------------------------------- |
| 108 | ;; Relocate 8-bit values (e.g. where low and high parts of an address |
| 109 | ;; are loaded seperatly into registers) |
| 110 | ;; |
| 111 | ;; IX = list of 16-bit addresses. Each address identifies an 8-bit |
| 112 | ;; value |
| 113 | ;; |
| 114 | relocate_8bith: |
| 115 | ld a,0(ix) |
| 116 | inc ix |
| 117 | or a |
| 118 | ret z |
| 119 | cp #0x0ff |
| 120 | jr nz,r8bh |
| 121 | ld e,0(ix) |
| 122 | inc ix |
| 123 | ld d,0(ix) |
| 124 | inc ix |
| 125 | add hl,de |
| 126 | jr relocate_8bith |
| 127 | |
| 128 | r8bh: |
| 129 | ;; add offset |
| 130 | add a,l |
| 131 | ld l,a |
| 132 | ld a,h |
| 133 | adc a,#0x0 |
| 134 | ld h,a |
| 135 | |
| 136 | ;; get low byte of address |
| 137 | ld e,0(ix) |
| 138 | inc ix |
| 139 | |
| 140 | ;; get high byte to relocate |
| 141 | ld d,(hl) |
| 142 | ex de,hl |
| 143 | add hl,bc |
| 144 | ex de,hl |
| 145 | ld (hl),d |
| 146 | jr relocate_8bith |
| 147 | |
| 148 | relocate_8bitl: |
| 149 | ld a,0(ix) |
| 150 | inc ix |
| 151 | or a |
| 152 | ret z |
| 153 | cp #0x0ff |
| 154 | jr nz,r8bl |
| 155 | ld e,0(ix) |
| 156 | inc ix |
| 157 | ld d,0(ix) |
| 158 | inc ix |
| 159 | add hl,de |
| 160 | jr relocate_8bitl |
| 161 | |
| 162 | r8bl: |
| 163 | add a,l |
| 164 | ld l,a |
| 165 | ld a,h |
| 166 | adc a,#0x0 |
| 167 | ld h,a |
| 168 | |
| 169 | ld e,(hl) |
| 170 | ld d,#0x0 |
| 171 | ex de,hl |
| 172 | add hl,bc |
| 173 | ex de,hl |
| 174 | ld (hl),e |
| 175 | jr relocate_8bitl |
| 176 | |
| 177 | ;;-------------------------------------------------------------------------- |
| 178 | ;; Relocate 16-bit values |
| 179 | ;; |
| 180 | ;; Entry conditions: |
| 181 | ;; |
| 182 | ;; IX = list of 16-bit addresses. Each address identifies a 16-bit |
| 183 | ;; value to relocate. |
| 184 | ;; |
| 185 | ;; BC = base address |
| 186 | ;; |
| 187 | ;; NOTE: |
| 188 | ;; - Relocatable 16-bit values come from CALL and JP instructions and |
| 189 | ;; loading a 16-bit register. |
| 190 | |
| 191 | relocate_16bit: |
| 192 | ld a,0(ix) ;; number of items to relocate |
| 193 | inc ix |
| 194 | or a |
| 195 | ret z |
| 196 | cp #0x0ff |
| 197 | jr nz,r16 |
| 198 | ld e,0(ix) |
| 199 | inc ix |
| 200 | ld d,0(ix) |
| 201 | inc ix |
| 202 | add hl,de |
| 203 | jr relocate_16bit |
| 204 | |
| 205 | r16: |
| 206 | ;; add offset |
| 207 | add a,l |
| 208 | ld l,a |
| 209 | ld a,h |
| 210 | adc a,#0x0 |
| 211 | ld h,a |
| 212 | |
| 213 | ;; get the 16-bit value |
| 214 | ld e,(hl) |
| 215 | inc hl |
| 216 | ld d,(hl) |
| 217 | |
| 218 | |
| 219 | ;; add base address; therefore relocating it. |
| 220 | ex de,hl |
| 221 | add hl,bc |
| 222 | ex de,hl |
| 223 | ;; write relocated value |
| 224 | ld (hl),d |
| 225 | dec hl |
| 226 | ld (hl),e |
| 227 | jr relocate_16bit |
| 228 | |