-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdg.cpp
678 lines (607 loc) · 19.6 KB
/
cdg.cpp
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//***************************************************************************
//
// File: cdg.cpp
//
// Author: Isaac Brodsky
//
// Project: CD+G Deck
//
// Version: 1.0.4
//
// Date: 2012 AUGUST 6
// 2012 OCTOBER 16
// 2012 OCTOBER 29
// 2013 DECEMBER 14
//
// Copyright 2012 Isaac Brodsky. All rights reserved.
//
// Compact disc subcode graphics decoder, implementation.
//
// Compact disc subcode graphics (CD + Graphics, CD+G, subcode graphics)
// are simple graphics programs that can be embedded on audio CDs. Audio
// CDs contain subcode information interspersed along with the audio
// tracks. Normally only timing and seek information is stored in the
// subcodes, but with the extensions to the standard in IEC 60908 (2ed)
// graphics programs can be stored as well.
//
// This is a decoder from CD+G subcodes to displayable graphics.
//
// Information about this format can be found in IEC 60908 2ed.
// Some (limited) information available online at:
// http://jbum.com/cdg_revealed.html
//
//***************************************************************************
#include <iostream> // stream input
using namespace std;
#include "cdg.h"
//***************************************************************************
// Constructs a blank CDG decoder, with all colors being {00, 00, 00}, all
// pixels set to the 0th color, and the PV and PH pointers at 0.
//***************************************************************************
CDG::CDG()
{
for (int i = 0; i < CDG_NUM_COLORS; i++)
for (int channel = 0; channel < CDG_NUM_COLOR_CHANNELS; channel++)
colorTable[i][channel] = 0x00;
for (int x = 0; x < CDG_WIDTH; x++)
for (int y = 0; y < CDG_HEIGHT; y++)
putPixel(x, y, 0);
ph = pv = 0;
border = 0;
channel = 1;
}
//***************************************************************************
// Returns true if the state of the other CDG decoder is the same as this
// one. State includes graphic memory, PV and PH pointers, and the color
// table. State does not include how many subcode packets have been
// processed or any detail of how the state was changed.
//***************************************************************************
bool CDG::operator==(const CDG& other) const
{
bool equal = true;
for (int i = 0; i < CDG_NUM_COLORS; i++)
{
for (int channel = 0; channel < CDG_NUM_COLOR_CHANNELS; channel++)
{
if (colorTable[i][channel] != other.colorTable[i][channel])
{
equal = false;
break;
}
}
if (!equal)
break;
}
equal &= (ph == other.ph); // Don't bother testing if (equal)
equal &= (pv == other.pv); // it's only a few compares
equal &= (border == other.border); // Do it before checking the
// screen array since that's
if (equal) // a heavier operation and if
{ // we can skip it we should.
for (int x = 0; x < CDG_WIDTH; x++)
{
for (int y = 0; y < CDG_HEIGHT; y++)
{
if (getPixel(x, y) != other.getPixel(x, y))
{
equal = false;
break;
}
}
if (!equal)
break;
}
}
return equal;
}
//***************************************************************************
// Reads the next SubCode structure in from the given input stream.
//
// Returns true if the subcode was read successfully, false if any error
// occured (e.g. EOF)
//***************************************************************************
bool CDG::readNext(istream &in, SubCode &out)
{
bool success = true;
out.command = SubCode_Command::SCCMD_NONE;
in.read((char*)&out, sizeof(SubCode)); // attempt read
if (!in.good()) // the read failed
{
success = false;
}
return success;
}
//***************************************************************************
// Internal implementation of the LOADCLUT command. This function accepts
// a color index and a packed color (such as one from the data field
// of a LOADCLUT command.)
//
// The color is unpacked and stored in the given index in the color table.
//***************************************************************************
void CDG::loadColor(int idx, short col)
{
// jbum and the spec both have nice
// charts of the layout of col.
uint8_t r, g, b, high, low;
high = col & LOWER_6_BITS;
low = (col >> 8) & LOWER_6_BITS;
r = (high >> 2);
g = (((high & LOWER_2_BITS) << 2) | (low >> 4));
b = (low & LOWER_4_BITS);
r = r << 4;
g = g << 4;
b = b << 4;
if (idx >= 0 && idx < CDG_NUM_COLORS)
{
colorTable[idx][0] = r;
colorTable[idx][1] = g;
colorTable[idx][2] = b; //don't change 3 - alpha
// the standard doesn't say anything
} // about that.
}
//***************************************************************************
// Executes the given LOADCLUT command on this CDG decoder.
//
// LOADCLUT commands change a given half of the color palette.
//***************************************************************************
void CDG::execLoadct(const SubCode &subCode)
{
int offset = ((subCode.instruction & LOWER_6_BITS) == CDG_LOADCTHIGH) ? 8 : 0;
for (int i = 0; i < 8; i++)
loadColor(i + offset, subCode.data.clutDat.colorSpec[i]);
}
//***************************************************************************
// Executes the given TRANSPARENT command on this CDG decoder.
//
// TRANSPARENT commands set the alpha channel of all colors in the palette
// to the level given.
//***************************************************************************
void CDG::execTransparent(const SubCode &subCode)
{
for (int i = 0; i < 16; i++)
{
colorTable[i][3] =
((subCode.data.transparentDat.alphaChannel[i] & LOWER_6_BITS)
<< 2);
}
}
//***************************************************************************
// Fills the pixels from (xs, ys) to (xe, ye) with the given color.
// Inclusive of (xs, ys) and exclusive of column xe and row ye.
//***************************************************************************
void CDG::fillPixels(int xs, int ys, int xe, int ye, uint8_t color)
{
for (int x = xs; x < xe; x++)
{
for (int y = ys; y < ye; y++)
{
putPixel(x, y, color);
}
}
}
//***************************************************************************
// Sets the pixel at (x, y) to the given color, where color is an index
// to the color table.
//
// If xor is true, the new color is XORed with the previous value at the
// given location. This means the new index is XORed with the old index.
//***************************************************************************
void CDG::putPixel(int x, int y, uint8_t color, bool isXor)
{
if (x < CDG_WIDTH && x >= 0
&& y < CDG_HEIGHT && y >= 0)
{
#ifdef SHRINK_CDG
//determine new color
uint8_t newcolor;
if (isXor)
{
newcolor = getPixel(x, y);
newcolor ^= color;
}
else
{
newcolor = color;
}
//commit new color to screen
if (y % 2 == 1)
screen[x][y / 2] = (screen[x][y / 2] & LOWER_4_BITS) | (newcolor << 4);
else
screen[x][y / 2] = (screen[x][y / 2] & ~LOWER_4_BITS) | newcolor;
#else
if (isXor)
screen[x][y] ^= color;
else
screen[x][y] = color;
#endif
}
}
//***************************************************************************
// Retrieves the 32 bit (8 bit each RGBA) representation of the given color
// code.
//
// All outputs are set to 0 if the given code is invalid.
//***************************************************************************
void CDG::getColor(uint8_t code, uint8_t &r, uint8_t &g, uint8_t &b, uint8_t &a) const
{
if (code >= CDG_NUM_COLORS || code < 0)
{
r = g = b = a = 0;
}
else
{
r = colorTable[code][0];
g = colorTable[code][1];
b = colorTable[code][2];
a = colorTable[code][3];
}
}
//***************************************************************************
// Returns the color code (the index to the color table) stored at the given
// location, or 0 if the location is out of bounds.
//***************************************************************************
uint8_t CDG::getPixel(int x, int y) const
{
uint8_t ret;
if (x < CDG_WIDTH && x >= 0
&& y < CDG_HEIGHT && y >= 0)
{
#ifdef SHRINK_CDG
if (y % 2 == 1)
ret = screen[x][y / 2] >> 4;
else
ret = screen[x][y / 2] & LOWER_4_BITS;
#else
ret = screen[x][y];
#endif
}
else
{
ret = 0;
}
return ret;
}
//***************************************************************************
// Retrieves the PV and PH values of this decoder. These are the offsets
// renderers should offset the data by (to the left for H or up for V.)
//
// So with a PH of 1, the pixel at (100, 100) would be displayed instead at
// (99, 100)
//***************************************************************************
void CDG::getPointers(uint8_t &v, uint8_t &h) const
{
v = pv;
h = ph;
}
//***************************************************************************
// Returns the index of the color to be used in masking the border area
// of the display.
//***************************************************************************
uint8_t CDG::getBorderColor() const
{
return border;
}
//***************************************************************************
// Returns the channel number being decoded. Channel 0 is always decoded.
//***************************************************************************
uint8_t CDG::getChannel() const {
return channel;
}
//***************************************************************************
// Sets the channel number being decoded. Channel 0 is always decoded.
//***************************************************************************
void CDG::setChannel(uint8_t newChannel) {
channel = newChannel;
}
//***************************************************************************
// Executes the given MEMORYPRESET command on this CDG decoder.
//
// MEMORYPRESET commands clear the screen (and the PV/PH offsets.) The screen
// is filled with the given color. The specification has a repeat field
// which should be used to prevent contiously clearing the screen, that
// field is ignored in this implementation.
//***************************************************************************
void CDG::execMemoryPreset(const SubCode &subCode)
{
int color = subCode.data.memDat.color & LOWER_4_BITS;
fillPixels(0, 0, CDG_WIDTH, CDG_HEIGHT, color);
ph = pv = 0;
}
//***************************************************************************
// Executes the given BORDERPRESET command on this CDG decoder.
//
// Sets the color to be used in masking the border area.
//***************************************************************************
void CDG::execBorderPreset(const SubCode &subCode)
{
// I'm not entirely clear on the spec. for BORDERPRESET.
// What exactly the "border" part it should clear isn't, well, clear.
// I understand vintage decoders had a "border area" which displayed
// just a solid color.
//
// VLC implements it by cleaing that part of graphics RAM, and the
// jbum.com document is ambiguous as well.
//BORDERPRESET
//6,12,294,204
//WRONG - do not use
//fillPixels(0, 0, CDG_WIDTH, ROW_MULT, color);
//fillPixels(0, CDG_HEIGHT - ROW_MULT, CDG_WIDTH, CDG_HEIGHT, color);
//fillPixels(0, 0, COL_MULT, CDG_HEIGHT, color);
//fillPixels(CDG_WIDTH - COL_MULT, 0, CDG_WIDTH, CDG_HEIGHT, color);
// Jimi Hendrix - Smash Hits proves that the code for BORDPRESET
// above is wrong. The disk does not play properly with that code.
border = subCode.data.borderDat.color & LOWER_4_BITS;
}
//***************************************************************************
// Executes the given TILE or TILEXOR command on this CDG decoder.
//
// TILE (or FONT) commands are used to draw a block of data to the screen.
//***************************************************************************
void CDG::execTile(const SubCode &subCode)
{
uint8_t color[2];
uint8_t channel; // Lou Reed's "New York" uses channels.
bool useXor = ((subCode.instruction & LOWER_6_BITS) == CDG_TILEBLOCKXOR);
int point;
int row = (subCode.data.tileDat.row & LOWER_5_BITS) * ROW_MULT;
int col = (subCode.data.tileDat.column & LOWER_6_BITS) * COL_MULT;
color[0] = subCode.data.tileDat.color0 & LOWER_4_BITS;
color[1] = subCode.data.tileDat.color1 & LOWER_4_BITS;
channel = (subCode.data.tileDat.color0) >> 4;
channel = (channel << 2) | (subCode.data.tileDat.color1 >> 4);
// Channel 0 is always shown - otherwise check that the channel
// matches the user's selection
// TODO: Sega Saturn may allow the user to select arbitrary
// sets of channels and deselect 0. Could in the future support this.
if (channel != 0 && channel != this->channel) {
return;
}
if (row < CDG_HEIGHT || col < CDG_WIDTH) //not offscreen
{
for (int y = 0; y < ROW_MULT; y++)
{
for (int x = 0; x < COL_MULT; x++)
{
point = (subCode.data.tileDat.tilePixels[y] >> (5 - x))
& 1;
putPixel((col+x), (row+y), color[point], useXor);
}
}
}
}
//***************************************************************************
// Swaps the given pixels at (x1, y1) and (x2, y2)
//***************************************************************************
void CDG::swapPixels(int x1, int y1, int x2, int y2)
{
uint8_t p1 = getPixel(x1, y1);
uint8_t p2 = getPixel(x2, y2);
putPixel(x2, y2, p1);
putPixel(x1, y1, p2);
}
//***************************************************************************
// Internal implementation of SCROLL commands. Accepts the COPV part of
// the SCROLL data field and shifts the graphics array appropriately.
//***************************************************************************
void CDG::rotateV(int cmd)
{
//For the std algorithm, see
//http://www.cplusplus.com/reference/algorithm/rotate/
//(not used here)
int next;
if (cmd == 2)
{
for (int y = 0; y < CDG_HEIGHT - ROW_MULT; y++)
{
next = y - ROW_MULT;
if (next < 0)
next += CDG_HEIGHT;
for (int x = 0; x < CDG_WIDTH; x++)
swapPixels(x, y, x, next);
}
}
else if (cmd == 1)
{
for (int y = CDG_HEIGHT - 1; y >= ROW_MULT; y--)
{
next = y + ROW_MULT;
if (next >= CDG_HEIGHT)
next -= CDG_HEIGHT;
for (int x = 0; x < CDG_WIDTH; x++)
swapPixels(x, y, x, next);
}
}
}
//***************************************************************************
// Internal implementation of SCROLL commands. Accepts the COPH part of
// the SCROLL data field and shifts the graphics array appropriately.
//***************************************************************************
void CDG::rotateH(int cmd)
{
//For the std algorithm, see
//http://www.cplusplus.com/reference/algorithm/rotate/
//(not used here)
int next;
if (cmd == 2)
{
for (int x = 0; x < CDG_WIDTH - COL_MULT; x++)
{
next = x - COL_MULT;
if (next < 0)
next += CDG_WIDTH;
for (int y = 0; y < CDG_HEIGHT; y++)
swapPixels(x, y, next, y);
}
}
else if (cmd == 1)
{
for (int x = CDG_WIDTH - 1; x >= COL_MULT; x--)
{
next = x + COL_MULT;
if (next >= CDG_WIDTH)
next -= CDG_WIDTH;
for (int y = 0; y < CDG_HEIGHT; y++)
swapPixels(x, y, next, y);
}
}
}
//***************************************************************************
// Executes the given SCROLLPRESET or SCROLLCOPY command on this CDG decoder.
//
// SCROLL commands can be used to simulate animation, by panning new
// graphics into view in a relatively smooth manner. This uses the PH and PV
// pointers (accessible through getPointers(int&, int&)) the offset where
// the screen will be rendered to.
//
// SCROLL commands can also trigger larger "copy" shift operations which
// shift the entire screen contents.
//***************************************************************************
void CDG::execScroll(const SubCode &subCode)
{
uint8_t color = subCode.data.scrollDat.color & LOWER_4_BITS;
uint8_t scrollH = subCode.data.scrollDat.hScroll & LOWER_6_BITS,
scrollV = subCode.data.scrollDat.vScroll & LOWER_6_BITS;
uint8_t cmdH = (scrollH & 0x30) >> 4;
uint8_t offsetH = (scrollH & 0x07);
uint8_t cmdV = (scrollV & 0x30) >> 4;
uint8_t offsetV = (scrollV & 0x0F);
ph = offsetH;
pv = offsetV;
if (cmdH)
rotateH(cmdH);
if (cmdV)
rotateV(cmdV);
if ((subCode.instruction & LOWER_6_BITS) == CDG_SCROLLPRESET)
{
if (cmdH == 1)
fillPixels(0, 0, COL_MULT, CDG_HEIGHT, color);
if (cmdH == 2)
fillPixels(CDG_WIDTH - COL_MULT, 0, COL_MULT, CDG_HEIGHT, color);
if (cmdV == 1)
fillPixels(0, 0, CDG_WIDTH, ROW_MULT, color);
if (cmdV == 2)
fillPixels(0, CDG_HEIGHT - ROW_MULT, CDG_WIDTH, ROW_MULT, color);
} // else CDG_SCROLLCOPY, which has
// no extra steps.
}
//***************************************************************************
// Reads and executes the given number of subcodes from the input stream.
// Dirty is incremented by the number of CDG commands that have been
// executed.
//***************************************************************************
int CDG::execCount(istream &in, int count, int &dirty)
{
bool success = true;
SubCode code;
for (int i = 0; i < count; i++)
{
if (readNext(in, code))
{
execNext(code, dirty);
}
else
{
success = false;
break;
}
}
return success;
}
//***************************************************************************
// Wraps to execNext but discards the dirty output parameter.
//***************************************************************************
void CDG::execNext(const SubCode &subCode)
{
int i = 0;
execNext(subCode, i);
}
//***************************************************************************
// Executes the given subcode. If the subcode was a CDG command
// the parameter dirty is incremented.
//***************************************************************************
void CDG::execNext(const SubCode &subCode, int &dirty)
{
//seems like a nice place to use function pointers
//but let's not do that
if ((subCode.command & LOWER_6_BITS) == SubCode_Command::SCCMD_CDG)
{
switch (subCode.instruction & LOWER_6_BITS)
{
case CDG_MEMORYPRESET:
execMemoryPreset(subCode);
break;
case CDG_BORDERPRESET:
execBorderPreset(subCode);
break;
case CDG_TILEBLOCK:
execTile(subCode);
break;
case CDG_SCROLLPRESET:
execScroll(subCode);
break;
case CDG_SCROLLCOPY:
execScroll(subCode);
break;
case CDG_TRANSPARENT:
execTransparent(subCode);
break;
case CDG_LOADCTLOW:
execLoadct(subCode);
break;
case CDG_LOADCTHIGH:
execLoadct(subCode);
break;
case CDG_TILEBLOCKXOR:
execTile(subCode);
break;
default:
// unknown
break;
}
dirty++;
}
}
//***************************************************************************
// Has this decoder seek through the given input stream (using the given
// SeekMode) to the byte location loc.
//***************************************************************************
void CDG::seekTo(istream &in, int loc, SeekMode mode)
{
int dirty = 0;
switch (mode)
{
case SEEK_ENHANCED:
in.seekg(0, ios::beg);
pv = ph = 0; //reset
execCount(in, (loc / sizeof(SubCode)), dirty);
break;
case SEEK_DIRECT:
default:
in.seekg(loc, ios::beg);
break;
}
}
//***************************************************************************
// Calculates the length, in seconds, of the given number of bytes of CDG
// data.
//***************************************************************************
int CDG::sizeToSeconds(int f)
{
f /= 96; // 96 bytes per sector
f /= 75; // 75 sectors per second
return f;
}
//***************************************************************************
// Converts the given percent of total (where total is the size in bytes of
// a stream of CDG data) to a seconds position in time.
//***************************************************************************
int CDG::percentToSecond(double percent, int total)
{
int max = int(percent * total);
int ret = 0;
while (ret < max)
{
ret += BYTES_PER_SECOND;
}
return ret - BYTES_PER_SECOND;
}