summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDBios.c
blob: 0a1fb45ba266e9909dcf9fc09c7ff957dea59135 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#include <dolphin/card.h>
#include <dolphin/dsp.h>
#include <dolphin/dvd.h>
#include <dolphin/os.h>

#include <dolphin/CARDPriv.h>

const char* __CARDVersion =
    "<< Dolphin SDK - CARD\trelease build: Sep  5 2002 05:35:20 (0x2301) >>";

CARDControl __CARDBlock[2];
DVDDiskID __CARDDiskNone;

static u16 __CARDEncode;

s32 __CARDReadStatus(s32 chan, u8* status);
s32 __CARDClearStatus(s32 chan);
void __CARDSetDiskID(const DVDDiskID* id);
static s32 Retry(s32 chan);

BOOL OnReset(BOOL f);
static OSResetFunctionInfo ResetFunctionInfo = {OnReset, 127};

void __CARDDefaultApiCallback(s32 chan, s32 result) {}

void __CARDExtHandler(s32 chan, OSContext* context) {
  CARDControl* card;
  CARDCallback callback;

  card = &__CARDBlock[chan];
  if (card->attached) {
    card->attached = FALSE;
    EXISetExiCallback(chan, 0);
    OSCancelAlarm(&card->alarm);
    callback = card->exiCallback;

    if (callback) {
      card->exiCallback = 0;
      callback(chan, CARD_RESULT_NOCARD);
    }

    if (card->result != CARD_RESULT_BUSY) {
      card->result = CARD_RESULT_NOCARD;
    }

    callback = card->extCallback;
    if (callback && CARD_MAX_MOUNT_STEP <= card->mountStep) {
      card->extCallback = 0;
      callback(chan, CARD_RESULT_NOCARD);
    }
  }
}

void __CARDExiHandler(s32 chan, OSContext* context) {
  CARDControl* card;
  CARDCallback callback;
  u8 status;
  s32 result;

  card = &__CARDBlock[chan];

  OSCancelAlarm(&card->alarm);

  if (!card->attached) {
    return;
  }

  if (!EXILock(chan, 0, 0)) {
    result = CARD_RESULT_FATAL_ERROR;
    goto fatal;
  }

  if ((result = __CARDReadStatus(chan, &status)) < 0 || (result = __CARDClearStatus(chan)) < 0) {
    goto error;
  }

  if ((result = (status & 0x18) ? CARD_RESULT_IOERROR : CARD_RESULT_READY) == CARD_RESULT_IOERROR &&
      --card->retry > 0) {
    result = Retry(chan);
    if (result >= 0) {
      return;
    }
    goto fatal;
  }

error:
  EXIUnlock(chan);

fatal:
  callback = card->exiCallback;
  if (callback) {
    card->exiCallback = 0;
    callback(chan, result);
  }
}

void __CARDTxHandler(s32 chan, OSContext* context) {
  CARDControl* card;
  CARDCallback callback;
  BOOL err;

  card = &__CARDBlock[chan];
  err = !EXIDeselect(chan);
  EXIUnlock(chan);
  callback = card->txCallback;
  if (callback) {
    card->txCallback = 0;
    callback(chan, (!err && EXIProbe(chan)) ? CARD_RESULT_READY : CARD_RESULT_NOCARD);
  }
}

void __CARDUnlockedHandler(s32 chan, OSContext* context) {
  CARDControl* card;
  CARDCallback callback;

  card = &__CARDBlock[chan];
  callback = card->unlockCallback;
  if (callback) {
    card->unlockCallback = 0;
    callback(chan, EXIProbe(chan) ? CARD_RESULT_UNLOCKED : CARD_RESULT_NOCARD);
  }
}

