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

#include <dolphin/CARDPriv.h>

static void UpdateIconOffsets(CARDDir* ent, CARDStat* stat) {
  u32 offset;
  BOOL iconTlut;
  int i;

  offset = ent->iconAddr;
  if (offset == 0xffffffff) {
    stat->bannerFormat = 0;
    stat->iconFormat = 0;
    stat->iconSpeed = 0;
    offset = 0;
  }

  iconTlut = FALSE;
  switch (CARDGetBannerFormat(ent)) {
  case CARD_STAT_BANNER_C8:
    stat->offsetBanner = offset;
    offset += CARD_BANNER_WIDTH * CARD_BANNER_HEIGHT;
    stat->offsetBannerTlut = offset;
    offset += 2 * 256;
    break;
  case CARD_STAT_BANNER_RGB5A3:
    stat->offsetBanner = offset;
    offset += 2 * CARD_BANNER_WIDTH * CARD_BANNER_HEIGHT;
    stat->offsetBannerTlut = 0xffffffff;
    break;
  default:
    stat->offsetBanner = 0xffffffff;
    stat->offsetBannerTlut = 0xffffffff;
    break;
  }
  for (i = 0; i < CARD_ICON_MAX; ++i) {
    switch (CARDGetIconFormat(ent, i)) {
    case CARD_STAT_ICON_C8:
      stat->offsetIcon[i] = offset;
      offset += CARD_ICON_WIDTH * CARD_ICON_HEIGHT;
      iconTlut = TRUE;
      break;
    case CARD_STAT_ICON_RGB5A3:
      stat->offsetIcon[i] = offset;
      offset += 2 * CARD_ICON_WIDTH * CARD_ICON_HEIGHT;
      break;
    default:
      stat->offsetIcon[i] = 0xffffffff;
      break;
    }
  }
  if (iconTlut) {
    stat->offsetIconTlut = offset;
    offset += 2 * 256;
  } else {
    stat->offsetIconTlut = 0xffffffff;
  }
  stat->offsetData = offset;
}

s32 CARDGetStatus(s32 chan, s32 fileNo, CARDStat* stat) {
  CARDControl* card;
  CARDDir* dir;
  CARDDir* ent;
  s32 result;

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

  dir = __CARDGetDirBlock(card);
  ent = &dir[fileNo];
  result = __CARDAccess(card, ent);
  if (result == CARD_RESULT_NOPERM) {
    result = __CARDIsWritable(ent);
  }

  if (result >= 0) {
    memcpy(stat->gameName, ent->gameName, sizeof(stat->gameName));
    memcpy(stat->company, ent->company, sizeof(stat->company));
    stat->length = (u32)ent->length * card->sectorSize;
    memcpy(stat->fileName, ent->fileName, CARD_FILENAME_MAX);
    stat->time = ent->time;

    stat->bannerFormat = ent->bannerFormat;
    stat->iconAddr = ent->iconAddr;
    stat->iconFormat = ent->iconFormat;
    stat->iconSpeed = ent->iconSpeed;
    stat->commentAddr = ent->commentAddr;

    UpdateIconOffsets(ent, stat);
  }
  return __CARDPutControlBlock(card, result);
}

s32 CARDSetStatusAsync(s32 chan, s32 fileNo, CARDStat* stat, CARDCallback callback) {
  CARDControl* card;
  CARDDir* dir;
  CARDDir* ent;
  s32 result;

  if (fileNo < 0 || CARD_MAX_FILE <= fileNo ||
      (stat->iconAddr != 0xffffffff && CARD_READ_SIZE <= stat->iconAddr) ||
      (stat->commentAddr != 0xffffffff &&
       CARD_SYSTEM_BLOCK_SIZE - CARD_COMMENT_SIZE < stat->commentAddr % CARD_SYSTEM_BLOCK_SIZE)) {
    return CARD_RESULT_FATAL_ERROR;
  }
  result = __CARDGetControlBlock(chan, &card);
  if (result < 0) {
    return result;
  }

  dir = __CARDGetDirBlock(card);
  ent = &dir[fileNo];
  result = __CARDAccess(card, ent);
  if (result < 0) {
    return __CARDPutControlBlock(card, result);
  }

  ent->bannerFormat = stat->bannerFormat;
  ent->iconAddr = stat->iconAddr;
  ent->iconFormat = stat->iconFormat;
  ent->iconSpeed = stat->iconSpeed;
  ent->commentAddr = stat->commentAddr;
  UpdateIconOffsets(ent, stat);

  if (ent->iconAddr == 0xffffffff) {
    CARDSetIconSpeed(ent, 0, CARD_STAT_SPEED_FAST);
  }

  ent->time = (u32)OSTicksToSeconds(OSGetTime());
  result = __CARDUpdateDir(chan, callback);
  if (result < 0) {
    __CARDPutControlBlock(card, result);
  }
  return result;
}