summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDRdwr.c
blob: 2f41be755516c22cc5267482bf77a5d1f42fd50e (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
#include <dolphin/card.h>
#include <dolphin/dsp.h>
#include <dolphin/dvd.h>
#include <dolphin/os.h>

#include <dolphin/CARDPriv.h>

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

  card = &__CARDBlock[chan];
  if (result < 0) {
    goto error;
  }

  card->xferred += CARD_SEG_SIZE;

  card->addr += CARD_SEG_SIZE;
  (u8*)card->buffer += CARD_SEG_SIZE;
  if (--card->repeat <= 0) {
    goto error;
  }

  result = __CARDReadSegment(chan, BlockReadCallback);
  if (result < 0) {
    goto error;
  }
  return;

error:
  if (card->apiCallback == 0) {
    __CARDPutControlBlock(card, result);
  }
  callback = card->xferCallback;
  if (callback) {
    card->xferCallback = 0;
    callback(chan, result);
  }
}

s32 __CARDRead(s32 chan, u32 addr, s32 length, void* dst, CARDCallback callback) {
  CARDControl* card;
  card = &__CARDBlock[chan];
  if (!card->attached) {
    return CARD_RESULT_NOCARD;
  }

  card->xferCallback = callback;
  card->repeat = (int)(length / CARD_SEG_SIZE);
  card->addr = addr;
  card->buffer = dst;

  return __CARDReadSegment(chan, BlockReadCallback);
}

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

  card = &__CARDBlock[chan];
  if (result < 0) {
    goto error;
  }

  card->xferred += CARD_PAGE_SIZE;

  card->addr += CARD_PAGE_SIZE;
  (u8*)card->buffer += CARD_PAGE_SIZE;
  if (--card->repeat <= 0) {
    goto error;
  }

  result = __CARDWritePage(chan, BlockWriteCallback);
  if (result < 0) {
    goto error;
  }
  return;

error:
  if (card->apiCallback == 0) {
    __CARDPutControlBlock(card, result);
  }
  callback = card->xferCallback;
  if (callback) {
    card->xferCallback = 0;
    callback(chan, result);
  }
}

s32 __CARDWrite(s32 chan, u32 addr, s32 length, void* dst, CARDCallback callback) {
  CARDControl* card;
  card = &__CARDBlock[chan];
  if (!card->attached) {
    return CARD_RESULT_NOCARD;
  }

  card->xferCallback = callback;
  card->repeat = (int)(length / CARD_PAGE_SIZE);
  card->addr = addr;
  card->buffer = dst;

  return __CARDWritePage(chan, BlockWriteCallback);
}