Path: blob/main/misc/emulator/xnes/snes9x/c4emu.cpp
28515 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#include <math.h>179#include "snes9x.h"180#include "memmap.h"181#include "sar.h"182183static int16 C4SinTable[512] =184{1850, 402, 804, 1206, 1607, 2009, 2410, 2811,1863211, 3611, 4011, 4409, 4808, 5205, 5602, 5997,1876392, 6786, 7179, 7571, 7961, 8351, 8739, 9126,1889512, 9896, 10278, 10659, 11039, 11416, 11793, 12167,18912539, 12910, 13278, 13645, 14010, 14372, 14732, 15090,19015446, 15800, 16151, 16499, 16846, 17189, 17530, 17869,19118204, 18537, 18868, 19195, 19519, 19841, 20159, 20475,19220787, 21097, 21403, 21706, 22005, 22301, 22594, 22884,19323170, 23453, 23732, 24007, 24279, 24547, 24812, 25073,19425330, 25583, 25832, 26077, 26319, 26557, 26790, 27020,19527245, 27466, 27684, 27897, 28106, 28310, 28511, 28707,19628898, 29086, 29269, 29447, 29621, 29791, 29956, 30117,19730273, 30425, 30572, 30714, 30852, 30985, 31114, 31237,19831357, 31471, 31581, 31685, 31785, 31881, 31971, 32057,19932138, 32214, 32285, 32351, 32413, 32469, 32521, 32568,20032610, 32647, 32679, 32706, 32728, 32745, 32758, 32765,20132767, 32765, 32758, 32745, 32728, 32706, 32679, 32647,20232610, 32568, 32521, 32469, 32413, 32351, 32285, 32214,20332138, 32057, 31971, 31881, 31785, 31685, 31581, 31471,20431357, 31237, 31114, 30985, 30852, 30714, 30572, 30425,20530273, 30117, 29956, 29791, 29621, 29447, 29269, 29086,20628898, 28707, 28511, 28310, 28106, 27897, 27684, 27466,20727245, 27020, 26790, 26557, 26319, 26077, 25832, 25583,20825330, 25073, 24812, 24547, 24279, 24007, 23732, 23453,20923170, 22884, 22594, 22301, 22005, 21706, 21403, 21097,21020787, 20475, 20159, 19841, 19519, 19195, 18868, 18537,21118204, 17869, 17530, 17189, 16846, 16499, 16151, 15800,21215446, 15090, 14732, 14372, 14010, 13645, 13278, 12910,21312539, 12167, 11793, 11416, 11039, 10659, 10278, 9896,2149512, 9126, 8739, 8351, 7961, 7571, 7179, 6786,2156392, 5997, 5602, 5205, 4808, 4409, 4011, 3611,2163211, 2811, 2410, 2009, 1607, 1206, 804, 402,2170, -402, -804, -1206, -1607, -2009, -2410, -2811,218-3211, -3611, -4011, -4409, -4808, -5205, -5602, -5997,219-6392, -6786, -7179, -7571, -7961, -8351, -8739, -9126,220-9512, -9896, -10278, -10659, -11039, -11416, -11793, -12167,221-12539, -12910, -13278, -13645, -14010, -14372, -14732, -15090,222-15446, -15800, -16151, -16499, -16846, -17189, -17530, -17869,223-18204, -18537, -18868, -19195, -19519, -19841, -20159, -20475,224-20787, -21097, -21403, -21706, -22005, -22301, -22594, -22884,225-23170, -23453, -23732, -24007, -24279, -24547, -24812, -25073,226-25330, -25583, -25832, -26077, -26319, -26557, -26790, -27020,227-27245, -27466, -27684, -27897, -28106, -28310, -28511, -28707,228-28898, -29086, -29269, -29447, -29621, -29791, -29956, -30117,229-30273, -30425, -30572, -30714, -30852, -30985, -31114, -31237,230-31357, -31471, -31581, -31685, -31785, -31881, -31971, -32057,231-32138, -32214, -32285, -32351, -32413, -32469, -32521, -32568,232-32610, -32647, -32679, -32706, -32728, -32745, -32758, -32765,233-32767, -32765, -32758, -32745, -32728, -32706, -32679, -32647,234-32610, -32568, -32521, -32469, -32413, -32351, -32285, -32214,235-32138, -32057, -31971, -31881, -31785, -31685, -31581, -31471,236-31357, -31237, -31114, -30985, -30852, -30714, -30572, -30425,237-30273, -30117, -29956, -29791, -29621, -29447, -29269, -29086,238-28898, -28707, -28511, -28310, -28106, -27897, -27684, -27466,239-27245, -27020, -26790, -26557, -26319, -26077, -25832, -25583,240-25330, -25073, -24812, -24547, -24279, -24007, -23732, -23453,241-23170, -22884, -22594, -22301, -22005, -21706, -21403, -21097,242-20787, -20475, -20159, -19841, -19519, -19195, -18868, -18537,243-18204, -17869, -17530, -17189, -16846, -16499, -16151, -15800,244-15446, -15090, -14732, -14372, -14010, -13645, -13278, -12910,245-12539, -12167, -11793, -11416, -11039, -10659, -10278, -9896,246-9512, -9126, -8739, -8351, -7961, -7571, -7179, -6786,247-6392, -5997, -5602, -5205, -4808, -4409, -4011, -3611,248-3211, -2811, -2410, -2009, -1607, -1206, -804, -402249};250251static int16 C4CosTable[512] =252{25332767, 32765, 32758, 32745, 32728, 32706, 32679, 32647,25432610, 32568, 32521, 32469, 32413, 32351, 32285, 32214,25532138, 32057, 31971, 31881, 31785, 31685, 31581, 31471,25631357, 31237, 31114, 30985, 30852, 30714, 30572, 30425,25730273, 30117, 29956, 29791, 29621, 29447, 29269, 29086,25828898, 28707, 28511, 28310, 28106, 27897, 27684, 27466,25927245, 27020, 26790, 26557, 26319, 26077, 25832, 25583,26025330, 25073, 24812, 24547, 24279, 24007, 23732, 23453,26123170, 22884, 22594, 22301, 22005, 21706, 21403, 21097,26220787, 20475, 20159, 19841, 19519, 19195, 18868, 18537,26318204, 17869, 17530, 17189, 16846, 16499, 16151, 15800,26415446, 15090, 14732, 14372, 14010, 13645, 13278, 12910,26512539, 12167, 11793, 11416, 11039, 10659, 10278, 9896,2669512, 9126, 8739, 8351, 7961, 7571, 7179, 6786,2676392, 5997, 5602, 5205, 4808, 4409, 4011, 3611,2683211, 2811, 2410, 2009, 1607, 1206, 804, 402,2690, -402, -804, -1206, -1607, -2009, -2410, -2811,270-3211, -3611, -4011, -4409, -4808, -5205, -5602, -5997,271-6392, -6786, -7179, -7571, -7961, -8351, -8739, -9126,272-9512, -9896, -10278, -10659, -11039, -11416, -11793, -12167,273-12539, -12910, -13278, -13645, -14010, -14372, -14732, -15090,274-15446, -15800, -16151, -16499, -16846, -17189, -17530, -17869,275-18204, -18537, -18868, -19195, -19519, -19841, -20159, -20475,276-20787, -21097, -21403, -21706, -22005, -22301, -22594, -22884,277-23170, -23453, -23732, -24007, -24279, -24547, -24812, -25073,278-25330, -25583, -25832, -26077, -26319, -26557, -26790, -27020,279-27245, -27466, -27684, -27897, -28106, -28310, -28511, -28707,280-28898, -29086, -29269, -29447, -29621, -29791, -29956, -30117,281-30273, -30425, -30572, -30714, -30852, -30985, -31114, -31237,282-31357, -31471, -31581, -31685, -31785, -31881, -31971, -32057,283-32138, -32214, -32285, -32351, -32413, -32469, -32521, -32568,284-32610, -32647, -32679, -32706, -32728, -32745, -32758, -32765,285-32767, -32765, -32758, -32745, -32728, -32706, -32679, -32647,286-32610, -32568, -32521, -32469, -32413, -32351, -32285, -32214,287-32138, -32057, -31971, -31881, -31785, -31685, -31581, -31471,288-31357, -31237, -31114, -30985, -30852, -30714, -30572, -30425,289-30273, -30117, -29956, -29791, -29621, -29447, -29269, -29086,290-28898, -28707, -28511, -28310, -28106, -27897, -27684, -27466,291-27245, -27020, -26790, -26557, -26319, -26077, -25832, -25583,292-25330, -25073, -24812, -24547, -24279, -24007, -23732, -23453,293-23170, -22884, -22594, -22301, -22005, -21706, -21403, -21097,294-20787, -20475, -20159, -19841, -19519, -19195, -18868, -18537,295-18204, -17869, -17530, -17189, -16846, -16499, -16151, -15800,296-15446, -15090, -14732, -14372, -14010, -13645, -13278, -12910,297-12539, -12167, -11793, -11416, -11039, -10659, -10278, -9896,298-9512, -9126, -8739, -8351, -7961, -7571, -7179, -6786,299-6392, -5997, -5602, -5205, -4808, -4409, -4011, -3611,300-3211, -2811, -2410, -2009, -1607, -1206, -804, -402,3010, 402, 804, 1206, 1607, 2009, 2410, 2811,3023211, 3611, 4011, 4409, 4808, 5205, 5602, 5997,3036392, 6786, 7179, 7571, 7961, 8351, 8739, 9126,3049512, 9896, 10278, 10659, 11039, 11416, 11793, 12167,30512539, 12910, 13278, 13645, 14010, 14372, 14732, 15090,30615446, 15800, 16151, 16499, 16846, 17189, 17530, 17869,30718204, 18537, 18868, 19195, 19519, 19841, 20159, 20475,30820787, 21097, 21403, 21706, 22005, 22301, 22594, 22884,30923170, 23453, 23732, 24007, 24279, 24547, 24812, 25073,31025330, 25583, 25832, 26077, 26319, 26557, 26790, 27020,31127245, 27466, 27684, 27897, 28106, 28310, 28511, 28707,31228898, 29086, 29269, 29447, 29621, 29791, 29956, 30117,31330273, 30425, 30572, 30714, 30852, 30985, 31114, 31237,31431357, 31471, 31581, 31685, 31785, 31881, 31971, 32057,31532138, 32214, 32285, 32351, 32413, 32469, 32521, 32568,31632610, 32647, 32679, 32706, 32728, 32745, 32758, 32765317};318319static uint8 C4TestPattern[12 * 4] =320{3210x00, 0x00, 0x00, 0xff,3220xff, 0xff, 0x00, 0xff,3230x00, 0x00, 0x00, 0xff,3240xff, 0xff, 0x00, 0x00,3250xff, 0xff, 0x00, 0x00,3260x80, 0xff, 0xff, 0x7f,3270x00, 0x80, 0x00, 0xff,3280x7f, 0x00, 0xff, 0x7f,3290xff, 0x7f, 0xff, 0xff,3300x00, 0x00, 0x01, 0xff,3310xff, 0xfe, 0x00, 0x01,3320x00, 0xff, 0xfe, 0x00333};334335static void C4ConvOAM (void);336static void C4DoScaleRotate (int);337static void C4DrawLine (int32, int32, int16, int32, int32, int16, uint8);338static void C4DrawWireFrame (void);339static void C4TransformLines (void);340static void C4BitPlaneWave (void);341static void C4SprDisintegrate (void);342static void C4ProcessSprites (void);343344345static void C4ConvOAM (void)346{347uint8 *OAMptr = Memory.C4RAM + (Memory.C4RAM[0x626] << 2);348for (uint8 *i = Memory.C4RAM + 0x1fd; i > OAMptr; i -= 4)349*i = 0xe0; // Clear OAM-to-be350351uint8 *OAMptr2;352uint16 globalX, globalY;353int16 SprX, SprY;354uint8 SprName, SprAttr;355uint8 SprCount;356357globalX = READ_WORD(Memory.C4RAM + 0x0621);358globalY = READ_WORD(Memory.C4RAM + 0x0623);359OAMptr2 = Memory.C4RAM + 0x200 + (Memory.C4RAM[0x626] >> 2);360361#ifdef DEBUGGER362if (Memory.C4RAM[0x625] != 0)363printf("$6625=%02x, expected 00\n", Memory.C4RAM[0x625]);364if ((Memory.C4RAM[0x626] >> 2) != Memory.C4RAM[0x629])365printf("$6629=%02x, expected %02x\n", Memory.C4RAM[0x629], (Memory.C4RAM[0x626] >> 2));366if (((uint16) Memory.C4RAM[0x626] << 2) != READ_WORD(Memory.C4RAM + 0x627))367printf("$6627=%04x, expected %04x\n", READ_WORD(Memory.C4RAM + 0x627), ((uint16) Memory.C4RAM[0x626] << 2));368#endif369370if (Memory.C4RAM[0x0620] != 0)371{372SprCount = 128 - Memory.C4RAM[0x626];373374uint8 offset = (Memory.C4RAM[0x626] & 3) * 2;375uint8 *srcptr = Memory.C4RAM + 0x220;376377for (int i = Memory.C4RAM[0x0620]; i > 0 && SprCount > 0; i--, srcptr += 16)378{379SprX = READ_WORD(srcptr) - globalX;380SprY = READ_WORD(srcptr + 2) - globalY;381SprName = srcptr[5];382SprAttr = srcptr[4] | srcptr[0x06]; // XXX: mask bits?383384uint8 *sprptr = C4GetMemPointer(READ_3WORD(srcptr + 7));385if (*sprptr != 0)386{387int16 X, Y;388389for (int SprCnt = *sprptr++; SprCnt > 0 && SprCount > 0; SprCnt--, sprptr += 4)390{391X = (int8) sprptr[1];392if (SprAttr & 0x40)393X = -X - ((sprptr[0] & 0x20) ? 16 : 8); // flip X394X += SprX;395396if (X >= -16 && X <= 272)397{398Y = (int8) sprptr[2];399if (SprAttr & 0x80)400Y = -Y - ((sprptr[0] & 0x20) ? 16 : 8);401Y += SprY;402403if (Y >= -16 && Y <= 224)404{405OAMptr[0] = X & 0xff;406OAMptr[1] = (uint8) Y;407OAMptr[2] = SprName + sprptr[3];408OAMptr[3] = SprAttr ^ (sprptr[0] & 0xc0); // XXX: Carry from SprName addition?409410*OAMptr2 &= ~(3 << offset);411if (X & 0x100)412*OAMptr2 |= 1 << offset;413if (sprptr[0] & 0x20)414*OAMptr2 |= 2 << offset;415416OAMptr += 4;417SprCount--;418419offset = (offset + 2) & 6;420if (offset == 0)421OAMptr2++;422}423}424}425}426else427if (SprCount > 0)428{429// XXX: Should we be testing -16<=SprX<=272 and -16<=SprY<=224?430OAMptr[0] = (uint8) SprX;431OAMptr[1] = (uint8) SprY;432OAMptr[2] = SprName;433OAMptr[3] = SprAttr;434435*OAMptr2 &= ~(3 << offset);436if (SprX & 0x100)437*OAMptr2 |= 3 << offset;438else439*OAMptr2 |= 2 << offset;440441OAMptr += 4;442SprCount--;443444offset = (offset + 2) & 6;445if (offset == 0)446OAMptr2++;447}448}449}450}451452static void C4DoScaleRotate (int row_padding)453{454int16 A, B, C, D;455456// Calculate matrix457int32 XScale = READ_WORD(Memory.C4RAM + 0x1f8f);458if (XScale & 0x8000)459XScale = 0x7fff;460461int32 YScale = READ_WORD(Memory.C4RAM + 0x1f92);462if (YScale & 0x8000)463YScale = 0x7fff;464465if (READ_WORD(Memory.C4RAM + 0x1f80) == 0) // no rotation466{467// XXX: only do this for C and D?468// XXX: and then only when YScale is 0x1000?469A = (int16) XScale;470B = 0;471C = 0;472D = (int16) YScale;473}474else475if (READ_WORD(Memory.C4RAM + 0x1f80) == 128) // 90 degree rotation476{477// XXX: Really do this?478A = 0;479B = (int16) (-YScale);480C = (int16) XScale;481D = 0;482}483else484if (READ_WORD(Memory.C4RAM + 0x1f80) == 256) // 180 degree rotation485{486// XXX: Really do this?487A = (int16) (-XScale);488B = 0;489C = 0;490D = (int16) (-YScale);491}492else493if (READ_WORD(Memory.C4RAM + 0x1f80) == 384) // 270 degree rotation494{495// XXX: Really do this?496A = 0;497B = (int16) YScale;498C = (int16) (-XScale);499D = 0;500}501else502{503A = (int16) SAR(C4CosTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * XScale, 15);504B = (int16) (-SAR(C4SinTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * YScale, 15));505C = (int16) SAR(C4SinTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * XScale, 15);506D = (int16) SAR(C4CosTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * YScale, 15);507}508509// Calculate Pixel Resolution510uint8 w = Memory.C4RAM[0x1f89] & ~7;511uint8 h = Memory.C4RAM[0x1f8c] & ~7;512513//printf("%dx%d XScale=%04x YScale=%04x angle=%03x\n", w, h, XScale, YScale, READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff);514//printf("Matrix: [%10g %10g] [%04x %04x]\n", A / 4096.0, B / 4096.0, A & 0xffff, B & 0xffff);515//printf(" [%10g %10g] [%04x %04x]\n", C / 4096.0, D / 4096.0, C & 0xffff, D & 0xffff);516517// Clear the output RAM518memset(Memory.C4RAM, 0, (w + row_padding / 4) * h / 2);519520int32 Cx = (int16) READ_WORD(Memory.C4RAM + 0x1f83);521int32 Cy = (int16) READ_WORD(Memory.C4RAM + 0x1f86);522523#ifdef DEBUGGER524if (Memory.C4RAM[0x1f97] != 0)525printf("$7f97=%02x, expected 00\n", Memory.C4RAM[0x1f97]);526if ((Cx & ~1) != w / 2 || (Cy & ~1) != h / 2)527printf("Center is not middle of image! (%d, %d) != (%d, %d)\n", Cx, Cy, w / 2, h / 2);528#endif529530// Calculate start position (i.e. (Ox, Oy) = (0, 0))531// The low 12 bits are fractional, so (Cx<<12) gives us the Cx we want in the function.532// We do Cx*A etc normally because the matrix parameters already have the fractional parts.533int32 LineX = (Cx << 12) - Cx * A - Cx * B;534int32 LineY = (Cy << 12) - Cy * C - Cy * D;535536// Start loop537uint32 X, Y;538uint8 byte;539int outidx = 0;540uint8 bit = 0x80;541542for (int y = 0; y < h; y++)543{544X = LineX;545Y = LineY;546547for (int x = 0; x < w; x++)548{549if ((X >> 12) >= w || (Y >> 12) >= h)550byte = 0;551else552{553uint32 addr = (Y >> 12) * w + (X >> 12);554byte = Memory.C4RAM[0x600 + (addr >> 1)];555if (addr & 1)556byte >>= 4;557}558559// De-bitplanify560if (byte & 1)561Memory.C4RAM[outidx] |= bit;562if (byte & 2)563Memory.C4RAM[outidx + 1] |= bit;564if (byte & 4)565Memory.C4RAM[outidx + 16] |= bit;566if (byte & 8)567Memory.C4RAM[outidx + 17] |= bit;568569bit >>= 1;570if (bit == 0)571{572bit = 0x80;573outidx += 32;574}575576X += A; // Add 1 to output x => add an A and a C577Y += C;578}579580outidx += 2 + row_padding;581if (outidx & 0x10)582outidx &= ~0x10;583else584outidx -= w * 4 + row_padding;585586LineX += B; // Add 1 to output y => add a B and a D587LineY += D;588}589}590591static void C4DrawLine (int32 X1, int32 Y1, int16 Z1, int32 X2, int32 Y2, int16 Z2, uint8 Color)592{593// Transform coordinates594C4WFXVal = (int16) X1;595C4WFYVal = (int16) Y1;596C4WFZVal = Z1;597C4WFScale = Memory.C4RAM[0x1f90];598C4WFX2Val = Memory.C4RAM[0x1f86];599C4WFY2Val = Memory.C4RAM[0x1f87];600C4WFDist = Memory.C4RAM[0x1f88];601C4TransfWireFrame2();602X1 = (C4WFXVal + 48) << 8;603Y1 = (C4WFYVal + 48) << 8;604605C4WFXVal = (int16) X2;606C4WFYVal = (int16) Y2;607C4WFZVal = Z2;608C4TransfWireFrame2();609X2 = (C4WFXVal + 48) << 8;610Y2 = (C4WFYVal + 48) << 8;611612// Get line info613C4WFXVal = (int16) (X1 >> 8);614C4WFYVal = (int16) (Y1 >> 8);615C4WFX2Val = (int16) (X2 >> 8);616C4WFY2Val = (int16) (Y2 >> 8);617C4CalcWireFrame();618X2 = (int16) C4WFXVal;619Y2 = (int16) C4WFYVal;620621// Render line622for (int i = C4WFDist ? C4WFDist : 1; i > 0; i--)623{624if (X1 > 0xff && Y1 > 0xff && X1 < 0x6000 && Y1 < 0x6000)625{626uint16 addr = (((Y1 >> 8) >> 3) << 8) - (((Y1 >> 8) >> 3) << 6) + (((X1 >> 8) >> 3) << 4) + ((Y1 >> 8) & 7) * 2;627uint8 bit = 0x80 >> ((X1 >> 8) & 7);628629Memory.C4RAM[addr + 0x300] &= ~bit;630Memory.C4RAM[addr + 0x301] &= ~bit;631if (Color & 1)632Memory.C4RAM[addr + 0x300] |= bit;633if (Color & 2)634Memory.C4RAM[addr + 0x301] |= bit;635}636637X1 += X2;638Y1 += Y2;639}640}641642static void C4DrawWireFrame (void)643{644uint8 *line = C4GetMemPointer(READ_3WORD(Memory.C4RAM + 0x1f80));645uint8 *point1, *point2;646int16 X1, Y1, Z1;647int16 X2, Y2, Z2;648uint8 Color;649650#ifdef DEBUGGER651if (READ_3WORD(Memory.C4RAM + 0x1f8f) & 0xff00ff)652printf("wireframe: Unexpected value in $7f8f: %06x\n", READ_3WORD(Memory.C4RAM + 0x1f8f));653if (READ_3WORD(Memory.C4RAM + 0x1fa4) != 0x001000)654printf("wireframe: Unexpected value in $7fa4: %06x\n", READ_3WORD(Memory.C4RAM + 0x1fa4));655#endif656657for (int i = Memory.C4RAM[0x0295]; i > 0; i--, line += 5)658{659if (line[0] == 0xff && line[1] == 0xff)660{661uint8 *tmp = line - 5;662while (tmp[2] == 0xff && tmp[3] == 0xff)663tmp -= 5;664point1 = C4GetMemPointer((Memory.C4RAM[0x1f82] << 16) | (tmp[2] << 8) | tmp[3]);665}666else667point1 = C4GetMemPointer((Memory.C4RAM[0x1f82] << 16) | (line[0] << 8) | line[1]);668669point2 = C4GetMemPointer((Memory.C4RAM[0x1f82] << 16) | (line[2] << 8) | line[3]);670671X1 = (point1[0] << 8) | point1[1];672Y1 = (point1[2] << 8) | point1[3];673Z1 = (point1[4] << 8) | point1[5];674X2 = (point2[0] << 8) | point2[1];675Y2 = (point2[2] << 8) | point2[3];676Z2 = (point2[4] << 8) | point2[5];677678Color = line[4];679680C4DrawLine(X1, Y1, Z1, X2, Y2, Z2, Color);681}682}683684static void C4TransformLines (void)685{686C4WFX2Val = Memory.C4RAM[0x1f83];687C4WFY2Val = Memory.C4RAM[0x1f86];688C4WFDist = Memory.C4RAM[0x1f89];689C4WFScale = Memory.C4RAM[0x1f8c];690691#ifdef DEBUGGER692if (Memory.C4RAM[0x1f8a] != 0x90)693printf("lines: $7f8a = %02x, expected 90\n", READ_WORD(Memory.C4RAM + 0x1f8a));694#endif695696// Transform vertices697uint8 *ptr = Memory.C4RAM;698699for (int i = READ_WORD(Memory.C4RAM + 0x1f80); i > 0; i--, ptr += 0x10)700{701C4WFXVal = READ_WORD(ptr + 1);702C4WFYVal = READ_WORD(ptr + 5);703C4WFZVal = READ_WORD(ptr + 9);704C4TransfWireFrame();705706// Displace707WRITE_WORD(ptr + 1, C4WFXVal + 0x80);708WRITE_WORD(ptr + 5, C4WFYVal + 0x50);709}710711WRITE_WORD(Memory.C4RAM + 0x600, 23);712WRITE_WORD(Memory.C4RAM + 0x602, 0x60);713WRITE_WORD(Memory.C4RAM + 0x605, 0x40);714WRITE_WORD(Memory.C4RAM + 0x600 + 8, 23);715WRITE_WORD(Memory.C4RAM + 0x602 + 8, 0x60);716WRITE_WORD(Memory.C4RAM + 0x605 + 8, 0x40);717718ptr = Memory.C4RAM + 0xb02;719uint8 *ptr2 = Memory.C4RAM;720721for (int i = READ_WORD(Memory.C4RAM + 0xb00); i > 0; i--, ptr += 2, ptr2 += 8)722{723C4WFXVal = READ_WORD(Memory.C4RAM + (ptr[0] << 4) + 1);724C4WFYVal = READ_WORD(Memory.C4RAM + (ptr[0] << 4) + 5);725C4WFX2Val = READ_WORD(Memory.C4RAM + (ptr[1] << 4) + 1);726C4WFY2Val = READ_WORD(Memory.C4RAM + (ptr[1] << 4) + 5);727C4CalcWireFrame();728729WRITE_WORD(ptr2 + 0x600, C4WFDist ? C4WFDist : 1);730WRITE_WORD(ptr2 + 0x602, C4WFXVal);731WRITE_WORD(ptr2 + 0x605, C4WFYVal);732}733}734735static void C4BitPlaneWave (void)736{737static uint16 bmpdata[] =738{7390x0000, 0x0002, 0x0004, 0x0006, 0x0008, 0x000A, 0x000C, 0x000E,7400x0200, 0x0202, 0x0204, 0x0206, 0x0208, 0x020A, 0x020C, 0x020E,7410x0400, 0x0402, 0x0404, 0x0406, 0x0408, 0x040A, 0x040C, 0x040E,7420x0600, 0x0602, 0x0604, 0x0606, 0x0608, 0x060A, 0x060C, 0x060E,7430x0800, 0x0802, 0x0804, 0x0806, 0x0808, 0x080A, 0x080C, 0x080E744};745746uint8 *dst = Memory.C4RAM;747uint32 waveptr = Memory.C4RAM[0x1f83];748uint16 mask1 = 0xc0c0;749uint16 mask2 = 0x3f3f;750751#ifdef DEBUGGER752if (READ_3WORD(Memory.C4RAM + 0x1f80) != Memory.C4RAM[waveptr + 0xb00])753printf("$7f80=%06x, expected %02x\n", READ_3WORD(Memory.C4RAM + 0x1f80), Memory.C4RAM[waveptr + 0xb00]);754#endif755756for (int j = 0; j < 0x10; j++)757{758do759{760int16 height = -((int8) Memory.C4RAM[waveptr + 0xb00]) - 16;761762for (int i = 0; i < 40; i++)763{764uint16 tmp = READ_WORD(dst + bmpdata[i]) & mask2;765if (height >= 0)766{767if (height < 8)768tmp |= mask1 & READ_WORD(Memory.C4RAM + 0xa00 + height * 2);769else770tmp |= mask1 & 0xff00;771}772773WRITE_WORD(dst + bmpdata[i], tmp);774775height++;776}777778waveptr = (waveptr + 1) & 0x7f;779mask1 = (mask1 >> 2) | (mask1 << 6);780mask2 = (mask2 >> 2) | (mask2 << 6);781}782while (mask1 != 0xc0c0);783784dst += 16;785786do787{788int16 height = -((int8) Memory.C4RAM[waveptr + 0xb00]) - 16;789790for (int i = 0; i < 40; i++)791{792uint16 tmp = READ_WORD(dst + bmpdata[i]) & mask2;793if (height >= 0)794{795if (height < 8)796tmp |= mask1 & READ_WORD(Memory.C4RAM + 0xa10 + height * 2);797else798tmp |= mask1 & 0xff00;799}800801WRITE_WORD(dst + bmpdata[i], tmp);802803height++;804}805806waveptr = (waveptr + 1) & 0x7f;807mask1 = (mask1 >> 2) | (mask1 << 6);808mask2 = (mask2 >> 2) | (mask2 << 6);809}810while (mask1 != 0xc0c0);811812dst += 16;813}814}815816static void C4SprDisintegrate (void)817{818uint8 *src;819uint8 width, height;820uint32 StartX, StartY;821int32 scaleX, scaleY;822int32 Cx, Cy;823824width = Memory.C4RAM[0x1f89];825height = Memory.C4RAM[0x1f8c];826Cx = (int16) READ_WORD(Memory.C4RAM + 0x1f80);827Cy = (int16) READ_WORD(Memory.C4RAM + 0x1f83);828829#ifdef DEBUGGER830if ((Cx & ~1) != width / 2 || (Cy & ~1) != height / 2)831printf("Center is not middle of image for disintegrate! (%d, %d) != (%d, %d)\n", Cx, Cy, width / 2, height / 2);832#endif833834scaleX = (int16) READ_WORD(Memory.C4RAM + 0x1f86);835scaleY = (int16) READ_WORD(Memory.C4RAM + 0x1f8f);836StartX = -Cx * scaleX + (Cx << 8);837StartY = -Cy * scaleY + (Cy << 8);838839src = Memory.C4RAM + 0x600;840841memset(Memory.C4RAM, 0, width * height / 2);842843for (uint32 y = StartY, i = 0; i < height; i++, y += scaleY)844{845for (uint32 x = StartX, j = 0; j < width; j++, x += scaleX)846{847if ((x >> 8) < width && (y >> 8) < height && (y >> 8) * width + (x >> 8) < 0x2000)848{849uint8 pixel = (j & 1) ? (*src >> 4) : *src;850int idx = (y >> 11) * width * 4 + (x >> 11) * 32 + ((y >> 8) & 7) * 2;851uint8 mask = 0x80 >> ((x >> 8) & 7);852853if (pixel & 1)854Memory.C4RAM[idx] |= mask;855if (pixel & 2)856Memory.C4RAM[idx + 1] |= mask;857if (pixel & 4)858Memory.C4RAM[idx + 16] |= mask;859if (pixel & 8)860Memory.C4RAM[idx + 17] |= mask;861}862863if (j & 1)864src++;865}866}867}868869static void C4ProcessSprites (void)870{871switch (Memory.C4RAM[0x1f4d])872{873case 0x00: // Build OAM874#ifdef DEBUGGER875//printf("00 00 Build OAM!\n");876#endif877C4ConvOAM();878break;879880case 0x03: // Scale/Rotate881#ifdef DEBUGGER882//printf("00 03 Scale/Rotate!\n");883#endif884C4DoScaleRotate(0);885break;886887case 0x05: // Transform Lines888#ifdef DEBUGGER889//printf("00 05 Transform Lines!\n");890#endif891C4TransformLines();892break;893894case 0x07: // Scale/Rotate895#ifdef DEBUGGER896//printf("00 07 Scale/Rotate!\n");897#endif898C4DoScaleRotate(64);899break;900901case 0x08: // Draw wireframe902#ifdef DEBUGGER903//printf("00 08 Draw wireframe!\n");904#endif905C4DrawWireFrame();906break;907908case 0x0b: // Disintegrate909#ifdef DEBUGGER910//printf("00 0b Disintegrate!\n");911#endif912C4SprDisintegrate();913break;914915case 0x0c: // Wave916#ifdef DEBUGGER917//printf("00 0b Wave!\n");918#endif919C4BitPlaneWave();920break;921922default:923#ifdef DEBUGGER924printf("Unknown C4 sprite command (%02x)\n", Memory.C4RAM[0x1f4d]);925#endif926break;927}928}929930void S9xInitC4 (void)931{932// Stupid zsnes code, we can't do the logical thing without breaking savestates933// Memory.C4RAM = &Memory.FillRAM [0x6000];934memset(Memory.C4RAM, 0, 0x2000);935}936937uint8 S9xGetC4 (uint16 Address)938{939if (Address == 0x7f5e)940return (0);941942return (Memory.C4RAM[Address - 0x6000]);943}944945void S9xSetC4 (uint8 byte, uint16 Address)946{947Memory.C4RAM[Address - 0x6000] = byte;948949if (Address == 0x7f4f)950{951if (Memory.C4RAM[0x1f4d] == 0x0e && byte < 0x40 && (byte & 3) == 0)952{953#ifdef DEBUGGER954printf("Test command %02x 0e used!\n", byte);955#endif956Memory.C4RAM[0x1f80] = byte >> 2;957}958else959{960switch (byte)961{962case 0x00: // Sprite963#ifdef DEBUGGER964//printf("00 Sprite!\n");965#endif966C4ProcessSprites();967break;968969case 0x01: // Draw wireframe970#ifdef DEBUGGER971//printf("01 Draw wireframe!\n");972if (Memory.C4RAM[0x1f4d] != 8)973printf("$7f4d=%02x, expected 08 for command 01 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);974#endif975memset(Memory.C4RAM + 0x300, 0, 16 * 12 * 3 * 4);976C4DrawWireFrame();977break;978979case 0x05: // Propulsion (?)980{981#ifdef DEBUGGER982//printf("05 Propulsion (?)!\n");983if (Memory.C4RAM[0x1f4d] != 2)984printf("$7f4d=%02x, expected 02 for command 05 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);985#endif986int32 tmp = 0x10000;987if (READ_WORD(Memory.C4RAM + 0x1f83))988tmp = SAR((tmp / READ_WORD(Memory.C4RAM + 0x1f83)) * READ_WORD(Memory.C4RAM + 0x1f81), 8);989990WRITE_WORD(Memory.C4RAM + 0x1f80, (uint16) tmp);991break;992}993994case 0x0d: // Set vector length995#ifdef DEBUGGER996//printf("0d Set vector length!\n");997if (Memory.C4RAM[0x1f4d] != 2)998printf("$7f4d=%02x, expected 02 for command 0d %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);999#endif1000C41FXVal = READ_WORD(Memory.C4RAM + 0x1f80);1001C41FYVal = READ_WORD(Memory.C4RAM + 0x1f83);1002C41FDistVal = READ_WORD(Memory.C4RAM + 0x1f86);1003C4Op0D();1004WRITE_WORD(Memory.C4RAM + 0x1f89, C41FXVal);1005WRITE_WORD(Memory.C4RAM + 0x1f8c, C41FYVal);1006break;10071008case 0x10: // Polar to rectangluar1009{1010#ifdef DEBUGGER1011//printf("10 Polar->Rect!\n");1012if (Memory.C4RAM[0x1f4d] != 2)1013printf("$7f4d=%02x, expected 02 for command 10 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1014#endif1015int32 tmp;1016int32 r1;1017r1 = READ_WORD(Memory.C4RAM + 0x1f83);1018if (r1 & 0x8000)1019r1 |= ~0x7fff;1020else1021r1 &= 0x7fff;10221023tmp = SAR(r1 * C4CosTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * 2, 16);1024WRITE_3WORD(Memory.C4RAM + 0x1f86, tmp);1025tmp = SAR(r1 * C4SinTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * 2, 16);1026WRITE_3WORD(Memory.C4RAM + 0x1f89, (tmp - SAR(tmp, 6)));1027break;1028}10291030case 0x13: // Polar to rectangluar1031{1032#ifdef DEBUGGER1033//printf("13 Polar->Rect!\n");1034if (Memory.C4RAM[0x1f4d] != 2)1035printf("$7f4d=%02x, expected 02 for command 13 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1036#endif1037int32 tmp;1038tmp = SAR((int32) READ_WORD(Memory.C4RAM + 0x1f83) * C4CosTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * 2, 8);1039WRITE_3WORD(Memory.C4RAM + 0x1f86, tmp);1040tmp = SAR((int32) READ_WORD(Memory.C4RAM + 0x1f83) * C4SinTable[READ_WORD(Memory.C4RAM + 0x1f80) & 0x1ff] * 2, 8);1041WRITE_3WORD(Memory.C4RAM + 0x1f89, tmp);1042break;1043}10441045case 0x15: // Pythagorean1046#ifdef DEBUGGER1047//printf("15 Pythagorean!\n");1048if (Memory.C4RAM[0x1f4d] != 2)1049printf("$7f4d=%02x, expected 02 for command 15 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1050#endif1051C41FXVal = READ_WORD(Memory.C4RAM + 0x1f80);1052C41FYVal = READ_WORD(Memory.C4RAM + 0x1f83);1053//C4Op15(); // optimized to:1054C41FDist = (int16) sqrt((double) C41FXVal * C41FXVal + (double) C41FYVal * C41FYVal);1055WRITE_WORD(Memory.C4RAM + 0x1f80, C41FDist);1056break;10571058case 0x1f: // atan1059#ifdef DEBUGGER1060//printf("1f atan!\n");1061if (Memory.C4RAM[0x1f4d] != 2)1062printf("$7f4d=%02x, expected 02 for command 1f %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1063#endif1064C41FXVal = READ_WORD(Memory.C4RAM + 0x1f80);1065C41FYVal = READ_WORD(Memory.C4RAM + 0x1f83);1066C4Op1F();1067WRITE_WORD(Memory.C4RAM + 0x1f86, C41FAngleRes);1068break;10691070case 0x22: // Trapezoid1071{1072#ifdef DEBUGGER1073//printf("22 Trapezoid!\n");1074if (Memory.C4RAM[0x1f4d] != 2)1075printf("$7f4d=%02x, expected 02 for command 22 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1076#endif1077int16 angle1 = READ_WORD(Memory.C4RAM + 0x1f8c) & 0x1ff;1078int16 angle2 = READ_WORD(Memory.C4RAM + 0x1f8f) & 0x1ff;10791080#ifdef DEBUGGER1081if (C4CosTable[angle1] == 0)1082fprintf(stderr, "22 Trapezoid: Invalid tangent! angle1=%d\n", angle1);1083if (C4CosTable[angle2] == 0)1084fprintf(stderr, "22 Trapezoid: Invalid tangent! angle2=%d\n", angle2);1085#endif10861087int32 tan1 = (C4CosTable[angle1] != 0) ? ((((int32) C4SinTable[angle1]) << 16) / C4CosTable[angle1]) : 0x80000000;1088int32 tan2 = (C4CosTable[angle2] != 0) ? ((((int32) C4SinTable[angle2]) << 16) / C4CosTable[angle2]) : 0x80000000;10891090int16 y = READ_WORD(Memory.C4RAM + 0x1f83) - READ_WORD(Memory.C4RAM + 0x1f89);1091int16 left, right;10921093for (int j = 0; j < 225; j++)1094{1095if (y >= 0)1096{1097left = SAR((int32) tan1 * y, 16) - READ_WORD(Memory.C4RAM + 0x1f80) + READ_WORD(Memory.C4RAM + 0x1f86);1098right = SAR((int32) tan2 * y, 16) - READ_WORD(Memory.C4RAM + 0x1f80) + READ_WORD(Memory.C4RAM + 0x1f86) + READ_WORD(Memory.C4RAM + 0x1f93);10991100if (left < 0 && right < 0)1101{1102left = 1;1103right = 0;1104}1105else1106if (left < 0)1107left = 0;1108else1109if (right < 0)1110right = 0;11111112if (left > 255 && right > 255)1113{1114left = 255;1115right = 254;1116}1117else1118if (left > 255)1119left = 255;1120else1121if (right > 255)1122right = 255;1123}1124else1125{1126left = 1;1127right = 0;1128}11291130Memory.C4RAM[j + 0x800] = (uint8) left;1131Memory.C4RAM[j + 0x900] = (uint8) right;11321133y++;1134}11351136break;1137}11381139case 0x25: // Multiply1140{1141#ifdef DEBUGGER1142//printf("25 Multiply!\n");1143if (Memory.C4RAM[0x1f4d] != 2)1144printf("$7f4d=%02x, expected 02 for command 25 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1145#endif1146int32 foo = READ_3WORD(Memory.C4RAM + 0x1f80);1147int32 bar = READ_3WORD(Memory.C4RAM + 0x1f83);1148foo *= bar;1149WRITE_3WORD(Memory.C4RAM + 0x1f80, foo);1150break;1151}11521153case 0x2d: // Transform Coords1154#ifdef DEBUGGER1155//printf("2d Transform Coords!\n");1156if (Memory.C4RAM[0x1f4d] != 2)1157printf("$7f4d=%02x, expected 02 for command 2d %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1158if (READ_3WORD(Memory.C4RAM + 0x1f8f) & 0xff00ff)1159printf("2d transform coords: Unexpected value in $7f8f: %06x\n", READ_3WORD(Memory.C4RAM + 0x1f8f));1160if (READ_3WORD(Memory.C4RAM + 0x1f8c) != 0x001000)1161printf("0d transform coords: Unexpected value in $7f8c: %06x\n", READ_3WORD(Memory.C4RAM + 0x1f8c));1162#endif1163C4WFXVal = READ_WORD(Memory.C4RAM + 0x1f81);1164C4WFYVal = READ_WORD(Memory.C4RAM + 0x1f84);1165C4WFZVal = READ_WORD(Memory.C4RAM + 0x1f87);1166C4WFX2Val = Memory.C4RAM[0x1f89];1167C4WFY2Val = Memory.C4RAM[0x1f8a];1168C4WFDist = Memory.C4RAM[0x1f8b];1169C4WFScale = READ_WORD(Memory.C4RAM + 0x1f90);1170C4TransfWireFrame2();1171WRITE_WORD(Memory.C4RAM + 0x1f80, C4WFXVal);1172WRITE_WORD(Memory.C4RAM + 0x1f83, C4WFYVal);1173break;11741175case 0x40: // Sum1176{1177#ifdef DEBUGGER1178//printf("40 Sum!\n");1179if (Memory.C4RAM[0x1f4d] != 0x0e)1180printf("$7f4d=%02x, expected 0e for command 40 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1181#endif1182uint16 sum = 0;1183for (int i = 0; i < 0x800; sum += Memory.C4RAM[i++]) ;1184WRITE_WORD(Memory.C4RAM + 0x1f80, sum);1185break;1186}11871188case 0x54: // Square1189{1190#ifdef DEBUGGER1191//printf("54 Square!\n");1192if (Memory.C4RAM[0x1f4d] != 0x0e)1193printf("$7f4d=%02x, expected 0e for command 54 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1194#endif1195int64 a = SAR((int64) READ_3WORD(Memory.C4RAM + 0x1f80) << 40, 40);1196//printf("%08X%08X\n", (uint32) (a>>32), (uint32) (a&0xFFFFFFFF));1197a *= a;1198//printf("%08X%08X\n", (uint32) (a>>32), (uint32) (a&0xFFFFFFFF));1199WRITE_3WORD(Memory.C4RAM + 0x1f83, a);1200WRITE_3WORD(Memory.C4RAM + 0x1f86, (a >> 24));1201break;1202}12031204case 0x5c: // Immediate Reg1205#ifdef DEBUGGER1206//printf("5c Immediate Reg!\n");1207if (Memory.C4RAM[0x1f4d] != 0x0e)1208printf("$7f4d=%02x, expected 0e for command 5c %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1209#endif1210for (int i = 0; i < 12 * 4; i++)1211Memory.C4RAM[i] = C4TestPattern[i];1212break;12131214case 0x89: // Immediate ROM1215#ifdef DEBUGGER1216//printf("89 Immediate ROM!\n");1217if (Memory.C4RAM[0x1f4d] != 0x0e)1218printf("$7f4d=%02x, expected 0e for command 89 %02x\n", Memory.C4RAM[0x1f4d], Memory.C4RAM[0x1f4d]);1219#endif1220Memory.C4RAM[0x1f80] = 0x36;1221Memory.C4RAM[0x1f81] = 0x43;1222Memory.C4RAM[0x1f82] = 0x05;1223break;12241225default:1226#ifdef DEBUGGER1227printf("Unknown C4 command (%02x)\n", byte);1228#endif1229break;1230}1231}1232}1233else1234if (Address == 0x7f47)1235{1236#ifdef DEBUGGER1237//printf("C4 load memory %06x => %04x, %04x bytes\n", READ_3WORD(Memory.C4RAM + 0x1f40), READ_WORD(Memory.C4RAM + 0x1f45), READ_WORD(Memory.C4RAM + 0x1f43));1238if (byte != 0)1239printf("C4 load: non-0 written to $7f47! Wrote %02x\n", byte);1240if (READ_WORD(Memory.C4RAM + 0x1f45) < 0x6000 || (READ_WORD(Memory.C4RAM + 0x1f45) + READ_WORD(Memory.C4RAM + 0x1f43)) > 0x6c00)1241printf("C4 load: Dest unusual! It's %04x\n", READ_WORD(Memory.C4RAM + 0x1f45));1242#endif1243memmove(Memory.C4RAM + (READ_WORD(Memory.C4RAM + 0x1f45) & 0x1fff), C4GetMemPointer(READ_3WORD(Memory.C4RAM + 0x1f40)), READ_WORD(Memory.C4RAM + 0x1f43));1244}1245}124612471248