s32 __CARDEnableInterrupt(s32 chan, BOOL enable) {
  BOOL err;
  u32 cmd;

  if (!EXISelect(chan, 0, 4)) {
    return CARD_RESULT_NOCARD;
  }

  cmd = enable ? 0x81010000 : 0x81000000;
  err = FALSE;
  err |= !EXIImm(chan, &cmd, 2, 1, NULL);
  err |= !EXISync(chan);
  err |= !EXIDeselect(chan);
  return err ? CARD_RESULT_NOCARD : CARD_RESULT_READY;
}

s32 __CARDReadStatus(s32 chan, u8* status) {
  BOOL err;
  u32 cmd;

  if (!EXISelect(chan, 0, 4)) {
    return CARD_RESULT_NOCARD;
  }

  cmd = 0x83000000;
  err = FALSE;
  err |= !EXIImm(chan, &cmd, 2, 1, NULL);
  err |= !EXISync(chan);
  err |= !EXIImm(chan, status, 1, 0, NULL);
  err |= !EXISync(chan);
  err |= !EXIDeselect(chan);
  return err ? CARD_RESULT_NOCARD : CARD_RESULT_READY;
}

s32 __CARDClearStatus(s32 chan) {
  BOOL err;
  u32 cmd;

  if (!EXISelect(chan, 0, 4)) {
    return CARD_RESULT_NOCARD;
  }

  cmd = 0x89000000;
  err = FALSE;
  err |= !EXIImm(chan, &cmd, 1, 1, 0);
  err |= !EXISync(chan);
  err |= !EXIDeselect(chan);

  return err ? CARD_RESULT_NOCARD : CARD_RESULT_READY;
}

static void TimeoutHandler(OSAlarm* alarm, OSContext* context) {
  s32 chan;
  CARDControl* card;
  CARDCallback callback;
  for (chan = 0; chan < 2; ++chan) {
    card = &__CARDBlock[chan];
    if (alarm == &card->alarm) {
      break;
    }
  }

  if (!card->attached) {
    return;
  }

  EXISetExiCallback(chan, NULL);
  callback = card->exiCallback;
  if (callback) {
    card->exiCallback = 0;
    callback(chan, CARD_RESULT_IOERROR);
  }
}

static void SetupTimeoutAlarm(CARDControl* card) {
  OSCancelAlarm(&card->alarm);
  switch (card->cmd[0]) {
  case 0xF2:
    OSSetAlarm(&card->alarm, OSMillisecondsToTicks(100), TimeoutHandler);
    break;
  case 0xF3:
    break;
  case 0xF4:
  case 0xF1:
    OSSetAlarm(&card->alarm, OSSecondsToTicks((OSTime)2) * (card->sectorSize / 0x2000),
               TimeoutHandler);
    break;
  }
}

static s32 Retry(s32 chan) {
  CARDControl* card;
  card = &__CARDBlock[chan];

  if (!EXISelect(chan, 0, 4)) {
    EXIUnlock(chan);
    return CARD_RESULT_NOCARD;
  }

  SetupTimeoutAlarm(card);

  if (!EXIImmEx(chan, card->cmd, card->cmdlen, 1)) {
    EXIDeselect(chan);
    EXIUnlock(chan);
    return CARD_RESULT_NOCARD;
  }

  if (card->cmd[0] == 0x52 &&
      !EXIImmEx(chan, (u8*)card->workArea + sizeof(CARDID), card->latency, 1)) {
    EXIDeselect(chan);
    EXIUnlock(chan);
    return CARD_RESULT_NOCARD;
  }

  if (card->mode == 0xffffffff) {
    EXIDeselect(chan);
    EXIUnlock(chan);
    return CARD_RESULT_READY;
  }

  if (!EXIDma(chan, card->buffer, (s32)((card->cmd[0] == 0x52) ? 512 : 128), card->mode,
              __CARDTxHandler)) {
    EXIDeselect(chan);
    EXIUnlock(chan);
    return CARD_RESULT_NOCARD;
  }

  return CARD_RESULT_READY;
}

