-
Notifications
You must be signed in to change notification settings - Fork 1
/
hipatch.src
510 lines (414 loc) · 6.54 KB
/
hipatch.src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
; hipatch - 6502 high speed SIO code OS patch program
;
; Copyright (c) 2006-2023 by Matthias Reichl <[email protected]>
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
.include "hipatch.inc"
.include "cio.inc"
.bank
.if .def ROMABLE
PATCHRESET=1
.endif
.if .def ATRBOOT
ORG = $700
.else
ORG = $6000
.endif
XLSIO = $E971
TMPVEC = $E0 ; temporary ZP vector
.if .def ATRBOOT
; ATR header
* = ORG-16
.WORD $0296 ; magic
.WORD (MYEND-ORG+15)/16
.WORD 128
.WORD 0,0,0,0,0
; boot header
.BYTE 0
.BYTE (MYEND-ORG+127)/128
.WORD ORG
.WORD EXIT
.else
* = ORG
.endif
BPUT 0, TTITL, TTIT
; check if OS is already patched
LDX #3
CHKP LDA XLSIO,X
CMP MYCODE,X
BNE CHKP1
DEX
BPL CHKP
PRINT 0, TISP
JSR CLRSIO
INC WARNING
JMP EXIT
; just clear SIO settings
CLRSIO LDX #12
LDA #0
CLRSIOL STA RAMBUF,X
DEX
BPL CLRSIOL
RTS
; check if OS is compatible
CHKP1 LDX #3
CHKCO LDA XLSIO,X
CMP XLCODE,X
BNE CHKCO1
DEX
BPL CHKCO
BMI CHKCO2
CHKCO1 PRINT 0, TINC
INC WARNING
JMP EXIT
CHKCO2
.IF .DEF ROMABLE
; check if we need to update the ROM checksums
JSR CHKCSUM
.ENDIF
PRINT 0, TPAT
; check if we need to move ROM to RAM
LDA $D301
AND #$01
STA NEEDRES
BEQ NOCOPY
JSR ROMRAM
; write highspeed SIO code to PDEST
NOCOPY LDX #0
COPLP LDA PCODE,X
STA PDEST,X
LDA PCODE+$100,X
STA PDEST+$100,X
LDA PCODE+$200,X
STA PDEST+$200,X
LDA PCODE+$300,X
STA PDEST+$300,X
INX
BNE COPLP
; clear speed tab
JSR CLRSIO
; patch OS SIO routine
LDX #3
COP2 LDA XLSIO,X
STA PSTD,X
LDA MYCODE,X
STA XLSIO,X
DEX
BPL COP2
LDA #$4C
STA PSTD+4
LDA #<(XLSIO+4)
STA PSTD+5
LDA #>(XLSIO+4)
STA PSTD+6
.if .def PATCHKEY
; check if we can patch the keyboard IRQ handler
LDX #2
CHKKY LDA KEYIRQ,X
CMP XLKEY,X
BNE CHKKY1
DEX
BPL CHKKY
BMI CHKKY2
CHKKY1 PRINT 0, TNOKEY
INC WARNING
JMP CHKKY4
CHKKY2 LDX #2
CHKKY3 LDA MYKEY,X
STA KEYIRQ,X
DEX
BPL CHKKY3
PRINT 0, TKEY
CHKKY4
.endif
.if .def PATCHRESET
; check if we can patch the powerup/reset code
LDX #2
CHKBT LDA PUPCODE,X
CMP XLPUP,X
BNE CHKBT1
DEX
BPL CHKBT
BMI CHKBT2
CHKBT1 PRINT 0, TNOPUP
INC WARNING
JMP CHKBT4
CHKBT2 LDX #2
CHKBT3 LDA MYPUP,X
STA PUPCODE,X
DEX
BPL CHKBT3
PRINT 0, TPUP
CHKBT4
.endif
.IF .DEF ROMABLE
; update ROM checksums
JSR UPDCSUM
.ENDIF
LDA NEEDRES
BEQ NORES
; install reset handler
LDX #RESETL-1
COPRES LDA RESET,X
STA RESETAD,X
DEX
BPL COPRES
LDA #<RESETAD
STA 2
LDA #>RESETAD
STA 3
LDA 9
ORA #2
STA 9
NORES JMP EXIT
NEEDRES .BYTE 0
WARNING .BYTE 0
.include "romram.src"
.if .def ATARISIO_SWAP
.include "atarisio.src"
.endif
; reset routine:
RESET LDA $D20F
AND #8
BNE ?NOCOLD
JMP $E477 ; do a coldstart if SHIFT is pressed
?NOCOLD LDA $D301
AND #$FE
STA $D301
RTS
RESETL = * - RESET
EXIT
.if .def ATRBOOT
.if .def ATARISIO_SWAP
JSR SWAP_DRIVES
BCC EXITX
.endif
BPUT 0, TBOOTL, TBOOT
WCONSOL
LDA $D01F
CMP #6
BNE WCONSOL
LDA #155
JSR PUTCHR
EXITX
LDA #<ORG
STA TMPVEC
LDA #>ORG
STA TMPVEC+1
LDY #CLEANUPL-1
CPCLUP
LDA CLEANUP,Y
STA RESETAD+RESETL,Y
DEY
BPL CPCLUP
LDA #0
TAY
LDX #>(MYEND-ORG+255)
JMP RESETAD+RESETL
CLEANUP STA (TMPVEC),Y
INY
BNE CLEANUP
INC TMPVEC+1
DEX
BNE CLEANUP
SEC
RTS
CLEANUPL = * - CLEANUP
PUTCHR
TAY
LDA $E407
PHA
LDA $E406
PHA
TYA
RTS
.else
; delay for approx. 2 seconds if some warning occurred
LDA WARNING
BEQ NODELAY
CLC
LDA 20
ADC #100
W20 CMP 20
BNE W20
NODELAY RTS
.endif
XLCODE TSX
STX $0318
MYCODE NOP
JMP PSIO
.if .def PATCHKEY
; original contents of keyboard IRQ handler
XLKEY LDA $D209
; patched contents of keyboard IRQ handler
MYKEY JSR PKEY
.endif
.if .def PATCHRESET
; original contents of reset code
XLPUP LDA $033D
; patched contents of reset code
MYPUP JMP PPWRUP
.endif
.if .def ROMABLE
ROM1ID = $C000
ROM2ID = $FFF8
CADR = $E0
CEND = $E2
CSUM = $E4
; calculate checksum of a ROM-block
CSBLK LDY #0
CSSET LDA ADRTAB,X
STA CADR,Y
INX
INY
CPY #4
BNE CSSET
LDY #0
CSBLKL CLC
LDA (CADR),Y
ADC CSUM
STA CSUM
BCC CSBLKL1
INC CSUM+1
CSBLKL1 INC CADR
BNE CSBLKL2
INC CADR+1
CSBLKL2 LDA CADR
CMP CEND
BNE CSBLKL
LDA CADR+1
CMP CEND+1
BNE CSBLKL
RTS
; checksum of first 8k ROM
CHK1 LDA #0
STA CSUM
STA CSUM+1
TAX
JSR CSBLK
LDA $D301
PHA
AND #$7F
ORA #1
STA $D301
JSR CSBLK
PLA
STA $D301
JSR CSBLK
RTS
; checksum of second 8k ROM
CHK2 LDA #0
STA CSUM
STA CSUM+1
LDX #12
JSR CSBLK
JSR CSBLK
RTS
ADRTAB .WORD $C002,$D000
.WORD $5000,$5800
.WORD $D800,$E000
.WORD $E000,$FFF8
.WORD $FFFA,$0000
DOCSUPD .BYTE 0
CHKCSUM LDA #0
STA DOCSUPD
JSR CHK1
LDA ROM1ID
CMP CSUM
BNE CSERR
LDA ROM1ID+1
CMP CSUM+1
BNE CSERR
JSR CHK2
LDA ROM2ID
CMP CSUM
BNE CSERR
LDA ROM2ID+1
CMP CSUM+1
BNE CSERR
INC DOCSUPD
RTS
CSERR BPUT 0, TCSERRL, TCSERR
RTS
UPDCSUM LDA DOCSUPD
BEQ UPDCSE
PRINT 0, TUPDCS
JSR CHK1
LDA CSUM
STA ROM1ID
LDA CSUM+1
STA ROM1ID+1
JSR CHK2
LDA CSUM
STA ROM2ID
LDA CSUM+1
STA ROM2ID+1
UPDCSE RTS
TCSERR .BYTE "ROM checksum error - skipping",155
.BYTE "checksum update", 155,155
TCSERRL = * - TCSERR
TUPDCS .BYTE "updating ROM checksum",155
.endif
.if .def PATCHKEY
TNOKEY .BYTE "unknown ROM, not patching keyboard",155
TKEY .BYTE "patched keyboard IRQ handler",155
.endif
.if .def PATCHRESET
TNOPUP .BYTE "unknown ROM, not patching powerup code",155
TPUP .BYTE "patched powerup code",155
.endif
.if .def ROMABLE
TTIT
.if .def ATRBOOT
.BYTE 125
.else
.BYTE 155
.endif
.BYTE "Highspeed SIO patch V1.33R",155
.else
TTIT
.if .def ATRBOOT
.BYTE 125
.else
.BYTE 155
.endif
.BYTE "Highspeed SIO patch V1.33",155
.endif
.BYTE "(c) 2023 by Matthias Reichl",155,155
TTITL = * - TTIT
.if .def ATRBOOT
TBOOT .BYTE 155,"Press START to boot disk"
TBOOTL = * - TBOOT
.endif
TISP .BYTE "OS already patched",155
TINC .BYTE "incompatible OS",155
TPAT .BYTE "patching OS",155
PCODE = *
.if .def ROMABLE
.incbin "hipatch-code-rom.bin"
.else
.incbin "hipatch-code.bin"
.endif
.if .def ATRBOOT
.if (*&127)<>0
.dc (128-(*&127)) 0
.endif
MYEND = *
.else
.bank
* = $2E0
.WORD ORG
.endif