aboutsummaryrefslogtreecommitdiff
path: root/source/arm_dynarec/armfn.S
blob: c5d1b3f120822c44b1d8a06006453a7f3cc1b8b0 (plain)
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
#define RegA      r4
#define RegX      r5
#define RegY      r6
#define RegP      r7
#define RegCPU    r8
#define RegCPUPC  r9
#define RegCycles r10
#define RegChecks ip

#ifndef NDEBUG
#define METRICS_GETSETS_OFF     28
#define METRICS_SLOWGETSETS_OFF 32
#endif

#define CPU_FLAGS_OFF              0
#define CPU_PC_OFF                 12
#define CPU_PCBASE_OFF             16
#define CPU_PC_AT_OPCODE_START_OFF 20
#define CPU_WAIT_ADDRESS_OFF       24
#define CPU_CYCLES_OFF             32
#define CPU_NEXT_EVENT_OFF         36
#define CPU_ICPU_OFF               0x80
#define CPU_MEMORY_MAP_OFF         0x100

#define ICPU_S9X_OPCODES_OFF (CPU_ICPU_OFF + 4)
#define ICPU_REG_PB_OFF      (CPU_ICPU_OFF + 8)
#define ICPU_REG_DB_OFF      (CPU_ICPU_OFF + 9)
#define ICPU_REG_P_OFF       (CPU_ICPU_OFF + 10)
#define ICPU_REG_A_OFF       (CPU_ICPU_OFF + 12)
#define ICPU_REG_D_OFF       (CPU_ICPU_OFF + 14)
#define ICPU_REG_S_OFF       (CPU_ICPU_OFF + 16)
#define ICPU_REG_X_OFF       (CPU_ICPU_OFF + 18)
#define ICPU_REG_Y_OFF       (CPU_ICPU_OFF + 20)
#define ICPU_REG_PC_OFF      (CPU_ICPU_OFF + 22)
#define ICPU_CARRY_OFF       (CPU_ICPU_OFF + 24)
#define ICPU_ZERO_OFF        (CPU_ICPU_OFF + 25)
#define ICPU_NEGATIVE_OFF    (CPU_ICPU_OFF + 26)
#define ICPU_OVERFLOW_OFF    (CPU_ICPU_OFF + 27)

#define MEMORY_MAP_OFF                    0x0024
#define MEMORY_MAP_WRITE_MAP_OFF          0x4000
#define MEMORY_MAP_SPEED_OFF              0x8000
#define MEMORY_MAP_BLOCK_IS_RAM_OFF       0x9000
#define MEMORY_WRITE_MAP_SPEED_OFF        0x4000
#define MEMORY_WRITE_MAP_BLOCK_IS_RAM_OFF 0x5000

#define SA1_OPCODES_OFF   0
#define SA1_EXECUTING_OFF 24
#define SA1_WAIT_COUNTER_OFF 52
#define SA1_WAIT_BYTE_ADDRESS_1_OFF 56
#define SA1_WAIT_BYTE_ADDRESS_2_OFF 60

#define FLAG_DECIMAL   0x08
#define FLAG_INDEX     0x10
#define FLAG_MEMORY    0x20
#define FLAG_EMULATION 0x100

#define FUNC(name) \
  .global name ;   \
name:

.macro S9xCall func
  push { ip, lr }
  bl S9xRegsToMem
  bl \func
  bl S9xMemToRegs
  pop { ip, pc }
.endm

  .text
  .align 2

