Path: blob/main/misc/emulator/xnes/snes9x/dsp2.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 "snes9x.h"179#include "memmap.h"180181static void DSP2_Op01 (void);182static void DSP2_Op03 (void);183static void DSP2_Op05 (void);184static void DSP2_Op06 (void);185static void DSP2_Op09 (void);186static void DSP2_Op0D (void);187188189// convert bitmap to bitplane tile190static void DSP2_Op01 (void)191{192// Op01 size is always 32 bytes input and output193// The hardware does strange things if you vary the size194195uint8 c0, c1, c2, c3;196uint8 *p1 = DSP2.parameters;197uint8 *p2a = DSP2.output;198uint8 *p2b = DSP2.output + 16; // halfway199200// Process 8 blocks of 4 bytes each201202for (int j = 0; j < 8; j++)203{204c0 = *p1++;205c1 = *p1++;206c2 = *p1++;207c3 = *p1++;208209*p2a++ = (c0 & 0x10) << 3 |210(c0 & 0x01) << 6 |211(c1 & 0x10) << 1 |212(c1 & 0x01) << 4 |213(c2 & 0x10) >> 1 |214(c2 & 0x01) << 2 |215(c3 & 0x10) >> 3 |216(c3 & 0x01);217218*p2a++ = (c0 & 0x20) << 2 |219(c0 & 0x02) << 5 |220(c1 & 0x20) |221(c1 & 0x02) << 3 |222(c2 & 0x20) >> 2 |223(c2 & 0x02) << 1 |224(c3 & 0x20) >> 4 |225(c3 & 0x02) >> 1;226227*p2b++ = (c0 & 0x40) << 1 |228(c0 & 0x04) << 4 |229(c1 & 0x40) >> 1 |230(c1 & 0x04) << 2 |231(c2 & 0x40) >> 3 |232(c2 & 0x04) |233(c3 & 0x40) >> 5 |234(c3 & 0x04) >> 2;235236*p2b++ = (c0 & 0x80) |237(c0 & 0x08) << 3 |238(c1 & 0x80) >> 2 |239(c1 & 0x08) << 1 |240(c2 & 0x80) >> 4 |241(c2 & 0x08) >> 1 |242(c3 & 0x80) >> 6 |243(c3 & 0x08) >> 3;244}245}246247// set transparent color248static void DSP2_Op03 (void)249{250DSP2.Op05Transparent = DSP2.parameters[0];251}252253// replace bitmap using transparent color254static void DSP2_Op05 (void)255{256// Overlay bitmap with transparency.257// Input:258//259// Bitmap 1: i[0] <=> i[size-1]260// Bitmap 2: i[size] <=> i[2*size-1]261//262// Output:263//264// Bitmap 3: o[0] <=> o[size-1]265//266// Processing:267//268// Process all 4-bit pixels (nibbles) in the bitmap269//270// if ( BM2_pixel == transparent_color )271// pixelout = BM1_pixel272// else273// pixelout = BM2_pixel274275// The max size bitmap is limited to 255 because the size parameter is a byte276// I think size=0 is an error. The behavior of the chip on size=0 is to277// return the last value written to DR if you read DR on Op05 with278// size = 0. I don't think it's worth implementing this quirk unless it's279// proven necessary.280281uint8 color;282uint8 c1, c2;283uint8 *p1 = DSP2.parameters;284uint8 *p2 = DSP2.parameters + DSP2.Op05Len;285uint8 *p3 = DSP2.output;286287color = DSP2.Op05Transparent & 0x0f;288289for (int32 n = 0; n < DSP2.Op05Len; n++)290{291c1 = *p1++;292c2 = *p2++;293*p3++ = (((c2 >> 4) == color) ? c1 & 0xf0: c2 & 0xf0) | (((c2 & 0x0f) == color) ? c1 & 0x0f: c2 & 0x0f);294}295}296297// reverse bitmap298static void DSP2_Op06 (void)299{300// Input:301// size302// bitmap303304for (int32 i = 0, j = DSP2.Op06Len - 1; i < DSP2.Op06Len; i++, j--)305DSP2.output[j] = (DSP2.parameters[i] << 4) | (DSP2.parameters[i] >> 4);306}307308// multiply309static void DSP2_Op09 (void)310{311DSP2.Op09Word1 = DSP2.parameters[0] | (DSP2.parameters[1] << 8);312DSP2.Op09Word2 = DSP2.parameters[2] | (DSP2.parameters[3] << 8);313314uint32 temp = DSP2.Op09Word1 * DSP2.Op09Word2;315DSP2.output[0] = temp & 0xFF;316DSP2.output[1] = (temp >> 8) & 0xFF;317DSP2.output[2] = (temp >> 16) & 0xFF;318DSP2.output[3] = (temp >> 24) & 0xFF;319}320321// scale bitmap322static void DSP2_Op0D (void)323{324// Bit accurate hardware algorithm - uses fixed point math325// This should match the DSP2 Op0D output exactly326// I wouldn't recommend using this unless you're doing hardware debug.327// In some situations it has small visual artifacts that328// are not readily apparent on a TV screen but show up clearly329// on a monitor. Use Overload's scaling instead.330// This is for hardware verification testing.331//332// One note: the HW can do odd byte scaling but since we divide333// by two to get the count of bytes this won't work well for334// odd byte scaling (in any of the current algorithm implementations).335// So far I haven't seen Dungeon Master use it.336// If it does we can adjust the parameters and code to work with it337338uint32 multiplier; // Any size int >= 32-bits339uint32 pixloc; // match size of multiplier340uint8 pixelarray[512];341342if (DSP2.Op0DInLen <= DSP2.Op0DOutLen)343multiplier = 0x10000; // In our self defined fixed point 0x10000 == 1344else345multiplier = (DSP2.Op0DInLen << 17) / ((DSP2.Op0DOutLen << 1) + 1);346347pixloc = 0;348349for (int32 i = 0; i < DSP2.Op0DOutLen * 2; i++)350{351int32 j = pixloc >> 16;352353if (j & 1)354pixelarray[i] = DSP2.parameters[j >> 1] & 0x0f;355else356pixelarray[i] = (DSP2.parameters[j >> 1] & 0xf0) >> 4;357358pixloc += multiplier;359}360361for (int32 i = 0; i < DSP2.Op0DOutLen; i++)362DSP2.output[i] = (pixelarray[i << 1] << 4) | pixelarray[(i << 1) + 1];363}364365/*366static void DSP2_Op0D (void)367{368// Overload's algorithm - use this unless doing hardware testing369370// One note: the HW can do odd byte scaling but since we divide371// by two to get the count of bytes this won't work well for372// odd byte scaling (in any of the current algorithm implementations).373// So far I haven't seen Dungeon Master use it.374// If it does we can adjust the parameters and code to work with it375376int32 pixel_offset;377uint8 pixelarray[512];378379for (int32 i = 0; i < DSP2.Op0DOutLen * 2; i++)380{381pixel_offset = (i * DSP2.Op0DInLen) / DSP2.Op0DOutLen;382383if ((pixel_offset & 1) == 0)384pixelarray[i] = DSP2.parameters[pixel_offset >> 1] >> 4;385else386pixelarray[i] = DSP2.parameters[pixel_offset >> 1] & 0x0f;387}388389for (int32 i = 0; i < DSP2.Op0DOutLen; i++)390DSP2.output[i] = (pixelarray[i << 1] << 4) | pixelarray[(i << 1) + 1];391}392*/393394void DSP2SetByte (uint8 byte, uint16 address)395{396if ((address & 0xf000) == 0x6000 || (address >= 0x8000 && address < 0xc000))397{398if (DSP2.waiting4command)399{400DSP2.command = byte;401DSP2.in_index = 0;402DSP2.waiting4command = FALSE;403404switch (byte)405{406case 0x01: DSP2.in_count = 32; break;407case 0x03: DSP2.in_count = 1; break;408case 0x05: DSP2.in_count = 1; break;409case 0x06: DSP2.in_count = 1; break;410case 0x09: DSP2.in_count = 4; break;411case 0x0D: DSP2.in_count = 2; break;412default:413#ifdef DEBUGGER414//printf("Op%02X\n", byte);415#endif416case 0x0f: DSP2.in_count = 0; break;417}418}419else420{421DSP2.parameters[DSP2.in_index] = byte;422DSP2.in_index++;423}424425if (DSP2.in_count == DSP2.in_index)426{427DSP2.waiting4command = TRUE;428DSP2.out_index = 0;429430switch (DSP2.command)431{432case 0x01:433DSP2.out_count = 32;434DSP2_Op01();435break;436437case 0x03:438DSP2_Op03();439break;440441case 0x05:442if (DSP2.Op05HasLen)443{444DSP2.Op05HasLen = FALSE;445DSP2.out_count = DSP2.Op05Len;446DSP2_Op05();447}448else449{450DSP2.Op05Len = DSP2.parameters[0];451DSP2.in_index = 0;452DSP2.in_count = 2 * DSP2.Op05Len;453DSP2.Op05HasLen = TRUE;454if (byte)455DSP2.waiting4command = FALSE;456}457458break;459460case 0x06:461if (DSP2.Op06HasLen)462{463DSP2.Op06HasLen = FALSE;464DSP2.out_count = DSP2.Op06Len;465DSP2_Op06();466}467else468{469DSP2.Op06Len = DSP2.parameters[0];470DSP2.in_index = 0;471DSP2.in_count = DSP2.Op06Len;472DSP2.Op06HasLen = TRUE;473if (byte)474DSP2.waiting4command = FALSE;475}476477break;478479case 0x09:480DSP2.out_count = 4;481DSP2_Op09();482break;483484case 0x0D:485if (DSP2.Op0DHasLen)486{487DSP2.Op0DHasLen = FALSE;488DSP2.out_count = DSP2.Op0DOutLen;489DSP2_Op0D();490}491else492{493DSP2.Op0DInLen = DSP2.parameters[0];494DSP2.Op0DOutLen = DSP2.parameters[1];495DSP2.in_index = 0;496DSP2.in_count = (DSP2.Op0DInLen + 1) >> 1;497DSP2.Op0DHasLen = TRUE;498if (byte)499DSP2.waiting4command = FALSE;500}501502break;503504case 0x0f:505default:506break;507}508}509}510}511512uint8 DSP2GetByte (uint16 address)513{514uint8 t;515516if ((address & 0xf000) == 0x6000 || (address >= 0x8000 && address < 0xc000))517{518if (DSP2.out_count)519{520t = (uint8) DSP2.output[DSP2.out_index];521DSP2.out_index++;522if (DSP2.out_count == DSP2.out_index)523DSP2.out_count = 0;524}525else526t = 0xff;527}528else529t = 0x80;530531return (t);532}533534535