-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathplatform.c
440 lines (357 loc) · 10.1 KB
/
platform.c
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
#include <signal.h>
#include <streams/file_stream.h>
#include "platform.h"
#define COL_MASK 15
#define SC_MASK 0x3F
#define SC_CDG_COMMAND 0x09
#define GP_RGB24(r, g, b) (((((r >> 3)) & 0x1f) << 11) | ((((g >> 3)) & 0x1f) << 6) | ((((b >> 3)) & 0x1f) << 1))
#define CDG_Pixel(x, y) CDG_screenBuffer[(240 * (x)) + (240 - (y))]
#define CDG_pal_Pixel(x, y, c) CDG_pal_screenBuffer[(x) * xPitch0 + (y) * yPitch0] = palette[c]
typedef struct {
unsigned char command;
unsigned char instruction;
unsigned char parityQ[2];
unsigned char data[16];
unsigned char parityP[4];
} SUBCODE;
/* Forward declarations */
RFILE* rfopen(const char *path, const char *mode);
int rfclose(RFILE* stream);
int64_t rfread(void* buffer,
size_t elem_size, size_t elem_count, RFILE* stream);
/* Variables */
int save, load;
int xPitch0, yPitch0;
u8 *CDG_screenBuffer;
u16 *CDG_pal_screenBuffer;
static RFILE *fp;
u16 palette[16];
int pos_cdg;
int firsttime;
int play;
int stop;
int action;
int pauseCDG;
int pause_pos;
int cdg_refresh;
#ifdef ABGR1555
#define RGB15(R, G, B) ((((B) & 0xF8) << 7) | (((G) & 0xF8) << 2) | (((R) & 0xF8) >> 3))
#else
#define RGB15(R, G, B) ((((R) & 0xF8) << 8) | (((G) & 0xFC) << 3) | (((B) & 0xF8) >> 3))
#endif
// ---
void CDG_Handler(SUBCODE *subCode);
void CDG_MemPreset(unsigned char *data);
void CDG_BorderPreset(unsigned char *data);
void CDG_TileBlock(unsigned char *data);
void GpSetPaletteEntry(u8 i, u8 r, u8 g, u8 b);
void CDG_LoadCLUT(unsigned char *data, short first);
void CDG_TileBlockXOR(unsigned char *data);
void CDG_SetTransparentColor(unsigned char *data);
void CDG_Reset(void);
// ---
void GpSetPaletteEntry(u8 i, u8 r, u8 g, u8 b)
{
palette[i] = RGB15(r, g, b);
cdg_refresh = 1;
}
void CDGUnload(void)
{
if (fp)
rfclose(fp);
fp = NULL;
}
void CDGLoad(const char *filename)
{
int n;
firsttime = 1;
for (n = 0; n < 16; n++)
GpSetPaletteEntry(n, 0, 0, 0);
CDG_screenBuffer = (unsigned char *)malloc(76800);
memset(CDG_screenBuffer, 0, 76800);
cdg_refresh = 0;
pos_cdg = 0;
pauseCDG = 0;
save = 0;
load = 0;
action = 0;
fp = rfopen(filename, "rb");
}
void getFrame(u16 *frame, int pos_mp3, int fps)
{
SUBCODE subCode;
unsigned long m_size;
int n;
int cont = 1;
if (pauseCDG == 1)
cont = 0;
if (!fp)
cont = 0;
if (cont)
{
int until;
if (firsttime == 1)
{
memset(frame, 0, 320 * 240 * 2);
// [backbuffer FillRect:NULL dwColor:RGB(0, 0, 0) dwFlags:0 GDFillRectFx:NULL];
firsttime = 0;
}
xPitch0 = 1;
yPitch0 = 320;
CDG_pal_screenBuffer = frame;
#if 0
if (pos_mp3 == -1) {
// [self Shutdown];
}
#endif
/*
* int old_pos_cdg; // pos_cdg en 1000/300 ms
* old_pos_cdg = pos_cdg;
*/
until = 300 / fps;
if ((pos_mp3 * 3 - pos_cdg * 10) > 300) // Plus d'une seconde de decalage
until = (pos_mp3 * 3 - pos_cdg * 10) / 10;
if ((pos_mp3 * 3 - pos_cdg * 10) < -300) // Plus d'une seconde de decalage
until = 0;
for (n = 0; n < until; n++)
{
pos_cdg++;
m_size = rfread(&subCode, 1, sizeof(SUBCODE), fp);
if (m_size != 0)
{
if ((subCode.command & SC_MASK) == SC_CDG_COMMAND)
CDG_Handler(&subCode);
}
}
}
} /* getFrame */
void CDG_Handler(SUBCODE *subCode)
{
static int p = 0;
switch (subCode->instruction & SC_MASK) {
case 1: // Memory Preset
CDG_MemPreset(subCode->data);
break;
case 2: // Border Preset
CDG_BorderPreset(subCode->data);
break;
case 6: // Tile Block (Normal)
CDG_TileBlock(subCode->data);
break;
case 30: // Load Color Table (entries 0-7)
CDG_LoadCLUT(subCode->data, 0);
break;
case 31: // Load Color Table (entries 8-15)
CDG_LoadCLUT(subCode->data, 8);
break;
case 38: // Tile Block (XOR)
CDG_TileBlockXOR(subCode->data);
break;
case 28: // Define Transparent Color
CDG_SetTransparentColor(subCode->data);
break;
// Presumably, this is not very useful unless we do
// video overlays in a future version.
case 20: // Scroll Preset
// I have no idea how this is supposed to work.. I
// need to find a CD+G subcode dump that utilizes
// this feature.
case 24: // Scroll Copy
// Ditto on the above Scroll Preset comment
default:
subCode->instruction = 0;
break;
} /* switch */
p++;
if (p == 50) {
p = 0;
if (cdg_refresh == 1) {
CDG_Reset();
cdg_refresh = 0;
}
}
}
void CDG_MemPreset(unsigned char *data)
{
int color = data[0] & COL_MASK;
short repeat = data[1] & SC_MASK;
if (repeat == 0)
{
int oldpos, pos;
int x, y, j;
u16 c = palette[color];
for (j = 240 * 10; j < 240 * 310; j += 240)
{
for (y = 12; y < 228; y++)
CDG_screenBuffer[j + y] = color;
}
pos = 10 * xPitch0 + 12 * yPitch0;
for (x = 10; x < 310; x++)
{
oldpos = pos;
for (y = 12; y < 228; y++)
{
CDG_pal_screenBuffer[pos] = c;
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
}
} /* CDG_MemPreset */
void CDG_BorderPreset(unsigned char *data)
{
int i, j;
u16 c;
int oldpos, pos;
int x, y;
unsigned char color = data[0] & COL_MASK;
for (i = 0; i < 2400; i++)
CDG_screenBuffer[i] = color;
for (j = 2400; j < 74400; j += 240)
{
for (y = 228; y < 240; y++)
CDG_screenBuffer[j + y] = color;
for (y = 0; y < 12; y++)
CDG_screenBuffer[j + y] = color;
}
for (i = 74400; i < 76800; i++)
CDG_screenBuffer[i] = color;
c = palette[color];
// Haut
pos = 0 * xPitch0 + 0 * yPitch0;
for (x = 0; x < 320; x++)
{
oldpos = pos;
for (y = 0; y < 12; y++)
{
CDG_pal_screenBuffer[pos] = c;
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
// Bas
pos = 0 * xPitch0 + 228 * yPitch0;
for (x = 0; x < 320; x++) {
oldpos = pos;
for (y = 228; y < 240; y++) {
CDG_pal_screenBuffer[pos] = c;
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
// Gauche
pos = 0 * xPitch0 + 12 * yPitch0;
for (x = 0; x < 10; x++) {
oldpos = pos;
for (y = 12; y < 228; y++) {
CDG_pal_screenBuffer[pos] = c;
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
// Droit
pos = 310 * xPitch0 + 12 * yPitch0;
for (x = 310; x < 320; x++) {
oldpos = pos;
for (y = 12; y < 228; y++) {
CDG_pal_screenBuffer[pos] = c;
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
}
void CDG_TileBlock(unsigned char *data)
{
unsigned char color0 = data[0] & COL_MASK;
unsigned char color1 = data[1] & COL_MASK;
unsigned char c;
short row = data[2] & 0x1F;
short col = data[3] & 0x3F;
int x_pos = (col * 6) + 10;
int y_pos = (row * 12) + 12;
int y;
for (y = 0; y < 12; y++) {
c = data[y + 4] & 0x20 ? color1 : color0;
CDG_Pixel(x_pos, y_pos + y) = c;
CDG_pal_Pixel(x_pos, y_pos + y, c);
c = data[y + 4] & 0x10 ? color1 : color0;
CDG_Pixel(x_pos + 1, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 1, y_pos + y, c);
c = data[y + 4] & 0x08 ? color1 : color0;
CDG_Pixel(x_pos + 2, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 2, y_pos + y, c);
c = data[y + 4] & 0x04 ? color1 : color0;
CDG_Pixel(x_pos + 3, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 3, y_pos + y, c);
c = data[y + 4] & 0x02 ? color1 : color0;
CDG_Pixel(x_pos + 4, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 4, y_pos + y, c);
c = data[y + 4] & 0x01 ? color1 : color0;
CDG_Pixel(x_pos + 5, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 5, y_pos + y, c);
}
}
void CDG_LoadCLUT(unsigned char *data, short first)
{
int i;
int j = 0;
for (i = 0; i < 8; i++)
{
unsigned char r = ((data[j] & SC_MASK) >> 2) * 17;
unsigned char g = (((data[j] & 0x03) << 2) | ((data[j + 1] & SC_MASK) >> 4)) * 17;
unsigned char b = (data[j + 1] & 0x0F) * 17;
j += 2;
GpSetPaletteEntry(i + first, r, g, b);
}
}
void CDG_TileBlockXOR(unsigned char *data)
{
unsigned char c;
unsigned char color0 = data[0] & COL_MASK;
unsigned char color1 = data[1] & COL_MASK;
short row = data[2] & 0x1F;
short col = data[3] & 0x3F;
int x_pos = (col * 6) + 10;
int y_pos = (row * 12) + 12;
int y;
for (y = 0; y < 12; y++)
{
c = CDG_Pixel(x_pos, y_pos + y) ^ (data[y + 4] & 0x20 ? color1 : color0);
CDG_Pixel(x_pos, y_pos + y) = c;
CDG_pal_Pixel(x_pos, y_pos + y, c);
c = CDG_Pixel(x_pos + 1, y_pos + y) ^ (data[y + 4] & 0x10 ? color1 : color0);
CDG_Pixel(x_pos + 1, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 1, y_pos + y, c);
c = CDG_Pixel(x_pos + 2, y_pos + y) ^ (data[y + 4] & 0x08 ? color1 : color0);
CDG_Pixel(x_pos + 2, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 2, y_pos + y, c);
c = CDG_Pixel(x_pos + 3, y_pos + y) ^ (data[y + 4] & 0x04 ? color1 : color0);
CDG_Pixel(x_pos + 3, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 3, y_pos + y, c);
c = CDG_Pixel(x_pos + 4, y_pos + y) ^ (data[y + 4] & 0x02 ? color1 : color0);
CDG_Pixel(x_pos + 4, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 4, y_pos + y, c);
c = CDG_Pixel(x_pos + 5, y_pos + y) ^ (data[y + 4] & 0x01 ? color1 : color0);
CDG_Pixel(x_pos + 5, y_pos + y) = c;
CDG_pal_Pixel(x_pos + 5, y_pos + y, c);
}
}
void CDG_SetTransparentColor(unsigned char *data)
{
}
void CDG_Reset(void)
{
int oldpos;
int x, y, j;
int pos = 10 * xPitch0 + 12 * yPitch0;
for (x = 10, j = 240 * 10; x < 310; x++, j += 240)
{
oldpos = pos;
for (y = 12; y < 228; y++)
{
CDG_pal_screenBuffer[pos] = palette[CDG_screenBuffer[j + (240 - y)]];
pos += yPitch0;
}
pos = oldpos + xPitch0;
}
}