static void UnlockedCallback(s32 chan, s32 result) {
  CARDCallback callback;
  CARDControl* card;

  card = &__CARDBlock[chan];
  if (result >= 0) {
    card->unlockCallback = UnlockedCallback;
    if (!EXILock(chan, 0, __CARDUnlockedHandler)) {
      result = CARD_RESULT_READY;
    } else {
      card->unlockCallback = 0;
      result = Retry(chan);
    }
  }

  if (result < 0) {
    switch (card->cmd[0]) {
    case 0x52:
      callback = card->txCallback;
      if (callback) {
        card->txCallback = 0;
        callback(chan, result);
      }

      break;
    case 0xF2:
    case 0xF4:
    case 0xF1:
      callback = card->exiCallback;
      if (callback) {
        card->exiCallback = 0;
        callback(chan, result);
      }
      break;
    }
  }
}

static s32 __CARDStart(s32 chan, CARDCallback txCallback, CARDCallback exiCallback) {
  BOOL enabled;
  CARDControl* card;
  s32 result;

  enabled = OSDisableInterrupts();

  card = &__CARDBlock[chan];
  if (!card->attached) {
    result = CARD_RESULT_NOCARD;
  } else {

    if (txCallback) {
      card->txCallback = txCallback;
    }
    if (exiCallback) {
      card->exiCallback = exiCallback;
    }
    card->unlockCallback = UnlockedCallback;
    if (!EXILock(chan, 0, __CARDUnlockedHandler)) {
      result = CARD_RESULT_BUSY;
    } else {
      card->unlockCallback = 0;

      if (!EXISelect(chan, 0, 4)) {
        EXIUnlock(chan);
        result = CARD_RESULT_NOCARD;
      } else {
        SetupTimeoutAlarm(card);
        result = CARD_RESULT_READY;
      }
    }
  }

  OSRestoreInterrupts(enabled);
  return result;
}

#define AD1(x) ((u8)(((x) >> 17) & 0x7f))
#define AD1EX(x) ((u8)(AD1(x) | 0x80));
#define AD2(x) ((u8)(((x) >> 9) & 0xff))
#define AD3(x) ((u8)(((x) >> 7) & 0x03))
#define BA(x) ((u8)((x)&0x7f))

s32 __CARDReadSegment(s32 chan, CARDCallback callback) {
  CARDControl* card;
  s32 result;

  card = &__CARDBlock[chan];
  card->cmd[0] = 0x52;
  card->cmd[1] = AD1(card->addr);
  card->cmd[2] = AD2(card->addr);
  card->cmd[3] = AD3(card->addr);
  card->cmd[4] = BA(card->addr);
  card->cmdlen = 5;
  card->mode = 0;
  card->retry = 0;

  result = __CARDStart(chan, callback, 0);
  if (result == CARD_RESULT_BUSY) {
    result = CARD_RESULT_READY;
  } else if (result >= 0) {
    if (!EXIImmEx(chan, card->cmd, card->cmdlen, 1) ||
        !EXIImmEx(chan, (u8*)card->workArea + sizeof(CARDID), card->latency,
                  1) || // XXX use DMA if possible
        !EXIDma(chan, card->buffer, 512, card->mode, __CARDTxHandler)) {
      card->txCallback = 0;
      EXIDeselect(chan);
      EXIUnlock(chan);
      result = CARD_RESULT_NOCARD;
    } else {
      result = CARD_RESULT_READY;
    }
  }
  return result;
}

