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
|
/*
* PSn00bSDK CD-ROM library (misc. functions)
* (C) 2020-2022 Lameguy64, spicyjpeg - MPL licensed
*/
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <psxetc.h>
#include <psxcd.h>
#include <hwregs_c.h>
#define DATA_SYNC_TIMEOUT 0x100000
/* Unlock command strings */
static const char *_unlock_strings[] = {
"",
"Licensed by",
"Sony",
"Computer",
"Entertainment",
"",
""
};
static const char *const _unlock_regions[] = {
"of America", // CdlRegionSCEA
"(Europe)", // CdlRegionSCEE
"World wide" // CdlRegionSCEW
};
/* Sector DMA transfer functions */
int CdGetSector(void *madr, int size) {
_sdk_validate_args(madr && (size > 0), 0);
//while (!(CD_REG(0) & (1 << 6)))
//__asm__ volatile("");
DMA_MADR(DMA_CD) = (uint32_t) madr;
DMA_BCR(DMA_CD) = size | (1 << 16);
DMA_CHCR(DMA_CD) = 0x11000000;
while (DMA_CHCR(DMA_CD) & (1 << 24))
__asm__ volatile("");
return 1;
}
int CdGetSector2(void *madr, int size) {
_sdk_validate_args(madr && (size > 0), 0);
//while (!(CD_REG(0) & (1 << 6)))
//__asm__ volatile("");
DMA_MADR(DMA_CD) = (uint32_t) madr;
DMA_BCR(DMA_CD) = size | (1 << 16);
DMA_CHCR(DMA_CD) = 0x11400100; // Transfer 1 word every 16 CPU cycles
return 1;
}
int CdDataSync(int mode) {
if (mode)
return (DMA_CHCR(DMA_CD) >> 24) & 1;
for (int i = DATA_SYNC_TIMEOUT; i; i--) {
if (!(DMA_CHCR(DMA_CD) & (1 << 24)))
return 0;
}
_sdk_log("CdDataSync() timeout, CHCR=0x%08x\n", DMA_CHCR(DMA_CD));
return -1;
}
/* LBA/MSF conversion */
CdlLOC *CdIntToPos(int i, CdlLOC *p) {
i += 150;
p->minute = itob(i / (75 * 60));
p->second = itob((i / 75) % 60);
p->sector = itob(i % 75);
return p;
}
int CdPosToInt(const CdlLOC *p) {
return (
(btoi(p->minute) * (75 * 60)) +
(btoi(p->second) * 75) +
btoi(p->sector)
) - 150;
}
/* Drive unlocking API */
CdlRegionCode CdGetRegion(void) {
uint8_t param;
uint8_t result[16];
// Firmware version C0 does not support test command 0x22 to retrieve the
// region, but it was only used in the SCPH-1000 Japanese model. Version D1
// (and possibly others?) is used in debug consoles.
// https://psx-spx.consoledev.net/cdromdrive/#19h20h-int3yymmddver
// https://psx-spx.consoledev.net/cdromdrive/#19h22h-int3for-europe
param = 0x20;
memset(result, 0, 4);
if (!CdCommand(CdlTest, ¶m, 1, result)) {
_sdk_log("failed to probe drive firmware version\n");
return CdlRegionUnknown;
}
_sdk_log("drive firmware version: 0x%02x\n", result[3]);
if (result[3] == 0xc0)
return CdlRegionSCEI;
if (result[3] >= 0xd0)
return CdlRegionDebug;
param = 0x22;
memset(result, 0, 16);
if (!CdCommand(CdlTest, ¶m, 1, result)) {
_sdk_log("failed to probe drive region\n");
return CdlRegionUnknown;
}
_sdk_log("drive region: %s\n", result);
if (!strcmp(result, "for Japan"))
return CdlRegionSCEI;
if (!strcmp(result, "for U/C"))
return CdlRegionSCEA;
if (!strcmp(result, "for Europe"))
return CdlRegionSCEE;
if (!strcmp(result, "for NETNA") || !strcmp(result, "for NETEU"))
return CdlRegionSCEW;
if (!strcmp(result, "for US/AEP"))
return CdlRegionDebug;
return CdlRegionUnknown;
}
int CdUnlock(CdlRegionCode region) {
if (region <= CdlRegionSCEI)
return 0;
if (region >= CdlRegionDebug)
return 1;
// This is by far the most efficient way to do it.
_unlock_strings[5] = _unlock_regions[region - CdlRegionSCEA];
for (int i = 0; i < 7; i++) {
uint8_t result[4];
if (!CdCommand(
0x50 + i,
_unlock_strings[i],
strlen(_unlock_strings[i]),
result
))
return 0;
if (!(result[0] & CdlStatError) || (result[1] != 0x40)) {
_sdk_log("unlock failed, status=0x%02x, code=0x%02x\n", result[0], result[1]);
return 0;
}
}
_sdk_log("unlock successful\n");
return CdCommand(CdlNop, 0, 0, 0);
}
/* Misc. functions */
int CdGetToc(CdlLOC *toc) {
_sdk_validate_args(toc, 0);
uint8_t result[4];
if (!CdCommand(CdlGetTN, 0, 0, result))
return 0;
if (CdSync(1, 0) != CdlComplete)
return 0;
int first = btoi(result[1]);
int tracks = btoi(result[2]) + 1 - first;
//assert(first == 1);
for (int i = 0; i < tracks; i++) {
uint8_t track = itob(first + i);
if (!CdCommand(CdlGetTD, &track, 1, result))
return 0;
if (CdSync(1, 0) != CdlComplete)
return 0;
toc[i].minute = result[1];
toc[i].second = result[2];
toc[i].sector = 0;
toc[i].track = track;
}
return tracks;
}
int CdMix(const CdlATV *vol) {
_sdk_validate_args(vol, 0);
CD_REG(0) = 2;
CD_REG(2) = vol->val0;
CD_REG(3) = vol->val1;
CD_REG(0) = 3;
CD_REG(1) = vol->val2;
CD_REG(2) = vol->val3;
CD_REG(3) = 0x20; // Unmute XA, apply volume changes
return 1;
}
|