summaryrefslogtreecommitdiff
path: root/src/Dolphin/GBA/GBA.c
blob: bb89f2e05c9f679c9e6872b204974895def37201 (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
#include "dolphin/GBAPriv.h"

static GBASecParams SecParams[4];
GBA __GBA[4];
BOOL __GBAReset = FALSE;

static BOOL OnReset(BOOL);

static OSResetFunctionInfo ResetFunctionInfo = {
  OnReset,
  127
};

void ShortCommandProc(s32 chan) {
  GBA* gba;
  gba = &__GBA[chan];

  if (gba->result != 0) {
    return;
  }

  if (gba->dst[0] != 0 || gba->dst[1] != 4) {
    gba->result = 1;
    return;
  }

  gba->status[0] = gba->dst[2] & GBA_JSTAT_MASK;
}

void GBAInit() {
  s32 i;
  GBA* gba;
  for (i = 0; i < 4; ++i) {
    gba = &__GBA[i];
    gba->delay = OSMicrosecondsToTicks(60);
    OSInitThreadQueue(&gba->thread_queue);
    gba->param = &SecParams[i];

    // ASSERTMSG((u32) gba->param % 32 == 0)
  } 

  OSInitAlarm();
  DSPInit();
  __GBAReset = FALSE;

  OSRegisterResetFunction(&ResetFunctionInfo);
}

s32 GBAGetStatusAsync(s32 chan, u8* status, GBACallback callback) {
  GBA* gba;
  s32 ret;
  gba = &__GBA[chan];
  if (gba->callback != NULL) {
    ret = GBA_BUSY;
  } else {
    gba->command = 0;
    gba->status = status;
    gba->callback = callback;
    ret = __GBATransfer(chan, 1, 3, ShortCommandProc);
  }

  return ret;
}


s32 GBAGetStatus(s32 chan, u8* status) {
  s32 ret;
  ret = GBAGetStatusAsync(chan, status, __GBASyncCallback);

  if (ret != GBA_READY) {
    return ret;
  }

  return __GBASync(chan);
}


s32 GBAResetAsync(s32 chan, u8* status, GBACallback callback) {
  GBA* gba;
  s32 ret;
  gba = &__GBA[chan];
  if (gba->callback != NULL) {
    ret = GBA_BUSY;
  } else {
    gba->command = 0xFF;
    gba->status = status;
    gba->callback = callback;
    ret = __GBATransfer(chan, 1, 3, ShortCommandProc);
  }

  return ret;
}


s32 GBAReset(s32 chan, u8* status) {
  s32 ret;

  ret = GBAResetAsync(chan, status, __GBASyncCallback);
  if (ret != GBA_READY) {
    return ret;
  }

  return __GBASync(chan);
}

BOOL OnReset(BOOL) {
  __GBAReset = TRUE;
  return TRUE;
}