1 #include "cps.h"
2 #include "bitswap.h"
3 
4 // CPS (palette)
5 
6 static UINT8* CpsPalSrc = NULL;			// Copy of current input palette
7 UINT32* CpsPal = NULL;					// Hicolor version of palette
8 INT32 nCpsPalCtrlReg;
9 INT32 bCpsUpdatePalEveryFrame = 0;		// Some of the hacks need this as they don't write to CpsReg 0x0a
10 
CpsPalInit()11 INT32 CpsPalInit()
12 {
13 	INT32 nLen = 0;
14 
15 	nLen = 0xc00 * sizeof(UINT16);
16 	CpsPalSrc = (UINT8*)BurnMalloc(nLen);
17 	if (CpsPalSrc == NULL) {
18 		return 1;
19 	}
20 	memset(CpsPalSrc, 0, nLen);
21 
22 	nLen = 0xc00 * sizeof(UINT32);
23 	CpsPal = (UINT32*)BurnMalloc(nLen);
24 	if (CpsPal == NULL) {
25 		return 1;
26 	}
27 
28 	return 0;
29 }
30 
CpsPalExit()31 INT32 CpsPalExit()
32 {
33 	BurnFree(CpsPal);
34 	BurnFree(CpsPalSrc);
35 	return 0;
36 }
37 
38 // Update CpsPal with the new palette at pNewPal (length 0xc00 bytes)
CpsPalUpdate(UINT8 * pNewPal)39 INT32 CpsPalUpdate(UINT8* pNewPal)
40 {
41 	UINT16 *ps, *pn;
42 
43 	ps = (UINT16*)CpsPalSrc;
44 	pn = (UINT16*)pNewPal;
45 
46 	memcpy(ps, pn, 0xc00 * sizeof(UINT16));
47 
48 	INT32 nCtrl = CpsReg[nCpsPalCtrlReg];
49 	UINT16 *PaletteRAM = (UINT16*)CpsPalSrc;
50 
51 	for (INT32 nPage = 0; nPage < 6; nPage++) {
52 		if (BIT(nCtrl, nPage)) {
53 			for (INT32 Offset = 0; Offset < 0x200; ++Offset) {
54 				INT32 Palette = BURN_ENDIAN_SWAP_INT16(*(PaletteRAM++));
55 				INT32 r, g, b, Bright;
56 
57 				Bright = 0x0f + ((Palette >> 12) << 1);
58 
59 				r = ((Palette >> 8) & 0x0f) * 0x11 * Bright / 0x2d;
60 				g = ((Palette >> 4) & 0x0f) * 0x11 * Bright / 0x2d;
61 				b = ((Palette >> 0) & 0x0f) * 0x11 * Bright / 0x2d;
62 
63 				CpsPal[(0x200 * nPage) + (Offset ^ 15)] = BurnHighCol(r, g, b, 0);
64 			}
65 		} else {
66 			if (PaletteRAM != (UINT16*)CpsPalSrc) {
67 				PaletteRAM += 0x200;
68 			}
69 		}
70 	}
71 
72 	return 0;
73 }
74