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