-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.S
500 lines (450 loc) · 8.31 KB
/
asm.S
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
CPU_CHUNK = 4
FPU_CHUNK = CPU_CHUNK+4
CIAA_CHUNK = FPU_CHUNK+4
CIAB_CHUNK = CIAA_CHUNK+4
CUSTOM_CHUNK = CIAB_CHUNK+4
AGA_COLORS_CHUNK = CUSTOM_CHUNK+4
FLOPPY_CHUNK = AGA_COLORS_CHUNK+4
AUDIO_CHUNK = FLOPPY_CHUNK+4*4
SPRITE_CHUNK = AUDIO_CHUNK+4*4
CD32_CHUNK = SPRITE_CHUNK+4*8
CDTV_CHUNK = CD32_CHUNK+4
CDTV_DMAC_CHUNK = CDTV_CHUNK+4
MMU_TABLE = CDTV_DMAC_CHUNK+4
VBR_TABLE = MMU_TABLE+4
DEBUG_ENTRY = VBR_TABLE+4
WAITLINES = 15
.text
.globl _runit
.globl _killsystem
.globl _callinflate
.globl _inflate
.globl _flushcache
.globl _detect060
.globl _detect030040
.globl _detectmmu
_flushcache:
move.l a6,-(sp)
move.l 4.w,a6
jsr -0x27c(a6) | CacheClearU
move.l (sp)+,a6
rts
_detect030040:
.arch 68030
movem.l a5-a6,-(sp)
move.l 4.w,a6
lea .super030040(pc),a5
jsr -0x1e(a6)
movem.l (sp)+,a5-a6
rts
.super030040:
or.w #0x700,sr
movec vbr,a0
movem.l d2/d3,-(sp)
move.l sp,a5
subq.l #4,sp
move.l 0x10(a0),d1
move.l 0x2c(a0),d2
lea .is68040(pc),a1
move.l a1,0x10(a0)
lea .atleast030(pc),a1
move.l a1,0x2c(a0)
btst #2,297(a6)
bne.s .atleast030
pmove tt0,(sp)
| it is 68030
bset #2,297(a6)
.atleast030:
move.b 297(a6),d3
or.b #0x4+0x8,297(a6) | AttnFlags (68030 + 68040)
movec CAAR,d0 | (68020-68030 only)
| it is not 68040
move.b d3,297(a6) | AttnFlags
.is68040:
move.l a5,sp
move.l d1,0x10(a0)
move.l d2,0x2c(a0)
movem.l (sp)+,d2/d3
rte
.arch 68040
_detect060:
movem.l a5-a6,-(sp)
move.l 4.w,a6
lea .super060(pc),a5
jsr -0x1e(a6)
movem.l (sp)+,a5-a6
rts
.super060:
or.w #0x700,sr
movec vbr,a0
move.l sp,a5
move.l 0x10(a0),d1
lea .not68060(pc),a1
move.l a1,0x10(a0)
.arch 68060
movec PCR,d0
.arch 68040
| it is 68060
bset #7,297(a6) | AttnFlags
.not68060:
move.l d1,0x10(a0)
move.l a5,sp
rte
_detectmmu:
movem.l a5-a6,-(sp)
move.l 4.w,a6
lea .supermmu(pc),a5
jsr -0x1e(a6)
movem.l (sp)+,a5-a6
rts
.supermmu:
or.w #0x700,sr
movec tc,d0
btst #15,d0
bne.s .done
move.w d0,d1
or.w #0x4000,d0
movec d0,tc
movec tc,d0
btst #14,d0
beq.s .done
movec d1,tc
.done:
rte
_callinflate:
movem.l a4-a5,-(sp)
move.l 4+2*4(sp),a4
move.l 8+2*4(sp),a5
bsr _inflate
movem.l (sp)+,a4-a5
rts
| params: new stack 4, uaestate 8, func(uaestate) 12
_killsystem:
move.l 8(sp),a0 | uaestate
move.l a6,a1
move.l 4.w,a6
move.w 0x128(a6),(a0) | AttFlags
move.l sp,d1
move.l a0,d0
move.l a5,a0
lea .super(pc),a5
jsr -0x1e(a6) | Supervisor
.super:
move.w #0x2700,sr
move.l a0,a5
move.l a1,a6
move.l d0,a0
| debug trigger
| move.w #0x1234,0xef0006
| CIA: stop timers, clear interrupts
bclr #0,0xbfde00
bclr #0,0xbfdf00
bclr #0,0xbfee01
bclr #0,0xbfef01
move.b #0x7f,0xbfdd00
move.b #0x7f,0xbfed01
tst.b 0xbfdd00
tst.b 0xbfed01
lea 0xdff000,a0
move.w #0x7fff,d0
move.w d0,0x96(a0)
move.w d0,0x9a(a0)
move.w d0,0x9c(a0)
move.w d0,0x9e(a0)
move.l d1,a0
move.l 8(a0),a1
btst #4,3(a1)
beq.s .nocache2
moveq #0,d0
movec d0,cacr
.nocache2:
move.l 8(a0),d0
bsr.w mmu_enable
move.l 4(a0),sp | new temp super stack
move.l 8(a0),-(sp) | uaestate
move.l 12(a0),a0 | func
jsr (a0) | func(uaestate)
| never returns
_runit:
move.l 4(sp),a4 | get pointer to struct uaestate
subq.l #8,sp
lea 0xdff000,a6
| debug trigger
| move.w #0x1234,0xef000a
| restore FPU
move.l FPU_CHUNK(a4),d0
beq.s .nofpu
move.l d0,a0
| system has FPU?
move.w (a4),d1
btst #4,d1
beq.s .nofpu
clr.l -(sp)
| FPU -> NULL state
frestore (sp)+
btst #6,4(a0) | flag bit 30 (=old FPU state)
bne.s .oldfpustate
btst #5,4(a0) | flag bit 29 (=new FPU state)
beq.s .loadfpu
btst #0,4+8*10+4+4+4+4+4+3(a0)
beq.s .nofpu
bra.s .loadfpu
.oldfpustate:
btst #0,4+3(a0) | flag bit 0: 1 = NULL
bne.s .nofpu
.loadfpu:
move.l (a0),a0 | model points to pre-processed data
fmovem.x (a0)+,fp0-fp7
fmove.l (a0)+,fpcr
fmove.l (a0)+,fpsr
fmove.l (a0)+,fpiar
.nofpu:
| find last line
moveq #0,d4
lea 6(a6),a2
.wait1:
cmp.b #250,(a2)
bne.s .wait1
.wait1a:
cmp.b #2,(a2)
bne.s .wait1a
.wait1b:
move.w 4(a6),d0
btst #0,d0
beq.s .wait1c
move.b (a2),d0
cmp.b d0,d4
bcc.s .wait1b
move.b d0,d4
bra.s .wait1b
.wait1c:
cmp.b #160,(a2)
bne.s .wait1c
| last line - WAITLINES
sub.b #WAITLINES,d4
.wait3:
cmp.b (a2),d4
bne.s .wait3
| debug trigger
| move.w #0x1234,0xef0004
| don't play annoying sounds if pause
btst #3,3(a4)
bne.s .pausemode1
bsr begin_audio
.pausemode1:
| restore possible side-effect causing
| custom bits as late as possible
move.l CIAA_CHUNK(a4),(sp)
clr.l 4(sp)
bsr _set_cia_final
move.l CIAB_CHUNK(a4),(sp)
addq.l #1,4(sp)
bsr _set_cia_final
move.l CUSTOM_CHUNK(a4),(sp)
bsr _set_custom_final
move.w d0,d5
| pause
btst #3,3(a4)
beq.s .pausemode2
btst #0,1(a4) | 68010+?
beq.s .pausevbr
move.l VBR_TABLE(a4),d0
beq.s .pausevbr
movec d0,VBR
.pausevbr:
.wait4:
tst.b (a2)
bne.s .wait4
move.w d5,0x96(a6)
.pausemode3:
btst #6,0xbfe001
bne.s .pausemode3
| debounce delay
.pausemode4:
cmp.b 6(a6),d4
bne.s .pausemode4
subq.b #1,d4
.pausemode5:
cmp.b 6(a6),d4
bne.s .pausemode5
addq.b #1,d4
btst #6,0xbfe001
beq.s .pausemode4
lea 1(a2),a0
move.b d4,d0
add.b #WAITLINES,d0
.wait4b:
cmp.b (a2),d0
bne.s .wait4b
.wait4e:
cmp.b #0xa0,(a0)
bcs.s .wait4e
move.w #0x1f0,0x96(a6)
.wait4c:
cmp.b #160,(a2)
bne.s .wait4c
.wait4d:
cmp.b (a2),d4
bne.s .wait4d
bsr begin_audio
.pausemode2:
| restore CPU state
move.w (a4),d1
move.l CPU_CHUNK(a4),a0
| stack pointers
move.l 4+4+60+4+2+2+4(a0),SP
move.l 4+4+60+4+2+2(a0),a1 | USP
move.l a1,USP
.arch 68030
.cpu68020:
| 68020 (or higher)
cmp.l #68020,(a0)
bcs.s .cpu68010
btst #1,d1
beq.s .cpu68010
lea 4+4+60+4+2+2+4+4+2+4+(4+4+4)(a0),a1
move.l (a1)+,d0
btst #3,d1
bne.s .cpu68020_040
| CAAR is 68020-68030 only. Skip if 68040+
movec d0,CAAR
.cpu68020_040:
move.l (a1)+,d0
movec d0,CACR
move.l (a1)+,d0
btst #7,d1
bne.s .cpu68010
| MSP is 68020-68040 only. Skip if 68060.
movec d0,MSP
.cpu68010:
.arch 68040
| 68010 (or higher)
cmp.l #68010,(a0)
bcs.s .cpu68000
btst #0,d1
beq.s .cpu68000
lea 4+4+60+4+2+2+4+4+2+4(a0),a1
move.l (a1)+,d0
movec d0,DFC
move.l (a1)+,d0
movec d0,SFC
move.l (a1)+,d0
movec d0,VBR
.cpu68000:
btst #0,d1
beq.s .nocpu68010
btst #1,d1
beq.s .nocpu68020
btst #0,3(a4) | nocache?
beq.s .nocpu68020
moveq #0,d0
movec d0,CACR
.nocpu68020:
| 68010+ stack frame fix
move.w #0x0020,-(sp) | Format 0, Trap #0
| Debugging VBR, don't change VBR if originally non-zero
movec vbr,d0
tst.l d0
bne.s .nocpu68010
move.l VBR_TABLE(a4),d0
beq.s .nocpu68010
movec d0,VBR
.nocpu68010:
| prepare CIA timer control values
move.l CIAA_CHUNK(a4),a1
move.b 15(a1),d6
swap d6
move.b 14(a1),d6
move.l CIAB_CHUNK(a4),a1
move.b 15(a1),d7
swap d7
move.b 14(a1),d7
| PC, SR, registers
move.l 4+4+60(a0),-(sp) | PC
move.w 4+4+60+4+2+2+4+4(a0),-(sp) | SR
movem.l 4+4(a0),d0-d4
movem.l 4+4+8*4+4+4(a0),a2-a6
add.w #4+4+5*4,a0
| debug trigger
| move.w #0x1234,0xef0000
lea 0xbfde00,a1
| wait until beginning of first line
.wait2:
tst.b 0xdff006
bne.s .wait2
| enable DMA (except audio which was already enabled)
| enable last because program's copper list might modify copper pointer or blitter registers
move.w d5,0xdff096
| start timers
move.b d6,0xbfee01-0xbfde00(a1)
swap d6
move.b d6,0xbfef01-0xbfde00(a1)
move.b d7,(a1)
swap d7
move.b d7,0x100(a1)
| restore remaining registers
movem.l (a0),d5-d7/a0-a1
rte | GO! GO! GO!
mmu_enable:
movem.l d0-d2/a0-a2,-(sp)
move.l d0,a2
| debug trigger
| move.w #0x1234,0xef0008
move.l MMU_TABLE(a2),d2
beq.s .mmuend
move.w (a2),d1
btst #3,d1
beq.s .no68040
| 68040/060
movec d2,urp
movec d2,srp
cpusha dc
cinva dc
pflusha
move.l #0x8000,d0
movec d0,tc
moveq #0,d0
movec d0,itt0
movec d0,itt1
movec d0,dtt0
movec d0,dtt1
bra.s .mmuend
.no68040:
| 68030?
btst #2,d1
beq.s .mmuend
.arch 68030
sub.w #12,sp
move.l #0x00c07760,(sp)
pmove (sp),tc
move.l #0x80000002,4(sp)
move.l d2,8(sp)
pmove 4(sp),crp
bset #7,(sp)
pmove (sp),tc
clr.l (sp)
pmove (sp),tt0
pmove (sp),tt1
add.w #12,sp
.arch 68040
.mmuend:
movem.l (sp)+,d0-d2/a0-a2
rts
begin_audio:
move.l CUSTOM_CHUNK(a4),a0
move.w 4+0x96(a0),d0 | DMACON
and.w #15,d0
or.w #0x8200,d0
| enable audio DMA
move.w d0,0x96(a6)
| wait few lines, enough time for audio channels to enter normal 2/3 states
move.b d4,d0
addq.b #4,d0
.audwait:
cmp.b (a2),d0
bne.s .audwait
| restore latched AUDxLEN and AUDxPT
move.l a4,-(sp)
bsr _set_audio_final
addq.l #4,sp
rts