📄 c4emu.cpp
字号:
/******************************************************************************* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator. (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and Jerremy Koot (jkoot@snes9x.com) (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net) (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net), funkyass (funkyass@spam.shaw.ca), Joel Yliluoma (http://iki.fi/bisqwit/) Kris Bleakley (codeviolation@hotmail.com), Matthew Kendora, Nach (n-a-c-h@users.sourceforge.net), Peter Bortas (peter@bortas.org) and zones (kasumitokoduck@yahoo.com) C4 x86 assembler and some C emulation code (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com), _Demo_ (_demo_@zsnes.com), and Nach C4 C++ code (c) Copyright 2003 Brad Jorsch DSP-1 emulator code (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson, John Weidman, neviksti (neviksti@hotmail.com), Kris Bleakley, Andreas Naive DSP-2 emulator code (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and Lord Nightmare (lord_nightmare@users.sourceforge.net OBC1 emulator code (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and Kris Bleakley Ported from x86 assembler to C by sanmaiwashi SPC7110 and RTC C++ emulator code (c) Copyright 2002 Matthew Kendora with research by zsKnight, John Weidman, and Dark Force S-DD1 C emulator code (c) Copyright 2003 Brad Jorsch with research by Andreas Naive and John Weidman S-RTC C emulator code (c) Copyright 2001 John Weidman ST010 C++ emulator code (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora Super FX x86 assembler emulator code (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault Super FX C emulator code (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman SH assembler code partly based on x86 assembler code (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) Specific ports contains the works of other authors. See headers in individual files. Snes9x homepage: http://www.snes9x.com Permission to use, copy, modify and distribute Snes9x in both binary and source form, for non-commercial purposes, is hereby granted without fee, providing that this license information and copyright notice appear with all copies and any derived work. This software is provided 'as-is', without any express or implied warranty. In no event shall the authors be held liable for any damages arising from the use of this software. Snes9x is freeware for PERSONAL USE only. Commercial users should seek permission of the copyright holders first. Commercial use includes charging money for Snes9x or software derived from Snes9x. The copyright holders request that bug fixes and improvements to the code should be forwarded to them so everyone can benefit from the modifications in future versions. Super NES and Super Nintendo Entertainment System are trademarks of Nintendo Co., Limited and its subsidiary companies.*******************************************************************************/#ifdef HAVE_CONFIG_H #include <config.h>#endif#include <math.h>#include "snes9x.h"#include "sar.h"#include "memmap.h"#include "ppu.h"#include "c4.h"void S9xInitC4 (){ // Stupid zsnes code, we can't do the logical thing without breaking // savestates// Memory.C4RAM = &Memory.FillRAM [0x6000]; memset(Memory.C4RAM, 0, 0x2000);}uint8 S9xGetC4 (uint16 Address){#ifdef DEBUGGER if(Settings.BGLayering) printf("%02x from %04x\n", Memory.C4RAM[Address-0x6000], Address);#endif return (Memory.C4RAM [Address-0x6000]);}static uint8 C4TestPattern [12 * 4] ={ 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x80, 0xff, 0xff, 0x7f, 0x00, 0x80, 0x00, 0xff, 0x7f, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0xff, 0x00, 0x00, 0x01, 0xff, 0xff, 0xfe, 0x00, 0x01, 0x00, 0xff, 0xfe, 0x00};static void C4ConvOAM(void){ uint8 *OAMptr=Memory.C4RAM+(Memory.C4RAM[0x626]<<2); for(uint8 *i=Memory.C4RAM+0x1fd; i>OAMptr; i-=4){ // Clear OAM-to-be *i=0xe0; } uint16 globalX, globalY; uint8 *OAMptr2; int16 SprX, SprY; uint8 SprName, SprAttr; uint8 SprCount; globalX=READ_WORD(Memory.C4RAM+0x0621); globalY=READ_WORD(Memory.C4RAM+0x0623); OAMptr2=Memory.C4RAM+0x200+(Memory.C4RAM[0x626]>>2);#ifdef DEBUGGER if(Memory.C4RAM[0x625]!=0) printf("$6625=%02x, expected 00\n", Memory.C4RAM[0x625]); if((Memory.C4RAM[0x626]>>2)!=Memory.C4RAM[0x629]) printf("$6629=%02x, expected %02x\n", Memory.C4RAM[0x629], (Memory.C4RAM[0x626]>>2)); if(((uint16)Memory.C4RAM[0x626]<<2)!=READ_WORD(Memory.C4RAM+0x627)) printf("$6627=%04x, expected %04x\n", READ_WORD(Memory.C4RAM+0x627), ((uint16)Memory.C4RAM[0x626]<<2));#endif if(Memory.C4RAM[0x0620]!=0){ SprCount=128-Memory.C4RAM[0x626]; uint8 offset=(Memory.C4RAM[0x626]&3)*2; for(int prio=0x30; prio>=0; prio-=0x10){ uint8 *srcptr=Memory.C4RAM+0x220; for(int i=Memory.C4RAM[0x0620]; i>0 && SprCount>0; i--, srcptr+=16){ if((srcptr[4]&0x30)!=prio) continue; SprX=READ_WORD(srcptr)-globalX; SprY=READ_WORD(srcptr+2)-globalY; SprName=srcptr[5]; SprAttr=srcptr[4] | srcptr[0x06]; // XXX: mask bits? uint8 *sprptr=S9xGetMemPointer(READ_3WORD(srcptr+7)); if(*sprptr!=0){ int16 X, Y; for(int SprCnt=*sprptr++; SprCnt>0 && SprCount>0; SprCnt--, sprptr+=4){ X=(int8)sprptr[1]; if(SprAttr&0x40){ // flip X X=-X-((sprptr[0]&0x20)?16:8); } X+=SprX; if(X>=-16 && X<=272){ Y=(int8)sprptr[2]; if(SprAttr&0x80){ Y=-Y-((sprptr[0]&0x20)?16:8); } Y+=SprY; if(Y>=-16 && Y<=224){ OAMptr[0]=X&0xff; OAMptr[1]=(uint8)Y; OAMptr[2]=SprName+sprptr[3]; OAMptr[3]=SprAttr^(sprptr[0]&0xc0); // XXX: Carry from SprName addition? *OAMptr2 &= ~(3<<offset); if(X&0x100) *OAMptr2 |= 1<<offset; if(sprptr[0]&0x20) *OAMptr2 |= 2<<offset; OAMptr+=4; SprCount--; offset=(offset+2)&6; if(offset==0) OAMptr2++; } } } } else if(SprCount>0){ OAMptr[0]=(uint8)SprX; OAMptr[1]=(uint8)SprY; OAMptr[2]=SprName; OAMptr[3]=SprAttr; *OAMptr2 &= ~(3<<offset); if(SprX&0x100) *OAMptr2 |= 3<<offset; else *OAMptr2 |= 2<<offset; OAMptr+=4; SprCount--; offset=(offset+2)&6; if(offset==0) OAMptr2++; } } } } // XXX: Copy to OAM? I doubt it.}static void C4DoScaleRotate(int row_padding){ int16 A, B, C, D; // Calculate matrix int32 XScale=READ_WORD(Memory.C4RAM+0x1f8f); if(XScale&0x8000) XScale=0x7fff; int32 YScale=READ_WORD(Memory.C4RAM+0x1f92); if(YScale&0x8000) YScale=0x7fff; if(READ_WORD(Memory.C4RAM+0x1f80)==0) { // no rotation // XXX: only do this for C and D? // XXX: and then only when YScale is 0x1000? A=(int16)XScale; B=0; C=0; D=(int16)YScale; } else if(READ_WORD(Memory.C4RAM+0x1f80)==128){ // 90 degree rotation // XXX: Really do this? A=0; B=(int16)(-YScale); C=(int16)XScale; D=0; } else if(READ_WORD(Memory.C4RAM+0x1f80)==256){ // 180 degree rotation // XXX: Really do this? A=(int16)(-XScale); B=0; C=0; D=(int16)(-YScale); } else if(READ_WORD(Memory.C4RAM+0x1f80)==384){ // 270 degree rotation // XXX: Really do this? A=0; B=(int16)YScale; C=(int16)(-XScale); D=0; } else { A=(int16)SAR(C4CosTable[READ_WORD(Memory.C4RAM+0x1f80)&0x1ff]*XScale, 15); B=(int16)(-SAR(C4SinTable[READ_WORD(Memory.C4RAM+0x1f80)&0x1ff]*YScale, 15)); C=(int16)SAR(C4SinTable[READ_WORD(Memory.C4RAM+0x1f80)&0x1ff]*XScale, 15); D=(int16)SAR(C4CosTable[READ_WORD(Memory.C4RAM+0x1f80)&0x1ff]*YScale, 15); } // Calculate Pixel Resolution uint8 w=Memory.C4RAM[0x1f89]&~7; uint8 h=Memory.C4RAM[0x1f8c]&~7;// printf("%dx%d XScale=%04x YScale=%04x angle=%03x\n", w, h, XScale, YScale, READ_WORD(Memory.C4RAM+0x1f80)&0x1ff);// printf("Matrix: [%10g %10g] [%04x %04x]\n", A/4096.0, B/4096.0, A&0xffff, B&0xffff);// printf(" [%10g %10g] [%04x %04x]\n", C/4096.0, D/4096.0, C&0xffff, D&0xffff); // Clear the output RAM memset(Memory.C4RAM, 0, (w+row_padding/4)*h/2); int32 Cx=(int16)READ_WORD(Memory.C4RAM+0x1f83); int32 Cy=(int16)READ_WORD(Memory.C4RAM+0x1f86);#ifdef DEBUGGER if(Memory.C4RAM[0x1f97]!=0) printf("$7f97=%02x, expected 00\n", Memory.C4RAM[0x1f97]); if((Cx&~1)!=w/2 || (Cy&~1)!=h/2) printf("Center is not middle of image! (%d, %d) != (%d, %d)\n", Cx, Cy, w/2, h/2);#endif // Calculate start position (i.e. (Ox, Oy) = (0, 0)) // The low 12 bits are fractional, so (Cx<<12) gives us the Cx we want in // the function. We do Cx*A etc normally because the matrix parameters // already have the fractional parts. int32 LineX=(Cx<<12) - Cx*A - Cx*B; int32 LineY=(Cy<<12) - Cy*C - Cy*D; // Start loop uint32 X, Y; uint8 byte; int outidx=0; uint8 bit=0x80; for(int y=0; y<h; y++){ X=LineX; Y=LineY; for(int x=0; x<w; x++){ if((X>>12)>=w || (Y>>12)>=h){ byte=0; } else { uint32 addr=(Y>>12)*w+(X>>12); byte=Memory.C4RAM[0x600+(addr>>1)]; if(addr&1) byte>>=4; } // De-bitplanify if(byte&1) Memory.C4RAM[outidx]|=bit; if(byte&2) Memory.C4RAM[outidx+1]|=bit; if(byte&4) Memory.C4RAM[outidx+16]|=bit; if(byte&8) Memory.C4RAM[outidx+17]|=bit; bit>>=1; if(bit==0){ bit=0x80; outidx+=32; } X+=A; // Add 1 to output x => add an A and a C Y+=C; } outidx+=2+row_padding; if(outidx&0x10){ outidx&=~0x10; } else { outidx-=w*4+row_padding; } LineX+=B; // Add 1 to output y => add a B and a D LineY+=D; }}static void C4DrawLine(int32 X1, int32 Y1, int16 Z1, int32 X2, int32 Y2, int16 Z2, uint8 Color){ // Transform coordinates C4WFXVal=(short)X1; C4WFYVal=(short)Y1; C4WFZVal=Z1; C4WFScale=Memory.C4RAM[0x1f90]; C4WFX2Val=Memory.C4RAM[0x1f86]; C4WFY2Val=Memory.C4RAM[0x1f87]; C4WFDist=Memory.C4RAM[0x1f88]; C4TransfWireFrame2(); X1=(C4WFXVal+48)<<8; Y1=(C4WFYVal+48)<<8;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -