OLD DSP-2 support code

void Hackc_DSP2Write()
{
	g_fp.Append1();
}
void Hackc_DSP2_W8(double t)
{
	BYTE *p = (BYTE *)&t;
	for (BYTE i = 0; i < 8; i++) { g_iDSPBuff[g_iDSPWPos & 1023] = *p; p++; g_iDSPWPos++; }
}
void Hackc_DSP2_W4(float t)
{
	BYTE *p = (BYTE *)&t;
	for (BYTE i = 0; i < 4; i++) { g_iDSPBuff[g_iDSPWPos & 1023] = *p; p++; g_iDSPWPos++; }
}
void Hackc_DSP2_W4(DWORD t)
{
	BYTE *p = (BYTE *)&t;
	for (BYTE i = 0; i < 4; i++) { g_iDSPBuff[g_iDSPWPos & 1023] = *p; p++; g_iDSPWPos++; }
}
void Hackc_DSP2_W1(BYTE t)
{
	g_iDSPBuff[g_iDSPWPos & 1023] = t; g_iDSPWPos++;
}
// DSP2 Readbyte 8-bit ver
void Hackc_DSP2_R_8B_C()
{
	BYTE x;
	if (g_iDSPSendRes) {
		g_iDSPSendRes = 0;
		x = 0;
	} else {
		x = g_iDSPBuff[g_iDSPRPos & 1023]; g_iDSPRPos++;
	}
	if (g_vo.fp) { fprintf(g_vo.fp, "RB %02X    [%02X/%04X] !$%04lX\n", 0+x, Hackc_PB_PC/0x8000, 0x8000|(Hackc_PB_PC & 0x7FFF), 0UL+Hackc_DSP2Ptr); }
	Hackc_DSP2Var[0] = x;
//	if (g_nDSPWriting != 0) {
//		if (g_vo.fp) fprintf(g_vo.fp, "W-#%lu\n", g_nDSPWriting);
//		g_nDSPWriting = 0;
//	}
//	g_nDSPReading++;
}
static BYTE iResetDSP2 = 0;
void Hackc_DSP2_R_16B_C()
{
	BYTE x[2] = {0, 0};
	if (g_iDSPSendRes) {
		g_iDSPSendRes = 0;
	}
	Hackc_DSP2Var[0] = x[0];
	Hackc_DSP2Var[1] = x[1];
	if ((Hackc_DSP2Ptr & 0x7FFF) == (0xC000 & 0x7FFF)) {
		iResetDSP2++;
	}
	if (g_vo.fp) { fprintf(g_vo.fp, "RW %02X %02X [%02X/%04X] !$%04lX\n", 0+x[0], 0+x[1], Hackc_PB_PC/0x8000, 0x8000|(Hackc_PB_PC & 0x7FFF), 0UL+Hackc_DSP2Ptr); }
}
void Hackc_DSP2_W_8B_C()
{
	static BYTE iHalt = 0;
	static BYTE iCurCmd = -1;
	static BYTE iMemo1, iMemo2, iMemo3, iScale1, iScale2, iValue;
	static BYTE iColorKey = 0;
	static BYTE iTemp09[4];
	static BYTE iTemp01A[32], iTemp01B[32];
	static BYTE iTemp05A[256], iTemp05B[256];
	static BYTE iTemp0DA[512], iTemp0DB[512];
	static BYTE iTemp06[256];
	BYTE x = Hackc_DSP2Var[0];
	if (iResetDSP2) iHalt = 0, iCurCmd = -1, iResetDSP2 = 0;
//	if (g_nDSPReading != 0) {
//		if (g_vo.fp) fprintf(g_vo.fp, "R-#%lu\n", g_nDSPReading);
//		g_nDSPReading = 0;
//	}
//	g_nDSPWriting++;
	if (g_vo.fp) { fprintf(g_vo.fp, "WB %02X    [%02X/%04X] !$%04lX\n", 0+x, Hackc_PB_PC/0x8000, 0x8000|(Hackc_PB_PC & 0x7FFF), 0UL+Hackc_DSP2Ptr); }
	if (iHalt) { Hackc_DSP2Var[0] = 0; return; }
	switch (iCurCmd) {
	case 0xFF:
		{
			switch (iCurCmd = x) {
			case 0x09:
			case 0x01:
			case 0x03:
				{
					iMemo1 = 0;
					break;
				}
			case 0x0F:
				{
					iCurCmd = 0xFF;
					break;
				}
			case 0x05:
				{
					iMemo1 = iMemo2 = iMemo3 = 0;
					break;
				}
			case 0x0D:
			case 0x06:
				{
					iMemo1 = iMemo2 = 0;
					break;
				}
			default:
				{
					iHalt = 1;
					///if (g_vo.fp) { fprintf(g_vo.fp, "H %02X!!\n", 0+x); }
					break;
				}
			}
			if (g_vo.fp) { fprintf(g_vo.fp, "-- -- %02X [%02X/%04X]\n", 0+x, Hackc_PB_PC/0x8000, 0x8000|(Hackc_PB_PC & 0x7FFF)); }
			break;
		}
	case 0x0F: // OK
		{
			iCurCmd = 0xFF;
			break;
		}
	case 0x09: // ?
		{
			iTemp09[iMemo1] = x; iMemo1++;
			if (iMemo1 == 4) {
				iCurCmd = 0xFF;
				g_iDSPRPos = g_iDSPWPos;
				Hackc_DSP2_W4(0UL+ (*(WORD *)&iTemp09[0] * *(WORD *)&iTemp09[2]) );
			}
			break;
		}
	case 0x01:
		{
			static const BYTE bOr [8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
			static const BYTE bAnd[8] = {~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80};
			static const BYTE bTbl1[4] = {0,0,16,16};
			static const BYTE bTbl2[8] = {6,7,4,5,2,3,0,1};
			iTemp01A[iMemo1] = x; iMemo1++;
			if (iMemo1 == 32) {
				iCurCmd = 0xFF;
				UINT v = 0;
				for (BYTE o = 0; o < 32; o++) {
					x = iTemp01A[o];
					for (BYTE r = 0; r < 8; r++, v++) {
						BYTE iByte = bTbl1[(v & 3)] + (v & 1) + 2 * (v / 32);
						BYTE iBito = bTbl2[(v & 31) / 4];
						if (x & bOr[r]) {
							// Set
							iTemp01B[iByte] |= bOr[iBito];
						} else {
							// Clear
							iTemp01B[iByte] &= bAnd[iBito];
						}
					}
				}
				g_iDSPRPos = g_iDSPWPos;
				//for (BYTE z = 0; z < 32; z++) iTemp2[z] = 0x00;
				//iTemp2[16] = 0xFF;
				for (BYTE p = 0; p < 32; p++) Hackc_DSP2_W1(iTemp01B[p]);
			}
			break;
		}
	case 0x03: // OK
		{
			iColorKey = x; iMemo1++;
			iCurCmd = 0xFF;
			break;
		}
	case 0x05: // OK
		{
			if (iMemo1 != 1) {
				iValue = x;
				iMemo1++;
			} else if (iMemo2 != iValue) {
				iTemp05A[iMemo2] = x;
				iMemo2++;
			} else if (iMemo3 != iValue) {
				iTemp05B[iMemo3] = x;
				iMemo3++;
				if (iMemo3 == iValue) {
					g_iDSPRPos = g_iDSPWPos;
					for (BYTE p = 0; p < iValue; p++) {
						BYTE t = iTemp05B[p];
						if ((t >> 4) == iColorKey) t = (t & 0x0F) | (iTemp05A[p] & 0xF0);
						if ((t & 15) == iColorKey) t = (t & 0xF0) | (iTemp05A[p] & 0x0F);
						Hackc_DSP2_W1(t);
					}
					Hackc_DSP2_W1(0);
					iCurCmd = 0xFF;
				}
			}
			break;
		}
	case 0x0D: // OK?
		{
			if (iMemo1 != 2) {
				if (iMemo1 == 0) iScale1 = (x) / 2; // iScale1 = (x + 1) / 2;
				if (iMemo1 == 1) iScale2 = (x) / 2; // iScale2 = (x + 1) / 2;
				iMemo1++;
			} else {
				iTemp0DB[iMemo2] = x;
				iMemo2++;
				if (iMemo2 == iScale1) {
					g_iDSPRPos = g_iDSPWPos;
					BYTE s;
					for (s = 0; s < iScale2; s++) {
						int x0 = (2*s +0) * iScale1 / iScale2;
						int x1 = (2*s +1) * iScale1 / iScale2;
						BYTE v0 = (x0 & 1) ? (iTemp0DB[x0/2] & 15) : (iTemp0DB[x0/2] / 16);
						BYTE v1 = (x1 & 1) ? (iTemp0DB[x1/2] & 15) : (iTemp0DB[x1/2] / 16);
						Hackc_DSP2_W1(v0 * 16 + v1);
					}
					g_iDSPSendRes = 1;
					iCurCmd = 0xFF;
				}
			}
			break;
		}
	case 0x06: // OK
		{
			if (iMemo1 != 1) {
				iValue = x;
				iMemo1++;
			} else {
				iTemp06[iMemo2] = x;
				iMemo2++;
				if (iMemo2 == iValue) {
					g_iDSPRPos = g_iDSPWPos;
					for (BYTE s = 0; s < iValue; s++) {
						BYTE t = iTemp06[iValue - s - 1];
						Hackc_DSP2_W1(0
							|((t & 0x0F) << 4)
							|((t & 0xF0) >> 4)
						);
					}
					iCurCmd = 0xFF;
				}
			}
			break;
		}
	}
}