#ifndef NDEBUG
.macro MetricsIncOffset off
  push { r0, r1 }
  ldr r0, = Metrics
  ldr r1, [r0, #\off]
  add r1, r1, #1
  str r1, [r0, #\off]
  pop { r0, r1 }
.endm

FUNC(DynaBreak)
  bx lr
#endif

FUNC(BlockEnter)
  push { r4-r11, ip, lr }
  ldr RegCPU, = CPU
  bl S9xMemToRegs
  bx r0

FUNC(BlockReturn)
  bl S9xRegsToMem
  pop { r4-r11, ip, pc }

.macro LoadChecks rs
  ldr \rs, [RegCPU, #CPU_NEXT_EVENT_OFF]
  ldr RegChecks, [RegCPU, #CPU_FLAGS_OFF]
  orr RegChecks, RegChecks, \rs, lsl #16
.endm

.macro PushCPUState
  str RegCPUPC, [RegCPU, #CPU_PC_OFF]
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
.endm

.macro PopCPUState rs
  ldr RegCPUPC, [RegCPU, #CPU_PC_OFF]
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  LoadChecks \rs
.endm

.macro S9xUnpackStatusFast
  strh RegP, [RegCPU, #ICPU_REG_P_OFF]
  mov r3, #1
  and r2, r3, RegP, lsr #1
  sub r2, r2, #1
  strb r2, [RegCPU, #ICPU_ZERO_OFF]
  and r2, RegP, #0x80
  strb r2, [RegCPU, #ICPU_NEGATIVE_OFF]
  and r2, RegP, r3
  strb r2, [RegCPU, #ICPU_CARRY_OFF]
  and r2, r3, RegP, lsr #6
  strb r2, [RegCPU, #ICPU_OVERFLOW_OFF]
.endm

.macro S9xPackStatusFast
  ldrh RegP, [RegCPU, #ICPU_REG_P_OFF]
  ldrb r2, [RegCPU, #ICPU_ZERO_OFF]
  ldrb r3, [RegCPU, #ICPU_NEGATIVE_OFF]
  bic RegP, RegP, #0xc3
  cmp r2, #0
  ldrb r2, [RegCPU, #ICPU_CARRY_OFF]
  orreq RegP, RegP, #0x2
  tst r3, #0x80
  ldrb r3, [RegCPU, #ICPU_OVERFLOW_OFF]
  orrne RegP, RegP, #0x80
  orr RegP, RegP, r2
  orr RegP, RegP, r3, lsl #6
  strh RegP, [RegCPU, #ICPU_REG_P_OFF]
.endm

  @@ Callers assume r0 is not touched
FUNC(S9xCallUnpackStatusFast)
  S9xUnpackStatusFast
  bx lr

  @@ Preserves scratch and r0, can be used as a wrapper
FUNC(S9xRegsToMem)
  push { r0, r1, r2, r3, ip, lr }
  str RegCPUPC, [RegCPU, #CPU_PC_OFF]
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  strh RegA, [RegCPU, #ICPU_REG_A_OFF]
  strh RegX, [RegCPU, #ICPU_REG_X_OFF]
  strh RegY, [RegCPU, #ICPU_REG_Y_OFF]
  S9xUnpackStatusFast
  pop { r0, r1, r2, r3, ip, pc }


  @@ Preserves scratch and r0, can be used as a wrapper
FUNC(S9xMemToRegs)
  push { r0, r1, r2, r3, ip, lr }
  ldr RegCPUPC, [RegCPU, #CPU_PC_OFF]
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  S9xPackStatusFast
  ldrh RegA, [RegCPU, #ICPU_REG_A_OFF]
  ldrh RegX, [RegCPU, #ICPU_REG_X_OFF]
  ldrh RegY, [RegCPU, #ICPU_REG_Y_OFF]
  pop { r0, r1, r2, r3, ip, lr }
  LoadChecks r2
  bx lr

FUNC(UpdateSection)
  push { RegP, RegCPU, lr }
  ldr RegCPU, =CPU
  ldrh RegP, [RegCPU, #ICPU_REG_P_OFF]
  bl S9xCallUpdateSection
  pop { RegP, RegCPU, pc }

FUNC(S9xCallUpdateSection)
  ldr r1, =CacheSection
  ldrb r0, [RegCPU, #ICPU_REG_PB_OFF]
  tst RegP, #FLAG_DECIMAL
  lsl r0, #4
  orrne r0, r0, #0x8000
  tst RegP, #FLAG_EMULATION
  orrne r0, r0, #0x4000
  bxne lr
  tst RegP, #FLAG_INDEX
  orrne r0, r0, #0x1000
  tst RegP, #FLAG_MEMORY
  orrne r0, r0, #0x2000
  str r0, [r1]
  bx lr

FUNC(LoadChecks)
  LoadChecks r2
  bx lr

FUNC(S9xCallSA1MainLoop)
  push { ip, lr }
  bl S9xSA1MainLoop
  pop { ip, pc }

FUNC(S9xCallSetPCBase)
  push { ip, lr }
  bl S9xSetPCBase
  pop { ip, pc }

.macro S9xCallHandleHBlank type
FUNC(S9xCallHandleHBlank\type)
  @ changes cycles, maybe PC in IRQ
  push { ip, lr }
  str RegCPUPC, [RegCPU, #CPU_PC_OFF]
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  bl HandleHBlank\type
  ldr RegCPUPC, [RegCPU, #CPU_PC_OFF]
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  pop { ip, lr }
  LoadChecks r2
  bx lr
.endm

S9xCallHandleHBlank SFX
S9xCallHandleHBlank NoSFX

.macro S9xCallHandleFlags WFI
FUNC(S9xCallHandleFlags\WFI)
  @ needs S, changes flags (unpack status needed), changes PC, changes cycles
  push { ip, lr }
  str RegCPUPC, [RegCPU, #CPU_PC_OFF]
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  S9xUnpackStatusFast
  bl HandleFlags\WFI
  ldr RegCPUPC, [RegCPU, #CPU_PC_OFF]
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  S9xPackStatusFast
  pop { ip, lr }
  LoadChecks r2
  bx lr
.endm

S9xCallHandleFlags WFI
S9xCallHandleFlags NoWFI

.macro S9xCallHandleChecks WFI, SFX
FUNC(S9xCallHandleChecks\WFI\SFX)
  push { r1, lr }
  mov r0, #0
  cmp RegCycles, RegChecks, lsr #16
  blhs S9xCallHandleHBlank\SFX
  cmp r0, #0
  popne { r1, lr }
  bne BlockReturn

  tst RegChecks, #0x880
  blne S9xCallHandleFlags\WFI
  cmp r0, #0
  movne RegCPUPC, r0
  popne { r1, lr }
  bne JumpIndirect

  pop { r1, pc }
.endm

S9xCallHandleChecks WFI, SFX
S9xCallHandleChecks WFI, NoSFX
S9xCallHandleChecks NoWFI, SFX
S9xCallHandleChecks NoWFI, NoSFX

#ifndef NDEBUG
FUNC(S9xCallCheckInstruction)
  S9xCall CheckInstruction
#endif

FUNC(JumpIndirect)
  push { r1, ip }
  ldr r1, [RegCPU, #CPU_PCBASE_OFF]
  sub r0, RegCPUPC, r1
  bl FetchBlock
  pop { r1, ip }
  ldr r0, [r0]
  bx r0

FUNC(JumpDirectChecks)
  push { r1, ip }
  mov r0, RegCPUPC
  sub r1, lr, #4                @ Patch the opcode at the address of this caller
  bl PatchJumpDirectChecks
  pop { r1, ip }
  bx r0

FUNC(JumpDirect)
  push { r1, ip }
  mov r0, RegCPUPC
  sub r1, lr, #4                @ Patch the opcode at the address of this caller
  bl PatchJumpDirect
  pop { r1, ip }
  bx r0

.macro GetMemFast Type
FUNC(S9xGet\Type\()Fast)
  push { r0, r3, ip, lr }
#ifndef NDEBUG
  MetricsIncOffset METRICS_GETSETS_OFF
#endif
  ldr r2, =0x0fff               @ MEMMAP_MASK
  .ifc \Type,Word
  and r3, r0, r2
  cmp r3, r2
  beq 9f
  .endif

  add ip, RegCPU, #CPU_MEMORY_MAP_OFF
  and r3, r2, r0, lsr #12       @ MEMMAP_SHIFT
  ldr r2, [ip, r3, lsl #2]
  add lr, ip, #MEMORY_MAP_BLOCK_IS_RAM_OFF
  cmp r2, #18                   @ MAP_LAST
  blo 9f

  ldrb lr, [lr, r3]
  add ip, ip, #MEMORY_MAP_SPEED_OFF
  ldrb ip, [ip, r3]
  cmp lr, #1
  streq r1, [RegCPU, #CPU_WAIT_ADDRESS_OFF]

  mov r1, r0, lsl #16
  mov r1, r1, lsr #16
  ldrb r0, [r2, r1]
  .ifc \Type,Word
  add r1, r1, #1
  ldrb r1, [r2, r1]
  .endif
  .ifc \Type,Byte
  add RegCycles, RegCycles, ip
  .else
  add RegCycles, RegCycles, ip, lsl #1 @ * 2 for GetWord
  .endif
  .ifc \Type,Word
  orr r0, r0, r1, lsl #8
  .endif
  pop { r1, r3, ip, pc }
9:
#ifndef NDEBUG
  MetricsIncOffset METRICS_SLOWGETSETS_OFF
#endif
  str r1, [RegCPU, #CPU_PC_AT_OPCODE_START_OFF]
  PushCPUState
  bl S9xGet\Type
  pop { r1, r3, ip, lr }
  PopCPUState r2
  bx lr
.endm

GetMemFast Byte
GetMemFast Word

  .macro SetMemFast Type, SA1
FUNC(S9xSet\Type\()Fast\SA1)
  push { r1, r3, ip, lr}
#ifndef NDEBUG
  MetricsIncOffset METRICS_GETSETS_OFF
#endif
  ldr r2, =0x0fff               @ MEMMAP_MASK
  mov ip, #0
  .ifc \Type,Word
  and r3, r1, r2
  cmp r3, r2
  beq 9f
  .endif

  str ip, [RegCPU, #CPU_WAIT_ADDRESS_OFF]

  and r3, r2, r1, lsr #12       @ MEMMAP_SHIFT
  add ip, RegCPU, #(CPU_MEMORY_MAP_OFF + MEMORY_MAP_WRITE_MAP_OFF)
  ldr r2, [ip, r3, lsl #2]
  cmp r2, #18                   @ MAP_LAST
  blo 9f

  add ip, ip, #MEMORY_WRITE_MAP_SPEED_OFF
  ldrb ip, [ip, r3]
  .ifc \Type,Byte
  add RegCycles, RegCycles, ip
  .else
  add RegCycles, RegCycles, ip, lsl #1 @ * 2 for GetWord
  .endif


  mov r1, r1, lsl #16
  mov r1, r1, lsr #16

  @@ Handle SA1 WaitAddress if needed
  .ifc \SA1,SA1
  push { r1 }
  ldr ip, =SA1
  add r1, r1, r2
  ldr r3, [ip, #SA1_WAIT_BYTE_ADDRESS_1_OFF]
  cmp r3, r1
  beq 8f
  ldr r3, [ip, #SA1_WAIT_BYTE_ADDRESS_2_OFF]
  cmp r3, r1
  beq 8f
2:
  pop { r1 }
  .endif

  strb r0, [r2, r1]
  .ifc \Type,Word
  add r1, r1, #1
  mov r0, r0, lsr #8
  strb r0, [r2, r1]
  .endif
  pop { r1, r3, ip, pc }

  .ifc \SA1,SA1
8:
  ldr r1, [ip, #SA1_OPCODES_OFF]
  mov r3, #0
  str r3, [ip, #SA1_WAIT_COUNTER_OFF]
  cmp r1, #0
  movne r3, #1
  strb r3, [ip, #SA1_EXECUTING_OFF]
  b 2b
  .endif
9:
#ifndef NDEBUG
  MetricsIncOffset METRICS_SLOWGETSETS_OFF
#endif
  PushCPUState
  .ifc \Type,Word
  mov r0, r0, lsl #16
  mov r0, r0, lsr #16
  .else
  and r0, r0, #0xFF
  .endif
  bl S9xSet\Type
  pop { r1, r3, ip, lr }
  PopCPUState r2
  bx lr
.endm

SetMemFast Byte, NoSA1
SetMemFast Word, NoSA1

SetMemFast Byte, SA1
SetMemFast Word, SA1

FUNC(S9xCallFixCycles)
  tst RegP, #FLAG_EMULATION
  ldrne r0, =S9xOpcodesE1
  bne 9f

  tst RegP, #FLAG_MEMORY
  beq 2f
  tst RegP, #FLAG_INDEX
  ldrne r0, =S9xOpcodesM1X1
  ldreq r0, =S9xOpcodesM1X0
  b 9f
2:
  tst RegP, #FLAG_INDEX
  ldrne r0, =S9xOpcodesM0X1
  ldreq r0, =S9xOpcodesM0X0
9:
  str r0, [RegCPU, #ICPU_S9X_OPCODES_OFF]
  bx lr

FUNC(S9xCallCPUShutdown)
  push { ip, lr }
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  bl DynaCPUShutdown
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  pop { ip, pc }

FUNC(S9xCallWAIShutdown)
  push { ip, lr }
  str RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  bl DynaWAIShutdown
  ldr RegCycles, [RegCPU, #CPU_CYCLES_OFF]
  pop { ip, pc }

  .bss
  .align 4
  .global CPU
CPU:
  .space 0x80 @ Actual: 0x50

  .global ICPU
ICPU:
  .space 0x40 @ Actual: 0x34

  .global OpenBus
OpenBus:
  .space 0x1

  @ padding so Memory.Map is at 0x100
  .space 0x3F - 0x24

  .global Memory
Memory:
  .space 0xb468