s32 __CARDWritePage(s32 chan, CARDCallback callback) {
  CARDControl* card;
  s32 result;

  card = &__CARDBlock[chan];
  card->cmd[0] = 0xF2;
  card->cmd[1] = AD1(card->addr);
  card->cmd[2] = AD2(card->addr);
  card->cmd[3] = AD3(card->addr);
  card->cmd[4] = BA(card->addr);
  card->cmdlen = 5;
  card->mode = 1;
  card->retry = 3;

  result = __CARDStart(chan, 0, callback);
  if (result == CARD_RESULT_BUSY) {
    result = CARD_RESULT_READY;
  } else if (result >= 0) {
    if (!EXIImmEx(chan, card->cmd, card->cmdlen, 1) ||
        !EXIDma(chan, card->buffer, 128, card->mode, __CARDTxHandler)) {
      card->exiCallback = 0;
      EXIDeselect(chan);
      EXIUnlock(chan);
      result = CARD_RESULT_NOCARD;
    } else {
      result = CARD_RESULT_READY;
    }
  }
  return result;
}

#ifdef FULL_FRANK
/* TODO: Needs frank fix for disconnected stack epilogue */
s32 __CARDEraseSector(s32 chan, u32 addr, CARDCallback callback) {
  CARDControl* card;
  s32 result;

  card = &__CARDBlock[chan];
  card->cmd[0] = 0xF1;
  card->cmd[1] = AD1(addr);
  card->cmd[2] = AD2(addr);
  card->cmdlen = 3;
  card->mode = -1;
  card->retry = 3;

  result = __CARDStart(chan, 0, callback);

  if (result == CARD_RESULT_BUSY) {
    result = CARD_RESULT_READY;
  } else if (result >= 0) {
    if (!EXIImmEx(chan, card->cmd, card->cmdlen, 1)) {
      card->exiCallback = NULL;
      result = CARD_RESULT_NOCARD;
    } else {
      result = CARD_RESULT_READY;
    }

    EXIDeselect(chan);
    EXIUnlock(chan);
  }
  return result;
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm s32 __CARDEraseSector(s32 chan, u32 addr, CARDCallback callback) {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x28(r1)
  stw r31, 0x24(r1)
  stw r30, 0x20(r1)
  stw r29, 0x1c(r1)
  addi r29, r3, 0
  mulli r6, r29, 0x110
  lis r3, __CARDBlock@ha
  addi r0, r3, __CARDBlock@l
  add r31, r0, r6
  li r0, 0xf1
  stb r0, 0x94(r31)
  rlwinm r3, r4, 0xf, 0x19, 0x1f
  rlwinm r0, r4, 0x17, 0x18, 0x1f
  stb r3, 0x95(r31)
  li r6, 3
  addi r3, r29, 0
  stb r0, 0x96(r31)
  li r0, -1
  li r4, 0
  stw r6, 0xa0(r31)
  stw r0, 0xa4(r31)
  stw r6, 0xa8(r31)
  bl __CARDStart
  addi r30, r3, 0
  cmpwi r30, -1
  bne lbl_803B8C4C
  li r30, 0
  b lbl_803B8C94
lbl_803B8C4C:
  cmpwi r30, 0
  blt lbl_803B8C94
  lwz r5, 0xa0(r31)
  addi r3, r29, 0
  addi r4, r31, 0x94
  li r6, 1
  bl EXIImmEx
  cmpwi r3, 0
  bne lbl_803B8C80
  li r0, 0
  stw r0, 0xcc(r31)
  li r30, -3
  b lbl_803B8C84
lbl_803B8C80:
  li r30, 0
lbl_803B8C84:
  mr r3, r29
  bl EXIDeselect
  mr r3, r29
  bl EXIUnlock
lbl_803B8C94:
  mr r3, r30
  lwz r0, 0x2c(r1)
  lwz r31, 0x24(r1)
  lwz r30, 0x20(r1)
  lwz r29, 0x1c(r1)
  addi r1, r1, 0x28
  mtlr r0
  blr
}
/* clang-format on */
#pragma pop
#endif 
void CARDInit(void) {
  int chan;

  if (__CARDBlock[0].diskID && __CARDBlock[1].diskID) {
    return;
  }

  __CARDEncode = OSGetFontEncode();

  OSRegisterVersion(__CARDVersion);

  DSPInit();
  OSInitAlarm();

  for (chan = 0; chan < 2; ++chan) {
    CARDControl* card = &__CARDBlock[chan];

    card->result = CARD_RESULT_NOCARD;
    OSInitThreadQueue(&card->threadQueue);
    OSCreateAlarm(&card->alarm);
  }
  __CARDSetDiskID((DVDDiskID*)OSPhysicalToCached(0x0));

  OSRegisterResetFunction(&ResetFunctionInfo);
}

u16 __CARDGetFontEncode() { return __CARDEncode; }

void __CARDSetDiskID(const DVDDiskID* id) {
  __CARDBlock[0].diskID = id ? id : &__CARDDiskNone;
  __CARDBlock[1].diskID = id ? id : &__CARDDiskNone;
}

s32 __CARDGetControlBlock(s32 chan, CARDControl** pcard) {
  BOOL enabled;
  s32 result;
  CARDControl* card;

  card = &__CARDBlock[chan];
  if (chan < 0 || chan >= 2 || card->diskID == NULL) {
    return CARD_RESULT_FATAL_ERROR;
  }

  enabled = OSDisableInterrupts();
  if (!card->attached) {
    result = CARD_RESULT_NOCARD;
  } else if (card->result == CARD_RESULT_BUSY) {
    result = CARD_RESULT_BUSY;
  } else {
    card->result = CARD_RESULT_BUSY;
    result = CARD_RESULT_READY;
    card->apiCallback = 0;
    *pcard = card;
  }
  OSRestoreInterrupts(enabled);
  return result;
}

#ifdef FULL_FRANK
/* TODO: Needs frank fix for disconnected stack epilogue */
s32 __CARDPutControlBlock(CARDControl* card, s32 result) {
  BOOL enabled;

  enabled = OSDisableInterrupts();
  if (card->attached) {
    card->result = result;
  } else if (card->result == CARD_RESULT_BUSY) {
    card->result = result;
  }
  OSRestoreInterrupts(enabled);
  return result;
}
#else
#pragma push
/* clang-format off */
#pragma optimization_level 0
#pragma optimizewithasm off
asm s32 __CARDPutControlBlock(CARDControl* card, s32 result) {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x18(r1)
  stw r31, 0x14(r1)
  addi r31, r4, 0
  stw r30, 0x10(r1)
  addi r30, r3, 0
  bl OSDisableInterrupts
  lwz r0, 0(r30)
  cmpwi r0, 0
  beq lbl_803B8E8C
  stw r31, 4(r30)
  b lbl_803B8E9C
lbl_803B8E8C:
  lwz r0, 4(r30)
  cmpwi r0, -1
  bne lbl_803B8E9C
  stw r31, 4(r30)
lbl_803B8E9C:
  bl OSRestoreInterrupts
  mr r3, r31
  lwz r0, 0x1c(r1)
  lwz r31, 0x14(r1)
  lwz r30, 0x10(r1)
  addi r1, r1, 0x18
  mtlr r0
  blr
}
/* clang-format on */
#pragma pop
#endif

s32 CARDGetResultCode(s32 chan) {
  CARDControl* card;
  if (chan < 0 || chan >= 2) {
    return CARD_RESULT_FATAL_ERROR;
  }
  card = &__CARDBlock[chan];
  return card->result;
}

#ifdef FULL_FRANK
s32 CARDFreeBlocks(s32 chan, s32* byteNotUsed, s32* filesNotUsed) {
  CARDControl* card;
  s32 result;
  u16* fat;
  CARDDir* dir;
  CARDDir* ent;
  u16 fileNo;

  result = __CARDGetControlBlock(chan, &card);
  if (result < 0) {
    return result;
  }

  fat = __CARDGetFatBlock(card);
  dir = __CARDGetDirBlock(card);
  if (fat == 0 || dir == 0) {
    return __CARDPutControlBlock(card, CARD_RESULT_BROKEN);
  }

  if (byteNotUsed) {
    *byteNotUsed = (s32)(card->sectorSize * fat[CARD_FAT_FREEBLOCKS]);
  }

  if (filesNotUsed) {
    *filesNotUsed = 0;
    for (fileNo = 0; fileNo < CARD_MAX_FILE; fileNo++) {
      ent = &dir[fileNo];
      if (ent->fileName[0] == 0xff) {
        ++*filesNotUsed;
      }
    }
  }

  return __CARDPutControlBlock(card, CARD_RESULT_READY);
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm s32 CARDFreeBlocks(s32 chan, s32* byteNotUsed, s32* filesNotUsed) {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x30(r1)
  stw r31, 0x2c(r1)
  addi r31, r5, 0
  stw r30, 0x28(r1)
  stw r29, 0x24(r1)
  addi r29, r4, 0
  addi r4, r1, 0x18
  bl __CARDGetControlBlock
  cmpwi r3, 0
  bge lbl_803B8F20
  b lbl_803B9020
lbl_803B8F20:
  lwz r3, 0x18(r1)
  bl __CARDGetFatBlock
  mr r30, r3
  lwz r3, 0x18(r1)
  bl __CARDGetDirBlock
  cmplwi r30, 0
  beq lbl_803B8F44
  cmplwi r3, 0
  bne lbl_803B8F84
lbl_803B8F44:
  lwz r30, 0x18(r1)
  bl OSDisableInterrupts
  lwz r0, 0(r30)
  cmpwi r0, 0
  beq lbl_803B8F64
  li r0, -6
  stw r0, 4(r30)
  b lbl_803B8F78
lbl_803B8F64:
  lwz r0, 4(r30)
  cmpwi r0, -1
  bne lbl_803B8F78
  li r0, -6
  stw r0, 4(r30)
lbl_803B8F78:
  bl OSRestoreInterrupts
  li r3, -6
  b lbl_803B9020
lbl_803B8F84:
  cmplwi r29, 0
  beq lbl_803B8FA0
  lwz r4, 0x18(r1)
  lhz r0, 6(r30)
  lwz r4, 0xc(r4)
  mullw r0, r4, r0
  stw r0, 0(r29)
lbl_803B8FA0:
  cmplwi r31, 0
  beq lbl_803B8FE4
  li r0, 0
  stw r0, 0(r31)
  li r5, 0
  b lbl_803B8FD8
lbl_803B8FB8:
  lbz r0, 8(r3)
  cmplwi r0, 0xff
  bne lbl_803B8FD0
  lwz r4, 0(r31)
  addi r0, r4, 1
  stw r0, 0(r31)
lbl_803B8FD0:
  addi r3, r3, 0x40
  addi r5, r5, 1
lbl_803B8FD8:
  clrlwi r0, r5, 0x10
  cmplwi r0, 0x7f
  blt lbl_803B8FB8
lbl_803B8FE4:
  lwz r30, 0x18(r1)
  bl OSDisableInterrupts
  lwz r0, 0(r30)
  cmpwi r0, 0
  beq lbl_803B9004
  li r0, 0
  stw r0, 4(r30)
  b lbl_803B9018
lbl_803B9004:
  lwz r0, 4(r30)
  cmpwi r0, -1
  bne lbl_803B9018
  li r0, 0
  stw r0, 4(r30)
lbl_803B9018:
  bl OSRestoreInterrupts
  li r3, 0
lbl_803B9020:
  lwz r0, 0x34(r1)
  lwz r31, 0x2c(r1)
  lwz r30, 0x28(r1)
  lwz r29, 0x24(r1)
  addi r1, r1, 0x30
  mtlr r0
  blr
}
/* clang-format on */
#pragma pop
#endif

static BOOL OnReset(BOOL f) {
  if (!f) {
    if (CARDUnmount(0) == CARD_RESULT_BUSY || CARDUnmount(1) == CARD_RESULT_BUSY) {
      return FALSE;
    }
  }

  return TRUE;
}