From a963483ee01d0cfcf05062cc3e1c0202d8204fe4 Mon Sep 17 00:00:00 2001 From: mudlord Date: Tue, 30 Oct 2007 19:04:22 +0000 Subject: [PATCH] Added VBALink to the SVN code. Note that this is completely untested git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@2 a31d4220-a93d-0410-bf67-fe4944624d44 --- Gb_Apu/Blip_Buffer.cpp | 406 ++ Gb_Apu/Blip_Buffer.h | 354 + Gb_Apu/Blip_Synth.h | 204 + Gb_Apu/COPYING | 504 ++ Gb_Apu/Gb_Apu.cpp | 318 + Gb_Apu/Gb_Apu.h | 99 + Gb_Apu/Gb_Oscs.cpp | 360 + Gb_Apu/Gb_Oscs.h | 90 + Gb_Apu/Multi_Buffer.cpp | 215 + Gb_Apu/Multi_Buffer.h | 175 + Gb_Apu/blargg_common.h | 242 + Gb_Apu/blargg_endian.h | 156 + Gb_Apu/blargg_source.h | 76 + Gb_Apu/boost/config.hpp | 13 + Gb_Apu/boost/cstdint.hpp | 42 + Gb_Apu/boost/static_assert.hpp | 22 + SDL.dll | Bin 0 -> 225280 bytes VBA.sln | 45 + VBA.sln.old | 47 + VBA.suo | Bin 0 -> 53248 bytes VBA.vcproj | 1580 +++++ VBA.vcproj.7.10.old | 1116 +++ gbafilter.cpp | 227 + gbafilter.h | 5 + libpng/libpng.vcproj | 226 + libpng/libpng.vcproj.7.10.old | 158 + libpng/png.c | 828 +++ libpng/png.h | 3419 ++++++++++ libpng/pngconf.h | 1437 ++++ libpng/pngerror.c | 295 + libpng/pngget.c | 934 +++ libpng/pngmem.c | 595 ++ libpng/pngpread.c | 1573 +++++ libpng/pngread.c | 1456 ++++ libpng/pngrio.c | 161 + libpng/pngrtran.c | 4177 ++++++++++++ libpng/pngrutil.c | 3124 +++++++++ libpng/pngset.c | 1219 ++++ libpng/pngtrans.c | 650 ++ libpng/pngvcrd.c | 3903 +++++++++++ libpng/pngwio.c | 228 + libpng/pngwrite.c | 1464 ++++ libpng/pngwtran.c | 563 ++ libpng/pngwutil.c | 2730 ++++++++ libresample-0.1.3/LICENSE.txt | 463 ++ libresample-0.1.3/Makefile.in | 57 + libresample-0.1.3/README.txt | 84 + libresample-0.1.3/config.guess | 1308 ++++ libresample-0.1.3/config.sub | 1413 ++++ libresample-0.1.3/configure | 2937 ++++++++ libresample-0.1.3/configure.in | 66 + libresample-0.1.3/include/libresample.h | 44 + libresample-0.1.3/install-sh | 251 + libresample-0.1.3/src/configtemplate.h | 7 + libresample-0.1.3/src/filterkit.c | 215 + libresample-0.1.3/src/filterkit.h | 28 + libresample-0.1.3/src/resample.c | 347 + libresample-0.1.3/src/resample_defs.h | 86 + libresample-0.1.3/src/resamplesubs.c | 123 + libresample-0.1.3/tests/compareresample.c | 183 + libresample-0.1.3/tests/resample-sndfile.c | 213 + libresample-0.1.3/tests/testresample.c | 182 + libresample-0.1.3/win/libresample.dsp | 116 + res/DevInfo.txt | 20 + res/Known Bugs.txt | 4 + res/ReadMe.txt | 81 + res/VBA.APS | Bin 0 -> 280832 bytes res/VBA.ico | Bin 0 -> 120582 bytes res/VBA.manifest | 22 + res/VBA.rc | 2235 ++++++ res/gpl.txt | 340 + res/resource.h | 748 ++ snd_interp.cpp | 754 ++ snd_interp.h | 30 + src/2xSaI.cpp | 1274 ++++ src/2xSaImmx.asm | 2109 ++++++ src/CheatSearch.cpp | 329 + src/CheatSearch.h | 73 + src/Cheats.cpp | 2770 ++++++++ src/Cheats.h | 55 + src/EEprom.cpp | 191 + src/EEprom.h | 38 + src/Flash.cpp | 259 + src/Flash.h | 35 + src/GBA.cpp | 4027 +++++++++++ src/GBA.h | 147 + src/GBAinline.h | 427 ++ src/Gb_Apu/Blip_Buffer.cpp | 406 ++ src/Gb_Apu/Blip_Buffer.h | 354 + src/Gb_Apu/Blip_Synth.h | 204 + src/Gb_Apu/COPYING | 504 ++ src/Gb_Apu/Gb_Apu.cpp | 318 + src/Gb_Apu/Gb_Apu.h | 99 + src/Gb_Apu/Gb_Oscs.cpp | 360 + src/Gb_Apu/Gb_Oscs.h | 90 + src/Gb_Apu/Multi_Buffer.cpp | 215 + src/Gb_Apu/Multi_Buffer.h | 175 + src/Gb_Apu/blargg_common.h | 242 + src/Gb_Apu/blargg_endian.h | 156 + src/Gb_Apu/blargg_source.h | 76 + src/Gb_Apu/boost/config.hpp | 13 + src/Gb_Apu/boost/cstdint.hpp | 42 + src/Gb_Apu/boost/static_assert.hpp | 22 + src/Gfx.cpp | 47 + src/Gfx.h | 1581 +++++ src/Globals.cpp | 135 + src/Globals.h | 151 + src/Link.cpp | 1057 +++ src/Link.h | 121 + src/Mode0.cpp | 552 ++ src/Mode1.cpp | 513 ++ src/Mode2.cpp | 478 ++ src/Mode3.cpp | 405 ++ src/Mode4.cpp | 402 ++ src/Mode5.cpp | 405 ++ src/NLS.h | 62 + src/Port.h | 75 + src/RTC.cpp | 220 + src/RTC.h | 31 + src/Sound.cpp | 1177 ++++ src/Sound.h | 95 + src/Sram.cpp | 39 + src/Sram.h | 27 + src/System.h | 125 + src/Text.cpp | 157 + src/Text.h | 21 + src/Util.cpp | 1115 +++ src/Util.h | 65 + src/admame.cpp | 1010 +++ src/agbprint.cpp | 99 + src/agbprint.h | 27 + src/arm-new.h | 7188 ++++++++++++++++++++ src/armdis.cpp | 742 ++ src/armdis.h | 33 + src/bilinear.cpp | 420 ++ src/bios.cpp | 1156 ++++ src/bios.h | 46 + src/elf.cpp | 2999 ++++++++ src/elf.h | 283 + src/gb/GB.cpp | 3217 +++++++++ src/gb/gb.h | 60 + src/gb/gbCheats.cpp | 462 ++ src/gb/gbCheats.h | 58 + src/gb/gbCodes.h | 1407 ++++ src/gb/gbCodesCB.h | 1288 ++++ src/gb/gbDis.cpp | 249 + src/gb/gbGfx.cpp | 490 ++ src/gb/gbGlobals.cpp | 54 + src/gb/gbGlobals.h | 68 + src/gb/gbMemory.cpp | 967 +++ src/gb/gbMemory.h | 149 + src/gb/gbPrinter.cpp | 229 + src/gb/gbPrinter.h | 20 + src/gb/gbSGB.cpp | 917 +++ src/gb/gbSGB.h | 39 + src/gb/gbSound.cpp | 525 ++ src/gb/gbSound.h | 61 + src/gbafilter.cpp | 227 + src/gbafilter.h | 5 + src/getopt.cpp | 1048 +++ src/getopt.h | 144 + src/getopt1.cpp | 180 + src/hq2x.cpp | 604 ++ src/hq2x.dat | 1960 ++++++ src/hq2x.h | 1824 +++++ src/hq2x3.dat | 2478 +++++++ src/hq2x4.dat | 2748 ++++++++ src/hq3x.dat | 2913 ++++++++ src/hq3x32.cpp | 96 + src/hq3x32.h | 3634 ++++++++++ src/hq3x_16.asm | 2527 +++++++ src/hq3x_32.asm | 2571 +++++++ src/hq4x.cpp | 212 + src/hq4x.dat | 4244 ++++++++++++ src/hq4x.h | 40 + src/hq_shared32.cpp | 375 + src/hq_shared32.h | 90 + src/interframe.cpp | 648 ++ src/interp.h | 299 + src/lq2x.dat | 1318 ++++ src/lq2x.h | 1284 ++++ src/lq2x3.dat | 1574 +++++ src/lq2x4.dat | 1746 +++++ src/lq3x.cpp | 209 + src/lq3x.dat | 1759 +++++ src/lq3x.h | 40 + src/lq4x.cpp | 212 + src/lq4x.dat | 2506 +++++++ src/lq4x.h | 40 + src/memgzio.cpp | 659 ++ src/memgzio.h | 17 + src/motionblur.cpp | 192 + src/pixel.cpp | 150 + src/portable.cpp | 81 + src/portable.h | 220 + src/remote.cpp | 698 ++ src/scanline.cpp | 230 + src/simpleFilter.cpp | 303 + src/snd_interp.cpp | 619 ++ src/snd_interp.h | 30 + src/thumb.h | 2474 +++++++ src/unzip.cpp | 1270 ++++ src/unzip.h | 294 + src/win32/AVIWrite.cpp | 187 + src/win32/AVIWrite.h | 49 + src/win32/AboutDialog.cpp | 77 + src/win32/AboutDialog.h | 73 + src/win32/AccelEditor.cpp | 289 + src/win32/AccelEditor.h | 79 + src/win32/AcceleratorManager.cpp | 784 +++ src/win32/AcceleratorManager.h | 142 + src/win32/Associate.cpp | 128 + src/win32/Associate.h | 73 + src/win32/BitmapControl.cpp | 287 + src/win32/BitmapControl.h | 96 + src/win32/BugReport.cpp | 248 + src/win32/BugReport.h | 68 + src/win32/CmdAccelOb.cpp | 526 ++ src/win32/CmdAccelOb.h | 113 + src/win32/ColorButton.cpp | 120 + src/win32/ColorButton.h | 74 + src/win32/ColorControl.cpp | 103 + src/win32/ColorControl.h | 74 + src/win32/Commands.cpp | 253 + src/win32/Direct3D.cpp | 950 +++ src/win32/DirectDraw.cpp | 960 +++ src/win32/DirectDraw.cpp.orig | 845 +++ src/win32/DirectInput.cpp | 1060 +++ src/win32/DirectInput.cpp.orig | 1056 +++ src/win32/DirectSound.cpp | 373 + src/win32/Directories.cpp | 257 + src/win32/Directories.h | 83 + src/win32/Disassemble.cpp | 349 + src/win32/Disassemble.h | 94 + src/win32/Display.h | 44 + src/win32/ExportGSASnapshot.cpp | 117 + src/win32/ExportGSASnapshot.h | 70 + src/win32/FileDlg.cpp | 197 + src/win32/FileDlg.h | 66 + src/win32/GBACheats.cpp | 1172 ++++ src/win32/GBACheats.h | 291 + src/win32/GBCheatsDlg.cpp | 1009 +++ src/win32/GBCheatsDlg.h | 217 + src/win32/GBColorDlg.cpp | 255 + src/win32/GBColorDlg.h | 81 + src/win32/GBDisassemble.cpp | 262 + src/win32/GBDisassemble.h | 89 + src/win32/GBMapView.cpp | 577 ++ src/win32/GBMapView.h | 102 + src/win32/GBMemoryViewerDlg.cpp | 421 ++ src/win32/GBMemoryViewerDlg.h | 94 + src/win32/GBOamView.cpp | 599 ++ src/win32/GBOamView.h | 103 + src/win32/GBPaletteView.cpp | 245 + src/win32/GBPaletteView.h | 90 + src/win32/GBPrinterDlg.cpp | 494 ++ src/win32/GBPrinterDlg.h | 81 + src/win32/GBTileView.cpp | 524 ++ src/win32/GBTileView.h | 104 + src/win32/GDBConnection.cpp | 261 + src/win32/GDBConnection.h | 114 + src/win32/GDIDisplay.cpp | 479 ++ src/win32/GSACodeSelect.cpp | 120 + src/win32/GSACodeSelect.h | 69 + src/win32/Hyperlink.cpp | 118 + src/win32/Hyperlink.h | 75 + src/win32/IOViewer.cpp | 202 + src/win32/IOViewer.h | 78 + src/win32/IOViewerRegs.h | 2087 ++++++ src/win32/IUpdate.h | 27 + src/win32/Input.h | 55 + src/win32/Joypad.cpp | 435 ++ src/win32/Joypad.h | 168 + src/win32/KeyboardEdit.cpp | 148 + src/win32/KeyboardEdit.h | 87 + src/win32/LangSelect.cpp | 97 + src/win32/LangSelect.h | 68 + src/win32/LinkOptions.cpp | 423 ++ src/win32/LinkOptions.h | 207 + src/win32/Logging.cpp | 287 + src/win32/Logging.h | 98 + src/win32/MainWnd.cpp | 1137 ++++ src/win32/MainWnd.h | 442 ++ src/win32/MainWndCheats.cpp | 165 + src/win32/MainWndFile.cpp | 897 +++ src/win32/MainWndHelp.cpp | 46 + src/win32/MainWndOptions.cpp | 1770 +++++ src/win32/MainWndTools.cpp | 603 ++ src/win32/MapView.cpp | 1026 +++ src/win32/MapView.h | 122 + src/win32/MaxScale.cpp | 93 + src/win32/MaxScale.h | 67 + src/win32/MemoryViewer.cpp | 622 ++ src/win32/MemoryViewer.h | 114 + src/win32/MemoryViewerAddressSize.cpp | 137 + src/win32/MemoryViewerAddressSize.h | 75 + src/win32/MemoryViewerDlg.cpp | 424 ++ src/win32/MemoryViewerDlg.h | 94 + src/win32/ModeConfirm.cpp | 113 + src/win32/ModeConfirm.h | 71 + src/win32/OamView.cpp | 669 ++ src/win32/OamView.h | 100 + src/win32/OpenGL.cpp | 552 ++ src/win32/PaletteView.cpp | 242 + src/win32/PaletteView.h | 90 + src/win32/PaletteViewControl.cpp | 409 ++ src/win32/PaletteViewControl.h | 90 + src/win32/Reg.cpp | 369 + src/win32/Reg.h | 37 + src/win32/ResizeDlg.cpp | 561 ++ src/win32/ResizeDlg.h | 59 + src/win32/RewindInterval.cpp | 100 + src/win32/RewindInterval.h | 68 + src/win32/RomInfo.cpp | 585 ++ src/win32/RomInfo.h | 101 + src/win32/Sound.h | 35 + src/win32/StringTokenizer.cpp | 69 + src/win32/StringTokenizer.h | 44 + src/win32/Throttle.cpp | 87 + src/win32/Throttle.h | 67 + src/win32/TileView.cpp | 587 ++ src/win32/TileView.h | 109 + src/win32/UniVideoModeDlg.cpp | 278 + src/win32/UniVideoModeDlg.h | 47 + src/win32/VBA.cpp | 2294 +++++++ src/win32/VBA.h | 280 + src/win32/VideoMode.cpp | 365 + src/win32/VideoMode.h | 102 + src/win32/WavWriter.cpp | 117 + src/win32/WavWriter.h | 52 + src/win32/WinHelper.h | 232 + src/win32/WinResUtil.cpp | 109 + src/win32/WinResUtil.h | 31 + src/win32/ZoomControl.cpp | 190 + src/win32/ZoomControl.h | 78 + src/win32/stdafx.cpp | 1 + src/win32/stdafx.h | 37 + zlib/adler32.c | 149 + zlib/compress.c | 79 + zlib/crc32.c | 423 ++ zlib/crc32.h | 441 ++ zlib/deflate.c | 1736 +++++ zlib/deflate.h | 331 + zlib/gzio.c | 1026 +++ zlib/infback.c | 623 ++ zlib/inffast.c | 318 + zlib/inffast.h | 11 + zlib/inffixed.h | 94 + zlib/inflate.c | 1368 ++++ zlib/inflate.h | 115 + zlib/inftrees.c | 329 + zlib/inftrees.h | 55 + zlib/trees.c | 1219 ++++ zlib/trees.h | 128 + zlib/uncompr.c | 61 + zlib/zconf.h | 332 + zlib/zlib.h | 1357 ++++ zlib/zlib.vcproj | 239 + zlib/zlib.vcproj.7.10.old | 167 + zlib/zutil.c | 318 + zlib/zutil.h | 269 + 361 files changed, 194240 insertions(+) create mode 100644 Gb_Apu/Blip_Buffer.cpp create mode 100644 Gb_Apu/Blip_Buffer.h create mode 100644 Gb_Apu/Blip_Synth.h create mode 100644 Gb_Apu/COPYING create mode 100644 Gb_Apu/Gb_Apu.cpp create mode 100644 Gb_Apu/Gb_Apu.h create mode 100644 Gb_Apu/Gb_Oscs.cpp create mode 100644 Gb_Apu/Gb_Oscs.h create mode 100644 Gb_Apu/Multi_Buffer.cpp create mode 100644 Gb_Apu/Multi_Buffer.h create mode 100644 Gb_Apu/blargg_common.h create mode 100644 Gb_Apu/blargg_endian.h create mode 100644 Gb_Apu/blargg_source.h create mode 100644 Gb_Apu/boost/config.hpp create mode 100644 Gb_Apu/boost/cstdint.hpp create mode 100644 Gb_Apu/boost/static_assert.hpp create mode 100644 SDL.dll create mode 100644 VBA.sln create mode 100644 VBA.sln.old create mode 100644 VBA.suo create mode 100644 VBA.vcproj create mode 100644 VBA.vcproj.7.10.old create mode 100644 gbafilter.cpp create mode 100644 gbafilter.h create mode 100644 libpng/libpng.vcproj create mode 100644 libpng/libpng.vcproj.7.10.old create mode 100644 libpng/png.c create mode 100644 libpng/png.h create mode 100644 libpng/pngconf.h create mode 100644 libpng/pngerror.c create mode 100644 libpng/pngget.c create mode 100644 libpng/pngmem.c create mode 100644 libpng/pngpread.c create mode 100644 libpng/pngread.c create mode 100644 libpng/pngrio.c create mode 100644 libpng/pngrtran.c create mode 100644 libpng/pngrutil.c create mode 100644 libpng/pngset.c create mode 100644 libpng/pngtrans.c create mode 100644 libpng/pngvcrd.c create mode 100644 libpng/pngwio.c create mode 100644 libpng/pngwrite.c create mode 100644 libpng/pngwtran.c create mode 100644 libpng/pngwutil.c create mode 100644 libresample-0.1.3/LICENSE.txt create mode 100644 libresample-0.1.3/Makefile.in create mode 100644 libresample-0.1.3/README.txt create mode 100644 libresample-0.1.3/config.guess create mode 100644 libresample-0.1.3/config.sub create mode 100644 libresample-0.1.3/configure create mode 100644 libresample-0.1.3/configure.in create mode 100644 libresample-0.1.3/include/libresample.h create mode 100644 libresample-0.1.3/install-sh create mode 100644 libresample-0.1.3/src/configtemplate.h create mode 100644 libresample-0.1.3/src/filterkit.c create mode 100644 libresample-0.1.3/src/filterkit.h create mode 100644 libresample-0.1.3/src/resample.c create mode 100644 libresample-0.1.3/src/resample_defs.h create mode 100644 libresample-0.1.3/src/resamplesubs.c create mode 100644 libresample-0.1.3/tests/compareresample.c create mode 100644 libresample-0.1.3/tests/resample-sndfile.c create mode 100644 libresample-0.1.3/tests/testresample.c create mode 100644 libresample-0.1.3/win/libresample.dsp create mode 100644 res/DevInfo.txt create mode 100644 res/Known Bugs.txt create mode 100644 res/ReadMe.txt create mode 100644 res/VBA.APS create mode 100644 res/VBA.ico create mode 100644 res/VBA.manifest create mode 100644 res/VBA.rc create mode 100644 res/gpl.txt create mode 100644 res/resource.h create mode 100644 snd_interp.cpp create mode 100644 snd_interp.h create mode 100644 src/2xSaI.cpp create mode 100644 src/2xSaImmx.asm create mode 100644 src/CheatSearch.cpp create mode 100644 src/CheatSearch.h create mode 100644 src/Cheats.cpp create mode 100644 src/Cheats.h create mode 100644 src/EEprom.cpp create mode 100644 src/EEprom.h create mode 100644 src/Flash.cpp create mode 100644 src/Flash.h create mode 100644 src/GBA.cpp create mode 100644 src/GBA.h create mode 100644 src/GBAinline.h create mode 100644 src/Gb_Apu/Blip_Buffer.cpp create mode 100644 src/Gb_Apu/Blip_Buffer.h create mode 100644 src/Gb_Apu/Blip_Synth.h create mode 100644 src/Gb_Apu/COPYING create mode 100644 src/Gb_Apu/Gb_Apu.cpp create mode 100644 src/Gb_Apu/Gb_Apu.h create mode 100644 src/Gb_Apu/Gb_Oscs.cpp create mode 100644 src/Gb_Apu/Gb_Oscs.h create mode 100644 src/Gb_Apu/Multi_Buffer.cpp create mode 100644 src/Gb_Apu/Multi_Buffer.h create mode 100644 src/Gb_Apu/blargg_common.h create mode 100644 src/Gb_Apu/blargg_endian.h create mode 100644 src/Gb_Apu/blargg_source.h create mode 100644 src/Gb_Apu/boost/config.hpp create mode 100644 src/Gb_Apu/boost/cstdint.hpp create mode 100644 src/Gb_Apu/boost/static_assert.hpp create mode 100644 src/Gfx.cpp create mode 100644 src/Gfx.h create mode 100644 src/Globals.cpp create mode 100644 src/Globals.h create mode 100644 src/Link.cpp create mode 100644 src/Link.h create mode 100644 src/Mode0.cpp create mode 100644 src/Mode1.cpp create mode 100644 src/Mode2.cpp create mode 100644 src/Mode3.cpp create mode 100644 src/Mode4.cpp create mode 100644 src/Mode5.cpp create mode 100644 src/NLS.h create mode 100644 src/Port.h create mode 100644 src/RTC.cpp create mode 100644 src/RTC.h create mode 100644 src/Sound.cpp create mode 100644 src/Sound.h create mode 100644 src/Sram.cpp create mode 100644 src/Sram.h create mode 100644 src/System.h create mode 100644 src/Text.cpp create mode 100644 src/Text.h create mode 100644 src/Util.cpp create mode 100644 src/Util.h create mode 100644 src/admame.cpp create mode 100644 src/agbprint.cpp create mode 100644 src/agbprint.h create mode 100644 src/arm-new.h create mode 100644 src/armdis.cpp create mode 100644 src/armdis.h create mode 100644 src/bilinear.cpp create mode 100644 src/bios.cpp create mode 100644 src/bios.h create mode 100644 src/elf.cpp create mode 100644 src/elf.h create mode 100644 src/gb/GB.cpp create mode 100644 src/gb/gb.h create mode 100644 src/gb/gbCheats.cpp create mode 100644 src/gb/gbCheats.h create mode 100644 src/gb/gbCodes.h create mode 100644 src/gb/gbCodesCB.h create mode 100644 src/gb/gbDis.cpp create mode 100644 src/gb/gbGfx.cpp create mode 100644 src/gb/gbGlobals.cpp create mode 100644 src/gb/gbGlobals.h create mode 100644 src/gb/gbMemory.cpp create mode 100644 src/gb/gbMemory.h create mode 100644 src/gb/gbPrinter.cpp create mode 100644 src/gb/gbPrinter.h create mode 100644 src/gb/gbSGB.cpp create mode 100644 src/gb/gbSGB.h create mode 100644 src/gb/gbSound.cpp create mode 100644 src/gb/gbSound.h create mode 100644 src/gbafilter.cpp create mode 100644 src/gbafilter.h create mode 100644 src/getopt.cpp create mode 100644 src/getopt.h create mode 100644 src/getopt1.cpp create mode 100644 src/hq2x.cpp create mode 100644 src/hq2x.dat create mode 100644 src/hq2x.h create mode 100644 src/hq2x3.dat create mode 100644 src/hq2x4.dat create mode 100644 src/hq3x.dat create mode 100644 src/hq3x32.cpp create mode 100644 src/hq3x32.h create mode 100644 src/hq3x_16.asm create mode 100644 src/hq3x_32.asm create mode 100644 src/hq4x.cpp create mode 100644 src/hq4x.dat create mode 100644 src/hq4x.h create mode 100644 src/hq_shared32.cpp create mode 100644 src/hq_shared32.h create mode 100644 src/interframe.cpp create mode 100644 src/interp.h create mode 100644 src/lq2x.dat create mode 100644 src/lq2x.h create mode 100644 src/lq2x3.dat create mode 100644 src/lq2x4.dat create mode 100644 src/lq3x.cpp create mode 100644 src/lq3x.dat create mode 100644 src/lq3x.h create mode 100644 src/lq4x.cpp create mode 100644 src/lq4x.dat create mode 100644 src/lq4x.h create mode 100644 src/memgzio.cpp create mode 100644 src/memgzio.h create mode 100644 src/motionblur.cpp create mode 100644 src/pixel.cpp create mode 100644 src/portable.cpp create mode 100644 src/portable.h create mode 100644 src/remote.cpp create mode 100644 src/scanline.cpp create mode 100644 src/simpleFilter.cpp create mode 100644 src/snd_interp.cpp create mode 100644 src/snd_interp.h create mode 100644 src/thumb.h create mode 100644 src/unzip.cpp create mode 100644 src/unzip.h create mode 100644 src/win32/AVIWrite.cpp create mode 100644 src/win32/AVIWrite.h create mode 100644 src/win32/AboutDialog.cpp create mode 100644 src/win32/AboutDialog.h create mode 100644 src/win32/AccelEditor.cpp create mode 100644 src/win32/AccelEditor.h create mode 100644 src/win32/AcceleratorManager.cpp create mode 100644 src/win32/AcceleratorManager.h create mode 100644 src/win32/Associate.cpp create mode 100644 src/win32/Associate.h create mode 100644 src/win32/BitmapControl.cpp create mode 100644 src/win32/BitmapControl.h create mode 100644 src/win32/BugReport.cpp create mode 100644 src/win32/BugReport.h create mode 100644 src/win32/CmdAccelOb.cpp create mode 100644 src/win32/CmdAccelOb.h create mode 100644 src/win32/ColorButton.cpp create mode 100644 src/win32/ColorButton.h create mode 100644 src/win32/ColorControl.cpp create mode 100644 src/win32/ColorControl.h create mode 100644 src/win32/Commands.cpp create mode 100644 src/win32/Direct3D.cpp create mode 100644 src/win32/DirectDraw.cpp create mode 100644 src/win32/DirectDraw.cpp.orig create mode 100644 src/win32/DirectInput.cpp create mode 100644 src/win32/DirectInput.cpp.orig create mode 100644 src/win32/DirectSound.cpp create mode 100644 src/win32/Directories.cpp create mode 100644 src/win32/Directories.h create mode 100644 src/win32/Disassemble.cpp create mode 100644 src/win32/Disassemble.h create mode 100644 src/win32/Display.h create mode 100644 src/win32/ExportGSASnapshot.cpp create mode 100644 src/win32/ExportGSASnapshot.h create mode 100644 src/win32/FileDlg.cpp create mode 100644 src/win32/FileDlg.h create mode 100644 src/win32/GBACheats.cpp create mode 100644 src/win32/GBACheats.h create mode 100644 src/win32/GBCheatsDlg.cpp create mode 100644 src/win32/GBCheatsDlg.h create mode 100644 src/win32/GBColorDlg.cpp create mode 100644 src/win32/GBColorDlg.h create mode 100644 src/win32/GBDisassemble.cpp create mode 100644 src/win32/GBDisassemble.h create mode 100644 src/win32/GBMapView.cpp create mode 100644 src/win32/GBMapView.h create mode 100644 src/win32/GBMemoryViewerDlg.cpp create mode 100644 src/win32/GBMemoryViewerDlg.h create mode 100644 src/win32/GBOamView.cpp create mode 100644 src/win32/GBOamView.h create mode 100644 src/win32/GBPaletteView.cpp create mode 100644 src/win32/GBPaletteView.h create mode 100644 src/win32/GBPrinterDlg.cpp create mode 100644 src/win32/GBPrinterDlg.h create mode 100644 src/win32/GBTileView.cpp create mode 100644 src/win32/GBTileView.h create mode 100644 src/win32/GDBConnection.cpp create mode 100644 src/win32/GDBConnection.h create mode 100644 src/win32/GDIDisplay.cpp create mode 100644 src/win32/GSACodeSelect.cpp create mode 100644 src/win32/GSACodeSelect.h create mode 100644 src/win32/Hyperlink.cpp create mode 100644 src/win32/Hyperlink.h create mode 100644 src/win32/IOViewer.cpp create mode 100644 src/win32/IOViewer.h create mode 100644 src/win32/IOViewerRegs.h create mode 100644 src/win32/IUpdate.h create mode 100644 src/win32/Input.h create mode 100644 src/win32/Joypad.cpp create mode 100644 src/win32/Joypad.h create mode 100644 src/win32/KeyboardEdit.cpp create mode 100644 src/win32/KeyboardEdit.h create mode 100644 src/win32/LangSelect.cpp create mode 100644 src/win32/LangSelect.h create mode 100644 src/win32/LinkOptions.cpp create mode 100644 src/win32/LinkOptions.h create mode 100644 src/win32/Logging.cpp create mode 100644 src/win32/Logging.h create mode 100644 src/win32/MainWnd.cpp create mode 100644 src/win32/MainWnd.h create mode 100644 src/win32/MainWndCheats.cpp create mode 100644 src/win32/MainWndFile.cpp create mode 100644 src/win32/MainWndHelp.cpp create mode 100644 src/win32/MainWndOptions.cpp create mode 100644 src/win32/MainWndTools.cpp create mode 100644 src/win32/MapView.cpp create mode 100644 src/win32/MapView.h create mode 100644 src/win32/MaxScale.cpp create mode 100644 src/win32/MaxScale.h create mode 100644 src/win32/MemoryViewer.cpp create mode 100644 src/win32/MemoryViewer.h create mode 100644 src/win32/MemoryViewerAddressSize.cpp create mode 100644 src/win32/MemoryViewerAddressSize.h create mode 100644 src/win32/MemoryViewerDlg.cpp create mode 100644 src/win32/MemoryViewerDlg.h create mode 100644 src/win32/ModeConfirm.cpp create mode 100644 src/win32/ModeConfirm.h create mode 100644 src/win32/OamView.cpp create mode 100644 src/win32/OamView.h create mode 100644 src/win32/OpenGL.cpp create mode 100644 src/win32/PaletteView.cpp create mode 100644 src/win32/PaletteView.h create mode 100644 src/win32/PaletteViewControl.cpp create mode 100644 src/win32/PaletteViewControl.h create mode 100644 src/win32/Reg.cpp create mode 100644 src/win32/Reg.h create mode 100644 src/win32/ResizeDlg.cpp create mode 100644 src/win32/ResizeDlg.h create mode 100644 src/win32/RewindInterval.cpp create mode 100644 src/win32/RewindInterval.h create mode 100644 src/win32/RomInfo.cpp create mode 100644 src/win32/RomInfo.h create mode 100644 src/win32/Sound.h create mode 100644 src/win32/StringTokenizer.cpp create mode 100644 src/win32/StringTokenizer.h create mode 100644 src/win32/Throttle.cpp create mode 100644 src/win32/Throttle.h create mode 100644 src/win32/TileView.cpp create mode 100644 src/win32/TileView.h create mode 100644 src/win32/UniVideoModeDlg.cpp create mode 100644 src/win32/UniVideoModeDlg.h create mode 100644 src/win32/VBA.cpp create mode 100644 src/win32/VBA.h create mode 100644 src/win32/VideoMode.cpp create mode 100644 src/win32/VideoMode.h create mode 100644 src/win32/WavWriter.cpp create mode 100644 src/win32/WavWriter.h create mode 100644 src/win32/WinHelper.h create mode 100644 src/win32/WinResUtil.cpp create mode 100644 src/win32/WinResUtil.h create mode 100644 src/win32/ZoomControl.cpp create mode 100644 src/win32/ZoomControl.h create mode 100644 src/win32/stdafx.cpp create mode 100644 src/win32/stdafx.h create mode 100644 zlib/adler32.c create mode 100644 zlib/compress.c create mode 100644 zlib/crc32.c create mode 100644 zlib/crc32.h create mode 100644 zlib/deflate.c create mode 100644 zlib/deflate.h create mode 100644 zlib/gzio.c create mode 100644 zlib/infback.c create mode 100644 zlib/inffast.c create mode 100644 zlib/inffast.h create mode 100644 zlib/inffixed.h create mode 100644 zlib/inflate.c create mode 100644 zlib/inflate.h create mode 100644 zlib/inftrees.c create mode 100644 zlib/inftrees.h create mode 100644 zlib/trees.c create mode 100644 zlib/trees.h create mode 100644 zlib/uncompr.c create mode 100644 zlib/zconf.h create mode 100644 zlib/zlib.h create mode 100644 zlib/zlib.vcproj create mode 100644 zlib/zlib.vcproj.7.10.old create mode 100644 zlib/zutil.c create mode 100644 zlib/zutil.h diff --git a/Gb_Apu/Blip_Buffer.cpp b/Gb_Apu/Blip_Buffer.cpp new file mode 100644 index 00000000..81da8362 --- /dev/null +++ b/Gb_Apu/Blip_Buffer.cpp @@ -0,0 +1,406 @@ + +// Blip_Buffer 0.4.0. http://www.slack.net/~ant/ + +#include "Blip_Buffer.h" + +#include +#include +#include +#include +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +int const buffer_extra = blip_widest_impulse_ + 2; + +Blip_Buffer::Blip_Buffer() +{ + factor_ = LONG_MAX; + offset_ = 0; + buffer_ = 0; + buffer_size_ = 0; + sample_rate_ = 0; + reader_accum = 0; + bass_shift = 0; + clock_rate_ = 0; + bass_freq_ = 16; + length_ = 0; + + // assumptions code makes about implementation-defined features + #ifndef NDEBUG + // right shift of negative value preserves sign + buf_t_ i = -0x7FFFFFFE; + assert( (i >> 1) == -0x3FFFFFFF ); + + // casting to short truncates to 16 bits and sign-extends + i = 0x18000; + assert( (short) i == -0x8000 ); + #endif +} + +Blip_Buffer::~Blip_Buffer() +{ + free( buffer_ ); +} + +void Blip_Buffer::clear( int entire_buffer ) +{ + offset_ = 0; + reader_accum = 0; + if ( buffer_ ) + { + long count = (entire_buffer ? buffer_size_ : samples_avail()); + memset( buffer_, 0, (count + buffer_extra) * sizeof (buf_t_) ); + } +} + +Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) +{ + // start with maximum length that resampled time can represent + long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - buffer_extra - 64; + if ( msec != blip_max_length ) + { + long s = (new_rate * (msec + 1) + 999) / 1000; + if ( s < new_size ) + new_size = s; + else + assert( 0 ); // fails if requested buffer length exceeds limit + } + + if ( buffer_size_ != new_size ) + { + void* p = realloc( buffer_, (new_size + buffer_extra) * sizeof *buffer_ ); + if ( !p ) + return "Out of memory"; + buffer_ = (buf_t_*) p; + } + + buffer_size_ = new_size; + + // update things based on the sample rate + sample_rate_ = new_rate; + length_ = new_size * 1000 / new_rate - 1; + if ( msec ) + assert( length_ == msec ); // ensure length is same as that passed in + if ( clock_rate_ ) + clock_rate( clock_rate_ ); + bass_freq( bass_freq_ ); + + clear(); + + return 0; // success +} + +blip_resampled_time_t Blip_Buffer::clock_rate_factor( long clock_rate ) const +{ + double ratio = (double) sample_rate_ / clock_rate; + long factor = (long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 ); + assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large + return (blip_resampled_time_t) factor; +} + +void Blip_Buffer::bass_freq( int freq ) +{ + bass_freq_ = freq; + int shift = 31; + if ( freq > 0 ) + { + shift = 13; + long f = (freq << 16) / sample_rate_; + while ( (f >>= 1) && --shift ) { } + } + bass_shift = shift; +} + +void Blip_Buffer::end_frame( blip_time_t t ) +{ + offset_ += t * factor_; + assert( samples_avail() <= (long) buffer_size_ ); // time outside buffer length +} + +void Blip_Buffer::remove_silence( long count ) +{ + assert( count <= samples_avail() ); // tried to remove more samples than available + offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; +} + +long Blip_Buffer::count_samples( blip_time_t t ) const +{ + unsigned long last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; + unsigned long first_sample = offset_ >> BLIP_BUFFER_ACCURACY; + return (long) (last_sample - first_sample); +} + +blip_time_t Blip_Buffer::count_clocks( long count ) const +{ + if ( count > buffer_size_ ) + count = buffer_size_; + blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; + return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); +} + +void Blip_Buffer::remove_samples( long count ) +{ + if ( count ) + { + remove_silence( count ); + + // copy remaining samples to beginning and clear old samples + long remain = samples_avail() + buffer_extra; + memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); + memset( buffer_ + remain, 0, count * sizeof *buffer_ ); + } +} + +// Blip_Synth_ + +Blip_Synth_::Blip_Synth_( short* p, int w ) : + impulses( p ), + width( w ) +{ + volume_unit_ = 0.0; + kernel_unit = 0; + buf = 0; + last_amp = 0; + delta_factor = 0; +} + +static double const pi = 3.1415926535897932384626433832795029; + +static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff ) +{ + if ( cutoff >= 0.999 ) + cutoff = 0.999; + + if ( treble < -300.0 ) + treble = -300.0; + if ( treble > 5.0 ) + treble = 5.0; + + double const maxh = 4096.0; + double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) ); + double const pow_a_n = pow( rolloff, maxh - maxh * cutoff ); + double const to_angle = pi / 2 / maxh / oversample; + for ( int i = 0; i < count; i++ ) + { + double angle = ((i - count) * 2 + 1) * to_angle; + double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle ); + double cos_nc_angle = cos( maxh * cutoff * angle ); + double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle ); + double cos_angle = cos( angle ); + + c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle; + double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle); + double b = 2.0 - cos_angle - cos_angle; + double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle; + + out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d + } +} + +void blip_eq_t::generate( float* out, int count ) const +{ + // lower cutoff freq for narrow kernels with their wider transition band + // (8 points->1.49, 16 points->1.15) + double oversample = blip_res * 2.25 / count + 0.85; + double half_rate = sample_rate * 0.5; + if ( cutoff_freq ) + oversample = half_rate / cutoff_freq; + double cutoff = rolloff_freq * oversample / half_rate; + + gen_sinc( out, count, blip_res * oversample, treble, cutoff ); + + // apply (half of) hamming window + double to_fraction = pi / (count - 1); + for ( int i = count; i--; ) + out [i] *= 0.54 - 0.46 * cos( i * to_fraction ); +} + +void Blip_Synth_::adjust_impulse() +{ + // sum pairs for each phase and add error correction to end of first half + int const size = impulses_size(); + for ( int p = blip_res; p-- >= blip_res / 2; ) + { + int p2 = blip_res - 2 - p; + long error = kernel_unit; + for ( int i = 1; i < size; i += blip_res ) + { + error -= impulses [i + p ]; + error -= impulses [i + p2]; + } + if ( p == p2 ) + error /= 2; // phase = 0.5 impulse uses same half for both sides + impulses [size - blip_res + p] += error; + //printf( "error: %ld\n", error ); + } + + //for ( int i = blip_res; i--; printf( "\n" ) ) + // for ( int j = 0; j < width / 2; j++ ) + // printf( "%5ld,", impulses [j * blip_res + i + 1] ); +} + +void Blip_Synth_::treble_eq( blip_eq_t const& eq ) +{ + float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; + + int const half_size = blip_res / 2 * (width - 1); + eq.generate( &fimpulse [blip_res], half_size ); + + int i; + + // need mirror slightly past center for calculation + for ( i = blip_res; i--; ) + fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i]; + + // starts at 0 + for ( i = 0; i < blip_res; i++ ) + fimpulse [i] = 0.0f; + + // find rescale factor + double total = 0.0; + for ( i = 0; i < half_size; i++ ) + total += fimpulse [blip_res + i]; + + //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB + //double const base_unit = 37888.0; // allows treble to +5 dB + double const base_unit = 32768.0; // necessary for blip_unscaled to work + double rescale = base_unit / 2 / total; + kernel_unit = (long) base_unit; + + // integrate, first difference, rescale, convert to int + double sum = 0.0; + double next = 0.0; + int const impulses_size = this->impulses_size(); + for ( i = 0; i < impulses_size; i++ ) + { + impulses [i] = (short) floor( (next - sum) * rescale + 0.5 ); + sum += fimpulse [i]; + next += fimpulse [i + blip_res]; + } + adjust_impulse(); + + // volume might require rescaling + double vol = volume_unit_; + if ( vol ) + { + volume_unit_ = 0.0; + volume_unit( vol ); + } +} + +void Blip_Synth_::volume_unit( double new_unit ) +{ + if ( new_unit != volume_unit_ ) + { + // use default eq if it hasn't been set yet + if ( !kernel_unit ) + treble_eq( -8.0 ); + + volume_unit_ = new_unit; + double factor = new_unit * (1L << blip_sample_bits) / kernel_unit; + + if ( factor > 0.0 ) + { + int shift = 0; + + // if unit is really small, might need to attenuate kernel + while ( factor < 2.0 ) + { + shift++; + factor *= 2.0; + } + + if ( shift ) + { + kernel_unit >>= shift; + assert( kernel_unit > 0 ); // fails if volume unit is too low + + // keep values positive to avoid round-towards-zero of sign-preserving + // right shift for negative values + long offset = 0x8000 + (1 << (shift - 1)); + long offset2 = 0x8000 >> shift; + for ( int i = impulses_size(); i--; ) + impulses [i] = (short) (((impulses [i] + offset) >> shift) - offset2); + adjust_impulse(); + } + } + delta_factor = (int) floor( factor + 0.5 ); + //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); + } +} + +long Blip_Buffer::read_samples( blip_sample_t* out, long max_samples, int stereo ) +{ + long count = samples_avail(); + if ( count > max_samples ) + count = max_samples; + + if ( count ) + { + int const sample_shift = blip_sample_bits - 16; + int const bass_shift = this->bass_shift; + long accum = reader_accum; + buf_t_* in = buffer_; + + if ( !stereo ) + { + for ( long n = count; n--; ) + { + long s = accum >> sample_shift; + accum -= accum >> bass_shift; + accum += *in++; + *out++ = (blip_sample_t) s; + + // clamp sample + if ( (blip_sample_t) s != s ) + out [-1] = (blip_sample_t) (0x7FFF - (s >> 24)); + } + } + else + { + for ( long n = count; n--; ) + { + long s = accum >> sample_shift; + accum -= accum >> bass_shift; + accum += *in++; + *out = (blip_sample_t) s; + out += 2; + + // clamp sample + if ( (blip_sample_t) s != s ) + out [-2] = (blip_sample_t) (0x7FFF - (s >> 24)); + } + } + + reader_accum = accum; + remove_samples( count ); + } + return count; +} + +void Blip_Buffer::mix_samples( blip_sample_t const* in, long count ) +{ + buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2; + + int const sample_shift = blip_sample_bits - 16; + int prev = 0; + while ( count-- ) + { + long s = (long) *in++ << sample_shift; + *out += s - prev; + prev = s; + ++out; + } + *out -= prev; +} + diff --git a/Gb_Apu/Blip_Buffer.h b/Gb_Apu/Blip_Buffer.h new file mode 100644 index 00000000..49a156a3 --- /dev/null +++ b/Gb_Apu/Blip_Buffer.h @@ -0,0 +1,354 @@ + +// Band-limited sound synthesis and buffering + +// Blip_Buffer 0.4.0 + +#ifndef BLIP_BUFFER_H +#define BLIP_BUFFER_H + +// Time unit at source clock rate +typedef long blip_time_t; + +// Output samples are 16-bit signed, with a range of -32768 to 32767 +typedef short blip_sample_t; +enum { blip_sample_max = 32767 }; + +class Blip_Buffer { +public: + typedef const char* blargg_err_t; + + // Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults + // to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there + // isn't enough memory, returns error without affecting current buffer setup. + blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 ); + + // Set number of source time units per second + void clock_rate( long ); + + // End current time frame of specified duration and make its samples available + // (along with any still-unread samples) for reading with read_samples(). Begins + // a new time frame at the end of the current frame. + void end_frame( blip_time_t time ); + + // Read at most 'max_samples' out of buffer into 'dest', removing them from from + // the buffer. Returns number of samples actually read and removed. If stereo is + // true, increments 'dest' one extra time after writing each sample, to allow + // easy interleving of two channels into a stereo output buffer. + long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 ); + +// Additional optional features + + // Current output sample rate + long sample_rate() const; + + // Length of buffer, in milliseconds + int length() const; + + // Number of source time units per second + long clock_rate() const; + + // Set frequency high-pass filter frequency, where higher values reduce bass more + void bass_freq( int frequency ); + + // Number of samples delay from synthesis to samples read out + int output_latency() const; + + // Remove all available samples and clear buffer to silence. If 'entire_buffer' is + // false, just clears out any samples waiting rather than the entire buffer. + void clear( int entire_buffer = 1 ); + + // Number of samples available for reading with read_samples() + long samples_avail() const; + + // Remove 'count' samples from those waiting to be read + void remove_samples( long count ); + +// Experimental features + + // Number of raw samples that can be mixed within frame of specified duration. + long count_samples( blip_time_t duration ) const; + + // Mix 'count' samples from 'buf' into buffer. + void mix_samples( blip_sample_t const* buf, long count ); + + // Count number of clocks needed until 'count' samples will be available. + // If buffer can't even hold 'count' samples, returns number of clocks until + // buffer becomes full. + blip_time_t count_clocks( long count ) const; + + // not documented yet + typedef unsigned long blip_resampled_time_t; + void remove_silence( long count ); + blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; } + blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; } + blip_resampled_time_t clock_rate_factor( long clock_rate ) const; +public: + Blip_Buffer(); + ~Blip_Buffer(); + + // Deprecated + typedef blip_resampled_time_t resampled_time_t; + blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); } + blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); } +private: + // noncopyable + Blip_Buffer( const Blip_Buffer& ); + Blip_Buffer& operator = ( const Blip_Buffer& ); +public: + typedef long buf_t_; + unsigned long factor_; + blip_resampled_time_t offset_; + buf_t_* buffer_; + long buffer_size_; +private: + long reader_accum; + int bass_shift; + long sample_rate_; + long clock_rate_; + int bass_freq_; + int length_; + friend class Blip_Reader; +}; + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +// Number of bits in resample ratio fraction. Higher values give a more accurate ratio +// but reduce maximum buffer size. +#ifndef BLIP_BUFFER_ACCURACY + #define BLIP_BUFFER_ACCURACY 16 +#endif + +// Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in +// noticeable broadband noise when synthesizing high frequency square waves. +// Affects size of Blip_Synth objects since they store the waveform directly. +#ifndef BLIP_PHASE_BITS + #define BLIP_PHASE_BITS 6 +#endif + + // Internal + typedef unsigned long blip_resampled_time_t; + int const blip_widest_impulse_ = 16; + int const blip_res = 1 << BLIP_PHASE_BITS; + class blip_eq_t; + + class Blip_Synth_ { + double volume_unit_; + short* const impulses; + int const width; + long kernel_unit; + int impulses_size() const { return blip_res / 2 * width + 1; } + void adjust_impulse(); + public: + Blip_Buffer* buf; + int last_amp; + int delta_factor; + + Blip_Synth_( short* impulses, int width ); + void treble_eq( blip_eq_t const& ); + void volume_unit( double ); + }; + +// Quality level. Start with blip_good_quality. +const int blip_med_quality = 8; +const int blip_good_quality = 12; +const int blip_high_quality = 16; + +// Range specifies the greatest expected change in amplitude. Calculate it +// by finding the difference between the maximum and minimum expected +// amplitudes (max - min). +template +class Blip_Synth { +public: + // Set overall volume of waveform + void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); } + + // Configure low-pass filter (see notes.txt) + void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); } + + // Get/set Blip_Buffer used for output + Blip_Buffer* output() const { return impl.buf; } + void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; } + + // Update amplitude of waveform at given time. Using this requires a separate + // Blip_Synth for each waveform. + void update( blip_time_t time, int amplitude ); + +// Low-level interface + + // Add an amplitude transition of specified delta, optionally into specified buffer + // rather than the one set with output(). Delta can be positive or negative. + // The actual change in amplitude is delta * (volume / range) + void offset( blip_time_t, int delta, Blip_Buffer* ) const; + void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); } + + // Works directly in terms of fractional output samples. Contact author for more. + void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; + + // Same as offset(), except code is inlined for higher performance + void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const { + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); + } + void offset_inline( blip_time_t t, int delta ) const { + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); + } + +public: + Blip_Synth() : impl( impulses, quality ) { } +private: + typedef short imp_t; + imp_t impulses [blip_res * (quality / 2) + 1]; + Blip_Synth_ impl; +}; + +// Low-pass equalization parameters +class blip_eq_t { +public: + // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce + // treble, small positive values (0 to 5.0) increase treble. + blip_eq_t( double treble_db = 0 ); + + // See notes.txt + blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 ); + +private: + double treble; + long rolloff_freq; + long sample_rate; + long cutoff_freq; + void generate( float* out, int count ) const; + friend class Blip_Synth_; +}; + +int const blip_sample_bits = 30; + +// Optimized inline sample reader for custom sample formats and mixing of Blip_Buffer samples +class Blip_Reader { +public: + // Begin reading samples from buffer. Returns value to pass to next() (can + // be ignored if default bass_freq is acceptable). + int begin( Blip_Buffer& ); + + // Current sample + long read() const { return accum >> (blip_sample_bits - 16); } + + // Current raw sample in full internal resolution + long read_raw() const { return accum; } + + // Advance to next sample + void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); } + + // End reading samples from buffer. The number of samples read must now be removed + // using Blip_Buffer::remove_samples(). + void end( Blip_Buffer& b ) { b.reader_accum = accum; } + +private: + const Blip_Buffer::buf_t_* buf; + long accum; +}; + + +// End of public interface + + +#include + +// Compatibility with older version +const long blip_unscaled = 65535; +const int blip_low_quality = blip_med_quality; +const int blip_best_quality = blip_high_quality; + +#define BLIP_FWD( i ) { \ + long t0 = i0 * delta + buf [fwd + i]; \ + long t1 = imp [blip_res * (i + 1)] * delta + buf [fwd + 1 + i]; \ + i0 = imp [blip_res * (i + 2)]; \ + buf [fwd + i] = t0; \ + buf [fwd + 1 + i] = t1; } + +#define BLIP_REV( r ) { \ + long t0 = i0 * delta + buf [rev - r]; \ + long t1 = imp [blip_res * r] * delta + buf [rev + 1 - r]; \ + i0 = imp [blip_res * (r - 1)]; \ + buf [rev - r] = t0; \ + buf [rev + 1 - r] = t1; } + +template +inline void Blip_Synth::offset_resampled( blip_resampled_time_t time, + int delta, Blip_Buffer* blip_buf ) const +{ + // Fails if time is beyond end of Blip_Buffer, due to a bug in caller code or the + // need for a longer buffer as set by set_sample_rate(). + assert( (long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ ); + delta *= impl.delta_factor; + int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1)); + imp_t const* imp = impulses + blip_res - phase; + long* buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY); + long i0 = *imp; + + int const fwd = (blip_widest_impulse_ - quality) / 2; + int const rev = fwd + quality - 2; + + BLIP_FWD( 0 ) + if ( quality > 8 ) BLIP_FWD( 2 ) + if ( quality > 12 ) BLIP_FWD( 4 ) + { + int const mid = quality / 2 - 1; + long t0 = i0 * delta + buf [fwd + mid - 1]; + long t1 = imp [blip_res * mid] * delta + buf [fwd + mid]; + imp = impulses + phase; + i0 = imp [blip_res * mid]; + buf [fwd + mid - 1] = t0; + buf [fwd + mid] = t1; + } + if ( quality > 12 ) BLIP_REV( 6 ) + if ( quality > 8 ) BLIP_REV( 4 ) + BLIP_REV( 2 ) + + long t0 = i0 * delta + buf [rev]; + long t1 = *imp * delta + buf [rev + 1]; + buf [rev] = t0; + buf [rev + 1] = t1; +} + +#undef BLIP_FWD +#undef BLIP_REV + +template +void Blip_Synth::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const +{ + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); +} + +template +void Blip_Synth::update( blip_time_t t, int amp ) +{ + int delta = amp - impl.last_amp; + impl.last_amp = amp; + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); +} + +inline blip_eq_t::blip_eq_t( double t ) : + treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { } +inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) : + treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { } + +inline int Blip_Buffer::length() const { return length_; } +inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); } +inline long Blip_Buffer::sample_rate() const { return sample_rate_; } +inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; } +inline long Blip_Buffer::clock_rate() const { return clock_rate_; } +inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); } + +inline int Blip_Reader::begin( Blip_Buffer& blip_buf ) +{ + buf = blip_buf.buffer_; + accum = blip_buf.reader_accum; + return blip_buf.bass_shift; +} + +int const blip_max_length = 0; +int const blip_default_length = 250; + +#endif + diff --git a/Gb_Apu/Blip_Synth.h b/Gb_Apu/Blip_Synth.h new file mode 100644 index 00000000..9dadb972 --- /dev/null +++ b/Gb_Apu/Blip_Synth.h @@ -0,0 +1,204 @@ + +// Blip_Synth and Blip_Wave are waveform transition synthesizers for adding +// waveforms to a Blip_Buffer. + +// Blip_Buffer 0.3.3. Copyright (C) 2003-2005 Shay Green. GNU LGPL license. + +#ifndef BLIP_SYNTH_H +#define BLIP_SYNTH_H + +#ifndef BLIP_BUFFER_H + #include "Blip_Buffer.h" +#endif + +// Quality level. Higher levels are slower, and worse in a few cases. +// Use blip_good_quality as a starting point. +const int blip_low_quality = 1; +const int blip_med_quality = 2; +const int blip_good_quality = 3; +const int blip_high_quality = 4; + +// Blip_Synth is a transition waveform synthesizer which adds band-limited +// offsets (transitions) into a Blip_Buffer. For a simpler interface, use +// Blip_Wave (below). +// +// Range specifies the greatest expected offset that will occur. For a +// waveform that goes between +amp and -amp, range should be amp * 2 (half +// that if it only goes between +amp and 0). When range is large, a higher +// accuracy scheme is used; to force this even when range is small, pass +// the negative of range (i.e. -range). +template +class Blip_Synth { + BOOST_STATIC_ASSERT( 1 <= quality && quality <= 5 ); + BOOST_STATIC_ASSERT( -32768 <= range && range <= 32767 ); + enum { + abs_range = (range < 0) ? -range : range, + fine_mode = (range > 512 || range < 0), + width = (quality < 5 ? quality * 4 : Blip_Buffer::widest_impulse_), + res = 1 << blip_res_bits_, + impulse_size = width / 2 * (fine_mode + 1), + base_impulses_size = width / 2 * (res / 2 + 1), + fine_bits = (fine_mode ? (abs_range <= 64 ? 2 : abs_range <= 128 ? 3 : + abs_range <= 256 ? 4 : abs_range <= 512 ? 5 : abs_range <= 1024 ? 6 : + abs_range <= 2048 ? 7 : 8) : 0) + }; + blip_pair_t_ impulses [impulse_size * res * 2 + base_impulses_size]; + Blip_Impulse_ impulse; +public: + Blip_Synth() { impulse.init( impulses, width, res, fine_bits ); } + + // Configure low-pass filter (see notes.txt). Not optimized for real-time control + void treble_eq( const blip_eq_t& eq ) { impulse.treble_eq( eq ); } + + // Set volume of a transition at amplitude 'range' by setting volume_unit + // to v / range + void volume( double v ) { impulse.volume_unit( v * (1.0 / abs_range) ); } + + // Set base volume unit of transitions, where 1.0 is a full swing between the + // positive and negative extremes. Not optimized for real-time control. + void volume_unit( double unit ) { impulse.volume_unit( unit ); } + + // Default Blip_Buffer used for output when none is specified for a given call + Blip_Buffer* output() const { return impulse.buf; } + void output( Blip_Buffer* b ) { impulse.buf = b; } + + // Add an amplitude offset (transition) with an amplitude of delta * volume_unit + // into the specified buffer (default buffer if none specified) at the + // specified source time. Amplitude can be positive or negative. To increase + // performance by inlining code at the call site, use offset_inline(). + void offset( blip_time_t, int delta, Blip_Buffer* ) const; + + void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; + void offset_resampled( blip_resampled_time_t t, int o ) const { + offset_resampled( t, o, impulse.buf ); + } + void offset( blip_time_t t, int delta ) const { + offset( t, delta, impulse.buf ); + } + void offset_inline( blip_time_t time, int delta, Blip_Buffer* buf ) const { + offset_resampled( time * buf->factor_ + buf->offset_, delta, buf ); + } + void offset_inline( blip_time_t time, int delta ) const { + offset_inline( time, delta, impulse.buf ); + } +}; + +// Blip_Wave is a synthesizer for adding a *single* waveform to a Blip_Buffer. +// A wave is built from a series of delays and new amplitudes. This provides a +// simpler interface than Blip_Synth. +template +class Blip_Wave { + Blip_Synth synth; + blip_time_t time_; + int last_amp; +public: + // Start wave at time 0 and amplitude 0 + Blip_Wave() : time_( 0 ), last_amp( 0 ) { } + + // See Blip_Synth for description + void volume( double v ) { synth.volume( v ); } + void volume_unit( double v ) { synth.volume_unit( v ); } + void treble_eq( const blip_eq_t& eq){ synth.treble_eq( eq ); } + Blip_Buffer* output() const { return synth.output(); } + void output( Blip_Buffer* b ) { synth.output( b ); if ( !b ) time_ = last_amp = 0; } + + // Current time in frame + blip_time_t time() const { return time_; } + void time( blip_time_t t ) { time_ = t; } + + // Current amplitude of wave + int amplitude() const { return last_amp; } + void amplitude( int ); + + // Move forward by 't' time units + void delay( blip_time_t t ) { time_ += t; } + + // End time frame of specified duration. Localize time to new frame. + void end_frame( blip_time_t duration ) { + assert(( "Blip_Wave::end_frame(): Wave hadn't yet been run for entire frame", + duration <= time_ )); + time_ -= duration; + } +}; + + + +// End of public interface + + template + void Blip_Wave::amplitude( int amp ) { + int delta = amp - last_amp; + last_amp = amp; + synth.offset_inline( time_, delta ); + } + + template + inline void Blip_Synth::offset_resampled( blip_resampled_time_t time, + int delta, Blip_Buffer* blip_buf ) const + { + typedef blip_pair_t_ pair_t; + + unsigned sample_index = (time >> BLIP_BUFFER_ACCURACY) & ~1; + assert(( "Blip_Synth/Blip_wave: Went past end of buffer", + sample_index < blip_buf->buffer_size_ )); + enum { const_offset = Blip_Buffer::widest_impulse_ / 2 - width / 2 }; + pair_t* buf = (pair_t*) &blip_buf->buffer_ [const_offset + sample_index]; + + enum { shift = BLIP_BUFFER_ACCURACY - blip_res_bits_ }; + enum { mask = res * 2 - 1 }; + const pair_t* imp = &impulses [((time >> shift) & mask) * impulse_size]; + + pair_t offset = impulse.offset * delta; + + if ( !fine_bits ) + { + // normal mode + for ( int n = width / 4; n; --n ) + { + pair_t t0 = buf [0] - offset; + pair_t t1 = buf [1] - offset; + + t0 += imp [0] * delta; + t1 += imp [1] * delta; + imp += 2; + + buf [0] = t0; + buf [1] = t1; + buf += 2; + } + } + else + { + // fine mode + enum { sub_range = 1 << fine_bits }; + delta += sub_range / 2; + int delta2 = (delta & (sub_range - 1)) - sub_range / 2; + delta >>= fine_bits; + + for ( int n = width / 4; n; --n ) + { + pair_t t0 = buf [0] - offset; + pair_t t1 = buf [1] - offset; + + t0 += imp [0] * delta2; + t0 += imp [1] * delta; + + t1 += imp [2] * delta2; + t1 += imp [3] * delta; + + imp += 4; + + buf [0] = t0; + buf [1] = t1; + buf += 2; + } + } + } + + template + void Blip_Synth::offset( blip_time_t time, int delta, Blip_Buffer* buf ) const { + offset_resampled( time * buf->factor_ + buf->offset_, delta, buf ); + } + +#endif + diff --git a/Gb_Apu/COPYING b/Gb_Apu/COPYING new file mode 100644 index 00000000..54905a60 --- /dev/null +++ b/Gb_Apu/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/Gb_Apu/Gb_Apu.cpp b/Gb_Apu/Gb_Apu.cpp new file mode 100644 index 00000000..0dfd32ce --- /dev/null +++ b/Gb_Apu/Gb_Apu.cpp @@ -0,0 +1,318 @@ + +// Gb_Snd_Emu 0.1.4. http://www.slack.net/~ant/ + +#include "Gb_Apu.h" + +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +int const vol_reg = 0xFF24; +int const status_reg = 0xFF26; + +Gb_Apu::Gb_Apu() +{ + square1.synth = &square_synth; + square2.synth = &square_synth; + wave.synth = &other_synth; + noise.synth = &other_synth; + + oscs [0] = &square1; + oscs [1] = &square2; + oscs [2] = &wave; + oscs [3] = &noise; + + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + osc.regs = ®s [i * 5]; + osc.output = NULL; + osc.outputs [0] = NULL; + osc.outputs [1] = NULL; + osc.outputs [2] = NULL; + osc.outputs [3] = NULL; + } + + volume( 1.0 ); + reset(); +} + +Gb_Apu::~Gb_Apu() +{ +} + +void Gb_Apu::treble_eq( const blip_eq_t& eq ) +{ + square_synth.treble_eq( eq ); + other_synth.treble_eq( eq ); +} + +void Gb_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + require( (unsigned) index < osc_count ); + require( (center && left && right) || (!center && !left && !right) ); + Gb_Osc& osc = *oscs [index]; + osc.outputs [1] = right; + osc.outputs [2] = left; + osc.outputs [3] = center; + osc.output = osc.outputs [osc.output_select]; +} + +void Gb_Apu::output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + for ( int i = 0; i < osc_count; i++ ) + osc_output( i, center, left, right ); +} + +void Gb_Apu::update_volume() +{ + // to do: doesn't handle differing left/right global volume + int data = regs [vol_reg - start_addr]; + double vol = (max( data & 7, data >> 4 & 7 ) + 1) * volume_unit; + square_synth.volume( vol ); + other_synth.volume( vol ); +} + +static unsigned char const powerup_regs [0x30] = { + 0x80,0x3F,0x00,0xFF,0xBF, // square 1 + 0xFF,0x3F,0x00,0xFF,0xBF, // square 2 + 0x7F,0xFF,0x9F,0xFF,0xBF, // wave + 0xFF,0xFF,0x00,0x00,0xBF, // noise + 0x00, // left/right enables + 0x77, // master volume + 0x80, // power + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C, // wave table + 0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA +}; + +void Gb_Apu::reset(bool igba) +{ + next_frame_time = 0; + last_time = 0; + frame_count = 0; + stereo_found = false; + + square1.reset(); + square2.reset(); + wave.reset(gba = igba); + noise.reset(); + noise.bits = 1; + wave.wave_pos = 0; + + // avoid click at beginning + regs [vol_reg - start_addr] = 0x77; + update_volume(); + + regs [status_reg - start_addr] = 0x01; // force power + write_register( 0, status_reg, 0x00 ); +} + +// to do: remove +//static unsigned long abs_time; + +void Gb_Apu::run_until( gb_time_t end_time ) +{ + require( end_time >= last_time ); // end_time must not be before previous time + if ( end_time == last_time ) + return; + + while ( true ) + { + gb_time_t time = next_frame_time; + if ( time > end_time ) + time = end_time; + + // run oscillators + for ( int i = 0; i < osc_count; ++i ) + { + Gb_Osc& osc = *oscs [i]; + if ( osc.output ) + { + int playing = false; + if ( osc.enabled && osc.volume && + (!(osc.regs [4] & osc.len_enabled_mask) || osc.length) ) + playing = -1; + if ( osc.output != osc.outputs [3] ) + stereo_found = true; + switch ( i ) + { + case 0: square1.run( last_time, time, playing ); break; + case 1: square2.run( last_time, time, playing ); break; + case 2: wave .run( last_time, time, playing ); break; + case 3: noise .run( last_time, time, playing ); break; + } + } + } + last_time = time; + + if ( time == end_time ) + break; + + next_frame_time += 4194304 / 256; // 256 Hz + + // 256 Hz actions + square1.clock_length(); + square2.clock_length(); + wave.clock_length(); + noise.clock_length(); + + frame_count = (frame_count + 1) & 3; + if ( frame_count == 0 ) + { + // 64 Hz actions + square1.clock_envelope(); + square2.clock_envelope(); + noise.clock_envelope(); + } + + if ( frame_count & 1 ) + square1.clock_sweep(); // 128 Hz action + } +} + +bool Gb_Apu::end_frame( gb_time_t end_time ) +{ + if ( end_time > last_time ) + run_until( end_time ); + + //abs_time += end_time; + + assert( next_frame_time >= end_time ); + next_frame_time -= end_time; + + assert( last_time >= end_time ); + last_time -= end_time; + + bool result = stereo_found; + stereo_found = false; + return result; +} + +void Gb_Apu::write_register( gb_time_t time, gb_addr_t addr, int data ) +{ + require( (unsigned) data < 0x100 ); + + int reg = addr - start_addr; + if ( (unsigned) reg >= register_count ) + return; + + run_until( time ); + + int old_reg = regs [reg]; + regs [reg] = data; + + if ( addr < vol_reg ) + { + write_osc( reg / 5, reg, data ); + } + else if ( addr == vol_reg && data != old_reg ) // global volume + { + // return all oscs to 0 + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + int amp = osc.last_amp; + osc.last_amp = 0; + if ( amp && osc.enabled && osc.output ) + other_synth.offset( time, -amp, osc.output ); + } + + if ( wave.outputs [3] ) + other_synth.offset( time, 30, wave.outputs [3] ); + + update_volume(); + + if ( wave.outputs [3] ) + other_synth.offset( time, -30, wave.outputs [3] ); + + // oscs will update with new amplitude when next run + } + else if ( addr == 0xFF25 || addr == status_reg ) + { + int mask = (regs [status_reg - start_addr] & 0x80) ? ~0 : 0; + int flags = regs [0xFF25 - start_addr] & mask; + + // left/right assignments + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + osc.enabled &= mask; + int bits = flags >> i; + Blip_Buffer* old_output = osc.output; + osc.output_select = (bits >> 3 & 2) | (bits & 1); + osc.output = osc.outputs [osc.output_select]; + if ( osc.output != old_output ) + { + int amp = osc.last_amp; + osc.last_amp = 0; + if ( amp && old_output ) + other_synth.offset( time, -amp, old_output ); + } + } + + if ( addr == status_reg && data != old_reg ) + { + if ( !(data & 0x80) ) + { + for ( int i = 0; i < (int) sizeof powerup_regs; i++ ) + { + if ( i != status_reg - start_addr ) + write_register( time, i + start_addr, powerup_regs [i] ); + } + } + else + { + //dprintf( "APU powered on\n" ); + } + } + } + else if ( addr >= 0xFF30 ) + { + int bank; + if (gba) bank = (wave.wave_bank ^ 0x20); + else bank = 0; + int index = (addr & 0x0F) * 2 + bank; + wave.wave [index] = data >> 4; + wave.wave [index + 1] = data & 0x0F; + } +} + +int Gb_Apu::read_register( gb_time_t time, gb_addr_t addr ) +{ + run_until( time ); + + int index = addr - start_addr; + require( (unsigned) index < register_count ); + int data = regs [index]; + + if ( addr == status_reg ) + { + data = (data & 0x80) | 0x70; + for ( int i = 0; i < osc_count; i++ ) + { + const Gb_Osc& osc = *oscs [i]; + if ( osc.enabled && (osc.length || !(osc.regs [4] & osc.len_enabled_mask)) ) + data |= 1 << i; + } + } else if ( gba && addr >= 0xff30 ) { + int bank = (wave.wave_bank ^ 0x20); + int index = (addr & 0x0f) * 2; + data = wave.wave [bank + index] << 4; + data |= wave.wave [bank + index + 1]; + } + + return data; +} + diff --git a/Gb_Apu/Gb_Apu.h b/Gb_Apu/Gb_Apu.h new file mode 100644 index 00000000..6ae61580 --- /dev/null +++ b/Gb_Apu/Gb_Apu.h @@ -0,0 +1,99 @@ + +// Nintendo Game Boy PAPU sound chip emulator + +// Gb_Snd_Emu 0.1.4 + +#ifndef GB_APU_H +#define GB_APU_H + +typedef long gb_time_t; // clock cycle count +typedef unsigned gb_addr_t; // 16-bit address + +#include "Gb_Oscs.h" + +class Gb_Apu { +public: + + // Set overall volume of all oscillators, where 1.0 is full volume + void volume( double ); + + // Set treble equalization + void treble_eq( const blip_eq_t& ); + + // Outputs can be assigned to a single buffer for mono output, or to three + // buffers for stereo output (using Stereo_Buffer to do the mixing). + + // Assign all oscillator outputs to specified buffer(s). If buffer + // is NULL, silences all oscillators. + void output( Blip_Buffer* mono ); + void output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Assign single oscillator output to buffer(s). Valid indicies are 0 to 3, + // which refer to Square 1, Square 2, Wave, and Noise. If buffer is NULL, + // silences oscillator. + enum { osc_count = 4 }; + void osc_output( int index, Blip_Buffer* mono ); + void osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Reset oscillators and internal state + void reset(bool gba = false); + + // Reads and writes at addr must satisfy start_addr <= addr <= end_addr + enum { start_addr = 0xFF10 }; + enum { end_addr = 0xFF3f }; + enum { register_count = end_addr - start_addr + 1 }; + + // Write 'data' to address at specified time + void write_register( gb_time_t, gb_addr_t, int data ); + + // Read from address at specified time + int read_register( gb_time_t, gb_addr_t ); + + // Run all oscillators up to specified time, end current time frame, then + // start a new frame at time 0. Returns true if any oscillators added + // sound to one of the left/right buffers, false if they only added + // to the center buffer. + bool end_frame( gb_time_t ); + +public: + Gb_Apu(); + ~Gb_Apu(); +private: + // noncopyable + Gb_Apu( const Gb_Apu& ); + Gb_Apu& operator = ( const Gb_Apu& ); + + Gb_Osc* oscs [osc_count]; + gb_time_t next_frame_time; + gb_time_t last_time; + double volume_unit; + int frame_count; + bool stereo_found; + + Gb_Square square1; + Gb_Square square2; + Gb_Wave wave; + Gb_Noise noise; + BOOST::uint8_t regs [register_count]; + Gb_Square::Synth square_synth; // used by squares + Gb_Wave::Synth other_synth; // used by wave and noise + + bool gba; // enable GBA extensions to wave channel + + void update_volume(); + void run_until( gb_time_t ); + void write_osc( int index, int reg, int data ); +}; + +inline void Gb_Apu::output( Blip_Buffer* b ) { output( b, b, b ); } + +inline void Gb_Apu::osc_output( int i, Blip_Buffer* b ) { osc_output( i, b, b, b ); } + +inline void Gb_Apu::volume( double vol ) +{ + volume_unit = 0.60 / osc_count / 15 /*steps*/ / 2 /*?*/ / 8 /*master vol range*/ * vol; + update_volume(); +} + +#endif + diff --git a/Gb_Apu/Gb_Oscs.cpp b/Gb_Apu/Gb_Oscs.cpp new file mode 100644 index 00000000..674c039a --- /dev/null +++ b/Gb_Apu/Gb_Oscs.cpp @@ -0,0 +1,360 @@ + +// Gb_Snd_Emu 0.1.4. http://www.slack.net/~ant/ + +#include "Gb_Apu.h" + +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +// Gb_Osc + +void Gb_Osc::reset() +{ + delay = 0; + last_amp = 0; + length = 0; + output_select = 3; + output = outputs [output_select]; +} + +void Gb_Osc::clock_length() +{ + if ( (regs [4] & len_enabled_mask) && length ) + length--; +} + +// Gb_Env + +void Gb_Env::clock_envelope() +{ + if ( env_delay && !--env_delay ) + { + env_delay = regs [2] & 7; + int v = volume - 1 + (regs [2] >> 2 & 2); + if ( (unsigned) v < 15 ) + volume = v; + } +} + +bool Gb_Env::write_register( int reg, int data ) +{ + switch ( reg ) + { + case 1: + length = 64 - (regs [1] & 0x3f); + break; + + case 2: + if ( !(data >> 4) ) + enabled = false; + break; + + case 4: + if ( data & trigger ) + { + env_delay = regs [2] & 7; + volume = regs [2] >> 4; + enabled = true; + if ( length == 0 ) + length = 64; + return true; + } + } + return false; +} + +// Gb_Square + +void Gb_Square::reset() +{ + phase = 0; + sweep_freq = 0; + sweep_delay = 0; + Gb_Env::reset(); +} + +void Gb_Square::clock_sweep() +{ + int sweep_period = (regs [0] & period_mask) >> 4; + if ( sweep_period && sweep_delay && !--sweep_delay ) + { + sweep_delay = sweep_period; + regs [3] = sweep_freq & 0xFF; + regs [4] = (regs [4] & ~0x07) | (sweep_freq >> 8 & 0x07); + + int offset = sweep_freq >> (regs [0] & shift_mask); + if ( regs [0] & 0x08 ) + offset = -offset; + sweep_freq += offset; + + if ( sweep_freq < 0 ) + { + sweep_freq = 0; + } + else if ( sweep_freq >= 2048 ) + { + sweep_delay = 0; // don't modify channel frequency any further + sweep_freq = 2048; // silence sound immediately + } + } +} + +void Gb_Square::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + if ( sweep_freq == 2048 ) + playing = false; + + static unsigned char const table [4] = { 1, 2, 4, 6 }; + int const duty = table [regs [1] >> 6]; + int amp = volume & playing; + if ( phase >= duty ) + amp = -amp; + + int frequency = this->frequency(); + if ( unsigned (frequency - 1) > 2040 ) // frequency < 1 || frequency > 2041 + { + // really high frequency results in DC at half volume + amp = volume >> 1; + playing = false; + } + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + int const period = (2048 - frequency) * 4; + Blip_Buffer* const output = this->output; + int phase = this->phase; + int delta = amp * 2; + do + { + phase = (phase + 1) & 7; + if ( phase == 0 || phase == duty ) + { + delta = -delta; + synth->offset_inline( time, delta, output ); + } + time += period; + } + while ( time < end_time ); + + this->phase = phase; + last_amp = delta >> 1; + } + delay = time - end_time; +} + +// Gb_Noise + +#include BLARGG_ENABLE_OPTIMIZER + +void Gb_Noise::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + int amp = volume & playing; + int tap = 13 - (regs [3] & 8); + if ( bits >> tap & 2 ) + amp = -amp; + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + static unsigned char const table [8] = { 8, 16, 32, 48, 64, 80, 96, 112 }; + int period = table [regs [3] & 7] << (regs [3] >> 4); + + // keep parallel resampled time to eliminate time conversion in the loop + Blip_Buffer* const output = this->output; + const blip_resampled_time_t resampled_period = + output->resampled_duration( period ); + blip_resampled_time_t resampled_time = output->resampled_time( time ); + unsigned bits = this->bits; + int delta = amp * 2; + + do + { + unsigned changed = (bits >> tap) + 1; + time += period; + bits <<= 1; + if ( changed & 2 ) + { + delta = -delta; + bits |= 1; + synth->offset_resampled( resampled_time, delta, output ); + } + resampled_time += resampled_period; + } + while ( time < end_time ); + + this->bits = bits; + last_amp = delta >> 1; + } + delay = time - end_time; +} + +// Gb_Wave + +void Gb_Wave::reset(bool gba) +{ + volume_forced = 0; + wave_pos = 0; + wave_mode = gba; + wave_size = 32; + wave_bank = 0; + memset( wave, 0, sizeof wave ); + Gb_Osc::reset(); +} + +inline void Gb_Wave::write_register( int reg, int data ) +{ + switch ( reg ) + { + case 0: + if ( !(data & 0x80) ) + enabled = false; + if (wave_mode) + { + wave_bank = (data & 0x40) >> 1; + wave_size = (data & 0x20) + 32; + } + if (wave_pos > wave_size) wave_pos %= wave_size; + break; + + case 1: + length = 256 - regs [1]; + break; + + case 2: + volume = data >> 5 & 3; + if (wave_mode) volume_forced = data & 0x80; + if (volume_forced) volume = -1; + break; + + case 4: + if ( data & trigger & regs [0] ) + { + wave_pos = 0; + enabled = true; + if ( length == 0 ) + length = 256; + } + } +} + +void Gb_Wave::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + int volume_shift = (volume - 1) & 7; // volume = 0 causes shift = 7 + int amp = (wave_size == 32) ? wave [wave_bank + wave_pos] : wave [wave_pos]; + if (volume_forced) amp = ((amp >> 1) + amp) >> 1; + else amp >>= volume_shift; + amp = (amp & playing) * 2; + + int frequency = this->frequency(); + if ( unsigned (frequency - 1) > 2044 ) // frequency < 1 || frequency > 2045 + { + if (volume_forced) amp = ((30 >> 1) + 30) >> 1; + else amp = 30 >> volume_shift; + amp &= playing; + playing = false; + } + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + Blip_Buffer* const output = this->output; + int const period = (2048 - frequency) * 2; + int wave_pos = (this->wave_pos + 1) & (wave_size - 1); + + do + { + int amp = (wave_size == 32) ? wave [wave_bank + wave_pos] : wave [wave_pos]; + if (volume_forced) amp = ((amp >> 1) + amp) >> 1; + else amp >>= volume_shift; + amp *= 2; + wave_pos = (wave_pos + 1) & (wave_size - 1); + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset_inline( time, delta, output ); + } + time += period; + } + while ( time < end_time ); + + this->wave_pos = (wave_pos - 1) & (wave_size - 1); + } + delay = time - end_time; +} + +// Gb_Apu::write_osc + +void Gb_Apu::write_osc( int index, int reg, int data ) +{ + reg -= index * 5; + Gb_Square* sq = &square2; + switch ( index ) + { + case 0: + sq = &square1; + case 1: + if ( sq->write_register( reg, data ) && index == 0 ) + { + square1.sweep_freq = square1.frequency(); + if ( (regs [0] & sq->period_mask) && (regs [0] & sq->shift_mask) ) + { + square1.sweep_delay = 1; // cause sweep to recalculate now + square1.clock_sweep(); + } + } + break; + + case 2: + wave.write_register( reg, data ); + break; + + case 3: + if ( noise.write_register( reg, data ) ) + noise.bits = 0x7FFF; + } +} + diff --git a/Gb_Apu/Gb_Oscs.h b/Gb_Apu/Gb_Oscs.h new file mode 100644 index 00000000..40aa0d1a --- /dev/null +++ b/Gb_Apu/Gb_Oscs.h @@ -0,0 +1,90 @@ + +// Private oscillators used by Gb_Apu + +// Gb_Snd_Emu 0.1.4 + +#ifndef GB_OSCS_H +#define GB_OSCS_H + +#include "blargg_common.h" +#include "Blip_Buffer.h" + +struct Gb_Osc +{ + enum { trigger = 0x80 }; + enum { len_enabled_mask = 0x40 }; + + Blip_Buffer* outputs [4]; // NULL, right, left, center + Blip_Buffer* output; + int output_select; + BOOST::uint8_t* regs; // osc's 5 registers + + int delay; + int last_amp; + int volume; + int length; + bool enabled; + + void reset(); + void clock_length(); + int frequency() const { return (regs [4] & 7) * 0x100 + regs [3]; } +}; + +struct Gb_Env : Gb_Osc +{ + int env_delay; + + void reset(); + void clock_envelope(); + bool write_register( int, int ); +}; + +struct Gb_Square : Gb_Env +{ + enum { period_mask = 0x70 }; + enum { shift_mask = 0x07 }; + + typedef Blip_Synth Synth; + Synth const* synth; + int sweep_delay; + int sweep_freq; + int phase; + + void reset(); + void clock_sweep(); + void run( gb_time_t, gb_time_t, int playing ); +}; + +struct Gb_Noise : Gb_Env +{ + typedef Blip_Synth Synth; + Synth const* synth; + unsigned bits; + + void run( gb_time_t, gb_time_t, int playing ); +}; + +struct Gb_Wave : Gb_Osc +{ + typedef Blip_Synth Synth; + Synth const* synth; + int volume_forced; + int wave_pos; + unsigned wave_mode; + unsigned wave_size; + unsigned wave_bank; + BOOST::uint8_t wave [32 * 2]; + + void reset(bool gba = false); + void write_register( int, int ); + void run( gb_time_t, gb_time_t, int playing ); +}; + +inline void Gb_Env::reset() +{ + env_delay = 0; + Gb_Osc::reset(); +} + +#endif + diff --git a/Gb_Apu/Multi_Buffer.cpp b/Gb_Apu/Multi_Buffer.cpp new file mode 100644 index 00000000..37adb816 --- /dev/null +++ b/Gb_Apu/Multi_Buffer.cpp @@ -0,0 +1,215 @@ + +// Blip_Buffer 0.4.0. http://www.slack.net/~ant/ + +#include "Multi_Buffer.h" + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf ) +{ + length_ = 0; + sample_rate_ = 0; + channels_changed_count_ = 1; +} + +blargg_err_t Multi_Buffer::set_channel_count( int ) +{ + return blargg_success; +} + +Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 ) +{ +} + +Mono_Buffer::~Mono_Buffer() +{ +} + +blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec ) +{ + BLARGG_RETURN_ERR( buf.set_sample_rate( rate, msec ) ); + return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() ); +} + +// Silent_Buffer + +Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse +{ + chan.left = NULL; + chan.center = NULL; + chan.right = NULL; +} + +// Mono_Buffer + +Mono_Buffer::channel_t Mono_Buffer::channel( int ) +{ + channel_t ch; + ch.center = &buf; + ch.left = &buf; + ch.right = &buf; + return ch; +} + +void Mono_Buffer::end_frame( blip_time_t t, bool ) +{ + buf.end_frame( t ); +} + +// Stereo_Buffer + +Stereo_Buffer::Stereo_Buffer() : Multi_Buffer( 2 ) +{ + chan.center = &bufs [0]; + chan.left = &bufs [1]; + chan.right = &bufs [2]; +} + +Stereo_Buffer::~Stereo_Buffer() +{ +} + +blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec ) +{ + for ( int i = 0; i < buf_count; i++ ) + BLARGG_RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) ); + return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() ); +} + +void Stereo_Buffer::clock_rate( long rate ) +{ + for ( int i = 0; i < buf_count; i++ ) + bufs [i].clock_rate( rate ); +} + +void Stereo_Buffer::bass_freq( int bass ) +{ + for ( unsigned i = 0; i < buf_count; i++ ) + bufs [i].bass_freq( bass ); +} + +void Stereo_Buffer::clear() +{ + stereo_added = false; + was_stereo = false; + for ( int i = 0; i < buf_count; i++ ) + bufs [i].clear(); +} + +void Stereo_Buffer::end_frame( blip_time_t clock_count, bool stereo ) +{ + for ( unsigned i = 0; i < buf_count; i++ ) + bufs [i].end_frame( clock_count ); + + stereo_added |= stereo; +} + +long Stereo_Buffer::read_samples( blip_sample_t* out, long count ) +{ + require( !(count & 1) ); // count must be even + count = (unsigned) count / 2; + + long avail = bufs [0].samples_avail(); + if ( count > avail ) + count = avail; + if ( count ) + { + if ( stereo_added || was_stereo ) + { + mix_stereo( out, count ); + + bufs [0].remove_samples( count ); + bufs [1].remove_samples( count ); + bufs [2].remove_samples( count ); + } + else + { + mix_mono( out, count ); + + bufs [0].remove_samples( count ); + + bufs [1].remove_silence( count ); + bufs [2].remove_silence( count ); + } + + // to do: this might miss opportunities for optimization + if ( !bufs [0].samples_avail() ) { + was_stereo = stereo_added; + stereo_added = false; + } + } + + return count * 2; +} + +#include BLARGG_ENABLE_OPTIMIZER + +void Stereo_Buffer::mix_stereo( blip_sample_t* out, long count ) +{ + Blip_Reader left; + Blip_Reader right; + Blip_Reader center; + + left.begin( bufs [1] ); + right.begin( bufs [2] ); + int bass = center.begin( bufs [0] ); + + while ( count-- ) + { + int c = center.read(); + long l = c + left.read(); + long r = c + right.read(); + center.next( bass ); + out [0] = l; + out [1] = r; + out += 2; + + if ( (BOOST::int16_t) l != l ) + out [-2] = 0x7FFF - (l >> 24); + + left.next( bass ); + right.next( bass ); + + if ( (BOOST::int16_t) r != r ) + out [-1] = 0x7FFF - (r >> 24); + } + + center.end( bufs [0] ); + right.end( bufs [2] ); + left.end( bufs [1] ); +} + +void Stereo_Buffer::mix_mono( blip_sample_t* out, long count ) +{ + Blip_Reader in; + int bass = in.begin( bufs [0] ); + + while ( count-- ) + { + long s = in.read(); + in.next( bass ); + out [0] = s; + out [1] = s; + out += 2; + + if ( (BOOST::int16_t) s != s ) { + s = 0x7FFF - (s >> 24); + out [-2] = s; + out [-1] = s; + } + } + + in.end( bufs [0] ); +} + diff --git a/Gb_Apu/Multi_Buffer.h b/Gb_Apu/Multi_Buffer.h new file mode 100644 index 00000000..155b6ec5 --- /dev/null +++ b/Gb_Apu/Multi_Buffer.h @@ -0,0 +1,175 @@ + +// Multi-channel sound buffer interface, and basic mono and stereo buffers + +// Blip_Buffer 0.4.0 + +#ifndef MULTI_BUFFER_H +#define MULTI_BUFFER_H + +#include "blargg_common.h" +#include "Blip_Buffer.h" + +// Interface to one or more Blip_Buffers mapped to one or more channels +// consisting of left, center, and right buffers. +class Multi_Buffer { +public: + Multi_Buffer( int samples_per_frame ); + virtual ~Multi_Buffer() { } + + // Set the number of channels available + virtual blargg_err_t set_channel_count( int ); + + // Get indexed channel, from 0 to channel count - 1 + struct channel_t { + Blip_Buffer* center; + Blip_Buffer* left; + Blip_Buffer* right; + }; + virtual channel_t channel( int index ) = 0; + + // See Blip_Buffer.h + virtual blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ) = 0; + virtual void clock_rate( long ) = 0; + virtual void bass_freq( int ) = 0; + virtual void clear() = 0; + long sample_rate() const; + + // Length of buffer, in milliseconds + int length() const; + + // See Blip_Buffer.h. For optimal operation, pass false for 'added_stereo' + // if nothing was added to the left and right buffers of any channel for + // this time frame. + virtual void end_frame( blip_time_t, bool added_stereo = true ) = 0; + + // Number of samples per output frame (1 = mono, 2 = stereo) + int samples_per_frame() const; + + // Count of changes to channel configuration. Incremented whenever + // a change is made to any of the Blip_Buffers for any channel. + unsigned channels_changed_count() { return channels_changed_count_; } + + // See Blip_Buffer.h + virtual long read_samples( blip_sample_t*, long ) = 0; + virtual long samples_avail() const = 0; + +protected: + void channels_changed() { channels_changed_count_++; } +private: + // noncopyable + Multi_Buffer( const Multi_Buffer& ); + Multi_Buffer& operator = ( const Multi_Buffer& ); + + unsigned channels_changed_count_; + long sample_rate_; + int length_; + int const samples_per_frame_; +}; + +// Uses a single buffer and outputs mono samples. +class Mono_Buffer : public Multi_Buffer { + Blip_Buffer buf; +public: + Mono_Buffer(); + ~Mono_Buffer(); + + // Buffer used for all channels + Blip_Buffer* center() { return &buf; } + + // See Multi_Buffer + blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ); + void clock_rate( long ); + void bass_freq( int ); + void clear(); + channel_t channel( int ); + void end_frame( blip_time_t, bool unused = true ); + long samples_avail() const; + long read_samples( blip_sample_t*, long ); +}; + +// Uses three buffers (one for center) and outputs stereo sample pairs. +class Stereo_Buffer : public Multi_Buffer { +public: + Stereo_Buffer(); + ~Stereo_Buffer(); + + // Buffers used for all channels + Blip_Buffer* center() { return &bufs [0]; } + Blip_Buffer* left() { return &bufs [1]; } + Blip_Buffer* right() { return &bufs [2]; } + + // See Multi_Buffer + blargg_err_t set_sample_rate( long, int msec = blip_default_length ); + void clock_rate( long ); + void bass_freq( int ); + void clear(); + channel_t channel( int index ); + void end_frame( blip_time_t, bool added_stereo = true ); + + long samples_avail() const; + long read_samples( blip_sample_t*, long ); + +private: + enum { buf_count = 3 }; + Blip_Buffer bufs [buf_count]; + channel_t chan; + bool stereo_added; + bool was_stereo; + + void mix_stereo( blip_sample_t*, long ); + void mix_mono( blip_sample_t*, long ); +}; + +// Silent_Buffer generates no samples, useful where no sound is wanted +class Silent_Buffer : public Multi_Buffer { + channel_t chan; +public: + Silent_Buffer(); + + blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ); + void clock_rate( long ) { } + void bass_freq( int ) { } + void clear() { } + channel_t channel( int ) { return chan; } + void end_frame( blip_time_t, bool unused = true ) { } + long samples_avail() const { return 0; } + long read_samples( blip_sample_t*, long ) { return 0; } +}; + + +// End of public interface + +inline blargg_err_t Multi_Buffer::set_sample_rate( long rate, int msec ) +{ + sample_rate_ = rate; + length_ = msec; + return blargg_success; +} + +inline blargg_err_t Silent_Buffer::set_sample_rate( long rate, int msec ) +{ + return Multi_Buffer::set_sample_rate( rate, msec ); +} + +inline int Multi_Buffer::samples_per_frame() const { return samples_per_frame_; } + +inline long Stereo_Buffer::samples_avail() const { return bufs [0].samples_avail() * 2; } + +inline Stereo_Buffer::channel_t Stereo_Buffer::channel( int ) { return chan; } + +inline long Multi_Buffer::sample_rate() const { return sample_rate_; } + +inline int Multi_Buffer::length() const { return length_; } + +inline void Mono_Buffer::clock_rate( long rate ) { buf.clock_rate( rate ); } + +inline void Mono_Buffer::clear() { buf.clear(); } + +inline void Mono_Buffer::bass_freq( int freq ) { buf.bass_freq( freq ); } + +inline long Mono_Buffer::read_samples( blip_sample_t* p, long s ) { return buf.read_samples( p, s ); } + +inline long Mono_Buffer::samples_avail() const { return buf.samples_avail(); } + +#endif + diff --git a/Gb_Apu/blargg_common.h b/Gb_Apu/blargg_common.h new file mode 100644 index 00000000..b7e9136e --- /dev/null +++ b/Gb_Apu/blargg_common.h @@ -0,0 +1,242 @@ + +// Sets up common environment for Shay Green's libraries. +// +// To change configuration options, modify blargg_config.h, not this file. + +#ifndef BLARGG_COMMON_H +#define BLARGG_COMMON_H + +// HAVE_CONFIG_H: If defined, include user's "config.h" first (which *can* +// re-include blargg_common.h if it needs to) +#ifdef HAVE_CONFIG_H + #undef BLARGG_COMMON_H + #include "config.h" + #define BLARGG_COMMON_H +#endif + +// BLARGG_NONPORTABLE: If defined to 1, platform-specific (and possibly non-portable) +// optimizations are used. Defaults to off. Report any problems that occur only when +// this is enabled. +#ifndef BLARGG_NONPORTABLE + #define BLARGG_NONPORTABLE 0 +#endif + +// BLARGG_BIG_ENDIAN, BLARGG_LITTLE_ENDIAN: Determined automatically, otherwise only +// one must be #defined to 1. Only needed if something actually depends on byte order. +#if !defined (BLARGG_BIG_ENDIAN) && !defined (BLARGG_LITTLE_ENDIAN) + #if defined (MSB_FIRST) || defined (__powerc) || defined (macintosh) || \ + defined (WORDS_BIGENDIAN) || defined (__BIG_ENDIAN__) + #define BLARGG_BIG_ENDIAN 1 + #else + #define BLARGG_LITTLE_ENDIAN 1 + #endif +#endif + +// Determine compiler's language support + +// Metrowerks CodeWarrior +#if defined (__MWERKS__) + #define BLARGG_COMPILER_HAS_NAMESPACE 1 + #if !__option(bool) + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + #define STATIC_CAST(T,expr) static_cast< T > (expr) + +// Microsoft Visual C++ +#elif defined (_MSC_VER) + #if _MSC_VER < 1100 + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + +// GNU C++ +#elif defined (__GNUC__) + #if __GNUC__ > 2 + #define BLARGG_COMPILER_HAS_NAMESPACE 1 + #endif + +// Mingw +#elif defined (__MINGW32__) + // empty + +// Pre-ISO C++ compiler +#elif __cplusplus < 199711 + #ifndef BLARGG_COMPILER_HAS_BOOL + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + +#endif + +/* BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compilers. + If errors occur here, add the following line to your config.h file: + #define BLARGG_COMPILER_HAS_BOOL 0 +*/ +#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL + typedef int bool; + const bool true = 1; + const bool false = 0; +#endif + +// BLARGG_USE_NAMESPACE: If 1, use headers rather than +#if BLARGG_USE_NAMESPACE || (!defined (BLARGG_USE_NAMESPACE) && BLARGG_COMPILER_HAS_NAMESPACE) + #include + #include + #include + #include + #define STD std +#else + #include + #include + #include + #include + #define STD +#endif + +// BLARGG_NEW is used in place of 'new' to create objects. By default, plain new is used. +// To prevent an exception if out of memory, #define BLARGG_NEW new (std::nothrow) +#ifndef BLARGG_NEW + #define BLARGG_NEW new +#endif + +// BOOST::int8_t etc. + +// HAVE_STDINT_H: If defined, use for int8_t etc. +#if defined (HAVE_STDINT_H) + #include + #define BOOST + +// HAVE_INTTYPES_H: If defined, use for int8_t etc. +#elif defined (HAVE_INTTYPES_H) + #include + #define BOOST + +#else + struct BOOST + { + #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F + typedef signed char int8_t; + typedef unsigned char uint8_t; + #else + // No suitable 8-bit type available + typedef struct see_blargg_common_h int8_t; + typedef struct see_blargg_common_h uint8_t; + #endif + + #if USHRT_MAX == 0xFFFF + typedef short int16_t; + typedef unsigned short uint16_t; + #else + // No suitable 16-bit type available + typedef struct see_blargg_common_h int16_t; + typedef struct see_blargg_common_h uint16_t; + #endif + + #if ULONG_MAX == 0xFFFFFFFF + typedef long int32_t; + typedef unsigned long uint32_t; + #elif UINT_MAX == 0xFFFFFFFF + typedef int int32_t; + typedef unsigned int uint32_t; + #else + // No suitable 32-bit type available + typedef struct see_blargg_common_h int32_t; + typedef struct see_blargg_common_h uint32_t; + #endif + }; +#endif + +// BLARGG_SOURCE_BEGIN: Library sources #include this after other #includes. +#ifndef BLARGG_SOURCE_BEGIN + #define BLARGG_SOURCE_BEGIN "blargg_source.h" +#endif + +// BLARGG_ENABLE_OPTIMIZER: Library sources #include this for speed-critical code +#ifndef BLARGG_ENABLE_OPTIMIZER + #define BLARGG_ENABLE_OPTIMIZER "blargg_common.h" +#endif + +// BLARGG_CPU_*: Used to select between some optimizations +#if !defined (BLARGG_CPU_POWERPC) && !defined (BLARGG_CPU_X86) + #if defined (__powerc) + #define BLARGG_CPU_POWERPC 1 + #elif defined (_MSC_VER) && defined (_M_IX86) + #define BLARGG_CPU_X86 1 + #endif +#endif + +// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0. +#ifndef BOOST_STATIC_ASSERT + #ifdef _MSC_VER + // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified + #define BOOST_STATIC_ASSERT( expr ) \ + void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] ) + #else + // Some other compilers fail when declaring same function multiple times in class, + // so differentiate them by line + #define BOOST_STATIC_ASSERT( expr ) \ + void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] [__LINE__] ) + #endif +#endif + +// STATIC_CAST(T,expr): Used in place of static_cast (expr) +#ifndef STATIC_CAST + #define STATIC_CAST(T,expr) ((T) (expr)) +#endif + +// blargg_err_t (NULL on success, otherwise error string) +#ifndef blargg_err_t + typedef const char* blargg_err_t; +#endif +const char* const blargg_success = 0; + +// blargg_vector: Simple array that does *not* work for types with a constructor (non-POD). +template +class blargg_vector { + T* begin_; + STD::size_t size_; +public: + blargg_vector() : begin_( 0 ), size_( 0 ) { } + ~blargg_vector() { STD::free( begin_ ); } + + typedef STD::size_t size_type; + + blargg_err_t resize( size_type n ) + { + void* p = STD::realloc( begin_, n * sizeof (T) ); + if ( !p && n ) + return "Out of memory"; + begin_ = (T*) p; + size_ = n; + return 0; + } + + void clear() + { + void* p = begin_; + begin_ = 0; + size_ = 0; + STD::free( p ); + } + + size_type size() const { return size_; } + + T* begin() { return begin_; } + T* end() { return begin_ + size_; } + + const T* begin() const { return begin_; } + const T* end() const { return begin_ + size_; } + + T& operator [] ( size_type n ) + { + assert( n <= size_ ); // allow for past-the-end value + return begin_ [n]; + } + + const T& operator [] ( size_type n ) const + { + assert( n <= size_ ); // allow for past-the-end value + return begin_ [n]; + } +}; + +#endif + diff --git a/Gb_Apu/blargg_endian.h b/Gb_Apu/blargg_endian.h new file mode 100644 index 00000000..af461b9d --- /dev/null +++ b/Gb_Apu/blargg_endian.h @@ -0,0 +1,156 @@ + +// CPU Byte Order Utilities + +// Game_Music_Emu 0.3.0 + +#ifndef BLARGG_ENDIAN +#define BLARGG_ENDIAN + +#include "blargg_common.h" + +#if 0 + // Read 16/32-bit little-endian integer from memory + unsigned GET_LE16( void const* ); + unsigned long GET_LE32( void const* ); + + // Read 16/32-bit big-endian integer from memory + unsigned GET_BE16( void const* ); + unsigned long GET_BE32( void const* ); + + // Write 16/32-bit integer to memory in little-endian format + void SET_LE16( void*, unsigned ); + void SET_LE32( void*, unsigned ); + + // Write 16/32-bit integer to memory in big-endian format + void SET_BE16( void*, unsigned long ); + void SET_BE32( void*, unsigned long ); +#endif + +inline unsigned get_le16( void const* p ) +{ + return ((unsigned char*) p) [1] * 0x100 + + ((unsigned char*) p) [0]; +} + +inline unsigned get_be16( void const* p ) +{ + return ((unsigned char*) p) [0] * 0x100 + + ((unsigned char*) p) [1]; +} + +inline unsigned long get_le32( void const* p ) +{ + return ((unsigned char*) p) [3] * 0x01000000 + + ((unsigned char*) p) [2] * 0x00010000 + + ((unsigned char*) p) [1] * 0x00000100 + + ((unsigned char*) p) [0]; +} + +inline unsigned long get_be32( void const* p ) +{ + return ((unsigned char*) p) [0] * 0x01000000 + + ((unsigned char*) p) [1] * 0x00010000 + + ((unsigned char*) p) [2] * 0x00000100 + + ((unsigned char*) p) [3]; +} + +inline void set_le16( void* p, unsigned n ) +{ + ((unsigned char*) p) [1] = (unsigned char) (n >> 8); + ((unsigned char*) p) [0] = (unsigned char) n; +} + +inline void set_be16( void* p, unsigned n ) +{ + ((unsigned char*) p) [0] = (unsigned char) (n >> 8); + ((unsigned char*) p) [1] = (unsigned char) n; +} + +inline void set_le32( void* p, unsigned long n ) +{ + ((unsigned char*) p) [3] = (unsigned char) (n >> 24); + ((unsigned char*) p) [2] = (unsigned char) (n >> 16); + ((unsigned char*) p) [1] = (unsigned char) (n >> 8); + ((unsigned char*) p) [0] = (unsigned char) n; +} + +inline void set_be32( void* p, unsigned long n ) +{ + ((unsigned char*) p) [0] = (unsigned char) (n >> 24); + ((unsigned char*) p) [1] = (unsigned char) (n >> 16); + ((unsigned char*) p) [2] = (unsigned char) (n >> 8); + ((unsigned char*) p) [3] = (unsigned char) n; +} + +#ifndef GET_LE16 + // Optimized implementation if byte order is known + #if BLARGG_NONPORTABLE && BLARGG_LITTLE_ENDIAN + #define GET_LE16( addr ) (*(BOOST::uint16_t*) (addr)) + #define GET_LE32( addr ) (*(BOOST::uint32_t*) (addr)) + #define SET_LE16( addr, data ) (void (*(BOOST::uint16_t*) (addr) = (data))) + #define SET_LE32( addr, data ) (void (*(BOOST::uint32_t*) (addr) = (data))) + + #elif BLARGG_NONPORTABLE && BLARGG_CPU_POWERPC + // PowerPC has special byte-reversed instructions + // to do: assumes that PowerPC is running in big-endian mode + #define GET_LE16( addr ) (__lhbrx( (addr), 0 )) + #define GET_LE32( addr ) (__lwbrx( (addr), 0 )) + #define SET_LE16( addr, data ) (__sthbrx( (data), (addr), 0 )) + #define SET_LE32( addr, data ) (__stwbrx( (data), (addr), 0 )) + + #define GET_BE16( addr ) (*(BOOST::uint16_t*) (addr)) + #define GET_BE32( addr ) (*(BOOST::uint32_t*) (addr)) + #define SET_BE16( addr, data ) (void (*(BOOST::uint16_t*) (addr) = (data))) + #define SET_BE32( addr, data ) (void (*(BOOST::uint32_t*) (addr) = (data))) + + #endif +#endif + +#ifndef GET_LE16 + #define GET_LE16( addr ) get_le16( addr ) +#endif + +#ifndef GET_LE32 + #define GET_LE32( addr ) get_le32( addr ) +#endif + +#ifndef SET_LE16 + #define SET_LE16( addr, data ) set_le16( addr, data ) +#endif + +#ifndef SET_LE32 + #define SET_LE32( addr, data ) set_le32( addr, data ) +#endif + +#ifndef GET_BE16 + #define GET_BE16( addr ) get_be16( addr ) +#endif + +#ifndef GET_BE32 + #define GET_BE32( addr ) get_be32( addr ) +#endif + +#ifndef SET_BE16 + #define SET_BE16( addr, data ) set_be16( addr, data ) +#endif + +#ifndef SET_BE32 + #define SET_BE32( addr, data ) set_be32( addr, data ) +#endif + +// auto-selecting versions + +inline void set_le( BOOST::uint16_t* p, unsigned n ) { SET_LE16( p, n ); } +inline void set_le( BOOST::uint32_t* p, unsigned long n ) { SET_LE32( p, n ); } + +inline void set_be( BOOST::uint16_t* p, unsigned n ) { SET_BE16( p, n ); } +inline void set_be( BOOST::uint32_t* p, unsigned long n ) { SET_BE32( p, n ); } + +inline unsigned get_le( BOOST::uint16_t* p ) { return GET_LE16( p ); } +inline unsigned long get_le( BOOST::uint32_t* p ) { return GET_LE32( p ); } + +inline unsigned get_be( BOOST::uint16_t* p ) { return GET_BE16( p ); } +inline unsigned long get_be( BOOST::uint32_t* p ) { return GET_BE32( p ); } + +#endif + diff --git a/Gb_Apu/blargg_source.h b/Gb_Apu/blargg_source.h new file mode 100644 index 00000000..34f17f8d --- /dev/null +++ b/Gb_Apu/blargg_source.h @@ -0,0 +1,76 @@ + +// By default, #included at beginning of library source files. +// Can be overridden by #defining BLARGG_SOURCE_BEGIN to path of alternate file. + +// Copyright (C) 2005 Shay Green. + +#ifndef BLARGG_SOURCE_H +#define BLARGG_SOURCE_H + +// If debugging is enabled, abort program if expr is false. Meant for checking +// internal state and consistency. A failed assertion indicates a bug in the module. +// void assert( bool expr ); +#include + +// If debugging is enabled and expr is false, abort program. Meant for checking +// caller-supplied parameters and operations that are outside the control of the +// module. A failed requirement indicates a bug outside the module. +// void require( bool expr ); +#undef require +#define require( expr ) assert( expr ) + +// Like printf() except output goes to debug log file. Might be defined to do +// nothing (not even evaluate its arguments). +// void dprintf( const char* format, ... ); +#undef dprintf +#ifdef BLARGG_DPRINTF + #define dprintf BLARGG_DPRINTF +#else + inline void blargg_dprintf_( const char*, ... ) { } + #define dprintf (1) ? (void) 0 : blargg_dprintf_ +#endif + +// If enabled, evaluate expr and if false, make debug log entry with source file +// and line. Meant for finding situations that should be examined further, but that +// don't indicate a problem. In all cases, execution continues normally. +#undef check +#ifdef BLARGG_CHECK + #define check( expr ) BLARGG_CHECK( expr ) +#else + #define check( expr ) ((void) 0) +#endif + +// If expr returns non-NULL error string, return it from current function, otherwise continue. +#define BLARGG_RETURN_ERR( expr ) do { \ + blargg_err_t blargg_return_err_ = (expr); \ + if ( blargg_return_err_ ) return blargg_return_err_; \ + } while ( 0 ) + +// If ptr is NULL, return out of memory error string. +#define BLARGG_CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 ) + +// Avoid any macros which evaluate their arguments multiple times +#undef min +#undef max + +// using const references generates crappy code, and I am currenly only using these +// for built-in types, so they take arguments by value + +template +inline T min( T x, T y ) +{ + if ( x < y ) + return x; + return y; +} + +template +inline T max( T x, T y ) +{ + if ( x < y ) + return y; + return x; +} + +#endif + diff --git a/Gb_Apu/boost/config.hpp b/Gb_Apu/boost/config.hpp new file mode 100644 index 00000000..76c2441f --- /dev/null +++ b/Gb_Apu/boost/config.hpp @@ -0,0 +1,13 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_CONFIG_HPP +#define BOOST_CONFIG_HPP + +#define BOOST_MINIMAL 1 + +#define BLARGG_BEGIN_NAMESPACE( name ) +#define BLARGG_END_NAMESPACE + +#endif + diff --git a/Gb_Apu/boost/cstdint.hpp b/Gb_Apu/boost/cstdint.hpp new file mode 100644 index 00000000..97f4feed --- /dev/null +++ b/Gb_Apu/boost/cstdint.hpp @@ -0,0 +1,42 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_CSTDINT_HPP +#define BOOST_CSTDINT_HPP + +#if BLARGG_USE_NAMESPACE + #include +#else + #include +#endif + +BLARGG_BEGIN_NAMESPACE( boost ) + +#if UCHAR_MAX != 0xFF || SCHAR_MAX != 0x7F +# error "No suitable 8-bit type available" +#endif + +typedef unsigned char uint8_t; +typedef signed char int8_t; + +#if USHRT_MAX != 0xFFFF +# error "No suitable 16-bit type available" +#endif + +typedef short int16_t; +typedef unsigned short uint16_t; + +#if ULONG_MAX == 0xFFFFFFFF + typedef long int32_t; + typedef unsigned long uint32_t; +#elif UINT_MAX == 0xFFFFFFFF + typedef int int32_t; + typedef unsigned int uint32_t; +#else +# error "No suitable 32-bit type available" +#endif + +BLARGG_END_NAMESPACE + +#endif + diff --git a/Gb_Apu/boost/static_assert.hpp b/Gb_Apu/boost/static_assert.hpp new file mode 100644 index 00000000..2f80ff05 --- /dev/null +++ b/Gb_Apu/boost/static_assert.hpp @@ -0,0 +1,22 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_STATIC_ASSERT_HPP +#define BOOST_STATIC_ASSERT_HPP + +#if defined (_MSC_VER) && _MSC_VER <= 1200 + // MSVC6 can't handle the ##line concatenation + #define BOOST_STATIC_ASSERT( expr ) struct { int n [1 / ((expr) ? 1 : 0)]; } + +#else + #define BOOST_STATIC_ASSERT3( expr, line ) \ + typedef int boost_static_assert_##line [1 / ((expr) ? 1 : 0)] + + #define BOOST_STATIC_ASSERT2( expr, line ) BOOST_STATIC_ASSERT3( expr, line ) + + #define BOOST_STATIC_ASSERT( expr ) BOOST_STATIC_ASSERT2( expr, __LINE__ ) + +#endif + +#endif + diff --git a/SDL.dll b/SDL.dll new file mode 100644 index 0000000000000000000000000000000000000000..2af886c6db5eee352c32d174a1ee15dd80346582 GIT binary patch literal 225280 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 literal 0 HcmV?d00001 diff --git a/VBA.sln b/VBA.sln new file mode 100644 index 00000000..2e9c9bff --- /dev/null +++ b/VBA.sln @@ -0,0 +1,45 @@ +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisualBoyAdvance", "VBA.vcproj", "{6D4C5EC8-933F-4C05-A1BF-498E658576DF}" + ProjectSection(ProjectDependencies) = postProject + {664BE444-CCED-4C81-9724-7057ECBAAC82} = {664BE444-CCED-4C81-9724-7057ECBAAC82} + {34DC39BF-F93A-4573-85BF-789B90B63179} = {34DC39BF-F93A-4573-85BF-789B90B63179} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zlib", "zlib\zlib.vcproj", "{34DC39BF-F93A-4573-85BF-789B90B63179}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libpng", "libpng\libpng.vcproj", "{664BE444-CCED-4C81-9724-7057ECBAAC82}" + ProjectSection(ProjectDependencies) = postProject + {34DC39BF-F93A-4573-85BF-789B90B63179} = {34DC39BF-F93A-4573-85BF-789B90B63179} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Optimized|Win32 = Optimized|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Debug|Win32.ActiveCfg = Debug|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Debug|Win32.Build.0 = Debug|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Optimized|Win32.ActiveCfg = Optimized|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Optimized|Win32.Build.0 = Optimized|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Release|Win32.ActiveCfg = Release|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Release|Win32.Build.0 = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Debug|Win32.ActiveCfg = Debug|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Debug|Win32.Build.0 = Debug|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Optimized|Win32.ActiveCfg = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Optimized|Win32.Build.0 = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Release|Win32.ActiveCfg = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Release|Win32.Build.0 = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Debug|Win32.ActiveCfg = Debug|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Debug|Win32.Build.0 = Debug|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Optimized|Win32.ActiveCfg = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Optimized|Win32.Build.0 = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Release|Win32.ActiveCfg = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/VBA.sln.old b/VBA.sln.old new file mode 100644 index 00000000..e05604ab --- /dev/null +++ b/VBA.sln.old @@ -0,0 +1,47 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisualBoyAdvance", "VBA.vcproj", "{6D4C5EC8-933F-4C05-A1BF-498E658576DF}" + ProjectSection(ProjectDependencies) = postProject + {664BE444-CCED-4C81-9724-7057ECBAAC82} = {664BE444-CCED-4C81-9724-7057ECBAAC82} + {34DC39BF-F93A-4573-85BF-789B90B63179} = {34DC39BF-F93A-4573-85BF-789B90B63179} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zlib", "zlib\zlib.vcproj", "{34DC39BF-F93A-4573-85BF-789B90B63179}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libpng", "libpng\libpng.vcproj", "{664BE444-CCED-4C81-9724-7057ECBAAC82}" + ProjectSection(ProjectDependencies) = postProject + {34DC39BF-F93A-4573-85BF-789B90B63179} = {34DC39BF-F93A-4573-85BF-789B90B63179} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Optimized = Optimized + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Debug.ActiveCfg = Debug|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Debug.Build.0 = Debug|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Optimized.ActiveCfg = Optimized|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Optimized.Build.0 = Optimized|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Release.ActiveCfg = Release|Win32 + {6D4C5EC8-933F-4C05-A1BF-498E658576DF}.Release.Build.0 = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Debug.ActiveCfg = Debug|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Debug.Build.0 = Debug|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Optimized.ActiveCfg = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Optimized.Build.0 = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Release.ActiveCfg = Release|Win32 + {34DC39BF-F93A-4573-85BF-789B90B63179}.Release.Build.0 = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Debug.ActiveCfg = Debug|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Debug.Build.0 = Debug|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Optimized.ActiveCfg = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Optimized.Build.0 = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Release.ActiveCfg = Release|Win32 + {664BE444-CCED-4C81-9724-7057ECBAAC82}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/VBA.suo b/VBA.suo new file mode 100644 index 0000000000000000000000000000000000000000..8a28adcf92d3bce76df72493398d151a60dfb80a GIT binary patch literal 53248 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 RWHw_JH75W2^Zzgh{tr1=@jw6o literal 0 HcmV?d00001 diff --git a/VBA.vcproj b/VBA.vcproj new file mode 100644 index 00000000..4a45c764 --- /dev/null +++ b/VBA.vcproj @@ -0,0 +1,1580 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/VBA.vcproj.7.10.old b/VBA.vcproj.7.10.old new file mode 100644 index 00000000..581eb856 --- /dev/null +++ b/VBA.vcproj.7.10.old @@ -0,0 +1,1116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gbafilter.cpp b/gbafilter.cpp new file mode 100644 index 00000000..171ec417 --- /dev/null +++ b/gbafilter.cpp @@ -0,0 +1,227 @@ +#include "gbafilter.h" + +#include + +extern int systemColorDepth; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; + +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; + +static const unsigned char curve[32] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x10, 0x12, + 0x14, 0x16, 0x18, 0x1c, 0x20, 0x28, 0x30, 0x38, + 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x80, + 0x88, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}; + +// output R G B +static const unsigned char influence[3 * 3] = { 16, 4, 4, // red + 8, 16, 8, // green + 0, 8, 16};// blue + +inline void swap(short & a, short & b) +{ + short temp = a; + a = b; + b = temp; +} + +void gbafilter_pal(u16 * buf, int count) +{ + short temp[3 * 3], s; + unsigned pix; + u8 red, green, blue; + + while (count--) + { + pix = *buf; + + s = curve[(pix >> systemGreenShift) & 0x1f]; + temp[3] = s * influence[3]; + temp[4] = s * influence[4]; + temp[5] = s * influence[5]; + + s = curve[(pix >> systemRedShift) & 0x1f]; + temp[0] = s * influence[0]; + temp[1] = s * influence[1]; + temp[2] = s * influence[2]; + + s = curve[(pix >> systemBlueShift) & 0x1f]; + temp[6] = s * influence[6]; + temp[7] = s * influence[7]; + temp[8] = s * influence[8]; + + if (temp[0] < temp[3]) swap(temp[0], temp[3]); + if (temp[0] < temp[6]) swap(temp[0], temp[6]); + if (temp[3] < temp[6]) swap(temp[3], temp[6]); + temp[3] <<= 1; + temp[0] <<= 2; + temp[0] += temp[3] + temp[6]; + + red = ((int(temp[0]) * 160) >> 17) + 4; + if (red > 31) red = 31; + + if (temp[2] < temp[5]) swap(temp[2], temp[5]); + if (temp[2] < temp[8]) swap(temp[2], temp[8]); + if (temp[5] < temp[8]) swap(temp[5], temp[8]); + temp[5] <<= 1; + temp[2] <<= 2; + temp[2] += temp[5] + temp[8]; + + blue = ((int(temp[2]) * 160) >> 17) + 4; + if (blue > 31) blue = 31; + + if (temp[1] < temp[4]) swap(temp[1], temp[4]); + if (temp[1] < temp[7]) swap(temp[1], temp[7]); + if (temp[4] < temp[7]) swap(temp[4], temp[7]); + temp[4] <<= 1; + temp[1] <<= 2; + temp[1] += temp[4] + temp[7]; + + green = ((int(temp[1]) * 160) >> 17) + 4; + if (green > 31) green = 31; + + pix = red << systemRedShift; + pix += green << systemGreenShift; + pix += blue << systemBlueShift; + + *buf++ = pix; + } +} + +void gbafilter_pal32(u32 * buf, int count) +{ + short temp[3 * 3], s; + unsigned pix; + u8 red, green, blue; + + while (count--) + { + pix = *buf; + + s = curve[(pix >> systemGreenShift) & 0x1f]; + temp[3] = s * influence[3]; + temp[4] = s * influence[4]; + temp[5] = s * influence[5]; + + s = curve[(pix >> systemRedShift) & 0x1f]; + temp[0] = s * influence[0]; + temp[1] = s * influence[1]; + temp[2] = s * influence[2]; + + s = curve[(pix >> systemBlueShift) & 0x1f]; + temp[6] = s * influence[6]; + temp[7] = s * influence[7]; + temp[8] = s * influence[8]; + + if (temp[0] < temp[3]) swap(temp[0], temp[3]); + if (temp[0] < temp[6]) swap(temp[0], temp[6]); + if (temp[3] < temp[6]) swap(temp[3], temp[6]); + temp[3] <<= 1; + temp[0] <<= 2; + temp[0] += temp[3] + temp[6]; + + //red = ((int(temp[0]) * 160) >> 17) + 4; + red = ((int(temp[0]) * 160) >> 14) + 32; + + if (temp[2] < temp[5]) swap(temp[2], temp[5]); + if (temp[2] < temp[8]) swap(temp[2], temp[8]); + if (temp[5] < temp[8]) swap(temp[5], temp[8]); + temp[5] <<= 1; + temp[2] <<= 2; + temp[2] += temp[5] + temp[8]; + + //blue = ((int(temp[2]) * 160) >> 17) + 4; + blue = ((int(temp[2]) * 160) >> 14) + 32; + + if (temp[1] < temp[4]) swap(temp[1], temp[4]); + if (temp[1] < temp[7]) swap(temp[1], temp[7]); + if (temp[4] < temp[7]) swap(temp[4], temp[7]); + temp[4] <<= 1; + temp[1] <<= 2; + temp[1] += temp[4] + temp[7]; + + //green = ((int(temp[1]) * 160) >> 17) + 4; + green = ((int(temp[1]) * 160) >> 14) + 32; + + //pix = red << redshift; + //pix += green << greenshift; + //pix += blue << blueshift; + + pix = red << (systemRedShift - 3); + pix += green << (systemGreenShift - 3); + pix += blue << (systemBlueShift - 3); + + *buf++ = pix; + } +} + +// for palette mode to work with the three spoony filters in 32bpp depth + +void gbafilter_pad(u8 * buf, int count) +{ + union + { + struct + { + u8 r; + u8 g; + u8 b; + u8 a; + } part; + unsigned whole; + } + mask; + + mask.whole = 0x1f << systemRedShift; + mask.whole += 0x1f << systemGreenShift; + mask.whole += 0x1f << systemBlueShift; + + switch (systemColorDepth) + { + case 24: + while (count--) + { + *buf++ &= mask.part.r; + *buf++ &= mask.part.g; + *buf++ &= mask.part.b; + } + break; + case 32: + while (count--) + { + *((u32*)buf) &= mask.whole; + buf += 4; + } + } +} + +/* +void UpdateSystemColorMaps(int lcd) +{ + switch(systemColorDepth) { + case 16: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal(systemColorMap16, 0x10000); + } + break; + case 24: + case 32: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal32(systemColorMap32, 0x10000); + } + break; + } +} +*/ \ No newline at end of file diff --git a/gbafilter.h b/gbafilter.h new file mode 100644 index 00000000..3c171e07 --- /dev/null +++ b/gbafilter.h @@ -0,0 +1,5 @@ +#include "System.h" + +void gbafilter_pal(u16 * buf, int count); +void gbafilter_pal32(u32 * buf, int count); +void gbafilter_pad(u8 * buf, int count); diff --git a/libpng/libpng.vcproj b/libpng/libpng.vcproj new file mode 100644 index 00000000..4042293f --- /dev/null +++ b/libpng/libpng.vcproj @@ -0,0 +1,226 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libpng/libpng.vcproj.7.10.old b/libpng/libpng.vcproj.7.10.old new file mode 100644 index 00000000..dbce8c2f --- /dev/null +++ b/libpng/libpng.vcproj.7.10.old @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libpng/png.c b/libpng/png.c new file mode 100644 index 00000000..608ea2ce --- /dev/null +++ b/libpng/png.c @@ -0,0 +1,828 @@ + +/* png.c - location for general purpose libpng functions + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#define PNG_NO_EXTERN +#include "png.h" + +/* Generate a compiler error if there is an old png.h in the search path. */ +typedef version_1_2_8 Your_png_h_is_not_version_1_2_8; + +/* Version information for C files. This had better match the version + * string defined in png.h. */ + +#ifdef PNG_USE_GLOBAL_ARRAYS +/* png_libpng_ver was changed to a function in version 1.0.5c */ +const char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING; + +/* png_sig was changed to a function in version 1.0.5c */ +/* Place to hold the signature string for a PNG file. */ +const png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; + +/* Invoke global declarations for constant strings for known chunk types */ +PNG_IHDR; +PNG_IDAT; +PNG_IEND; +PNG_PLTE; +PNG_bKGD; +PNG_cHRM; +PNG_gAMA; +PNG_hIST; +PNG_iCCP; +PNG_iTXt; +PNG_oFFs; +PNG_pCAL; +PNG_sCAL; +PNG_pHYs; +PNG_sBIT; +PNG_sPLT; +PNG_sRGB; +PNG_tEXt; +PNG_tIME; +PNG_tRNS; +PNG_zTXt; + +/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + +/* start of interlace block */ +const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; + +/* offset to next interlace block */ +const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; + +/* start of interlace block in the y direction */ +const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; + +/* offset to next interlace block in the y direction */ +const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; + +/* width of interlace block (used in assembler routines only) */ +#ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW +const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1}; +#endif + +/* Height of interlace block. This is not currently used - if you need + * it, uncomment it here and in png.h +const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; +*/ + +/* Mask to determine which pixels are valid in a pass */ +const int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; + +/* Mask to determine which pixels to overwrite while displaying */ +const int FARDATA png_pass_dsp_mask[] + = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; + +#endif /* PNG_USE_GLOBAL_ARRAYS */ + +/* Tells libpng that we have already handled the first "num_bytes" bytes + * of the PNG file signature. If the PNG data is embedded into another + * stream we can set num_bytes = 8 so that libpng will not attempt to read + * or write any of the magic bytes before it starts on the IHDR. + */ + +void PNGAPI +png_set_sig_bytes(png_structp png_ptr, int num_bytes) +{ + png_debug(1, "in png_set_sig_bytes\n"); + if (num_bytes > 8) + png_error(png_ptr, "Too many bytes for PNG signature."); + + png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); +} + +/* Checks whether the supplied bytes match the PNG signature. We allow + * checking less than the full 8-byte signature so that those apps that + * already read the first few bytes of a file to determine the file type + * can simply check the remaining bytes for extra assurance. Returns + * an integer less than, equal to, or greater than zero if sig is found, + * respectively, to be less than, to match, or be greater than the correct + * PNG signature (this is the same behaviour as strcmp, memcmp, etc). + */ +int PNGAPI +png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check) +{ + png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; + if (num_to_check > 8) + num_to_check = 8; + else if (num_to_check < 1) + return (0); + + if (start > 7) + return (0); + + if (start + num_to_check > 8) + num_to_check = 8 - start; + + return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); +} + +/* (Obsolete) function to check signature bytes. It does not allow one + * to check a partial signature. This function might be removed in the + * future - use png_sig_cmp(). Returns true (nonzero) if the file is a PNG. + */ +int PNGAPI +png_check_sig(png_bytep sig, int num) +{ + return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num)); +} + +/* Function to allocate memory for zlib and clear it to 0. */ +#ifdef PNG_1_0_X +voidpf PNGAPI +#else +voidpf /* private */ +#endif +png_zalloc(voidpf png_ptr, uInt items, uInt size) +{ + png_voidp ptr; + png_structp p=png_ptr; + png_uint_32 save_flags=p->flags; + png_uint_32 num_bytes; + + if (items > PNG_UINT_32_MAX/size) + { + png_warning (png_ptr, "Potential overflow in png_zalloc()"); + return (NULL); + } + num_bytes = (png_uint_32)items * size; + + p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; + ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); + p->flags=save_flags; + +#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO) + if (ptr == NULL) + return ((voidpf)ptr); + + if (num_bytes > (png_uint_32)0x8000L) + { + png_memset(ptr, 0, (png_size_t)0x8000L); + png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0, + (png_size_t)(num_bytes - (png_uint_32)0x8000L)); + } + else + { + png_memset(ptr, 0, (png_size_t)num_bytes); + } +#endif + return ((voidpf)ptr); +} + +/* function to free memory for zlib */ +#ifdef PNG_1_0_X +void PNGAPI +#else +void /* private */ +#endif +png_zfree(voidpf png_ptr, voidpf ptr) +{ + png_free((png_structp)png_ptr, (png_voidp)ptr); +} + +/* Reset the CRC variable to 32 bits of 1's. Care must be taken + * in case CRC is > 32 bits to leave the top bits 0. + */ +void /* PRIVATE */ +png_reset_crc(png_structp png_ptr) +{ + png_ptr->crc = crc32(0, Z_NULL, 0); +} + +/* Calculate the CRC over a section of data. We can only pass as + * much data to this routine as the largest single buffer size. We + * also check that this data will actually be used before going to the + * trouble of calculating it. + */ +void /* PRIVATE */ +png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length) +{ + int need_crc = 1; + + if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ + { + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == + (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) + need_crc = 0; + } + else /* critical */ + { + if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + need_crc = 0; + } + + if (need_crc) + png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length); +} + +/* Allocate the memory for an info_struct for the application. We don't + * really need the png_ptr, but it could potentially be useful in the + * future. This should be used in favour of malloc(png_sizeof(png_info)) + * and png_info_init() so that applications that want to use a shared + * libpng don't have to be recompiled if png_info changes size. + */ +png_infop PNGAPI +png_create_info_struct(png_structp png_ptr) +{ + png_infop info_ptr; + + png_debug(1, "in png_create_info_struct\n"); + if(png_ptr == NULL) return (NULL); +#ifdef PNG_USER_MEM_SUPPORTED + info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, + png_ptr->malloc_fn, png_ptr->mem_ptr); +#else + info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); +#endif + if (info_ptr != NULL) + png_info_init_3(&info_ptr, png_sizeof(png_info)); + + return (info_ptr); +} + +/* This function frees the memory associated with a single info struct. + * Normally, one would use either png_destroy_read_struct() or + * png_destroy_write_struct() to free an info struct, but this may be + * useful for some applications. + */ +void PNGAPI +png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) +{ + png_infop info_ptr = NULL; + + png_debug(1, "in png_destroy_info_struct\n"); + if (info_ptr_ptr != NULL) + info_ptr = *info_ptr_ptr; + + if (info_ptr != NULL) + { + png_info_destroy(png_ptr, info_ptr); + +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn, + png_ptr->mem_ptr); +#else + png_destroy_struct((png_voidp)info_ptr); +#endif + *info_ptr_ptr = NULL; + } +} + +/* Initialize the info structure. This is now an internal function (0.89) + * and applications using it are urged to use png_create_info_struct() + * instead. + */ +#if defined(PNG_1_0_X) || defined (PNG_1_2_X) +#undef png_info_init +void PNGAPI +png_info_init(png_infop info_ptr) +{ + /* We only come here via pre-1.0.12-compiled applications */ + png_info_init_3(&info_ptr, 0); +} +#endif + +void PNGAPI +png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) +{ + png_infop info_ptr = *ptr_ptr; + + png_debug(1, "in png_info_init_3\n"); + + if(png_sizeof(png_info) > png_info_struct_size) + { + png_destroy_struct(info_ptr); + info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); + *ptr_ptr = info_ptr; + } + + /* set everything to 0 */ + png_memset(info_ptr, 0, png_sizeof (png_info)); +} + +#ifdef PNG_FREE_ME_SUPPORTED +void PNGAPI +png_data_freer(png_structp png_ptr, png_infop info_ptr, + int freer, png_uint_32 mask) +{ + png_debug(1, "in png_data_freer\n"); + if (png_ptr == NULL || info_ptr == NULL) + return; + if(freer == PNG_DESTROY_WILL_FREE_DATA) + info_ptr->free_me |= mask; + else if(freer == PNG_USER_WILL_FREE_DATA) + info_ptr->free_me &= ~mask; + else + png_warning(png_ptr, + "Unknown freer parameter in png_data_freer."); +} +#endif + +void PNGAPI +png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, + int num) +{ + png_debug(1, "in png_free_data\n"); + if (png_ptr == NULL || info_ptr == NULL) + return; + +#if defined(PNG_TEXT_SUPPORTED) +/* free text item num or (if num == -1) all text items */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) +#else +if (mask & PNG_FREE_TEXT) +#endif +{ + if (num != -1) + { + if (info_ptr->text && info_ptr->text[num].key) + { + png_free(png_ptr, info_ptr->text[num].key); + info_ptr->text[num].key = NULL; + } + } + else + { + int i; + for (i = 0; i < info_ptr->num_text; i++) + png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); + png_free(png_ptr, info_ptr->text); + info_ptr->text = NULL; + info_ptr->num_text=0; + } +} +#endif + +#if defined(PNG_tRNS_SUPPORTED) +/* free any tRNS entry */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) +#else +if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS)) +#endif +{ + png_free(png_ptr, info_ptr->trans); + info_ptr->valid &= ~PNG_INFO_tRNS; +#ifndef PNG_FREE_ME_SUPPORTED + png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; +#endif + info_ptr->trans = NULL; +} +#endif + +#if defined(PNG_sCAL_SUPPORTED) +/* free any sCAL entry */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) +#else +if (mask & PNG_FREE_SCAL) +#endif +{ +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) + png_free(png_ptr, info_ptr->scal_s_width); + png_free(png_ptr, info_ptr->scal_s_height); + info_ptr->scal_s_width = NULL; + info_ptr->scal_s_height = NULL; +#endif + info_ptr->valid &= ~PNG_INFO_sCAL; +} +#endif + +#if defined(PNG_pCAL_SUPPORTED) +/* free any pCAL entry */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) +#else +if (mask & PNG_FREE_PCAL) +#endif +{ + png_free(png_ptr, info_ptr->pcal_purpose); + png_free(png_ptr, info_ptr->pcal_units); + info_ptr->pcal_purpose = NULL; + info_ptr->pcal_units = NULL; + if (info_ptr->pcal_params != NULL) + { + int i; + for (i = 0; i < (int)info_ptr->pcal_nparams; i++) + { + png_free(png_ptr, info_ptr->pcal_params[i]); + info_ptr->pcal_params[i]=NULL; + } + png_free(png_ptr, info_ptr->pcal_params); + info_ptr->pcal_params = NULL; + } + info_ptr->valid &= ~PNG_INFO_pCAL; +} +#endif + +#if defined(PNG_iCCP_SUPPORTED) +/* free any iCCP entry */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) +#else +if (mask & PNG_FREE_ICCP) +#endif +{ + png_free(png_ptr, info_ptr->iccp_name); + png_free(png_ptr, info_ptr->iccp_profile); + info_ptr->iccp_name = NULL; + info_ptr->iccp_profile = NULL; + info_ptr->valid &= ~PNG_INFO_iCCP; +} +#endif + +#if defined(PNG_sPLT_SUPPORTED) +/* free a given sPLT entry, or (if num == -1) all sPLT entries */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) +#else +if (mask & PNG_FREE_SPLT) +#endif +{ + if (num != -1) + { + if(info_ptr->splt_palettes) + { + png_free(png_ptr, info_ptr->splt_palettes[num].name); + png_free(png_ptr, info_ptr->splt_palettes[num].entries); + info_ptr->splt_palettes[num].name = NULL; + info_ptr->splt_palettes[num].entries = NULL; + } + } + else + { + if(info_ptr->splt_palettes_num) + { + int i; + for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) + png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i); + + png_free(png_ptr, info_ptr->splt_palettes); + info_ptr->splt_palettes = NULL; + info_ptr->splt_palettes_num = 0; + } + info_ptr->valid &= ~PNG_INFO_sPLT; + } +} +#endif + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) +#else +if (mask & PNG_FREE_UNKN) +#endif +{ + if (num != -1) + { + if(info_ptr->unknown_chunks) + { + png_free(png_ptr, info_ptr->unknown_chunks[num].data); + info_ptr->unknown_chunks[num].data = NULL; + } + } + else + { + int i; + + if(info_ptr->unknown_chunks_num) + { + for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++) + png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i); + + png_free(png_ptr, info_ptr->unknown_chunks); + info_ptr->unknown_chunks = NULL; + info_ptr->unknown_chunks_num = 0; + } + } +} +#endif + +#if defined(PNG_hIST_SUPPORTED) +/* free any hIST entry */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_HIST) & info_ptr->free_me) +#else +if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST)) +#endif +{ + png_free(png_ptr, info_ptr->hist); + info_ptr->hist = NULL; + info_ptr->valid &= ~PNG_INFO_hIST; +#ifndef PNG_FREE_ME_SUPPORTED + png_ptr->flags &= ~PNG_FLAG_FREE_HIST; +#endif +} +#endif + +/* free any PLTE entry that was internally allocated */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) +#else +if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE)) +#endif +{ + png_zfree(png_ptr, info_ptr->palette); + info_ptr->palette = NULL; + info_ptr->valid &= ~PNG_INFO_PLTE; +#ifndef PNG_FREE_ME_SUPPORTED + png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; +#endif + info_ptr->num_palette = 0; +} + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +/* free any image bits attached to the info structure */ +#ifdef PNG_FREE_ME_SUPPORTED +if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) +#else +if (mask & PNG_FREE_ROWS) +#endif +{ + if(info_ptr->row_pointers) + { + int row; + for (row = 0; row < (int)info_ptr->height; row++) + { + png_free(png_ptr, info_ptr->row_pointers[row]); + info_ptr->row_pointers[row]=NULL; + } + png_free(png_ptr, info_ptr->row_pointers); + info_ptr->row_pointers=NULL; + } + info_ptr->valid &= ~PNG_INFO_IDAT; +} +#endif + +#ifdef PNG_FREE_ME_SUPPORTED + if(num == -1) + info_ptr->free_me &= ~mask; + else + info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL); +#endif +} + +/* This is an internal routine to free any memory that the info struct is + * pointing to before re-using it or freeing the struct itself. Recall + * that png_free() checks for NULL pointers for us. + */ +void /* PRIVATE */ +png_info_destroy(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_info_destroy\n"); + + png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) + if (png_ptr->num_chunk_list) + { + png_free(png_ptr, png_ptr->chunk_list); + png_ptr->chunk_list=NULL; + png_ptr->num_chunk_list=0; + } +#endif + + png_info_init_3(&info_ptr, png_sizeof(png_info)); +} + +/* This function returns a pointer to the io_ptr associated with the user + * functions. The application should free any memory associated with this + * pointer before png_write_destroy() or png_read_destroy() are called. + */ +png_voidp PNGAPI +png_get_io_ptr(png_structp png_ptr) +{ + return (png_ptr->io_ptr); +} + +#if !defined(PNG_NO_STDIO) +/* Initialize the default input/output functions for the PNG file. If you + * use your own read or write routines, you can call either png_set_read_fn() + * or png_set_write_fn() instead of png_init_io(). If you have defined + * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't + * necessarily available. + */ +void PNGAPI +png_init_io(png_structp png_ptr, png_FILE_p fp) +{ + png_debug(1, "in png_init_io\n"); + png_ptr->io_ptr = (png_voidp)fp; +} +#endif + +#if defined(PNG_TIME_RFC1123_SUPPORTED) +/* Convert the supplied time into an RFC 1123 string suitable for use in + * a "Creation Time" or other text-based time string. + */ +png_charp PNGAPI +png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime) +{ + static PNG_CONST char short_months[12][4] = + {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + if (png_ptr->time_buffer == NULL) + { + png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29* + png_sizeof(char))); + } + +#if defined(_WIN32_WCE) + { + wchar_t time_buf[29]; + wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"), + ptime->day % 32, short_months[(ptime->month - 1) % 12], + ptime->year, ptime->hour % 24, ptime->minute % 60, + ptime->second % 61); + WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29, + NULL, NULL); + } +#else +#ifdef USE_FAR_KEYWORD + { + char near_time_buf[29]; + sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000", + ptime->day % 32, short_months[(ptime->month - 1) % 12], + ptime->year, ptime->hour % 24, ptime->minute % 60, + ptime->second % 61); + png_memcpy(png_ptr->time_buffer, near_time_buf, + 29*png_sizeof(char)); + } +#else + sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000", + ptime->day % 32, short_months[(ptime->month - 1) % 12], + ptime->year, ptime->hour % 24, ptime->minute % 60, + ptime->second % 61); +#endif +#endif /* _WIN32_WCE */ + return ((png_charp)png_ptr->time_buffer); +} +#endif /* PNG_TIME_RFC1123_SUPPORTED */ + +#if 0 +/* Signature string for a PNG file. */ +png_bytep PNGAPI +png_sig_bytes(void) +{ + return ((png_bytep)"\211\120\116\107\015\012\032\012"); +} +#endif + +png_charp PNGAPI +png_get_copyright(png_structp png_ptr) +{ + if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */ + return ((png_charp) "\n libpng version 1.2.8 - December 3, 2004\n\ + Copyright (c) 1998-2004 Glenn Randers-Pehrson\n\ + Copyright (c) 1996-1997 Andreas Dilger\n\ + Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n"); + return ((png_charp) ""); +} + +/* The following return the library version as a short string in the + * format 1.0.0 through 99.99.99zz. To get the version of *.h files + * used with your application, print out PNG_LIBPNG_VER_STRING, which + * is defined in png.h. + * Note: now there is no difference between png_get_libpng_ver() and + * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, + * it is guaranteed that png.c uses the correct version of png.h. + */ +png_charp PNGAPI +png_get_libpng_ver(png_structp png_ptr) +{ + /* Version of *.c files used when building libpng */ + if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */ + return ((png_charp) PNG_LIBPNG_VER_STRING); + return ((png_charp) ""); +} + +png_charp PNGAPI +png_get_header_ver(png_structp png_ptr) +{ + /* Version of *.h files used when building libpng */ + if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */ + return ((png_charp) PNG_LIBPNG_VER_STRING); + return ((png_charp) ""); +} + +png_charp PNGAPI +png_get_header_version(png_structp png_ptr) +{ + /* Returns longer string containing both version and date */ + if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */ + return ((png_charp) PNG_HEADER_VERSION_STRING); + return ((png_charp) ""); +} + +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +int PNGAPI +png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name) +{ + /* check chunk_name and return "keep" value if it's on the list, else 0 */ + int i; + png_bytep p; + if((png_ptr == NULL && chunk_name == NULL) || png_ptr->num_chunk_list<=0) + return 0; + p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5; + for (i = png_ptr->num_chunk_list; i; i--, p-=5) + if (!png_memcmp(chunk_name, p, 4)) + return ((int)*(p+4)); + return 0; +} +#endif + +/* This function, added to libpng-1.0.6g, is untested. */ +int PNGAPI +png_reset_zstream(png_structp png_ptr) +{ + return (inflateReset(&png_ptr->zstream)); +} + +/* This function was added to libpng-1.0.7 */ +png_uint_32 PNGAPI +png_access_version_number(void) +{ + /* Version of *.c files used when building libpng */ + return((png_uint_32) PNG_LIBPNG_VER); +} + + +#if !defined(PNG_1_0_X) +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) + /* GRR: could add this: && defined(PNG_MMX_CODE_SUPPORTED) */ +/* this INTERNAL function was added to libpng 1.2.0 */ +void /* PRIVATE */ +png_init_mmx_flags (png_structp png_ptr) +{ + png_ptr->mmx_rowbytes_threshold = 0; + png_ptr->mmx_bitdepth_threshold = 0; + +# if (defined(PNG_USE_PNGVCRD) || defined(PNG_USE_PNGGCCRD)) + + png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_COMPILED; + + if (png_mmx_support() > 0) { + png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU +# ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW + | PNG_ASM_FLAG_MMX_READ_COMBINE_ROW +# endif +# ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE + | PNG_ASM_FLAG_MMX_READ_INTERLACE +# endif +# ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW + ; +# else + | PNG_ASM_FLAG_MMX_READ_FILTER_SUB + | PNG_ASM_FLAG_MMX_READ_FILTER_UP + | PNG_ASM_FLAG_MMX_READ_FILTER_AVG + | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ; + + png_ptr->mmx_rowbytes_threshold = PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT; + png_ptr->mmx_bitdepth_threshold = PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT; +# endif + } else { + png_ptr->asm_flags &= ~( PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU + | PNG_MMX_READ_FLAGS + | PNG_MMX_WRITE_FLAGS ); + } + +# else /* !((PNGVCRD || PNGGCCRD) && PNG_ASSEMBLER_CODE_SUPPORTED)) */ + + /* clear all MMX flags; no support is compiled in */ + png_ptr->asm_flags &= ~( PNG_MMX_FLAGS ); + +# endif /* ?(PNGVCRD || PNGGCCRD) */ +} + +#endif /* !(PNG_ASSEMBLER_CODE_SUPPORTED) */ + +/* this function was added to libpng 1.2.0 */ +#if !defined(PNG_USE_PNGGCCRD) && \ + !(defined(PNG_ASSEMBLER_CODE_SUPPORTED) && defined(PNG_USE_PNGVCRD)) +int PNGAPI +png_mmx_support(void) +{ + return -1; +} +#endif +#endif /* PNG_1_0_X */ + +#ifdef PNG_SIZE_T +/* Added at libpng version 1.2.6 */ + PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size)); +png_size_t PNGAPI +png_convert_size(size_t size) +{ + if (size > (png_size_t)-1) + PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */ + return ((png_size_t)size); +} +#endif /* PNG_SIZE_T */ diff --git a/libpng/png.h b/libpng/png.h new file mode 100644 index 00000000..e87a3011 --- /dev/null +++ b/libpng/png.h @@ -0,0 +1,3419 @@ +/* png.h - header file for PNG reference library + * + * libpng version 1.2.8 - December 3, 2004 + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * Authors and maintainers: + * libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat + * libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger + * libpng versions 0.97, January 1998, through 1.2.8 - December 3, 2004: Glenn + * See also "Contributing Authors", below. + * + * Note about libpng version numbers: + * + * Due to various miscommunications, unforeseen code incompatibilities + * and occasional factors outside the authors' control, version numbering + * on the library has not always been consistent and straightforward. + * The following table summarizes matters since version 0.89c, which was + * the first widely used release: + * + * source png.h png.h shared-lib + * version string int version + * ------- ------ ----- ---------- + * 0.89c "1.0 beta 3" 0.89 89 1.0.89 + * 0.90 "1.0 beta 4" 0.90 90 0.90 [should have been 2.0.90] + * 0.95 "1.0 beta 5" 0.95 95 0.95 [should have been 2.0.95] + * 0.96 "1.0 beta 6" 0.96 96 0.96 [should have been 2.0.96] + * 0.97b "1.00.97 beta 7" 1.00.97 97 1.0.1 [should have been 2.0.97] + * 0.97c 0.97 97 2.0.97 + * 0.98 0.98 98 2.0.98 + * 0.99 0.99 98 2.0.99 + * 0.99a-m 0.99 99 2.0.99 + * 1.00 1.00 100 2.1.0 [100 should be 10000] + * 1.0.0 (from here on, the 100 2.1.0 [100 should be 10000] + * 1.0.1 png.h string is 10001 2.1.0 + * 1.0.1a-e identical to the 10002 from here on, the shared library + * 1.0.2 source version) 10002 is 2.V where V is the source code + * 1.0.2a-b 10003 version, except as noted. + * 1.0.3 10003 + * 1.0.3a-d 10004 + * 1.0.4 10004 + * 1.0.4a-f 10005 + * 1.0.5 (+ 2 patches) 10005 + * 1.0.5a-d 10006 + * 1.0.5e-r 10100 (not source compatible) + * 1.0.5s-v 10006 (not binary compatible) + * 1.0.6 (+ 3 patches) 10006 (still binary incompatible) + * 1.0.6d-f 10007 (still binary incompatible) + * 1.0.6g 10007 + * 1.0.6h 10007 10.6h (testing xy.z so-numbering) + * 1.0.6i 10007 10.6i + * 1.0.6j 10007 2.1.0.6j (incompatible with 1.0.0) + * 1.0.7beta11-14 DLLNUM 10007 2.1.0.7beta11-14 (binary compatible) + * 1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible) + * 1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible) + * 1.0.7 1 10007 (still compatible) + * 1.0.8beta1-4 1 10008 2.1.0.8beta1-4 + * 1.0.8rc1 1 10008 2.1.0.8rc1 + * 1.0.8 1 10008 2.1.0.8 + * 1.0.9beta1-6 1 10009 2.1.0.9beta1-6 + * 1.0.9rc1 1 10009 2.1.0.9rc1 + * 1.0.9beta7-10 1 10009 2.1.0.9beta7-10 + * 1.0.9rc2 1 10009 2.1.0.9rc2 + * 1.0.9 1 10009 2.1.0.9 + * 1.0.10beta1 1 10010 2.1.0.10beta1 + * 1.0.10rc1 1 10010 2.1.0.10rc1 + * 1.0.10 1 10010 2.1.0.10 + * 1.0.11beta1-3 1 10011 2.1.0.11beta1-3 + * 1.0.11rc1 1 10011 2.1.0.11rc1 + * 1.0.11 1 10011 2.1.0.11 + * 1.0.12beta1-2 2 10012 2.1.0.12beta1-2 + * 1.0.12rc1 2 10012 2.1.0.12rc1 + * 1.0.12 2 10012 2.1.0.12 + * 1.1.0a-f - 10100 2.1.1.0a-f (branch abandoned) + * 1.2.0beta1-2 2 10200 2.1.2.0beta1-2 + * 1.2.0beta3-5 3 10200 3.1.2.0beta3-5 + * 1.2.0rc1 3 10200 3.1.2.0rc1 + * 1.2.0 3 10200 3.1.2.0 + * 1.2.1beta1-4 3 10201 3.1.2.1beta1-4 + * 1.2.1rc1-2 3 10201 3.1.2.1rc1-2 + * 1.2.1 3 10201 3.1.2.1 + * 1.2.2beta1-6 12 10202 12.so.0.1.2.2beta1-6 + * 1.0.13beta1 10 10013 10.so.0.1.0.13beta1 + * 1.0.13rc1 10 10013 10.so.0.1.0.13rc1 + * 1.2.2rc1 12 10202 12.so.0.1.2.2rc1 + * 1.0.13 10 10013 10.so.0.1.0.13 + * 1.2.2 12 10202 12.so.0.1.2.2 + * 1.2.3rc1-6 12 10203 12.so.0.1.2.3rc1-6 + * 1.2.3 12 10203 12.so.0.1.2.3 + * 1.2.4beta1-3 13 10204 12.so.0.1.2.4beta1-3 + * 1.0.14rc1 13 10014 10.so.0.1.0.14rc1 + * 1.2.4rc1 13 10204 12.so.0.1.2.4rc1 + * 1.0.14 10 10014 10.so.0.1.0.14 + * 1.2.4 13 10204 12.so.0.1.2.4 + * 1.2.5beta1-2 13 10205 12.so.0.1.2.5beta1-2 + * 1.0.15rc1-3 10 10015 10.so.0.1.0.15rc1-3 + * 1.2.5rc1-3 13 10205 12.so.0.1.2.5rc1-3 + * 1.0.15 10 10015 10.so.0.1.0.15 + * 1.2.5 13 10205 12.so.0.1.2.5 + * 1.2.6beta1-4 13 10206 12.so.0.1.2.6beta1-4 + * 1.0.16 10 10016 10.so.0.1.0.16 + * 1.2.6 13 10206 12.so.0.1.2.6 + * 1.2.7beta1-2 13 10207 12.so.0.1.2.7beta1-2 + * 1.0.17rc1 10 10017 12.so.0.1.0.17rc1 + * 1.2.7rc1 13 10207 12.so.0.1.2.7rc1 + * 1.0.17 10 10017 12.so.0.1.0.17 + * 1.2.7 13 10207 12.so.0.1.2.7 + * 1.2.8beta1-5 13 10208 12.so.0.1.2.8beta1-5 + * 1.0.18rc1-5 10 10018 12.so.0.1.0.18rc1-5 + * 1.2.8rc1-5 13 10208 12.so.0.1.2.8rc1-5 + * 1.0.18 10 10018 12.so.0.1.0.18 + * 1.2.8 13 10208 12.so.0.1.2.8 + * + * Henceforth the source version will match the shared-library major + * and minor numbers; the shared-library major version number will be + * used for changes in backward compatibility, as it is intended. The + * PNG_LIBPNG_VER macro, which is not used within libpng but is available + * for applications, is an unsigned integer of the form xyyzz corresponding + * to the source version x.y.z (leading zeros in y and z). Beta versions + * were given the previous public release number plus a letter, until + * version 1.0.6j; from then on they were given the upcoming public + * release number plus "betaNN" or "rcN". + * + * Binary incompatibility exists only when applications make direct access + * to the info_ptr or png_ptr members through png.h, and the compiled + * application is loaded with a different version of the library. + * + * DLLNUM will change each time there are forward or backward changes + * in binary compatibility (e.g., when a new feature is added). + * + * See libpng.txt or libpng.3 for more information. The PNG specification + * is available as a W3C Recommendation and as an ISO Specification, + * defines should NOT be changed. + */ +#define PNG_INFO_gAMA 0x0001 +#define PNG_INFO_sBIT 0x0002 +#define PNG_INFO_cHRM 0x0004 +#define PNG_INFO_PLTE 0x0008 +#define PNG_INFO_tRNS 0x0010 +#define PNG_INFO_bKGD 0x0020 +#define PNG_INFO_hIST 0x0040 +#define PNG_INFO_pHYs 0x0080 +#define PNG_INFO_oFFs 0x0100 +#define PNG_INFO_tIME 0x0200 +#define PNG_INFO_pCAL 0x0400 +#define PNG_INFO_sRGB 0x0800 /* GR-P, 0.96a */ +#define PNG_INFO_iCCP 0x1000 /* ESR, 1.0.6 */ +#define PNG_INFO_sPLT 0x2000 /* ESR, 1.0.6 */ +#define PNG_INFO_sCAL 0x4000 /* ESR, 1.0.6 */ +#define PNG_INFO_IDAT 0x8000L /* ESR, 1.0.6 */ + +/* This is used for the transformation routines, as some of them + * change these values for the row. It also should enable using + * the routines for other purposes. + */ +typedef struct png_row_info_struct +{ + png_uint_32 width; /* width of row */ + png_uint_32 rowbytes; /* number of bytes in row */ + png_byte color_type; /* color type of row */ + png_byte bit_depth; /* bit depth of row */ + png_byte channels; /* number of channels (1, 2, 3, or 4) */ + png_byte pixel_depth; /* bits per pixel (depth * channels) */ +} png_row_info; + +typedef png_row_info FAR * png_row_infop; +typedef png_row_info FAR * FAR * png_row_infopp; + +/* These are the function types for the I/O functions and for the functions + * that allow the user to override the default I/O functions with his or her + * own. The png_error_ptr type should match that of user-supplied warning + * and error functions, while the png_rw_ptr type should match that of the + * user read/write data functions. + */ +typedef struct png_struct_def png_struct; +typedef png_struct FAR * png_structp; + +typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp)); +typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t)); +typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp)); +typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32, + int)); +typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32, + int)); + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop)); +typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop)); +typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep, + png_uint_32, int)); +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp, + png_row_infop, png_bytep)); +#endif + +#if defined(PNG_USER_CHUNKS_SUPPORTED) +typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp)); +#endif +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp)); +#endif + +/* Transform masks for the high-level interface */ +#define PNG_TRANSFORM_IDENTITY 0x0000 /* read and write */ +#define PNG_TRANSFORM_STRIP_16 0x0001 /* read only */ +#define PNG_TRANSFORM_STRIP_ALPHA 0x0002 /* read only */ +#define PNG_TRANSFORM_PACKING 0x0004 /* read and write */ +#define PNG_TRANSFORM_PACKSWAP 0x0008 /* read and write */ +#define PNG_TRANSFORM_EXPAND 0x0010 /* read only */ +#define PNG_TRANSFORM_INVERT_MONO 0x0020 /* read and write */ +#define PNG_TRANSFORM_SHIFT 0x0040 /* read and write */ +#define PNG_TRANSFORM_BGR 0x0080 /* read and write */ +#define PNG_TRANSFORM_SWAP_ALPHA 0x0100 /* read and write */ +#define PNG_TRANSFORM_SWAP_ENDIAN 0x0200 /* read and write */ +#define PNG_TRANSFORM_INVERT_ALPHA 0x0400 /* read and write */ +#define PNG_TRANSFORM_STRIP_FILLER 0x0800 /* WRITE only */ + +/* Flags for MNG supported features */ +#define PNG_FLAG_MNG_EMPTY_PLTE 0x01 +#define PNG_FLAG_MNG_FILTER_64 0x04 +#define PNG_ALL_MNG_FEATURES 0x05 + +typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t)); +typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp)); + +/* The structure that holds the information to read and write PNG files. + * The only people who need to care about what is inside of this are the + * people who will be modifying the library for their own special needs. + * It should NOT be accessed directly by an application, except to store + * the jmp_buf. + */ + +struct png_struct_def +{ +#ifdef PNG_SETJMP_SUPPORTED + jmp_buf jmpbuf; /* used in png_error */ +#endif + png_error_ptr error_fn; /* function for printing errors and aborting */ + png_error_ptr warning_fn; /* function for printing warnings */ + png_voidp error_ptr; /* user supplied struct for error functions */ + png_rw_ptr write_data_fn; /* function for writing output data */ + png_rw_ptr read_data_fn; /* function for reading input data */ + png_voidp io_ptr; /* ptr to application struct for I/O functions */ + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) + png_user_transform_ptr read_user_transform_fn; /* user read transform */ +#endif + +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) + png_user_transform_ptr write_user_transform_fn; /* user write transform */ +#endif + +/* These were added in libpng-1.0.2 */ +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) + png_voidp user_transform_ptr; /* user supplied struct for user transform */ + png_byte user_transform_depth; /* bit depth of user transformed pixels */ + png_byte user_transform_channels; /* channels in user transformed pixels */ +#endif +#endif + + png_uint_32 mode; /* tells us where we are in the PNG file */ + png_uint_32 flags; /* flags indicating various things to libpng */ + png_uint_32 transformations; /* which transformations to perform */ + + z_stream zstream; /* pointer to decompression structure (below) */ + png_bytep zbuf; /* buffer for zlib */ + png_size_t zbuf_size; /* size of zbuf */ + int zlib_level; /* holds zlib compression level */ + int zlib_method; /* holds zlib compression method */ + int zlib_window_bits; /* holds zlib compression window bits */ + int zlib_mem_level; /* holds zlib compression memory level */ + int zlib_strategy; /* holds zlib compression strategy */ + + png_uint_32 width; /* width of image in pixels */ + png_uint_32 height; /* height of image in pixels */ + png_uint_32 num_rows; /* number of rows in current pass */ + png_uint_32 usr_width; /* width of row at start of write */ + png_uint_32 rowbytes; /* size of row in bytes */ + png_uint_32 irowbytes; /* size of current interlaced row in bytes */ + png_uint_32 iwidth; /* width of current interlaced row in pixels */ + png_uint_32 row_number; /* current row in interlace pass */ + png_bytep prev_row; /* buffer to save previous (unfiltered) row */ + png_bytep row_buf; /* buffer to save current (unfiltered) row */ + png_bytep sub_row; /* buffer to save "sub" row when filtering */ + png_bytep up_row; /* buffer to save "up" row when filtering */ + png_bytep avg_row; /* buffer to save "avg" row when filtering */ + png_bytep paeth_row; /* buffer to save "Paeth" row when filtering */ + png_row_info row_info; /* used for transformation routines */ + + png_uint_32 idat_size; /* current IDAT size for read */ + png_uint_32 crc; /* current chunk CRC value */ + png_colorp palette; /* palette from the input file */ + png_uint_16 num_palette; /* number of color entries in palette */ + png_uint_16 num_trans; /* number of transparency values */ + png_byte chunk_name[5]; /* null-terminated name of current chunk */ + png_byte compression; /* file compression type (always 0) */ + png_byte filter; /* file filter type (always 0) */ + png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ + png_byte pass; /* current interlace pass (0 - 6) */ + png_byte do_filter; /* row filter flags (see PNG_FILTER_ below ) */ + png_byte color_type; /* color type of file */ + png_byte bit_depth; /* bit depth of file */ + png_byte usr_bit_depth; /* bit depth of users row */ + png_byte pixel_depth; /* number of bits per pixel */ + png_byte channels; /* number of channels in file */ + png_byte usr_channels; /* channels at start of write */ + png_byte sig_bytes; /* magic bytes read/written from start of file */ + +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) +#ifdef PNG_LEGACY_SUPPORTED + png_byte filler; /* filler byte for pixel expansion */ +#else + png_uint_16 filler; /* filler bytes for pixel expansion */ +#endif +#endif + +#if defined(PNG_bKGD_SUPPORTED) + png_byte background_gamma_type; +# ifdef PNG_FLOATING_POINT_SUPPORTED + float background_gamma; +# endif + png_color_16 background; /* background color in screen gamma space */ +#if defined(PNG_READ_GAMMA_SUPPORTED) + png_color_16 background_1; /* background normalized to gamma 1.0 */ +#endif +#endif /* PNG_bKGD_SUPPORTED */ + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) + png_flush_ptr output_flush_fn;/* Function for flushing output */ + png_uint_32 flush_dist; /* how many rows apart to flush, 0 - no flush */ + png_uint_32 flush_rows; /* number of rows written since last flush */ +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + int gamma_shift; /* number of "insignificant" bits 16-bit gamma */ +#ifdef PNG_FLOATING_POINT_SUPPORTED + float gamma; /* file gamma value */ + float screen_gamma; /* screen gamma value (display_exponent) */ +#endif +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + png_bytep gamma_table; /* gamma table for 8-bit depth files */ + png_bytep gamma_from_1; /* converts from 1.0 to screen */ + png_bytep gamma_to_1; /* converts from file to 1.0 */ + png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */ + png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */ + png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */ +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED) + png_color_8 sig_bit; /* significant bits in each available channel */ +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) + png_color_8 shift; /* shift for significant bit tranformation */ +#endif + +#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \ + || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + png_bytep trans; /* transparency values for paletted files */ + png_color_16 trans_values; /* transparency values for non-paletted files */ +#endif + + png_read_status_ptr read_row_fn; /* called after each row is decoded */ + png_write_status_ptr write_row_fn; /* called after each row is encoded */ +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED + png_progressive_info_ptr info_fn; /* called after header data fully read */ + png_progressive_row_ptr row_fn; /* called after each prog. row is decoded */ + png_progressive_end_ptr end_fn; /* called after image is complete */ + png_bytep save_buffer_ptr; /* current location in save_buffer */ + png_bytep save_buffer; /* buffer for previously read data */ + png_bytep current_buffer_ptr; /* current location in current_buffer */ + png_bytep current_buffer; /* buffer for recently used data */ + png_uint_32 push_length; /* size of current input chunk */ + png_uint_32 skip_length; /* bytes to skip in input data */ + png_size_t save_buffer_size; /* amount of data now in save_buffer */ + png_size_t save_buffer_max; /* total size of save_buffer */ + png_size_t buffer_size; /* total amount of available input data */ + png_size_t current_buffer_size; /* amount of data now in current_buffer */ + int process_mode; /* what push library is currently doing */ + int cur_palette; /* current push library palette index */ + +# if defined(PNG_TEXT_SUPPORTED) + png_size_t current_text_size; /* current size of text input data */ + png_size_t current_text_left; /* how much text left to read in input */ + png_charp current_text; /* current text chunk buffer */ + png_charp current_text_ptr; /* current location in current_text */ +# endif /* PNG_PROGRESSIVE_READ_SUPPORTED && PNG_TEXT_SUPPORTED */ + +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ + +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) +/* for the Borland special 64K segment handler */ + png_bytepp offset_table_ptr; + png_bytep offset_table; + png_uint_16 offset_table_number; + png_uint_16 offset_table_count; + png_uint_16 offset_table_count_free; +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) + png_bytep palette_lookup; /* lookup table for dithering */ + png_bytep dither_index; /* index translation for palette files */ +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED) + png_uint_16p hist; /* histogram */ +#endif + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + png_byte heuristic_method; /* heuristic for row filter selection */ + png_byte num_prev_filters; /* number of weights for previous rows */ + png_bytep prev_filters; /* filter type(s) of previous row(s) */ + png_uint_16p filter_weights; /* weight(s) for previous line(s) */ + png_uint_16p inv_filter_weights; /* 1/weight(s) for previous line(s) */ + png_uint_16p filter_costs; /* relative filter calculation cost */ + png_uint_16p inv_filter_costs; /* 1/relative filter calculation cost */ +#endif + +#if defined(PNG_TIME_RFC1123_SUPPORTED) + png_charp time_buffer; /* String to hold RFC 1123 time text */ +#endif + +/* New members added in libpng-1.0.6 */ + +#ifdef PNG_FREE_ME_SUPPORTED + png_uint_32 free_me; /* flags items libpng is responsible for freeing */ +#endif + +#if defined(PNG_USER_CHUNKS_SUPPORTED) + png_voidp user_chunk_ptr; + png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */ +#endif + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) + int num_chunk_list; + png_bytep chunk_list; +#endif + +/* New members added in libpng-1.0.3 */ +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) + png_byte rgb_to_gray_status; + /* These were changed from png_byte in libpng-1.0.6 */ + png_uint_16 rgb_to_gray_red_coeff; + png_uint_16 rgb_to_gray_green_coeff; + png_uint_16 rgb_to_gray_blue_coeff; +#endif + +/* New member added in libpng-1.0.4 (renamed in 1.0.9) */ +#if defined(PNG_MNG_FEATURES_SUPPORTED) || \ + defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ + defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) +/* changed from png_byte to png_uint_32 at version 1.2.0 */ +#ifdef PNG_1_0_X + png_byte mng_features_permitted; +#else + png_uint_32 mng_features_permitted; +#endif /* PNG_1_0_X */ +#endif + +/* New member added in libpng-1.0.7 */ +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + png_fixed_point int_gamma; +#endif + +/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */ +#if defined(PNG_MNG_FEATURES_SUPPORTED) + png_byte filter_type; +#endif + +#if defined(PNG_1_0_X) || (defined(PNG_DEBUG) && defined(PNG_USE_PNGGCCRD)) +/* New member added in libpng-1.0.10, ifdef'ed out in 1.2.0 */ + png_uint_32 row_buf_size; +#endif + +/* New members added in libpng-1.2.0 */ +#if !defined(PNG_1_0_X) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) + png_byte mmx_bitdepth_threshold; + png_uint_32 mmx_rowbytes_threshold; + png_uint_32 asm_flags; +#endif + +/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */ +#ifdef PNG_USER_MEM_SUPPORTED + png_voidp mem_ptr; /* user supplied struct for mem functions */ + png_malloc_ptr malloc_fn; /* function for allocating memory */ + png_free_ptr free_fn; /* function for freeing memory */ +#endif + +/* New member added in libpng-1.0.13 and 1.2.0 */ + png_bytep big_row_buf; /* buffer to save current (unfiltered) row */ + +#if defined(PNG_READ_DITHER_SUPPORTED) +/* The following three members were added at version 1.0.14 and 1.2.4 */ + png_bytep dither_sort; /* working sort array */ + png_bytep index_to_palette; /* where the original index currently is */ + /* in the palette */ + png_bytep palette_to_index; /* which original index points to this */ + /* palette color */ +#endif + +/* New members added in libpng-1.0.16 and 1.2.6 */ + png_byte compression_type; + +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + png_uint_32 user_width_max; + png_uint_32 user_height_max; +#endif + +}; + + +/* This triggers a compiler error in png.c, if png.c and png.h + * do not agree upon the version number. + */ +typedef png_structp version_1_2_8; + +typedef png_struct FAR * FAR * png_structpp; + +/* Here are the function definitions most commonly used. This is not + * the place to find out how to use libpng. See libpng.txt for the + * full explanation, see example.c for the summary. This just provides + * a simple one line description of the use of each function. + */ + +/* Returns the version number of the library */ +extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void)); + +/* Tell lib we have already handled the first magic bytes. + * Handling more than 8 bytes from the beginning of the file is an error. + */ +extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr, + int num_bytes)); + +/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a + * PNG file. Returns zero if the supplied bytes match the 8-byte PNG + * signature, and non-zero otherwise. Having num_to_check == 0 or + * start > 7 will always fail (ie return non-zero). + */ +extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start, + png_size_t num_to_check)); + +/* Simple signature checking function. This is the same as calling + * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n). + */ +extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num)); + +/* Allocate and initialize png_ptr struct for reading, and any other memory. */ +extern PNG_EXPORT(png_structp,png_create_read_struct) + PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn)); + +/* Allocate and initialize png_ptr struct for writing, and any other memory */ +extern PNG_EXPORT(png_structp,png_create_write_struct) + PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn)); + +#ifdef PNG_WRITE_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size) + PNGARG((png_structp png_ptr)); +#endif + +#ifdef PNG_WRITE_SUPPORTED +extern PNG_EXPORT(void,png_set_compression_buffer_size) + PNGARG((png_structp png_ptr, png_uint_32 size)); +#endif + +/* Reset the compression stream */ +extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr)); + +/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */ +#ifdef PNG_USER_MEM_SUPPORTED +extern PNG_EXPORT(png_structp,png_create_read_struct_2) + PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, + png_malloc_ptr malloc_fn, png_free_ptr free_fn)); +extern PNG_EXPORT(png_structp,png_create_write_struct_2) + PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, + png_malloc_ptr malloc_fn, png_free_ptr free_fn)); +#endif + +/* Write a PNG chunk - size, type, (optional) data, CRC. */ +extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr, + png_bytep chunk_name, png_bytep data, png_size_t length)); + +/* Write the start of a PNG chunk - length and chunk name. */ +extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr, + png_bytep chunk_name, png_uint_32 length)); + +/* Write the data of a PNG chunk started with png_write_chunk_start(). */ +extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr, + png_bytep data, png_size_t length)); + +/* Finish a chunk started with png_write_chunk_start() (includes CRC). */ +extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr)); + +/* Allocate and initialize the info structure */ +extern PNG_EXPORT(png_infop,png_create_info_struct) + PNGARG((png_structp png_ptr)); + +/* Initialize the info structure (old interface - DEPRECATED) */ +extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr)); +#undef png_info_init +#define png_info_init(info_ptr) png_info_init_3(&info_ptr,\ + png_sizeof(png_info)); +extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr, + png_size_t png_info_struct_size)); + +/* Writes all the PNG information before the image. */ +extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr, + png_infop info_ptr)); +extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr, + png_infop info_ptr)); + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* read the information before the actual image data. */ +extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr, + png_infop info_ptr)); +#endif + +#if defined(PNG_TIME_RFC1123_SUPPORTED) +extern PNG_EXPORT(png_charp,png_convert_to_rfc1123) + PNGARG((png_structp png_ptr, png_timep ptime)); +#endif + +#if !defined(_WIN32_WCE) +/* "time.h" functions are not supported on WindowsCE */ +#if defined(PNG_WRITE_tIME_SUPPORTED) +/* convert from a struct tm to png_time */ +extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime, + struct tm FAR * ttime)); + +/* convert from time_t to png_time. Uses gmtime() */ +extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime, + time_t ttime)); +#endif /* PNG_WRITE_tIME_SUPPORTED */ +#endif /* _WIN32_WCE */ + +#if defined(PNG_READ_EXPAND_SUPPORTED) +/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */ +extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) +/* Use blue, green, red order for pixels. */ +extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +/* Expand the grayscale to 24-bit RGB if necessary. */ +extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +/* Reduce RGB to grayscale. */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr, + int error_action, double red, double green )); +#endif +extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr, + int error_action, png_fixed_point red, png_fixed_point green )); +extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp + png_ptr)); +#endif + +extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth, + png_colorp palette)); + +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) +extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ + defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) +extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ + defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) +extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) +/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */ +extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr, + png_uint_32 filler, int flags)); +/* The values of the PNG_FILLER_ defines should NOT be changed */ +#define PNG_FILLER_BEFORE 0 +#define PNG_FILLER_AFTER 1 +/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */ +#if !defined(PNG_1_0_X) +extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr, + png_uint_32 filler, int flags)); +#endif +#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */ + +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) +/* Swap bytes in 16-bit depth files. */ +extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) +/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */ +extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED) +/* Swap packing order of pixels in bytes. */ +extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) +/* Converts files to legal bit depths. */ +extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr, + png_color_8p true_bits)); +#endif + +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \ + defined(PNG_WRITE_INTERLACING_SUPPORTED) +/* Have the code handle the interlacing. Returns the number of passes. */ +extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) +/* Invert monochrome files */ +extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) +/* Handle alpha and tRNS by replacing with a background color. */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr, + png_color_16p background_color, int background_gamma_code, + int need_expand, double background_gamma)); +#endif +#define PNG_BACKGROUND_GAMMA_UNKNOWN 0 +#define PNG_BACKGROUND_GAMMA_SCREEN 1 +#define PNG_BACKGROUND_GAMMA_FILE 2 +#define PNG_BACKGROUND_GAMMA_UNIQUE 3 +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) +/* strip the second byte of information from a 16-bit depth file. */ +extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) +/* Turn on dithering, and reduce the palette to the number of colors available. */ +extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr, + png_colorp palette, int num_palette, int maximum_colors, + png_uint_16p histogram, int full_dither)); +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) +/* Handle gamma correction. Screen_gamma=(display_exponent) */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr, + double screen_gamma, double default_file_gamma)); +#endif +#endif + +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ + defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) +/* Permit or disallow empty PLTE (0: not permitted, 1: permitted) */ +/* Deprecated and will be removed. Use png_permit_mng_features() instead. */ +extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr, + int empty_plte_permitted)); +#endif + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +/* Set how many lines between output flushes - 0 for no flushing */ +extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows)); +/* Flush the current PNG output buffer */ +extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr)); +#endif + +/* optional update palette with requested transformations */ +extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr)); + +/* optional call to update the users info structure */ +extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr, + png_infop info_ptr)); + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* read one or more rows of image data. */ +extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr, + png_bytepp row, png_bytepp display_row, png_uint_32 num_rows)); +#endif + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* read a row of data. */ +extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr, + png_bytep row, + png_bytep display_row)); +#endif + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* read the whole image into memory at once. */ +extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr, + png_bytepp image)); +#endif + +/* write a row of image data */ +extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr, + png_bytep row)); + +/* write a few rows of image data */ +extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr, + png_bytepp row, png_uint_32 num_rows)); + +/* write the image data */ +extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr, + png_bytepp image)); + +/* writes the end of the PNG file. */ +extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr, + png_infop info_ptr)); + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* read the end of the PNG file. */ +extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr, + png_infop info_ptr)); +#endif + +/* free any memory associated with the png_info_struct */ +extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr, + png_infopp info_ptr_ptr)); + +/* free any memory associated with the png_struct and the png_info_structs */ +extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp + png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)); + +/* free all memory used by the read (old method - NOT DLL EXPORTED) */ +extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr, + png_infop end_info_ptr)); + +/* free any memory associated with the png_struct and the png_info_structs */ +extern PNG_EXPORT(void,png_destroy_write_struct) + PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)); + +/* free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */ +extern void png_write_destroy PNGARG((png_structp png_ptr)); + +/* set the libpng method of handling chunk CRC errors */ +extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr, + int crit_action, int ancil_action)); + +/* Values for png_set_crc_action() to say how to handle CRC errors in + * ancillary and critical chunks, and whether to use the data contained + * therein. Note that it is impossible to "discard" data in a critical + * chunk. For versions prior to 0.90, the action was always error/quit, + * whereas in version 0.90 and later, the action for CRC errors in ancillary + * chunks is warn/discard. These values should NOT be changed. + * + * value action:critical action:ancillary + */ +#define PNG_CRC_DEFAULT 0 /* error/quit warn/discard data */ +#define PNG_CRC_ERROR_QUIT 1 /* error/quit error/quit */ +#define PNG_CRC_WARN_DISCARD 2 /* (INVALID) warn/discard data */ +#define PNG_CRC_WARN_USE 3 /* warn/use data warn/use data */ +#define PNG_CRC_QUIET_USE 4 /* quiet/use data quiet/use data */ +#define PNG_CRC_NO_CHANGE 5 /* use current value use current value */ + +/* These functions give the user control over the scan-line filtering in + * libpng and the compression methods used by zlib. These functions are + * mainly useful for testing, as the defaults should work with most users. + * Those users who are tight on memory or want faster performance at the + * expense of compression can modify them. See the compression library + * header file (zlib.h) for an explination of the compression functions. + */ + +/* set the filtering method(s) used by libpng. Currently, the only valid + * value for "method" is 0. + */ +extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method, + int filters)); + +/* Flags for png_set_filter() to say which filters to use. The flags + * are chosen so that they don't conflict with real filter types + * below, in case they are supplied instead of the #defined constants. + * These values should NOT be changed. + */ +#define PNG_NO_FILTERS 0x00 +#define PNG_FILTER_NONE 0x08 +#define PNG_FILTER_SUB 0x10 +#define PNG_FILTER_UP 0x20 +#define PNG_FILTER_AVG 0x40 +#define PNG_FILTER_PAETH 0x80 +#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \ + PNG_FILTER_AVG | PNG_FILTER_PAETH) + +/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now. + * These defines should NOT be changed. + */ +#define PNG_FILTER_VALUE_NONE 0 +#define PNG_FILTER_VALUE_SUB 1 +#define PNG_FILTER_VALUE_UP 2 +#define PNG_FILTER_VALUE_AVG 3 +#define PNG_FILTER_VALUE_PAETH 4 +#define PNG_FILTER_VALUE_LAST 5 + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */ +/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_ + * defines, either the default (minimum-sum-of-absolute-differences), or + * the experimental method (weighted-minimum-sum-of-absolute-differences). + * + * Weights are factors >= 1.0, indicating how important it is to keep the + * filter type consistent between rows. Larger numbers mean the current + * filter is that many times as likely to be the same as the "num_weights" + * previous filters. This is cumulative for each previous row with a weight. + * There needs to be "num_weights" values in "filter_weights", or it can be + * NULL if the weights aren't being specified. Weights have no influence on + * the selection of the first row filter. Well chosen weights can (in theory) + * improve the compression for a given image. + * + * Costs are factors >= 1.0 indicating the relative decoding costs of a + * filter type. Higher costs indicate more decoding expense, and are + * therefore less likely to be selected over a filter with lower computational + * costs. There needs to be a value in "filter_costs" for each valid filter + * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't + * setting the costs. Costs try to improve the speed of decompression without + * unduly increasing the compressed image size. + * + * A negative weight or cost indicates the default value is to be used, and + * values in the range [0.0, 1.0) indicate the value is to remain unchanged. + * The default values for both weights and costs are currently 1.0, but may + * change if good general weighting/cost heuristics can be found. If both + * the weights and costs are set to 1.0, this degenerates the WEIGHTED method + * to the UNWEIGHTED method, but with added encoding time/computation. + */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr, + int heuristic_method, int num_weights, png_doublep filter_weights, + png_doublep filter_costs)); +#endif +#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ + +/* Heuristic used for row filter selection. These defines should NOT be + * changed. + */ +#define PNG_FILTER_HEURISTIC_DEFAULT 0 /* Currently "UNWEIGHTED" */ +#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1 /* Used by libpng < 0.95 */ +#define PNG_FILTER_HEURISTIC_WEIGHTED 2 /* Experimental feature */ +#define PNG_FILTER_HEURISTIC_LAST 3 /* Not a valid value */ + +/* Set the library compression level. Currently, valid values range from + * 0 - 9, corresponding directly to the zlib compression levels 0 - 9 + * (0 - no compression, 9 - "maximal" compression). Note that tests have + * shown that zlib compression levels 3-6 usually perform as well as level 9 + * for PNG images, and do considerably fewer caclulations. In the future, + * these values may not correspond directly to the zlib compression levels. + */ +extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr, + int level)); + +extern PNG_EXPORT(void,png_set_compression_mem_level) + PNGARG((png_structp png_ptr, int mem_level)); + +extern PNG_EXPORT(void,png_set_compression_strategy) + PNGARG((png_structp png_ptr, int strategy)); + +extern PNG_EXPORT(void,png_set_compression_window_bits) + PNGARG((png_structp png_ptr, int window_bits)); + +extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr, + int method)); + +/* These next functions are called for input/output, memory, and error + * handling. They are in the file pngrio.c, pngwio.c, and pngerror.c, + * and call standard C I/O routines such as fread(), fwrite(), and + * fprintf(). These functions can be made to use other I/O routines + * at run time for those applications that need to handle I/O in a + * different manner by calling png_set_???_fn(). See libpng.txt for + * more information. + */ + +#if !defined(PNG_NO_STDIO) +/* Initialize the input/output for the PNG file to the default functions. */ +extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp)); +#endif + +/* Replace the (error and abort), and warning functions with user + * supplied functions. If no messages are to be printed you must still + * write and use replacement functions. The replacement error_fn should + * still do a longjmp to the last setjmp location if you are using this + * method of error handling. If error_fn or warning_fn is NULL, the + * default function will be used. + */ + +extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr, + png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn)); + +/* Return the user pointer associated with the error functions */ +extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr)); + +/* Replace the default data output functions with a user supplied one(s). + * If buffered output is not used, then output_flush_fn can be set to NULL. + * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time + * output_flush_fn will be ignored (and thus can be NULL). + */ +extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr, + png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)); + +/* Replace the default data input function with a user supplied one. */ +extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr, + png_voidp io_ptr, png_rw_ptr read_data_fn)); + +/* Return the user pointer associated with the I/O functions */ +extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr)); + +extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr, + png_read_status_ptr read_row_fn)); + +extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr, + png_write_status_ptr write_row_fn)); + +#ifdef PNG_USER_MEM_SUPPORTED +/* Replace the default memory allocation functions with user supplied one(s). */ +extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr, + png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)); +/* Return the user pointer associated with the memory functions */ +extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr)); +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp + png_ptr, png_user_transform_ptr read_user_transform_fn)); +#endif + +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp + png_ptr, png_user_transform_ptr write_user_transform_fn)); +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp + png_ptr, png_voidp user_transform_ptr, int user_transform_depth, + int user_transform_channels)); +/* Return the user pointer associated with the user transform functions */ +extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr) + PNGARG((png_structp png_ptr)); +#endif + +#ifdef PNG_USER_CHUNKS_SUPPORTED +extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr, + png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn)); +extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp + png_ptr)); +#endif + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +/* Sets the function callbacks for the push reader, and a pointer to a + * user-defined structure available to the callback functions. + */ +extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr, + png_voidp progressive_ptr, + png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, + png_progressive_end_ptr end_fn)); + +/* returns the user pointer associated with the push read functions */ +extern PNG_EXPORT(png_voidp,png_get_progressive_ptr) + PNGARG((png_structp png_ptr)); + +/* function to be called when data becomes available */ +extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_bytep buffer, png_size_t buffer_size)); + +/* function that combines rows. Not very much different than the + * png_combine_row() call. Is this even used????? + */ +extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr, + png_bytep old_row, png_bytep new_row)); +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ + +extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr, + png_uint_32 size)); + +#if defined(PNG_1_0_X) +# define png_malloc_warn png_malloc +#else +/* Added at libpng version 1.2.4 */ +extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr, + png_uint_32 size)); +#endif + +/* frees a pointer allocated by png_malloc() */ +extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr)); + +#if defined(PNG_1_0_X) +/* Function to allocate memory for zlib. */ +extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items, + uInt size)); + +/* Function to free memory for zlib */ +extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr)); +#endif + +/* Free data that was allocated internally */ +extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 free_me, int num)); +#ifdef PNG_FREE_ME_SUPPORTED +/* Reassign responsibility for freeing existing data, whether allocated + * by libpng or by the application */ +extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr, + png_infop info_ptr, int freer, png_uint_32 mask)); +#endif +/* assignments for png_data_freer */ +#define PNG_DESTROY_WILL_FREE_DATA 1 +#define PNG_SET_WILL_FREE_DATA 1 +#define PNG_USER_WILL_FREE_DATA 2 +/* Flags for png_ptr->free_me and info_ptr->free_me */ +#define PNG_FREE_HIST 0x0008 +#define PNG_FREE_ICCP 0x0010 +#define PNG_FREE_SPLT 0x0020 +#define PNG_FREE_ROWS 0x0040 +#define PNG_FREE_PCAL 0x0080 +#define PNG_FREE_SCAL 0x0100 +#define PNG_FREE_UNKN 0x0200 +#define PNG_FREE_LIST 0x0400 +#define PNG_FREE_PLTE 0x1000 +#define PNG_FREE_TRNS 0x2000 +#define PNG_FREE_TEXT 0x4000 +#define PNG_FREE_ALL 0x7fff +#define PNG_FREE_MUL 0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */ + +#ifdef PNG_USER_MEM_SUPPORTED +extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr, + png_uint_32 size)); +extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr, + png_voidp ptr)); +#endif + +extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr, + png_voidp s1, png_voidp s2, png_uint_32 size)); + +extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr, + png_voidp s1, int value, png_uint_32 size)); + +#if defined(USE_FAR_KEYWORD) /* memory model conversion function */ +extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr, + int check)); +#endif /* USE_FAR_KEYWORD */ + +/* Fatal error in PNG image of libpng - can't continue */ +extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr, + png_const_charp error_message)); + +/* The same, but the chunk name is prepended to the error string. */ +extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr, + png_const_charp error_message)); + +/* Non-fatal error in libpng. Can continue, but may have a problem. */ +extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr, + png_const_charp warning_message)); + +/* Non-fatal error in libpng, chunk name is prepended to message. */ +extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr, + png_const_charp warning_message)); + +/* The png_set_ functions are for storing values in the png_info_struct. + * Similarly, the png_get_ calls are used to read values from the + * png_info_struct, either storing the parameters in the passed variables, or + * setting pointers into the png_info_struct where the data is stored. The + * png_get_ functions return a non-zero value if the data was available + * in info_ptr, or return zero and do not change any of the parameters if the + * data was not available. + * + * These functions should be used instead of directly accessing png_info + * to avoid problems with future changes in the size and internal layout of + * png_info_struct. + */ +/* Returns "flag" if chunk data is valid in info_ptr. */ +extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr, +png_infop info_ptr, png_uint_32 flag)); + +/* Returns number of bytes needed to hold a transformed row. */ +extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr, +png_infop info_ptr)); + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +/* Returns row_pointers, which is an array of pointers to scanlines that was +returned from png_read_png(). */ +extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr, +png_infop info_ptr)); +/* Set row_pointers, which is an array of pointers to scanlines for use +by png_write_png(). */ +extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_bytepp row_pointers)); +#endif + +/* Returns number of color channels in image. */ +extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr, +png_infop info_ptr)); + +#ifdef PNG_EASY_ACCESS_SUPPORTED +/* Returns image width in pixels. */ +extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image height in pixels. */ +extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image bit_depth. */ +extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image color_type. */ +extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image filter_type. */ +extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image interlace_type. */ +extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image compression_type. */ +extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns image resolution in pixels per meter, from pHYs chunk data. */ +extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +/* Returns pixel aspect ratio, computed from pHYs chunk data. */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +#endif + +/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */ +extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp +png_ptr, png_infop info_ptr)); +extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp +png_ptr, png_infop info_ptr)); + +#endif /* PNG_EASY_ACCESS_SUPPORTED */ + +/* Returns pointer to signature string read from PNG header */ +extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr, +png_infop info_ptr)); + +#if defined(PNG_bKGD_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_color_16p *background)); +#endif + +#if defined(PNG_bKGD_SUPPORTED) +extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_color_16p background)); +#endif + +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr, + png_infop info_ptr, double *white_x, double *white_y, double *red_x, + double *red_y, double *green_x, double *green_y, double *blue_x, + double *blue_y)); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point + *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y, + png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point + *int_blue_x, png_fixed_point *int_blue_y)); +#endif +#endif + +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr, + png_infop info_ptr, double white_x, double white_y, double red_x, + double red_y, double green_x, double green_y, double blue_x, double blue_y)); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y, + png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point + int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, + png_fixed_point int_blue_y)); +#endif +#endif + +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr, + png_infop info_ptr, double *file_gamma)); +#endif +extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_fixed_point *int_file_gamma)); +#endif + +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr, + png_infop info_ptr, double file_gamma)); +#endif +extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_fixed_point int_file_gamma)); +#endif + +#if defined(PNG_hIST_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_16p *hist)); +#endif + +#if defined(PNG_hIST_SUPPORTED) +extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_16p hist)); +#endif + +extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, + int *bit_depth, int *color_type, int *interlace_method, + int *compression_method, int *filter_method)); + +extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, + int color_type, int interlace_method, int compression_method, + int filter_method)); + +#if defined(PNG_oFFs_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, + int *unit_type)); +#endif + +#if defined(PNG_oFFs_SUPPORTED) +extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y, + int unit_type)); +#endif + +#if defined(PNG_pCAL_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1, + int *type, int *nparams, png_charp *units, png_charpp *params)); +#endif + +#if defined(PNG_pCAL_SUPPORTED) +extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, + int type, int nparams, png_charp units, png_charpp params)); +#endif + +#if defined(PNG_pHYs_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)); +#endif + +#if defined(PNG_pHYs_SUPPORTED) +extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)); +#endif + +extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_colorp *palette, int *num_palette)); + +extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_colorp palette, int num_palette)); + +#if defined(PNG_sBIT_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_color_8p *sig_bit)); +#endif + +#if defined(PNG_sBIT_SUPPORTED) +extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_color_8p sig_bit)); +#endif + +#if defined(PNG_sRGB_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr, + png_infop info_ptr, int *intent)); +#endif + +#if defined(PNG_sRGB_SUPPORTED) +extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr, + png_infop info_ptr, int intent)); +extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr, + png_infop info_ptr, int intent)); +#endif + +#if defined(PNG_iCCP_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_charpp name, int *compression_type, + png_charpp profile, png_uint_32 *proflen)); + /* Note to maintainer: profile should be png_bytepp */ +#endif + +#if defined(PNG_iCCP_SUPPORTED) +extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_charp name, int compression_type, + png_charp profile, png_uint_32 proflen)); + /* Note to maintainer: profile should be png_bytep */ +#endif + +#if defined(PNG_sPLT_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_sPLT_tpp entries)); +#endif + +#if defined(PNG_sPLT_SUPPORTED) +extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_sPLT_tp entries, int nentries)); +#endif + +#if defined(PNG_TEXT_SUPPORTED) +/* png_get_text also returns the number of text chunks in *num_text */ +extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_textp *text_ptr, int *num_text)); +#endif + +/* + * Note while png_set_text() will accept a structure whose text, + * language, and translated keywords are NULL pointers, the structure + * returned by png_get_text will always contain regular + * zero-terminated C strings. They might be empty strings but + * they will never be NULL pointers. + */ + +#if defined(PNG_TEXT_SUPPORTED) +extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_textp text_ptr, int num_text)); +#endif + +#if defined(PNG_tIME_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_timep *mod_time)); +#endif + +#if defined(PNG_tIME_SUPPORTED) +extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_timep mod_time)); +#endif + +#if defined(PNG_tRNS_SUPPORTED) +extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_bytep *trans, int *num_trans, + png_color_16p *trans_values)); +#endif + +#if defined(PNG_tRNS_SUPPORTED) +extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_bytep trans, int num_trans, + png_color_16p trans_values)); +#endif + +#if defined(PNG_tRNS_SUPPORTED) +#endif + +#if defined(PNG_sCAL_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr, + png_infop info_ptr, int *unit, double *width, double *height)); +#else +#ifdef PNG_FIXED_POINT_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr, + png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight)); +#endif +#endif +#endif /* PNG_sCAL_SUPPORTED */ + +#if defined(PNG_sCAL_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr, + png_infop info_ptr, int unit, double width, double height)); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr, + png_infop info_ptr, int unit, png_charp swidth, png_charp sheight)); +#endif +#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */ + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +/* provide a list of chunks and how they are to be handled, if the built-in + handling or default unknown chunk handling is not desired. Any chunks not + listed will be handled in the default manner. The IHDR and IEND chunks + must not be listed. + keep = 0: follow default behavour + = 1: do not keep + = 2: keep only if safe-to-copy + = 3: keep even if unsafe-to-copy +*/ +extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp + png_ptr, int keep, png_bytep chunk_list, int num_chunks)); +extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr, + png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)); +extern PNG_EXPORT(void, png_set_unknown_chunk_location) + PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location)); +extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp + png_ptr, png_infop info_ptr, png_unknown_chunkpp entries)); +#endif +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep + chunk_name)); +#endif + +/* Png_free_data() will turn off the "valid" flag for anything it frees. + If you need to turn it off for a chunk that your application has freed, + you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */ +extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr, + png_infop info_ptr, int mask)); + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +/* The "params" pointer is currently not used and is for future expansion. */ +extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr, + png_infop info_ptr, + int transforms, + png_voidp params)); +extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr, + png_infop info_ptr, + int transforms, + png_voidp params)); +#endif + +/* Define PNG_DEBUG at compile time for debugging information. Higher + * numbers for PNG_DEBUG mean more debugging information. This has + * only been added since version 0.95 so it is not implemented throughout + * libpng yet, but more support will be added as needed. + */ +#ifdef PNG_DEBUG +#if (PNG_DEBUG > 0) +#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER) +#include +#if (PNG_DEBUG > 1) +#define png_debug(l,m) _RPT0(_CRT_WARN,m) +#define png_debug1(l,m,p1) _RPT1(_CRT_WARN,m,p1) +#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m,p1,p2) +#endif +#else /* PNG_DEBUG_FILE || !_MSC_VER */ +#ifndef PNG_DEBUG_FILE +#define PNG_DEBUG_FILE stderr +#endif /* PNG_DEBUG_FILE */ +#if (PNG_DEBUG > 1) +#define png_debug(l,m) \ +{ \ + int num_tabs=l; \ + fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \ + (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \ +} +#define png_debug1(l,m,p1) \ +{ \ + int num_tabs=l; \ + fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \ + (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \ +} +#define png_debug2(l,m,p1,p2) \ +{ \ + int num_tabs=l; \ + fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \ + (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \ +} +#endif /* (PNG_DEBUG > 1) */ +#endif /* _MSC_VER */ +#endif /* (PNG_DEBUG > 0) */ +#endif /* PNG_DEBUG */ +#ifndef png_debug +#define png_debug(l, m) +#endif +#ifndef png_debug1 +#define png_debug1(l, m, p1) +#endif +#ifndef png_debug2 +#define png_debug2(l, m, p1, p2) +#endif + +extern PNG_EXPORT(png_bytep,png_sig_bytes) PNGARG((void)); + +extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr)); +extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr)); + +#ifdef PNG_MNG_FEATURES_SUPPORTED +extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp + png_ptr, png_uint_32 mng_features_permitted)); +#endif + +/* For use in png_set_keep_unknown, added to version 1.2.6 */ +#define PNG_HANDLE_CHUNK_AS_DEFAULT 0 +#define PNG_HANDLE_CHUNK_NEVER 1 +#define PNG_HANDLE_CHUNK_IF_SAFE 2 +#define PNG_HANDLE_CHUNK_ALWAYS 3 + +/* Added to version 1.2.0 */ +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) +#define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED 0x01 /* not user-settable */ +#define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU 0x02 /* not user-settable */ +#define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW 0x04 +#define PNG_ASM_FLAG_MMX_READ_INTERLACE 0x08 +#define PNG_ASM_FLAG_MMX_READ_FILTER_SUB 0x10 +#define PNG_ASM_FLAG_MMX_READ_FILTER_UP 0x20 +#define PNG_ASM_FLAG_MMX_READ_FILTER_AVG 0x40 +#define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80 +#define PNG_ASM_FLAGS_INITIALIZED 0x80000000 /* not user-settable */ + +#define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ + | PNG_ASM_FLAG_MMX_READ_INTERLACE \ + | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ + | PNG_ASM_FLAG_MMX_READ_FILTER_UP \ + | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ + | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ) +#define PNG_MMX_WRITE_FLAGS ( 0 ) + +#define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \ + | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU \ + | PNG_MMX_READ_FLAGS \ + | PNG_MMX_WRITE_FLAGS ) + +#define PNG_SELECT_READ 1 +#define PNG_SELECT_WRITE 2 + +#if !defined(PNG_1_0_X) +/* pngget.c */ +extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask) + PNGARG((int flag_select, int *compilerID)); + +/* pngget.c */ +extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask) + PNGARG((int flag_select)); + +/* pngget.c */ +extern PNG_EXPORT(png_uint_32,png_get_asm_flags) + PNGARG((png_structp png_ptr)); + +/* pngget.c */ +extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold) + PNGARG((png_structp png_ptr)); + +/* pngget.c */ +extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold) + PNGARG((png_structp png_ptr)); + +/* pngset.c */ +extern PNG_EXPORT(void,png_set_asm_flags) + PNGARG((png_structp png_ptr, png_uint_32 asm_flags)); + +/* pngset.c */ +extern PNG_EXPORT(void,png_set_mmx_thresholds) + PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold, + png_uint_32 mmx_rowbytes_threshold)); + +#endif /* PNG_1_0_X */ +#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */ + +#if !defined(PNG_1_0_X) +/* png.c, pnggccrd.c, or pngvcrd.c */ +extern PNG_EXPORT(int,png_mmx_support) PNGARG((void)); + +/* Strip the prepended error numbers ("#nnn ") from error and warning + * messages before passing them to the error or warning handler. */ +#ifdef PNG_ERROR_NUMBERS_SUPPORTED +extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp + png_ptr, png_uint_32 strip_mode)); +#endif + +#endif /* PNG_1_0_X */ + +/* Added at libpng-1.2.6 */ +#ifdef PNG_SET_USER_LIMITS_SUPPORTED +extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp + png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max)); +extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp + png_ptr)); +extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp + png_ptr)); +#endif + +/* Maintainer: Put new public prototypes here ^, in libpng.3, and project defs */ + +#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED +/* With these routines we avoid an integer divide, which will be slower on + * most machines. However, it does take more operations than the corresponding + * divide method, so it may be slower on a few RISC systems. There are two + * shifts (by 8 or 16 bits) and an addition, versus a single integer divide. + * + * Note that the rounding factors are NOT supposed to be the same! 128 and + * 32768 are correct for the NODIV code; 127 and 32767 are correct for the + * standard method. + * + * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ] + */ + + /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */ + +# define png_composite(composite, fg, alpha, bg) \ + { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \ + + (png_uint_16)(bg)*(png_uint_16)(255 - \ + (png_uint_16)(alpha)) + (png_uint_16)128); \ + (composite) = (png_byte)((temp + (temp >> 8)) >> 8); } + +# define png_composite_16(composite, fg, alpha, bg) \ + { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \ + + (png_uint_32)(bg)*(png_uint_32)(65535L - \ + (png_uint_32)(alpha)) + (png_uint_32)32768L); \ + (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); } + +#else /* standard method using integer division */ + +# define png_composite(composite, fg, alpha, bg) \ + (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) + \ + (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \ + (png_uint_16)127) / 255) + +# define png_composite_16(composite, fg, alpha, bg) \ + (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \ + (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) + \ + (png_uint_32)32767) / (png_uint_32)65535L) + +#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */ + +/* These next functions are used internally in the code. They generally + * shouldn't be used unless you are writing code to add or replace some + * functionality in libpng. More information about most functions can + * be found in the files where the functions are located. + */ + +#if defined(PNG_INTERNAL) + +/* Various modes of operation. Note that after an init, mode is set to + * zero automatically when the structure is created. + */ +#define PNG_HAVE_IHDR 0x01 +#define PNG_HAVE_PLTE 0x02 +#define PNG_HAVE_IDAT 0x04 +#define PNG_AFTER_IDAT 0x08 +#define PNG_HAVE_IEND 0x10 +#define PNG_HAVE_gAMA 0x20 +#define PNG_HAVE_cHRM 0x40 +#define PNG_HAVE_sRGB 0x80 +#define PNG_HAVE_CHUNK_HEADER 0x100 +#define PNG_WROTE_tIME 0x200 +#define PNG_WROTE_INFO_BEFORE_PLTE 0x400 +#define PNG_BACKGROUND_IS_GRAY 0x800 +#define PNG_HAVE_PNG_SIGNATURE 0x1000 + +/* flags for the transformations the PNG library does on the image data */ +#define PNG_BGR 0x0001 +#define PNG_INTERLACE 0x0002 +#define PNG_PACK 0x0004 +#define PNG_SHIFT 0x0008 +#define PNG_SWAP_BYTES 0x0010 +#define PNG_INVERT_MONO 0x0020 +#define PNG_DITHER 0x0040 +#define PNG_BACKGROUND 0x0080 +#define PNG_BACKGROUND_EXPAND 0x0100 + /* 0x0200 unused */ +#define PNG_16_TO_8 0x0400 +#define PNG_RGBA 0x0800 +#define PNG_EXPAND 0x1000 +#define PNG_GAMMA 0x2000 +#define PNG_GRAY_TO_RGB 0x4000 +#define PNG_FILLER 0x8000L +#define PNG_PACKSWAP 0x10000L +#define PNG_SWAP_ALPHA 0x20000L +#define PNG_STRIP_ALPHA 0x40000L +#define PNG_INVERT_ALPHA 0x80000L +#define PNG_USER_TRANSFORM 0x100000L +#define PNG_RGB_TO_GRAY_ERR 0x200000L +#define PNG_RGB_TO_GRAY_WARN 0x400000L +#define PNG_RGB_TO_GRAY 0x600000L /* two bits, RGB_TO_GRAY_ERR|WARN */ + /* 0x800000L Unused */ +#define PNG_ADD_ALPHA 0x1000000L /* Added to libpng-1.2.7 */ + /* 0x2000000L unused */ + /* 0x4000000L unused */ + /* 0x8000000L unused */ + /* 0x10000000L unused */ + /* 0x20000000L unused */ + /* 0x40000000L unused */ + +/* flags for png_create_struct */ +#define PNG_STRUCT_PNG 0x0001 +#define PNG_STRUCT_INFO 0x0002 + +/* Scaling factor for filter heuristic weighting calculations */ +#define PNG_WEIGHT_SHIFT 8 +#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT)) +#define PNG_COST_SHIFT 3 +#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT)) + +/* flags for the png_ptr->flags rather than declaring a byte for each one */ +#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001 +#define PNG_FLAG_ZLIB_CUSTOM_LEVEL 0x0002 +#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL 0x0004 +#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS 0x0008 +#define PNG_FLAG_ZLIB_CUSTOM_METHOD 0x0010 +#define PNG_FLAG_ZLIB_FINISHED 0x0020 +#define PNG_FLAG_ROW_INIT 0x0040 +#define PNG_FLAG_FILLER_AFTER 0x0080 +#define PNG_FLAG_CRC_ANCILLARY_USE 0x0100 +#define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200 +#define PNG_FLAG_CRC_CRITICAL_USE 0x0400 +#define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800 +#define PNG_FLAG_FREE_PLTE 0x1000 +#define PNG_FLAG_FREE_TRNS 0x2000 +#define PNG_FLAG_FREE_HIST 0x4000 +#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000L +#define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000L +#define PNG_FLAG_LIBRARY_MISMATCH 0x20000L +#define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000L +#define PNG_FLAG_STRIP_ERROR_TEXT 0x80000L +#define PNG_FLAG_MALLOC_NULL_MEM_OK 0x100000L +#define PNG_FLAG_ADD_ALPHA 0x200000L /* Added to libpng-1.2.8 */ +#define PNG_FLAG_STRIP_ALPHA 0x400000L /* Added to libpng-1.2.8 */ + /* 0x800000L unused */ + /* 0x1000000L unused */ + /* 0x2000000L unused */ + /* 0x4000000L unused */ + /* 0x8000000L unused */ + /* 0x10000000L unused */ + /* 0x20000000L unused */ + /* 0x40000000L unused */ + +#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ + PNG_FLAG_CRC_ANCILLARY_NOWARN) + +#define PNG_FLAG_CRC_CRITICAL_MASK (PNG_FLAG_CRC_CRITICAL_USE | \ + PNG_FLAG_CRC_CRITICAL_IGNORE) + +#define PNG_FLAG_CRC_MASK (PNG_FLAG_CRC_ANCILLARY_MASK | \ + PNG_FLAG_CRC_CRITICAL_MASK) + +/* save typing and make code easier to understand */ + +#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \ + abs((int)((c1).green) - (int)((c2).green)) + \ + abs((int)((c1).blue) - (int)((c2).blue))) + +/* Added to libpng-1.2.6 JB */ +#define PNG_ROWBYTES(pixel_bits, width) \ + ((pixel_bits) >= 8 ? \ + ((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \ + (( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) ) + +/* PNG_OUT_OF_RANGE returns true if value is outside the range + ideal-delta..ideal+delta. Each argument is evaluated twice. + "ideal" and "delta" should be constants, normally simple + integers, "value" a variable. Added to libpng-1.2.6 JB */ +#define PNG_OUT_OF_RANGE(value, ideal, delta) \ + ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) ) + +/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */ +#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN) +/* place to hold the signature string for a PNG file. */ +#ifdef PNG_USE_GLOBAL_ARRAYS + PNG_EXPORT_VAR (const png_byte FARDATA) png_sig[8]; +#else +#define png_sig png_sig_bytes(NULL) +#endif +#endif /* PNG_NO_EXTERN */ + +/* Constant strings for known chunk types. If you need to add a chunk, + * define the name here, and add an invocation of the macro in png.c and + * wherever it's needed. + */ +#define PNG_IHDR const png_byte png_IHDR[5] = { 73, 72, 68, 82, '\0'} +#define PNG_IDAT const png_byte png_IDAT[5] = { 73, 68, 65, 84, '\0'} +#define PNG_IEND const png_byte png_IEND[5] = { 73, 69, 78, 68, '\0'} +#define PNG_PLTE const png_byte png_PLTE[5] = { 80, 76, 84, 69, '\0'} +#define PNG_bKGD const png_byte png_bKGD[5] = { 98, 75, 71, 68, '\0'} +#define PNG_cHRM const png_byte png_cHRM[5] = { 99, 72, 82, 77, '\0'} +#define PNG_gAMA const png_byte png_gAMA[5] = {103, 65, 77, 65, '\0'} +#define PNG_hIST const png_byte png_hIST[5] = {104, 73, 83, 84, '\0'} +#define PNG_iCCP const png_byte png_iCCP[5] = {105, 67, 67, 80, '\0'} +#define PNG_iTXt const png_byte png_iTXt[5] = {105, 84, 88, 116, '\0'} +#define PNG_oFFs const png_byte png_oFFs[5] = {111, 70, 70, 115, '\0'} +#define PNG_pCAL const png_byte png_pCAL[5] = {112, 67, 65, 76, '\0'} +#define PNG_sCAL const png_byte png_sCAL[5] = {115, 67, 65, 76, '\0'} +#define PNG_pHYs const png_byte png_pHYs[5] = {112, 72, 89, 115, '\0'} +#define PNG_sBIT const png_byte png_sBIT[5] = {115, 66, 73, 84, '\0'} +#define PNG_sPLT const png_byte png_sPLT[5] = {115, 80, 76, 84, '\0'} +#define PNG_sRGB const png_byte png_sRGB[5] = {115, 82, 71, 66, '\0'} +#define PNG_tEXt const png_byte png_tEXt[5] = {116, 69, 88, 116, '\0'} +#define PNG_tIME const png_byte png_tIME[5] = {116, 73, 77, 69, '\0'} +#define PNG_tRNS const png_byte png_tRNS[5] = {116, 82, 78, 83, '\0'} +#define PNG_zTXt const png_byte png_zTXt[5] = {122, 84, 88, 116, '\0'} + +#ifdef PNG_USE_GLOBAL_ARRAYS +PNG_EXPORT_VAR (const png_byte FARDATA) png_IHDR[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_IDAT[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_IEND[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_PLTE[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_bKGD[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_cHRM[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_gAMA[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_hIST[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_iCCP[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_iTXt[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_oFFs[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_pCAL[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_sCAL[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_pHYs[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_sBIT[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_sPLT[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_sRGB[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_tEXt[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_tIME[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_tRNS[5]; +PNG_EXPORT_VAR (const png_byte FARDATA) png_zTXt[5]; +#endif /* PNG_USE_GLOBAL_ARRAYS */ + + +/* Inline macros to do direct reads of bytes from the input buffer. These + * require that you are using an architecture that uses PNG byte ordering + * (MSB first) and supports unaligned data storage. I think that PowerPC + * in big-endian mode and 680x0 are the only ones that will support this. + * The x86 line of processors definitely do not. The png_get_int_32() + * routine also assumes we are using two's complement format for negative + * values, which is almost certainly true. + */ +#if defined(PNG_READ_BIG_ENDIAN_SUPPORTED) +# if defined(PNG_pCAL_SUPPORTED) || defined(PNG_oFFs_SUPPORTED) +# define png_get_int_32(buf) ( *((png_int_32p) (buf))) +# endif +# define png_get_uint_32(buf) ( *((png_uint_32p) (buf))) +# define png_get_uint_16(buf) ( *((png_uint_16p) (buf))) +#else +# if defined(PNG_pCAL_SUPPORTED) || defined(PNG_oFFs_SUPPORTED) +PNG_EXTERN png_int_32 png_get_int_32 PNGARG((png_bytep buf)); +# endif +PNG_EXTERN png_uint_32 png_get_uint_32 PNGARG((png_bytep buf)); +PNG_EXTERN png_uint_16 png_get_uint_16 PNGARG((png_bytep buf)); +#endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */ +PNG_EXTERN png_uint_32 png_get_uint_31 PNGARG((png_structp png_ptr, + png_bytep buf)); + +/* Initialize png_ptr struct for reading, and allocate any other memory. + * (old interface - DEPRECATED - use png_create_read_struct instead). + */ +extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr)); +#undef png_read_init +#define png_read_init(png_ptr) png_read_init_3(&png_ptr, \ + PNG_LIBPNG_VER_STRING, png_sizeof(png_struct)); +extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr, + png_const_charp user_png_ver, png_size_t png_struct_size)); +extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr, + png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t + png_info_size)); + +/* Initialize png_ptr struct for writing, and allocate any other memory. + * (old interface - DEPRECATED - use png_create_write_struct instead). + */ +extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr)); +#undef png_write_init +#define png_write_init(png_ptr) png_write_init_3(&png_ptr, \ + PNG_LIBPNG_VER_STRING, png_sizeof(png_struct)); +extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr, + png_const_charp user_png_ver, png_size_t png_struct_size)); +extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr, + png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t + png_info_size)); + +/* Allocate memory for an internal libpng struct */ +PNG_EXTERN png_voidp png_create_struct PNGARG((int type)); + +/* Free memory from internal libpng struct */ +PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr)); + +PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr + malloc_fn, png_voidp mem_ptr)); +PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr, + png_free_ptr free_fn, png_voidp mem_ptr)); + +/* Free any memory that info_ptr points to and reset struct. */ +PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr, + png_infop info_ptr)); + +#ifndef PNG_1_0_X +/* Function to allocate memory for zlib. */ +PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size)); + +/* Function to free memory for zlib */ +PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr)); + +#ifdef PNG_SIZE_T +/* Function to convert a sizeof an item to png_sizeof item */ + PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size)); +#endif + +/* Next four functions are used internally as callbacks. PNGAPI is required + * but not PNG_EXPORT. PNGAPI added at libpng version 1.2.3. */ + +PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr, + png_bytep data, png_size_t length)); + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr, + png_bytep buffer, png_size_t length)); +#endif + +PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr, + png_bytep data, png_size_t length)); + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +#if !defined(PNG_NO_STDIO) +PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr)); +#endif +#endif +#else /* PNG_1_0_X */ +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr, + png_bytep buffer, png_size_t length)); +#endif +#endif /* PNG_1_0_X */ + +/* Reset the CRC variable */ +PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr)); + +/* Write the "data" buffer to whatever output you are using. */ +PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data, + png_size_t length)); + +/* Read data from whatever input you are using into the "data" buffer */ +PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data, + png_size_t length)); + +/* Read bytes into buf, and update png_ptr->crc */ +PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, + png_size_t length)); + +/* Decompress data in a chunk that uses compression */ +#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \ + defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) +PNG_EXTERN png_charp png_decompress_chunk PNGARG((png_structp png_ptr, + int comp_type, png_charp chunkdata, png_size_t chunklength, + png_size_t prefix_length, png_size_t *data_length)); +#endif + +/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */ +PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip)); + +/* Read the CRC from the file and compare it to the libpng calculated CRC */ +PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr)); + +/* Calculate the CRC over a section of data. Note that we are only + * passing a maximum of 64K on systems that have this as a memory limit, + * since this is the maximum buffer size we can specify. + */ +PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr, + png_size_t length)); + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +PNG_EXTERN void png_flush PNGARG((png_structp png_ptr)); +#endif + + +/* Place a 32-bit number into a buffer in PNG byte order (big-endian). + * The only currently known PNG chunks that use signed numbers are + * the ancillary extension chunks, oFFs and pCAL. + */ +PNG_EXTERN void png_save_uint_32 PNGARG((png_bytep buf, png_uint_32 i)); + +#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED) +PNG_EXTERN void png_save_int_32 PNGARG((png_bytep buf, png_int_32 i)); +#endif + +/* Place a 16-bit number into a buffer in PNG byte order. + * The parameter is declared unsigned int, not png_uint_16, + * just to avoid potential problems on pre-ANSI C compilers. + */ +PNG_EXTERN void png_save_uint_16 PNGARG((png_bytep buf, unsigned int i)); + +/* simple function to write the signature */ +PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr)); + +/* write various chunks */ + +/* Write the IHDR chunk, and update the png_struct with the necessary + * information. + */ +PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width, + png_uint_32 height, + int bit_depth, int color_type, int compression_method, int filter_method, + int interlace_method)); + +PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette, + png_uint_32 num_pal)); + +PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data, + png_size_t length)); + +PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr)); + +#if defined(PNG_WRITE_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma)); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point + file_gamma)); +#endif +#endif + +#if defined(PNG_WRITE_sBIT_SUPPORTED) +PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit, + int color_type)); +#endif + +#if defined(PNG_WRITE_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr, + double white_x, double white_y, + double red_x, double red_y, double green_x, double green_y, + double blue_x, double blue_y)); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr, + png_fixed_point int_white_x, png_fixed_point int_white_y, + png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point + int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, + png_fixed_point int_blue_y)); +#endif +#endif + +#if defined(PNG_WRITE_sRGB_SUPPORTED) +PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr, + int intent)); +#endif + +#if defined(PNG_WRITE_iCCP_SUPPORTED) +PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr, + png_charp name, int compression_type, + png_charp profile, int proflen)); + /* Note to maintainer: profile should be png_bytep */ +#endif + +#if defined(PNG_WRITE_sPLT_SUPPORTED) +PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr, + png_sPLT_tp palette)); +#endif + +#if defined(PNG_WRITE_tRNS_SUPPORTED) +PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans, + png_color_16p values, int number, int color_type)); +#endif + +#if defined(PNG_WRITE_bKGD_SUPPORTED) +PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr, + png_color_16p values, int color_type)); +#endif + +#if defined(PNG_WRITE_hIST_SUPPORTED) +PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist, + int num_hist)); +#endif + +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ + defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) +PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr, + png_charp key, png_charpp new_key)); +#endif + +#if defined(PNG_WRITE_tEXt_SUPPORTED) +PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key, + png_charp text, png_size_t text_len)); +#endif + +#if defined(PNG_WRITE_zTXt_SUPPORTED) +PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key, + png_charp text, png_size_t text_len, int compression)); +#endif + +#if defined(PNG_WRITE_iTXt_SUPPORTED) +PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr, + int compression, png_charp key, png_charp lang, png_charp lang_key, + png_charp text)); +#endif + +#if defined(PNG_TEXT_SUPPORTED) /* Added at version 1.0.14 and 1.2.4 */ +PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr, + png_infop info_ptr, png_textp text_ptr, int num_text)); +#endif + +#if defined(PNG_WRITE_oFFs_SUPPORTED) +PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr, + png_int_32 x_offset, png_int_32 y_offset, int unit_type)); +#endif + +#if defined(PNG_WRITE_pCAL_SUPPORTED) +PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose, + png_int_32 X0, png_int_32 X1, int type, int nparams, + png_charp units, png_charpp params)); +#endif + +#if defined(PNG_WRITE_pHYs_SUPPORTED) +PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr, + png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, + int unit_type)); +#endif + +#if defined(PNG_WRITE_tIME_SUPPORTED) +PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr, + png_timep mod_time)); +#endif + +#if defined(PNG_WRITE_sCAL_SUPPORTED) +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) +PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr, + int unit, double width, double height)); +#else +#ifdef PNG_FIXED_POINT_SUPPORTED +PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr, + int unit, png_charp width, png_charp height)); +#endif +#endif +#endif + +/* Called when finished processing a row of data */ +PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr)); + +/* Internal use only. Called before first row of data */ +PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr)); + +#if defined(PNG_READ_GAMMA_SUPPORTED) +PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr)); +#endif + +/* combine a row of data, dealing with alpha, etc. if requested */ +PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row, + int mask)); + +#if defined(PNG_READ_INTERLACING_SUPPORTED) +/* expand an interlaced row */ +/* OLD pre-1.0.9 interface: +PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info, + png_bytep row, int pass, png_uint_32 transformations)); + */ +PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr)); +#endif + +/* GRR TO DO (2.0 or whenever): simplify other internal calling interfaces */ + +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) +/* grab pixels out of a row for an interlaced pass */ +PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info, + png_bytep row, int pass)); +#endif + +/* unfilter a row */ +PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr, + png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter)); + +/* Choose the best filter to use and filter the row data */ +PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr, + png_row_infop row_info)); + +/* Write out the filtered row. */ +PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr, + png_bytep filtered_row)); +/* finish a row while reading, dealing with interlacing passes, etc. */ +PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr)); + +/* initialize the row buffers, etc. */ +PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr)); +/* optional call to update the users info structure */ +PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr, + png_infop info_ptr)); + +/* these are the functions that do the transformations */ +#if defined(PNG_READ_FILLER_SUPPORTED) +PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info, + png_bytep row, png_uint_32 filler, png_uint_32 flags)); +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) +PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) +PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) +PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) +PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \ + defined(PNG_READ_STRIP_ALPHA_SUPPORTED) +PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info, + png_bytep row, png_uint_32 flags)); +#endif + +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) +PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED) +PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop + row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) +PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) +PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row, + png_color_8p sig_bits)); +#endif + +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) +PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) +PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) +PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info, + png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup)); + +# if defined(PNG_CORRECT_PALETTE_SUPPORTED) +PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr, + png_colorp palette, int num_palette)); +# endif +#endif + +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) +PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_WRITE_PACK_SUPPORTED) +PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info, + png_bytep row, png_uint_32 bit_depth)); +#endif + +#if defined(PNG_WRITE_SHIFT_SUPPORTED) +PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row, + png_color_8p bit_depth)); +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) +#if defined(PNG_READ_GAMMA_SUPPORTED) +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row, + png_color_16p trans_values, png_color_16p background, + png_color_16p background_1, + png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, + png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, + png_uint_16pp gamma_16_to_1, int gamma_shift)); +#else +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row, + png_color_16p trans_values, png_color_16p background)); +#endif +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) +PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row, + png_bytep gamma_table, png_uint_16pp gamma_16_table, + int gamma_shift)); +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) +PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info, + png_bytep row, png_colorp palette, png_bytep trans, int num_trans)); +PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info, + png_bytep row, png_color_16p trans_value)); +#endif + +/* The following decodes the appropriate chunks, and does error correction, + * then calls the appropriate callback for the chunk if it is valid. + */ + +/* decode the IHDR chunk */ +PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); + +#if defined(PNG_READ_bKGD_SUPPORTED) +PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_cHRM_SUPPORTED) +PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_gAMA_SUPPORTED) +PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_hIST_SUPPORTED) +PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_iCCP_SUPPORTED) +extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif /* PNG_READ_iCCP_SUPPORTED */ + +#if defined(PNG_READ_iTXt_SUPPORTED) +PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_oFFs_SUPPORTED) +PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_pCAL_SUPPORTED) +PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_pHYs_SUPPORTED) +PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_sBIT_SUPPORTED) +PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_sCAL_SUPPORTED) +PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_sPLT_SUPPORTED) +extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif /* PNG_READ_sPLT_SUPPORTED */ + +#if defined(PNG_READ_sRGB_SUPPORTED) +PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_tEXt_SUPPORTED) +PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_tIME_SUPPORTED) +PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_tRNS_SUPPORTED) +PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +#if defined(PNG_READ_zTXt_SUPPORTED) +PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +#endif + +PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 length)); + +PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr, + png_bytep chunk_name)); + +/* handle the transformations for reading and writing */ +PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr)); + +PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr)); + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr, + png_uint_32 length)); +PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr, + png_bytep buffer, png_size_t buffer_length)); +PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr, + png_bytep buffer, png_size_t buffer_length)); +PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr)); +PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 length)); +PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row)); +PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr, + png_infop info_ptr)); +PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr)); +#if defined(PNG_READ_tEXt_SUPPORTED) +PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 length)); +PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr, + png_infop info_ptr)); +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) +PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 length)); +PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr, + png_infop info_ptr)); +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) +PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr, + png_infop info_ptr, png_uint_32 length)); +PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr, + png_infop info_ptr)); +#endif + +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ + +#ifdef PNG_MNG_FEATURES_SUPPORTED +PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info, + png_bytep row)); +PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info, + png_bytep row)); +#endif + +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) +/* png.c */ /* PRIVATE */ +PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr)); +#endif +/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */ + +#endif /* PNG_INTERNAL */ + +#ifdef __cplusplus +} +#endif + +#endif /* PNG_VERSION_INFO_ONLY */ +/* do not put anything past this line */ +#endif /* PNG_H */ diff --git a/libpng/pngconf.h b/libpng/pngconf.h new file mode 100644 index 00000000..ba508384 --- /dev/null +++ b/libpng/pngconf.h @@ -0,0 +1,1437 @@ + +/* pngconf.h - machine configurable file for libpng + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +/* Any machine specific code is near the front of this file, so if you + * are configuring libpng for a machine, you may want to read the section + * starting here down to where it starts to typedef png_color, png_text, + * and png_info. + */ + +#ifndef PNGCONF_H +#define PNGCONF_H + +#define PNG_1_2_X + +/* + * PNG_USER_CONFIG has to be defined on the compiler command line. This + * includes the resource compiler for Windows DLL configurations. + */ +#ifdef PNG_USER_CONFIG +#include "pngusr.h" +#endif + +/* + * Added at libpng-1.2.8 + * + * If you create a private DLL you need to define in "pngusr.h" the followings: + * #define PNG_USER_PRIVATEBUILD + * e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons." + * #define PNG_USER_DLLFNAME_POSTFIX + * e.g. // private DLL "libpng13gx.dll" + * #define PNG_USER_DLLFNAME_POSTFIX "gx" + * + * The following macros are also at your disposal if you want to complete the + * DLL VERSIONINFO structure. + * - PNG_USER_VERSIONINFO_COMMENTS + * - PNG_USER_VERSIONINFO_COMPANYNAME + * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS + */ + +#ifdef __STDC__ +#ifdef SPECIALBUILD +# pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\ + are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.") +#endif + +#ifdef PRIVATEBUILD +# pragma message("PRIVATEBUILD is deprecated. Use\ + PNG_USER_PRIVATEBUILD instead.") +# define PNG_USER_PRIVATEBUILD PRIVATEBUILD +#endif +#endif /* __STDC__ */ + +#ifndef PNG_VERSION_INFO_ONLY + +/* End of material added to libpng-1.2.8 */ + +/* This is the size of the compression buffer, and thus the size of + * an IDAT chunk. Make this whatever size you feel is best for your + * machine. One of these will be allocated per png_struct. When this + * is full, it writes the data to the disk, and does some other + * calculations. Making this an extremely small size will slow + * the library down, but you may want to experiment to determine + * where it becomes significant, if you are concerned with memory + * usage. Note that zlib allocates at least 32Kb also. For readers, + * this describes the size of the buffer available to read the data in. + * Unless this gets smaller than the size of a row (compressed), + * it should not make much difference how big this is. + */ + +#ifndef PNG_ZBUF_SIZE +# define PNG_ZBUF_SIZE 8192 +#endif + +/* Enable if you want a write-only libpng */ + +#ifndef PNG_NO_READ_SUPPORTED +# define PNG_READ_SUPPORTED +#endif + +/* Enable if you want a read-only libpng */ + +#ifndef PNG_NO_WRITE_SUPPORTED +# define PNG_WRITE_SUPPORTED +#endif + +/* Enabled by default in 1.2.0. You can disable this if you don't need to + support PNGs that are embedded in MNG datastreams */ +#if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES) +# ifndef PNG_MNG_FEATURES_SUPPORTED +# define PNG_MNG_FEATURES_SUPPORTED +# endif +#endif + +#ifndef PNG_NO_FLOATING_POINT_SUPPORTED +# ifndef PNG_FLOATING_POINT_SUPPORTED +# define PNG_FLOATING_POINT_SUPPORTED +# endif +#endif + +/* If you are running on a machine where you cannot allocate more + * than 64K of memory at once, uncomment this. While libpng will not + * normally need that much memory in a chunk (unless you load up a very + * large file), zlib needs to know how big of a chunk it can use, and + * libpng thus makes sure to check any memory allocation to verify it + * will fit into memory. +#define PNG_MAX_MALLOC_64K + */ +#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) +# define PNG_MAX_MALLOC_64K +#endif + +/* Special munging to support doing things the 'cygwin' way: + * 'Normal' png-on-win32 defines/defaults: + * PNG_BUILD_DLL -- building dll + * PNG_USE_DLL -- building an application, linking to dll + * (no define) -- building static library, or building an + * application and linking to the static lib + * 'Cygwin' defines/defaults: + * PNG_BUILD_DLL -- (ignored) building the dll + * (no define) -- (ignored) building an application, linking to the dll + * PNG_STATIC -- (ignored) building the static lib, or building an + * application that links to the static lib. + * ALL_STATIC -- (ignored) building various static libs, or building an + * application that links to the static libs. + * Thus, + * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and + * this bit of #ifdefs will define the 'correct' config variables based on + * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but + * unnecessary. + * + * Also, the precedence order is: + * ALL_STATIC (since we can't #undef something outside our namespace) + * PNG_BUILD_DLL + * PNG_STATIC + * (nothing) == PNG_USE_DLL + * + * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent + * of auto-import in binutils, we no longer need to worry about + * __declspec(dllexport) / __declspec(dllimport) and friends. Therefore, + * we don't need to worry about PNG_STATIC or ALL_STATIC when it comes + * to __declspec() stuff. However, we DO need to worry about + * PNG_BUILD_DLL and PNG_STATIC because those change some defaults + * such as CONSOLE_IO and whether GLOBAL_ARRAYS are allowed. + */ +#if defined(__CYGWIN__) +# if defined(ALL_STATIC) +# if defined(PNG_BUILD_DLL) +# undef PNG_BUILD_DLL +# endif +# if defined(PNG_USE_DLL) +# undef PNG_USE_DLL +# endif +# if defined(PNG_DLL) +# undef PNG_DLL +# endif +# if !defined(PNG_STATIC) +# define PNG_STATIC +# endif +# else +# if defined (PNG_BUILD_DLL) +# if defined(PNG_STATIC) +# undef PNG_STATIC +# endif +# if defined(PNG_USE_DLL) +# undef PNG_USE_DLL +# endif +# if !defined(PNG_DLL) +# define PNG_DLL +# endif +# else +# if defined(PNG_STATIC) +# if defined(PNG_USE_DLL) +# undef PNG_USE_DLL +# endif +# if defined(PNG_DLL) +# undef PNG_DLL +# endif +# else +# if !defined(PNG_USE_DLL) +# define PNG_USE_DLL +# endif +# if !defined(PNG_DLL) +# define PNG_DLL +# endif +# endif +# endif +# endif +#endif + +/* This protects us against compilers that run on a windowing system + * and thus don't have or would rather us not use the stdio types: + * stdin, stdout, and stderr. The only one currently used is stderr + * in png_error() and png_warning(). #defining PNG_NO_CONSOLE_IO will + * prevent these from being compiled and used. #defining PNG_NO_STDIO + * will also prevent these, plus will prevent the entire set of stdio + * macros and functions (FILE *, printf, etc.) from being compiled and used, + * unless (PNG_DEBUG > 0) has been #defined. + * + * #define PNG_NO_CONSOLE_IO + * #define PNG_NO_STDIO + */ + +#if defined(_WIN32_WCE) +# include + /* Console I/O functions are not supported on WindowsCE */ +# define PNG_NO_CONSOLE_IO +# ifdef PNG_DEBUG +# undef PNG_DEBUG +# endif +#endif + +#ifdef PNG_BUILD_DLL +# ifndef PNG_CONSOLE_IO_SUPPORTED +# ifndef PNG_NO_CONSOLE_IO +# define PNG_NO_CONSOLE_IO +# endif +# endif +#endif + +# ifdef PNG_NO_STDIO +# ifndef PNG_NO_CONSOLE_IO +# define PNG_NO_CONSOLE_IO +# endif +# ifdef PNG_DEBUG +# if (PNG_DEBUG > 0) +# include +# endif +# endif +# else +# if !defined(_WIN32_WCE) +/* "stdio.h" functions are not supported on WindowsCE */ +# include +# endif +# endif + +/* This macro protects us against machines that don't have function + * prototypes (ie K&R style headers). If your compiler does not handle + * function prototypes, define this macro and use the included ansi2knr. + * I've always been able to use _NO_PROTO as the indicator, but you may + * need to drag the empty declaration out in front of here, or change the + * ifdef to suit your own needs. + */ +#ifndef PNGARG + +#ifdef OF /* zlib prototype munger */ +# define PNGARG(arglist) OF(arglist) +#else + +#ifdef _NO_PROTO +# define PNGARG(arglist) () +# ifndef PNG_TYPECAST_NULL +# define PNG_TYPECAST_NULL +# endif +#else +# define PNGARG(arglist) arglist +#endif /* _NO_PROTO */ + +#endif /* OF */ + +#endif /* PNGARG */ + +/* Try to determine if we are compiling on a Mac. Note that testing for + * just __MWERKS__ is not good enough, because the Codewarrior is now used + * on non-Mac platforms. + */ +#ifndef MACOS +# if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \ + defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC) +# define MACOS +# endif +#endif + +/* enough people need this for various reasons to include it here */ +#if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE) +# include +#endif + +#if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED) +# define PNG_SETJMP_SUPPORTED +#endif + +#ifdef PNG_SETJMP_SUPPORTED +/* This is an attempt to force a single setjmp behaviour on Linux. If + * the X config stuff didn't define _BSD_SOURCE we wouldn't need this. + */ + +# ifdef __linux__ +# ifdef _BSD_SOURCE +# define PNG_SAVE_BSD_SOURCE +# undef _BSD_SOURCE +# endif +# ifdef _SETJMP_H + /* If you encounter a compiler error here, see the explanation + * near the end of INSTALL. + */ + __png.h__ already includes setjmp.h; + __dont__ include it again.; +# endif +# endif /* __linux__ */ + + /* include setjmp.h for error handling */ +# include + +# ifdef __linux__ +# ifdef PNG_SAVE_BSD_SOURCE +# define _BSD_SOURCE +# undef PNG_SAVE_BSD_SOURCE +# endif +# endif /* __linux__ */ +#endif /* PNG_SETJMP_SUPPORTED */ + +#ifdef BSD +# include +#else +# include +#endif + +/* Other defines for things like memory and the like can go here. */ +#ifdef PNG_INTERNAL + +#include + +/* The functions exported by PNG_EXTERN are PNG_INTERNAL functions, which + * aren't usually used outside the library (as far as I know), so it is + * debatable if they should be exported at all. In the future, when it is + * possible to have run-time registry of chunk-handling functions, some of + * these will be made available again. +#define PNG_EXTERN extern + */ +#define PNG_EXTERN + +/* Other defines specific to compilers can go here. Try to keep + * them inside an appropriate ifdef/endif pair for portability. + */ + +#if defined(PNG_FLOATING_POINT_SUPPORTED) +# if defined(MACOS) + /* We need to check that hasn't already been included earlier + * as it seems it doesn't agree with , yet we should really use + * if possible. + */ +# if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__) +# include +# endif +# else +# include +# endif +# if defined(_AMIGA) && defined(__SASC) && defined(_M68881) + /* Amiga SAS/C: We must include builtin FPU functions when compiling using + * MATH=68881 + */ +# include +# endif +#endif + +/* Codewarrior on NT has linking problems without this. */ +#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__) +# define PNG_ALWAYS_EXTERN +#endif + +/* This provides the non-ANSI (far) memory allocation routines. */ +#if defined(__TURBOC__) && defined(__MSDOS__) +# include +# include +#endif + +/* I have no idea why is this necessary... */ +#if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \ + defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__)) +# include +#endif + +/* This controls how fine the dithering gets. As this allocates + * a largish chunk of memory (32K), those who are not as concerned + * with dithering quality can decrease some or all of these. + */ +#ifndef PNG_DITHER_RED_BITS +# define PNG_DITHER_RED_BITS 5 +#endif +#ifndef PNG_DITHER_GREEN_BITS +# define PNG_DITHER_GREEN_BITS 5 +#endif +#ifndef PNG_DITHER_BLUE_BITS +# define PNG_DITHER_BLUE_BITS 5 +#endif + +/* This controls how fine the gamma correction becomes when you + * are only interested in 8 bits anyway. Increasing this value + * results in more memory being used, and more pow() functions + * being called to fill in the gamma tables. Don't set this value + * less then 8, and even that may not work (I haven't tested it). + */ + +#ifndef PNG_MAX_GAMMA_8 +# define PNG_MAX_GAMMA_8 11 +#endif + +/* This controls how much a difference in gamma we can tolerate before + * we actually start doing gamma conversion. + */ +#ifndef PNG_GAMMA_THRESHOLD +# define PNG_GAMMA_THRESHOLD 0.05 +#endif + +#endif /* PNG_INTERNAL */ + +/* The following uses const char * instead of char * for error + * and warning message functions, so some compilers won't complain. + * If you do not want to use const, define PNG_NO_CONST here. + */ + +#ifndef PNG_NO_CONST +# define PNG_CONST const +#else +# define PNG_CONST +#endif + +/* The following defines give you the ability to remove code from the + * library that you will not be using. I wish I could figure out how to + * automate this, but I can't do that without making it seriously hard + * on the users. So if you are not using an ability, change the #define + * to and #undef, and that part of the library will not be compiled. If + * your linker can't find a function, you may want to make sure the + * ability is defined here. Some of these depend upon some others being + * defined. I haven't figured out all the interactions here, so you may + * have to experiment awhile to get everything to compile. If you are + * creating or using a shared library, you probably shouldn't touch this, + * as it will affect the size of the structures, and this will cause bad + * things to happen if the library and/or application ever change. + */ + +/* Any features you will not be using can be undef'ed here */ + +/* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user + * to turn it off with "*TRANSFORMS_NOT_SUPPORTED" or *PNG_NO_*_TRANSFORMS + * on the compile line, then pick and choose which ones to define without + * having to edit this file. It is safe to use the *TRANSFORMS_NOT_SUPPORTED + * if you only want to have a png-compliant reader/writer but don't need + * any of the extra transformations. This saves about 80 kbytes in a + * typical installation of the library. (PNG_NO_* form added in version + * 1.0.1c, for consistency) + */ + +/* The size of the png_text structure changed in libpng-1.0.6 when + * iTXt is supported. It is turned off by default, to support old apps + * that malloc the png_text structure instead of calling png_set_text() + * and letting libpng malloc it. It will be turned on by default in + * libpng-1.3.0. + */ + +#ifndef PNG_iTXt_SUPPORTED +# if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt) +# define PNG_NO_READ_iTXt +# endif +# if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt) +# define PNG_NO_WRITE_iTXt +# endif +#endif + +/* The following support, added after version 1.0.0, can be turned off here en + * masse by defining PNG_LEGACY_SUPPORTED in case you need binary compatibility + * with old applications that require the length of png_struct and png_info + * to remain unchanged. + */ + +#ifdef PNG_LEGACY_SUPPORTED +# define PNG_NO_FREE_ME +# define PNG_NO_READ_UNKNOWN_CHUNKS +# define PNG_NO_WRITE_UNKNOWN_CHUNKS +# define PNG_NO_READ_USER_CHUNKS +# define PNG_NO_READ_iCCP +# define PNG_NO_WRITE_iCCP +# define PNG_NO_READ_iTXt +# define PNG_NO_WRITE_iTXt +# define PNG_NO_READ_sCAL +# define PNG_NO_WRITE_sCAL +# define PNG_NO_READ_sPLT +# define PNG_NO_WRITE_sPLT +# define PNG_NO_INFO_IMAGE +# define PNG_NO_READ_RGB_TO_GRAY +# define PNG_NO_READ_USER_TRANSFORM +# define PNG_NO_WRITE_USER_TRANSFORM +# define PNG_NO_USER_MEM +# define PNG_NO_READ_EMPTY_PLTE +# define PNG_NO_MNG_FEATURES +# define PNG_NO_FIXED_POINT_SUPPORTED +#endif + +/* Ignore attempt to turn off both floating and fixed point support */ +#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \ + !defined(PNG_NO_FIXED_POINT_SUPPORTED) +# define PNG_FIXED_POINT_SUPPORTED +#endif + +#ifndef PNG_NO_FREE_ME +# define PNG_FREE_ME_SUPPORTED +#endif + +#if defined(PNG_READ_SUPPORTED) + +#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \ + !defined(PNG_NO_READ_TRANSFORMS) +# define PNG_READ_TRANSFORMS_SUPPORTED +#endif + +#ifdef PNG_READ_TRANSFORMS_SUPPORTED +# ifndef PNG_NO_READ_EXPAND +# define PNG_READ_EXPAND_SUPPORTED +# endif +# ifndef PNG_NO_READ_SHIFT +# define PNG_READ_SHIFT_SUPPORTED +# endif +# ifndef PNG_NO_READ_PACK +# define PNG_READ_PACK_SUPPORTED +# endif +# ifndef PNG_NO_READ_BGR +# define PNG_READ_BGR_SUPPORTED +# endif +# ifndef PNG_NO_READ_SWAP +# define PNG_READ_SWAP_SUPPORTED +# endif +# ifndef PNG_NO_READ_PACKSWAP +# define PNG_READ_PACKSWAP_SUPPORTED +# endif +# ifndef PNG_NO_READ_INVERT +# define PNG_READ_INVERT_SUPPORTED +# endif +# ifndef PNG_NO_READ_DITHER +# define PNG_READ_DITHER_SUPPORTED +# endif +# ifndef PNG_NO_READ_BACKGROUND +# define PNG_READ_BACKGROUND_SUPPORTED +# endif +# ifndef PNG_NO_READ_16_TO_8 +# define PNG_READ_16_TO_8_SUPPORTED +# endif +# ifndef PNG_NO_READ_FILLER +# define PNG_READ_FILLER_SUPPORTED +# endif +# ifndef PNG_NO_READ_GAMMA +# define PNG_READ_GAMMA_SUPPORTED +# endif +# ifndef PNG_NO_READ_GRAY_TO_RGB +# define PNG_READ_GRAY_TO_RGB_SUPPORTED +# endif +# ifndef PNG_NO_READ_SWAP_ALPHA +# define PNG_READ_SWAP_ALPHA_SUPPORTED +# endif +# ifndef PNG_NO_READ_INVERT_ALPHA +# define PNG_READ_INVERT_ALPHA_SUPPORTED +# endif +# ifndef PNG_NO_READ_STRIP_ALPHA +# define PNG_READ_STRIP_ALPHA_SUPPORTED +# endif +# ifndef PNG_NO_READ_USER_TRANSFORM +# define PNG_READ_USER_TRANSFORM_SUPPORTED +# endif +# ifndef PNG_NO_READ_RGB_TO_GRAY +# define PNG_READ_RGB_TO_GRAY_SUPPORTED +# endif +#endif /* PNG_READ_TRANSFORMS_SUPPORTED */ + +#if !defined(PNG_NO_PROGRESSIVE_READ) && \ + !defined(PNG_PROGRESSIVE_READ_NOT_SUPPORTED) /* if you don't do progressive */ +# define PNG_PROGRESSIVE_READ_SUPPORTED /* reading. This is not talking */ +#endif /* about interlacing capability! You'll */ + /* still have interlacing unless you change the following line: */ + +#define PNG_READ_INTERLACING_SUPPORTED /* required for PNG-compliant decoders */ + +#ifndef PNG_NO_READ_COMPOSITE_NODIV +# ifndef PNG_NO_READ_COMPOSITED_NODIV /* libpng-1.0.x misspelling */ +# define PNG_READ_COMPOSITE_NODIV_SUPPORTED /* well tested on Intel, SGI */ +# endif +#endif + +/* Deprecated, will be removed from version 2.0.0. + Use PNG_MNG_FEATURES_SUPPORTED instead. */ +#ifndef PNG_NO_READ_EMPTY_PLTE +# define PNG_READ_EMPTY_PLTE_SUPPORTED +#endif + +#endif /* PNG_READ_SUPPORTED */ + +#if defined(PNG_WRITE_SUPPORTED) + +# if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \ + !defined(PNG_NO_WRITE_TRANSFORMS) +# define PNG_WRITE_TRANSFORMS_SUPPORTED +#endif + +#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED +# ifndef PNG_NO_WRITE_SHIFT +# define PNG_WRITE_SHIFT_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_PACK +# define PNG_WRITE_PACK_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_BGR +# define PNG_WRITE_BGR_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_SWAP +# define PNG_WRITE_SWAP_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_PACKSWAP +# define PNG_WRITE_PACKSWAP_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_INVERT +# define PNG_WRITE_INVERT_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_FILLER +# define PNG_WRITE_FILLER_SUPPORTED /* same as WRITE_STRIP_ALPHA */ +# endif +# ifndef PNG_NO_WRITE_SWAP_ALPHA +# define PNG_WRITE_SWAP_ALPHA_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_INVERT_ALPHA +# define PNG_WRITE_INVERT_ALPHA_SUPPORTED +# endif +# ifndef PNG_NO_WRITE_USER_TRANSFORM +# define PNG_WRITE_USER_TRANSFORM_SUPPORTED +# endif +#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */ + +#define PNG_WRITE_INTERLACING_SUPPORTED /* not required for PNG-compliant + encoders, but can cause trouble + if left undefined */ + +#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \ + defined(PNG_FLOATING_POINT_SUPPORTED) +# define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED +#endif + +#ifndef PNG_NO_WRITE_FLUSH +# define PNG_WRITE_FLUSH_SUPPORTED +#endif + +/* Deprecated, see PNG_MNG_FEATURES_SUPPORTED, above */ +#ifndef PNG_NO_WRITE_EMPTY_PLTE +# define PNG_WRITE_EMPTY_PLTE_SUPPORTED +#endif + +#endif /* PNG_WRITE_SUPPORTED */ + +#ifndef PNG_1_0_X +# ifndef PNG_NO_ERROR_NUMBERS +# define PNG_ERROR_NUMBERS_SUPPORTED +# endif +#endif /* PNG_1_0_X */ + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) +# ifndef PNG_NO_USER_TRANSFORM_PTR +# define PNG_USER_TRANSFORM_PTR_SUPPORTED +# endif +#endif + +#ifndef PNG_NO_STDIO +# define PNG_TIME_RFC1123_SUPPORTED +#endif + +/* This adds extra functions in pngget.c for accessing data from the + * info pointer (added in version 0.99) + * png_get_image_width() + * png_get_image_height() + * png_get_bit_depth() + * png_get_color_type() + * png_get_compression_type() + * png_get_filter_type() + * png_get_interlace_type() + * png_get_pixel_aspect_ratio() + * png_get_pixels_per_meter() + * png_get_x_offset_pixels() + * png_get_y_offset_pixels() + * png_get_x_offset_microns() + * png_get_y_offset_microns() + */ +#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED) +# define PNG_EASY_ACCESS_SUPPORTED +#endif + +/* PNG_ASSEMBLER_CODE was enabled by default in version 1.2.0 + even when PNG_USE_PNGVCRD or PNG_USE_PNGGCCRD is not defined */ +#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE) +# ifndef PNG_ASSEMBLER_CODE_SUPPORTED +# define PNG_ASSEMBLER_CODE_SUPPORTED +# endif +# if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE) +# define PNG_MMX_CODE_SUPPORTED +# endif +#endif + +/* If you are sure that you don't need thread safety and you are compiling + with PNG_USE_PNGCCRD for an MMX application, you can define this for + faster execution. See pnggccrd.c. +#define PNG_THREAD_UNSAFE_OK +*/ + +#if !defined(PNG_1_0_X) +#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED) +# define PNG_USER_MEM_SUPPORTED +#endif +#endif /* PNG_1_0_X */ + +/* Added at libpng-1.2.6 */ +#if !defined(PNG_1_0_X) +#ifndef PNG_SET_USER_LIMITS_SUPPORTED +#if !defined(PNG_NO_SET_USER_LIMITS) && !defined(PNG_SET_USER_LIMITS_SUPPORTED) +# define PNG_SET_USER_LIMITS_SUPPORTED +#endif +#endif +#endif /* PNG_1_0_X */ + +/* Added at libpng-1.0.16 and 1.2.6. To accept all valid PNGS no matter + * how large, set these limits to 0x7fffffffL + */ +#ifndef PNG_USER_WIDTH_MAX +# define PNG_USER_WIDTH_MAX 1000000L +#endif +#ifndef PNG_USER_HEIGHT_MAX +# define PNG_USER_HEIGHT_MAX 1000000L +#endif + +/* These are currently experimental features, define them if you want */ + +/* very little testing */ +/* +#ifdef PNG_READ_SUPPORTED +# ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED +# define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED +# endif +#endif +*/ + +/* This is only for PowerPC big-endian and 680x0 systems */ +/* some testing */ +/* +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED +# define PNG_READ_BIG_ENDIAN_SUPPORTED +#endif +*/ + +/* Buggy compilers (e.g., gcc 2.7.2.2) need this */ +/* +#define PNG_NO_POINTER_INDEXING +*/ + +/* These functions are turned off by default, as they will be phased out. */ +/* +#define PNG_USELESS_TESTS_SUPPORTED +#define PNG_CORRECT_PALETTE_SUPPORTED +*/ + +/* Any chunks you are not interested in, you can undef here. The + * ones that allocate memory may be expecially important (hIST, + * tEXt, zTXt, tRNS, pCAL). Others will just save time and make png_info + * a bit smaller. + */ + +#if defined(PNG_READ_SUPPORTED) && \ + !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \ + !defined(PNG_NO_READ_ANCILLARY_CHUNKS) +# define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED +#endif + +#if defined(PNG_WRITE_SUPPORTED) && \ + !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \ + !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS) +# define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED +#endif + +#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED + +#ifdef PNG_NO_READ_TEXT +# define PNG_NO_READ_iTXt +# define PNG_NO_READ_tEXt +# define PNG_NO_READ_zTXt +#endif +#ifndef PNG_NO_READ_bKGD +# define PNG_READ_bKGD_SUPPORTED +# define PNG_bKGD_SUPPORTED +#endif +#ifndef PNG_NO_READ_cHRM +# define PNG_READ_cHRM_SUPPORTED +# define PNG_cHRM_SUPPORTED +#endif +#ifndef PNG_NO_READ_gAMA +# define PNG_READ_gAMA_SUPPORTED +# define PNG_gAMA_SUPPORTED +#endif +#ifndef PNG_NO_READ_hIST +# define PNG_READ_hIST_SUPPORTED +# define PNG_hIST_SUPPORTED +#endif +#ifndef PNG_NO_READ_iCCP +# define PNG_READ_iCCP_SUPPORTED +# define PNG_iCCP_SUPPORTED +#endif +#ifndef PNG_NO_READ_iTXt +# ifndef PNG_READ_iTXt_SUPPORTED +# define PNG_READ_iTXt_SUPPORTED +# endif +# ifndef PNG_iTXt_SUPPORTED +# define PNG_iTXt_SUPPORTED +# endif +#endif +#ifndef PNG_NO_READ_oFFs +# define PNG_READ_oFFs_SUPPORTED +# define PNG_oFFs_SUPPORTED +#endif +#ifndef PNG_NO_READ_pCAL +# define PNG_READ_pCAL_SUPPORTED +# define PNG_pCAL_SUPPORTED +#endif +#ifndef PNG_NO_READ_sCAL +# define PNG_READ_sCAL_SUPPORTED +# define PNG_sCAL_SUPPORTED +#endif +#ifndef PNG_NO_READ_pHYs +# define PNG_READ_pHYs_SUPPORTED +# define PNG_pHYs_SUPPORTED +#endif +#ifndef PNG_NO_READ_sBIT +# define PNG_READ_sBIT_SUPPORTED +# define PNG_sBIT_SUPPORTED +#endif +#ifndef PNG_NO_READ_sPLT +# define PNG_READ_sPLT_SUPPORTED +# define PNG_sPLT_SUPPORTED +#endif +#ifndef PNG_NO_READ_sRGB +# define PNG_READ_sRGB_SUPPORTED +# define PNG_sRGB_SUPPORTED +#endif +#ifndef PNG_NO_READ_tEXt +# define PNG_READ_tEXt_SUPPORTED +# define PNG_tEXt_SUPPORTED +#endif +#ifndef PNG_NO_READ_tIME +# define PNG_READ_tIME_SUPPORTED +# define PNG_tIME_SUPPORTED +#endif +#ifndef PNG_NO_READ_tRNS +# define PNG_READ_tRNS_SUPPORTED +# define PNG_tRNS_SUPPORTED +#endif +#ifndef PNG_NO_READ_zTXt +# define PNG_READ_zTXt_SUPPORTED +# define PNG_zTXt_SUPPORTED +#endif +#ifndef PNG_NO_READ_UNKNOWN_CHUNKS +# define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED +# ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED +# define PNG_UNKNOWN_CHUNKS_SUPPORTED +# endif +# ifndef PNG_NO_HANDLE_AS_UNKNOWN +# define PNG_HANDLE_AS_UNKNOWN_SUPPORTED +# endif +#endif +#if !defined(PNG_NO_READ_USER_CHUNKS) && \ + defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) +# define PNG_READ_USER_CHUNKS_SUPPORTED +# define PNG_USER_CHUNKS_SUPPORTED +# ifdef PNG_NO_READ_UNKNOWN_CHUNKS +# undef PNG_NO_READ_UNKNOWN_CHUNKS +# endif +# ifdef PNG_NO_HANDLE_AS_UNKNOWN +# undef PNG_NO_HANDLE_AS_UNKNOWN +# endif +#endif +#ifndef PNG_NO_READ_OPT_PLTE +# define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */ +#endif /* optional PLTE chunk in RGB and RGBA images */ +#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \ + defined(PNG_READ_zTXt_SUPPORTED) +# define PNG_READ_TEXT_SUPPORTED +# define PNG_TEXT_SUPPORTED +#endif + +#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */ + +#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED + +#ifdef PNG_NO_WRITE_TEXT +# define PNG_NO_WRITE_iTXt +# define PNG_NO_WRITE_tEXt +# define PNG_NO_WRITE_zTXt +#endif +#ifndef PNG_NO_WRITE_bKGD +# define PNG_WRITE_bKGD_SUPPORTED +# ifndef PNG_bKGD_SUPPORTED +# define PNG_bKGD_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_cHRM +# define PNG_WRITE_cHRM_SUPPORTED +# ifndef PNG_cHRM_SUPPORTED +# define PNG_cHRM_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_gAMA +# define PNG_WRITE_gAMA_SUPPORTED +# ifndef PNG_gAMA_SUPPORTED +# define PNG_gAMA_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_hIST +# define PNG_WRITE_hIST_SUPPORTED +# ifndef PNG_hIST_SUPPORTED +# define PNG_hIST_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_iCCP +# define PNG_WRITE_iCCP_SUPPORTED +# ifndef PNG_iCCP_SUPPORTED +# define PNG_iCCP_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_iTXt +# ifndef PNG_WRITE_iTXt_SUPPORTED +# define PNG_WRITE_iTXt_SUPPORTED +# endif +# ifndef PNG_iTXt_SUPPORTED +# define PNG_iTXt_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_oFFs +# define PNG_WRITE_oFFs_SUPPORTED +# ifndef PNG_oFFs_SUPPORTED +# define PNG_oFFs_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_pCAL +# define PNG_WRITE_pCAL_SUPPORTED +# ifndef PNG_pCAL_SUPPORTED +# define PNG_pCAL_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_sCAL +# define PNG_WRITE_sCAL_SUPPORTED +# ifndef PNG_sCAL_SUPPORTED +# define PNG_sCAL_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_pHYs +# define PNG_WRITE_pHYs_SUPPORTED +# ifndef PNG_pHYs_SUPPORTED +# define PNG_pHYs_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_sBIT +# define PNG_WRITE_sBIT_SUPPORTED +# ifndef PNG_sBIT_SUPPORTED +# define PNG_sBIT_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_sPLT +# define PNG_WRITE_sPLT_SUPPORTED +# ifndef PNG_sPLT_SUPPORTED +# define PNG_sPLT_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_sRGB +# define PNG_WRITE_sRGB_SUPPORTED +# ifndef PNG_sRGB_SUPPORTED +# define PNG_sRGB_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_tEXt +# define PNG_WRITE_tEXt_SUPPORTED +# ifndef PNG_tEXt_SUPPORTED +# define PNG_tEXt_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_tIME +# define PNG_WRITE_tIME_SUPPORTED +# ifndef PNG_tIME_SUPPORTED +# define PNG_tIME_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_tRNS +# define PNG_WRITE_tRNS_SUPPORTED +# ifndef PNG_tRNS_SUPPORTED +# define PNG_tRNS_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_zTXt +# define PNG_WRITE_zTXt_SUPPORTED +# ifndef PNG_zTXt_SUPPORTED +# define PNG_zTXt_SUPPORTED +# endif +#endif +#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS +# define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED +# ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED +# define PNG_UNKNOWN_CHUNKS_SUPPORTED +# endif +# ifndef PNG_NO_HANDLE_AS_UNKNOWN +# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +# define PNG_HANDLE_AS_UNKNOWN_SUPPORTED +# endif +# endif +#endif +#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \ + defined(PNG_WRITE_zTXt_SUPPORTED) +# define PNG_WRITE_TEXT_SUPPORTED +# ifndef PNG_TEXT_SUPPORTED +# define PNG_TEXT_SUPPORTED +# endif +#endif + +#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */ + +/* Turn this off to disable png_read_png() and + * png_write_png() and leave the row_pointers member + * out of the info structure. + */ +#ifndef PNG_NO_INFO_IMAGE +# define PNG_INFO_IMAGE_SUPPORTED +#endif + +/* need the time information for reading tIME chunks */ +#if defined(PNG_tIME_SUPPORTED) +# if !defined(_WIN32_WCE) + /* "time.h" functions are not supported on WindowsCE */ +# include +# endif +#endif + +/* Some typedefs to get us started. These should be safe on most of the + * common platforms. The typedefs should be at least as large as the + * numbers suggest (a png_uint_32 must be at least 32 bits long), but they + * don't have to be exactly that size. Some compilers dislike passing + * unsigned shorts as function parameters, so you may be better off using + * unsigned int for png_uint_16. Likewise, for 64-bit systems, you may + * want to have unsigned int for png_uint_32 instead of unsigned long. + */ + +typedef unsigned long png_uint_32; +typedef long png_int_32; +typedef unsigned short png_uint_16; +typedef short png_int_16; +typedef unsigned char png_byte; + +/* This is usually size_t. It is typedef'ed just in case you need it to + change (I'm not sure if you will or not, so I thought I'd be safe) */ +#ifdef PNG_SIZE_T + typedef PNG_SIZE_T png_size_t; +# define png_sizeof(x) png_convert_size(sizeof (x)) +#else + typedef size_t png_size_t; +# define png_sizeof(x) sizeof (x) +#endif + +/* The following is needed for medium model support. It cannot be in the + * PNG_INTERNAL section. Needs modification for other compilers besides + * MSC. Model independent support declares all arrays and pointers to be + * large using the far keyword. The zlib version used must also support + * model independent data. As of version zlib 1.0.4, the necessary changes + * have been made in zlib. The USE_FAR_KEYWORD define triggers other + * changes that are needed. (Tim Wegner) + */ + +/* Separate compiler dependencies (problem here is that zlib.h always + defines FAR. (SJT) */ +#ifdef __BORLANDC__ +# if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__) +# define LDATA 1 +# else +# define LDATA 0 +# endif + /* GRR: why is Cygwin in here? Cygwin is not Borland C... */ +# if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__) +# define PNG_MAX_MALLOC_64K +# if (LDATA != 1) +# ifndef FAR +# define FAR __far +# endif +# define USE_FAR_KEYWORD +# endif /* LDATA != 1 */ + /* Possibly useful for moving data out of default segment. + * Uncomment it if you want. Could also define FARDATA as + * const if your compiler supports it. (SJT) +# define FARDATA FAR + */ +# endif /* __WIN32__, __FLAT__, __CYGWIN__ */ +#endif /* __BORLANDC__ */ + + +/* Suggest testing for specific compiler first before testing for + * FAR. The Watcom compiler defines both __MEDIUM__ and M_I86MM, + * making reliance oncertain keywords suspect. (SJT) + */ + +/* MSC Medium model */ +#if defined(FAR) +# if defined(M_I86MM) +# define USE_FAR_KEYWORD +# define FARDATA FAR +# include +# endif +#endif + +/* SJT: default case */ +#ifndef FAR +# define FAR +#endif + +/* At this point FAR is always defined */ +#ifndef FARDATA +# define FARDATA +#endif + +/* Typedef for floating-point numbers that are converted + to fixed-point with a multiple of 100,000, e.g., int_gamma */ +typedef png_int_32 png_fixed_point; + +/* Add typedefs for pointers */ +typedef void FAR * png_voidp; +typedef png_byte FAR * png_bytep; +typedef png_uint_32 FAR * png_uint_32p; +typedef png_int_32 FAR * png_int_32p; +typedef png_uint_16 FAR * png_uint_16p; +typedef png_int_16 FAR * png_int_16p; +typedef PNG_CONST char FAR * png_const_charp; +typedef char FAR * png_charp; +typedef png_fixed_point FAR * png_fixed_point_p; + +#ifndef PNG_NO_STDIO +#if defined(_WIN32_WCE) +typedef HANDLE png_FILE_p; +#else +typedef FILE * png_FILE_p; +#endif +#endif + +#ifdef PNG_FLOATING_POINT_SUPPORTED +typedef double FAR * png_doublep; +#endif + +/* Pointers to pointers; i.e. arrays */ +typedef png_byte FAR * FAR * png_bytepp; +typedef png_uint_32 FAR * FAR * png_uint_32pp; +typedef png_int_32 FAR * FAR * png_int_32pp; +typedef png_uint_16 FAR * FAR * png_uint_16pp; +typedef png_int_16 FAR * FAR * png_int_16pp; +typedef PNG_CONST char FAR * FAR * png_const_charpp; +typedef char FAR * FAR * png_charpp; +typedef png_fixed_point FAR * FAR * png_fixed_point_pp; +#ifdef PNG_FLOATING_POINT_SUPPORTED +typedef double FAR * FAR * png_doublepp; +#endif + +/* Pointers to pointers to pointers; i.e., pointer to array */ +typedef char FAR * FAR * FAR * png_charppp; + +#if defined(PNG_1_0_X) || defined(PNG_1_2_X) +/* SPC - Is this stuff deprecated? */ +/* It'll be removed as of libpng-1.3.0 - GR-P */ +/* libpng typedefs for types in zlib. If zlib changes + * or another compression library is used, then change these. + * Eliminates need to change all the source files. + */ +typedef charf * png_zcharp; +typedef charf * FAR * png_zcharpp; +typedef z_stream FAR * png_zstreamp; +#endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */ + +/* + * Define PNG_BUILD_DLL if the module being built is a Windows + * LIBPNG DLL. + * + * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL. + * It is equivalent to Microsoft predefined macro _DLL that is + * automatically defined when you compile using the share + * version of the CRT (C Run-Time library) + * + * The cygwin mods make this behavior a little different: + * Define PNG_BUILD_DLL if you are building a dll for use with cygwin + * Define PNG_STATIC if you are building a static library for use with cygwin, + * -or- if you are building an application that you want to link to the + * static library. + * PNG_USE_DLL is defined by default (no user action needed) unless one of + * the other flags is defined. + */ + +#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL)) +# define PNG_DLL +#endif +/* If CYGWIN, then disallow GLOBAL ARRAYS unless building a static lib. + * When building a static lib, default to no GLOBAL ARRAYS, but allow + * command-line override + */ +#if defined(__CYGWIN__) +# if !defined(PNG_STATIC) +# if defined(PNG_USE_GLOBAL_ARRAYS) +# undef PNG_USE_GLOBAL_ARRAYS +# endif +# if !defined(PNG_USE_LOCAL_ARRAYS) +# define PNG_USE_LOCAL_ARRAYS +# endif +# else +# if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS) +# if defined(PNG_USE_GLOBAL_ARRAYS) +# undef PNG_USE_GLOBAL_ARRAYS +# endif +# endif +# endif +# if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS) +# define PNG_USE_LOCAL_ARRAYS +# endif +#endif + +/* Do not use global arrays (helps with building DLL's) + * They are no longer used in libpng itself, since version 1.0.5c, + * but might be required for some pre-1.0.5c applications. + */ +#if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS) +# if defined(PNG_NO_GLOBAL_ARRAYS) || (defined(__GNUC__) && defined(PNG_DLL)) +# define PNG_USE_LOCAL_ARRAYS +# else +# define PNG_USE_GLOBAL_ARRAYS +# endif +#endif + +#if defined(__CYGWIN__) +# undef PNGAPI +# define PNGAPI __cdecl +# undef PNG_IMPEXP +# define PNG_IMPEXP +#endif + +/* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall", + * you may get warnings regarding the linkage of png_zalloc and png_zfree. + * Don't ignore those warnings; you must also reset the default calling + * convention in your compiler to match your PNGAPI, and you must build + * zlib and your applications the same way you build libpng. + */ + +#if defined(__MINGW32__) && !defined(PNG_MODULEDEF) +# ifndef PNG_NO_MODULEDEF +# define PNG_NO_MODULEDEF +# endif +#endif + +#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF) +# define PNG_IMPEXP +#endif + +#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \ + (( defined(_Windows) || defined(_WINDOWS) || \ + defined(WIN32) || defined(_WIN32) || defined(__WIN32__) )) + +# ifndef PNGAPI +# if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800)) +# define PNGAPI __cdecl +# else +# define PNGAPI _cdecl +# endif +# endif + +# if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \ + 0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */) +# define PNG_IMPEXP +# endif + +# if !defined(PNG_IMPEXP) + +# define PNG_EXPORT_TYPE1(type,symbol) PNG_IMPEXP type PNGAPI symbol +# define PNG_EXPORT_TYPE2(type,symbol) type PNG_IMPEXP PNGAPI symbol + + /* Borland/Microsoft */ +# if defined(_MSC_VER) || defined(__BORLANDC__) +# if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500) +# define PNG_EXPORT PNG_EXPORT_TYPE1 +# else +# define PNG_EXPORT PNG_EXPORT_TYPE2 +# if defined(PNG_BUILD_DLL) +# define PNG_IMPEXP __export +# else +# define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in + VC++ */ +# endif /* Exists in Borland C++ for + C++ classes (== huge) */ +# endif +# endif + +# if !defined(PNG_IMPEXP) +# if defined(PNG_BUILD_DLL) +# define PNG_IMPEXP __declspec(dllexport) +# else +# define PNG_IMPEXP __declspec(dllimport) +# endif +# endif +# endif /* PNG_IMPEXP */ +#else /* !(DLL || non-cygwin WINDOWS) */ +# if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__) +# ifndef PNGAPI +# define PNGAPI _System +# endif +# else +# if 0 /* ... other platforms, with other meanings */ +# endif +# endif +#endif + +#ifndef PNGAPI +# define PNGAPI +#endif +#ifndef PNG_IMPEXP +# define PNG_IMPEXP +#endif + +#ifdef PNG_BUILDSYMS +# ifndef PNG_EXPORT +# define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END +# endif +# ifdef PNG_USE_GLOBAL_ARRAYS +# ifndef PNG_EXPORT_VAR +# define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT +# endif +# endif +#endif + +#ifndef PNG_EXPORT +# define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol +#endif + +#ifdef PNG_USE_GLOBAL_ARRAYS +# ifndef PNG_EXPORT_VAR +# define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type +# endif +#endif + +/* User may want to use these so they are not in PNG_INTERNAL. Any library + * functions that are passed far data must be model independent. + */ + +#ifndef PNG_ABORT +# define PNG_ABORT() abort() +#endif + +#ifdef PNG_SETJMP_SUPPORTED +# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) +#else +# define png_jmpbuf(png_ptr) \ + (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED) +#endif + +#if defined(USE_FAR_KEYWORD) /* memory model independent fns */ +/* use this to make far-to-near assignments */ +# define CHECK 1 +# define NOCHECK 0 +# define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK)) +# define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK)) +# define png_strcpy _fstrcpy +# define png_strncpy _fstrncpy /* Added to v 1.2.6 */ +# define png_strlen _fstrlen +# define png_memcmp _fmemcmp /* SJT: added */ +# define png_memcpy _fmemcpy +# define png_memset _fmemset +#else /* use the usual functions */ +# define CVT_PTR(ptr) (ptr) +# define CVT_PTR_NOCHECK(ptr) (ptr) +# define png_strcpy strcpy +# define png_strncpy strncpy /* Added to v 1.2.6 */ +# define png_strlen strlen +# define png_memcmp memcmp /* SJT: added */ +# define png_memcpy memcpy +# define png_memset memset +#endif +/* End of memory model independent support */ + +/* Just a little check that someone hasn't tried to define something + * contradictory. + */ +#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K) +# undef PNG_ZBUF_SIZE +# define PNG_ZBUF_SIZE 65536L +#endif + +#ifdef PNG_READ_SUPPORTED +/* Prior to libpng-1.0.9, this block was in pngasmrd.h */ +#if defined(PNG_INTERNAL) + +/* These are the default thresholds before the MMX code kicks in; if either + * rowbytes or bitdepth is below the threshold, plain C code is used. These + * can be overridden at runtime via the png_set_mmx_thresholds() call in + * libpng 1.2.0 and later. The values below were chosen by Intel. + */ + +#ifndef PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT +# define PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT 128 /* >= */ +#endif +#ifndef PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT +# define PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT 9 /* >= */ +#endif + +/* Set this in the makefile for VC++ on Pentium, not here. */ +/* Platform must be Pentium. Makefile must assemble and load pngvcrd.c . + * MMX will be detected at run time and used if present. + */ +#ifdef PNG_USE_PNGVCRD +# define PNG_HAVE_ASSEMBLER_COMBINE_ROW +# define PNG_HAVE_ASSEMBLER_READ_INTERLACE +# define PNG_HAVE_ASSEMBLER_READ_FILTER_ROW +#endif + +/* Set this in the makefile for gcc/as on Pentium, not here. */ +/* Platform must be Pentium. Makefile must assemble and load pnggccrd.c . + * MMX will be detected at run time and used if present. + */ +#ifdef PNG_USE_PNGGCCRD +# define PNG_HAVE_ASSEMBLER_COMBINE_ROW +# define PNG_HAVE_ASSEMBLER_READ_INTERLACE +# define PNG_HAVE_ASSEMBLER_READ_FILTER_ROW +#endif +/* - see pnggccrd.c for info about what is currently enabled */ + +#endif /* PNG_INTERNAL */ +#endif /* PNG_READ_SUPPORTED */ + +/* Added at libpng-1.2.8 */ +#endif /* PNG_VERSION_INFO_ONLY */ + +#endif /* PNGCONF_H */ diff --git a/libpng/pngerror.c b/libpng/pngerror.c new file mode 100644 index 00000000..6fa40123 --- /dev/null +++ b/libpng/pngerror.c @@ -0,0 +1,295 @@ + +/* pngerror.c - stub functions for i/o and memory allocation + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file provides a location for all error handling. Users who + * need special error handling are expected to write replacement functions + * and use png_set_error_fn() to use those functions. See the instructions + * at each function. + */ + +#define PNG_INTERNAL +#include "png.h" + +static void /* PRIVATE */ +png_default_error PNGARG((png_structp png_ptr, + png_const_charp error_message)); +static void /* PRIVATE */ +png_default_warning PNGARG((png_structp png_ptr, + png_const_charp warning_message)); + +/* This function is called whenever there is a fatal error. This function + * should not be changed. If there is a need to handle errors differently, + * you should supply a replacement error function and use png_set_error_fn() + * to replace the error function at run-time. + */ +void PNGAPI +png_error(png_structp png_ptr, png_const_charp error_message) +{ +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + char msg[16]; + if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) + { + if (*error_message == '#') + { + int offset; + for (offset=1; offset<15; offset++) + if (*(error_message+offset) == ' ') + break; + if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) + { + int i; + for (i=0; iflags&PNG_FLAG_STRIP_ERROR_TEXT) + { + msg[0]='0'; + msg[1]='\0'; + error_message=msg; + } + } + } +#endif + if (png_ptr != NULL && png_ptr->error_fn != NULL) + (*(png_ptr->error_fn))(png_ptr, error_message); + + /* If the custom handler doesn't exist, or if it returns, + use the default handler, which will not return. */ + png_default_error(png_ptr, error_message); +} + +/* This function is called whenever there is a non-fatal error. This function + * should not be changed. If there is a need to handle warnings differently, + * you should supply a replacement warning function and use + * png_set_error_fn() to replace the warning function at run-time. + */ +void PNGAPI +png_warning(png_structp png_ptr, png_const_charp warning_message) +{ + int offset = 0; +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) +#endif + { + if (*warning_message == '#') + { + for (offset=1; offset<15; offset++) + if (*(warning_message+offset) == ' ') + break; + } + } + if (png_ptr != NULL && png_ptr->warning_fn != NULL) + (*(png_ptr->warning_fn))(png_ptr, warning_message+offset); + else + png_default_warning(png_ptr, warning_message+offset); +} + +/* These utilities are used internally to build an error message that relates + * to the current chunk. The chunk name comes from png_ptr->chunk_name, + * this is used to prefix the message. The message is limited in length + * to 63 bytes, the name characters are output as hex digits wrapped in [] + * if the character is invalid. + */ +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) +static PNG_CONST char png_digit[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F' +}; + +static void /* PRIVATE */ +png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp + error_message) +{ + int iout = 0, iin = 0; + + while (iin < 4) + { + int c = png_ptr->chunk_name[iin++]; + if (isnonalpha(c)) + { + buffer[iout++] = '['; + buffer[iout++] = png_digit[(c & 0xf0) >> 4]; + buffer[iout++] = png_digit[c & 0x0f]; + buffer[iout++] = ']'; + } + else + { + buffer[iout++] = (png_byte)c; + } + } + + if (error_message == NULL) + buffer[iout] = 0; + else + { + buffer[iout++] = ':'; + buffer[iout++] = ' '; + png_strncpy(buffer+iout, error_message, 63); + buffer[iout+63] = 0; + } +} + +void PNGAPI +png_chunk_error(png_structp png_ptr, png_const_charp error_message) +{ + char msg[18+64]; + png_format_buffer(png_ptr, msg, error_message); + png_error(png_ptr, msg); +} + +void PNGAPI +png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) +{ + char msg[18+64]; + png_format_buffer(png_ptr, msg, warning_message); + png_warning(png_ptr, msg); +} + +/* This is the default error handling function. Note that replacements for + * this function MUST NOT RETURN, or the program will likely crash. This + * function is used by default, or if the program supplies NULL for the + * error function pointer in png_set_error_fn(). + */ +static void /* PRIVATE */ +png_default_error(png_structp png_ptr, png_const_charp error_message) +{ +#ifndef PNG_NO_CONSOLE_IO +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + if (*error_message == '#') + { + int offset; + char error_number[16]; + for (offset=0; offset<15; offset++) + { + error_number[offset] = *(error_message+offset+1); + if (*(error_message+offset) == ' ') + break; + } + if((offset > 1) && (offset < 15)) + { + error_number[offset-1]='\0'; + fprintf(stderr, "libpng error no. %s: %s\n", error_number, + error_message+offset); + } + else + fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset); + } + else +#endif + fprintf(stderr, "libpng error: %s\n", error_message); +#endif + +#ifdef PNG_SETJMP_SUPPORTED +# ifdef USE_FAR_KEYWORD + { + jmp_buf jmpbuf; + png_memcpy(jmpbuf,png_ptr->jmpbuf,png_sizeof(jmp_buf)); + longjmp(jmpbuf, 1); + } +# else + longjmp(png_ptr->jmpbuf, 1); +# endif +#else + /* make compiler happy */ ; + if (png_ptr) + PNG_ABORT(); +#endif +#ifdef PNG_NO_CONSOLE_IO + /* make compiler happy */ ; + if (&error_message != NULL) + return; +#endif +} + +/* This function is called when there is a warning, but the library thinks + * it can continue anyway. Replacement functions don't have to do anything + * here if you don't want them to. In the default configuration, png_ptr is + * not used, but it is passed in case it may be useful. + */ +static void /* PRIVATE */ +png_default_warning(png_structp png_ptr, png_const_charp warning_message) +{ +#ifndef PNG_NO_CONSOLE_IO +# ifdef PNG_ERROR_NUMBERS_SUPPORTED + if (*warning_message == '#') + { + int offset; + char warning_number[16]; + for (offset=0; offset<15; offset++) + { + warning_number[offset]=*(warning_message+offset+1); + if (*(warning_message+offset) == ' ') + break; + } + if((offset > 1) && (offset < 15)) + { + warning_number[offset-1]='\0'; + fprintf(stderr, "libpng warning no. %s: %s\n", warning_number, + warning_message+offset); + } + else + fprintf(stderr, "libpng warning: %s\n", warning_message); + } + else +# endif + fprintf(stderr, "libpng warning: %s\n", warning_message); +#else + /* make compiler happy */ ; + if (warning_message) + return; +#endif + /* make compiler happy */ ; + if (png_ptr) + return; +} + +/* This function is called when the application wants to use another method + * of handling errors and warnings. Note that the error function MUST NOT + * return to the calling routine or serious problems will occur. The return + * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1) + */ +void PNGAPI +png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warning_fn) +{ + png_ptr->error_ptr = error_ptr; + png_ptr->error_fn = error_fn; + png_ptr->warning_fn = warning_fn; +} + + +/* This function returns a pointer to the error_ptr associated with the user + * functions. The application should free any memory associated with this + * pointer before png_write_destroy and png_read_destroy are called. + */ +png_voidp PNGAPI +png_get_error_ptr(png_structp png_ptr) +{ + return ((png_voidp)png_ptr->error_ptr); +} + + +#ifdef PNG_ERROR_NUMBERS_SUPPORTED +void PNGAPI +png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) +{ + if(png_ptr != NULL) + { + png_ptr->flags &= + ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); + } +} +#endif diff --git a/libpng/pngget.c b/libpng/pngget.c new file mode 100644 index 00000000..8eefa777 --- /dev/null +++ b/libpng/pngget.c @@ -0,0 +1,934 @@ + +/* pngget.c - retrieval of values from info struct + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#include "png.h" + +png_uint_32 PNGAPI +png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag) +{ + if (png_ptr != NULL && info_ptr != NULL) + return(info_ptr->valid & flag); + else + return(0); +} + +png_uint_32 PNGAPI +png_get_rowbytes(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + return(info_ptr->rowbytes); + else + return(0); +} + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +png_bytepp PNGAPI +png_get_rows(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + return(info_ptr->row_pointers); + else + return(0); +} +#endif + +#ifdef PNG_EASY_ACCESS_SUPPORTED +/* easy access to info, added in libpng-0.99 */ +png_uint_32 PNGAPI +png_get_image_width(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->width; + } + return (0); +} + +png_uint_32 PNGAPI +png_get_image_height(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->height; + } + return (0); +} + +png_byte PNGAPI +png_get_bit_depth(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->bit_depth; + } + return (0); +} + +png_byte PNGAPI +png_get_color_type(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->color_type; + } + return (0); +} + +png_byte PNGAPI +png_get_filter_type(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->filter_type; + } + return (0); +} + +png_byte PNGAPI +png_get_interlace_type(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->interlace_type; + } + return (0); +} + +png_byte PNGAPI +png_get_compression_type(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + { + return info_ptr->compression_type; + } + return (0); +} + +png_uint_32 PNGAPI +png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_pHYs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pHYs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter"); + if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER) + return (0); + else return (info_ptr->x_pixels_per_unit); + } +#else + return (0); +#endif + return (0); +} + +png_uint_32 PNGAPI +png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_pHYs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pHYs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter"); + if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER) + return (0); + else return (info_ptr->y_pixels_per_unit); + } +#else + return (0); +#endif + return (0); +} + +png_uint_32 PNGAPI +png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_pHYs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pHYs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter"); + if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER || + info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit) + return (0); + else return (info_ptr->x_pixels_per_unit); + } +#else + return (0); +#endif + return (0); +} + +#ifdef PNG_FLOATING_POINT_SUPPORTED +float PNGAPI +png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr) + { + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_pHYs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pHYs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio"); + if (info_ptr->x_pixels_per_unit == 0) + return ((float)0.0); + else + return ((float)((float)info_ptr->y_pixels_per_unit + /(float)info_ptr->x_pixels_per_unit)); + } +#else + return (0.0); +#endif + return ((float)0.0); +} +#endif + +png_int_32 PNGAPI +png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_oFFs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_oFFs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns"); + if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER) + return (0); + else return (info_ptr->x_offset); + } +#else + return (0); +#endif + return (0); +} + +png_int_32 PNGAPI +png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_oFFs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_oFFs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns"); + if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER) + return (0); + else return (info_ptr->y_offset); + } +#else + return (0); +#endif + return (0); +} + +png_int_32 PNGAPI +png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_oFFs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_oFFs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns"); + if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL) + return (0); + else return (info_ptr->x_offset); + } +#else + return (0); +#endif + return (0); +} + +png_int_32 PNGAPI +png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) +#if defined(PNG_oFFs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_oFFs) + { + png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns"); + if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL) + return (0); + else return (info_ptr->y_offset); + } +#else + return (0); +#endif + return (0); +} + +#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED) +png_uint_32 PNGAPI +png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) +{ + return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr) + *.0254 +.5)); +} + +png_uint_32 PNGAPI +png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) +{ + return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr) + *.0254 +.5)); +} + +png_uint_32 PNGAPI +png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) +{ + return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr) + *.0254 +.5)); +} + +float PNGAPI +png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr) +{ + return ((float)png_get_x_offset_microns(png_ptr, info_ptr) + *.00003937); +} + +float PNGAPI +png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr) +{ + return ((float)png_get_y_offset_microns(png_ptr, info_ptr) + *.00003937); +} + +#if defined(PNG_pHYs_SUPPORTED) +png_uint_32 PNGAPI +png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr, + png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) +{ + png_uint_32 retval = 0; + + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + { + png_debug1(1, "in %s retrieval function\n", "pHYs"); + if (res_x != NULL) + { + *res_x = info_ptr->x_pixels_per_unit; + retval |= PNG_INFO_pHYs; + } + if (res_y != NULL) + { + *res_y = info_ptr->y_pixels_per_unit; + retval |= PNG_INFO_pHYs; + } + if (unit_type != NULL) + { + *unit_type = (int)info_ptr->phys_unit_type; + retval |= PNG_INFO_pHYs; + if(*unit_type == 1) + { + if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50); + if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50); + } + } + } + return (retval); +} +#endif /* PNG_pHYs_SUPPORTED */ +#endif /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */ + +/* png_get_channels really belongs in here, too, but it's been around longer */ + +#endif /* PNG_EASY_ACCESS_SUPPORTED */ + +png_byte PNGAPI +png_get_channels(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + return(info_ptr->channels); + else + return (0); +} + +png_bytep PNGAPI +png_get_signature(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr != NULL && info_ptr != NULL) + return(info_ptr->signature); + else + return (NULL); +} + +#if defined(PNG_bKGD_SUPPORTED) +png_uint_32 PNGAPI +png_get_bKGD(png_structp png_ptr, png_infop info_ptr, + png_color_16p *background) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) + && background != NULL) + { + png_debug1(1, "in %s retrieval function\n", "bKGD"); + *background = &(info_ptr->background); + return (PNG_INFO_bKGD); + } + return (0); +} +#endif + +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_cHRM(png_structp png_ptr, png_infop info_ptr, + double *white_x, double *white_y, double *red_x, double *red_y, + double *green_x, double *green_y, double *blue_x, double *blue_y) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) + { + png_debug1(1, "in %s retrieval function\n", "cHRM"); + if (white_x != NULL) + *white_x = (double)info_ptr->x_white; + if (white_y != NULL) + *white_y = (double)info_ptr->y_white; + if (red_x != NULL) + *red_x = (double)info_ptr->x_red; + if (red_y != NULL) + *red_y = (double)info_ptr->y_red; + if (green_x != NULL) + *green_x = (double)info_ptr->x_green; + if (green_y != NULL) + *green_y = (double)info_ptr->y_green; + if (blue_x != NULL) + *blue_x = (double)info_ptr->x_blue; + if (blue_y != NULL) + *blue_y = (double)info_ptr->y_blue; + return (PNG_INFO_cHRM); + } + return (0); +} +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr, + png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x, + png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y, + png_fixed_point *blue_x, png_fixed_point *blue_y) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) + { + png_debug1(1, "in %s retrieval function\n", "cHRM"); + if (white_x != NULL) + *white_x = info_ptr->int_x_white; + if (white_y != NULL) + *white_y = info_ptr->int_y_white; + if (red_x != NULL) + *red_x = info_ptr->int_x_red; + if (red_y != NULL) + *red_y = info_ptr->int_y_red; + if (green_x != NULL) + *green_x = info_ptr->int_x_green; + if (green_y != NULL) + *green_y = info_ptr->int_y_green; + if (blue_x != NULL) + *blue_x = info_ptr->int_x_blue; + if (blue_y != NULL) + *blue_y = info_ptr->int_y_blue; + return (PNG_INFO_cHRM); + } + return (0); +} +#endif +#endif + +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) + && file_gamma != NULL) + { + png_debug1(1, "in %s retrieval function\n", "gAMA"); + *file_gamma = (double)info_ptr->gamma; + return (PNG_INFO_gAMA); + } + return (0); +} +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, + png_fixed_point *int_file_gamma) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) + && int_file_gamma != NULL) + { + png_debug1(1, "in %s retrieval function\n", "gAMA"); + *int_file_gamma = info_ptr->int_gamma; + return (PNG_INFO_gAMA); + } + return (0); +} +#endif +#endif + +#if defined(PNG_sRGB_SUPPORTED) +png_uint_32 PNGAPI +png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB) + && file_srgb_intent != NULL) + { + png_debug1(1, "in %s retrieval function\n", "sRGB"); + *file_srgb_intent = (int)info_ptr->srgb_intent; + return (PNG_INFO_sRGB); + } + return (0); +} +#endif + +#if defined(PNG_iCCP_SUPPORTED) +png_uint_32 PNGAPI +png_get_iCCP(png_structp png_ptr, png_infop info_ptr, + png_charpp name, int *compression_type, + png_charpp profile, png_uint_32 *proflen) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP) + && name != NULL && profile != NULL && proflen != NULL) + { + png_debug1(1, "in %s retrieval function\n", "iCCP"); + *name = info_ptr->iccp_name; + *profile = info_ptr->iccp_profile; + /* compression_type is a dummy so the API won't have to change + if we introduce multiple compression types later. */ + *proflen = (int)info_ptr->iccp_proflen; + *compression_type = (int)info_ptr->iccp_compression; + return (PNG_INFO_iCCP); + } + return (0); +} +#endif + +#if defined(PNG_sPLT_SUPPORTED) +png_uint_32 PNGAPI +png_get_sPLT(png_structp png_ptr, png_infop info_ptr, + png_sPLT_tpp spalettes) +{ + if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL) + *spalettes = info_ptr->splt_palettes; + return ((png_uint_32)info_ptr->splt_palettes_num); +} +#endif + +#if defined(PNG_hIST_SUPPORTED) +png_uint_32 PNGAPI +png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) + && hist != NULL) + { + png_debug1(1, "in %s retrieval function\n", "hIST"); + *hist = info_ptr->hist; + return (PNG_INFO_hIST); + } + return (0); +} +#endif + +png_uint_32 PNGAPI +png_get_IHDR(png_structp png_ptr, png_infop info_ptr, + png_uint_32 *width, png_uint_32 *height, int *bit_depth, + int *color_type, int *interlace_type, int *compression_type, + int *filter_type) + +{ + if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL && + bit_depth != NULL && color_type != NULL) + { + png_debug1(1, "in %s retrieval function\n", "IHDR"); + *width = info_ptr->width; + *height = info_ptr->height; + *bit_depth = info_ptr->bit_depth; + if (info_ptr->bit_depth < 1 || info_ptr->bit_depth > 16) + png_error(png_ptr, "Invalid bit depth"); + *color_type = info_ptr->color_type; + if (info_ptr->color_type > 6) + png_error(png_ptr, "Invalid color type"); + if (compression_type != NULL) + *compression_type = info_ptr->compression_type; + if (filter_type != NULL) + *filter_type = info_ptr->filter_type; + if (interlace_type != NULL) + *interlace_type = info_ptr->interlace_type; + + /* check for potential overflow of rowbytes */ + if (*width == 0 || *width > PNG_UINT_31_MAX) + png_error(png_ptr, "Invalid image width"); + if (*height == 0 || *height > PNG_UINT_31_MAX) + png_error(png_ptr, "Invalid image height"); + if (info_ptr->width > (PNG_UINT_32_MAX + >> 3) /* 8-byte RGBA pixels */ + - 64 /* bigrowbuf hack */ + - 1 /* filter byte */ + - 7*8 /* rounding of width to multiple of 8 pixels */ + - 8) /* extra max_pixel_depth pad */ + { + png_warning(png_ptr, + "Width too large for libpng to process image data."); + } + return (1); + } + return (0); +} + +#if defined(PNG_oFFs_SUPPORTED) +png_uint_32 PNGAPI +png_get_oFFs(png_structp png_ptr, png_infop info_ptr, + png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) + && offset_x != NULL && offset_y != NULL && unit_type != NULL) + { + png_debug1(1, "in %s retrieval function\n", "oFFs"); + *offset_x = info_ptr->x_offset; + *offset_y = info_ptr->y_offset; + *unit_type = (int)info_ptr->offset_unit_type; + return (PNG_INFO_oFFs); + } + return (0); +} +#endif + +#if defined(PNG_pCAL_SUPPORTED) +png_uint_32 PNGAPI +png_get_pCAL(png_structp png_ptr, png_infop info_ptr, + png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams, + png_charp *units, png_charpp *params) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) + && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL && + nparams != NULL && units != NULL && params != NULL) + { + png_debug1(1, "in %s retrieval function\n", "pCAL"); + *purpose = info_ptr->pcal_purpose; + *X0 = info_ptr->pcal_X0; + *X1 = info_ptr->pcal_X1; + *type = (int)info_ptr->pcal_type; + *nparams = (int)info_ptr->pcal_nparams; + *units = info_ptr->pcal_units; + *params = info_ptr->pcal_params; + return (PNG_INFO_pCAL); + } + return (0); +} +#endif + +#if defined(PNG_sCAL_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_sCAL(png_structp png_ptr, png_infop info_ptr, + int *unit, double *width, double *height) +{ + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_sCAL)) + { + *unit = info_ptr->scal_unit; + *width = info_ptr->scal_pixel_width; + *height = info_ptr->scal_pixel_height; + return (PNG_INFO_sCAL); + } + return(0); +} +#else +#ifdef PNG_FIXED_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr, + int *unit, png_charpp width, png_charpp height) +{ + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_sCAL)) + { + *unit = info_ptr->scal_unit; + *width = info_ptr->scal_s_width; + *height = info_ptr->scal_s_height; + return (PNG_INFO_sCAL); + } + return(0); +} +#endif +#endif +#endif + +#if defined(PNG_pHYs_SUPPORTED) +png_uint_32 PNGAPI +png_get_pHYs(png_structp png_ptr, png_infop info_ptr, + png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) +{ + png_uint_32 retval = 0; + + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs)) + { + png_debug1(1, "in %s retrieval function\n", "pHYs"); + if (res_x != NULL) + { + *res_x = info_ptr->x_pixels_per_unit; + retval |= PNG_INFO_pHYs; + } + if (res_y != NULL) + { + *res_y = info_ptr->y_pixels_per_unit; + retval |= PNG_INFO_pHYs; + } + if (unit_type != NULL) + { + *unit_type = (int)info_ptr->phys_unit_type; + retval |= PNG_INFO_pHYs; + } + } + return (retval); +} +#endif + +png_uint_32 PNGAPI +png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette, + int *num_palette) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE) + && palette != NULL) + { + png_debug1(1, "in %s retrieval function\n", "PLTE"); + *palette = info_ptr->palette; + *num_palette = info_ptr->num_palette; + png_debug1(3, "num_palette = %d\n", *num_palette); + return (PNG_INFO_PLTE); + } + return (0); +} + +#if defined(PNG_sBIT_SUPPORTED) +png_uint_32 PNGAPI +png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) + && sig_bit != NULL) + { + png_debug1(1, "in %s retrieval function\n", "sBIT"); + *sig_bit = &(info_ptr->sig_bit); + return (PNG_INFO_sBIT); + } + return (0); +} +#endif + +#if defined(PNG_TEXT_SUPPORTED) +png_uint_32 PNGAPI +png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr, + int *num_text) +{ + if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0) + { + png_debug1(1, "in %s retrieval function\n", + (png_ptr->chunk_name[0] == '\0' ? "text" + : (png_const_charp)png_ptr->chunk_name)); + if (text_ptr != NULL) + *text_ptr = info_ptr->text; + if (num_text != NULL) + *num_text = info_ptr->num_text; + return ((png_uint_32)info_ptr->num_text); + } + if (num_text != NULL) + *num_text = 0; + return(0); +} +#endif + +#if defined(PNG_tIME_SUPPORTED) +png_uint_32 PNGAPI +png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time) +{ + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) + && mod_time != NULL) + { + png_debug1(1, "in %s retrieval function\n", "tIME"); + *mod_time = &(info_ptr->mod_time); + return (PNG_INFO_tIME); + } + return (0); +} +#endif + +#if defined(PNG_tRNS_SUPPORTED) +png_uint_32 PNGAPI +png_get_tRNS(png_structp png_ptr, png_infop info_ptr, + png_bytep *trans, int *num_trans, png_color_16p *trans_values) +{ + png_uint_32 retval = 0; + if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) + { + png_debug1(1, "in %s retrieval function\n", "tRNS"); + if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (trans != NULL) + { + *trans = info_ptr->trans; + retval |= PNG_INFO_tRNS; + } + if (trans_values != NULL) + *trans_values = &(info_ptr->trans_values); + } + else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */ + { + if (trans_values != NULL) + { + *trans_values = &(info_ptr->trans_values); + retval |= PNG_INFO_tRNS; + } + if(trans != NULL) + *trans = NULL; + } + if(num_trans != NULL) + { + *num_trans = info_ptr->num_trans; + retval |= PNG_INFO_tRNS; + } + } + return (retval); +} +#endif + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +png_uint_32 PNGAPI +png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr, + png_unknown_chunkpp unknowns) +{ + if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL) + *unknowns = info_ptr->unknown_chunks; + return ((png_uint_32)info_ptr->unknown_chunks_num); +} +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +png_byte PNGAPI +png_get_rgb_to_gray_status (png_structp png_ptr) +{ + return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0); +} +#endif + +#if defined(PNG_USER_CHUNKS_SUPPORTED) +png_voidp PNGAPI +png_get_user_chunk_ptr(png_structp png_ptr) +{ + return (png_ptr? png_ptr->user_chunk_ptr : NULL); +} +#endif + +#ifdef PNG_WRITE_SUPPORTED +png_uint_32 PNGAPI +png_get_compression_buffer_size(png_structp png_ptr) +{ + return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L); +} +#endif + +#ifndef PNG_1_0_X +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED +/* this function was added to libpng 1.2.0 and should exist by default */ +png_uint_32 PNGAPI +png_get_asm_flags (png_structp png_ptr) +{ + return (png_uint_32)(png_ptr? png_ptr->asm_flags : 0L); +} + +/* this function was added to libpng 1.2.0 and should exist by default */ +png_uint_32 PNGAPI +png_get_asm_flagmask (int flag_select) +{ + png_uint_32 settable_asm_flags = 0; + + if (flag_select & PNG_SELECT_READ) + settable_asm_flags |= + PNG_ASM_FLAG_MMX_READ_COMBINE_ROW | + PNG_ASM_FLAG_MMX_READ_INTERLACE | + PNG_ASM_FLAG_MMX_READ_FILTER_SUB | + PNG_ASM_FLAG_MMX_READ_FILTER_UP | + PNG_ASM_FLAG_MMX_READ_FILTER_AVG | + PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ; + /* no non-MMX flags yet */ + +#if 0 + /* GRR: no write-flags yet, either, but someday... */ + if (flag_select & PNG_SELECT_WRITE) + settable_asm_flags |= + PNG_ASM_FLAG_MMX_WRITE_ [whatever] ; +#endif /* 0 */ + + return settable_asm_flags; /* _theoretically_ settable capabilities only */ +} +#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */ + + +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) + /* GRR: could add this: && defined(PNG_MMX_CODE_SUPPORTED) */ +/* this function was added to libpng 1.2.0 */ +png_uint_32 PNGAPI +png_get_mmx_flagmask (int flag_select, int *compilerID) +{ + png_uint_32 settable_mmx_flags = 0; + + if (flag_select & PNG_SELECT_READ) + settable_mmx_flags |= + PNG_ASM_FLAG_MMX_READ_COMBINE_ROW | + PNG_ASM_FLAG_MMX_READ_INTERLACE | + PNG_ASM_FLAG_MMX_READ_FILTER_SUB | + PNG_ASM_FLAG_MMX_READ_FILTER_UP | + PNG_ASM_FLAG_MMX_READ_FILTER_AVG | + PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ; +#if 0 + /* GRR: no MMX write support yet, but someday... */ + if (flag_select & PNG_SELECT_WRITE) + settable_mmx_flags |= + PNG_ASM_FLAG_MMX_WRITE_ [whatever] ; +#endif /* 0 */ + + if (compilerID != NULL) { +#ifdef PNG_USE_PNGVCRD + *compilerID = 1; /* MSVC */ +#else +#ifdef PNG_USE_PNGGCCRD + *compilerID = 2; /* gcc/gas */ +#else + *compilerID = -1; /* unknown (i.e., no asm/MMX code compiled) */ +#endif +#endif + } + + return settable_mmx_flags; /* _theoretically_ settable capabilities only */ +} + +/* this function was added to libpng 1.2.0 */ +png_byte PNGAPI +png_get_mmx_bitdepth_threshold (png_structp png_ptr) +{ + return (png_byte)(png_ptr? png_ptr->mmx_bitdepth_threshold : 0); +} + +/* this function was added to libpng 1.2.0 */ +png_uint_32 PNGAPI +png_get_mmx_rowbytes_threshold (png_structp png_ptr) +{ + return (png_uint_32)(png_ptr? png_ptr->mmx_rowbytes_threshold : 0L); +} +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */ + +#ifdef PNG_SET_USER_LIMITS_SUPPORTED +/* these functions were added to libpng 1.2.6 */ +png_uint_32 PNGAPI +png_get_user_width_max (png_structp png_ptr) +{ + return (png_ptr? png_ptr->user_width_max : 0); +} +png_uint_32 PNGAPI +png_get_user_height_max (png_structp png_ptr) +{ + return (png_ptr? png_ptr->user_height_max : 0); +} +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ + +#endif /* ?PNG_1_0_X */ diff --git a/libpng/pngmem.c b/libpng/pngmem.c new file mode 100644 index 00000000..f1cb6939 --- /dev/null +++ b/libpng/pngmem.c @@ -0,0 +1,595 @@ + +/* pngmem.c - stub functions for memory allocation + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file provides a location for all memory allocation. Users who + * need special memory handling are expected to supply replacement + * functions for png_malloc() and png_free(), and to use + * png_create_read_struct_2() and png_create_write_struct_2() to + * identify the replacement functions. + */ + +#define PNG_INTERNAL +#include "png.h" + +/* Borland DOS special memory handler */ +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) +/* if you change this, be sure to change the one in png.h also */ + +/* Allocate memory for a png_struct. The malloc and memset can be replaced + by a single call to calloc() if this is thought to improve performance. */ +png_voidp /* PRIVATE */ +png_create_struct(int type) +{ +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); +} + +/* Alternate version of png_create_struct, for use with user-defined malloc. */ +png_voidp /* PRIVATE */ +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + png_size_t size; + png_voidp struct_ptr; + + if (type == PNG_STRUCT_INFO) + size = png_sizeof(png_info); + else if (type == PNG_STRUCT_PNG) + size = png_sizeof(png_struct); + else + return (png_get_copyright(NULL)); + +#ifdef PNG_USER_MEM_SUPPORTED + if(malloc_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + png_ptr->mem_ptr=mem_ptr; + struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); + } + else +#endif /* PNG_USER_MEM_SUPPORTED */ + struct_ptr = (png_voidp)farmalloc(size); + if (struct_ptr != NULL) + png_memset(struct_ptr, 0, size); + return (struct_ptr); +} + +/* Free memory allocated by a png_create_struct() call */ +void /* PRIVATE */ +png_destroy_struct(png_voidp struct_ptr) +{ +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); +} + +/* Free memory allocated by a png_create_struct() call */ +void /* PRIVATE */ +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, + png_voidp mem_ptr) +{ +#endif + if (struct_ptr != NULL) + { +#ifdef PNG_USER_MEM_SUPPORTED + if(free_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + png_ptr->mem_ptr=mem_ptr; + (*(free_fn))(png_ptr, struct_ptr); + return; + } +#endif /* PNG_USER_MEM_SUPPORTED */ + farfree (struct_ptr); + } +} + +/* Allocate memory. For reasonable files, size should never exceed + * 64K. However, zlib may allocate more then 64K if you don't tell + * it not to. See zconf.h and png.h for more information. zlib does + * need to allocate exactly 64K, so whatever you call here must + * have the ability to do that. + * + * Borland seems to have a problem in DOS mode for exactly 64K. + * It gives you a segment with an offset of 8 (perhaps to store its + * memory stuff). zlib doesn't like this at all, so we have to + * detect and deal with it. This code should not be needed in + * Windows or OS/2 modes, and only in 16 bit mode. This code has + * been updated by Alexander Lehmann for version 0.89 to waste less + * memory. + * + * Note that we can't use png_size_t for the "size" declaration, + * since on some systems a png_size_t is a 16-bit quantity, and as a + * result, we would be truncating potentially larger memory requests + * (which should cause a fatal error) and introducing major problems. + */ + +png_voidp PNGAPI +png_malloc(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; + + if (png_ptr == NULL || size == 0) + return (NULL); + +#ifdef PNG_USER_MEM_SUPPORTED + if(png_ptr->malloc_fn != NULL) + ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); + else + ret = (png_malloc_default(png_ptr, size)); + if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out of memory!"); + return (ret); +} + +png_voidp PNGAPI +png_malloc_default(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; +#endif /* PNG_USER_MEM_SUPPORTED */ + +#ifdef PNG_MAX_MALLOC_64K + if (size > (png_uint_32)65536L) + { + png_warning(png_ptr, "Cannot Allocate > 64K"); + ret = NULL; + } + else +#endif + + if (size != (size_t)size) + ret = NULL; + else if (size == (png_uint_32)65536L) + { + if (png_ptr->offset_table == NULL) + { + /* try to see if we need to do any of this fancy stuff */ + ret = farmalloc(size); + if (ret == NULL || ((png_size_t)ret & 0xffff)) + { + int num_blocks; + png_uint_32 total_size; + png_bytep table; + int i; + png_byte huge * hptr; + + if (ret != NULL) + { + farfree(ret); + ret = NULL; + } + + if(png_ptr->zlib_window_bits > 14) + num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); + else + num_blocks = 1; + if (png_ptr->zlib_mem_level >= 7) + num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); + else + num_blocks++; + + total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; + + table = farmalloc(total_size); + + if (table == NULL) + { +#ifndef PNG_USER_MEM_SUPPORTED + if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ + else + png_warning(png_ptr, "Out Of Memory."); +#endif + return (NULL); + } + + if ((png_size_t)table & 0xfff0) + { +#ifndef PNG_USER_MEM_SUPPORTED + if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, + "Farmalloc didn't return normalized pointer"); + else + png_warning(png_ptr, + "Farmalloc didn't return normalized pointer"); +#endif + return (NULL); + } + + png_ptr->offset_table = table; + png_ptr->offset_table_ptr = farmalloc(num_blocks * + png_sizeof (png_bytep)); + + if (png_ptr->offset_table_ptr == NULL) + { +#ifndef PNG_USER_MEM_SUPPORTED + if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */ + else + png_warning(png_ptr, "Out Of memory."); +#endif + return (NULL); + } + + hptr = (png_byte huge *)table; + if ((png_size_t)hptr & 0xf) + { + hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); + hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ + } + for (i = 0; i < num_blocks; i++) + { + png_ptr->offset_table_ptr[i] = (png_bytep)hptr; + hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ + } + + png_ptr->offset_table_number = num_blocks; + png_ptr->offset_table_count = 0; + png_ptr->offset_table_count_free = 0; + } + } + + if (png_ptr->offset_table_count >= png_ptr->offset_table_number) + { +#ifndef PNG_USER_MEM_SUPPORTED + if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ + else + png_warning(png_ptr, "Out of Memory."); +#endif + return (NULL); + } + + ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; + } + else + ret = farmalloc(size); + +#ifndef PNG_USER_MEM_SUPPORTED + if (ret == NULL) + { + if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ + else + png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */ + } +#endif + + return (ret); +} + +/* free a pointer allocated by png_malloc(). In the default + configuration, png_ptr is not used, but is passed in case it + is needed. If ptr is NULL, return without taking any action. */ +void PNGAPI +png_free(png_structp png_ptr, png_voidp ptr) +{ + if (png_ptr == NULL || ptr == NULL) + return; + +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr->free_fn != NULL) + { + (*(png_ptr->free_fn))(png_ptr, ptr); + return; + } + else png_free_default(png_ptr, ptr); +} + +void PNGAPI +png_free_default(png_structp png_ptr, png_voidp ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + + if (png_ptr->offset_table != NULL) + { + int i; + + for (i = 0; i < png_ptr->offset_table_count; i++) + { + if (ptr == png_ptr->offset_table_ptr[i]) + { + ptr = NULL; + png_ptr->offset_table_count_free++; + break; + } + } + if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) + { + farfree(png_ptr->offset_table); + farfree(png_ptr->offset_table_ptr); + png_ptr->offset_table = NULL; + png_ptr->offset_table_ptr = NULL; + } + } + + if (ptr != NULL) + { + farfree(ptr); + } +} + +#else /* Not the Borland DOS special memory handler */ + +/* Allocate memory for a png_struct or a png_info. The malloc and + memset can be replaced by a single call to calloc() if this is thought + to improve performance noticably. */ +png_voidp /* PRIVATE */ +png_create_struct(int type) +{ +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); +} + +/* Allocate memory for a png_struct or a png_info. The malloc and + memset can be replaced by a single call to calloc() if this is thought + to improve performance noticably. */ +png_voidp /* PRIVATE */ +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + png_size_t size; + png_voidp struct_ptr; + + if (type == PNG_STRUCT_INFO) + size = png_sizeof(png_info); + else if (type == PNG_STRUCT_PNG) + size = png_sizeof(png_struct); + else + return (NULL); + +#ifdef PNG_USER_MEM_SUPPORTED + if(malloc_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + png_ptr->mem_ptr=mem_ptr; + struct_ptr = (*(malloc_fn))(png_ptr, size); + if (struct_ptr != NULL) + png_memset(struct_ptr, 0, size); + return (struct_ptr); + } +#endif /* PNG_USER_MEM_SUPPORTED */ + +#if defined(__TURBOC__) && !defined(__FLAT__) + struct_ptr = (png_voidp)farmalloc(size); +#else +# if defined(_MSC_VER) && defined(MAXSEG_64K) + struct_ptr = (png_voidp)halloc(size,1); +# else + struct_ptr = (png_voidp)malloc(size); +# endif +#endif + if (struct_ptr != NULL) + png_memset(struct_ptr, 0, size); + + return (struct_ptr); +} + + +/* Free memory allocated by a png_create_struct() call */ +void /* PRIVATE */ +png_destroy_struct(png_voidp struct_ptr) +{ +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); +} + +/* Free memory allocated by a png_create_struct() call */ +void /* PRIVATE */ +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, + png_voidp mem_ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + if (struct_ptr != NULL) + { +#ifdef PNG_USER_MEM_SUPPORTED + if(free_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + png_ptr->mem_ptr=mem_ptr; + (*(free_fn))(png_ptr, struct_ptr); + return; + } +#endif /* PNG_USER_MEM_SUPPORTED */ +#if defined(__TURBOC__) && !defined(__FLAT__) + farfree(struct_ptr); +#else +# if defined(_MSC_VER) && defined(MAXSEG_64K) + hfree(struct_ptr); +# else + free(struct_ptr); +# endif +#endif + } +} + +/* Allocate memory. For reasonable files, size should never exceed + 64K. However, zlib may allocate more then 64K if you don't tell + it not to. See zconf.h and png.h for more information. zlib does + need to allocate exactly 64K, so whatever you call here must + have the ability to do that. */ + +png_voidp PNGAPI +png_malloc(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; + +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr == NULL || size == 0) + return (NULL); + + if(png_ptr->malloc_fn != NULL) + ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); + else + ret = (png_malloc_default(png_ptr, size)); + if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out of Memory!"); + return (ret); +} + +png_voidp PNGAPI +png_malloc_default(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; +#endif /* PNG_USER_MEM_SUPPORTED */ + + if (png_ptr == NULL || size == 0) + return (NULL); + +#ifdef PNG_MAX_MALLOC_64K + if (size > (png_uint_32)65536L) + { +#ifndef PNG_USER_MEM_SUPPORTED + if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Cannot Allocate > 64K"); + else +#endif + return NULL; + } +#endif + + /* Check for overflow */ +#if defined(__TURBOC__) && !defined(__FLAT__) + if (size != (unsigned long)size) + ret = NULL; + else + ret = farmalloc(size); +#else +# if defined(_MSC_VER) && defined(MAXSEG_64K) + if (size != (unsigned long)size) + ret = NULL; + else + ret = halloc(size, 1); +# else + if (size != (size_t)size) + ret = NULL; + else + ret = malloc((size_t)size); +# endif +#endif + +#ifndef PNG_USER_MEM_SUPPORTED + if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) + png_error(png_ptr, "Out of Memory"); +#endif + + return (ret); +} + +/* Free a pointer allocated by png_malloc(). If ptr is NULL, return + without taking any action. */ +void PNGAPI +png_free(png_structp png_ptr, png_voidp ptr) +{ + if (png_ptr == NULL || ptr == NULL) + return; + +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr->free_fn != NULL) + { + (*(png_ptr->free_fn))(png_ptr, ptr); + return; + } + else png_free_default(png_ptr, ptr); +} +void PNGAPI +png_free_default(png_structp png_ptr, png_voidp ptr) +{ + if (png_ptr == NULL || ptr == NULL) + return; + +#endif /* PNG_USER_MEM_SUPPORTED */ + +#if defined(__TURBOC__) && !defined(__FLAT__) + farfree(ptr); +#else +# if defined(_MSC_VER) && defined(MAXSEG_64K) + hfree(ptr); +# else + free(ptr); +# endif +#endif +} + +#endif /* Not Borland DOS special memory handler */ + +#if defined(PNG_1_0_X) +# define png_malloc_warn png_malloc +#else +/* This function was added at libpng version 1.2.3. The png_malloc_warn() + * function will set up png_malloc() to issue a png_warning and return NULL + * instead of issuing a png_error, if it fails to allocate the requested + * memory. + */ +png_voidp PNGAPI +png_malloc_warn(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ptr; + png_uint_32 save_flags=png_ptr->flags; + + png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; + ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); + png_ptr->flags=save_flags; + return(ptr); +} +#endif + +png_voidp PNGAPI +png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, + png_uint_32 length) +{ + png_size_t size; + + size = (png_size_t)length; + if ((png_uint_32)size != length) + png_error(png_ptr,"Overflow in png_memcpy_check."); + + return(png_memcpy (s1, s2, size)); +} + +png_voidp PNGAPI +png_memset_check (png_structp png_ptr, png_voidp s1, int value, + png_uint_32 length) +{ + png_size_t size; + + size = (png_size_t)length; + if ((png_uint_32)size != length) + png_error(png_ptr,"Overflow in png_memset_check."); + + return (png_memset (s1, value, size)); + +} + +#ifdef PNG_USER_MEM_SUPPORTED +/* This function is called when the application wants to use another method + * of allocating and freeing memory. + */ +void PNGAPI +png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr + malloc_fn, png_free_ptr free_fn) +{ + png_ptr->mem_ptr = mem_ptr; + png_ptr->malloc_fn = malloc_fn; + png_ptr->free_fn = free_fn; +} + +/* This function returns a pointer to the mem_ptr associated with the user + * functions. The application should free any memory associated with this + * pointer before png_write_destroy and png_read_destroy are called. + */ +png_voidp PNGAPI +png_get_mem_ptr(png_structp png_ptr) +{ + return ((png_voidp)png_ptr->mem_ptr); +} +#endif /* PNG_USER_MEM_SUPPORTED */ diff --git a/libpng/pngpread.c b/libpng/pngpread.c new file mode 100644 index 00000000..8c35faae --- /dev/null +++ b/libpng/pngpread.c @@ -0,0 +1,1573 @@ + +/* pngpread.c - read a png file in push mode + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#include "png.h" + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED + +/* push model modes */ +#define PNG_READ_SIG_MODE 0 +#define PNG_READ_CHUNK_MODE 1 +#define PNG_READ_IDAT_MODE 2 +#define PNG_SKIP_MODE 3 +#define PNG_READ_tEXt_MODE 4 +#define PNG_READ_zTXt_MODE 5 +#define PNG_READ_DONE_MODE 6 +#define PNG_READ_iTXt_MODE 7 +#define PNG_ERROR_MODE 8 + +void PNGAPI +png_process_data(png_structp png_ptr, png_infop info_ptr, + png_bytep buffer, png_size_t buffer_size) +{ + png_push_restore_buffer(png_ptr, buffer, buffer_size); + + while (png_ptr->buffer_size) + { + png_process_some_data(png_ptr, info_ptr); + } +} + +/* What we do with the incoming data depends on what we were previously + * doing before we ran out of data... + */ +void /* PRIVATE */ +png_process_some_data(png_structp png_ptr, png_infop info_ptr) +{ + switch (png_ptr->process_mode) + { + case PNG_READ_SIG_MODE: + { + png_push_read_sig(png_ptr, info_ptr); + break; + } + case PNG_READ_CHUNK_MODE: + { + png_push_read_chunk(png_ptr, info_ptr); + break; + } + case PNG_READ_IDAT_MODE: + { + png_push_read_IDAT(png_ptr); + break; + } +#if defined(PNG_READ_tEXt_SUPPORTED) + case PNG_READ_tEXt_MODE: + { + png_push_read_tEXt(png_ptr, info_ptr); + break; + } +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + case PNG_READ_zTXt_MODE: + { + png_push_read_zTXt(png_ptr, info_ptr); + break; + } +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + case PNG_READ_iTXt_MODE: + { + png_push_read_iTXt(png_ptr, info_ptr); + break; + } +#endif + case PNG_SKIP_MODE: + { + png_push_crc_finish(png_ptr); + break; + } + default: + { + png_ptr->buffer_size = 0; + break; + } + } +} + +/* Read any remaining signature bytes from the stream and compare them with + * the correct PNG signature. It is possible that this routine is called + * with bytes already read from the signature, either because they have been + * checked by the calling application, or because of multiple calls to this + * routine. + */ +void /* PRIVATE */ +png_push_read_sig(png_structp png_ptr, png_infop info_ptr) +{ + png_size_t num_checked = png_ptr->sig_bytes, + num_to_check = 8 - num_checked; + + if (png_ptr->buffer_size < num_to_check) + { + num_to_check = png_ptr->buffer_size; + } + + png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), + num_to_check); + png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check); + + if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) + { + if (num_checked < 4 && + png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) + png_error(png_ptr, "Not a PNG file"); + else + png_error(png_ptr, "PNG file corrupted by ASCII conversion"); + } + else + { + if (png_ptr->sig_bytes >= 8) + { + png_ptr->process_mode = PNG_READ_CHUNK_MODE; + } + } +} + +void /* PRIVATE */ +png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IHDR; + PNG_IDAT; + PNG_IEND; + PNG_PLTE; +#if defined(PNG_READ_bKGD_SUPPORTED) + PNG_bKGD; +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + PNG_cHRM; +#endif +#if defined(PNG_READ_gAMA_SUPPORTED) + PNG_gAMA; +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + PNG_hIST; +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + PNG_iCCP; +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + PNG_iTXt; +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + PNG_oFFs; +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + PNG_pCAL; +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + PNG_pHYs; +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + PNG_sBIT; +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + PNG_sCAL; +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + PNG_sRGB; +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + PNG_sPLT; +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + PNG_tEXt; +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + PNG_tIME; +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + PNG_tRNS; +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + PNG_zTXt; +#endif +#endif /* PNG_USE_LOCAL_ARRAYS */ + /* First we make sure we have enough data for the 4 byte chunk name + * and the 4 byte chunk length before proceeding with decoding the + * chunk data. To fully decode each of these chunks, we also make + * sure we have enough data in the buffer for the 4 byte CRC at the + * end of every chunk (except IDAT, which is handled separately). + */ + if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) + { + png_byte chunk_length[4]; + + if (png_ptr->buffer_size < 8) + { + png_push_save_buffer(png_ptr); + return; + } + + png_push_fill_buffer(png_ptr, chunk_length, 4); + png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length); + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; + } + + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); + } + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); + + png_ptr->process_mode = PNG_READ_DONE_MODE; + png_push_have_end(png_ptr, info_ptr); + } +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + png_ptr->mode |= PNG_HAVE_IDAT; + png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); + if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_ptr->mode |= PNG_HAVE_PLTE; + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + { + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before IDAT"); + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + !(png_ptr->mode & PNG_HAVE_PLTE)) + png_error(png_ptr, "Missing PLTE before IDAT"); + } + } +#endif + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); + } + else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4)) + { + /* If we reach an IDAT chunk, this means we have read all of the + * header chunks, and we can start reading the image (or if this + * is called after the image has been read - we have an error). + */ + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before IDAT"); + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + !(png_ptr->mode & PNG_HAVE_PLTE)) + png_error(png_ptr, "Missing PLTE before IDAT"); + + if (png_ptr->mode & PNG_HAVE_IDAT) + { + if (png_ptr->push_length == 0) + return; + + if (png_ptr->mode & PNG_AFTER_IDAT) + png_error(png_ptr, "Too many IDAT's found"); + } + + png_ptr->idat_size = png_ptr->push_length; + png_ptr->mode |= PNG_HAVE_IDAT; + png_ptr->process_mode = PNG_READ_IDAT_MODE; + png_push_have_info(png_ptr, info_ptr); + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; + png_ptr->zstream.next_out = png_ptr->row_buf; + return; + } +#if defined(PNG_READ_gAMA_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_bKGD_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); + } +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length); + } +#endif + else + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } + png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); + } + + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; +} + +void /* PRIVATE */ +png_push_crc_skip(png_structp png_ptr, png_uint_32 skip) +{ + png_ptr->process_mode = PNG_SKIP_MODE; + png_ptr->skip_length = skip; +} + +void /* PRIVATE */ +png_push_crc_finish(png_structp png_ptr) +{ + if (png_ptr->skip_length && png_ptr->save_buffer_size) + { + png_size_t save_size; + + if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size) + save_size = (png_size_t)png_ptr->skip_length; + else + save_size = png_ptr->save_buffer_size; + + png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); + + png_ptr->skip_length -= save_size; + png_ptr->buffer_size -= save_size; + png_ptr->save_buffer_size -= save_size; + png_ptr->save_buffer_ptr += save_size; + } + if (png_ptr->skip_length && png_ptr->current_buffer_size) + { + png_size_t save_size; + + if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size) + save_size = (png_size_t)png_ptr->skip_length; + else + save_size = png_ptr->current_buffer_size; + + png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); + + png_ptr->skip_length -= save_size; + png_ptr->buffer_size -= save_size; + png_ptr->current_buffer_size -= save_size; + png_ptr->current_buffer_ptr += save_size; + } + if (!png_ptr->skip_length) + { + if (png_ptr->buffer_size < 4) + { + png_push_save_buffer(png_ptr); + return; + } + + png_crc_finish(png_ptr, 0); + png_ptr->process_mode = PNG_READ_CHUNK_MODE; + } +} + +void PNGAPI +png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) +{ + png_bytep ptr; + + ptr = buffer; + if (png_ptr->save_buffer_size) + { + png_size_t save_size; + + if (length < png_ptr->save_buffer_size) + save_size = length; + else + save_size = png_ptr->save_buffer_size; + + png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); + length -= save_size; + ptr += save_size; + png_ptr->buffer_size -= save_size; + png_ptr->save_buffer_size -= save_size; + png_ptr->save_buffer_ptr += save_size; + } + if (length && png_ptr->current_buffer_size) + { + png_size_t save_size; + + if (length < png_ptr->current_buffer_size) + save_size = length; + else + save_size = png_ptr->current_buffer_size; + + png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); + png_ptr->buffer_size -= save_size; + png_ptr->current_buffer_size -= save_size; + png_ptr->current_buffer_ptr += save_size; + } +} + +void /* PRIVATE */ +png_push_save_buffer(png_structp png_ptr) +{ + if (png_ptr->save_buffer_size) + { + if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) + { + png_size_t i,istop; + png_bytep sp; + png_bytep dp; + + istop = png_ptr->save_buffer_size; + for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; + i < istop; i++, sp++, dp++) + { + *dp = *sp; + } + } + } + if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > + png_ptr->save_buffer_max) + { + png_size_t new_max; + png_bytep old_buffer; + + if (png_ptr->save_buffer_size > PNG_SIZE_MAX - + (png_ptr->current_buffer_size + 256)) + { + png_error(png_ptr, "Potential overflow of save_buffer"); + } + new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; + old_buffer = png_ptr->save_buffer; + png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr, + (png_uint_32)new_max); + png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); + png_free(png_ptr, old_buffer); + png_ptr->save_buffer_max = new_max; + } + if (png_ptr->current_buffer_size) + { + png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, + png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); + png_ptr->save_buffer_size += png_ptr->current_buffer_size; + png_ptr->current_buffer_size = 0; + } + png_ptr->save_buffer_ptr = png_ptr->save_buffer; + png_ptr->buffer_size = 0; +} + +void /* PRIVATE */ +png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, + png_size_t buffer_length) +{ + png_ptr->current_buffer = buffer; + png_ptr->current_buffer_size = buffer_length; + png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; + png_ptr->current_buffer_ptr = png_ptr->current_buffer; +} + +void /* PRIVATE */ +png_push_read_IDAT(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IDAT; +#endif + if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) + { + png_byte chunk_length[4]; + + if (png_ptr->buffer_size < 8) + { + png_push_save_buffer(png_ptr); + return; + } + + png_push_fill_buffer(png_ptr, chunk_length, 4); + png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length); + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; + + if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4)) + { + png_ptr->process_mode = PNG_READ_CHUNK_MODE; + if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + png_error(png_ptr, "Not enough compressed data"); + return; + } + + png_ptr->idat_size = png_ptr->push_length; + } + if (png_ptr->idat_size && png_ptr->save_buffer_size) + { + png_size_t save_size; + + if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size) + { + save_size = (png_size_t)png_ptr->idat_size; + /* check for overflow */ + if((png_uint_32)save_size != png_ptr->idat_size) + png_error(png_ptr, "save_size overflowed in pngpread"); + } + else + save_size = png_ptr->save_buffer_size; + + png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); + if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); + png_ptr->idat_size -= save_size; + png_ptr->buffer_size -= save_size; + png_ptr->save_buffer_size -= save_size; + png_ptr->save_buffer_ptr += save_size; + } + if (png_ptr->idat_size && png_ptr->current_buffer_size) + { + png_size_t save_size; + + if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) + { + save_size = (png_size_t)png_ptr->idat_size; + /* check for overflow */ + if((png_uint_32)save_size != png_ptr->idat_size) + png_error(png_ptr, "save_size overflowed in pngpread"); + } + else + save_size = png_ptr->current_buffer_size; + + png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); + if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); + + png_ptr->idat_size -= save_size; + png_ptr->buffer_size -= save_size; + png_ptr->current_buffer_size -= save_size; + png_ptr->current_buffer_ptr += save_size; + } + if (!png_ptr->idat_size) + { + if (png_ptr->buffer_size < 4) + { + png_push_save_buffer(png_ptr); + return; + } + + png_crc_finish(png_ptr, 0); + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; + png_ptr->mode |= PNG_AFTER_IDAT; + } +} + +void /* PRIVATE */ +png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, + png_size_t buffer_length) +{ + int ret; + + if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length) + png_error(png_ptr, "Extra compression data"); + + png_ptr->zstream.next_in = buffer; + png_ptr->zstream.avail_in = (uInt)buffer_length; + for(;;) + { + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); + if (ret != Z_OK) + { + if (ret == Z_STREAM_END) + { + if (png_ptr->zstream.avail_in) + png_error(png_ptr, "Extra compressed data"); + if (!(png_ptr->zstream.avail_out)) + { + png_push_process_row(png_ptr); + } + + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + break; + } + else if (ret == Z_BUF_ERROR) + break; + else + png_error(png_ptr, "Decompression Error"); + } + if (!(png_ptr->zstream.avail_out)) + { + if (( +#if defined(PNG_READ_INTERLACING_SUPPORTED) + png_ptr->interlaced && png_ptr->pass > 6) || + (!png_ptr->interlaced && +#endif + png_ptr->row_number == png_ptr->num_rows)) + { + if (png_ptr->zstream.avail_in) + png_warning(png_ptr, "Too much data in IDAT chunks"); + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + break; + } + png_push_process_row(png_ptr); + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; + png_ptr->zstream.next_out = png_ptr->row_buf; + } + else + break; + } +} + +void /* PRIVATE */ +png_push_process_row(png_structp png_ptr) +{ + png_ptr->row_info.color_type = png_ptr->color_type; + png_ptr->row_info.width = png_ptr->iwidth; + png_ptr->row_info.channels = png_ptr->channels; + png_ptr->row_info.bit_depth = png_ptr->bit_depth; + png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; + + png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, + png_ptr->row_info.width); + + png_read_filter_row(png_ptr, &(png_ptr->row_info), + png_ptr->row_buf + 1, png_ptr->prev_row + 1, + (int)(png_ptr->row_buf[0])); + + png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, + png_ptr->rowbytes + 1); + + if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) + png_do_read_transformations(png_ptr); + +#if defined(PNG_READ_INTERLACING_SUPPORTED) + /* blow up interlaced rows to full size */ + if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + { + if (png_ptr->pass < 6) +/* old interface (pre-1.0.9): + png_do_read_interlace(&(png_ptr->row_info), + png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); + */ + png_do_read_interlace(png_ptr); + + switch (png_ptr->pass) + { + case 0: + { + int i; + for (i = 0; i < 8 && png_ptr->pass == 0; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */ + } + if (png_ptr->pass == 2) /* pass 1 might be empty */ + { + for (i = 0; i < 4 && png_ptr->pass == 2; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + if (png_ptr->pass == 4 && png_ptr->height <= 4) + { + for (i = 0; i < 2 && png_ptr->pass == 4; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + if (png_ptr->pass == 6 && png_ptr->height <= 4) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + break; + } + case 1: + { + int i; + for (i = 0; i < 8 && png_ptr->pass == 1; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } + if (png_ptr->pass == 2) /* skip top 4 generated rows */ + { + for (i = 0; i < 4 && png_ptr->pass == 2; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + break; + } + case 2: + { + int i; + for (i = 0; i < 4 && png_ptr->pass == 2; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } + for (i = 0; i < 4 && png_ptr->pass == 2; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + if (png_ptr->pass == 4) /* pass 3 might be empty */ + { + for (i = 0; i < 2 && png_ptr->pass == 4; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + break; + } + case 3: + { + int i; + for (i = 0; i < 4 && png_ptr->pass == 3; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } + if (png_ptr->pass == 4) /* skip top two generated rows */ + { + for (i = 0; i < 2 && png_ptr->pass == 4; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + break; + } + case 4: + { + int i; + for (i = 0; i < 2 && png_ptr->pass == 4; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } + for (i = 0; i < 2 && png_ptr->pass == 4; i++) + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + if (png_ptr->pass == 6) /* pass 5 might be empty */ + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + break; + } + case 5: + { + int i; + for (i = 0; i < 2 && png_ptr->pass == 5; i++) + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } + if (png_ptr->pass == 6) /* skip top generated row */ + { + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + break; + } + case 6: + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + if (png_ptr->pass != 6) + break; + png_push_have_row(png_ptr, png_bytep_NULL); + png_read_push_finish_row(png_ptr); + } + } + } + else +#endif + { + png_push_have_row(png_ptr, png_ptr->row_buf + 1); + png_read_push_finish_row(png_ptr); + } +} + +void /* PRIVATE */ +png_read_push_finish_row(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; + + /* start of interlace block in the y direction */ + const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; + + /* offset to next interlace block in the y direction */ + const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; + + /* Width of interlace block. This is not currently used - if you need + * it, uncomment it here and in png.h + const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1}; + */ + + /* Height of interlace block. This is not currently used - if you need + * it, uncomment it here and in png.h + const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; + */ +#endif + + png_ptr->row_number++; + if (png_ptr->row_number < png_ptr->num_rows) + return; + + if (png_ptr->interlaced) + { + png_ptr->row_number = 0; + png_memset_check(png_ptr, png_ptr->prev_row, 0, + png_ptr->rowbytes + 1); + do + { + png_ptr->pass++; + if ((png_ptr->pass == 1 && png_ptr->width < 5) || + (png_ptr->pass == 3 && png_ptr->width < 3) || + (png_ptr->pass == 5 && png_ptr->width < 2)) + png_ptr->pass++; + + if (png_ptr->pass > 7) + png_ptr->pass--; + if (png_ptr->pass >= 7) + break; + + png_ptr->iwidth = (png_ptr->width + + png_pass_inc[png_ptr->pass] - 1 - + png_pass_start[png_ptr->pass]) / + png_pass_inc[png_ptr->pass]; + + png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, + png_ptr->iwidth) + 1; + + if (png_ptr->transformations & PNG_INTERLACE) + break; + + png_ptr->num_rows = (png_ptr->height + + png_pass_yinc[png_ptr->pass] - 1 - + png_pass_ystart[png_ptr->pass]) / + png_pass_yinc[png_ptr->pass]; + + } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); + } +} + +#if defined(PNG_READ_tEXt_SUPPORTED) +void /* PRIVATE */ +png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 + length) +{ + if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) + { + png_error(png_ptr, "Out of place tEXt"); + /* to quiet some compiler warnings */ + if(info_ptr == NULL) return; + } + +#ifdef PNG_MAX_MALLOC_64K + png_ptr->skip_length = 0; /* This may not be necessary */ + + if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */ + { + png_warning(png_ptr, "tEXt chunk too large to fit in memory"); + png_ptr->skip_length = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + png_ptr->current_text = (png_charp)png_malloc(png_ptr, + (png_uint_32)(length+1)); + png_ptr->current_text[length] = '\0'; + png_ptr->current_text_ptr = png_ptr->current_text; + png_ptr->current_text_size = (png_size_t)length; + png_ptr->current_text_left = (png_size_t)length; + png_ptr->process_mode = PNG_READ_tEXt_MODE; +} + +void /* PRIVATE */ +png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr->buffer_size && png_ptr->current_text_left) + { + png_size_t text_size; + + if (png_ptr->buffer_size < png_ptr->current_text_left) + text_size = png_ptr->buffer_size; + else + text_size = png_ptr->current_text_left; + png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); + png_ptr->current_text_left -= text_size; + png_ptr->current_text_ptr += text_size; + } + if (!(png_ptr->current_text_left)) + { + png_textp text_ptr; + png_charp text; + png_charp key; + int ret; + + if (png_ptr->buffer_size < 4) + { + png_push_save_buffer(png_ptr); + return; + } + + png_push_crc_finish(png_ptr); + +#if defined(PNG_MAX_MALLOC_64K) + if (png_ptr->skip_length) + return; +#endif + + key = png_ptr->current_text; + + for (text = key; *text; text++) + /* empty loop */ ; + + if (text != key + png_ptr->current_text_size) + text++; + + text_ptr = (png_textp)png_malloc(png_ptr, + (png_uint_32)png_sizeof(png_text)); + text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; + text_ptr->key = key; +#ifdef PNG_iTXt_SUPPORTED + text_ptr->lang = NULL; + text_ptr->lang_key = NULL; +#endif + text_ptr->text = text; + + ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_free(png_ptr, key); + png_free(png_ptr, text_ptr); + png_ptr->current_text = NULL; + + if (ret) + png_warning(png_ptr, "Insufficient memory to store text chunk."); + } +} +#endif + +#if defined(PNG_READ_zTXt_SUPPORTED) +void /* PRIVATE */ +png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 + length) +{ + if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) + { + png_error(png_ptr, "Out of place zTXt"); + /* to quiet some compiler warnings */ + if(info_ptr == NULL) return; + } + +#ifdef PNG_MAX_MALLOC_64K + /* We can't handle zTXt chunks > 64K, since we don't have enough space + * to be able to store the uncompressed data. Actually, the threshold + * is probably around 32K, but it isn't as definite as 64K is. + */ + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "zTXt chunk too large to fit in memory"); + png_push_crc_skip(png_ptr, length); + return; + } +#endif + + png_ptr->current_text = (png_charp)png_malloc(png_ptr, + (png_uint_32)(length+1)); + png_ptr->current_text[length] = '\0'; + png_ptr->current_text_ptr = png_ptr->current_text; + png_ptr->current_text_size = (png_size_t)length; + png_ptr->current_text_left = (png_size_t)length; + png_ptr->process_mode = PNG_READ_zTXt_MODE; +} + +void /* PRIVATE */ +png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr->buffer_size && png_ptr->current_text_left) + { + png_size_t text_size; + + if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left) + text_size = png_ptr->buffer_size; + else + text_size = png_ptr->current_text_left; + png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); + png_ptr->current_text_left -= text_size; + png_ptr->current_text_ptr += text_size; + } + if (!(png_ptr->current_text_left)) + { + png_textp text_ptr; + png_charp text; + png_charp key; + int ret; + png_size_t text_size, key_size; + + if (png_ptr->buffer_size < 4) + { + png_push_save_buffer(png_ptr); + return; + } + + png_push_crc_finish(png_ptr); + + key = png_ptr->current_text; + + for (text = key; *text; text++) + /* empty loop */ ; + + /* zTXt can't have zero text */ + if (text == key + png_ptr->current_text_size) + { + png_ptr->current_text = NULL; + png_free(png_ptr, key); + return; + } + + text++; + + if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */ + { + png_ptr->current_text = NULL; + png_free(png_ptr, key); + return; + } + + text++; + + png_ptr->zstream.next_in = (png_bytep )text; + png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size - + (text - key)); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + + key_size = text - key; + text_size = 0; + text = NULL; + ret = Z_STREAM_END; + + while (png_ptr->zstream.avail_in) + { + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); + if (ret != Z_OK && ret != Z_STREAM_END) + { + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; + png_ptr->current_text = NULL; + png_free(png_ptr, key); + png_free(png_ptr, text); + return; + } + if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END) + { + if (text == NULL) + { + text = (png_charp)png_malloc(png_ptr, + (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out + + key_size + 1)); + png_memcpy(text + key_size, png_ptr->zbuf, + png_ptr->zbuf_size - png_ptr->zstream.avail_out); + png_memcpy(text, key, key_size); + text_size = key_size + png_ptr->zbuf_size - + png_ptr->zstream.avail_out; + *(text + text_size) = '\0'; + } + else + { + png_charp tmp; + + tmp = text; + text = (png_charp)png_malloc(png_ptr, text_size + + (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out + + 1)); + png_memcpy(text, tmp, text_size); + png_free(png_ptr, tmp); + png_memcpy(text + text_size, png_ptr->zbuf, + png_ptr->zbuf_size - png_ptr->zstream.avail_out); + text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; + *(text + text_size) = '\0'; + } + if (ret != Z_STREAM_END) + { + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + } + } + else + { + break; + } + + if (ret == Z_STREAM_END) + break; + } + + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; + + if (ret != Z_STREAM_END) + { + png_ptr->current_text = NULL; + png_free(png_ptr, key); + png_free(png_ptr, text); + return; + } + + png_ptr->current_text = NULL; + png_free(png_ptr, key); + key = text; + text += key_size; + + text_ptr = (png_textp)png_malloc(png_ptr, + (png_uint_32)png_sizeof(png_text)); + text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt; + text_ptr->key = key; +#ifdef PNG_iTXt_SUPPORTED + text_ptr->lang = NULL; + text_ptr->lang_key = NULL; +#endif + text_ptr->text = text; + + ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_free(png_ptr, key); + png_free(png_ptr, text_ptr); + + if (ret) + png_warning(png_ptr, "Insufficient memory to store text chunk."); + } +} +#endif + +#if defined(PNG_READ_iTXt_SUPPORTED) +void /* PRIVATE */ +png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 + length) +{ + if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) + { + png_error(png_ptr, "Out of place iTXt"); + /* to quiet some compiler warnings */ + if(info_ptr == NULL) return; + } + +#ifdef PNG_MAX_MALLOC_64K + png_ptr->skip_length = 0; /* This may not be necessary */ + + if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */ + { + png_warning(png_ptr, "iTXt chunk too large to fit in memory"); + png_ptr->skip_length = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + png_ptr->current_text = (png_charp)png_malloc(png_ptr, + (png_uint_32)(length+1)); + png_ptr->current_text[length] = '\0'; + png_ptr->current_text_ptr = png_ptr->current_text; + png_ptr->current_text_size = (png_size_t)length; + png_ptr->current_text_left = (png_size_t)length; + png_ptr->process_mode = PNG_READ_iTXt_MODE; +} + +void /* PRIVATE */ +png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr) +{ + + if (png_ptr->buffer_size && png_ptr->current_text_left) + { + png_size_t text_size; + + if (png_ptr->buffer_size < png_ptr->current_text_left) + text_size = png_ptr->buffer_size; + else + text_size = png_ptr->current_text_left; + png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); + png_ptr->current_text_left -= text_size; + png_ptr->current_text_ptr += text_size; + } + if (!(png_ptr->current_text_left)) + { + png_textp text_ptr; + png_charp key; + int comp_flag; + png_charp lang; + png_charp lang_key; + png_charp text; + int ret; + + if (png_ptr->buffer_size < 4) + { + png_push_save_buffer(png_ptr); + return; + } + + png_push_crc_finish(png_ptr); + +#if defined(PNG_MAX_MALLOC_64K) + if (png_ptr->skip_length) + return; +#endif + + key = png_ptr->current_text; + + for (lang = key; *lang; lang++) + /* empty loop */ ; + + if (lang != key + png_ptr->current_text_size) + lang++; + + comp_flag = *lang++; + lang++; /* skip comp_type, always zero */ + + for (lang_key = lang; *lang_key; lang_key++) + /* empty loop */ ; + lang_key++; /* skip NUL separator */ + + for (text = lang_key; *text; text++) + /* empty loop */ ; + + if (text != key + png_ptr->current_text_size) + text++; + + text_ptr = (png_textp)png_malloc(png_ptr, + (png_uint_32)png_sizeof(png_text)); + text_ptr->compression = comp_flag + 2; + text_ptr->key = key; + text_ptr->lang = lang; + text_ptr->lang_key = lang_key; + text_ptr->text = text; + text_ptr->text_length = 0; + text_ptr->itxt_length = png_strlen(text); + + ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_ptr->current_text = NULL; + + png_free(png_ptr, text_ptr); + if (ret) + png_warning(png_ptr, "Insufficient memory to store iTXt chunk."); + } +} +#endif + +/* This function is called when we haven't found a handler for this + * chunk. If there isn't a problem with the chunk itself (ie a bad chunk + * name or a critical chunk), the chunk is (currently) silently ignored. + */ +void /* PRIVATE */ +png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 + length) +{ + png_uint_32 skip=0; + png_check_chunk_name(png_ptr, png_ptr->chunk_name); + + if (!(png_ptr->chunk_name[0] & 0x20)) + { +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != + PNG_HANDLE_CHUNK_ALWAYS +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) + && png_ptr->read_user_chunk_fn == NULL +#endif + ) +#endif + png_chunk_error(png_ptr, "unknown critical chunk"); + + /* to quiet compiler warnings about unused info_ptr */ + if (info_ptr == NULL) + return; + } + +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) + { + png_unknown_chunk chunk; + +#ifdef PNG_MAX_MALLOC_64K + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "unknown chunk too large to fit in memory"); + skip = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name); + chunk.data = (png_bytep)png_malloc(png_ptr, length); + png_crc_read(png_ptr, chunk.data, length); + chunk.size = length; +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) + if(png_ptr->read_user_chunk_fn != NULL) + { + /* callback to user unknown chunk handler */ + if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0) + { + if (!(png_ptr->chunk_name[0] & 0x20)) + if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != + PNG_HANDLE_CHUNK_ALWAYS) + png_chunk_error(png_ptr, "unknown critical chunk"); + } + png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); + } + else +#endif + png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); + png_free(png_ptr, chunk.data); + } + else +#endif + skip=length; + png_push_crc_skip(png_ptr, skip); +} + +void /* PRIVATE */ +png_push_have_info(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr->info_fn != NULL) + (*(png_ptr->info_fn))(png_ptr, info_ptr); +} + +void /* PRIVATE */ +png_push_have_end(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr->end_fn != NULL) + (*(png_ptr->end_fn))(png_ptr, info_ptr); +} + +void /* PRIVATE */ +png_push_have_row(png_structp png_ptr, png_bytep row) +{ + if (png_ptr->row_fn != NULL) + (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, + (int)png_ptr->pass); +} + +void PNGAPI +png_progressive_combine_row (png_structp png_ptr, + png_bytep old_row, png_bytep new_row) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + const int FARDATA png_pass_dsp_mask[7] = + {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; +#endif + if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */ + png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); +} + +void PNGAPI +png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, + png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, + png_progressive_end_ptr end_fn) +{ + png_ptr->info_fn = info_fn; + png_ptr->row_fn = row_fn; + png_ptr->end_fn = end_fn; + + png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); +} + +png_voidp PNGAPI +png_get_progressive_ptr(png_structp png_ptr) +{ + return png_ptr->io_ptr; +} +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ diff --git a/libpng/pngread.c b/libpng/pngread.c new file mode 100644 index 00000000..5924333d --- /dev/null +++ b/libpng/pngread.c @@ -0,0 +1,1456 @@ + +/* pngread.c - read a PNG file + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file contains routines that an application calls directly to + * read a PNG file or stream. + */ + +#define PNG_INTERNAL +#include "png.h" + +/* Create a PNG structure for reading, and allocate any memory needed. */ +png_structp PNGAPI +png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn) +{ + +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, + warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); +} + +/* Alternate create PNG structure for reading, and allocate any memory needed. */ +png_structp PNGAPI +png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, + png_malloc_ptr malloc_fn, png_free_ptr free_fn) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + + png_structp png_ptr; + +#ifdef PNG_SETJMP_SUPPORTED +#ifdef USE_FAR_KEYWORD + jmp_buf jmpbuf; +#endif +#endif + + int i; + + png_debug(1, "in png_create_read_struct\n"); +#ifdef PNG_USER_MEM_SUPPORTED + png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, + (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); +#else + png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); +#endif + if (png_ptr == NULL) + return (NULL); + +#if !defined(PNG_1_0_X) +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED + png_init_mmx_flags(png_ptr); /* 1.2.0 addition */ +#endif +#endif /* PNG_1_0_X */ + + /* added at libpng-1.2.6 */ +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + png_ptr->user_width_max=PNG_USER_WIDTH_MAX; + png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; +#endif + +#ifdef PNG_SETJMP_SUPPORTED +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) +#else + if (setjmp(png_ptr->jmpbuf)) +#endif + { + png_free(png_ptr, png_ptr->zbuf); + png_ptr->zbuf=NULL; +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)png_ptr, + (png_free_ptr)free_fn, (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)png_ptr); +#endif + return (NULL); + } +#ifdef USE_FAR_KEYWORD + png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf)); +#endif +#endif + +#ifdef PNG_USER_MEM_SUPPORTED + png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); +#endif + + png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); + + i=0; + do + { + if(user_png_ver[i] != png_libpng_ver[i]) + png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; + } while (png_libpng_ver[i++]); + + if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) + { + /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so + * we must recompile any applications that use any older library version. + * For versions after libpng 1.0, we will be compatible, so we need + * only check the first digit. + */ + if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || + (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || + (user_png_ver[0] == '0' && user_png_ver[2] < '9')) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char msg[80]; + if (user_png_ver) + { + sprintf(msg, "Application was compiled with png.h from libpng-%.20s", + user_png_ver); + png_warning(png_ptr, msg); + } + sprintf(msg, "Application is running with png.c from libpng-%.20s", + png_libpng_ver); + png_warning(png_ptr, msg); +#endif +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "Incompatible libpng version in application and library"); + } + } + + /* initialize zbuf - compression buffer */ + png_ptr->zbuf_size = PNG_ZBUF_SIZE; + png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, + (png_uint_32)png_ptr->zbuf_size); + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; + + switch (inflateInit(&png_ptr->zstream)) + { + case Z_OK: /* Do nothing */ break; + case Z_MEM_ERROR: + case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; + case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; + default: png_error(png_ptr, "Unknown zlib error"); + } + + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + + png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL); + +#ifdef PNG_SETJMP_SUPPORTED +/* Applications that neglect to set up their own setjmp() and then encounter + a png_error() will longjmp here. Since the jmpbuf is then meaningless we + abort instead of returning. */ +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) + PNG_ABORT(); + png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf)); +#else + if (setjmp(png_ptr->jmpbuf)) + PNG_ABORT(); +#endif +#endif + return (png_ptr); +} + +/* Initialize PNG structure for reading, and allocate any memory needed. + This interface is deprecated in favour of the png_create_read_struct(), + and it will eventually disappear. */ +#if defined(PNG_1_0_X) || defined (PNG_1_2_X) +#undef png_read_init +void PNGAPI +png_read_init(png_structp png_ptr) +{ + /* We only come here via pre-1.0.7-compiled applications */ + png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0); +} +#endif + +void PNGAPI +png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver, + png_size_t png_struct_size, png_size_t png_info_size) +{ + /* We only come here via pre-1.0.12-compiled applications */ +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + if(png_sizeof(png_struct) > png_struct_size || + png_sizeof(png_info) > png_info_size) + { + char msg[80]; + png_ptr->warning_fn=NULL; + if (user_png_ver) + { + sprintf(msg, "Application was compiled with png.h from libpng-%.20s", + user_png_ver); + png_warning(png_ptr, msg); + } + sprintf(msg, "Application is running with png.c from libpng-%.20s", + png_libpng_ver); + png_warning(png_ptr, msg); + } +#endif + if(png_sizeof(png_struct) > png_struct_size) + { + png_ptr->error_fn=NULL; +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "The png struct allocated by the application for reading is too small."); + } + if(png_sizeof(png_info) > png_info_size) + { + png_ptr->error_fn=NULL; +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "The info struct allocated by application for reading is too small."); + } + png_read_init_3(&png_ptr, user_png_ver, png_struct_size); +} + +void PNGAPI +png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, + png_size_t png_struct_size) +{ +#ifdef PNG_SETJMP_SUPPORTED + jmp_buf tmp_jmp; /* to save current jump buffer */ +#endif + + int i=0; + + png_structp png_ptr=*ptr_ptr; + + do + { + if(user_png_ver[i] != png_libpng_ver[i]) + { +#ifdef PNG_LEGACY_SUPPORTED + png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; +#else + png_ptr->warning_fn=NULL; + png_warning(png_ptr, + "Application uses deprecated png_read_init() and should be recompiled."); + break; +#endif + } + } while (png_libpng_ver[i++]); + + png_debug(1, "in png_read_init_3\n"); + +#ifdef PNG_SETJMP_SUPPORTED + /* save jump buffer and error functions */ + png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf)); +#endif + + if(png_sizeof(png_struct) > png_struct_size) + { + png_destroy_struct(png_ptr); + *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); + png_ptr = *ptr_ptr; + } + + /* reset all variables to 0 */ + png_memset(png_ptr, 0, png_sizeof (png_struct)); + +#ifdef PNG_SETJMP_SUPPORTED + /* restore jump buffer */ + png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf)); +#endif + + /* added at libpng-1.2.6 */ +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + png_ptr->user_width_max=PNG_USER_WIDTH_MAX; + png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; +#endif + + /* initialize zbuf - compression buffer */ + png_ptr->zbuf_size = PNG_ZBUF_SIZE; + png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, + (png_uint_32)png_ptr->zbuf_size); + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; + + switch (inflateInit(&png_ptr->zstream)) + { + case Z_OK: /* Do nothing */ break; + case Z_MEM_ERROR: + case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break; + case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break; + default: png_error(png_ptr, "Unknown zlib error"); + } + + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + + png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL); +} + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* Read the information before the actual image data. This has been + * changed in v0.90 to allow reading a file that already has the magic + * bytes read from the stream. You can tell libpng how many bytes have + * been read from the beginning of the stream (up to the maximum of 8) + * via png_set_sig_bytes(), and we will only check the remaining bytes + * here. The application can then have access to the signature bytes we + * read if it is determined that this isn't a valid PNG file. + */ +void PNGAPI +png_read_info(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_read_info\n"); + /* If we haven't checked all of the PNG signature bytes, do so now. */ + if (png_ptr->sig_bytes < 8) + { + png_size_t num_checked = png_ptr->sig_bytes, + num_to_check = 8 - num_checked; + + png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); + png_ptr->sig_bytes = 8; + + if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) + { + if (num_checked < 4 && + png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) + png_error(png_ptr, "Not a PNG file"); + else + png_error(png_ptr, "PNG file corrupted by ASCII conversion"); + } + if (num_checked < 3) + png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; + } + + for(;;) + { +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IHDR; + PNG_IDAT; + PNG_IEND; + PNG_PLTE; +#if defined(PNG_READ_bKGD_SUPPORTED) + PNG_bKGD; +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + PNG_cHRM; +#endif +#if defined(PNG_READ_gAMA_SUPPORTED) + PNG_gAMA; +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + PNG_hIST; +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + PNG_iCCP; +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + PNG_iTXt; +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + PNG_oFFs; +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + PNG_pCAL; +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + PNG_pHYs; +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + PNG_sBIT; +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + PNG_sCAL; +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + PNG_sPLT; +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + PNG_sRGB; +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + PNG_tEXt; +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + PNG_tIME; +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + PNG_tRNS; +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + PNG_zTXt; +#endif +#endif /* PNG_USE_LOCAL_ARRAYS */ + png_byte chunk_length[4]; + png_uint_32 length; + + png_read_data(png_ptr, chunk_length, 4); + length = png_get_uint_31(png_ptr,chunk_length); + + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + + png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name, + length); + + /* This should be a binary subdivision search or a hash for + * matching the chunk name rather than a linear search. + */ + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) + png_handle_IHDR(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) + png_handle_IEND(png_ptr, info_ptr, length); +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) + { + if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + png_ptr->mode |= PNG_HAVE_IDAT; + png_handle_unknown(png_ptr, info_ptr, length); + if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_ptr->mode |= PNG_HAVE_PLTE; + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + { + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before IDAT"); + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + !(png_ptr->mode & PNG_HAVE_PLTE)) + png_error(png_ptr, "Missing PLTE before IDAT"); + break; + } + } +#endif + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_handle_PLTE(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + { + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before IDAT"); + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + !(png_ptr->mode & PNG_HAVE_PLTE)) + png_error(png_ptr, "Missing PLTE before IDAT"); + + png_ptr->idat_size = length; + png_ptr->mode |= PNG_HAVE_IDAT; + break; + } +#if defined(PNG_READ_bKGD_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) + png_handle_bKGD(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) + png_handle_cHRM(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_gAMA_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + png_handle_gAMA(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) + png_handle_hIST(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) + png_handle_oFFs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) + png_handle_pCAL(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) + png_handle_sCAL(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) + png_handle_pHYs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) + png_handle_sBIT(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) + png_handle_sRGB(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) + png_handle_iCCP(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) + png_handle_sPLT(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) + png_handle_tEXt(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) + png_handle_tIME(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) + png_handle_tRNS(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) + png_handle_zTXt(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) + png_handle_iTXt(png_ptr, info_ptr, length); +#endif + else + png_handle_unknown(png_ptr, info_ptr, length); + } +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +/* optional call to update the users info_ptr structure */ +void PNGAPI +png_read_update_info(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_read_update_info\n"); + if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) + png_read_start_row(png_ptr); + else + png_warning(png_ptr, + "Ignoring extra png_read_update_info() call; row buffer not reallocated"); + png_read_transform_info(png_ptr, info_ptr); +} + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* Initialize palette, background, etc, after transformations + * are set, but before any reading takes place. This allows + * the user to obtain a gamma-corrected palette, for example. + * If the user doesn't call this, we will do it ourselves. + */ +void PNGAPI +png_start_read_image(png_structp png_ptr) +{ + png_debug(1, "in png_start_read_image\n"); + if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) + png_read_start_row(png_ptr); +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +void PNGAPI +png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IDAT; + const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; + const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; +#endif + int ret; + png_debug2(1, "in png_read_row (row %lu, pass %d)\n", + png_ptr->row_number, png_ptr->pass); + if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) + png_read_start_row(png_ptr); + if (png_ptr->row_number == 0 && png_ptr->pass == 0) + { + /* check for transforms that have been set but were defined out */ +#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_MONO) + png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) + if (png_ptr->transformations & PNG_FILLER) + png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) + if (png_ptr->transformations & PNG_PACK) + png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) + if (png_ptr->transformations & PNG_SHIFT) + png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) + if (png_ptr->transformations & PNG_BGR) + png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined."); +#endif +#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_BYTES) + png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined."); +#endif + } + +#if defined(PNG_READ_INTERLACING_SUPPORTED) + /* if interlaced and we do not need a new row, combine row and return */ + if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + { + switch (png_ptr->pass) + { + case 0: + if (png_ptr->row_number & 0x07) + { + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 1: + if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) + { + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 2: + if ((png_ptr->row_number & 0x07) != 4) + { + if (dsp_row != NULL && (png_ptr->row_number & 4)) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 3: + if ((png_ptr->row_number & 3) || png_ptr->width < 3) + { + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 4: + if ((png_ptr->row_number & 3) != 2) + { + if (dsp_row != NULL && (png_ptr->row_number & 2)) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 5: + if ((png_ptr->row_number & 1) || png_ptr->width < 2) + { + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + png_read_finish_row(png_ptr); + return; + } + break; + case 6: + if (!(png_ptr->row_number & 1)) + { + png_read_finish_row(png_ptr); + return; + } + break; + } + } +#endif + + if (!(png_ptr->mode & PNG_HAVE_IDAT)) + png_error(png_ptr, "Invalid attempt to read row data"); + + png_ptr->zstream.next_out = png_ptr->row_buf; + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; + do + { + if (!(png_ptr->zstream.avail_in)) + { + while (!png_ptr->idat_size) + { + png_byte chunk_length[4]; + + png_crc_finish(png_ptr, 0); + + png_read_data(png_ptr, chunk_length, 4); + png_ptr->idat_size = png_get_uint_31(png_ptr,chunk_length); + + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + png_error(png_ptr, "Not enough image data"); + } + png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_in = png_ptr->zbuf; + if (png_ptr->zbuf_size > png_ptr->idat_size) + png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; + png_crc_read(png_ptr, png_ptr->zbuf, + (png_size_t)png_ptr->zstream.avail_in); + png_ptr->idat_size -= png_ptr->zstream.avail_in; + } + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); + if (ret == Z_STREAM_END) + { + if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || + png_ptr->idat_size) + png_error(png_ptr, "Extra compressed data"); + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + break; + } + if (ret != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : + "Decompression error"); + + } while (png_ptr->zstream.avail_out); + + png_ptr->row_info.color_type = png_ptr->color_type; + png_ptr->row_info.width = png_ptr->iwidth; + png_ptr->row_info.channels = png_ptr->channels; + png_ptr->row_info.bit_depth = png_ptr->bit_depth; + png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; + png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, + png_ptr->row_info.width); + + if(png_ptr->row_buf[0]) + png_read_filter_row(png_ptr, &(png_ptr->row_info), + png_ptr->row_buf + 1, png_ptr->prev_row + 1, + (int)(png_ptr->row_buf[0])); + + png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, + png_ptr->rowbytes + 1); + +#if defined(PNG_MNG_FEATURES_SUPPORTED) + if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) + { + /* Intrapixel differencing */ + png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); + } +#endif + + + if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) + png_do_read_transformations(png_ptr); + +#if defined(PNG_READ_INTERLACING_SUPPORTED) + /* blow up interlaced rows to full size */ + if (png_ptr->interlaced && + (png_ptr->transformations & PNG_INTERLACE)) + { + if (png_ptr->pass < 6) +/* old interface (pre-1.0.9): + png_do_read_interlace(&(png_ptr->row_info), + png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); + */ + png_do_read_interlace(png_ptr); + + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, + png_pass_dsp_mask[png_ptr->pass]); + if (row != NULL) + png_combine_row(png_ptr, row, + png_pass_mask[png_ptr->pass]); + } + else +#endif + { + if (row != NULL) + png_combine_row(png_ptr, row, 0xff); + if (dsp_row != NULL) + png_combine_row(png_ptr, dsp_row, 0xff); + } + png_read_finish_row(png_ptr); + + if (png_ptr->read_row_fn != NULL) + (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* Read one or more rows of image data. If the image is interlaced, + * and png_set_interlace_handling() has been called, the rows need to + * contain the contents of the rows from the previous pass. If the + * image has alpha or transparency, and png_handle_alpha()[*] has been + * called, the rows contents must be initialized to the contents of the + * screen. + * + * "row" holds the actual image, and pixels are placed in it + * as they arrive. If the image is displayed after each pass, it will + * appear to "sparkle" in. "display_row" can be used to display a + * "chunky" progressive image, with finer detail added as it becomes + * available. If you do not want this "chunky" display, you may pass + * NULL for display_row. If you do not want the sparkle display, and + * you have not called png_handle_alpha(), you may pass NULL for rows. + * If you have called png_handle_alpha(), and the image has either an + * alpha channel or a transparency chunk, you must provide a buffer for + * rows. In this case, you do not have to provide a display_row buffer + * also, but you may. If the image is not interlaced, or if you have + * not called png_set_interlace_handling(), the display_row buffer will + * be ignored, so pass NULL to it. + * + * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.8 + */ + +void PNGAPI +png_read_rows(png_structp png_ptr, png_bytepp row, + png_bytepp display_row, png_uint_32 num_rows) +{ + png_uint_32 i; + png_bytepp rp; + png_bytepp dp; + + png_debug(1, "in png_read_rows\n"); + rp = row; + dp = display_row; + if (rp != NULL && dp != NULL) + for (i = 0; i < num_rows; i++) + { + png_bytep rptr = *rp++; + png_bytep dptr = *dp++; + + png_read_row(png_ptr, rptr, dptr); + } + else if(rp != NULL) + for (i = 0; i < num_rows; i++) + { + png_bytep rptr = *rp; + png_read_row(png_ptr, rptr, png_bytep_NULL); + rp++; + } + else if(dp != NULL) + for (i = 0; i < num_rows; i++) + { + png_bytep dptr = *dp; + png_read_row(png_ptr, png_bytep_NULL, dptr); + dp++; + } +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* Read the entire image. If the image has an alpha channel or a tRNS + * chunk, and you have called png_handle_alpha()[*], you will need to + * initialize the image to the current image that PNG will be overlaying. + * We set the num_rows again here, in case it was incorrectly set in + * png_read_start_row() by a call to png_read_update_info() or + * png_start_read_image() if png_set_interlace_handling() wasn't called + * prior to either of these functions like it should have been. You can + * only call this function once. If you desire to have an image for + * each pass of a interlaced image, use png_read_rows() instead. + * + * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.8 + */ +void PNGAPI +png_read_image(png_structp png_ptr, png_bytepp image) +{ + png_uint_32 i,image_height; + int pass, j; + png_bytepp rp; + + png_debug(1, "in png_read_image\n"); + +#ifdef PNG_READ_INTERLACING_SUPPORTED + pass = png_set_interlace_handling(png_ptr); +#else + if (png_ptr->interlaced) + png_error(png_ptr, + "Cannot read interlaced image -- interlace handler disabled."); + pass = 1; +#endif + + + image_height=png_ptr->height; + png_ptr->num_rows = image_height; /* Make sure this is set correctly */ + + for (j = 0; j < pass; j++) + { + rp = image; + for (i = 0; i < image_height; i++) + { + png_read_row(png_ptr, *rp, png_bytep_NULL); + rp++; + } + } +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +/* Read the end of the PNG file. Will not read past the end of the + * file, will verify the end is accurate, and will read any comments + * or time information at the end of the file, if info is not NULL. + */ +void PNGAPI +png_read_end(png_structp png_ptr, png_infop info_ptr) +{ + png_byte chunk_length[4]; + png_uint_32 length; + + png_debug(1, "in png_read_end\n"); + png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ + + do + { +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IHDR; + PNG_IDAT; + PNG_IEND; + PNG_PLTE; +#if defined(PNG_READ_bKGD_SUPPORTED) + PNG_bKGD; +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + PNG_cHRM; +#endif +#if defined(PNG_READ_gAMA_SUPPORTED) + PNG_gAMA; +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + PNG_hIST; +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + PNG_iCCP; +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + PNG_iTXt; +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + PNG_oFFs; +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + PNG_pCAL; +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + PNG_pHYs; +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + PNG_sBIT; +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + PNG_sCAL; +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + PNG_sPLT; +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + PNG_sRGB; +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + PNG_tEXt; +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + PNG_tIME; +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + PNG_tRNS; +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + PNG_zTXt; +#endif +#endif /* PNG_USE_LOCAL_ARRAYS */ + + png_read_data(png_ptr, chunk_length, 4); + length = png_get_uint_31(png_ptr,chunk_length); + + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + + png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name); + + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) + png_handle_IHDR(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) + png_handle_IEND(png_ptr, info_ptr, length); +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) + { + if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + { + if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT) + png_error(png_ptr, "Too many IDAT's found"); + } + else + png_ptr->mode |= PNG_AFTER_IDAT; + png_handle_unknown(png_ptr, info_ptr, length); + if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_ptr->mode |= PNG_HAVE_PLTE; + } +#endif + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + { + /* Zero length IDATs are legal after the last IDAT has been + * read, but not after other chunks have been read. + */ + if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT) + png_error(png_ptr, "Too many IDAT's found"); + png_crc_finish(png_ptr, length); + } + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_handle_PLTE(png_ptr, info_ptr, length); +#if defined(PNG_READ_bKGD_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) + png_handle_bKGD(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) + png_handle_cHRM(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_gAMA_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + png_handle_gAMA(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) + png_handle_hIST(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) + png_handle_oFFs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_pCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) + png_handle_pCAL(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sCAL_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) + png_handle_sCAL(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) + png_handle_pHYs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) + png_handle_sBIT(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sRGB_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) + png_handle_sRGB(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_iCCP_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) + png_handle_iCCP(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sPLT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) + png_handle_sPLT(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) + png_handle_tEXt(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) + png_handle_tIME(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) + png_handle_tRNS(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) + png_handle_zTXt(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_iTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) + png_handle_iTXt(png_ptr, info_ptr, length); +#endif + else + png_handle_unknown(png_ptr, info_ptr, length); + } while (!(png_ptr->mode & PNG_HAVE_IEND)); +} +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ + +/* free all memory used by the read */ +void PNGAPI +png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, + png_infopp end_info_ptr_ptr) +{ + png_structp png_ptr = NULL; + png_infop info_ptr = NULL, end_info_ptr = NULL; +#ifdef PNG_USER_MEM_SUPPORTED + png_free_ptr free_fn; + png_voidp mem_ptr; +#endif + + png_debug(1, "in png_destroy_read_struct\n"); + if (png_ptr_ptr != NULL) + png_ptr = *png_ptr_ptr; + + if (info_ptr_ptr != NULL) + info_ptr = *info_ptr_ptr; + + if (end_info_ptr_ptr != NULL) + end_info_ptr = *end_info_ptr_ptr; + +#ifdef PNG_USER_MEM_SUPPORTED + free_fn = png_ptr->free_fn; + mem_ptr = png_ptr->mem_ptr; +#endif + + png_read_destroy(png_ptr, info_ptr, end_info_ptr); + + if (info_ptr != NULL) + { +#if defined(PNG_TEXT_SUPPORTED) + png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); +#endif + +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, + (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)info_ptr); +#endif + *info_ptr_ptr = NULL; + } + + if (end_info_ptr != NULL) + { +#if defined(PNG_READ_TEXT_SUPPORTED) + png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); +#endif +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, + (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)end_info_ptr); +#endif + *end_info_ptr_ptr = NULL; + } + + if (png_ptr != NULL) + { +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, + (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)png_ptr); +#endif + *png_ptr_ptr = NULL; + } +} + +/* free all memory used by the read (old method) */ +void /* PRIVATE */ +png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr) +{ +#ifdef PNG_SETJMP_SUPPORTED + jmp_buf tmp_jmp; +#endif + png_error_ptr error_fn; + png_error_ptr warning_fn; + png_voidp error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + png_free_ptr free_fn; +#endif + + png_debug(1, "in png_read_destroy\n"); + if (info_ptr != NULL) + png_info_destroy(png_ptr, info_ptr); + + if (end_info_ptr != NULL) + png_info_destroy(png_ptr, end_info_ptr); + + png_free(png_ptr, png_ptr->zbuf); + png_free(png_ptr, png_ptr->big_row_buf); + png_free(png_ptr, png_ptr->prev_row); +#if defined(PNG_READ_DITHER_SUPPORTED) + png_free(png_ptr, png_ptr->palette_lookup); + png_free(png_ptr, png_ptr->dither_index); +#endif +#if defined(PNG_READ_GAMMA_SUPPORTED) + png_free(png_ptr, png_ptr->gamma_table); +#endif +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + png_free(png_ptr, png_ptr->gamma_from_1); + png_free(png_ptr, png_ptr->gamma_to_1); +#endif +#ifdef PNG_FREE_ME_SUPPORTED + if (png_ptr->free_me & PNG_FREE_PLTE) + png_zfree(png_ptr, png_ptr->palette); + png_ptr->free_me &= ~PNG_FREE_PLTE; +#else + if (png_ptr->flags & PNG_FLAG_FREE_PLTE) + png_zfree(png_ptr, png_ptr->palette); + png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; +#endif +#if defined(PNG_tRNS_SUPPORTED) || \ + defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) +#ifdef PNG_FREE_ME_SUPPORTED + if (png_ptr->free_me & PNG_FREE_TRNS) + png_free(png_ptr, png_ptr->trans); + png_ptr->free_me &= ~PNG_FREE_TRNS; +#else + if (png_ptr->flags & PNG_FLAG_FREE_TRNS) + png_free(png_ptr, png_ptr->trans); + png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; +#endif +#endif +#if defined(PNG_READ_hIST_SUPPORTED) +#ifdef PNG_FREE_ME_SUPPORTED + if (png_ptr->free_me & PNG_FREE_HIST) + png_free(png_ptr, png_ptr->hist); + png_ptr->free_me &= ~PNG_FREE_HIST; +#else + if (png_ptr->flags & PNG_FLAG_FREE_HIST) + png_free(png_ptr, png_ptr->hist); + png_ptr->flags &= ~PNG_FLAG_FREE_HIST; +#endif +#endif +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (png_ptr->gamma_16_table != NULL) + { + int i; + int istop = (1 << (8 - png_ptr->gamma_shift)); + for (i = 0; i < istop; i++) + { + png_free(png_ptr, png_ptr->gamma_16_table[i]); + } + png_free(png_ptr, png_ptr->gamma_16_table); + } +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->gamma_16_from_1 != NULL) + { + int i; + int istop = (1 << (8 - png_ptr->gamma_shift)); + for (i = 0; i < istop; i++) + { + png_free(png_ptr, png_ptr->gamma_16_from_1[i]); + } + png_free(png_ptr, png_ptr->gamma_16_from_1); + } + if (png_ptr->gamma_16_to_1 != NULL) + { + int i; + int istop = (1 << (8 - png_ptr->gamma_shift)); + for (i = 0; i < istop; i++) + { + png_free(png_ptr, png_ptr->gamma_16_to_1[i]); + } + png_free(png_ptr, png_ptr->gamma_16_to_1); + } +#endif +#endif +#if defined(PNG_TIME_RFC1123_SUPPORTED) + png_free(png_ptr, png_ptr->time_buffer); +#endif + + inflateEnd(&png_ptr->zstream); +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED + png_free(png_ptr, png_ptr->save_buffer); +#endif + +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED +#ifdef PNG_TEXT_SUPPORTED + png_free(png_ptr, png_ptr->current_text); +#endif /* PNG_TEXT_SUPPORTED */ +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ + + /* Save the important info out of the png_struct, in case it is + * being used again. + */ +#ifdef PNG_SETJMP_SUPPORTED + png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf)); +#endif + + error_fn = png_ptr->error_fn; + warning_fn = png_ptr->warning_fn; + error_ptr = png_ptr->error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + free_fn = png_ptr->free_fn; +#endif + + png_memset(png_ptr, 0, png_sizeof (png_struct)); + + png_ptr->error_fn = error_fn; + png_ptr->warning_fn = warning_fn; + png_ptr->error_ptr = error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + png_ptr->free_fn = free_fn; +#endif + +#ifdef PNG_SETJMP_SUPPORTED + png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf)); +#endif + +} + +void PNGAPI +png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) +{ + png_ptr->read_row_fn = read_row_fn; +} + + +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED +#if defined(PNG_INFO_IMAGE_SUPPORTED) +void PNGAPI +png_read_png(png_structp png_ptr, png_infop info_ptr, + int transforms, + voidp params) +{ + int row; + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) + /* invert the alpha channel from opacity to transparency + */ + if (transforms & PNG_TRANSFORM_INVERT_ALPHA) + png_set_invert_alpha(png_ptr); +#endif + + /* png_read_info() gives us all of the information from the + * PNG file before the first IDAT (image data chunk). + */ + png_read_info(png_ptr, info_ptr); + if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) + png_error(png_ptr,"Image is too high to process with png_read_png()"); + + /* -------------- image transformations start here ------------------- */ + +#if defined(PNG_READ_16_TO_8_SUPPORTED) + /* tell libpng to strip 16 bit/color files down to 8 bits per color + */ + if (transforms & PNG_TRANSFORM_STRIP_16) + png_set_strip_16(png_ptr); +#endif + +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) + /* Strip alpha bytes from the input data without combining with + * the background (not recommended). + */ + if (transforms & PNG_TRANSFORM_STRIP_ALPHA) + png_set_strip_alpha(png_ptr); +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) + /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single + * byte into separate bytes (useful for paletted and grayscale images). + */ + if (transforms & PNG_TRANSFORM_PACKING) + png_set_packing(png_ptr); +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + /* Change the order of packed pixels to least significant bit first + * (not useful if you are using png_set_packing). + */ + if (transforms & PNG_TRANSFORM_PACKSWAP) + png_set_packswap(png_ptr); +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) + /* Expand paletted colors into true RGB triplets + * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel + * Expand paletted or RGB images with transparency to full alpha + * channels so the data will be available as RGBA quartets. + */ + if (transforms & PNG_TRANSFORM_EXPAND) + if ((png_ptr->bit_depth < 8) || + (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || + (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) + png_set_expand(png_ptr); +#endif + + /* We don't handle background color or gamma transformation or dithering. + */ + +#if defined(PNG_READ_INVERT_SUPPORTED) + /* invert monochrome files to have 0 as white and 1 as black + */ + if (transforms & PNG_TRANSFORM_INVERT_MONO) + png_set_invert_mono(png_ptr); +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) + /* If you want to shift the pixel values from the range [0,255] or + * [0,65535] to the original [0,7] or [0,31], or whatever range the + * colors were originally in: + */ + if ((transforms & PNG_TRANSFORM_SHIFT) + && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) + { + png_color_8p sig_bit; + + png_get_sBIT(png_ptr, info_ptr, &sig_bit); + png_set_shift(png_ptr, sig_bit); + } +#endif + +#if defined(PNG_READ_BGR_SUPPORTED) + /* flip the RGB pixels to BGR (or RGBA to BGRA) + */ + if (transforms & PNG_TRANSFORM_BGR) + png_set_bgr(png_ptr); +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) + /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) + */ + if (transforms & PNG_TRANSFORM_SWAP_ALPHA) + png_set_swap_alpha(png_ptr); +#endif + +#if defined(PNG_READ_SWAP_SUPPORTED) + /* swap bytes of 16 bit files to least significant byte first + */ + if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) + png_set_swap(png_ptr); +#endif + + /* We don't handle adding filler bytes */ + + /* Optional call to gamma correct and add the background to the palette + * and update info structure. REQUIRED if you are expecting libpng to + * update the palette for you (i.e., you selected such a transform above). + */ + png_read_update_info(png_ptr, info_ptr); + + /* -------------- image transformations end here ------------------- */ + +#ifdef PNG_FREE_ME_SUPPORTED + png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); +#endif + if(info_ptr->row_pointers == NULL) + { + info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, + info_ptr->height * png_sizeof(png_bytep)); +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_ROWS; +#endif + for (row = 0; row < (int)info_ptr->height; row++) + { + info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, + png_get_rowbytes(png_ptr, info_ptr)); + } + } + + png_read_image(png_ptr, info_ptr->row_pointers); + info_ptr->valid |= PNG_INFO_IDAT; + + /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ + png_read_end(png_ptr, info_ptr); + + if(transforms == 0 || params == NULL) + /* quiet compiler warnings */ return; + +} +#endif +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ diff --git a/libpng/pngrio.c b/libpng/pngrio.c new file mode 100644 index 00000000..cae501a5 --- /dev/null +++ b/libpng/pngrio.c @@ -0,0 +1,161 @@ + +/* pngrio.c - functions for data input + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file provides a location for all input. Users who need + * special handling are expected to write a function that has the same + * arguments as this and performs a similar function, but that possibly + * has a different input method. Note that you shouldn't change this + * function, but rather write a replacement function and then make + * libpng use it at run time with png_set_read_fn(...). + */ + +#define PNG_INTERNAL +#include "png.h" + +/* Read the data from whatever input you are using. The default routine + reads from a file pointer. Note that this routine sometimes gets called + with very small lengths, so you should implement some kind of simple + buffering if you are using unbuffered reads. This should never be asked + to read more then 64K on a 16 bit machine. */ +void /* PRIVATE */ +png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + png_debug1(4,"reading %d bytes\n", (int)length); + if (png_ptr->read_data_fn != NULL) + (*(png_ptr->read_data_fn))(png_ptr, data, length); + else + png_error(png_ptr, "Call to NULL read function"); +} + +#if !defined(PNG_NO_STDIO) +/* This is the function that does the actual reading of data. If you are + not reading from a standard C stream, you should create a replacement + read_data function and use it at run time with png_set_read_fn(), rather + than changing the library. */ +#ifndef USE_FAR_KEYWORD +void PNGAPI +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + png_size_t check; + + /* fread() returns 0 on error, so it is OK to store this in a png_size_t + * instead of an int, which is what fread() actually returns. + */ +#if defined(_WIN32_WCE) + if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) + check = 0; +#else + check = (png_size_t)fread(data, (png_size_t)1, length, + (png_FILE_p)png_ptr->io_ptr); +#endif + + if (check != length) + png_error(png_ptr, "Read Error"); +} +#else +/* this is the model-independent version. Since the standard I/O library + can't handle far buffers in the medium and small models, we have to copy + the data. +*/ + +#define NEAR_BUF_SIZE 1024 +#define MIN(a,b) (a <= b ? a : b) + +static void /* PRIVATE */ +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + int check; + png_byte *n_data; + png_FILE_p io_ptr; + + /* Check if data really is near. If so, use usual code. */ + n_data = (png_byte *)CVT_PTR_NOCHECK(data); + io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); + if ((png_bytep)n_data == data) + { +#if defined(_WIN32_WCE) + if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) + check = 0; +#else + check = fread(n_data, 1, length, io_ptr); +#endif + } + else + { + png_byte buf[NEAR_BUF_SIZE]; + png_size_t read, remaining, err; + check = 0; + remaining = length; + do + { + read = MIN(NEAR_BUF_SIZE, remaining); +#if defined(_WIN32_WCE) + if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) ) + err = 0; +#else + err = fread(buf, (png_size_t)1, read, io_ptr); +#endif + png_memcpy(data, buf, read); /* copy far buffer to near buffer */ + if(err != read) + break; + else + check += err; + data += read; + remaining -= read; + } + while (remaining != 0); + } + if ((png_uint_32)check != (png_uint_32)length) + png_error(png_ptr, "read Error"); +} +#endif +#endif + +/* This function allows the application to supply a new input function + for libpng if standard C streams aren't being used. + + This function takes as its arguments: + png_ptr - pointer to a png input data structure + io_ptr - pointer to user supplied structure containing info about + the input functions. May be NULL. + read_data_fn - pointer to a new input function that takes as its + arguments a pointer to a png_struct, a pointer to + a location where input data can be stored, and a 32-bit + unsigned int that is the number of bytes to be read. + To exit and output any fatal error messages the new write + function should call png_error(png_ptr, "Error msg"). */ +void PNGAPI +png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, + png_rw_ptr read_data_fn) +{ + png_ptr->io_ptr = io_ptr; + +#if !defined(PNG_NO_STDIO) + if (read_data_fn != NULL) + png_ptr->read_data_fn = read_data_fn; + else + png_ptr->read_data_fn = png_default_read_data; +#else + png_ptr->read_data_fn = read_data_fn; +#endif + + /* It is an error to write to a read device */ + if (png_ptr->write_data_fn != NULL) + { + png_ptr->write_data_fn = NULL; + png_warning(png_ptr, + "It's an error to set both read_data_fn and write_data_fn in the "); + png_warning(png_ptr, + "same structure. Resetting write_data_fn to NULL."); + } + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) + png_ptr->output_flush_fn = NULL; +#endif +} diff --git a/libpng/pngrtran.c b/libpng/pngrtran.c new file mode 100644 index 00000000..e1d6e3ce --- /dev/null +++ b/libpng/pngrtran.c @@ -0,0 +1,4177 @@ + +/* pngrtran.c - transforms the data in a row for PNG readers + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file contains functions optionally called by an application + * in order to tell libpng how to handle data when reading a PNG. + * Transformations that are used in both reading and writing are + * in pngtrans.c. + */ + +#define PNG_INTERNAL +#include "png.h" + +/* Set the action on getting a CRC error for an ancillary or critical chunk. */ +void PNGAPI +png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) +{ + png_debug(1, "in png_set_crc_action\n"); + /* Tell libpng how we react to CRC errors in critical chunks */ + switch (crit_action) + { + case PNG_CRC_NO_CHANGE: /* leave setting as is */ + break; + case PNG_CRC_WARN_USE: /* warn/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; + break; + case PNG_CRC_QUIET_USE: /* quiet/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | + PNG_FLAG_CRC_CRITICAL_IGNORE; + break; + case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ + png_warning(png_ptr, "Can't discard critical data on CRC error."); + case PNG_CRC_ERROR_QUIT: /* error/quit */ + case PNG_CRC_DEFAULT: + default: + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + break; + } + + switch (ancil_action) + { + case PNG_CRC_NO_CHANGE: /* leave setting as is */ + break; + case PNG_CRC_WARN_USE: /* warn/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; + break; + case PNG_CRC_QUIET_USE: /* quiet/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | + PNG_FLAG_CRC_ANCILLARY_NOWARN; + break; + case PNG_CRC_ERROR_QUIT: /* error/quit */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; + break; + case PNG_CRC_WARN_DISCARD: /* warn/discard data */ + case PNG_CRC_DEFAULT: + default: + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + break; + } +} + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ + defined(PNG_FLOATING_POINT_SUPPORTED) +/* handle alpha and tRNS via a background color */ +void PNGAPI +png_set_background(png_structp png_ptr, + png_color_16p background_color, int background_gamma_code, + int need_expand, double background_gamma) +{ + png_debug(1, "in png_set_background\n"); + if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) + { + png_warning(png_ptr, "Application must supply a known background gamma"); + return; + } + + png_ptr->transformations |= PNG_BACKGROUND; + png_memcpy(&(png_ptr->background), background_color, + png_sizeof(png_color_16)); + png_ptr->background_gamma = (float)background_gamma; + png_ptr->background_gamma_type = (png_byte)(background_gamma_code); + png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0); + + /* Note: if need_expand is set and color_type is either RGB or RGB_ALPHA + * (in which case need_expand is superfluous anyway), the background color + * might actually be gray yet not be flagged as such. This is not a problem + * for the current code, which uses PNG_BACKGROUND_IS_GRAY only to + * decide when to do the png_do_gray_to_rgb() transformation. + */ + if ((need_expand && !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) || + (!need_expand && background_color->red == background_color->green && + background_color->red == background_color->blue)) + png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; +} +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) +/* strip 16 bit depth files to 8 bit depth */ +void PNGAPI +png_set_strip_16(png_structp png_ptr) +{ + png_debug(1, "in png_set_strip_16\n"); + png_ptr->transformations |= PNG_16_TO_8; +} +#endif + +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) +void PNGAPI +png_set_strip_alpha(png_structp png_ptr) +{ + png_debug(1, "in png_set_strip_alpha\n"); + png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; +} +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) +/* Dither file to 8 bit. Supply a palette, the current number + * of elements in the palette, the maximum number of elements + * allowed, and a histogram if possible. If the current number + * of colors is greater then the maximum number, the palette will be + * modified to fit in the maximum number. "full_dither" indicates + * whether we need a dithering cube set up for RGB images, or if we + * simply are reducing the number of colors in a paletted image. + */ + +typedef struct png_dsort_struct +{ + struct png_dsort_struct FAR * next; + png_byte left; + png_byte right; +} png_dsort; +typedef png_dsort FAR * png_dsortp; +typedef png_dsort FAR * FAR * png_dsortpp; + +void PNGAPI +png_set_dither(png_structp png_ptr, png_colorp palette, + int num_palette, int maximum_colors, png_uint_16p histogram, + int full_dither) +{ + png_debug(1, "in png_set_dither\n"); + png_ptr->transformations |= PNG_DITHER; + + if (!full_dither) + { + int i; + + png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, + (png_uint_32)(num_palette * png_sizeof (png_byte))); + for (i = 0; i < num_palette; i++) + png_ptr->dither_index[i] = (png_byte)i; + } + + if (num_palette > maximum_colors) + { + if (histogram != NULL) + { + /* This is easy enough, just throw out the least used colors. + Perhaps not the best solution, but good enough. */ + + int i; + + /* initialize an array to sort colors */ + png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr, + (png_uint_32)(num_palette * png_sizeof (png_byte))); + + /* initialize the dither_sort array */ + for (i = 0; i < num_palette; i++) + png_ptr->dither_sort[i] = (png_byte)i; + + /* Find the least used palette entries by starting a + bubble sort, and running it until we have sorted + out enough colors. Note that we don't care about + sorting all the colors, just finding which are + least used. */ + + for (i = num_palette - 1; i >= maximum_colors; i--) + { + int done; /* to stop early if the list is pre-sorted */ + int j; + + done = 1; + for (j = 0; j < i; j++) + { + if (histogram[png_ptr->dither_sort[j]] + < histogram[png_ptr->dither_sort[j + 1]]) + { + png_byte t; + + t = png_ptr->dither_sort[j]; + png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1]; + png_ptr->dither_sort[j + 1] = t; + done = 0; + } + } + if (done) + break; + } + + /* swap the palette around, and set up a table, if necessary */ + if (full_dither) + { + int j = num_palette; + + /* put all the useful colors within the max, but don't + move the others */ + for (i = 0; i < maximum_colors; i++) + { + if ((int)png_ptr->dither_sort[i] >= maximum_colors) + { + do + j--; + while ((int)png_ptr->dither_sort[j] >= maximum_colors); + palette[i] = palette[j]; + } + } + } + else + { + int j = num_palette; + + /* move all the used colors inside the max limit, and + develop a translation table */ + for (i = 0; i < maximum_colors; i++) + { + /* only move the colors we need to */ + if ((int)png_ptr->dither_sort[i] >= maximum_colors) + { + png_color tmp_color; + + do + j--; + while ((int)png_ptr->dither_sort[j] >= maximum_colors); + + tmp_color = palette[j]; + palette[j] = palette[i]; + palette[i] = tmp_color; + /* indicate where the color went */ + png_ptr->dither_index[j] = (png_byte)i; + png_ptr->dither_index[i] = (png_byte)j; + } + } + + /* find closest color for those colors we are not using */ + for (i = 0; i < num_palette; i++) + { + if ((int)png_ptr->dither_index[i] >= maximum_colors) + { + int min_d, k, min_k, d_index; + + /* find the closest color to one we threw out */ + d_index = png_ptr->dither_index[i]; + min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); + for (k = 1, min_k = 0; k < maximum_colors; k++) + { + int d; + + d = PNG_COLOR_DIST(palette[d_index], palette[k]); + + if (d < min_d) + { + min_d = d; + min_k = k; + } + } + /* point to closest color */ + png_ptr->dither_index[i] = (png_byte)min_k; + } + } + } + png_free(png_ptr, png_ptr->dither_sort); + png_ptr->dither_sort=NULL; + } + else + { + /* This is much harder to do simply (and quickly). Perhaps + we need to go through a median cut routine, but those + don't always behave themselves with only a few colors + as input. So we will just find the closest two colors, + and throw out one of them (chosen somewhat randomly). + [We don't understand this at all, so if someone wants to + work on improving it, be our guest - AED, GRP] + */ + int i; + int max_d; + int num_new_palette; + png_dsortp t; + png_dsortpp hash; + + t=NULL; + + /* initialize palette index arrays */ + png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, + (png_uint_32)(num_palette * png_sizeof (png_byte))); + png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, + (png_uint_32)(num_palette * png_sizeof (png_byte))); + + /* initialize the sort array */ + for (i = 0; i < num_palette; i++) + { + png_ptr->index_to_palette[i] = (png_byte)i; + png_ptr->palette_to_index[i] = (png_byte)i; + } + + hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * + png_sizeof (png_dsortp))); + for (i = 0; i < 769; i++) + hash[i] = NULL; +/* png_memset(hash, 0, 769 * png_sizeof (png_dsortp)); */ + + num_new_palette = num_palette; + + /* initial wild guess at how far apart the farthest pixel + pair we will be eliminating will be. Larger + numbers mean more areas will be allocated, Smaller + numbers run the risk of not saving enough data, and + having to do this all over again. + + I have not done extensive checking on this number. + */ + max_d = 96; + + while (num_new_palette > maximum_colors) + { + for (i = 0; i < num_new_palette - 1; i++) + { + int j; + + for (j = i + 1; j < num_new_palette; j++) + { + int d; + + d = PNG_COLOR_DIST(palette[i], palette[j]); + + if (d <= max_d) + { + + t = (png_dsortp)png_malloc_warn(png_ptr, + (png_uint_32)(png_sizeof(png_dsort))); + if (t == NULL) + break; + t->next = hash[d]; + t->left = (png_byte)i; + t->right = (png_byte)j; + hash[d] = t; + } + } + if (t == NULL) + break; + } + + if (t != NULL) + for (i = 0; i <= max_d; i++) + { + if (hash[i] != NULL) + { + png_dsortp p; + + for (p = hash[i]; p; p = p->next) + { + if ((int)png_ptr->index_to_palette[p->left] + < num_new_palette && + (int)png_ptr->index_to_palette[p->right] + < num_new_palette) + { + int j, next_j; + + if (num_new_palette & 0x01) + { + j = p->left; + next_j = p->right; + } + else + { + j = p->right; + next_j = p->left; + } + + num_new_palette--; + palette[png_ptr->index_to_palette[j]] + = palette[num_new_palette]; + if (!full_dither) + { + int k; + + for (k = 0; k < num_palette; k++) + { + if (png_ptr->dither_index[k] == + png_ptr->index_to_palette[j]) + png_ptr->dither_index[k] = + png_ptr->index_to_palette[next_j]; + if ((int)png_ptr->dither_index[k] == + num_new_palette) + png_ptr->dither_index[k] = + png_ptr->index_to_palette[j]; + } + } + + png_ptr->index_to_palette[png_ptr->palette_to_index + [num_new_palette]] = png_ptr->index_to_palette[j]; + png_ptr->palette_to_index[png_ptr->index_to_palette[j]] + = png_ptr->palette_to_index[num_new_palette]; + + png_ptr->index_to_palette[j] = (png_byte)num_new_palette; + png_ptr->palette_to_index[num_new_palette] = (png_byte)j; + } + if (num_new_palette <= maximum_colors) + break; + } + if (num_new_palette <= maximum_colors) + break; + } + } + + for (i = 0; i < 769; i++) + { + if (hash[i] != NULL) + { + png_dsortp p = hash[i]; + while (p) + { + t = p->next; + png_free(png_ptr, p); + p = t; + } + } + hash[i] = 0; + } + max_d += 96; + } + png_free(png_ptr, hash); + png_free(png_ptr, png_ptr->palette_to_index); + png_free(png_ptr, png_ptr->index_to_palette); + png_ptr->palette_to_index=NULL; + png_ptr->index_to_palette=NULL; + } + num_palette = maximum_colors; + } + if (png_ptr->palette == NULL) + { + png_ptr->palette = palette; + } + png_ptr->num_palette = (png_uint_16)num_palette; + + if (full_dither) + { + int i; + png_bytep distance; + int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS + + PNG_DITHER_BLUE_BITS; + int num_red = (1 << PNG_DITHER_RED_BITS); + int num_green = (1 << PNG_DITHER_GREEN_BITS); + int num_blue = (1 << PNG_DITHER_BLUE_BITS); + png_size_t num_entries = ((png_size_t)1 << total_bits); + + png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr, + (png_uint_32)(num_entries * png_sizeof (png_byte))); + + png_memset(png_ptr->palette_lookup, 0, num_entries * + png_sizeof (png_byte)); + + distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * + png_sizeof(png_byte))); + + png_memset(distance, 0xff, num_entries * png_sizeof(png_byte)); + + for (i = 0; i < num_palette; i++) + { + int ir, ig, ib; + int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS)); + int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS)); + int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS)); + + for (ir = 0; ir < num_red; ir++) + { + /* int dr = abs(ir - r); */ + int dr = ((ir > r) ? ir - r : r - ir); + int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS)); + + for (ig = 0; ig < num_green; ig++) + { + /* int dg = abs(ig - g); */ + int dg = ((ig > g) ? ig - g : g - ig); + int dt = dr + dg; + int dm = ((dr > dg) ? dr : dg); + int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS); + + for (ib = 0; ib < num_blue; ib++) + { + int d_index = index_g | ib; + /* int db = abs(ib - b); */ + int db = ((ib > b) ? ib - b : b - ib); + int dmax = ((dm > db) ? dm : db); + int d = dmax + dt + db; + + if (d < (int)distance[d_index]) + { + distance[d_index] = (png_byte)d; + png_ptr->palette_lookup[d_index] = (png_byte)i; + } + } + } + } + } + + png_free(png_ptr, distance); + } +} +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) +/* Transform the image from the file_gamma to the screen_gamma. We + * only do transformations on images where the file_gamma and screen_gamma + * are not close reciprocals, otherwise it slows things down slightly, and + * also needlessly introduces small errors. + * + * We will turn off gamma transformation later if no semitransparent entries + * are present in the tRNS array for palette images. We can't do it here + * because we don't necessarily have the tRNS chunk yet. + */ +void PNGAPI +png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) +{ + png_debug(1, "in png_set_gamma\n"); + if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || + (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || + (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) + png_ptr->transformations |= PNG_GAMMA; + png_ptr->gamma = (float)file_gamma; + png_ptr->screen_gamma = (float)scrn_gamma; +} +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) +/* Expand paletted images to RGB, expand grayscale images of + * less than 8-bit depth to 8-bit depth, and expand tRNS chunks + * to alpha channels. + */ +void PNGAPI +png_set_expand(png_structp png_ptr) +{ + png_debug(1, "in png_set_expand\n"); + png_ptr->transformations |= PNG_EXPAND; +} + +/* GRR 19990627: the following three functions currently are identical + * to png_set_expand(). However, it is entirely reasonable that someone + * might wish to expand an indexed image to RGB but *not* expand a single, + * fully transparent palette entry to a full alpha channel--perhaps instead + * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace + * the transparent color with a particular RGB value, or drop tRNS entirely. + * IOW, a future version of the library may make the transformations flag + * a bit more fine-grained, with separate bits for each of these three + * functions. + * + * More to the point, these functions make it obvious what libpng will be + * doing, whereas "expand" can (and does) mean any number of things. + */ + +/* Expand paletted images to RGB. */ +void PNGAPI +png_set_palette_to_rgb(png_structp png_ptr) +{ + png_debug(1, "in png_set_expand\n"); + png_ptr->transformations |= PNG_EXPAND; +} + +/* Expand grayscale images of less than 8-bit depth to 8 bits. */ +void PNGAPI +png_set_gray_1_2_4_to_8(png_structp png_ptr) +{ + png_debug(1, "in png_set_expand\n"); + png_ptr->transformations |= PNG_EXPAND; +} + +/* Expand tRNS chunks to alpha channels. */ +void PNGAPI +png_set_tRNS_to_alpha(png_structp png_ptr) +{ + png_debug(1, "in png_set_expand\n"); + png_ptr->transformations |= PNG_EXPAND; +} +#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +void PNGAPI +png_set_gray_to_rgb(png_structp png_ptr) +{ + png_debug(1, "in png_set_gray_to_rgb\n"); + png_ptr->transformations |= PNG_GRAY_TO_RGB; +} +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +#if defined(PNG_FLOATING_POINT_SUPPORTED) +/* Convert a RGB image to a grayscale of the same width. This allows us, + * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. + */ + +void PNGAPI +png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, + double green) +{ + int red_fixed = (int)((float)red*100000.0 + 0.5); + int green_fixed = (int)((float)green*100000.0 + 0.5); + png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); +} +#endif + +void PNGAPI +png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, + png_fixed_point red, png_fixed_point green) +{ + png_debug(1, "in png_set_rgb_to_gray\n"); + switch(error_action) + { + case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; + break; + case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; + break; + case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; + } + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) +#if defined(PNG_READ_EXPAND_SUPPORTED) + png_ptr->transformations |= PNG_EXPAND; +#else + { + png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); + png_ptr->transformations &= ~PNG_RGB_TO_GRAY; + } +#endif + { + png_uint_16 red_int, green_int; + if(red < 0 || green < 0) + { + red_int = 6968; /* .212671 * 32768 + .5 */ + green_int = 23434; /* .715160 * 32768 + .5 */ + } + else if(red + green < 100000L) + { + red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); + green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); + } + else + { + png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); + red_int = 6968; + green_int = 23434; + } + png_ptr->rgb_to_gray_red_coeff = red_int; + png_ptr->rgb_to_gray_green_coeff = green_int; + png_ptr->rgb_to_gray_blue_coeff = (png_uint_16)(32768-red_int-green_int); + } +} +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +void PNGAPI +png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr + read_user_transform_fn) +{ + png_debug(1, "in png_set_read_user_transform_fn\n"); +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) + png_ptr->transformations |= PNG_USER_TRANSFORM; + png_ptr->read_user_transform_fn = read_user_transform_fn; +#endif +#ifdef PNG_LEGACY_SUPPORTED + if(read_user_transform_fn) + png_warning(png_ptr, + "This version of libpng does not support user transforms"); +#endif +} +#endif + +/* Initialize everything needed for the read. This includes modifying + * the palette. + */ +void /* PRIVATE */ +png_init_read_transformations(png_structp png_ptr) +{ + png_debug(1, "in png_init_read_transformations\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if(png_ptr != NULL) +#endif + { +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \ + || defined(PNG_READ_GAMMA_SUPPORTED) + int color_type = png_ptr->color_type; +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) + if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && + (png_ptr->transformations & PNG_EXPAND)) + { + if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ + { + /* expand background chunk. */ + switch (png_ptr->bit_depth) + { + case 1: + png_ptr->background.gray *= (png_uint_16)0xff; + png_ptr->background.red = png_ptr->background.green + = png_ptr->background.blue = png_ptr->background.gray; + break; + case 2: + png_ptr->background.gray *= (png_uint_16)0x55; + png_ptr->background.red = png_ptr->background.green + = png_ptr->background.blue = png_ptr->background.gray; + break; + case 4: + png_ptr->background.gray *= (png_uint_16)0x11; + png_ptr->background.red = png_ptr->background.green + = png_ptr->background.blue = png_ptr->background.gray; + break; + case 8: + case 16: + png_ptr->background.red = png_ptr->background.green + = png_ptr->background.blue = png_ptr->background.gray; + break; + } + } + else if (color_type == PNG_COLOR_TYPE_PALETTE) + { + png_ptr->background.red = + png_ptr->palette[png_ptr->background.index].red; + png_ptr->background.green = + png_ptr->palette[png_ptr->background.index].green; + png_ptr->background.blue = + png_ptr->palette[png_ptr->background.index].blue; + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_ALPHA) + { +#if defined(PNG_READ_EXPAND_SUPPORTED) + if (!(png_ptr->transformations & PNG_EXPAND)) +#endif + { + /* invert the alpha channel (in tRNS) unless the pixels are + going to be expanded, in which case leave it for later */ + int i,istop; + istop=(int)png_ptr->num_trans; + for (i=0; itrans[i] = (png_byte)(255 - png_ptr->trans[i]); + } + } +#endif + + } + } +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) + png_ptr->background_1 = png_ptr->background; +#endif +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) + + if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0) + && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0) + < PNG_GAMMA_THRESHOLD)) + { + int i,k; + k=0; + for (i=0; inum_trans; i++) + { + if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) + k=1; /* partial transparency is present */ + } + if (k == 0) + png_ptr->transformations &= (~PNG_GAMMA); + } + + if (png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) + { + png_build_gamma_table(png_ptr); +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->transformations & PNG_BACKGROUND) + { + if (color_type == PNG_COLOR_TYPE_PALETTE) + { + /* could skip if no transparency and + */ + png_color back, back_1; + png_colorp palette = png_ptr->palette; + int num_palette = png_ptr->num_palette; + int i; + if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) + { + back.red = png_ptr->gamma_table[png_ptr->background.red]; + back.green = png_ptr->gamma_table[png_ptr->background.green]; + back.blue = png_ptr->gamma_table[png_ptr->background.blue]; + + back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; + back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; + back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; + } + else + { + double g, gs; + + switch (png_ptr->background_gamma_type) + { + case PNG_BACKGROUND_GAMMA_SCREEN: + g = (png_ptr->screen_gamma); + gs = 1.0; + break; + case PNG_BACKGROUND_GAMMA_FILE: + g = 1.0 / (png_ptr->gamma); + gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); + break; + case PNG_BACKGROUND_GAMMA_UNIQUE: + g = 1.0 / (png_ptr->background_gamma); + gs = 1.0 / (png_ptr->background_gamma * + png_ptr->screen_gamma); + break; + default: + g = 1.0; /* back_1 */ + gs = 1.0; /* back */ + } + + if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD) + { + back.red = (png_byte)png_ptr->background.red; + back.green = (png_byte)png_ptr->background.green; + back.blue = (png_byte)png_ptr->background.blue; + } + else + { + back.red = (png_byte)(pow( + (double)png_ptr->background.red/255, gs) * 255.0 + .5); + back.green = (png_byte)(pow( + (double)png_ptr->background.green/255, gs) * 255.0 + .5); + back.blue = (png_byte)(pow( + (double)png_ptr->background.blue/255, gs) * 255.0 + .5); + } + + back_1.red = (png_byte)(pow( + (double)png_ptr->background.red/255, g) * 255.0 + .5); + back_1.green = (png_byte)(pow( + (double)png_ptr->background.green/255, g) * 255.0 + .5); + back_1.blue = (png_byte)(pow( + (double)png_ptr->background.blue/255, g) * 255.0 + .5); + } + for (i = 0; i < num_palette; i++) + { + if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff) + { + if (png_ptr->trans[i] == 0) + { + palette[i] = back; + } + else /* if (png_ptr->trans[i] != 0xff) */ + { + png_byte v, w; + + v = png_ptr->gamma_to_1[palette[i].red]; + png_composite(w, v, png_ptr->trans[i], back_1.red); + palette[i].red = png_ptr->gamma_from_1[w]; + + v = png_ptr->gamma_to_1[palette[i].green]; + png_composite(w, v, png_ptr->trans[i], back_1.green); + palette[i].green = png_ptr->gamma_from_1[w]; + + v = png_ptr->gamma_to_1[palette[i].blue]; + png_composite(w, v, png_ptr->trans[i], back_1.blue); + palette[i].blue = png_ptr->gamma_from_1[w]; + } + } + else + { + palette[i].red = png_ptr->gamma_table[palette[i].red]; + palette[i].green = png_ptr->gamma_table[palette[i].green]; + palette[i].blue = png_ptr->gamma_table[palette[i].blue]; + } + } + } + /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ + else + /* color_type != PNG_COLOR_TYPE_PALETTE */ + { + double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1); + double g = 1.0; + double gs = 1.0; + + switch (png_ptr->background_gamma_type) + { + case PNG_BACKGROUND_GAMMA_SCREEN: + g = (png_ptr->screen_gamma); + gs = 1.0; + break; + case PNG_BACKGROUND_GAMMA_FILE: + g = 1.0 / (png_ptr->gamma); + gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); + break; + case PNG_BACKGROUND_GAMMA_UNIQUE: + g = 1.0 / (png_ptr->background_gamma); + gs = 1.0 / (png_ptr->background_gamma * + png_ptr->screen_gamma); + break; + } + + png_ptr->background_1.gray = (png_uint_16)(pow( + (double)png_ptr->background.gray / m, g) * m + .5); + png_ptr->background.gray = (png_uint_16)(pow( + (double)png_ptr->background.gray / m, gs) * m + .5); + + if ((png_ptr->background.red != png_ptr->background.green) || + (png_ptr->background.red != png_ptr->background.blue) || + (png_ptr->background.red != png_ptr->background.gray)) + { + /* RGB or RGBA with color background */ + png_ptr->background_1.red = (png_uint_16)(pow( + (double)png_ptr->background.red / m, g) * m + .5); + png_ptr->background_1.green = (png_uint_16)(pow( + (double)png_ptr->background.green / m, g) * m + .5); + png_ptr->background_1.blue = (png_uint_16)(pow( + (double)png_ptr->background.blue / m, g) * m + .5); + png_ptr->background.red = (png_uint_16)(pow( + (double)png_ptr->background.red / m, gs) * m + .5); + png_ptr->background.green = (png_uint_16)(pow( + (double)png_ptr->background.green / m, gs) * m + .5); + png_ptr->background.blue = (png_uint_16)(pow( + (double)png_ptr->background.blue / m, gs) * m + .5); + } + else + { + /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ + png_ptr->background_1.red = png_ptr->background_1.green + = png_ptr->background_1.blue = png_ptr->background_1.gray; + png_ptr->background.red = png_ptr->background.green + = png_ptr->background.blue = png_ptr->background.gray; + } + } + } + else + /* transformation does not include PNG_BACKGROUND */ +#endif /* PNG_READ_BACKGROUND_SUPPORTED */ + if (color_type == PNG_COLOR_TYPE_PALETTE) + { + png_colorp palette = png_ptr->palette; + int num_palette = png_ptr->num_palette; + int i; + + for (i = 0; i < num_palette; i++) + { + palette[i].red = png_ptr->gamma_table[palette[i].red]; + palette[i].green = png_ptr->gamma_table[palette[i].green]; + palette[i].blue = png_ptr->gamma_table[palette[i].blue]; + } + } + } +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + else +#endif +#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */ +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + /* No GAMMA transformation */ + if ((png_ptr->transformations & PNG_BACKGROUND) && + (color_type == PNG_COLOR_TYPE_PALETTE)) + { + int i; + int istop = (int)png_ptr->num_trans; + png_color back; + png_colorp palette = png_ptr->palette; + + back.red = (png_byte)png_ptr->background.red; + back.green = (png_byte)png_ptr->background.green; + back.blue = (png_byte)png_ptr->background.blue; + + for (i = 0; i < istop; i++) + { + if (png_ptr->trans[i] == 0) + { + palette[i] = back; + } + else if (png_ptr->trans[i] != 0xff) + { + /* The png_composite() macro is defined in png.h */ + png_composite(palette[i].red, palette[i].red, + png_ptr->trans[i], back.red); + png_composite(palette[i].green, palette[i].green, + png_ptr->trans[i], back.green); + png_composite(palette[i].blue, palette[i].blue, + png_ptr->trans[i], back.blue); + } + } + } +#endif /* PNG_READ_BACKGROUND_SUPPORTED */ + +#if defined(PNG_READ_SHIFT_SUPPORTED) + if ((png_ptr->transformations & PNG_SHIFT) && + (color_type == PNG_COLOR_TYPE_PALETTE)) + { + png_uint_16 i; + png_uint_16 istop = png_ptr->num_palette; + int sr = 8 - png_ptr->sig_bit.red; + int sg = 8 - png_ptr->sig_bit.green; + int sb = 8 - png_ptr->sig_bit.blue; + + if (sr < 0 || sr > 8) + sr = 0; + if (sg < 0 || sg > 8) + sg = 0; + if (sb < 0 || sb > 8) + sb = 0; + for (i = 0; i < istop; i++) + { + png_ptr->palette[i].red >>= sr; + png_ptr->palette[i].green >>= sg; + png_ptr->palette[i].blue >>= sb; + } + } +#endif /* PNG_READ_SHIFT_SUPPORTED */ + } +#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ + && !defined(PNG_READ_BACKGROUND_SUPPORTED) + if(png_ptr) + return; +#endif +} + +/* Modify the info structure to reflect the transformations. The + * info should be updated so a PNG file could be written with it, + * assuming the transformations result in valid PNG data. + */ +void /* PRIVATE */ +png_read_transform_info(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_read_transform_info\n"); +#if defined(PNG_READ_EXPAND_SUPPORTED) + if (png_ptr->transformations & PNG_EXPAND) + { + if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (png_ptr->num_trans) + info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; + else + info_ptr->color_type = PNG_COLOR_TYPE_RGB; + info_ptr->bit_depth = 8; + info_ptr->num_trans = 0; + } + else + { + if (png_ptr->num_trans) + info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; + if (info_ptr->bit_depth < 8) + info_ptr->bit_depth = 8; + info_ptr->num_trans = 0; + } + } +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->transformations & PNG_BACKGROUND) + { + info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; + info_ptr->num_trans = 0; + info_ptr->background = png_ptr->background; + } +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (png_ptr->transformations & PNG_GAMMA) + { +#ifdef PNG_FLOATING_POINT_SUPPORTED + info_ptr->gamma = png_ptr->gamma; +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + info_ptr->int_gamma = png_ptr->int_gamma; +#endif + } +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) + if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16)) + info_ptr->bit_depth = 8; +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) + if (png_ptr->transformations & PNG_DITHER) + { + if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || + (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && + png_ptr->palette_lookup && info_ptr->bit_depth == 8) + { + info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; + } + } +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) + if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) + info_ptr->bit_depth = 8; +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) + if (png_ptr->transformations & PNG_GRAY_TO_RGB) + info_ptr->color_type |= PNG_COLOR_MASK_COLOR; +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) + if (png_ptr->transformations & PNG_RGB_TO_GRAY) + info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; +#endif + + if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + info_ptr->channels = 1; + else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) + info_ptr->channels = 3; + else + info_ptr->channels = 1; + +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) + if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) + info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; +#endif + + if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) + info_ptr->channels++; + +#if defined(PNG_READ_FILLER_SUPPORTED) + /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ + if ((png_ptr->transformations & PNG_FILLER) && + ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || + (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) + { + info_ptr->channels++; + /* if adding a true alpha channel not just filler */ +#if !defined(PNG_1_0_X) + if (png_ptr->transformations & PNG_ADD_ALPHA) + info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; +#endif + } +#endif + +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ +defined(PNG_READ_USER_TRANSFORM_SUPPORTED) + if(png_ptr->transformations & PNG_USER_TRANSFORM) + { + if(info_ptr->bit_depth < png_ptr->user_transform_depth) + info_ptr->bit_depth = png_ptr->user_transform_depth; + if(info_ptr->channels < png_ptr->user_transform_channels) + info_ptr->channels = png_ptr->user_transform_channels; + } +#endif + + info_ptr->pixel_depth = (png_byte)(info_ptr->channels * + info_ptr->bit_depth); + + info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,info_ptr->width); + +#if !defined(PNG_READ_EXPAND_SUPPORTED) + if(png_ptr) + return; +#endif +} + +/* Transform the row. The order of transformations is significant, + * and is very touchy. If you add a transformation, take care to + * decide how it fits in with the other transformations here. + */ +void /* PRIVATE */ +png_do_read_transformations(png_structp png_ptr) +{ + png_debug(1, "in png_do_read_transformations\n"); +#if !defined(PNG_USELESS_TESTS_SUPPORTED) + if (png_ptr->row_buf == NULL) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char msg[50]; + + sprintf(msg, "NULL row buffer for row %ld, pass %d", png_ptr->row_number, + png_ptr->pass); + png_error(png_ptr, msg); +#else + png_error(png_ptr, "NULL row buffer"); +#endif + } +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) + if (png_ptr->transformations & PNG_EXPAND) + { + if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) + { + png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1, + png_ptr->palette, png_ptr->trans, png_ptr->num_trans); + } + else + { + if (png_ptr->num_trans) + png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, + &(png_ptr->trans_values)); + else + png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, + NULL); + } + } +#endif + +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) + if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) + png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, + PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)); +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) + if (png_ptr->transformations & PNG_RGB_TO_GRAY) + { + int rgb_error = + png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); + if(rgb_error) + { + png_ptr->rgb_to_gray_status=1; + if(png_ptr->transformations == PNG_RGB_TO_GRAY_WARN) + png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); + if(png_ptr->transformations == PNG_RGB_TO_GRAY_ERR) + png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); + } + } +#endif + +/* +From Andreas Dilger e-mail to png-implement, 26 March 1998: + + In most cases, the "simple transparency" should be done prior to doing + gray-to-RGB, or you will have to test 3x as many bytes to check if a + pixel is transparent. You would also need to make sure that the + transparency information is upgraded to RGB. + + To summarize, the current flow is: + - Gray + simple transparency -> compare 1 or 2 gray bytes and composite + with background "in place" if transparent, + convert to RGB if necessary + - Gray + alpha -> composite with gray background and remove alpha bytes, + convert to RGB if necessary + + To support RGB backgrounds for gray images we need: + - Gray + simple transparency -> convert to RGB + simple transparency, compare + 3 or 6 bytes and composite with background + "in place" if transparent (3x compare/pixel + compared to doing composite with gray bkgrnd) + - Gray + alpha -> convert to RGB + alpha, composite with background and + remove alpha bytes (3x float operations/pixel + compared with composite on gray background) + + Greg's change will do this. The reason it wasn't done before is for + performance, as this increases the per-pixel operations. If we would check + in advance if the background was gray or RGB, and position the gray-to-RGB + transform appropriately, then it would save a lot of work/time. + */ + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) + /* if gray -> RGB, do so now only if background is non-gray; else do later + * for performance reasons */ + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && + !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) + png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + if ((png_ptr->transformations & PNG_BACKGROUND) && + ((png_ptr->num_trans != 0 ) || + (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) + png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1, + &(png_ptr->trans_values), &(png_ptr->background) +#if defined(PNG_READ_GAMMA_SUPPORTED) + , &(png_ptr->background_1), + png_ptr->gamma_table, png_ptr->gamma_from_1, + png_ptr->gamma_to_1, png_ptr->gamma_16_table, + png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1, + png_ptr->gamma_shift +#endif +); +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) + if ((png_ptr->transformations & PNG_GAMMA) && +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + !((png_ptr->transformations & PNG_BACKGROUND) && + ((png_ptr->num_trans != 0) || + (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && +#endif + (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) + png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, + png_ptr->gamma_table, png_ptr->gamma_16_table, + png_ptr->gamma_shift); +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) + if (png_ptr->transformations & PNG_16_TO_8) + png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) + if (png_ptr->transformations & PNG_DITHER) + { + png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, + png_ptr->palette_lookup, png_ptr->dither_index); + if(png_ptr->row_info.rowbytes == (png_uint_32)0) + png_error(png_ptr, "png_do_dither returned rowbytes=0"); + } +#endif + +#if defined(PNG_READ_INVERT_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_MONO) + png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) + if (png_ptr->transformations & PNG_SHIFT) + png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1, + &(png_ptr->shift)); +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) + if (png_ptr->transformations & PNG_PACK) + png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_BGR_SUPPORTED) + if (png_ptr->transformations & PNG_BGR) + png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) + /* if gray -> RGB, do so now only if we did not do so above */ + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && + (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) + png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_FILLER_SUPPORTED) + if (png_ptr->transformations & PNG_FILLER) + png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, + (png_uint_32)png_ptr->filler, png_ptr->flags); +#endif + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_ALPHA) + png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_ALPHA) + png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_SWAP_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_BYTES) + png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) + if (png_ptr->transformations & PNG_USER_TRANSFORM) + { + if(png_ptr->read_user_transform_fn != NULL) + (*(png_ptr->read_user_transform_fn)) /* user read transform function */ + (png_ptr, /* png_ptr */ + &(png_ptr->row_info), /* row_info: */ + /* png_uint_32 width; width of row */ + /* png_uint_32 rowbytes; number of bytes in row */ + /* png_byte color_type; color type of pixels */ + /* png_byte bit_depth; bit depth of samples */ + /* png_byte channels; number of channels (1-4) */ + /* png_byte pixel_depth; bits per pixel (depth*channels) */ + png_ptr->row_buf + 1); /* start of pixel data for row */ +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) + if(png_ptr->user_transform_depth) + png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; + if(png_ptr->user_transform_channels) + png_ptr->row_info.channels = png_ptr->user_transform_channels; +#endif + png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * + png_ptr->row_info.channels); + png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, + png_ptr->row_info.width); + } +#endif + +} + +#if defined(PNG_READ_PACK_SUPPORTED) +/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, + * without changing the actual values. Thus, if you had a row with + * a bit depth of 1, you would end up with bytes that only contained + * the numbers 0 or 1. If you would rather they contain 0 and 255, use + * png_do_shift() after this. + */ +void /* PRIVATE */ +png_do_unpack(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_unpack\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL && row_info->bit_depth < 8) +#else + if (row_info->bit_depth < 8) +#endif + { + png_uint_32 i; + png_uint_32 row_width=row_info->width; + + switch (row_info->bit_depth) + { + case 1: + { + png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); + png_bytep dp = row + (png_size_t)row_width - 1; + png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); + for (i = 0; i < row_width; i++) + { + *dp = (png_byte)((*sp >> shift) & 0x01); + if (shift == 7) + { + shift = 0; + sp--; + } + else + shift++; + + dp--; + } + break; + } + case 2: + { + + png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); + png_bytep dp = row + (png_size_t)row_width - 1; + png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); + for (i = 0; i < row_width; i++) + { + *dp = (png_byte)((*sp >> shift) & 0x03); + if (shift == 6) + { + shift = 0; + sp--; + } + else + shift += 2; + + dp--; + } + break; + } + case 4: + { + png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); + png_bytep dp = row + (png_size_t)row_width - 1; + png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); + for (i = 0; i < row_width; i++) + { + *dp = (png_byte)((*sp >> shift) & 0x0f); + if (shift == 4) + { + shift = 0; + sp--; + } + else + shift = 4; + + dp--; + } + break; + } + } + row_info->bit_depth = 8; + row_info->pixel_depth = (png_byte)(8 * row_info->channels); + row_info->rowbytes = row_width * row_info->channels; + } +} +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) +/* Reverse the effects of png_do_shift. This routine merely shifts the + * pixels back to their significant bits values. Thus, if you have + * a row of bit depth 8, but only 5 are significant, this will shift + * the values back to 0 through 31. + */ +void /* PRIVATE */ +png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits) +{ + png_debug(1, "in png_do_unshift\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && sig_bits != NULL && +#endif + row_info->color_type != PNG_COLOR_TYPE_PALETTE) + { + int shift[4]; + int channels = 0; + int c; + png_uint_16 value = 0; + png_uint_32 row_width = row_info->width; + + if (row_info->color_type & PNG_COLOR_MASK_COLOR) + { + shift[channels++] = row_info->bit_depth - sig_bits->red; + shift[channels++] = row_info->bit_depth - sig_bits->green; + shift[channels++] = row_info->bit_depth - sig_bits->blue; + } + else + { + shift[channels++] = row_info->bit_depth - sig_bits->gray; + } + if (row_info->color_type & PNG_COLOR_MASK_ALPHA) + { + shift[channels++] = row_info->bit_depth - sig_bits->alpha; + } + + for (c = 0; c < channels; c++) + { + if (shift[c] <= 0) + shift[c] = 0; + else + value = 1; + } + + if (!value) + return; + + switch (row_info->bit_depth) + { + case 2: + { + png_bytep bp; + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + + for (bp = row, i = 0; i < istop; i++) + { + *bp >>= 1; + *bp++ &= 0x55; + } + break; + } + case 4: + { + png_bytep bp = row; + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | + (png_byte)((int)0xf >> shift[0])); + + for (i = 0; i < istop; i++) + { + *bp >>= shift[0]; + *bp++ &= mask; + } + break; + } + case 8: + { + png_bytep bp = row; + png_uint_32 i; + png_uint_32 istop = row_width * channels; + + for (i = 0; i < istop; i++) + { + *bp++ >>= shift[i%channels]; + } + break; + } + case 16: + { + png_bytep bp = row; + png_uint_32 i; + png_uint_32 istop = channels * row_width; + + for (i = 0; i < istop; i++) + { + value = (png_uint_16)((*bp << 8) + *(bp + 1)); + value >>= shift[i%channels]; + *bp++ = (png_byte)(value >> 8); + *bp++ = (png_byte)(value & 0xff); + } + break; + } + } + } +} +#endif + +#if defined(PNG_READ_16_TO_8_SUPPORTED) +/* chop rows of bit depth 16 down to 8 */ +void /* PRIVATE */ +png_do_chop(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_chop\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL && row_info->bit_depth == 16) +#else + if (row_info->bit_depth == 16) +#endif + { + png_bytep sp = row; + png_bytep dp = row; + png_uint_32 i; + png_uint_32 istop = row_info->width * row_info->channels; + + for (i = 0; i> 8)) >> 8; + * + * Approximate calculation with shift/add instead of multiply/divide: + * *dp = ((((png_uint_32)(*sp) << 8) | + * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; + * + * What we actually do to avoid extra shifting and conversion: + */ + + *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); +#else + /* Simply discard the low order byte */ + *dp = *sp; +#endif + } + row_info->bit_depth = 8; + row_info->pixel_depth = (png_byte)(8 * row_info->channels); + row_info->rowbytes = row_info->width * row_info->channels; + } +} +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) +void /* PRIVATE */ +png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_read_swap_alpha\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + png_uint_32 row_width = row_info->width; + if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + /* This converts from RGBA to ARGB */ + if (row_info->bit_depth == 8) + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_byte save; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + save = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = save; + } + } + /* This converts from RRGGBBAA to AARRGGBB */ + else + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_byte save[2]; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + save[0] = *(--sp); + save[1] = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = save[0]; + *(--dp) = save[1]; + } + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + /* This converts from GA to AG */ + if (row_info->bit_depth == 8) + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_byte save; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + save = *(--sp); + *(--dp) = *(--sp); + *(--dp) = save; + } + } + /* This converts from GGAA to AAGG */ + else + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_byte save[2]; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + save[0] = *(--sp); + save[1] = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = save[0]; + *(--dp) = save[1]; + } + } + } + } +} +#endif + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) +void /* PRIVATE */ +png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_read_invert_alpha\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + png_uint_32 row_width = row_info->width; + if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + /* This inverts the alpha channel in RGBA */ + if (row_info->bit_depth == 8) + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + *(--dp) = (png_byte)(255 - *(--sp)); + +/* This does nothing: + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + We can replace it with: +*/ + sp-=3; + dp=sp; + } + } + /* This inverts the alpha channel in RRGGBBAA */ + else + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + *(--dp) = (png_byte)(255 - *(--sp)); + *(--dp) = (png_byte)(255 - *(--sp)); + +/* This does nothing: + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + We can replace it with: +*/ + sp-=6; + dp=sp; + } + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + /* This inverts the alpha channel in GA */ + if (row_info->bit_depth == 8) + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + *(--dp) = (png_byte)(255 - *(--sp)); + *(--dp) = *(--sp); + } + } + /* This inverts the alpha channel in GGAA */ + else + { + png_bytep sp = row + row_info->rowbytes; + png_bytep dp = sp; + png_uint_32 i; + + for (i = 0; i < row_width; i++) + { + *(--dp) = (png_byte)(255 - *(--sp)); + *(--dp) = (png_byte)(255 - *(--sp)); +/* + *(--dp) = *(--sp); + *(--dp) = *(--sp); +*/ + sp-=2; + dp=sp; + } + } + } + } +} +#endif + +#if defined(PNG_READ_FILLER_SUPPORTED) +/* Add filler channel if we have RGB color */ +void /* PRIVATE */ +png_do_read_filler(png_row_infop row_info, png_bytep row, + png_uint_32 filler, png_uint_32 flags) +{ + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + png_byte hi_filler = (png_byte)((filler>>8) & 0xff); + png_byte lo_filler = (png_byte)(filler & 0xff); + + png_debug(1, "in png_do_read_filler\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + row_info->color_type == PNG_COLOR_TYPE_GRAY) + { + if(row_info->bit_depth == 8) + { + /* This changes the data from G to GX */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + png_bytep sp = row + (png_size_t)row_width; + png_bytep dp = sp + (png_size_t)row_width; + for (i = 1; i < row_width; i++) + { + *(--dp) = lo_filler; + *(--dp) = *(--sp); + } + *(--dp) = lo_filler; + row_info->channels = 2; + row_info->pixel_depth = 16; + row_info->rowbytes = row_width * 2; + } + /* This changes the data from G to XG */ + else + { + png_bytep sp = row + (png_size_t)row_width; + png_bytep dp = sp + (png_size_t)row_width; + for (i = 0; i < row_width; i++) + { + *(--dp) = *(--sp); + *(--dp) = lo_filler; + } + row_info->channels = 2; + row_info->pixel_depth = 16; + row_info->rowbytes = row_width * 2; + } + } + else if(row_info->bit_depth == 16) + { + /* This changes the data from GG to GGXX */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + png_bytep sp = row + (png_size_t)row_width * 2; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 1; i < row_width; i++) + { + *(--dp) = hi_filler; + *(--dp) = lo_filler; + *(--dp) = *(--sp); + *(--dp) = *(--sp); + } + *(--dp) = hi_filler; + *(--dp) = lo_filler; + row_info->channels = 2; + row_info->pixel_depth = 32; + row_info->rowbytes = row_width * 4; + } + /* This changes the data from GG to XXGG */ + else + { + png_bytep sp = row + (png_size_t)row_width * 2; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 0; i < row_width; i++) + { + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = hi_filler; + *(--dp) = lo_filler; + } + row_info->channels = 2; + row_info->pixel_depth = 32; + row_info->rowbytes = row_width * 4; + } + } + } /* COLOR_TYPE == GRAY */ + else if (row_info->color_type == PNG_COLOR_TYPE_RGB) + { + if(row_info->bit_depth == 8) + { + /* This changes the data from RGB to RGBX */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + png_bytep sp = row + (png_size_t)row_width * 3; + png_bytep dp = sp + (png_size_t)row_width; + for (i = 1; i < row_width; i++) + { + *(--dp) = lo_filler; + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + } + *(--dp) = lo_filler; + row_info->channels = 4; + row_info->pixel_depth = 32; + row_info->rowbytes = row_width * 4; + } + /* This changes the data from RGB to XRGB */ + else + { + png_bytep sp = row + (png_size_t)row_width * 3; + png_bytep dp = sp + (png_size_t)row_width; + for (i = 0; i < row_width; i++) + { + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = lo_filler; + } + row_info->channels = 4; + row_info->pixel_depth = 32; + row_info->rowbytes = row_width * 4; + } + } + else if(row_info->bit_depth == 16) + { + /* This changes the data from RRGGBB to RRGGBBXX */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + png_bytep sp = row + (png_size_t)row_width * 6; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 1; i < row_width; i++) + { + *(--dp) = hi_filler; + *(--dp) = lo_filler; + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + } + *(--dp) = hi_filler; + *(--dp) = lo_filler; + row_info->channels = 4; + row_info->pixel_depth = 64; + row_info->rowbytes = row_width * 8; + } + /* This changes the data from RRGGBB to XXRRGGBB */ + else + { + png_bytep sp = row + (png_size_t)row_width * 6; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 0; i < row_width; i++) + { + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = *(--sp); + *(--dp) = hi_filler; + *(--dp) = lo_filler; + } + row_info->channels = 4; + row_info->pixel_depth = 64; + row_info->rowbytes = row_width * 8; + } + } + } /* COLOR_TYPE == RGB */ +} +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +/* expand grayscale files to RGB, with or without alpha */ +void /* PRIVATE */ +png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) +{ + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + png_debug(1, "in png_do_gray_to_rgb\n"); + if (row_info->bit_depth >= 8 && +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + !(row_info->color_type & PNG_COLOR_MASK_COLOR)) + { + if (row_info->color_type == PNG_COLOR_TYPE_GRAY) + { + if (row_info->bit_depth == 8) + { + png_bytep sp = row + (png_size_t)row_width - 1; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 0; i < row_width; i++) + { + *(dp--) = *sp; + *(dp--) = *sp; + *(dp--) = *(sp--); + } + } + else + { + png_bytep sp = row + (png_size_t)row_width * 2 - 1; + png_bytep dp = sp + (png_size_t)row_width * 4; + for (i = 0; i < row_width; i++) + { + *(dp--) = *sp; + *(dp--) = *(sp - 1); + *(dp--) = *sp; + *(dp--) = *(sp - 1); + *(dp--) = *(sp--); + *(dp--) = *(sp--); + } + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + if (row_info->bit_depth == 8) + { + png_bytep sp = row + (png_size_t)row_width * 2 - 1; + png_bytep dp = sp + (png_size_t)row_width * 2; + for (i = 0; i < row_width; i++) + { + *(dp--) = *(sp--); + *(dp--) = *sp; + *(dp--) = *sp; + *(dp--) = *(sp--); + } + } + else + { + png_bytep sp = row + (png_size_t)row_width * 4 - 1; + png_bytep dp = sp + (png_size_t)row_width * 4; + for (i = 0; i < row_width; i++) + { + *(dp--) = *(sp--); + *(dp--) = *(sp--); + *(dp--) = *sp; + *(dp--) = *(sp - 1); + *(dp--) = *sp; + *(dp--) = *(sp - 1); + *(dp--) = *(sp--); + *(dp--) = *(sp--); + } + } + } + row_info->channels += (png_byte)2; + row_info->color_type |= PNG_COLOR_MASK_COLOR; + row_info->pixel_depth = (png_byte)(row_info->channels * + row_info->bit_depth); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } +} +#endif + +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +/* reduce RGB files to grayscale, with or without alpha + * using the equation given in Poynton's ColorFAQ at + * + * Copyright (c) 1998-01-04 Charles Poynton poynton at inforamp.net + * + * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B + * + * We approximate this with + * + * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B + * + * which can be expressed with integers as + * + * Y = (6969 * R + 23434 * G + 2365 * B)/32768 + * + * The calculation is to be done in a linear colorspace. + * + * Other integer coefficents can be used via png_set_rgb_to_gray(). + */ +int /* PRIVATE */ +png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) + +{ + png_uint_32 i; + + png_uint_32 row_width = row_info->width; + int rgb_error = 0; + + png_debug(1, "in png_do_rgb_to_gray\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + (row_info->color_type & PNG_COLOR_MASK_COLOR)) + { + png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; + png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; + png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + { + if (row_info->bit_depth == 8) + { +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) + { + png_bytep sp = row; + png_bytep dp = row; + + for (i = 0; i < row_width; i++) + { + png_byte red = png_ptr->gamma_to_1[*(sp++)]; + png_byte green = png_ptr->gamma_to_1[*(sp++)]; + png_byte blue = png_ptr->gamma_to_1[*(sp++)]; + if(red != green || red != blue) + { + rgb_error |= 1; + *(dp++) = png_ptr->gamma_from_1[ + (rc*red+gc*green+bc*blue)>>15]; + } + else + *(dp++) = *(sp-1); + } + } + else +#endif + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_byte red = *(sp++); + png_byte green = *(sp++); + png_byte blue = *(sp++); + if(red != green || red != blue) + { + rgb_error |= 1; + *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15); + } + else + *(dp++) = *(sp-1); + } + } + } + + else /* RGB bit_depth == 16 */ + { +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->gamma_16_to_1 != NULL && + png_ptr->gamma_16_from_1 != NULL) + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 red, green, blue, w; + + red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + + if(red == green && red == blue) + w = red; + else + { + png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> + png_ptr->gamma_shift][red>>8]; + png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> + png_ptr->gamma_shift][green>>8]; + png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> + png_ptr->gamma_shift][blue>>8]; + png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 + + bc*blue_1)>>15); + w = png_ptr->gamma_16_from_1[(gray16&0xff) >> + png_ptr->gamma_shift][gray16 >> 8]; + rgb_error |= 1; + } + + *(dp++) = (png_byte)((w>>8) & 0xff); + *(dp++) = (png_byte)(w & 0xff); + } + } + else +#endif + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 red, green, blue, gray16; + + red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + + if(red != green || red != blue) + rgb_error |= 1; + gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); + *(dp++) = (png_byte)((gray16>>8) & 0xff); + *(dp++) = (png_byte)(gray16 & 0xff); + } + } + } + } + if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + if (row_info->bit_depth == 8) + { +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_byte red = png_ptr->gamma_to_1[*(sp++)]; + png_byte green = png_ptr->gamma_to_1[*(sp++)]; + png_byte blue = png_ptr->gamma_to_1[*(sp++)]; + if(red != green || red != blue) + rgb_error |= 1; + *(dp++) = png_ptr->gamma_from_1 + [(rc*red + gc*green + bc*blue)>>15]; + *(dp++) = *(sp++); /* alpha */ + } + } + else +#endif + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_byte red = *(sp++); + png_byte green = *(sp++); + png_byte blue = *(sp++); + if(red != green || red != blue) + rgb_error |= 1; + *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); + *(dp++) = *(sp++); /* alpha */ + } + } + } + else /* RGBA bit_depth == 16 */ + { +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->gamma_16_to_1 != NULL && + png_ptr->gamma_16_from_1 != NULL) + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 red, green, blue, w; + + red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; + + if(red == green && red == blue) + w = red; + else + { + png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> + png_ptr->gamma_shift][red>>8]; + png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> + png_ptr->gamma_shift][green>>8]; + png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> + png_ptr->gamma_shift][blue>>8]; + png_uint_16 gray16 = (png_uint_16)((rc * red_1 + + gc * green_1 + bc * blue_1)>>15); + w = png_ptr->gamma_16_from_1[(gray16&0xff) >> + png_ptr->gamma_shift][gray16 >> 8]; + rgb_error |= 1; + } + + *(dp++) = (png_byte)((w>>8) & 0xff); + *(dp++) = (png_byte)(w & 0xff); + *(dp++) = *(sp++); /* alpha */ + *(dp++) = *(sp++); + } + } + else +#endif + { + png_bytep sp = row; + png_bytep dp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 red, green, blue, gray16; + red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; + green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; + blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; + if(red != green || red != blue) + rgb_error |= 1; + gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); + *(dp++) = (png_byte)((gray16>>8) & 0xff); + *(dp++) = (png_byte)(gray16 & 0xff); + *(dp++) = *(sp++); /* alpha */ + *(dp++) = *(sp++); + } + } + } + } + row_info->channels -= (png_byte)2; + row_info->color_type &= ~PNG_COLOR_MASK_COLOR; + row_info->pixel_depth = (png_byte)(row_info->channels * + row_info->bit_depth); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } + return rgb_error; +} +#endif + +/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth + * large of png_color. This lets grayscale images be treated as + * paletted. Most useful for gamma correction and simplification + * of code. + */ +void PNGAPI +png_build_grayscale_palette(int bit_depth, png_colorp palette) +{ + int num_palette; + int color_inc; + int i; + int v; + + png_debug(1, "in png_do_build_grayscale_palette\n"); + if (palette == NULL) + return; + + switch (bit_depth) + { + case 1: + num_palette = 2; + color_inc = 0xff; + break; + case 2: + num_palette = 4; + color_inc = 0x55; + break; + case 4: + num_palette = 16; + color_inc = 0x11; + break; + case 8: + num_palette = 256; + color_inc = 1; + break; + default: + num_palette = 0; + color_inc = 0; + break; + } + + for (i = 0, v = 0; i < num_palette; i++, v += color_inc) + { + palette[i].red = (png_byte)v; + palette[i].green = (png_byte)v; + palette[i].blue = (png_byte)v; + } +} + +/* This function is currently unused. Do we really need it? */ +#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED) +void /* PRIVATE */ +png_correct_palette(png_structp png_ptr, png_colorp palette, + int num_palette) +{ + png_debug(1, "in png_correct_palette\n"); +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ + defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) + if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND)) + { + png_color back, back_1; + + if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) + { + back.red = png_ptr->gamma_table[png_ptr->background.red]; + back.green = png_ptr->gamma_table[png_ptr->background.green]; + back.blue = png_ptr->gamma_table[png_ptr->background.blue]; + + back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; + back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; + back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; + } + else + { + double g; + + g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); + + if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN || + fabs(g - 1.0) < PNG_GAMMA_THRESHOLD) + { + back.red = png_ptr->background.red; + back.green = png_ptr->background.green; + back.blue = png_ptr->background.blue; + } + else + { + back.red = + (png_byte)(pow((double)png_ptr->background.red/255, g) * + 255.0 + 0.5); + back.green = + (png_byte)(pow((double)png_ptr->background.green/255, g) * + 255.0 + 0.5); + back.blue = + (png_byte)(pow((double)png_ptr->background.blue/255, g) * + 255.0 + 0.5); + } + + g = 1.0 / png_ptr->background_gamma; + + back_1.red = + (png_byte)(pow((double)png_ptr->background.red/255, g) * + 255.0 + 0.5); + back_1.green = + (png_byte)(pow((double)png_ptr->background.green/255, g) * + 255.0 + 0.5); + back_1.blue = + (png_byte)(pow((double)png_ptr->background.blue/255, g) * + 255.0 + 0.5); + } + + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + png_uint_32 i; + + for (i = 0; i < (png_uint_32)num_palette; i++) + { + if (i < png_ptr->num_trans && png_ptr->trans[i] == 0) + { + palette[i] = back; + } + else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff) + { + png_byte v, w; + + v = png_ptr->gamma_to_1[png_ptr->palette[i].red]; + png_composite(w, v, png_ptr->trans[i], back_1.red); + palette[i].red = png_ptr->gamma_from_1[w]; + + v = png_ptr->gamma_to_1[png_ptr->palette[i].green]; + png_composite(w, v, png_ptr->trans[i], back_1.green); + palette[i].green = png_ptr->gamma_from_1[w]; + + v = png_ptr->gamma_to_1[png_ptr->palette[i].blue]; + png_composite(w, v, png_ptr->trans[i], back_1.blue); + palette[i].blue = png_ptr->gamma_from_1[w]; + } + else + { + palette[i].red = png_ptr->gamma_table[palette[i].red]; + palette[i].green = png_ptr->gamma_table[palette[i].green]; + palette[i].blue = png_ptr->gamma_table[palette[i].blue]; + } + } + } + else + { + int i; + + for (i = 0; i < num_palette; i++) + { + if (palette[i].red == (png_byte)png_ptr->trans_values.gray) + { + palette[i] = back; + } + else + { + palette[i].red = png_ptr->gamma_table[palette[i].red]; + palette[i].green = png_ptr->gamma_table[palette[i].green]; + palette[i].blue = png_ptr->gamma_table[palette[i].blue]; + } + } + } + } + else +#endif +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (png_ptr->transformations & PNG_GAMMA) + { + int i; + + for (i = 0; i < num_palette; i++) + { + palette[i].red = png_ptr->gamma_table[palette[i].red]; + palette[i].green = png_ptr->gamma_table[palette[i].green]; + palette[i].blue = png_ptr->gamma_table[palette[i].blue]; + } + } +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + else +#endif +#endif +#if defined(PNG_READ_BACKGROUND_SUPPORTED) + if (png_ptr->transformations & PNG_BACKGROUND) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + png_color back; + + back.red = (png_byte)png_ptr->background.red; + back.green = (png_byte)png_ptr->background.green; + back.blue = (png_byte)png_ptr->background.blue; + + for (i = 0; i < (int)png_ptr->num_trans; i++) + { + if (png_ptr->trans[i] == 0) + { + palette[i].red = back.red; + palette[i].green = back.green; + palette[i].blue = back.blue; + } + else if (png_ptr->trans[i] != 0xff) + { + png_composite(palette[i].red, png_ptr->palette[i].red, + png_ptr->trans[i], back.red); + png_composite(palette[i].green, png_ptr->palette[i].green, + png_ptr->trans[i], back.green); + png_composite(palette[i].blue, png_ptr->palette[i].blue, + png_ptr->trans[i], back.blue); + } + } + } + else /* assume grayscale palette (what else could it be?) */ + { + int i; + + for (i = 0; i < num_palette; i++) + { + if (i == (png_byte)png_ptr->trans_values.gray) + { + palette[i].red = (png_byte)png_ptr->background.red; + palette[i].green = (png_byte)png_ptr->background.green; + palette[i].blue = (png_byte)png_ptr->background.blue; + } + } + } + } +#endif +} +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) +/* Replace any alpha or transparency with the supplied background color. + * "background" is already in the screen gamma, while "background_1" is + * at a gamma of 1.0. Paletted files have already been taken care of. + */ +void /* PRIVATE */ +png_do_background(png_row_infop row_info, png_bytep row, + png_color_16p trans_values, png_color_16p background +#if defined(PNG_READ_GAMMA_SUPPORTED) + , png_color_16p background_1, + png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, + png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, + png_uint_16pp gamma_16_to_1, int gamma_shift +#endif + ) +{ + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width=row_info->width; + int shift; + + png_debug(1, "in png_do_background\n"); + if (background != NULL && +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) || + (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values))) + { + switch (row_info->color_type) + { + case PNG_COLOR_TYPE_GRAY: + { + switch (row_info->bit_depth) + { + case 1: + { + sp = row; + shift = 7; + for (i = 0; i < row_width; i++) + { + if ((png_uint_16)((*sp >> shift) & 0x01) + == trans_values->gray) + { + *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); + *sp |= (png_byte)(background->gray << shift); + } + if (!shift) + { + shift = 7; + sp++; + } + else + shift--; + } + break; + } + case 2: + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_table != NULL) + { + sp = row; + shift = 6; + for (i = 0; i < row_width; i++) + { + if ((png_uint_16)((*sp >> shift) & 0x03) + == trans_values->gray) + { + *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); + *sp |= (png_byte)(background->gray << shift); + } + else + { + png_byte p = (png_byte)((*sp >> shift) & 0x03); + png_byte g = (png_byte)((gamma_table [p | (p << 2) | + (p << 4) | (p << 6)] >> 6) & 0x03); + *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); + *sp |= (png_byte)(g << shift); + } + if (!shift) + { + shift = 6; + sp++; + } + else + shift -= 2; + } + } + else +#endif + { + sp = row; + shift = 6; + for (i = 0; i < row_width; i++) + { + if ((png_uint_16)((*sp >> shift) & 0x03) + == trans_values->gray) + { + *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); + *sp |= (png_byte)(background->gray << shift); + } + if (!shift) + { + shift = 6; + sp++; + } + else + shift -= 2; + } + } + break; + } + case 4: + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_table != NULL) + { + sp = row; + shift = 4; + for (i = 0; i < row_width; i++) + { + if ((png_uint_16)((*sp >> shift) & 0x0f) + == trans_values->gray) + { + *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); + *sp |= (png_byte)(background->gray << shift); + } + else + { + png_byte p = (png_byte)((*sp >> shift) & 0x0f); + png_byte g = (png_byte)((gamma_table[p | + (p << 4)] >> 4) & 0x0f); + *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); + *sp |= (png_byte)(g << shift); + } + if (!shift) + { + shift = 4; + sp++; + } + else + shift -= 4; + } + } + else +#endif + { + sp = row; + shift = 4; + for (i = 0; i < row_width; i++) + { + if ((png_uint_16)((*sp >> shift) & 0x0f) + == trans_values->gray) + { + *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); + *sp |= (png_byte)(background->gray << shift); + } + if (!shift) + { + shift = 4; + sp++; + } + else + shift -= 4; + } + } + break; + } + case 8: + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_table != NULL) + { + sp = row; + for (i = 0; i < row_width; i++, sp++) + { + if (*sp == trans_values->gray) + { + *sp = (png_byte)background->gray; + } + else + { + *sp = gamma_table[*sp]; + } + } + } + else +#endif + { + sp = row; + for (i = 0; i < row_width; i++, sp++) + { + if (*sp == trans_values->gray) + { + *sp = (png_byte)background->gray; + } + } + } + break; + } + case 16: + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_16 != NULL) + { + sp = row; + for (i = 0; i < row_width; i++, sp += 2) + { + png_uint_16 v; + + v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); + if (v == trans_values->gray) + { + /* background is already in screen gamma */ + *sp = (png_byte)((background->gray >> 8) & 0xff); + *(sp + 1) = (png_byte)(background->gray & 0xff); + } + else + { + v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + } + } + } + else +#endif + { + sp = row; + for (i = 0; i < row_width; i++, sp += 2) + { + png_uint_16 v; + + v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); + if (v == trans_values->gray) + { + *sp = (png_byte)((background->gray >> 8) & 0xff); + *(sp + 1) = (png_byte)(background->gray & 0xff); + } + } + } + break; + } + } + break; + } + case PNG_COLOR_TYPE_RGB: + { + if (row_info->bit_depth == 8) + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_table != NULL) + { + sp = row; + for (i = 0; i < row_width; i++, sp += 3) + { + if (*sp == trans_values->red && + *(sp + 1) == trans_values->green && + *(sp + 2) == trans_values->blue) + { + *sp = (png_byte)background->red; + *(sp + 1) = (png_byte)background->green; + *(sp + 2) = (png_byte)background->blue; + } + else + { + *sp = gamma_table[*sp]; + *(sp + 1) = gamma_table[*(sp + 1)]; + *(sp + 2) = gamma_table[*(sp + 2)]; + } + } + } + else +#endif + { + sp = row; + for (i = 0; i < row_width; i++, sp += 3) + { + if (*sp == trans_values->red && + *(sp + 1) == trans_values->green && + *(sp + 2) == trans_values->blue) + { + *sp = (png_byte)background->red; + *(sp + 1) = (png_byte)background->green; + *(sp + 2) = (png_byte)background->blue; + } + } + } + } + else /* if (row_info->bit_depth == 16) */ + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_16 != NULL) + { + sp = row; + for (i = 0; i < row_width; i++, sp += 6) + { + png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); + png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); + png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); + if (r == trans_values->red && g == trans_values->green && + b == trans_values->blue) + { + /* background is already in screen gamma */ + *sp = (png_byte)((background->red >> 8) & 0xff); + *(sp + 1) = (png_byte)(background->red & 0xff); + *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); + *(sp + 3) = (png_byte)(background->green & 0xff); + *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); + *(sp + 5) = (png_byte)(background->blue & 0xff); + } + else + { + png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; + *(sp + 2) = (png_byte)((v >> 8) & 0xff); + *(sp + 3) = (png_byte)(v & 0xff); + v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; + *(sp + 4) = (png_byte)((v >> 8) & 0xff); + *(sp + 5) = (png_byte)(v & 0xff); + } + } + } + else +#endif + { + sp = row; + for (i = 0; i < row_width; i++, sp += 6) + { + png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1)); + png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); + png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); + + if (r == trans_values->red && g == trans_values->green && + b == trans_values->blue) + { + *sp = (png_byte)((background->red >> 8) & 0xff); + *(sp + 1) = (png_byte)(background->red & 0xff); + *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); + *(sp + 3) = (png_byte)(background->green & 0xff); + *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); + *(sp + 5) = (png_byte)(background->blue & 0xff); + } + } + } + } + break; + } + case PNG_COLOR_TYPE_GRAY_ALPHA: + { + if (row_info->bit_depth == 8) + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_to_1 != NULL && gamma_from_1 != NULL && + gamma_table != NULL) + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 2, dp++) + { + png_uint_16 a = *(sp + 1); + + if (a == 0xff) + { + *dp = gamma_table[*sp]; + } + else if (a == 0) + { + /* background is already in screen gamma */ + *dp = (png_byte)background->gray; + } + else + { + png_byte v, w; + + v = gamma_to_1[*sp]; + png_composite(w, v, a, background_1->gray); + *dp = gamma_from_1[w]; + } + } + } + else +#endif + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 2, dp++) + { + png_byte a = *(sp + 1); + + if (a == 0xff) + { + *dp = *sp; + } +#if defined(PNG_READ_GAMMA_SUPPORTED) + else if (a == 0) + { + *dp = (png_byte)background->gray; + } + else + { + png_composite(*dp, *sp, a, background_1->gray); + } +#else + *dp = (png_byte)background->gray; +#endif + } + } + } + else /* if (png_ptr->bit_depth == 16) */ + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_16 != NULL && gamma_16_from_1 != NULL && + gamma_16_to_1 != NULL) + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 4, dp += 2) + { + png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); + + if (a == (png_uint_16)0xffff) + { + png_uint_16 v; + + v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; + *dp = (png_byte)((v >> 8) & 0xff); + *(dp + 1) = (png_byte)(v & 0xff); + } +#if defined(PNG_READ_GAMMA_SUPPORTED) + else if (a == 0) +#else + else +#endif + { + /* background is already in screen gamma */ + *dp = (png_byte)((background->gray >> 8) & 0xff); + *(dp + 1) = (png_byte)(background->gray & 0xff); + } +#if defined(PNG_READ_GAMMA_SUPPORTED) + else + { + png_uint_16 g, v, w; + + g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; + png_composite_16(v, g, a, background_1->gray); + w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; + *dp = (png_byte)((w >> 8) & 0xff); + *(dp + 1) = (png_byte)(w & 0xff); + } +#endif + } + } + else +#endif + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 4, dp += 2) + { + png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); + if (a == (png_uint_16)0xffff) + { + png_memcpy(dp, sp, 2); + } +#if defined(PNG_READ_GAMMA_SUPPORTED) + else if (a == 0) +#else + else +#endif + { + *dp = (png_byte)((background->gray >> 8) & 0xff); + *(dp + 1) = (png_byte)(background->gray & 0xff); + } +#if defined(PNG_READ_GAMMA_SUPPORTED) + else + { + png_uint_16 g, v; + + g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); + png_composite_16(v, g, a, background_1->gray); + *dp = (png_byte)((v >> 8) & 0xff); + *(dp + 1) = (png_byte)(v & 0xff); + } +#endif + } + } + } + break; + } + case PNG_COLOR_TYPE_RGB_ALPHA: + { + if (row_info->bit_depth == 8) + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_to_1 != NULL && gamma_from_1 != NULL && + gamma_table != NULL) + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 4, dp += 3) + { + png_byte a = *(sp + 3); + + if (a == 0xff) + { + *dp = gamma_table[*sp]; + *(dp + 1) = gamma_table[*(sp + 1)]; + *(dp + 2) = gamma_table[*(sp + 2)]; + } + else if (a == 0) + { + /* background is already in screen gamma */ + *dp = (png_byte)background->red; + *(dp + 1) = (png_byte)background->green; + *(dp + 2) = (png_byte)background->blue; + } + else + { + png_byte v, w; + + v = gamma_to_1[*sp]; + png_composite(w, v, a, background_1->red); + *dp = gamma_from_1[w]; + v = gamma_to_1[*(sp + 1)]; + png_composite(w, v, a, background_1->green); + *(dp + 1) = gamma_from_1[w]; + v = gamma_to_1[*(sp + 2)]; + png_composite(w, v, a, background_1->blue); + *(dp + 2) = gamma_from_1[w]; + } + } + } + else +#endif + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 4, dp += 3) + { + png_byte a = *(sp + 3); + + if (a == 0xff) + { + *dp = *sp; + *(dp + 1) = *(sp + 1); + *(dp + 2) = *(sp + 2); + } + else if (a == 0) + { + *dp = (png_byte)background->red; + *(dp + 1) = (png_byte)background->green; + *(dp + 2) = (png_byte)background->blue; + } + else + { + png_composite(*dp, *sp, a, background->red); + png_composite(*(dp + 1), *(sp + 1), a, + background->green); + png_composite(*(dp + 2), *(sp + 2), a, + background->blue); + } + } + } + } + else /* if (row_info->bit_depth == 16) */ + { +#if defined(PNG_READ_GAMMA_SUPPORTED) + if (gamma_16 != NULL && gamma_16_from_1 != NULL && + gamma_16_to_1 != NULL) + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 8, dp += 6) + { + png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) + << 8) + (png_uint_16)(*(sp + 7))); + if (a == (png_uint_16)0xffff) + { + png_uint_16 v; + + v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; + *dp = (png_byte)((v >> 8) & 0xff); + *(dp + 1) = (png_byte)(v & 0xff); + v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; + *(dp + 2) = (png_byte)((v >> 8) & 0xff); + *(dp + 3) = (png_byte)(v & 0xff); + v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; + *(dp + 4) = (png_byte)((v >> 8) & 0xff); + *(dp + 5) = (png_byte)(v & 0xff); + } + else if (a == 0) + { + /* background is already in screen gamma */ + *dp = (png_byte)((background->red >> 8) & 0xff); + *(dp + 1) = (png_byte)(background->red & 0xff); + *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); + *(dp + 3) = (png_byte)(background->green & 0xff); + *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); + *(dp + 5) = (png_byte)(background->blue & 0xff); + } + else + { + png_uint_16 v, w, x; + + v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; + png_composite_16(w, v, a, background_1->red); + x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; + *dp = (png_byte)((x >> 8) & 0xff); + *(dp + 1) = (png_byte)(x & 0xff); + v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; + png_composite_16(w, v, a, background_1->green); + x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; + *(dp + 2) = (png_byte)((x >> 8) & 0xff); + *(dp + 3) = (png_byte)(x & 0xff); + v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; + png_composite_16(w, v, a, background_1->blue); + x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8]; + *(dp + 4) = (png_byte)((x >> 8) & 0xff); + *(dp + 5) = (png_byte)(x & 0xff); + } + } + } + else +#endif + { + sp = row; + dp = row; + for (i = 0; i < row_width; i++, sp += 8, dp += 6) + { + png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) + << 8) + (png_uint_16)(*(sp + 7))); + if (a == (png_uint_16)0xffff) + { + png_memcpy(dp, sp, 6); + } + else if (a == 0) + { + *dp = (png_byte)((background->red >> 8) & 0xff); + *(dp + 1) = (png_byte)(background->red & 0xff); + *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); + *(dp + 3) = (png_byte)(background->green & 0xff); + *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); + *(dp + 5) = (png_byte)(background->blue & 0xff); + } + else + { + png_uint_16 v; + + png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); + png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) + + *(sp + 3)); + png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) + + *(sp + 5)); + + png_composite_16(v, r, a, background->red); + *dp = (png_byte)((v >> 8) & 0xff); + *(dp + 1) = (png_byte)(v & 0xff); + png_composite_16(v, g, a, background->green); + *(dp + 2) = (png_byte)((v >> 8) & 0xff); + *(dp + 3) = (png_byte)(v & 0xff); + png_composite_16(v, b, a, background->blue); + *(dp + 4) = (png_byte)((v >> 8) & 0xff); + *(dp + 5) = (png_byte)(v & 0xff); + } + } + } + } + break; + } + } + + if (row_info->color_type & PNG_COLOR_MASK_ALPHA) + { + row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; + row_info->channels--; + row_info->pixel_depth = (png_byte)(row_info->channels * + row_info->bit_depth); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } + } +} +#endif + +#if defined(PNG_READ_GAMMA_SUPPORTED) +/* Gamma correct the image, avoiding the alpha channel. Make sure + * you do this after you deal with the transparency issue on grayscale + * or RGB images. If your bit depth is 8, use gamma_table, if it + * is 16, use gamma_16_table and gamma_shift. Build these with + * build_gamma_table(). + */ +void /* PRIVATE */ +png_do_gamma(png_row_infop row_info, png_bytep row, + png_bytep gamma_table, png_uint_16pp gamma_16_table, + int gamma_shift) +{ + png_bytep sp; + png_uint_32 i; + png_uint_32 row_width=row_info->width; + + png_debug(1, "in png_do_gamma\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + ((row_info->bit_depth <= 8 && gamma_table != NULL) || + (row_info->bit_depth == 16 && gamma_16_table != NULL))) + { + switch (row_info->color_type) + { + case PNG_COLOR_TYPE_RGB: + { + if (row_info->bit_depth == 8) + { + sp = row; + for (i = 0; i < row_width; i++) + { + *sp = gamma_table[*sp]; + sp++; + *sp = gamma_table[*sp]; + sp++; + *sp = gamma_table[*sp]; + sp++; + } + } + else /* if (row_info->bit_depth == 16) */ + { + sp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 v; + + v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + } + } + break; + } + case PNG_COLOR_TYPE_RGB_ALPHA: + { + if (row_info->bit_depth == 8) + { + sp = row; + for (i = 0; i < row_width; i++) + { + *sp = gamma_table[*sp]; + sp++; + *sp = gamma_table[*sp]; + sp++; + *sp = gamma_table[*sp]; + sp++; + sp++; + } + } + else /* if (row_info->bit_depth == 16) */ + { + sp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 4; + } + } + break; + } + case PNG_COLOR_TYPE_GRAY_ALPHA: + { + if (row_info->bit_depth == 8) + { + sp = row; + for (i = 0; i < row_width; i++) + { + *sp = gamma_table[*sp]; + sp += 2; + } + } + else /* if (row_info->bit_depth == 16) */ + { + sp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 4; + } + } + break; + } + case PNG_COLOR_TYPE_GRAY: + { + if (row_info->bit_depth == 2) + { + sp = row; + for (i = 0; i < row_width; i += 4) + { + int a = *sp & 0xc0; + int b = *sp & 0x30; + int c = *sp & 0x0c; + int d = *sp & 0x03; + + *sp = (png_byte)( + ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| + ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| + ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| + ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); + sp++; + } + } + if (row_info->bit_depth == 4) + { + sp = row; + for (i = 0; i < row_width; i += 2) + { + int msb = *sp & 0xf0; + int lsb = *sp & 0x0f; + + *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) + | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); + sp++; + } + } + else if (row_info->bit_depth == 8) + { + sp = row; + for (i = 0; i < row_width; i++) + { + *sp = gamma_table[*sp]; + sp++; + } + } + else if (row_info->bit_depth == 16) + { + sp = row; + for (i = 0; i < row_width; i++) + { + png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; + *sp = (png_byte)((v >> 8) & 0xff); + *(sp + 1) = (png_byte)(v & 0xff); + sp += 2; + } + } + break; + } + } + } +} +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) +/* Expands a palette row to an RGB or RGBA row depending + * upon whether you supply trans and num_trans. + */ +void /* PRIVATE */ +png_do_expand_palette(png_row_infop row_info, png_bytep row, + png_colorp palette, png_bytep trans, int num_trans) +{ + int shift, value; + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width=row_info->width; + + png_debug(1, "in png_do_expand_palette\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + row_info->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (row_info->bit_depth < 8) + { + switch (row_info->bit_depth) + { + case 1: + { + sp = row + (png_size_t)((row_width - 1) >> 3); + dp = row + (png_size_t)row_width - 1; + shift = 7 - (int)((row_width + 7) & 0x07); + for (i = 0; i < row_width; i++) + { + if ((*sp >> shift) & 0x01) + *dp = 1; + else + *dp = 0; + if (shift == 7) + { + shift = 0; + sp--; + } + else + shift++; + + dp--; + } + break; + } + case 2: + { + sp = row + (png_size_t)((row_width - 1) >> 2); + dp = row + (png_size_t)row_width - 1; + shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); + for (i = 0; i < row_width; i++) + { + value = (*sp >> shift) & 0x03; + *dp = (png_byte)value; + if (shift == 6) + { + shift = 0; + sp--; + } + else + shift += 2; + + dp--; + } + break; + } + case 4: + { + sp = row + (png_size_t)((row_width - 1) >> 1); + dp = row + (png_size_t)row_width - 1; + shift = (int)((row_width & 0x01) << 2); + for (i = 0; i < row_width; i++) + { + value = (*sp >> shift) & 0x0f; + *dp = (png_byte)value; + if (shift == 4) + { + shift = 0; + sp--; + } + else + shift += 4; + + dp--; + } + break; + } + } + row_info->bit_depth = 8; + row_info->pixel_depth = 8; + row_info->rowbytes = row_width; + } + switch (row_info->bit_depth) + { + case 8: + { + if (trans != NULL) + { + sp = row + (png_size_t)row_width - 1; + dp = row + (png_size_t)(row_width << 2) - 1; + + for (i = 0; i < row_width; i++) + { + if ((int)(*sp) >= num_trans) + *dp-- = 0xff; + else + *dp-- = trans[*sp]; + *dp-- = palette[*sp].blue; + *dp-- = palette[*sp].green; + *dp-- = palette[*sp].red; + sp--; + } + row_info->bit_depth = 8; + row_info->pixel_depth = 32; + row_info->rowbytes = row_width * 4; + row_info->color_type = 6; + row_info->channels = 4; + } + else + { + sp = row + (png_size_t)row_width - 1; + dp = row + (png_size_t)(row_width * 3) - 1; + + for (i = 0; i < row_width; i++) + { + *dp-- = palette[*sp].blue; + *dp-- = palette[*sp].green; + *dp-- = palette[*sp].red; + sp--; + } + row_info->bit_depth = 8; + row_info->pixel_depth = 24; + row_info->rowbytes = row_width * 3; + row_info->color_type = 2; + row_info->channels = 3; + } + break; + } + } + } +} + +/* If the bit depth < 8, it is expanded to 8. Also, if the + * transparency value is supplied, an alpha channel is built. + */ +void /* PRIVATE */ +png_do_expand(png_row_infop row_info, png_bytep row, + png_color_16p trans_value) +{ + int shift, value; + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width=row_info->width; + + png_debug(1, "in png_do_expand\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + if (row_info->color_type == PNG_COLOR_TYPE_GRAY) + { + png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0); + + if (row_info->bit_depth < 8) + { + switch (row_info->bit_depth) + { + case 1: + { + gray = (png_uint_16)(gray*0xff); + sp = row + (png_size_t)((row_width - 1) >> 3); + dp = row + (png_size_t)row_width - 1; + shift = 7 - (int)((row_width + 7) & 0x07); + for (i = 0; i < row_width; i++) + { + if ((*sp >> shift) & 0x01) + *dp = 0xff; + else + *dp = 0; + if (shift == 7) + { + shift = 0; + sp--; + } + else + shift++; + + dp--; + } + break; + } + case 2: + { + gray = (png_uint_16)(gray*0x55); + sp = row + (png_size_t)((row_width - 1) >> 2); + dp = row + (png_size_t)row_width - 1; + shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); + for (i = 0; i < row_width; i++) + { + value = (*sp >> shift) & 0x03; + *dp = (png_byte)(value | (value << 2) | (value << 4) | + (value << 6)); + if (shift == 6) + { + shift = 0; + sp--; + } + else + shift += 2; + + dp--; + } + break; + } + case 4: + { + gray = (png_uint_16)(gray*0x11); + sp = row + (png_size_t)((row_width - 1) >> 1); + dp = row + (png_size_t)row_width - 1; + shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); + for (i = 0; i < row_width; i++) + { + value = (*sp >> shift) & 0x0f; + *dp = (png_byte)(value | (value << 4)); + if (shift == 4) + { + shift = 0; + sp--; + } + else + shift = 4; + + dp--; + } + break; + } + } + row_info->bit_depth = 8; + row_info->pixel_depth = 8; + row_info->rowbytes = row_width; + } + + if (trans_value != NULL) + { + if (row_info->bit_depth == 8) + { + sp = row + (png_size_t)row_width - 1; + dp = row + (png_size_t)(row_width << 1) - 1; + for (i = 0; i < row_width; i++) + { + if (*sp == gray) + *dp-- = 0; + else + *dp-- = 0xff; + *dp-- = *sp--; + } + } + else if (row_info->bit_depth == 16) + { + sp = row + row_info->rowbytes - 1; + dp = row + (row_info->rowbytes << 1) - 1; + for (i = 0; i < row_width; i++) + { + if (((png_uint_16)*(sp) | + ((png_uint_16)*(sp - 1) << 8)) == gray) + { + *dp-- = 0; + *dp-- = 0; + } + else + { + *dp-- = 0xff; + *dp-- = 0xff; + } + *dp-- = *sp--; + *dp-- = *sp--; + } + } + row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; + row_info->channels = 2; + row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, + row_width); + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) + { + if (row_info->bit_depth == 8) + { + sp = row + (png_size_t)row_info->rowbytes - 1; + dp = row + (png_size_t)(row_width << 2) - 1; + for (i = 0; i < row_width; i++) + { + if (*(sp - 2) == trans_value->red && + *(sp - 1) == trans_value->green && + *(sp - 0) == trans_value->blue) + *dp-- = 0; + else + *dp-- = 0xff; + *dp-- = *sp--; + *dp-- = *sp--; + *dp-- = *sp--; + } + } + else if (row_info->bit_depth == 16) + { + sp = row + row_info->rowbytes - 1; + dp = row + (png_size_t)(row_width << 3) - 1; + for (i = 0; i < row_width; i++) + { + if ((((png_uint_16)*(sp - 4) | + ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) && + (((png_uint_16)*(sp - 2) | + ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) && + (((png_uint_16)*(sp - 0) | + ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue)) + { + *dp-- = 0; + *dp-- = 0; + } + else + { + *dp-- = 0xff; + *dp-- = 0xff; + } + *dp-- = *sp--; + *dp-- = *sp--; + *dp-- = *sp--; + *dp-- = *sp--; + *dp-- = *sp--; + *dp-- = *sp--; + } + } + row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; + row_info->channels = 4; + row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } + } +} +#endif + +#if defined(PNG_READ_DITHER_SUPPORTED) +void /* PRIVATE */ +png_do_dither(png_row_infop row_info, png_bytep row, + png_bytep palette_lookup, png_bytep dither_lookup) +{ + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width=row_info->width; + + png_debug(1, "in png_do_dither\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + if (row_info->color_type == PNG_COLOR_TYPE_RGB && + palette_lookup && row_info->bit_depth == 8) + { + int r, g, b, p; + sp = row; + dp = row; + for (i = 0; i < row_width; i++) + { + r = *sp++; + g = *sp++; + b = *sp++; + + /* this looks real messy, but the compiler will reduce + it down to a reasonable formula. For example, with + 5 bits per color, we get: + p = (((r >> 3) & 0x1f) << 10) | + (((g >> 3) & 0x1f) << 5) | + ((b >> 3) & 0x1f); + */ + p = (((r >> (8 - PNG_DITHER_RED_BITS)) & + ((1 << PNG_DITHER_RED_BITS) - 1)) << + (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | + (((g >> (8 - PNG_DITHER_GREEN_BITS)) & + ((1 << PNG_DITHER_GREEN_BITS) - 1)) << + (PNG_DITHER_BLUE_BITS)) | + ((b >> (8 - PNG_DITHER_BLUE_BITS)) & + ((1 << PNG_DITHER_BLUE_BITS) - 1)); + + *dp++ = palette_lookup[p]; + } + row_info->color_type = PNG_COLOR_TYPE_PALETTE; + row_info->channels = 1; + row_info->pixel_depth = row_info->bit_depth; + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && + palette_lookup != NULL && row_info->bit_depth == 8) + { + int r, g, b, p; + sp = row; + dp = row; + for (i = 0; i < row_width; i++) + { + r = *sp++; + g = *sp++; + b = *sp++; + sp++; + + p = (((r >> (8 - PNG_DITHER_RED_BITS)) & + ((1 << PNG_DITHER_RED_BITS) - 1)) << + (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | + (((g >> (8 - PNG_DITHER_GREEN_BITS)) & + ((1 << PNG_DITHER_GREEN_BITS) - 1)) << + (PNG_DITHER_BLUE_BITS)) | + ((b >> (8 - PNG_DITHER_BLUE_BITS)) & + ((1 << PNG_DITHER_BLUE_BITS) - 1)); + + *dp++ = palette_lookup[p]; + } + row_info->color_type = PNG_COLOR_TYPE_PALETTE; + row_info->channels = 1; + row_info->pixel_depth = row_info->bit_depth; + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width); + } + else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && + dither_lookup && row_info->bit_depth == 8) + { + sp = row; + for (i = 0; i < row_width; i++, sp++) + { + *sp = dither_lookup[*sp]; + } + } + } +} +#endif + +#ifdef PNG_FLOATING_POINT_SUPPORTED +#if defined(PNG_READ_GAMMA_SUPPORTED) +static int png_gamma_shift[] = + {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0}; + +/* We build the 8- or 16-bit gamma tables here. Note that for 16-bit + * tables, we don't make a full table if we are reducing to 8-bit in + * the future. Note also how the gamma_16 tables are segmented so that + * we don't need to allocate > 64K chunks for a full 16-bit table. + */ +void /* PRIVATE */ +png_build_gamma_table(png_structp png_ptr) +{ + png_debug(1, "in png_build_gamma_table\n"); + if(png_ptr->gamma != 0.0) + { + if (png_ptr->bit_depth <= 8) + { + int i; + double g; + + if (png_ptr->screen_gamma > .000001) + g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); + else + g = 1.0; + + png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, + (png_uint_32)256); + + for (i = 0; i < 256; i++) + { + png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, + g) * 255.0 + .5); + } + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ + defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) + if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) + { + + g = 1.0 / (png_ptr->gamma); + + png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, + (png_uint_32)256); + + for (i = 0; i < 256; i++) + { + png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, + g) * 255.0 + .5); + } + + + png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, + (png_uint_32)256); + + if(png_ptr->screen_gamma > 0.000001) + g = 1.0 / png_ptr->screen_gamma; + else + g = png_ptr->gamma; /* probably doing rgb_to_gray */ + + for (i = 0; i < 256; i++) + { + png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, + g) * 255.0 + .5); + + } + } +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ + } + else + { + double g; + int i, j, shift, num; + int sig_bit; + png_uint_32 ig; + + if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + { + sig_bit = (int)png_ptr->sig_bit.red; + if ((int)png_ptr->sig_bit.green > sig_bit) + sig_bit = png_ptr->sig_bit.green; + if ((int)png_ptr->sig_bit.blue > sig_bit) + sig_bit = png_ptr->sig_bit.blue; + } + else + { + sig_bit = (int)png_ptr->sig_bit.gray; + } + + if (sig_bit > 0) + shift = 16 - sig_bit; + else + shift = 0; + + if (png_ptr->transformations & PNG_16_TO_8) + { + if (shift < (16 - PNG_MAX_GAMMA_8)) + shift = (16 - PNG_MAX_GAMMA_8); + } + + if (shift > 8) + shift = 8; + if (shift < 0) + shift = 0; + + png_ptr->gamma_shift = (png_byte)shift; + + num = (1 << (8 - shift)); + + if (png_ptr->screen_gamma > .000001) + g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); + else + g = 1.0; + + png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr, + (png_uint_32)(num * png_sizeof (png_uint_16p))); + + if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) + { + double fin, fout; + png_uint_32 last, max; + + for (i = 0; i < num; i++) + { + png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(256 * png_sizeof (png_uint_16))); + } + + g = 1.0 / g; + last = 0; + for (i = 0; i < 256; i++) + { + fout = ((double)i + 0.5) / 256.0; + fin = pow(fout, g); + max = (png_uint_32)(fin * (double)((png_uint_32)num << 8)); + while (last <= max) + { + png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] + [(int)(last >> (8 - shift))] = (png_uint_16)( + (png_uint_16)i | ((png_uint_16)i << 8)); + last++; + } + } + while (last < ((png_uint_32)num << 8)) + { + png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] + [(int)(last >> (8 - shift))] = (png_uint_16)65535L; + last++; + } + } + else + { + for (i = 0; i < num; i++) + { + png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(256 * png_sizeof (png_uint_16))); + + ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); + for (j = 0; j < 256; j++) + { + png_ptr->gamma_16_table[i][j] = + (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / + 65535.0, g) * 65535.0 + .5); + } + } + } + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ + defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) + if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) + { + + g = 1.0 / (png_ptr->gamma); + + png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr, + (png_uint_32)(num * png_sizeof (png_uint_16p ))); + + for (i = 0; i < num; i++) + { + png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(256 * png_sizeof (png_uint_16))); + + ig = (((png_uint_32)i * + (png_uint_32)png_gamma_shift[shift]) >> 4); + for (j = 0; j < 256; j++) + { + png_ptr->gamma_16_to_1[i][j] = + (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / + 65535.0, g) * 65535.0 + .5); + } + } + + if(png_ptr->screen_gamma > 0.000001) + g = 1.0 / png_ptr->screen_gamma; + else + g = png_ptr->gamma; /* probably doing rgb_to_gray */ + + png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, + (png_uint_32)(num * png_sizeof (png_uint_16p))); + + for (i = 0; i < num; i++) + { + png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(256 * png_sizeof (png_uint_16))); + + ig = (((png_uint_32)i * + (png_uint_32)png_gamma_shift[shift]) >> 4); + for (j = 0; j < 256; j++) + { + png_ptr->gamma_16_from_1[i][j] = + (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / + 65535.0, g) * 65535.0 + .5); + } + } + } +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ + } + } +} +#endif +/* To do: install integer version of png_build_gamma_table here */ +#endif + +#if defined(PNG_MNG_FEATURES_SUPPORTED) +/* undoes intrapixel differencing */ +void /* PRIVATE */ +png_do_read_intrapixel(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_read_intrapixel\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + (row_info->color_type & PNG_COLOR_MASK_COLOR)) + { + int bytes_per_pixel; + png_uint_32 row_width = row_info->width; + if (row_info->bit_depth == 8) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 3; + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 4; + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); + *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); + } + } + else if (row_info->bit_depth == 16) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 6; + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 8; + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + png_uint_32 s0 = (*(rp ) << 8) | *(rp+1); + png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3); + png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5); + png_uint_32 red = (png_uint_32)((s0+s1+65536L) & 0xffffL); + png_uint_32 blue = (png_uint_32)((s2+s1+65536L) & 0xffffL); + *(rp ) = (png_byte)((red >> 8) & 0xff); + *(rp+1) = (png_byte)(red & 0xff); + *(rp+4) = (png_byte)((blue >> 8) & 0xff); + *(rp+5) = (png_byte)(blue & 0xff); + } + } + } +} +#endif /* PNG_MNG_FEATURES_SUPPORTED */ diff --git a/libpng/pngrutil.c b/libpng/pngrutil.c new file mode 100644 index 00000000..99e466f1 --- /dev/null +++ b/libpng/pngrutil.c @@ -0,0 +1,3124 @@ +/* pngrutil.c - utilities to read a PNG file + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file contains routines that are only called from within + * libpng itself during the course of reading an image. + */ + +#define PNG_INTERNAL +#include "png.h" + +#if defined(_WIN32_WCE) +/* strtod() function is not supported on WindowsCE */ +# ifdef PNG_FLOATING_POINT_SUPPORTED +__inline double strtod(const char *nptr, char **endptr) +{ + double result = 0; + int len; + wchar_t *str, *end; + + len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0); + str = (wchar_t *)malloc(len * sizeof(wchar_t)); + if ( NULL != str ) + { + MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len); + result = wcstod(str, &end); + len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL); + *endptr = (char *)nptr + (png_strlen(nptr) - len + 1); + free(str); + } + return result; +} +# endif +#endif + +png_uint_32 /* PRIVATE */ +png_get_uint_31(png_structp png_ptr, png_bytep buf) +{ + png_uint_32 i = png_get_uint_32(buf); + if (i > PNG_UINT_31_MAX) + png_error(png_ptr, "PNG unsigned integer out of range.\n"); + return (i); +} +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED +/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ +png_uint_32 /* PRIVATE */ +png_get_uint_32(png_bytep buf) +{ + png_uint_32 i = ((png_uint_32)(*buf) << 24) + + ((png_uint_32)(*(buf + 1)) << 16) + + ((png_uint_32)(*(buf + 2)) << 8) + + (png_uint_32)(*(buf + 3)); + + return (i); +} + +#if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_oFFs_SUPPORTED) +/* Grab a signed 32-bit integer from a buffer in big-endian format. The + * data is stored in the PNG file in two's complement format, and it is + * assumed that the machine format for signed integers is the same. */ +png_int_32 /* PRIVATE */ +png_get_int_32(png_bytep buf) +{ + png_int_32 i = ((png_int_32)(*buf) << 24) + + ((png_int_32)(*(buf + 1)) << 16) + + ((png_int_32)(*(buf + 2)) << 8) + + (png_int_32)(*(buf + 3)); + + return (i); +} +#endif /* PNG_READ_pCAL_SUPPORTED */ + +/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ +png_uint_16 /* PRIVATE */ +png_get_uint_16(png_bytep buf) +{ + png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + + (png_uint_16)(*(buf + 1))); + + return (i); +} +#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ + +/* Read data, and (optionally) run it through the CRC. */ +void /* PRIVATE */ +png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) +{ + png_read_data(png_ptr, buf, length); + png_calculate_crc(png_ptr, buf, length); +} + +/* Optionally skip data and then check the CRC. Depending on whether we + are reading a ancillary or critical chunk, and how the program has set + things up, we may calculate the CRC on the data and print a message. + Returns '1' if there was a CRC error, '0' otherwise. */ +int /* PRIVATE */ +png_crc_finish(png_structp png_ptr, png_uint_32 skip) +{ + png_size_t i; + png_size_t istop = png_ptr->zbuf_size; + + for (i = (png_size_t)skip; i > istop; i -= istop) + { + png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); + } + if (i) + { + png_crc_read(png_ptr, png_ptr->zbuf, i); + } + + if (png_crc_error(png_ptr)) + { + if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */ + !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || + (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ + (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))) + { + png_chunk_warning(png_ptr, "CRC error"); + } + else + { + png_chunk_error(png_ptr, "CRC error"); + } + return (1); + } + + return (0); +} + +/* Compare the CRC stored in the PNG file with that calculated by libpng from + the data it has read thus far. */ +int /* PRIVATE */ +png_crc_error(png_structp png_ptr) +{ + png_byte crc_bytes[4]; + png_uint_32 crc; + int need_crc = 1; + + if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ + { + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == + (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) + need_crc = 0; + } + else /* critical */ + { + if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + need_crc = 0; + } + + png_read_data(png_ptr, crc_bytes, 4); + + if (need_crc) + { + crc = png_get_uint_32(crc_bytes); + return ((int)(crc != png_ptr->crc)); + } + else + return (0); +} + +#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ + defined(PNG_READ_iCCP_SUPPORTED) +/* + * Decompress trailing data in a chunk. The assumption is that chunkdata + * points at an allocated area holding the contents of a chunk with a + * trailing compressed part. What we get back is an allocated area + * holding the original prefix part and an uncompressed version of the + * trailing part (the malloc area passed in is freed). + */ +png_charp /* PRIVATE */ +png_decompress_chunk(png_structp png_ptr, int comp_type, + png_charp chunkdata, png_size_t chunklength, + png_size_t prefix_size, png_size_t *newlength) +{ + static char msg[] = "Error decoding compressed text"; + png_charp text; + png_size_t text_size; + + if (comp_type == PNG_COMPRESSION_TYPE_BASE) + { + int ret = Z_OK; + png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size); + png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + + text_size = 0; + text = NULL; + + while (png_ptr->zstream.avail_in) + { + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); + if (ret != Z_OK && ret != Z_STREAM_END) + { + if (png_ptr->zstream.msg != NULL) + png_warning(png_ptr, png_ptr->zstream.msg); + else + png_warning(png_ptr, msg); + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; + + if (text == NULL) + { + text_size = prefix_size + png_sizeof(msg) + 1; + text = (png_charp)png_malloc_warn(png_ptr, text_size); + if (text == NULL) + { + png_free(png_ptr,chunkdata); + png_error(png_ptr,"Not enough memory to decompress chunk"); + } + png_memcpy(text, chunkdata, prefix_size); + } + + text[text_size - 1] = 0x00; + + /* Copy what we can of the error message into the text chunk */ + text_size = (png_size_t)(chunklength - (text - chunkdata) - 1); + text_size = png_sizeof(msg) > text_size ? text_size : + png_sizeof(msg); + png_memcpy(text + prefix_size, msg, text_size + 1); + break; + } + if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) + { + if (text == NULL) + { + text_size = prefix_size + + png_ptr->zbuf_size - png_ptr->zstream.avail_out; + text = (png_charp)png_malloc_warn(png_ptr, text_size + 1); + if (text == NULL) + { + png_free(png_ptr,chunkdata); + png_error(png_ptr,"Not enough memory to decompress chunk."); + } + png_memcpy(text + prefix_size, png_ptr->zbuf, + text_size - prefix_size); + png_memcpy(text, chunkdata, prefix_size); + *(text + text_size) = 0x00; + } + else + { + png_charp tmp; + + tmp = text; + text = (png_charp)png_malloc_warn(png_ptr, + (png_uint_32)(text_size + + png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); + if (text == NULL) + { + png_free(png_ptr, tmp); + png_free(png_ptr, chunkdata); + png_error(png_ptr,"Not enough memory to decompress chunk.."); + } + png_memcpy(text, tmp, text_size); + png_free(png_ptr, tmp); + png_memcpy(text + text_size, png_ptr->zbuf, + (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); + text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; + *(text + text_size) = 0x00; + } + if (ret == Z_STREAM_END) + break; + else + { + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + } + } + } + if (ret != Z_STREAM_END) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char umsg[50]; + + if (ret == Z_BUF_ERROR) + sprintf(umsg,"Buffer error in compressed datastream in %s chunk", + png_ptr->chunk_name); + else if (ret == Z_DATA_ERROR) + sprintf(umsg,"Data error in compressed datastream in %s chunk", + png_ptr->chunk_name); + else + sprintf(umsg,"Incomplete compressed datastream in %s chunk", + png_ptr->chunk_name); + png_warning(png_ptr, umsg); +#else + png_warning(png_ptr, + "Incomplete compressed datastream in chunk other than IDAT"); +#endif + text_size=prefix_size; + if (text == NULL) + { + text = (png_charp)png_malloc_warn(png_ptr, text_size+1); + if (text == NULL) + { + png_free(png_ptr, chunkdata); + png_error(png_ptr,"Not enough memory for text."); + } + png_memcpy(text, chunkdata, prefix_size); + } + *(text + text_size) = 0x00; + } + + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; + + png_free(png_ptr, chunkdata); + chunkdata = text; + *newlength=text_size; + } + else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char umsg[50]; + + sprintf(umsg, "Unknown zTXt compression type %d", comp_type); + png_warning(png_ptr, umsg); +#else + png_warning(png_ptr, "Unknown zTXt compression type"); +#endif + + *(chunkdata + prefix_size) = 0x00; + *newlength=prefix_size; + } + + return chunkdata; +} +#endif + +/* read and check the IDHR chunk */ +void /* PRIVATE */ +png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte buf[13]; + png_uint_32 width, height; + int bit_depth, color_type, compression_type, filter_type; + int interlace_type; + + png_debug(1, "in png_handle_IHDR\n"); + + if (png_ptr->mode & PNG_HAVE_IHDR) + png_error(png_ptr, "Out of place IHDR"); + + /* check the length */ + if (length != 13) + png_error(png_ptr, "Invalid IHDR chunk"); + + png_ptr->mode |= PNG_HAVE_IHDR; + + png_crc_read(png_ptr, buf, 13); + png_crc_finish(png_ptr, 0); + + width = png_get_uint_31(png_ptr, buf); + height = png_get_uint_31(png_ptr, buf + 4); + bit_depth = buf[8]; + color_type = buf[9]; + compression_type = buf[10]; + filter_type = buf[11]; + interlace_type = buf[12]; + + /* set internal variables */ + png_ptr->width = width; + png_ptr->height = height; + png_ptr->bit_depth = (png_byte)bit_depth; + png_ptr->interlaced = (png_byte)interlace_type; + png_ptr->color_type = (png_byte)color_type; +#if defined(PNG_MNG_FEATURES_SUPPORTED) + png_ptr->filter_type = (png_byte)filter_type; +#endif + png_ptr->compression_type = (png_byte)compression_type; + + /* find number of channels */ + switch (png_ptr->color_type) + { + case PNG_COLOR_TYPE_GRAY: + case PNG_COLOR_TYPE_PALETTE: + png_ptr->channels = 1; + break; + case PNG_COLOR_TYPE_RGB: + png_ptr->channels = 3; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + png_ptr->channels = 2; + break; + case PNG_COLOR_TYPE_RGB_ALPHA: + png_ptr->channels = 4; + break; + } + + /* set up other useful info */ + png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * + png_ptr->channels); + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width); + png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth); + png_debug1(3,"channels = %d\n", png_ptr->channels); + png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes); + png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, + color_type, interlace_type, compression_type, filter_type); +} + +/* read and check the palette */ +void /* PRIVATE */ +png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_color palette[PNG_MAX_PALETTE_LENGTH]; + int num, i; +#ifndef PNG_NO_POINTER_INDEXING + png_colorp pal_ptr; +#endif + + png_debug(1, "in png_handle_PLTE\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before PLTE"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid PLTE after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + png_error(png_ptr, "Duplicate PLTE chunk"); + + png_ptr->mode |= PNG_HAVE_PLTE; + + if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) + { + png_warning(png_ptr, + "Ignoring PLTE chunk in grayscale PNG"); + png_crc_finish(png_ptr, length); + return; + } +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED) + if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) + { + png_crc_finish(png_ptr, length); + return; + } +#endif + + if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) + { + if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) + { + png_warning(png_ptr, "Invalid palette chunk"); + png_crc_finish(png_ptr, length); + return; + } + else + { + png_error(png_ptr, "Invalid palette chunk"); + } + } + + num = (int)length / 3; + +#ifndef PNG_NO_POINTER_INDEXING + for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) + { + png_byte buf[3]; + + png_crc_read(png_ptr, buf, 3); + pal_ptr->red = buf[0]; + pal_ptr->green = buf[1]; + pal_ptr->blue = buf[2]; + } +#else + for (i = 0; i < num; i++) + { + png_byte buf[3]; + + png_crc_read(png_ptr, buf, 3); + /* don't depend upon png_color being any order */ + palette[i].red = buf[0]; + palette[i].green = buf[1]; + palette[i].blue = buf[2]; + } +#endif + + /* If we actually NEED the PLTE chunk (ie for a paletted image), we do + whatever the normal CRC configuration tells us. However, if we + have an RGB image, the PLTE can be considered ancillary, so + we will act as though it is. */ +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED) + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) +#endif + { + png_crc_finish(png_ptr, 0); + } +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED) + else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ + { + /* If we don't want to use the data from an ancillary chunk, + we have two options: an error abort, or a warning and we + ignore the data in this chunk (which should be OK, since + it's considered ancillary for a RGB or RGBA image). */ + if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) + { + if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) + { + png_chunk_error(png_ptr, "CRC error"); + } + else + { + png_chunk_warning(png_ptr, "CRC error"); + return; + } + } + /* Otherwise, we (optionally) emit a warning and use the chunk. */ + else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) + { + png_chunk_warning(png_ptr, "CRC error"); + } + } +#endif + + png_set_PLTE(png_ptr, info_ptr, palette, num); + +#if defined(PNG_READ_tRNS_SUPPORTED) + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) + { + if (png_ptr->num_trans > (png_uint_16)num) + { + png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); + png_ptr->num_trans = (png_uint_16)num; + } + if (info_ptr->num_trans > (png_uint_16)num) + { + png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); + info_ptr->num_trans = (png_uint_16)num; + } + } + } +#endif + +} + +void /* PRIVATE */ +png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_debug(1, "in png_handle_IEND\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) + { + png_error(png_ptr, "No image in file"); + } + + png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); + + if (length != 0) + { + png_warning(png_ptr, "Incorrect IEND chunk length"); + } + png_crc_finish(png_ptr, length); + + if (&info_ptr == NULL) /* quiet compiler warnings about unused info_ptr */ + return; +} + +#if defined(PNG_READ_gAMA_SUPPORTED) +void /* PRIVATE */ +png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_fixed_point igamma; +#ifdef PNG_FLOATING_POINT_SUPPORTED + float file_gamma; +#endif + png_byte buf[4]; + + png_debug(1, "in png_handle_gAMA\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before gAMA"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid gAMA after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + /* Should be an error, but we can cope with it */ + png_warning(png_ptr, "Out of place gAMA chunk"); + + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) +#if defined(PNG_READ_sRGB_SUPPORTED) + && !(info_ptr->valid & PNG_INFO_sRGB) +#endif + ) + { + png_warning(png_ptr, "Duplicate gAMA chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (length != 4) + { + png_warning(png_ptr, "Incorrect gAMA chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 4); + if (png_crc_finish(png_ptr, 0)) + return; + + igamma = (png_fixed_point)png_get_uint_32(buf); + /* check for zero gamma */ + if (igamma == 0) + { + png_warning(png_ptr, + "Ignoring gAMA chunk with gamma=0"); + return; + } + +#if defined(PNG_READ_sRGB_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sRGB) + if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) + { + png_warning(png_ptr, + "Ignoring incorrect gAMA value when sRGB is also present"); +#ifndef PNG_NO_CONSOLE_IO + fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma); +#endif + return; + } +#endif /* PNG_READ_sRGB_SUPPORTED */ + +#ifdef PNG_FLOATING_POINT_SUPPORTED + file_gamma = (float)igamma / (float)100000.0; +# ifdef PNG_READ_GAMMA_SUPPORTED + png_ptr->gamma = file_gamma; +# endif + png_set_gAMA(png_ptr, info_ptr, file_gamma); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + png_set_gAMA_fixed(png_ptr, info_ptr, igamma); +#endif +} +#endif + +#if defined(PNG_READ_sBIT_SUPPORTED) +void /* PRIVATE */ +png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_size_t truelen; + png_byte buf[4]; + + png_debug(1, "in png_handle_sBIT\n"); + + buf[0] = buf[1] = buf[2] = buf[3] = 0; + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before sBIT"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid sBIT after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + { + /* Should be an error, but we can cope with it */ + png_warning(png_ptr, "Out of place sBIT chunk"); + } + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) + { + png_warning(png_ptr, "Duplicate sBIT chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + truelen = 3; + else + truelen = (png_size_t)png_ptr->channels; + + if (length != truelen || length > 4) + { + png_warning(png_ptr, "Incorrect sBIT chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, truelen); + if (png_crc_finish(png_ptr, 0)) + return; + + if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + { + png_ptr->sig_bit.red = buf[0]; + png_ptr->sig_bit.green = buf[1]; + png_ptr->sig_bit.blue = buf[2]; + png_ptr->sig_bit.alpha = buf[3]; + } + else + { + png_ptr->sig_bit.gray = buf[0]; + png_ptr->sig_bit.red = buf[0]; + png_ptr->sig_bit.green = buf[0]; + png_ptr->sig_bit.blue = buf[0]; + png_ptr->sig_bit.alpha = buf[1]; + } + png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); +} +#endif + +#if defined(PNG_READ_cHRM_SUPPORTED) +void /* PRIVATE */ +png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte buf[4]; +#ifdef PNG_FLOATING_POINT_SUPPORTED + float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; +#endif + png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, + int_y_green, int_x_blue, int_y_blue; + + png_uint_32 uint_x, uint_y; + + png_debug(1, "in png_handle_cHRM\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before cHRM"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid cHRM after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + /* Should be an error, but we can cope with it */ + png_warning(png_ptr, "Missing PLTE before cHRM"); + + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) +#if defined(PNG_READ_sRGB_SUPPORTED) + && !(info_ptr->valid & PNG_INFO_sRGB) +#endif + ) + { + png_warning(png_ptr, "Duplicate cHRM chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (length != 32) + { + png_warning(png_ptr, "Incorrect cHRM chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 4); + uint_x = png_get_uint_32(buf); + + png_crc_read(png_ptr, buf, 4); + uint_y = png_get_uint_32(buf); + + if (uint_x > 80000L || uint_y > 80000L || + uint_x + uint_y > 100000L) + { + png_warning(png_ptr, "Invalid cHRM white point"); + png_crc_finish(png_ptr, 24); + return; + } + int_x_white = (png_fixed_point)uint_x; + int_y_white = (png_fixed_point)uint_y; + + png_crc_read(png_ptr, buf, 4); + uint_x = png_get_uint_32(buf); + + png_crc_read(png_ptr, buf, 4); + uint_y = png_get_uint_32(buf); + + if (uint_x > 80000L || uint_y > 80000L || + uint_x + uint_y > 100000L) + { + png_warning(png_ptr, "Invalid cHRM red point"); + png_crc_finish(png_ptr, 16); + return; + } + int_x_red = (png_fixed_point)uint_x; + int_y_red = (png_fixed_point)uint_y; + + png_crc_read(png_ptr, buf, 4); + uint_x = png_get_uint_32(buf); + + png_crc_read(png_ptr, buf, 4); + uint_y = png_get_uint_32(buf); + + if (uint_x > 80000L || uint_y > 80000L || + uint_x + uint_y > 100000L) + { + png_warning(png_ptr, "Invalid cHRM green point"); + png_crc_finish(png_ptr, 8); + return; + } + int_x_green = (png_fixed_point)uint_x; + int_y_green = (png_fixed_point)uint_y; + + png_crc_read(png_ptr, buf, 4); + uint_x = png_get_uint_32(buf); + + png_crc_read(png_ptr, buf, 4); + uint_y = png_get_uint_32(buf); + + if (uint_x > 80000L || uint_y > 80000L || + uint_x + uint_y > 100000L) + { + png_warning(png_ptr, "Invalid cHRM blue point"); + png_crc_finish(png_ptr, 0); + return; + } + int_x_blue = (png_fixed_point)uint_x; + int_y_blue = (png_fixed_point)uint_y; + +#ifdef PNG_FLOATING_POINT_SUPPORTED + white_x = (float)int_x_white / (float)100000.0; + white_y = (float)int_y_white / (float)100000.0; + red_x = (float)int_x_red / (float)100000.0; + red_y = (float)int_y_red / (float)100000.0; + green_x = (float)int_x_green / (float)100000.0; + green_y = (float)int_y_green / (float)100000.0; + blue_x = (float)int_x_blue / (float)100000.0; + blue_y = (float)int_y_blue / (float)100000.0; +#endif + +#if defined(PNG_READ_sRGB_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sRGB) + { + if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) || + PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) || + PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) || + PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) || + PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) || + PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) || + PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) || + PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000)) + { + + png_warning(png_ptr, + "Ignoring incorrect cHRM value when sRGB is also present"); +#ifndef PNG_NO_CONSOLE_IO +#ifdef PNG_FLOATING_POINT_SUPPORTED + fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n", + white_x, white_y, red_x, red_y); + fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n", + green_x, green_y, blue_x, blue_y); +#else + fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", + int_x_white, int_y_white, int_x_red, int_y_red); + fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n", + int_x_green, int_y_green, int_x_blue, int_y_blue); +#endif +#endif /* PNG_NO_CONSOLE_IO */ + } + png_crc_finish(png_ptr, 0); + return; + } +#endif /* PNG_READ_sRGB_SUPPORTED */ + +#ifdef PNG_FLOATING_POINT_SUPPORTED + png_set_cHRM(png_ptr, info_ptr, + white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + png_set_cHRM_fixed(png_ptr, info_ptr, + int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, + int_y_green, int_x_blue, int_y_blue); +#endif + if (png_crc_finish(png_ptr, 0)) + return; +} +#endif + +#if defined(PNG_READ_sRGB_SUPPORTED) +void /* PRIVATE */ +png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + int intent; + png_byte buf[1]; + + png_debug(1, "in png_handle_sRGB\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before sRGB"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid sRGB after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + /* Should be an error, but we can cope with it */ + png_warning(png_ptr, "Out of place sRGB chunk"); + + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) + { + png_warning(png_ptr, "Duplicate sRGB chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (length != 1) + { + png_warning(png_ptr, "Incorrect sRGB chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 1); + if (png_crc_finish(png_ptr, 0)) + return; + + intent = buf[0]; + /* check for bad intent */ + if (intent >= PNG_sRGB_INTENT_LAST) + { + png_warning(png_ptr, "Unknown sRGB intent"); + return; + } + +#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) + if ((info_ptr->valid & PNG_INFO_gAMA)) + { + png_fixed_point igamma; +#ifdef PNG_FIXED_POINT_SUPPORTED + igamma=info_ptr->int_gamma; +#else +# ifdef PNG_FLOATING_POINT_SUPPORTED + igamma=(png_fixed_point)(info_ptr->gamma * 100000.); +# endif +#endif + if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) + { + png_warning(png_ptr, + "Ignoring incorrect gAMA value when sRGB is also present"); +#ifndef PNG_NO_CONSOLE_IO +# ifdef PNG_FIXED_POINT_SUPPORTED + fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma); +# else +# ifdef PNG_FLOATING_POINT_SUPPORTED + fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma); +# endif +# endif +#endif + } + } +#endif /* PNG_READ_gAMA_SUPPORTED */ + +#ifdef PNG_READ_cHRM_SUPPORTED +#ifdef PNG_FIXED_POINT_SUPPORTED + if (info_ptr->valid & PNG_INFO_cHRM) + if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) || + PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000)) + { + png_warning(png_ptr, + "Ignoring incorrect cHRM value when sRGB is also present"); + } +#endif /* PNG_FIXED_POINT_SUPPORTED */ +#endif /* PNG_READ_cHRM_SUPPORTED */ + + png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); +} +#endif /* PNG_READ_sRGB_SUPPORTED */ + +#if defined(PNG_READ_iCCP_SUPPORTED) +void /* PRIVATE */ +png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +/* Note: this does not properly handle chunks that are > 64K under DOS */ +{ + png_charp chunkdata; + png_byte compression_type; + png_bytep pC; + png_charp profile; + png_uint_32 skip = 0; + png_uint_32 profile_size, profile_length; + png_size_t slength, prefix_length, data_length; + + png_debug(1, "in png_handle_iCCP\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before iCCP"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid iCCP after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->mode & PNG_HAVE_PLTE) + /* Should be an error, but we can cope with it */ + png_warning(png_ptr, "Out of place iCCP chunk"); + + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)) + { + png_warning(png_ptr, "Duplicate iCCP chunk"); + png_crc_finish(png_ptr, length); + return; + } + +#ifdef PNG_MAX_MALLOC_64K + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "iCCP chunk too large to fit in memory"); + skip = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + chunkdata = (png_charp)png_malloc(png_ptr, length + 1); + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)chunkdata, slength); + + if (png_crc_finish(png_ptr, skip)) + { + png_free(png_ptr, chunkdata); + return; + } + + chunkdata[slength] = 0x00; + + for (profile = chunkdata; *profile; profile++) + /* empty loop to find end of name */ ; + + ++profile; + + /* there should be at least one zero (the compression type byte) + following the separator, and we should be on it */ + if ( profile >= chunkdata + slength) + { + png_free(png_ptr, chunkdata); + png_warning(png_ptr, "Malformed iCCP chunk"); + return; + } + + /* compression_type should always be zero */ + compression_type = *profile++; + if (compression_type) + { + png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); + compression_type=0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 + wrote nonzero) */ + } + + prefix_length = profile - chunkdata; + chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata, + slength, prefix_length, &data_length); + + profile_length = data_length - prefix_length; + + if ( prefix_length > data_length || profile_length < 4) + { + png_free(png_ptr, chunkdata); + png_warning(png_ptr, "Profile size field missing from iCCP chunk"); + return; + } + + /* Check the profile_size recorded in the first 32 bits of the ICC profile */ + pC = (png_bytep)(chunkdata+prefix_length); + profile_size = ((*(pC ))<<24) | + ((*(pC+1))<<16) | + ((*(pC+2))<< 8) | + ((*(pC+3)) ); + + if(profile_size < profile_length) + profile_length = profile_size; + + if(profile_size > profile_length) + { + png_free(png_ptr, chunkdata); + png_warning(png_ptr, "Ignoring truncated iCCP profile.\n"); + return; + } + + png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type, + chunkdata + prefix_length, profile_length); + png_free(png_ptr, chunkdata); +} +#endif /* PNG_READ_iCCP_SUPPORTED */ + +#if defined(PNG_READ_sPLT_SUPPORTED) +void /* PRIVATE */ +png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +/* Note: this does not properly handle chunks that are > 64K under DOS */ +{ + png_bytep chunkdata; + png_bytep entry_start; + png_sPLT_t new_palette; +#ifdef PNG_NO_POINTER_INDEXING + png_sPLT_entryp pp; +#endif + int data_length, entry_size, i; + png_uint_32 skip = 0; + png_size_t slength; + + png_debug(1, "in png_handle_sPLT\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before sPLT"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid sPLT after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + +#ifdef PNG_MAX_MALLOC_64K + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "sPLT chunk too large to fit in memory"); + skip = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + chunkdata = (png_bytep)png_malloc(png_ptr, length + 1); + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)chunkdata, slength); + + if (png_crc_finish(png_ptr, skip)) + { + png_free(png_ptr, chunkdata); + return; + } + + chunkdata[slength] = 0x00; + + for (entry_start = chunkdata; *entry_start; entry_start++) + /* empty loop to find end of name */ ; + ++entry_start; + + /* a sample depth should follow the separator, and we should be on it */ + if (entry_start > chunkdata + slength) + { + png_free(png_ptr, chunkdata); + png_warning(png_ptr, "malformed sPLT chunk"); + return; + } + + new_palette.depth = *entry_start++; + entry_size = (new_palette.depth == 8 ? 6 : 10); + data_length = (slength - (entry_start - chunkdata)); + + /* integrity-check the data length */ + if (data_length % entry_size) + { + png_free(png_ptr, chunkdata); + png_warning(png_ptr, "sPLT chunk has bad length"); + return; + } + + new_palette.nentries = (png_uint_32) (data_length / entry_size); + if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX / + png_sizeof(png_sPLT_entry))) + { + png_warning(png_ptr, "sPLT chunk too long"); + return; + } + new_palette.entries = (png_sPLT_entryp)png_malloc_warn( + png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); + if (new_palette.entries == NULL) + { + png_warning(png_ptr, "sPLT chunk requires too much memory"); + return; + } + +#ifndef PNG_NO_POINTER_INDEXING + for (i = 0; i < new_palette.nentries; i++) + { + png_sPLT_entryp pp = new_palette.entries + i; + + if (new_palette.depth == 8) + { + pp->red = *entry_start++; + pp->green = *entry_start++; + pp->blue = *entry_start++; + pp->alpha = *entry_start++; + } + else + { + pp->red = png_get_uint_16(entry_start); entry_start += 2; + pp->green = png_get_uint_16(entry_start); entry_start += 2; + pp->blue = png_get_uint_16(entry_start); entry_start += 2; + pp->alpha = png_get_uint_16(entry_start); entry_start += 2; + } + pp->frequency = png_get_uint_16(entry_start); entry_start += 2; + } +#else + pp = new_palette.entries; + for (i = 0; i < new_palette.nentries; i++) + { + + if (new_palette.depth == 8) + { + pp[i].red = *entry_start++; + pp[i].green = *entry_start++; + pp[i].blue = *entry_start++; + pp[i].alpha = *entry_start++; + } + else + { + pp[i].red = png_get_uint_16(entry_start); entry_start += 2; + pp[i].green = png_get_uint_16(entry_start); entry_start += 2; + pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; + pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; + } + pp->frequency = png_get_uint_16(entry_start); entry_start += 2; + } +#endif + + /* discard all chunk data except the name and stash that */ + new_palette.name = (png_charp)chunkdata; + + png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); + + png_free(png_ptr, chunkdata); + png_free(png_ptr, new_palette.entries); +} +#endif /* PNG_READ_sPLT_SUPPORTED */ + +#if defined(PNG_READ_tRNS_SUPPORTED) +void /* PRIVATE */ +png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; + + png_debug(1, "in png_handle_tRNS\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before tRNS"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid tRNS after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) + { + png_warning(png_ptr, "Duplicate tRNS chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) + { + png_byte buf[2]; + + if (length != 2) + { + png_warning(png_ptr, "Incorrect tRNS chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 2); + png_ptr->num_trans = 1; + png_ptr->trans_values.gray = png_get_uint_16(buf); + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) + { + png_byte buf[6]; + + if (length != 6) + { + png_warning(png_ptr, "Incorrect tRNS chunk length"); + png_crc_finish(png_ptr, length); + return; + } + png_crc_read(png_ptr, buf, (png_size_t)length); + png_ptr->num_trans = 1; + png_ptr->trans_values.red = png_get_uint_16(buf); + png_ptr->trans_values.green = png_get_uint_16(buf + 2); + png_ptr->trans_values.blue = png_get_uint_16(buf + 4); + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (!(png_ptr->mode & PNG_HAVE_PLTE)) + { + /* Should be an error, but we can cope with it. */ + png_warning(png_ptr, "Missing PLTE before tRNS"); + } + if (length > (png_uint_32)png_ptr->num_palette || + length > PNG_MAX_PALETTE_LENGTH) + { + png_warning(png_ptr, "Incorrect tRNS chunk length"); + png_crc_finish(png_ptr, length); + return; + } + if (length == 0) + { + png_warning(png_ptr, "Zero length tRNS chunk"); + png_crc_finish(png_ptr, length); + return; + } + png_crc_read(png_ptr, readbuf, (png_size_t)length); + png_ptr->num_trans = (png_uint_16)length; + } + else + { + png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_crc_finish(png_ptr, 0)) + return; + + png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, + &(png_ptr->trans_values)); +} +#endif + +#if defined(PNG_READ_bKGD_SUPPORTED) +void /* PRIVATE */ +png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_size_t truelen; + png_byte buf[6]; + + png_debug(1, "in png_handle_bKGD\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before bKGD"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid bKGD after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + !(png_ptr->mode & PNG_HAVE_PLTE)) + { + png_warning(png_ptr, "Missing PLTE before bKGD"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) + { + png_warning(png_ptr, "Duplicate bKGD chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + truelen = 1; + else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + truelen = 6; + else + truelen = 2; + + if (length != truelen) + { + png_warning(png_ptr, "Incorrect bKGD chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, truelen); + if (png_crc_finish(png_ptr, 0)) + return; + + /* We convert the index value into RGB components so that we can allow + * arbitrary RGB values for background when we have transparency, and + * so it is easy to determine the RGB values of the background color + * from the info_ptr struct. */ + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + png_ptr->background.index = buf[0]; + if(info_ptr->num_palette) + { + if(buf[0] > info_ptr->num_palette) + { + png_warning(png_ptr, "Incorrect bKGD chunk index value"); + return; + } + png_ptr->background.red = + (png_uint_16)png_ptr->palette[buf[0]].red; + png_ptr->background.green = + (png_uint_16)png_ptr->palette[buf[0]].green; + png_ptr->background.blue = + (png_uint_16)png_ptr->palette[buf[0]].blue; + } + } + else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ + { + png_ptr->background.red = + png_ptr->background.green = + png_ptr->background.blue = + png_ptr->background.gray = png_get_uint_16(buf); + } + else + { + png_ptr->background.red = png_get_uint_16(buf); + png_ptr->background.green = png_get_uint_16(buf + 2); + png_ptr->background.blue = png_get_uint_16(buf + 4); + } + + png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); +} +#endif + +#if defined(PNG_READ_hIST_SUPPORTED) +void /* PRIVATE */ +png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + unsigned int num, i; + png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; + + png_debug(1, "in png_handle_hIST\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before hIST"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid hIST after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (!(png_ptr->mode & PNG_HAVE_PLTE)) + { + png_warning(png_ptr, "Missing PLTE before hIST"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) + { + png_warning(png_ptr, "Duplicate hIST chunk"); + png_crc_finish(png_ptr, length); + return; + } + + num = length / 2 ; + if (num != (unsigned int) png_ptr->num_palette || num > + (unsigned int) PNG_MAX_PALETTE_LENGTH) + { + png_warning(png_ptr, "Incorrect hIST chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + for (i = 0; i < num; i++) + { + png_byte buf[2]; + + png_crc_read(png_ptr, buf, 2); + readbuf[i] = png_get_uint_16(buf); + } + + if (png_crc_finish(png_ptr, 0)) + return; + + png_set_hIST(png_ptr, info_ptr, readbuf); +} +#endif + +#if defined(PNG_READ_pHYs_SUPPORTED) +void /* PRIVATE */ +png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte buf[9]; + png_uint_32 res_x, res_y; + int unit_type; + + png_debug(1, "in png_handle_pHYs\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before pHYs"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid pHYs after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + { + png_warning(png_ptr, "Duplicate pHYs chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (length != 9) + { + png_warning(png_ptr, "Incorrect pHYs chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 9); + if (png_crc_finish(png_ptr, 0)) + return; + + res_x = png_get_uint_32(buf); + res_y = png_get_uint_32(buf + 4); + unit_type = buf[8]; + png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); +} +#endif + +#if defined(PNG_READ_oFFs_SUPPORTED) +void /* PRIVATE */ +png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte buf[9]; + png_int_32 offset_x, offset_y; + int unit_type; + + png_debug(1, "in png_handle_oFFs\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before oFFs"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid oFFs after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + { + png_warning(png_ptr, "Duplicate oFFs chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (length != 9) + { + png_warning(png_ptr, "Incorrect oFFs chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 9); + if (png_crc_finish(png_ptr, 0)) + return; + + offset_x = png_get_int_32(buf); + offset_y = png_get_int_32(buf + 4); + unit_type = buf[8]; + png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); +} +#endif + +#if defined(PNG_READ_pCAL_SUPPORTED) +/* read the pCAL chunk (described in the PNG Extensions document) */ +void /* PRIVATE */ +png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_charp purpose; + png_int_32 X0, X1; + png_byte type, nparams; + png_charp buf, units, endptr; + png_charpp params; + png_size_t slength; + int i; + + png_debug(1, "in png_handle_pCAL\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before pCAL"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid pCAL after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) + { + png_warning(png_ptr, "Duplicate pCAL chunk"); + png_crc_finish(png_ptr, length); + return; + } + + png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n", + length + 1); + purpose = (png_charp)png_malloc_warn(png_ptr, length + 1); + if (purpose == NULL) + { + png_warning(png_ptr, "No memory for pCAL purpose."); + return; + } + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)purpose, slength); + + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, purpose); + return; + } + + purpose[slength] = 0x00; /* null terminate the last string */ + + png_debug(3, "Finding end of pCAL purpose string\n"); + for (buf = purpose; *buf; buf++) + /* empty loop */ ; + + endptr = purpose + slength; + + /* We need to have at least 12 bytes after the purpose string + in order to get the parameter information. */ + if (endptr <= buf + 12) + { + png_warning(png_ptr, "Invalid pCAL data"); + png_free(png_ptr, purpose); + return; + } + + png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n"); + X0 = png_get_int_32((png_bytep)buf+1); + X1 = png_get_int_32((png_bytep)buf+5); + type = buf[9]; + nparams = buf[10]; + units = buf + 11; + + png_debug(3, "Checking pCAL equation type and number of parameters\n"); + /* Check that we have the right number of parameters for known + equation types. */ + if ((type == PNG_EQUATION_LINEAR && nparams != 2) || + (type == PNG_EQUATION_BASE_E && nparams != 3) || + (type == PNG_EQUATION_ARBITRARY && nparams != 3) || + (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) + { + png_warning(png_ptr, "Invalid pCAL parameters for equation type"); + png_free(png_ptr, purpose); + return; + } + else if (type >= PNG_EQUATION_LAST) + { + png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); + } + + for (buf = units; *buf; buf++) + /* Empty loop to move past the units string. */ ; + + png_debug(3, "Allocating pCAL parameters array\n"); + params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams + *png_sizeof(png_charp))) ; + if (params == NULL) + { + png_free(png_ptr, purpose); + png_warning(png_ptr, "No memory for pCAL params."); + return; + } + + /* Get pointers to the start of each parameter string. */ + for (i = 0; i < (int)nparams; i++) + { + buf++; /* Skip the null string terminator from previous parameter. */ + + png_debug1(3, "Reading pCAL parameter %d\n", i); + for (params[i] = buf; *buf != 0x00 && buf <= endptr; buf++) + /* Empty loop to move past each parameter string */ ; + + /* Make sure we haven't run out of data yet */ + if (buf > endptr) + { + png_warning(png_ptr, "Invalid pCAL data"); + png_free(png_ptr, purpose); + png_free(png_ptr, params); + return; + } + } + + png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams, + units, params); + + png_free(png_ptr, purpose); + png_free(png_ptr, params); +} +#endif + +#if defined(PNG_READ_sCAL_SUPPORTED) +/* read the sCAL chunk */ +void /* PRIVATE */ +png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_charp buffer, ep; +#ifdef PNG_FLOATING_POINT_SUPPORTED + double width, height; + png_charp vp; +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + png_charp swidth, sheight; +#endif +#endif + png_size_t slength; + + png_debug(1, "in png_handle_sCAL\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before sCAL"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid sCAL after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) + { + png_warning(png_ptr, "Duplicate sCAL chunk"); + png_crc_finish(png_ptr, length); + return; + } + + png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n", + length + 1); + buffer = (png_charp)png_malloc_warn(png_ptr, length + 1); + if (buffer == NULL) + { + png_warning(png_ptr, "Out of memory while processing sCAL chunk"); + return; + } + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)buffer, slength); + + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, buffer); + return; + } + + buffer[slength] = 0x00; /* null terminate the last string */ + + ep = buffer + 1; /* skip unit byte */ + +#ifdef PNG_FLOATING_POINT_SUPPORTED + width = strtod(ep, &vp); + if (*vp) + { + png_warning(png_ptr, "malformed width string in sCAL chunk"); + return; + } +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); + if (swidth == NULL) + { + png_warning(png_ptr, "Out of memory while processing sCAL chunk width"); + return; + } + png_memcpy(swidth, ep, (png_size_t)png_strlen(ep)); +#endif +#endif + + for (ep = buffer; *ep; ep++) + /* empty loop */ ; + ep++; + +#ifdef PNG_FLOATING_POINT_SUPPORTED + height = strtod(ep, &vp); + if (*vp) + { + png_warning(png_ptr, "malformed height string in sCAL chunk"); + return; + } +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); + if (swidth == NULL) + { + png_warning(png_ptr, "Out of memory while processing sCAL chunk height"); + return; + } + png_memcpy(sheight, ep, (png_size_t)png_strlen(ep)); +#endif +#endif + + if (buffer + slength < ep +#ifdef PNG_FLOATING_POINT_SUPPORTED + || width <= 0. || height <= 0. +#endif + ) + { + png_warning(png_ptr, "Invalid sCAL data"); + png_free(png_ptr, buffer); +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) + png_free(png_ptr, swidth); + png_free(png_ptr, sheight); +#endif + return; + } + + +#ifdef PNG_FLOATING_POINT_SUPPORTED + png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height); +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight); +#endif +#endif + + png_free(png_ptr, buffer); +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) + png_free(png_ptr, swidth); + png_free(png_ptr, sheight); +#endif +} +#endif + +#if defined(PNG_READ_tIME_SUPPORTED) +void /* PRIVATE */ +png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_byte buf[7]; + png_time mod_time; + + png_debug(1, "in png_handle_tIME\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Out of place tIME chunk"); + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) + { + png_warning(png_ptr, "Duplicate tIME chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + + if (length != 7) + { + png_warning(png_ptr, "Incorrect tIME chunk length"); + png_crc_finish(png_ptr, length); + return; + } + + png_crc_read(png_ptr, buf, 7); + if (png_crc_finish(png_ptr, 0)) + return; + + mod_time.second = buf[6]; + mod_time.minute = buf[5]; + mod_time.hour = buf[4]; + mod_time.day = buf[3]; + mod_time.month = buf[2]; + mod_time.year = png_get_uint_16(buf); + + png_set_tIME(png_ptr, info_ptr, &mod_time); +} +#endif + +#if defined(PNG_READ_tEXt_SUPPORTED) +/* Note: this does not properly handle chunks that are > 64K under DOS */ +void /* PRIVATE */ +png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_textp text_ptr; + png_charp key; + png_charp text; + png_uint_32 skip = 0; + png_size_t slength; + int ret; + + png_debug(1, "in png_handle_tEXt\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before tEXt"); + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + +#ifdef PNG_MAX_MALLOC_64K + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "tEXt chunk too large to fit in memory"); + skip = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + + key = (png_charp)png_malloc_warn(png_ptr, length + 1); + if (key == NULL) + { + png_warning(png_ptr, "No memory to process text chunk."); + return; + } + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)key, slength); + + if (png_crc_finish(png_ptr, skip)) + { + png_free(png_ptr, key); + return; + } + + key[slength] = 0x00; + + for (text = key; *text; text++) + /* empty loop to find end of key */ ; + + if (text != key + slength) + text++; + + text_ptr = (png_textp)png_malloc_warn(png_ptr, + (png_uint_32)png_sizeof(png_text)); + if (text_ptr == NULL) + { + png_warning(png_ptr, "Not enough memory to process text chunk."); + png_free(png_ptr, key); + return; + } + text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; + text_ptr->key = key; +#ifdef PNG_iTXt_SUPPORTED + text_ptr->lang = NULL; + text_ptr->lang_key = NULL; + text_ptr->itxt_length = 0; +#endif + text_ptr->text = text; + text_ptr->text_length = png_strlen(text); + + ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_free(png_ptr, key); + png_free(png_ptr, text_ptr); + if (ret) + png_warning(png_ptr, "Insufficient memory to process text chunk."); +} +#endif + +#if defined(PNG_READ_zTXt_SUPPORTED) +/* note: this does not correctly handle chunks that are > 64K under DOS */ +void /* PRIVATE */ +png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_textp text_ptr; + png_charp chunkdata; + png_charp text; + int comp_type; + int ret; + png_size_t slength, prefix_len, data_len; + + png_debug(1, "in png_handle_zTXt\n"); + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before zTXt"); + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + +#ifdef PNG_MAX_MALLOC_64K + /* We will no doubt have problems with chunks even half this size, but + there is no hard and fast rule to tell us where to stop. */ + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr,"zTXt chunk too large to fit in memory"); + png_crc_finish(png_ptr, length); + return; + } +#endif + + chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); + if (chunkdata == NULL) + { + png_warning(png_ptr,"Out of memory processing zTXt chunk."); + return; + } + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)chunkdata, slength); + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, chunkdata); + return; + } + + chunkdata[slength] = 0x00; + + for (text = chunkdata; *text; text++) + /* empty loop */ ; + + /* zTXt must have some text after the chunkdataword */ + if (text == chunkdata + slength) + { + comp_type = PNG_TEXT_COMPRESSION_NONE; + png_warning(png_ptr, "Zero length zTXt chunk"); + } + else + { + comp_type = *(++text); + if (comp_type != PNG_TEXT_COMPRESSION_zTXt) + { + png_warning(png_ptr, "Unknown compression type in zTXt chunk"); + comp_type = PNG_TEXT_COMPRESSION_zTXt; + } + text++; /* skip the compression_method byte */ + } + prefix_len = text - chunkdata; + + chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata, + (png_size_t)length, prefix_len, &data_len); + + text_ptr = (png_textp)png_malloc_warn(png_ptr, + (png_uint_32)png_sizeof(png_text)); + if (text_ptr == NULL) + { + png_warning(png_ptr,"Not enough memory to process zTXt chunk."); + png_free(png_ptr, chunkdata); + return; + } + text_ptr->compression = comp_type; + text_ptr->key = chunkdata; +#ifdef PNG_iTXt_SUPPORTED + text_ptr->lang = NULL; + text_ptr->lang_key = NULL; + text_ptr->itxt_length = 0; +#endif + text_ptr->text = chunkdata + prefix_len; + text_ptr->text_length = data_len; + + ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_free(png_ptr, text_ptr); + png_free(png_ptr, chunkdata); + if (ret) + png_error(png_ptr, "Insufficient memory to store zTXt chunk."); +} +#endif + +#if defined(PNG_READ_iTXt_SUPPORTED) +/* note: this does not correctly handle chunks that are > 64K under DOS */ +void /* PRIVATE */ +png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_textp text_ptr; + png_charp chunkdata; + png_charp key, lang, text, lang_key; + int comp_flag; + int comp_type = 0; + int ret; + png_size_t slength, prefix_len, data_len; + + png_debug(1, "in png_handle_iTXt\n"); + + if (!(png_ptr->mode & PNG_HAVE_IHDR)) + png_error(png_ptr, "Missing IHDR before iTXt"); + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + +#ifdef PNG_MAX_MALLOC_64K + /* We will no doubt have problems with chunks even half this size, but + there is no hard and fast rule to tell us where to stop. */ + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr,"iTXt chunk too large to fit in memory"); + png_crc_finish(png_ptr, length); + return; + } +#endif + + chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); + if (chunkdata == NULL) + { + png_warning(png_ptr, "No memory to process iTXt chunk."); + return; + } + slength = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)chunkdata, slength); + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, chunkdata); + return; + } + + chunkdata[slength] = 0x00; + + for (lang = chunkdata; *lang; lang++) + /* empty loop */ ; + lang++; /* skip NUL separator */ + + /* iTXt must have a language tag (possibly empty), two compression bytes, + translated keyword (possibly empty), and possibly some text after the + keyword */ + + if (lang >= chunkdata + slength) + { + comp_flag = PNG_TEXT_COMPRESSION_NONE; + png_warning(png_ptr, "Zero length iTXt chunk"); + } + else + { + comp_flag = *lang++; + comp_type = *lang++; + } + + for (lang_key = lang; *lang_key; lang_key++) + /* empty loop */ ; + lang_key++; /* skip NUL separator */ + + for (text = lang_key; *text; text++) + /* empty loop */ ; + text++; /* skip NUL separator */ + + prefix_len = text - chunkdata; + + key=chunkdata; + if (comp_flag) + chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata, + (size_t)length, prefix_len, &data_len); + else + data_len=png_strlen(chunkdata + prefix_len); + text_ptr = (png_textp)png_malloc_warn(png_ptr, + (png_uint_32)png_sizeof(png_text)); + if (text_ptr == NULL) + { + png_warning(png_ptr,"Not enough memory to process iTXt chunk."); + png_free(png_ptr, chunkdata); + return; + } + text_ptr->compression = (int)comp_flag + 1; + text_ptr->lang_key = chunkdata+(lang_key-key); + text_ptr->lang = chunkdata+(lang-key); + text_ptr->itxt_length = data_len; + text_ptr->text_length = 0; + text_ptr->key = chunkdata; + text_ptr->text = chunkdata + prefix_len; + + ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); + + png_free(png_ptr, text_ptr); + png_free(png_ptr, chunkdata); + if (ret) + png_error(png_ptr, "Insufficient memory to store iTXt chunk."); +} +#endif + +/* This function is called when we haven't found a handler for a + chunk. If there isn't a problem with the chunk itself (ie bad + chunk name, CRC, or a critical chunk), the chunk is silently ignored + -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which + case it will be saved away to be written out later. */ +void /* PRIVATE */ +png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + png_uint_32 skip = 0; + + png_debug(1, "in png_handle_unknown\n"); + + if (png_ptr->mode & PNG_HAVE_IDAT) + { +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IDAT; +#endif + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */ + png_ptr->mode |= PNG_AFTER_IDAT; + } + + png_check_chunk_name(png_ptr, png_ptr->chunk_name); + + if (!(png_ptr->chunk_name[0] & 0x20)) + { +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != + PNG_HANDLE_CHUNK_ALWAYS +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) + && png_ptr->read_user_chunk_fn == NULL +#endif + ) +#endif + png_chunk_error(png_ptr, "unknown critical chunk"); + } + +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) + { + png_unknown_chunk chunk; + +#ifdef PNG_MAX_MALLOC_64K + if (length > (png_uint_32)65535L) + { + png_warning(png_ptr, "unknown chunk too large to fit in memory"); + skip = length - (png_uint_32)65535L; + length = (png_uint_32)65535L; + } +#endif + png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name); + chunk.data = (png_bytep)png_malloc(png_ptr, length); + chunk.size = (png_size_t)length; + png_crc_read(png_ptr, (png_bytep)chunk.data, length); +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) + if(png_ptr->read_user_chunk_fn != NULL) + { + /* callback to user unknown chunk handler */ + if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0) + { + if (!(png_ptr->chunk_name[0] & 0x20)) + if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != + PNG_HANDLE_CHUNK_ALWAYS) + { + png_free(png_ptr, chunk.data); + png_chunk_error(png_ptr, "unknown critical chunk"); + } + png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); + } + } + else +#endif + png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); + png_free(png_ptr, chunk.data); + } + else +#endif + skip = length; + + png_crc_finish(png_ptr, skip); + +#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED) + if (&info_ptr == NULL) /* quiet compiler warnings about unused info_ptr */ + return; +#endif +} + +/* This function is called to verify that a chunk name is valid. + This function can't have the "critical chunk check" incorporated + into it, since in the future we will need to be able to call user + functions to handle unknown critical chunks after we check that + the chunk name itself is valid. */ + +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) + +void /* PRIVATE */ +png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) +{ + png_debug(1, "in png_check_chunk_name\n"); + if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || + isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) + { + png_chunk_error(png_ptr, "invalid chunk type"); + } +} + +/* Combines the row recently read in with the existing pixels in the + row. This routine takes care of alpha and transparency if requested. + This routine also handles the two methods of progressive display + of interlaced images, depending on the mask value. + The mask value describes which pixels are to be combined with + the row. The pattern always repeats every 8 pixels, so just 8 + bits are needed. A one indicates the pixel is to be combined, + a zero indicates the pixel is to be skipped. This is in addition + to any alpha or transparency value associated with the pixel. If + you want all pixels to be combined, pass 0xff (255) in mask. */ +#ifndef PNG_HAVE_ASSEMBLER_COMBINE_ROW +void /* PRIVATE */ +png_combine_row(png_structp png_ptr, png_bytep row, int mask) +{ + png_debug(1,"in png_combine_row\n"); + if (mask == 0xff) + { + png_memcpy(row, png_ptr->row_buf + 1, + PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width)); + } + else + { + switch (png_ptr->row_info.pixel_depth) + { + case 1: + { + png_bytep sp = png_ptr->row_buf + 1; + png_bytep dp = row; + int s_inc, s_start, s_end; + int m = 0x80; + int shift; + png_uint_32 i; + png_uint_32 row_width = png_ptr->width; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 7; + s_inc = 1; + } + else +#endif + { + s_start = 7; + s_end = 0; + s_inc = -1; + } + + shift = s_start; + + for (i = 0; i < row_width; i++) + { + if (m & mask) + { + int value; + + value = (*sp >> shift) & 0x01; + *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + case 2: + { + png_bytep sp = png_ptr->row_buf + 1; + png_bytep dp = row; + int s_start, s_end, s_inc; + int m = 0x80; + int shift; + png_uint_32 i; + png_uint_32 row_width = png_ptr->width; + int value; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 6; + s_inc = 2; + } + else +#endif + { + s_start = 6; + s_end = 0; + s_inc = -2; + } + + shift = s_start; + + for (i = 0; i < row_width; i++) + { + if (m & mask) + { + value = (*sp >> shift) & 0x03; + *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + case 4: + { + png_bytep sp = png_ptr->row_buf + 1; + png_bytep dp = row; + int s_start, s_end, s_inc; + int m = 0x80; + int shift; + png_uint_32 i; + png_uint_32 row_width = png_ptr->width; + int value; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 4; + s_inc = 4; + } + else +#endif + { + s_start = 4; + s_end = 0; + s_inc = -4; + } + shift = s_start; + + for (i = 0; i < row_width; i++) + { + if (m & mask) + { + value = (*sp >> shift) & 0xf; + *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + default: + { + png_bytep sp = png_ptr->row_buf + 1; + png_bytep dp = row; + png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + png_uint_32 i; + png_uint_32 row_width = png_ptr->width; + png_byte m = 0x80; + + + for (i = 0; i < row_width; i++) + { + if (m & mask) + { + png_memcpy(dp, sp, pixel_bytes); + } + + sp += pixel_bytes; + dp += pixel_bytes; + + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + } + } +} +#endif /* !PNG_HAVE_ASSEMBLER_COMBINE_ROW */ + +#ifdef PNG_READ_INTERLACING_SUPPORTED +#ifndef PNG_HAVE_ASSEMBLER_READ_INTERLACE /* else in pngvcrd.c, pnggccrd.c */ +/* OLD pre-1.0.9 interface: +void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, + png_uint_32 transformations) + */ +void /* PRIVATE */ +png_do_read_interlace(png_structp png_ptr) +{ + png_row_infop row_info = &(png_ptr->row_info); + png_bytep row = png_ptr->row_buf + 1; + int pass = png_ptr->pass; + png_uint_32 transformations = png_ptr->transformations; +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + /* offset to next interlace block */ + const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; +#endif + + png_debug(1,"in png_do_read_interlace (stock C version)\n"); + if (row != NULL && row_info != NULL) + { + png_uint_32 final_width; + + final_width = row_info->width * png_pass_inc[pass]; + + switch (row_info->pixel_depth) + { + case 1: + { + png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); + png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); + int sshift, dshift; + int s_start, s_end, s_inc; + int jstop = png_pass_inc[pass]; + png_byte v; + png_uint_32 i; + int j; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (int)((row_info->width + 7) & 0x07); + dshift = (int)((final_width + 7) & 0x07); + s_start = 7; + s_end = 0; + s_inc = -1; + } + else +#endif + { + sshift = 7 - (int)((row_info->width + 7) & 0x07); + dshift = 7 - (int)((final_width + 7) & 0x07); + s_start = 0; + s_end = 7; + s_inc = 1; + } + + for (i = 0; i < row_info->width; i++) + { + v = (png_byte)((*sp >> sshift) & 0x01); + for (j = 0; j < jstop; j++) + { + *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + case 2: + { + png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); + png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); + int sshift, dshift; + int s_start, s_end, s_inc; + int jstop = png_pass_inc[pass]; + png_uint_32 i; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (int)(((row_info->width + 3) & 0x03) << 1); + dshift = (int)(((final_width + 3) & 0x03) << 1); + s_start = 6; + s_end = 0; + s_inc = -2; + } + else +#endif + { + sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); + dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); + s_start = 0; + s_end = 6; + s_inc = 2; + } + + for (i = 0; i < row_info->width; i++) + { + png_byte v; + int j; + + v = (png_byte)((*sp >> sshift) & 0x03); + for (j = 0; j < jstop; j++) + { + *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + case 4: + { + png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); + png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); + int sshift, dshift; + int s_start, s_end, s_inc; + png_uint_32 i; + int jstop = png_pass_inc[pass]; + +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (int)(((row_info->width + 1) & 0x01) << 2); + dshift = (int)(((final_width + 1) & 0x01) << 2); + s_start = 4; + s_end = 0; + s_inc = -4; + } + else +#endif + { + sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); + dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); + s_start = 0; + s_end = 4; + s_inc = 4; + } + + for (i = 0; i < row_info->width; i++) + { + png_byte v = (png_byte)((*sp >> sshift) & 0xf); + int j; + + for (j = 0; j < jstop; j++) + { + *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + default: + { + png_size_t pixel_bytes = (row_info->pixel_depth >> 3); + png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; + png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; + + int jstop = png_pass_inc[pass]; + png_uint_32 i; + + for (i = 0; i < row_info->width; i++) + { + png_byte v[8]; + int j; + + png_memcpy(v, sp, pixel_bytes); + for (j = 0; j < jstop; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sp -= pixel_bytes; + } + break; + } + } + row_info->width = final_width; + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width); + } +#if !defined(PNG_READ_PACKSWAP_SUPPORTED) + if (&transformations == NULL) /* silence compiler warning */ + return; +#endif +} +#endif /* !PNG_HAVE_ASSEMBLER_READ_INTERLACE */ +#endif /* PNG_READ_INTERLACING_SUPPORTED */ + +#ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW +void /* PRIVATE */ +png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, + png_bytep prev_row, int filter) +{ + png_debug(1, "in png_read_filter_row\n"); + png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter); + switch (filter) + { + case PNG_FILTER_VALUE_NONE: + break; + case PNG_FILTER_VALUE_SUB: + { + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_bytep rp = row + bpp; + png_bytep lp = row; + + for (i = bpp; i < istop; i++) + { + *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); + rp++; + } + break; + } + case PNG_FILTER_VALUE_UP: + { + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + png_bytep rp = row; + png_bytep pp = prev_row; + + for (i = 0; i < istop; i++) + { + *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); + rp++; + } + break; + } + case PNG_FILTER_VALUE_AVG: + { + png_uint_32 i; + png_bytep rp = row; + png_bytep pp = prev_row; + png_bytep lp = row; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_uint_32 istop = row_info->rowbytes - bpp; + + for (i = 0; i < bpp; i++) + { + *rp = (png_byte)(((int)(*rp) + + ((int)(*pp++) / 2 )) & 0xff); + rp++; + } + + for (i = 0; i < istop; i++) + { + *rp = (png_byte)(((int)(*rp) + + (int)(*pp++ + *lp++) / 2 ) & 0xff); + rp++; + } + break; + } + case PNG_FILTER_VALUE_PAETH: + { + png_uint_32 i; + png_bytep rp = row; + png_bytep pp = prev_row; + png_bytep lp = row; + png_bytep cp = prev_row; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_uint_32 istop=row_info->rowbytes - bpp; + + for (i = 0; i < bpp; i++) + { + *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); + rp++; + } + + for (i = 0; i < istop; i++) /* use leftover rp,pp */ + { + int a, b, c, pa, pb, pc, p; + + a = *lp++; + b = *pp++; + c = *cp++; + + p = b - c; + pc = a - c; + +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif + + /* + if (pa <= pb && pa <= pc) + p = a; + else if (pb <= pc) + p = b; + else + p = c; + */ + + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; + + *rp = (png_byte)(((int)(*rp) + p) & 0xff); + rp++; + } + break; + } + default: + png_warning(png_ptr, "Ignoring bad adaptive filter type"); + *row=0; + break; + } +} +#endif /* !PNG_HAVE_ASSEMBLER_READ_FILTER_ROW */ + +void /* PRIVATE */ +png_read_finish_row(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; + + /* start of interlace block in the y direction */ + const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; + + /* offset to next interlace block in the y direction */ + const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; +#endif + + png_debug(1, "in png_read_finish_row\n"); + png_ptr->row_number++; + if (png_ptr->row_number < png_ptr->num_rows) + return; + + if (png_ptr->interlaced) + { + png_ptr->row_number = 0; + png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); + do + { + png_ptr->pass++; + if (png_ptr->pass >= 7) + break; + png_ptr->iwidth = (png_ptr->width + + png_pass_inc[png_ptr->pass] - 1 - + png_pass_start[png_ptr->pass]) / + png_pass_inc[png_ptr->pass]; + + png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, + png_ptr->iwidth) + 1; + + if (!(png_ptr->transformations & PNG_INTERLACE)) + { + png_ptr->num_rows = (png_ptr->height + + png_pass_yinc[png_ptr->pass] - 1 - + png_pass_ystart[png_ptr->pass]) / + png_pass_yinc[png_ptr->pass]; + if (!(png_ptr->num_rows)) + continue; + } + else /* if (png_ptr->transformations & PNG_INTERLACE) */ + break; + } while (png_ptr->iwidth == 0); + + if (png_ptr->pass < 7) + return; + } + + if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + { +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IDAT; +#endif + char extra; + int ret; + + png_ptr->zstream.next_out = (Byte *)&extra; + png_ptr->zstream.avail_out = (uInt)1; + for(;;) + { + if (!(png_ptr->zstream.avail_in)) + { + while (!png_ptr->idat_size) + { + png_byte chunk_length[4]; + + png_crc_finish(png_ptr, 0); + + png_read_data(png_ptr, chunk_length, 4); + png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length); + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4)) + png_error(png_ptr, "Not enough image data"); + + } + png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_in = png_ptr->zbuf; + if (png_ptr->zbuf_size > png_ptr->idat_size) + png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; + png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); + png_ptr->idat_size -= png_ptr->zstream.avail_in; + } + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); + if (ret == Z_STREAM_END) + { + if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || + png_ptr->idat_size) + png_warning(png_ptr, "Extra compressed data"); + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + break; + } + if (ret != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : + "Decompression Error"); + + if (!(png_ptr->zstream.avail_out)) + { + png_warning(png_ptr, "Extra compressed data."); + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + break; + } + + } + png_ptr->zstream.avail_out = 0; + } + + if (png_ptr->idat_size || png_ptr->zstream.avail_in) + png_warning(png_ptr, "Extra compression data"); + + inflateReset(&png_ptr->zstream); + + png_ptr->mode |= PNG_AFTER_IDAT; +} + +void /* PRIVATE */ +png_read_start_row(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; + + /* start of interlace block in the y direction */ + const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; + + /* offset to next interlace block in the y direction */ + const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; +#endif + + int max_pixel_depth; + png_uint_32 row_bytes; + + png_debug(1, "in png_read_start_row\n"); + png_ptr->zstream.avail_in = 0; + png_init_read_transformations(png_ptr); + if (png_ptr->interlaced) + { + if (!(png_ptr->transformations & PNG_INTERLACE)) + png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - + png_pass_ystart[0]) / png_pass_yinc[0]; + else + png_ptr->num_rows = png_ptr->height; + + png_ptr->iwidth = (png_ptr->width + + png_pass_inc[png_ptr->pass] - 1 - + png_pass_start[png_ptr->pass]) / + png_pass_inc[png_ptr->pass]; + + row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1; + + png_ptr->irowbytes = (png_size_t)row_bytes; + if((png_uint_32)png_ptr->irowbytes != row_bytes) + png_error(png_ptr, "Rowbytes overflow in png_read_start_row"); + } + else + { + png_ptr->num_rows = png_ptr->height; + png_ptr->iwidth = png_ptr->width; + png_ptr->irowbytes = png_ptr->rowbytes + 1; + } + max_pixel_depth = png_ptr->pixel_depth; + +#if defined(PNG_READ_PACK_SUPPORTED) + if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) + max_pixel_depth = 8; +#endif + +#if defined(PNG_READ_EXPAND_SUPPORTED) + if (png_ptr->transformations & PNG_EXPAND) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (png_ptr->num_trans) + max_pixel_depth = 32; + else + max_pixel_depth = 24; + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) + { + if (max_pixel_depth < 8) + max_pixel_depth = 8; + if (png_ptr->num_trans) + max_pixel_depth *= 2; + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) + { + if (png_ptr->num_trans) + { + max_pixel_depth *= 4; + max_pixel_depth /= 3; + } + } + } +#endif + +#if defined(PNG_READ_FILLER_SUPPORTED) + if (png_ptr->transformations & (PNG_FILLER)) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + max_pixel_depth = 32; + else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) + { + if (max_pixel_depth <= 8) + max_pixel_depth = 16; + else + max_pixel_depth = 32; + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) + { + if (max_pixel_depth <= 32) + max_pixel_depth = 32; + else + max_pixel_depth = 64; + } + } +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) + if (png_ptr->transformations & PNG_GRAY_TO_RGB) + { + if ( +#if defined(PNG_READ_EXPAND_SUPPORTED) + (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || +#endif +#if defined(PNG_READ_FILLER_SUPPORTED) + (png_ptr->transformations & (PNG_FILLER)) || +#endif + png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + if (max_pixel_depth <= 16) + max_pixel_depth = 32; + else + max_pixel_depth = 64; + } + else + { + if (max_pixel_depth <= 8) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + max_pixel_depth = 32; + else + max_pixel_depth = 24; + } + else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + max_pixel_depth = 64; + else + max_pixel_depth = 48; + } + } +#endif + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ +defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) + if(png_ptr->transformations & PNG_USER_TRANSFORM) + { + int user_pixel_depth=png_ptr->user_transform_depth* + png_ptr->user_transform_channels; + if(user_pixel_depth > max_pixel_depth) + max_pixel_depth=user_pixel_depth; + } +#endif + + /* align the width on the next larger 8 pixels. Mainly used + for interlacing */ + row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); + /* calculate the maximum bytes needed, adding a byte and a pixel + for safety's sake */ + row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) + + 1 + ((max_pixel_depth + 7) >> 3); +#ifdef PNG_MAX_MALLOC_64K + if (row_bytes > (png_uint_32)65536L) + png_error(png_ptr, "This image requires a row greater than 64KB"); +#endif + png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64); + png_ptr->row_buf = png_ptr->big_row_buf+32; +#if defined(PNG_DEBUG) && defined(PNG_USE_PNGGCCRD) + png_ptr->row_buf_size = row_bytes; +#endif + +#ifdef PNG_MAX_MALLOC_64K + if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) + png_error(png_ptr, "This image requires a row greater than 64KB"); +#endif + if ((png_uint_32)png_ptr->rowbytes > PNG_SIZE_MAX - 1) + png_error(png_ptr, "Row has too many bytes to allocate in memory."); + png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( + png_ptr->rowbytes + 1)); + + png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); + + png_debug1(3, "width = %lu,\n", png_ptr->width); + png_debug1(3, "height = %lu,\n", png_ptr->height); + png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth); + png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows); + png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes); + png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes); + + png_ptr->flags |= PNG_FLAG_ROW_INIT; +} diff --git a/libpng/pngset.c b/libpng/pngset.c new file mode 100644 index 00000000..5922cad0 --- /dev/null +++ b/libpng/pngset.c @@ -0,0 +1,1219 @@ + +/* pngset.c - storage of image information into info struct + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * The functions here are used during reads to store data from the file + * into the info struct, and during writes to store application data + * into the info struct for writing into the file. This abstracts the + * info struct and allows us to change the structure in the future. + */ + +#define PNG_INTERNAL +#include "png.h" + +#if defined(PNG_bKGD_SUPPORTED) +void PNGAPI +png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background) +{ + png_debug1(1, "in %s storage function\n", "bKGD"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16)); + info_ptr->valid |= PNG_INFO_bKGD; +} +#endif + +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +void PNGAPI +png_set_cHRM(png_structp png_ptr, png_infop info_ptr, + double white_x, double white_y, double red_x, double red_y, + double green_x, double green_y, double blue_x, double blue_y) +{ + png_debug1(1, "in %s storage function\n", "cHRM"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + if (white_x < 0.0 || white_y < 0.0 || + red_x < 0.0 || red_y < 0.0 || + green_x < 0.0 || green_y < 0.0 || + blue_x < 0.0 || blue_y < 0.0) + { + png_warning(png_ptr, + "Ignoring attempt to set negative chromaticity value"); + return; + } + if (white_x > 21474.83 || white_y > 21474.83 || + red_x > 21474.83 || red_y > 21474.83 || + green_x > 21474.83 || green_y > 21474.83 || + blue_x > 21474.83 || blue_y > 21474.83) + { + png_warning(png_ptr, + "Ignoring attempt to set chromaticity value exceeding 21474.83"); + return; + } + + info_ptr->x_white = (float)white_x; + info_ptr->y_white = (float)white_y; + info_ptr->x_red = (float)red_x; + info_ptr->y_red = (float)red_y; + info_ptr->x_green = (float)green_x; + info_ptr->y_green = (float)green_y; + info_ptr->x_blue = (float)blue_x; + info_ptr->y_blue = (float)blue_y; +#ifdef PNG_FIXED_POINT_SUPPORTED + info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5); + info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5); + info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5); + info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5); + info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5); + info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5); + info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5); + info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5); +#endif + info_ptr->valid |= PNG_INFO_cHRM; +} +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +void PNGAPI +png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr, + png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, + png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, + png_fixed_point blue_x, png_fixed_point blue_y) +{ + png_debug1(1, "in %s storage function\n", "cHRM"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + if (white_x < 0 || white_y < 0 || + red_x < 0 || red_y < 0 || + green_x < 0 || green_y < 0 || + blue_x < 0 || blue_y < 0) + { + png_warning(png_ptr, + "Ignoring attempt to set negative chromaticity value"); + return; + } + if (white_x > (double) PNG_UINT_31_MAX || + white_y > (double) PNG_UINT_31_MAX || + red_x > (double) PNG_UINT_31_MAX || + red_y > (double) PNG_UINT_31_MAX || + green_x > (double) PNG_UINT_31_MAX || + green_y > (double) PNG_UINT_31_MAX || + blue_x > (double) PNG_UINT_31_MAX || + blue_y > (double) PNG_UINT_31_MAX) + { + png_warning(png_ptr, + "Ignoring attempt to set chromaticity value exceeding 21474.83"); + return; + } + info_ptr->int_x_white = white_x; + info_ptr->int_y_white = white_y; + info_ptr->int_x_red = red_x; + info_ptr->int_y_red = red_y; + info_ptr->int_x_green = green_x; + info_ptr->int_y_green = green_y; + info_ptr->int_x_blue = blue_x; + info_ptr->int_y_blue = blue_y; +#ifdef PNG_FLOATING_POINT_SUPPORTED + info_ptr->x_white = (float)(white_x/100000.); + info_ptr->y_white = (float)(white_y/100000.); + info_ptr->x_red = (float)( red_x/100000.); + info_ptr->y_red = (float)( red_y/100000.); + info_ptr->x_green = (float)(green_x/100000.); + info_ptr->y_green = (float)(green_y/100000.); + info_ptr->x_blue = (float)( blue_x/100000.); + info_ptr->y_blue = (float)( blue_y/100000.); +#endif + info_ptr->valid |= PNG_INFO_cHRM; +} +#endif +#endif + +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +void PNGAPI +png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma) +{ + double gamma; + png_debug1(1, "in %s storage function\n", "gAMA"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + /* Check for overflow */ + if (file_gamma > 21474.83) + { + png_warning(png_ptr, "Limiting gamma to 21474.83"); + gamma=21474.83; + } + else + gamma=file_gamma; + info_ptr->gamma = (float)gamma; +#ifdef PNG_FIXED_POINT_SUPPORTED + info_ptr->int_gamma = (int)(gamma*100000.+.5); +#endif + info_ptr->valid |= PNG_INFO_gAMA; + if(gamma == 0.0) + png_warning(png_ptr, "Setting gamma=0"); +} +#endif +void PNGAPI +png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point + int_gamma) +{ + png_fixed_point gamma; + + png_debug1(1, "in %s storage function\n", "gAMA"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX) + { + png_warning(png_ptr, "Limiting gamma to 21474.83"); + gamma=PNG_UINT_31_MAX; + } + else + { + if (int_gamma < 0) + { + png_warning(png_ptr, "Setting negative gamma to zero"); + gamma=0; + } + else + gamma=int_gamma; + } +#ifdef PNG_FLOATING_POINT_SUPPORTED + info_ptr->gamma = (float)(gamma/100000.); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + info_ptr->int_gamma = gamma; +#endif + info_ptr->valid |= PNG_INFO_gAMA; + if(gamma == 0) + png_warning(png_ptr, "Setting gamma=0"); +} +#endif + +#if defined(PNG_hIST_SUPPORTED) +void PNGAPI +png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist) +{ + int i; + + png_debug1(1, "in %s storage function\n", "hIST"); + if (png_ptr == NULL || info_ptr == NULL) + return; + if (info_ptr->num_palette == 0) + { + png_warning(png_ptr, + "Palette size 0, hIST allocation skipped."); + return; + } + +#ifdef PNG_FREE_ME_SUPPORTED + png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0); +#endif + /* Changed from info->num_palette to 256 in version 1.2.1 */ + png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr, + (png_uint_32)(256 * png_sizeof (png_uint_16))); + if (png_ptr->hist == NULL) + { + png_warning(png_ptr, "Insufficient memory for hIST chunk data."); + return; + } + + for (i = 0; i < info_ptr->num_palette; i++) + png_ptr->hist[i] = hist[i]; + info_ptr->hist = png_ptr->hist; + info_ptr->valid |= PNG_INFO_hIST; + +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_HIST; +#else + png_ptr->flags |= PNG_FLAG_FREE_HIST; +#endif +} +#endif + +void PNGAPI +png_set_IHDR(png_structp png_ptr, png_infop info_ptr, + png_uint_32 width, png_uint_32 height, int bit_depth, + int color_type, int interlace_type, int compression_type, + int filter_type) +{ + png_debug1(1, "in %s storage function\n", "IHDR"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + /* check for width and height valid values */ + if (width == 0 || height == 0) + png_error(png_ptr, "Image width or height is zero in IHDR"); +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + if (width > png_ptr->user_width_max || height > png_ptr->user_height_max) + png_error(png_ptr, "image size exceeds user limits in IHDR"); +#else + if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX) + png_error(png_ptr, "image size exceeds user limits in IHDR"); +#endif + if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX) + png_error(png_ptr, "Invalid image size in IHDR"); + if ( width > (PNG_UINT_32_MAX + >> 3) /* 8-byte RGBA pixels */ + - 64 /* bigrowbuf hack */ + - 1 /* filter byte */ + - 7*8 /* rounding of width to multiple of 8 pixels */ + - 8) /* extra max_pixel_depth pad */ + png_warning(png_ptr, "Width is too large for libpng to process pixels"); + + /* check other values */ + if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && + bit_depth != 8 && bit_depth != 16) + png_error(png_ptr, "Invalid bit depth in IHDR"); + + if (color_type < 0 || color_type == 1 || + color_type == 5 || color_type > 6) + png_error(png_ptr, "Invalid color type in IHDR"); + + if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || + ((color_type == PNG_COLOR_TYPE_RGB || + color_type == PNG_COLOR_TYPE_GRAY_ALPHA || + color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) + png_error(png_ptr, "Invalid color type/bit depth combination in IHDR"); + + if (interlace_type >= PNG_INTERLACE_LAST) + png_error(png_ptr, "Unknown interlace method in IHDR"); + + if (compression_type != PNG_COMPRESSION_TYPE_BASE) + png_error(png_ptr, "Unknown compression method in IHDR"); + +#if defined(PNG_MNG_FEATURES_SUPPORTED) + /* Accept filter_method 64 (intrapixel differencing) only if + * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and + * 2. Libpng did not read a PNG signature (this filter_method is only + * used in PNG datastreams that are embedded in MNG datastreams) and + * 3. The application called png_permit_mng_features with a mask that + * included PNG_FLAG_MNG_FILTER_64 and + * 4. The filter_method is 64 and + * 5. The color_type is RGB or RGBA + */ + if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted) + png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n"); + if(filter_type != PNG_FILTER_TYPE_BASE) + { + if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && + ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && + (color_type == PNG_COLOR_TYPE_RGB || + color_type == PNG_COLOR_TYPE_RGB_ALPHA))) + png_error(png_ptr, "Unknown filter method in IHDR"); + if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) + png_warning(png_ptr, "Invalid filter method in IHDR"); + } +#else + if(filter_type != PNG_FILTER_TYPE_BASE) + png_error(png_ptr, "Unknown filter method in IHDR"); +#endif + + info_ptr->width = width; + info_ptr->height = height; + info_ptr->bit_depth = (png_byte)bit_depth; + info_ptr->color_type =(png_byte) color_type; + info_ptr->compression_type = (png_byte)compression_type; + info_ptr->filter_type = (png_byte)filter_type; + info_ptr->interlace_type = (png_byte)interlace_type; + if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + info_ptr->channels = 1; + else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) + info_ptr->channels = 3; + else + info_ptr->channels = 1; + if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) + info_ptr->channels++; + info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); + + /* check for potential overflow */ + if ( width > (PNG_UINT_32_MAX + >> 3) /* 8-byte RGBA pixels */ + - 64 /* bigrowbuf hack */ + - 1 /* filter byte */ + - 7*8 /* rounding of width to multiple of 8 pixels */ + - 8) /* extra max_pixel_depth pad */ + info_ptr->rowbytes = (png_size_t)0; + else + info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,width); +} + +#if defined(PNG_oFFs_SUPPORTED) +void PNGAPI +png_set_oFFs(png_structp png_ptr, png_infop info_ptr, + png_int_32 offset_x, png_int_32 offset_y, int unit_type) +{ + png_debug1(1, "in %s storage function\n", "oFFs"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + info_ptr->x_offset = offset_x; + info_ptr->y_offset = offset_y; + info_ptr->offset_unit_type = (png_byte)unit_type; + info_ptr->valid |= PNG_INFO_oFFs; +} +#endif + +#if defined(PNG_pCAL_SUPPORTED) +void PNGAPI +png_set_pCAL(png_structp png_ptr, png_infop info_ptr, + png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, + png_charp units, png_charpp params) +{ + png_uint_32 length; + int i; + + png_debug1(1, "in %s storage function\n", "pCAL"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + length = png_strlen(purpose) + 1; + png_debug1(3, "allocating purpose for info (%lu bytes)\n", length); + info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length); + if (info_ptr->pcal_purpose == NULL) + { + png_warning(png_ptr, "Insufficient memory for pCAL purpose."); + return; + } + png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length); + + png_debug(3, "storing X0, X1, type, and nparams in info\n"); + info_ptr->pcal_X0 = X0; + info_ptr->pcal_X1 = X1; + info_ptr->pcal_type = (png_byte)type; + info_ptr->pcal_nparams = (png_byte)nparams; + + length = png_strlen(units) + 1; + png_debug1(3, "allocating units for info (%lu bytes)\n", length); + info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length); + if (info_ptr->pcal_units == NULL) + { + png_warning(png_ptr, "Insufficient memory for pCAL units."); + return; + } + png_memcpy(info_ptr->pcal_units, units, (png_size_t)length); + + info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr, + (png_uint_32)((nparams + 1) * png_sizeof(png_charp))); + if (info_ptr->pcal_params == NULL) + { + png_warning(png_ptr, "Insufficient memory for pCAL params."); + return; + } + + info_ptr->pcal_params[nparams] = NULL; + + for (i = 0; i < nparams; i++) + { + length = png_strlen(params[i]) + 1; + png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length); + info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length); + if (info_ptr->pcal_params[i] == NULL) + { + png_warning(png_ptr, "Insufficient memory for pCAL parameter."); + return; + } + png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length); + } + + info_ptr->valid |= PNG_INFO_pCAL; +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_PCAL; +#endif +} +#endif + +#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED +void PNGAPI +png_set_sCAL(png_structp png_ptr, png_infop info_ptr, + int unit, double width, double height) +{ + png_debug1(1, "in %s storage function\n", "sCAL"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + info_ptr->scal_unit = (png_byte)unit; + info_ptr->scal_pixel_width = width; + info_ptr->scal_pixel_height = height; + + info_ptr->valid |= PNG_INFO_sCAL; +} +#else +#ifdef PNG_FIXED_POINT_SUPPORTED +void PNGAPI +png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, + int unit, png_charp swidth, png_charp sheight) +{ + png_uint_32 length; + + png_debug1(1, "in %s storage function\n", "sCAL"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + info_ptr->scal_unit = (png_byte)unit; + + length = png_strlen(swidth) + 1; + png_debug1(3, "allocating unit for info (%d bytes)\n", length); + info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length); + if (info_ptr->scal_s_width == NULL) + { + png_warning(png_ptr, "Memory allocation failed while processing sCAL."); + } + png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length); + + length = png_strlen(sheight) + 1; + png_debug1(3, "allocating unit for info (%d bytes)\n", length); + info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length); + if (info_ptr->scal_s_height == NULL) + { + png_free (png_ptr, info_ptr->scal_s_width); + png_warning(png_ptr, "Memory allocation failed while processing sCAL."); + } + png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length); + + info_ptr->valid |= PNG_INFO_sCAL; +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_SCAL; +#endif +} +#endif +#endif +#endif + +#if defined(PNG_pHYs_SUPPORTED) +void PNGAPI +png_set_pHYs(png_structp png_ptr, png_infop info_ptr, + png_uint_32 res_x, png_uint_32 res_y, int unit_type) +{ + png_debug1(1, "in %s storage function\n", "pHYs"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + info_ptr->x_pixels_per_unit = res_x; + info_ptr->y_pixels_per_unit = res_y; + info_ptr->phys_unit_type = (png_byte)unit_type; + info_ptr->valid |= PNG_INFO_pHYs; +} +#endif + +void PNGAPI +png_set_PLTE(png_structp png_ptr, png_infop info_ptr, + png_colorp palette, int num_palette) +{ + + png_debug1(1, "in %s storage function\n", "PLTE"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + /* + * It may not actually be necessary to set png_ptr->palette here; + * we do it for backward compatibility with the way the png_handle_tRNS + * function used to do the allocation. + */ +#ifdef PNG_FREE_ME_SUPPORTED + png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); +#endif + + /* Changed in libpng-1.2.1 to allocate 256 instead of num_palette entries, + in case of an invalid PNG file that has too-large sample values. */ + png_ptr->palette = (png_colorp)png_malloc(png_ptr, + 256 * png_sizeof(png_color)); + png_memset(png_ptr->palette, 0, 256 * png_sizeof(png_color)); + png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof (png_color)); + info_ptr->palette = png_ptr->palette; + info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette; + +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_PLTE; +#else + png_ptr->flags |= PNG_FLAG_FREE_PLTE; +#endif + + info_ptr->valid |= PNG_INFO_PLTE; +} + +#if defined(PNG_sBIT_SUPPORTED) +void PNGAPI +png_set_sBIT(png_structp png_ptr, png_infop info_ptr, + png_color_8p sig_bit) +{ + png_debug1(1, "in %s storage function\n", "sBIT"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof (png_color_8)); + info_ptr->valid |= PNG_INFO_sBIT; +} +#endif + +#if defined(PNG_sRGB_SUPPORTED) +void PNGAPI +png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent) +{ + png_debug1(1, "in %s storage function\n", "sRGB"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + info_ptr->srgb_intent = (png_byte)intent; + info_ptr->valid |= PNG_INFO_sRGB; +} + +void PNGAPI +png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr, + int intent) +{ +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED + float file_gamma; +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + png_fixed_point int_file_gamma; +#endif +#endif +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED + float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, + int_green_y, int_blue_x, int_blue_y; +#endif +#endif + png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + png_set_sRGB(png_ptr, info_ptr, intent); + +#if defined(PNG_gAMA_SUPPORTED) +#ifdef PNG_FLOATING_POINT_SUPPORTED + file_gamma = (float).45455; + png_set_gAMA(png_ptr, info_ptr, file_gamma); +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED + int_file_gamma = 45455L; + png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma); +#endif +#endif + +#if defined(PNG_cHRM_SUPPORTED) +#ifdef PNG_FIXED_POINT_SUPPORTED + int_white_x = 31270L; + int_white_y = 32900L; + int_red_x = 64000L; + int_red_y = 33000L; + int_green_x = 30000L; + int_green_y = 60000L; + int_blue_x = 15000L; + int_blue_y = 6000L; + + png_set_cHRM_fixed(png_ptr, info_ptr, + int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y, + int_blue_x, int_blue_y); +#endif +#ifdef PNG_FLOATING_POINT_SUPPORTED + white_x = (float).3127; + white_y = (float).3290; + red_x = (float).64; + red_y = (float).33; + green_x = (float).30; + green_y = (float).60; + blue_x = (float).15; + blue_y = (float).06; + + png_set_cHRM(png_ptr, info_ptr, + white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); +#endif +#endif +} +#endif + + +#if defined(PNG_iCCP_SUPPORTED) +void PNGAPI +png_set_iCCP(png_structp png_ptr, png_infop info_ptr, + png_charp name, int compression_type, + png_charp profile, png_uint_32 proflen) +{ + png_charp new_iccp_name; + png_charp new_iccp_profile; + + png_debug1(1, "in %s storage function\n", "iCCP"); + if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL) + return; + + new_iccp_name = (png_charp)png_malloc_warn(png_ptr, png_strlen(name)+1); + if (new_iccp_name == NULL) + { + png_warning(png_ptr, "Insufficient memory to process iCCP chunk."); + return; + } + png_strcpy(new_iccp_name, name); + new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen); + if (new_iccp_profile == NULL) + { + png_free (png_ptr, new_iccp_name); + png_warning(png_ptr, "Insufficient memory to process iCCP profile."); + return; + } + png_memcpy(new_iccp_profile, profile, (png_size_t)proflen); + + png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0); + + info_ptr->iccp_proflen = proflen; + info_ptr->iccp_name = new_iccp_name; + info_ptr->iccp_profile = new_iccp_profile; + /* Compression is always zero but is here so the API and info structure + * does not have to change if we introduce multiple compression types */ + info_ptr->iccp_compression = (png_byte)compression_type; +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_ICCP; +#endif + info_ptr->valid |= PNG_INFO_iCCP; +} +#endif + +#if defined(PNG_TEXT_SUPPORTED) +void PNGAPI +png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, + int num_text) +{ + int ret; + ret=png_set_text_2(png_ptr, info_ptr, text_ptr, num_text); + if (ret) + png_error(png_ptr, "Insufficient memory to store text"); +} + +int /* PRIVATE */ +png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, + int num_text) +{ + int i; + + png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ? + "text" : (png_const_charp)png_ptr->chunk_name)); + + if (png_ptr == NULL || info_ptr == NULL || num_text == 0) + return(0); + + /* Make sure we have enough space in the "text" array in info_struct + * to hold all of the incoming text_ptr objects. + */ + if (info_ptr->num_text + num_text > info_ptr->max_text) + { + if (info_ptr->text != NULL) + { + png_textp old_text; + int old_max; + + old_max = info_ptr->max_text; + info_ptr->max_text = info_ptr->num_text + num_text + 8; + old_text = info_ptr->text; + info_ptr->text = (png_textp)png_malloc_warn(png_ptr, + (png_uint_32)(info_ptr->max_text * png_sizeof (png_text))); + if (info_ptr->text == NULL) + { + png_free(png_ptr, old_text); + return(1); + } + png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max * + png_sizeof(png_text))); + png_free(png_ptr, old_text); + } + else + { + info_ptr->max_text = num_text + 8; + info_ptr->num_text = 0; + info_ptr->text = (png_textp)png_malloc_warn(png_ptr, + (png_uint_32)(info_ptr->max_text * png_sizeof (png_text))); + if (info_ptr->text == NULL) + return(1); +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_TEXT; +#endif + } + png_debug1(3, "allocated %d entries for info_ptr->text\n", + info_ptr->max_text); + } + for (i = 0; i < num_text; i++) + { + png_size_t text_length,key_len; + png_size_t lang_len,lang_key_len; + png_textp textp = &(info_ptr->text[info_ptr->num_text]); + + if (text_ptr[i].key == NULL) + continue; + + key_len = png_strlen(text_ptr[i].key); + + if(text_ptr[i].compression <= 0) + { + lang_len = 0; + lang_key_len = 0; + } + else +#ifdef PNG_iTXt_SUPPORTED + { + /* set iTXt data */ + if (text_ptr[i].lang != NULL) + lang_len = png_strlen(text_ptr[i].lang); + else + lang_len = 0; + if (text_ptr[i].lang_key != NULL) + lang_key_len = png_strlen(text_ptr[i].lang_key); + else + lang_key_len = 0; + } +#else + { + png_warning(png_ptr, "iTXt chunk not supported."); + continue; + } +#endif + + if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0') + { + text_length = 0; +#ifdef PNG_iTXt_SUPPORTED + if(text_ptr[i].compression > 0) + textp->compression = PNG_ITXT_COMPRESSION_NONE; + else +#endif + textp->compression = PNG_TEXT_COMPRESSION_NONE; + } + else + { + text_length = png_strlen(text_ptr[i].text); + textp->compression = text_ptr[i].compression; + } + + textp->key = (png_charp)png_malloc_warn(png_ptr, + (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4)); + if (textp->key == NULL) + return(1); + png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n", + (png_uint_32)(key_len + lang_len + lang_key_len + text_length + 4), + (int)textp->key); + + png_memcpy(textp->key, text_ptr[i].key, + (png_size_t)(key_len)); + *(textp->key+key_len) = '\0'; +#ifdef PNG_iTXt_SUPPORTED + if (text_ptr[i].compression > 0) + { + textp->lang=textp->key + key_len + 1; + png_memcpy(textp->lang, text_ptr[i].lang, lang_len); + *(textp->lang+lang_len) = '\0'; + textp->lang_key=textp->lang + lang_len + 1; + png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len); + *(textp->lang_key+lang_key_len) = '\0'; + textp->text=textp->lang_key + lang_key_len + 1; + } + else +#endif + { +#ifdef PNG_iTXt_SUPPORTED + textp->lang=NULL; + textp->lang_key=NULL; +#endif + textp->text=textp->key + key_len + 1; + } + if(text_length) + png_memcpy(textp->text, text_ptr[i].text, + (png_size_t)(text_length)); + *(textp->text+text_length) = '\0'; + +#ifdef PNG_iTXt_SUPPORTED + if(textp->compression > 0) + { + textp->text_length = 0; + textp->itxt_length = text_length; + } + else +#endif + { + textp->text_length = text_length; +#ifdef PNG_iTXt_SUPPORTED + textp->itxt_length = 0; +#endif + } + info_ptr->text[info_ptr->num_text]= *textp; + info_ptr->num_text++; + png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text); + } + return(0); +} +#endif + +#if defined(PNG_tIME_SUPPORTED) +void PNGAPI +png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time) +{ + png_debug1(1, "in %s storage function\n", "tIME"); + if (png_ptr == NULL || info_ptr == NULL || + (png_ptr->mode & PNG_WROTE_tIME)) + return; + + png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof (png_time)); + info_ptr->valid |= PNG_INFO_tIME; +} +#endif + +#if defined(PNG_tRNS_SUPPORTED) +void PNGAPI +png_set_tRNS(png_structp png_ptr, png_infop info_ptr, + png_bytep trans, int num_trans, png_color_16p trans_values) +{ + png_debug1(1, "in %s storage function\n", "tRNS"); + if (png_ptr == NULL || info_ptr == NULL) + return; + + if (trans != NULL) + { + /* + * It may not actually be necessary to set png_ptr->trans here; + * we do it for backward compatibility with the way the png_handle_tRNS + * function used to do the allocation. + */ +#ifdef PNG_FREE_ME_SUPPORTED + png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); +#endif + /* Changed from num_trans to 256 in version 1.2.1 */ + png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr, + (png_uint_32)256); + png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans); +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_TRNS; +#else + png_ptr->flags |= PNG_FLAG_FREE_TRNS; +#endif + } + + if (trans_values != NULL) + { + png_memcpy(&(info_ptr->trans_values), trans_values, + png_sizeof(png_color_16)); + if (num_trans == 0) + num_trans = 1; + } + info_ptr->num_trans = (png_uint_16)num_trans; + info_ptr->valid |= PNG_INFO_tRNS; +} +#endif + +#if defined(PNG_sPLT_SUPPORTED) +void PNGAPI +png_set_sPLT(png_structp png_ptr, + png_infop info_ptr, png_sPLT_tp entries, int nentries) +{ + png_sPLT_tp np; + int i; + + np = (png_sPLT_tp)png_malloc_warn(png_ptr, + (info_ptr->splt_palettes_num + nentries) * png_sizeof(png_sPLT_t)); + if (np == NULL) + { + png_warning(png_ptr, "No memory for sPLT palettes."); + return; + } + + png_memcpy(np, info_ptr->splt_palettes, + info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t)); + png_free(png_ptr, info_ptr->splt_palettes); + info_ptr->splt_palettes=NULL; + + for (i = 0; i < nentries; i++) + { + png_sPLT_tp to = np + info_ptr->splt_palettes_num + i; + png_sPLT_tp from = entries + i; + + to->name = (png_charp)png_malloc(png_ptr, + png_strlen(from->name) + 1); + /* TODO: use png_malloc_warn */ + png_strcpy(to->name, from->name); + to->entries = (png_sPLT_entryp)png_malloc(png_ptr, + from->nentries * png_sizeof(png_sPLT_t)); + /* TODO: use png_malloc_warn */ + png_memcpy(to->entries, from->entries, + from->nentries * png_sizeof(png_sPLT_t)); + to->nentries = from->nentries; + to->depth = from->depth; + } + + info_ptr->splt_palettes = np; + info_ptr->splt_palettes_num += nentries; + info_ptr->valid |= PNG_INFO_sPLT; +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_SPLT; +#endif +} +#endif /* PNG_sPLT_SUPPORTED */ + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +void PNGAPI +png_set_unknown_chunks(png_structp png_ptr, + png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns) +{ + png_unknown_chunkp np; + int i; + + if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0) + return; + + np = (png_unknown_chunkp)png_malloc_warn(png_ptr, + (info_ptr->unknown_chunks_num + num_unknowns) * + png_sizeof(png_unknown_chunk)); + if (np == NULL) + { + png_warning(png_ptr, "Out of memory while processing unknown chunk."); + return; + } + + png_memcpy(np, info_ptr->unknown_chunks, + info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk)); + png_free(png_ptr, info_ptr->unknown_chunks); + info_ptr->unknown_chunks=NULL; + + for (i = 0; i < num_unknowns; i++) + { + png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i; + png_unknown_chunkp from = unknowns + i; + + png_strncpy((png_charp)to->name, (png_charp)from->name, 5); + to->data = (png_bytep)png_malloc_warn(png_ptr, from->size); + if (to->data == NULL) + { + png_warning(png_ptr, "Out of memory processing unknown chunk."); + } + else + { + png_memcpy(to->data, from->data, from->size); + to->size = from->size; + + /* note our location in the read or write sequence */ + to->location = (png_byte)(png_ptr->mode & 0xff); + } + } + + info_ptr->unknown_chunks = np; + info_ptr->unknown_chunks_num += num_unknowns; +#ifdef PNG_FREE_ME_SUPPORTED + info_ptr->free_me |= PNG_FREE_UNKN; +#endif +} +void PNGAPI +png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr, + int chunk, int location) +{ + if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk < + (int)info_ptr->unknown_chunks_num) + info_ptr->unknown_chunks[chunk].location = (png_byte)location; +} +#endif + +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ + defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) +void PNGAPI +png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted) +{ + /* This function is deprecated in favor of png_permit_mng_features() + and will be removed from libpng-2.0.0 */ + png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n"); + if (png_ptr == NULL) + return; + png_ptr->mng_features_permitted = (png_byte) + ((png_ptr->mng_features_permitted & (~(PNG_FLAG_MNG_EMPTY_PLTE))) | + ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE))); +} +#endif + +#if defined(PNG_MNG_FEATURES_SUPPORTED) +png_uint_32 PNGAPI +png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features) +{ + png_debug(1, "in png_permit_mng_features\n"); + if (png_ptr == NULL) + return (png_uint_32)0; + png_ptr->mng_features_permitted = + (png_byte)(mng_features & PNG_ALL_MNG_FEATURES); + return (png_uint_32)png_ptr->mng_features_permitted; +} +#endif + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) +void PNGAPI +png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep + chunk_list, int num_chunks) +{ + png_bytep new_list, p; + int i, old_num_chunks; + if (num_chunks == 0) + { + if(keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE) + png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS; + else + png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS; + + if(keep == PNG_HANDLE_CHUNK_ALWAYS) + png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS; + else + png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS; + return; + } + if (chunk_list == NULL) + return; + old_num_chunks=png_ptr->num_chunk_list; + new_list=(png_bytep)png_malloc(png_ptr, + (png_uint_32)(5*(num_chunks+old_num_chunks))); + if(png_ptr->chunk_list != NULL) + { + png_memcpy(new_list, png_ptr->chunk_list, + (png_size_t)(5*old_num_chunks)); + png_free(png_ptr, png_ptr->chunk_list); + png_ptr->chunk_list=NULL; + } + png_memcpy(new_list+5*old_num_chunks, chunk_list, + (png_size_t)(5*num_chunks)); + for (p=new_list+5*old_num_chunks+4, i=0; inum_chunk_list=old_num_chunks+num_chunks; + png_ptr->chunk_list=new_list; +#ifdef PNG_FREE_ME_SUPPORTED + png_ptr->free_me |= PNG_FREE_LIST; +#endif +} +#endif + +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) +void PNGAPI +png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr, + png_user_chunk_ptr read_user_chunk_fn) +{ + png_debug(1, "in png_set_read_user_chunk_fn\n"); + png_ptr->read_user_chunk_fn = read_user_chunk_fn; + png_ptr->user_chunk_ptr = user_chunk_ptr; +} +#endif + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +void PNGAPI +png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers) +{ + png_debug1(1, "in %s storage function\n", "rows"); + + if (png_ptr == NULL || info_ptr == NULL) + return; + + if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers)) + png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); + info_ptr->row_pointers = row_pointers; + if(row_pointers) + info_ptr->valid |= PNG_INFO_IDAT; +} +#endif + +#ifdef PNG_WRITE_SUPPORTED +void PNGAPI +png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size) +{ + if(png_ptr->zbuf) + png_free(png_ptr, png_ptr->zbuf); + png_ptr->zbuf_size = (png_size_t)size; + png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; +} +#endif + +void PNGAPI +png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask) +{ + if (png_ptr && info_ptr) + info_ptr->valid &= ~(mask); +} + + +#ifndef PNG_1_0_X +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED +/* this function was added to libpng 1.2.0 and should always exist by default */ +void PNGAPI +png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags) +{ + png_uint_32 settable_asm_flags; + png_uint_32 settable_mmx_flags; + + settable_mmx_flags = +#ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW + PNG_ASM_FLAG_MMX_READ_COMBINE_ROW | +#endif +#ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE + PNG_ASM_FLAG_MMX_READ_INTERLACE | +#endif +#ifdef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW + PNG_ASM_FLAG_MMX_READ_FILTER_SUB | + PNG_ASM_FLAG_MMX_READ_FILTER_UP | + PNG_ASM_FLAG_MMX_READ_FILTER_AVG | + PNG_ASM_FLAG_MMX_READ_FILTER_PAETH | +#endif + 0; + + /* could be some non-MMX ones in the future, but not currently: */ + settable_asm_flags = settable_mmx_flags; + + if (!(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED) || + !(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU)) + { + /* clear all MMX flags if MMX isn't supported */ + settable_asm_flags &= ~settable_mmx_flags; + png_ptr->asm_flags &= ~settable_mmx_flags; + } + + /* we're replacing the settable bits with those passed in by the user, + * so first zero them out of the master copy, then logical-OR in the + * allowed subset that was requested */ + + png_ptr->asm_flags &= ~settable_asm_flags; /* zero them */ + png_ptr->asm_flags |= (asm_flags & settable_asm_flags); /* set them */ +} +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */ + +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED +/* this function was added to libpng 1.2.0 */ +void PNGAPI +png_set_mmx_thresholds (png_structp png_ptr, + png_byte mmx_bitdepth_threshold, + png_uint_32 mmx_rowbytes_threshold) +{ + png_ptr->mmx_bitdepth_threshold = mmx_bitdepth_threshold; + png_ptr->mmx_rowbytes_threshold = mmx_rowbytes_threshold; +} +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */ + +#ifdef PNG_SET_USER_LIMITS_SUPPORTED +/* this function was added to libpng 1.2.6 */ +void PNGAPI +png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max, + png_uint_32 user_height_max) +{ + /* Images with dimensions larger than these limits will be + * rejected by png_set_IHDR(). To accept any PNG datastream + * regardless of dimensions, set both limits to 0x7ffffffL. + */ + png_ptr->user_width_max = user_width_max; + png_ptr->user_height_max = user_height_max; +} +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ + +#endif /* ?PNG_1_0_X */ diff --git a/libpng/pngtrans.c b/libpng/pngtrans.c new file mode 100644 index 00000000..9003a210 --- /dev/null +++ b/libpng/pngtrans.c @@ -0,0 +1,650 @@ + +/* pngtrans.c - transforms the data in a row (used by both readers and writers) + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#include "png.h" + +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) +/* turn on BGR-to-RGB mapping */ +void PNGAPI +png_set_bgr(png_structp png_ptr) +{ + png_debug(1, "in png_set_bgr\n"); + png_ptr->transformations |= PNG_BGR; +} +#endif + +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) +/* turn on 16 bit byte swapping */ +void PNGAPI +png_set_swap(png_structp png_ptr) +{ + png_debug(1, "in png_set_swap\n"); + if (png_ptr->bit_depth == 16) + png_ptr->transformations |= PNG_SWAP_BYTES; +} +#endif + +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) +/* turn on pixel packing */ +void PNGAPI +png_set_packing(png_structp png_ptr) +{ + png_debug(1, "in png_set_packing\n"); + if (png_ptr->bit_depth < 8) + { + png_ptr->transformations |= PNG_PACK; + png_ptr->usr_bit_depth = 8; + } +} +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) +/* turn on packed pixel swapping */ +void PNGAPI +png_set_packswap(png_structp png_ptr) +{ + png_debug(1, "in png_set_packswap\n"); + if (png_ptr->bit_depth < 8) + png_ptr->transformations |= PNG_PACKSWAP; +} +#endif + +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) +void PNGAPI +png_set_shift(png_structp png_ptr, png_color_8p true_bits) +{ + png_debug(1, "in png_set_shift\n"); + png_ptr->transformations |= PNG_SHIFT; + png_ptr->shift = *true_bits; +} +#endif + +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \ + defined(PNG_WRITE_INTERLACING_SUPPORTED) +int PNGAPI +png_set_interlace_handling(png_structp png_ptr) +{ + png_debug(1, "in png_set_interlace handling\n"); + if (png_ptr->interlaced) + { + png_ptr->transformations |= PNG_INTERLACE; + return (7); + } + + return (1); +} +#endif + +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) +/* Add a filler byte on read, or remove a filler or alpha byte on write. + * The filler type has changed in v0.95 to allow future 2-byte fillers + * for 48-bit input data, as well as to avoid problems with some compilers + * that don't like bytes as parameters. + */ +void PNGAPI +png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) +{ + png_debug(1, "in png_set_filler\n"); + png_ptr->transformations |= PNG_FILLER; + png_ptr->filler = (png_byte)filler; + if (filler_loc == PNG_FILLER_AFTER) + png_ptr->flags |= PNG_FLAG_FILLER_AFTER; + else + png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER; + + /* This should probably go in the "do_read_filler" routine. + * I attempted to do that in libpng-1.0.1a but that caused problems + * so I restored it in libpng-1.0.2a + */ + + if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) + { + png_ptr->usr_channels = 4; + } + + /* Also I added this in libpng-1.0.2a (what happens when we expand + * a less-than-8-bit grayscale to GA? */ + + if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8) + { + png_ptr->usr_channels = 2; + } +} + +#if !defined(PNG_1_0_X) +/* Added to libpng-1.2.7 */ +void PNGAPI +png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) +{ + png_debug(1, "in png_set_add_alpha\n"); + png_set_filler(png_ptr, filler, filler_loc); + png_ptr->transformations |= PNG_ADD_ALPHA; +} +#endif + +#endif + +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ + defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) +void PNGAPI +png_set_swap_alpha(png_structp png_ptr) +{ + png_debug(1, "in png_set_swap_alpha\n"); + png_ptr->transformations |= PNG_SWAP_ALPHA; +} +#endif + +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ + defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) +void PNGAPI +png_set_invert_alpha(png_structp png_ptr) +{ + png_debug(1, "in png_set_invert_alpha\n"); + png_ptr->transformations |= PNG_INVERT_ALPHA; +} +#endif + +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) +void PNGAPI +png_set_invert_mono(png_structp png_ptr) +{ + png_debug(1, "in png_set_invert_mono\n"); + png_ptr->transformations |= PNG_INVERT_MONO; +} + +/* invert monochrome grayscale data */ +void /* PRIVATE */ +png_do_invert(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_invert\n"); + /* This test removed from libpng version 1.0.13 and 1.2.0: + * if (row_info->bit_depth == 1 && + */ +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row == NULL || row_info == NULL) + return; +#endif + if (row_info->color_type == PNG_COLOR_TYPE_GRAY) + { + png_bytep rp = row; + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + + for (i = 0; i < istop; i++) + { + *rp = (png_byte)(~(*rp)); + rp++; + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && + row_info->bit_depth == 8) + { + png_bytep rp = row; + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + + for (i = 0; i < istop; i+=2) + { + *rp = (png_byte)(~(*rp)); + rp+=2; + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && + row_info->bit_depth == 16) + { + png_bytep rp = row; + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + + for (i = 0; i < istop; i+=4) + { + *rp = (png_byte)(~(*rp)); + *(rp+1) = (png_byte)(~(*(rp+1))); + rp+=4; + } + } +} +#endif + +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) +/* swaps byte order on 16 bit depth images */ +void /* PRIVATE */ +png_do_swap(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_swap\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + row_info->bit_depth == 16) + { + png_bytep rp = row; + png_uint_32 i; + png_uint_32 istop= row_info->width * row_info->channels; + + for (i = 0; i < istop; i++, rp += 2) + { + png_byte t = *rp; + *rp = *(rp + 1); + *(rp + 1) = t; + } + } +} +#endif + +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) +static png_byte onebppswaptable[256] = { + 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, + 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, + 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, + 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, + 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, + 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, + 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, + 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, + 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, + 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, + 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, + 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, + 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, + 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, + 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, + 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, + 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, + 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, + 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, + 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, + 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, + 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, + 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, + 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, + 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, + 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, + 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, + 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, + 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, + 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, + 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, + 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF +}; + +static png_byte twobppswaptable[256] = { + 0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0, + 0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0, + 0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4, + 0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4, + 0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8, + 0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8, + 0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC, + 0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC, + 0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1, + 0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1, + 0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5, + 0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5, + 0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9, + 0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9, + 0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD, + 0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD, + 0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2, + 0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2, + 0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6, + 0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6, + 0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA, + 0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA, + 0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE, + 0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE, + 0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3, + 0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3, + 0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7, + 0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7, + 0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB, + 0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB, + 0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF, + 0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF +}; + +static png_byte fourbppswaptable[256] = { + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, + 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, + 0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71, + 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1, + 0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72, + 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2, + 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, + 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3, + 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74, + 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4, + 0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75, + 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5, + 0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76, + 0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6, + 0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77, + 0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7, + 0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78, + 0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8, + 0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79, + 0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9, + 0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A, + 0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA, + 0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B, + 0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB, + 0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C, + 0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC, + 0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D, + 0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD, + 0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E, + 0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE, + 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F, + 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF +}; + +/* swaps pixel packing order within bytes */ +void /* PRIVATE */ +png_do_packswap(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_packswap\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + row_info->bit_depth < 8) + { + png_bytep rp, end, table; + + end = row + row_info->rowbytes; + + if (row_info->bit_depth == 1) + table = onebppswaptable; + else if (row_info->bit_depth == 2) + table = twobppswaptable; + else if (row_info->bit_depth == 4) + table = fourbppswaptable; + else + return; + + for (rp = row; rp < end; rp++) + *rp = table[*rp]; + } +} +#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */ + +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \ + defined(PNG_READ_STRIP_ALPHA_SUPPORTED) +/* remove filler or alpha byte(s) */ +void /* PRIVATE */ +png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) +{ + png_debug(1, "in png_do_strip_filler\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + png_bytep sp=row; + png_bytep dp=row; + png_uint_32 row_width=row_info->width; + png_uint_32 i; + + if ((row_info->color_type == PNG_COLOR_TYPE_RGB || + (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && + (flags & PNG_FLAG_STRIP_ALPHA))) && + row_info->channels == 4) + { + if (row_info->bit_depth == 8) + { + /* This converts from RGBX or RGBA to RGB */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + dp+=3; sp+=4; + for (i = 1; i < row_width; i++) + { + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + sp++; + } + } + /* This converts from XRGB or ARGB to RGB */ + else + { + for (i = 0; i < row_width; i++) + { + sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + } + } + row_info->pixel_depth = 24; + row_info->rowbytes = row_width * 3; + } + else /* if (row_info->bit_depth == 16) */ + { + if (flags & PNG_FLAG_FILLER_AFTER) + { + /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */ + sp += 8; dp += 6; + for (i = 1; i < row_width; i++) + { + /* This could be (although png_memcpy is probably slower): + png_memcpy(dp, sp, 6); + sp += 8; + dp += 6; + */ + + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + sp += 2; + } + } + else + { + /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */ + for (i = 0; i < row_width; i++) + { + /* This could be (although png_memcpy is probably slower): + png_memcpy(dp, sp, 6); + sp += 8; + dp += 6; + */ + + sp+=2; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + *dp++ = *sp++; + } + } + row_info->pixel_depth = 48; + row_info->rowbytes = row_width * 6; + } + row_info->channels = 3; + } + else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY || + (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && + (flags & PNG_FLAG_STRIP_ALPHA))) && + row_info->channels == 2) + { + if (row_info->bit_depth == 8) + { + /* This converts from GX or GA to G */ + if (flags & PNG_FLAG_FILLER_AFTER) + { + for (i = 0; i < row_width; i++) + { + *dp++ = *sp++; + sp++; + } + } + /* This converts from XG or AG to G */ + else + { + for (i = 0; i < row_width; i++) + { + sp++; + *dp++ = *sp++; + } + } + row_info->pixel_depth = 8; + row_info->rowbytes = row_width; + } + else /* if (row_info->bit_depth == 16) */ + { + if (flags & PNG_FLAG_FILLER_AFTER) + { + /* This converts from GGXX or GGAA to GG */ + sp += 4; dp += 2; + for (i = 1; i < row_width; i++) + { + *dp++ = *sp++; + *dp++ = *sp++; + sp += 2; + } + } + else + { + /* This converts from XXGG or AAGG to GG */ + for (i = 0; i < row_width; i++) + { + sp += 2; + *dp++ = *sp++; + *dp++ = *sp++; + } + } + row_info->pixel_depth = 16; + row_info->rowbytes = row_width * 2; + } + row_info->channels = 1; + } + if (flags & PNG_FLAG_STRIP_ALPHA) + row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; + } +} +#endif + +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) +/* swaps red and blue bytes within a pixel */ +void /* PRIVATE */ +png_do_bgr(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_bgr\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + (row_info->color_type & PNG_COLOR_MASK_COLOR)) + { + png_uint_32 row_width = row_info->width; + if (row_info->bit_depth == 8) + { + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + { + png_bytep rp; + png_uint_32 i; + + for (i = 0, rp = row; i < row_width; i++, rp += 3) + { + png_byte save = *rp; + *rp = *(rp + 2); + *(rp + 2) = save; + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + png_bytep rp; + png_uint_32 i; + + for (i = 0, rp = row; i < row_width; i++, rp += 4) + { + png_byte save = *rp; + *rp = *(rp + 2); + *(rp + 2) = save; + } + } + } + else if (row_info->bit_depth == 16) + { + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + { + png_bytep rp; + png_uint_32 i; + + for (i = 0, rp = row; i < row_width; i++, rp += 6) + { + png_byte save = *rp; + *rp = *(rp + 4); + *(rp + 4) = save; + save = *(rp + 1); + *(rp + 1) = *(rp + 5); + *(rp + 5) = save; + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + png_bytep rp; + png_uint_32 i; + + for (i = 0, rp = row; i < row_width; i++, rp += 8) + { + png_byte save = *rp; + *rp = *(rp + 4); + *(rp + 4) = save; + save = *(rp + 1); + *(rp + 1) = *(rp + 5); + *(rp + 5) = save; + } + } + } + } +} +#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */ + +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ + defined(PNG_LEGACY_SUPPORTED) +void PNGAPI +png_set_user_transform_info(png_structp png_ptr, png_voidp + user_transform_ptr, int user_transform_depth, int user_transform_channels) +{ + png_debug(1, "in png_set_user_transform_info\n"); +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) + png_ptr->user_transform_ptr = user_transform_ptr; + png_ptr->user_transform_depth = (png_byte)user_transform_depth; + png_ptr->user_transform_channels = (png_byte)user_transform_channels; +#else + if(user_transform_ptr || user_transform_depth || user_transform_channels) + png_warning(png_ptr, + "This version of libpng does not support user transform info"); +#endif +} +#endif + +/* This function returns a pointer to the user_transform_ptr associated with + * the user transform functions. The application should free any memory + * associated with this pointer before png_write_destroy and png_read_destroy + * are called. + */ +png_voidp PNGAPI +png_get_user_transform_ptr(png_structp png_ptr) +{ +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) + return ((png_voidp)png_ptr->user_transform_ptr); +#else + if(png_ptr) + return (NULL); + return (NULL); +#endif +} diff --git a/libpng/pngvcrd.c b/libpng/pngvcrd.c new file mode 100644 index 00000000..940a7fc0 --- /dev/null +++ b/libpng/pngvcrd.c @@ -0,0 +1,3903 @@ +/* pngvcrd.c - mixed C/assembler version of utilities to read a PNG file + * + * For Intel x86 CPU and Microsoft Visual C++ compiler + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * Copyright (c) 1998, Intel Corporation + * + * Contributed by Nirav Chhatrapati, Intel Corporation, 1998 + * Interface to libpng contributed by Gilles Vollant, 1999 + * + * + * In png_do_read_interlace() in libpng versions 1.0.3a through 1.0.4d, + * a sign error in the post-MMX cleanup code for each pixel_depth resulted + * in bad pixels at the beginning of some rows of some images, and also + * (due to out-of-range memory reads and writes) caused heap corruption + * when compiled with MSVC 6.0. The error was fixed in version 1.0.4e. + * + * [png_read_filter_row_mmx_avg() bpp == 2 bugfix, GRR 20000916] + * + * [runtime MMX configuration, GRR 20010102] + * + */ + +#define PNG_INTERNAL +#include "png.h" + +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) && defined(PNG_USE_PNGVCRD) + +static int mmx_supported=2; + + +int PNGAPI +png_mmx_support(void) +{ + int mmx_supported_local = 0; + _asm { + push ebx //CPUID will trash these + push ecx + push edx + + pushfd //Save Eflag to stack + pop eax //Get Eflag from stack into eax + mov ecx, eax //Make another copy of Eflag in ecx + xor eax, 0x200000 //Toggle ID bit in Eflag [i.e. bit(21)] + push eax //Save modified Eflag back to stack + + popfd //Restored modified value back to Eflag reg + pushfd //Save Eflag to stack + pop eax //Get Eflag from stack + push ecx // save original Eflag to stack + popfd // restore original Eflag + xor eax, ecx //Compare the new Eflag with the original Eflag + jz NOT_SUPPORTED //If the same, CPUID instruction is not supported, + //skip following instructions and jump to + //NOT_SUPPORTED label + + xor eax, eax //Set eax to zero + + _asm _emit 0x0f //CPUID instruction (two bytes opcode) + _asm _emit 0xa2 + + cmp eax, 1 //make sure eax return non-zero value + jl NOT_SUPPORTED //If eax is zero, mmx not supported + + xor eax, eax //set eax to zero + inc eax //Now increment eax to 1. This instruction is + //faster than the instruction "mov eax, 1" + + _asm _emit 0x0f //CPUID instruction + _asm _emit 0xa2 + + and edx, 0x00800000 //mask out all bits but mmx bit(24) + cmp edx, 0 // 0 = mmx not supported + jz NOT_SUPPORTED // non-zero = Yes, mmx IS supported + + mov mmx_supported_local, 1 //set return value to 1 + +NOT_SUPPORTED: + mov eax, mmx_supported_local //move return value to eax + pop edx //CPUID trashed these + pop ecx + pop ebx + } + + //mmx_supported_local=0; // test code for force don't support MMX + //printf("MMX : %u (1=MMX supported)\n",mmx_supported_local); + + mmx_supported = mmx_supported_local; + return mmx_supported_local; +} + +/* Combines the row recently read in with the previous row. + This routine takes care of alpha and transparency if requested. + This routine also handles the two methods of progressive display + of interlaced images, depending on the mask value. + The mask value describes which pixels are to be combined with + the row. The pattern always repeats every 8 pixels, so just 8 + bits are needed. A one indicates the pixel is to be combined; a + zero indicates the pixel is to be skipped. This is in addition + to any alpha or transparency value associated with the pixel. If + you want all pixels to be combined, pass 0xff (255) in mask. */ + +/* Use this routine for x86 platform - uses faster MMX routine if machine + supports MMX */ + +void /* PRIVATE */ +png_combine_row(png_structp png_ptr, png_bytep row, int mask) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; +#endif + + png_debug(1,"in png_combine_row_asm\n"); + + if (mmx_supported == 2) { +#if !defined(PNG_1_0_X) + /* this should have happened in png_init_mmx_flags() already */ + png_warning(png_ptr, "asm_flags may not have been initialized"); +#endif + png_mmx_support(); + } + + if (mask == 0xff) + { + png_memcpy(row, png_ptr->row_buf + 1, + (png_size_t)PNG_ROWBYTES(png_ptr->row_info.pixel_depth, + png_ptr->width)); + } + /* GRR: add "else if (mask == 0)" case? + * or does png_combine_row() not even get called in that case? */ + else + { + switch (png_ptr->row_info.pixel_depth) + { + case 1: + { + png_bytep sp; + png_bytep dp; + int s_inc, s_start, s_end; + int m; + int shift; + png_uint_32 i; + + sp = png_ptr->row_buf + 1; + dp = row; + m = 0x80; +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 7; + s_inc = 1; + } + else +#endif + { + s_start = 7; + s_end = 0; + s_inc = -1; + } + + shift = s_start; + + for (i = 0; i < png_ptr->width; i++) + { + if (m & mask) + { + int value; + + value = (*sp >> shift) & 0x1; + *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + + case 2: + { + png_bytep sp; + png_bytep dp; + int s_start, s_end, s_inc; + int m; + int shift; + png_uint_32 i; + int value; + + sp = png_ptr->row_buf + 1; + dp = row; + m = 0x80; +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 6; + s_inc = 2; + } + else +#endif + { + s_start = 6; + s_end = 0; + s_inc = -2; + } + + shift = s_start; + + for (i = 0; i < png_ptr->width; i++) + { + if (m & mask) + { + value = (*sp >> shift) & 0x3; + *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + + case 4: + { + png_bytep sp; + png_bytep dp; + int s_start, s_end, s_inc; + int m; + int shift; + png_uint_32 i; + int value; + + sp = png_ptr->row_buf + 1; + dp = row; + m = 0x80; +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + { + s_start = 0; + s_end = 4; + s_inc = 4; + } + else +#endif + { + s_start = 4; + s_end = 0; + s_inc = -4; + } + shift = s_start; + + for (i = 0; i < png_ptr->width; i++) + { + if (m & mask) + { + value = (*sp >> shift) & 0xf; + *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); + *dp |= (png_byte)(value << shift); + } + + if (shift == s_end) + { + shift = s_start; + sp++; + dp++; + } + else + shift += s_inc; + if (m == 1) + m = 0x80; + else + m >>= 1; + } + break; + } + + case 8: + { + png_bytep srcptr; + png_bytep dstptr; + png_uint_32 len; + int m; + int diff, unmask; + + __int64 mask0=0x0102040810204080; + +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + srcptr = png_ptr->row_buf + 1; + dstptr = row; + m = 0x80; + unmask = ~mask; + len = png_ptr->width &~7; //reduce to multiple of 8 + diff = png_ptr->width & 7; //amount lost + + _asm + { + movd mm7, unmask //load bit pattern + psubb mm6,mm6 //zero mm6 + punpcklbw mm7,mm7 + punpcklwd mm7,mm7 + punpckldq mm7,mm7 //fill register with 8 masks + + movq mm0,mask0 + + pand mm0,mm7 //nonzero if keep byte + pcmpeqb mm0,mm6 //zeros->1s, v versa + + mov ecx,len //load length of line (pixels) + mov esi,srcptr //load source + mov ebx,dstptr //load dest + cmp ecx,0 //lcr + je mainloop8end + +mainloop8: + movq mm4,[esi] + pand mm4,mm0 + movq mm6,mm0 + pandn mm6,[ebx] + por mm4,mm6 + movq [ebx],mm4 + + add esi,8 //inc by 8 bytes processed + add ebx,8 + sub ecx,8 //dec by 8 pixels processed + + ja mainloop8 +mainloop8end: + + mov ecx,diff + cmp ecx,0 + jz end8 + + mov edx,mask + sal edx,24 //make low byte the high byte + +secondloop8: + sal edx,1 //move high bit to CF + jnc skip8 //if CF = 0 + mov al,[esi] + mov [ebx],al +skip8: + inc esi + inc ebx + + dec ecx + jnz secondloop8 +end8: + emms + } + } + else /* mmx not supported - use modified C routine */ + { + register unsigned int incr1, initial_val, final_val; + png_size_t pixel_bytes; + png_uint_32 i; + register int disp = png_pass_inc[png_ptr->pass]; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + srcptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dstptr = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dstptr, srcptr, pixel_bytes); + srcptr += incr1; + dstptr += incr1; + } + } /* end of else */ + + break; + } // end 8 bpp + + case 16: + { + png_bytep srcptr; + png_bytep dstptr; + png_uint_32 len; + int unmask, diff; + __int64 mask1=0x0101020204040808, + mask0=0x1010202040408080; + +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + srcptr = png_ptr->row_buf + 1; + dstptr = row; + + unmask = ~mask; + len = (png_ptr->width)&~7; + diff = (png_ptr->width)&7; + _asm + { + movd mm7, unmask //load bit pattern + psubb mm6,mm6 //zero mm6 + punpcklbw mm7,mm7 + punpcklwd mm7,mm7 + punpckldq mm7,mm7 //fill register with 8 masks + + movq mm0,mask0 + movq mm1,mask1 + + pand mm0,mm7 + pand mm1,mm7 + + pcmpeqb mm0,mm6 + pcmpeqb mm1,mm6 + + mov ecx,len //load length of line + mov esi,srcptr //load source + mov ebx,dstptr //load dest + cmp ecx,0 //lcr + jz mainloop16end + +mainloop16: + movq mm4,[esi] + pand mm4,mm0 + movq mm6,mm0 + movq mm7,[ebx] + pandn mm6,mm7 + por mm4,mm6 + movq [ebx],mm4 + + movq mm5,[esi+8] + pand mm5,mm1 + movq mm7,mm1 + movq mm6,[ebx+8] + pandn mm7,mm6 + por mm5,mm7 + movq [ebx+8],mm5 + + add esi,16 //inc by 16 bytes processed + add ebx,16 + sub ecx,8 //dec by 8 pixels processed + + ja mainloop16 + +mainloop16end: + mov ecx,diff + cmp ecx,0 + jz end16 + + mov edx,mask + sal edx,24 //make low byte the high byte +secondloop16: + sal edx,1 //move high bit to CF + jnc skip16 //if CF = 0 + mov ax,[esi] + mov [ebx],ax +skip16: + add esi,2 + add ebx,2 + + dec ecx + jnz secondloop16 +end16: + emms + } + } + else /* mmx not supported - use modified C routine */ + { + register unsigned int incr1, initial_val, final_val; + png_size_t pixel_bytes; + png_uint_32 i; + register int disp = png_pass_inc[png_ptr->pass]; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + srcptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dstptr = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dstptr, srcptr, pixel_bytes); + srcptr += incr1; + dstptr += incr1; + } + } /* end of else */ + + break; + } // end 16 bpp + + case 24: + { + png_bytep srcptr; + png_bytep dstptr; + png_uint_32 len; + int unmask, diff; + + __int64 mask2=0x0101010202020404, //24bpp + mask1=0x0408080810101020, + mask0=0x2020404040808080; + + srcptr = png_ptr->row_buf + 1; + dstptr = row; + + unmask = ~mask; + len = (png_ptr->width)&~7; + diff = (png_ptr->width)&7; + +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + _asm + { + movd mm7, unmask //load bit pattern + psubb mm6,mm6 //zero mm6 + punpcklbw mm7,mm7 + punpcklwd mm7,mm7 + punpckldq mm7,mm7 //fill register with 8 masks + + movq mm0,mask0 + movq mm1,mask1 + movq mm2,mask2 + + pand mm0,mm7 + pand mm1,mm7 + pand mm2,mm7 + + pcmpeqb mm0,mm6 + pcmpeqb mm1,mm6 + pcmpeqb mm2,mm6 + + mov ecx,len //load length of line + mov esi,srcptr //load source + mov ebx,dstptr //load dest + cmp ecx,0 + jz mainloop24end + +mainloop24: + movq mm4,[esi] + pand mm4,mm0 + movq mm6,mm0 + movq mm7,[ebx] + pandn mm6,mm7 + por mm4,mm6 + movq [ebx],mm4 + + + movq mm5,[esi+8] + pand mm5,mm1 + movq mm7,mm1 + movq mm6,[ebx+8] + pandn mm7,mm6 + por mm5,mm7 + movq [ebx+8],mm5 + + movq mm6,[esi+16] + pand mm6,mm2 + movq mm4,mm2 + movq mm7,[ebx+16] + pandn mm4,mm7 + por mm6,mm4 + movq [ebx+16],mm6 + + add esi,24 //inc by 24 bytes processed + add ebx,24 + sub ecx,8 //dec by 8 pixels processed + + ja mainloop24 + +mainloop24end: + mov ecx,diff + cmp ecx,0 + jz end24 + + mov edx,mask + sal edx,24 //make low byte the high byte +secondloop24: + sal edx,1 //move high bit to CF + jnc skip24 //if CF = 0 + mov ax,[esi] + mov [ebx],ax + xor eax,eax + mov al,[esi+2] + mov [ebx+2],al +skip24: + add esi,3 + add ebx,3 + + dec ecx + jnz secondloop24 + +end24: + emms + } + } + else /* mmx not supported - use modified C routine */ + { + register unsigned int incr1, initial_val, final_val; + png_size_t pixel_bytes; + png_uint_32 i; + register int disp = png_pass_inc[png_ptr->pass]; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + srcptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dstptr = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dstptr, srcptr, pixel_bytes); + srcptr += incr1; + dstptr += incr1; + } + } /* end of else */ + + break; + } // end 24 bpp + + case 32: + { + png_bytep srcptr; + png_bytep dstptr; + png_uint_32 len; + int unmask, diff; + + __int64 mask3=0x0101010102020202, //32bpp + mask2=0x0404040408080808, + mask1=0x1010101020202020, + mask0=0x4040404080808080; + + srcptr = png_ptr->row_buf + 1; + dstptr = row; + + unmask = ~mask; + len = (png_ptr->width)&~7; + diff = (png_ptr->width)&7; + +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + _asm + { + movd mm7, unmask //load bit pattern + psubb mm6,mm6 //zero mm6 + punpcklbw mm7,mm7 + punpcklwd mm7,mm7 + punpckldq mm7,mm7 //fill register with 8 masks + + movq mm0,mask0 + movq mm1,mask1 + movq mm2,mask2 + movq mm3,mask3 + + pand mm0,mm7 + pand mm1,mm7 + pand mm2,mm7 + pand mm3,mm7 + + pcmpeqb mm0,mm6 + pcmpeqb mm1,mm6 + pcmpeqb mm2,mm6 + pcmpeqb mm3,mm6 + + mov ecx,len //load length of line + mov esi,srcptr //load source + mov ebx,dstptr //load dest + + cmp ecx,0 //lcr + jz mainloop32end + +mainloop32: + movq mm4,[esi] + pand mm4,mm0 + movq mm6,mm0 + movq mm7,[ebx] + pandn mm6,mm7 + por mm4,mm6 + movq [ebx],mm4 + + movq mm5,[esi+8] + pand mm5,mm1 + movq mm7,mm1 + movq mm6,[ebx+8] + pandn mm7,mm6 + por mm5,mm7 + movq [ebx+8],mm5 + + movq mm6,[esi+16] + pand mm6,mm2 + movq mm4,mm2 + movq mm7,[ebx+16] + pandn mm4,mm7 + por mm6,mm4 + movq [ebx+16],mm6 + + movq mm7,[esi+24] + pand mm7,mm3 + movq mm5,mm3 + movq mm4,[ebx+24] + pandn mm5,mm4 + por mm7,mm5 + movq [ebx+24],mm7 + + add esi,32 //inc by 32 bytes processed + add ebx,32 + sub ecx,8 //dec by 8 pixels processed + + ja mainloop32 + +mainloop32end: + mov ecx,diff + cmp ecx,0 + jz end32 + + mov edx,mask + sal edx,24 //make low byte the high byte +secondloop32: + sal edx,1 //move high bit to CF + jnc skip32 //if CF = 0 + mov eax,[esi] + mov [ebx],eax +skip32: + add esi,4 + add ebx,4 + + dec ecx + jnz secondloop32 + +end32: + emms + } + } + else /* mmx _not supported - Use modified C routine */ + { + register unsigned int incr1, initial_val, final_val; + png_size_t pixel_bytes; + png_uint_32 i; + register int disp = png_pass_inc[png_ptr->pass]; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + srcptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dstptr = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dstptr, srcptr, pixel_bytes); + srcptr += incr1; + dstptr += incr1; + } + } /* end of else */ + + break; + } // end 32 bpp + + case 48: + { + png_bytep srcptr; + png_bytep dstptr; + png_uint_32 len; + int unmask, diff; + + __int64 mask5=0x0101010101010202, + mask4=0x0202020204040404, + mask3=0x0404080808080808, + mask2=0x1010101010102020, + mask1=0x2020202040404040, + mask0=0x4040808080808080; + +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + srcptr = png_ptr->row_buf + 1; + dstptr = row; + + unmask = ~mask; + len = (png_ptr->width)&~7; + diff = (png_ptr->width)&7; + _asm + { + movd mm7, unmask //load bit pattern + psubb mm6,mm6 //zero mm6 + punpcklbw mm7,mm7 + punpcklwd mm7,mm7 + punpckldq mm7,mm7 //fill register with 8 masks + + movq mm0,mask0 + movq mm1,mask1 + movq mm2,mask2 + movq mm3,mask3 + movq mm4,mask4 + movq mm5,mask5 + + pand mm0,mm7 + pand mm1,mm7 + pand mm2,mm7 + pand mm3,mm7 + pand mm4,mm7 + pand mm5,mm7 + + pcmpeqb mm0,mm6 + pcmpeqb mm1,mm6 + pcmpeqb mm2,mm6 + pcmpeqb mm3,mm6 + pcmpeqb mm4,mm6 + pcmpeqb mm5,mm6 + + mov ecx,len //load length of line + mov esi,srcptr //load source + mov ebx,dstptr //load dest + + cmp ecx,0 + jz mainloop48end + +mainloop48: + movq mm7,[esi] + pand mm7,mm0 + movq mm6,mm0 + pandn mm6,[ebx] + por mm7,mm6 + movq [ebx],mm7 + + movq mm6,[esi+8] + pand mm6,mm1 + movq mm7,mm1 + pandn mm7,[ebx+8] + por mm6,mm7 + movq [ebx+8],mm6 + + movq mm6,[esi+16] + pand mm6,mm2 + movq mm7,mm2 + pandn mm7,[ebx+16] + por mm6,mm7 + movq [ebx+16],mm6 + + movq mm7,[esi+24] + pand mm7,mm3 + movq mm6,mm3 + pandn mm6,[ebx+24] + por mm7,mm6 + movq [ebx+24],mm7 + + movq mm6,[esi+32] + pand mm6,mm4 + movq mm7,mm4 + pandn mm7,[ebx+32] + por mm6,mm7 + movq [ebx+32],mm6 + + movq mm7,[esi+40] + pand mm7,mm5 + movq mm6,mm5 + pandn mm6,[ebx+40] + por mm7,mm6 + movq [ebx+40],mm7 + + add esi,48 //inc by 32 bytes processed + add ebx,48 + sub ecx,8 //dec by 8 pixels processed + + ja mainloop48 +mainloop48end: + + mov ecx,diff + cmp ecx,0 + jz end48 + + mov edx,mask + sal edx,24 //make low byte the high byte + +secondloop48: + sal edx,1 //move high bit to CF + jnc skip48 //if CF = 0 + mov eax,[esi] + mov [ebx],eax +skip48: + add esi,4 + add ebx,4 + + dec ecx + jnz secondloop48 + +end48: + emms + } + } + else /* mmx _not supported - Use modified C routine */ + { + register unsigned int incr1, initial_val, final_val; + png_size_t pixel_bytes; + png_uint_32 i; + register int disp = png_pass_inc[png_ptr->pass]; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + srcptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dstptr = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dstptr, srcptr, pixel_bytes); + srcptr += incr1; + dstptr += incr1; + } + } /* end of else */ + + break; + } // end 48 bpp + + default: + { + png_bytep sptr; + png_bytep dp; + png_size_t pixel_bytes; + int offset_table[7] = {0, 4, 0, 2, 0, 1, 0}; + unsigned int i; + register int disp = png_pass_inc[png_ptr->pass]; // get the offset + register unsigned int incr1, initial_val, final_val; + + pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); + sptr = png_ptr->row_buf + 1 + offset_table[png_ptr->pass]* + pixel_bytes; + dp = row + offset_table[png_ptr->pass]*pixel_bytes; + initial_val = offset_table[png_ptr->pass]*pixel_bytes; + final_val = png_ptr->width*pixel_bytes; + incr1 = (disp)*pixel_bytes; + for (i = initial_val; i < final_val; i += incr1) + { + png_memcpy(dp, sptr, pixel_bytes); + sptr += incr1; + dp += incr1; + } + break; + } + } /* end switch (png_ptr->row_info.pixel_depth) */ + } /* end if (non-trivial mask) */ + +} /* end png_combine_row() */ + + +#if defined(PNG_READ_INTERLACING_SUPPORTED) + +void /* PRIVATE */ +png_do_read_interlace(png_structp png_ptr) +{ + png_row_infop row_info = &(png_ptr->row_info); + png_bytep row = png_ptr->row_buf + 1; + int pass = png_ptr->pass; + png_uint_32 transformations = png_ptr->transformations; +#ifdef PNG_USE_LOCAL_ARRAYS + const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; +#endif + + png_debug(1,"in png_do_read_interlace\n"); + + if (mmx_supported == 2) { +#if !defined(PNG_1_0_X) + /* this should have happened in png_init_mmx_flags() already */ + png_warning(png_ptr, "asm_flags may not have been initialized"); +#endif + png_mmx_support(); + } + + if (row != NULL && row_info != NULL) + { + png_uint_32 final_width; + + final_width = row_info->width * png_pass_inc[pass]; + + switch (row_info->pixel_depth) + { + case 1: + { + png_bytep sp, dp; + int sshift, dshift; + int s_start, s_end, s_inc; + png_byte v; + png_uint_32 i; + int j; + + sp = row + (png_size_t)((row_info->width - 1) >> 3); + dp = row + (png_size_t)((final_width - 1) >> 3); +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (int)((row_info->width + 7) & 7); + dshift = (int)((final_width + 7) & 7); + s_start = 7; + s_end = 0; + s_inc = -1; + } + else +#endif + { + sshift = 7 - (int)((row_info->width + 7) & 7); + dshift = 7 - (int)((final_width + 7) & 7); + s_start = 0; + s_end = 7; + s_inc = 1; + } + + for (i = row_info->width; i; i--) + { + v = (png_byte)((*sp >> sshift) & 0x1); + for (j = 0; j < png_pass_inc[pass]; j++) + { + *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + + case 2: + { + png_bytep sp, dp; + int sshift, dshift; + int s_start, s_end, s_inc; + png_uint_32 i; + + sp = row + (png_size_t)((row_info->width - 1) >> 2); + dp = row + (png_size_t)((final_width - 1) >> 2); +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (png_size_t)(((row_info->width + 3) & 3) << 1); + dshift = (png_size_t)(((final_width + 3) & 3) << 1); + s_start = 6; + s_end = 0; + s_inc = -2; + } + else +#endif + { + sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1); + dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1); + s_start = 0; + s_end = 6; + s_inc = 2; + } + + for (i = row_info->width; i; i--) + { + png_byte v; + int j; + + v = (png_byte)((*sp >> sshift) & 0x3); + for (j = 0; j < png_pass_inc[pass]; j++) + { + *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + + case 4: + { + png_bytep sp, dp; + int sshift, dshift; + int s_start, s_end, s_inc; + png_uint_32 i; + + sp = row + (png_size_t)((row_info->width - 1) >> 1); + dp = row + (png_size_t)((final_width - 1) >> 1); +#if defined(PNG_READ_PACKSWAP_SUPPORTED) + if (transformations & PNG_PACKSWAP) + { + sshift = (png_size_t)(((row_info->width + 1) & 1) << 2); + dshift = (png_size_t)(((final_width + 1) & 1) << 2); + s_start = 4; + s_end = 0; + s_inc = -4; + } + else +#endif + { + sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2); + dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2); + s_start = 0; + s_end = 4; + s_inc = 4; + } + + for (i = row_info->width; i; i--) + { + png_byte v; + int j; + + v = (png_byte)((*sp >> sshift) & 0xf); + for (j = 0; j < png_pass_inc[pass]; j++) + { + *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); + *dp |= (png_byte)(v << dshift); + if (dshift == s_end) + { + dshift = s_start; + dp--; + } + else + dshift += s_inc; + } + if (sshift == s_end) + { + sshift = s_start; + sp--; + } + else + sshift += s_inc; + } + break; + } + + default: // This is the place where the routine is modified + { + __int64 const4 = 0x0000000000FFFFFF; + // __int64 const5 = 0x000000FFFFFF0000; // unused... + __int64 const6 = 0x00000000000000FF; + png_bytep sptr, dp; + png_uint_32 i; + png_size_t pixel_bytes; + int width = row_info->width; + + pixel_bytes = (row_info->pixel_depth >> 3); + + sptr = row + (width - 1) * pixel_bytes; + dp = row + (final_width - 1) * pixel_bytes; + // New code by Nirav Chhatrapati - Intel Corporation + // sign fix by GRR + // NOTE: there is NO MMX code for 48-bit and 64-bit images + + // use MMX routine if machine supports it +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_INTERLACE) + /* && mmx_supported */ ) +#else + if (mmx_supported) +#endif + { + if (pixel_bytes == 3) + { + if (((pass == 0) || (pass == 1)) && width) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width + sub edi, 21 // (png_pass_inc[pass] - 1)*pixel_bytes +loop_pass0: + movd mm0, [esi] ; X X X X X v2 v1 v0 + pand mm0, const4 ; 0 0 0 0 0 v2 v1 v0 + movq mm1, mm0 ; 0 0 0 0 0 v2 v1 v0 + psllq mm0, 16 ; 0 0 0 v2 v1 v0 0 0 + movq mm2, mm0 ; 0 0 0 v2 v1 v0 0 0 + psllq mm0, 24 ; v2 v1 v0 0 0 0 0 0 + psrlq mm1, 8 ; 0 0 0 0 0 0 v2 v1 + por mm0, mm2 ; v2 v1 v0 v2 v1 v0 0 0 + por mm0, mm1 ; v2 v1 v0 v2 v1 v0 v2 v1 + movq mm3, mm0 ; v2 v1 v0 v2 v1 v0 v2 v1 + psllq mm0, 16 ; v0 v2 v1 v0 v2 v1 0 0 + movq mm4, mm3 ; v2 v1 v0 v2 v1 v0 v2 v1 + punpckhdq mm3, mm0 ; v0 v2 v1 v0 v2 v1 v0 v2 + movq [edi+16] , mm4 + psrlq mm0, 32 ; 0 0 0 0 v0 v2 v1 v0 + movq [edi+8] , mm3 + punpckldq mm0, mm4 ; v1 v0 v2 v1 v0 v2 v1 v0 + sub esi, 3 + movq [edi], mm0 + sub edi, 24 + //sub esi, 3 + dec ecx + jnz loop_pass0 + EMMS + } + } + else if (((pass == 2) || (pass == 3)) && width) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width + sub edi, 9 // (png_pass_inc[pass] - 1)*pixel_bytes +loop_pass2: + movd mm0, [esi] ; X X X X X v2 v1 v0 + pand mm0, const4 ; 0 0 0 0 0 v2 v1 v0 + movq mm1, mm0 ; 0 0 0 0 0 v2 v1 v0 + psllq mm0, 16 ; 0 0 0 v2 v1 v0 0 0 + movq mm2, mm0 ; 0 0 0 v2 v1 v0 0 0 + psllq mm0, 24 ; v2 v1 v0 0 0 0 0 0 + psrlq mm1, 8 ; 0 0 0 0 0 0 v2 v1 + por mm0, mm2 ; v2 v1 v0 v2 v1 v0 0 0 + por mm0, mm1 ; v2 v1 v0 v2 v1 v0 v2 v1 + movq [edi+4], mm0 ; move to memory + psrlq mm0, 16 ; 0 0 v2 v1 v0 v2 v1 v0 + movd [edi], mm0 ; move to memory + sub esi, 3 + sub edi, 12 + dec ecx + jnz loop_pass2 + EMMS + } + } + else if (width) /* && ((pass == 4) || (pass == 5)) */ + { + int width_mmx = ((width >> 1) << 1) - 8; + if (width_mmx < 0) + width_mmx = 0; + width -= width_mmx; // 8 or 9 pix, 24 or 27 bytes + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 3 + sub edi, 9 +loop_pass4: + movq mm0, [esi] ; X X v2 v1 v0 v5 v4 v3 + movq mm7, mm0 ; X X v2 v1 v0 v5 v4 v3 + movq mm6, mm0 ; X X v2 v1 v0 v5 v4 v3 + psllq mm0, 24 ; v1 v0 v5 v4 v3 0 0 0 + pand mm7, const4 ; 0 0 0 0 0 v5 v4 v3 + psrlq mm6, 24 ; 0 0 0 X X v2 v1 v0 + por mm0, mm7 ; v1 v0 v5 v4 v3 v5 v4 v3 + movq mm5, mm6 ; 0 0 0 X X v2 v1 v0 + psllq mm6, 8 ; 0 0 X X v2 v1 v0 0 + movq [edi], mm0 ; move quad to memory + psrlq mm5, 16 ; 0 0 0 0 0 X X v2 + pand mm5, const6 ; 0 0 0 0 0 0 0 v2 + por mm6, mm5 ; 0 0 X X v2 v1 v0 v2 + movd [edi+8], mm6 ; move double to memory + sub esi, 6 + sub edi, 12 + sub ecx, 2 + jnz loop_pass4 + EMMS + } + } + + sptr -= width_mmx*3; + dp -= width_mmx*6; + for (i = width; i; i--) + { + png_byte v[8]; + int j; + + png_memcpy(v, sptr, 3); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, 3); + dp -= 3; + } + sptr -= 3; + } + } + } /* end of pixel_bytes == 3 */ + + else if (pixel_bytes == 1) + { + if (((pass == 0) || (pass == 1)) && width) + { + int width_mmx = ((width >> 2) << 2); + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub edi, 31 + sub esi, 3 +loop1_pass0: + movd mm0, [esi] ; X X X X v0 v1 v2 v3 + movq mm1, mm0 ; X X X X v0 v1 v2 v3 + punpcklbw mm0, mm0 ; v0 v0 v1 v1 v2 v2 v3 v3 + movq mm2, mm0 ; v0 v0 v1 v1 v2 v2 v3 v3 + punpcklwd mm0, mm0 ; v2 v2 v2 v2 v3 v3 v3 v3 + movq mm3, mm0 ; v2 v2 v2 v2 v3 v3 v3 v3 + punpckldq mm0, mm0 ; v3 v3 v3 v3 v3 v3 v3 v3 + punpckhdq mm3, mm3 ; v2 v2 v2 v2 v2 v2 v2 v2 + movq [edi], mm0 ; move to memory v3 + punpckhwd mm2, mm2 ; v0 v0 v0 v0 v1 v1 v1 v1 + movq [edi+8], mm3 ; move to memory v2 + movq mm4, mm2 ; v0 v0 v0 v0 v1 v1 v1 v1 + punpckldq mm2, mm2 ; v1 v1 v1 v1 v1 v1 v1 v1 + punpckhdq mm4, mm4 ; v0 v0 v0 v0 v0 v0 v0 v0 + movq [edi+16], mm2 ; move to memory v1 + movq [edi+24], mm4 ; move to memory v0 + sub esi, 4 + sub edi, 32 + sub ecx, 4 + jnz loop1_pass0 + EMMS + } + } + + sptr -= width_mmx; + dp -= width_mmx*8; + for (i = width; i; i--) + { + int j; + + /* I simplified this part in version 1.0.4e + * here and in several other instances where + * pixel_bytes == 1 -- GR-P + * + * Original code: + * + * png_byte v[8]; + * png_memcpy(v, sptr, pixel_bytes); + * for (j = 0; j < png_pass_inc[pass]; j++) + * { + * png_memcpy(dp, v, pixel_bytes); + * dp -= pixel_bytes; + * } + * sptr -= pixel_bytes; + * + * Replacement code is in the next three lines: + */ + + for (j = 0; j < png_pass_inc[pass]; j++) + *dp-- = *sptr; + sptr--; + } + } + else if (((pass == 2) || (pass == 3)) && width) + { + int width_mmx = ((width >> 2) << 2); + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub edi, 15 + sub esi, 3 +loop1_pass2: + movd mm0, [esi] ; X X X X v0 v1 v2 v3 + punpcklbw mm0, mm0 ; v0 v0 v1 v1 v2 v2 v3 v3 + movq mm1, mm0 ; v0 v0 v1 v1 v2 v2 v3 v3 + punpcklwd mm0, mm0 ; v2 v2 v2 v2 v3 v3 v3 v3 + punpckhwd mm1, mm1 ; v0 v0 v0 v0 v1 v1 v1 v1 + movq [edi], mm0 ; move to memory v2 and v3 + sub esi, 4 + movq [edi+8], mm1 ; move to memory v1 and v0 + sub edi, 16 + sub ecx, 4 + jnz loop1_pass2 + EMMS + } + } + + sptr -= width_mmx; + dp -= width_mmx*4; + for (i = width; i; i--) + { + int j; + + for (j = 0; j < png_pass_inc[pass]; j++) + { + *dp-- = *sptr; + } + sptr --; + } + } + else if (width) /* && ((pass == 4) || (pass == 5))) */ + { + int width_mmx = ((width >> 3) << 3); + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub edi, 15 + sub esi, 7 +loop1_pass4: + movq mm0, [esi] ; v0 v1 v2 v3 v4 v5 v6 v7 + movq mm1, mm0 ; v0 v1 v2 v3 v4 v5 v6 v7 + punpcklbw mm0, mm0 ; v4 v4 v5 v5 v6 v6 v7 v7 + //movq mm1, mm0 ; v0 v0 v1 v1 v2 v2 v3 v3 + punpckhbw mm1, mm1 ;v0 v0 v1 v1 v2 v2 v3 v3 + movq [edi+8], mm1 ; move to memory v0 v1 v2 and v3 + sub esi, 8 + movq [edi], mm0 ; move to memory v4 v5 v6 and v7 + //sub esi, 4 + sub edi, 16 + sub ecx, 8 + jnz loop1_pass4 + EMMS + } + } + + sptr -= width_mmx; + dp -= width_mmx*2; + for (i = width; i; i--) + { + int j; + + for (j = 0; j < png_pass_inc[pass]; j++) + { + *dp-- = *sptr; + } + sptr --; + } + } + } /* end of pixel_bytes == 1 */ + + else if (pixel_bytes == 2) + { + if (((pass == 0) || (pass == 1)) && width) + { + int width_mmx = ((width >> 1) << 1); + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 2 + sub edi, 30 +loop2_pass0: + movd mm0, [esi] ; X X X X v1 v0 v3 v2 + punpcklwd mm0, mm0 ; v1 v0 v1 v0 v3 v2 v3 v2 + movq mm1, mm0 ; v1 v0 v1 v0 v3 v2 v3 v2 + punpckldq mm0, mm0 ; v3 v2 v3 v2 v3 v2 v3 v2 + punpckhdq mm1, mm1 ; v1 v0 v1 v0 v1 v0 v1 v0 + movq [edi], mm0 + movq [edi + 8], mm0 + movq [edi + 16], mm1 + movq [edi + 24], mm1 + sub esi, 4 + sub edi, 32 + sub ecx, 2 + jnz loop2_pass0 + EMMS + } + } + + sptr -= (width_mmx*2 - 2); // sign fixed + dp -= (width_mmx*16 - 2); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 2; + png_memcpy(v, sptr, 2); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 2; + png_memcpy(dp, v, 2); + } + } + } + else if (((pass == 2) || (pass == 3)) && width) + { + int width_mmx = ((width >> 1) << 1) ; + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 2 + sub edi, 14 +loop2_pass2: + movd mm0, [esi] ; X X X X v1 v0 v3 v2 + punpcklwd mm0, mm0 ; v1 v0 v1 v0 v3 v2 v3 v2 + movq mm1, mm0 ; v1 v0 v1 v0 v3 v2 v3 v2 + punpckldq mm0, mm0 ; v3 v2 v3 v2 v3 v2 v3 v2 + punpckhdq mm1, mm1 ; v1 v0 v1 v0 v1 v0 v1 v0 + movq [edi], mm0 + sub esi, 4 + movq [edi + 8], mm1 + //sub esi, 4 + sub edi, 16 + sub ecx, 2 + jnz loop2_pass2 + EMMS + } + } + + sptr -= (width_mmx*2 - 2); // sign fixed + dp -= (width_mmx*8 - 2); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 2; + png_memcpy(v, sptr, 2); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 2; + png_memcpy(dp, v, 2); + } + } + } + else if (width) // pass == 4 or 5 + { + int width_mmx = ((width >> 1) << 1) ; + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 2 + sub edi, 6 +loop2_pass4: + movd mm0, [esi] ; X X X X v1 v0 v3 v2 + punpcklwd mm0, mm0 ; v1 v0 v1 v0 v3 v2 v3 v2 + sub esi, 4 + movq [edi], mm0 + sub edi, 8 + sub ecx, 2 + jnz loop2_pass4 + EMMS + } + } + + sptr -= (width_mmx*2 - 2); // sign fixed + dp -= (width_mmx*4 - 2); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 2; + png_memcpy(v, sptr, 2); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 2; + png_memcpy(dp, v, 2); + } + } + } + } /* end of pixel_bytes == 2 */ + + else if (pixel_bytes == 4) + { + if (((pass == 0) || (pass == 1)) && width) + { + int width_mmx = ((width >> 1) << 1) ; + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 4 + sub edi, 60 +loop4_pass0: + movq mm0, [esi] ; v3 v2 v1 v0 v7 v6 v5 v4 + movq mm1, mm0 ; v3 v2 v1 v0 v7 v6 v5 v4 + punpckldq mm0, mm0 ; v7 v6 v5 v4 v7 v6 v5 v4 + punpckhdq mm1, mm1 ; v3 v2 v1 v0 v3 v2 v1 v0 + movq [edi], mm0 + movq [edi + 8], mm0 + movq [edi + 16], mm0 + movq [edi + 24], mm0 + movq [edi+32], mm1 + movq [edi + 40], mm1 + movq [edi+ 48], mm1 + sub esi, 8 + movq [edi + 56], mm1 + sub edi, 64 + sub ecx, 2 + jnz loop4_pass0 + EMMS + } + } + + sptr -= (width_mmx*4 - 4); // sign fixed + dp -= (width_mmx*32 - 4); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 4; + png_memcpy(v, sptr, 4); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 4; + png_memcpy(dp, v, 4); + } + } + } + else if (((pass == 2) || (pass == 3)) && width) + { + int width_mmx = ((width >> 1) << 1) ; + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 4 + sub edi, 28 +loop4_pass2: + movq mm0, [esi] ; v3 v2 v1 v0 v7 v6 v5 v4 + movq mm1, mm0 ; v3 v2 v1 v0 v7 v6 v5 v4 + punpckldq mm0, mm0 ; v7 v6 v5 v4 v7 v6 v5 v4 + punpckhdq mm1, mm1 ; v3 v2 v1 v0 v3 v2 v1 v0 + movq [edi], mm0 + movq [edi + 8], mm0 + movq [edi+16], mm1 + movq [edi + 24], mm1 + sub esi, 8 + sub edi, 32 + sub ecx, 2 + jnz loop4_pass2 + EMMS + } + } + + sptr -= (width_mmx*4 - 4); // sign fixed + dp -= (width_mmx*16 - 4); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 4; + png_memcpy(v, sptr, 4); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 4; + png_memcpy(dp, v, 4); + } + } + } + else if (width) // pass == 4 or 5 + { + int width_mmx = ((width >> 1) << 1) ; + width -= width_mmx; + if (width_mmx) + { + _asm + { + mov esi, sptr + mov edi, dp + mov ecx, width_mmx + sub esi, 4 + sub edi, 12 +loop4_pass4: + movq mm0, [esi] ; v3 v2 v1 v0 v7 v6 v5 v4 + movq mm1, mm0 ; v3 v2 v1 v0 v7 v6 v5 v4 + punpckldq mm0, mm0 ; v7 v6 v5 v4 v7 v6 v5 v4 + punpckhdq mm1, mm1 ; v3 v2 v1 v0 v3 v2 v1 v0 + movq [edi], mm0 + sub esi, 8 + movq [edi + 8], mm1 + sub edi, 16 + sub ecx, 2 + jnz loop4_pass4 + EMMS + } + } + + sptr -= (width_mmx*4 - 4); // sign fixed + dp -= (width_mmx*8 - 4); // sign fixed + for (i = width; i; i--) + { + png_byte v[8]; + int j; + sptr -= 4; + png_memcpy(v, sptr, 4); + for (j = 0; j < png_pass_inc[pass]; j++) + { + dp -= 4; + png_memcpy(dp, v, 4); + } + } + } + + } /* end of pixel_bytes == 4 */ + + else if (pixel_bytes == 6) + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, 6); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, 6); + dp -= 6; + } + sptr -= 6; + } + } /* end of pixel_bytes == 6 */ + + else + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr-= pixel_bytes; + } + } + } /* end of mmx_supported */ + + else /* MMX not supported: use modified C code - takes advantage + * of inlining of memcpy for a constant */ + { + if (pixel_bytes == 1) + { + for (i = width; i; i--) + { + int j; + for (j = 0; j < png_pass_inc[pass]; j++) + *dp-- = *sptr; + sptr--; + } + } + else if (pixel_bytes == 3) + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr -= pixel_bytes; + } + } + else if (pixel_bytes == 2) + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr -= pixel_bytes; + } + } + else if (pixel_bytes == 4) + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr -= pixel_bytes; + } + } + else if (pixel_bytes == 6) + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr -= pixel_bytes; + } + } + else + { + for (i = width; i; i--) + { + png_byte v[8]; + int j; + png_memcpy(v, sptr, pixel_bytes); + for (j = 0; j < png_pass_inc[pass]; j++) + { + png_memcpy(dp, v, pixel_bytes); + dp -= pixel_bytes; + } + sptr -= pixel_bytes; + } + } + + } /* end of MMX not supported */ + break; + } + } /* end switch (row_info->pixel_depth) */ + + row_info->width = final_width; + + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width); + } + +} + +#endif /* PNG_READ_INTERLACING_SUPPORTED */ + + +// These variables are utilized in the functions below. They are declared +// globally here to ensure alignment on 8-byte boundaries. + +union uAll { + __int64 use; + double align; +} LBCarryMask = {0x0101010101010101}, + HBClearMask = {0x7f7f7f7f7f7f7f7f}, + ActiveMask, ActiveMask2, ActiveMaskEnd, ShiftBpp, ShiftRem; + + +// Optimized code for PNG Average filter decoder +void /* PRIVATE */ +png_read_filter_row_mmx_avg(png_row_infop row_info, png_bytep row + , png_bytep prev_row) +{ + int bpp; + png_uint_32 FullLength; + png_uint_32 MMXLength; + //png_uint_32 len; + int diff; + + bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel + FullLength = row_info->rowbytes; // # of bytes to filter + _asm { + // Init address pointers and offset + mov edi, row // edi ==> Avg(x) + xor ebx, ebx // ebx ==> x + mov edx, edi + mov esi, prev_row // esi ==> Prior(x) + sub edx, bpp // edx ==> Raw(x-bpp) + + xor eax, eax + // Compute the Raw value for the first bpp bytes + // Raw(x) = Avg(x) + (Prior(x)/2) +davgrlp: + mov al, [esi + ebx] // Load al with Prior(x) + inc ebx + shr al, 1 // divide by 2 + add al, [edi+ebx-1] // Add Avg(x); -1 to offset inc ebx + cmp ebx, bpp + mov [edi+ebx-1], al // Write back Raw(x); + // mov does not affect flags; -1 to offset inc ebx + jb davgrlp + // get # of bytes to alignment + mov diff, edi // take start of row + add diff, ebx // add bpp + add diff, 0xf // add 7 + 8 to incr past alignment boundary + and diff, 0xfffffff8 // mask to alignment boundary + sub diff, edi // subtract from start ==> value ebx at alignment + jz davggo + // fix alignment + // Compute the Raw value for the bytes upto the alignment boundary + // Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2) + xor ecx, ecx +davglp1: + xor eax, eax + mov cl, [esi + ebx] // load cl with Prior(x) + mov al, [edx + ebx] // load al with Raw(x-bpp) + add ax, cx + inc ebx + shr ax, 1 // divide by 2 + add al, [edi+ebx-1] // Add Avg(x); -1 to offset inc ebx + cmp ebx, diff // Check if at alignment boundary + mov [edi+ebx-1], al // Write back Raw(x); + // mov does not affect flags; -1 to offset inc ebx + jb davglp1 // Repeat until at alignment boundary +davggo: + mov eax, FullLength + mov ecx, eax + sub eax, ebx // subtract alignment fix + and eax, 0x00000007 // calc bytes over mult of 8 + sub ecx, eax // drop over bytes from original length + mov MMXLength, ecx + } // end _asm block + // Now do the math for the rest of the row + switch ( bpp ) + { + case 3: + { + ActiveMask.use = 0x0000000000ffffff; + ShiftBpp.use = 24; // == 3 * 8 + ShiftRem.use = 40; // == 64 - 24 + _asm { + // Re-init address pointers and offset + movq mm7, ActiveMask + mov ebx, diff // ebx ==> x = offset to alignment boundary + movq mm5, LBCarryMask + mov edi, row // edi ==> Avg(x) + movq mm4, HBClearMask + mov esi, prev_row // esi ==> Prior(x) + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm2, [edi + ebx - 8] // Load previous aligned 8 bytes + // (we correct position in loop below) +davg3lp: + movq mm0, [edi + ebx] // Load mm0 with Avg(x) + // Add (Prev_row/2) to Average + movq mm3, mm5 + psrlq mm2, ShiftRem // Correct position Raw(x-bpp) data + movq mm1, [esi + ebx] // Load mm1 with Prior(x) + movq mm6, mm7 + pand mm3, mm1 // get lsb for each prev_row byte + psrlq mm1, 1 // divide prev_row bytes by 2 + pand mm1, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm1 // add (Prev_row/2) to Avg for each byte + // Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 1 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active + // byte + // Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry + psllq mm6, ShiftBpp // shift the mm6 mask to cover bytes 3-5 + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active + // byte + + // Add 3rd active group (Raw(x-bpp)/2) to Average with LBCarry + psllq mm6, ShiftBpp // shift the mm6 mask to cover the last two + // bytes + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + // Data only needs to be shifted once here to + // get the correct x-bpp offset. + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + add ebx, 8 + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active + // byte + + // Now ready to write back to memory + movq [edi + ebx - 8], mm0 + // Move updated Raw(x) to use as Raw(x-bpp) for next loop + cmp ebx, MMXLength + movq mm2, mm0 // mov updated Raw(x) to mm2 + jb davg3lp + } // end _asm block + } + break; + + case 6: + case 4: + case 7: + case 5: + { + ActiveMask.use = 0xffffffffffffffff; // use shift below to clear + // appropriate inactive bytes + ShiftBpp.use = bpp << 3; + ShiftRem.use = 64 - ShiftBpp.use; + _asm { + movq mm4, HBClearMask + // Re-init address pointers and offset + mov ebx, diff // ebx ==> x = offset to alignment boundary + // Load ActiveMask and clear all bytes except for 1st active group + movq mm7, ActiveMask + mov edi, row // edi ==> Avg(x) + psrlq mm7, ShiftRem + mov esi, prev_row // esi ==> Prior(x) + movq mm6, mm7 + movq mm5, LBCarryMask + psllq mm6, ShiftBpp // Create mask for 2nd active group + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm2, [edi + ebx - 8] // Load previous aligned 8 bytes + // (we correct position in loop below) +davg4lp: + movq mm0, [edi + ebx] + psrlq mm2, ShiftRem // shift data to position correctly + movq mm1, [esi + ebx] + // Add (Prev_row/2) to Average + movq mm3, mm5 + pand mm3, mm1 // get lsb for each prev_row byte + psrlq mm1, 1 // divide prev_row bytes by 2 + pand mm1, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm1 // add (Prev_row/2) to Avg for each byte + // Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm7 // Leave only Active Group 1 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active + // byte + // Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + add ebx, 8 + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active + // byte + cmp ebx, MMXLength + // Now ready to write back to memory + movq [edi + ebx - 8], mm0 + // Prep Raw(x-bpp) for next loop + movq mm2, mm0 // mov updated Raws to mm2 + jb davg4lp + } // end _asm block + } + break; + case 2: + { + ActiveMask.use = 0x000000000000ffff; + ShiftBpp.use = 16; // == 2 * 8 [BUGFIX] + ShiftRem.use = 48; // == 64 - 16 [BUGFIX] + _asm { + // Load ActiveMask + movq mm7, ActiveMask + // Re-init address pointers and offset + mov ebx, diff // ebx ==> x = offset to alignment boundary + movq mm5, LBCarryMask + mov edi, row // edi ==> Avg(x) + movq mm4, HBClearMask + mov esi, prev_row // esi ==> Prior(x) + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm2, [edi + ebx - 8] // Load previous aligned 8 bytes + // (we correct position in loop below) +davg2lp: + movq mm0, [edi + ebx] + psrlq mm2, ShiftRem // shift data to position correctly [BUGFIX] + movq mm1, [esi + ebx] + // Add (Prev_row/2) to Average + movq mm3, mm5 + pand mm3, mm1 // get lsb for each prev_row byte + psrlq mm1, 1 // divide prev_row bytes by 2 + pand mm1, mm4 // clear invalid bit 7 of each byte + movq mm6, mm7 + paddb mm0, mm1 // add (Prev_row/2) to Avg for each byte + // Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 1 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active byte + // Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry + psllq mm6, ShiftBpp // shift the mm6 mask to cover bytes 2 & 3 + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active byte + + // Add rdd active group (Raw(x-bpp)/2) to Average with LBCarry + psllq mm6, ShiftBpp // shift the mm6 mask to cover bytes 4 & 5 + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + // Data only needs to be shifted once here to + // get the correct x-bpp offset. + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active byte + + // Add 4th active group (Raw(x-bpp)/2) to Average with LBCarry + psllq mm6, ShiftBpp // shift the mm6 mask to cover bytes 6 & 7 + movq mm2, mm0 // mov updated Raws to mm2 + psllq mm2, ShiftBpp // shift data to position correctly + // Data only needs to be shifted once here to + // get the correct x-bpp offset. + add ebx, 8 + movq mm1, mm3 // now use mm1 for getting LBCarrys + pand mm1, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 (Only valid for active group) + psrlq mm2, 1 // divide raw bytes by 2 + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm2, mm1 // add LBCarrys to (Raw(x-bpp)/2) for each byte + pand mm2, mm6 // Leave only Active Group 2 bytes to add to Avg + paddb mm0, mm2 // add (Raw/2) + LBCarrys to Avg for each Active byte + + cmp ebx, MMXLength + // Now ready to write back to memory + movq [edi + ebx - 8], mm0 + // Prep Raw(x-bpp) for next loop + movq mm2, mm0 // mov updated Raws to mm2 + jb davg2lp + } // end _asm block + } + break; + + case 1: // bpp == 1 + { + _asm { + // Re-init address pointers and offset + mov ebx, diff // ebx ==> x = offset to alignment boundary + mov edi, row // edi ==> Avg(x) + cmp ebx, FullLength // Test if offset at end of array + jnb davg1end + // Do Paeth decode for remaining bytes + mov esi, prev_row // esi ==> Prior(x) + mov edx, edi + xor ecx, ecx // zero ecx before using cl & cx in loop below + sub edx, bpp // edx ==> Raw(x-bpp) +davg1lp: + // Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2) + xor eax, eax + mov cl, [esi + ebx] // load cl with Prior(x) + mov al, [edx + ebx] // load al with Raw(x-bpp) + add ax, cx + inc ebx + shr ax, 1 // divide by 2 + add al, [edi+ebx-1] // Add Avg(x); -1 to offset inc ebx + cmp ebx, FullLength // Check if at end of array + mov [edi+ebx-1], al // Write back Raw(x); + // mov does not affect flags; -1 to offset inc ebx + jb davg1lp +davg1end: + } // end _asm block + } + return; + + case 8: // bpp == 8 + { + _asm { + // Re-init address pointers and offset + mov ebx, diff // ebx ==> x = offset to alignment boundary + movq mm5, LBCarryMask + mov edi, row // edi ==> Avg(x) + movq mm4, HBClearMask + mov esi, prev_row // esi ==> Prior(x) + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm2, [edi + ebx - 8] // Load previous aligned 8 bytes + // (NO NEED to correct position in loop below) +davg8lp: + movq mm0, [edi + ebx] + movq mm3, mm5 + movq mm1, [esi + ebx] + add ebx, 8 + pand mm3, mm1 // get lsb for each prev_row byte + psrlq mm1, 1 // divide prev_row bytes by 2 + pand mm3, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 + psrlq mm2, 1 // divide raw bytes by 2 + pand mm1, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm3 // add LBCarrys to Avg for each byte + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm1 // add (Prev_row/2) to Avg for each byte + paddb mm0, mm2 // add (Raw/2) to Avg for each byte + cmp ebx, MMXLength + movq [edi + ebx - 8], mm0 + movq mm2, mm0 // reuse as Raw(x-bpp) + jb davg8lp + } // end _asm block + } + break; + default: // bpp greater than 8 + { + _asm { + movq mm5, LBCarryMask + // Re-init address pointers and offset + mov ebx, diff // ebx ==> x = offset to alignment boundary + mov edi, row // edi ==> Avg(x) + movq mm4, HBClearMask + mov edx, edi + mov esi, prev_row // esi ==> Prior(x) + sub edx, bpp // edx ==> Raw(x-bpp) +davgAlp: + movq mm0, [edi + ebx] + movq mm3, mm5 + movq mm1, [esi + ebx] + pand mm3, mm1 // get lsb for each prev_row byte + movq mm2, [edx + ebx] + psrlq mm1, 1 // divide prev_row bytes by 2 + pand mm3, mm2 // get LBCarrys for each byte where both + // lsb's were == 1 + psrlq mm2, 1 // divide raw bytes by 2 + pand mm1, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm3 // add LBCarrys to Avg for each byte + pand mm2, mm4 // clear invalid bit 7 of each byte + paddb mm0, mm1 // add (Prev_row/2) to Avg for each byte + add ebx, 8 + paddb mm0, mm2 // add (Raw/2) to Avg for each byte + cmp ebx, MMXLength + movq [edi + ebx - 8], mm0 + jb davgAlp + } // end _asm block + } + break; + } // end switch ( bpp ) + + _asm { + // MMX acceleration complete now do clean-up + // Check if any remaining bytes left to decode + mov ebx, MMXLength // ebx ==> x = offset bytes remaining after MMX + mov edi, row // edi ==> Avg(x) + cmp ebx, FullLength // Test if offset at end of array + jnb davgend + // Do Paeth decode for remaining bytes + mov esi, prev_row // esi ==> Prior(x) + mov edx, edi + xor ecx, ecx // zero ecx before using cl & cx in loop below + sub edx, bpp // edx ==> Raw(x-bpp) +davglp2: + // Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2) + xor eax, eax + mov cl, [esi + ebx] // load cl with Prior(x) + mov al, [edx + ebx] // load al with Raw(x-bpp) + add ax, cx + inc ebx + shr ax, 1 // divide by 2 + add al, [edi+ebx-1] // Add Avg(x); -1 to offset inc ebx + cmp ebx, FullLength // Check if at end of array + mov [edi+ebx-1], al // Write back Raw(x); + // mov does not affect flags; -1 to offset inc ebx + jb davglp2 +davgend: + emms // End MMX instructions; prep for possible FP instrs. + } // end _asm block +} + +// Optimized code for PNG Paeth filter decoder +void /* PRIVATE */ +png_read_filter_row_mmx_paeth(png_row_infop row_info, png_bytep row, + png_bytep prev_row) +{ + png_uint_32 FullLength; + png_uint_32 MMXLength; + //png_uint_32 len; + int bpp; + int diff; + //int ptemp; + int patemp, pbtemp, pctemp; + + bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel + FullLength = row_info->rowbytes; // # of bytes to filter + _asm + { + xor ebx, ebx // ebx ==> x offset + mov edi, row + xor edx, edx // edx ==> x-bpp offset + mov esi, prev_row + xor eax, eax + + // Compute the Raw value for the first bpp bytes + // Note: the formula works out to be always + // Paeth(x) = Raw(x) + Prior(x) where x < bpp +dpthrlp: + mov al, [edi + ebx] + add al, [esi + ebx] + inc ebx + cmp ebx, bpp + mov [edi + ebx - 1], al + jb dpthrlp + // get # of bytes to alignment + mov diff, edi // take start of row + add diff, ebx // add bpp + xor ecx, ecx + add diff, 0xf // add 7 + 8 to incr past alignment boundary + and diff, 0xfffffff8 // mask to alignment boundary + sub diff, edi // subtract from start ==> value ebx at alignment + jz dpthgo + // fix alignment +dpthlp1: + xor eax, eax + // pav = p - a = (a + b - c) - a = b - c + mov al, [esi + ebx] // load Prior(x) into al + mov cl, [esi + edx] // load Prior(x-bpp) into cl + sub eax, ecx // subtract Prior(x-bpp) + mov patemp, eax // Save pav for later use + xor eax, eax + // pbv = p - b = (a + b - c) - b = a - c + mov al, [edi + edx] // load Raw(x-bpp) into al + sub eax, ecx // subtract Prior(x-bpp) + mov ecx, eax + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + add eax, patemp // pcv = pav + pbv + // pc = abs(pcv) + test eax, 0x80000000 + jz dpthpca + neg eax // reverse sign of neg values +dpthpca: + mov pctemp, eax // save pc for later use + // pb = abs(pbv) + test ecx, 0x80000000 + jz dpthpba + neg ecx // reverse sign of neg values +dpthpba: + mov pbtemp, ecx // save pb for later use + // pa = abs(pav) + mov eax, patemp + test eax, 0x80000000 + jz dpthpaa + neg eax // reverse sign of neg values +dpthpaa: + mov patemp, eax // save pa for later use + // test if pa <= pb + cmp eax, ecx + jna dpthabb + // pa > pb; now test if pb <= pc + cmp ecx, pctemp + jna dpthbbc + // pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthpaeth +dpthbbc: + // pb <= pc; Raw(x) = Paeth(x) + Prior(x) + mov cl, [esi + ebx] // load Prior(x) into cl + jmp dpthpaeth +dpthabb: + // pa <= pb; now test if pa <= pc + cmp eax, pctemp + jna dpthabc + // pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthpaeth +dpthabc: + // pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp) + mov cl, [edi + edx] // load Raw(x-bpp) into cl +dpthpaeth: + inc ebx + inc edx + // Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256 + add [edi + ebx - 1], cl + cmp ebx, diff + jb dpthlp1 +dpthgo: + mov ecx, FullLength + mov eax, ecx + sub eax, ebx // subtract alignment fix + and eax, 0x00000007 // calc bytes over mult of 8 + sub ecx, eax // drop over bytes from original length + mov MMXLength, ecx + } // end _asm block + // Now do the math for the rest of the row + switch ( bpp ) + { + case 3: + { + ActiveMask.use = 0x0000000000ffffff; + ActiveMaskEnd.use = 0xffff000000000000; + ShiftBpp.use = 24; // == bpp(3) * 8 + ShiftRem.use = 40; // == 64 - 24 + _asm + { + mov ebx, diff + mov edi, row + mov esi, prev_row + pxor mm0, mm0 + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] +dpth3lp: + psrlq mm1, ShiftRem // shift last 3 bytes to 1st 3 bytes + movq mm2, [esi + ebx] // load b=Prior(x) + punpcklbw mm1, mm0 // Unpack High bytes of a + movq mm3, [esi+ebx-8] // Prep c=Prior(x-bpp) bytes + punpcklbw mm2, mm0 // Unpack High bytes of b + psrlq mm3, ShiftRem // shift last 3 bytes to 1st 3 bytes + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + punpcklbw mm3, mm0 // Unpack High bytes of c + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + paddw mm7, mm3 + pxor mm0, mm0 + packuswb mm7, mm1 + movq mm3, [esi + ebx] // load c=Prior(x-bpp) + pand mm7, ActiveMask + movq mm2, mm3 // load b=Prior(x) step 1 + paddb mm7, [edi + ebx] // add Paeth predictor with Raw(x) + punpcklbw mm3, mm0 // Unpack High bytes of c + movq [edi + ebx], mm7 // write back updated value + movq mm1, mm7 // Now mm1 will be used as Raw(x-bpp) + // Now do Paeth for 2nd set of bytes (3-5) + psrlq mm2, ShiftBpp // load b=Prior(x) step 2 + punpcklbw mm1, mm0 // Unpack High bytes of a + pxor mm7, mm7 + punpcklbw mm2, mm0 // Unpack High bytes of b + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + psubw mm5, mm3 + psubw mm4, mm3 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = + // pav + pbv = pbv + pav + movq mm6, mm5 + paddw mm6, mm4 + + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm5 // Create mask pbv bytes < 0 + pcmpgtw mm7, mm4 // Create mask pav bytes < 0 + pand mm0, mm5 // Only pbv bytes < 0 in mm0 + pand mm7, mm4 // Only pav bytes < 0 in mm7 + psubw mm5, mm0 + psubw mm4, mm7 + psubw mm5, mm0 + psubw mm4, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + movq mm2, [esi + ebx] // load b=Prior(x) + pand mm3, mm7 + pandn mm7, mm0 + pxor mm1, mm1 + paddw mm7, mm3 + pxor mm0, mm0 + packuswb mm7, mm1 + movq mm3, mm2 // load c=Prior(x-bpp) step 1 + pand mm7, ActiveMask + punpckhbw mm2, mm0 // Unpack High bytes of b + psllq mm7, ShiftBpp // Shift bytes to 2nd group of 3 bytes + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + paddb mm7, [edi + ebx] // add Paeth predictor with Raw(x) + psllq mm3, ShiftBpp // load c=Prior(x-bpp) step 2 + movq [edi + ebx], mm7 // write back updated value + movq mm1, mm7 + punpckhbw mm3, mm0 // Unpack High bytes of c + psllq mm1, ShiftBpp // Shift bytes + // Now mm1 will be used as Raw(x-bpp) + // Now do Paeth for 3rd, and final, set of bytes (6-7) + pxor mm7, mm7 + punpckhbw mm1, mm0 // Unpack High bytes of a + psubw mm4, mm3 + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + pxor mm0, mm0 + paddw mm6, mm5 + + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + pandn mm0, mm1 + pandn mm7, mm4 + paddw mm0, mm2 + paddw mm7, mm5 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pand mm3, mm7 + pandn mm7, mm0 + paddw mm7, mm3 + pxor mm1, mm1 + packuswb mm1, mm7 + // Step ebx to next set of 8 bytes and repeat loop til done + add ebx, 8 + pand mm1, ActiveMaskEnd + paddb mm1, [edi + ebx - 8] // add Paeth predictor with Raw(x) + + cmp ebx, MMXLength + pxor mm0, mm0 // pxor does not affect flags + movq [edi + ebx - 8], mm1 // write back updated value + // mm1 will be used as Raw(x-bpp) next loop + // mm3 ready to be used as Prior(x-bpp) next loop + jb dpth3lp + } // end _asm block + } + break; + + case 6: + case 7: + case 5: + { + ActiveMask.use = 0x00000000ffffffff; + ActiveMask2.use = 0xffffffff00000000; + ShiftBpp.use = bpp << 3; // == bpp * 8 + ShiftRem.use = 64 - ShiftBpp.use; + _asm + { + mov ebx, diff + mov edi, row + mov esi, prev_row + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] + pxor mm0, mm0 +dpth6lp: + // Must shift to position Raw(x-bpp) data + psrlq mm1, ShiftRem + // Do first set of 4 bytes + movq mm3, [esi+ebx-8] // read c=Prior(x-bpp) bytes + punpcklbw mm1, mm0 // Unpack Low bytes of a + movq mm2, [esi + ebx] // load b=Prior(x) + punpcklbw mm2, mm0 // Unpack Low bytes of b + // Must shift to position Prior(x-bpp) data + psrlq mm3, ShiftRem + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + punpcklbw mm3, mm0 // Unpack Low bytes of c + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + paddw mm7, mm3 + pxor mm0, mm0 + packuswb mm7, mm1 + movq mm3, [esi + ebx - 8] // load c=Prior(x-bpp) + pand mm7, ActiveMask + psrlq mm3, ShiftRem + movq mm2, [esi + ebx] // load b=Prior(x) step 1 + paddb mm7, [edi + ebx] // add Paeth predictor with Raw(x) + movq mm6, mm2 + movq [edi + ebx], mm7 // write back updated value + movq mm1, [edi+ebx-8] + psllq mm6, ShiftBpp + movq mm5, mm7 + psrlq mm1, ShiftRem + por mm3, mm6 + psllq mm5, ShiftBpp + punpckhbw mm3, mm0 // Unpack High bytes of c + por mm1, mm5 + // Do second set of 4 bytes + punpckhbw mm2, mm0 // Unpack High bytes of b + punpckhbw mm1, mm0 // Unpack High bytes of a + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + pxor mm1, mm1 + paddw mm7, mm3 + pxor mm0, mm0 + // Step ex to next set of 8 bytes and repeat loop til done + add ebx, 8 + packuswb mm1, mm7 + paddb mm1, [edi + ebx - 8] // add Paeth predictor with Raw(x) + cmp ebx, MMXLength + movq [edi + ebx - 8], mm1 // write back updated value + // mm1 will be used as Raw(x-bpp) next loop + jb dpth6lp + } // end _asm block + } + break; + + case 4: + { + ActiveMask.use = 0x00000000ffffffff; + _asm { + mov ebx, diff + mov edi, row + mov esi, prev_row + pxor mm0, mm0 + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] // Only time should need to read + // a=Raw(x-bpp) bytes +dpth4lp: + // Do first set of 4 bytes + movq mm3, [esi+ebx-8] // read c=Prior(x-bpp) bytes + punpckhbw mm1, mm0 // Unpack Low bytes of a + movq mm2, [esi + ebx] // load b=Prior(x) + punpcklbw mm2, mm0 // Unpack High bytes of b + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + punpckhbw mm3, mm0 // Unpack High bytes of c + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + paddw mm7, mm3 + pxor mm0, mm0 + packuswb mm7, mm1 + movq mm3, [esi + ebx] // load c=Prior(x-bpp) + pand mm7, ActiveMask + movq mm2, mm3 // load b=Prior(x) step 1 + paddb mm7, [edi + ebx] // add Paeth predictor with Raw(x) + punpcklbw mm3, mm0 // Unpack High bytes of c + movq [edi + ebx], mm7 // write back updated value + movq mm1, mm7 // Now mm1 will be used as Raw(x-bpp) + // Do second set of 4 bytes + punpckhbw mm2, mm0 // Unpack Low bytes of b + punpcklbw mm1, mm0 // Unpack Low bytes of a + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + pxor mm1, mm1 + paddw mm7, mm3 + pxor mm0, mm0 + // Step ex to next set of 8 bytes and repeat loop til done + add ebx, 8 + packuswb mm1, mm7 + paddb mm1, [edi + ebx - 8] // add Paeth predictor with Raw(x) + cmp ebx, MMXLength + movq [edi + ebx - 8], mm1 // write back updated value + // mm1 will be used as Raw(x-bpp) next loop + jb dpth4lp + } // end _asm block + } + break; + case 8: // bpp == 8 + { + ActiveMask.use = 0x00000000ffffffff; + _asm { + mov ebx, diff + mov edi, row + mov esi, prev_row + pxor mm0, mm0 + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] // Only time should need to read + // a=Raw(x-bpp) bytes +dpth8lp: + // Do first set of 4 bytes + movq mm3, [esi+ebx-8] // read c=Prior(x-bpp) bytes + punpcklbw mm1, mm0 // Unpack Low bytes of a + movq mm2, [esi + ebx] // load b=Prior(x) + punpcklbw mm2, mm0 // Unpack Low bytes of b + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + punpcklbw mm3, mm0 // Unpack Low bytes of c + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + paddw mm7, mm3 + pxor mm0, mm0 + packuswb mm7, mm1 + movq mm3, [esi+ebx-8] // read c=Prior(x-bpp) bytes + pand mm7, ActiveMask + movq mm2, [esi + ebx] // load b=Prior(x) + paddb mm7, [edi + ebx] // add Paeth predictor with Raw(x) + punpckhbw mm3, mm0 // Unpack High bytes of c + movq [edi + ebx], mm7 // write back updated value + movq mm1, [edi+ebx-8] // read a=Raw(x-bpp) bytes + + // Do second set of 4 bytes + punpckhbw mm2, mm0 // Unpack High bytes of b + punpckhbw mm1, mm0 // Unpack High bytes of a + // pav = p - a = (a + b - c) - a = b - c + movq mm4, mm2 + // pbv = p - b = (a + b - c) - b = a - c + movq mm5, mm1 + psubw mm4, mm3 + pxor mm7, mm7 + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + movq mm6, mm4 + psubw mm5, mm3 + // pa = abs(p-a) = abs(pav) + // pb = abs(p-b) = abs(pbv) + // pc = abs(p-c) = abs(pcv) + pcmpgtw mm0, mm4 // Create mask pav bytes < 0 + paddw mm6, mm5 + pand mm0, mm4 // Only pav bytes < 0 in mm7 + pcmpgtw mm7, mm5 // Create mask pbv bytes < 0 + psubw mm4, mm0 + pand mm7, mm5 // Only pbv bytes < 0 in mm0 + psubw mm4, mm0 + psubw mm5, mm7 + pxor mm0, mm0 + pcmpgtw mm0, mm6 // Create mask pcv bytes < 0 + pand mm0, mm6 // Only pav bytes < 0 in mm7 + psubw mm5, mm7 + psubw mm6, mm0 + // test pa <= pb + movq mm7, mm4 + psubw mm6, mm0 + pcmpgtw mm7, mm5 // pa > pb? + movq mm0, mm7 + // use mm7 mask to merge pa & pb + pand mm5, mm7 + // use mm0 mask copy to merge a & b + pand mm2, mm0 + pandn mm7, mm4 + pandn mm0, mm1 + paddw mm7, mm5 + paddw mm0, mm2 + // test ((pa <= pb)? pa:pb) <= pc + pcmpgtw mm7, mm6 // pab > pc? + pxor mm1, mm1 + pand mm3, mm7 + pandn mm7, mm0 + pxor mm1, mm1 + paddw mm7, mm3 + pxor mm0, mm0 + // Step ex to next set of 8 bytes and repeat loop til done + add ebx, 8 + packuswb mm1, mm7 + paddb mm1, [edi + ebx - 8] // add Paeth predictor with Raw(x) + cmp ebx, MMXLength + movq [edi + ebx - 8], mm1 // write back updated value + // mm1 will be used as Raw(x-bpp) next loop + jb dpth8lp + } // end _asm block + } + break; + + case 1: // bpp = 1 + case 2: // bpp = 2 + default: // bpp > 8 + { + _asm { + mov ebx, diff + cmp ebx, FullLength + jnb dpthdend + mov edi, row + mov esi, prev_row + // Do Paeth decode for remaining bytes + mov edx, ebx + xor ecx, ecx // zero ecx before using cl & cx in loop below + sub edx, bpp // Set edx = ebx - bpp +dpthdlp: + xor eax, eax + // pav = p - a = (a + b - c) - a = b - c + mov al, [esi + ebx] // load Prior(x) into al + mov cl, [esi + edx] // load Prior(x-bpp) into cl + sub eax, ecx // subtract Prior(x-bpp) + mov patemp, eax // Save pav for later use + xor eax, eax + // pbv = p - b = (a + b - c) - b = a - c + mov al, [edi + edx] // load Raw(x-bpp) into al + sub eax, ecx // subtract Prior(x-bpp) + mov ecx, eax + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + add eax, patemp // pcv = pav + pbv + // pc = abs(pcv) + test eax, 0x80000000 + jz dpthdpca + neg eax // reverse sign of neg values +dpthdpca: + mov pctemp, eax // save pc for later use + // pb = abs(pbv) + test ecx, 0x80000000 + jz dpthdpba + neg ecx // reverse sign of neg values +dpthdpba: + mov pbtemp, ecx // save pb for later use + // pa = abs(pav) + mov eax, patemp + test eax, 0x80000000 + jz dpthdpaa + neg eax // reverse sign of neg values +dpthdpaa: + mov patemp, eax // save pa for later use + // test if pa <= pb + cmp eax, ecx + jna dpthdabb + // pa > pb; now test if pb <= pc + cmp ecx, pctemp + jna dpthdbbc + // pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthdpaeth +dpthdbbc: + // pb <= pc; Raw(x) = Paeth(x) + Prior(x) + mov cl, [esi + ebx] // load Prior(x) into cl + jmp dpthdpaeth +dpthdabb: + // pa <= pb; now test if pa <= pc + cmp eax, pctemp + jna dpthdabc + // pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthdpaeth +dpthdabc: + // pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp) + mov cl, [edi + edx] // load Raw(x-bpp) into cl +dpthdpaeth: + inc ebx + inc edx + // Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256 + add [edi + ebx - 1], cl + cmp ebx, FullLength + jb dpthdlp +dpthdend: + } // end _asm block + } + return; // No need to go further with this one + } // end switch ( bpp ) + _asm + { + // MMX acceleration complete now do clean-up + // Check if any remaining bytes left to decode + mov ebx, MMXLength + cmp ebx, FullLength + jnb dpthend + mov edi, row + mov esi, prev_row + // Do Paeth decode for remaining bytes + mov edx, ebx + xor ecx, ecx // zero ecx before using cl & cx in loop below + sub edx, bpp // Set edx = ebx - bpp +dpthlp2: + xor eax, eax + // pav = p - a = (a + b - c) - a = b - c + mov al, [esi + ebx] // load Prior(x) into al + mov cl, [esi + edx] // load Prior(x-bpp) into cl + sub eax, ecx // subtract Prior(x-bpp) + mov patemp, eax // Save pav for later use + xor eax, eax + // pbv = p - b = (a + b - c) - b = a - c + mov al, [edi + edx] // load Raw(x-bpp) into al + sub eax, ecx // subtract Prior(x-bpp) + mov ecx, eax + // pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv + add eax, patemp // pcv = pav + pbv + // pc = abs(pcv) + test eax, 0x80000000 + jz dpthpca2 + neg eax // reverse sign of neg values +dpthpca2: + mov pctemp, eax // save pc for later use + // pb = abs(pbv) + test ecx, 0x80000000 + jz dpthpba2 + neg ecx // reverse sign of neg values +dpthpba2: + mov pbtemp, ecx // save pb for later use + // pa = abs(pav) + mov eax, patemp + test eax, 0x80000000 + jz dpthpaa2 + neg eax // reverse sign of neg values +dpthpaa2: + mov patemp, eax // save pa for later use + // test if pa <= pb + cmp eax, ecx + jna dpthabb2 + // pa > pb; now test if pb <= pc + cmp ecx, pctemp + jna dpthbbc2 + // pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthpaeth2 +dpthbbc2: + // pb <= pc; Raw(x) = Paeth(x) + Prior(x) + mov cl, [esi + ebx] // load Prior(x) into cl + jmp dpthpaeth2 +dpthabb2: + // pa <= pb; now test if pa <= pc + cmp eax, pctemp + jna dpthabc2 + // pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp) + mov cl, [esi + edx] // load Prior(x-bpp) into cl + jmp dpthpaeth2 +dpthabc2: + // pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp) + mov cl, [edi + edx] // load Raw(x-bpp) into cl +dpthpaeth2: + inc ebx + inc edx + // Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256 + add [edi + ebx - 1], cl + cmp ebx, FullLength + jb dpthlp2 +dpthend: + emms // End MMX instructions; prep for possible FP instrs. + } // end _asm block +} + +// Optimized code for PNG Sub filter decoder +void /* PRIVATE */ +png_read_filter_row_mmx_sub(png_row_infop row_info, png_bytep row) +{ + //int test; + int bpp; + png_uint_32 FullLength; + png_uint_32 MMXLength; + int diff; + + bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel + FullLength = row_info->rowbytes - bpp; // # of bytes to filter + _asm { + mov edi, row + mov esi, edi // lp = row + add edi, bpp // rp = row + bpp + xor eax, eax + // get # of bytes to alignment + mov diff, edi // take start of row + add diff, 0xf // add 7 + 8 to incr past + // alignment boundary + xor ebx, ebx + and diff, 0xfffffff8 // mask to alignment boundary + sub diff, edi // subtract from start ==> value + // ebx at alignment + jz dsubgo + // fix alignment +dsublp1: + mov al, [esi+ebx] + add [edi+ebx], al + inc ebx + cmp ebx, diff + jb dsublp1 +dsubgo: + mov ecx, FullLength + mov edx, ecx + sub edx, ebx // subtract alignment fix + and edx, 0x00000007 // calc bytes over mult of 8 + sub ecx, edx // drop over bytes from length + mov MMXLength, ecx + } // end _asm block + + // Now do the math for the rest of the row + switch ( bpp ) + { + case 3: + { + ActiveMask.use = 0x0000ffffff000000; + ShiftBpp.use = 24; // == 3 * 8 + ShiftRem.use = 40; // == 64 - 24 + _asm { + mov edi, row + movq mm7, ActiveMask // Load ActiveMask for 2nd active byte group + mov esi, edi // lp = row + add edi, bpp // rp = row + bpp + movq mm6, mm7 + mov ebx, diff + psllq mm6, ShiftBpp // Move mask in mm6 to cover 3rd active + // byte group + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] +dsub3lp: + psrlq mm1, ShiftRem // Shift data for adding 1st bpp bytes + // no need for mask; shift clears inactive bytes + // Add 1st active group + movq mm0, [edi+ebx] + paddb mm0, mm1 + // Add 2nd active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + pand mm1, mm7 // mask to use only 2nd active group + paddb mm0, mm1 + // Add 3rd active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + pand mm1, mm6 // mask to use only 3rd active group + add ebx, 8 + paddb mm0, mm1 + cmp ebx, MMXLength + movq [edi+ebx-8], mm0 // Write updated Raws back to array + // Prep for doing 1st add at top of loop + movq mm1, mm0 + jb dsub3lp + } // end _asm block + } + break; + + case 1: + { + // Placed here just in case this is a duplicate of the + // non-MMX code for the SUB filter in png_read_filter_row below + // + // png_bytep rp; + // png_bytep lp; + // png_uint_32 i; + // bpp = (row_info->pixel_depth + 7) >> 3; + // for (i = (png_uint_32)bpp, rp = row + bpp, lp = row; + // i < row_info->rowbytes; i++, rp++, lp++) + // { + // *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff); + // } + _asm { + mov ebx, diff + mov edi, row + cmp ebx, FullLength + jnb dsub1end + mov esi, edi // lp = row + xor eax, eax + add edi, bpp // rp = row + bpp +dsub1lp: + mov al, [esi+ebx] + add [edi+ebx], al + inc ebx + cmp ebx, FullLength + jb dsub1lp +dsub1end: + } // end _asm block + } + return; + + case 6: + case 7: + case 4: + case 5: + { + ShiftBpp.use = bpp << 3; + ShiftRem.use = 64 - ShiftBpp.use; + _asm { + mov edi, row + mov ebx, diff + mov esi, edi // lp = row + add edi, bpp // rp = row + bpp + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] +dsub4lp: + psrlq mm1, ShiftRem // Shift data for adding 1st bpp bytes + // no need for mask; shift clears inactive bytes + movq mm0, [edi+ebx] + paddb mm0, mm1 + // Add 2nd active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + // there is no need for any mask + // since shift clears inactive bits/bytes + add ebx, 8 + paddb mm0, mm1 + cmp ebx, MMXLength + movq [edi+ebx-8], mm0 + movq mm1, mm0 // Prep for doing 1st add at top of loop + jb dsub4lp + } // end _asm block + } + break; + + case 2: + { + ActiveMask.use = 0x00000000ffff0000; + ShiftBpp.use = 16; // == 2 * 8 + ShiftRem.use = 48; // == 64 - 16 + _asm { + movq mm7, ActiveMask // Load ActiveMask for 2nd active byte group + mov ebx, diff + movq mm6, mm7 + mov edi, row + psllq mm6, ShiftBpp // Move mask in mm6 to cover 3rd active + // byte group + mov esi, edi // lp = row + movq mm5, mm6 + add edi, bpp // rp = row + bpp + psllq mm5, ShiftBpp // Move mask in mm5 to cover 4th active + // byte group + // PRIME the pump (load the first Raw(x-bpp) data set + movq mm1, [edi+ebx-8] +dsub2lp: + // Add 1st active group + psrlq mm1, ShiftRem // Shift data for adding 1st bpp bytes + // no need for mask; shift clears inactive + // bytes + movq mm0, [edi+ebx] + paddb mm0, mm1 + // Add 2nd active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + pand mm1, mm7 // mask to use only 2nd active group + paddb mm0, mm1 + // Add 3rd active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + pand mm1, mm6 // mask to use only 3rd active group + paddb mm0, mm1 + // Add 4th active group + movq mm1, mm0 // mov updated Raws to mm1 + psllq mm1, ShiftBpp // shift data to position correctly + pand mm1, mm5 // mask to use only 4th active group + add ebx, 8 + paddb mm0, mm1 + cmp ebx, MMXLength + movq [edi+ebx-8], mm0 // Write updated Raws back to array + movq mm1, mm0 // Prep for doing 1st add at top of loop + jb dsub2lp + } // end _asm block + } + break; + case 8: + { + _asm { + mov edi, row + mov ebx, diff + mov esi, edi // lp = row + add edi, bpp // rp = row + bpp + mov ecx, MMXLength + movq mm7, [edi+ebx-8] // PRIME the pump (load the first + // Raw(x-bpp) data set + and ecx, 0x0000003f // calc bytes over mult of 64 +dsub8lp: + movq mm0, [edi+ebx] // Load Sub(x) for 1st 8 bytes + paddb mm0, mm7 + movq mm1, [edi+ebx+8] // Load Sub(x) for 2nd 8 bytes + movq [edi+ebx], mm0 // Write Raw(x) for 1st 8 bytes + // Now mm0 will be used as Raw(x-bpp) for + // the 2nd group of 8 bytes. This will be + // repeated for each group of 8 bytes with + // the 8th group being used as the Raw(x-bpp) + // for the 1st group of the next loop. + paddb mm1, mm0 + movq mm2, [edi+ebx+16] // Load Sub(x) for 3rd 8 bytes + movq [edi+ebx+8], mm1 // Write Raw(x) for 2nd 8 bytes + paddb mm2, mm1 + movq mm3, [edi+ebx+24] // Load Sub(x) for 4th 8 bytes + movq [edi+ebx+16], mm2 // Write Raw(x) for 3rd 8 bytes + paddb mm3, mm2 + movq mm4, [edi+ebx+32] // Load Sub(x) for 5th 8 bytes + movq [edi+ebx+24], mm3 // Write Raw(x) for 4th 8 bytes + paddb mm4, mm3 + movq mm5, [edi+ebx+40] // Load Sub(x) for 6th 8 bytes + movq [edi+ebx+32], mm4 // Write Raw(x) for 5th 8 bytes + paddb mm5, mm4 + movq mm6, [edi+ebx+48] // Load Sub(x) for 7th 8 bytes + movq [edi+ebx+40], mm5 // Write Raw(x) for 6th 8 bytes + paddb mm6, mm5 + movq mm7, [edi+ebx+56] // Load Sub(x) for 8th 8 bytes + movq [edi+ebx+48], mm6 // Write Raw(x) for 7th 8 bytes + add ebx, 64 + paddb mm7, mm6 + cmp ebx, ecx + movq [edi+ebx-8], mm7 // Write Raw(x) for 8th 8 bytes + jb dsub8lp + cmp ebx, MMXLength + jnb dsub8lt8 +dsub8lpA: + movq mm0, [edi+ebx] + add ebx, 8 + paddb mm0, mm7 + cmp ebx, MMXLength + movq [edi+ebx-8], mm0 // use -8 to offset early add to ebx + movq mm7, mm0 // Move calculated Raw(x) data to mm1 to + // be the new Raw(x-bpp) for the next loop + jb dsub8lpA +dsub8lt8: + } // end _asm block + } + break; + + default: // bpp greater than 8 bytes + { + _asm { + mov ebx, diff + mov edi, row + mov esi, edi // lp = row + add edi, bpp // rp = row + bpp +dsubAlp: + movq mm0, [edi+ebx] + movq mm1, [esi+ebx] + add ebx, 8 + paddb mm0, mm1 + cmp ebx, MMXLength + movq [edi+ebx-8], mm0 // mov does not affect flags; -8 to offset + // add ebx + jb dsubAlp + } // end _asm block + } + break; + + } // end switch ( bpp ) + + _asm { + mov ebx, MMXLength + mov edi, row + cmp ebx, FullLength + jnb dsubend + mov esi, edi // lp = row + xor eax, eax + add edi, bpp // rp = row + bpp +dsublp2: + mov al, [esi+ebx] + add [edi+ebx], al + inc ebx + cmp ebx, FullLength + jb dsublp2 +dsubend: + emms // End MMX instructions; prep for possible FP instrs. + } // end _asm block +} + +// Optimized code for PNG Up filter decoder +void /* PRIVATE */ +png_read_filter_row_mmx_up(png_row_infop row_info, png_bytep row, + png_bytep prev_row) +{ + png_uint_32 len; + len = row_info->rowbytes; // # of bytes to filter + _asm { + mov edi, row + // get # of bytes to alignment + mov ecx, edi + xor ebx, ebx + add ecx, 0x7 + xor eax, eax + and ecx, 0xfffffff8 + mov esi, prev_row + sub ecx, edi + jz dupgo + // fix alignment +duplp1: + mov al, [edi+ebx] + add al, [esi+ebx] + inc ebx + cmp ebx, ecx + mov [edi + ebx-1], al // mov does not affect flags; -1 to offset inc ebx + jb duplp1 +dupgo: + mov ecx, len + mov edx, ecx + sub edx, ebx // subtract alignment fix + and edx, 0x0000003f // calc bytes over mult of 64 + sub ecx, edx // drop over bytes from length + // Unrolled loop - use all MMX registers and interleave to reduce + // number of branch instructions (loops) and reduce partial stalls +duploop: + movq mm1, [esi+ebx] + movq mm0, [edi+ebx] + movq mm3, [esi+ebx+8] + paddb mm0, mm1 + movq mm2, [edi+ebx+8] + movq [edi+ebx], mm0 + paddb mm2, mm3 + movq mm5, [esi+ebx+16] + movq [edi+ebx+8], mm2 + movq mm4, [edi+ebx+16] + movq mm7, [esi+ebx+24] + paddb mm4, mm5 + movq mm6, [edi+ebx+24] + movq [edi+ebx+16], mm4 + paddb mm6, mm7 + movq mm1, [esi+ebx+32] + movq [edi+ebx+24], mm6 + movq mm0, [edi+ebx+32] + movq mm3, [esi+ebx+40] + paddb mm0, mm1 + movq mm2, [edi+ebx+40] + movq [edi+ebx+32], mm0 + paddb mm2, mm3 + movq mm5, [esi+ebx+48] + movq [edi+ebx+40], mm2 + movq mm4, [edi+ebx+48] + movq mm7, [esi+ebx+56] + paddb mm4, mm5 + movq mm6, [edi+ebx+56] + movq [edi+ebx+48], mm4 + add ebx, 64 + paddb mm6, mm7 + cmp ebx, ecx + movq [edi+ebx-8], mm6 // (+56)movq does not affect flags; + // -8 to offset add ebx + jb duploop + + cmp edx, 0 // Test for bytes over mult of 64 + jz dupend + + + // 2 lines added by lcreeve at netins.net + // (mail 11 Jul 98 in png-implement list) + cmp edx, 8 //test for less than 8 bytes + jb duplt8 + + + add ecx, edx + and edx, 0x00000007 // calc bytes over mult of 8 + sub ecx, edx // drop over bytes from length + jz duplt8 + // Loop using MMX registers mm0 & mm1 to update 8 bytes simultaneously +duplpA: + movq mm1, [esi+ebx] + movq mm0, [edi+ebx] + add ebx, 8 + paddb mm0, mm1 + cmp ebx, ecx + movq [edi+ebx-8], mm0 // movq does not affect flags; -8 to offset add ebx + jb duplpA + cmp edx, 0 // Test for bytes over mult of 8 + jz dupend +duplt8: + xor eax, eax + add ecx, edx // move over byte count into counter + // Loop using x86 registers to update remaining bytes +duplp2: + mov al, [edi + ebx] + add al, [esi + ebx] + inc ebx + cmp ebx, ecx + mov [edi + ebx-1], al // mov does not affect flags; -1 to offset inc ebx + jb duplp2 +dupend: + // Conversion of filtered row completed + emms // End MMX instructions; prep for possible FP instrs. + } // end _asm block +} + + +// Optimized png_read_filter_row routines +void /* PRIVATE */ +png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep + row, png_bytep prev_row, int filter) +{ +#ifdef PNG_DEBUG + char filnm[10]; +#endif + + if (mmx_supported == 2) { +#if !defined(PNG_1_0_X) + /* this should have happened in png_init_mmx_flags() already */ + png_warning(png_ptr, "asm_flags may not have been initialized"); +#endif + png_mmx_support(); + } + +#ifdef PNG_DEBUG + png_debug(1, "in png_read_filter_row\n"); + switch (filter) + { + case 0: sprintf(filnm, "none"); + break; +#if !defined(PNG_1_0_X) + case 1: sprintf(filnm, "sub-%s", + (png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_SUB)? "MMX" : "x86"); + break; + case 2: sprintf(filnm, "up-%s", + (png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_UP)? "MMX" : "x86"); + break; + case 3: sprintf(filnm, "avg-%s", + (png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_AVG)? "MMX" : "x86"); + break; + case 4: sprintf(filnm, "Paeth-%s", + (png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH)? "MMX":"x86"); + break; +#else + case 1: sprintf(filnm, "sub"); + break; + case 2: sprintf(filnm, "up"); + break; + case 3: sprintf(filnm, "avg"); + break; + case 4: sprintf(filnm, "Paeth"); + break; +#endif + default: sprintf(filnm, "unknw"); + break; + } + png_debug2(0,"row=%5d, %s, ", png_ptr->row_number, filnm); + png_debug2(0, "pd=%2d, b=%d, ", (int)row_info->pixel_depth, + (int)((row_info->pixel_depth + 7) >> 3)); + png_debug1(0,"len=%8d, ", row_info->rowbytes); +#endif /* PNG_DEBUG */ + + switch (filter) + { + case PNG_FILTER_VALUE_NONE: + break; + + case PNG_FILTER_VALUE_SUB: + { +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_SUB) && + (row_info->pixel_depth >= png_ptr->mmx_bitdepth_threshold) && + (row_info->rowbytes >= png_ptr->mmx_rowbytes_threshold)) +#else + if (mmx_supported) +#endif + { + png_read_filter_row_mmx_sub(row_info, row); + } + else + { + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_bytep rp = row + bpp; + png_bytep lp = row; + + for (i = bpp; i < istop; i++) + { + *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); + rp++; + } + } + break; + } + + case PNG_FILTER_VALUE_UP: + { +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_UP) && + (row_info->pixel_depth >= png_ptr->mmx_bitdepth_threshold) && + (row_info->rowbytes >= png_ptr->mmx_rowbytes_threshold)) +#else + if (mmx_supported) +#endif + { + png_read_filter_row_mmx_up(row_info, row, prev_row); + } + else + { + png_uint_32 i; + png_uint_32 istop = row_info->rowbytes; + png_bytep rp = row; + png_bytep pp = prev_row; + + for (i = 0; i < istop; ++i) + { + *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); + rp++; + } + } + break; + } + + case PNG_FILTER_VALUE_AVG: + { +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_AVG) && + (row_info->pixel_depth >= png_ptr->mmx_bitdepth_threshold) && + (row_info->rowbytes >= png_ptr->mmx_rowbytes_threshold)) +#else + if (mmx_supported) +#endif + { + png_read_filter_row_mmx_avg(row_info, row, prev_row); + } + else + { + png_uint_32 i; + png_bytep rp = row; + png_bytep pp = prev_row; + png_bytep lp = row; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_uint_32 istop = row_info->rowbytes - bpp; + + for (i = 0; i < bpp; i++) + { + *rp = (png_byte)(((int)(*rp) + + ((int)(*pp++) >> 1)) & 0xff); + rp++; + } + + for (i = 0; i < istop; i++) + { + *rp = (png_byte)(((int)(*rp) + + ((int)(*pp++ + *lp++) >> 1)) & 0xff); + rp++; + } + } + break; + } + + case PNG_FILTER_VALUE_PAETH: + { +#if !defined(PNG_1_0_X) + if ((png_ptr->asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH) && + (row_info->pixel_depth >= png_ptr->mmx_bitdepth_threshold) && + (row_info->rowbytes >= png_ptr->mmx_rowbytes_threshold)) +#else + if (mmx_supported) +#endif + { + png_read_filter_row_mmx_paeth(row_info, row, prev_row); + } + else + { + png_uint_32 i; + png_bytep rp = row; + png_bytep pp = prev_row; + png_bytep lp = row; + png_bytep cp = prev_row; + png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; + png_uint_32 istop=row_info->rowbytes - bpp; + + for (i = 0; i < bpp; i++) + { + *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); + rp++; + } + + for (i = 0; i < istop; i++) // use leftover rp,pp + { + int a, b, c, pa, pb, pc, p; + + a = *lp++; + b = *pp++; + c = *cp++; + + p = b - c; + pc = a - c; + +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif + + /* + if (pa <= pb && pa <= pc) + p = a; + else if (pb <= pc) + p = b; + else + p = c; + */ + + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; + + *rp = (png_byte)(((int)(*rp) + p) & 0xff); + rp++; + } + } + break; + } + + default: + png_warning(png_ptr, "Ignoring bad row filter type"); + *row=0; + break; + } +} + +#endif /* PNG_ASSEMBLER_CODE_SUPPORTED && PNG_USE_PNGVCRD */ diff --git a/libpng/pngwio.c b/libpng/pngwio.c new file mode 100644 index 00000000..d5d61f49 --- /dev/null +++ b/libpng/pngwio.c @@ -0,0 +1,228 @@ + +/* pngwio.c - functions for data output + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + * + * This file provides a location for all output. Users who need + * special handling are expected to write functions that have the same + * arguments as these and perform similar functions, but that possibly + * use different output methods. Note that you shouldn't change these + * functions, but rather write replacement functions and then change + * them at run time with png_set_write_fn(...). + */ + +#define PNG_INTERNAL +#include "png.h" +#ifdef PNG_WRITE_SUPPORTED + +/* Write the data to whatever output you are using. The default routine + writes to a file pointer. Note that this routine sometimes gets called + with very small lengths, so you should implement some kind of simple + buffering if you are using unbuffered writes. This should never be asked + to write more than 64K on a 16 bit machine. */ + +void /* PRIVATE */ +png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + if (png_ptr->write_data_fn != NULL ) + (*(png_ptr->write_data_fn))(png_ptr, data, length); + else + png_error(png_ptr, "Call to NULL write function"); +} + +#if !defined(PNG_NO_STDIO) +/* This is the function that does the actual writing of data. If you are + not writing to a standard C stream, you should create a replacement + write_data function and use it at run time with png_set_write_fn(), rather + than changing the library. */ +#ifndef USE_FAR_KEYWORD +void PNGAPI +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + png_uint_32 check; + +#if defined(_WIN32_WCE) + if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) + check = 0; +#else + check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); +#endif + if (check != length) + png_error(png_ptr, "Write Error"); +} +#else +/* this is the model-independent version. Since the standard I/O library + can't handle far buffers in the medium and small models, we have to copy + the data. +*/ + +#define NEAR_BUF_SIZE 1024 +#define MIN(a,b) (a <= b ? a : b) + +void PNGAPI +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + png_uint_32 check; + png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ + png_FILE_p io_ptr; + + /* Check if data really is near. If so, use usual code. */ + near_data = (png_byte *)CVT_PTR_NOCHECK(data); + io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); + if ((png_bytep)near_data == data) + { +#if defined(_WIN32_WCE) + if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) + check = 0; +#else + check = fwrite(near_data, 1, length, io_ptr); +#endif + } + else + { + png_byte buf[NEAR_BUF_SIZE]; + png_size_t written, remaining, err; + check = 0; + remaining = length; + do + { + written = MIN(NEAR_BUF_SIZE, remaining); + png_memcpy(buf, data, written); /* copy far buffer to near buffer */ +#if defined(_WIN32_WCE) + if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) + err = 0; +#else + err = fwrite(buf, 1, written, io_ptr); +#endif + if (err != written) + break; + else + check += err; + data += written; + remaining -= written; + } + while (remaining != 0); + } + if (check != length) + png_error(png_ptr, "Write Error"); +} + +#endif +#endif + +/* This function is called to output any data pending writing (normally + to disk). After png_flush is called, there should be no data pending + writing in any buffers. */ +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +void /* PRIVATE */ +png_flush(png_structp png_ptr) +{ + if (png_ptr->output_flush_fn != NULL) + (*(png_ptr->output_flush_fn))(png_ptr); +} + +#if !defined(PNG_NO_STDIO) +void PNGAPI +png_default_flush(png_structp png_ptr) +{ +#if !defined(_WIN32_WCE) + png_FILE_p io_ptr; + io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); + if (io_ptr != NULL) + fflush(io_ptr); +#endif +} +#endif +#endif + +/* This function allows the application to supply new output functions for + libpng if standard C streams aren't being used. + + This function takes as its arguments: + png_ptr - pointer to a png output data structure + io_ptr - pointer to user supplied structure containing info about + the output functions. May be NULL. + write_data_fn - pointer to a new output function that takes as its + arguments a pointer to a png_struct, a pointer to + data to be written, and a 32-bit unsigned int that is + the number of bytes to be written. The new write + function should call png_error(png_ptr, "Error msg") + to exit and output any fatal error messages. + flush_data_fn - pointer to a new flush function that takes as its + arguments a pointer to a png_struct. After a call to + the flush function, there should be no data in any buffers + or pending transmission. If the output method doesn't do + any buffering of ouput, a function prototype must still be + supplied although it doesn't have to do anything. If + PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile + time, output_flush_fn will be ignored, although it must be + supplied for compatibility. */ +void PNGAPI +png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, + png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) +{ + png_ptr->io_ptr = io_ptr; + +#if !defined(PNG_NO_STDIO) + if (write_data_fn != NULL) + png_ptr->write_data_fn = write_data_fn; + else + png_ptr->write_data_fn = png_default_write_data; +#else + png_ptr->write_data_fn = write_data_fn; +#endif + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +#if !defined(PNG_NO_STDIO) + if (output_flush_fn != NULL) + png_ptr->output_flush_fn = output_flush_fn; + else + png_ptr->output_flush_fn = png_default_flush; +#else + png_ptr->output_flush_fn = output_flush_fn; +#endif +#endif /* PNG_WRITE_FLUSH_SUPPORTED */ + + /* It is an error to read while writing a png file */ + if (png_ptr->read_data_fn != NULL) + { + png_ptr->read_data_fn = NULL; + png_warning(png_ptr, + "Attempted to set both read_data_fn and write_data_fn in"); + png_warning(png_ptr, + "the same structure. Resetting read_data_fn to NULL."); + } +} + +#if defined(USE_FAR_KEYWORD) +#if defined(_MSC_VER) +void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check) +{ + void *near_ptr; + void FAR *far_ptr; + FP_OFF(near_ptr) = FP_OFF(ptr); + far_ptr = (void FAR *)near_ptr; + if(check != 0) + if(FP_SEG(ptr) != FP_SEG(far_ptr)) + png_error(png_ptr,"segment lost in conversion"); + return(near_ptr); +} +# else +void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check) +{ + void *near_ptr; + void FAR *far_ptr; + near_ptr = (void FAR *)ptr; + far_ptr = (void FAR *)near_ptr; + if(check != 0) + if(far_ptr != ptr) + png_error(png_ptr,"segment lost in conversion"); + return(near_ptr); +} +# endif +# endif +#endif /* PNG_WRITE_SUPPORTED */ diff --git a/libpng/pngwrite.c b/libpng/pngwrite.c new file mode 100644 index 00000000..3246fdaa --- /dev/null +++ b/libpng/pngwrite.c @@ -0,0 +1,1464 @@ + +/* pngwrite.c - general routines to write a PNG file + * + * libpng 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +/* get internal access to png.h */ +#define PNG_INTERNAL +#include "png.h" +#ifdef PNG_WRITE_SUPPORTED + +/* Writes all the PNG information. This is the suggested way to use the + * library. If you have a new chunk to add, make a function to write it, + * and put it in the correct location here. If you want the chunk written + * after the image data, put it in png_write_end(). I strongly encourage + * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing + * the chunk, as that will keep the code from breaking if you want to just + * write a plain PNG file. If you have long comments, I suggest writing + * them in png_write_end(), and compressing them. + */ +void PNGAPI +png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_write_info_before_PLTE\n"); + if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) + { + png_write_sig(png_ptr); /* write PNG signature */ +#if defined(PNG_MNG_FEATURES_SUPPORTED) + if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted)) + { + png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n"); + png_ptr->mng_features_permitted=0; + } +#endif + /* write IHDR information. */ + png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, + info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, + info_ptr->filter_type, +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) + info_ptr->interlace_type); +#else + 0); +#endif + /* the rest of these check to see if the valid field has the appropriate + flag set, and if it does, writes the chunk. */ +#if defined(PNG_WRITE_gAMA_SUPPORTED) + if (info_ptr->valid & PNG_INFO_gAMA) + { +# ifdef PNG_FLOATING_POINT_SUPPORTED + png_write_gAMA(png_ptr, info_ptr->gamma); +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma); +# endif +#endif + } +#endif +#if defined(PNG_WRITE_sRGB_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sRGB) + png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent); +#endif +#if defined(PNG_WRITE_iCCP_SUPPORTED) + if (info_ptr->valid & PNG_INFO_iCCP) + png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE, + info_ptr->iccp_profile, (int)info_ptr->iccp_proflen); +#endif +#if defined(PNG_WRITE_sBIT_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sBIT) + png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); +#endif +#if defined(PNG_WRITE_cHRM_SUPPORTED) + if (info_ptr->valid & PNG_INFO_cHRM) + { +#ifdef PNG_FLOATING_POINT_SUPPORTED + png_write_cHRM(png_ptr, + info_ptr->x_white, info_ptr->y_white, + info_ptr->x_red, info_ptr->y_red, + info_ptr->x_green, info_ptr->y_green, + info_ptr->x_blue, info_ptr->y_blue); +#else +# ifdef PNG_FIXED_POINT_SUPPORTED + png_write_cHRM_fixed(png_ptr, + info_ptr->int_x_white, info_ptr->int_y_white, + info_ptr->int_x_red, info_ptr->int_y_red, + info_ptr->int_x_green, info_ptr->int_y_green, + info_ptr->int_x_blue, info_ptr->int_y_blue); +# endif +#endif + } +#endif +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) + if (info_ptr->unknown_chunks_num) + { + png_unknown_chunk *up; + + png_debug(5, "writing extra chunks\n"); + + for (up = info_ptr->unknown_chunks; + up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; + up++) + { + int keep=png_handle_as_unknown(png_ptr, up->name); + if (keep != PNG_HANDLE_CHUNK_NEVER && + up->location && !(up->location & PNG_HAVE_PLTE) && + !(up->location & PNG_HAVE_IDAT) && + ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || + (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) + { + png_write_chunk(png_ptr, up->name, up->data, up->size); + } + } + } +#endif + png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; + } +} + +void PNGAPI +png_write_info(png_structp png_ptr, png_infop info_ptr) +{ +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) + int i; +#endif + + png_debug(1, "in png_write_info\n"); + + png_write_info_before_PLTE(png_ptr, info_ptr); + + if (info_ptr->valid & PNG_INFO_PLTE) + png_write_PLTE(png_ptr, info_ptr->palette, + (png_uint_32)info_ptr->num_palette); + else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + png_error(png_ptr, "Valid palette required for paletted images\n"); + +#if defined(PNG_WRITE_tRNS_SUPPORTED) + if (info_ptr->valid & PNG_INFO_tRNS) + { +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) + /* invert the alpha channel (in tRNS) */ + if ((png_ptr->transformations & PNG_INVERT_ALPHA) && + info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + int j; + for (j=0; j<(int)info_ptr->num_trans; j++) + info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]); + } +#endif + png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values), + info_ptr->num_trans, info_ptr->color_type); + } +#endif +#if defined(PNG_WRITE_bKGD_SUPPORTED) + if (info_ptr->valid & PNG_INFO_bKGD) + png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); +#endif +#if defined(PNG_WRITE_hIST_SUPPORTED) + if (info_ptr->valid & PNG_INFO_hIST) + png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); +#endif +#if defined(PNG_WRITE_oFFs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_oFFs) + png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, + info_ptr->offset_unit_type); +#endif +#if defined(PNG_WRITE_pCAL_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pCAL) + png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, + info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, + info_ptr->pcal_units, info_ptr->pcal_params); +#endif +#if defined(PNG_WRITE_sCAL_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sCAL) +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) + png_write_sCAL(png_ptr, (int)info_ptr->scal_unit, + info_ptr->scal_pixel_width, info_ptr->scal_pixel_height); +#else +#ifdef PNG_FIXED_POINT_SUPPORTED + png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, + info_ptr->scal_s_width, info_ptr->scal_s_height); +#else + png_warning(png_ptr, + "png_write_sCAL not supported; sCAL chunk not written.\n"); +#endif +#endif +#endif +#if defined(PNG_WRITE_pHYs_SUPPORTED) + if (info_ptr->valid & PNG_INFO_pHYs) + png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, + info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); +#endif +#if defined(PNG_WRITE_tIME_SUPPORTED) + if (info_ptr->valid & PNG_INFO_tIME) + { + png_write_tIME(png_ptr, &(info_ptr->mod_time)); + png_ptr->mode |= PNG_WROTE_tIME; + } +#endif +#if defined(PNG_WRITE_sPLT_SUPPORTED) + if (info_ptr->valid & PNG_INFO_sPLT) + for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) + png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); +#endif +#if defined(PNG_WRITE_TEXT_SUPPORTED) + /* Check to see if we need to write text chunks */ + for (i = 0; i < info_ptr->num_text; i++) + { + png_debug2(2, "Writing header text chunk %d, type %d\n", i, + info_ptr->text[i].compression); + /* an internationalized chunk? */ + if (info_ptr->text[i].compression > 0) + { +#if defined(PNG_WRITE_iTXt_SUPPORTED) + /* write international chunk */ + png_write_iTXt(png_ptr, + info_ptr->text[i].compression, + info_ptr->text[i].key, + info_ptr->text[i].lang, + info_ptr->text[i].lang_key, + info_ptr->text[i].text); +#else + png_warning(png_ptr, "Unable to write international text\n"); +#endif + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + } + /* If we want a compressed text chunk */ + else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) + { +#if defined(PNG_WRITE_zTXt_SUPPORTED) + /* write compressed chunk */ + png_write_zTXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, 0, + info_ptr->text[i].compression); +#else + png_warning(png_ptr, "Unable to write compressed text\n"); +#endif + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; + } + else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) + { +#if defined(PNG_WRITE_tEXt_SUPPORTED) + /* write uncompressed chunk */ + png_write_tEXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, + 0); +#else + png_warning(png_ptr, "Unable to write uncompressed text\n"); +#endif + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + } + } +#endif +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) + if (info_ptr->unknown_chunks_num) + { + png_unknown_chunk *up; + + png_debug(5, "writing extra chunks\n"); + + for (up = info_ptr->unknown_chunks; + up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; + up++) + { + int keep=png_handle_as_unknown(png_ptr, up->name); + if (keep != PNG_HANDLE_CHUNK_NEVER && + up->location && (up->location & PNG_HAVE_PLTE) && + !(up->location & PNG_HAVE_IDAT) && + ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || + (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) + { + png_write_chunk(png_ptr, up->name, up->data, up->size); + } + } + } +#endif +} + +/* Writes the end of the PNG file. If you don't want to write comments or + * time information, you can pass NULL for info. If you already wrote these + * in png_write_info(), do not write them again here. If you have long + * comments, I suggest writing them here, and compressing them. + */ +void PNGAPI +png_write_end(png_structp png_ptr, png_infop info_ptr) +{ + png_debug(1, "in png_write_end\n"); + if (!(png_ptr->mode & PNG_HAVE_IDAT)) + png_error(png_ptr, "No IDATs written into file"); + + /* see if user wants us to write information chunks */ + if (info_ptr != NULL) + { +#if defined(PNG_WRITE_TEXT_SUPPORTED) + int i; /* local index variable */ +#endif +#if defined(PNG_WRITE_tIME_SUPPORTED) + /* check to see if user has supplied a time chunk */ + if ((info_ptr->valid & PNG_INFO_tIME) && + !(png_ptr->mode & PNG_WROTE_tIME)) + png_write_tIME(png_ptr, &(info_ptr->mod_time)); +#endif +#if defined(PNG_WRITE_TEXT_SUPPORTED) + /* loop through comment chunks */ + for (i = 0; i < info_ptr->num_text; i++) + { + png_debug2(2, "Writing trailer text chunk %d, type %d\n", i, + info_ptr->text[i].compression); + /* an internationalized chunk? */ + if (info_ptr->text[i].compression > 0) + { +#if defined(PNG_WRITE_iTXt_SUPPORTED) + /* write international chunk */ + png_write_iTXt(png_ptr, + info_ptr->text[i].compression, + info_ptr->text[i].key, + info_ptr->text[i].lang, + info_ptr->text[i].lang_key, + info_ptr->text[i].text); +#else + png_warning(png_ptr, "Unable to write international text\n"); +#endif + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + } + else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) + { +#if defined(PNG_WRITE_zTXt_SUPPORTED) + /* write compressed chunk */ + png_write_zTXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, 0, + info_ptr->text[i].compression); +#else + png_warning(png_ptr, "Unable to write compressed text\n"); +#endif + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; + } + else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) + { +#if defined(PNG_WRITE_tEXt_SUPPORTED) + /* write uncompressed chunk */ + png_write_tEXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, 0); +#else + png_warning(png_ptr, "Unable to write uncompressed text\n"); +#endif + + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + } + } +#endif +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) + if (info_ptr->unknown_chunks_num) + { + png_unknown_chunk *up; + + png_debug(5, "writing extra chunks\n"); + + for (up = info_ptr->unknown_chunks; + up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; + up++) + { + int keep=png_handle_as_unknown(png_ptr, up->name); + if (keep != PNG_HANDLE_CHUNK_NEVER && + up->location && (up->location & PNG_AFTER_IDAT) && + ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || + (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) + { + png_write_chunk(png_ptr, up->name, up->data, up->size); + } + } + } +#endif + } + + png_ptr->mode |= PNG_AFTER_IDAT; + + /* write end of PNG file */ + png_write_IEND(png_ptr); +#if 0 +/* This flush, added in libpng-1.0.8, causes some applications to crash + because they do not set png_ptr->output_flush_fn */ + png_flush(png_ptr); +#endif +} + +#if defined(PNG_WRITE_tIME_SUPPORTED) +#if !defined(_WIN32_WCE) +/* "time.h" functions are not supported on WindowsCE */ +void PNGAPI +png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime) +{ + png_debug(1, "in png_convert_from_struct_tm\n"); + ptime->year = (png_uint_16)(1900 + ttime->tm_year); + ptime->month = (png_byte)(ttime->tm_mon + 1); + ptime->day = (png_byte)ttime->tm_mday; + ptime->hour = (png_byte)ttime->tm_hour; + ptime->minute = (png_byte)ttime->tm_min; + ptime->second = (png_byte)ttime->tm_sec; +} + +void PNGAPI +png_convert_from_time_t(png_timep ptime, time_t ttime) +{ + struct tm *tbuf; + + png_debug(1, "in png_convert_from_time_t\n"); + tbuf = gmtime(&ttime); + png_convert_from_struct_tm(ptime, tbuf); +} +#endif +#endif + +/* Initialize png_ptr structure, and allocate any memory needed */ +png_structp PNGAPI +png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn) +{ +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn, + warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); +} + +/* Alternate initialize png_ptr structure, and allocate any memory needed */ +png_structp PNGAPI +png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, + png_malloc_ptr malloc_fn, png_free_ptr free_fn) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + png_structp png_ptr; +#ifdef PNG_SETJMP_SUPPORTED +#ifdef USE_FAR_KEYWORD + jmp_buf jmpbuf; +#endif +#endif + int i; + png_debug(1, "in png_create_write_struct\n"); +#ifdef PNG_USER_MEM_SUPPORTED + png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, + (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); +#else + png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); +#endif /* PNG_USER_MEM_SUPPORTED */ + if (png_ptr == NULL) + return (NULL); + +#if !defined(PNG_1_0_X) +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED + png_init_mmx_flags(png_ptr); /* 1.2.0 addition */ +#endif +#endif /* PNG_1_0_X */ + + /* added at libpng-1.2.6 */ +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + png_ptr->user_width_max=PNG_USER_WIDTH_MAX; + png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; +#endif + +#ifdef PNG_SETJMP_SUPPORTED +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) +#else + if (setjmp(png_ptr->jmpbuf)) +#endif + { + png_free(png_ptr, png_ptr->zbuf); + png_ptr->zbuf=NULL; + png_destroy_struct(png_ptr); + return (NULL); + } +#ifdef USE_FAR_KEYWORD + png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf)); +#endif +#endif + +#ifdef PNG_USER_MEM_SUPPORTED + png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); +#endif /* PNG_USER_MEM_SUPPORTED */ + png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); + + i=0; + do + { + if(user_png_ver[i] != png_libpng_ver[i]) + png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; + } while (png_libpng_ver[i++]); + + if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) + { + /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so + * we must recompile any applications that use any older library version. + * For versions after libpng 1.0, we will be compatible, so we need + * only check the first digit. + */ + if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || + (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || + (user_png_ver[0] == '0' && user_png_ver[2] < '9')) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char msg[80]; + if (user_png_ver) + { + sprintf(msg, "Application was compiled with png.h from libpng-%.20s", + user_png_ver); + png_warning(png_ptr, msg); + } + sprintf(msg, "Application is running with png.c from libpng-%.20s", + png_libpng_ver); + png_warning(png_ptr, msg); +#endif +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "Incompatible libpng version in application and library"); + } + } + + /* initialize zbuf - compression buffer */ + png_ptr->zbuf_size = PNG_ZBUF_SIZE; + png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, + (png_uint_32)png_ptr->zbuf_size); + + png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL, + png_flush_ptr_NULL); + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, + 1, png_doublep_NULL, png_doublep_NULL); +#endif + +#ifdef PNG_SETJMP_SUPPORTED +/* Applications that neglect to set up their own setjmp() and then encounter + a png_error() will longjmp here. Since the jmpbuf is then meaningless we + abort instead of returning. */ +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) + PNG_ABORT(); + png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf)); +#else + if (setjmp(png_ptr->jmpbuf)) + PNG_ABORT(); +#endif +#endif + return (png_ptr); +} + +/* Initialize png_ptr structure, and allocate any memory needed */ +#undef png_write_init +void PNGAPI +png_write_init(png_structp png_ptr) +{ + /* We only come here via pre-1.0.7-compiled applications */ + png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0); +} + +void PNGAPI +png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver, + png_size_t png_struct_size, png_size_t png_info_size) +{ + /* We only come here via pre-1.0.12-compiled applications */ +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + if(png_sizeof(png_struct) > png_struct_size || + png_sizeof(png_info) > png_info_size) + { + char msg[80]; + png_ptr->warning_fn=NULL; + if (user_png_ver) + { + sprintf(msg, "Application was compiled with png.h from libpng-%.20s", + user_png_ver); + png_warning(png_ptr, msg); + } + sprintf(msg, "Application is running with png.c from libpng-%.20s", + png_libpng_ver); + png_warning(png_ptr, msg); + } +#endif + if(png_sizeof(png_struct) > png_struct_size) + { + png_ptr->error_fn=NULL; +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "The png struct allocated by the application for writing is too small."); + } + if(png_sizeof(png_info) > png_info_size) + { + png_ptr->error_fn=NULL; +#ifdef PNG_ERROR_NUMBERS_SUPPORTED + png_ptr->flags=0; +#endif + png_error(png_ptr, + "The info struct allocated by the application for writing is too small."); + } + png_write_init_3(&png_ptr, user_png_ver, png_struct_size); +} + + +void PNGAPI +png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, + png_size_t png_struct_size) +{ + png_structp png_ptr=*ptr_ptr; +#ifdef PNG_SETJMP_SUPPORTED + jmp_buf tmp_jmp; /* to save current jump buffer */ +#endif + int i = 0; + do + { + if (user_png_ver[i] != png_libpng_ver[i]) + { +#ifdef PNG_LEGACY_SUPPORTED + png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; +#else + png_ptr->warning_fn=NULL; + png_warning(png_ptr, + "Application uses deprecated png_write_init() and should be recompiled."); + break; +#endif + } + } while (png_libpng_ver[i++]); + + png_debug(1, "in png_write_init_3\n"); + +#ifdef PNG_SETJMP_SUPPORTED + /* save jump buffer and error functions */ + png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf)); +#endif + + if (png_sizeof(png_struct) > png_struct_size) + { + png_destroy_struct(png_ptr); + png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); + *ptr_ptr = png_ptr; + } + + /* reset all variables to 0 */ + png_memset(png_ptr, 0, png_sizeof (png_struct)); + + /* added at libpng-1.2.6 */ +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + png_ptr->user_width_max=PNG_USER_WIDTH_MAX; + png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; +#endif + +#if !defined(PNG_1_0_X) +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED + png_init_mmx_flags(png_ptr); /* 1.2.0 addition */ +#endif +#endif /* PNG_1_0_X */ + +#ifdef PNG_SETJMP_SUPPORTED + /* restore jump buffer */ + png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf)); +#endif + + png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL, + png_flush_ptr_NULL); + + /* initialize zbuf - compression buffer */ + png_ptr->zbuf_size = PNG_ZBUF_SIZE; + png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, + (png_uint_32)png_ptr->zbuf_size); + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, + 1, png_doublep_NULL, png_doublep_NULL); +#endif +} + +/* Write a few rows of image data. If the image is interlaced, + * either you will have to write the 7 sub images, or, if you + * have called png_set_interlace_handling(), you will have to + * "write" the image seven times. + */ +void PNGAPI +png_write_rows(png_structp png_ptr, png_bytepp row, + png_uint_32 num_rows) +{ + png_uint_32 i; /* row counter */ + png_bytepp rp; /* row pointer */ + + png_debug(1, "in png_write_rows\n"); + /* loop through the rows */ + for (i = 0, rp = row; i < num_rows; i++, rp++) + { + png_write_row(png_ptr, *rp); + } +} + +/* Write the image. You only need to call this function once, even + * if you are writing an interlaced image. + */ +void PNGAPI +png_write_image(png_structp png_ptr, png_bytepp image) +{ + png_uint_32 i; /* row index */ + int pass, num_pass; /* pass variables */ + png_bytepp rp; /* points to current row */ + + png_debug(1, "in png_write_image\n"); +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) + /* intialize interlace handling. If image is not interlaced, + this will set pass to 1 */ + num_pass = png_set_interlace_handling(png_ptr); +#else + num_pass = 1; +#endif + /* loop through passes */ + for (pass = 0; pass < num_pass; pass++) + { + /* loop through image */ + for (i = 0, rp = image; i < png_ptr->height; i++, rp++) + { + png_write_row(png_ptr, *rp); + } + } +} + +/* called by user to write a row of image data */ +void PNGAPI +png_write_row(png_structp png_ptr, png_bytep row) +{ + png_debug2(1, "in png_write_row (row %ld, pass %d)\n", + png_ptr->row_number, png_ptr->pass); + /* initialize transformations and other stuff if first time */ + if (png_ptr->row_number == 0 && png_ptr->pass == 0) + { + /* make sure we wrote the header info */ + if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) + png_error(png_ptr, + "png_write_info was never called before png_write_row."); + + /* check for transforms that have been set but were defined out */ +#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_MONO) + png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) + if (png_ptr->transformations & PNG_FILLER) + png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) + if (png_ptr->transformations & PNG_PACK) + png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) + if (png_ptr->transformations & PNG_SHIFT) + png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) + if (png_ptr->transformations & PNG_BGR) + png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined."); +#endif +#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_BYTES) + png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined."); +#endif + + png_write_start_row(png_ptr); + } + +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) + /* if interlaced and not interested in row, return */ + if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + { + switch (png_ptr->pass) + { + case 0: + if (png_ptr->row_number & 0x07) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 1: + if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 2: + if ((png_ptr->row_number & 0x07) != 4) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 3: + if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 4: + if ((png_ptr->row_number & 0x03) != 2) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 5: + if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) + { + png_write_finish_row(png_ptr); + return; + } + break; + case 6: + if (!(png_ptr->row_number & 0x01)) + { + png_write_finish_row(png_ptr); + return; + } + break; + } + } +#endif + + /* set up row info for transformations */ + png_ptr->row_info.color_type = png_ptr->color_type; + png_ptr->row_info.width = png_ptr->usr_width; + png_ptr->row_info.channels = png_ptr->usr_channels; + png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth; + png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * + png_ptr->row_info.channels); + + png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, + png_ptr->row_info.width); + + png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type); + png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width); + png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels); + png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth); + png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth); + png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes); + + /* Copy user's row into buffer, leaving room for filter byte. */ + png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row, + png_ptr->row_info.rowbytes); + +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) + /* handle interlacing */ + if (png_ptr->interlaced && png_ptr->pass < 6 && + (png_ptr->transformations & PNG_INTERLACE)) + { + png_do_write_interlace(&(png_ptr->row_info), + png_ptr->row_buf + 1, png_ptr->pass); + /* this should always get caught above, but still ... */ + if (!(png_ptr->row_info.width)) + { + png_write_finish_row(png_ptr); + return; + } + } +#endif + + /* handle other transformations */ + if (png_ptr->transformations) + png_do_write_transformations(png_ptr); + +#if defined(PNG_MNG_FEATURES_SUPPORTED) + /* Write filter_method 64 (intrapixel differencing) only if + * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and + * 2. Libpng did not write a PNG signature (this filter_method is only + * used in PNG datastreams that are embedded in MNG datastreams) and + * 3. The application called png_permit_mng_features with a mask that + * included PNG_FLAG_MNG_FILTER_64 and + * 4. The filter_method is 64 and + * 5. The color_type is RGB or RGBA + */ + if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) + { + /* Intrapixel differencing */ + png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); + } +#endif + + /* Find a filter if necessary, filter the row and write it out. */ + png_write_find_filter(png_ptr, &(png_ptr->row_info)); + + if (png_ptr->write_row_fn != NULL) + (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); +} + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) +/* Set the automatic flush interval or 0 to turn flushing off */ +void PNGAPI +png_set_flush(png_structp png_ptr, int nrows) +{ + png_debug(1, "in png_set_flush\n"); + png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); +} + +/* flush the current output buffers now */ +void PNGAPI +png_write_flush(png_structp png_ptr) +{ + int wrote_IDAT; + + png_debug(1, "in png_write_flush\n"); + /* We have already written out all of the data */ + if (png_ptr->row_number >= png_ptr->num_rows) + return; + + do + { + int ret; + + /* compress the data */ + ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); + wrote_IDAT = 0; + + /* check for compression errors */ + if (ret != Z_OK) + { + if (png_ptr->zstream.msg != NULL) + png_error(png_ptr, png_ptr->zstream.msg); + else + png_error(png_ptr, "zlib error"); + } + + if (!(png_ptr->zstream.avail_out)) + { + /* write the IDAT and reset the zlib output buffer */ + png_write_IDAT(png_ptr, png_ptr->zbuf, + png_ptr->zbuf_size); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + wrote_IDAT = 1; + } + } while(wrote_IDAT == 1); + + /* If there is any data left to be output, write it into a new IDAT */ + if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) + { + /* write the IDAT and reset the zlib output buffer */ + png_write_IDAT(png_ptr, png_ptr->zbuf, + png_ptr->zbuf_size - png_ptr->zstream.avail_out); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + } + png_ptr->flush_rows = 0; + png_flush(png_ptr); +} +#endif /* PNG_WRITE_FLUSH_SUPPORTED */ + +/* free all memory used by the write */ +void PNGAPI +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) +{ + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; +#ifdef PNG_USER_MEM_SUPPORTED + png_free_ptr free_fn = NULL; + png_voidp mem_ptr = NULL; +#endif + + png_debug(1, "in png_destroy_write_struct\n"); + if (png_ptr_ptr != NULL) + { + png_ptr = *png_ptr_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + free_fn = png_ptr->free_fn; + mem_ptr = png_ptr->mem_ptr; +#endif + } + + if (info_ptr_ptr != NULL) + info_ptr = *info_ptr_ptr; + + if (info_ptr != NULL) + { + png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); + +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) + if (png_ptr->num_chunk_list) + { + png_free(png_ptr, png_ptr->chunk_list); + png_ptr->chunk_list=NULL; + png_ptr->num_chunk_list=0; + } +#endif + +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, + (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)info_ptr); +#endif + *info_ptr_ptr = NULL; + } + + if (png_ptr != NULL) + { + png_write_destroy(png_ptr); +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, + (png_voidp)mem_ptr); +#else + png_destroy_struct((png_voidp)png_ptr); +#endif + *png_ptr_ptr = NULL; + } +} + + +/* Free any memory used in png_ptr struct (old method) */ +void /* PRIVATE */ +png_write_destroy(png_structp png_ptr) +{ +#ifdef PNG_SETJMP_SUPPORTED + jmp_buf tmp_jmp; /* save jump buffer */ +#endif + png_error_ptr error_fn; + png_error_ptr warning_fn; + png_voidp error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + png_free_ptr free_fn; +#endif + + png_debug(1, "in png_write_destroy\n"); + /* free any memory zlib uses */ + deflateEnd(&png_ptr->zstream); + + /* free our memory. png_free checks NULL for us. */ + png_free(png_ptr, png_ptr->zbuf); + png_free(png_ptr, png_ptr->row_buf); + png_free(png_ptr, png_ptr->prev_row); + png_free(png_ptr, png_ptr->sub_row); + png_free(png_ptr, png_ptr->up_row); + png_free(png_ptr, png_ptr->avg_row); + png_free(png_ptr, png_ptr->paeth_row); + +#if defined(PNG_TIME_RFC1123_SUPPORTED) + png_free(png_ptr, png_ptr->time_buffer); +#endif + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + png_free(png_ptr, png_ptr->prev_filters); + png_free(png_ptr, png_ptr->filter_weights); + png_free(png_ptr, png_ptr->inv_filter_weights); + png_free(png_ptr, png_ptr->filter_costs); + png_free(png_ptr, png_ptr->inv_filter_costs); +#endif + +#ifdef PNG_SETJMP_SUPPORTED + /* reset structure */ + png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf)); +#endif + + error_fn = png_ptr->error_fn; + warning_fn = png_ptr->warning_fn; + error_ptr = png_ptr->error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + free_fn = png_ptr->free_fn; +#endif + + png_memset(png_ptr, 0, png_sizeof (png_struct)); + + png_ptr->error_fn = error_fn; + png_ptr->warning_fn = warning_fn; + png_ptr->error_ptr = error_ptr; +#ifdef PNG_USER_MEM_SUPPORTED + png_ptr->free_fn = free_fn; +#endif + +#ifdef PNG_SETJMP_SUPPORTED + png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf)); +#endif +} + +/* Allow the application to select one or more row filters to use. */ +void PNGAPI +png_set_filter(png_structp png_ptr, int method, int filters) +{ + png_debug(1, "in png_set_filter\n"); +#if defined(PNG_MNG_FEATURES_SUPPORTED) + if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + (method == PNG_INTRAPIXEL_DIFFERENCING)) + method = PNG_FILTER_TYPE_BASE; +#endif + if (method == PNG_FILTER_TYPE_BASE) + { + switch (filters & (PNG_ALL_FILTERS | 0x07)) + { + case 5: + case 6: + case 7: png_warning(png_ptr, "Unknown row filter for method 0"); + case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break; + case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break; + case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break; + case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break; + case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break; + default: png_ptr->do_filter = (png_byte)filters; break; + } + + /* If we have allocated the row_buf, this means we have already started + * with the image and we should have allocated all of the filter buffers + * that have been selected. If prev_row isn't already allocated, then + * it is too late to start using the filters that need it, since we + * will be missing the data in the previous row. If an application + * wants to start and stop using particular filters during compression, + * it should start out with all of the filters, and then add and + * remove them after the start of compression. + */ + if (png_ptr->row_buf != NULL) + { + if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL) + { + png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; + } + + if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL) + { + if (png_ptr->prev_row == NULL) + { + png_warning(png_ptr, "Can't add Up filter after starting"); + png_ptr->do_filter &= ~PNG_FILTER_UP; + } + else + { + png_ptr->up_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; + } + } + + if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL) + { + if (png_ptr->prev_row == NULL) + { + png_warning(png_ptr, "Can't add Average filter after starting"); + png_ptr->do_filter &= ~PNG_FILTER_AVG; + } + else + { + png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; + } + } + + if ((png_ptr->do_filter & PNG_FILTER_PAETH) && + png_ptr->paeth_row == NULL) + { + if (png_ptr->prev_row == NULL) + { + png_warning(png_ptr, "Can't add Paeth filter after starting"); + png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); + } + else + { + png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; + } + } + + if (png_ptr->do_filter == PNG_NO_FILTERS) + png_ptr->do_filter = PNG_FILTER_NONE; + } + } + else + png_error(png_ptr, "Unknown custom filter method"); +} + +/* This allows us to influence the way in which libpng chooses the "best" + * filter for the current scanline. While the "minimum-sum-of-absolute- + * differences metric is relatively fast and effective, there is some + * question as to whether it can be improved upon by trying to keep the + * filtered data going to zlib more consistent, hopefully resulting in + * better compression. + */ +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */ +void PNGAPI +png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, + int num_weights, png_doublep filter_weights, + png_doublep filter_costs) +{ + int i; + + png_debug(1, "in png_set_filter_heuristics\n"); + if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST) + { + png_warning(png_ptr, "Unknown filter heuristic method"); + return; + } + + if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT) + { + heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; + } + + if (num_weights < 0 || filter_weights == NULL || + heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) + { + num_weights = 0; + } + + png_ptr->num_prev_filters = (png_byte)num_weights; + png_ptr->heuristic_method = (png_byte)heuristic_method; + + if (num_weights > 0) + { + if (png_ptr->prev_filters == NULL) + { + png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, + (png_uint_32)(png_sizeof(png_byte) * num_weights)); + + /* To make sure that the weighting starts out fairly */ + for (i = 0; i < num_weights; i++) + { + png_ptr->prev_filters[i] = 255; + } + } + + if (png_ptr->filter_weights == NULL) + { + png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); + + png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); + for (i = 0; i < num_weights; i++) + { + png_ptr->inv_filter_weights[i] = + png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; + } + } + + for (i = 0; i < num_weights; i++) + { + if (filter_weights[i] < 0.0) + { + png_ptr->inv_filter_weights[i] = + png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; + } + else + { + png_ptr->inv_filter_weights[i] = + (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5); + png_ptr->filter_weights[i] = + (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5); + } + } + } + + /* If, in the future, there are other filter methods, this would + * need to be based on png_ptr->filter. + */ + if (png_ptr->filter_costs == NULL) + { + png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); + + png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, + (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); + + for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) + { + png_ptr->inv_filter_costs[i] = + png_ptr->filter_costs[i] = PNG_COST_FACTOR; + } + } + + /* Here is where we set the relative costs of the different filters. We + * should take the desired compression level into account when setting + * the costs, so that Paeth, for instance, has a high relative cost at low + * compression levels, while it has a lower relative cost at higher + * compression settings. The filter types are in order of increasing + * relative cost, so it would be possible to do this with an algorithm. + */ + for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) + { + if (filter_costs == NULL || filter_costs[i] < 0.0) + { + png_ptr->inv_filter_costs[i] = + png_ptr->filter_costs[i] = PNG_COST_FACTOR; + } + else if (filter_costs[i] >= 1.0) + { + png_ptr->inv_filter_costs[i] = + (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5); + png_ptr->filter_costs[i] = + (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5); + } + } +} +#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ + +void PNGAPI +png_set_compression_level(png_structp png_ptr, int level) +{ + png_debug(1, "in png_set_compression_level\n"); + png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; + png_ptr->zlib_level = level; +} + +void PNGAPI +png_set_compression_mem_level(png_structp png_ptr, int mem_level) +{ + png_debug(1, "in png_set_compression_mem_level\n"); + png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; + png_ptr->zlib_mem_level = mem_level; +} + +void PNGAPI +png_set_compression_strategy(png_structp png_ptr, int strategy) +{ + png_debug(1, "in png_set_compression_strategy\n"); + png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; + png_ptr->zlib_strategy = strategy; +} + +void PNGAPI +png_set_compression_window_bits(png_structp png_ptr, int window_bits) +{ + if (window_bits > 15) + png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); + else if (window_bits < 8) + png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); +#ifndef WBITS_8_OK + /* avoid libpng bug with 256-byte windows */ + if (window_bits == 8) + { + png_warning(png_ptr, "Compression window is being reset to 512"); + window_bits=9; + } +#endif + png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; + png_ptr->zlib_window_bits = window_bits; +} + +void PNGAPI +png_set_compression_method(png_structp png_ptr, int method) +{ + png_debug(1, "in png_set_compression_method\n"); + if (method != 8) + png_warning(png_ptr, "Only compression method 8 is supported by PNG"); + png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; + png_ptr->zlib_method = method; +} + +void PNGAPI +png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) +{ + png_ptr->write_row_fn = write_row_fn; +} + +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) +void PNGAPI +png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr + write_user_transform_fn) +{ + png_debug(1, "in png_set_write_user_transform_fn\n"); + png_ptr->transformations |= PNG_USER_TRANSFORM; + png_ptr->write_user_transform_fn = write_user_transform_fn; +} +#endif + + +#if defined(PNG_INFO_IMAGE_SUPPORTED) +void PNGAPI +png_write_png(png_structp png_ptr, png_infop info_ptr, + int transforms, voidp params) +{ +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) + /* invert the alpha channel from opacity to transparency */ + if (transforms & PNG_TRANSFORM_INVERT_ALPHA) + png_set_invert_alpha(png_ptr); +#endif + + /* Write the file header information. */ + png_write_info(png_ptr, info_ptr); + + /* ------ these transformations don't touch the info structure ------- */ + +#if defined(PNG_WRITE_INVERT_SUPPORTED) + /* invert monochrome pixels */ + if (transforms & PNG_TRANSFORM_INVERT_MONO) + png_set_invert_mono(png_ptr); +#endif + +#if defined(PNG_WRITE_SHIFT_SUPPORTED) + /* Shift the pixels up to a legal bit depth and fill in + * as appropriate to correctly scale the image. + */ + if ((transforms & PNG_TRANSFORM_SHIFT) + && (info_ptr->valid & PNG_INFO_sBIT)) + png_set_shift(png_ptr, &info_ptr->sig_bit); +#endif + +#if defined(PNG_WRITE_PACK_SUPPORTED) + /* pack pixels into bytes */ + if (transforms & PNG_TRANSFORM_PACKING) + png_set_packing(png_ptr); +#endif + +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) + /* swap location of alpha bytes from ARGB to RGBA */ + if (transforms & PNG_TRANSFORM_SWAP_ALPHA) + png_set_swap_alpha(png_ptr); +#endif + +#if defined(PNG_WRITE_FILLER_SUPPORTED) + /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into + * RGB (4 channels -> 3 channels). The second parameter is not used. + */ + if (transforms & PNG_TRANSFORM_STRIP_FILLER) + png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); +#endif + +#if defined(PNG_WRITE_BGR_SUPPORTED) + /* flip BGR pixels to RGB */ + if (transforms & PNG_TRANSFORM_BGR) + png_set_bgr(png_ptr); +#endif + +#if defined(PNG_WRITE_SWAP_SUPPORTED) + /* swap bytes of 16-bit files to most significant byte first */ + if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) + png_set_swap(png_ptr); +#endif + +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) + /* swap bits of 1, 2, 4 bit packed pixel formats */ + if (transforms & PNG_TRANSFORM_PACKSWAP) + png_set_packswap(png_ptr); +#endif + + /* ----------------------- end of transformations ------------------- */ + + /* write the bits */ + if (info_ptr->valid & PNG_INFO_IDAT) + png_write_image(png_ptr, info_ptr->row_pointers); + + /* It is REQUIRED to call this to finish writing the rest of the file */ + png_write_end(png_ptr, info_ptr); + + if(transforms == 0 || params == NULL) + /* quiet compiler warnings */ return; +} +#endif +#endif /* PNG_WRITE_SUPPORTED */ diff --git a/libpng/pngwtran.c b/libpng/pngwtran.c new file mode 100644 index 00000000..f1c8b3e6 --- /dev/null +++ b/libpng/pngwtran.c @@ -0,0 +1,563 @@ + +/* pngwtran.c - transforms the data in a row for PNG writers + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#include "png.h" +#ifdef PNG_WRITE_SUPPORTED + +/* Transform the data according to the user's wishes. The order of + * transformations is significant. + */ +void /* PRIVATE */ +png_do_write_transformations(png_structp png_ptr) +{ + png_debug(1, "in png_do_write_transformations\n"); + + if (png_ptr == NULL) + return; + +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) + if (png_ptr->transformations & PNG_USER_TRANSFORM) + if(png_ptr->write_user_transform_fn != NULL) + (*(png_ptr->write_user_transform_fn)) /* user write transform function */ + (png_ptr, /* png_ptr */ + &(png_ptr->row_info), /* row_info: */ + /* png_uint_32 width; width of row */ + /* png_uint_32 rowbytes; number of bytes in row */ + /* png_byte color_type; color type of pixels */ + /* png_byte bit_depth; bit depth of samples */ + /* png_byte channels; number of channels (1-4) */ + /* png_byte pixel_depth; bits per pixel (depth*channels) */ + png_ptr->row_buf + 1); /* start of pixel data for row */ +#endif +#if defined(PNG_WRITE_FILLER_SUPPORTED) + if (png_ptr->transformations & PNG_FILLER) + png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, + png_ptr->flags); +#endif +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) + if (png_ptr->transformations & PNG_PACKSWAP) + png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +#if defined(PNG_WRITE_PACK_SUPPORTED) + if (png_ptr->transformations & PNG_PACK) + png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, + (png_uint_32)png_ptr->bit_depth); +#endif +#if defined(PNG_WRITE_SWAP_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_BYTES) + png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +#if defined(PNG_WRITE_SHIFT_SUPPORTED) + if (png_ptr->transformations & PNG_SHIFT) + png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, + &(png_ptr->shift)); +#endif +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_ALPHA) + png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) + if (png_ptr->transformations & PNG_SWAP_ALPHA) + png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +#if defined(PNG_WRITE_BGR_SUPPORTED) + if (png_ptr->transformations & PNG_BGR) + png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +#if defined(PNG_WRITE_INVERT_SUPPORTED) + if (png_ptr->transformations & PNG_INVERT_MONO) + png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif +} + +#if defined(PNG_WRITE_PACK_SUPPORTED) +/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The + * row_info bit depth should be 8 (one pixel per byte). The channels + * should be 1 (this only happens on grayscale and paletted images). + */ +void /* PRIVATE */ +png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) +{ + png_debug(1, "in png_do_pack\n"); + if (row_info->bit_depth == 8 && +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + row_info->channels == 1) + { + switch ((int)bit_depth) + { + case 1: + { + png_bytep sp, dp; + int mask, v; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + sp = row; + dp = row; + mask = 0x80; + v = 0; + + for (i = 0; i < row_width; i++) + { + if (*sp != 0) + v |= mask; + sp++; + if (mask > 1) + mask >>= 1; + else + { + mask = 0x80; + *dp = (png_byte)v; + dp++; + v = 0; + } + } + if (mask != 0x80) + *dp = (png_byte)v; + break; + } + case 2: + { + png_bytep sp, dp; + int shift, v; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + sp = row; + dp = row; + shift = 6; + v = 0; + for (i = 0; i < row_width; i++) + { + png_byte value; + + value = (png_byte)(*sp & 0x03); + v |= (value << shift); + if (shift == 0) + { + shift = 6; + *dp = (png_byte)v; + dp++; + v = 0; + } + else + shift -= 2; + sp++; + } + if (shift != 6) + *dp = (png_byte)v; + break; + } + case 4: + { + png_bytep sp, dp; + int shift, v; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + sp = row; + dp = row; + shift = 4; + v = 0; + for (i = 0; i < row_width; i++) + { + png_byte value; + + value = (png_byte)(*sp & 0x0f); + v |= (value << shift); + + if (shift == 0) + { + shift = 4; + *dp = (png_byte)v; + dp++; + v = 0; + } + else + shift -= 4; + + sp++; + } + if (shift != 4) + *dp = (png_byte)v; + break; + } + } + row_info->bit_depth = (png_byte)bit_depth; + row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, + row_info->width); + } +} +#endif + +#if defined(PNG_WRITE_SHIFT_SUPPORTED) +/* Shift pixel values to take advantage of whole range. Pass the + * true number of bits in bit_depth. The row should be packed + * according to row_info->bit_depth. Thus, if you had a row of + * bit depth 4, but the pixels only had values from 0 to 7, you + * would pass 3 as bit_depth, and this routine would translate the + * data to 0 to 15. + */ +void /* PRIVATE */ +png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) +{ + png_debug(1, "in png_do_shift\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL && +#else + if ( +#endif + row_info->color_type != PNG_COLOR_TYPE_PALETTE) + { + int shift_start[4], shift_dec[4]; + int channels = 0; + + if (row_info->color_type & PNG_COLOR_MASK_COLOR) + { + shift_start[channels] = row_info->bit_depth - bit_depth->red; + shift_dec[channels] = bit_depth->red; + channels++; + shift_start[channels] = row_info->bit_depth - bit_depth->green; + shift_dec[channels] = bit_depth->green; + channels++; + shift_start[channels] = row_info->bit_depth - bit_depth->blue; + shift_dec[channels] = bit_depth->blue; + channels++; + } + else + { + shift_start[channels] = row_info->bit_depth - bit_depth->gray; + shift_dec[channels] = bit_depth->gray; + channels++; + } + if (row_info->color_type & PNG_COLOR_MASK_ALPHA) + { + shift_start[channels] = row_info->bit_depth - bit_depth->alpha; + shift_dec[channels] = bit_depth->alpha; + channels++; + } + + /* with low row depths, could only be grayscale, so one channel */ + if (row_info->bit_depth < 8) + { + png_bytep bp = row; + png_uint_32 i; + png_byte mask; + png_uint_32 row_bytes = row_info->rowbytes; + + if (bit_depth->gray == 1 && row_info->bit_depth == 2) + mask = 0x55; + else if (row_info->bit_depth == 4 && bit_depth->gray == 3) + mask = 0x11; + else + mask = 0xff; + + for (i = 0; i < row_bytes; i++, bp++) + { + png_uint_16 v; + int j; + + v = *bp; + *bp = 0; + for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) + { + if (j > 0) + *bp |= (png_byte)((v << j) & 0xff); + else + *bp |= (png_byte)((v >> (-j)) & mask); + } + } + } + else if (row_info->bit_depth == 8) + { + png_bytep bp = row; + png_uint_32 i; + png_uint_32 istop = channels * row_info->width; + + for (i = 0; i < istop; i++, bp++) + { + + png_uint_16 v; + int j; + int c = (int)(i%channels); + + v = *bp; + *bp = 0; + for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) + { + if (j > 0) + *bp |= (png_byte)((v << j) & 0xff); + else + *bp |= (png_byte)((v >> (-j)) & 0xff); + } + } + } + else + { + png_bytep bp; + png_uint_32 i; + png_uint_32 istop = channels * row_info->width; + + for (bp = row, i = 0; i < istop; i++) + { + int c = (int)(i%channels); + png_uint_16 value, v; + int j; + + v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1)); + value = 0; + for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) + { + if (j > 0) + value |= (png_uint_16)((v << j) & (png_uint_16)0xffff); + else + value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); + } + *bp++ = (png_byte)(value >> 8); + *bp++ = (png_byte)(value & 0xff); + } + } + } +} +#endif + +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) +void /* PRIVATE */ +png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_write_swap_alpha\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + /* This converts from ARGB to RGBA */ + if (row_info->bit_depth == 8) + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + for (i = 0, sp = dp = row; i < row_width; i++) + { + png_byte save = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = save; + } + } + /* This converts from AARRGGBB to RRGGBBAA */ + else + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + png_byte save[2]; + save[0] = *(sp++); + save[1] = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = save[0]; + *(dp++) = save[1]; + } + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + /* This converts from AG to GA */ + if (row_info->bit_depth == 8) + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + png_byte save = *(sp++); + *(dp++) = *(sp++); + *(dp++) = save; + } + } + /* This converts from AAGG to GGAA */ + else + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + png_byte save[2]; + save[0] = *(sp++); + save[1] = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = save[0]; + *(dp++) = save[1]; + } + } + } + } +} +#endif + +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) +void /* PRIVATE */ +png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_write_invert_alpha\n"); +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL) +#endif + { + if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + { + /* This inverts the alpha channel in RGBA */ + if (row_info->bit_depth == 8) + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + for (i = 0, sp = dp = row; i < row_width; i++) + { + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = (png_byte)(255 - *(sp++)); + } + } + /* This inverts the alpha channel in RRGGBBAA */ + else + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = (png_byte)(255 - *(sp++)); + *(dp++) = (png_byte)(255 - *(sp++)); + } + } + } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + /* This inverts the alpha channel in GA */ + if (row_info->bit_depth == 8) + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + *(dp++) = *(sp++); + *(dp++) = (png_byte)(255 - *(sp++)); + } + } + /* This inverts the alpha channel in GGAA */ + else + { + png_bytep sp, dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + for (i = 0, sp = dp = row; i < row_width; i++) + { + *(dp++) = *(sp++); + *(dp++) = *(sp++); + *(dp++) = (png_byte)(255 - *(sp++)); + *(dp++) = (png_byte)(255 - *(sp++)); + } + } + } + } +} +#endif + +#if defined(PNG_MNG_FEATURES_SUPPORTED) +/* undoes intrapixel differencing */ +void /* PRIVATE */ +png_do_write_intrapixel(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_write_intrapixel\n"); + if ( +#if defined(PNG_USELESS_TESTS_SUPPORTED) + row != NULL && row_info != NULL && +#endif + (row_info->color_type & PNG_COLOR_MASK_COLOR)) + { + int bytes_per_pixel; + png_uint_32 row_width = row_info->width; + if (row_info->bit_depth == 8) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 3; + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 4; + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + *(rp) = (png_byte)((*rp - *(rp+1))&0xff); + *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff); + } + } + else if (row_info->bit_depth == 16) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 6; + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 8; + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + png_uint_32 s0 = (*(rp ) << 8) | *(rp+1); + png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3); + png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5); + png_uint_32 red = (png_uint_32)((s0-s1) & 0xffffL); + png_uint_32 blue = (png_uint_32)((s2-s1) & 0xffffL); + *(rp ) = (png_byte)((red >> 8) & 0xff); + *(rp+1) = (png_byte)(red & 0xff); + *(rp+4) = (png_byte)((blue >> 8) & 0xff); + *(rp+5) = (png_byte)(blue & 0xff); + } + } + } +} +#endif /* PNG_MNG_FEATURES_SUPPORTED */ +#endif /* PNG_WRITE_SUPPORTED */ diff --git a/libpng/pngwutil.c b/libpng/pngwutil.c new file mode 100644 index 00000000..dd7b150b --- /dev/null +++ b/libpng/pngwutil.c @@ -0,0 +1,2730 @@ + +/* pngwutil.c - utilities to write a PNG file + * + * libpng version 1.2.8 - December 3, 2004 + * For conditions of distribution and use, see copyright notice in png.h + * Copyright (c) 1998-2004 Glenn Randers-Pehrson + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) + */ + +#define PNG_INTERNAL +#include "png.h" +#ifdef PNG_WRITE_SUPPORTED + +/* Place a 32-bit number into a buffer in PNG byte order. We work + * with unsigned numbers for convenience, although one supported + * ancillary chunk uses signed (two's complement) numbers. + */ +void /* PRIVATE */ +png_save_uint_32(png_bytep buf, png_uint_32 i) +{ + buf[0] = (png_byte)((i >> 24) & 0xff); + buf[1] = (png_byte)((i >> 16) & 0xff); + buf[2] = (png_byte)((i >> 8) & 0xff); + buf[3] = (png_byte)(i & 0xff); +} + +#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED) +/* The png_save_int_32 function assumes integers are stored in two's + * complement format. If this isn't the case, then this routine needs to + * be modified to write data in two's complement format. + */ +void /* PRIVATE */ +png_save_int_32(png_bytep buf, png_int_32 i) +{ + buf[0] = (png_byte)((i >> 24) & 0xff); + buf[1] = (png_byte)((i >> 16) & 0xff); + buf[2] = (png_byte)((i >> 8) & 0xff); + buf[3] = (png_byte)(i & 0xff); +} +#endif + +/* Place a 16-bit number into a buffer in PNG byte order. + * The parameter is declared unsigned int, not png_uint_16, + * just to avoid potential problems on pre-ANSI C compilers. + */ +void /* PRIVATE */ +png_save_uint_16(png_bytep buf, unsigned int i) +{ + buf[0] = (png_byte)((i >> 8) & 0xff); + buf[1] = (png_byte)(i & 0xff); +} + +/* Write a PNG chunk all at once. The type is an array of ASCII characters + * representing the chunk name. The array must be at least 4 bytes in + * length, and does not need to be null terminated. To be safe, pass the + * pre-defined chunk names here, and if you need a new one, define it + * where the others are defined. The length is the length of the data. + * All the data must be present. If that is not possible, use the + * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() + * functions instead. + */ +void PNGAPI +png_write_chunk(png_structp png_ptr, png_bytep chunk_name, + png_bytep data, png_size_t length) +{ + png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length); + png_write_chunk_data(png_ptr, data, length); + png_write_chunk_end(png_ptr); +} + +/* Write the start of a PNG chunk. The type is the chunk type. + * The total_length is the sum of the lengths of all the data you will be + * passing in png_write_chunk_data(). + */ +void PNGAPI +png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, + png_uint_32 length) +{ + png_byte buf[4]; + png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length); + + /* write the length */ + png_save_uint_32(buf, length); + png_write_data(png_ptr, buf, (png_size_t)4); + + /* write the chunk name */ + png_write_data(png_ptr, chunk_name, (png_size_t)4); + /* reset the crc and run it over the chunk name */ + png_reset_crc(png_ptr); + png_calculate_crc(png_ptr, chunk_name, (png_size_t)4); +} + +/* Write the data of a PNG chunk started with png_write_chunk_start(). + * Note that multiple calls to this function are allowed, and that the + * sum of the lengths from these calls *must* add up to the total_length + * given to png_write_chunk_start(). + */ +void PNGAPI +png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length) +{ + /* write the data, and run the CRC over it */ + if (data != NULL && length > 0) + { + png_calculate_crc(png_ptr, data, length); + png_write_data(png_ptr, data, length); + } +} + +/* Finish a chunk started with png_write_chunk_start(). */ +void PNGAPI +png_write_chunk_end(png_structp png_ptr) +{ + png_byte buf[4]; + + /* write the crc */ + png_save_uint_32(buf, png_ptr->crc); + + png_write_data(png_ptr, buf, (png_size_t)4); +} + +/* Simple function to write the signature. If we have already written + * the magic bytes of the signature, or more likely, the PNG stream is + * being embedded into another stream and doesn't need its own signature, + * we should call png_set_sig_bytes() to tell libpng how many of the + * bytes have already been written. + */ +void /* PRIVATE */ +png_write_sig(png_structp png_ptr) +{ + png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; + /* write the rest of the 8 byte signature */ + png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], + (png_size_t)8 - png_ptr->sig_bytes); + if(png_ptr->sig_bytes < 3) + png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; +} + +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED) +/* + * This pair of functions encapsulates the operation of (a) compressing a + * text string, and (b) issuing it later as a series of chunk data writes. + * The compression_state structure is shared context for these functions + * set up by the caller in order to make the whole mess thread-safe. + */ + +typedef struct +{ + char *input; /* the uncompressed input data */ + int input_len; /* its length */ + int num_output_ptr; /* number of output pointers used */ + int max_output_ptr; /* size of output_ptr */ + png_charpp output_ptr; /* array of pointers to output */ +} compression_state; + +/* compress given text into storage in the png_ptr structure */ +static int /* PRIVATE */ +png_text_compress(png_structp png_ptr, + png_charp text, png_size_t text_len, int compression, + compression_state *comp) +{ + int ret; + + comp->num_output_ptr = comp->max_output_ptr = 0; + comp->output_ptr = NULL; + comp->input = NULL; + + /* we may just want to pass the text right through */ + if (compression == PNG_TEXT_COMPRESSION_NONE) + { + comp->input = text; + comp->input_len = text_len; + return((int)text_len); + } + + if (compression >= PNG_TEXT_COMPRESSION_LAST) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char msg[50]; + sprintf(msg, "Unknown compression type %d", compression); + png_warning(png_ptr, msg); +#else + png_warning(png_ptr, "Unknown compression type"); +#endif + } + + /* We can't write the chunk until we find out how much data we have, + * which means we need to run the compressor first and save the + * output. This shouldn't be a problem, as the vast majority of + * comments should be reasonable, but we will set up an array of + * malloc'd pointers to be sure. + * + * If we knew the application was well behaved, we could simplify this + * greatly by assuming we can always malloc an output buffer large + * enough to hold the compressed text ((1001 * text_len / 1000) + 12) + * and malloc this directly. The only time this would be a bad idea is + * if we can't malloc more than 64K and we have 64K of random input + * data, or if the input string is incredibly large (although this + * wouldn't cause a failure, just a slowdown due to swapping). + */ + + /* set up the compression buffers */ + png_ptr->zstream.avail_in = (uInt)text_len; + png_ptr->zstream.next_in = (Bytef *)text; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf; + + /* this is the same compression loop as in png_write_row() */ + do + { + /* compress the data */ + ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); + if (ret != Z_OK) + { + /* error */ + if (png_ptr->zstream.msg != NULL) + png_error(png_ptr, png_ptr->zstream.msg); + else + png_error(png_ptr, "zlib error"); + } + /* check to see if we need more room */ + if (!(png_ptr->zstream.avail_out)) + { + /* make sure the output array has room */ + if (comp->num_output_ptr >= comp->max_output_ptr) + { + int old_max; + + old_max = comp->max_output_ptr; + comp->max_output_ptr = comp->num_output_ptr + 4; + if (comp->output_ptr != NULL) + { + png_charpp old_ptr; + + old_ptr = comp->output_ptr; + comp->output_ptr = (png_charpp)png_malloc(png_ptr, + (png_uint_32)(comp->max_output_ptr * + png_sizeof (png_charpp))); + png_memcpy(comp->output_ptr, old_ptr, old_max + * png_sizeof (png_charp)); + png_free(png_ptr, old_ptr); + } + else + comp->output_ptr = (png_charpp)png_malloc(png_ptr, + (png_uint_32)(comp->max_output_ptr * + png_sizeof (png_charp))); + } + + /* save the data */ + comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr, + (png_uint_32)png_ptr->zbuf_size); + png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, + png_ptr->zbuf_size); + comp->num_output_ptr++; + + /* and reset the buffer */ + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + } + /* continue until we don't have any more to compress */ + } while (png_ptr->zstream.avail_in); + + /* finish the compression */ + do + { + /* tell zlib we are finished */ + ret = deflate(&png_ptr->zstream, Z_FINISH); + + if (ret == Z_OK) + { + /* check to see if we need more room */ + if (!(png_ptr->zstream.avail_out)) + { + /* check to make sure our output array has room */ + if (comp->num_output_ptr >= comp->max_output_ptr) + { + int old_max; + + old_max = comp->max_output_ptr; + comp->max_output_ptr = comp->num_output_ptr + 4; + if (comp->output_ptr != NULL) + { + png_charpp old_ptr; + + old_ptr = comp->output_ptr; + /* This could be optimized to realloc() */ + comp->output_ptr = (png_charpp)png_malloc(png_ptr, + (png_uint_32)(comp->max_output_ptr * + png_sizeof (png_charpp))); + png_memcpy(comp->output_ptr, old_ptr, + old_max * png_sizeof (png_charp)); + png_free(png_ptr, old_ptr); + } + else + comp->output_ptr = (png_charpp)png_malloc(png_ptr, + (png_uint_32)(comp->max_output_ptr * + png_sizeof (png_charp))); + } + + /* save off the data */ + comp->output_ptr[comp->num_output_ptr] = + (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); + png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, + png_ptr->zbuf_size); + comp->num_output_ptr++; + + /* and reset the buffer pointers */ + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + } + } + else if (ret != Z_STREAM_END) + { + /* we got an error */ + if (png_ptr->zstream.msg != NULL) + png_error(png_ptr, png_ptr->zstream.msg); + else + png_error(png_ptr, "zlib error"); + } + } while (ret != Z_STREAM_END); + + /* text length is number of buffers plus last buffer */ + text_len = png_ptr->zbuf_size * comp->num_output_ptr; + if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) + text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; + + return((int)text_len); +} + +/* ship the compressed text out via chunk writes */ +static void /* PRIVATE */ +png_write_compressed_data_out(png_structp png_ptr, compression_state *comp) +{ + int i; + + /* handle the no-compression case */ + if (comp->input) + { + png_write_chunk_data(png_ptr, (png_bytep)comp->input, + (png_size_t)comp->input_len); + return; + } + + /* write saved output buffers, if any */ + for (i = 0; i < comp->num_output_ptr; i++) + { + png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i], + png_ptr->zbuf_size); + png_free(png_ptr, comp->output_ptr[i]); + comp->output_ptr[i]=NULL; + } + if (comp->max_output_ptr != 0) + png_free(png_ptr, comp->output_ptr); + comp->output_ptr=NULL; + /* write anything left in zbuf */ + if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) + png_write_chunk_data(png_ptr, png_ptr->zbuf, + png_ptr->zbuf_size - png_ptr->zstream.avail_out); + + /* reset zlib for another zTXt/iTXt or image data */ + deflateReset(&png_ptr->zstream); + png_ptr->zstream.data_type = Z_BINARY; +} +#endif + +/* Write the IHDR chunk, and update the png_struct with the necessary + * information. Note that the rest of this code depends upon this + * information being correct. + */ +void /* PRIVATE */ +png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, + int bit_depth, int color_type, int compression_type, int filter_type, + int interlace_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IHDR; +#endif + png_byte buf[13]; /* buffer to store the IHDR info */ + + png_debug(1, "in png_write_IHDR\n"); + /* Check that we have valid input data from the application info */ + switch (color_type) + { + case PNG_COLOR_TYPE_GRAY: + switch (bit_depth) + { + case 1: + case 2: + case 4: + case 8: + case 16: png_ptr->channels = 1; break; + default: png_error(png_ptr,"Invalid bit depth for grayscale image"); + } + break; + case PNG_COLOR_TYPE_RGB: + if (bit_depth != 8 && bit_depth != 16) + png_error(png_ptr, "Invalid bit depth for RGB image"); + png_ptr->channels = 3; + break; + case PNG_COLOR_TYPE_PALETTE: + switch (bit_depth) + { + case 1: + case 2: + case 4: + case 8: png_ptr->channels = 1; break; + default: png_error(png_ptr, "Invalid bit depth for paletted image"); + } + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + if (bit_depth != 8 && bit_depth != 16) + png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); + png_ptr->channels = 2; + break; + case PNG_COLOR_TYPE_RGB_ALPHA: + if (bit_depth != 8 && bit_depth != 16) + png_error(png_ptr, "Invalid bit depth for RGBA image"); + png_ptr->channels = 4; + break; + default: + png_error(png_ptr, "Invalid image color type specified"); + } + + if (compression_type != PNG_COMPRESSION_TYPE_BASE) + { + png_warning(png_ptr, "Invalid compression type specified"); + compression_type = PNG_COMPRESSION_TYPE_BASE; + } + + /* Write filter_method 64 (intrapixel differencing) only if + * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and + * 2. Libpng did not write a PNG signature (this filter_method is only + * used in PNG datastreams that are embedded in MNG datastreams) and + * 3. The application called png_permit_mng_features with a mask that + * included PNG_FLAG_MNG_FILTER_64 and + * 4. The filter_method is 64 and + * 5. The color_type is RGB or RGBA + */ + if ( +#if defined(PNG_MNG_FEATURES_SUPPORTED) + !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && + (color_type == PNG_COLOR_TYPE_RGB || + color_type == PNG_COLOR_TYPE_RGB_ALPHA) && + (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && +#endif + filter_type != PNG_FILTER_TYPE_BASE) + { + png_warning(png_ptr, "Invalid filter type specified"); + filter_type = PNG_FILTER_TYPE_BASE; + } + +#ifdef PNG_WRITE_INTERLACING_SUPPORTED + if (interlace_type != PNG_INTERLACE_NONE && + interlace_type != PNG_INTERLACE_ADAM7) + { + png_warning(png_ptr, "Invalid interlace type specified"); + interlace_type = PNG_INTERLACE_ADAM7; + } +#else + interlace_type=PNG_INTERLACE_NONE; +#endif + + /* save off the relevent information */ + png_ptr->bit_depth = (png_byte)bit_depth; + png_ptr->color_type = (png_byte)color_type; + png_ptr->interlaced = (png_byte)interlace_type; +#if defined(PNG_MNG_FEATURES_SUPPORTED) + png_ptr->filter_type = (png_byte)filter_type; +#endif + png_ptr->compression_type = (png_byte)compression_type; + png_ptr->width = width; + png_ptr->height = height; + + png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); + /* set the usr info, so any transformations can modify it */ + png_ptr->usr_width = png_ptr->width; + png_ptr->usr_bit_depth = png_ptr->bit_depth; + png_ptr->usr_channels = png_ptr->channels; + + /* pack the header information into the buffer */ + png_save_uint_32(buf, width); + png_save_uint_32(buf + 4, height); + buf[8] = (png_byte)bit_depth; + buf[9] = (png_byte)color_type; + buf[10] = (png_byte)compression_type; + buf[11] = (png_byte)filter_type; + buf[12] = (png_byte)interlace_type; + + /* write the chunk */ + png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13); + + /* initialize zlib with PNG info */ + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; + if (!(png_ptr->do_filter)) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || + png_ptr->bit_depth < 8) + png_ptr->do_filter = PNG_FILTER_NONE; + else + png_ptr->do_filter = PNG_ALL_FILTERS; + } + if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) + { + if (png_ptr->do_filter != PNG_FILTER_NONE) + png_ptr->zlib_strategy = Z_FILTERED; + else + png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; + } + if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) + png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; + if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) + png_ptr->zlib_mem_level = 8; + if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) + png_ptr->zlib_window_bits = 15; + if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) + png_ptr->zlib_method = 8; + deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, + png_ptr->zlib_method, png_ptr->zlib_window_bits, + png_ptr->zlib_mem_level, png_ptr->zlib_strategy); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + /* libpng is not interested in zstream.data_type */ + /* set it to a predefined value, to avoid its evaluation inside zlib */ + png_ptr->zstream.data_type = Z_BINARY; + + png_ptr->mode = PNG_HAVE_IHDR; +} + +/* write the palette. We are careful not to trust png_color to be in the + * correct order for PNG, so people can redefine it to any convenient + * structure. + */ +void /* PRIVATE */ +png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_PLTE; +#endif + png_uint_32 i; + png_colorp pal_ptr; + png_byte buf[3]; + + png_debug(1, "in png_write_PLTE\n"); + if (( +#if defined(PNG_MNG_FEATURES_SUPPORTED) + !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && +#endif + num_pal == 0) || num_pal > 256) + { + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + png_error(png_ptr, "Invalid number of colors in palette"); + } + else + { + png_warning(png_ptr, "Invalid number of colors in palette"); + return; + } + } + + if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) + { + png_warning(png_ptr, + "Ignoring request to write a PLTE chunk in grayscale PNG"); + return; + } + + png_ptr->num_palette = (png_uint_16)num_pal; + png_debug1(3, "num_palette = %d\n", png_ptr->num_palette); + + png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3); +#ifndef PNG_NO_POINTER_INDEXING + for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) + { + buf[0] = pal_ptr->red; + buf[1] = pal_ptr->green; + buf[2] = pal_ptr->blue; + png_write_chunk_data(png_ptr, buf, (png_size_t)3); + } +#else + /* This is a little slower but some buggy compilers need to do this instead */ + pal_ptr=palette; + for (i = 0; i < num_pal; i++) + { + buf[0] = pal_ptr[i].red; + buf[1] = pal_ptr[i].green; + buf[2] = pal_ptr[i].blue; + png_write_chunk_data(png_ptr, buf, (png_size_t)3); + } +#endif + png_write_chunk_end(png_ptr); + png_ptr->mode |= PNG_HAVE_PLTE; +} + +/* write an IDAT chunk */ +void /* PRIVATE */ +png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IDAT; +#endif + png_debug(1, "in png_write_IDAT\n"); + + /* Optimize the CMF field in the zlib stream. */ + /* This hack of the zlib stream is compliant to the stream specification. */ + if (!(png_ptr->mode & PNG_HAVE_IDAT) && + png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) + { + unsigned int z_cmf = data[0]; /* zlib compression method and flags */ + if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) + { + /* Avoid memory underflows and multiplication overflows. */ + /* The conditions below are practically always satisfied; + however, they still must be checked. */ + if (length >= 2 && + png_ptr->height < 16384 && png_ptr->width < 16384) + { + png_uint_32 uncompressed_idat_size = png_ptr->height * + ((png_ptr->width * + png_ptr->channels * png_ptr->bit_depth + 15) >> 3); + unsigned int z_cinfo = z_cmf >> 4; + unsigned int half_z_window_size = 1 << (z_cinfo + 7); + while (uncompressed_idat_size <= half_z_window_size && + half_z_window_size >= 256) + { + z_cinfo--; + half_z_window_size >>= 1; + } + z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); + if (data[0] != (png_byte)z_cmf) + { + data[0] = (png_byte)z_cmf; + data[1] &= 0xe0; + data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f); + } + } + } + else + png_error(png_ptr, + "Invalid zlib compression method or flags in IDAT"); + } + + png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length); + png_ptr->mode |= PNG_HAVE_IDAT; +} + +/* write an IEND chunk */ +void /* PRIVATE */ +png_write_IEND(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_IEND; +#endif + png_debug(1, "in png_write_IEND\n"); + png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL, + (png_size_t)0); + png_ptr->mode |= PNG_HAVE_IEND; +} + +#if defined(PNG_WRITE_gAMA_SUPPORTED) +/* write a gAMA chunk */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +void /* PRIVATE */ +png_write_gAMA(png_structp png_ptr, double file_gamma) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_gAMA; +#endif + png_uint_32 igamma; + png_byte buf[4]; + + png_debug(1, "in png_write_gAMA\n"); + /* file_gamma is saved in 1/100,000ths */ + igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5); + png_save_uint_32(buf, igamma); + png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); +} +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +void /* PRIVATE */ +png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_gAMA; +#endif + png_byte buf[4]; + + png_debug(1, "in png_write_gAMA\n"); + /* file_gamma is saved in 1/100,000ths */ + png_save_uint_32(buf, (png_uint_32)file_gamma); + png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); +} +#endif +#endif + +#if defined(PNG_WRITE_sRGB_SUPPORTED) +/* write a sRGB chunk */ +void /* PRIVATE */ +png_write_sRGB(png_structp png_ptr, int srgb_intent) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_sRGB; +#endif + png_byte buf[1]; + + png_debug(1, "in png_write_sRGB\n"); + if(srgb_intent >= PNG_sRGB_INTENT_LAST) + png_warning(png_ptr, + "Invalid sRGB rendering intent specified"); + buf[0]=(png_byte)srgb_intent; + png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1); +} +#endif + +#if defined(PNG_WRITE_iCCP_SUPPORTED) +/* write an iCCP chunk */ +void /* PRIVATE */ +png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type, + png_charp profile, int profile_len) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_iCCP; +#endif + png_size_t name_len; + png_charp new_name; + compression_state comp; + + png_debug(1, "in png_write_iCCP\n"); + if (name == NULL || (name_len = png_check_keyword(png_ptr, name, + &new_name)) == 0) + { + png_warning(png_ptr, "Empty keyword in iCCP chunk"); + return; + } + + if (compression_type != PNG_COMPRESSION_TYPE_BASE) + png_warning(png_ptr, "Unknown compression type in iCCP chunk"); + + if (profile == NULL) + profile_len = 0; + + if (profile_len) + profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len, + PNG_COMPRESSION_TYPE_BASE, &comp); + + /* make sure we include the NULL after the name and the compression type */ + png_write_chunk_start(png_ptr, (png_bytep)png_iCCP, + (png_uint_32)name_len+profile_len+2); + new_name[name_len+1]=0x00; + png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2); + + if (profile_len) + png_write_compressed_data_out(png_ptr, &comp); + + png_write_chunk_end(png_ptr); + png_free(png_ptr, new_name); +} +#endif + +#if defined(PNG_WRITE_sPLT_SUPPORTED) +/* write a sPLT chunk */ +void /* PRIVATE */ +png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_sPLT; +#endif + png_size_t name_len; + png_charp new_name; + png_byte entrybuf[10]; + int entry_size = (spalette->depth == 8 ? 6 : 10); + int palette_size = entry_size * spalette->nentries; + png_sPLT_entryp ep; +#ifdef PNG_NO_POINTER_INDEXING + int i; +#endif + + png_debug(1, "in png_write_sPLT\n"); + if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr, + spalette->name, &new_name))==0) + { + png_warning(png_ptr, "Empty keyword in sPLT chunk"); + return; + } + + /* make sure we include the NULL after the name */ + png_write_chunk_start(png_ptr, (png_bytep)png_sPLT, + (png_uint_32)(name_len + 2 + palette_size)); + png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1); + png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1); + + /* loop through each palette entry, writing appropriately */ +#ifndef PNG_NO_POINTER_INDEXING + for (ep = spalette->entries; epentries+spalette->nentries; ep++) + { + if (spalette->depth == 8) + { + entrybuf[0] = (png_byte)ep->red; + entrybuf[1] = (png_byte)ep->green; + entrybuf[2] = (png_byte)ep->blue; + entrybuf[3] = (png_byte)ep->alpha; + png_save_uint_16(entrybuf + 4, ep->frequency); + } + else + { + png_save_uint_16(entrybuf + 0, ep->red); + png_save_uint_16(entrybuf + 2, ep->green); + png_save_uint_16(entrybuf + 4, ep->blue); + png_save_uint_16(entrybuf + 6, ep->alpha); + png_save_uint_16(entrybuf + 8, ep->frequency); + } + png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); + } +#else + ep=spalette->entries; + for (i=0; i>spalette->nentries; i++) + { + if (spalette->depth == 8) + { + entrybuf[0] = (png_byte)ep[i].red; + entrybuf[1] = (png_byte)ep[i].green; + entrybuf[2] = (png_byte)ep[i].blue; + entrybuf[3] = (png_byte)ep[i].alpha; + png_save_uint_16(entrybuf + 4, ep[i].frequency); + } + else + { + png_save_uint_16(entrybuf + 0, ep[i].red); + png_save_uint_16(entrybuf + 2, ep[i].green); + png_save_uint_16(entrybuf + 4, ep[i].blue); + png_save_uint_16(entrybuf + 6, ep[i].alpha); + png_save_uint_16(entrybuf + 8, ep[i].frequency); + } + png_write_chunk_data(png_ptr, entrybuf, entry_size); + } +#endif + + png_write_chunk_end(png_ptr); + png_free(png_ptr, new_name); +} +#endif + +#if defined(PNG_WRITE_sBIT_SUPPORTED) +/* write the sBIT chunk */ +void /* PRIVATE */ +png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_sBIT; +#endif + png_byte buf[4]; + png_size_t size; + + png_debug(1, "in png_write_sBIT\n"); + /* make sure we don't depend upon the order of PNG_COLOR_8 */ + if (color_type & PNG_COLOR_MASK_COLOR) + { + png_byte maxbits; + + maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : + png_ptr->usr_bit_depth); + if (sbit->red == 0 || sbit->red > maxbits || + sbit->green == 0 || sbit->green > maxbits || + sbit->blue == 0 || sbit->blue > maxbits) + { + png_warning(png_ptr, "Invalid sBIT depth specified"); + return; + } + buf[0] = sbit->red; + buf[1] = sbit->green; + buf[2] = sbit->blue; + size = 3; + } + else + { + if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) + { + png_warning(png_ptr, "Invalid sBIT depth specified"); + return; + } + buf[0] = sbit->gray; + size = 1; + } + + if (color_type & PNG_COLOR_MASK_ALPHA) + { + if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) + { + png_warning(png_ptr, "Invalid sBIT depth specified"); + return; + } + buf[size++] = sbit->alpha; + } + + png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size); +} +#endif + +#if defined(PNG_WRITE_cHRM_SUPPORTED) +/* write the cHRM chunk */ +#ifdef PNG_FLOATING_POINT_SUPPORTED +void /* PRIVATE */ +png_write_cHRM(png_structp png_ptr, double white_x, double white_y, + double red_x, double red_y, double green_x, double green_y, + double blue_x, double blue_y) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_cHRM; +#endif + png_byte buf[32]; + png_uint_32 itemp; + + png_debug(1, "in png_write_cHRM\n"); + /* each value is saved in 1/100,000ths */ + if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 || + white_x + white_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM white point specified"); +#if !defined(PNG_NO_CONSOLE_IO) + fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y); +#endif + return; + } + itemp = (png_uint_32)(white_x * 100000.0 + 0.5); + png_save_uint_32(buf, itemp); + itemp = (png_uint_32)(white_y * 100000.0 + 0.5); + png_save_uint_32(buf + 4, itemp); + + if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 || + red_x + red_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM red point specified"); + return; + } + itemp = (png_uint_32)(red_x * 100000.0 + 0.5); + png_save_uint_32(buf + 8, itemp); + itemp = (png_uint_32)(red_y * 100000.0 + 0.5); + png_save_uint_32(buf + 12, itemp); + + if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 || + green_x + green_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM green point specified"); + return; + } + itemp = (png_uint_32)(green_x * 100000.0 + 0.5); + png_save_uint_32(buf + 16, itemp); + itemp = (png_uint_32)(green_y * 100000.0 + 0.5); + png_save_uint_32(buf + 20, itemp); + + if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 || + blue_x + blue_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM blue point specified"); + return; + } + itemp = (png_uint_32)(blue_x * 100000.0 + 0.5); + png_save_uint_32(buf + 24, itemp); + itemp = (png_uint_32)(blue_y * 100000.0 + 0.5); + png_save_uint_32(buf + 28, itemp); + + png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); +} +#endif +#ifdef PNG_FIXED_POINT_SUPPORTED +void /* PRIVATE */ +png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, + png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, + png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, + png_fixed_point blue_y) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_cHRM; +#endif + png_byte buf[32]; + + png_debug(1, "in png_write_cHRM\n"); + /* each value is saved in 1/100,000ths */ + if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L) + { + png_warning(png_ptr, "Invalid fixed cHRM white point specified"); +#if !defined(PNG_NO_CONSOLE_IO) + fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y); +#endif + return; + } + png_save_uint_32(buf, (png_uint_32)white_x); + png_save_uint_32(buf + 4, (png_uint_32)white_y); + + if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L) + { + png_warning(png_ptr, "Invalid cHRM fixed red point specified"); + return; + } + png_save_uint_32(buf + 8, (png_uint_32)red_x); + png_save_uint_32(buf + 12, (png_uint_32)red_y); + + if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L) + { + png_warning(png_ptr, "Invalid fixed cHRM green point specified"); + return; + } + png_save_uint_32(buf + 16, (png_uint_32)green_x); + png_save_uint_32(buf + 20, (png_uint_32)green_y); + + if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L) + { + png_warning(png_ptr, "Invalid fixed cHRM blue point specified"); + return; + } + png_save_uint_32(buf + 24, (png_uint_32)blue_x); + png_save_uint_32(buf + 28, (png_uint_32)blue_y); + + png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); +} +#endif +#endif + +#if defined(PNG_WRITE_tRNS_SUPPORTED) +/* write the tRNS chunk */ +void /* PRIVATE */ +png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran, + int num_trans, int color_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_tRNS; +#endif + png_byte buf[6]; + + png_debug(1, "in png_write_tRNS\n"); + if (color_type == PNG_COLOR_TYPE_PALETTE) + { + if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) + { + png_warning(png_ptr,"Invalid number of transparent colors specified"); + return; + } + /* write the chunk out as it is */ + png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans); + } + else if (color_type == PNG_COLOR_TYPE_GRAY) + { + /* one 16 bit value */ + if(tran->gray >= (1 << png_ptr->bit_depth)) + { + png_warning(png_ptr, + "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); + return; + } + png_save_uint_16(buf, tran->gray); + png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2); + } + else if (color_type == PNG_COLOR_TYPE_RGB) + { + /* three 16 bit values */ + png_save_uint_16(buf, tran->red); + png_save_uint_16(buf + 2, tran->green); + png_save_uint_16(buf + 4, tran->blue); + if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) + { + png_warning(png_ptr, + "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); + return; + } + png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6); + } + else + { + png_warning(png_ptr, "Can't write tRNS with an alpha channel"); + } +} +#endif + +#if defined(PNG_WRITE_bKGD_SUPPORTED) +/* write the background chunk */ +void /* PRIVATE */ +png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_bKGD; +#endif + png_byte buf[6]; + + png_debug(1, "in png_write_bKGD\n"); + if (color_type == PNG_COLOR_TYPE_PALETTE) + { + if ( +#if defined(PNG_MNG_FEATURES_SUPPORTED) + (png_ptr->num_palette || + (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && +#endif + back->index > png_ptr->num_palette) + { + png_warning(png_ptr, "Invalid background palette index"); + return; + } + buf[0] = back->index; + png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1); + } + else if (color_type & PNG_COLOR_MASK_COLOR) + { + png_save_uint_16(buf, back->red); + png_save_uint_16(buf + 2, back->green); + png_save_uint_16(buf + 4, back->blue); + if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) + { + png_warning(png_ptr, + "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); + return; + } + png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6); + } + else + { + if(back->gray >= (1 << png_ptr->bit_depth)) + { + png_warning(png_ptr, + "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); + return; + } + png_save_uint_16(buf, back->gray); + png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2); + } +} +#endif + +#if defined(PNG_WRITE_hIST_SUPPORTED) +/* write the histogram */ +void /* PRIVATE */ +png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_hIST; +#endif + int i; + png_byte buf[3]; + + png_debug(1, "in png_write_hIST\n"); + if (num_hist > (int)png_ptr->num_palette) + { + png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist, + png_ptr->num_palette); + png_warning(png_ptr, "Invalid number of histogram entries specified"); + return; + } + + png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2)); + for (i = 0; i < num_hist; i++) + { + png_save_uint_16(buf, hist[i]); + png_write_chunk_data(png_ptr, buf, (png_size_t)2); + } + png_write_chunk_end(png_ptr); +} +#endif + +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ + defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) +/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, + * and if invalid, correct the keyword rather than discarding the entire + * chunk. The PNG 1.0 specification requires keywords 1-79 characters in + * length, forbids leading or trailing whitespace, multiple internal spaces, + * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. + * + * The new_key is allocated to hold the corrected keyword and must be freed + * by the calling routine. This avoids problems with trying to write to + * static keywords without having to have duplicate copies of the strings. + */ +png_size_t /* PRIVATE */ +png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key) +{ + png_size_t key_len; + png_charp kp, dp; + int kflag; + int kwarn=0; + + png_debug(1, "in png_check_keyword\n"); + *new_key = NULL; + + if (key == NULL || (key_len = png_strlen(key)) == 0) + { + png_warning(png_ptr, "zero length keyword"); + return ((png_size_t)0); + } + + png_debug1(2, "Keyword to be checked is '%s'\n", key); + + *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); + if (*new_key == NULL) + { + png_warning(png_ptr, "Out of memory while procesing keyword"); + return ((png_size_t)0); + } + + /* Replace non-printing characters with a blank and print a warning */ + for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++) + { + if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1)) + { +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) + char msg[40]; + + sprintf(msg, "invalid keyword character 0x%02X", *kp); + png_warning(png_ptr, msg); +#else + png_warning(png_ptr, "invalid character in keyword"); +#endif + *dp = ' '; + } + else + { + *dp = *kp; + } + } + *dp = '\0'; + + /* Remove any trailing white space. */ + kp = *new_key + key_len - 1; + if (*kp == ' ') + { + png_warning(png_ptr, "trailing spaces removed from keyword"); + + while (*kp == ' ') + { + *(kp--) = '\0'; + key_len--; + } + } + + /* Remove any leading white space. */ + kp = *new_key; + if (*kp == ' ') + { + png_warning(png_ptr, "leading spaces removed from keyword"); + + while (*kp == ' ') + { + kp++; + key_len--; + } + } + + png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp); + + /* Remove multiple internal spaces. */ + for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) + { + if (*kp == ' ' && kflag == 0) + { + *(dp++) = *kp; + kflag = 1; + } + else if (*kp == ' ') + { + key_len--; + kwarn=1; + } + else + { + *(dp++) = *kp; + kflag = 0; + } + } + *dp = '\0'; + if(kwarn) + png_warning(png_ptr, "extra interior spaces removed from keyword"); + + if (key_len == 0) + { + png_free(png_ptr, *new_key); + *new_key=NULL; + png_warning(png_ptr, "Zero length keyword"); + } + + if (key_len > 79) + { + png_warning(png_ptr, "keyword length must be 1 - 79 characters"); + new_key[79] = '\0'; + key_len = 79; + } + + return (key_len); +} +#endif + +#if defined(PNG_WRITE_tEXt_SUPPORTED) +/* write a tEXt chunk */ +void /* PRIVATE */ +png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text, + png_size_t text_len) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_tEXt; +#endif + png_size_t key_len; + png_charp new_key; + + png_debug(1, "in png_write_tEXt\n"); + if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) + { + png_warning(png_ptr, "Empty keyword in tEXt chunk"); + return; + } + + if (text == NULL || *text == '\0') + text_len = 0; + else + text_len = png_strlen(text); + + /* make sure we include the 0 after the key */ + png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1); + /* + * We leave it to the application to meet PNG-1.0 requirements on the + * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of + * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. + * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. + */ + png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1); + if (text_len) + png_write_chunk_data(png_ptr, (png_bytep)text, text_len); + + png_write_chunk_end(png_ptr); + png_free(png_ptr, new_key); +} +#endif + +#if defined(PNG_WRITE_zTXt_SUPPORTED) +/* write a compressed text chunk */ +void /* PRIVATE */ +png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, + png_size_t text_len, int compression) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_zTXt; +#endif + png_size_t key_len; + char buf[1]; + png_charp new_key; + compression_state comp; + + png_debug(1, "in png_write_zTXt\n"); + + if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) + { + png_warning(png_ptr, "Empty keyword in zTXt chunk"); + return; + } + + if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) + { + png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); + png_free(png_ptr, new_key); + return; + } + + text_len = png_strlen(text); + + png_free(png_ptr, new_key); + + /* compute the compressed data; do it now for the length */ + text_len = png_text_compress(png_ptr, text, text_len, compression, + &comp); + + /* write start of chunk */ + png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32) + (key_len+text_len+2)); + /* write key */ + png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1); + buf[0] = (png_byte)compression; + /* write compression */ + png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1); + /* write the compressed data */ + png_write_compressed_data_out(png_ptr, &comp); + + /* close the chunk */ + png_write_chunk_end(png_ptr); +} +#endif + +#if defined(PNG_WRITE_iTXt_SUPPORTED) +/* write an iTXt chunk */ +void /* PRIVATE */ +png_write_iTXt(png_structp png_ptr, int compression, png_charp key, + png_charp lang, png_charp lang_key, png_charp text) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_iTXt; +#endif + png_size_t lang_len, key_len, lang_key_len, text_len; + png_charp new_lang, new_key; + png_byte cbuf[2]; + compression_state comp; + + png_debug(1, "in png_write_iTXt\n"); + + if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) + { + png_warning(png_ptr, "Empty keyword in iTXt chunk"); + return; + } + if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0) + { + png_warning(png_ptr, "Empty language field in iTXt chunk"); + new_lang = NULL; + lang_len = 0; + } + + if (lang_key == NULL) + lang_key_len = 0; + else + lang_key_len = png_strlen(lang_key); + + if (text == NULL) + text_len = 0; + else + text_len = png_strlen(text); + + /* compute the compressed data; do it now for the length */ + text_len = png_text_compress(png_ptr, text, text_len, compression-2, + &comp); + + + /* make sure we include the compression flag, the compression byte, + * and the NULs after the key, lang, and lang_key parts */ + + png_write_chunk_start(png_ptr, (png_bytep)png_iTXt, + (png_uint_32)( + 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ + + key_len + + lang_len + + lang_key_len + + text_len)); + + /* + * We leave it to the application to meet PNG-1.0 requirements on the + * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of + * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. + * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. + */ + png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1); + + /* set the compression flag */ + if (compression == PNG_ITXT_COMPRESSION_NONE || \ + compression == PNG_TEXT_COMPRESSION_NONE) + cbuf[0] = 0; + else /* compression == PNG_ITXT_COMPRESSION_zTXt */ + cbuf[0] = 1; + /* set the compression method */ + cbuf[1] = 0; + png_write_chunk_data(png_ptr, cbuf, 2); + + cbuf[0] = 0; + png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1); + png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1); + png_write_compressed_data_out(png_ptr, &comp); + + png_write_chunk_end(png_ptr); + png_free(png_ptr, new_key); + if (new_lang) + png_free(png_ptr, new_lang); +} +#endif + +#if defined(PNG_WRITE_oFFs_SUPPORTED) +/* write the oFFs chunk */ +void /* PRIVATE */ +png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, + int unit_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_oFFs; +#endif + png_byte buf[9]; + + png_debug(1, "in png_write_oFFs\n"); + if (unit_type >= PNG_OFFSET_LAST) + png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); + + png_save_int_32(buf, x_offset); + png_save_int_32(buf + 4, y_offset); + buf[8] = (png_byte)unit_type; + + png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9); +} +#endif + +#if defined(PNG_WRITE_pCAL_SUPPORTED) +/* write the pCAL chunk (described in the PNG extensions document) */ +void /* PRIVATE */ +png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, + png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_pCAL; +#endif + png_size_t purpose_len, units_len, total_len; + png_uint_32p params_len; + png_byte buf[10]; + png_charp new_purpose; + int i; + + png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams); + if (type >= PNG_EQUATION_LAST) + png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); + + purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; + png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len); + units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); + png_debug1(3, "pCAL units length = %d\n", (int)units_len); + total_len = purpose_len + units_len + 10; + + params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams + *png_sizeof(png_uint_32))); + + /* Find the length of each parameter, making sure we don't count the + null terminator for the last parameter. */ + for (i = 0; i < nparams; i++) + { + params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); + png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]); + total_len += (png_size_t)params_len[i]; + } + + png_debug1(3, "pCAL total length = %d\n", (int)total_len); + png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len); + png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len); + png_save_int_32(buf, X0); + png_save_int_32(buf + 4, X1); + buf[8] = (png_byte)type; + buf[9] = (png_byte)nparams; + png_write_chunk_data(png_ptr, buf, (png_size_t)10); + png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len); + + png_free(png_ptr, new_purpose); + + for (i = 0; i < nparams; i++) + { + png_write_chunk_data(png_ptr, (png_bytep)params[i], + (png_size_t)params_len[i]); + } + + png_free(png_ptr, params_len); + png_write_chunk_end(png_ptr); +} +#endif + +#if defined(PNG_WRITE_sCAL_SUPPORTED) +/* write the sCAL chunk */ +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) +void /* PRIVATE */ +png_write_sCAL(png_structp png_ptr, int unit, double width,double height) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_sCAL; +#endif + png_size_t total_len; + char wbuf[32], hbuf[32]; + png_byte bunit = unit; + + png_debug(1, "in png_write_sCAL\n"); + +#if defined(_WIN32_WCE) +/* sprintf() function is not supported on WindowsCE */ + { + wchar_t wc_buf[32]; + swprintf(wc_buf, TEXT("%12.12e"), width); + WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL); + swprintf(wc_buf, TEXT("%12.12e"), height); + WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL); + } +#else + sprintf(wbuf, "%12.12e", width); + sprintf(hbuf, "%12.12e", height); +#endif + total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf); + + png_debug1(3, "sCAL total length = %d\n", (int)total_len); + png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len); + png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1); + png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1); + png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf)); + + png_write_chunk_end(png_ptr); +} +#else +#ifdef PNG_FIXED_POINT_SUPPORTED +void /* PRIVATE */ +png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width, + png_charp height) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_sCAL; +#endif + png_size_t total_len; + char wbuf[32], hbuf[32]; + png_byte bunit = unit; + + png_debug(1, "in png_write_sCAL_s\n"); + + png_strcpy(wbuf,(const char *)width); + png_strcpy(hbuf,(const char *)height); + total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf); + + png_debug1(3, "sCAL total length = %d\n", total_len); + png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len); + png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1); + png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1); + png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf)); + + png_write_chunk_end(png_ptr); +} +#endif +#endif +#endif + +#if defined(PNG_WRITE_pHYs_SUPPORTED) +/* write the pHYs chunk */ +void /* PRIVATE */ +png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, + png_uint_32 y_pixels_per_unit, + int unit_type) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_pHYs; +#endif + png_byte buf[9]; + + png_debug(1, "in png_write_pHYs\n"); + if (unit_type >= PNG_RESOLUTION_LAST) + png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); + + png_save_uint_32(buf, x_pixels_per_unit); + png_save_uint_32(buf + 4, y_pixels_per_unit); + buf[8] = (png_byte)unit_type; + + png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9); +} +#endif + +#if defined(PNG_WRITE_tIME_SUPPORTED) +/* Write the tIME chunk. Use either png_convert_from_struct_tm() + * or png_convert_from_time_t(), or fill in the structure yourself. + */ +void /* PRIVATE */ +png_write_tIME(png_structp png_ptr, png_timep mod_time) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + PNG_tIME; +#endif + png_byte buf[7]; + + png_debug(1, "in png_write_tIME\n"); + if (mod_time->month > 12 || mod_time->month < 1 || + mod_time->day > 31 || mod_time->day < 1 || + mod_time->hour > 23 || mod_time->second > 60) + { + png_warning(png_ptr, "Invalid time specified for tIME chunk"); + return; + } + + png_save_uint_16(buf, mod_time->year); + buf[2] = mod_time->month; + buf[3] = mod_time->day; + buf[4] = mod_time->hour; + buf[5] = mod_time->minute; + buf[6] = mod_time->second; + + png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7); +} +#endif + +/* initializes the row writing capability of libpng */ +void /* PRIVATE */ +png_write_start_row(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; + + /* start of interlace block in the y direction */ + int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; + + /* offset to next interlace block in the y direction */ + int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; +#endif + + png_size_t buf_size; + + png_debug(1, "in png_write_start_row\n"); + buf_size = (png_size_t)(PNG_ROWBYTES( + png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1); + + /* set up row buffer */ + png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size); + png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; + + /* set up filtering buffer, if using this filter */ + if (png_ptr->do_filter & PNG_FILTER_SUB) + { + png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; + } + + /* We only need to keep the previous row if we are using one of these. */ + if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) + { + /* set up previous row buffer */ + png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size); + png_memset(png_ptr->prev_row, 0, buf_size); + + if (png_ptr->do_filter & PNG_FILTER_UP) + { + png_ptr->up_row = (png_bytep )png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; + } + + if (png_ptr->do_filter & PNG_FILTER_AVG) + { + png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; + } + + if (png_ptr->do_filter & PNG_FILTER_PAETH) + { + png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr, + (png_ptr->rowbytes + 1)); + png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; + } + } + +#ifdef PNG_WRITE_INTERLACING_SUPPORTED + /* if interlaced, we need to set up width and height of pass */ + if (png_ptr->interlaced) + { + if (!(png_ptr->transformations & PNG_INTERLACE)) + { + png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - + png_pass_ystart[0]) / png_pass_yinc[0]; + png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - + png_pass_start[0]) / png_pass_inc[0]; + } + else + { + png_ptr->num_rows = png_ptr->height; + png_ptr->usr_width = png_ptr->width; + } + } + else +#endif + { + png_ptr->num_rows = png_ptr->height; + png_ptr->usr_width = png_ptr->width; + } + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; +} + +/* Internal use only. Called when finished processing a row of data. */ +void /* PRIVATE */ +png_write_finish_row(png_structp png_ptr) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; + + /* start of interlace block in the y direction */ + int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; + + /* offset to next interlace block in the y direction */ + int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; +#endif + + int ret; + + png_debug(1, "in png_write_finish_row\n"); + /* next row */ + png_ptr->row_number++; + + /* see if we are done */ + if (png_ptr->row_number < png_ptr->num_rows) + return; + +#ifdef PNG_WRITE_INTERLACING_SUPPORTED + /* if interlaced, go to next pass */ + if (png_ptr->interlaced) + { + png_ptr->row_number = 0; + if (png_ptr->transformations & PNG_INTERLACE) + { + png_ptr->pass++; + } + else + { + /* loop until we find a non-zero width or height pass */ + do + { + png_ptr->pass++; + if (png_ptr->pass >= 7) + break; + png_ptr->usr_width = (png_ptr->width + + png_pass_inc[png_ptr->pass] - 1 - + png_pass_start[png_ptr->pass]) / + png_pass_inc[png_ptr->pass]; + png_ptr->num_rows = (png_ptr->height + + png_pass_yinc[png_ptr->pass] - 1 - + png_pass_ystart[png_ptr->pass]) / + png_pass_yinc[png_ptr->pass]; + if (png_ptr->transformations & PNG_INTERLACE) + break; + } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); + + } + + /* reset the row above the image for the next pass */ + if (png_ptr->pass < 7) + { + if (png_ptr->prev_row != NULL) + png_memset(png_ptr->prev_row, 0, + (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* + png_ptr->usr_bit_depth,png_ptr->width))+1); + return; + } + } +#endif + + /* if we get here, we've just written the last row, so we need + to flush the compressor */ + do + { + /* tell the compressor we are done */ + ret = deflate(&png_ptr->zstream, Z_FINISH); + /* check for an error */ + if (ret == Z_OK) + { + /* check to see if we need more room */ + if (!(png_ptr->zstream.avail_out)) + { + png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + } + } + else if (ret != Z_STREAM_END) + { + if (png_ptr->zstream.msg != NULL) + png_error(png_ptr, png_ptr->zstream.msg); + else + png_error(png_ptr, "zlib error"); + } + } while (ret != Z_STREAM_END); + + /* write any extra space */ + if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) + { + png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - + png_ptr->zstream.avail_out); + } + + deflateReset(&png_ptr->zstream); + png_ptr->zstream.data_type = Z_BINARY; +} + +#if defined(PNG_WRITE_INTERLACING_SUPPORTED) +/* Pick out the correct pixels for the interlace pass. + * The basic idea here is to go through the row with a source + * pointer and a destination pointer (sp and dp), and copy the + * correct pixels for the pass. As the row gets compacted, + * sp will always be >= dp, so we should never overwrite anything. + * See the default: case for the easiest code to understand. + */ +void /* PRIVATE */ +png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) +{ +#ifdef PNG_USE_LOCAL_ARRAYS + /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ + + /* start of interlace block */ + int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; + + /* offset to next interlace block */ + int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; +#endif + + png_debug(1, "in png_do_write_interlace\n"); + /* we don't have to do anything on the last pass (6) */ +#if defined(PNG_USELESS_TESTS_SUPPORTED) + if (row != NULL && row_info != NULL && pass < 6) +#else + if (pass < 6) +#endif + { + /* each pixel depth is handled separately */ + switch (row_info->pixel_depth) + { + case 1: + { + png_bytep sp; + png_bytep dp; + int shift; + int d; + int value; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + dp = row; + d = 0; + shift = 7; + for (i = png_pass_start[pass]; i < row_width; + i += png_pass_inc[pass]) + { + sp = row + (png_size_t)(i >> 3); + value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; + d |= (value << shift); + + if (shift == 0) + { + shift = 7; + *dp++ = (png_byte)d; + d = 0; + } + else + shift--; + + } + if (shift != 7) + *dp = (png_byte)d; + break; + } + case 2: + { + png_bytep sp; + png_bytep dp; + int shift; + int d; + int value; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + dp = row; + shift = 6; + d = 0; + for (i = png_pass_start[pass]; i < row_width; + i += png_pass_inc[pass]) + { + sp = row + (png_size_t)(i >> 2); + value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; + d |= (value << shift); + + if (shift == 0) + { + shift = 6; + *dp++ = (png_byte)d; + d = 0; + } + else + shift -= 2; + } + if (shift != 6) + *dp = (png_byte)d; + break; + } + case 4: + { + png_bytep sp; + png_bytep dp; + int shift; + int d; + int value; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + + dp = row; + shift = 4; + d = 0; + for (i = png_pass_start[pass]; i < row_width; + i += png_pass_inc[pass]) + { + sp = row + (png_size_t)(i >> 1); + value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; + d |= (value << shift); + + if (shift == 0) + { + shift = 4; + *dp++ = (png_byte)d; + d = 0; + } + else + shift -= 4; + } + if (shift != 4) + *dp = (png_byte)d; + break; + } + default: + { + png_bytep sp; + png_bytep dp; + png_uint_32 i; + png_uint_32 row_width = row_info->width; + png_size_t pixel_bytes; + + /* start at the beginning */ + dp = row; + /* find out how many bytes each pixel takes up */ + pixel_bytes = (row_info->pixel_depth >> 3); + /* loop through the row, only looking at the pixels that + matter */ + for (i = png_pass_start[pass]; i < row_width; + i += png_pass_inc[pass]) + { + /* find out where the original pixel is */ + sp = row + (png_size_t)i * pixel_bytes; + /* move the pixel */ + if (dp != sp) + png_memcpy(dp, sp, pixel_bytes); + /* next pixel */ + dp += pixel_bytes; + } + break; + } + } + /* set new row width */ + row_info->width = (row_info->width + + png_pass_inc[pass] - 1 - + png_pass_start[pass]) / + png_pass_inc[pass]; + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, + row_info->width); + } +} +#endif + +/* This filters the row, chooses which filter to use, if it has not already + * been specified by the application, and then writes the row out with the + * chosen filter. + */ +#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) +#define PNG_HISHIFT 10 +#define PNG_LOMASK ((png_uint_32)0xffffL) +#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) +void /* PRIVATE */ +png_write_find_filter(png_structp png_ptr, png_row_infop row_info) +{ + png_bytep prev_row, best_row, row_buf; + png_uint_32 mins, bpp; + png_byte filter_to_do = png_ptr->do_filter; + png_uint_32 row_bytes = row_info->rowbytes; +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + int num_p_filters = (int)png_ptr->num_prev_filters; +#endif + + png_debug(1, "in png_write_find_filter\n"); + /* find out how many bytes offset each pixel is */ + bpp = (row_info->pixel_depth + 7) >> 3; + + prev_row = png_ptr->prev_row; + best_row = row_buf = png_ptr->row_buf; + mins = PNG_MAXSUM; + + /* The prediction method we use is to find which method provides the + * smallest value when summing the absolute values of the distances + * from zero, using anything >= 128 as negative numbers. This is known + * as the "minimum sum of absolute differences" heuristic. Other + * heuristics are the "weighted minimum sum of absolute differences" + * (experimental and can in theory improve compression), and the "zlib + * predictive" method (not implemented yet), which does test compressions + * of lines using different filter methods, and then chooses the + * (series of) filter(s) that give minimum compressed data size (VERY + * computationally expensive). + * + * GRR 980525: consider also + * (1) minimum sum of absolute differences from running average (i.e., + * keep running sum of non-absolute differences & count of bytes) + * [track dispersion, too? restart average if dispersion too large?] + * (1b) minimum sum of absolute differences from sliding average, probably + * with window size <= deflate window (usually 32K) + * (2) minimum sum of squared differences from zero or running average + * (i.e., ~ root-mean-square approach) + */ + + + /* We don't need to test the 'no filter' case if this is the only filter + * that has been chosen, as it doesn't actually do anything to the data. + */ + if ((filter_to_do & PNG_FILTER_NONE) && + filter_to_do != PNG_FILTER_NONE) + { + png_bytep rp; + png_uint_32 sum = 0; + png_uint_32 i; + int v; + + for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) + { + v = *rp; + sum += (v < 128) ? v : 256 - v; + } + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + png_uint_32 sumhi, sumlo; + int j; + sumlo = sum & PNG_LOMASK; + sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ + + /* Reduce the sum if we match any of the previous rows */ + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) + { + sumlo = (sumlo * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + sumhi = (sumhi * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + /* Factor in the cost of this filter (this is here for completeness, + * but it makes no sense to have a "cost" for the NONE filter, as + * it has the minimum possible computational cost - none). + */ + sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> + PNG_COST_SHIFT; + sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> + PNG_COST_SHIFT; + + if (sumhi > PNG_HIMASK) + sum = PNG_MAXSUM; + else + sum = (sumhi << PNG_HISHIFT) + sumlo; + } +#endif + mins = sum; + } + + /* sub filter */ + if (filter_to_do == PNG_FILTER_SUB) + /* it's the only filter so no testing is needed */ + { + png_bytep rp, lp, dp; + png_uint_32 i; + for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; + i++, rp++, dp++) + { + *dp = *rp; + } + for (lp = row_buf + 1; i < row_bytes; + i++, rp++, lp++, dp++) + { + *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); + } + best_row = png_ptr->sub_row; + } + + else if (filter_to_do & PNG_FILTER_SUB) + { + png_bytep rp, dp, lp; + png_uint_32 sum = 0, lmins = mins; + png_uint_32 i; + int v; + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + /* We temporarily increase the "minimum sum" by the factor we + * would reduce the sum of this filter, so that we can do the + * early exit comparison without scaling the sum each time. + */ + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 lmhi, lmlo; + lmlo = lmins & PNG_LOMASK; + lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) + { + lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> + PNG_COST_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> + PNG_COST_SHIFT; + + if (lmhi > PNG_HIMASK) + lmins = PNG_MAXSUM; + else + lmins = (lmhi << PNG_HISHIFT) + lmlo; + } +#endif + + for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; + i++, rp++, dp++) + { + v = *dp = *rp; + + sum += (v < 128) ? v : 256 - v; + } + for (lp = row_buf + 1; i < row_bytes; + i++, rp++, lp++, dp++) + { + v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 sumhi, sumlo; + sumlo = sum & PNG_LOMASK; + sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) + { + sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> + PNG_COST_SHIFT; + sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> + PNG_COST_SHIFT; + + if (sumhi > PNG_HIMASK) + sum = PNG_MAXSUM; + else + sum = (sumhi << PNG_HISHIFT) + sumlo; + } +#endif + + if (sum < mins) + { + mins = sum; + best_row = png_ptr->sub_row; + } + } + + /* up filter */ + if (filter_to_do == PNG_FILTER_UP) + { + png_bytep rp, dp, pp; + png_uint_32 i; + + for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, + pp = prev_row + 1; i < row_bytes; + i++, rp++, pp++, dp++) + { + *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); + } + best_row = png_ptr->up_row; + } + + else if (filter_to_do & PNG_FILTER_UP) + { + png_bytep rp, dp, pp; + png_uint_32 sum = 0, lmins = mins; + png_uint_32 i; + int v; + + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 lmhi, lmlo; + lmlo = lmins & PNG_LOMASK; + lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) + { + lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> + PNG_COST_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> + PNG_COST_SHIFT; + + if (lmhi > PNG_HIMASK) + lmins = PNG_MAXSUM; + else + lmins = (lmhi << PNG_HISHIFT) + lmlo; + } +#endif + + for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, + pp = prev_row + 1; i < row_bytes; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 sumhi, sumlo; + sumlo = sum & PNG_LOMASK; + sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) + { + sumlo = (sumlo * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + sumhi = (sumhi * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> + PNG_COST_SHIFT; + sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> + PNG_COST_SHIFT; + + if (sumhi > PNG_HIMASK) + sum = PNG_MAXSUM; + else + sum = (sumhi << PNG_HISHIFT) + sumlo; + } +#endif + + if (sum < mins) + { + mins = sum; + best_row = png_ptr->up_row; + } + } + + /* avg filter */ + if (filter_to_do == PNG_FILTER_AVG) + { + png_bytep rp, dp, pp, lp; + png_uint_32 i; + for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, + pp = prev_row + 1; i < bpp; i++) + { + *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); + } + for (lp = row_buf + 1; i < row_bytes; i++) + { + *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) + & 0xff); + } + best_row = png_ptr->avg_row; + } + + else if (filter_to_do & PNG_FILTER_AVG) + { + png_bytep rp, dp, pp, lp; + png_uint_32 sum = 0, lmins = mins; + png_uint_32 i; + int v; + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 lmhi, lmlo; + lmlo = lmins & PNG_LOMASK; + lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) + { + lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> + PNG_COST_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> + PNG_COST_SHIFT; + + if (lmhi > PNG_HIMASK) + lmins = PNG_MAXSUM; + else + lmins = (lmhi << PNG_HISHIFT) + lmlo; + } +#endif + + for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, + pp = prev_row + 1; i < bpp; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); + + sum += (v < 128) ? v : 256 - v; + } + for (lp = row_buf + 1; i < row_bytes; i++) + { + v = *dp++ = + (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 sumhi, sumlo; + sumlo = sum & PNG_LOMASK; + sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) + { + sumlo = (sumlo * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + sumhi = (sumhi * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> + PNG_COST_SHIFT; + sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> + PNG_COST_SHIFT; + + if (sumhi > PNG_HIMASK) + sum = PNG_MAXSUM; + else + sum = (sumhi << PNG_HISHIFT) + sumlo; + } +#endif + + if (sum < mins) + { + mins = sum; + best_row = png_ptr->avg_row; + } + } + + /* Paeth filter */ + if (filter_to_do == PNG_FILTER_PAETH) + { + png_bytep rp, dp, pp, cp, lp; + png_uint_32 i; + for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, + pp = prev_row + 1; i < bpp; i++) + { + *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); + } + + for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) + { + int a, b, c, pa, pb, pc, p; + + b = *pp++; + c = *cp++; + a = *lp++; + + p = b - c; + pc = a - c; + +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif + + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; + + *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); + } + best_row = png_ptr->paeth_row; + } + + else if (filter_to_do & PNG_FILTER_PAETH) + { + png_bytep rp, dp, pp, cp, lp; + png_uint_32 sum = 0, lmins = mins; + png_uint_32 i; + int v; + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 lmhi, lmlo; + lmlo = lmins & PNG_LOMASK; + lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) + { + lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> + PNG_COST_SHIFT; + lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> + PNG_COST_SHIFT; + + if (lmhi > PNG_HIMASK) + lmins = PNG_MAXSUM; + else + lmins = (lmhi << PNG_HISHIFT) + lmlo; + } +#endif + + for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, + pp = prev_row + 1; i < bpp; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); + + sum += (v < 128) ? v : 256 - v; + } + + for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) + { + int a, b, c, pa, pb, pc, p; + + b = *pp++; + c = *cp++; + a = *lp++; + +#ifndef PNG_SLOW_PAETH + p = b - c; + pc = a - c; +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; +#else /* PNG_SLOW_PAETH */ + p = a + b - c; + pa = abs(p - a); + pb = abs(p - b); + pc = abs(p - c); + if (pa <= pb && pa <= pc) + p = a; + else if (pb <= pc) + p = b; + else + p = c; +#endif /* PNG_SLOW_PAETH */ + + v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) + { + int j; + png_uint_32 sumhi, sumlo; + sumlo = sum & PNG_LOMASK; + sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; + + for (j = 0; j < num_p_filters; j++) + { + if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) + { + sumlo = (sumlo * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + sumhi = (sumhi * png_ptr->filter_weights[j]) >> + PNG_WEIGHT_SHIFT; + } + } + + sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> + PNG_COST_SHIFT; + sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> + PNG_COST_SHIFT; + + if (sumhi > PNG_HIMASK) + sum = PNG_MAXSUM; + else + sum = (sumhi << PNG_HISHIFT) + sumlo; + } +#endif + + if (sum < mins) + { + best_row = png_ptr->paeth_row; + } + } + + /* Do the actual writing of the filtered row data from the chosen filter. */ + + png_write_filtered_row(png_ptr, best_row); + +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) + /* Save the type of filter we picked this time for future calculations */ + if (png_ptr->num_prev_filters > 0) + { + int j; + for (j = 1; j < num_p_filters; j++) + { + png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; + } + png_ptr->prev_filters[j] = best_row[0]; + } +#endif +} + + +/* Do the actual writing of a previously filtered row. */ +void /* PRIVATE */ +png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) +{ + png_debug(1, "in png_write_filtered_row\n"); + png_debug1(2, "filter = %d\n", filtered_row[0]); + /* set up the zlib input buffer */ + + png_ptr->zstream.next_in = filtered_row; + png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; + /* repeat until we have compressed all the data */ + do + { + int ret; /* return of zlib */ + + /* compress the data */ + ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); + /* check for compression errors */ + if (ret != Z_OK) + { + if (png_ptr->zstream.msg != NULL) + png_error(png_ptr, png_ptr->zstream.msg); + else + png_error(png_ptr, "zlib error"); + } + + /* see if it is time to write another IDAT */ + if (!(png_ptr->zstream.avail_out)) + { + /* write the IDAT and reset the zlib output buffer */ + png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + } + /* repeat until all data has been compressed */ + } while (png_ptr->zstream.avail_in); + + /* swap the current and previous rows */ + if (png_ptr->prev_row != NULL) + { + png_bytep tptr; + + tptr = png_ptr->prev_row; + png_ptr->prev_row = png_ptr->row_buf; + png_ptr->row_buf = tptr; + } + + /* finish row - updates counters and flushes zlib if last row */ + png_write_finish_row(png_ptr); + +#if defined(PNG_WRITE_FLUSH_SUPPORTED) + png_ptr->flush_rows++; + + if (png_ptr->flush_dist > 0 && + png_ptr->flush_rows >= png_ptr->flush_dist) + { + png_write_flush(png_ptr); + } +#endif +} +#endif /* PNG_WRITE_SUPPORTED */ diff --git a/libresample-0.1.3/LICENSE.txt b/libresample-0.1.3/LICENSE.txt new file mode 100644 index 00000000..4ccd6ccf --- /dev/null +++ b/libresample-0.1.3/LICENSE.txt @@ -0,0 +1,463 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations +below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control +compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply, and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License +may add an explicit geographical distribution limitation excluding those +countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + diff --git a/libresample-0.1.3/Makefile.in b/libresample-0.1.3/Makefile.in new file mode 100644 index 00000000..febf70aa --- /dev/null +++ b/libresample-0.1.3/Makefile.in @@ -0,0 +1,57 @@ +# Run configure to generate Makefile from Makefile.in on +# any system supported by GNU autoconf. For all other +# systems, use this file as a template to create a +# working Makefile. + +CC = @CC@ +CFLAGS = @CFLAGS@ -Wall + +LIBS = @LIBS@ -lm + +AR = @AR@ +RANLIB = @RANLIB@ +srcdir=@srcdir@ + +OBJS = \ + src/resample.c.o \ + src/resamplesubs.c.o \ + src/filterkit.c.o + +TARGETS = @TARGETS@ +DIRS=tests + +all: $(TARGETS) + +libresample.a: $(OBJS) Makefile + $(AR) ruv libresample.a $(OBJS) + ranlib libresample.a + +tests/testresample: libresample.a $(srcdir)/tests/testresample.c $(DIRS) + $(CC) -o tests/testresample \ + $(CFLAGS) $(srcdir)/tests/testresample.c \ + libresample.a $(LIBS) + +tests/compareresample: libresample.a $(srcdir)/tests/compareresample.c $(DIRS) + $(CC) -o tests/compareresample \ + $(CFLAGS) $(srcdir)/tests/compareresample.c \ + libresample.a -lsamplerate $(LIBS) + +tests/resample-sndfile: libresample.a $(srcdir)/tests/resample-sndfile.c $(DIRS) + $(CC) -o tests/resample-sndfile \ + $(CFLAGS) $(srcdir)/tests/resample-sndfile.c \ + libresample.a -lsndfile $(LIBS) + +$(DIRS): + mkdir $(DIRS) + +clean: + rm -f $(TARGETS) $(OBJS) + +dist: clean + rm -f Makefile + rm -f config.status config.cache config.log src/config.h + rm -f *~ src/*~ tests/*~ include/*~ + +$(OBJS): %.c.o: $(srcdir)/%.c Makefile $(srcdir)/include/libresample.h \ + $(srcdir)/src/resample_defs.h $(srcdir)/src/filterkit.h $(srcdir)/src/config.h + $(CC) -c $(CFLAGS) $< -o $@ diff --git a/libresample-0.1.3/README.txt b/libresample-0.1.3/README.txt new file mode 100644 index 00000000..14be45b8 --- /dev/null +++ b/libresample-0.1.3/README.txt @@ -0,0 +1,84 @@ +libresample + +Real-time library interface by Dominic Mazzoni + +Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + +License: LGPL - see the file LICENSE.txt for more information + +History: + +This library is not the highest-quality resampling library +available, nor is it the most flexible, nor is it the +fastest. But it is pretty good in all of these regards, and +it is quite portable. The best resampling library I am aware +of is libsamplerate by Erik de Castro Lopo. It's small, fast, +and very high quality. However, it uses the GPL for its +license (with commercial options available) and I needed +a more free library. So I wrote this library, using +the LGPL resample-1.7 library by Julius Smith as a basis. + +Resample-1.7 is a fixed-point resampler, and as a result +has only limited precision. I rewrote it to use single-precision +floating-point arithmetic instead and increased the number +of filter coefficients between time steps significantly. +On modern processors it can resample in real time even +with this extra overhead. + +Resample-1.7 was designed to read and write from files, so +I removed all of that code and replaced it with an API that +lets you pass samples in small chunks. It should be easy +to link to resample-1.7 as a library. + +Changes in version 0.1.3: + +* Fixed two bugs that were causing subtle problems + on Intel x86 processors due to differences in roundoff errors. + +* Prefixed most function names with lrs and changed header file + from resample.h to libresample.h, to avoid namespace + collisions with existing programs and libraries. + +* Added resample_dup (thanks to Glenn Maynard) + +* Argument to resample_get_filter_width takes a const void * + (thanks to Glenn Maynard) + +* resample-sndfile clips output to -1...1 (thanks to Glenn Maynard) + +Usage notes: + +- If the output buffer you pass is too small, resample_process + may not use any input samples because its internal output + buffer is too full to process any more. So do not assume + that it is an error just because no input samples were + consumed. Just keep passing valid output buffers. + +- Given a resampling factor f > 1, and a number of input + samples n, the number of output samples should be between + floor(n - f) and ceil(n + f). In other words, if you + resample 1000 samples at a factor of 8, the number of + output samples might be between 7992 and 8008. Do not + assume that it will be exactly 8000. If you need exactly + 8000 outputs, pad the input with extra zeros as necessary. + +License and warranty: + +All of the files in this package are Copyright 2003 by Dominic +Mazzoni . This library was based heavily +on Resample-1.7, Copyright 1994-2002 by Julius O. Smith III +, all rights reserved. + +Permission to use and copy is granted subject to the terms of the +"GNU Lesser General Public License" (LGPL) as published by the +Free Software Foundation; either version 2.1 of the License, +or any later version. In addition, Julius O. Smith III requests +that a copy of any modified files be sent by email to +jos@ccrma.stanford.edu so that he may incorporate them into the +CCRMA version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. diff --git a/libresample-0.1.3/config.guess b/libresample-0.1.3/config.guess new file mode 100644 index 00000000..297e5c30 --- /dev/null +++ b/libresample-0.1.3/config.guess @@ -0,0 +1,1308 @@ +#! /bin/sh +# Attempt to guess a canonical system name. +# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. + +timestamp='2001-10-05' + +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that program. + +# Originally written by Per Bothner . +# Please send patches to . Submit a context +# diff and a properly formatted ChangeLog entry. +# +# This script attempts to guess a canonical system name similar to +# config.sub. If it succeeds, it prints the system name on stdout, and +# exits with 0. Otherwise, it exits with 1. +# +# The plan is that this can be called by configure scripts if you +# don't specify an explicit build system type. + +me=`echo "$0" | sed -e 's,.*/,,'` + +usage="\ +Usage: $0 [OPTION] + +Output the configuration name of the system \`$me' is run on. + +Operation modes: + -h, --help print this help, then exit + -t, --time-stamp print date of last modification, then exit + -v, --version print version number, then exit + +Report bugs and patches to ." + +version="\ +GNU config.guess ($timestamp) + +Originally written by Per Bothner. +Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +Free Software Foundation, Inc. + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +help=" +Try \`$me --help' for more information." + +# Parse command line +while test $# -gt 0 ; do + case $1 in + --time-stamp | --time* | -t ) + echo "$timestamp" ; exit 0 ;; + --version | -v ) + echo "$version" ; exit 0 ;; + --help | --h* | -h ) + echo "$usage"; exit 0 ;; + -- ) # Stop option processing + shift; break ;; + - ) # Use stdin as input. + break ;; + -* ) + echo "$me: invalid option $1$help" >&2 + exit 1 ;; + * ) + break ;; + esac +done + +if test $# != 0; then + echo "$me: too many arguments$help" >&2 + exit 1 +fi + + +dummy=dummy-$$ +trap 'rm -f $dummy.c $dummy.o $dummy.rel $dummy; exit 1' 1 2 15 + +# CC_FOR_BUILD -- compiler used by this script. +# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still +# use `HOST_CC' if defined, but it is deprecated. + +set_cc_for_build='case $CC_FOR_BUILD,$HOST_CC,$CC in + ,,) echo "int dummy(){}" > $dummy.c ; + for c in cc gcc c89 ; do + ($c $dummy.c -c -o $dummy.o) >/dev/null 2>&1 ; + if test $? = 0 ; then + CC_FOR_BUILD="$c"; break ; + fi ; + done ; + rm -f $dummy.c $dummy.o $dummy.rel ; + if test x"$CC_FOR_BUILD" = x ; then + CC_FOR_BUILD=no_compiler_found ; + fi + ;; + ,,*) CC_FOR_BUILD=$CC ;; + ,*,*) CC_FOR_BUILD=$HOST_CC ;; +esac' + +# This is needed to find uname on a Pyramid OSx when run in the BSD universe. +# (ghazi@noc.rutgers.edu 1994-08-24) +if (test -f /.attbin/uname) >/dev/null 2>&1 ; then + PATH=$PATH:/.attbin ; export PATH +fi + +UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown +UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown +UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown +UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown + +# Note: order is significant - the case branches are not exclusive. + +case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in + *:NetBSD:*:*) + # NetBSD (nbsd) targets should (where applicable) match one or + # more of the tupples: *-*-netbsdelf*, *-*-netbsdaout*, + # *-*-netbsdecoff* and *-*-netbsd*. For targets that recently + # switched to ELF, *-*-netbsd* would select the old + # object file format. This provides both forward + # compatibility and a consistent mechanism for selecting the + # object file format. + # Determine the machine/vendor (is the vendor relevant). + case "${UNAME_MACHINE}" in + amiga) machine=m68k-unknown ;; + arm32) machine=arm-unknown ;; + atari*) machine=m68k-atari ;; + sun3*) machine=m68k-sun ;; + mac68k) machine=m68k-apple ;; + macppc) machine=powerpc-apple ;; + hp3[0-9][05]) machine=m68k-hp ;; + ibmrt|romp-ibm) machine=romp-ibm ;; + sparc*) machine=`uname -p`-unknown ;; + *) machine=${UNAME_MACHINE}-unknown ;; + esac + # The Operating System including object format, if it has switched + # to ELF recently, or will in the future. + case "${UNAME_MACHINE}" in + i386|sparc|amiga|arm*|hp300|mvme68k|vax|atari|luna68k|mac68k|news68k|next68k|pc532|sun3*|x68k) + eval $set_cc_for_build + if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep __ELF__ >/dev/null + then + # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout). + # Return netbsd for either. FIX? + os=netbsd + else + os=netbsdelf + fi + ;; + *) + os=netbsd + ;; + esac + # The OS release + release=`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'` + # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM: + # contains redundant information, the shorter form: + # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used. + echo "${machine}-${os}${release}" + exit 0 ;; + amiga:OpenBSD:*:*) + echo m68k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + arc:OpenBSD:*:*) + echo mipsel-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + hp300:OpenBSD:*:*) + echo m68k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + mac68k:OpenBSD:*:*) + echo m68k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + macppc:OpenBSD:*:*) + echo powerpc-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + mvme68k:OpenBSD:*:*) + echo m68k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + mvme88k:OpenBSD:*:*) + echo m88k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + mvmeppc:OpenBSD:*:*) + echo powerpc-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + pmax:OpenBSD:*:*) + echo mipsel-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + sgi:OpenBSD:*:*) + echo mipseb-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + sun3:OpenBSD:*:*) + echo m68k-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + wgrisc:OpenBSD:*:*) + echo mipsel-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + *:OpenBSD:*:*) + echo ${UNAME_MACHINE}-unknown-openbsd${UNAME_RELEASE} + exit 0 ;; + alpha:OSF1:*:*) + if test $UNAME_RELEASE = "V4.0"; then + UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'` + fi + # A Vn.n version is a released version. + # A Tn.n version is a released field test version. + # A Xn.n version is an unreleased experimental baselevel. + # 1.2 uses "1.2" for uname -r. + cat <$dummy.s + .data +\$Lformat: + .byte 37,100,45,37,120,10,0 # "%d-%x\n" + + .text + .globl main + .align 4 + .ent main +main: + .frame \$30,16,\$26,0 + ldgp \$29,0(\$27) + .prologue 1 + .long 0x47e03d80 # implver \$0 + lda \$2,-1 + .long 0x47e20c21 # amask \$2,\$1 + lda \$16,\$Lformat + mov \$0,\$17 + not \$1,\$18 + jsr \$26,printf + ldgp \$29,0(\$26) + mov 0,\$16 + jsr \$26,exit + .end main +EOF + eval $set_cc_for_build + $CC_FOR_BUILD $dummy.s -o $dummy 2>/dev/null + if test "$?" = 0 ; then + case `./$dummy` in + 0-0) + UNAME_MACHINE="alpha" + ;; + 1-0) + UNAME_MACHINE="alphaev5" + ;; + 1-1) + UNAME_MACHINE="alphaev56" + ;; + 1-101) + UNAME_MACHINE="alphapca56" + ;; + 2-303) + UNAME_MACHINE="alphaev6" + ;; + 2-307) + UNAME_MACHINE="alphaev67" + ;; + 2-1307) + UNAME_MACHINE="alphaev68" + ;; + esac + fi + rm -f $dummy.s $dummy + echo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[VTX]//' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'` + exit 0 ;; + Alpha\ *:Windows_NT*:*) + # How do we know it's Interix rather than the generic POSIX subsystem? + # Should we change UNAME_MACHINE based on the output of uname instead + # of the specific Alpha model? + echo alpha-pc-interix + exit 0 ;; + 21064:Windows_NT:50:3) + echo alpha-dec-winnt3.5 + exit 0 ;; + Amiga*:UNIX_System_V:4.0:*) + echo m68k-unknown-sysv4 + exit 0;; + *:[Aa]miga[Oo][Ss]:*:*) + echo ${UNAME_MACHINE}-unknown-amigaos + exit 0 ;; + *:OS/390:*:*) + echo i370-ibm-openedition + exit 0 ;; + arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*) + echo arm-acorn-riscix${UNAME_RELEASE} + exit 0;; + SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*) + echo hppa1.1-hitachi-hiuxmpp + exit 0;; + Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*) + # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE. + if test "`(/bin/universe) 2>/dev/null`" = att ; then + echo pyramid-pyramid-sysv3 + else + echo pyramid-pyramid-bsd + fi + exit 0 ;; + NILE*:*:*:dcosx) + echo pyramid-pyramid-svr4 + exit 0 ;; + sun4H:SunOS:5.*:*) + echo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit 0 ;; + sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*) + echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit 0 ;; + i86pc:SunOS:5.*:*) + echo i386-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit 0 ;; + sun4*:SunOS:6*:*) + # According to config.sub, this is the proper way to canonicalize + # SunOS6. Hard to guess exactly what SunOS6 will be like, but + # it's likely to be more like Solaris than SunOS4. + echo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit 0 ;; + sun4*:SunOS:*:*) + case "`/usr/bin/arch -k`" in + Series*|S4*) + UNAME_RELEASE=`uname -v` + ;; + esac + # Japanese Language versions have a version number like `4.1.3-JL'. + echo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'` + exit 0 ;; + sun3*:SunOS:*:*) + echo m68k-sun-sunos${UNAME_RELEASE} + exit 0 ;; + sun*:*:4.2BSD:*) + UNAME_RELEASE=`(head -1 /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null` + test "x${UNAME_RELEASE}" = "x" && UNAME_RELEASE=3 + case "`/bin/arch`" in + sun3) + echo m68k-sun-sunos${UNAME_RELEASE} + ;; + sun4) + echo sparc-sun-sunos${UNAME_RELEASE} + ;; + esac + exit 0 ;; + aushp:SunOS:*:*) + echo sparc-auspex-sunos${UNAME_RELEASE} + exit 0 ;; + # The situation for MiNT is a little confusing. The machine name + # can be virtually everything (everything which is not + # "atarist" or "atariste" at least should have a processor + # > m68000). The system name ranges from "MiNT" over "FreeMiNT" + # to the lowercase version "mint" (or "freemint"). Finally + # the system name "TOS" denotes a system which is actually not + # MiNT. But MiNT is downward compatible to TOS, so this should + # be no problem. + atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit 0 ;; + atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit 0 ;; + *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit 0 ;; + milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*) + echo m68k-milan-mint${UNAME_RELEASE} + exit 0 ;; + hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*) + echo m68k-hades-mint${UNAME_RELEASE} + exit 0 ;; + *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*) + echo m68k-unknown-mint${UNAME_RELEASE} + exit 0 ;; + powerpc:machten:*:*) + echo powerpc-apple-machten${UNAME_RELEASE} + exit 0 ;; + RISC*:Mach:*:*) + echo mips-dec-mach_bsd4.3 + exit 0 ;; + RISC*:ULTRIX:*:*) + echo mips-dec-ultrix${UNAME_RELEASE} + exit 0 ;; + VAX*:ULTRIX*:*:*) + echo vax-dec-ultrix${UNAME_RELEASE} + exit 0 ;; + 2020:CLIX:*:* | 2430:CLIX:*:*) + echo clipper-intergraph-clix${UNAME_RELEASE} + exit 0 ;; + mips:*:*:UMIPS | mips:*:*:RISCos) + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c +#ifdef __cplusplus +#include /* for printf() prototype */ + int main (int argc, char *argv[]) { +#else + int main (argc, argv) int argc; char *argv[]; { +#endif + #if defined (host_mips) && defined (MIPSEB) + #if defined (SYSTYPE_SYSV) + printf ("mips-mips-riscos%ssysv\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_SVR4) + printf ("mips-mips-riscos%ssvr4\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD) + printf ("mips-mips-riscos%sbsd\n", argv[1]); exit (0); + #endif + #endif + exit (-1); + } +EOF + $CC_FOR_BUILD $dummy.c -o $dummy \ + && ./$dummy `echo "${UNAME_RELEASE}" | sed -n 's/\([0-9]*\).*/\1/p'` \ + && rm -f $dummy.c $dummy && exit 0 + rm -f $dummy.c $dummy + echo mips-mips-riscos${UNAME_RELEASE} + exit 0 ;; + Motorola:PowerMAX_OS:*:*) + echo powerpc-motorola-powermax + exit 0 ;; + Night_Hawk:Power_UNIX:*:*) + echo powerpc-harris-powerunix + exit 0 ;; + m88k:CX/UX:7*:*) + echo m88k-harris-cxux7 + exit 0 ;; + m88k:*:4*:R4*) + echo m88k-motorola-sysv4 + exit 0 ;; + m88k:*:3*:R3*) + echo m88k-motorola-sysv3 + exit 0 ;; + AViiON:dgux:*:*) + # DG/UX returns AViiON for all architectures + UNAME_PROCESSOR=`/usr/bin/uname -p` + if [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ] + then + if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \ + [ ${TARGET_BINARY_INTERFACE}x = x ] + then + echo m88k-dg-dgux${UNAME_RELEASE} + else + echo m88k-dg-dguxbcs${UNAME_RELEASE} + fi + else + echo i586-dg-dgux${UNAME_RELEASE} + fi + exit 0 ;; + M88*:DolphinOS:*:*) # DolphinOS (SVR3) + echo m88k-dolphin-sysv3 + exit 0 ;; + M88*:*:R3*:*) + # Delta 88k system running SVR3 + echo m88k-motorola-sysv3 + exit 0 ;; + XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3) + echo m88k-tektronix-sysv3 + exit 0 ;; + Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD) + echo m68k-tektronix-bsd + exit 0 ;; + *:IRIX*:*:*) + echo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'` + exit 0 ;; + ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX. + echo romp-ibm-aix # uname -m gives an 8 hex-code CPU id + exit 0 ;; # Note that: echo "'`uname -s`'" gives 'AIX ' + i*86:AIX:*:*) + echo i386-ibm-aix + exit 0 ;; + ia64:AIX:*:*) + if [ -x /usr/bin/oslevel ] ; then + IBM_REV=`/usr/bin/oslevel` + else + IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} + fi + echo ${UNAME_MACHINE}-ibm-aix${IBM_REV} + exit 0 ;; + *:AIX:2:3) + if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + #include + + main() + { + if (!__power_pc()) + exit(1); + puts("powerpc-ibm-aix3.2.5"); + exit(0); + } +EOF + $CC_FOR_BUILD $dummy.c -o $dummy && ./$dummy && rm -f $dummy.c $dummy && exit 0 + rm -f $dummy.c $dummy + echo rs6000-ibm-aix3.2.5 + elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then + echo rs6000-ibm-aix3.2.4 + else + echo rs6000-ibm-aix3.2 + fi + exit 0 ;; + *:AIX:*:[45]) + IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | head -1 | awk '{ print $1 }'` + if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then + IBM_ARCH=rs6000 + else + IBM_ARCH=powerpc + fi + if [ -x /usr/bin/oslevel ] ; then + IBM_REV=`/usr/bin/oslevel` + else + IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} + fi + echo ${IBM_ARCH}-ibm-aix${IBM_REV} + exit 0 ;; + *:AIX:*:*) + echo rs6000-ibm-aix + exit 0 ;; + ibmrt:4.4BSD:*|romp-ibm:BSD:*) + echo romp-ibm-bsd4.4 + exit 0 ;; + ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC BSD and + echo romp-ibm-bsd${UNAME_RELEASE} # 4.3 with uname added to + exit 0 ;; # report: romp-ibm BSD 4.3 + *:BOSX:*:*) + echo rs6000-bull-bosx + exit 0 ;; + DPX/2?00:B.O.S.:*:*) + echo m68k-bull-sysv3 + exit 0 ;; + 9000/[34]??:4.3bsd:1.*:*) + echo m68k-hp-bsd + exit 0 ;; + hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*) + echo m68k-hp-bsd4.4 + exit 0 ;; + 9000/[34678]??:HP-UX:*:*) + HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` + case "${UNAME_MACHINE}" in + 9000/31? ) HP_ARCH=m68000 ;; + 9000/[34]?? ) HP_ARCH=m68k ;; + 9000/[678][0-9][0-9]) + if [ -x /usr/bin/getconf ]; then + sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null` + sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null` + case "${sc_cpu_version}" in + 523) HP_ARCH="hppa1.0" ;; # CPU_PA_RISC1_0 + 528) HP_ARCH="hppa1.1" ;; # CPU_PA_RISC1_1 + 532) # CPU_PA_RISC2_0 + case "${sc_kernel_bits}" in + 32) HP_ARCH="hppa2.0n" ;; + 64) HP_ARCH="hppa2.0w" ;; + '') HP_ARCH="hppa2.0" ;; # HP-UX 10.20 + esac ;; + esac + fi + if [ "${HP_ARCH}" = "" ]; then + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + + #define _HPUX_SOURCE + #include + #include + + int main () + { + #if defined(_SC_KERNEL_BITS) + long bits = sysconf(_SC_KERNEL_BITS); + #endif + long cpu = sysconf (_SC_CPU_VERSION); + + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1"); break; + case CPU_PA_RISC2_0: + #if defined(_SC_KERNEL_BITS) + switch (bits) + { + case 64: puts ("hppa2.0w"); break; + case 32: puts ("hppa2.0n"); break; + default: puts ("hppa2.0"); break; + } break; + #else /* !defined(_SC_KERNEL_BITS) */ + puts ("hppa2.0"); break; + #endif + default: puts ("hppa1.0"); break; + } + exit (0); + } +EOF + (CCOPTS= $CC_FOR_BUILD $dummy.c -o $dummy 2>/dev/null) && HP_ARCH=`./$dummy` + if test -z "$HP_ARCH"; then HP_ARCH=hppa; fi + rm -f $dummy.c $dummy + fi ;; + esac + echo ${HP_ARCH}-hp-hpux${HPUX_REV} + exit 0 ;; + ia64:HP-UX:*:*) + HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` + echo ia64-hp-hpux${HPUX_REV} + exit 0 ;; + 3050*:HI-UX:*:*) + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + #include + int + main () + { + long cpu = sysconf (_SC_CPU_VERSION); + /* The order matters, because CPU_IS_HP_MC68K erroneously returns + true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct + results, however. */ + if (CPU_IS_PA_RISC (cpu)) + { + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break; + case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break; + default: puts ("hppa-hitachi-hiuxwe2"); break; + } + } + else if (CPU_IS_HP_MC68K (cpu)) + puts ("m68k-hitachi-hiuxwe2"); + else puts ("unknown-hitachi-hiuxwe2"); + exit (0); + } +EOF + $CC_FOR_BUILD $dummy.c -o $dummy && ./$dummy && rm -f $dummy.c $dummy && exit 0 + rm -f $dummy.c $dummy + echo unknown-hitachi-hiuxwe2 + exit 0 ;; + 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* ) + echo hppa1.1-hp-bsd + exit 0 ;; + 9000/8??:4.3bsd:*:*) + echo hppa1.0-hp-bsd + exit 0 ;; + *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*) + echo hppa1.0-hp-mpeix + exit 0 ;; + hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* ) + echo hppa1.1-hp-osf + exit 0 ;; + hp8??:OSF1:*:*) + echo hppa1.0-hp-osf + exit 0 ;; + i*86:OSF1:*:*) + if [ -x /usr/sbin/sysversion ] ; then + echo ${UNAME_MACHINE}-unknown-osf1mk + else + echo ${UNAME_MACHINE}-unknown-osf1 + fi + exit 0 ;; + parisc*:Lites*:*:*) + echo hppa1.1-hp-lites + exit 0 ;; + C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*) + echo c1-convex-bsd + exit 0 ;; + C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*) + if getsysinfo -f scalar_acc + then echo c32-convex-bsd + else echo c2-convex-bsd + fi + exit 0 ;; + C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*) + echo c34-convex-bsd + exit 0 ;; + C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*) + echo c38-convex-bsd + exit 0 ;; + C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*) + echo c4-convex-bsd + exit 0 ;; + CRAY*X-MP:*:*:*) + echo xmp-cray-unicos + exit 0 ;; + CRAY*Y-MP:*:*:*) + echo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY*[A-Z]90:*:*:*) + echo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \ + | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \ + -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \ + -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY*TS:*:*:*) + echo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY*T3D:*:*:*) + echo alpha-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY*T3E:*:*:*) + echo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY*SV1:*:*:*) + echo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit 0 ;; + CRAY-2:*:*:*) + echo cray2-cray-unicos + exit 0 ;; + F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*) + FUJITSU_PROC=`uname -m | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'` + FUJITSU_SYS=`uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\///'` + FUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'` + echo "${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" + exit 0 ;; + i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*) + echo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE} + exit 0 ;; + sparc*:BSD/OS:*:*) + echo sparc-unknown-bsdi${UNAME_RELEASE} + exit 0 ;; + *:BSD/OS:*:*) + echo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE} + exit 0 ;; + *:FreeBSD:*:*) + echo ${UNAME_MACHINE}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` + exit 0 ;; + i*:CYGWIN*:*) + echo ${UNAME_MACHINE}-pc-cygwin + exit 0 ;; + i*:MINGW*:*) + echo ${UNAME_MACHINE}-pc-mingw32 + exit 0 ;; + i*:PW*:*) + echo ${UNAME_MACHINE}-pc-pw32 + exit 0 ;; + i*:Windows_NT*:* | Pentium*:Windows_NT*:*) + # How do we know it's Interix rather than the generic POSIX subsystem? + # It also conflicts with pre-2.0 versions of AT&T UWIN. Should we + # UNAME_MACHINE based on the output of uname instead of i386? + echo i386-pc-interix + exit 0 ;; + i*:UWIN*:*) + echo ${UNAME_MACHINE}-pc-uwin + exit 0 ;; + p*:CYGWIN*:*) + echo powerpcle-unknown-cygwin + exit 0 ;; + prep*:SunOS:5.*:*) + echo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit 0 ;; + *:GNU:*:*) + echo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-gnu`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'` + exit 0 ;; + i*86:Minix:*:*) + echo ${UNAME_MACHINE}-pc-minix + exit 0 ;; + arm*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-gnu + exit 0 ;; + ia64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux + exit 0 ;; + m68*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-gnu + exit 0 ;; + mips:Linux:*:*) + case `sed -n '/^byte/s/^.*: \(.*\) endian/\1/p' < /proc/cpuinfo` in + big) echo mips-unknown-linux-gnu && exit 0 ;; + little) echo mipsel-unknown-linux-gnu && exit 0 ;; + esac + ;; + ppc:Linux:*:*) + echo powerpc-unknown-linux-gnu + exit 0 ;; + ppc64:Linux:*:*) + echo powerpc64-unknown-linux-gnu + exit 0 ;; + alpha:Linux:*:*) + case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in + EV5) UNAME_MACHINE=alphaev5 ;; + EV56) UNAME_MACHINE=alphaev56 ;; + PCA56) UNAME_MACHINE=alphapca56 ;; + PCA57) UNAME_MACHINE=alphapca56 ;; + EV6) UNAME_MACHINE=alphaev6 ;; + EV67) UNAME_MACHINE=alphaev67 ;; + EV68*) UNAME_MACHINE=alphaev68 ;; + esac + objdump --private-headers /bin/sh | grep ld.so.1 >/dev/null + if test "$?" = 0 ; then LIBC="libc1" ; else LIBC="" ; fi + echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC} + exit 0 ;; + parisc:Linux:*:* | hppa:Linux:*:*) + # Look for CPU level + case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in + PA7*) echo hppa1.1-unknown-linux-gnu ;; + PA8*) echo hppa2.0-unknown-linux-gnu ;; + *) echo hppa-unknown-linux-gnu ;; + esac + exit 0 ;; + parisc64:Linux:*:* | hppa64:Linux:*:*) + echo hppa64-unknown-linux-gnu + exit 0 ;; + s390:Linux:*:* | s390x:Linux:*:*) + echo ${UNAME_MACHINE}-ibm-linux + exit 0 ;; + sh*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-gnu + exit 0 ;; + sparc:Linux:*:* | sparc64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-gnu + exit 0 ;; + x86_64:Linux:*:*) + echo x86_64-unknown-linux-gnu + exit 0 ;; + i*86:Linux:*:*) + # The BFD linker knows what the default object file format is, so + # first see if it will tell us. cd to the root directory to prevent + # problems with other programs or directories called `ld' in the path. + ld_supported_targets=`cd /; ld --help 2>&1 \ + | sed -ne '/supported targets:/!d + s/[ ][ ]*/ /g + s/.*supported targets: *// + s/ .*// + p'` + case "$ld_supported_targets" in + elf32-i386) + TENTATIVE="${UNAME_MACHINE}-pc-linux-gnu" + ;; + a.out-i386-linux) + echo "${UNAME_MACHINE}-pc-linux-gnuaout" + exit 0 ;; + coff-i386) + echo "${UNAME_MACHINE}-pc-linux-gnucoff" + exit 0 ;; + "") + # Either a pre-BFD a.out linker (linux-gnuoldld) or + # one that does not give us useful --help. + echo "${UNAME_MACHINE}-pc-linux-gnuoldld" + exit 0 ;; + esac + # Determine whether the default compiler is a.out or elf + eval $set_cc_for_build + cat >$dummy.c < +#ifdef __cplusplus +#include /* for printf() prototype */ + int main (int argc, char *argv[]) { +#else + int main (argc, argv) int argc; char *argv[]; { +#endif +#ifdef __ELF__ +# ifdef __GLIBC__ +# if __GLIBC__ >= 2 + printf ("%s-pc-linux-gnu\n", argv[1]); +# else + printf ("%s-pc-linux-gnulibc1\n", argv[1]); +# endif +# else + printf ("%s-pc-linux-gnulibc1\n", argv[1]); +# endif +#else + printf ("%s-pc-linux-gnuaout\n", argv[1]); +#endif + return 0; +} +EOF + $CC_FOR_BUILD $dummy.c -o $dummy 2>/dev/null && ./$dummy "${UNAME_MACHINE}" && rm -f $dummy.c $dummy && exit 0 + rm -f $dummy.c $dummy + test x"${TENTATIVE}" != x && echo "${TENTATIVE}" && exit 0 + ;; + i*86:DYNIX/ptx:4*:*) + # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. + # earlier versions are messed up and put the nodename in both + # sysname and nodename. + echo i386-sequent-sysv4 + exit 0 ;; + i*86:UNIX_SV:4.2MP:2.*) + # Unixware is an offshoot of SVR4, but it has its own version + # number series starting with 2... + # I am not positive that other SVR4 systems won't match this, + # I just have to hope. -- rms. + # Use sysv4.2uw... so that sysv4* matches it. + echo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION} + exit 0 ;; + i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*) + UNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\/MP$//'` + if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then + echo ${UNAME_MACHINE}-univel-sysv${UNAME_REL} + else + echo ${UNAME_MACHINE}-pc-sysv${UNAME_REL} + fi + exit 0 ;; + i*86:*:5:[78]*) + case `/bin/uname -X | grep "^Machine"` in + *486*) UNAME_MACHINE=i486 ;; + *Pentium) UNAME_MACHINE=i586 ;; + *Pent*|*Celeron) UNAME_MACHINE=i686 ;; + esac + echo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION} + exit 0 ;; + i*86:*:3.2:*) + if test -f /usr/options/cb.name; then + UNAME_REL=`sed -n 's/.*Version //p' /dev/null >/dev/null ; then + UNAME_REL=`(/bin/uname -X|egrep Release|sed -e 's/.*= //')` + (/bin/uname -X|egrep i80486 >/dev/null) && UNAME_MACHINE=i486 + (/bin/uname -X|egrep '^Machine.*Pentium' >/dev/null) \ + && UNAME_MACHINE=i586 + (/bin/uname -X|egrep '^Machine.*Pent ?II' >/dev/null) \ + && UNAME_MACHINE=i686 + (/bin/uname -X|egrep '^Machine.*Pentium Pro' >/dev/null) \ + && UNAME_MACHINE=i686 + echo ${UNAME_MACHINE}-pc-sco$UNAME_REL + else + echo ${UNAME_MACHINE}-pc-sysv32 + fi + exit 0 ;; + i*86:*DOS:*:*) + echo ${UNAME_MACHINE}-pc-msdosdjgpp + exit 0 ;; + pc:*:*:*) + # Left here for compatibility: + # uname -m prints for DJGPP always 'pc', but it prints nothing about + # the processor, so we play safe by assuming i386. + echo i386-pc-msdosdjgpp + exit 0 ;; + Intel:Mach:3*:*) + echo i386-pc-mach3 + exit 0 ;; + paragon:*:*:*) + echo i860-intel-osf1 + exit 0 ;; + i860:*:4.*:*) # i860-SVR4 + if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then + echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4 + else # Add other i860-SVR4 vendors below as they are discovered. + echo i860-unknown-sysv${UNAME_RELEASE} # Unknown i860-SVR4 + fi + exit 0 ;; + mini*:CTIX:SYS*5:*) + # "miniframe" + echo m68010-convergent-sysv + exit 0 ;; + M68*:*:R3V[567]*:*) + test -r /sysV68 && echo 'm68k-motorola-sysv' && exit 0 ;; + 3[34]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0) + OS_REL='' + test -r /etc/.relid \ + && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && echo i486-ncr-sysv4.3${OS_REL} && exit 0 + /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ + && echo i586-ncr-sysv4.3${OS_REL} && exit 0 ;; + 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*) + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && echo i486-ncr-sysv4 && exit 0 ;; + m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*) + echo m68k-unknown-lynxos${UNAME_RELEASE} + exit 0 ;; + mc68030:UNIX_System_V:4.*:*) + echo m68k-atari-sysv4 + exit 0 ;; + i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.0*:*) + echo i386-unknown-lynxos${UNAME_RELEASE} + exit 0 ;; + TSUNAMI:LynxOS:2.*:*) + echo sparc-unknown-lynxos${UNAME_RELEASE} + exit 0 ;; + rs6000:LynxOS:2.*:*) + echo rs6000-unknown-lynxos${UNAME_RELEASE} + exit 0 ;; + PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.0*:*) + echo powerpc-unknown-lynxos${UNAME_RELEASE} + exit 0 ;; + SM[BE]S:UNIX_SV:*:*) + echo mips-dde-sysv${UNAME_RELEASE} + exit 0 ;; + RM*:ReliantUNIX-*:*:*) + echo mips-sni-sysv4 + exit 0 ;; + RM*:SINIX-*:*:*) + echo mips-sni-sysv4 + exit 0 ;; + *:SINIX-*:*:*) + if uname -p 2>/dev/null >/dev/null ; then + UNAME_MACHINE=`(uname -p) 2>/dev/null` + echo ${UNAME_MACHINE}-sni-sysv4 + else + echo ns32k-sni-sysv + fi + exit 0 ;; + PENTIUM:*:4.0*:*) # Unisys `ClearPath HMP IX 4000' SVR4/MP effort + # says + echo i586-unisys-sysv4 + exit 0 ;; + *:UNIX_System_V:4*:FTX*) + # From Gerald Hewes . + # How about differentiating between stratus architectures? -djm + echo hppa1.1-stratus-sysv4 + exit 0 ;; + *:*:*:FTX*) + # From seanf@swdc.stratus.com. + echo i860-stratus-sysv4 + exit 0 ;; + *:VOS:*:*) + # From Paul.Green@stratus.com. + echo hppa1.1-stratus-vos + exit 0 ;; + mc68*:A/UX:*:*) + echo m68k-apple-aux${UNAME_RELEASE} + exit 0 ;; + news*:NEWS-OS:6*:*) + echo mips-sony-newsos6 + exit 0 ;; + R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*) + if [ -d /usr/nec ]; then + echo mips-nec-sysv${UNAME_RELEASE} + else + echo mips-unknown-sysv${UNAME_RELEASE} + fi + exit 0 ;; + BeBox:BeOS:*:*) # BeOS running on hardware made by Be, PPC only. + echo powerpc-be-beos + exit 0 ;; + BeMac:BeOS:*:*) # BeOS running on Mac or Mac clone, PPC only. + echo powerpc-apple-beos + exit 0 ;; + BePC:BeOS:*:*) # BeOS running on Intel PC compatible. + echo i586-pc-beos + exit 0 ;; + SX-4:SUPER-UX:*:*) + echo sx4-nec-superux${UNAME_RELEASE} + exit 0 ;; + SX-5:SUPER-UX:*:*) + echo sx5-nec-superux${UNAME_RELEASE} + exit 0 ;; + Power*:Rhapsody:*:*) + echo powerpc-apple-rhapsody${UNAME_RELEASE} + exit 0 ;; + *:Rhapsody:*:*) + echo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE} + exit 0 ;; + *:Darwin:*:*) + echo `uname -p`-apple-darwin${UNAME_RELEASE} + exit 0 ;; + *:procnto*:*:* | *:QNX:[0123456789]*:*) + if test "${UNAME_MACHINE}" = "x86pc"; then + UNAME_MACHINE=pc + fi + echo `uname -p`-${UNAME_MACHINE}-nto-qnx + exit 0 ;; + *:QNX:*:4*) + echo i386-pc-qnx + exit 0 ;; + NSR-[KW]:NONSTOP_KERNEL:*:*) + echo nsr-tandem-nsk${UNAME_RELEASE} + exit 0 ;; + *:NonStop-UX:*:*) + echo mips-compaq-nonstopux + exit 0 ;; + BS2000:POSIX*:*:*) + echo bs2000-siemens-sysv + exit 0 ;; + DS/*:UNIX_System_V:*:*) + echo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE} + exit 0 ;; + *:Plan9:*:*) + # "uname -m" is not consistent, so use $cputype instead. 386 + # is converted to i386 for consistency with other x86 + # operating systems. + if test "$cputype" = "386"; then + UNAME_MACHINE=i386 + else + UNAME_MACHINE="$cputype" + fi + echo ${UNAME_MACHINE}-unknown-plan9 + exit 0 ;; + i*86:OS/2:*:*) + # If we were able to find `uname', then EMX Unix compatibility + # is probably installed. + echo ${UNAME_MACHINE}-pc-os2-emx + exit 0 ;; + *:TOPS-10:*:*) + echo pdp10-unknown-tops10 + exit 0 ;; + *:TENEX:*:*) + echo pdp10-unknown-tenex + exit 0 ;; + KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*) + echo pdp10-dec-tops20 + exit 0 ;; + XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*) + echo pdp10-xkl-tops20 + exit 0 ;; + *:TOPS-20:*:*) + echo pdp10-unknown-tops20 + exit 0 ;; + *:ITS:*:*) + echo pdp10-unknown-its + exit 0 ;; + i*86:XTS-300:*:STOP) + echo ${UNAME_MACHINE}-unknown-stop + exit 0 ;; + i*86:atheos:*:*) + echo ${UNAME_MACHINE}-unknown-atheos + exit 0 ;; +esac + +#echo '(No uname command or uname output not recognized.)' 1>&2 +#echo "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" 1>&2 + +eval $set_cc_for_build +cat >$dummy.c < +# include +#endif +main () +{ +#if defined (sony) +#if defined (MIPSEB) + /* BFD wants "bsd" instead of "newsos". Perhaps BFD should be changed, + I don't know.... */ + printf ("mips-sony-bsd\n"); exit (0); +#else +#include + printf ("m68k-sony-newsos%s\n", +#ifdef NEWSOS4 + "4" +#else + "" +#endif + ); exit (0); +#endif +#endif + +#if defined (__arm) && defined (__acorn) && defined (__unix) + printf ("arm-acorn-riscix"); exit (0); +#endif + +#if defined (hp300) && !defined (hpux) + printf ("m68k-hp-bsd\n"); exit (0); +#endif + +#if defined (NeXT) +#if !defined (__ARCHITECTURE__) +#define __ARCHITECTURE__ "m68k" +#endif + int version; + version=`(hostinfo | sed -n 's/.*NeXT Mach \([0-9]*\).*/\1/p') 2>/dev/null`; + if (version < 4) + printf ("%s-next-nextstep%d\n", __ARCHITECTURE__, version); + else + printf ("%s-next-openstep%d\n", __ARCHITECTURE__, version); + exit (0); +#endif + +#if defined (MULTIMAX) || defined (n16) +#if defined (UMAXV) + printf ("ns32k-encore-sysv\n"); exit (0); +#else +#if defined (CMU) + printf ("ns32k-encore-mach\n"); exit (0); +#else + printf ("ns32k-encore-bsd\n"); exit (0); +#endif +#endif +#endif + +#if defined (__386BSD__) + printf ("i386-pc-bsd\n"); exit (0); +#endif + +#if defined (sequent) +#if defined (i386) + printf ("i386-sequent-dynix\n"); exit (0); +#endif +#if defined (ns32000) + printf ("ns32k-sequent-dynix\n"); exit (0); +#endif +#endif + +#if defined (_SEQUENT_) + struct utsname un; + + uname(&un); + + if (strncmp(un.version, "V2", 2) == 0) { + printf ("i386-sequent-ptx2\n"); exit (0); + } + if (strncmp(un.version, "V1", 2) == 0) { /* XXX is V1 correct? */ + printf ("i386-sequent-ptx1\n"); exit (0); + } + printf ("i386-sequent-ptx\n"); exit (0); + +#endif + +#if defined (vax) +# if !defined (ultrix) +# include +# if defined (BSD) +# if BSD == 43 + printf ("vax-dec-bsd4.3\n"); exit (0); +# else +# if BSD == 199006 + printf ("vax-dec-bsd4.3reno\n"); exit (0); +# else + printf ("vax-dec-bsd\n"); exit (0); +# endif +# endif +# else + printf ("vax-dec-bsd\n"); exit (0); +# endif +# else + printf ("vax-dec-ultrix\n"); exit (0); +# endif +#endif + +#if defined (alliant) && defined (i860) + printf ("i860-alliant-bsd\n"); exit (0); +#endif + + exit (1); +} +EOF + +$CC_FOR_BUILD $dummy.c -o $dummy 2>/dev/null && ./$dummy && rm -f $dummy.c $dummy && exit 0 +rm -f $dummy.c $dummy + +# Apollos put the system type in the environment. + +test -d /usr/apollo && { echo ${ISP}-apollo-${SYSTYPE}; exit 0; } + +# Convex versions that predate uname can use getsysinfo(1) + +if [ -x /usr/convex/getsysinfo ] +then + case `getsysinfo -f cpu_type` in + c1*) + echo c1-convex-bsd + exit 0 ;; + c2*) + if getsysinfo -f scalar_acc + then echo c32-convex-bsd + else echo c2-convex-bsd + fi + exit 0 ;; + c34*) + echo c34-convex-bsd + exit 0 ;; + c38*) + echo c38-convex-bsd + exit 0 ;; + c4*) + echo c4-convex-bsd + exit 0 ;; + esac +fi + +cat >&2 < in order to provide the needed +information to handle your system. + +config.guess timestamp = $timestamp + +uname -m = `(uname -m) 2>/dev/null || echo unknown` +uname -r = `(uname -r) 2>/dev/null || echo unknown` +uname -s = `(uname -s) 2>/dev/null || echo unknown` +uname -v = `(uname -v) 2>/dev/null || echo unknown` + +/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null` +/bin/uname -X = `(/bin/uname -X) 2>/dev/null` + +hostinfo = `(hostinfo) 2>/dev/null` +/bin/universe = `(/bin/universe) 2>/dev/null` +/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null` +/bin/arch = `(/bin/arch) 2>/dev/null` +/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null` +/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null` + +UNAME_MACHINE = ${UNAME_MACHINE} +UNAME_RELEASE = ${UNAME_RELEASE} +UNAME_SYSTEM = ${UNAME_SYSTEM} +UNAME_VERSION = ${UNAME_VERSION} +EOF + +exit 1 + +# Local variables: +# eval: (add-hook 'write-file-hooks 'time-stamp) +# time-stamp-start: "timestamp='" +# time-stamp-format: "%:y-%02m-%02d" +# time-stamp-end: "'" +# End: diff --git a/libresample-0.1.3/config.sub b/libresample-0.1.3/config.sub new file mode 100644 index 00000000..791bcded --- /dev/null +++ b/libresample-0.1.3/config.sub @@ -0,0 +1,1413 @@ +#! /bin/sh +# Configuration validation subroutine script. +# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. + +timestamp='2001-10-05' + +# This file is (in principle) common to ALL GNU software. +# The presence of a machine in this file suggests that SOME GNU software +# can handle that machine. It does not imply ALL GNU software can. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that program. + +# Please send patches to . Submit a context +# diff and a properly formatted ChangeLog entry. +# +# Configuration subroutine to validate and canonicalize a configuration type. +# Supply the specified configuration type as an argument. +# If it is invalid, we print an error message on stderr and exit with code 1. +# Otherwise, we print the canonical config type on stdout and succeed. + +# This file is supposed to be the same for all GNU packages +# and recognize all the CPU types, system types and aliases +# that are meaningful with *any* GNU software. +# Each package is responsible for reporting which valid configurations +# it does not support. The user should be able to distinguish +# a failure to support a valid configuration from a meaningless +# configuration. + +# The goal of this file is to map all the various variations of a given +# machine specification into a single specification in the form: +# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM +# or in some cases, the newer four-part form: +# CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM +# It is wrong to echo any other type of specification. + +me=`echo "$0" | sed -e 's,.*/,,'` + +usage="\ +Usage: $0 [OPTION] CPU-MFR-OPSYS + $0 [OPTION] ALIAS + +Canonicalize a configuration name. + +Operation modes: + -h, --help print this help, then exit + -t, --time-stamp print date of last modification, then exit + -v, --version print version number, then exit + +Report bugs and patches to ." + +version="\ +GNU config.sub ($timestamp) + +Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +Free Software Foundation, Inc. + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +help=" +Try \`$me --help' for more information." + +# Parse command line +while test $# -gt 0 ; do + case $1 in + --time-stamp | --time* | -t ) + echo "$timestamp" ; exit 0 ;; + --version | -v ) + echo "$version" ; exit 0 ;; + --help | --h* | -h ) + echo "$usage"; exit 0 ;; + -- ) # Stop option processing + shift; break ;; + - ) # Use stdin as input. + break ;; + -* ) + echo "$me: invalid option $1$help" + exit 1 ;; + + *local*) + # First pass through any local machine types. + echo $1 + exit 0;; + + * ) + break ;; + esac +done + +case $# in + 0) echo "$me: missing argument$help" >&2 + exit 1;; + 1) ;; + *) echo "$me: too many arguments$help" >&2 + exit 1;; +esac + +# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any). +# Here we must recognize all the valid KERNEL-OS combinations. +maybe_os=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'` +case $maybe_os in + nto-qnx* | linux-gnu* | storm-chaos* | os2-emx* | windows32-*) + os=-$maybe_os + basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'` + ;; + *) + basic_machine=`echo $1 | sed 's/-[^-]*$//'` + if [ $basic_machine != $1 ] + then os=`echo $1 | sed 's/.*-/-/'` + else os=; fi + ;; +esac + +### Let's recognize common machines as not being operating systems so +### that things like config.sub decstation-3100 work. We also +### recognize some manufacturers as not being operating systems, so we +### can provide default operating systems below. +case $os in + -sun*os*) + # Prevent following clause from handling this invalid input. + ;; + -dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \ + -att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \ + -unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \ + -convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\ + -c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \ + -harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \ + -apple | -axis) + os= + basic_machine=$1 + ;; + -sim | -cisco | -oki | -wec | -winbond) + os= + basic_machine=$1 + ;; + -scout) + ;; + -wrs) + os=-vxworks + basic_machine=$1 + ;; + -chorusos*) + os=-chorusos + basic_machine=$1 + ;; + -chorusrdb) + os=-chorusrdb + basic_machine=$1 + ;; + -hiux*) + os=-hiuxwe2 + ;; + -sco5) + os=-sco3.2v5 + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -sco4) + os=-sco3.2v4 + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -sco3.2.[4-9]*) + os=`echo $os | sed -e 's/sco3.2./sco3.2v/'` + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -sco3.2v[4-9]*) + # Don't forget version if it is 3.2v4 or newer. + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -sco*) + os=-sco3.2v2 + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -udk*) + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -isc) + os=-isc2.2 + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -clix*) + basic_machine=clipper-intergraph + ;; + -isc*) + basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` + ;; + -lynx*) + os=-lynxos + ;; + -ptx*) + basic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'` + ;; + -windowsnt*) + os=`echo $os | sed -e 's/windowsnt/winnt/'` + ;; + -psos*) + os=-psos + ;; + -mint | -mint[0-9]*) + basic_machine=m68k-atari + os=-mint + ;; +esac + +# Decode aliases for certain CPU-COMPANY combinations. +case $basic_machine in + # Recognize the basic CPU types without company name. + # Some are omitted here because they have special meanings below. + 1750a | 580 \ + | a29k \ + | alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \ + | arc | arm | arm[bl]e | arme[lb] | armv[2345] | armv[345][lb] | avr \ + | c4x | clipper \ + | d10v | d30v | dsp16xx \ + | fr30 \ + | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \ + | i370 | i860 | i960 | ia64 \ + | m32r | m68000 | m68k | m88k | mcore \ + | mips16 | mips64 | mips64el | mips64orion | mips64orionel \ + | mips64vr4100 | mips64vr4100el | mips64vr4300 \ + | mips64vr4300el | mips64vr5000 | mips64vr5000el \ + | mipsbe | mipseb | mipsel | mipsle | mipstx39 | mipstx39el \ + | mipsisa32 \ + | mn10200 | mn10300 \ + | ns16k | ns32k \ + | openrisc \ + | pdp10 | pdp11 | pj | pjl \ + | powerpc | powerpc64 | powerpc64le | powerpcle | ppcbe \ + | pyramid \ + | s390 | s390x \ + | sh | sh[34] | sh[34]eb | shbe | shle \ + | sparc | sparc64 | sparclet | sparclite | sparcv9 | sparcv9b \ + | stormy16 | strongarm \ + | tahoe | thumb | tic80 | tron \ + | v850 \ + | we32k \ + | x86 | xscale \ + | z8k) + basic_machine=$basic_machine-unknown + ;; + m6811 | m68hc11 | m6812 | m68hc12) + # Motorola 68HC11/12. + basic_machine=$basic_machine-unknown + os=-none + ;; + m88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65 | z8k) + ;; + + # We use `pc' rather than `unknown' + # because (1) that's what they normally are, and + # (2) the word "unknown" tends to confuse beginning users. + i*86 | x86_64) + basic_machine=$basic_machine-pc + ;; + # Object if more than one company name word. + *-*-*) + echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2 + exit 1 + ;; + # Recognize the basic CPU types with company name. + 580-* \ + | a29k-* \ + | alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \ + | alphapca5[67]-* | arc-* \ + | arm-* | armbe-* | armle-* | armv*-* \ + | avr-* \ + | bs2000-* \ + | c[123]* | c30-* | [cjt]90-* | c54x-* \ + | clipper-* | cray2-* | cydra-* \ + | d10v-* | d30v-* \ + | elxsi-* \ + | f30[01]-* | f700-* | fr30-* | fx80-* \ + | h8300-* | h8500-* \ + | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \ + | i*86-* | i860-* | i960-* | ia64-* \ + | m32r-* \ + | m68000-* | m680[01234]0-* | m68360-* | m683?2-* | m68k-* \ + | m88110-* | m88k-* | mcore-* \ + | mips-* | mips16-* | mips64-* | mips64el-* | mips64orion-* \ + | mips64orionel-* | mips64vr4100-* | mips64vr4100el-* \ + | mips64vr4300-* | mips64vr4300el-* | mipsbe-* | mipseb-* \ + | mipsle-* | mipsel-* | mipstx39-* | mipstx39el-* \ + | none-* | np1-* | ns16k-* | ns32k-* \ + | orion-* \ + | pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \ + | powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* | ppcbe-* \ + | pyramid-* \ + | romp-* | rs6000-* \ + | s390-* | s390x-* \ + | sh-* | sh[34]-* | sh[34]eb-* | shbe-* | shle-* \ + | sparc-* | sparc64-* | sparc86x-* | sparclite-* \ + | sparcv9-* | sparcv9b-* | stormy16-* | strongarm-* | sv1-* \ + | t3e-* | tahoe-* | thumb-* | tic30-* | tic54x-* | tic80-* | tron-* \ + | v850-* | vax-* \ + | we32k-* \ + | x86-* | x86_64-* | xmp-* | xps100-* | xscale-* \ + | ymp-* \ + | z8k-*) + ;; + # Recognize the various machine names and aliases which stand + # for a CPU type and a company and sometimes even an OS. + 386bsd) + basic_machine=i386-unknown + os=-bsd + ;; + 3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc) + basic_machine=m68000-att + ;; + 3b*) + basic_machine=we32k-att + ;; + a29khif) + basic_machine=a29k-amd + os=-udi + ;; + adobe68k) + basic_machine=m68010-adobe + os=-scout + ;; + alliant | fx80) + basic_machine=fx80-alliant + ;; + altos | altos3068) + basic_machine=m68k-altos + ;; + am29k) + basic_machine=a29k-none + os=-bsd + ;; + amdahl) + basic_machine=580-amdahl + os=-sysv + ;; + amiga | amiga-*) + basic_machine=m68k-unknown + ;; + amigaos | amigados) + basic_machine=m68k-unknown + os=-amigaos + ;; + amigaunix | amix) + basic_machine=m68k-unknown + os=-sysv4 + ;; + apollo68) + basic_machine=m68k-apollo + os=-sysv + ;; + apollo68bsd) + basic_machine=m68k-apollo + os=-bsd + ;; + aux) + basic_machine=m68k-apple + os=-aux + ;; + balance) + basic_machine=ns32k-sequent + os=-dynix + ;; + convex-c1) + basic_machine=c1-convex + os=-bsd + ;; + convex-c2) + basic_machine=c2-convex + os=-bsd + ;; + convex-c32) + basic_machine=c32-convex + os=-bsd + ;; + convex-c34) + basic_machine=c34-convex + os=-bsd + ;; + convex-c38) + basic_machine=c38-convex + os=-bsd + ;; + cray | ymp) + basic_machine=ymp-cray + os=-unicos + ;; + cray2) + basic_machine=cray2-cray + os=-unicos + ;; + [cjt]90) + basic_machine=${basic_machine}-cray + os=-unicos + ;; + crds | unos) + basic_machine=m68k-crds + ;; + cris | cris-* | etrax*) + basic_machine=cris-axis + ;; + da30 | da30-*) + basic_machine=m68k-da30 + ;; + decstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn) + basic_machine=mips-dec + ;; + delta | 3300 | motorola-3300 | motorola-delta \ + | 3300-motorola | delta-motorola) + basic_machine=m68k-motorola + ;; + delta88) + basic_machine=m88k-motorola + os=-sysv3 + ;; + dpx20 | dpx20-*) + basic_machine=rs6000-bull + os=-bosx + ;; + dpx2* | dpx2*-bull) + basic_machine=m68k-bull + os=-sysv3 + ;; + ebmon29k) + basic_machine=a29k-amd + os=-ebmon + ;; + elxsi) + basic_machine=elxsi-elxsi + os=-bsd + ;; + encore | umax | mmax) + basic_machine=ns32k-encore + ;; + es1800 | OSE68k | ose68k | ose | OSE) + basic_machine=m68k-ericsson + os=-ose + ;; + fx2800) + basic_machine=i860-alliant + ;; + genix) + basic_machine=ns32k-ns + ;; + gmicro) + basic_machine=tron-gmicro + os=-sysv + ;; + go32) + basic_machine=i386-pc + os=-go32 + ;; + h3050r* | hiux*) + basic_machine=hppa1.1-hitachi + os=-hiuxwe2 + ;; + h8300hms) + basic_machine=h8300-hitachi + os=-hms + ;; + h8300xray) + basic_machine=h8300-hitachi + os=-xray + ;; + h8500hms) + basic_machine=h8500-hitachi + os=-hms + ;; + harris) + basic_machine=m88k-harris + os=-sysv3 + ;; + hp300-*) + basic_machine=m68k-hp + ;; + hp300bsd) + basic_machine=m68k-hp + os=-bsd + ;; + hp300hpux) + basic_machine=m68k-hp + os=-hpux + ;; + hp3k9[0-9][0-9] | hp9[0-9][0-9]) + basic_machine=hppa1.0-hp + ;; + hp9k2[0-9][0-9] | hp9k31[0-9]) + basic_machine=m68000-hp + ;; + hp9k3[2-9][0-9]) + basic_machine=m68k-hp + ;; + hp9k6[0-9][0-9] | hp6[0-9][0-9]) + basic_machine=hppa1.0-hp + ;; + hp9k7[0-79][0-9] | hp7[0-79][0-9]) + basic_machine=hppa1.1-hp + ;; + hp9k78[0-9] | hp78[0-9]) + # FIXME: really hppa2.0-hp + basic_machine=hppa1.1-hp + ;; + hp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893) + # FIXME: really hppa2.0-hp + basic_machine=hppa1.1-hp + ;; + hp9k8[0-9][13679] | hp8[0-9][13679]) + basic_machine=hppa1.1-hp + ;; + hp9k8[0-9][0-9] | hp8[0-9][0-9]) + basic_machine=hppa1.0-hp + ;; + hppa-next) + os=-nextstep3 + ;; + hppaosf) + basic_machine=hppa1.1-hp + os=-osf + ;; + hppro) + basic_machine=hppa1.1-hp + os=-proelf + ;; + i370-ibm* | ibm*) + basic_machine=i370-ibm + ;; +# I'm not sure what "Sysv32" means. Should this be sysv3.2? + i*86v32) + basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` + os=-sysv32 + ;; + i*86v4*) + basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` + os=-sysv4 + ;; + i*86v) + basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` + os=-sysv + ;; + i*86sol2) + basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` + os=-solaris2 + ;; + i386mach) + basic_machine=i386-mach + os=-mach + ;; + i386-vsta | vsta) + basic_machine=i386-unknown + os=-vsta + ;; + iris | iris4d) + basic_machine=mips-sgi + case $os in + -irix*) + ;; + *) + os=-irix4 + ;; + esac + ;; + isi68 | isi) + basic_machine=m68k-isi + os=-sysv + ;; + m88k-omron*) + basic_machine=m88k-omron + ;; + magnum | m3230) + basic_machine=mips-mips + os=-sysv + ;; + merlin) + basic_machine=ns32k-utek + os=-sysv + ;; + mingw32) + basic_machine=i386-pc + os=-mingw32 + ;; + miniframe) + basic_machine=m68000-convergent + ;; + *mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*) + basic_machine=m68k-atari + os=-mint + ;; + mipsel*-linux*) + basic_machine=mipsel-unknown + os=-linux-gnu + ;; + mips*-linux*) + basic_machine=mips-unknown + os=-linux-gnu + ;; + mips3*-*) + basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'` + ;; + mips3*) + basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown + ;; + mmix*) + basic_machine=mmix-knuth + os=-mmixware + ;; + monitor) + basic_machine=m68k-rom68k + os=-coff + ;; + msdos) + basic_machine=i386-pc + os=-msdos + ;; + mvs) + basic_machine=i370-ibm + os=-mvs + ;; + ncr3000) + basic_machine=i486-ncr + os=-sysv4 + ;; + netbsd386) + basic_machine=i386-unknown + os=-netbsd + ;; + netwinder) + basic_machine=armv4l-rebel + os=-linux + ;; + news | news700 | news800 | news900) + basic_machine=m68k-sony + os=-newsos + ;; + news1000) + basic_machine=m68030-sony + os=-newsos + ;; + news-3600 | risc-news) + basic_machine=mips-sony + os=-newsos + ;; + necv70) + basic_machine=v70-nec + os=-sysv + ;; + next | m*-next ) + basic_machine=m68k-next + case $os in + -nextstep* ) + ;; + -ns2*) + os=-nextstep2 + ;; + *) + os=-nextstep3 + ;; + esac + ;; + nh3000) + basic_machine=m68k-harris + os=-cxux + ;; + nh[45]000) + basic_machine=m88k-harris + os=-cxux + ;; + nindy960) + basic_machine=i960-intel + os=-nindy + ;; + mon960) + basic_machine=i960-intel + os=-mon960 + ;; + nonstopux) + basic_machine=mips-compaq + os=-nonstopux + ;; + np1) + basic_machine=np1-gould + ;; + nsr-tandem) + basic_machine=nsr-tandem + ;; + op50n-* | op60c-*) + basic_machine=hppa1.1-oki + os=-proelf + ;; + OSE68000 | ose68000) + basic_machine=m68000-ericsson + os=-ose + ;; + os68k) + basic_machine=m68k-none + os=-os68k + ;; + pa-hitachi) + basic_machine=hppa1.1-hitachi + os=-hiuxwe2 + ;; + paragon) + basic_machine=i860-intel + os=-osf + ;; + pbd) + basic_machine=sparc-tti + ;; + pbb) + basic_machine=m68k-tti + ;; + pc532 | pc532-*) + basic_machine=ns32k-pc532 + ;; + pentium | p5 | k5 | k6 | nexgen | viac3) + basic_machine=i586-pc + ;; + pentiumpro | p6 | 6x86 | athlon) + basic_machine=i686-pc + ;; + pentiumii | pentium2) + basic_machine=i686-pc + ;; + pentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*) + basic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + pentiumpro-* | p6-* | 6x86-* | athlon-*) + basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + pentiumii-* | pentium2-*) + basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + pn) + basic_machine=pn-gould + ;; + power) basic_machine=power-ibm + ;; + ppc) basic_machine=powerpc-unknown + ;; + ppc-*) basic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + ppcle | powerpclittle | ppc-le | powerpc-little) + basic_machine=powerpcle-unknown + ;; + ppcle-* | powerpclittle-*) + basic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + ppc64) basic_machine=powerpc64-unknown + ;; + ppc64-*) basic_machine=powerpc64-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + ppc64le | powerpc64little | ppc64-le | powerpc64-little) + basic_machine=powerpc64le-unknown + ;; + ppc64le-* | powerpc64little-*) + basic_machine=powerpc64le-`echo $basic_machine | sed 's/^[^-]*-//'` + ;; + ps2) + basic_machine=i386-ibm + ;; + pw32) + basic_machine=i586-unknown + os=-pw32 + ;; + rom68k) + basic_machine=m68k-rom68k + os=-coff + ;; + rm[46]00) + basic_machine=mips-siemens + ;; + rtpc | rtpc-*) + basic_machine=romp-ibm + ;; + sa29200) + basic_machine=a29k-amd + os=-udi + ;; + sequent) + basic_machine=i386-sequent + ;; + sh) + basic_machine=sh-hitachi + os=-hms + ;; + sparclite-wrs | simso-wrs) + basic_machine=sparclite-wrs + os=-vxworks + ;; + sps7) + basic_machine=m68k-bull + os=-sysv2 + ;; + spur) + basic_machine=spur-unknown + ;; + st2000) + basic_machine=m68k-tandem + ;; + stratus) + basic_machine=i860-stratus + os=-sysv4 + ;; + sun2) + basic_machine=m68000-sun + ;; + sun2os3) + basic_machine=m68000-sun + os=-sunos3 + ;; + sun2os4) + basic_machine=m68000-sun + os=-sunos4 + ;; + sun3os3) + basic_machine=m68k-sun + os=-sunos3 + ;; + sun3os4) + basic_machine=m68k-sun + os=-sunos4 + ;; + sun4os3) + basic_machine=sparc-sun + os=-sunos3 + ;; + sun4os4) + basic_machine=sparc-sun + os=-sunos4 + ;; + sun4sol2) + basic_machine=sparc-sun + os=-solaris2 + ;; + sun3 | sun3-*) + basic_machine=m68k-sun + ;; + sun4) + basic_machine=sparc-sun + ;; + sun386 | sun386i | roadrunner) + basic_machine=i386-sun + ;; + sv1) + basic_machine=sv1-cray + os=-unicos + ;; + symmetry) + basic_machine=i386-sequent + os=-dynix + ;; + t3e) + basic_machine=t3e-cray + os=-unicos + ;; + tic54x | c54x*) + basic_machine=tic54x-unknown + os=-coff + ;; + tx39) + basic_machine=mipstx39-unknown + ;; + tx39el) + basic_machine=mipstx39el-unknown + ;; + tower | tower-32) + basic_machine=m68k-ncr + ;; + udi29k) + basic_machine=a29k-amd + os=-udi + ;; + ultra3) + basic_machine=a29k-nyu + os=-sym1 + ;; + v810 | necv810) + basic_machine=v810-nec + os=-none + ;; + vaxv) + basic_machine=vax-dec + os=-sysv + ;; + vms) + basic_machine=vax-dec + os=-vms + ;; + vpp*|vx|vx-*) + basic_machine=f301-fujitsu + ;; + vxworks960) + basic_machine=i960-wrs + os=-vxworks + ;; + vxworks68) + basic_machine=m68k-wrs + os=-vxworks + ;; + vxworks29k) + basic_machine=a29k-wrs + os=-vxworks + ;; + w65*) + basic_machine=w65-wdc + os=-none + ;; + w89k-*) + basic_machine=hppa1.1-winbond + os=-proelf + ;; + windows32) + basic_machine=i386-pc + os=-windows32-msvcrt + ;; + xmp) + basic_machine=xmp-cray + os=-unicos + ;; + xps | xps100) + basic_machine=xps100-honeywell + ;; + z8k-*-coff) + basic_machine=z8k-unknown + os=-sim + ;; + none) + basic_machine=none-none + os=-none + ;; + +# Here we handle the default manufacturer of certain CPU types. It is in +# some cases the only manufacturer, in others, it is the most popular. + w89k) + basic_machine=hppa1.1-winbond + ;; + op50n) + basic_machine=hppa1.1-oki + ;; + op60c) + basic_machine=hppa1.1-oki + ;; + mips) + if [ x$os = x-linux-gnu ]; then + basic_machine=mips-unknown + else + basic_machine=mips-mips + fi + ;; + romp) + basic_machine=romp-ibm + ;; + rs6000) + basic_machine=rs6000-ibm + ;; + vax) + basic_machine=vax-dec + ;; + pdp10) + # there are many clones, so DEC is not a safe bet + basic_machine=pdp10-unknown + ;; + pdp11) + basic_machine=pdp11-dec + ;; + we32k) + basic_machine=we32k-att + ;; + sh3 | sh4 | sh3eb | sh4eb) + basic_machine=sh-unknown + ;; + sparc | sparcv9 | sparcv9b) + basic_machine=sparc-sun + ;; + cydra) + basic_machine=cydra-cydrome + ;; + orion) + basic_machine=orion-highlevel + ;; + orion105) + basic_machine=clipper-highlevel + ;; + mac | mpw | mac-mpw) + basic_machine=m68k-apple + ;; + pmac | pmac-mpw) + basic_machine=powerpc-apple + ;; + c4x*) + basic_machine=c4x-none + os=-coff + ;; + *-unknown) + # Make sure to match an already-canonicalized machine name. + ;; + *) + echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2 + exit 1 + ;; +esac + +# Here we canonicalize certain aliases for manufacturers. +case $basic_machine in + *-digital*) + basic_machine=`echo $basic_machine | sed 's/digital.*/dec/'` + ;; + *-commodore*) + basic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'` + ;; + *) + ;; +esac + +# Decode manufacturer-specific aliases for certain operating systems. + +if [ x"$os" != x"" ] +then +case $os in + # First match some system type aliases + # that might get confused with valid system types. + # -solaris* is a basic system type, with this one exception. + -solaris1 | -solaris1.*) + os=`echo $os | sed -e 's|solaris1|sunos4|'` + ;; + -solaris) + os=-solaris2 + ;; + -svr4*) + os=-sysv4 + ;; + -unixware*) + os=-sysv4.2uw + ;; + -gnu/linux*) + os=`echo $os | sed -e 's|gnu/linux|linux-gnu|'` + ;; + # First accept the basic system types. + # The portable systems comes first. + # Each alternative MUST END IN A *, to match a version number. + # -sysv* is not here because it comes later, after sysvr4. + -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \ + | -*vms* | -sco* | -esix* | -isc* | -aix* | -sunos | -sunos[34]*\ + | -hpux* | -unos* | -osf* | -luna* | -dgux* | -solaris* | -sym* \ + | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \ + | -aos* \ + | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \ + | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \ + | -hiux* | -386bsd* | -netbsd* | -openbsd* | -freebsd* | -riscix* \ + | -lynxos* | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \ + | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \ + | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \ + | -chorusos* | -chorusrdb* \ + | -cygwin* | -pe* | -psos* | -moss* | -proelf* | -rtems* \ + | -mingw32* | -linux-gnu* | -uxpv* | -beos* | -mpeix* | -udk* \ + | -interix* | -uwin* | -rhapsody* | -darwin* | -opened* \ + | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \ + | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \ + | -os2* | -vos*) + # Remember, each alternative MUST END IN *, to match a version number. + ;; + -qnx*) + case $basic_machine in + x86-* | i*86-*) + ;; + *) + os=-nto$os + ;; + esac + ;; + -nto*) + os=-nto-qnx + ;; + -sim | -es1800* | -hms* | -xray | -os68k* | -none* | -v88r* \ + | -windows* | -osx | -abug | -netware* | -os9* | -beos* \ + | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*) + ;; + -mac*) + os=`echo $os | sed -e 's|mac|macos|'` + ;; + -linux*) + os=`echo $os | sed -e 's|linux|linux-gnu|'` + ;; + -sunos5*) + os=`echo $os | sed -e 's|sunos5|solaris2|'` + ;; + -sunos6*) + os=`echo $os | sed -e 's|sunos6|solaris3|'` + ;; + -opened*) + os=-openedition + ;; + -wince*) + os=-wince + ;; + -osfrose*) + os=-osfrose + ;; + -osf*) + os=-osf + ;; + -utek*) + os=-bsd + ;; + -dynix*) + os=-bsd + ;; + -acis*) + os=-aos + ;; + -386bsd) + os=-bsd + ;; + -ctix* | -uts*) + os=-sysv + ;; + -ns2 ) + os=-nextstep2 + ;; + -nsk*) + os=-nsk + ;; + # Preserve the version number of sinix5. + -sinix5.*) + os=`echo $os | sed -e 's|sinix|sysv|'` + ;; + -sinix*) + os=-sysv4 + ;; + -triton*) + os=-sysv3 + ;; + -oss*) + os=-sysv3 + ;; + -svr4) + os=-sysv4 + ;; + -svr3) + os=-sysv3 + ;; + -sysvr4) + os=-sysv4 + ;; + # This must come after -sysvr4. + -sysv*) + ;; + -ose*) + os=-ose + ;; + -es1800*) + os=-ose + ;; + -xenix) + os=-xenix + ;; + -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) + os=-mint + ;; + -none) + ;; + *) + # Get rid of the `-' at the beginning of $os. + os=`echo $os | sed 's/[^-]*-//'` + echo Invalid configuration \`$1\': system \`$os\' not recognized 1>&2 + exit 1 + ;; +esac +else + +# Here we handle the default operating systems that come with various machines. +# The value should be what the vendor currently ships out the door with their +# machine or put another way, the most popular os provided with the machine. + +# Note that if you're going to try to match "-MANUFACTURER" here (say, +# "-sun"), then you have to tell the case statement up towards the top +# that MANUFACTURER isn't an operating system. Otherwise, code above +# will signal an error saying that MANUFACTURER isn't an operating +# system, and we'll never get to this point. + +case $basic_machine in + *-acorn) + os=-riscix1.2 + ;; + arm*-rebel) + os=-linux + ;; + arm*-semi) + os=-aout + ;; + pdp10-*) + os=-tops20 + ;; + pdp11-*) + os=-none + ;; + *-dec | vax-*) + os=-ultrix4.2 + ;; + m68*-apollo) + os=-domain + ;; + i386-sun) + os=-sunos4.0.2 + ;; + m68000-sun) + os=-sunos3 + # This also exists in the configure program, but was not the + # default. + # os=-sunos4 + ;; + m68*-cisco) + os=-aout + ;; + mips*-cisco) + os=-elf + ;; + mips*-*) + os=-elf + ;; + *-tti) # must be before sparc entry or we get the wrong os. + os=-sysv3 + ;; + sparc-* | *-sun) + os=-sunos4.1.1 + ;; + *-be) + os=-beos + ;; + *-ibm) + os=-aix + ;; + *-wec) + os=-proelf + ;; + *-winbond) + os=-proelf + ;; + *-oki) + os=-proelf + ;; + *-hp) + os=-hpux + ;; + *-hitachi) + os=-hiux + ;; + i860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent) + os=-sysv + ;; + *-cbm) + os=-amigaos + ;; + *-dg) + os=-dgux + ;; + *-dolphin) + os=-sysv3 + ;; + m68k-ccur) + os=-rtu + ;; + m88k-omron*) + os=-luna + ;; + *-next ) + os=-nextstep + ;; + *-sequent) + os=-ptx + ;; + *-crds) + os=-unos + ;; + *-ns) + os=-genix + ;; + i370-*) + os=-mvs + ;; + *-next) + os=-nextstep3 + ;; + *-gould) + os=-sysv + ;; + *-highlevel) + os=-bsd + ;; + *-encore) + os=-bsd + ;; + *-sgi) + os=-irix + ;; + *-siemens) + os=-sysv4 + ;; + *-masscomp) + os=-rtu + ;; + f30[01]-fujitsu | f700-fujitsu) + os=-uxpv + ;; + *-rom68k) + os=-coff + ;; + *-*bug) + os=-coff + ;; + *-apple) + os=-macos + ;; + *-atari*) + os=-mint + ;; + *) + os=-none + ;; +esac +fi + +# Here we handle the case where we know the os, and the CPU type, but not the +# manufacturer. We pick the logical manufacturer. +vendor=unknown +case $basic_machine in + *-unknown) + case $os in + -riscix*) + vendor=acorn + ;; + -sunos*) + vendor=sun + ;; + -aix*) + vendor=ibm + ;; + -beos*) + vendor=be + ;; + -hpux*) + vendor=hp + ;; + -mpeix*) + vendor=hp + ;; + -hiux*) + vendor=hitachi + ;; + -unos*) + vendor=crds + ;; + -dgux*) + vendor=dg + ;; + -luna*) + vendor=omron + ;; + -genix*) + vendor=ns + ;; + -mvs* | -opened*) + vendor=ibm + ;; + -ptx*) + vendor=sequent + ;; + -vxsim* | -vxworks*) + vendor=wrs + ;; + -aux*) + vendor=apple + ;; + -hms*) + vendor=hitachi + ;; + -mpw* | -macos*) + vendor=apple + ;; + -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) + vendor=atari + ;; + -vos*) + vendor=stratus + ;; + esac + basic_machine=`echo $basic_machine | sed "s/unknown/$vendor/"` + ;; +esac + +echo $basic_machine$os +exit 0 + +# Local variables: +# eval: (add-hook 'write-file-hooks 'time-stamp) +# time-stamp-start: "timestamp='" +# time-stamp-format: "%:y-%02m-%02d" +# time-stamp-end: "'" +# End: diff --git a/libresample-0.1.3/configure b/libresample-0.1.3/configure new file mode 100644 index 00000000..37fd9c11 --- /dev/null +++ b/libresample-0.1.3/configure @@ -0,0 +1,2937 @@ +#! /bin/sh +# Guess values for system-dependent variables and create Makefiles. +# Generated by Autoconf 2.52. +# +# Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. +# This configure script is free software; the Free Software Foundation +# gives unlimited permission to copy, distribute and modify it. + +# Avoid depending upon Character Ranges. +as_cr_letters='abcdefghijklmnopqrstuvwxyz' +as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ' +as_cr_Letters=$as_cr_letters$as_cr_LETTERS +as_cr_digits='0123456789' +as_cr_alnum=$as_cr_Letters$as_cr_digits + +# Sed expression to map a string onto a valid variable name. +as_tr_sh="sed y%*+%pp%;s%[^_$as_cr_alnum]%_%g" + +# Sed expression to map a string onto a valid CPP name. +as_tr_cpp="sed y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g" + +# Be Bourne compatible +if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then + emulate sh + NULLCMD=: +elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then + set -o posix +fi + +# Name of the executable. +as_me=`echo "$0" |sed 's,.*[\\/],,'` + +if expr a : '\(a\)' >/dev/null 2>&1; then + as_expr=expr +else + as_expr=false +fi + +rm -f conf$$ conf$$.exe conf$$.file +echo >conf$$.file +if ln -s conf$$.file conf$$ 2>/dev/null; then + # We could just check for DJGPP; but this test a) works b) is more generic + # and c) will remain valid once DJGPP supports symlinks (DJGPP 2.04). + if test -f conf$$.exe; then + # Don't use ln at all; we don't have any links + as_ln_s='cp -p' + else + as_ln_s='ln -s' + fi +elif ln conf$$.file conf$$ 2>/dev/null; then + as_ln_s=ln +else + as_ln_s='cp -p' +fi +rm -f conf$$ conf$$.exe conf$$.file + +as_executable_p="test -f" + +# Support unset when possible. +if (FOO=FOO; unset FOO) >/dev/null 2>&1; then + as_unset=unset +else + as_unset=false +fi + +# NLS nuisances. +$as_unset LANG || test "${LANG+set}" != set || { LANG=C; export LANG; } +$as_unset LC_ALL || test "${LC_ALL+set}" != set || { LC_ALL=C; export LC_ALL; } +$as_unset LC_TIME || test "${LC_TIME+set}" != set || { LC_TIME=C; export LC_TIME; } +$as_unset LC_CTYPE || test "${LC_CTYPE+set}" != set || { LC_CTYPE=C; export LC_CTYPE; } +$as_unset LANGUAGE || test "${LANGUAGE+set}" != set || { LANGUAGE=C; export LANGUAGE; } +$as_unset LC_COLLATE || test "${LC_COLLATE+set}" != set || { LC_COLLATE=C; export LC_COLLATE; } +$as_unset LC_NUMERIC || test "${LC_NUMERIC+set}" != set || { LC_NUMERIC=C; export LC_NUMERIC; } +$as_unset LC_MESSAGES || test "${LC_MESSAGES+set}" != set || { LC_MESSAGES=C; export LC_MESSAGES; } + +# IFS +# We need space, tab and new line, in precisely that order. +as_nl=' +' +IFS=" $as_nl" + +# CDPATH. +$as_unset CDPATH || test "${CDPATH+set}" != set || { CDPATH=:; export CDPATH; } + +# Name of the host. +# hostname on some systems (SVR3.2, Linux) returns a bogus exit status, +# so uname gets run too. +ac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q` + +exec 6>&1 + +# +# Initializations. +# +ac_default_prefix=/usr/local +cross_compiling=no +subdirs= +MFLAGS= MAKEFLAGS= +SHELL=${CONFIG_SHELL-/bin/sh} + +# Maximum number of lines to put in a shell here document. +# This variable seems obsolete. It should probably be removed, and +# only ac_max_sed_lines should be used. +: ${ac_max_here_lines=38} + +ac_unique_file="src/resample.c" + +# Initialize some variables set by options. +ac_init_help= +ac_init_version=false +# The variables have the same names as the options, with +# dashes changed to underlines. +cache_file=/dev/null +exec_prefix=NONE +no_create= +no_recursion= +prefix=NONE +program_prefix=NONE +program_suffix=NONE +program_transform_name=s,x,x, +silent= +site= +srcdir= +verbose= +x_includes=NONE +x_libraries=NONE + +# Installation directory options. +# These are left unexpanded so users can "make install exec_prefix=/foo" +# and all the variables that are supposed to be based on exec_prefix +# by default will actually change. +# Use braces instead of parens because sh, perl, etc. also accept them. +bindir='${exec_prefix}/bin' +sbindir='${exec_prefix}/sbin' +libexecdir='${exec_prefix}/libexec' +datadir='${prefix}/share' +sysconfdir='${prefix}/etc' +sharedstatedir='${prefix}/com' +localstatedir='${prefix}/var' +libdir='${exec_prefix}/lib' +includedir='${prefix}/include' +oldincludedir='/usr/include' +infodir='${prefix}/info' +mandir='${prefix}/man' + +# Identity of this package. +PACKAGE_NAME= +PACKAGE_TARNAME= +PACKAGE_VERSION= +PACKAGE_STRING= +PACKAGE_BUGREPORT= + +ac_prev= +for ac_option +do + # If the previous option needs an argument, assign it. + if test -n "$ac_prev"; then + eval "$ac_prev=\$ac_option" + ac_prev= + continue + fi + + ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'` + + # Accept the important Cygnus configure options, so we can diagnose typos. + + case $ac_option in + + -bindir | --bindir | --bindi | --bind | --bin | --bi) + ac_prev=bindir ;; + -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*) + bindir=$ac_optarg ;; + + -build | --build | --buil | --bui | --bu) + ac_prev=build_alias ;; + -build=* | --build=* | --buil=* | --bui=* | --bu=*) + build_alias=$ac_optarg ;; + + -cache-file | --cache-file | --cache-fil | --cache-fi \ + | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c) + ac_prev=cache_file ;; + -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \ + | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*) + cache_file=$ac_optarg ;; + + --config-cache | -C) + cache_file=config.cache ;; + + -datadir | --datadir | --datadi | --datad | --data | --dat | --da) + ac_prev=datadir ;; + -datadir=* | --datadir=* | --datadi=* | --datad=* | --data=* | --dat=* \ + | --da=*) + datadir=$ac_optarg ;; + + -disable-* | --disable-*) + ac_feature=`expr "x$ac_option" : 'x-*disable-\(.*\)'` + # Reject names that are not valid shell variable names. + expr "x$ac_feature" : ".*[^-_$as_cr_alnum]" >/dev/null && + { echo "$as_me: error: invalid feature name: $ac_feature" >&2 + { (exit 1); exit 1; }; } + ac_feature=`echo $ac_feature | sed 's/-/_/g'` + eval "enable_$ac_feature=no" ;; + + -enable-* | --enable-*) + ac_feature=`expr "x$ac_option" : 'x-*enable-\([^=]*\)'` + # Reject names that are not valid shell variable names. + expr "x$ac_feature" : ".*[^-_$as_cr_alnum]" >/dev/null && + { echo "$as_me: error: invalid feature name: $ac_feature" >&2 + { (exit 1); exit 1; }; } + ac_feature=`echo $ac_feature | sed 's/-/_/g'` + case $ac_option in + *=*) ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"`;; + *) ac_optarg=yes ;; + esac + eval "enable_$ac_feature='$ac_optarg'" ;; + + -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \ + | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \ + | --exec | --exe | --ex) + ac_prev=exec_prefix ;; + -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \ + | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \ + | --exec=* | --exe=* | --ex=*) + exec_prefix=$ac_optarg ;; + + -gas | --gas | --ga | --g) + # Obsolete; use --with-gas. + with_gas=yes ;; + + -help | --help | --hel | --he | -h) + ac_init_help=long ;; + -help=r* | --help=r* | --hel=r* | --he=r* | -hr*) + ac_init_help=recursive ;; + -help=s* | --help=s* | --hel=s* | --he=s* | -hs*) + ac_init_help=short ;; + + -host | --host | --hos | --ho) + ac_prev=host_alias ;; + -host=* | --host=* | --hos=* | --ho=*) + host_alias=$ac_optarg ;; + + -includedir | --includedir | --includedi | --included | --include \ + | --includ | --inclu | --incl | --inc) + ac_prev=includedir ;; + -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \ + | --includ=* | --inclu=* | --incl=* | --inc=*) + includedir=$ac_optarg ;; + + -infodir | --infodir | --infodi | --infod | --info | --inf) + ac_prev=infodir ;; + -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*) + infodir=$ac_optarg ;; + + -libdir | --libdir | --libdi | --libd) + ac_prev=libdir ;; + -libdir=* | --libdir=* | --libdi=* | --libd=*) + libdir=$ac_optarg ;; + + -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \ + | --libexe | --libex | --libe) + ac_prev=libexecdir ;; + -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \ + | --libexe=* | --libex=* | --libe=*) + libexecdir=$ac_optarg ;; + + -localstatedir | --localstatedir | --localstatedi | --localstated \ + | --localstate | --localstat | --localsta | --localst \ + | --locals | --local | --loca | --loc | --lo) + ac_prev=localstatedir ;; + -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \ + | --localstate=* | --localstat=* | --localsta=* | --localst=* \ + | --locals=* | --local=* | --loca=* | --loc=* | --lo=*) + localstatedir=$ac_optarg ;; + + -mandir | --mandir | --mandi | --mand | --man | --ma | --m) + ac_prev=mandir ;; + -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*) + mandir=$ac_optarg ;; + + -nfp | --nfp | --nf) + # Obsolete; use --without-fp. + with_fp=no ;; + + -no-create | --no-create | --no-creat | --no-crea | --no-cre \ + | --no-cr | --no-c) + no_create=yes ;; + + -no-recursion | --no-recursion | --no-recursio | --no-recursi \ + | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r) + no_recursion=yes ;; + + -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \ + | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \ + | --oldin | --oldi | --old | --ol | --o) + ac_prev=oldincludedir ;; + -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \ + | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \ + | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*) + oldincludedir=$ac_optarg ;; + + -prefix | --prefix | --prefi | --pref | --pre | --pr | --p) + ac_prev=prefix ;; + -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*) + prefix=$ac_optarg ;; + + -program-prefix | --program-prefix | --program-prefi | --program-pref \ + | --program-pre | --program-pr | --program-p) + ac_prev=program_prefix ;; + -program-prefix=* | --program-prefix=* | --program-prefi=* \ + | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*) + program_prefix=$ac_optarg ;; + + -program-suffix | --program-suffix | --program-suffi | --program-suff \ + | --program-suf | --program-su | --program-s) + ac_prev=program_suffix ;; + -program-suffix=* | --program-suffix=* | --program-suffi=* \ + | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*) + program_suffix=$ac_optarg ;; + + -program-transform-name | --program-transform-name \ + | --program-transform-nam | --program-transform-na \ + | --program-transform-n | --program-transform- \ + | --program-transform | --program-transfor \ + | --program-transfo | --program-transf \ + | --program-trans | --program-tran \ + | --progr-tra | --program-tr | --program-t) + ac_prev=program_transform_name ;; + -program-transform-name=* | --program-transform-name=* \ + | --program-transform-nam=* | --program-transform-na=* \ + | --program-transform-n=* | --program-transform-=* \ + | --program-transform=* | --program-transfor=* \ + | --program-transfo=* | --program-transf=* \ + | --program-trans=* | --program-tran=* \ + | --progr-tra=* | --program-tr=* | --program-t=*) + program_transform_name=$ac_optarg ;; + + -q | -quiet | --quiet | --quie | --qui | --qu | --q \ + | -silent | --silent | --silen | --sile | --sil) + silent=yes ;; + + -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) + ac_prev=sbindir ;; + -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ + | --sbi=* | --sb=*) + sbindir=$ac_optarg ;; + + -sharedstatedir | --sharedstatedir | --sharedstatedi \ + | --sharedstated | --sharedstate | --sharedstat | --sharedsta \ + | --sharedst | --shareds | --shared | --share | --shar \ + | --sha | --sh) + ac_prev=sharedstatedir ;; + -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \ + | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \ + | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \ + | --sha=* | --sh=*) + sharedstatedir=$ac_optarg ;; + + -site | --site | --sit) + ac_prev=site ;; + -site=* | --site=* | --sit=*) + site=$ac_optarg ;; + + -srcdir | --srcdir | --srcdi | --srcd | --src | --sr) + ac_prev=srcdir ;; + -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*) + srcdir=$ac_optarg ;; + + -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \ + | --syscon | --sysco | --sysc | --sys | --sy) + ac_prev=sysconfdir ;; + -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \ + | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*) + sysconfdir=$ac_optarg ;; + + -target | --target | --targe | --targ | --tar | --ta | --t) + ac_prev=target_alias ;; + -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*) + target_alias=$ac_optarg ;; + + -v | -verbose | --verbose | --verbos | --verbo | --verb) + verbose=yes ;; + + -version | --version | --versio | --versi | --vers | -V) + ac_init_version=: ;; + + -with-* | --with-*) + ac_package=`expr "x$ac_option" : 'x-*with-\([^=]*\)'` + # Reject names that are not valid shell variable names. + expr "x$ac_package" : ".*[^-_$as_cr_alnum]" >/dev/null && + { echo "$as_me: error: invalid package name: $ac_package" >&2 + { (exit 1); exit 1; }; } + ac_package=`echo $ac_package| sed 's/-/_/g'` + case $ac_option in + *=*) ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"`;; + *) ac_optarg=yes ;; + esac + eval "with_$ac_package='$ac_optarg'" ;; + + -without-* | --without-*) + ac_package=`expr "x$ac_option" : 'x-*without-\(.*\)'` + # Reject names that are not valid shell variable names. + expr "x$ac_package" : ".*[^-_$as_cr_alnum]" >/dev/null && + { echo "$as_me: error: invalid package name: $ac_package" >&2 + { (exit 1); exit 1; }; } + ac_package=`echo $ac_package | sed 's/-/_/g'` + eval "with_$ac_package=no" ;; + + --x) + # Obsolete; use --with-x. + with_x=yes ;; + + -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \ + | --x-incl | --x-inc | --x-in | --x-i) + ac_prev=x_includes ;; + -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \ + | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*) + x_includes=$ac_optarg ;; + + -x-libraries | --x-libraries | --x-librarie | --x-librari \ + | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l) + ac_prev=x_libraries ;; + -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \ + | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*) + x_libraries=$ac_optarg ;; + + -*) { echo "$as_me: error: unrecognized option: $ac_option +Try \`$0 --help' for more information." >&2 + { (exit 1); exit 1; }; } + ;; + + *=*) + ac_envvar=`expr "x$ac_option" : 'x\([^=]*\)='` + # Reject names that are not valid shell variable names. + expr "x$ac_envvar" : ".*[^_$as_cr_alnum]" >/dev/null && + { echo "$as_me: error: invalid variable name: $ac_envvar" >&2 + { (exit 1); exit 1; }; } + ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` + eval "$ac_envvar='$ac_optarg'" + export $ac_envvar ;; + + *) + # FIXME: should be removed in autoconf 3.0. + echo "$as_me: WARNING: you should use --build, --host, --target" >&2 + expr "x$ac_option" : ".*[^-._$as_cr_alnum]" >/dev/null && + echo "$as_me: WARNING: invalid host type: $ac_option" >&2 + : ${build_alias=$ac_option} ${host_alias=$ac_option} ${target_alias=$ac_option} + ;; + + esac +done + +if test -n "$ac_prev"; then + ac_option=--`echo $ac_prev | sed 's/_/-/g'` + { echo "$as_me: error: missing argument to $ac_option" >&2 + { (exit 1); exit 1; }; } +fi + +# Be sure to have absolute paths. +for ac_var in exec_prefix prefix +do + eval ac_val=$`echo $ac_var` + case $ac_val in + [\\/$]* | ?:[\\/]* | NONE | '' ) ;; + *) { echo "$as_me: error: expected an absolute path for --$ac_var: $ac_val" >&2 + { (exit 1); exit 1; }; };; + esac +done + +# Be sure to have absolute paths. +for ac_var in bindir sbindir libexecdir datadir sysconfdir sharedstatedir \ + localstatedir libdir includedir oldincludedir infodir mandir +do + eval ac_val=$`echo $ac_var` + case $ac_val in + [\\/$]* | ?:[\\/]* ) ;; + *) { echo "$as_me: error: expected an absolute path for --$ac_var: $ac_val" >&2 + { (exit 1); exit 1; }; };; + esac +done + +# There might be people who depend on the old broken behavior: `$host' +# used to hold the argument of --host etc. +build=$build_alias +host=$host_alias +target=$target_alias + +# FIXME: should be removed in autoconf 3.0. +if test "x$host_alias" != x; then + if test "x$build_alias" = x; then + cross_compiling=maybe + echo "$as_me: WARNING: If you wanted to set the --build type, don't use --host. + If a cross compiler is detected then cross compile mode will be used." >&2 + elif test "x$build_alias" != "x$host_alias"; then + cross_compiling=yes + fi +fi + +ac_tool_prefix= +test -n "$host_alias" && ac_tool_prefix=$host_alias- + +test "$silent" = yes && exec 6>/dev/null + +# Find the source files, if location was not specified. +if test -z "$srcdir"; then + ac_srcdir_defaulted=yes + # Try the directory containing this script, then its parent. + ac_prog=$0 + ac_confdir=`echo "$ac_prog" | sed 's%[\\/][^\\/][^\\/]*$%%'` + test "x$ac_confdir" = "x$ac_prog" && ac_confdir=. + srcdir=$ac_confdir + if test ! -r $srcdir/$ac_unique_file; then + srcdir=.. + fi +else + ac_srcdir_defaulted=no +fi +if test ! -r $srcdir/$ac_unique_file; then + if test "$ac_srcdir_defaulted" = yes; then + { echo "$as_me: error: cannot find sources in $ac_confdir or .." >&2 + { (exit 1); exit 1; }; } + else + { echo "$as_me: error: cannot find sources in $srcdir" >&2 + { (exit 1); exit 1; }; } + fi +fi +srcdir=`echo "$srcdir" | sed 's%\([^\\/]\)[\\/]*$%\1%'` +ac_env_build_alias_set=${build_alias+set} +ac_env_build_alias_value=$build_alias +ac_cv_env_build_alias_set=${build_alias+set} +ac_cv_env_build_alias_value=$build_alias +ac_env_host_alias_set=${host_alias+set} +ac_env_host_alias_value=$host_alias +ac_cv_env_host_alias_set=${host_alias+set} +ac_cv_env_host_alias_value=$host_alias +ac_env_target_alias_set=${target_alias+set} +ac_env_target_alias_value=$target_alias +ac_cv_env_target_alias_set=${target_alias+set} +ac_cv_env_target_alias_value=$target_alias +ac_env_CC_set=${CC+set} +ac_env_CC_value=$CC +ac_cv_env_CC_set=${CC+set} +ac_cv_env_CC_value=$CC +ac_env_CFLAGS_set=${CFLAGS+set} +ac_env_CFLAGS_value=$CFLAGS +ac_cv_env_CFLAGS_set=${CFLAGS+set} +ac_cv_env_CFLAGS_value=$CFLAGS +ac_env_LDFLAGS_set=${LDFLAGS+set} +ac_env_LDFLAGS_value=$LDFLAGS +ac_cv_env_LDFLAGS_set=${LDFLAGS+set} +ac_cv_env_LDFLAGS_value=$LDFLAGS +ac_env_CPPFLAGS_set=${CPPFLAGS+set} +ac_env_CPPFLAGS_value=$CPPFLAGS +ac_cv_env_CPPFLAGS_set=${CPPFLAGS+set} +ac_cv_env_CPPFLAGS_value=$CPPFLAGS +ac_env_CPP_set=${CPP+set} +ac_env_CPP_value=$CPP +ac_cv_env_CPP_set=${CPP+set} +ac_cv_env_CPP_value=$CPP + +# +# Report the --help message. +# +if test "$ac_init_help" = "long"; then + # Omit some internal or obsolete options to make the list less imposing. + # This message is too long to be a string in the A/UX 3.1 sh. + cat < if you have libraries in a + nonstandard directory + CPPFLAGS C/C++ preprocessor flags, e.g. -I if you have + headers in a nonstandard directory + CPP C preprocessor + +Use these variables to override the choices made by `configure' or to help +it to find libraries and programs with nonstandard names/locations. + +EOF +fi + +if test "$ac_init_help" = "recursive"; then + # If there are subdirs, report their specific --help. + ac_popdir=`pwd` + for ac_subdir in : $ac_subdirs_all; do test "x$ac_subdir" = x: && continue + cd $ac_subdir + # A "../" for each directory in /$ac_subdir. + ac_dots=`echo $ac_subdir | + sed 's,^\./,,;s,[^/]$,&/,;s,[^/]*/,../,g'` + + case $srcdir in + .) # No --srcdir option. We are building in place. + ac_sub_srcdir=$srcdir ;; + [\\/]* | ?:[\\/]* ) # Absolute path. + ac_sub_srcdir=$srcdir/$ac_subdir ;; + *) # Relative path. + ac_sub_srcdir=$ac_dots$srcdir/$ac_subdir ;; + esac + + # Check for guested configure; otherwise get Cygnus style configure. + if test -f $ac_sub_srcdir/configure.gnu; then + echo + $SHELL $ac_sub_srcdir/configure.gnu --help=recursive + elif test -f $ac_sub_srcdir/configure; then + echo + $SHELL $ac_sub_srcdir/configure --help=recursive + elif test -f $ac_sub_srcdir/configure.ac || + test -f $ac_sub_srcdir/configure.in; then + echo + $ac_configure --help + else + echo "$as_me: WARNING: no configuration information is in $ac_subdir" >&2 + fi + cd $ac_popdir + done +fi + +test -n "$ac_init_help" && exit 0 +if $ac_init_version; then + cat <<\EOF + +Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001 +Free Software Foundation, Inc. +This configure script is free software; the Free Software Foundation +gives unlimited permission to copy, distribute and modify it. +EOF + exit 0 +fi +exec 5>config.log +cat >&5 </dev/null | sed 1q` +uname -m = `(uname -m) 2>/dev/null || echo unknown` +uname -r = `(uname -r) 2>/dev/null || echo unknown` +uname -s = `(uname -s) 2>/dev/null || echo unknown` +uname -v = `(uname -v) 2>/dev/null || echo unknown` + +/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null || echo unknown` +/bin/uname -X = `(/bin/uname -X) 2>/dev/null || echo unknown` + +/bin/arch = `(/bin/arch) 2>/dev/null || echo unknown` +/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null || echo unknown` +/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null || echo unknown` +hostinfo = `(hostinfo) 2>/dev/null || echo unknown` +/bin/machine = `(/bin/machine) 2>/dev/null || echo unknown` +/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null || echo unknown` +/bin/universe = `(/bin/universe) 2>/dev/null || echo unknown` + +PATH = $PATH + +_ASUNAME +} >&5 + +cat >&5 <\?\"\']*) + ac_arg=`echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` + ac_configure_args="$ac_configure_args$ac_sep'$ac_arg'" + ac_sep=" " ;; + *) ac_configure_args="$ac_configure_args$ac_sep$ac_arg" + ac_sep=" " ;; + esac + # Get rid of the leading space. +done + +# When interrupted or exit'd, cleanup temporary files, and complete +# config.log. We remove comments because anyway the quotes in there +# would cause problems or look ugly. +trap 'exit_status=$? + # Save into config.log some information that might help in debugging. + echo >&5 + echo "## ----------------- ##" >&5 + echo "## Cache variables. ##" >&5 + echo "## ----------------- ##" >&5 + echo >&5 + # The following way of writing the cache mishandles newlines in values, +{ + (set) 2>&1 | + case `(ac_space='"'"' '"'"'; set | grep ac_space) 2>&1` in + *ac_space=\ *) + sed -n \ + "s/'"'"'/'"'"'\\\\'"'"''"'"'/g; + s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='"'"'\\2'"'"'/p" + ;; + *) + sed -n \ + "s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1=\\2/p" + ;; + esac; +} >&5 + sed "/^$/d" confdefs.h >conftest.log + if test -s conftest.log; then + echo >&5 + echo "## ------------ ##" >&5 + echo "## confdefs.h. ##" >&5 + echo "## ------------ ##" >&5 + echo >&5 + cat conftest.log >&5 + fi + (echo; echo) >&5 + test "$ac_signal" != 0 && + echo "$as_me: caught signal $ac_signal" >&5 + echo "$as_me: exit $exit_status" >&5 + rm -rf conftest* confdefs* core core.* *.core conf$$* $ac_clean_files && + exit $exit_status + ' 0 +for ac_signal in 1 2 13 15; do + trap 'ac_signal='$ac_signal'; { (exit 1); exit 1; }' $ac_signal +done +ac_signal=0 + +# confdefs.h avoids OS command line length limits that DEFS can exceed. +rm -rf conftest* confdefs.h +# AIX cpp loses on an empty file, so make sure it contains at least a newline. +echo >confdefs.h + +# Let the site file select an alternate cache file if it wants to. +# Prefer explicitly selected file to automatically selected ones. +if test -z "$CONFIG_SITE"; then + if test "x$prefix" != xNONE; then + CONFIG_SITE="$prefix/share/config.site $prefix/etc/config.site" + else + CONFIG_SITE="$ac_default_prefix/share/config.site $ac_default_prefix/etc/config.site" + fi +fi +for ac_site_file in $CONFIG_SITE; do + if test -r "$ac_site_file"; then + { echo "$as_me:819: loading site script $ac_site_file" >&5 +echo "$as_me: loading site script $ac_site_file" >&6;} + cat "$ac_site_file" >&5 + . "$ac_site_file" + fi +done + +if test -r "$cache_file"; then + # Some versions of bash will fail to source /dev/null (special + # files actually), so we avoid doing that. + if test -f "$cache_file"; then + { echo "$as_me:830: loading cache $cache_file" >&5 +echo "$as_me: loading cache $cache_file" >&6;} + case $cache_file in + [\\/]* | ?:[\\/]* ) . $cache_file;; + *) . ./$cache_file;; + esac + fi +else + { echo "$as_me:838: creating cache $cache_file" >&5 +echo "$as_me: creating cache $cache_file" >&6;} + >$cache_file +fi + +# Check that the precious variables saved in the cache have kept the same +# value. +ac_cache_corrupted=false +for ac_var in `(set) 2>&1 | + sed -n 's/^ac_env_\([a-zA-Z_0-9]*\)_set=.*/\1/p'`; do + eval ac_old_set=\$ac_cv_env_${ac_var}_set + eval ac_new_set=\$ac_env_${ac_var}_set + eval ac_old_val="\$ac_cv_env_${ac_var}_value" + eval ac_new_val="\$ac_env_${ac_var}_value" + case $ac_old_set,$ac_new_set in + set,) + { echo "$as_me:854: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&5 +echo "$as_me: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&2;} + ac_cache_corrupted=: ;; + ,set) + { echo "$as_me:858: error: \`$ac_var' was not set in the previous run" >&5 +echo "$as_me: error: \`$ac_var' was not set in the previous run" >&2;} + ac_cache_corrupted=: ;; + ,);; + *) + if test "x$ac_old_val" != "x$ac_new_val"; then + { echo "$as_me:864: error: \`$ac_var' has changed since the previous run:" >&5 +echo "$as_me: error: \`$ac_var' has changed since the previous run:" >&2;} + { echo "$as_me:866: former value: $ac_old_val" >&5 +echo "$as_me: former value: $ac_old_val" >&2;} + { echo "$as_me:868: current value: $ac_new_val" >&5 +echo "$as_me: current value: $ac_new_val" >&2;} + ac_cache_corrupted=: + fi;; + esac + # Pass precious variables to config.status. It doesn't matter if + # we pass some twice (in addition to the command line arguments). + if test "$ac_new_set" = set; then + case $ac_new_val in + *" "*|*" "*|*[\[\]\~\#\$\^\&\*\(\)\{\}\\\|\;\<\>\?\"\']*) + ac_arg=$ac_var=`echo "$ac_new_val" | sed "s/'/'\\\\\\\\''/g"` + ac_configure_args="$ac_configure_args '$ac_arg'" + ;; + *) ac_configure_args="$ac_configure_args $ac_var=$ac_new_val" + ;; + esac + fi +done +if $ac_cache_corrupted; then + { echo "$as_me:887: error: changes in the environment can compromise the build" >&5 +echo "$as_me: error: changes in the environment can compromise the build" >&2;} + { { echo "$as_me:889: error: run \`make distclean' and/or \`rm $cache_file' and start over" >&5 +echo "$as_me: error: run \`make distclean' and/or \`rm $cache_file' and start over" >&2;} + { (exit 1); exit 1; }; } +fi + +ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu + +case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in + *c*,-n*) ECHO_N= ECHO_C=' +' ECHO_T=' ' ;; + *c*,* ) ECHO_N=-n ECHO_C= ECHO_T= ;; + *) ECHO_N= ECHO_C='\c' ECHO_T= ;; +esac +echo "#! $SHELL" >conftest.sh +echo "exit 0" >>conftest.sh +chmod +x conftest.sh +if { (echo "$as_me:909: PATH=\".;.\"; conftest.sh") >&5 + (PATH=".;."; conftest.sh) 2>&5 + ac_status=$? + echo "$as_me:912: \$? = $ac_status" >&5 + (exit $ac_status); }; then + ac_path_separator=';' +else + ac_path_separator=: +fi +PATH_SEPARATOR="$ac_path_separator" +rm -f conftest.sh + +ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu +if test -n "$ac_tool_prefix"; then + # Extract the first word of "${ac_tool_prefix}gcc", so it can be a program name with args. +set dummy ${ac_tool_prefix}gcc; ac_word=$2 +echo "$as_me:929: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$CC"; then + ac_cv_prog_CC="$CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_CC="${ac_tool_prefix}gcc" +echo "$as_me:944: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +CC=$ac_cv_prog_CC +if test -n "$CC"; then + echo "$as_me:952: result: $CC" >&5 +echo "${ECHO_T}$CC" >&6 +else + echo "$as_me:955: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + +fi +if test -z "$ac_cv_prog_CC"; then + ac_ct_CC=$CC + # Extract the first word of "gcc", so it can be a program name with args. +set dummy gcc; ac_word=$2 +echo "$as_me:964: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_ac_ct_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$ac_ct_CC"; then + ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_ac_ct_CC="gcc" +echo "$as_me:979: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +ac_ct_CC=$ac_cv_prog_ac_ct_CC +if test -n "$ac_ct_CC"; then + echo "$as_me:987: result: $ac_ct_CC" >&5 +echo "${ECHO_T}$ac_ct_CC" >&6 +else + echo "$as_me:990: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + + CC=$ac_ct_CC +else + CC="$ac_cv_prog_CC" +fi + +if test -z "$CC"; then + if test -n "$ac_tool_prefix"; then + # Extract the first word of "${ac_tool_prefix}cc", so it can be a program name with args. +set dummy ${ac_tool_prefix}cc; ac_word=$2 +echo "$as_me:1003: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$CC"; then + ac_cv_prog_CC="$CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_CC="${ac_tool_prefix}cc" +echo "$as_me:1018: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +CC=$ac_cv_prog_CC +if test -n "$CC"; then + echo "$as_me:1026: result: $CC" >&5 +echo "${ECHO_T}$CC" >&6 +else + echo "$as_me:1029: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + +fi +if test -z "$ac_cv_prog_CC"; then + ac_ct_CC=$CC + # Extract the first word of "cc", so it can be a program name with args. +set dummy cc; ac_word=$2 +echo "$as_me:1038: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_ac_ct_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$ac_ct_CC"; then + ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_ac_ct_CC="cc" +echo "$as_me:1053: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +ac_ct_CC=$ac_cv_prog_ac_ct_CC +if test -n "$ac_ct_CC"; then + echo "$as_me:1061: result: $ac_ct_CC" >&5 +echo "${ECHO_T}$ac_ct_CC" >&6 +else + echo "$as_me:1064: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + + CC=$ac_ct_CC +else + CC="$ac_cv_prog_CC" +fi + +fi +if test -z "$CC"; then + # Extract the first word of "cc", so it can be a program name with args. +set dummy cc; ac_word=$2 +echo "$as_me:1077: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$CC"; then + ac_cv_prog_CC="$CC" # Let the user override the test. +else + ac_prog_rejected=no + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +if test "$ac_dir/$ac_word" = "/usr/ucb/cc"; then + ac_prog_rejected=yes + continue +fi +ac_cv_prog_CC="cc" +echo "$as_me:1097: found $ac_dir/$ac_word" >&5 +break +done + +if test $ac_prog_rejected = yes; then + # We found a bogon in the path, so make sure we never use it. + set dummy $ac_cv_prog_CC + shift + if test $# != 0; then + # We chose a different compiler from the bogus one. + # However, it has the same basename, so the bogon will be chosen + # first if we set CC to just the basename; use the full file name. + shift + set dummy "$ac_dir/$ac_word" ${1+"$@"} + shift + ac_cv_prog_CC="$@" + fi +fi +fi +fi +CC=$ac_cv_prog_CC +if test -n "$CC"; then + echo "$as_me:1119: result: $CC" >&5 +echo "${ECHO_T}$CC" >&6 +else + echo "$as_me:1122: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + +fi +if test -z "$CC"; then + if test -n "$ac_tool_prefix"; then + for ac_prog in cl + do + # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args. +set dummy $ac_tool_prefix$ac_prog; ac_word=$2 +echo "$as_me:1133: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$CC"; then + ac_cv_prog_CC="$CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_CC="$ac_tool_prefix$ac_prog" +echo "$as_me:1148: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +CC=$ac_cv_prog_CC +if test -n "$CC"; then + echo "$as_me:1156: result: $CC" >&5 +echo "${ECHO_T}$CC" >&6 +else + echo "$as_me:1159: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + + test -n "$CC" && break + done +fi +if test -z "$CC"; then + ac_ct_CC=$CC + for ac_prog in cl +do + # Extract the first word of "$ac_prog", so it can be a program name with args. +set dummy $ac_prog; ac_word=$2 +echo "$as_me:1172: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_ac_ct_CC+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$ac_ct_CC"; then + ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_ac_ct_CC="$ac_prog" +echo "$as_me:1187: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +ac_ct_CC=$ac_cv_prog_ac_ct_CC +if test -n "$ac_ct_CC"; then + echo "$as_me:1195: result: $ac_ct_CC" >&5 +echo "${ECHO_T}$ac_ct_CC" >&6 +else + echo "$as_me:1198: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + + test -n "$ac_ct_CC" && break +done + + CC=$ac_ct_CC +fi + +fi + +test -z "$CC" && { { echo "$as_me:1210: error: no acceptable cc found in \$PATH" >&5 +echo "$as_me: error: no acceptable cc found in \$PATH" >&2;} + { (exit 1); exit 1; }; } + +# Provide some information about the compiler. +echo "$as_me:1215:" \ + "checking for C compiler version" >&5 +ac_compiler=`set X $ac_compile; echo $2` +{ (eval echo "$as_me:1218: \"$ac_compiler --version &5\"") >&5 + (eval $ac_compiler --version &5) 2>&5 + ac_status=$? + echo "$as_me:1221: \$? = $ac_status" >&5 + (exit $ac_status); } +{ (eval echo "$as_me:1223: \"$ac_compiler -v &5\"") >&5 + (eval $ac_compiler -v &5) 2>&5 + ac_status=$? + echo "$as_me:1226: \$? = $ac_status" >&5 + (exit $ac_status); } +{ (eval echo "$as_me:1228: \"$ac_compiler -V &5\"") >&5 + (eval $ac_compiler -V &5) 2>&5 + ac_status=$? + echo "$as_me:1231: \$? = $ac_status" >&5 + (exit $ac_status); } + +cat >conftest.$ac_ext <<_ACEOF +#line 1235 "configure" +#include "confdefs.h" + +int +main () +{ + + ; + return 0; +} +_ACEOF +ac_clean_files_save=$ac_clean_files +ac_clean_files="$ac_clean_files a.out a.exe" +# Try to create an executable without -o first, disregard a.out. +# It will help us diagnose broken compilers, and finding out an intuition +# of exeext. +echo "$as_me:1251: checking for C compiler default output" >&5 +echo $ECHO_N "checking for C compiler default output... $ECHO_C" >&6 +ac_link_default=`echo "$ac_link" | sed 's/ -o *conftest[^ ]*//'` +if { (eval echo "$as_me:1254: \"$ac_link_default\"") >&5 + (eval $ac_link_default) 2>&5 + ac_status=$? + echo "$as_me:1257: \$? = $ac_status" >&5 + (exit $ac_status); }; then + # Find the output, starting from the most likely. This scheme is +# not robust to junk in `.', hence go to wildcards (a.*) only as a last +# resort. +for ac_file in `ls a.exe conftest.exe 2>/dev/null; + ls a.out conftest 2>/dev/null; + ls a.* conftest.* 2>/dev/null`; do + case $ac_file in + *.$ac_ext | *.o | *.obj | *.xcoff | *.tds | *.d | *.pdb ) ;; + a.out ) # We found the default executable, but exeext='' is most + # certainly right. + break;; + *.* ) ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'` + # FIXME: I believe we export ac_cv_exeext for Libtool --akim. + export ac_cv_exeext + break;; + * ) break;; + esac +done +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +{ { echo "$as_me:1280: error: C compiler cannot create executables" >&5 +echo "$as_me: error: C compiler cannot create executables" >&2;} + { (exit 77); exit 77; }; } +fi + +ac_exeext=$ac_cv_exeext +echo "$as_me:1286: result: $ac_file" >&5 +echo "${ECHO_T}$ac_file" >&6 + +# Check the compiler produces executables we can run. If not, either +# the compiler is broken, or we cross compile. +echo "$as_me:1291: checking whether the C compiler works" >&5 +echo $ECHO_N "checking whether the C compiler works... $ECHO_C" >&6 +# FIXME: These cross compiler hacks should be removed for Autoconf 3.0 +# If not cross compiling, check that we can run a simple program. +if test "$cross_compiling" != yes; then + if { ac_try='./$ac_file' + { (eval echo "$as_me:1297: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1300: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + cross_compiling=no + else + if test "$cross_compiling" = maybe; then + cross_compiling=yes + else + { { echo "$as_me:1307: error: cannot run C compiled programs. +If you meant to cross compile, use \`--host'." >&5 +echo "$as_me: error: cannot run C compiled programs. +If you meant to cross compile, use \`--host'." >&2;} + { (exit 1); exit 1; }; } + fi + fi +fi +echo "$as_me:1315: result: yes" >&5 +echo "${ECHO_T}yes" >&6 + +rm -f a.out a.exe conftest$ac_cv_exeext +ac_clean_files=$ac_clean_files_save +# Check the compiler produces executables we can run. If not, either +# the compiler is broken, or we cross compile. +echo "$as_me:1322: checking whether we are cross compiling" >&5 +echo $ECHO_N "checking whether we are cross compiling... $ECHO_C" >&6 +echo "$as_me:1324: result: $cross_compiling" >&5 +echo "${ECHO_T}$cross_compiling" >&6 + +echo "$as_me:1327: checking for executable suffix" >&5 +echo $ECHO_N "checking for executable suffix... $ECHO_C" >&6 +if { (eval echo "$as_me:1329: \"$ac_link\"") >&5 + (eval $ac_link) 2>&5 + ac_status=$? + echo "$as_me:1332: \$? = $ac_status" >&5 + (exit $ac_status); }; then + # If both `conftest.exe' and `conftest' are `present' (well, observable) +# catch `conftest.exe'. For instance with Cygwin, `ls conftest' will +# work properly (i.e., refer to `conftest.exe'), while it won't with +# `rm'. +for ac_file in `(ls conftest.exe; ls conftest; ls conftest.*) 2>/dev/null`; do + case $ac_file in + *.$ac_ext | *.o | *.obj | *.xcoff | *.tds | *.d | *.pdb ) ;; + *.* ) ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'` + export ac_cv_exeext + break;; + * ) break;; + esac +done +else + { { echo "$as_me:1348: error: cannot compute EXEEXT: cannot compile and link" >&5 +echo "$as_me: error: cannot compute EXEEXT: cannot compile and link" >&2;} + { (exit 1); exit 1; }; } +fi + +rm -f conftest$ac_cv_exeext +echo "$as_me:1354: result: $ac_cv_exeext" >&5 +echo "${ECHO_T}$ac_cv_exeext" >&6 + +rm -f conftest.$ac_ext +EXEEXT=$ac_cv_exeext +ac_exeext=$EXEEXT +echo "$as_me:1360: checking for object suffix" >&5 +echo $ECHO_N "checking for object suffix... $ECHO_C" >&6 +if test "${ac_cv_objext+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line 1366 "configure" +#include "confdefs.h" + +int +main () +{ + + ; + return 0; +} +_ACEOF +rm -f conftest.o conftest.obj +if { (eval echo "$as_me:1378: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1381: \$? = $ac_status" >&5 + (exit $ac_status); }; then + for ac_file in `(ls conftest.o conftest.obj; ls conftest.*) 2>/dev/null`; do + case $ac_file in + *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb ) ;; + *) ac_cv_objext=`expr "$ac_file" : '.*\.\(.*\)'` + break;; + esac +done +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +{ { echo "$as_me:1393: error: cannot compute OBJEXT: cannot compile" >&5 +echo "$as_me: error: cannot compute OBJEXT: cannot compile" >&2;} + { (exit 1); exit 1; }; } +fi + +rm -f conftest.$ac_cv_objext conftest.$ac_ext +fi +echo "$as_me:1400: result: $ac_cv_objext" >&5 +echo "${ECHO_T}$ac_cv_objext" >&6 +OBJEXT=$ac_cv_objext +ac_objext=$OBJEXT +echo "$as_me:1404: checking whether we are using the GNU C compiler" >&5 +echo $ECHO_N "checking whether we are using the GNU C compiler... $ECHO_C" >&6 +if test "${ac_cv_c_compiler_gnu+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line 1410 "configure" +#include "confdefs.h" + +int +main () +{ +#ifndef __GNUC__ + choke me +#endif + + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:1425: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1428: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:1431: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1434: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_compiler_gnu=yes +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +ac_compiler_gnu=no +fi +rm -f conftest.$ac_objext conftest.$ac_ext +ac_cv_c_compiler_gnu=$ac_compiler_gnu + +fi +echo "$as_me:1446: result: $ac_cv_c_compiler_gnu" >&5 +echo "${ECHO_T}$ac_cv_c_compiler_gnu" >&6 +GCC=`test $ac_compiler_gnu = yes && echo yes` +ac_test_CFLAGS=${CFLAGS+set} +ac_save_CFLAGS=$CFLAGS +CFLAGS="-g" +echo "$as_me:1452: checking whether $CC accepts -g" >&5 +echo $ECHO_N "checking whether $CC accepts -g... $ECHO_C" >&6 +if test "${ac_cv_prog_cc_g+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line 1458 "configure" +#include "confdefs.h" + +int +main () +{ + + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:1470: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1473: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:1476: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1479: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_prog_cc_g=yes +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +ac_cv_prog_cc_g=no +fi +rm -f conftest.$ac_objext conftest.$ac_ext +fi +echo "$as_me:1489: result: $ac_cv_prog_cc_g" >&5 +echo "${ECHO_T}$ac_cv_prog_cc_g" >&6 +if test "$ac_test_CFLAGS" = set; then + CFLAGS=$ac_save_CFLAGS +elif test $ac_cv_prog_cc_g = yes; then + if test "$GCC" = yes; then + CFLAGS="-g -O2" + else + CFLAGS="-g" + fi +else + if test "$GCC" = yes; then + CFLAGS="-O2" + else + CFLAGS= + fi +fi +# Some people use a C++ compiler to compile C. Since we use `exit', +# in C++ we need to declare it. In case someone uses the same compiler +# for both compiling C and C++ we need to have the C++ compiler decide +# the declaration of exit, since it's the most demanding environment. +cat >conftest.$ac_ext <<_ACEOF +#ifndef __cplusplus + choke me +#endif +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:1516: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1519: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:1522: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1525: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + for ac_declaration in \ + ''\ + '#include ' \ + 'extern "C" void std::exit (int) throw (); using std::exit;' \ + 'extern "C" void std::exit (int); using std::exit;' \ + 'extern "C" void exit (int) throw ();' \ + 'extern "C" void exit (int);' \ + 'void exit (int);' +do + cat >conftest.$ac_ext <<_ACEOF +#line 1537 "configure" +#include "confdefs.h" +#include +$ac_declaration +int +main () +{ +exit (42); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:1550: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1553: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:1556: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1559: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + : +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +continue +fi +rm -f conftest.$ac_objext conftest.$ac_ext + cat >conftest.$ac_ext <<_ACEOF +#line 1569 "configure" +#include "confdefs.h" +$ac_declaration +int +main () +{ +exit (42); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:1581: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:1584: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:1587: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1590: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + break +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +fi +rm -f conftest.$ac_objext conftest.$ac_ext +done +rm -f conftest* +if test -n "$ac_declaration"; then + echo '#ifdef __cplusplus' >>confdefs.h + echo $ac_declaration >>confdefs.h + echo '#endif' >>confdefs.h +fi + +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +fi +rm -f conftest.$ac_objext conftest.$ac_ext +ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu + +if test -n "$ac_tool_prefix"; then + # Extract the first word of "${ac_tool_prefix}ranlib", so it can be a program name with args. +set dummy ${ac_tool_prefix}ranlib; ac_word=$2 +echo "$as_me:1620: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_RANLIB+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$RANLIB"; then + ac_cv_prog_RANLIB="$RANLIB" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_RANLIB="${ac_tool_prefix}ranlib" +echo "$as_me:1635: found $ac_dir/$ac_word" >&5 +break +done + +fi +fi +RANLIB=$ac_cv_prog_RANLIB +if test -n "$RANLIB"; then + echo "$as_me:1643: result: $RANLIB" >&5 +echo "${ECHO_T}$RANLIB" >&6 +else + echo "$as_me:1646: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + +fi +if test -z "$ac_cv_prog_RANLIB"; then + ac_ct_RANLIB=$RANLIB + # Extract the first word of "ranlib", so it can be a program name with args. +set dummy ranlib; ac_word=$2 +echo "$as_me:1655: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_prog_ac_ct_RANLIB+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + if test -n "$ac_ct_RANLIB"; then + ac_cv_prog_ac_ct_RANLIB="$ac_ct_RANLIB" # Let the user override the test. +else + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + $as_executable_p "$ac_dir/$ac_word" || continue +ac_cv_prog_ac_ct_RANLIB="ranlib" +echo "$as_me:1670: found $ac_dir/$ac_word" >&5 +break +done + + test -z "$ac_cv_prog_ac_ct_RANLIB" && ac_cv_prog_ac_ct_RANLIB=":" +fi +fi +ac_ct_RANLIB=$ac_cv_prog_ac_ct_RANLIB +if test -n "$ac_ct_RANLIB"; then + echo "$as_me:1679: result: $ac_ct_RANLIB" >&5 +echo "${ECHO_T}$ac_ct_RANLIB" >&6 +else + echo "$as_me:1682: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + + RANLIB=$ac_ct_RANLIB +else + RANLIB="$ac_cv_prog_RANLIB" +fi + +# Extract the first word of "ar", so it can be a program name with args. +set dummy ar; ac_word=$2 +echo "$as_me:1693: checking for $ac_word" >&5 +echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6 +if test "${ac_cv_path_AR+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + case $AR in + [\\/]* | ?:[\\/]*) + ac_cv_path_AR="$AR" # Let the user override the test with a path. + ;; + *) + ac_save_IFS=$IFS; IFS=$ac_path_separator +ac_dummy="$PATH" +for ac_dir in $ac_dummy; do + IFS=$ac_save_IFS + test -z "$ac_dir" && ac_dir=. + if $as_executable_p "$ac_dir/$ac_word"; then + ac_cv_path_AR="$ac_dir/$ac_word" + echo "$as_me:1710: found $ac_dir/$ac_word" >&5 + break +fi +done + + test -z "$ac_cv_path_AR" && ac_cv_path_AR="no" + ;; +esac +fi +AR=$ac_cv_path_AR + +if test -n "$AR"; then + echo "$as_me:1722: result: $AR" >&5 +echo "${ECHO_T}$AR" >&6 +else + echo "$as_me:1725: result: no" >&5 +echo "${ECHO_T}no" >&6 +fi + +if [ $AR = "no" ] ; then + { { echo "$as_me:1730: error: \"Could not find ar - needed to create a library\"" >&5 +echo "$as_me: error: \"Could not find ar - needed to create a library\"" >&2;} + { (exit 1); exit 1; }; }; +fi + +TARGETS="libresample.a tests/testresample" + +echo "$as_me:1737: checking for sf_open in -lsndfile" >&5 +echo $ECHO_N "checking for sf_open in -lsndfile... $ECHO_C" >&6 +if test "${ac_cv_lib_sndfile_sf_open+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-lsndfile $LIBS" +cat >conftest.$ac_ext <<_ACEOF +#line 1745 "configure" +#include "confdefs.h" + +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char sf_open (); +int +main () +{ +sf_open (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:1764: \"$ac_link\"") >&5 + (eval $ac_link) 2>&5 + ac_status=$? + echo "$as_me:1767: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:1770: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1773: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_lib_sndfile_sf_open=yes +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +ac_cv_lib_sndfile_sf_open=no +fi +rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +echo "$as_me:1784: result: $ac_cv_lib_sndfile_sf_open" >&5 +echo "${ECHO_T}$ac_cv_lib_sndfile_sf_open" >&6 +if test $ac_cv_lib_sndfile_sf_open = yes; then + have_libsndfile=yes +else + have_libsndfile=no +fi + +if [ $have_libsndfile = "yes" ] ; then + TARGETS="$TARGETS tests/resample-sndfile" +fi + +echo "$as_me:1796: checking for src_simple in -lsamplerate" >&5 +echo $ECHO_N "checking for src_simple in -lsamplerate... $ECHO_C" >&6 +if test "${ac_cv_lib_samplerate_src_simple+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-lsamplerate $LIBS" +cat >conftest.$ac_ext <<_ACEOF +#line 1804 "configure" +#include "confdefs.h" + +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char src_simple (); +int +main () +{ +src_simple (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:1823: \"$ac_link\"") >&5 + (eval $ac_link) 2>&5 + ac_status=$? + echo "$as_me:1826: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:1829: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:1832: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_lib_samplerate_src_simple=yes +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +ac_cv_lib_samplerate_src_simple=no +fi +rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +echo "$as_me:1843: result: $ac_cv_lib_samplerate_src_simple" >&5 +echo "${ECHO_T}$ac_cv_lib_samplerate_src_simple" >&6 +if test $ac_cv_lib_samplerate_src_simple = yes; then + have_libsamplerate=yes +else + have_libsamplerate=no +fi + +if [ $have_libsamplerate = "yes" ] ; then + TARGETS="$TARGETS tests/compareresample" +fi + +ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu +echo "$as_me:1860: checking how to run the C preprocessor" >&5 +echo $ECHO_N "checking how to run the C preprocessor... $ECHO_C" >&6 +# On Suns, sometimes $CPP names a directory. +if test -n "$CPP" && test -d "$CPP"; then + CPP= +fi +if test -z "$CPP"; then + if test "${ac_cv_prog_CPP+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + # Double quotes because CPP needs to be expanded + for CPP in "$CC -E" "$CC -E -traditional-cpp" "/lib/cpp" + do + ac_preproc_ok=false +for ac_c_preproc_warn_flag in '' yes +do + # Use a header file that comes with gcc, so configuring glibc + # with a fresh cross-compiler works. + # On the NeXT, cc -E runs the code through the compiler's parser, + # not just through cpp. "Syntax error" is here to catch this case. + cat >conftest.$ac_ext <<_ACEOF +#line 1881 "configure" +#include "confdefs.h" +#include + Syntax error +_ACEOF +if { (eval echo "$as_me:1886: \"$ac_cpp conftest.$ac_ext\"") >&5 + (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 + ac_status=$? + egrep -v '^ *\+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:1892: \$? = $ac_status" >&5 + (exit $ac_status); } >/dev/null; then + if test -s conftest.err; then + ac_cpp_err=$ac_c_preproc_warn_flag + else + ac_cpp_err= + fi +else + ac_cpp_err=yes +fi +if test -z "$ac_cpp_err"; then + : +else + echo "$as_me: failed program was:" >&5 + cat conftest.$ac_ext >&5 + # Broken: fails on valid input. +continue +fi +rm -f conftest.err conftest.$ac_ext + + # OK, works on sane cases. Now check whether non-existent headers + # can be detected and how. + cat >conftest.$ac_ext <<_ACEOF +#line 1915 "configure" +#include "confdefs.h" +#include +_ACEOF +if { (eval echo "$as_me:1919: \"$ac_cpp conftest.$ac_ext\"") >&5 + (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 + ac_status=$? + egrep -v '^ *\+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:1925: \$? = $ac_status" >&5 + (exit $ac_status); } >/dev/null; then + if test -s conftest.err; then + ac_cpp_err=$ac_c_preproc_warn_flag + else + ac_cpp_err= + fi +else + ac_cpp_err=yes +fi +if test -z "$ac_cpp_err"; then + # Broken: success on invalid input. +continue +else + echo "$as_me: failed program was:" >&5 + cat conftest.$ac_ext >&5 + # Passes both tests. +ac_preproc_ok=: +break +fi +rm -f conftest.err conftest.$ac_ext + +done +# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped. +rm -f conftest.err conftest.$ac_ext +if $ac_preproc_ok; then + break +fi + + done + ac_cv_prog_CPP=$CPP + +fi + CPP=$ac_cv_prog_CPP +else + ac_cv_prog_CPP=$CPP +fi +echo "$as_me:1962: result: $CPP" >&5 +echo "${ECHO_T}$CPP" >&6 +ac_preproc_ok=false +for ac_c_preproc_warn_flag in '' yes +do + # Use a header file that comes with gcc, so configuring glibc + # with a fresh cross-compiler works. + # On the NeXT, cc -E runs the code through the compiler's parser, + # not just through cpp. "Syntax error" is here to catch this case. + cat >conftest.$ac_ext <<_ACEOF +#line 1972 "configure" +#include "confdefs.h" +#include + Syntax error +_ACEOF +if { (eval echo "$as_me:1977: \"$ac_cpp conftest.$ac_ext\"") >&5 + (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 + ac_status=$? + egrep -v '^ *\+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:1983: \$? = $ac_status" >&5 + (exit $ac_status); } >/dev/null; then + if test -s conftest.err; then + ac_cpp_err=$ac_c_preproc_warn_flag + else + ac_cpp_err= + fi +else + ac_cpp_err=yes +fi +if test -z "$ac_cpp_err"; then + : +else + echo "$as_me: failed program was:" >&5 + cat conftest.$ac_ext >&5 + # Broken: fails on valid input. +continue +fi +rm -f conftest.err conftest.$ac_ext + + # OK, works on sane cases. Now check whether non-existent headers + # can be detected and how. + cat >conftest.$ac_ext <<_ACEOF +#line 2006 "configure" +#include "confdefs.h" +#include +_ACEOF +if { (eval echo "$as_me:2010: \"$ac_cpp conftest.$ac_ext\"") >&5 + (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 + ac_status=$? + egrep -v '^ *\+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:2016: \$? = $ac_status" >&5 + (exit $ac_status); } >/dev/null; then + if test -s conftest.err; then + ac_cpp_err=$ac_c_preproc_warn_flag + else + ac_cpp_err= + fi +else + ac_cpp_err=yes +fi +if test -z "$ac_cpp_err"; then + # Broken: success on invalid input. +continue +else + echo "$as_me: failed program was:" >&5 + cat conftest.$ac_ext >&5 + # Passes both tests. +ac_preproc_ok=: +break +fi +rm -f conftest.err conftest.$ac_ext + +done +# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped. +rm -f conftest.err conftest.$ac_ext +if $ac_preproc_ok; then + : +else + { { echo "$as_me:2044: error: C preprocessor \"$CPP\" fails sanity check" >&5 +echo "$as_me: error: C preprocessor \"$CPP\" fails sanity check" >&2;} + { (exit 1); exit 1; }; } +fi + +ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu + +for ac_header in inttypes.h +do +as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh` +echo "$as_me:2058: checking for $ac_header" >&5 +echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6 +if eval "test \"\${$as_ac_Header+set}\" = set"; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line 2064 "configure" +#include "confdefs.h" +#include <$ac_header> +_ACEOF +if { (eval echo "$as_me:2068: \"$ac_cpp conftest.$ac_ext\"") >&5 + (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 + ac_status=$? + egrep -v '^ *\+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:2074: \$? = $ac_status" >&5 + (exit $ac_status); } >/dev/null; then + if test -s conftest.err; then + ac_cpp_err=$ac_c_preproc_warn_flag + else + ac_cpp_err= + fi +else + ac_cpp_err=yes +fi +if test -z "$ac_cpp_err"; then + eval "$as_ac_Header=yes" +else + echo "$as_me: failed program was:" >&5 + cat conftest.$ac_ext >&5 + eval "$as_ac_Header=no" +fi +rm -f conftest.err conftest.$ac_ext +fi +echo "$as_me:2093: result: `eval echo '${'$as_ac_Header'}'`" >&5 +echo "${ECHO_T}`eval echo '${'$as_ac_Header'}'`" >&6 +if test `eval echo '${'$as_ac_Header'}'` = yes; then + cat >>confdefs.h <confcache <<\_ACEOF +# This file is a shell script that caches the results of configure +# tests run on this system so they can be shared between configure +# scripts and configure runs, see configure's option --config-cache. +# It is not useful on other systems. If it contains results you don't +# want to keep, you may remove or edit it. +# +# config.status only pays attention to the cache file if you give it +# the --recheck option to rerun configure. +# +# `ac_cv_env_foo' variables (set or unset) will be overriden when +# loading this file, other *unset* `ac_cv_foo' will be assigned the +# following values. + +_ACEOF + +# The following way of writing the cache mishandles newlines in values, +# but we know of no workaround that is simple, portable, and efficient. +# So, don't put newlines in cache variables' values. +# Ultrix sh set writes to stderr and can't be redirected directly, +# and sets the high bit in the cache file unless we assign to the vars. +{ + (set) 2>&1 | + case `(ac_space=' '; set | grep ac_space) 2>&1` in + *ac_space=\ *) + # `set' does not quote correctly, so add quotes (double-quote + # substitution turns \\\\ into \\, and sed turns \\ into \). + sed -n \ + "s/'/'\\\\''/g; + s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\\2'/p" + ;; + *) + # `set' quotes correctly as required by POSIX, so do not add quotes. + sed -n \ + "s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1=\\2/p" + ;; + esac; +} | + sed ' + t clear + : clear + s/^\([^=]*\)=\(.*[{}].*\)$/test "${\1+set}" = set || &/ + t end + /^ac_cv_env/!s/^\([^=]*\)=\(.*\)$/\1=${\1=\2}/ + : end' >>confcache +if cmp -s $cache_file confcache; then :; else + if test -w $cache_file; then + test "x$cache_file" != "x/dev/null" && echo "updating cache $cache_file" + cat confcache >$cache_file + else + echo "not updating unwritable cache $cache_file" + fi +fi +rm -f confcache + +test "x$prefix" = xNONE && prefix=$ac_default_prefix +# Let make expand exec_prefix. +test "x$exec_prefix" = xNONE && exec_prefix='${prefix}' + +# VPATH may cause trouble with some makes, so we remove $(srcdir), +# ${srcdir} and @srcdir@ from VPATH if srcdir is ".", strip leading and +# trailing colons and then remove the whole line if VPATH becomes empty +# (actually we leave an empty line to preserve line numbers). +if test "x$srcdir" = x.; then + ac_vpsub='/^[ ]*VPATH[ ]*=/{ +s/:*\$(srcdir):*/:/; +s/:*\${srcdir}:*/:/; +s/:*@srcdir@:*/:/; +s/^\([^=]*=[ ]*\):*/\1/; +s/:*$//; +s/^[^=]*=[ ]*$//; +}' +fi + +DEFS=-DHAVE_CONFIG_H + +: ${CONFIG_STATUS=./config.status} +ac_clean_files_save=$ac_clean_files +ac_clean_files="$ac_clean_files $CONFIG_STATUS" +{ echo "$as_me:2185: creating $CONFIG_STATUS" >&5 +echo "$as_me: creating $CONFIG_STATUS" >&6;} +cat >$CONFIG_STATUS <<_ACEOF +#! $SHELL +# Generated automatically by configure. +# Run this file to recreate the current configuration. +# Compiler output produced by configure, useful for debugging +# configure, is in config.log if it exists. + +debug=false +SHELL=\${CONFIG_SHELL-$SHELL} +ac_cs_invocation="\$0 \$@" + +_ACEOF + +cat >>$CONFIG_STATUS <<\_ACEOF +# Be Bourne compatible +if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then + emulate sh + NULLCMD=: +elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then + set -o posix +fi + +# Name of the executable. +as_me=`echo "$0" |sed 's,.*[\\/],,'` + +if expr a : '\(a\)' >/dev/null 2>&1; then + as_expr=expr +else + as_expr=false +fi + +rm -f conf$$ conf$$.exe conf$$.file +echo >conf$$.file +if ln -s conf$$.file conf$$ 2>/dev/null; then + # We could just check for DJGPP; but this test a) works b) is more generic + # and c) will remain valid once DJGPP supports symlinks (DJGPP 2.04). + if test -f conf$$.exe; then + # Don't use ln at all; we don't have any links + as_ln_s='cp -p' + else + as_ln_s='ln -s' + fi +elif ln conf$$.file conf$$ 2>/dev/null; then + as_ln_s=ln +else + as_ln_s='cp -p' +fi +rm -f conf$$ conf$$.exe conf$$.file + +as_executable_p="test -f" + +# Support unset when possible. +if (FOO=FOO; unset FOO) >/dev/null 2>&1; then + as_unset=unset +else + as_unset=false +fi + +# NLS nuisances. +$as_unset LANG || test "${LANG+set}" != set || { LANG=C; export LANG; } +$as_unset LC_ALL || test "${LC_ALL+set}" != set || { LC_ALL=C; export LC_ALL; } +$as_unset LC_TIME || test "${LC_TIME+set}" != set || { LC_TIME=C; export LC_TIME; } +$as_unset LC_CTYPE || test "${LC_CTYPE+set}" != set || { LC_CTYPE=C; export LC_CTYPE; } +$as_unset LANGUAGE || test "${LANGUAGE+set}" != set || { LANGUAGE=C; export LANGUAGE; } +$as_unset LC_COLLATE || test "${LC_COLLATE+set}" != set || { LC_COLLATE=C; export LC_COLLATE; } +$as_unset LC_NUMERIC || test "${LC_NUMERIC+set}" != set || { LC_NUMERIC=C; export LC_NUMERIC; } +$as_unset LC_MESSAGES || test "${LC_MESSAGES+set}" != set || { LC_MESSAGES=C; export LC_MESSAGES; } + +# IFS +# We need space, tab and new line, in precisely that order. +as_nl=' +' +IFS=" $as_nl" + +# CDPATH. +$as_unset CDPATH || test "${CDPATH+set}" != set || { CDPATH=:; export CDPATH; } + +exec 6>&1 + +_ACEOF + +# Files that config.status was made for. +if test -n "$ac_config_files"; then + echo "config_files=\"$ac_config_files\"" >>$CONFIG_STATUS +fi + +if test -n "$ac_config_headers"; then + echo "config_headers=\"$ac_config_headers\"" >>$CONFIG_STATUS +fi + +if test -n "$ac_config_links"; then + echo "config_links=\"$ac_config_links\"" >>$CONFIG_STATUS +fi + +if test -n "$ac_config_commands"; then + echo "config_commands=\"$ac_config_commands\"" >>$CONFIG_STATUS +fi + +cat >>$CONFIG_STATUS <<\EOF + +ac_cs_usage="\ +\`$as_me' instantiates files from templates according to the +current configuration. + +Usage: $0 [OPTIONS] [FILE]... + + -h, --help print this help, then exit + -V, --version print version number, then exit + -d, --debug don't remove temporary files + --recheck update $as_me by reconfiguring in the same conditions + --file=FILE[:TEMPLATE] + instantiate the configuration file FILE + --header=FILE[:TEMPLATE] + instantiate the configuration header FILE + +Configuration files: +$config_files + +Configuration headers: +$config_headers + +Report bugs to ." +EOF + +cat >>$CONFIG_STATUS <>$CONFIG_STATUS <<\EOF +# If no file are specified by the user, then we need to provide default +# value. By we need to know if files were specified by the user. +ac_need_defaults=: +while test $# != 0 +do + case $1 in + --*=*) + ac_option=`expr "x$1" : 'x\([^=]*\)='` + ac_optarg=`expr "x$1" : 'x[^=]*=\(.*\)'` + shift + set dummy "$ac_option" "$ac_optarg" ${1+"$@"} + shift + ;; + -*);; + *) # This is not an option, so the user has probably given explicit + # arguments. + ac_need_defaults=false;; + esac + + case $1 in + # Handling of the options. +EOF +cat >>$CONFIG_STATUS <>$CONFIG_STATUS <<\EOF + --version | --vers* | -V ) + echo "$ac_cs_version"; exit 0 ;; + --he | --h) + # Conflict between --help and --header + { { echo "$as_me:2357: error: ambiguous option: $1 +Try \`$0 --help' for more information." >&5 +echo "$as_me: error: ambiguous option: $1 +Try \`$0 --help' for more information." >&2;} + { (exit 1); exit 1; }; };; + --help | --hel | -h ) + echo "$ac_cs_usage"; exit 0 ;; + --debug | --d* | -d ) + debug=: ;; + --file | --fil | --fi | --f ) + shift + CONFIG_FILES="$CONFIG_FILES $1" + ac_need_defaults=false;; + --header | --heade | --head | --hea ) + shift + CONFIG_HEADERS="$CONFIG_HEADERS $1" + ac_need_defaults=false;; + + # This is an error. + -*) { { echo "$as_me:2376: error: unrecognized option: $1 +Try \`$0 --help' for more information." >&5 +echo "$as_me: error: unrecognized option: $1 +Try \`$0 --help' for more information." >&2;} + { (exit 1); exit 1; }; } ;; + + *) ac_config_targets="$ac_config_targets $1" ;; + + esac + shift +done + +exec 5>>config.log +cat >&5 << _ACEOF + +## ----------------------- ## +## Running config.status. ## +## ----------------------- ## + +This file was extended by $as_me 2.52, executed with + CONFIG_FILES = $CONFIG_FILES + CONFIG_HEADERS = $CONFIG_HEADERS + CONFIG_LINKS = $CONFIG_LINKS + CONFIG_COMMANDS = $CONFIG_COMMANDS + > $ac_cs_invocation +on `(hostname || uname -n) 2>/dev/null | sed 1q` + +_ACEOF +EOF + +cat >>$CONFIG_STATUS <<\EOF +for ac_config_target in $ac_config_targets +do + case "$ac_config_target" in + # Handling of arguments. + "Makefile" ) CONFIG_FILES="$CONFIG_FILES Makefile" ;; + "src/config.h" ) CONFIG_HEADERS="$CONFIG_HEADERS src/config.h:src/configtemplate.h" ;; + *) { { echo "$as_me:2413: error: invalid argument: $ac_config_target" >&5 +echo "$as_me: error: invalid argument: $ac_config_target" >&2;} + { (exit 1); exit 1; }; };; + esac +done + +# If the user did not use the arguments to specify the items to instantiate, +# then the envvar interface is used. Set only those that are not. +# We use the long form for the default assignment because of an extremely +# bizarre bug on SunOS 4.1.3. +if $ac_need_defaults; then + test "${CONFIG_FILES+set}" = set || CONFIG_FILES=$config_files + test "${CONFIG_HEADERS+set}" = set || CONFIG_HEADERS=$config_headers +fi + +# Create a temporary directory, and hook for its removal unless debugging. +$debug || +{ + trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 + trap '{ (exit 1); exit 1; }' 1 2 13 15 +} + +# Create a (secure) tmp directory for tmp files. +: ${TMPDIR=/tmp} +{ + tmp=`(umask 077 && mktemp -d -q "$TMPDIR/csXXXXXX") 2>/dev/null` && + test -n "$tmp" && test -d "$tmp" +} || +{ + tmp=$TMPDIR/cs$$-$RANDOM + (umask 077 && mkdir $tmp) +} || +{ + echo "$me: cannot create a temporary directory in $TMPDIR" >&2 + { (exit 1); exit 1; } +} + +EOF + +cat >>$CONFIG_STATUS <\$tmp/subs.sed <<\\CEOF +s,@SHELL@,$SHELL,;t t +s,@exec_prefix@,$exec_prefix,;t t +s,@prefix@,$prefix,;t t +s,@program_transform_name@,$program_transform_name,;t t +s,@bindir@,$bindir,;t t +s,@sbindir@,$sbindir,;t t +s,@libexecdir@,$libexecdir,;t t +s,@datadir@,$datadir,;t t +s,@sysconfdir@,$sysconfdir,;t t +s,@sharedstatedir@,$sharedstatedir,;t t +s,@localstatedir@,$localstatedir,;t t +s,@libdir@,$libdir,;t t +s,@includedir@,$includedir,;t t +s,@oldincludedir@,$oldincludedir,;t t +s,@infodir@,$infodir,;t t +s,@mandir@,$mandir,;t t +s,@PACKAGE_NAME@,$PACKAGE_NAME,;t t +s,@PACKAGE_TARNAME@,$PACKAGE_TARNAME,;t t +s,@PACKAGE_VERSION@,$PACKAGE_VERSION,;t t +s,@PACKAGE_STRING@,$PACKAGE_STRING,;t t +s,@PACKAGE_BUGREPORT@,$PACKAGE_BUGREPORT,;t t +s,@build_alias@,$build_alias,;t t +s,@host_alias@,$host_alias,;t t +s,@target_alias@,$target_alias,;t t +s,@ECHO_C@,$ECHO_C,;t t +s,@ECHO_N@,$ECHO_N,;t t +s,@ECHO_T@,$ECHO_T,;t t +s,@PATH_SEPARATOR@,$PATH_SEPARATOR,;t t +s,@DEFS@,$DEFS,;t t +s,@LIBS@,$LIBS,;t t +s,@CC@,$CC,;t t +s,@CFLAGS@,$CFLAGS,;t t +s,@LDFLAGS@,$LDFLAGS,;t t +s,@CPPFLAGS@,$CPPFLAGS,;t t +s,@ac_ct_CC@,$ac_ct_CC,;t t +s,@EXEEXT@,$EXEEXT,;t t +s,@OBJEXT@,$OBJEXT,;t t +s,@RANLIB@,$RANLIB,;t t +s,@ac_ct_RANLIB@,$ac_ct_RANLIB,;t t +s,@AR@,$AR,;t t +s,@TARGETS@,$TARGETS,;t t +s,@CPP@,$CPP,;t t +CEOF + +EOF + + cat >>$CONFIG_STATUS <<\EOF + # Split the substitutions into bite-sized pieces for seds with + # small command number limits, like on Digital OSF/1 and HP-UX. + ac_max_sed_lines=48 + ac_sed_frag=1 # Number of current file. + ac_beg=1 # First line for current file. + ac_end=$ac_max_sed_lines # Line after last line for current file. + ac_more_lines=: + ac_sed_cmds= + while $ac_more_lines; do + if test $ac_beg -gt 1; then + sed "1,${ac_beg}d; ${ac_end}q" $tmp/subs.sed >$tmp/subs.frag + else + sed "${ac_end}q" $tmp/subs.sed >$tmp/subs.frag + fi + if test ! -s $tmp/subs.frag; then + ac_more_lines=false + else + # The purpose of the label and of the branching condition is to + # speed up the sed processing (if there are no `@' at all, there + # is no need to browse any of the substitutions). + # These are the two extra sed commands mentioned above. + (echo ':t + /@[a-zA-Z_][a-zA-Z_0-9]*@/!b' && cat $tmp/subs.frag) >$tmp/subs-$ac_sed_frag.sed + if test -z "$ac_sed_cmds"; then + ac_sed_cmds="sed -f $tmp/subs-$ac_sed_frag.sed" + else + ac_sed_cmds="$ac_sed_cmds | sed -f $tmp/subs-$ac_sed_frag.sed" + fi + ac_sed_frag=`expr $ac_sed_frag + 1` + ac_beg=$ac_end + ac_end=`expr $ac_end + $ac_max_sed_lines` + fi + done + if test -z "$ac_sed_cmds"; then + ac_sed_cmds=cat + fi +fi # test -n "$CONFIG_FILES" + +EOF +cat >>$CONFIG_STATUS <<\EOF +for ac_file in : $CONFIG_FILES; do test "x$ac_file" = x: && continue + # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in". + case $ac_file in + - | *:- | *:-:* ) # input from stdin + cat >$tmp/stdin + ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'` + ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;; + *:* ) ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'` + ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;; + * ) ac_file_in=$ac_file.in ;; + esac + + # Compute @srcdir@, @top_srcdir@, and @INSTALL@ for subdirectories. + ac_dir=`$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ + X"$ac_file" : 'X\(//\)[^/]' \| \ + X"$ac_file" : 'X\(//\)$' \| \ + X"$ac_file" : 'X\(/\)' \| \ + . : '\(.\)' 2>/dev/null || +echo X"$ac_file" | + sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; } + /^X\(\/\/\)[^/].*/{ s//\1/; q; } + /^X\(\/\/\)$/{ s//\1/; q; } + /^X\(\/\).*/{ s//\1/; q; } + s/.*/./; q'` + if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then + { case "$ac_dir" in + [\\/]* | ?:[\\/]* ) as_incr_dir=;; + *) as_incr_dir=.;; +esac +as_dummy="$ac_dir" +for as_mkdir_dir in `IFS='/\\'; set X $as_dummy; shift; echo "$@"`; do + case $as_mkdir_dir in + # Skip DOS drivespec + ?:) as_incr_dir=$as_mkdir_dir ;; + *) + as_incr_dir=$as_incr_dir/$as_mkdir_dir + test -d "$as_incr_dir" || mkdir "$as_incr_dir" + ;; + esac +done; } + + ac_dir_suffix="/`echo $ac_dir|sed 's,^\./,,'`" + # A "../" for each directory in $ac_dir_suffix. + ac_dots=`echo "$ac_dir_suffix" | sed 's,/[^/]*,../,g'` + else + ac_dir_suffix= ac_dots= + fi + + case $srcdir in + .) ac_srcdir=. + if test -z "$ac_dots"; then + ac_top_srcdir=. + else + ac_top_srcdir=`echo $ac_dots | sed 's,/$,,'` + fi ;; + [\\/]* | ?:[\\/]* ) + ac_srcdir=$srcdir$ac_dir_suffix; + ac_top_srcdir=$srcdir ;; + *) # Relative path. + ac_srcdir=$ac_dots$srcdir$ac_dir_suffix + ac_top_srcdir=$ac_dots$srcdir ;; + esac + + if test x"$ac_file" != x-; then + { echo "$as_me:2615: creating $ac_file" >&5 +echo "$as_me: creating $ac_file" >&6;} + rm -f "$ac_file" + fi + # Let's still pretend it is `configure' which instantiates (i.e., don't + # use $as_me), people would be surprised to read: + # /* config.h. Generated automatically by config.status. */ + configure_input="Generated automatically from `echo $ac_file_in | + sed 's,.*/,,'` by configure." + + # First look for the input files in the build tree, otherwise in the + # src tree. + ac_file_inputs=`IFS=: + for f in $ac_file_in; do + case $f in + -) echo $tmp/stdin ;; + [\\/$]*) + # Absolute (can't be DOS-style, as IFS=:) + test -f "$f" || { { echo "$as_me:2633: error: cannot find input file: $f" >&5 +echo "$as_me: error: cannot find input file: $f" >&2;} + { (exit 1); exit 1; }; } + echo $f;; + *) # Relative + if test -f "$f"; then + # Build tree + echo $f + elif test -f "$srcdir/$f"; then + # Source tree + echo $srcdir/$f + else + # /dev/null tree + { { echo "$as_me:2646: error: cannot find input file: $f" >&5 +echo "$as_me: error: cannot find input file: $f" >&2;} + { (exit 1); exit 1; }; } + fi;; + esac + done` || { (exit 1); exit 1; } +EOF +cat >>$CONFIG_STATUS <>$CONFIG_STATUS <<\EOF +:t +/@[a-zA-Z_][a-zA-Z_0-9]*@/!b +s,@configure_input@,$configure_input,;t t +s,@srcdir@,$ac_srcdir,;t t +s,@top_srcdir@,$ac_top_srcdir,;t t +" $ac_file_inputs | (eval "$ac_sed_cmds") >$tmp/out + rm -f $tmp/stdin + if test x"$ac_file" != x-; then + mv $tmp/out $ac_file + else + cat $tmp/out + rm -f $tmp/out + fi + +done +EOF +cat >>$CONFIG_STATUS <<\EOF + +# +# CONFIG_HEADER section. +# + +# These sed commands are passed to sed as "A NAME B NAME C VALUE D", where +# NAME is the cpp macro being defined and VALUE is the value it is being given. +# +# ac_d sets the value in "#define NAME VALUE" lines. +ac_dA='s,^\([ ]*\)#\([ ]*define[ ][ ]*\)' +ac_dB='[ ].*$,\1#\2' +ac_dC=' ' +ac_dD=',;t' +# ac_u turns "#undef NAME" without trailing blanks into "#define NAME VALUE". +ac_uA='s,^\([ ]*\)#\([ ]*\)undef\([ ][ ]*\)' +ac_uB='$,\1#\2define\3' +ac_uC=' ' +ac_uD=',;t' + +for ac_file in : $CONFIG_HEADERS; do test "x$ac_file" = x: && continue + # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in". + case $ac_file in + - | *:- | *:-:* ) # input from stdin + cat >$tmp/stdin + ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'` + ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;; + *:* ) ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'` + ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;; + * ) ac_file_in=$ac_file.in ;; + esac + + test x"$ac_file" != x- && { echo "$as_me:2706: creating $ac_file" >&5 +echo "$as_me: creating $ac_file" >&6;} + + # First look for the input files in the build tree, otherwise in the + # src tree. + ac_file_inputs=`IFS=: + for f in $ac_file_in; do + case $f in + -) echo $tmp/stdin ;; + [\\/$]*) + # Absolute (can't be DOS-style, as IFS=:) + test -f "$f" || { { echo "$as_me:2717: error: cannot find input file: $f" >&5 +echo "$as_me: error: cannot find input file: $f" >&2;} + { (exit 1); exit 1; }; } + echo $f;; + *) # Relative + if test -f "$f"; then + # Build tree + echo $f + elif test -f "$srcdir/$f"; then + # Source tree + echo $srcdir/$f + else + # /dev/null tree + { { echo "$as_me:2730: error: cannot find input file: $f" >&5 +echo "$as_me: error: cannot find input file: $f" >&2;} + { (exit 1); exit 1; }; } + fi;; + esac + done` || { (exit 1); exit 1; } + # Remove the trailing spaces. + sed 's/[ ]*$//' $ac_file_inputs >$tmp/in + +EOF + +# Transform confdefs.h into two sed scripts, `conftest.defines' and +# `conftest.undefs', that substitutes the proper values into +# config.h.in to produce config.h. The first handles `#define' +# templates, and the second `#undef' templates. +# And first: Protect against being on the right side of a sed subst in +# config.status. Protect against being in an unquoted here document +# in config.status. +rm -f conftest.defines conftest.undefs +# Using a here document instead of a string reduces the quoting nightmare. +# Putting comments in sed scripts is not portable. +# +# `end' is used to avoid that the second main sed command (meant for +# 0-ary CPP macros) applies to n-ary macro definitions. +# See the Autoconf documentation for `clear'. +cat >confdef2sed.sed <<\EOF +s/[\\&,]/\\&/g +s,[\\$`],\\&,g +t clear +: clear +s,^[ ]*#[ ]*define[ ][ ]*\(\([^ (][^ (]*\)([^)]*)\)[ ]*\(.*\)$,${ac_dA}\2${ac_dB}\1${ac_dC}\3${ac_dD},gp +t end +s,^[ ]*#[ ]*define[ ][ ]*\([^ ][^ ]*\)[ ]*\(.*\)$,${ac_dA}\1${ac_dB}\1${ac_dC}\2${ac_dD},gp +: end +EOF +# If some macros were called several times there might be several times +# the same #defines, which is useless. Nevertheless, we may not want to +# sort them, since we want the *last* AC-DEFINE to be honored. +uniq confdefs.h | sed -n -f confdef2sed.sed >conftest.defines +sed 's/ac_d/ac_u/g' conftest.defines >conftest.undefs +rm -f confdef2sed.sed + +# This sed command replaces #undef with comments. This is necessary, for +# example, in the case of _POSIX_SOURCE, which is predefined and required +# on some systems where configure will not decide to define it. +cat >>conftest.undefs <<\EOF +s,^[ ]*#[ ]*undef[ ][ ]*[a-zA-Z_][a-zA-Z_0-9]*,/* & */, +EOF + +# Break up conftest.defines because some shells have a limit on the size +# of here documents, and old seds have small limits too (100 cmds). +echo ' # Handle all the #define templates only if necessary.' >>$CONFIG_STATUS +echo ' if egrep "^[ ]*#[ ]*define" $tmp/in >/dev/null; then' >>$CONFIG_STATUS +echo ' # If there are no defines, we may have an empty if/fi' >>$CONFIG_STATUS +echo ' :' >>$CONFIG_STATUS +rm -f conftest.tail +while grep . conftest.defines >/dev/null +do + # Write a limited-size here document to $tmp/defines.sed. + echo ' cat >$tmp/defines.sed <>$CONFIG_STATUS + # Speed up: don't consider the non `#define' lines. + echo '/^[ ]*#[ ]*define/!b' >>$CONFIG_STATUS + # Work around the forget-to-reset-the-flag bug. + echo 't clr' >>$CONFIG_STATUS + echo ': clr' >>$CONFIG_STATUS + sed ${ac_max_here_lines}q conftest.defines >>$CONFIG_STATUS + echo 'CEOF + sed -f $tmp/defines.sed $tmp/in >$tmp/out + rm -f $tmp/in + mv $tmp/out $tmp/in +' >>$CONFIG_STATUS + sed 1,${ac_max_here_lines}d conftest.defines >conftest.tail + rm -f conftest.defines + mv conftest.tail conftest.defines +done +rm -f conftest.defines +echo ' fi # egrep' >>$CONFIG_STATUS +echo >>$CONFIG_STATUS + +# Break up conftest.undefs because some shells have a limit on the size +# of here documents, and old seds have small limits too (100 cmds). +echo ' # Handle all the #undef templates' >>$CONFIG_STATUS +rm -f conftest.tail +while grep . conftest.undefs >/dev/null +do + # Write a limited-size here document to $tmp/undefs.sed. + echo ' cat >$tmp/undefs.sed <>$CONFIG_STATUS + # Speed up: don't consider the non `#undef' + echo '/^[ ]*#[ ]*undef/!b' >>$CONFIG_STATUS + # Work around the forget-to-reset-the-flag bug. + echo 't clr' >>$CONFIG_STATUS + echo ': clr' >>$CONFIG_STATUS + sed ${ac_max_here_lines}q conftest.undefs >>$CONFIG_STATUS + echo 'CEOF + sed -f $tmp/undefs.sed $tmp/in >$tmp/out + rm -f $tmp/in + mv $tmp/out $tmp/in +' >>$CONFIG_STATUS + sed 1,${ac_max_here_lines}d conftest.undefs >conftest.tail + rm -f conftest.undefs + mv conftest.tail conftest.undefs +done +rm -f conftest.undefs + +cat >>$CONFIG_STATUS <<\EOF + # Let's still pretend it is `configure' which instantiates (i.e., don't + # use $as_me), people would be surprised to read: + # /* config.h. Generated automatically by config.status. */ + if test x"$ac_file" = x-; then + echo "/* Generated automatically by configure. */" >$tmp/config.h + else + echo "/* $ac_file. Generated automatically by configure. */" >$tmp/config.h + fi + cat $tmp/in >>$tmp/config.h + rm -f $tmp/in + if test x"$ac_file" != x-; then + if cmp -s $ac_file $tmp/config.h 2>/dev/null; then + { echo "$as_me:2847: $ac_file is unchanged" >&5 +echo "$as_me: $ac_file is unchanged" >&6;} + else + ac_dir=`$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ + X"$ac_file" : 'X\(//\)[^/]' \| \ + X"$ac_file" : 'X\(//\)$' \| \ + X"$ac_file" : 'X\(/\)' \| \ + . : '\(.\)' 2>/dev/null || +echo X"$ac_file" | + sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; } + /^X\(\/\/\)[^/].*/{ s//\1/; q; } + /^X\(\/\/\)$/{ s//\1/; q; } + /^X\(\/\).*/{ s//\1/; q; } + s/.*/./; q'` + if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then + { case "$ac_dir" in + [\\/]* | ?:[\\/]* ) as_incr_dir=;; + *) as_incr_dir=.;; +esac +as_dummy="$ac_dir" +for as_mkdir_dir in `IFS='/\\'; set X $as_dummy; shift; echo "$@"`; do + case $as_mkdir_dir in + # Skip DOS drivespec + ?:) as_incr_dir=$as_mkdir_dir ;; + *) + as_incr_dir=$as_incr_dir/$as_mkdir_dir + test -d "$as_incr_dir" || mkdir "$as_incr_dir" + ;; + esac +done; } + + fi + rm -f $ac_file + mv $tmp/config.h $ac_file + fi + else + cat $tmp/config.h + rm -f $tmp/config.h + fi +done +EOF + +cat >>$CONFIG_STATUS <<\EOF + +{ (exit 0); exit 0; } +EOF +chmod +x $CONFIG_STATUS +ac_clean_files=$ac_clean_files_save + +# configure is writing to config.log, and then calls config.status. +# config.status does its own redirection, appending to config.log. +# Unfortunately, on DOS this fails, as config.log is still kept open +# by configure, so config.status won't be able to write to it; its +# output is simply discarded. So we exec the FD to /dev/null, +# effectively closing config.log, so it can be properly (re)opened and +# appended to by config.status. When coming back to configure, we +# need to make the FD available again. +if test "$no_create" != yes; then + ac_cs_success=: + exec 5>/dev/null + $SHELL $CONFIG_STATUS || ac_cs_success=false + exec 5>>config.log + # Use ||, not &&, to avoid exiting from the if with $? = 1, which + # would make configure fail if this is the last instruction. + $ac_cs_success || { (exit 1); exit 1; } +fi + +echo "" + +if [ $have_libsamplerate = "yes" ] ; then + echo "Configured to build tests/resample-sndfile using libsndfile" + echo "" +else + echo "Could not find libsndfile - needed if you want to" + echo "compile tests/resample-sndfile" + echo "" +fi + +if [ $have_libsamplerate = "yes" ] ; then + echo "Configured to build tests/compareresample to compare against" + echo "Erik de Castro Lopo's libsamplerate library." + echo "" +else + echo "Could not find libsamplerate - only needed if you want to" + echo "compile tests/compareresample to compare their performance." + echo "" +fi + +echo "Type 'configure --help' to see options." +echo "" +echo "Type 'make' to build libresample and tests." diff --git a/libresample-0.1.3/configure.in b/libresample-0.1.3/configure.in new file mode 100644 index 00000000..bd537d1f --- /dev/null +++ b/libresample-0.1.3/configure.in @@ -0,0 +1,66 @@ +dnl +dnl libresample configure.in script +dnl +dnl Dominic Mazzoni +dnl + +dnl Require autoconf >= 2.13 +AC_PREREQ(2.13) + +dnl Init autoconf and make sure configure is being called +dnl from the right directory +AC_INIT([src/resample.c]) + +dnl Checks for programs. +AC_PROG_CC +AC_PROG_RANLIB + +AC_PATH_PROG(AR, ar, no) +if [[ $AR = "no" ]] ; then + AC_MSG_ERROR("Could not find ar - needed to create a library"); +fi + +AC_SUBST(TARGETS) +TARGETS="libresample.a tests/testresample" + +AC_CHECK_LIB(sndfile, sf_open, have_libsndfile=yes, have_libsndfile=no) + +if [[ $have_libsndfile = "yes" ]] ; then + TARGETS="$TARGETS tests/resample-sndfile" +fi + +AC_CHECK_LIB(samplerate, src_simple, have_libsamplerate=yes, have_libsamplerate=no) + +if [[ $have_libsamplerate = "yes" ]] ; then + TARGETS="$TARGETS tests/compareresample" +fi + +AC_CHECK_HEADERS(inttypes.h) + +AC_CONFIG_HEADER(src/config.h:src/configtemplate.h) +AC_OUTPUT([Makefile]) + +echo "" + +if [[ $have_libsamplerate = "yes" ]] ; then + echo "Configured to build tests/resample-sndfile using libsndfile" + echo "" +else + echo "Could not find libsndfile - needed if you want to" + echo "compile tests/resample-sndfile" + echo "" +fi + +if [[ $have_libsamplerate = "yes" ]] ; then + echo "Configured to build tests/compareresample to compare against" + echo "Erik de Castro Lopo's libsamplerate library." + echo "" +else + echo "Could not find libsamplerate - only needed if you want to" + echo "compile tests/compareresample to compare their performance." + echo "" +fi + +echo "Type 'configure --help' to see options." +echo "" +echo "Type 'make' to build libresample and tests." diff --git a/libresample-0.1.3/include/libresample.h b/libresample-0.1.3/include/libresample.h new file mode 100644 index 00000000..ca08e044 --- /dev/null +++ b/libresample-0.1.3/include/libresample.h @@ -0,0 +1,44 @@ +/********************************************************************** + + resample.h + + Real-time library interface by Dominic Mazzoni + + Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + + License: LGPL - see the file LICENSE.txt for more information + +**********************************************************************/ + +#ifndef LIBRESAMPLE_INCLUDED +#define LIBRESAMPLE_INCLUDED + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void *resample_open(int highQuality, + double minFactor, + double maxFactor); + +void *resample_dup(const void *handle); + +int resample_get_filter_width(const void *handle); + +int resample_process(void *handle, + double factor, + float *inBuffer, + int inBufferLen, + int lastFlag, + int *inBufferUsed, + float *outBuffer, + int outBufferLen); + +void resample_close(void *handle); + +#ifdef __cplusplus +} /* extern "C" */ +#endif /* __cplusplus */ + +#endif /* LIBRESAMPLE_INCLUDED */ diff --git a/libresample-0.1.3/install-sh b/libresample-0.1.3/install-sh new file mode 100644 index 00000000..e9de2384 --- /dev/null +++ b/libresample-0.1.3/install-sh @@ -0,0 +1,251 @@ +#!/bin/sh +# +# install - install a program, script, or datafile +# This comes from X11R5 (mit/util/scripts/install.sh). +# +# Copyright 1991 by the Massachusetts Institute of Technology +# +# Permission to use, copy, modify, distribute, and sell this software and its +# documentation for any purpose is hereby granted without fee, provided that +# the above copyright notice appear in all copies and that both that +# copyright notice and this permission notice appear in supporting +# documentation, and that the name of M.I.T. not be used in advertising or +# publicity pertaining to distribution of the software without specific, +# written prior permission. M.I.T. makes no representations about the +# suitability of this software for any purpose. It is provided "as is" +# without express or implied warranty. +# +# Calling this script install-sh is preferred over install.sh, to prevent +# `make' implicit rules from creating a file called install from it +# when there is no Makefile. +# +# This script is compatible with the BSD install script, but was written +# from scratch. It can only install one file at a time, a restriction +# shared with many OS's install programs. + + +# set DOITPROG to echo to test this script + +# Don't use :- since 4.3BSD and earlier shells don't like it. +doit="${DOITPROG-}" + + +# put in absolute paths if you don't have them in your path; or use env. vars. + +mvprog="${MVPROG-mv}" +cpprog="${CPPROG-cp}" +chmodprog="${CHMODPROG-chmod}" +chownprog="${CHOWNPROG-chown}" +chgrpprog="${CHGRPPROG-chgrp}" +stripprog="${STRIPPROG-strip}" +rmprog="${RMPROG-rm}" +mkdirprog="${MKDIRPROG-mkdir}" + +transformbasename="" +transform_arg="" +instcmd="$mvprog" +chmodcmd="$chmodprog 0755" +chowncmd="" +chgrpcmd="" +stripcmd="" +rmcmd="$rmprog -f" +mvcmd="$mvprog" +src="" +dst="" +dir_arg="" + +while [ x"$1" != x ]; do + case $1 in + -c) instcmd="$cpprog" + shift + continue;; + + -d) dir_arg=true + shift + continue;; + + -m) chmodcmd="$chmodprog $2" + shift + shift + continue;; + + -o) chowncmd="$chownprog $2" + shift + shift + continue;; + + -g) chgrpcmd="$chgrpprog $2" + shift + shift + continue;; + + -s) stripcmd="$stripprog" + shift + continue;; + + -t=*) transformarg=`echo $1 | sed 's/-t=//'` + shift + continue;; + + -b=*) transformbasename=`echo $1 | sed 's/-b=//'` + shift + continue;; + + *) if [ x"$src" = x ] + then + src=$1 + else + # this colon is to work around a 386BSD /bin/sh bug + : + dst=$1 + fi + shift + continue;; + esac +done + +if [ x"$src" = x ] +then + echo "install: no input file specified" + exit 1 +else + true +fi + +if [ x"$dir_arg" != x ]; then + dst=$src + src="" + + if [ -d $dst ]; then + instcmd=: + chmodcmd="" + else + instcmd=mkdir + fi +else + +# Waiting for this to be detected by the "$instcmd $src $dsttmp" command +# might cause directories to be created, which would be especially bad +# if $src (and thus $dsttmp) contains '*'. + + if [ -f $src -o -d $src ] + then + true + else + echo "install: $src does not exist" + exit 1 + fi + + if [ x"$dst" = x ] + then + echo "install: no destination specified" + exit 1 + else + true + fi + +# If destination is a directory, append the input filename; if your system +# does not like double slashes in filenames, you may need to add some logic + + if [ -d $dst ] + then + dst="$dst"/`basename $src` + else + true + fi +fi + +## this sed command emulates the dirname command +dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` + +# Make sure that the destination directory exists. +# this part is taken from Noah Friedman's mkinstalldirs script + +# Skip lots of stat calls in the usual case. +if [ ! -d "$dstdir" ]; then +defaultIFS=' +' +IFS="${IFS-${defaultIFS}}" + +oIFS="${IFS}" +# Some sh's can't handle IFS=/ for some reason. +IFS='%' +set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` +IFS="${oIFS}" + +pathcomp='' + +while [ $# -ne 0 ] ; do + pathcomp="${pathcomp}${1}" + shift + + if [ ! -d "${pathcomp}" ] ; + then + $mkdirprog "${pathcomp}" + else + true + fi + + pathcomp="${pathcomp}/" +done +fi + +if [ x"$dir_arg" != x ] +then + $doit $instcmd $dst && + + if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi && + if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi && + if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi && + if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi +else + +# If we're going to rename the final executable, determine the name now. + + if [ x"$transformarg" = x ] + then + dstfile=`basename $dst` + else + dstfile=`basename $dst $transformbasename | + sed $transformarg`$transformbasename + fi + +# don't allow the sed command to completely eliminate the filename + + if [ x"$dstfile" = x ] + then + dstfile=`basename $dst` + else + true + fi + +# Make a temp file name in the proper directory. + + dsttmp=$dstdir/#inst.$$# + +# Move or copy the file name to the temp name + + $doit $instcmd $src $dsttmp && + + trap "rm -f ${dsttmp}" 0 && + +# and set any options; do chmod last to preserve setuid bits + +# If any of these fail, we abort the whole thing. If we want to +# ignore errors from any of these, just make sure not to ignore +# errors from the above "$doit $instcmd $src $dsttmp" command. + + if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi && + if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi && + if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi && + if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi && + +# Now rename the file to the real destination. + + $doit $rmcmd -f $dstdir/$dstfile && + $doit $mvcmd $dsttmp $dstdir/$dstfile + +fi && + + +exit 0 diff --git a/libresample-0.1.3/src/configtemplate.h b/libresample-0.1.3/src/configtemplate.h new file mode 100644 index 00000000..94ae1cea --- /dev/null +++ b/libresample-0.1.3/src/configtemplate.h @@ -0,0 +1,7 @@ +/* Run configure to generate config.h automatically on any + system supported by GNU autoconf. For all other systems, + use this file as a template to create config.h +*/ + +#undef HAVE_INTTYPES_H + diff --git a/libresample-0.1.3/src/filterkit.c b/libresample-0.1.3/src/filterkit.c new file mode 100644 index 00000000..bc92285f --- /dev/null +++ b/libresample-0.1.3/src/filterkit.c @@ -0,0 +1,215 @@ +/********************************************************************** + + resamplesubs.c + + Real-time library interface by Dominic Mazzoni + + Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + + License: LGPL - see the file LICENSE.txt for more information + + This file provides Kaiser-windowed low-pass filter support, + including a function to create the filter coefficients, and + two functions to apply the filter at a particular point. + +**********************************************************************/ + +/* Definitions */ +#include "resample_defs.h" + +#include "filterkit.h" + +#include +#include +#include +#include + +/* LpFilter() + * + * reference: "Digital Filters, 2nd edition" + * R.W. Hamming, pp. 178-179 + * + * Izero() computes the 0th order modified bessel function of the first kind. + * (Needed to compute Kaiser window). + * + * LpFilter() computes the coeffs of a Kaiser-windowed low pass filter with + * the following characteristics: + * + * c[] = array in which to store computed coeffs + * frq = roll-off frequency of filter + * N = Half the window length in number of coeffs + * Beta = parameter of Kaiser window + * Num = number of coeffs before 1/frq + * + * Beta trades the rejection of the lowpass filter against the transition + * width from passband to stopband. Larger Beta means a slower + * transition and greater stopband rejection. See Rabiner and Gold + * (Theory and Application of DSP) under Kaiser windows for more about + * Beta. The following table from Rabiner and Gold gives some feel + * for the effect of Beta: + * + * All ripples in dB, width of transition band = D*N where N = window length + * + * BETA D PB RIP SB RIP + * 2.120 1.50 +-0.27 -30 + * 3.384 2.23 0.0864 -40 + * 4.538 2.93 0.0274 -50 + * 5.658 3.62 0.00868 -60 + * 6.764 4.32 0.00275 -70 + * 7.865 5.0 0.000868 -80 + * 8.960 5.7 0.000275 -90 + * 10.056 6.4 0.000087 -100 + */ + +#define IzeroEPSILON 1E-21 /* Max error acceptable in Izero */ + +static double Izero(double x) +{ + double sum, u, halfx, temp; + int n; + + sum = u = n = 1; + halfx = x/2.0; + do { + temp = halfx/(double)n; + n += 1; + temp *= temp; + u *= temp; + sum += u; + } while (u >= IzeroEPSILON*sum); + return(sum); +} + +void lrsLpFilter(double c[], int N, double frq, double Beta, int Num) +{ + double IBeta, temp, temp1, inm1; + int i; + + /* Calculate ideal lowpass filter impulse response coefficients: */ + c[0] = 2.0*frq; + for (i=1; i +#include +#include +#include + +typedef struct { + float *Imp; + float *ImpD; + float LpScl; + UWORD Nmult; + UWORD Nwing; + double minFactor; + double maxFactor; + UWORD XSize; + float *X; + UWORD Xp; /* Current "now"-sample pointer for input */ + UWORD Xread; /* Position to put new samples */ + UWORD Xoff; + UWORD YSize; + float *Y; + UWORD Yp; + double Time; +} rsdata; + +void *resample_dup(const void * handle) +{ + const rsdata *cpy = (const rsdata *)handle; + rsdata *hp = (rsdata *)malloc(sizeof(rsdata)); + + hp->minFactor = cpy->minFactor; + hp->maxFactor = cpy->maxFactor; + hp->Nmult = cpy->Nmult; + hp->LpScl = cpy->LpScl; + hp->Nwing = cpy->Nwing; + + hp->Imp = (float *)malloc(hp->Nwing * sizeof(float)); + memcpy(hp->Imp, cpy->Imp, hp->Nwing * sizeof(float)); + hp->ImpD = (float *)malloc(hp->Nwing * sizeof(float)); + memcpy(hp->ImpD, cpy->ImpD, hp->Nwing * sizeof(float)); + + hp->Xoff = cpy->Xoff; + hp->XSize = cpy->XSize; + hp->X = (float *)malloc((hp->XSize + hp->Xoff) * sizeof(float)); + memcpy(hp->X, cpy->X, (hp->XSize + hp->Xoff) * sizeof(float)); + hp->Xp = cpy->Xp; + hp->Xread = cpy->Xread; + hp->YSize = cpy->YSize; + hp->Y = (float *)malloc(hp->YSize * sizeof(float)); + memcpy(hp->Y, cpy->Y, hp->YSize * sizeof(float)); + hp->Yp = cpy->Yp; + hp->Time = cpy->Time; + + return (void *)hp; +} + +void *resample_open(int highQuality, double minFactor, double maxFactor) +{ + double *Imp64; + double Rolloff, Beta; + rsdata *hp; + UWORD Xoff_min, Xoff_max; + int i; + + /* Just exit if we get invalid factors */ + if (minFactor <= 0.0 || maxFactor <= 0.0 || maxFactor < minFactor) { + #if DEBUG + fprintf(stderr, + "libresample: " + "minFactor and maxFactor must be positive real numbers,\n" + "and maxFactor should be larger than minFactor.\n"); + #endif + return 0; + } + + hp = (rsdata *)malloc(sizeof(rsdata)); + + hp->minFactor = minFactor; + hp->maxFactor = maxFactor; + + if (highQuality) + hp->Nmult = 35; + else + hp->Nmult = 11; + + hp->LpScl = 1.0; + hp->Nwing = Npc*(hp->Nmult-1)/2; /* # of filter coeffs in right wing */ + + Rolloff = 0.90; + Beta = 6; + + Imp64 = (double *)malloc(hp->Nwing * sizeof(double)); + + lrsLpFilter(Imp64, hp->Nwing, 0.5*Rolloff, Beta, Npc); + + hp->Imp = (float *)malloc(hp->Nwing * sizeof(float)); + hp->ImpD = (float *)malloc(hp->Nwing * sizeof(float)); + for(i=0; iNwing; i++) + hp->Imp[i] = Imp64[i]; + + /* Storing deltas in ImpD makes linear interpolation + of the filter coefficients faster */ + for (i=0; iNwing-1; i++) + hp->ImpD[i] = hp->Imp[i+1] - hp->Imp[i]; + + /* Last coeff. not interpolated */ + hp->ImpD[hp->Nwing-1] = - hp->Imp[hp->Nwing-1]; + + free(Imp64); + + /* Calc reach of LP filter wing (plus some creeping room) */ + Xoff_min = ((hp->Nmult+1)/2.0) * MAX(1.0, 1.0/minFactor) + 10; + Xoff_max = ((hp->Nmult+1)/2.0) * MAX(1.0, 1.0/maxFactor) + 10; + hp->Xoff = MAX(Xoff_min, Xoff_max); + + /* Make the inBuffer size at least 4096, but larger if necessary + in order to store the minimum reach of the LP filter and then some. + Then allocate the buffer an extra Xoff larger so that + we can zero-pad up to Xoff zeros at the end when we reach the + end of the input samples. */ + hp->XSize = MAX(2*hp->Xoff+10, 4096); + hp->X = (float *)malloc((hp->XSize + hp->Xoff) * sizeof(float)); + hp->Xp = hp->Xoff; + hp->Xread = hp->Xoff; + + /* Need Xoff zeros at begining of X buffer */ + for(i=0; iXoff; i++) + hp->X[i]=0; + + /* Make the outBuffer long enough to hold the entire processed + output of one inBuffer */ + hp->YSize = (int)(((double)hp->XSize)*maxFactor+2.0); + hp->Y = (float *)malloc(hp->YSize * sizeof(float)); + hp->Yp = 0; + + hp->Time = (double)hp->Xoff; /* Current-time pointer for converter */ + + return (void *)hp; +} + +int resample_get_filter_width(const void *handle) +{ + const rsdata *hp = (const rsdata *)handle; + return hp->Xoff; +} + +int resample_process(void *handle, + double factor, + float *inBuffer, + int inBufferLen, + int lastFlag, + int *inBufferUsed, /* output param */ + float *outBuffer, + int outBufferLen) +{ + rsdata *hp = (rsdata *)handle; + float *Imp = hp->Imp; + float *ImpD = hp->ImpD; + float LpScl = hp->LpScl; + UWORD Nwing = hp->Nwing; + BOOL interpFilt = FALSE; /* TRUE means interpolate filter coeffs */ + int outSampleCount; + UWORD Nout, Ncreep, Nreuse; + int Nx; + int i, len; + + #if DEBUG + fprintf(stderr, "resample_process: in=%d, out=%d lastFlag=%d\n", + inBufferLen, outBufferLen, lastFlag); + #endif + + /* Initialize inBufferUsed and outSampleCount to 0 */ + *inBufferUsed = 0; + outSampleCount = 0; + + if (factor < hp->minFactor || factor > hp->maxFactor) { + #if DEBUG + fprintf(stderr, + "libresample: factor %f is not between " + "minFactor=%f and maxFactor=%f", + factor, hp->minFactor, hp->maxFactor); + #endif + return -1; + } + + /* Start by copying any samples still in the Y buffer to the output + buffer */ + if (hp->Yp && (outBufferLen-outSampleCount)>0) { + len = MIN(outBufferLen-outSampleCount, hp->Yp); + for(i=0; iY[i]; + outSampleCount += len; + for(i=0; iYp-len; i++) + hp->Y[i] = hp->Y[i+len]; + hp->Yp -= len; + } + + /* If there are still output samples left, return now - we need + the full output buffer available to us... */ + if (hp->Yp) + return outSampleCount; + + /* Account for increased filter gain when using factors less than 1 */ + if (factor < 1) + LpScl = LpScl*factor; + + for(;;) { + + /* This is the maximum number of samples we can process + per loop iteration */ + + #ifdef DEBUG + printf("XSize: %d Xoff: %d Xread: %d Xp: %d lastFlag: %d\n", + hp->XSize, hp->Xoff, hp->Xread, hp->Xp, lastFlag); + #endif + + /* Copy as many samples as we can from the input buffer into X */ + len = hp->XSize - hp->Xread; + + if (len >= (inBufferLen - (*inBufferUsed))) + len = (inBufferLen - (*inBufferUsed)); + + for(i=0; iX[hp->Xread + i] = inBuffer[(*inBufferUsed) + i]; + + *inBufferUsed += len; + hp->Xread += len; + + if (lastFlag && (*inBufferUsed == inBufferLen)) { + /* If these are the last samples, zero-pad the + end of the input buffer and make sure we process + all the way to the end */ + Nx = hp->Xread - hp->Xoff; + for(i=0; iXoff; i++) + hp->X[hp->Xread + i] = 0; + } + else + Nx = hp->Xread - 2 * hp->Xoff; + + #ifdef DEBUG + fprintf(stderr, "new len=%d Nx=%d\n", len, Nx); + #endif + + if (Nx <= 0) + break; + + /* Resample stuff in input buffer */ + if (factor >= 1) { /* SrcUp() is faster if we can use it */ + Nout = lrsSrcUp(hp->X, hp->Y, factor, &hp->Time, Nx, + Nwing, LpScl, Imp, ImpD, interpFilt); + } + else { + Nout = lrsSrcUD(hp->X, hp->Y, factor, &hp->Time, Nx, + Nwing, LpScl, Imp, ImpD, interpFilt); + } + + #ifdef DEBUG + printf("Nout: %d\n", Nout); + #endif + + hp->Time -= Nx; /* Move converter Nx samples back in time */ + hp->Xp += Nx; /* Advance by number of samples processed */ + + /* Calc time accumulation in Time */ + Ncreep = (int)(hp->Time) - hp->Xoff; + if (Ncreep) { + hp->Time -= Ncreep; /* Remove time accumulation */ + hp->Xp += Ncreep; /* and add it to read pointer */ + } + + /* Copy part of input signal that must be re-used */ + Nreuse = hp->Xread - (hp->Xp - hp->Xoff); + + for (i=0; iX[i] = hp->X[i + (hp->Xp - hp->Xoff)]; + + #ifdef DEBUG + printf("New Xread=%d\n", Nreuse); + #endif + + hp->Xread = Nreuse; /* Pos in input buff to read new data into */ + hp->Xp = hp->Xoff; + + /* Check to see if output buff overflowed (shouldn't happen!) */ + if (Nout > hp->YSize) { + #ifdef DEBUG + printf("Nout: %d YSize: %d\n", Nout, hp->YSize); + #endif + fprintf(stderr, "libresample: Output array overflow!\n"); + return -1; + } + + hp->Yp = Nout; + + /* Copy as many samples as possible to the output buffer */ + if (hp->Yp && (outBufferLen-outSampleCount)>0) { + len = MIN(outBufferLen-outSampleCount, hp->Yp); + for(i=0; iY[i]; + outSampleCount += len; + for(i=0; iYp-len; i++) + hp->Y[i] = hp->Y[i+len]; + hp->Yp -= len; + } + + /* If there are still output samples left, return now, + since we need the full output buffer available */ + if (hp->Yp) + break; + } + + return outSampleCount; +} + +void resample_close(void *handle) +{ + rsdata *hp = (rsdata *)handle; + free(hp->X); + free(hp->Y); + free(hp->Imp); + free(hp->ImpD); + free(hp); +} + diff --git a/libresample-0.1.3/src/resample_defs.h b/libresample-0.1.3/src/resample_defs.h new file mode 100644 index 00000000..576c1bc2 --- /dev/null +++ b/libresample-0.1.3/src/resample_defs.h @@ -0,0 +1,86 @@ +/********************************************************************** + + resample_defs.h + + Real-time library interface by Dominic Mazzoni + + Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + + License: LGPL - see the file LICENSE.txt for more information + +**********************************************************************/ + +#ifndef __RESAMPLE_DEFS__ +#define __RESAMPLE_DEFS__ + +#if !defined(WIN32) && !defined(__CYGWIN__) +#include "config.h" +#endif + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef PI +#define PI (3.14159265358979232846) +#endif + +#ifndef PI2 +#define PI2 (6.28318530717958465692) +#endif + +#define D2R (0.01745329348) /* (2*pi)/360 */ +#define R2D (57.29577951) /* 360/(2*pi) */ + +#ifndef MAX +#define MAX(x,y) ((x)>(y) ?(x):(y)) +#endif +#ifndef MIN +#define MIN(x,y) ((x)<(y) ?(x):(y)) +#endif + +#ifndef ABS +#define ABS(x) ((x)<0 ?(-(x)):(x)) +#endif + +#ifndef SGN +#define SGN(x) ((x)<0 ?(-1):((x)==0?(0):(1))) +#endif + +#if HAVE_INTTYPES_H + #include + typedef char BOOL; + typedef int32_t WORD; + typedef uint32_t UWORD; +#else + typedef char BOOL; + typedef int WORD; + typedef unsigned int UWORD; +#endif + +#ifdef DEBUG +#define INLINE +#else +#define INLINE inline +#endif + +/* Accuracy */ + +#define Npc 4096 + +/* Function prototypes */ + +int lrsSrcUp(float X[], float Y[], double factor, double *Time, + UWORD Nx, UWORD Nwing, float LpScl, + float Imp[], float ImpD[], BOOL Interp); + +int lrsSrcUD(float X[], float Y[], double factor, double *Time, + UWORD Nx, UWORD Nwing, float LpScl, + float Imp[], float ImpD[], BOOL Interp); + +#endif diff --git a/libresample-0.1.3/src/resamplesubs.c b/libresample-0.1.3/src/resamplesubs.c new file mode 100644 index 00000000..c3c095dc --- /dev/null +++ b/libresample-0.1.3/src/resamplesubs.c @@ -0,0 +1,123 @@ +/********************************************************************** + + resamplesubs.c + + Real-time library interface by Dominic Mazzoni + + Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + + License: LGPL - see the file LICENSE.txt for more information + + This file provides the routines that do sample-rate conversion + on small arrays, calling routines from filterkit. + +**********************************************************************/ + +/* Definitions */ +#include "resample_defs.h" + +#include "filterkit.h" + +#include +#include +#include +#include + +/* Sampling rate up-conversion only subroutine; + * Slightly faster than down-conversion; + */ +int lrsSrcUp(float X[], + float Y[], + double factor, + double *TimePtr, + UWORD Nx, + UWORD Nwing, + float LpScl, + float Imp[], + float ImpD[], + BOOL Interp) +{ + float *Xp, *Ystart; + float v; + + double CurrentTime = *TimePtr; + double dt; /* Step through input signal */ + double endTime; /* When Time reaches EndTime, return to user */ + + dt = 1.0/factor; /* Output sampling period */ + + Ystart = Y; + endTime = CurrentTime + Nx; + while (CurrentTime < endTime) + { + double LeftPhase = CurrentTime-floor(CurrentTime); + double RightPhase = 1.0 - LeftPhase; + + Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */ + /* Perform left-wing inner product */ + v = lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp, + LeftPhase, -1); + /* Perform right-wing inner product */ + v += lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp+1, + RightPhase, 1); + + v *= LpScl; /* Normalize for unity filter gain */ + + *Y++ = v; /* Deposit output */ + CurrentTime += dt; /* Move to next sample by time increment */ + } + + *TimePtr = CurrentTime; + return (Y - Ystart); /* Return the number of output samples */ +} + +/* Sampling rate conversion subroutine */ + +int lrsSrcUD(float X[], + float Y[], + double factor, + double *TimePtr, + UWORD Nx, + UWORD Nwing, + float LpScl, + float Imp[], + float ImpD[], + BOOL Interp) +{ + float *Xp, *Ystart; + float v; + + double CurrentTime = (*TimePtr); + double dh; /* Step through filter impulse response */ + double dt; /* Step through input signal */ + double endTime; /* When Time reaches EndTime, return to user */ + + dt = 1.0/factor; /* Output sampling period */ + + dh = MIN(Npc, factor*Npc); /* Filter sampling period */ + + Ystart = Y; + endTime = CurrentTime + Nx; + while (CurrentTime < endTime) + { + double LeftPhase = CurrentTime-floor(CurrentTime); + double RightPhase = 1.0 - LeftPhase; + + Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */ + /* Perform left-wing inner product */ + v = lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp, + LeftPhase, -1, dh); + /* Perform right-wing inner product */ + v += lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp+1, + RightPhase, 1, dh); + + v *= LpScl; /* Normalize for unity filter gain */ + *Y++ = v; /* Deposit output */ + + CurrentTime += dt; /* Move to next sample by time increment */ + } + + *TimePtr = CurrentTime; + return (Y - Ystart); /* Return the number of output samples */ +} diff --git a/libresample-0.1.3/tests/compareresample.c b/libresample-0.1.3/tests/compareresample.c new file mode 100644 index 00000000..8773c9d4 --- /dev/null +++ b/libresample-0.1.3/tests/compareresample.c @@ -0,0 +1,183 @@ +/********************************************************************** + + compareresample.c + + Real-time library interface by Dominic Mazzoni + + Based on resample-1.7: + http://www-ccrma.stanford.edu/~jos/resample/ + + License: LGPL - see the file LICENSE.txt for more information + +**********************************************************************/ + +#include "../include/libresample.h" + +#include + +#include +#include +#include + +#include + +#define MIN(A, B) (A) < (B)? (A) : (B) + +void dostat(char *name, float *d1, float *d2, int len) +{ + int i; + double sum, sumsq, err, rmserr; + + sum = 0.0; + sumsq = 0.0; + for(i=0; i= 0) + out += o; + if (o < 0 || (o == 0 && srcpos == srclen)) + break; + } + resample_close(handle); + + gettimeofday(&tv1, NULL); + deltat = + (tv1.tv_sec + tv1.tv_usec * 0.000001) - + (tv0.tv_sec + tv0.tv_usec * 0.000001); + + if (o < 0) { + printf("Error: resample_process returned an error: %d\n", o); + } + + if (out <= 0) { + printf("Error: resample_process returned %d samples\n", out); + free(dst_rs); + return; + } + + printf(" resample: %.3f seconds, %d outputs\n", deltat, out); + + /* do rabbit (Erik's libsamplerate) */ + + for(i=0; i 0) + statlen = MIN(statlen, anslen); + + if (ans) { + dostat("resample ", dst_rs, ans, statlen); + dostat("rabbit ", dst_rabbit, ans, statlen); + } + dostat( "RS vs rabbit", dst_rs, dst_rabbit, statlen); + + free(dst_rs); + free(dst_rabbit); +} + +int main(int argc, char **argv) +{ + int i, srclen; + float *src, *ans; + + printf("\n*** sin wave, factor = 1.0 *** \n\n"); + srclen = 100000; + src = malloc(srclen * sizeof(float)); + for(i=0; i + +#include +#include +#include +#include + +#include + +#define MIN(A, B) (A) < (B)? (A) : (B) + +void usage(char *progname) +{ + fprintf(stderr, "Usage: %s -by \n", progname); + fprintf(stderr, " %s -to \n", progname); + fprintf(stderr, "\n"); + exit(-1); +} + +int main(int argc, char **argv) +{ + SNDFILE *srcfile, *dstfile; + SF_INFO srcinfo, dstinfo; + SF_FORMAT_INFO formatinfo; + char *extension; + void **handle; + int channels; + int srclen, dstlen; + float *src, *srci; + float *dst, *dsti; + double ratio = 0.0; + double srcrate; + double dstrate = 0.0; + struct timeval tv0, tv1; + double deltat; + int numformats; + int pos, bufferpos, outcount; + int i, c; + + if (argc != 5) + usage(argv[0]); + + if (!strcmp(argv[1], "-by")) { + ratio = atof(argv[2]); + if (ratio <= 0.0) { + fprintf(stderr, "Ratio of %f is illegal\n", ratio); + usage(argv[0]); + } + } + else if (!strcmp(argv[1], "-to")) { + dstrate = atof(argv[2]); + if (dstrate < 10.0 || dstrate > 100000.0) { + fprintf(stderr, "Sample rate of %f is illegal\n", dstrate); + usage(argv[0]); + } + } + else + usage(argv[0]); + + memset(&srcinfo, 0, sizeof(srcinfo)); + memset(&dstinfo, 0, sizeof(dstinfo)); + srcfile = sf_open(argv[3], SFM_READ, &srcinfo); + if (!srcfile) { + fprintf(stderr, "%s", sf_strerror(NULL)); + exit(-1); + } + + srcrate = srcinfo.samplerate; + if (dstrate == 0.0) + dstrate = srcrate * ratio; + else + ratio = dstrate / srcrate; + + channels = srcinfo.channels; + + /* figure out format of destination file */ + + extension = strstr(argv[4], "."); + if (extension) { + extension++; + sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT, + &numformats, sizeof(numformats)); + for(i=0; i= 1) + dsti[i*channels+c] = 1; + else + dsti[i*channels+c] = dst[i]; + } + } + + sf_writef_float(dstfile, dsti, out); + + bufferpos = block - inUsed; + for(i=0; i +#include +#include + +#define MIN(A, B) (A) < (B)? (A) : (B) + +void runtest(int srclen, double freq, double factor, + int srcblocksize, int dstblocksize) +{ + int expectedlen = (int)(srclen * factor); + int dstlen = expectedlen + 1000; + float *src = (float *)malloc((srclen+100) * sizeof(float)); + float *dst = (float *)malloc((dstlen+100) * sizeof(float)); + void *handle; + double sum, sumsq, err, rmserr; + int i, out, o, srcused, errcount, rangecount; + int statlen, srcpos, lendiff; + int fwidth; + + printf("-- srclen: %d sin freq: %.1f factor: %.3f srcblk: %d dstblk: %d\n", + srclen, freq, factor, srcblocksize, dstblocksize); + + for(i=0; i= 0) + out += o; + if (o < 0 || (o == 0 && srcpos == srclen)) + break; + } + resample_close(handle); + + if (o < 0) { + printf("Error: resample_process returned an error: %d\n", o); + } + + if (out <= 0) { + printf("Error: resample_process returned %d samples\n", out); + free(src); + free(dst); + return; + } + + lendiff = abs(out - expectedlen); + if (lendiff > (int)(2*factor + 1.0)) { + printf(" Expected ~%d, got %d samples out\n", + expectedlen, out); + } + + sum = 0.0; + sumsq = 0.0; + errcount = 0.0; + + /* Don't compute statistics on all output values; the last few + are guaranteed to be off because it's based on far less + interpolation. */ + statlen = out - fwidth; + + for(i=0; i 0.05) { + if (errcount == 0) + printf(" First error at i=%d: expected %.3f, got %.3f\n", + i, sin((i/freq)/factor), dst[i]); + errcount++; + } + sum += fabs(diff); + sumsq += diff * diff; + } + + rangecount = 0; + for(i=0; i 1.01) { + if (rangecount == 0) + printf(" Error at i=%d: value is %.3f\n", i, dst[i]); + rangecount++; + } + } + if (rangecount > 1) + printf(" At least %d samples were out of range\n", rangecount); + + if (errcount > 0) { + i = out - 1; + printf(" i=%d: expected %.3f, got %.3f\n", + i, sin((i/freq)/factor), dst[i]); + printf(" At least %d samples had significant error.\n", errcount); + } + err = sum / statlen; + rmserr = sqrt(sumsq / statlen); + printf(" Out: %d samples Avg err: %f RMS err: %f\n", out, err, rmserr); + free(src); + free(dst); +} + +int main(int argc, char **argv) +{ + int i, srclen, dstlen, ifreq; + double factor; + + printf("\n*** Vary source block size*** \n\n"); + srclen = 10000; + ifreq = 100; + for(i=0; i<20; i++) { + factor = ((rand() % 16) + 1) / 4.0; + dstlen = (int)(srclen * factor + 10); + runtest(srclen, (double)ifreq, factor, 64, dstlen); + runtest(srclen, (double)ifreq, factor, 32, dstlen); + runtest(srclen, (double)ifreq, factor, 8, dstlen); + runtest(srclen, (double)ifreq, factor, 2, dstlen); + runtest(srclen, (double)ifreq, factor, srclen, dstlen); + } + + printf("\n*** Vary dest block size ***\n\n"); + srclen = 10000; + ifreq = 100; + for(i=0; i<20; i++) { + factor = ((rand() % 16) + 1) / 4.0; + runtest(srclen, (double)ifreq, factor, srclen, 32); + dstlen = (int)(srclen * factor + 10); + runtest(srclen, (double)ifreq, factor, srclen, dstlen); + } + + printf("\n*** Resample factor 1.0, testing different srclen ***\n\n"); + ifreq = 40; + for(i=0; i<100; i++) { + srclen = (rand() % 30000) + 10; + dstlen = (int)(srclen + 10); + runtest(srclen, (double)ifreq, 1.0, srclen, dstlen); + } + + printf("\n*** Resample factor 1.0, testing different sin freq ***\n\n"); + srclen = 10000; + for(i=0; i<100; i++) { + ifreq = ((int)rand() % 10000) + 1; + dstlen = (int)(srclen * 10); + runtest(srclen, (double)ifreq, 1.0, srclen, dstlen); + } + + printf("\n*** Resample with different factors ***\n\n"); + srclen = 10000; + ifreq = 100; + for(i=0; i<100; i++) { + factor = ((rand() % 64) + 1) / 4.0; + dstlen = (int)(srclen * factor + 10); + runtest(srclen, (double)ifreq, factor, srclen, dstlen); + } + + return 0; +} diff --git a/libresample-0.1.3/win/libresample.dsp b/libresample-0.1.3/win/libresample.dsp new file mode 100644 index 00000000..4ebb51e4 --- /dev/null +++ b/libresample-0.1.3/win/libresample.dsp @@ -0,0 +1,116 @@ +# Microsoft Developer Studio Project File - Name="libresample" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Static Library" 0x0104 + +CFG=libresample - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "libresample.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "libresample.mak" CFG="libresample - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "libresample - Win32 Release" (based on "Win32 (x86) Static Library") +!MESSAGE "libresample - Win32 Debug" (based on "Win32 (x86) Static Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "libresample - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c +# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LIB32=link.exe -lib +# ADD BASE LIB32 /nologo +# ADD LIB32 /nologo /out:"libresample.lib" + +!ELSEIF "$(CFG)" == "libresample - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LIB32=link.exe -lib +# ADD BASE LIB32 /nologo +# ADD LIB32 /nologo /out:"libresampled.lib" + +!ENDIF + +# Begin Target + +# Name "libresample - Win32 Release" +# Name "libresample - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=..\src\filterkit.c +# End Source File +# Begin Source File + +SOURCE=..\src\resample.c +# End Source File +# Begin Source File + +SOURCE=..\src\resamplesubs.c +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=..\src\filterkit.h +# End Source File +# Begin Source File + +SOURCE=..\include\libresample.h +# End Source File +# Begin Source File + +SOURCE=..\src\resample_defs.h +# End Source File +# End Group +# End Target +# End Project diff --git a/res/DevInfo.txt b/res/DevInfo.txt new file mode 100644 index 00000000..fb151094 --- /dev/null +++ b/res/DevInfo.txt @@ -0,0 +1,20 @@ +Developer Information File: + +- Don't use the global optimization switch (/Og) because it causes a black screen in Metroid Prime. + +- Project Configurations: + - Debug: Debug ON | Optimizations OFF | UPX OFF + - Release: Debug OFF | Optimizations OFF | UPX OFF + - Optimized: Debug OFF | Optimizations ON | UPX ON + +Software Used: +OPTIONAL UPX 1.25 +/- +NEEDED nasm 0.98.39 +/- +INCLUDED zlib 1.2.3 +/- +INCLUDED libpng 1.2.8 +/- +NEEDED Microsoft Platform SDK 2003 SP1 +/- +NEEDED Microsoft DirectX 9.0c SDK (June 2005) + +NEEDED Microsoft Visual Studio .NET Professional 2003 German + + ++ newer is ok ++/- newer and older is ok \ No newline at end of file diff --git a/res/Known Bugs.txt b/res/Known Bugs.txt new file mode 100644 index 00000000..bad56035 --- /dev/null +++ b/res/Known Bugs.txt @@ -0,0 +1,4 @@ +Known Bugs: + +- fsMaxScale = 1 in Direct3D fullscreen results in wrong image size +- Direct3D: Menu toggle takes a little too long \ No newline at end of file diff --git a/res/ReadMe.txt b/res/ReadMe.txt new file mode 100644 index 00000000..5323920b --- /dev/null +++ b/res/ReadMe.txt @@ -0,0 +1,81 @@ +VisualBoyAdvance S1.7.6 +Nintendo Game Boy / Game Boy Advance Emulator + +This program is distributed under the GNU General Public License +http://www.gnu.org/licenses/gpl.html + +VBA Official Version 1.7.2 with changes by Spacy +Spacy51@gmx.de (Write in english or in german) +Special Build Aturhors Homepage: www.spacyhacks.de.vu +Original Project Homepage: vba.ngemu.com + +My aim: +I want to make this emulator fit my needs and hopefully the needs of others, +which want a light-weight small, fast and multimedial emulator for the GBA +that makes as much use of the power of modern PCs as possible (but with sense). +If you need one of the removed features, just use the original VBA emulator. + +Thanks go to: +suanyuan For help in compilation and other fixes +Tauwasser For help in assembler +WingX For fixing a linker error + + +The following changes have been made: + +S1.7.6: +Emu: +- Readded MMX macro +- Updated zlib to 1.2.3 +- Changed some first start options +- Other small changes +- Put zlib & libpng in seperate Projects +- Added some changes from the latest CVS source +- Small changes to ROM Header Info (just4fun) +- Fixed the linker error (new&delete defined twice) + +Filters: +- Speeded up HQ3X code +- Fixed LQ2X using HQ2X functions + +Display: +- Added extended display mode selection + (Display Adapter, Resolution, Bit Depth, Frequency) +- No more unnecessary black borders in full screen +- Direct3D doesn't take the whole screen (only if you want) +- Direct3D shows menu and windows correct +- Direct3D doesn't show a black screen if left fullscreen to Windows +- Changes on max scale are applied immediately + +Sound: +- Updated sound to DirectSound8 + + +S1.7.5: +- Removed screen flickering when switching to GDI mode. +- Changed some first start options. +- Rearranged Menu +- Added HQ3X in 32 bit mode +- Changed App Icon +- Added FINAL_VERSION definition again. +- Added 3x/4x filter support to OpenGL mode +- Some minor fixes + + +S1.7.4: +- optimized build: (many thanks to suanyuan) + - libpng, zlib, MFC linked static + - Target OS: Windows 2000 +- Keep in mind that HQ3X/HQ4X is NOT added + at the moment, but everything is ready for it + + +S1.7.3: +- Optimized build and project file + - Removed Skin support + - Removed SDL support + - Removed Linux support + - Removed Motion Blur Experimental Filter (the none-IFB version) +- Reworked GDI + - 3x / 4x filter support + - Fullscreen modes available \ No newline at end of file diff --git a/res/VBA.APS b/res/VBA.APS new file mode 100644 index 0000000000000000000000000000000000000000..6a09df4aeb03bddb1737cddba9694c37dc04b20c GIT binary patch literal 280832 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

&Lz*Q?u5xJaAam}qK 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π%Sd_Q`v%XxMxRhU*C_yJDu16l6B3l!iP)%6= z`D31Mz5hVM^Ut@IF$U)yM0ozW!kvI$e+c3E=Y|-wf9f!UqcS`p4>Iod`r!tjnt?M; zZR@cHpO%3$p6EBn8N5CNe+d0QbhN>zXW)!cc=1GobH$C@$2IKwF$Twv6r4W!Q^y&6 bW(H2*(B~m2l|EVD5beBSXBzyxO#c4|jQOlh literal 0 HcmV?d00001 diff --git a/res/VBA.ico b/res/VBA.ico new file mode 100644 index 0000000000000000000000000000000000000000..9a09a204765c64aa78c146d8c40673ae5aac6dda GIT binary patch literal 120582 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 + + +VisualBoyAdvance Emulator. + + + + + + \ No newline at end of file diff --git a/res/VBA.rc b/res/VBA.rc new file mode 100644 index 00000000..783fb0ff --- /dev/null +++ b/res/VBA.rc @@ -0,0 +1,2235 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// German (Germany) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_DEU) +#ifdef _WIN32 +LANGUAGE LANG_GERMAN, SUBLANG_GERMAN +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Icon +// + +// Icon with lowest ID value placed first to ensure application icon +// remains consistent on all systems. +IDI_ICON ICON "VBA.ico" +#endif // German (Germany) resources +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_LINKTAB DIALOG DISCARDABLE 0, 0, 225, 162 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Link Options" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Tab1",IDC_TAB1,"SysTabControl32",0x0,7,7,210,130 + PUSHBUTTON "OK",ID_OK,31,140,60,15 + PUSHBUTTON "Cancel",ID_CANCEL,114,140,57,15 +END + +IDD_LINKTAB1 DIALOG DISCARDABLE 0, 0, 210, 130 +STYLE WS_CHILD | WS_VISIBLE +FONT 8, "MS Sans Serif" +BEGIN + LTEXT "Link timeout (in milliseconds)",IDC_STATIC,17,12,92,16 + EDITTEXT IDC_LINKTIMEOUT,116,10,53,14,ES_AUTOHSCROLL | ES_NUMBER + CONTROL "Single Computer",IDC_LINK_SINGLE,"Button", + BS_AUTORADIOBUTTON,17,27,71,16 + CONTROL "Network",IDC_LINK_LAN,"Button",BS_AUTORADIOBUTTON,17,43, + 70,16 +END + +IDD_LINKTAB2 DIALOG DISCARDABLE 0, 0, 210, 113 +STYLE WS_CHILD +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "2",IDC_LINK2P,"Button",BS_AUTORADIOBUTTON | WS_GROUP,46, + 16,21,13 + CONTROL "3",IDC_LINK3P,"Button",BS_AUTORADIOBUTTON,94,16,21,13 + CONTROL "4",IDC_LINK4P,"Button",BS_AUTORADIOBUTTON,142,16,21,13 + CONTROL "TCP/IP",IDC_LINKTCP,"Button",BS_AUTORADIOBUTTON | + WS_GROUP,54,47,42,14 + CONTROL "UDP",IDC_LINKUDP,"Button",BS_AUTORADIOBUTTON | + WS_DISABLED,121,47,33,14 + PUSHBUTTON "Start!",IDC_SERVERSTART,79,89,50,17 + LTEXT "Select number of players:",IDC_STATIC,60,7,89,10 + LTEXT "Select protocol:",IDC_STATIC,78,33,53,11 + CONTROL "Speed hacks",IDC_SSPEED,"Button",BS_AUTOCHECKBOX | + WS_TABSTOP,76,70,57,12 +END + +IDD_LINKTAB3 DIALOG DISCARDABLE 0, 0, 210, 108 +STYLE WS_CHILD +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "TCP/IP",IDC_CLINKTCP,"Button",BS_AUTORADIOBUTTON | + WS_GROUP,58,20,39,12 + CONTROL "UDP",IDC_CLINKUDP,"Button",BS_AUTORADIOBUTTON | + WS_DISABLED,118,20,32,12 + EDITTEXT IDC_SERVERIP,84,39,79,12,ES_AUTOHSCROLL | WS_GROUP + PUSHBUTTON "Connect",IDC_LINKCONNECT,75,81,59,16 + LTEXT "Select protocol:",IDC_STATIC,78,7,53,9 + LTEXT "Server IP address or hostname:",IDC_STATIC,7,37,62,18 + LTEXT "Speed hacks:",IDC_STATIC,7,64,47,10 + CONTROL "Off (accurate)",IDC_SPEEDOFF,"Button", + BS_AUTORADIOBUTTON | WS_GROUP,60,63,57,12 + CONTROL "On (fast)",IDC_SPEEDON,"Button",BS_AUTORADIOBUTTON,128, + 63,48,12 +END + +IDD_SERVERWAIT DIALOG DISCARDABLE 0, 0, 186, 90 +STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "Waiting for players" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Cancel",IDCANCEL,63,69,50,14 + CONTROL "Progress1",IDC_SERVERWAIT,"msctls_progress32",WS_BORDER, + 33,50,120,13 + LTEXT "",IDC_STATIC1,7,7,154,8 + LTEXT "",IDC_STATIC2,7,17,105,8 + LTEXT "",IDC_STATIC3,7,25,105,8 + LTEXT "",IDC_STATIC4,7,33,105,8 +END + + + +IDD_OPENDLG DIALOG 36, 24, 202, 117 +STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_CONTEXTHELP | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Open" +FONT 8, "MS Shell Dlg" +BEGIN + LTEXT "File &name:",1090,2,1,81,8 + EDITTEXT 1152,0,10,104,12,ES_AUTOHSCROLL | ES_OEMCONVERT + LISTBOX 1120,1,24,104,53,LBS_SORT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "&Folders:",-1,112,0,53,9 + LTEXT "",1088,113,10,86,9,SS_NOPREFIX + LISTBOX 1121,112,24,88,52,LBS_SORT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "List files of &type:",1089,1,75,81,9 + COMBOBOX 1136,1,87,104,13,CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "Dri&ves:",1091,113,76,70,9 + COMBOBOX 1137,112,87,71,68,CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL | CBS_SORT | CBS_HASSTRINGS | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",IDOK,24,102,50,14,WS_GROUP + PUSHBUTTON "Cancel",IDCANCEL,90,102,50,14,WS_GROUP +END + +IDD_ABOUT DIALOGEX 0, 0, 172, 102 +STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "About" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + DEFPUSHBUTTON "OK",IDOK,6,84,162,12 + ICON IDI_ICON,IDC_STATIC,12,6,21,20,SS_CENTERIMAGE,WS_EX_TRANSPARENT + CTEXT "VisualBoyAdvance Emulator",IDC_STATIC,42,6,126,8 + CTEXT "Copyright © 2005 Forgotten and the VBA team",IDC_STATIC,6,30,162,8 + CTEXT "http://vba.ngemu.com",IDC_URL,90,42,76,8 + CTEXT "Contribution by Costis",IDC_STATIC,6,42,78,8 + CTEXT "Version:",IDC_STATIC,42,18,42,8 + LTEXT "",IDC_VERSION,90,18,78,8 + CTEXT "Changes by Spacy:",IDC_URL2,6,60,162,8 + CTEXT "http://www.spacyhacks.de.vu",IDC_URL3,6,72,162,8 +END + +IDD_DIRECTORIES DIALOG 0, 0, 284, 129 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Directories" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Browse...",IDC_ROM_DIR,174,7,50,14 + PUSHBUTTON "Browse...",IDC_GBROM_DIR,174,26,50,14 + PUSHBUTTON "Browse...",IDC_BATTERY_DIR,174,45,50,14 + PUSHBUTTON "Browse...",IDC_SAVE_DIR,174,64,50,14 + PUSHBUTTON "Browse...",IDC_CAPTURE_DIR,174,83,50,14 + DEFPUSHBUTTON "OK",IDOK,88,108,50,14 + PUSHBUTTON "Cancel",IDCANCEL,145,108,50,14 + LTEXT "ROM:",IDC_STATIC,7,9,20,8 + LTEXT "Battery:",IDC_STATIC,7,47,25,8 + LTEXT "Save Game:",IDC_STATIC,7,66,40,8 + LTEXT "Capture:",IDC_STATIC,7,85,28,8 + EDITTEXT IDC_ROM_PATH,49,7,121,14,ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_BATTERY_PATH,49,45,121,14,ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_SAVE_PATH,49,64,121,14,ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_CAPTURE_PATH,49,83,121,14,ES_AUTOHSCROLL | WS_DISABLED + LTEXT "GB ROM:",IDC_STATIC,7,28,32,8 + EDITTEXT IDC_GBROM_PATH,49,26,121,14,ES_AUTOHSCROLL | WS_DISABLED + PUSHBUTTON "Reset",IDC_ROM_DIR_RESET,228,6,50,14 + PUSHBUTTON "Reset",IDC_GBROM_DIR_RESET,228,25,50,14 + PUSHBUTTON "Reset",IDC_BATTERY_DIR_RESET,228,45,50,14 + PUSHBUTTON "Reset",IDC_SAVE_DIR_RESET,228,64,50,14 + PUSHBUTTON "Reset",IDC_CAPTURE_DIR_RESET,228,83,50,14 +END + +IDD_CONFIG DIALOGEX 0, 0, 135, 211 +STYLE DS_SYSMODAL | DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Joypad configuration" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + EDITTEXT IDC_EDIT_UP,47,5,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_DOWN,47,19,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_LEFT,47,33,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_RIGHT,47,47,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_A,47,61,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_B,47,75,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_L,47,89,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_R,47,103,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_SELECT,47,117,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_START,47,131,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_SPEED,47,145,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_CAPTURE,47,159,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_GS,47,173,81,12,ES_AUTOHSCROLL + PUSHBUTTON "OK",ID_OK,20,190,40,14 + PUSHBUTTON "Cancel",ID_CANCEL,74,190,40,14 + LTEXT "Up:",IDC_STATIC,5,5,35,10 + LTEXT "Down:",IDC_STATIC,5,19,35,10 + LTEXT "Left:",IDC_STATIC,5,33,35,10 + LTEXT "Right:",IDC_STATIC,5,47,35,10 + LTEXT "Button A:",IDC_STATIC,5,61,35,10 + LTEXT "Button B:",IDC_STATIC,5,75,35,10 + LTEXT "Button L:",IDC_STATIC,5,89,35,10 + LTEXT "Button R:",IDC_STATIC,5,103,35,10 + LTEXT "Select:",IDC_STATIC,5,117,35,10 + LTEXT "Start:",IDC_STATIC,5,131,35,10 + LTEXT "Turbo:",IDC_STATIC,5,145,35,10 + LTEXT "Capture:",IDC_STATIC,5,159,35,10 + LTEXT "GS:",IDC_STATIC,5,173,35,10 +END + +IDD_CHEATS DIALOG 0, 0, 276, 253 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Search for cheats" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | LVS_SINGLESEL | WS_BORDER | WS_TABSTOP,3,5,265,111 + CONTROL "Ol&d value",IDC_OLD_VALUE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,11,129,46,10 + CONTROL "Specifi&c value",IDC_SPECIFIC_VALUE,"Button",BS_AUTORADIOBUTTON,11,141,61,10 + CONTROL "&8 bits",IDC_SIZE_8,"Button",BS_AUTORADIOBUTTON | WS_GROUP,11,167,33,10 + CONTROL "&16 bits",IDC_SIZE_16,"Button",BS_AUTORADIOBUTTON,11,179,37,10 + CONTROL "&32 bits",IDC_SIZE_32,"Button",BS_AUTORADIOBUTTON,11,191,37,10 + CONTROL "&Equal",IDC_EQ,"Button",BS_AUTORADIOBUTTON | WS_GROUP,100,128,34,10 + CONTROL "&Not equal",IDC_NE,"Button",BS_AUTORADIOBUTTON,100,140,47,10 + CONTROL "&Less than",IDC_LT,"Button",BS_AUTORADIOBUTTON,100,152,47,10 + CONTROL "Le&ss or equal",IDC_LE,"Button",BS_AUTORADIOBUTTON,100,164,58,10 + CONTROL "&Greather than",IDC_GT,"Button",BS_AUTORADIOBUTTON,100,176,59,10 + CONTROL "G&reater or equal",IDC_GE,"Button",BS_AUTORADIOBUTTON,100,188,67,10 + CONTROL "S&igned",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP,202,130,38,10 + CONTROL "&Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,202,142,46,10 + CONTROL "&Hexadecimal",IDC_HEXADECIMAL,"Button",BS_AUTORADIOBUTTON,202,154,57,10 + CONTROL "U&pdate values",IDC_UPDATE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,192,192,62,10 + EDITTEXT IDC_VALUE,95,211,172,14,ES_AUTOHSCROLL + PUSHBUTTON "&Start",IDC_START,15,237,50,14,WS_GROUP + PUSHBUTTON "S&earch",IDC_SEARCH,80,236,50,14 + PUSHBUTTON "&Add cheat",IDC_ADD_CHEAT,145,236,50,14 + DEFPUSHBUTTON "OK",ID_OK,210,236,50,14 + GROUPBOX "&Search type",IDC_STATIC,3,118,84,36 + GROUPBOX "&Data size",IDC_STATIC,3,158,84,44 + GROUPBOX "Compare type",IDC_STATIC,95,118,92,84 + GROUPBOX "Signed/Unsigned",IDC_STATIC,192,118,76,50 + LTEXT "Enter &value:",IDC_STATIC,3,214,69,8 +END + +IDD_ADD_CHEAT DIALOG 0, 0, 186, 137 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Add cheat" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,60,6,123,14,ES_AUTOHSCROLL + EDITTEXT IDC_VALUE,60,24,123,14,ES_AUTOHSCROLL + EDITTEXT IDC_DESC,60,42,123,14,ES_AUTOHSCROLL + CONTROL "8-bit",IDC_SIZE_8,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,11,70,29,10 + CONTROL "16-bit",IDC_SIZE_16,"Button",BS_AUTORADIOBUTTON,62,70,33,10 + CONTROL "32-bit",IDC_SIZE_32,"Button",BS_AUTORADIOBUTTON,117,70,33,10 + CONTROL "&Signed",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,11,97,38,10 + CONTROL "&Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,62,98,46,10 + CONTROL "&Hexadecimal",IDC_HEXADECIMAL,"Button",BS_AUTORADIOBUTTON,117,98,57,10 + DEFPUSHBUTTON "&OK",ID_OK,36,116,50,14,WS_GROUP + PUSHBUTTON "&Cancel",ID_CANCEL,99,116,50,14 + LTEXT "&Value:",IDC_STATIC,3,27,54,8 + GROUPBOX "Number format",IDC_STATIC,3,88,180,24 + LTEXT "&Address:",IDC_STATIC,3,9,54,8 + GROUPBOX "Size",IDC_STATIC,3,60,180,24 + LTEXT "&Description:",IDC_STATIC,3,45,55,8 +END + +IDD_CHEAT_LIST DIALOG 0, 0, 280, 250 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Cheat list" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Restore &previous values",IDC_RESTORE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,5,183,92,10 + PUSHBUTTON "&Code...",IDC_ADD_CODE,9,208,64,14,WS_GROUP + PUSHBUTTON "C&heat...",IDC_ADD_CHEAT,75,208,64,14 + PUSHBUTTON "&Gameshark...",IDC_ADD_GAMESHARK,141,208,64,14 + PUSHBUTTON "CodeBreaker...",IDC_ADD_CODEBREAKER,206,208,64,14 + PUSHBUTTON "&Remove",IDC_REMOVE,9,230,64,14 + PUSHBUTTON "Remove A&ll",IDC_REMOVE_ALL,75,230,64,14 + PUSHBUTTON "&Enable/Dis.",IDC_ENABLE,141,230,64,14 + DEFPUSHBUTTON "&OK",ID_OK,206,230,64,14,WS_GROUP + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | WS_BORDER | WS_GROUP | WS_TABSTOP,5,15,269,156 + LTEXT "Status legend:",IDC_STATIC,6,3,46,8 + LTEXT "E: Enabled",IDC_STATIC,188,3,36,8 + LTEXT "D: Disabled",IDC_STATIC,234,3,38,8 + GROUPBOX "Add",IDC_STATIC,5,199,268,27 +END + +IDD_ASSOCIATIONS DIALOG 0, 0, 116, 95 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Associations" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL ".gb",IDC_GB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,15,26,10 + CONTROL ".sgb",IDC_SGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,29,29,10 + CONTROL ".cgb",IDC_CGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,43,30,10 + CONTROL ".gbc",IDC_GBC,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,57,30,10 + CONTROL ".gba",IDC_GBA,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,13,30,10 + CONTROL ".agb",IDC_AGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,27,30,10 + CONTROL ".bin",IDC_BIN,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,41,27,10 + DEFPUSHBUTTON "OK",ID_OK,3,78,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,63,78,50,14 + GROUPBOX "GBA Types",IDC_STATIC,63,3,50,51 + GROUPBOX "GB Types",IDC_STATIC,3,3,50,69 +END + +IDD_GBA_ROM_INFO DIALOGEX 0, 0, 208, 126 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "ROM Header" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + DEFPUSHBUTTON "OK",ID_OK,6,108,198,12 + RTEXT "Game Title:",IDC_STATIC,6,6,60,12 + RTEXT "Game Code:",IDC_STATIC,6,18,60,12 + RTEXT "Maker Code:",IDC_STATIC,6,30,60,12 + RTEXT "Main Unit Code:",IDC_STATIC,6,54,60,12 + RTEXT "Device Type:",IDC_STATIC,6,66,60,12 + RTEXT "ROM Version:",IDC_STATIC,6,78,60,12 + RTEXT "Complement:",IDC_STATIC,6,90,60,12 + LTEXT "",IDC_ROM_TITLE,78,6,126,12 + LTEXT "",IDC_ROM_GAME_CODE,78,18,126,12 + LTEXT "",IDC_ROM_MAKER_CODE,78,30,126,12 + LTEXT "",IDC_ROM_UNIT_CODE,78,54,126,12 + LTEXT "",IDC_ROM_DEVICE_TYPE,78,66,126,12 + LTEXT "",IDC_ROM_VERSION,78,78,126,12 + LTEXT "",IDC_ROM_CRC,78,90,126,12 + LTEXT "",IDC_ROM_MAKER_NAME,78,42,126,12 + RTEXT "Maker Name:",IDC_STATIC,6,42,60,12 +END + +IDD_GB_ROM_INFO DIALOG 0, 0, 220, 225 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Rom information" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,84,200,50,14 + LTEXT "Game title:",IDC_STATIC,7,10,60,8 + LTEXT "Maker code:",IDC_STATIC,7,38,60,8 + LTEXT "Unit code:",IDC_STATIC,7,68,60,8 + LTEXT "Cartridge type:",IDC_STATIC,7,82,60,8 + LTEXT "ROM version:",IDC_STATIC,7,152,60,8 + LTEXT "CRC:",IDC_STATIC,7,166,60,8 + LTEXT "",IDC_ROM_TITLE,80,10,133,8 + LTEXT "",IDC_ROM_MAKER_CODE,80,38,133,8 + LTEXT "",IDC_ROM_UNIT_CODE,80,68,133,8 + LTEXT "",IDC_ROM_DEVICE_TYPE,80,82,133,8 + LTEXT "",IDC_ROM_VERSION,80,152,133,8 + LTEXT "",IDC_ROM_CRC,80,166,133,8 + LTEXT "Color:",IDC_STATIC,7,24,60,8 + LTEXT "",IDC_ROM_COLOR,80,24,133,8 + LTEXT "ROM size:",IDC_STATIC,7,96,60,8 + LTEXT "",IDC_ROM_SIZE,80,96,133,8 + LTEXT "RAM size:",IDC_STATIC,7,110,60,8 + LTEXT "",IDC_ROM_RAM_SIZE,80,110,133,8 + LTEXT "Dest. code:",IDC_STATIC,7,124,60,8 + LTEXT "",IDC_ROM_DEST_CODE,80,124,133,8 + LTEXT "License code:",IDC_STATIC,7,138,60,8 + LTEXT "",IDC_ROM_LIC_CODE,80,138,133,8 + LTEXT "Checksum:",IDC_STATIC,7,180,60,8 + LTEXT "",IDC_ROM_CHECKSUM,80,180,133,8 + LTEXT "",IDC_ROM_MAKER_NAME2,80,52,133,8 + LTEXT "Maker name:",IDC_STATIC,7,52,60,8 +END + +IDD_GB_CHEAT_LIST DIALOG 0, 0, 286, 221 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Gameboy Cheat List" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | WS_BORDER | WS_GROUP | WS_TABSTOP,9,20,269,156 + PUSHBUTTON "Add &GameGenie...",IDC_ADD_GG_CHEAT,9,183,80,14,WS_GROUP + PUSHBUTTON "&Add GameShark...",IDC_ADD_GS_CHEAT,103,183,80,14,WS_GROUP + PUSHBUTTON "&Remove",IDC_REMOVE,197,183,80,14 + PUSHBUTTON "Remove A&ll",IDC_REMOVE_ALL,9,202,80,14 + PUSHBUTTON "&Enable/Dis.",IDC_ENABLE,103,202,80,14 + DEFPUSHBUTTON "&OK",ID_OK,197,202,80,14 + LTEXT "Status legend:",IDC_STATIC,10,9,46,8 + LTEXT "E: Enabled",IDC_STATIC,195,9,36,8 + LTEXT "D: Disabled",IDC_STATIC,241,9,38,8 +END + +IDD_ADD_CHEAT_DLG DIALOG 0, 0, 182, 107 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Title" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_DESC,60,7,120,14,ES_AUTOHSCROLL + EDITTEXT IDC_CODE,60,23,120,58,ES_MULTILINE | ES_UPPERCASE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",ID_OK,33,86,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,86,50,14 + LTEXT "&Description:",IDC_STATIC,3,10,54,8 + LTEXT "&Code:",IDC_STATIC,3,29,54,8 +END + +IDD_GB_PRINTER DIALOG 0, 0, 178, 209 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "GB Printer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "&1x",IDC_1X,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,166,22,10 + CONTROL "&2x",IDC_2X,"Button",BS_AUTORADIOBUTTON,55,166,23,10 + CONTROL "&3x",IDC_3X,"Button",BS_AUTORADIOBUTTON,98,166,23,10 + CONTROL "&4x",IDC_4X,"Button",BS_AUTORADIOBUTTON,141,166,23,10 + DEFPUSHBUTTON "&Print...",ID_PRINT,7,190,50,14,WS_GROUP + PUSHBUTTON "&Save...",ID_SAVE,64,190,50,14 + PUSHBUTTON "&Close",ID_OK,121,190,50,14 + CONTROL "",IDC_GB_PRINTER,"Static",SS_BLACKFRAME | WS_GROUP,7,6,162,146 + GROUPBOX "Print Size",IDC_STATIC,7,156,162,25 +END + +IDD_MOTION_CONFIG DIALOG 0, 0, 135, 78 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Motion Sensor" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_EDIT_UP,47,2,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_DOWN,47,16,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_LEFT,47,30,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_RIGHT,47,44,81,12,ES_AUTOHSCROLL + PUSHBUTTON "OK",ID_OK,20,60,40,14 + PUSHBUTTON "Cancel",ID_CANCEL,74,60,40,14 + LTEXT "Up:",IDC_STATIC,5,2,35,10 + LTEXT "Down:",IDC_STATIC,5,16,35,10 + LTEXT "Left:",IDC_STATIC,5,30,35,10 + LTEXT "Right:",IDC_STATIC,5,44,35,10 +END + +IDD_LANG_SELECT DIALOG 0, 0, 186, 68 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Language selection" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_LANG_STRING,140,25,40,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,30,49,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,104,49,50,14 + LTEXT "Current system language is:",IDC_STATIC,6,9,123,8 + LTEXT "Enter language name (3 letter):",IDC_STATIC,6,30,127,8 + LTEXT "",IDC_LANG_NAME,140,9,40,8 +END + +IDD_CODE_SELECT DIALOG 0, 0, 316, 235 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select codes to import" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,91,214,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,174,214,50,14 + LISTBOX IDC_GAME_LIST,7,7,302,205,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP +END + +IDD_MAP_VIEW DIALOG 0, 0, 322, 238 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Map view" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Frame 0",IDC_FRAME_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,22,41,10 + CONTROL "Frame 1",IDC_FRAME_1,"Button",BS_AUTORADIOBUTTON,13,36,41,10 + CONTROL "BG0",IDC_BG0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,63,30,10 + CONTROL "BG1",IDC_BG1,"Button",BS_AUTORADIOBUTTON,13,77,30,10 + CONTROL "BG2",IDC_BG2,"Button",BS_AUTORADIOBUTTON,13,91,30,10 + CONTROL "BG3",IDC_BG3,"Button",BS_AUTORADIOBUTTON,13,105,30,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,7,122,68,10 + CONTROL "Auto update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,7,134,55,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,25,217,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,88,217,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,155,217,50,14 + CONTROL "MapView",IDC_MAP_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,187,15,128,128 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,148,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,187,164,48,47 + LTEXT "",IDC_R,245,173,50,8 + LTEXT "",IDC_G,245,185,50,8 + LTEXT "",IDC_B,245,197,50,8 + GROUPBOX "Frame",IDC_STATIC,7,11,63,37 + GROUPBOX "Background",IDC_STATIC,7,52,63,67 + LTEXT "",IDC_XY,129,95,53,8 + LTEXT "Mode:",IDC_STATIC,80,15,34,8 + LTEXT "",IDC_MODE,130,15,53,8 + LTEXT "Map Base:",IDC_STATIC,80,25,35,8 + LTEXT "",IDC_MAPBASE,130,25,53,8 + LTEXT "Char Base:",IDC_STATIC,80,35,36,8 + LTEXT "",IDC_CHARBASE,130,35,53,8 + LTEXT "Size:",IDC_STATIC,80,45,37,8 + LTEXT "",IDC_DIM,130,45,53,8 + LTEXT "Colors:",IDC_STATIC,80,55,37,8 + LTEXT "",IDC_NUMCOLORS,130,55,53,8 + LTEXT "Priority:",IDC_STATIC,80,65,37,8 + LTEXT "",IDC_PRIORITY,130,65,53,8 + LTEXT "Mosaic:",IDC_STATIC,80,75,37,8 + LTEXT "",IDC_MOSAIC,130,75,53,8 + LTEXT "Overflow:",IDC_STATIC,80,85,37,8 + LTEXT "",IDC_OVERFLOW,130,85,53,8 + LTEXT "Address:",IDC_STATIC,80,105,37,8 + LTEXT "",IDC_ADDRESS,130,105,53,8 + LTEXT "Tile:",IDC_STATIC,80,115,37,8 + LTEXT "",IDC_TILE_NUM,130,115,53,8 + LTEXT "Flip:",IDC_STATIC,80,125,37,8 + LTEXT "",IDC_FLIP,130,125,53,8 + LTEXT "Palette:",IDC_STATIC,80,135,37,8 + LTEXT "",IDC_PALETTE_NUM,130,135,53,8 +END + +IDD_PALETTE_VIEW DIALOG 0, 0, 316, 266 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Palette View" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Save BG...",IDC_SAVE_BG,30,245,50,14 + PUSHBUTTON "Save OBJ...",IDC_SAVE_OBJ,98,245,50,14 + PUSHBUTTON "&Refresh",IDC_REFRESH2,166,245,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,234,245,50,14 + LTEXT "",IDC_ADDRESS,53,168,50,8 + LTEXT "",IDC_R,53,180,50,8 + LTEXT "",IDC_G,53,192,50,8 + LTEXT "",IDC_B,53,204,50,8 + LTEXT "",IDC_VALUE,53,216,50,8 + CONTROL "Custom1",IDC_COLOR,"VbaColorControl",WS_TABSTOP,161,168,50,50 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW,"VbaPaletteViewControl",WS_TABSTOP,12,30,128,128 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW_OBJ, + "VbaPaletteViewControl",WS_TABSTOP,166,30,128,128 + GROUPBOX "Background",IDC_STATIC,7,20,137,143 + GROUPBOX "Sprite",IDC_STATIC,161,20,137,143 + LTEXT "Address:",IDC_STATIC,7,168,38,8 + LTEXT "R:",IDC_STATIC,7,180,41,8 + LTEXT "G:",IDC_STATIC,7,192,43,8 + LTEXT "B:",IDC_STATIC,7,204,38,8 + LTEXT "Value:",IDC_STATIC,7,216,38,8 + LTEXT "Click on a color for more information",IDC_STATIC,7,7,302,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,229,71,10 +END + +IDD_MEM_VIEWER DIALOG 0, 0, 380, 178 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Memory viewer" +FONT 8, "MS Sans Serif" +BEGIN + COMBOBOX IDC_ADDRESSES,7,7,109,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + CONTROL "8-bit",IDC_8_BIT,"Button",BS_AUTORADIOBUTTON | WS_GROUP,120,9,29,10 + CONTROL "16-bit",IDC_16_BIT,"Button",BS_AUTORADIOBUTTON,154,9,33,10 + CONTROL "32-bit",IDC_32_BIT,"Button",BS_AUTORADIOBUTTON,192,9,33,10 + EDITTEXT IDC_ADDRESS,238,7,82,14,ES_UPPERCASE | ES_AUTOHSCROLL | ES_WANTRETURN | WS_GROUP + DEFPUSHBUTTON "&Go",IDC_GO,323,7,50,14,WS_GROUP + CONTROL "Viewer",IDC_VIEWER,"VbaMemoryViewer",WS_TABSTOP,7,22,366,112 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,139,71,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,67,157,50,14 + PUSHBUTTON "&Load...",IDC_LOAD,132,157,50,14 + PUSHBUTTON "&Save...",IDC_SAVE,197,157,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,262,157,50,14 + LTEXT "Current address:",IDC_CURRENT_ADDRESS_LABEL,210,142,77,8 + EDITTEXT IDC_CURRENT_ADDRESS,291,139,82,14,ES_RIGHT | ES_AUTOHSCROLL | WS_DISABLED +END + +IDD_OAM_VIEW DIALOG 0, 0, 234, 185 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Oam Viewer" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_SPRITE,7,19,76,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + SCROLLBAR IDC_SCROLLBAR,7,33,76,11 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,138,79,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,7,164,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,91,164,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,177,164,50,14 + CONTROL "MapView",IDC_OAM_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,87,7,64,64 + CONTROL "Zoom",IDC_OAM_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,163,7,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,87,79,48,47 + LTEXT "",IDC_POS,31,47,50,8 + LTEXT "",IDC_MODE,31,57,50,8 + LTEXT "",IDC_COLORS,31,67,50,8 + LTEXT "",IDC_PALETTE,31,77,50,8 + LTEXT "",IDC_TILE,31,87,50,8 + LTEXT "",IDC_PRIO,31,97,50,8 + LTEXT "",IDC_SIZE2,31,107,50,8 + LTEXT "",IDC_ROT,31,117,50,8 + LTEXT "",IDC_FLAGS,31,127,50,8 + LTEXT "",IDC_R,145,88,50,8 + LTEXT "",IDC_G,145,100,50,8 + LTEXT "",IDC_B,145,112,50,8 + LTEXT "Pos:",IDC_STATIC,7,47,24,8 + LTEXT "Mode:",IDC_STATIC,7,57,24,8 + LTEXT "Colors:",IDC_STATIC,7,67,24,8 + LTEXT "Pal:",IDC_STATIC,7,77,24,8 + LTEXT "Tile:",IDC_STATIC,7,87,24,8 + LTEXT "Prio:",IDC_STATIC,7,97,24,8 + LTEXT "Size:",IDC_STATIC,7,107,24,8 + LTEXT "Sprite:",IDC_STATIC,7,7,50,8 + LTEXT "Rot.:",IDC_STATIC,7,117,24,8 + LTEXT "Flags:",IDC_STATIC,7,127,24,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,150,71,10 +END + +IDD_ACCEL_EDITOR DIALOG 0, 0, 280, 121 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Accelerator editor" +FONT 8, "MS Sans Serif" +BEGIN + LTEXT "&Commands:",IDC_STATIC,9,9,38,8 + LISTBOX IDC_COMMANDS,9,18,100,67,LBS_SORT | LBS_NOINTEGRALHEIGHT | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "Current &Keys:",IDC_STATIC1,113,9,43,8 + LISTBOX IDC_CURRENTS,113,18,100,67,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",ID_OK,223,9,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,223,25,50,14 + LTEXT "Select &New Shortcut Key:",IDC_STATIC3,113,90,82,8 + EDITTEXT IDC_EDIT_KEY,113,102,100,12,ES_AUTOHSCROLL + PUSHBUTTON "&Assign",IDC_ASSIGN,223,70,50,14 + PUSHBUTTON "&Remove",IDC_REMOVE,223,86,50,14 + PUSHBUTTON "Re&set All",IDC_RESET,223,102,50,14 + LTEXT "Static",IDC_ALREADY_AFFECTED,9,102,100,12,SS_CENTERIMAGE + LTEXT "Currently assigned to :",IDC_STATIC2,9,90,73,10 +END + +IDD_TILE_VIEWER DIALOG 0, 0, 326, 266 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Tile Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "16",IDC_16_COLORS,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,20,24,10 + CONTROL "256",IDC_256_COLORS,"Button",BS_AUTORADIOBUTTON,13,30,28,10 + CONTROL "0x6000000",IDC_CHARBASE_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,57,51,10 + CONTROL "0x6004000",IDC_CHARBASE_1,"Button",BS_AUTORADIOBUTTON,13,67,51,10 + CONTROL "0x6008000",IDC_CHARBASE_2,"Button",BS_AUTORADIOBUTTON,13,77,51,10 + CONTROL "0x600C000",IDC_CHARBASE_3,"Button",BS_AUTORADIOBUTTON,13,87,52,10 + CONTROL "0x6010000",IDC_CHARBASE_4,"Button",BS_AUTORADIOBUTTON,13,97,49,10 + CONTROL "Slider1",IDC_PALETTE_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | WS_GROUP | WS_TABSTOP,1,124,76,22 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,149,79,10 + PUSHBUTTON "Refresh",IDC_REFRESH,7,245,50,14,WS_GROUP + PUSHBUTTON "Save...",IDC_SAVE,138,245,50,14 + PUSHBUTTON "Close",IDC_CLOSE,269,245,50,14 + CONTROL "Custom1",IDC_TILE_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,191,7,128,128 + GROUPBOX "Colors",IDC_STATIC,7,7,66,38 + GROUPBOX "Char Base",IDC_STATIC,7,46,65,64 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,174,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,98,183,48,47 + LTEXT "",IDC_R,156,192,50,8 + LTEXT "",IDC_G,156,204,50,8 + LTEXT "",IDC_B,156,216,50,8 + LTEXT "Palette:",IDC_STATIC,7,113,65,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,160,71,10 + LTEXT "Tile:",IDC_STATIC,79,14,41,8 + LTEXT "Address:",IDC_STATIC,79,26,41,8 + LTEXT "",IDC_TILE_NUMBER,135,14,50,8 + LTEXT "",IDC_ADDRESS,135,26,50,8 +END + +IDD_GB_COLORS DIALOG 0, 0, 169, 121 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Gameboy Mono Colors" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Default",IDC_DEFAULT,"Button",BS_AUTORADIOBUTTON | WS_GROUP,7,7,39,10 + CONTROL "User 1",IDC_USER1,"Button",BS_AUTORADIOBUTTON,67,7,37,10 + CONTROL "User 2",IDC_USER2,"Button",BS_AUTORADIOBUTTON,125,7,37,10 + COMBOBOX IDC_PREDEFINED,7,21,155,30,CBS_DROPDOWNLIST | CBS_SORT | WS_VSCROLL | WS_TABSTOP + PUSHBUTTON "",IDC_COLOR_BG0,15,47,28,14,WS_GROUP + PUSHBUTTON "",IDC_COLOR_BG1,52,47,28,14 + PUSHBUTTON "",IDC_COLOR_BG2,89,47,28,14 + PUSHBUTTON "",IDC_COLOR_BG3,126,47,28,14 + PUSHBUTTON "",IDC_COLOR_OB0,15,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB1,52,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB2,89,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB3,126,78,28,14 + PUSHBUTTON "Reset",IDC_RESET,7,100,50,14 + DEFPUSHBUTTON "OK",ID_OK,59,100,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,111,100,50,14 + GROUPBOX "Background",IDC_STATIC,8,37,154,29 + GROUPBOX "Sprite",IDC_STATIC,8,67,154,30 +END + +IDD_DISASSEMBLE DIALOG 0, 0, 402, 225 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Disassemble" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Automatic",IDC_AUTOMATIC,"Button",BS_AUTORADIOBUTTON | WS_GROUP,7,9,47,10 + CONTROL "ARM",IDC_ARM,"Button",BS_AUTORADIOBUTTON,62,9,32,10 + CONTROL "THUMB",IDC_THUMB,"Button",BS_AUTORADIOBUTTON,103,9,42,10 + EDITTEXT IDC_ADDRESS,158,7,65,14,ES_UPPERCASE | ES_AUTOHSCROLL | WS_GROUP + DEFPUSHBUTTON "Go",IDC_GO,232,7,50,14 + LISTBOX IDC_DISASSEMBLE,7,25,276,161,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_TABSTOP + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,191,71,10 + PUSHBUTTON "Refresh",IDC_REFRESH,120,204,50,14 + PUSHBUTTON "Next",IDC_NEXT,233,204,50,14 + PUSHBUTTON "Close",IDC_CLOSE,346,204,50,14 + LTEXT "R0:",IDC_STATIC,309,7,18,8 + LTEXT "R1:",IDC_STATIC,309,15,18,8 + LTEXT "R2:",IDC_STATIC,309,23,18,8 + LTEXT "R3:",IDC_STATIC,309,31,18,8 + LTEXT "R4:",IDC_STATIC,309,39,18,8 + LTEXT "R5:",IDC_STATIC,309,47,18,8 + LTEXT "R6:",IDC_STATIC,309,55,18,8 + LTEXT "R7:",IDC_STATIC,309,63,18,8 + LTEXT "",IDC_R0,344,7,52,8 + LTEXT "",IDC_R1,344,15,52,8 + LTEXT "",IDC_R2,344,23,52,8 + LTEXT "",IDC_R3,344,31,52,8 + LTEXT "",IDC_R4,344,39,52,8 + LTEXT "",IDC_R5,344,47,52,8 + LTEXT "",IDC_R6,344,55,52,8 + LTEXT "",IDC_R7,344,63,52,8 + LTEXT "",IDC_R8,344,71,52,8 + LTEXT "",IDC_R9,344,79,52,8 + LTEXT "",IDC_R10,344,87,52,8 + LTEXT "",IDC_R11,344,95,52,8 + LTEXT "",IDC_R12,344,103,52,8 + LTEXT "",IDC_R13,344,111,52,8 + LTEXT "",IDC_R14,344,119,52,8 + LTEXT "",IDC_R15,344,127,52,8 + LTEXT "R8:",IDC_STATIC,309,71,18,8 + LTEXT "R9:",IDC_STATIC,309,79,18,8 + LTEXT "R10:",IDC_STATIC,309,87,18,8 + LTEXT "R11:",IDC_STATIC,309,95,18,8 + LTEXT "R12:",IDC_STATIC,309,103,18,8 + LTEXT "R13:",IDC_STATIC,309,111,18,8 + LTEXT "R14:",IDC_STATIC,309,119,18,8 + LTEXT "R15:",IDC_STATIC,309,127,18,8 + LTEXT "",IDC_R16,344,135,52,8 + LTEXT "R16:",IDC_STATIC,309,135,20,8 + CONTROL "N",IDC_N,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,146,21,10 + CONTROL "Z",IDC_Z,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,156,21,10 + CONTROL "C",IDC_C,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,166,21,10 + CONTROL "V",IDC_V,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,176,21,10 + CONTROL "F",IDC_F,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,156,20,10 + CONTROL "I",IDC_I,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,146,18,10 + CONTROL "T",IDC_T,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,166,21,10 + LTEXT "Mode:",IDC_STATIC,341,176,21,8 + LTEXT "",IDC_MODE,376,176,20,8 + SCROLLBAR IDC_VSCROLL,283,25,10,161,SBS_VERT + PUSHBUTTON "Goto R15",IDC_GOPC,7,204,50,14 +END + +IDD_GDB_PORT DIALOG 0, 0, 186, 51 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "GDB connection" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,34,30,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,102,30,50,14 + LTEXT "Port to wait for connection:",IDC_STATIC,7,10,105,8 + EDITTEXT IDC_PORT,125,7,54,14,ES_RIGHT | ES_AUTOHSCROLL +END + +IDD_GDB_WAITING DIALOG 0, 0, 186, 44 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Waiting..." +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Cancel",ID_CANCEL,67,23,50,14 + LTEXT "Waiting for connection on port:",IDC_STATIC,7,7,117,8 + LTEXT "",IDC_PORT,143,7,36,8 +END + +IDD_LOGGING DIALOG 0, 0, 366, 218 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Logging" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "SWI",IDC_VERBOSE_SWI,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,17,87,10 + CONTROL "Unaligned memory",IDC_VERBOSE_UNALIGNED_ACCESS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,30,87,10 + CONTROL "Illegal write",IDC_VERBOSE_ILLEGAL_WRITE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,43,87,10 + CONTROL "Illegal read",IDC_VERBOSE_ILLEGAL_READ,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,56,87,10 + CONTROL "DMA 0",IDC_VERBOSE_DMA0,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,69,87,10 + CONTROL "DMA 1",IDC_VERBOSE_DMA1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,82,87,10 + CONTROL "DMA 2",IDC_VERBOSE_DMA2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,95,87,10 + CONTROL "DMA 3",IDC_VERBOSE_DMA3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,108,87,10 + CONTROL "Undefined instruction",IDC_VERBOSE_UNDEFINED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,121,87,10 + CONTROL "AGBPrint",IDC_VERBOSE_AGBPRINT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,134,87,10 + EDITTEXT IDC_LOG,107,7,252,183,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | WS_VSCROLL | WS_HSCROLL + PUSHBUTTON "Save...",IDC_SAVE,75,197,50,14 + PUSHBUTTON "Clear",IDC_CLEAR,137,197,50,14 + DEFPUSHBUTTON "OK",ID_OK,197,197,50,14 + GROUPBOX "Verbose",IDC_STATIC,7,7,93,142 +END + +IDD_EXPORT_SPS DIALOG 0, 0, 248, 148 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Export Gameshark Snapshot" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_TITLE,84,7,157,14,ES_AUTOHSCROLL + EDITTEXT IDC_DESC,84,27,157,14,ES_AUTOHSCROLL + EDITTEXT IDC_NOTES,84,47,157,73,ES_MULTILINE | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",ID_OK,67,127,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,130,127,50,14 + LTEXT "Title:",IDC_STATIC,7,8,62,8 + LTEXT "Description:",IDC_STATIC,7,28,63,8 + LTEXT "Notes:",IDC_STATIC,7,48,60,8 +END + +IDD_ADDR_SIZE DIALOG 0, 0, 186, 67 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Enter address and size" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,99,6,80,14,ES_AUTOHSCROLL + EDITTEXT IDC_SIZE_CONTROL,99,26,80,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,34,46,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,102,46,50,14 + LTEXT "Address:",IDC_STATIC,7,11,65,8 + LTEXT "Size:",IDC_STATIC,7,29,65,8 +END + +IDD_MODES DIALOG 0, 0, 208, 129 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select video mode" +FONT 8, "MS Sans Serif" +BEGIN + LISTBOX IDC_MODES,7,18,194,80,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",ID_OK,45,108,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,112,108,50,14 + LTEXT "Available video modes:",IDC_STATIC,7,7,194,8 +END + +IDD_DRIVERS DIALOG 0, 0, 208, 121 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select video driver" +FONT 8, "MS Sans Serif" +BEGIN + LISTBOX IDC_DRIVERS,7,17,194,80,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",ID_OK,45,104,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,112,104,50,14 + LTEXT "Available drivers:",IDC_STATIC,7,7,194,8 +END + +IDD_THROTTLE DIALOG 0, 0, 186, 63 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Throttle" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_THROTTLE,7,20,172,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,42,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,42,50,14 + LTEXT "Enter desired throttle (5%...1000%):",IDC_STATIC,7,7,172,8 +END + +IDD_GB_DISASSEMBLE DIALOG 0, 0, 344, 225 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "GB Disassemble" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,7,7,65,14,ES_UPPERCASE | ES_AUTOHSCROLL | WS_GROUP + DEFPUSHBUTTON "Go",IDC_GO,81,7,50,14 + LISTBOX IDC_DISASSEMBLE,7,25,222,161,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_TABSTOP + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,191,71,10 + PUSHBUTTON "Refresh",IDC_REFRESH,100,204,50,14 + PUSHBUTTON "Next",IDC_NEXT,193,204,50,14 + PUSHBUTTON "Close",IDC_CLOSE,287,204,50,14 + LTEXT "AF:",IDC_STATIC,250,25,18,8 + LTEXT "BC:",IDC_STATIC,250,35,18,8 + LTEXT "DE:",IDC_STATIC,250,45,18,8 + LTEXT "HL:",IDC_STATIC,250,55,18,8 + LTEXT "IFF:",IDC_STATIC,250,85,18,8 + LTEXT "",IDC_R0,285,25,52,8 + LTEXT "",IDC_R1,285,35,52,8 + LTEXT "",IDC_R2,285,45,52,8 + LTEXT "",IDC_R3,285,55,52,8 + LTEXT "",IDC_R6,285,85,52,8 + CONTROL "N",IDC_N,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,109,21,10 + CONTROL "Z",IDC_Z,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,97,21,10 + CONTROL "C",IDC_C,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,133,21,10 + CONTROL "H",IDC_H,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,121,21,10 + SCROLLBAR IDC_VSCROLL,229,25,10,161,SBS_VERT + PUSHBUTTON "Goto PC",IDC_GOPC,7,204,50,14 + LTEXT "SP:",IDC_STATIC,250,65,18,8 + LTEXT "",IDC_R4,285,65,52,8 + LTEXT "PC:",IDC_STATIC,250,75,18,8 + LTEXT "",IDC_R5,285,75,52,8 +END + +IDD_GB_OAM_VIEW DIALOG 0, 0, 234, 185 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Oam Viewer" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_SPRITE,7,19,76,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + SCROLLBAR IDC_SCROLLBAR,7,33,76,11 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,138,79,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,7,164,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,91,164,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,177,164,50,14 + CONTROL "MapView",IDC_OAM_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,87,7,64,64 + CONTROL "Zoom",IDC_OAM_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,163,7,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,87,79,48,47 + LTEXT "",IDC_POS,31,47,50,8 + LTEXT "",IDC_PALETTE,31,87,50,8 + LTEXT "",IDC_TILE,31,57,50,8 + LTEXT "",IDC_PRIO,31,67,50,8 + LTEXT "",IDC_OAP,31,77,50,8 + LTEXT "",IDC_FLAGS,31,97,50,8 + LTEXT "",IDC_R,145,88,50,8 + LTEXT "",IDC_G,145,100,50,8 + LTEXT "",IDC_B,145,112,50,8 + LTEXT "Pos:",IDC_STATIC,7,47,24,8 + LTEXT "Pal:",IDC_STATIC,7,87,24,8 + LTEXT "Tile:",IDC_STATIC,7,57,24,8 + LTEXT "Prio:",IDC_STATIC,7,67,24,8 + LTEXT "OAP:",IDC_STATIC,7,77,24,8 + LTEXT "Sprite:",IDC_STATIC,7,7,50,8 + LTEXT "Flags:",IDC_STATIC,7,97,24,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,150,71,10 + LTEXT "",IDC_BANK,31,107,50,8 + LTEXT "Bank:",IDC_STATIC,7,107,24,8 +END + +IDD_GB_TILE_VIEWER DIALOG 0, 0, 326, 238 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Tile Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "0",IDC_BANK_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,20,20,10 + CONTROL "1",IDC_BANK_1,"Button",BS_AUTORADIOBUTTON,13,30,20,10 + CONTROL "0x8000",IDC_CHARBASE_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,57,39,10 + CONTROL "0x8800",IDC_CHARBASE_1,"Button",BS_AUTORADIOBUTTON,13,67,39,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,122,79,10 + PUSHBUTTON "Refresh",IDC_REFRESH,7,217,50,14,WS_GROUP + PUSHBUTTON "Save...",IDC_SAVE,138,217,50,14 + PUSHBUTTON "Close",IDC_CLOSE,269,217,50,14 + CONTROL "Custom1",IDC_TILE_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,191,7,128,128 + GROUPBOX "VRAM Bank",IDC_STATIC,7,7,66,38 + GROUPBOX "Char Base",IDC_STATIC,7,46,65,35 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,147,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,98,156,48,47 + LTEXT "",IDC_R,156,164,50,8 + LTEXT "",IDC_G,156,176,50,8 + LTEXT "",IDC_B,156,188,50,8 + LTEXT "Palette:",IDC_STATIC,7,86,65,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,133,71,10 + LTEXT "Tile:",IDC_STATIC,79,14,41,8 + LTEXT "Address:",IDC_STATIC,79,26,41,8 + LTEXT "",IDC_TILE_NUMBER,135,14,50,8 + LTEXT "",IDC_ADDRESS,135,26,50,8 + CONTROL "Slider1",IDC_PALETTE_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | WS_GROUP | WS_TABSTOP,1,98,76,22 +END + +IDD_GB_MAP_VIEW DIALOG 0, 0, 322, 238 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "GB Map Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "0x8000",IDC_BANK_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,22,39,10 + CONTROL "0x8800",IDC_BANK_1,"Button",BS_AUTORADIOBUTTON,13,36,39,10 + CONTROL "0x9800",IDC_BG0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,63,39,10 + CONTROL "0x9C00",IDC_BG1,"Button",BS_AUTORADIOBUTTON,13,77,40,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,122,68,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,25,217,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,88,217,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,155,217,50,14 + CONTROL "MapView",IDC_MAP_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,187,15,128,128 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,148,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,187,164,48,47 + LTEXT "",IDC_R,245,173,50,8 + LTEXT "",IDC_G,245,185,50,8 + LTEXT "",IDC_B,245,197,50,8 + GROUPBOX "Char Base",IDC_STATIC,7,11,63,37 + GROUPBOX "Map Base",IDC_STATIC,7,52,63,41 + CONTROL "Auto update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,134,55,10 + LTEXT "",IDC_XY,129,18,53,8 + LTEXT "Priority:",IDC_STATIC,80,68,37,8 + LTEXT "",IDC_PRIORITY,130,68,53,8 + LTEXT "Address:",IDC_STATIC,80,28,37,8 + LTEXT "",IDC_ADDRESS,130,28,53,8 + LTEXT "Tile:",IDC_STATIC,80,38,37,8 + LTEXT "",IDC_TILE_NUM,130,38,53,8 + LTEXT "Flip:",IDC_STATIC,80,48,37,8 + LTEXT "",IDC_FLIP,130,48,53,8 + LTEXT "Palette:",IDC_STATIC,80,58,37,8 + LTEXT "",IDC_PALETTE_NUM,130,58,53,8 +END + +IDD_GB_PALETTE_VIEW DIALOG 0, 0, 196, 234 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Palette Viewer" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Save BG...",IDC_SAVE_BG,7,191,50,14 + PUSHBUTTON "Save OBJ...",IDC_SAVE_OBJ,73,191,50,14 + PUSHBUTTON "&Refresh",IDC_REFRESH2,139,191,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,73,213,50,14 + LTEXT "",IDC_ADDRESS,53,117,50,8 + LTEXT "",IDC_R,53,129,50,8 + LTEXT "",IDC_G,53,141,50,8 + LTEXT "",IDC_B,53,153,50,8 + LTEXT "",IDC_VALUE,53,165,50,8 + CONTROL "Custom1",IDC_COLOR,"VbaColorControl",WS_TABSTOP,119,117,50,50 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW,"VbaPaletteViewControl",WS_TABSTOP,11,30,64,64 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW_OBJ, + "VbaPaletteViewControl",WS_TABSTOP,120,30,64,64 + GROUPBOX "BG",IDC_STATIC,6,20,74,81 + GROUPBOX "Sprite",IDC_STATIC,115,20,74,81 + LTEXT "Index:",IDC_STATIC,7,117,38,8 + LTEXT "R:",IDC_STATIC,7,129,41,8 + LTEXT "G:",IDC_STATIC,7,141,43,8 + LTEXT "B:",IDC_STATIC,7,153,38,8 + LTEXT "Value:",IDC_STATIC,7,165,38,8 + LTEXT "Click on a color for more information",IDC_STATIC,7,7,182,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,178,71,10 +END + +IDD_MODE_CONFIRM DIALOG 0, 0, 186, 57 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Confirm mode" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,31,36,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,103,36,50,14 + CTEXT "Do you want to keep the current mode?",IDC_STATIC,7,7,172,8 + CTEXT "",IDC_TIMER,7,19,172,8 +END + +IDD_REWIND_INTERVAL DIALOG 0, 0, 186, 68 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select rewind interval" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_INTERVAL,7,28,172,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,47,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,47,50,14 + LTEXT "Enter rewind interval (0...600) seconds:",IDC_STATIC,7,7,172,8 + LTEXT "Enter 0 to disable rewind.",IDC_STATIC,7,17,172,8 +END + +IDD_IO_VIEWER DIALOG 0, 0, 269, 238 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "I/O Viewer" +FONT 8, "MS Sans Serif" +BEGIN + COMBOBOX IDC_ADDRESSES,7,7,255,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + RTEXT "",IDC_VALUE,103,23,159,8 + CONTROL "",IDC_BIT_15,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,34,255,10 + CONTROL "",IDC_BIT_14,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,44,255,10 + CONTROL "",IDC_BIT_13,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,54,255,8 + CONTROL "",IDC_BIT_12,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,64,255,10 + CONTROL "",IDC_BIT_11,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,74,255,10 + CONTROL "",IDC_BIT_10,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,84,255,10 + CONTROL "",IDC_BIT_9,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,94,255,10 + CONTROL "",IDC_BIT_8,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,104,255,10 + CONTROL "",IDC_BIT_7,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,114,255,10 + CONTROL "",IDC_BIT_6,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,124,255,10 + CONTROL "",IDC_BIT_5,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,134,255,10 + CONTROL "",IDC_BIT_4,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,144,255,10 + CONTROL "",IDC_BIT_3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,154,255,10 + CONTROL "",IDC_BIT_2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,164,255,10 + CONTROL "",IDC_BIT_1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,174,255,10 + CONTROL "",IDC_BIT_0,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,184,255,10 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,201,71,10 + DEFPUSHBUTTON "&Refresh",IDC_REFRESH,54,221,50,14 + DEFPUSHBUTTON "&Apply",IDC_APPLY,110,221,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,166,221,50,14 + LTEXT "Value:",IDC_STATIC,7,23,72,8 +END + +IDD_MAX_SCALE DIALOG 0, 0, 186, 68 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Fullscreen scale" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_VALUE,7,28,172,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,47,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,47,50,14 + LTEXT "Enter the maxium fullscreen scale:",IDC_STATIC,7,7,172,8 + LTEXT "Enter 0 to use maximum scale.",IDC_STATIC,7,17,172,8 +END + +IDD_BUG_REPORT DIALOG 0, 0, 296, 186 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Bug Report" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "&OK",ID_OK,157,165,50,14 + EDITTEXT IDC_BUG_REPORT,11,22,278,131,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | WS_VSCROLL + LTEXT "Bug report data:",IDC_STATIC,7,7,282,8 + DEFPUSHBUTTON "&Copy",IDC_COPY,87,164,50,14 +END + +IDD_UNIVIDMODE DIALOGEX 0, 0, 268, 114 +STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Video Mode" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + DEFPUSHBUTTON "OK",IDOK,138,96,60,12 + PUSHBUTTON "Cancel",IDCANCEL,204,96,60,12 + CTEXT "API",IDC_APINAME,138,18,126,12,0,WS_EX_CLIENTEDGE + LISTBOX IDC_LISTMODES,6,18,126,90,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + CTEXT "Select video mode:",IDC_STATIC,6,6,126,8 + PUSHBUTTON "Device Name",IDC_DISPLAYDEVICE,138,36,126,12 + PUSHBUTTON "Max scale...",IDC_BUTTON_MAXSCALE,138,60,60,12 + CONTROL "Stretch to fit",IDC_CHECK_STRETCHTOFIT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,204,60,60,12 +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO +BEGIN + IDD_OPENDLG, DIALOG + BEGIN + RIGHTMARGIN, 165 + END + + IDD_ABOUT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 165 + TOPMARGIN, 7 + BOTTOMMARGIN, 95 + END + + IDD_DIRECTORIES, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 277 + TOPMARGIN, 7 + BOTTOMMARGIN, 122 + END + + IDD_CONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 128 + TOPMARGIN, 7 + BOTTOMMARGIN, 204 + END + + IDD_CHEATS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 269 + TOPMARGIN, 7 + BOTTOMMARGIN, 246 + END + + IDD_ADD_CHEAT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 130 + END + + IDD_CHEAT_LIST, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 273 + TOPMARGIN, 7 + BOTTOMMARGIN, 243 + END + + IDD_ASSOCIATIONS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 109 + TOPMARGIN, 7 + BOTTOMMARGIN, 88 + END + + IDD_GBA_ROM_INFO, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 201 + TOPMARGIN, 7 + BOTTOMMARGIN, 119 + END + + IDD_GB_ROM_INFO, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 213 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GB_CHEAT_LIST, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 279 + TOPMARGIN, 7 + BOTTOMMARGIN, 214 + END + + IDD_ADD_CHEAT_DLG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 175 + TOPMARGIN, 7 + BOTTOMMARGIN, 100 + END + + IDD_GB_PRINTER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 171 + TOPMARGIN, 7 + BOTTOMMARGIN, 202 + END + + IDD_MOTION_CONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 128 + TOPMARGIN, 7 + BOTTOMMARGIN, 71 + END + + IDD_LANG_SELECT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END + + IDD_CODE_SELECT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 309 + TOPMARGIN, 7 + BOTTOMMARGIN, 228 + END + + IDD_MAP_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 315 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_PALETTE_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 309 + TOPMARGIN, 7 + BOTTOMMARGIN, 259 + END + + IDD_MEM_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 373 + TOPMARGIN, 7 + BOTTOMMARGIN, 171 + END + + IDD_OAM_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 227 + TOPMARGIN, 7 + BOTTOMMARGIN, 178 + END + + IDD_ACCEL_EDITOR, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 273 + TOPMARGIN, 7 + BOTTOMMARGIN, 114 + END + + IDD_TILE_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 319 + TOPMARGIN, 7 + BOTTOMMARGIN, 259 + END + + IDD_GB_COLORS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 162 + TOPMARGIN, 7 + BOTTOMMARGIN, 114 + END + + IDD_DISASSEMBLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 396 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GDB_PORT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 44 + END + + IDD_GDB_WAITING, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 37 + END + + IDD_LOGGING, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 359 + TOPMARGIN, 7 + BOTTOMMARGIN, 211 + END + + IDD_EXPORT_SPS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 241 + TOPMARGIN, 7 + BOTTOMMARGIN, 141 + END + + IDD_ADDR_SIZE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 60 + END + + IDD_MODES, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 201 + TOPMARGIN, 7 + BOTTOMMARGIN, 122 + END + + IDD_DRIVERS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 201 + TOPMARGIN, 7 + BOTTOMMARGIN, 114 + END + + IDD_THROTTLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 56 + END + + IDD_GB_DISASSEMBLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 337 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GB_OAM_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 227 + TOPMARGIN, 7 + BOTTOMMARGIN, 178 + END + + IDD_GB_TILE_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 319 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_GB_MAP_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 315 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_GB_PALETTE_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 189 + TOPMARGIN, 7 + BOTTOMMARGIN, 227 + END + + IDD_MODE_CONFIRM, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 50 + END + + IDD_REWIND_INTERVAL, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END + + IDD_IO_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 262 + TOPMARGIN, 7 + BOTTOMMARGIN, 235 + END + + IDD_MAX_SCALE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END + + IDD_BUG_REPORT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 289 + TOPMARGIN, 7 + BOTTOMMARGIN, 179 + END + + IDD_UNIVIDMODE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 261 + TOPMARGIN, 7 + BOTTOMMARGIN, 107 + END +END +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Menu +// + +IDR_MENU MENU +BEGIN + POPUP "&File" + BEGIN + MENUITEM "&Open GBA...", ID_FILE_OPEN + MENUITEM "Open GB...", ID_FILE_OPENGAMEBOY + MENUITEM SEPARATOR + MENUITEM "&Load...", ID_FILE_LOAD + MENUITEM "&Save...", ID_FILE_SAVE + POPUP "Loa&d Game" + BEGIN + MENUITEM "Most recent", ID_FILE_LOADGAME_MOSTRECENT + MENUITEM "Auto load most recent", ID_FILE_LOADGAME_AUTOLOADMOSTRECENT + MENUITEM SEPARATOR + MENUITEM "Slot #1", ID_FILE_LOADGAME_SLOT1 + MENUITEM "Slot #2", ID_FILE_LOADGAME_SLOT2 + MENUITEM "Slot #3", ID_FILE_LOADGAME_SLOT3 + MENUITEM "Slot #4", ID_FILE_LOADGAME_SLOT4 + MENUITEM "Slot #5", ID_FILE_LOADGAME_SLOT5 + MENUITEM "Slot #6", ID_FILE_LOADGAME_SLOT6 + MENUITEM "Slot #7", ID_FILE_LOADGAME_SLOT7 + MENUITEM "Slot #8", ID_FILE_LOADGAME_SLOT8 + MENUITEM "Slot #9", ID_FILE_LOADGAME_SLOT9 + MENUITEM "Slot #10", ID_FILE_LOADGAME_SLOT10 + END + POPUP "S&ave Game" + BEGIN + MENUITEM "Oldest slot", ID_FILE_SAVEGAME_OLDESTSLOT + MENUITEM SEPARATOR + MENUITEM "Slot #1", ID_FILE_SAVEGAME_SLOT1 + MENUITEM "Slot #2", ID_FILE_SAVEGAME_SLOT2 + MENUITEM "Slot #3", ID_FILE_SAVEGAME_SLOT3 + MENUITEM "Slot #4", ID_FILE_SAVEGAME_SLOT4 + MENUITEM "Slot #5", ID_FILE_SAVEGAME_SLOT5 + MENUITEM "Slot #6", ID_FILE_SAVEGAME_SLOT6 + MENUITEM "Slot #7", ID_FILE_SAVEGAME_SLOT7 + MENUITEM "Slot #8", ID_FILE_SAVEGAME_SLOT8 + MENUITEM "Slot #9", ID_FILE_SAVEGAME_SLOT9 + MENUITEM "Slot #10", ID_FILE_SAVEGAME_SLOT10 + END + MENUITEM SEPARATOR + MENUITEM "&Pause", ID_FILE_PAUSE + MENUITEM "&Reset", ID_FILE_RESET + MENUITEM SEPARATOR + POPUP "Re¢" + BEGIN + MENUITEM "&Reset", ID_FILE_RECENT_RESET + MENUITEM "&Freeze", ID_FILE_RECENT_FREEZE + MENUITEM SEPARATOR + END + MENUITEM SEPARATOR + POPUP "&Import" + BEGIN + MENUITEM "&Battery file...", ID_FILE_IMPORT_BATTERYFILE + MENUITEM "Gameshark &code file...", ID_FILE_IMPORT_GAMESHARKCODEFILE + MENUITEM "&Gameshark Snapshot...", ID_FILE_IMPORT_GAMESHARKSNAPSHOT + END + POPUP "E&xport" + BEGIN + MENUITEM "&Battery file...", ID_FILE_EXPORT_BATTERYFILE + MENUITEM "&Gameshark Snapshot...", ID_FILE_EXPORT_GAMESHARKSNAPSHOT + END + MENUITEM SEPARATOR + MENUITEM "S&creen capture...", ID_FILE_SCREENCAPTURE + MENUITEM "RO&M Header...", ID_FILE_ROMINFORMATION + MENUITEM "&Toggle menu", ID_FILE_TOGGLEMENU + MENUITEM SEPARATOR + MENUITEM "&Close", ID_FILE_CLOSE + MENUITEM SEPARATOR + MENUITEM "&Exit", ID_FILE_EXIT + END + POPUP "&Options" + BEGIN + POPUP "&Video" + BEGIN + POPUP "Render API" + BEGIN + MENUITEM "Windows &GDI", ID_OPTIONS_VIDEO_RENDERMETHOD_GDI + MENUITEM SEPARATOR + MENUITEM "Direct&Draw", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW + MENUITEM " Emulation only", ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY + MENUITEM " Use &Video Memory", ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY + MENUITEM " Triple Buffering", ID_OPTIONS_VIDEO_TRIPLEBUFFERING + MENUITEM SEPARATOR + MENUITEM "Direct&3D", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D + MENUITEM " Filter: Nearest", ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER + MENUITEM " Filter: Bilinear", ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR + MENUITEM SEPARATOR + MENUITEM "&OpenGL", ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL + MENUITEM " Filter: Nearest", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST + MENUITEM " Filter: Bilinear", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR + MENUITEM " Vertex: Triangle", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE + MENUITEM " Vertex: Quads", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS + MENUITEM SEPARATOR + MENUITEM "&VSync", ID_OPTIONS_VIDEO_VSYNC + END + MENUITEM SEPARATOR + POPUP "Windowed" + BEGIN + MENUITEM "&1x Size", ID_OPTIONS_VIDEO_X1 + MENUITEM "&2x Size", ID_OPTIONS_VIDEO_X2 + MENUITEM "&3x Size", ID_OPTIONS_VIDEO_X3 + MENUITEM "&4x Size", ID_OPTIONS_VIDEO_X4 + END + POPUP "Full-screen" + BEGIN + MENUITEM "&Select...", ID_OPTIONS_VIDEO_FULLSCREEN + MENUITEM SEPARATOR + MENUITEM "320x240x16", ID_OPTIONS_VIDEO_FULLSCREEN320X240 + MENUITEM "640x480x16", ID_OPTIONS_VIDEO_FULLSCREEN640X480 + MENUITEM "800x600x16", ID_OPTIONS_VIDEO_FULLSCREEN800X600 + MENUITEM "1024x768x32", ID_OPTIONS_VIDEO_FULLSCREEN1024X768 + MENUITEM "1280x1024x32", ID_OPTIONS_VIDEO_FULLSCREEN1280X1024 + MENUITEM SEPARATOR + MENUITEM "&Max Scale...", ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE + MENUITEM "Stretch to &fit", ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT + END + MENUITEM SEPARATOR + POPUP "&Frame skip" + BEGIN + MENUITEM "&Synchronize", ID_OPTIONS_EMULATOR_SYNCHRONIZE + MENUITEM "&Automatic", ID_OPTIONS_FRAMESKIP_AUTOMATIC + MENUITEM SEPARATOR + MENUITEM "&No frame skip", ID_OPTIONS_VIDEO_FRAMESKIP_0 + MENUITEM "&1 frame", ID_OPTIONS_VIDEO_FRAMESKIP_1 + MENUITEM "&2 frames", ID_OPTIONS_VIDEO_FRAMESKIP_2 + MENUITEM "&3 frames", ID_OPTIONS_VIDEO_FRAMESKIP_3 + MENUITEM "&4 frames", ID_OPTIONS_VIDEO_FRAMESKIP_4 + MENUITEM "&5 frames", ID_OPTIONS_VIDEO_FRAMESKIP_5 + MENUITEM "&6 frames", ID_OPTIONS_VIDEO_FRAMESKIP_6 + MENUITEM "&7 frames", ID_OPTIONS_VIDEO_FRAMESKIP_7 + MENUITEM "&8 frames", ID_OPTIONS_VIDEO_FRAMESKIP_8 + MENUITEM "&9 frames", ID_OPTIONS_VIDEO_FRAMESKIP_9 + MENUITEM SEPARATOR + MENUITEM "Turbo mode", ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE + END + POPUP "Throttle" + BEGIN + MENUITEM "No throttle", ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE + MENUITEM "25%", ID_OPTIONS_FRAMESKIP_THROTTLE_25 + MENUITEM "50%", ID_OPTIONS_FRAMESKIP_THROTTLE_50 + MENUITEM "100%", ID_OPTIONS_FRAMESKIP_THROTTLE_100 + MENUITEM "150%", ID_OPTIONS_FRAMESKIP_THROTTLE_150 + MENUITEM "200%", ID_OPTIONS_FRAMESKIP_THROTTLE_200 + MENUITEM "&Other...", ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER + END + MENUITEM SEPARATOR + MENUITEM "D&isable status messages", ID_OPTIONS_EMULATOR_DISABLESTATUSMESSAGES + END + POPUP "&Pixel Filter" + BEGIN + POPUP "Magnification" + BEGIN + MENUITEM "&None", ID_OPTIONS_FILTER_NORMAL + POPUP "&2X" + BEGIN + MENUITEM "&Simple 2x", ID_OPTIONS_FILTER16BIT_SIMPLE2X + MENUITEM SEPARATOR + MENUITEM "&Pixelate", ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL + MENUITEM "&TV Mode", ID_OPTIONS_FILTER_TVMODE + MENUITEM "Scan&lines", ID_OPTIONS_FILTER_SCANLINES + MENUITEM SEPARATOR + MENUITEM "&Bilinear", ID_OPTIONS_FILTER_BILINEAR + MENUITEM "B&ilinear Plus", ID_OPTIONS_FILTER_BILINEARPLUS + MENUITEM SEPARATOR + MENUITEM "AdvanceMAME Scale2x", ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X + MENUITEM "&2xSaI", ID_OPTIONS_FILTER_2XSAI + MENUITEM "S&uper 2xSaI", ID_OPTIONS_FILTER_SUPER2XSAI + MENUITEM "Super &Eagle", ID_OPTIONS_FILTER_SUPEREAGLE + MENUITEM "&LQ2x", ID_OPTIONS_FILTER_LQ2X + MENUITEM "&HQ2x", ID_OPTIONS_FILTER_HQ2X + END + POPUP "&3X" + BEGIN + MENUITEM "&Simple 3x", ID_OPTIONS_FILTER_SIMPLE3X + MENUITEM SEPARATOR + MENUITEM "&HQ3x", ID_OPTIONS_FILTER_HQ3X + END + POPUP "&4X" + BEGIN + MENUITEM "&Simple 4x", ID_OPTIONS_FILTER_SIMPLE4X + MENUITEM SEPARATOR + MENUITEM "&HQ4x", ID_OPTIONS_FILTER_HQ4X, GRAYED + END + END + POPUP "&Interframe Blending" + BEGIN + MENUITEM "&None", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE + MENUITEM "&Motion Blur", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR + MENUITEM "&Smart", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART + END + MENUITEM SEPARATOR + MENUITEM "&Disable MMX", ID_OPTIONS_FILTER_DISABLEMMX + END + POPUP "&Audio" + BEGIN + POPUP "&Volume" + BEGIN + MENUITEM "&0.25X", ID_OPTIONS_SOUND_VOLUME_25X + MENUITEM "0.&5X", ID_OPTIONS_SOUND_VOLUME_5X + MENUITEM "&1x", ID_OPTIONS_SOUND_VOLUME_1X + MENUITEM "&2x", ID_OPTIONS_SOUND_VOLUME_2X + MENUITEM "&3x", ID_OPTIONS_SOUND_VOLUME_3X + MENUITEM "&4x", ID_OPTIONS_SOUND_VOLUME_4X + END + MENUITEM SEPARATOR + POPUP "PCM interpolation" + BEGIN + MENUITEM "None", ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE + MENUITEM "Linear", ID_OPTIONS_SOUND_PCMINTERPOLATION_LINEAR + MENUITEM "Cubic", ID_OPTIONS_SOUND_PCMINTERPOLATION_CUBIC + MENUITEM "FIR (Kaiser 4T)", ID_OPTIONS_SOUND_PCMINTERPOLATION_FIR + MENUITEM "libresample", ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE + END + MENUITEM SEPARATOR + MENUITEM "O&ff", ID_OPTIONS_SOUND_OFF + MENUITEM "&Mute", ID_OPTIONS_SOUND_MUTE + MENUITEM "&On", ID_OPTIONS_SOUND_ON + MENUITEM SEPARATOR + MENUITEM "&Echo", ID_OPTIONS_SOUND_ECHO + MENUITEM "&Low pass filter", ID_OPTIONS_SOUND_LOWPASSFILTER + MENUITEM "&Reverse Stereo", ID_OPTIONS_SOUND_REVERSESTEREO + MENUITEM SEPARATOR + MENUITEM "11 &Khz", ID_OPTIONS_SOUND_11KHZ + MENUITEM "22 K&hz", ID_OPTIONS_SOUND_22KHZ + MENUITEM "44 Kh&z", ID_OPTIONS_SOUND_44KHZ + MENUITEM SEPARATOR + MENUITEM "Channel &1", ID_OPTIONS_SOUND_CHANNEL1, CHECKED + MENUITEM "Channel &2", ID_OPTIONS_SOUND_CHANNEL2, CHECKED + MENUITEM "Channel &3", ID_OPTIONS_SOUND_CHANNEL3, CHECKED + MENUITEM "Channel &4", ID_OPTIONS_SOUND_CHANNEL4, CHECKED + MENUITEM "Direct Sound &A", ID_OPTIONS_SOUND_DIRECTSOUNDA, CHECKED + MENUITEM "Direct Sound &B", ID_OPTIONS_SOUND_DIRECTSOUNDB, CHECKED + MENUITEM SEPARATOR + MENUITEM "Use old &synchronization", ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION + END + POPUP "&Input" + BEGIN + POPUP "&Set" + BEGIN + MENUITEM "Config &1...", ID_OPTIONS_JOYPAD_CONFIGURE_1 + MENUITEM "Config &2...", ID_OPTIONS_JOYPAD_CONFIGURE_2 + MENUITEM "Config &3...", ID_OPTIONS_JOYPAD_CONFIGURE_3 + MENUITEM "Config &4...", ID_OPTIONS_JOYPAD_CONFIGURE_4 + MENUITEM SEPARATOR + MENUITEM "&Motion...", ID_OPTIONS_JOYPAD_MOTIONCONFIGURE + END + POPUP "&Use" + BEGIN + MENUITEM "Config &1", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1 + MENUITEM "Config &2", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_2 + MENUITEM "Config &3", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_3 + MENUITEM "Config &4", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4 + END + MENUITEM SEPARATOR + POPUP "&Autofire" + BEGIN + MENUITEM "&A", ID_OPTIONS_JOYPAD_AUTOFIRE_A + MENUITEM "&B", ID_OPTIONS_JOYPAD_AUTOFIRE_B + MENUITEM "&L", ID_OPTIONS_JOYPAD_AUTOFIRE_L + MENUITEM "&R", ID_OPTIONS_JOYPAD_AUTOFIRE_R + END + MENUITEM "Rewind interval...", ID_OPTIONS_EMULATOR_REWINDINTERVAL + END + MENUITEM SEPARATOR + POPUP "&Emulator" + BEGIN + MENUITEM "&Associate...", ID_OPTIONS_EMULATOR_ASSOCIATE + MENUITEM "&Directories...", ID_OPTIONS_EMULATOR_DIRECTORIES + POPUP "&Priority" + BEGIN + MENUITEM "&Highest", ID_OPTIONS_PRIORITY_HIGHEST + MENUITEM "&Above Normal", ID_OPTIONS_PRIORITY_ABOVENORMAL + MENUITEM "&Normal", ID_OPTIONS_PRIORITY_NORMAL + MENUITEM "&Below Normal", ID_OPTIONS_PRIORITY_BELOWNORMAL + END + MENUITEM "Remove intros (GBA)", ID_OPTIONS_EMULATOR_REMOVEINTROSGBA + MENUITEM "Automatic IPS patching", ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH + MENUITEM "Pause when inactive", ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE + MENUITEM "AGB Print", ID_OPTIONS_EMULATOR_AGBPRINT + MENUITEM "Real Time Clock", ID_OPTIONS_EMULATOR_REALTIMECLOCK + MENUITEM "Auto hide menu", ID_OPTIONS_EMULATOR_AUTOHIDEMENU + POPUP "Show speed" + BEGIN + MENUITEM "None", ID_OPTIONS_EMULATOR_SHOWSPEED_NONE + MENUITEM "Percentage", ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE + MENUITEM "Detailed", ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED + MENUITEM SEPARATOR + MENUITEM "Transparent", ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT + END + POPUP "Save Type" + BEGIN + MENUITEM "&Automatic", ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC + MENUITEM "EEPROM", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM + MENUITEM "SRAM", ID_OPTIONS_EMULATOR_SAVETYPE_SRAM + MENUITEM "Flash", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH + MENUITEM "EEPROM+Sensor", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR + MENUITEM "None", ID_OPTIONS_EMULATOR_SAVETYPE_NONE + MENUITEM SEPARATOR + MENUITEM "Flash 64K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K + MENUITEM "Flash 128K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M + MENUITEM SEPARATOR + MENUITEM "Enhanced detection", ID_OPTIONS_EMULATOR_SAVETYPE_ENHANCEDDETECTION + END + MENUITEM SEPARATOR + MENUITEM "&Use BIOS file", ID_OPTIONS_EMULATOR_USEBIOSFILE + MENUITEM "Skip BIOS", ID_OPTIONS_EMULATOR_SKIPBIOS + MENUITEM "S&elect BIOS file...", ID_OPTIONS_EMULATOR_SELECTBIOSFILE + MENUITEM SEPARATOR + MENUITEM "PNG Screenshots", ID_OPTIONS_EMULATOR_PNGFORMAT + MENUITEM "BMP Screenshots", ID_OPTIONS_EMULATOR_BMPFORMAT + END + POPUP "&Gameboy" + BEGIN + MENUITEM "B&order", ID_OPTIONS_GAMEBOY_BORDER + MENUITEM "&Printer", ID_OPTIONS_GAMEBOY_PRINTER + MENUITEM "Border Au&tomatic", ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC + MENUITEM SEPARATOR + MENUITEM "&Automatic", ID_OPTIONS_GAMEBOY_AUTOMATIC + MENUITEM "&GBA", ID_OPTIONS_GAMEBOY_GBA + MENUITEM "GB&C", ID_OPTIONS_GAMEBOY_CGB + MENUITEM "&SGB", ID_OPTIONS_GAMEBOY_SGB + MENUITEM "SGB&2", ID_OPTIONS_GAMEBOY_SGB2 + MENUITEM "G&B", ID_OPTIONS_GAMEBOY_GB + MENUITEM SEPARATOR + MENUITEM "&Real Colors", ID_OPTIONS_GAMEBOY_REALCOLORS + MENUITEM "G&ameboy Colors", ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS + MENUITEM "LCD colors", ID_OPTIONS_FILTER_LCDCOLORS + MENUITEM SEPARATOR + MENUITEM "&Colors...", ID_OPTIONS_GAMEBOY_COLORS + END + POPUP "&Language" + BEGIN + MENUITEM "&System", ID_OPTIONS_LANGUAGE_SYSTEM + MENUITEM "&English", ID_OPTIONS_LANGUAGE_ENGLISH + MENUITEM "&Other...", ID_OPTIONS_LANGUAGE_OTHER + END + POPUP "Li&nk" + BEGIN + MENUITEM "&Wireless Adapter", ID_OPTIONS_LINK_WIRELESSADAPTER + + MENUITEM "&Log", ID_OPTIONS_LINK_LOG + MENUITEM "&Options...", ID_OPTIONS_LINK_OPTIONS + END + END + POPUP "&Cheats" + BEGIN + MENUITEM "&Search for cheats...", ID_CHEATS_SEARCHFORCHEATS + MENUITEM "&Cheat list...", ID_CHEATS_CHEATLIST + MENUITEM SEPARATOR + MENUITEM "&Load cheat list...", ID_CHEATS_LOADCHEATLIST + MENUITEM "Sa&ve cheat list...", ID_CHEATS_SAVECHEATLIST + MENUITEM SEPARATOR + MENUITEM "Disable cheats", ID_CHEATS_DISABLECHEATS + MENUITEM "&Automatic save/load cheats", ID_CHEATS_AUTOMATICSAVELOADCHEATS + END + POPUP "&Tools" + BEGIN + MENUITEM "Disassemble...", ID_TOOLS_DISASSEMBLE + MENUITEM "Logging...", ID_TOOLS_LOGGING + MENUITEM "&IO Viewer...", ID_TOOLS_IOVIEWER + MENUITEM "&Map Viewer...", ID_TOOLS_MAPVIEW + MENUITEM "&Memory viewer...", ID_TOOLS_MEMORYVIEWER + MENUITEM "OAM Viewer...", ID_TOOLS_OAMVIEWER + MENUITEM "&Palette Viewer...", ID_TOOLS_PALETTEVIEW + MENUITEM "Tile Viewer...", ID_TOOLS_TILEVIEWER + POPUP "Show" + BEGIN + MENUITEM "BG 0", ID_OPTIONS_VIDEO_LAYERS_BG0 + MENUITEM "BG 1", ID_OPTIONS_VIDEO_LAYERS_BG1 + MENUITEM "BG 2", ID_OPTIONS_VIDEO_LAYERS_BG2 + MENUITEM "BG 3", ID_OPTIONS_VIDEO_LAYERS_BG3 + MENUITEM "OBJ", ID_OPTIONS_VIDEO_LAYERS_OBJ + MENUITEM "WIN 0", ID_OPTIONS_VIDEO_LAYERS_WIN0 + MENUITEM "WIN 1", ID_OPTIONS_VIDEO_LAYERS_WIN1 + MENUITEM "OBJ WIN", ID_OPTIONS_VIDEO_LAYERS_OBJWIN + MENUITEM SEPARATOR + MENUITEM "SFX", ID_OPTIONS_VIDEO_DISABLESFX + END + MENUITEM SEPARATOR + MENUITEM "&Next frame", ID_DEBUG_NEXTFRAME + POPUP "GDB" + BEGIN + MENUITEM "Wait for connection...", ID_TOOLS_DEBUG_GDB + MENUITEM "Load and wait...", ID_TOOLS_DEBUG_LOADANDWAIT + MENUITEM "Break into GDB", ID_TOOLS_DEBUG_BREAK + MENUITEM "Disconnect", ID_TOOLS_DEBUG_DISCONNECT + END + MENUITEM SEPARATOR + POPUP "Record" + BEGIN + MENUITEM "Start sound recording...", ID_OPTIONS_SOUND_STARTRECORDING + MENUITEM "Stop sound recording", ID_OPTIONS_SOUND_STOPRECORDING + MENUITEM "Start AVI recording...", ID_TOOLS_RECORD_STARTAVIRECORDING + MENUITEM "Stop AVI recording", ID_TOOLS_RECORD_STOPAVIRECORDING + MENUITEM "Start movie recording...", ID_TOOLS_RECORD_STARTMOVIERECORDING + MENUITEM "Stop movie recording", ID_TOOLS_RECORD_STOPMOVIERECORDING + END + POPUP "Play" + BEGIN + MENUITEM "Start playing movie...", ID_TOOLS_PLAY_STARTMOVIEPLAYING + MENUITEM "Stop playing movie", ID_TOOLS_PLAY_STOPMOVIEPLAYING + END + MENUITEM SEPARATOR + MENUITEM "Rewind", ID_TOOLS_REWIND + MENUITEM "Customize...", ID_TOOLS_CUSTOMIZE + END + POPUP "&Help" + BEGIN + MENUITEM "Bug Report", ID_HELP_BUGREPORT + MENUITEM "FAQ (website)", ID_HELP_FAQ + MENUITEM SEPARATOR + MENUITEM "&About...", ID_HELP_ABOUT + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Accelerator +// + +IDR_ACCELERATOR ACCELERATORS +BEGIN + "1", ID_OPTIONS_VIDEO_LAYERS_BG0, VIRTKEY, CONTROL, NOINVERT + "1", ID_OPTIONS_JOYPAD_AUTOFIRE_A, VIRTKEY, ALT, NOINVERT + "2", ID_OPTIONS_VIDEO_LAYERS_BG1, VIRTKEY, CONTROL, NOINVERT + "2", ID_OPTIONS_JOYPAD_AUTOFIRE_B, VIRTKEY, ALT, NOINVERT + "3", ID_OPTIONS_VIDEO_LAYERS_BG2, VIRTKEY, CONTROL, NOINVERT + "3", ID_OPTIONS_JOYPAD_AUTOFIRE_L, VIRTKEY, ALT, NOINVERT + "4", ID_OPTIONS_VIDEO_LAYERS_BG3, VIRTKEY, CONTROL, NOINVERT + "4", ID_OPTIONS_JOYPAD_AUTOFIRE_R, VIRTKEY, ALT, NOINVERT + "5", ID_OPTIONS_VIDEO_LAYERS_OBJ, VIRTKEY, CONTROL, NOINVERT + "6", ID_OPTIONS_VIDEO_LAYERS_WIN0, VIRTKEY, CONTROL, NOINVERT + "7", ID_OPTIONS_VIDEO_LAYERS_WIN1, VIRTKEY, CONTROL, NOINVERT + "8", ID_OPTIONS_VIDEO_LAYERS_OBJWIN, VIRTKEY, CONTROL, NOINVERT + "B", ID_TOOLS_REWIND, VIRTKEY, CONTROL, NOINVERT + "C", ID_CHEATS_SEARCHFORCHEATS, VIRTKEY, CONTROL, NOINVERT + "L", ID_FILE_LOAD, VIRTKEY, CONTROL, NOINVERT + "N", ID_DEBUG_NEXTFRAME, VIRTKEY, CONTROL, NOINVERT + "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT + "P", ID_FILE_PAUSE, VIRTKEY, CONTROL, NOINVERT + "R", ID_FILE_RESET, VIRTKEY, CONTROL, NOINVERT + "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT + VK_ESCAPE, ID_FILE_TOGGLEMENU, VIRTKEY, NOINVERT + VK_F1, ID_FILE_LOADGAME_SLOT1, VIRTKEY, NOINVERT + VK_F1, ID_FILE_MRU_FILE1, VIRTKEY, CONTROL, NOINVERT + VK_F1, ID_FILE_SAVEGAME_SLOT1, VIRTKEY, SHIFT, NOINVERT + VK_F10, ID_FILE_LOADGAME_SLOT10, VIRTKEY, NOINVERT + VK_F10, ID_FILE_MRU_FILE10, VIRTKEY, CONTROL, NOINVERT + VK_F10, ID_FILE_SAVEGAME_SLOT10, VIRTKEY, SHIFT, NOINVERT + VK_F2, ID_FILE_LOADGAME_SLOT2, VIRTKEY, NOINVERT + VK_F2, ID_FILE_MRU_FILE2, VIRTKEY, CONTROL, NOINVERT + VK_F2, ID_FILE_SAVEGAME_SLOT2, VIRTKEY, SHIFT, NOINVERT + VK_F3, ID_FILE_LOADGAME_SLOT3, VIRTKEY, NOINVERT + VK_F3, ID_FILE_MRU_FILE3, VIRTKEY, CONTROL, NOINVERT + VK_F3, ID_FILE_SAVEGAME_SLOT3, VIRTKEY, SHIFT, NOINVERT + VK_F4, ID_FILE_LOADGAME_SLOT4, VIRTKEY, NOINVERT + VK_F4, ID_FILE_MRU_FILE4, VIRTKEY, CONTROL, NOINVERT + VK_F4, ID_FILE_SAVEGAME_SLOT4, VIRTKEY, SHIFT, NOINVERT + VK_F5, ID_FILE_LOADGAME_SLOT5, VIRTKEY, NOINVERT + VK_F5, ID_FILE_MRU_FILE5, VIRTKEY, CONTROL, NOINVERT + VK_F5, ID_FILE_SAVEGAME_SLOT5, VIRTKEY, SHIFT, NOINVERT + VK_F6, ID_FILE_LOADGAME_SLOT6, VIRTKEY, NOINVERT + VK_F6, ID_FILE_MRU_FILE6, VIRTKEY, CONTROL, NOINVERT + VK_F6, ID_FILE_SAVEGAME_SLOT6, VIRTKEY, SHIFT, NOINVERT + VK_F7, ID_FILE_LOADGAME_SLOT7, VIRTKEY, NOINVERT + VK_F7, ID_FILE_MRU_FILE7, VIRTKEY, CONTROL, NOINVERT + VK_F7, ID_FILE_SAVEGAME_SLOT7, VIRTKEY, SHIFT, NOINVERT + VK_F8, ID_FILE_LOADGAME_SLOT8, VIRTKEY, NOINVERT + VK_F8, ID_FILE_MRU_FILE8, VIRTKEY, CONTROL, NOINVERT + VK_F8, ID_FILE_SAVEGAME_SLOT8, VIRTKEY, SHIFT, NOINVERT + VK_F9, ID_FILE_LOADGAME_SLOT9, VIRTKEY, NOINVERT + VK_F9, ID_FILE_MRU_FILE9, VIRTKEY, CONTROL, NOINVERT + VK_F9, ID_FILE_SAVEGAME_SLOT9, VIRTKEY, SHIFT, NOINVERT + "X", ID_FILE_EXIT, VIRTKEY, CONTROL, NOINVERT +END + + +///////////////////////////////////////////////////////////////////////////// +// +// RT_MANIFEST +// + +1 RT_MANIFEST "VBA.manifest" + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,7,4,0 + PRODUCTVERSION 1,7,4,0 + FILEFLAGSMASK 0x37L +#ifdef _DEBUG + FILEFLAGS 0x21L +#else + FILEFLAGS 0x20L +#endif + FILEOS 0x4L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", "VisualBoyAdvance comes with NO WARRANTY. Use it at your own risk." + VALUE "CompanyName", "Forgotten and the VBA team" + VALUE "FileDescription", "VBA" + VALUE "FileVersion", "1, 7, 4, 0" + VALUE "InternalName", "VisualBoyAdvance" + VALUE "LegalCopyright", "Copyright © 2005 Forgotten and the VBA team" + VALUE "OriginalFilename", "VisualBoyAdvance.exe" + VALUE "ProductName", "VisualBoyAdvance emulator" + VALUE "ProductVersion", "S1.7.4" + VALUE "SpecialBuild", "S - Changes by Spacy" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// String Table +// + +STRINGTABLE +BEGIN + IDS_UNSUPPORTED_VBA_SGM "Unsupported VisualBoyAdvance save game version %d" + IDS_CANNOT_LOAD_SGM "Cannot load save game for %s" + IDS_SAVE_GAME_NOT_USING_BIOS "Save game is not using the BIOS file" + IDS_SAVE_GAME_USING_BIOS "Save game is using the BIOS file" + IDS_UNSUPPORTED_SAVE_TYPE "Unsupported save type %d" + IDS_CANNOT_OPEN_FILE "Cannot open file %s" + IDS_BAD_ZIP_FILE "Bad ZIP file %s" + IDS_NO_IMAGE_ON_ZIP "No image found on ZIP file %s" + IDS_ERROR_OPENING_IMAGE "Error opening image %s" + IDS_ERROR_READING_IMAGE "Error reading image %s" + IDS_UNSUPPORTED_BIOS_FUNCTION + "Unsupported BIOS function %02x called from %08x. A BIOS file is needed in order to get correct behaviour." + IDS_INVALID_BIOS_FILE_SIZE "Invalid BIOS file size" + IDS_INVALID_CHEAT_CODE "Invalid cheat code '%s'. Supported formats are:\nXXXXXXXX:YY, XXXXXXXX:YYYY, XXXXXXXX:YYYYYYYY." + IDS_UNKNOWN_ARM_OPCDOE "Unimplemented opcode %08x from %08x" + IDS_UNKNOWN_THUMB_OPCODE "Unknown opcode %04x from %08x" +END + +STRINGTABLE +BEGIN + IDS_ERROR_CREATING_FILE "Error creating file %s" + IDS_FAILED_TO_READ_SGM "Failed to read complete save game %s (%d)" + IDS_FAILED_TO_READ_RTC "Failed to read RTC from save game %s (continuing)" + IDS_UNSUPPORTED_VB_SGM "Unsupported VisualBoy save game version %d" + IDS_CANNOT_LOAD_SGM_FOR "Cannot load save game for %s. Playing %s" + IDS_ERROR_OPENING_IMAGE_FROM "Error opening image %s from zip file %s" + IDS_ERROR_READING_IMAGE_FROM "Error reading image %s from zip file %s" + IDS_UNSUPPORTED_ROM_SIZE "Unsupported rom size %02x" + IDS_UNSUPPORTED_RAM_SIZE "Unsupported ram size %02x" + IDS_UNKNOWN_CARTRIDGE_TYPE "Unknown cartridge type %02x" + IDS_MAXIMUM_NUMBER_OF_CHEATS "Maximum number of cheats reached." + IDS_INVALID_GAMESHARK_CODE "Invalid GameShark code: %s" + IDS_INVALID_GAMEGENIE_CODE "Invalid GameGenie code: %s" + IDS_INVALID_CHEAT_TO_REMOVE "Invalid cheat to remove %d" + IDS_INVALID_CHEAT_CODE_ADDRESS "Invalid cheat code address: %08x" + IDS_UNSUPPORTED_CHEAT_LIST_VERSION "Unsupported cheat list version %d" +END + +STRINGTABLE +BEGIN + IDS_DIRECTX_7_REQUIRED "DirectX 7.0 or greater is required to run.\nDownload at http://www.microsoft.com/directx.\n\nError found at: %s" + IDS_DISABLING_VIDEO_MEMORY "Disabling Use Video Memory setting" + IDS_SETTING_WILL_BE_EFFECTIVE + "Setting will be effective the next time you start the emulator" + IDS_DISABLING_EMULATION_ONLY "Disabling Emulation Only setting" + IDS_FAILED_TO_OPEN_FILE "Failed to open file %s" + IDS_FAILED_TO_READ_ZIP_DIR "Failed to read zip directory for file %s" + IDS_UNSUPPORTED_FILE_TYPE "Unsupported file type: %s" + IDS_CANNOT_CREATE_DIRECTSOUND "Cannot create DirectSound %08x" + IDS_CANNOT_SETCOOPERATIVELEVEL "Cannot SetCooperativeLevel %08x" + IDS_CANNOT_CREATESOUNDBUFFER "Cannot CreateSoundBuffer %08x" + IDS_CANNOT_SETFORMAT_PRIMARY "Cannot SetFormat for primary %08x" + IDS_CANNOT_CREATESOUNDBUFFER_SEC "Cannot CreateSoundBuffer secondary %08x" + IDS_CANNOT_PLAY_PRIMARY "Cannot Play primary %08x" + IDS_SEARCH_PRODUCED_TOO_MANY + "Search produced %d results. Please refine better" + IDS_NUMBER_CANNOT_BE_EMPTY "Number cannot be empty" + IDS_INVALID_ADDRESS "Invalid address: %08x" +END + +STRINGTABLE +BEGIN + IDS_MISALIGNED_HALFWORD "Misaligned half-word address: %08x" + IDS_MISALIGNED_WORD "Misaligned word address: %08x" + IDS_VALUE_CANNOT_BE_EMPTY "Value cannot be empty" + IDS_ERROR_ON_STARTDOC "Error on StartDoc" + IDS_ERROR_ON_STARTPAGE "Error on StartPage" + IDS_ERROR_PRINTING_ON_STRETCH "Error printing on StretchDIBits" + IDS_ERROR_ON_ENDPAGE "Error on EndPage" + IDS_ERROR_ON_ENDDOC "Error on EndDoc" + IDS_ERROR "Error" + IDS_JOY_LEFT "Joy %d Left" + IDS_JOY_RIGHT "Joy %d Right" + IDS_JOY_UP "Joy %d Up" + IDS_JOY_DOWN "Joy %d Down" + IDS_JOY_BUTTON "Joy %d %s" + IDS_SELECT_ROM_DIR "Select ROM directory:" + IDS_SELECT_BATTERY_DIR "Select Battery directory:" +END + +STRINGTABLE +BEGIN + IDS_SELECT_SAVE_DIR "Select Save Directory:" + IDS_SELECT_CAPTURE_DIR "Select Capture directory:" + IDS_SELECT_BIOS_FILE "Select BIOS file" + IDS_RESET "Reset" + IDS_AUTOFIRE_A_DISABLED "autofire A disabled" + IDS_AUTOFIRE_A "autofire A" + IDS_AUTOFIRE_B_DISABLED "autofire B disabled" + IDS_AUTOFIRE_B "autofire B" + IDS_AUTOFIRE_L_DISABLED "autofire L disabled" + IDS_AUTOFIRE_L "autofire L" + IDS_AUTOFIRE_R_DISABLED "autofire R disabled" + IDS_AUTOFIRE_R "autofire R" + IDS_SELECT_ROM "Select ROM" + IDS_SELECT_SAVE_GAME_NAME "Select save game name" + IDS_LOADED_STATE "Loaded state" + IDS_LOADED_STATE_N "Loaded state %d" +END + +STRINGTABLE +BEGIN + IDS_WROTE_STATE "Wrote state" + IDS_WROTE_STATE_N "Wrote state %d" + IDS_LOADED_BATTERY "Loaded battery" + IDS_SELECT_CAPTURE_NAME "Select screen capture name" + IDS_SCREEN_CAPTURE "Screen capture" + IDS_ADDRESS "Address" + IDS_OLD_VALUE "Old Value" + IDS_NEW_VALUE "New Value" + IDS_ADD_CHEAT_CODE "Add cheat code" + IDS_CODE "Code" + IDS_DESCRIPTION "Description" + IDS_STATUS "Status" + IDS_ADD_GG_CODE "Add GameGenie code" + IDS_ADD_GS_CODE "Add GameShark code" + IDS_POCKET_PRINTER "Pocket Printer" + IDS_UNKNOWN "Unknown" +END + +STRINGTABLE +BEGIN + IDS_NONE "None" + IDS_FAILED_TO_LOAD_LIBRARY "Failed to load library %s" + IDS_FAILED_TO_GET_LOCINFO "Failed to get locale information" + IDS_SELECT_CHEAT_LIST_NAME "Select cheat list name" + IDS_FILTER_BIOS "Gameboy Advance_*.BIN;*.AGB;*.GBA;*.BIOS;*.ZIP;*.Z;*.GZ__" + IDS_FILTER_ROM "All Gameboy Advance_*.BIN;*.AGB;*.GBA;*.MB;*.ELF;*.GB;*.SGB;*.CGB;*.GBC;*.ZIP;*.Z;*.GZ_Gameboy Advance_*.BIN;*.AGB;*.GBA_Gameboy_*.GB;*.SGB;*.CGB;*.GBC__" + IDS_FILTER_SGM "VisualBoyAdvance Save Game_*.SGM__" + IDS_FILTER_CHEAT_LIST "VisualBoyAdvance Cheat List_*.CLT__" + IDS_FILTER_PNG "PNG Image_*.PNG_BMP Image_*.BMP__" + IDS_LOADED_CHEATS "Loaded cheats" + IDS_ERROR_DISP_COLOR "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode." + IDS_ADD_GSA_CODE "Add GamesharkAdvance code" + IDS_FILTER_SPS "Gameshark Snapshot_*.SPS__" + IDS_SELECT_SNAPSHOT_FILE "Select snapshot file" + IDS_FILTER_SAV "Battery file_*.SAV_Flash save_*.DAT__" + IDS_SELECT_BATTERY_FILE "Select battery file" +END + +STRINGTABLE +BEGIN + IDS_UNSUPPORTED_CHEAT_LIST_TYPE "Unsupported cheat list type %d" + IDS_INVALID_GSA_CODE "Invalid GSA code. Format is XXXXXXXXYYYYYYYY." + IDS_CANNOT_IMPORT_SNAPSHOT_FOR + "Cannot import snapshot for %s. Current game is %s" + IDS_UNSUPPORTED_SNAPSHOT_FILE "Unsupported snapshot file %s" + IDS_UNSUPPORTED_ARM_MODE "Unsupported ARM mode %02x" + IDS_UNSUPPORTED_CODE_FILE "Unsupported code file %s" + IDS_GSA_CODE_WARNING "Warning: cheats are for game %s. Current game is %s.\nCodes may not work correctly." + IDS_INVALID_CBA_CODE "Invalid CBA code. Format is XXXXXXXX YYYY." + IDS_CBA_CODE_WARNING "Warning: Codes seem to be for a different game.\nCodes may not work correctly." + IDS_OUT_OF_MEMORY "Failed to allocate memory for %s" +END + +STRINGTABLE +BEGIN + IDS_FILTER_GBS "Gameboy Snapshot_*.GBS__" + IDS_FILTER_GCF "Gameshark Code File_*.GCF__" + IDS_SELECT_CODE_FILE "Select code file" + IDS_SAVE_WILL_BE_LOST "Importing a snapshot file will erase any saved games and reset the emulator. Do you want to continue?" + IDS_CONFIRM_ACTION "Please confirm action" + IDS_CODES_WILL_BE_LOST "Importing a code file will erase any entered codes. Do you want to continue?" + IDS_FILTER_SPC "Gameshark Code File_*.SPC;*.XPC__" + IDS_ADD_CBA_CODE "Add CodeBreakerAdvance code" + IDS_FILTER_WAV "Wave file_*.WAV__" + IDS_SELECT_WAV_NAME "Select wave file name" + IDS_FILTER_GBROM "All Gameboy_*.GB;*.SGB;*.CGB;*.GBC;*.ZIP;*.Z;*.GZ_Gameboy_*.GB_Super Gameboy_*.SGB_Color Gameboy_*.CGB;*.GBC__" + IDS_FILTER_PAL "Windows Palette (*.PAL)_*.PAL_PaintShop Palette (*.PAL)_*.PAL_Adobe Color Table (*.ACT)_*.ACT__" + IDS_SELECT_PALETTE_NAME "Select palette name:" + IDS_SEARCH_PRODUCED_NO_RESULTS "Search produced no results." + IDS_ERROR_BINDING "Error binding socket. Port probably in use." + IDS_ERROR_LISTENING "Error listening on socket." +END + +STRINGTABLE +BEGIN + IDS_ERROR_CREATING_SOCKET "Error creating socket." + IDS_ACK_NOT_RECEIVED "ACK not received from GDB." + IDS_ERROR_NOT_GBA_IMAGE "Error: not a GBA image." + IDS_EEPROM_NOT_SUPPORTED "EEPROM saves cannot be exported." + IDS_FILTER_DUMP "Memory Dump_*.DMP__" + IDS_SELECT_DUMP_FILE "Select dump file name" + IDS_FILTER_AVI "AVI File_*.AVI__" + IDS_SELECT_AVI_NAME "Select AVI file name" + IDS_INVALID_THROTTLE_VALUE + "Invalid throttle value. Please enter a number between 5 and 1000." + IDS_FILTER_INI "Skin INI File_*.INI__" + IDS_FILTER_VMV "VisualBoyAdvance Movie_*.VMV__" + IDS_SELECT_MOVIE_NAME "Select movie name" + IDS_BUG_REPORT "The bug report information is now available on the Windows Clipboard. Please paste it into any bug report made by email or on forums to help solve problems more easily." + IDS_UNSUPPORTED_MOVIE_VERSION "Unsupported movie version %d." + IDS_END_OF_MOVIE "end of movie" +END + +STRINGTABLE +BEGIN + IDS_INVALID_INTERVAL_VALUE + "Invalid rewind interval value. Please enter a number between 0 and 600 seconds." + IDS_REGISTRY "VisualBoyAdvance no longer uses the registry to store its settings. Your previous settings have been exported into the file: %s" + IDS_MOVIE_PLAY "Playing a movie will load a save state which may erase your previous battery saves. Please be sure to have a saved state if you don't want to loose any previous data." +END + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/res/gpl.txt b/res/gpl.txt new file mode 100644 index 00000000..a9702813 --- /dev/null +++ b/res/gpl.txt @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. \ No newline at end of file diff --git a/res/resource.h b/res/resource.h new file mode 100644 index 00000000..3495db52 --- /dev/null +++ b/res/resource.h @@ -0,0 +1,748 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by VBA.rc +// +#define IDS_UNSUPPORTED_VBA_SGM 1 +#define IDS_CANNOT_LOAD_SGM 2 +#define IDS_SAVE_GAME_NOT_USING_BIOS 3 +#define IDS_SAVE_GAME_USING_BIOS 4 +#define IDS_UNSUPPORTED_SAVE_TYPE 5 +#define IDS_CANNOT_OPEN_FILE 6 +#define IDS_BAD_ZIP_FILE 7 +#define IDS_NO_IMAGE_ON_ZIP 8 +#define IDS_ERROR_OPENING_IMAGE 9 +#define IDS_ERROR_READING_IMAGE 10 +#define IDS_UNSUPPORTED_BIOS_FUNCTION 11 +#define IDS_INVALID_BIOS_FILE_SIZE 12 +#define IDS_INVALID_CHEAT_CODE 13 +#define IDS_UNKNOWN_ARM_OPCDOE 14 +#define IDS_UNKNOWN_THUMB_OPCODE 15 +#define IDS_ERROR_CREATING_FILE 16 +#define IDS_FAILED_TO_READ_SGM 17 +#define IDS_FAILED_TO_READ_RTC 18 +#define IDS_UNSUPPORTED_VB_SGM 19 +#define IDS_CANNOT_LOAD_SGM_FOR 20 +#define IDS_ERROR_OPENING_IMAGE_FROM 21 +#define IDS_ERROR_READING_IMAGE_FROM 22 +#define IDS_UNSUPPORTED_ROM_SIZE 23 +#define IDS_UNSUPPORTED_RAM_SIZE 24 +#define IDS_UNKNOWN_CARTRIDGE_TYPE 25 +#define IDS_MAXIMUM_NUMBER_OF_CHEATS 26 +#define IDS_INVALID_GAMESHARK_CODE 27 +#define IDS_INVALID_GAMEGENIE_CODE 28 +#define IDS_INVALID_CHEAT_TO_REMOVE 29 +#define IDS_INVALID_CHEAT_CODE_ADDRESS 30 +#define IDS_UNSUPPORTED_CHEAT_LIST_VERSION 31 +#define IDS_UNSUPPORTED_CHEAT_LIST_TYPE 32 +#define IDS_INVALID_GSA_CODE 33 +#define IDS_CANNOT_IMPORT_SNAPSHOT_FOR 34 +#define IDS_UNSUPPORTED_SNAPSHOT_FILE 35 +#define IDS_UNSUPPORTED_ARM_MODE 36 +#define IDS_UNSUPPORTED_CODE_FILE 37 +#define IDS_GSA_CODE_WARNING 38 +#define IDS_INVALID_CBA_CODE 39 +#define IDS_CBA_CODE_WARNING 40 +#define IDS_OUT_OF_MEMORY 41 +#define IDI_ICON 101 +#define IDR_MENU 104 +#define IDD_ABOUT 105 +#define IDR_ACCELERATOR 106 +#define IDD_CHEATS 107 +#define IDD_ADD_CHEAT 108 +#define IDD_DIRECTORIES 109 +#define IDD_CONFIG 110 +#define IDD_CHEAT_LIST 113 +#define IDD_ASSOCIATIONS 114 +#define IDD_GBA_ROM_INFO 116 +#define IDD_GB_ROM_INFO 117 +#define IDD_GB_CHEAT_LIST 118 +#define IDD_ADD_CHEAT_DLG 119 +#define IDD_GB_PRINTER 120 +#define IDD_MOTION_CONFIG 121 +#define IDD_LANG_SELECT 122 +#define IDD_CODE_SELECT 123 +#define IDD_OPENDLG 124 +#define IDD_MAP_VIEW 126 +#define IDD_PALETTE_VIEW 127 +#define IDD_MEM_VIEWER 128 +#define IDD_OAM_VIEW 130 +#define IDD_ACCEL_EDITOR 131 +#define IDD_TILE_VIEWER 132 +#define IDD_GB_COLORS 133 +#define IDD_DISASSEMBLE 134 +#define IDD_GDB_PORT 135 +#define IDD_GDB_WAITING 136 +#define IDD_LOGGING 137 +#define IDD_EXPORT_SPS 138 +#define IDD_ADDR_SIZE 139 +#define IDD_MODES 140 +#define IDD_DRIVERS 142 +#define IDD_THROTTLE 143 +#define IDD_GB_DISASSEMBLE 144 +#define IDD_GB_OAM_VIEW 145 +#define IDD_GB_TILE_VIEWER 146 +#define IDD_GB_MAP_VIEW 147 +#define IDD_GB_PALETTE_VIEW 148 +#define IDD_MODE_CONFIRM 149 +#define IDD_REWIND_INTERVAL 150 +#define IDD_IO_VIEWER 151 +#define IDD_MAX_SCALE 154 +#define IDD_BUG_REPORT 155 +#define IDD_UNIVIDMODE 158 +#define IDC_R0 1000 +#define IDC_EDIT_UP 1000 +#define IDC_R1 1001 +#define IDC_EDIT_DOWN 1001 +#define IDC_R2 1002 +#define IDC_EDIT_LEFT 1002 +#define IDC_R3 1003 +#define IDC_EDIT_RIGHT 1003 +#define IDC_R4 1004 +#define IDC_EDIT_BUTTON_A 1004 +#define IDC_R5 1005 +#define IDC_EDIT_BUTTON_B 1005 +#define IDC_R6 1006 +#define IDC_EDIT_BUTTON_SELECT 1006 +#define IDC_R7 1007 +#define IDC_EDIT_BUTTON_START 1007 +#define IDC_R8 1008 +#define ID_OK 1008 +#define IDC_R9 1009 +#define ID_CANCEL 1009 +#define ID_SAVE 1009 +#define IDC_R10 1010 +#define IDC_EDIT_SPEED 1010 +#define IDC_R11 1011 +#define IDC_EDIT_CAPTURE 1011 +#define IDC_R12 1012 +#define IDC_EDIT_BUTTON_L 1012 +#define IDC_R13 1013 +#define IDC_EDIT_BUTTON_GS 1013 +#define IDC_R14 1014 +#define IDC_EDIT_BUTTON_R 1014 +#define IDC_R15 1015 +#define IDC_R16 1016 +#define IDC_ROM_DIR 1018 +#define IDC_NEXT 1019 +#define IDC_BATTERY_DIR 1019 +#define IDC_SAVE_DIR 1020 +#define IDC_CAPTURE_DIR 1021 +#define IDC_CHEAT_LIST 1021 +#define IDC_ROM_PATH 1022 +#define IDC_START 1022 +#define IDC_BATTERY_PATH 1023 +#define IDC_SEARCH 1023 +#define IDS_DIRECTX_7_REQUIRED 1024 +#define IDC_SAVE_PATH 1024 +#define IDC_ADD_CHEAT 1024 +#define IDC_CAPTURE_PATH 1025 +#define IDC_OLD_VALUE 1025 +#define IDC_ADD_GS_CHEAT 1025 +#define IDS_DISABLING_VIDEO_MEMORY 1025 +#define IDC_ADD_GAMESHARK 1025 +#define IDC_SPECIFIC_VALUE 1026 +#define IDS_SETTING_WILL_BE_EFFECTIVE 1026 +#define IDC_GBROM_DIR 1026 +#define IDS_DISABLING_EMULATION_ONLY 1027 +#define IDC_GBROM_PATH 1027 +#define IDC_SIZE_8 1028 +#define IDS_FAILED_TO_OPEN_FILE 1028 +#define IDC_ROM_DIR_RESET 1028 +#define IDC_SIZE_16 1029 +#define IDS_FAILED_TO_READ_ZIP_DIR 1029 +#define IDC_GBROM_DIR_RESET 1029 +#define IDC_SIZE_32 1030 +#define IDS_UNSUPPORTED_FILE_TYPE 1030 +#define IDC_BATTERY_DIR_RESET 1030 +#define IDC_EQ 1031 +#define IDS_CANNOT_CREATE_DIRECTSOUND 1031 +#define IDC_SAVE_DIR_RESET 1031 +#define IDC_NE 1032 +#define IDS_CANNOT_SETCOOPERATIVELEVEL 1032 +#define IDC_CAPTURE_DIR_RESET 1032 +#define IDC_LT 1033 +#define IDS_CANNOT_CREATESOUNDBUFFER 1033 +#define IDC_LE 1034 +#define IDS_CANNOT_SETFORMAT_PRIMARY 1034 +#define IDC_GT 1035 +#define IDS_CANNOT_CREATESOUNDBUFFER_SEC 1035 +#define IDC_GE 1036 +#define IDS_CANNOT_PLAY_PRIMARY 1036 +#define IDC_SIGNED 1037 +#define IDS_SEARCH_PRODUCED_TOO_MANY 1037 +#define IDC_UNSIGNED 1038 +#define IDS_NUMBER_CANNOT_BE_EMPTY 1038 +#define IDS_INVALID_ADDRESS 1039 +#define IDC_HEXADECIMAL 1040 +#define IDS_MISALIGNED_HALFWORD 1040 +#define IDC_VALUE 1041 +#define IDS_MISALIGNED_WORD 1041 +#define IDC_ADDRESS 1042 +#define IDS_VALUE_CANNOT_BE_EMPTY 1042 +#define IDS_ERROR_ON_STARTDOC 1043 +#define IDC_R 1043 +#define IDS_ERROR_ON_STARTPAGE 1044 +#define IDC_G 1044 +#define IDS_ERROR_PRINTING_ON_STRETCH 1045 +#define IDC_B 1045 +#define IDC_UPDATE 1046 +#define IDS_ERROR_ON_ENDPAGE 1046 +#define IDC_TILE_NUM 1046 +#define IDS_ERROR_ON_ENDDOC 1047 +#define IDC_FLIP 1047 +#define IDS_ERROR 1048 +#define IDC_PALETTE_NUM 1048 +#define IDS_JOY_LEFT 1049 +#define IDS_JOY_RIGHT 1050 +#define IDS_JOY_UP 1051 +#define IDS_JOY_DOWN 1052 +#define IDS_JOY_BUTTON 1053 +#define IDS_SELECT_ROM_DIR 1054 +#define IDS_SELECT_BATTERY_DIR 1055 +#define IDS_SELECT_SAVE_DIR 1056 +#define IDS_SELECT_CAPTURE_DIR 1057 +#define IDS_SELECT_BIOS_FILE 1058 +#define IDS_RESET 1059 +#define IDS_AUTOFIRE_A_DISABLED 1060 +#define IDS_AUTOFIRE_A 1061 +#define IDS_AUTOFIRE_B_DISABLED 1062 +#define IDS_AUTOFIRE_B 1063 +#define IDS_AUTOFIRE_L_DISABLED 1064 +#define IDS_AUTOFIRE_L 1065 +#define IDS_AUTOFIRE_R_DISABLED 1066 +#define IDC_REMOVE 1067 +#define IDS_AUTOFIRE_R 1067 +#define IDC_REMOVE_ALL 1068 +#define IDS_SELECT_ROM 1068 +#define IDS_SELECT_SAVE_GAME_NAME 1069 +#define IDC_ENABLE 1070 +#define IDS_LOADED_STATE 1070 +#define IDS_LOADED_STATE_N 1071 +#define IDS_WROTE_STATE 1072 +#define IDS_WROTE_STATE_N 1073 +#define IDC_RESTORE 1074 +#define IDS_LOADED_BATTERY 1074 +#define IDC_GBA 1075 +#define IDS_SELECT_CAPTURE_NAME 1075 +#define IDC_AGB 1076 +#define IDS_SCREEN_CAPTURE 1076 +#define IDC_BIN 1077 +#define IDS_ADDRESS 1077 +#define IDC_GB 1078 +#define IDS_OLD_VALUE 1078 +#define IDC_SGB 1079 +#define IDC_ROM_TITLE 1079 +#define IDS_NEW_VALUE 1079 +#define IDC_CGB 1080 +#define IDC_ROM_GAME_CODE 1080 +#define IDS_ADD_CHEAT_CODE 1080 +#define IDC_GBC 1081 +#define IDC_ROM_MAKER_CODE 1081 +#define IDS_CODE 1081 +#define IDC_ROM_UNIT_CODE 1082 +#define IDS_DESCRIPTION 1082 +#define IDC_ROM_DEVICE_TYPE 1083 +#define IDS_STATUS 1083 +#define IDC_ROM_VERSION 1084 +#define IDS_ADD_GG_CODE 1084 +#define IDC_ROM_CRC 1085 +#define IDS_ADD_GS_CODE 1085 +#define IDC_ROM_COLOR 1086 +#define IDC_CODE 1086 +#define IDS_POCKET_PRINTER 1086 +#define IDC_ROM_MAKER_NAME 1086 +#define IDC_ROM_SIZE 1087 +#define IDC_DESC 1087 +#define IDS_UNKNOWN 1087 +#define IDC_ROM_RAM_SIZE 1088 +#define IDC_ADD_GG_CHEAT 1088 +#define IDS_NONE 1088 +#define IDC_ROM_DEST_CODE 1089 +#define IDC_GB_PRINTER 1089 +#define IDS_FAILED_TO_LOAD_LIBRARY 1089 +#define IDC_ROM_LIC_CODE 1090 +#define IDC_1X 1090 +#define IDS_FAILED_TO_GET_LOCINFO 1090 +#define IDC_ROM_CHECKSUM 1091 +#define IDC_2X 1091 +#define IDS_SELECT_CHEAT_LIST_NAME 1091 +#define IDC_3X 1092 +#define IDS_FILTER_BIOS 1092 +#define IDC_4X 1093 +#define IDS_FILTER_ROM 1093 +#define IDC_ROM_MAKER_NAME2 1093 +#define ID_PRINT 1094 +#define IDS_FILTER_SGM 1094 +#define IDC_ADD_CODE 1095 +#define IDS_FILTER_CHEAT_LIST 1095 +#define IDS_FILTER_PNG 1096 +#define IDC_LANG_STRING 1097 +#define IDS_LOADED_CHEATS 1097 +#define IDC_LANG_NAME 1098 +#define IDS_ERROR_DISP_COLOR 1098 +#define IDS_ADD_GSA_CODE 1099 +#define IDC_GAME_LIST 1099 +#define IDS_FILTER_SPS 1100 +#define IDS_SELECT_SNAPSHOT_FILE 1101 +#define IDC_ADD_CODEBREAKER 1101 +#define IDS_FILTER_SAV 1102 +#define IDS_SELECT_BATTERY_FILE 1103 +#define IDS_FILTER_GBS 1104 +#define IDS_FILTER_GCF 1105 +#define IDS_SELECT_CODE_FILE 1106 +#define IDS_SAVE_WILL_BE_LOST 1107 +#define IDS_CONFIRM_ACTION 1108 +#define IDS_CODES_WILL_BE_LOST 1109 +#define IDS_FILTER_SPC 1110 +#define IDS_ADD_CBA_CODE 1111 +#define IDS_FILTER_WAV 1112 +#define IDS_SELECT_WAV_NAME 1113 +#define IDC_FRAME_0 1113 +#define IDS_FILTER_GBROM 1114 +#define IDC_FRAME_1 1114 +#define IDC_BG0 1115 +#define IDS_FILTER_PAL 1115 +#define IDC_BG1 1116 +#define IDS_SELECT_PALETTE_NAME 1116 +#define IDC_BG2 1117 +#define IDS_SEARCH_PRODUCED_NO_RESULTS 1117 +#define IDC_BG3 1118 +#define IDS_ERROR_BINDING 1118 +#define IDS_ERROR_LISTENING 1119 +#define IDS_ERROR_CREATING_SOCKET 1120 +#define IDS_ACK_NOT_RECEIVED 1121 +#define IDS_ERROR_NOT_GBA_IMAGE 1122 +#define IDS_EEPROM_NOT_SUPPORTED 1123 +#define IDC_MAP_VIEW 1124 +#define IDS_FILTER_DUMP 1124 +#define IDC_PALETTE_VIEW 1125 +#define IDS_SELECT_DUMP_FILE 1125 +#define IDC_PALETTE_VIEW_OBJ 1126 +#define IDC_REFRESH 1126 +#define IDS_FILTER_AVI 1126 +#define IDC_SAVE 1127 +#define IDC_GOPC 1127 +#define IDS_SELECT_AVI_NAME 1127 +#define IDC_APPLY 1127 +#define IDS_INVALID_THROTTLE_VALUE 1128 +#define IDC_REFRESH2 1129 +#define IDS_FILTER_INI 1129 +#define IDC_CLOSE 1131 +#define IDS_FILTER_VMV 1131 +#define IDS_SELECT_MOVIE_NAME 1132 +#define IDS_BUG_REPORT 1133 +#define IDS_UNSUPPORTED_MOVIE_VERSION 1134 +#define IDS_END_OF_MOVIE 1135 +#define IDC_COLOR 1136 +#define IDS_INVALID_INTERVAL_VALUE 1136 +#define IDC_SAVE_BG 1137 +#define IDS_REGISTRY 1137 +#define IDC_SAVE_OBJ 1138 +#define IDC_MAP_VIEW_ZOOM 1138 +#define IDS_MOVIE_PLAY 1138 +#define IDC_VIEWER 1140 +#define IDC_ADDRESSES 1141 +#define IDC_GO 1143 +#define IDC_8_BIT 1144 +#define IDC_16_BIT 1145 +#define IDC_32_BIT 1146 +#define IDC_OAM_VIEW 1147 +#define IDC_OAM_VIEW_ZOOM 1148 +#define IDC_SPRITE 1150 +#define IDC_POS 1151 +#define IDC_MODE 1152 +#define IDC_COLORS 1153 +#define IDC_MAPBASE 1153 +#define IDC_PALETTE 1154 +#define IDC_CHARBASE 1154 +#define IDC_TILE 1155 +#define IDC_DIM 1155 +#define IDC_PRIO 1156 +#define IDC_NUMCOLORS 1156 +#define IDC_SCROLLBAR 1157 +#define IDC_PRIORITY 1157 +#define IDC_MOSAIC 1158 +#define IDC_SIZE2 1159 +#define IDC_OVERFLOW 1159 +#define IDC_ROT 1160 +#define IDC_FLAGS 1161 +#define IDC_COMMANDS 1162 +#define IDC_BANK 1162 +#define IDC_CURRENTS 1163 +#define IDC_ASSIGN 1164 +#define IDC_RESET 1165 +#define IDC_EDIT_KEY 1166 +#define IDC_ALREADY_AFFECTED 1167 +#define IDC_TILE_VIEW 1168 +#define IDC_16_COLORS 1169 +#define IDC_256_COLORS 1170 +#define IDC_CHARBASE_0 1173 +#define IDC_CHARBASE_1 1174 +#define IDC_CHARBASE_2 1175 +#define IDC_CHARBASE_3 1176 +#define IDC_PALETTE_SLIDER 1177 +#define IDC_CHARBASE_4 1178 +#define IDC_COLOR_BG0 1178 +#define IDC_COLOR_BG1 1179 +#define IDC_URL 1179 +#define IDC_COLOR_BG2 1180 +#define IDC_STRETCH 1180 +#define IDC_URL2 1180 +#define IDC_COLOR_BG3 1181 +#define IDC_URL3 1181 +#define IDC_COLOR_OB0 1182 +#define IDC_COLOR_OB1 1183 +#define IDC_COLOR_OB2 1184 +#define IDC_COLOR_OB3 1185 +#define IDC_STATIC1 1187 +#define IDC_STATIC2 1188 +#define IDC_STATIC3 1189 +#define IDC_DEFAULT 1191 +#define IDC_USER1 1192 +#define IDC_USER2 1193 +#define IDC_DISASSEMBLE 1196 +#define IDC_AUTOMATIC 1199 +#define IDC_ARM 1200 +#define IDC_THUMB 1201 +#define IDC_AUTO_UPDATE 1204 +#define IDC_N 1210 +#define IDC_Z 1211 +#define IDC_C 1212 +#define IDC_V 1213 +#define IDC_F 1214 +#define IDC_I 1215 +#define IDC_T 1216 +#define IDC_PORT 1217 +#define IDC_VSCROLL 1218 +#define IDC_VERSION 1219 +#define IDC_VERBOSE_SWI 1223 +#define IDC_VERBOSE_UNALIGNED_ACCESS 1224 +#define IDC_VERBOSE_ILLEGAL_WRITE 1225 +#define IDC_VERBOSE_ILLEGAL_READ 1226 +#define IDC_LOG 1227 +#define IDC_CLEAR 1228 +#define IDC_VERBOSE_DMA0 1229 +#define IDC_VERBOSE_DMA1 1230 +#define IDC_TILE_NUMBER 1230 +#define IDC_VERBOSE_DMA2 1231 +#define IDC_XY 1231 +#define IDC_VERBOSE_DMA3 1232 +#define IDC_VERBOSE_UNDEFINED 1233 +#define IDC_TITLE 1234 +#define IDC_VERBOSE_AGBPRINT 1234 +#define IDC_CURRENT_ADDRESS 1235 +#define IDC_NOTES 1236 +#define IDC_CURRENT_ADDRESS_LABEL 1236 +#define IDC_LOAD 1238 +#define IDC_SIZE_CONTROL 1240 +#define IDC_MODES 1240 +#define IDC_DRIVERS 1241 +#define IDC_THROTTLE 1242 +#define IDC_H 1243 +#define IDC_OAP 1244 +#define IDC_BANK_0 1245 +#define IDC_BANK_1 1246 +#define IDC_TIMER 1247 +#define IDC_INTERVAL 1248 +#define IDC_BIT_0 1250 +#define IDC_BIT_1 1251 +#define IDC_PREDEFINED 1251 +#define IDC_BIT_2 1252 +#define IDC_BUG_REPORT 1252 +#define IDC_BIT_3 1253 +#define IDC_COPY 1253 +#define IDC_BIT_4 1254 +#define IDC_APINAME 1254 +#define IDC_BIT_5 1255 +#define IDC_DISPLAYDEVICE 1255 +#define IDC_BIT_6 1256 +#define IDC_LISTMODES 1256 +#define IDC_BIT_7 1257 +#define IDC_BIT_8 1258 +#define IDC_BIT_9 1259 +#define IDC_BUTTON1 1259 +#define IDC_BUTTON_MAXSCALE 1259 +#define IDC_BIT_10 1260 +#define IDC_CHECK_STRETCHTOFIT 1260 +#define IDC_BIT_11 1261 +#define IDC_BIT_12 1262 +#define IDC_BIT_13 1263 +#define IDC_BIT_14 1264 +#define IDC_BIT_15 1265 +#define ID_HELP_ABOUT 40001 +#define ID_FILE_EXIT 40002 +#define ID_OPTIONS_VIDEO_FRAMESKIP_0 40003 +#define ID_OPTIONS_VIDEO_FRAMESKIP_1 40004 +#define ID_OPTIONS_VIDEO_FRAMESKIP_2 40005 +#define ID_OPTIONS_VIDEO_FRAMESKIP_3 40006 +#define ID_OPTIONS_VIDEO_FRAMESKIP_4 40007 +#define ID_OPTIONS_VIDEO_FRAMESKIP_5 40008 +#define ID_OPTIONS_VIDEO_VSYNC 40009 +#define ID_OPTIONS_VIDEO_X1 40010 +#define ID_OPTIONS_VIDEO_X2 40011 +#define ID_OPTIONS_VIDEO_X3 40012 +#define ID_OPTIONS_VIDEO_X4 40013 +#define ID_FILE_PAUSE 40014 +#define ID_OPTIONS_EMULATOR_DIRECTORIES 40015 +#define ID_OPTIONS_EMULATOR_SYNCHRONIZE 40017 +#define ID_FILE_RESET 40018 +#define ID_FILE_LOAD 40019 +#define ID_OPTIONS_SOUND_DIRECTSOUNDA 40020 +#define ID_OPTIONS_SOUND_DIRECTSOUNDB 40021 +#define ID_OPTIONS_SOUND_OFF 40022 +#define ID_OPTIONS_SOUND_MUTE 40023 +#define ID_OPTIONS_SOUND_ON 40024 +#define ID_OPTIONS_SOUND_CHANNEL1 40025 +#define ID_OPTIONS_SOUND_CHANNEL2 40026 +#define ID_OPTIONS_SOUND_CHANNEL3 40027 +#define ID_OPTIONS_SOUND_CHANNEL4 40028 +#define ID_OPTIONS_EMULATOR_USEBIOSFILE 40029 +#define ID_OPTIONS_EMULATOR_SELECTBIOSFILE 40030 +#define ID_CHEATS_SEARCHFORCHEATS 40031 +#define ID_OPTIONS_VIDEO_DISABLESFX 40033 +#define ID_OPTIONS_GAMEBOY_BORDER 40034 +#define ID_FILE_SAVEGAME_SLOT1 40035 +#define ID_FILE_SAVEGAME_SLOT2 40036 +#define ID_FILE_SAVEGAME_SLOT3 40037 +#define ID_FILE_SAVEGAME_SLOT4 40038 +#define ID_FILE_SAVEGAME_SLOT5 40039 +#define ID_FILE_SAVEGAME_SLOT6 40040 +#define ID_FILE_SAVEGAME_SLOT7 40041 +#define ID_FILE_SAVEGAME_SLOT8 40042 +#define ID_FILE_SAVEGAME_SLOT9 40043 +#define ID_FILE_SAVEGAME_SLOT10 40044 +#define ID_FILE_LOADGAME_SLOT1 40045 +#define ID_FILE_LOADGAME_SLOT2 40046 +#define ID_FILE_LOADGAME_SLOT3 40047 +#define ID_FILE_LOADGAME_SLOT4 40048 +#define ID_FILE_LOADGAME_SLOT5 40049 +#define ID_FILE_LOADGAME_SLOT6 40050 +#define ID_FILE_LOADGAME_SLOT7 40051 +#define ID_FILE_LOADGAME_SLOT8 40052 +#define ID_FILE_LOADGAME_SLOT9 40053 +#define ID_FILE_LOADGAME_SLOT10 40054 +#define ID_OPTIONS_GAMEBOY_AUTOMATIC 40057 +#define ID_OPTIONS_GAMEBOY_CGB 40058 +#define ID_OPTIONS_GAMEBOY_GBA 40059 +#define ID_OPTIONS_GAMEBOY_SGB 40060 +#define ID_OPTIONS_GAMEBOY_GB 40062 +#define ID_OPTIONS_GAMEBOY_REALCOLORS 40063 +#define ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS 40064 +#define ID_OPTIONS_SOUND_11KHZ 40067 +#define ID_OPTIONS_SOUND_22KHZ 40068 +#define ID_OPTIONS_SOUND_44KHZ 40069 +#define ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY 40070 +#define ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY 40071 +#define ID_OPTIONS_PRIORITY_HIGHEST 40072 +#define ID_OPTIONS_PRIORITY_ABOVENORMAL 40073 +#define ID_OPTIONS_PRIORITY_NORMAL 40074 +#define ID_OPTIONS_PRIORITY_BELOWNORMAL 40075 +#define ID_OPTIONS_VIDEO_FULLSCREEN320X240 40076 +#define ID_OPTIONS_VIDEO_FULLSCREEN640X480 40077 +#define ID_OPTIONS_FILTER_NORMAL 40078 +#define ID_OPTIONS_FILTER_2XSAI 40079 +#define ID_OPTIONS_FILTER_SUPER2XSAI 40081 +#define ID_OPTIONS_FILTER_SUPEREAGLE 40082 +#define ID_OPTIONS_FILTER_TVMODE 40083 +#define ID_CHEATS_CHEATLIST 40084 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_A 40085 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_B 40086 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_L 40087 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_R 40088 +#define ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT 40089 +#define ID_OPTIONS_EMULATOR_ASSOCIATE 40091 +#define ID_OPTIONS_FILTER_DISABLEMMX 40093 +#define ID_FILE_ROMINFORMATION 40100 +#define ID_OPTIONS_EMULATOR_DISABLESTATUSMESSAGES 40102 +#define ID_OPTIONS_JOYPAD_MOTIONCONFIGURE 40103 +#define ID_FILE_SCREENCAPTURE 40104 +#define ID_OPTIONS_LANGUAGE_SYSTEM 40105 +#define ID_OPTIONS_LANGUAGE_ENGLISH 40106 +#define ID_OPTIONS_LANGUAGE_OTHER 40107 +#define ID_OPTIONS_GAMEBOY_PRINTER 40108 +#define ID_FILE_RECENT_RESET 40109 +#define ID_CHEATS_SAVECHEATLIST 40110 +#define ID_CHEATS_LOADCHEATLIST 40111 +#define ID_CHEATS_AUTOMATICSAVELOADCHEATS 40112 +#define ID_FILE_IMPORT_GAMESHARKSNAPSHOT 40115 +#define ID_FILE_IMPORT_BATTERYFILE 40116 +#define ID_FILE_IMPORT_GAMESHARKCODEFILE 40117 +#define ID_FILE_EXPORT_BATTERYFILE 40118 +#define ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL 40121 +#define ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE 40124 +#define ID_OPTIONS_SOUND_STARTRECORDING 40125 +#define ID_OPTIONS_SOUND_STOPRECORDING 40126 +#define ID_OPTIONS_VIDEO_LAYERS_BG0 40127 +#define ID_OPTIONS_VIDEO_LAYERS_BG1 40128 +#define ID_OPTIONS_VIDEO_LAYERS_BG2 40129 +#define ID_OPTIONS_VIDEO_LAYERS_BG3 40130 +#define ID_OPTIONS_VIDEO_LAYERS_OBJ 40131 +#define ID_OPTIONS_VIDEO_LAYERS_WIN0 40132 +#define ID_OPTIONS_VIDEO_LAYERS_WIN1 40133 +#define ID_OPTIONS_VIDEO_LAYERS_OBJWIN 40134 +#define ID_FILE_OPENGAMEBOY 40135 +#define ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION 40136 +#define ID_DEBUG_NEXTFRAME 40137 +#define ID_TOOLS_MAPVIEW 40138 +#define ID_TOOLS_PALETTEVIEW 40139 +#define ID_OPTIONS_EMULATOR_PNGFORMAT 40140 +#define ID_OPTIONS_EMULATOR_BMPFORMAT 40141 +#define ID_TOOLS_MEMORYVIEWER 40143 +#define ID_TOOLS_OAMVIEWER 40144 +#define ID_TOOLS_CUSTOMIZE 40145 +#define ID_TOOLS_TILEVIEWER 40146 +#define ID_OPTIONS_GAMEBOY_COLORS 40147 +#define ID_OPTIONS_SOUND_ECHO 40148 +#define ID_OPTIONS_SOUND_LOWPASSFILTER 40149 +#define ID_OPTIONS_SOUND_REVERSESTEREO 40150 +#define ID_TOOLS_DISASSEMBLE 40151 +#define ID_TOOLS_DEBUG_GDB 40152 +#define ID_TOOLS_DEBUG_LOADANDWAIT 40153 +#define ID_TOOLS_DEBUG_DISCONNECT 40154 +#define ID_TOOLS_DEBUG_BREAK 40155 +#define ID_TOOLS_LOGGING 40156 +#define ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE 40158 +#define ID_OPTIONS_EMULATOR_REMOVEINTROSGBA 40159 +#define ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X 40160 +#define ID_OPTIONS_FILTER16BIT_SIMPLE2X 40161 +#define ID_FILE_RECENT_FREEZE 40162 +#define ID_FILE_EXPORT_GAMESHARKSNAPSHOT 40163 +#define ID_OPTIONS_VIDEO_FULLSCREEN800X600 40164 +#define ID_OPTIONS_VIDEO_FRAMESKIP_6 40165 +#define ID_OPTIONS_VIDEO_FRAMESKIP_7 40166 +#define ID_OPTIONS_VIDEO_FRAMESKIP_8 40167 +#define ID_OPTIONS_VIDEO_FRAMESKIP_9 40168 +#define ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC 40169 +#define ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM 40170 +#define ID_OPTIONS_EMULATOR_SAVETYPE_SRAM 40171 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH 40172 +#define ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR 40173 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K 40174 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M 40175 +#define ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH 40176 +#define ID_TOOLS_RECORD_STARTAVIRECORDING 40178 +#define ID_TOOLS_RECORD_STOPAVIRECORDING 40179 +#define ID_OPTIONS_SOUND_VOLUME_1X 40182 +#define ID_OPTIONS_SOUND_VOLUME_2X 40183 +#define ID_OPTIONS_SOUND_VOLUME_3X 40184 +#define ID_OPTIONS_SOUND_VOLUME_4X 40185 +#define ID_OPTIONS_FILTER_BILINEAR 40186 +#define ID_OPTIONS_FILTER_BILINEARPLUS 40187 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE 40188 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR 40189 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART 40190 +#define ID_OPTIONS_VIDEO_FULLSCREEN 40191 +#define ID_OPTIONS_VIDEO_TRIPLEBUFFERING 40192 +#define ID_OPTIONS_FRAMESKIP_AUTOMATIC 40194 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_NONE 40195 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE 40196 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED 40197 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT 40198 +#define ID_OPTIONS_JOYPAD_CONFIGURE_1 40199 +#define ID_OPTIONS_JOYPAD_CONFIGURE_2 40200 +#define ID_OPTIONS_JOYPAD_CONFIGURE_3 40201 +#define ID_OPTIONS_JOYPAD_CONFIGURE_4 40202 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1 40208 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_2 40209 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_3 40210 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4 40211 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE 40216 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_50 40217 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_150 40218 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_200 40219 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_25 40220 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER 40221 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_100 40222 +#define ID_OPTIONS_FILTER_SCANLINES 40223 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_GDI 40228 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW 40229 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D 40230 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL 40231 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER 40233 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR 40234 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST 40237 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR 40238 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE 40239 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS 40240 +#define ID_OPTIONS_EMULATOR_REALTIMECLOCK 40248 +#define ID_OPTIONS_GAMEBOY_SGB2 40249 +#define ID_TOOLS_RECORD_STARTMOVIERECORDING 40251 +#define ID_TOOLS_RECORD_STOPMOVIERECORDING 40252 +#define ID_TOOLS_PLAY_STARTMOVIEPLAYING 40253 +#define ID_TOOLS_PLAY_STOPMOVIEPLAYING 40254 +#define ID_OPTIONS_EMULATOR_AUTOHIDEMENU 40255 +#define ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC 40256 +#define ID_TOOLS_REWIND 40258 +#define ID_OPTIONS_EMULATOR_SKIPBIOS 40259 +#define ID_HELP_BUGREPORT 40260 +#define ID_HELP_FAQ 40261 +#define ID_OPTIONS_EMULATOR_REWINDINTERVAL 40262 +#define ID_FILE_TOGGLEMENU 40263 +#define ID_OPTIONS_EMULATOR_SAVETYPE_NONE 40264 +#define ID_OPTIONS_EMULATOR_SAVETYPE_ENHANCEDDETECTION 40265 +#define ID_TOOLS_IOVIEWER 40266 +#define ID_FILE_LOADGAME_MOSTRECENT 40267 +#define ID_FILE_SAVEGAME_OLDESTSLOT 40268 +#define ID_FILE_LOADGAME_AUTOLOADMOSTRECENT 40269 +#define ID_OPTIONS_SOUND_VOLUME_5X 40270 +#define ID_OPTIONS_SOUND_VOLUME_25X 40271 +#define ID_CHEATS_DISABLECHEATS 40272 +#define ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE 40273 +#define ID_OPTIONS_FILTER_HQ2X 40274 +#define ID_OPTIONS_FILTER_LQ2X 40275 +#define ID_OPTIONS_EMULATOR_AGBPRINT 40281 +#define ID_OPTIONS_VIDEO_FULLSCREEN1024X768 40282 +#define ID_OPTIONS_VIDEO_FULLSCREEN1280X1024 40283 +#define ID_OPTIONS_FILTER_SIMPLE3X 40287 +#define ID_OPTIONS_FILTER_SIMPLE4X 40288 +#define ID_OPTIONS_FILTER_HQ3X 40290 +#define ID_OPTIONS_FILTER_HQ4X 40291 +#define ID_VIDEO_WINDOWED 40292 +#define ID_VIDEO_FULL 40293 +#define ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE 40294 +#define ID_OPTIONS_SOUND_PCMINTERPOLATION_LINEAR 40295 +#define ID_OPTIONS_SOUND_PCMINTERPOLATION_CUBIC 40296 +#define ID_OPTIONS_SOUND_PCMINTERPOLATION_FIR 40297 +#define ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE 40298 +#define ID_OPTIONS_FILTER_LCDCOLORS 40299 +#define IDD_LINKTAB1 40300 +#define IDD_LINKTAB 40301 +#define IDD_LINKTAB2 40302 +#define IDD_LINKTAB3 40303 +#define IDD_SERVERWAIT 40304 +#define IDC_TAB1 40305 +#define IDC_LINK_SINGLE 40306 +#define IDC_LINK_TIMEOUT 40307 +#define IDC_LINK_LAN 40308 +#define IDC_LINK2P 40309 +#define IDC_LINKTCP 40310 +#define IDC_SSPEED 40311 +#define IDC_SERVERSTART 40312 +#define IDC_SERVERIP 40313 +#define IDC_CLINKIP 40314 +#define IDC_SPEEDOFF 40315 +#define IDC_LINKCONNECT 40316 +#define IDC_STATIC4 40317 +#define ID_OPTIONS_LINK_OPTIONS 40318 +#define ID_OPTIONS_LINK_LOG 40319 +#define ID_OPTIONS_LINK_WIRELESSADAPTER 40320 +#define IDC_LINKTIMEOUT 40321 +#define IDC_CLINKTCP 40322 +#define IDC_SERVERWAIT 40323 +#define IDC_LINKUDP 40324 +#define IDC_LINK3P 40325 +#define IDC_LINK4P 40326 +#define IDC_CLINKUDP 40327 +#define IDC_SPEEDON 40328 + + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 159 +#define _APS_NEXT_COMMAND_VALUE 40300 +#define _APS_NEXT_CONTROL_VALUE 1261 +#define _APS_NEXT_SYMED_VALUE 103 +#endif +#endif diff --git a/snd_interp.cpp b/snd_interp.cpp new file mode 100644 index 00000000..c80f387b --- /dev/null +++ b/snd_interp.cpp @@ -0,0 +1,754 @@ +#include + +#define WIN32_LEAN_AND_MEAN +#include + +#include "../../libresample-0.1.3/include/libresample.h" + +//#define LIBSAMPLERATE // buggy +#ifdef LIBSAMPLERATE +#include "../../libsamplerate-0.1.2/src/samplerate.h" +#endif + +#include "snd_interp.h" + +// this was once borrowed from libmodplug, and was also used to generate the FIR coefficient +// tables that ZSNES uses for its "FIR" interpolation mode + +/* + ------------------------------------------------------------------------------------------------ + fir interpolation doc, + (derived from "an engineer's guide to fir digital filters", n.j. loy) + + calculate coefficients for ideal lowpass filter (with cutoff = fc in 0..1 (mapped to 0..nyquist)) + c[-N..N] = (i==0) ? fc : sin(fc*pi*i)/(pi*i) + + then apply selected window to coefficients + c[-N..N] *= w(0..N) + with n in 2*N and w(n) being a window function (see loy) + + then calculate gain and scale filter coefs to have unity gain. + ------------------------------------------------------------------------------------------------ +*/ +// quantizer scale of window coefs +#define WFIR_QUANTBITS 14 +#define WFIR_QUANTSCALE (1L<>1) +// cutoff (1.0 == pi/2) +#define WFIR_CUTOFF 0.95f +// wfir type +#define WFIR_HANN 0 +#define WFIR_HAMMING 1 +#define WFIR_BLACKMANEXACT 2 +#define WFIR_BLACKMAN3T61 3 +#define WFIR_BLACKMAN3T67 4 +#define WFIR_BLACKMAN4T92 5 +#define WFIR_BLACKMAN4T74 6 +#define WFIR_KAISER4T 7 +#define WFIR_TYPE WFIR_KAISER4T +// wfir help +#ifndef M_zPI +#define M_zPI 3.1415926535897932384626433832795 +#endif +#define M_zEPS 1e-8 +#define M_zBESSELEPS 1e-21 + +class CzWINDOWEDFIR +{ public: + CzWINDOWEDFIR( ); + ~CzWINDOWEDFIR( ); + float coef( int _PCnr, float _POfs, float _PCut, int _PWidth, int _PType ) //float _PPos, float _PFc, int _PLen ) + { double _LWidthM1 = _PWidth-1; + double _LWidthM1Half = 0.5*_LWidthM1; + double _LPosU = ((double)_PCnr - _POfs); + double _LPos = _LPosU-_LWidthM1Half; + double _LPIdl = 2.0*M_zPI/_LWidthM1; + double _LWc,_LSi; + if( fabs(_LPos)_LScale)?_LScale:_LCoef) ); + } + } +} + +CzWINDOWEDFIR::~CzWINDOWEDFIR() +{ // nothing todo +} + +CzWINDOWEDFIR sfir; + +template +class sample_buffer +{ + int ptr, filled; + T * buffer; + +public: + sample_buffer() : ptr(0), filled(0), buffer(0) {} + ~sample_buffer() + { + if (buffer) delete [] buffer; + } + + void clear() + { + if (buffer) + { + delete [] buffer; + buffer = 0; + } + ptr = filled = 0; + } + + inline int size() const + { + return filled; + } + + void push_back(T sample) + { + if (!buffer) buffer = new T[buffer_size]; + buffer[ptr] = sample; + if (++ptr >= buffer_size) ptr = 0; + if (filled < buffer_size) filled++; + } + + void erase(int count) + { + if (count > filled) filled = 0; + else filled -= count; + } + + T operator[] (int index) const + { + index += ptr - filled; + if (index < 0) index += buffer_size; + else if (index > buffer_size) index -= buffer_size; + return buffer[index]; + } + + // omghax! + void lock( T * & out1, unsigned & count1, T * & out2, unsigned & count2 ) + { + if (!buffer) buffer = new T[buffer_size]; + unsigned free = buffer_size - filled; + out1 = & buffer[ ptr ]; + if ( ptr ) + { + count1 = buffer_size - ptr; + out2 = &buffer[ 0 ]; + count2 = ptr; + if ( count1 > free ) + { + count1 = free; + out2 = 0; + count2 = 0; + } + else if ( count1 + count2 > free ) + { + count2 = free - count1; + if ( ! count2 ) out2 = 0; + } + } + else + { + count1 = free; + out2 = 0; + count2 = 0; + } + } + + void push_count( unsigned count ) + { + if ( count + filled > buffer_size ) + { + count = buffer_size - filled; + } + + ptr = ( ptr + count ) % buffer_size; + filled += count; + } +}; + +class foo_null : public foo_interpolate +{ + int sample; + +public: + foo_null() : sample(0) {} + ~foo_null() {} + + void reset() {} + + void push( double rate, int psample ) + { + sample = psample; + } + + int pop(double rate) + { + return sample; + } +}; + +class foo_linear : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_linear() + { + position = 0; + } + + ~foo_linear() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(double rate, int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 2) return 0; + + ret = smp(0) * (0x8000 - position); + ret += smp(1) * position; + ret >>= 15; + + // wahoo, takes care of drifting + if (samples.size() > 2) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +// and this integer cubic interpolation implementation was kind of borrowed from either TiMidity +// or the P.E.Op.S. SPU project, or is in use in both, or something... + +class foo_cubic : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_cubic() + { + position = 0; + } + + ~foo_cubic() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(double rate, int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 4) return 0; + + ret = smp(3) - 3 * smp(2) + 3 * smp(1) - smp(0); + ret *= (position - (2 << 15)) / 6; + ret >>= 15; + ret += smp(2) - 2 * smp(1) + smp(0); + ret *= (position - (1 << 15)) >> 1; + ret >>= 15; + ret += smp(1) - smp(0); + ret *= position; + ret >>= 15; + ret += smp(0); + + if (ret > 32767) ret = 32767; + else if (ret < -32768) ret = -32768; + + // wahoo, takes care of drifting + if (samples.size() > 8) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +class foo_fir : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_fir() + { + position = 0; + } + + ~foo_fir() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(double rate, int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 8) return 0; + + ret = smp(0) * CzWINDOWEDFIR::lut[(position & ~7) ]; + ret += smp(1) * CzWINDOWEDFIR::lut[(position & ~7) + 1]; + ret += smp(2) * CzWINDOWEDFIR::lut[(position & ~7) + 2]; + ret += smp(3) * CzWINDOWEDFIR::lut[(position & ~7) + 3]; + ret += smp(4) * CzWINDOWEDFIR::lut[(position & ~7) + 4]; + ret += smp(5) * CzWINDOWEDFIR::lut[(position & ~7) + 5]; + ret += smp(6) * CzWINDOWEDFIR::lut[(position & ~7) + 6]; + ret += smp(7) * CzWINDOWEDFIR::lut[(position & ~7) + 7]; + ret >>= WFIR_QUANTBITS; + + if (ret > 32767) ret = 32767; + else if (ret < -32768) ret = -32768; + + // wahoo, takes care of drifting + if (samples.size() > 16) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +class foo_libresample : public foo_interpolate +{ + sample_buffer samples; + + void * resampler; + +public: + foo_libresample() + { + resampler = 0; + } + + ~foo_libresample() + { + reset(); + } + + void reset() + { + samples.clear(); + if (resampler) + { + resample_close( resampler ); + resampler = 0; + } + } + + void push( double rate, int sample ) + { + if ( ! resampler ) + { + resampler = resample_open( 0, .25, 44100. / 4000. ); + } + + { + float in = float( sample ); + float * samples1, * samples2; + unsigned count1, count2; + + samples.lock( samples1, count1, samples2, count2 ); + + int used; + int processed = resample_process( resampler, 1. / rate, & in, 1, 0, & used, samples1, count1 ); + + samples.push_count( processed ); + + if ( ! used && count2 ) + { + processed = resample_process( resampler, 1. / rate, & in, 1, 0, & used, samples2, count2 ); + + samples.push_count( processed ); + } + } + } + + int pop( double rate ) + { + int ret; + + if ( samples.size() ) + { + ret = int( samples[ 0 ] ); + samples.erase( 1 ); + } + else ret = 0; + + if ( ret > 32767 ) ret = 32767; + else if ( ret < -32768 ) ret = -32768; + + return ret; + } +}; + +#ifdef LIBSAMPLERATE +class foo_src : public foo_interpolate +{ + sample_buffer samples; + + SRC_STATE * resampler; + + SRC_DATA resampler_data; + +public: + foo_src() + { + resampler = 0; + } + + ~foo_src() + { + reset(); + } + + void reset() + { + samples.clear(); + if (resampler) + { + resampler = src_delete( resampler ); + } + } + + void push( double rate, int sample ) + { + if ( ! resampler ) + { + int err; + resampler = src_new( SRC_LINEAR, 1, & err ); + if ( err ) + { + if ( resampler ) resampler = src_delete( resampler ); + return; + } + } + + { + float in = float( sample ); + float * samples1, * samples2; + unsigned count1, count2; + + samples.lock( samples1, count1, samples2, count2 ); + + resampler_data.data_in = & in; + resampler_data.input_frames = 1; + resampler_data.data_out = samples1; + resampler_data.output_frames = count1; + resampler_data.src_ratio = 1. / rate; + + if ( src_process( resampler, & resampler_data ) ) + return; + + samples.push_count( resampler_data.output_frames_gen ); + + if ( ! resampler_data.input_frames_used && count2 ) + { + resampler_data.data_out = samples2; + resampler_data.output_frames = count2; + + if ( src_process( resampler, & resampler_data ) ) + return; + + samples.push_count( resampler_data.output_frames_gen ); + } + } + } + + int pop(double rate) + { + int ret; + + if ( samples.size() ) + { + ret = int( samples[ 0 ] ); + samples.erase( 1 ); + } + else ret = 0; + + if ( ret > 32767 ) ret = 32767; + else if ( ret < -32768 ) ret = -32768; + + return ret; + } +}; +#endif + +foo_interpolate * get_filter(int which) +{ + switch (which) + { + default: + return new foo_null; + case 1: + return new foo_linear; + case 2: + return new foo_cubic; + case 3: + return new foo_fir; + case 4: + return new foo_libresample; + } +} + +// and here is the implementation specific code, in a messier state than the stuff above + +extern bool timer0On; +extern int timer0Reload; +extern int timer0ClockReload; +extern bool timer1On; +extern int timer1Reload; +extern int timer1ClockReload; + +extern int SOUND_CLOCK_TICKS; +extern int soundInterpolation; + +double calc_rate(int timer) +{ + if (timer ? timer1On : timer0On) + { + return double(SOUND_CLOCK_TICKS) / + double((0x10000 - (timer ? timer1Reload : timer0Reload)) << + (timer ? timer1ClockReload : timer0ClockReload)); + } + else + { + return 1.; + } +} + +static foo_interpolate * interp[2]; + +class foo_interpolate_setup +{ +public: + foo_interpolate_setup() + { + for (int i = 0; i < 2; i++) + { + interp[i] = get_filter(0); + } + } + + ~foo_interpolate_setup() + { + for (int i = 0; i < 2; i++) + { + delete interp[i]; + } + } +}; + +static foo_interpolate_setup blah; + +class critical_section +{ + CRITICAL_SECTION cs; + +public: + critical_section() { InitializeCriticalSection(&cs); } + ~critical_section() { DeleteCriticalSection(&cs); } + + void enter() { EnterCriticalSection(&cs); } + void leave() { LeaveCriticalSection(&cs); } +}; + +static critical_section interp_sync; +static int interpolation = 0; + +class scopelock +{ + critical_section * cs; + +public: + scopelock(critical_section & pcs) { cs = &pcs; cs->enter(); } + ~scopelock() { cs->leave(); } +}; + +void interp_switch(int which) +{ + scopelock sl(interp_sync); + + for (int i = 0; i < 2; i++) + { + delete interp[i]; + interp[i] = get_filter(which); + } + + interpolation = which; +} + +void interp_reset(int ch) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + interp[ch]->reset(); +} + +void interp_push(int ch, double rate, int sample) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + interp[ch]->push(rate, sample); +} + +int interp_pop(int ch, double rate) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + return interp[ch]->pop(rate); +} diff --git a/snd_interp.h b/snd_interp.h new file mode 100644 index 00000000..c335785e --- /dev/null +++ b/snd_interp.h @@ -0,0 +1,30 @@ +#ifndef __SND_INTERP_H__ +#define __SND_INTERP_H__ + +// simple interface that could easily be recycled + +class foo_interpolate +{ +public: + foo_interpolate() {} + virtual ~foo_interpolate() {}; + + virtual void reset() = 0; + + virtual void push( double rate, int sample ) = 0; + virtual int pop( double rate ) = 0; +}; + +foo_interpolate * get_filter(int which); + + +// complicated, synced interface, specific to this implementation + +double calc_rate(int timer); + +void interp_switch(int which); +void interp_reset(int ch); +void interp_push(int ch, double rate, int sample); +int interp_pop(int ch, double rate); + +#endif \ No newline at end of file diff --git a/src/2xSaI.cpp b/src/2xSaI.cpp new file mode 100644 index 00000000..61eb116b --- /dev/null +++ b/src/2xSaI.cpp @@ -0,0 +1,1274 @@ +#include "System.h" +#include "Port.h" + +extern "C" +{ + +#ifdef MMX + void _2xSaILine (u8 *srcPtr, u8 *deltaPtr, u32 srcPitch, + u32 width, u8 *dstPtr, u32 dstPitch); + void _2xSaISuperEagleLine (u8 *srcPtr, u8 *deltaPtr, + u32 srcPitch, u32 width, + u8 *dstPtr, u32 dstPitch); + void _2xSaISuper2xSaILine (u8 *srcPtr, u8 *deltaPtr, + u32 srcPitch, u32 width, + u8 *dstPtr, u32 dstPitch); + void Init_2xSaIMMX (u32 BitFormat); + void BilinearMMX (u16 * A, u16 * B, u16 * C, u16 * D, + u16 * dx, u16 * dy, u8 *dP); + void BilinearMMXGrid0 (u16 * A, u16 * B, u16 * C, u16 * D, + u16 * dx, u16 * dy, u8 *dP); + void BilinearMMXGrid1 (u16 * A, u16 * B, u16 * C, u16 * D, + u16 * dx, u16 * dy, u8 *dP); + void EndMMX (); + + bool cpu_mmx = 1; +#endif +} +static u32 colorMask = 0xF7DEF7DE; +static u32 lowPixelMask = 0x08210821; +static u32 qcolorMask = 0xE79CE79C; +static u32 qlowpixelMask = 0x18631863; +static u32 redblueMask = 0xF81F; +static u32 greenMask = 0x7E0; + +u32 qRGB_COLOR_MASK[2] = { 0xF7DEF7DE, 0xF7DEF7DE }; + +extern void hq2x_init(unsigned); + +int Init_2xSaI(u32 BitFormat) +{ + if(systemColorDepth == 16) { + if (BitFormat == 565) { + colorMask = 0xF7DEF7DE; + lowPixelMask = 0x08210821; + qcolorMask = 0xE79CE79C; + qlowpixelMask = 0x18631863; + redblueMask = 0xF81F; + greenMask = 0x7E0; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0xF7DEF7DE; + hq2x_init(16); + } else if (BitFormat == 555) { + colorMask = 0x7BDE7BDE; + lowPixelMask = 0x04210421; + qcolorMask = 0x739C739C; + qlowpixelMask = 0x0C630C63; + redblueMask = 0x7C1F; + greenMask = 0x3E0; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0x7BDE7BDE; + hq2x_init(15); + } else { + return 0; + } + } else if(systemColorDepth == 32) { + colorMask = 0xfefefe; + lowPixelMask = 0x010101; + qcolorMask = 0xfcfcfc; + qlowpixelMask = 0x030303; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0xfefefe; + hq2x_init(32); + } else + return 0; + +#ifdef MMX + Init_2xSaIMMX (BitFormat); +#endif + + return 1; +} + +static inline int GetResult1 (u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline int GetResult2 (u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r -= 1; + if (y <= 1) + r += 1; + return r; +} + +static inline int GetResult (u32 A, u32 B, u32 C, u32 D) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline u32 INTERPOLATE (u32 A, u32 B) +{ + if (A != B) { + return (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + + (A & B & lowPixelMask)); + } else + return A; +} + +static inline u32 Q_INTERPOLATE (u32 A, u32 B, u32 C, u32 D) +{ + register u32 x = ((A & qcolorMask) >> 2) + + ((B & qcolorMask) >> 2) + + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2); + register u32 y = (A & qlowpixelMask) + + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask); + + y = (y >> 2) & qlowpixelMask; + return x + y; +} + +static inline int GetResult1_32 (u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline int GetResult2_32 (u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r -= 1; + if (y <= 1) + r += 1; + return r; +} + +#define BLUE_MASK565 0x001F001F +#define RED_MASK565 0xF800F800 +#define GREEN_MASK565 0x07E007E0 + +#define BLUE_MASK555 0x001F001F +#define RED_MASK555 0x7C007C00 +#define GREEN_MASK555 0x03E003E0 + +void Super2xSaI (u8 *srcPtr, u32 srcPitch, + u8 *deltaPtr, u8 *dstPtr, u32 dstPitch, + int width, int height) +{ + u16 *bP; + u8 *dP; + u32 inc_bP; + u32 Nextline = srcPitch >> 1; +#ifdef MMX + if (cpu_mmx) { + for (; height; height--) { + _2xSaISuper2xSaILine (srcPtr, deltaPtr, srcPitch, width, + dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } else +#endif + { + inc_bP = 1; + + for (; height; height--) { + bP = (u16 *) srcPtr; + dP = (u8 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA0, colorA1, colorA2, colorA3, + colorB0, colorB1, colorB2, colorB3, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + //--------------------------------------- B1 B2 + // 4 5 6 S2 + // 1 2 3 S1 + // A1 A2 + + colorB0 = *(bP - Nextline - 1); + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + colorB3 = *(bP - Nextline + 2); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA0 = *(bP + Nextline + Nextline - 1); + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + colorA3 = *(bP + Nextline + Nextline + 2); + + //-------------------------------------- + if (color2 == color6 && color5 != color3) { + product2b = product1b = color2; + } else if (color5 == color3 && color2 != color6) { + product2b = product1b = color5; + } else if (color5 == color3 && color2 == color6) { + register int r = 0; + + r += GetResult (color6, color5, color1, colorA1); + r += GetResult (color6, color5, color4, colorB1); + r += GetResult (color6, color5, colorA2, colorS1); + r += GetResult (color6, color5, colorB2, colorS2); + + if (r > 0) + product2b = product1b = color6; + else if (r < 0) + product2b = product1b = color5; + else { + product2b = product1b = INTERPOLATE (color5, color6); + } + } else { + if (color6 == color3 && color3 == colorA1 + && color2 != colorA2 && color3 != colorA0) + product2b = + Q_INTERPOLATE (color3, color3, color3, color2); + else if (color5 == color2 && color2 == colorA2 + && colorA1 != color3 && color2 != colorA3) + product2b = + Q_INTERPOLATE (color2, color2, color2, color3); + else + product2b = INTERPOLATE (color2, color3); + + if (color6 == color3 && color6 == colorB1 + && color5 != colorB2 && color6 != colorB0) + product1b = + Q_INTERPOLATE (color6, color6, color6, color5); + else if (color5 == color2 && color5 == colorB2 + && colorB1 != color6 && color5 != colorB3) + product1b = + Q_INTERPOLATE (color6, color5, color5, color5); + else + product1b = INTERPOLATE (color5, color6); + } + + if (color5 == color3 && color2 != color6 && color4 == color5 + && color5 != colorA2) + product2a = INTERPOLATE (color2, color5); + else + if (color5 == color1 && color6 == color5 + && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE (color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 + && color2 != colorB2) + product1a = INTERPOLATE (color2, color5); + else + if (color4 == color2 && color3 == color2 + && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE (color2, color5); + else + product1a = color5; + +#ifdef WORDS_BIGENDIAN + product1a = (product1a << 16) | product1b; + product2a = (product2a << 16) | product2b; +#else + product1a = product1a | (product1b << 16); + product2a = product2a | (product2b << 16); +#endif + + *((u32 *) dP) = product1a; + *((u32 *) (dP + dstPitch)) = product2a; + + bP += inc_bP; + dP += sizeof (u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (; height; height--) + } +} + +void Super2xSaI32 (u8 *srcPtr, u32 srcPitch, + u8 * /* deltaPtr */, u8 *dstPtr, u32 dstPitch, + int width, int height) +{ + u32 *bP; + u32 *dP; + u32 inc_bP; + u32 Nextline = srcPitch >> 2; + inc_bP = 1; + + for (; height; height--) { + bP = (u32 *) srcPtr; + dP = (u32 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA0, colorA1, colorA2, colorA3, + colorB0, colorB1, colorB2, colorB3, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + //--------------------------------------- B1 B2 + // 4 5 6 S2 + // 1 2 3 S1 + // A1 A2 + + colorB0 = *(bP - Nextline - 1); + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + colorB3 = *(bP - Nextline + 2); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA0 = *(bP + Nextline + Nextline - 1); + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + colorA3 = *(bP + Nextline + Nextline + 2); + + //-------------------------------------- + if (color2 == color6 && color5 != color3) { + product2b = product1b = color2; + } else if (color5 == color3 && color2 != color6) { + product2b = product1b = color5; + } else if (color5 == color3 && color2 == color6) { + register int r = 0; + + r += GetResult (color6, color5, color1, colorA1); + r += GetResult (color6, color5, color4, colorB1); + r += GetResult (color6, color5, colorA2, colorS1); + r += GetResult (color6, color5, colorB2, colorS2); + + if (r > 0) + product2b = product1b = color6; + else if (r < 0) + product2b = product1b = color5; + else { + product2b = product1b = INTERPOLATE (color5, color6); + } + } else { + if (color6 == color3 && color3 == colorA1 + && color2 != colorA2 && color3 != colorA0) + product2b = + Q_INTERPOLATE (color3, color3, color3, color2); + else if (color5 == color2 && color2 == colorA2 + && colorA1 != color3 && color2 != colorA3) + product2b = + Q_INTERPOLATE (color2, color2, color2, color3); + else + product2b = INTERPOLATE (color2, color3); + + if (color6 == color3 && color6 == colorB1 + && color5 != colorB2 && color6 != colorB0) + product1b = + Q_INTERPOLATE (color6, color6, color6, color5); + else if (color5 == color2 && color5 == colorB2 + && colorB1 != color6 && color5 != colorB3) + product1b = + Q_INTERPOLATE (color6, color5, color5, color5); + else + product1b = INTERPOLATE (color5, color6); + } + + if (color5 == color3 && color2 != color6 && color4 == color5 + && color5 != colorA2) + product2a = INTERPOLATE (color2, color5); + else + if (color5 == color1 && color6 == color5 + && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE (color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 + && color2 != colorB2) + product1a = INTERPOLATE (color2, color5); + else + if (color4 == color2 && color3 == color2 + && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE (color2, color5); + else + product1a = color5; + *(dP) = product1a; + *(dP+1) = product1b; + *(dP + (dstPitch >> 2)) = product2a; + *(dP + (dstPitch >> 2) + 1) = product2b; + + bP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + // deltaPtr += srcPitch; + } // endof: for (; height; height--) +} + +void SuperEagle (u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *dP; + u16 *bP; + u16 *xP; + u32 inc_bP; + +#ifdef MMX + if (cpu_mmx) { + for (; height; height--) { + _2xSaISuperEagleLine (srcPtr, deltaPtr, srcPitch, width, + dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } else +#endif + { + inc_bP = 1; + + u32 Nextline = srcPitch >> 1; + + for (; height; height--) { + bP = (u16 *) srcPtr; + xP = (u16 *) deltaPtr; + dP = dstPtr; + for (u32 finish = width; finish; finish -= inc_bP) { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA1, colorA2, colorB1, colorB2, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + + // -------------------------------------- + if (color2 == color6 && color5 != color3) { + product1b = product2a = color2; + if ((color1 == color2) || (color6 == colorB2)) { + product1a = INTERPOLATE (color2, color5); + product1a = INTERPOLATE (color2, product1a); + // product1a = color2; + } else { + product1a = INTERPOLATE (color5, color6); + } + + if ((color6 == colorS2) || (color2 == colorA1)) { + product2b = INTERPOLATE (color2, color3); + product2b = INTERPOLATE (color2, product2b); + // product2b = color2; + } else { + product2b = INTERPOLATE (color2, color3); + } + } else if (color5 == color3 && color2 != color6) { + product2b = product1a = color5; + + if ((colorB1 == color5) || (color3 == colorS1)) { + product1b = INTERPOLATE (color5, color6); + product1b = INTERPOLATE (color5, product1b); + // product1b = color5; + } else { + product1b = INTERPOLATE (color5, color6); + } + + if ((color3 == colorA2) || (color4 == color5)) { + product2a = INTERPOLATE (color5, color2); + product2a = INTERPOLATE (color5, product2a); + // product2a = color5; + } else { + product2a = INTERPOLATE (color2, color3); + } + + } else if (color5 == color3 && color2 == color6) { + register int r = 0; + + r += GetResult (color6, color5, color1, colorA1); + r += GetResult (color6, color5, color4, colorB1); + r += GetResult (color6, color5, colorA2, colorS1); + r += GetResult (color6, color5, colorB2, colorS2); + + if (r > 0) { + product1b = product2a = color2; + product1a = product2b = INTERPOLATE (color5, color6); + } else if (r < 0) { + product2b = product1a = color5; + product1b = product2a = INTERPOLATE (color5, color6); + } else { + product2b = product1a = color5; + product1b = product2a = color2; + } + } else { + product2b = product1a = INTERPOLATE (color2, color6); + product2b = + Q_INTERPOLATE (color3, color3, color3, product2b); + product1a = + Q_INTERPOLATE (color5, color5, color5, product1a); + + product2a = product1b = INTERPOLATE (color5, color3); + product2a = + Q_INTERPOLATE (color2, color2, color2, product2a); + product1b = + Q_INTERPOLATE (color6, color6, color6, product1b); + + // product1a = color5; + // product1b = color6; + // product2a = color2; + // product2b = color3; + } +#ifdef WORDS_BIGENDIAN + product1a = (product1a << 16) | product1b; + product2a = (product2a << 16) | product2b; +#else + product1a = product1a | (product1b << 16); + product2a = product2a | (product2b << 16); +#endif + + *((u32 *) dP) = product1a; + *((u32 *) (dP + dstPitch)) = product2a; + *xP = color5; + + bP += inc_bP; + xP += inc_bP; + dP += sizeof (u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) + } +} + +void SuperEagle32 (u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dP; + u32 *bP; + u32 *xP; + u32 inc_bP; + + inc_bP = 1; + + u32 Nextline = srcPitch >> 2; + + for (; height; height--) { + bP = (u32 *) srcPtr; + xP = (u32 *) deltaPtr; + dP = (u32 *)dstPtr; + for (u32 finish = width; finish; finish -= inc_bP) { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA1, colorA2, colorB1, colorB2, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + + // -------------------------------------- + if (color2 == color6 && color5 != color3) { + product1b = product2a = color2; + if ((color1 == color2) || (color6 == colorB2)) { + product1a = INTERPOLATE (color2, color5); + product1a = INTERPOLATE (color2, product1a); + // product1a = color2; + } else { + product1a = INTERPOLATE (color5, color6); + } + + if ((color6 == colorS2) || (color2 == colorA1)) { + product2b = INTERPOLATE (color2, color3); + product2b = INTERPOLATE (color2, product2b); + // product2b = color2; + } else { + product2b = INTERPOLATE (color2, color3); + } + } else if (color5 == color3 && color2 != color6) { + product2b = product1a = color5; + + if ((colorB1 == color5) || (color3 == colorS1)) { + product1b = INTERPOLATE (color5, color6); + product1b = INTERPOLATE (color5, product1b); + // product1b = color5; + } else { + product1b = INTERPOLATE (color5, color6); + } + + if ((color3 == colorA2) || (color4 == color5)) { + product2a = INTERPOLATE (color5, color2); + product2a = INTERPOLATE (color5, product2a); + // product2a = color5; + } else { + product2a = INTERPOLATE (color2, color3); + } + + } else if (color5 == color3 && color2 == color6) { + register int r = 0; + + r += GetResult (color6, color5, color1, colorA1); + r += GetResult (color6, color5, color4, colorB1); + r += GetResult (color6, color5, colorA2, colorS1); + r += GetResult (color6, color5, colorB2, colorS2); + + if (r > 0) { + product1b = product2a = color2; + product1a = product2b = INTERPOLATE (color5, color6); + } else if (r < 0) { + product2b = product1a = color5; + product1b = product2a = INTERPOLATE (color5, color6); + } else { + product2b = product1a = color5; + product1b = product2a = color2; + } + } else { + product2b = product1a = INTERPOLATE (color2, color6); + product2b = + Q_INTERPOLATE (color3, color3, color3, product2b); + product1a = + Q_INTERPOLATE (color5, color5, color5, product1a); + + product2a = product1b = INTERPOLATE (color5, color3); + product2a = + Q_INTERPOLATE (color2, color2, color2, product2a); + product1b = + Q_INTERPOLATE (color6, color6, color6, product1b); + + // product1a = color5; + // product1b = color6; + // product2a = color2; + // product2b = color3; + } + *(dP) = product1a; + *(dP+1) = product1b; + *(dP + (dstPitch >> 2)) = product2a; + *(dP + (dstPitch >> 2) +1) = product2b; + *xP = color5; + + bP += inc_bP; + xP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) +} + +void _2xSaI (u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *dP; + u16 *bP; + u32 inc_bP; + +#ifdef MMX + if (cpu_mmx) { + for (; height; height -= 1) { + _2xSaILine (srcPtr, deltaPtr, srcPitch, width, dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } else +#endif + { + inc_bP = 1; + + u32 Nextline = srcPitch >> 1; + + for (; height; height--) { + bP = (u16 *) srcPtr; + dP = dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) { + + register u32 colorA, colorB; + u32 colorC, colorD, + colorE, colorF, colorG, colorH, + colorI, colorJ, colorK, colorL, + + colorM, colorN, colorO, colorP; + u32 product, product1, product2; + + //--------------------------------------- + // Map of the pixels: I|E F|J + // G|A B|K + // H|C D|L + // M|N O|P + colorI = *(bP - Nextline - 1); + colorE = *(bP - Nextline); + colorF = *(bP - Nextline + 1); + colorJ = *(bP - Nextline + 2); + + colorG = *(bP - 1); + colorA = *(bP); + colorB = *(bP + 1); + colorK = *(bP + 2); + + colorH = *(bP + Nextline - 1); + colorC = *(bP + Nextline); + colorD = *(bP + Nextline + 1); + colorL = *(bP + Nextline + 2); + + colorM = *(bP + Nextline + Nextline - 1); + colorN = *(bP + Nextline + Nextline); + colorO = *(bP + Nextline + Nextline + 1); + colorP = *(bP + Nextline + Nextline + 2); + + if ((colorA == colorD) && (colorB != colorC)) { + if (((colorA == colorE) && (colorB == colorL)) || + ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ))) { + product = colorA; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if (((colorA == colorG) && (colorC == colorO)) || + ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM))) { + product1 = colorA; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + product2 = colorA; + } else if ((colorB == colorC) && (colorA != colorD)) { + if (((colorB == colorF) && (colorA == colorH)) || + ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI))) { + product = colorB; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if (((colorC == colorH) && (colorA == colorF)) || + ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI))) { + product1 = colorC; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + product2 = colorB; + } else if ((colorA == colorD) && (colorB == colorC)) { + if (colorA == colorB) { + product = colorA; + product1 = colorA; + product2 = colorA; + } else { + register int r = 0; + + product1 = INTERPOLATE (colorA, colorC); + product = INTERPOLATE (colorA, colorB); + + r += + GetResult1 (colorA, colorB, colorG, colorE, + colorI); + r += + GetResult2 (colorB, colorA, colorK, colorF, + colorJ); + r += + GetResult2 (colorB, colorA, colorH, colorN, + colorM); + r += + GetResult1 (colorA, colorB, colorL, colorO, + colorP); + + if (r > 0) + product2 = colorA; + else if (r < 0) + product2 = colorB; + else { + product2 = + Q_INTERPOLATE (colorA, colorB, colorC, + colorD); + } + } + } else { + product2 = Q_INTERPOLATE (colorA, colorB, colorC, colorD); + + if ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ)) { + product = colorA; + } else if ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI)) { + product = colorB; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM)) { + product1 = colorA; + } else if ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI)) { + product1 = colorC; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + } + +#ifdef WORDS_BIGENDIAN + product = (colorA << 16) | product ; + product1 = (product1 << 16) | product2 ; +#else + product = colorA | (product << 16); + product1 = product1 | (product2 << 16); +#endif + *((s32 *) dP) = product; + *((u32 *) (dP + dstPitch)) = product1; + + bP += inc_bP; + dP += sizeof (u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) + } +} + +void _2xSaI32 (u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dP; + u32 *bP; + u32 inc_bP = 1; + + u32 Nextline = srcPitch >> 2; + + for (; height; height--) { + bP = (u32 *) srcPtr; + dP = (u32 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) { + register u32 colorA, colorB; + u32 colorC, colorD, + colorE, colorF, colorG, colorH, + colorI, colorJ, colorK, colorL, + + colorM, colorN, colorO, colorP; + u32 product, product1, product2; + + //--------------------------------------- + // Map of the pixels: I|E F|J + // G|A B|K + // H|C D|L + // M|N O|P + colorI = *(bP - Nextline - 1); + colorE = *(bP - Nextline); + colorF = *(bP - Nextline + 1); + colorJ = *(bP - Nextline + 2); + + colorG = *(bP - 1); + colorA = *(bP); + colorB = *(bP + 1); + colorK = *(bP + 2); + + colorH = *(bP + Nextline - 1); + colorC = *(bP + Nextline); + colorD = *(bP + Nextline + 1); + colorL = *(bP + Nextline + 2); + + colorM = *(bP + Nextline + Nextline - 1); + colorN = *(bP + Nextline + Nextline); + colorO = *(bP + Nextline + Nextline + 1); + colorP = *(bP + Nextline + Nextline + 2); + + if ((colorA == colorD) && (colorB != colorC)) { + if (((colorA == colorE) && (colorB == colorL)) || + ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ))) { + product = colorA; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if (((colorA == colorG) && (colorC == colorO)) || + ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM))) { + product1 = colorA; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + product2 = colorA; + } else if ((colorB == colorC) && (colorA != colorD)) { + if (((colorB == colorF) && (colorA == colorH)) || + ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI))) { + product = colorB; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if (((colorC == colorH) && (colorA == colorF)) || + ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI))) { + product1 = colorC; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + product2 = colorB; + } else if ((colorA == colorD) && (colorB == colorC)) { + if (colorA == colorB) { + product = colorA; + product1 = colorA; + product2 = colorA; + } else { + register int r = 0; + + product1 = INTERPOLATE (colorA, colorC); + product = INTERPOLATE (colorA, colorB); + + r += + GetResult1 (colorA, colorB, colorG, colorE, + colorI); + r += + GetResult2 (colorB, colorA, colorK, colorF, + colorJ); + r += + GetResult2 (colorB, colorA, colorH, colorN, + colorM); + r += + GetResult1 (colorA, colorB, colorL, colorO, + colorP); + + if (r > 0) + product2 = colorA; + else if (r < 0) + product2 = colorB; + else { + product2 = + Q_INTERPOLATE (colorA, colorB, colorC, + colorD); + } + } + } else { + product2 = Q_INTERPOLATE (colorA, colorB, colorC, colorD); + + if ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ)) { + product = colorA; + } else if ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI)) { + product = colorB; + } else { + product = INTERPOLATE (colorA, colorB); + } + + if ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM)) { + product1 = colorA; + } else if ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI)) { + product1 = colorC; + } else { + product1 = INTERPOLATE (colorA, colorC); + } + } + *(dP) = colorA; + *(dP + 1) = product; + *(dP + (dstPitch >> 2)) = product1; + *(dP + (dstPitch >> 2) + 1) = product2; + + bP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + // deltaPtr += srcPitch; + } // endof: for (height; height; height--) +} + +static u32 Bilinear (u32 A, u32 B, u32 x) +{ + unsigned long areaA, areaB; + unsigned long result; + + if (A == B) + return A; + + areaB = (x >> 11) & 0x1f; // reduce 16 bit fraction to 5 bits + areaA = 0x20 - areaB; + + A = (A & redblueMask) | ((A & greenMask) << 16); + B = (B & redblueMask) | ((B & greenMask) << 16); + + result = ((areaA * A) + (areaB * B)) >> 5; + + return (result & redblueMask) | ((result >> 16) & greenMask); +} + +static u32 Bilinear4 (u32 A, u32 B, u32 C, u32 D, u32 x, + u32 y) +{ + unsigned long areaA, areaB, areaC, areaD; + unsigned long result, xy; + + x = (x >> 11) & 0x1f; + y = (y >> 11) & 0x1f; + xy = (x * y) >> 5; + + A = (A & redblueMask) | ((A & greenMask) << 16); + B = (B & redblueMask) | ((B & greenMask) << 16); + C = (C & redblueMask) | ((C & greenMask) << 16); + D = (D & redblueMask) | ((D & greenMask) << 16); + + areaA = 0x20 + xy - x - y; + areaB = x - xy; + areaC = y - xy; + areaD = xy; + + result = ((areaA * A) + (areaB * B) + (areaC * C) + (areaD * D)) >> 5; + + return (result & redblueMask) | ((result >> 16) & greenMask); +} + +void Scale_2xSaI (u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, + u32 dstWidth, u32 dstHeight, int width, int height) +{ + u8 *dP; + u16 *bP; + + u32 w; + u32 h; + u32 dw; + u32 dh; + u32 hfinish; + u32 wfinish; + + u32 Nextline = srcPitch >> 1; + + wfinish = (width - 1) << 16; // convert to fixed point + dw = wfinish / (dstWidth - 1); + hfinish = (height - 1) << 16; // convert to fixed point + dh = hfinish / (dstHeight - 1); + + for (h = 0; h < hfinish; h += dh) { + u32 y1, y2; + + y1 = h & 0xffff; // fraction part of fixed point + bP = (u16 *) (srcPtr + ((h >> 16) * srcPitch)); + dP = dstPtr; + y2 = 0x10000 - y1; + + w = 0; + + for (; w < wfinish;) { + u32 A, B, C, D; + u32 E, F, G, H; + u32 I, J, K, L; + u32 x1, x2, a1, f1, f2; + u32 position, product1; + + position = w >> 16; + A = bP[position]; // current pixel + B = bP[position + 1]; // next pixel + C = bP[position + Nextline]; + D = bP[position + Nextline + 1]; + E = bP[position - Nextline]; + F = bP[position - Nextline + 1]; + G = bP[position - 1]; + H = bP[position + Nextline - 1]; + I = bP[position + 2]; + J = bP[position + Nextline + 2]; + K = bP[position + Nextline + Nextline]; + L = bP[position + Nextline + Nextline + 1]; + + x1 = w & 0xffff; // fraction part of fixed point + x2 = 0x10000 - x1; + + /*0*/ + if (A == B && C == D && A == C) + product1 = A; + else /*1*/ if (A == D && B != C) { + f1 = (x1 >> 1) + (0x10000 >> 2); + f2 = (y1 >> 1) + (0x10000 >> 2); + if (y1 <= f1 && A == J && A != E) // close to B + { + a1 = f1 - y1; + product1 = Bilinear (A, B, a1); + } else if (y1 >= f1 && A == G && A != L) // close to C + { + a1 = y1 - f1; + product1 = Bilinear (A, C, a1); + } + else if (x1 >= f2 && A == E && A != J) // close to B + { + a1 = x1 - f2; + product1 = Bilinear (A, B, a1); + } + else if (x1 <= f2 && A == L && A != G) // close to C + { + a1 = f2 - x1; + product1 = Bilinear (A, C, a1); + } + else if (y1 >= x1) // close to C + { + a1 = y1 - x1; + product1 = Bilinear (A, C, a1); + } + else if (y1 <= x1) // close to B + { + a1 = x1 - y1; + product1 = Bilinear (A, B, a1); + } + } + else + /*2*/ + if (B == C && A != D) + { + f1 = (x1 >> 1) + (0x10000 >> 2); + f2 = (y1 >> 1) + (0x10000 >> 2); + if (y2 >= f1 && B == H && B != F) // close to A + { + a1 = y2 - f1; + product1 = Bilinear (B, A, a1); + } + else if (y2 <= f1 && B == I && B != K) // close to D + { + a1 = f1 - y2; + product1 = Bilinear (B, D, a1); + } + else if (x2 >= f2 && B == F && B != H) // close to A + { + a1 = x2 - f2; + product1 = Bilinear (B, A, a1); + } + else if (x2 <= f2 && B == K && B != I) // close to D + { + a1 = f2 - x2; + product1 = Bilinear (B, D, a1); + } + else if (y2 >= x1) // close to A + { + a1 = y2 - x1; + product1 = Bilinear (B, A, a1); + } + else if (y2 <= x1) // close to D + { + a1 = x1 - y2; + product1 = Bilinear (B, D, a1); + } + } + /*3*/ + else + { + product1 = Bilinear4 (A, B, C, D, x1, y1); + } + + //end First Pixel + *(u32 *) dP = product1; + dP += 2; + w += dw; + } + dstPtr += dstPitch; + } +} + diff --git a/src/2xSaImmx.asm b/src/2xSaImmx.asm new file mode 100644 index 00000000..6bef8106 --- /dev/null +++ b/src/2xSaImmx.asm @@ -0,0 +1,2109 @@ +;/*---------------------------------------------------------------------* +; * The following (piece of) code, (part of) the 2xSaI engine, * +; * copyright (c) 1999 - 2001 by Derek Liauw Kie Fa. * +; * Non-Commercial use of this software is allowed and is encouraged, * +; * provided that appropriate credit be given. * +; * You may freely modify this code, but I request * +; * that any improvements to the engine be submitted to me, so * +; * that I can implement these improvements in newer versions of * +; * the software. * +; * If you need more information, have any comments or suggestions, * +; * you can e-mail me. My e-mail: derek-liauw@usa.net. * +; *---------------------------------------------------------------------*/ + +;---------------------- +; 2xSaI version 0.59 WIP, soon to become version 0.60 +;---------------------- + +;%define FAR_POINTER + + + + BITS 32 +%ifdef __DJGPP__ + GLOBAL __2xSaILine + GLOBAL __2xSaISuperEagleLine + GLOBAL __2xSaISuper2xSaILine + GLOBAL _Init_2xSaIMMX +%else + GLOBAL _2xSaILine + GLOBAL _2xSaISuperEagleLine + GLOBAL _2xSaISuper2xSaILine + GLOBAL Init_2xSaIMMX +%endif + SECTION .text ALIGN = 32 + +%ifdef FAR_POINTER +;EXTERN_C void _2xSaILine (uint8 *srcPtr, uint32 srcPitch, uint32 width, +; uint8 *dstPtr, uint32 dstPitch, uint16 dstSegment); +%else +;EXTERN_C void _2xSaILine (uint8 *srcPtr, uint32 srcPitch, uint32 width, +; uint8 *dstPtr, uint32 dstPitch); +%endif + +srcPtr equ 8 +deltaPtr equ 12 +srcPitch equ 16 +width equ 20 +dstOffset equ 24 +dstPitch equ 28 +dstSegment equ 32 + + + + +colorB0 equ -2 +colorB1 equ 0 +colorB2 equ 2 +colorB3 equ 4 + +color7 equ -2 +color8 equ 0 +color9 equ 2 + +color4 equ -2 +color5 equ 0 +color6 equ 2 +colorS2 equ 4 + +color1 equ -2 +color2 equ 0 +color3 equ 2 +colorS1 equ 4 + +colorA0 equ -2 +colorA1 equ 0 +colorA2 equ 2 +colorA3 equ 4 + + + + +%ifdef __DJGPP__ +__2xSaISuper2xSaILine: +%else +_2xSaISuper2xSaILine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] ;eax points to colorA + mov ebx, [ebp+srcPitch] ;ebx contains the source pitch + mov ecx, [ebp+width] ;ecx contains the number of pixels to process + ; eax now points to colorB1 + sub eax, ebx ;eax points to B1 which is the base + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + + ;load source img + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB3] + movq mm2, [eax+ebx+color4] + movq mm3, [eax+ebx+colorS2] + movq mm4, [eax+ebx+ebx+color1] + movq mm5, [eax+ebx+ebx+colorS1] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA0] + movq mm7, [eax+ebx+ebx+colorA3] + pop eax + + ;compare to delta + pcmpeqw mm0, [ecx+2+colorB0] + pcmpeqw mm1, [ecx+2+colorB3] + pcmpeqw mm2, [ecx+ebx+2+color4] + pcmpeqw mm3, [ecx+ebx+2+colorS2] + pcmpeqw mm4, [ecx+ebx+ebx+2+color1] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorS1] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorA0] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorA3] + sub ecx, ebx + + + ;compose results + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorB0] + pcmpeqw mm7, mm0 ;did any compare give us a zero ? + + movq [ecx+2+colorB0], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS ;no, so we can skip + + ;End Delta + + ;--------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I56Pixel], mm0 + movq mm7, mm0 + + ;------------------- + movq mm0, mm7 + movq mm1, mm4 ;5,5,5,6 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I5556Pixel], mm0 + ;-------------------- + + movq mm0, mm7 + movq mm1, mm5 ;6,6,6,5 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I5666Pixel], mm0 + + ;------------------------- + ;------------------------- + movq mm0, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color3] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I23Pixel], mm0 + movq mm7, mm0 + + ;--------------------- + movq mm0, mm7 + movq mm1, mm4 ;2,2,2,3 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I2223Pixel], mm0 + + ;---------------------- + movq mm0, mm7 + movq mm1, mm5 ;3,3,3,2 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I2333Pixel], mm0 + + + ;-------------------- +;//////////////////////////////// +; Decide which "branch" to take +;-------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm6, mm0 + movq mm7, mm1 + pcmpeqw mm0, [eax+ebx+ebx+color3] + pcmpeqw mm1, [eax+ebx+ebx+color2] + pcmpeqw mm6, mm7 + + movq mm2, mm0 + movq mm3, mm0 + + pand mm0, mm1 ;colorA == colorD && colorB == colorC + pxor mm7, mm7 + + pcmpeqw mm2, mm7 + pand mm6, mm0 + pand mm2, mm1 ;colorA != colorD && colorB == colorC + + pcmpeqw mm1, mm7 + + pand mm1, mm3 ;colorA == colorD && colorB != colorC + pxor mm0, mm6 + por mm1, mm6 + movq mm7, mm0 + movq [Mask26], mm2 + packsswb mm7, mm7 + movq [Mask35], mm1 + + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- + movq mm6, mm0 + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorE] + movq mm1, [eax+ebx+colorG] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorF] + movq mm1, [eax+ebx+colorK] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+colorH] + movq mm1, [eax+ebx+ebx+colorN] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorL] + movq mm1, [eax+ebx+ebx+colorO] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask35] + por mm0, [Mask26] + movq [Mask35], mm7 + movq [Mask26], mm0 + +.SKIP_GUESS: + + ;Start the ASSEMBLY !!! eh... compose all the results together to form the final image... + + + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+ebx+color2] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + ;--------------------------- + + + +%ifdef dfhsdfhsdahdsfhdsfh + + if (color5 == color3 && color2 != color6 && color4 == color5 && color5 != colorA2) + product2a = INTERPOLATE (color2, color5); + else + if (color5 == color1 && color6 == color5 && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE(color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 && color2 != colorB2) + product1a = INTERPOLATE (color2, color5); + else + if (color4 == color2 && color3 == color2 && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE(color2, color5); + else + product1a = color5; + +%endif + + + movq mm7, [Mask26] + movq mm6, [eax+colorB2] + movq mm5, [eax+ebx+ebx+color2] + movq mm4, [eax+ebx+ebx+color1] + pcmpeqw mm4, mm5 + pcmpeqw mm6, mm5 + pxor mm5, mm5 + pand mm7, mm4 + pcmpeqw mm6, mm5 + pand mm7, mm6 + + + + movq mm6, [eax+ebx+ebx+color3] + movq mm5, [eax+ebx+ebx+color2] + movq mm4, [eax+ebx+ebx+color1] + movq mm2, [eax+ebx+color5] + movq mm1, [eax+ebx+color4] + movq mm3, [eax+colorB0] + + pcmpeqw mm2, mm4 + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm3, mm5 + pxor mm5, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm6, mm1 + pand mm2, mm3 + pand mm6, mm2 + por mm7, mm6 + + + movq mm6, mm7 + pcmpeqw mm6, mm5 + pand mm7, mm0 + + movq mm1, [eax+ebx+color5] + pand mm6, mm1 + por mm7, mm6 + movq [final1a], mm7 ;finished 1a + + + + ;-------------------------------- + + movq mm7, [Mask35] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA2] + pop eax + movq mm5, [eax+ebx+color5] + movq mm4, [eax+ebx+color4] + pcmpeqw mm4, mm5 + pcmpeqw mm6, mm5 + pxor mm5, mm5 + pand mm7, mm4 + pcmpeqw mm6, mm5 + pand mm7, mm6 + + + + movq mm6, [eax+ebx+color6] + movq mm5, [eax+ebx+color5] + movq mm4, [eax+ebx+color4] + movq mm2, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color1] + push eax + add eax, ebx + movq mm3, [eax+ebx+ebx+colorA0] + pop eax + + pcmpeqw mm2, mm4 + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm3, mm5 + pxor mm5, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm6, mm1 + pand mm2, mm3 + pand mm6, mm2 + por mm7, mm6 + + + movq mm6, mm7 + pcmpeqw mm6, mm5 + pand mm7, mm0 + + movq mm1, [eax+ebx+ebx+color2] + pand mm6, mm1 + por mm7, mm6 + movq [final2a], mm7 ;finished 2a + + + ;-------------------------------------------- + + +%ifdef dfhsdfhsdahdsfhdsfh + if (color6 == color3 && color3 == colorA1 && color2 != colorA2 && color3 != colorA0) + product2b = Q_INTERPOLATE (color3, color3, color3, color2); + else + if (color5 == color2 && color2 == colorA2 && colorA1 != color3 && color2 != colorA3) + product2b = Q_INTERPOLATE (color2, color2, color2, color3); + else + product2b = INTERPOLATE (color2, color3); + + if (color6 == color3 && color6 == colorB1 && color5 != colorB2 && color6 != colorB0) + product1b = Q_INTERPOLATE (color6, color6, color6, color5); + else + if (color5 == color2 && color5 == colorB2 && colorB1 != color6 && color5 != colorB3) + product1b = Q_INTERPOLATE (color6, color5, color5, color5); + else + product1b = INTERPOLATE (color5, color6); +%endif + + push eax + add eax, ebx + pxor mm7, mm7 + movq mm0, [eax+ebx+ebx+colorA0] + movq mm1, [eax+ebx+ebx+colorA1] + movq mm2, [eax+ebx+ebx+colorA2] + movq mm3, [eax+ebx+ebx+colorA3] + pop eax + movq mm4, [eax+ebx+ebx+color2] + movq mm5, [eax+ebx+ebx+color3] + movq mm6, [eax+ebx+color6] + + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm4, mm2 + pcmpeqw mm0, mm5 + pcmpeqw mm4, mm7 + pcmpeqw mm0, mm7 + pand mm0, mm4 + pand mm6, mm1 + pand mm0, mm6 + + + push eax + add eax, ebx + movq mm1, [eax+ebx+ebx+colorA1] + pop eax + movq mm4, [eax+ebx+ebx+color2] + movq mm5, [eax+ebx+color5] + movq mm6, [eax+ebx+ebx+color3] + + pcmpeqw mm5, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm1, mm6 + pcmpeqw mm3, mm4 + pcmpeqw mm1, mm7 + pcmpeqw mm3, mm7 + pand mm2, mm5 + pand mm1, mm3 + pand mm1, mm2 + + + movq mm7, mm0 + por mm7, mm1 + + movq mm4, [Mask35] + movq mm3, [Mask26] + + movq mm6, mm4 + pand mm6, mm7 + pxor mm4, mm6 + + movq mm6, mm3 + pand mm6, mm7 + pxor mm3, mm6 + + movq mm2, mm0 + movq mm7, [I2333Pixel] + movq mm6, [I2223Pixel] + movq mm5, [I23Pixel] + + + por mm2, mm4 + pand mm4, [eax+ebx+ebx+color3] + por mm2, mm3 + pand mm3, [eax+ebx+ebx+color2] + por mm2, mm1 + pand mm0, mm7 + pand mm1, mm6 + pxor mm7, mm7 + pcmpeqw mm2, mm7 + por mm0, mm1 + por mm3, mm4 + pand mm2, mm5 + por mm0, mm3 + por mm0, mm2 + movq [final2b], mm0 + + ;----------------------------------- + + + pxor mm7, mm7 + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB1] + movq mm2, [eax+colorB2] + movq mm3, [eax+colorB3] + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + movq mm6, [eax+ebx+ebx+color3] + + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm4, mm2 + pcmpeqw mm0, mm5 + pcmpeqw mm4, mm7 + pcmpeqw mm0, mm7 + pand mm0, mm4 + pand mm6, mm1 + pand mm0, mm6 + + movq mm1, [eax+colorB1] + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+ebx+color2] + movq mm6, [eax+ebx+color6] + + pcmpeqw mm5, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm1, mm6 + pcmpeqw mm3, mm4 + pcmpeqw mm1, mm7 + pcmpeqw mm3, mm7 + pand mm2, mm5 + pand mm1, mm3 + pand mm1, mm2 + + + movq mm7, mm0 + por mm7, mm1 + + movq mm4, [Mask35] + movq mm3, [Mask26] + + movq mm6, mm4 + pand mm6, mm7 + pxor mm4, mm6 + + movq mm6, mm3 + pand mm6, mm7 + pxor mm3, mm6 + + movq mm2, mm0 + movq mm7, [I5666Pixel] + movq mm6, [I5556Pixel] + movq mm5, [I56Pixel] + + + por mm2, mm4 + pand mm4, [eax+ebx+color5] + por mm2, mm3 + pand mm3, [eax+ebx+color6] + por mm2, mm1 + pand mm0, mm7 + pand mm1, mm6 + pxor mm7, mm7 + pcmpeqw mm2, mm7 + por mm0, mm1 + por mm3, mm4 + pand mm2, mm5 + por mm0, mm3 + por mm0, mm2 + movq [final1b], mm0 + + ;--------- + + movq mm0, [final1a] + movq mm4, [final2a] + movq mm2, [final1b] + movq mm6, [final2b] + + + movq mm1, mm0 + movq mm5, mm4 + + + punpcklwd mm0, mm2 + punpckhwd mm1, mm2 + + punpcklwd mm4, mm6 + punpckhwd mm5, mm6 + + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [fs:edx], mm4 + movq [fs:edx+8], mm5 + pop edx +%else + movq [edx], mm0 + movq [edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [edx], mm4 + movq [edx+8], mm5 + pop edx +%endif +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + + +%ifdef __DJGPP__ +__2xSaISuperEagleLine: +%else +_2xSaISuperEagleLine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] + mov ebx, [ebp+srcPitch] + mov ecx, [ebp+width] + ; eax now points to colorB1 + sub eax, ebx + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB3] + movq mm2, [eax+ebx+color4] + movq mm3, [eax+ebx+colorS2] + movq mm4, [eax+ebx+ebx+color1] + movq mm5, [eax+ebx+ebx+colorS1] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA0] + movq mm7, [eax+ebx+ebx+colorA3] + pop eax + + pcmpeqw mm0, [ecx+2+colorB0] + pcmpeqw mm1, [ecx+2+colorB3] + pcmpeqw mm2, [ecx+ebx+2+color4] + pcmpeqw mm3, [ecx+ebx+2+colorS2] + pcmpeqw mm4, [ecx+ebx+ebx+2+color1] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorS1] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorA0] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorA3] + sub ecx, ebx + + + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorB0] + pcmpeqw mm7, mm0 + + movq [ecx+2+colorB0], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS + + ;End Delta + + ;--------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I56Pixel], mm0 + movq mm7, mm0 + + ;------------------- + movq mm0, mm7 + movq mm1, mm4 ;5,5,5,6 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [product1a], mm0 + ;-------------------- + + movq mm0, mm7 + movq mm1, mm5 ;6,6,6,5 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product1b], mm0 + + ;------------------------- + ;------------------------- + movq mm0, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color3] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I23Pixel], mm0 + movq mm7, mm0 + + ;--------------------- + movq mm0, mm7 + movq mm1, mm4 ;2,2,2,3 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product2a], mm0 + + ;---------------------- + movq mm0, mm7 + movq mm1, mm5 ;3,3,3,2 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product2b], mm0 + + + ;//////////////////////////////// + ; Decide which "branch" to take + ;-------------------------------- + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + movq mm6, [eax+ebx+ebx+color3] + movq mm7, [eax+ebx+ebx+color2] + + pxor mm3, mm3 + movq mm0, mm4 + movq mm1, mm5 + + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pcmpeqw mm1, mm3 + pand mm0, mm1 + movq [Mask35], mm0 + + movq mm0, [eax+ebx+ebx+colorS1] + movq mm1, [eax+ebx+color4] + push eax + add eax, ebx + movq mm2, [eax+ebx+ebx+colorA2] + pop eax + movq mm3, [eax+colorB1] + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm3, mm4 + pand mm0, mm1 + pand mm2, mm3 + por mm0, mm2 + pand mm0, [Mask35] + movq [Mask35b], mm0 + + ;----------- + pxor mm3, mm3 + movq mm0, mm4 + movq mm1, mm5 + + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pcmpeqw mm0, mm3 + pand mm0, mm1 + movq [Mask26], mm0 + + movq mm0, [eax+ebx+ebx+color1] + movq mm1, [eax+ebx+colorS2] + push eax + add eax, ebx + movq mm2, [eax+ebx+ebx+colorA1] + pop eax + movq mm3, [eax+colorB2] + pcmpeqw mm0, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm1 + pand mm2, mm3 + por mm0, mm2 + pand mm0, [Mask26] + movq [Mask26b], mm0 + + ;-------------------- + movq mm0, mm4 + movq mm1, mm5 + movq mm2, mm0 + + pcmpeqw mm2, mm1 + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pand mm0, mm1 + pand mm2, mm0 + pxor mm0, mm2 + movq mm7, mm0 + + ;------------------ + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- +; Map of the pixels: I|E F|J +; G|A B|K +; H|C D|L +; M|N O|P + movq mm6, mm0 + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorB1] + movq mm1, [eax+ebx+color4] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorB2] + movq mm1, [eax+ebx+colorS2] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+color1] + movq mm1, [eax+ebx+ebx+colorA1] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorS1] + movq mm1, [eax+ebx+ebx+colorA2] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask35] + por mm0, [Mask26] + movq [Mask35], mm7 + movq [Mask26], mm0 + +.SKIP_GUESS: + ;Start the ASSEMBLY !!! + + movq mm4, [Mask35] + movq mm5, [Mask26] + movq mm6, [Mask35b] + movq mm7, [Mask26b] + + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, [eax+ebx+ebx+color2] + movq mm3, [eax+ebx+ebx+color3] + pcmpeqw mm0, mm2 + pcmpeqw mm1, mm3 + movq mm2, mm4 + movq mm3, mm5 + por mm0, mm1 + por mm2, mm3 + pand mm2, mm0 + pxor mm0, mm2 + movq mm3, mm0 + + movq mm2, mm0 + pxor mm0, mm0 + por mm2, mm4 + pxor mm4, mm6 + por mm2, mm5 + pxor mm5, mm7 + pcmpeqw mm2, mm0 + ;---------------- + + movq mm0, [eax+ebx+color5] + movq mm1, mm3 + por mm1, mm4 + por mm1, mm6 + pand mm0, mm1 + movq mm1, mm5 + pand mm1, [I56Pixel] + por mm0, mm1 + movq mm1, mm7 + pand mm1, [product1b] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product1a] + por mm0, mm1 + movq [final1a], mm0 + + movq mm0, [eax+ebx+color6] + movq mm1, mm3 + por mm1, mm5 + por mm1, mm7 + pand mm0, mm1 + movq mm1, mm4 + pand mm1, [I56Pixel] + por mm0, mm1 + movq mm1, mm6 + pand mm1, [product1a] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product1b] + por mm0, mm1 + movq [final1b], mm0 + + movq mm0, [eax+ebx+ebx+color2] + movq mm1, mm3 + por mm1, mm5 + por mm1, mm7 + pand mm0, mm1 + movq mm1, mm4 + pand mm1, [I23Pixel] + por mm0, mm1 + movq mm1, mm6 + pand mm1, [product2b] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product2a] + por mm0, mm1 + movq [final2a], mm0 + + movq mm0, [eax+ebx+ebx+color3] + movq mm1, mm3 + por mm1, mm4 + por mm1, mm6 + pand mm0, mm1 + movq mm1, mm5 + pand mm1, [I23Pixel] + por mm0, mm1 + movq mm1, mm7 + pand mm1, [product2a] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product2b] + por mm0, mm1 + movq [final2b], mm0 + + + movq mm0, [final1a] + movq mm2, [final1b] + movq mm1, mm0 + movq mm4, [final2a] + movq mm6, [final2b] + movq mm5, mm4 + punpcklwd mm0, mm2 + punpckhwd mm1, mm2 + punpcklwd mm4, mm6 + punpckhwd mm5, mm6 + + + + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [fs:edx], mm4 + movq [fs:edx+8], mm5 + pop edx +%else + movq [edx], mm0 + movq [edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [edx], mm4 + movq [edx+8], mm5 + pop edx +%endif +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + +;This is version 0.50 +colorI equ -2 +colorE equ 0 +colorF equ 2 +colorJ equ 4 + +colorG equ -2 +colorA equ 0 +colorB equ 2 +colorK equ 4 + +colorH equ -2 +colorC equ 0 +colorD equ 2 +colorL equ 4 + +colorM equ -2 +colorN equ 0 +colorO equ 2 +colorP equ 4 + +%ifdef __DJGPP__ +__2xSaILine: +%else +_2xSaILine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] + mov ebx, [ebp+srcPitch] + mov ecx, [ebp+width] + ; eax now points to colorE + sub eax, ebx + + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + movq mm0, [eax+colorI] + movq mm1, [eax+colorJ] + movq mm2, [eax+ebx+colorG] + movq mm3, [eax+ebx+colorK] + movq mm4, [eax+ebx+ebx+colorH] + movq mm5, [eax+ebx+ebx+colorL] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorM] + movq mm7, [eax+ebx+ebx+colorP] + pop eax + + pcmpeqw mm0, [ecx+2+colorI] + pcmpeqw mm1, [ecx+2+colorK] + pcmpeqw mm2, [ecx+ebx+2+colorG] + pcmpeqw mm3, [ecx+ebx+2+colorK] + pcmpeqw mm4, [ecx+ebx+ebx+2+colorH] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorL] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorM] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorP] + sub ecx, ebx + + + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorI] + pcmpeqw mm7, mm0 + + movq [ecx+2+colorI], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS + + ;End Delta + + ;--------------------------------- + + +;1 + ;if ((colorA == colorD) && (colorB != colorC) && (colorA == colorE) && (colorB == colorL) + movq mm0, [eax+ebx+colorA] ;mm0 and mm1 contain colorA + movq mm2, [eax+ebx+colorB] ;mm2 and mm3 contain colorB + + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+ebx+colorD] + pcmpeqw mm1, [eax+colorE] + pcmpeqw mm2, [eax+ebx+ebx+colorL] + pcmpeqw mm3, [eax+ebx+ebx+colorC] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorA == colorC) && (colorB != colorE) && (colorA == colorF) && (colorB == colorJ) + movq mm4, [eax+ebx+colorA] ;mm4 and mm5 contain colorA + movq mm6, [eax+ebx+colorB] ;mm6 and mm7 contain colorB + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorC] + pcmpeqw mm5, [eax+colorF] + pcmpeqw mm6, [eax+colorJ] + pcmpeqw mm7, [eax+colorE] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask1], mm0 + + ;-------------------------------------------- + +;2 + ;if ((colorB == colorC) && (colorA != colorD) && (colorB == colorF) && (colorA == colorH) + movq mm0, [eax+ebx+colorB] ;mm0 and mm1 contain colorB + movq mm2, [eax+ebx+colorA] ;mm2 and mm3 contain colorA + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+ebx+colorC] + pcmpeqw mm1, [eax+colorF] + pcmpeqw mm2, [eax+ebx+ebx+colorH] + pcmpeqw mm3, [eax+ebx+ebx+colorD] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorB == colorE) && (colorB == colorD) && (colorA != colorF) && (colorA == colorI) + movq mm4, [eax+ebx+colorB] ;mm4 and mm5 contain colorB + movq mm6, [eax+ebx+colorA] ;mm6 and mm7 contain colorA + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorD] + pcmpeqw mm5, [eax+colorE] + pcmpeqw mm6, [eax+colorI] + pcmpeqw mm7, [eax+colorF] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask2], mm0 + + +;interpolate colorA and colorB + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + + ;assemble the pixels + movq mm1, [eax+ebx+colorA] + movq mm2, [eax+ebx+colorB] + + movq mm3, [Mask1] + movq mm5, mm1 + movq mm4, [Mask2] + movq mm6, mm1 + + pand mm1, mm3 + por mm3, mm4 + pxor mm7, mm7 + pand mm2, mm4 + + pcmpeqw mm3, mm7 + por mm1, mm2 + pand mm0, mm3 + + por mm0, mm1 + + punpcklwd mm5, mm0 + punpckhwd mm6, mm0 + +%ifdef FAR_POINTER + movq [fs:edx], mm5 + movq [fs:edx+8], mm6 +%else + movq [edx], mm5 + movq [edx+8], mm6 +%endif + +;------------------------------------------------ +; Create the Nextline +;------------------------------------------------ +;3 ;if ((colorA == colorD) && (colorB != colorC) && (colorA == colorG) && (colorC == colorO) + movq mm0, [eax+ebx+colorA] ;mm0 and mm1 contain colorA + movq mm2, [eax+ebx+ebx+colorC] ;mm2 and mm3 contain colorC + movq mm1, mm0 + movq mm3, mm2 + + push eax + add eax, ebx + pcmpeqw mm0, [eax+ebx+colorD] + pcmpeqw mm1, [eax+colorG] + pcmpeqw mm2, [eax+ebx+ebx+colorO] + pcmpeqw mm3, [eax+colorB] + pop eax + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorA == colorB) && (colorG != colorC) && (colorA == colorH) && (colorC == colorM) + movq mm4, [eax+ebx+colorA] ;mm4 and mm5 contain colorA + movq mm6, [eax+ebx+ebx+colorC] ;mm6 and mm7 contain colorC + movq mm5, mm4 + movq mm7, mm6 + + push eax + add eax, ebx + pcmpeqw mm4, [eax+ebx+colorH] + pcmpeqw mm5, [eax+colorB] + pcmpeqw mm6, [eax+ebx+ebx+colorM] + pcmpeqw mm7, [eax+colorG] + pop eax + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask1], mm0 + ;-------------------------------------------- + +;4 + ;if ((colorB == colorC) && (colorA != colorD) && (colorC == colorH) && (colorA == colorF) + movq mm0, [eax+ebx+ebx+colorC] ;mm0 and mm1 contain colorC + movq mm2, [eax+ebx+colorA] ;mm2 and mm3 contain colorA + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+colorB] + pcmpeqw mm1, [eax+ebx+ebx+colorH] + pcmpeqw mm2, [eax+colorF] + pcmpeqw mm3, [eax+ebx+ebx+colorD] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorC == colorG) && (colorC == colorD) && (colorA != colorH) && (colorA == colorI) + movq mm4, [eax+ebx+ebx+colorC] ;mm4 and mm5 contain colorC + movq mm6, [eax+ebx+colorA] ;mm6 and mm7 contain colorA + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorD] + pcmpeqw mm5, [eax+ebx+colorG] + pcmpeqw mm6, [eax+colorI] + pcmpeqw mm7, [eax+ebx+ebx+colorH] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask2], mm0 + ;---------------------------------------------- + +;interpolate colorA and colorC + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+ebx+colorC] + + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + ;------------- + + ;assemble the pixels + movq mm1, [eax+ebx+colorA] + movq mm2, [eax+ebx+ebx+colorC] + + movq mm3, [Mask1] + movq mm4, [Mask2] + + pand mm1, mm3 + pand mm2, mm4 + + por mm3, mm4 + pxor mm7, mm7 + por mm1, mm2 + + pcmpeqw mm3, mm7 + pand mm0, mm3 + por mm0, mm1 + movq [ACPixel], mm0 + +;//////////////////////////////// +; Decide which "branch" to take +;-------------------------------- + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + movq mm6, mm0 + movq mm7, mm1 + pcmpeqw mm0, [eax+ebx+ebx+colorD] + pcmpeqw mm1, [eax+ebx+ebx+colorC] + pcmpeqw mm6, mm7 + + movq mm2, mm0 + movq mm3, mm0 + + pand mm0, mm1 ;colorA == colorD && colorB == colorC + pxor mm7, mm7 + + pcmpeqw mm2, mm7 + pand mm6, mm0 + pand mm2, mm1 ;colorA != colorD && colorB == colorC + + pcmpeqw mm1, mm7 + + pand mm1, mm3 ;colorA == colorD && colorB != colorC + pxor mm0, mm6 + por mm1, mm6 + movq mm7, mm0 + movq [Mask2], mm2 + packsswb mm7, mm7 + movq [Mask1], mm1 + + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- +; Map of the pixels: I|E F|J +; G|A B|K +; H|C D|L +; M|N O|P + movq mm6, mm0 + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorE] + movq mm1, [eax+ebx+colorG] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorF] + movq mm1, [eax+ebx+colorK] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+colorH] + movq mm1, [eax+ebx+ebx+colorN] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorL] + movq mm1, [eax+ebx+ebx+colorO] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask1] + por mm0, [Mask2] + movq [Mask1], mm7 + movq [Mask2], mm0 + +.SKIP_GUESS: + ;---------------------------- + ;interpolate A, B, C and D + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + movq mm4, mm0 + movq mm2, [eax+ebx+ebx+colorC] + movq mm5, mm1 + movq mm3, [qcolorMask] + movq mm6, mm2 + movq mm7, [qlowpixelMask] + + pand mm0, mm3 + pand mm1, mm3 + pand mm2, mm3 + pand mm3, [eax+ebx+ebx+colorD] + + psrlw mm0, 2 + pand mm4, mm7 + psrlw mm1, 2 + pand mm5, mm7 + psrlw mm2, 2 + pand mm6, mm7 + psrlw mm3, 2 + pand mm7, [eax+ebx+ebx+colorD] + + paddw mm0, mm1 + paddw mm2, mm3 + + paddw mm4, mm5 + paddw mm6, mm7 + + paddw mm4, mm6 + paddw mm0, mm2 + psrlw mm4, 2 + pand mm4, [qlowpixelMask] + paddw mm0, mm4 ;mm0 contains the interpolated value of A, B, C and D + +;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ + ;assemble the pixels + movq mm1, [Mask1] + movq mm2, [Mask2] + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pand mm4, mm1 + pand mm5, mm2 + + pxor mm7, mm7 + por mm1, mm2 + por mm4, mm5 + pcmpeqw mm1, mm7 + pand mm0, mm1 + por mm4, mm0 ;mm4 contains the diagonal pixels + + movq mm0, [ACPixel] + movq mm1, mm0 + punpcklwd mm0, mm4 + punpckhwd mm1, mm4 + + push edx + add edx, [ebp+dstPitch] + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 +%else + movq [edx], mm0 + movq [edx+8], mm1 +%endif + pop edx + +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + +%ifdef __DJGPP__ +_Init_2xSaIMMX: +%else +Init_2xSaIMMX: +%endif +; Store some stuff + push ebp + mov ebp, esp + push edx + + +;Damn thing doesn't work +; mov eax,1 +; cpuid +; test edx, 0x00800000 ;test bit 23 +; jz end2 ;bit not set => no MMX detected + + mov eax, [ebp+8] ;PixelFormat + cmp eax, 555 + jz Bits555 + cmp eax, 565 + jz Bits565 +end2: + mov eax, 1 + jmp end3 +Bits555: + mov edx, 0x7BDE7BDE + mov eax, colorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x04210421 + mov eax, lowPixelMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x739C739C + mov eax, qcolorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x0C630C63 + mov eax, qlowpixelMask + mov [eax], edx + mov [eax+4], edx + mov eax, 0 + jmp end3 +Bits565: + mov edx, 0xF7DEF7DE + mov eax, colorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x08210821 + mov eax, lowPixelMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0xE79CE79C + mov eax, qcolorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x18631863 + mov eax, qlowpixelMask + mov [eax], edx + mov [eax+4], edx + mov eax, 0 + jmp end3 +end3: + pop edx + mov esp, ebp + pop ebp + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + SECTION .data ALIGN = 32 +;Some constants +colorMask dd 0xF7DEF7DE,0xF7DEF7DE +lowPixelMask dd 0x08210821,0x08210821 + +qcolorMask dd 0xE79CE79C,0xE79CE79C +qlowpixelMask dd 0x18631863,0x18631863 + +darkenMask dd 0xC718C718,0xC718C718 +GreenMask dd 0x07E007E0,0x07E007E0 +RedBlueMask dd 0xF81FF81F,0xF81FF81F + +FALSE dd 0x00000000,0x00000000 +TRUE dd 0xffffffff,0xffffffff +ONE dd 0x00010001,0x00010001 + + + SECTION .bss ALIGN = 32 +ACPixel resb 8 +Mask1 resb 8 +Mask2 resb 8 + +I56Pixel resb 8 +I23Pixel resb 8 +I5556Pixel resb 8 +I2223Pixel resb 8 +I5666Pixel resb 8 +I2333Pixel resb 8 +Mask26 resb 8 +Mask35 resb 8 +Mask26b resb 8 +Mask35b resb 8 +product1a resb 8 +product1b resb 8 +product2a resb 8 +product2b resb 8 +final1a resb 8 +final1b resb 8 +final2a resb 8 +final2b resb 8 diff --git a/src/CheatSearch.cpp b/src/CheatSearch.cpp new file mode 100644 index 00000000..ca344ab4 --- /dev/null +++ b/src/CheatSearch.cpp @@ -0,0 +1,329 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include + +#include "CheatSearch.h" + +CheatSearchBlock cheatSearchBlocks[4]; + +CheatSearchData cheatSearchData = { + 0, + cheatSearchBlocks +}; + +static bool cheatSearchEQ(u32 a, u32 b) +{ + return a == b; +} + +static bool cheatSearchNE(u32 a, u32 b) +{ + return a != b; +} + +static bool cheatSearchLT(u32 a, u32 b) +{ + return a < b; +} + +static bool cheatSearchLE(u32 a, u32 b) +{ + return a <= b; +} + +static bool cheatSearchGT(u32 a, u32 b) +{ + return a > b; +} + +static bool cheatSearchGE(u32 a, u32 b) +{ + return a >= b; +} + +static bool cheatSearchSignedEQ(s32 a, s32 b) +{ + return a == b; +} + +static bool cheatSearchSignedNE(s32 a, s32 b) +{ + return a != b; +} + +static bool cheatSearchSignedLT(s32 a, s32 b) +{ + return a < b; +} + +static bool cheatSearchSignedLE(s32 a, s32 b) +{ + return a <= b; +} + +static bool cheatSearchSignedGT(s32 a, s32 b) +{ + return a > b; +} + +static bool cheatSearchSignedGE(s32 a, s32 b) +{ + return a >= b; +} + +static bool (*cheatSearchFunc[])(u32,u32) = { + cheatSearchEQ, + cheatSearchNE, + cheatSearchLT, + cheatSearchLE, + cheatSearchGT, + cheatSearchGE +}; + +static bool (*cheatSearchSignedFunc[])(s32,s32) = { + cheatSearchSignedEQ, + cheatSearchSignedNE, + cheatSearchSignedLT, + cheatSearchSignedLE, + cheatSearchSignedGT, + cheatSearchSignedGE +}; + +void cheatSearchCleanup(CheatSearchData *cs) +{ + int count = cs->count; + + for(int i = 0; i < count; i++) { + free(cs->blocks[i].saved); + free(cs->blocks[i].bits); + } + cs->count = 0; +} + +void cheatSearchStart(const CheatSearchData *cs) +{ + int count = cs->count; + + for(int i = 0; i < count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + + memset(block->bits, 0xff, block->size >> 3); + memcpy(block->saved, block->data, block->size); + } +} + +s32 cheatSearchSignedRead(u8 *data, int off, int size) +{ + u32 res = data[off++]; + + switch(size) { + case BITS_8: + res <<= 24; + return ((s32)res) >> 24; + case BITS_16: + res |= ((u32)data[off++])<<8; + res <<= 16; + return ((s32)res) >> 16; + case BITS_32: + res |= ((u32)data[off++])<<8; + res |= ((u32)data[off++])<<16; + res |= ((u32)data[off++])<<24; + return (s32)res; + } + return (s32)res; +} + +u32 cheatSearchRead(u8 *data, int off, int size) +{ + u32 res = data[off++]; + if(size == BITS_16) + res |= ((u32)data[off++])<<8; + else if(size == BITS_32) { + res |= ((u32)data[off++])<<8; + res |= ((u32)data[off++])<<16; + res |= ((u32)data[off++])<<24; + } + return res; +} + +void cheatSearch(const CheatSearchData *cs, int compare, int size, + bool isSigned) +{ + if(compare < 0 || compare > SEARCH_GE) + return; + int inc = 1; + if(size == BITS_16) + inc = 2; + else if(size == BITS_32) + inc = 4; + + if(isSigned) { + bool (*func)(s32,s32) = cheatSearchSignedFunc[compare]; + + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + u8 *saved = block->saved; + + for(int j = 0; j < size2; j += inc) { + if(IS_BIT_SET(bits, j)) { + s32 a = cheatSearchSignedRead(data, j, size); + s32 b = cheatSearchSignedRead(saved,j, size); + + if(!func(a, b)) { + CLEAR_BIT(bits, j); + if(size == BITS_16) + CLEAR_BIT(bits, j+1); + if(size == BITS_32) { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } else { + bool (*func)(u32,u32) = cheatSearchFunc[compare]; + + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + u8 *saved = block->saved; + + for(int j = 0; j < size2; j += inc) { + if(IS_BIT_SET(bits, j)) { + u32 a = cheatSearchRead(data, j, size); + u32 b = cheatSearchRead(saved,j, size); + + if(!func(a, b)) { + CLEAR_BIT(bits, j); + if(size == BITS_16) + CLEAR_BIT(bits, j+1); + if(size == BITS_32) { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } +} + +void cheatSearchValue(const CheatSearchData *cs, int compare, int size, + bool isSigned, u32 value) +{ + if(compare < 0 || compare > SEARCH_GE) + return; + int inc = 1; + if(size == BITS_16) + inc = 2; + else if(size == BITS_32) + inc = 4; + + if(isSigned) { + bool (*func)(s32,s32) = cheatSearchSignedFunc[compare]; + + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + + for(int j = 0; j < size2; j += inc) { + if(IS_BIT_SET(bits, j)) { + s32 a = cheatSearchSignedRead(data, j, size); + s32 b = (s32)value; + + if(!func(a, b)) { + CLEAR_BIT(bits, j); + if(size == BITS_16) + CLEAR_BIT(bits, j+1); + if(size == BITS_32) { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } else { + bool (*func)(u32,u32) = cheatSearchFunc[compare]; + + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + + for(int j = 0; j < size2; j += inc) { + if(IS_BIT_SET(bits, j)) { + u32 a = cheatSearchRead(data, j, size); + + if(!func(a, value)) { + CLEAR_BIT(bits, j); + if(size == BITS_16) + CLEAR_BIT(bits, j+1); + if(size == BITS_32) { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } +} + +int cheatSearchGetCount(const CheatSearchData *cs, int size) +{ + int res = 0; + int inc = 1; + if(size == BITS_16) + inc = 2; + else if(size == BITS_32) + inc = 4; + + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + + int size2 = block->size; + u8 *bits = block->bits; + for(int j = 0; j < size2; j += inc) { + if(IS_BIT_SET(bits, j)) + res++; + } + } + return res; +} + +void cheatSearchUpdateValues(const CheatSearchData *cs) +{ + for(int i = 0; i < cs->count; i++) { + CheatSearchBlock *block = &cs->blocks[i]; + + memcpy(block->saved, block->data, block->size); + } +} + diff --git a/src/CheatSearch.h b/src/CheatSearch.h new file mode 100644 index 00000000..10db35d2 --- /dev/null +++ b/src/CheatSearch.h @@ -0,0 +1,73 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_CHEATSEARCH_H +#define VBA_CHEATSEARCH_H + +#include "System.h" + +struct CheatSearchBlock { + int size; + u32 offset; + u8 *bits; + u8 *data; + u8 *saved; +}; + +struct CheatSearchData { + int count; + CheatSearchBlock *blocks; +}; + +enum { + SEARCH_EQ, + SEARCH_NE, + SEARCH_LT, + SEARCH_LE, + SEARCH_GT, + SEARCH_GE +}; + +enum { + BITS_8, + BITS_16, + BITS_32 +}; + +#define SET_BIT(bits,off) \ + (bits)[(off) >> 3] |= (1 << ((off) & 7)) + +#define CLEAR_BIT(bits, off) \ + (bits)[(off) >> 3] &= ~(1 << ((off) & 7)) + +#define IS_BIT_SET(bits, off) \ + (bits)[(off) >> 3] & (1 << ((off) & 7)) + +extern CheatSearchData cheatSearchData; +extern void cheatSearchCleanup(CheatSearchData *cs); +extern void cheatSearchStart(const CheatSearchData *cs); +extern void cheatSearch(const CheatSearchData *cs, int compare, int size, + bool isSigned); +extern void cheatSearchValue(const CheatSearchData *cs, int compare, int size, + bool isSigned, u32 value); +extern int cheatSearchGetCount(const CheatSearchData *cs, int size); +extern void cheatSearchUpdateValues(const CheatSearchData *cs); +extern s32 cheatSearchSignedRead(u8 *data, int off, int size); +extern u32 cheatSearchRead(u8 *data, int off, int size); +#endif diff --git a/src/Cheats.cpp b/src/Cheats.cpp new file mode 100644 index 00000000..2eeae9b1 --- /dev/null +++ b/src/Cheats.cpp @@ -0,0 +1,2770 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include + +#include "GBA.h" +#include "GBAinline.h" +#include "Cheats.h" +#include "Globals.h" +#include "NLS.h" +#include "Util.h" + +/** + * Gameshark code types: (based on AR v1.0) + * + * NNNNNNNN 001DC0DE - ID code for the game (game 4 character name) from ROM + * DEADFACE XXXXXXXX - changes decryption seeds // Not supported by VBA. + * 0AAAAAAA 000000YY - 8-bit constant write + * 1AAAAAAA 0000YYYY - 16-bit constant write + * 2AAAAAAA YYYYYYYY - 32-bit constant write + * 30XXAAAA YYYYYYYY - 32bit Group Write, 8/16/32bit Sub/Add (depending on the XX value). + * 6AAAAAAA Z000YYYY - 16-bit ROM Patch (address >> 1). Z selects the Rom Patching register. + * - AR v1/2 hardware only supports Z=0. + * - AR v3 hardware should support Z=0,1,2 or 3. + * 8A1AAAAA 000000YY - 8-bit button write + * 8A2AAAAA 0000YYYY - 16-bit button write + * 8A4AAAAA YYYYYYYY - 32-bit button write // BUGGY ! Only writes 00000000 on the AR v1.0. + * 80F00000 0000YYYY - button slow motion + * DAAAAAAA 00Z0YYYY - Z = 0 : if 16-bit value at address != YYYY skip next line + * - Z = 1 : if 16-bit value at address == YYYY skip next line + * - Z = 2 : if 16-bit value at address > YYYY (Unsigned) skip next line + * - Z = 3 : if 16-bit value at address < YYYY (Unsigned) skip next line + * E0CCYYYY ZAAAAAAA - Z = 0 : if 16-bit value at address != YYYY skip CC lines + * - Z = 1 : if 16-bit value at address == YYYY skip CC lines + * - Z = 2 : if 16-bit value at address > YYYY (Unsigned) skip CC lines + * - Z = 3 : if 16-bit value at address < YYYY (Unsigned) skip CC lines + * FAAAAAAA 0000YYYY - Master code function + * + * + * + * CodeBreaker codes types: (based on the CBA clone "Cheatcode S" v1.1) + * + * 0000AAAA 000Y - Game CRC (Y are flags: 8 - CRC, 2 - DI) + * 1AAAAAAA YYYY - Master Code function (store address at ((YYYY << 0x16) + * + 0x08000100)) + * 2AAAAAAA YYYY - 16-bit or + * 3AAAAAAA YYYY - 8-bit constant write + * 4AAAAAAA YYYY - Slide code + * XXXXCCCC IIII (C is count and I is address increment, X is value incr.) + * 5AAAAAAA CCCC - Super code (Write bytes to address, 2*CCCC is count) + * BBBBBBBB BBBB + * 6AAAAAAA YYYY - 16-bit and + * 7AAAAAAA YYYY - if address contains 16-bit value enable next code + * 8AAAAAAA YYYY - 16-bit constant write + * 9AAAAAAA YYYY - change decryption (when first code only?) + * AAAAAAAA YYYY - if address does not contain 16-bit value enable next code + * BAAAAAAA YYYY - if 16-bit value at address <= YYYY skip next code + * CAAAAAAA YYYY - if 16-bit value at address >= YYYY skip next code + * D00000X0 YYYY - if button keys ... enable next code (else skip next code) + * EAAAAAAA YYYY - increase 16/32bit value stored in address + * FAAAAAAA YYYY - if 16-bit value at address AND YYYY = 0 then skip next code + **/ + +#define UNKNOWN_CODE -1 +#define INT_8_BIT_WRITE 0 +#define INT_16_BIT_WRITE 1 +#define INT_32_BIT_WRITE 2 +#define GSA_16_BIT_ROM_PATCH 3 +#define GSA_8_BIT_GS_WRITE 4 +#define GSA_16_BIT_GS_WRITE 5 +#define GSA_32_BIT_GS_WRITE 6 +#define CBA_IF_KEYS_PRESSED 7 +#define CBA_IF_TRUE 8 +#define CBA_SLIDE_CODE 9 +#define CBA_IF_FALSE 10 +#define CBA_AND 11 +#define GSA_8_BIT_GS_WRITE2 12 +#define GSA_16_BIT_GS_WRITE2 13 +#define GSA_32_BIT_GS_WRITE2 14 +#define GSA_16_BIT_ROM_PATCH2C 15 +#define GSA_8_BIT_SLIDE 16 +#define GSA_16_BIT_SLIDE 17 +#define GSA_32_BIT_SLIDE 18 +#define GSA_8_BIT_IF_TRUE 19 +#define GSA_32_BIT_IF_TRUE 20 +#define GSA_8_BIT_IF_FALSE 21 +#define GSA_32_BIT_IF_FALSE 22 +#define GSA_8_BIT_FILL 23 +#define GSA_16_BIT_FILL 24 +#define GSA_8_BIT_IF_TRUE2 25 +#define GSA_16_BIT_IF_TRUE2 26 +#define GSA_32_BIT_IF_TRUE2 27 +#define GSA_8_BIT_IF_FALSE2 28 +#define GSA_16_BIT_IF_FALSE2 29 +#define GSA_32_BIT_IF_FALSE2 30 +#define GSA_SLOWDOWN 31 +#define CBA_ADD 32 +#define CBA_OR 33 +#define CBA_LT 34 +#define CBA_GT 35 +#define CBA_SUPER 36 +#define GSA_8_BIT_POINTER 37 +#define GSA_16_BIT_POINTER 38 +#define GSA_32_BIT_POINTER 39 +#define GSA_8_BIT_ADD 40 +#define GSA_16_BIT_ADD 41 +#define GSA_32_BIT_ADD 42 +#define GSA_8_BIT_IF_LOWER_U 43 +#define GSA_16_BIT_IF_LOWER_U 44 +#define GSA_32_BIT_IF_LOWER_U 45 +#define GSA_8_BIT_IF_HIGHER_U 46 +#define GSA_16_BIT_IF_HIGHER_U 47 +#define GSA_32_BIT_IF_HIGHER_U 48 +#define GSA_8_BIT_IF_AND 49 +#define GSA_16_BIT_IF_AND 50 +#define GSA_32_BIT_IF_AND 51 +#define GSA_8_BIT_IF_LOWER_U2 52 +#define GSA_16_BIT_IF_LOWER_U2 53 +#define GSA_32_BIT_IF_LOWER_U2 54 +#define GSA_8_BIT_IF_HIGHER_U2 55 +#define GSA_16_BIT_IF_HIGHER_U2 56 +#define GSA_32_BIT_IF_HIGHER_U2 57 +#define GSA_8_BIT_IF_AND2 58 +#define GSA_16_BIT_IF_AND2 59 +#define GSA_32_BIT_IF_AND2 60 +#define GSA_ALWAYS 61 +#define GSA_ALWAYS2 62 +#define GSA_8_BIT_IF_LOWER_S 63 +#define GSA_16_BIT_IF_LOWER_S 64 +#define GSA_32_BIT_IF_LOWER_S 65 +#define GSA_8_BIT_IF_HIGHER_S 66 +#define GSA_16_BIT_IF_HIGHER_S 67 +#define GSA_32_BIT_IF_HIGHER_S 68 +#define GSA_8_BIT_IF_LOWER_S2 69 +#define GSA_16_BIT_IF_LOWER_S2 70 +#define GSA_32_BIT_IF_LOWER_S2 71 +#define GSA_8_BIT_IF_HIGHER_S2 72 +#define GSA_16_BIT_IF_HIGHER_S2 73 +#define GSA_32_BIT_IF_HIGHER_S2 74 +#define GSA_16_BIT_WRITE_IOREGS 75 +#define GSA_32_BIT_WRITE_IOREGS 76 +#define GSA_CODES_ON 77 +#define GSA_8_BIT_IF_TRUE3 78 +#define GSA_16_BIT_IF_TRUE3 79 +#define GSA_32_BIT_IF_TRUE3 80 +#define GSA_8_BIT_IF_FALSE3 81 +#define GSA_16_BIT_IF_FALSE3 82 +#define GSA_32_BIT_IF_FALSE3 83 +#define GSA_8_BIT_IF_LOWER_S3 84 +#define GSA_16_BIT_IF_LOWER_S3 85 +#define GSA_32_BIT_IF_LOWER_S3 86 +#define GSA_8_BIT_IF_HIGHER_S3 87 +#define GSA_16_BIT_IF_HIGHER_S3 88 +#define GSA_32_BIT_IF_HIGHER_S3 89 +#define GSA_8_BIT_IF_LOWER_U3 90 +#define GSA_16_BIT_IF_LOWER_U3 91 +#define GSA_32_BIT_IF_LOWER_U3 92 +#define GSA_8_BIT_IF_HIGHER_U3 93 +#define GSA_16_BIT_IF_HIGHER_U3 94 +#define GSA_32_BIT_IF_HIGHER_U3 95 +#define GSA_8_BIT_IF_AND3 96 +#define GSA_16_BIT_IF_AND3 97 +#define GSA_32_BIT_IF_AND3 98 +#define GSA_ALWAYS3 99 +#define GSA_16_BIT_ROM_PATCH2D 100 +#define GSA_16_BIT_ROM_PATCH2E 101 +#define GSA_16_BIT_ROM_PATCH2F 102 +#define GSA_GROUP_WRITE 103 +#define GSA_32_BIT_ADD2 104 +#define GSA_32_BIT_SUB2 105 +#define GSA_16_BIT_IF_LOWER_OR_EQ_U 106 +#define GSA_16_BIT_IF_HIGHER_OR_EQ_U 107 +#define GSA_16_BIT_MIF_TRUE 108 +#define GSA_16_BIT_MIF_FALSE 109 +#define GSA_16_BIT_MIF_LOWER_OR_EQ_U 110 +#define GSA_16_BIT_MIF_HIGHER_OR_EQ_U 111 + +CheatsData cheatsList[100]; +int cheatsNumber = 0; +u32 rompatch2addr [4]; +u16 rompatch2val [4]; +u16 rompatch2oldval [4]; + +u8 cheatsCBASeedBuffer[0x30]; +u32 cheatsCBASeed[4]; +u32 cheatsCBATemporaryValue = 0; +u16 cheatsCBATable[256]; +bool cheatsCBATableGenerated = false; +u16 super = 0; + +u8 cheatsCBACurrentSeed[12] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 +}; + +u32 seeds_v1[4]; +u32 seeds_v3[4]; + +u32 seed_gen(u8 upper, u8 seed, u8 *deadtable1, u8 *deadtable2); + +//seed tables for AR v1 +u8 v1_deadtable1[256] = { + 0x31, 0x1C, 0x23, 0xE5, 0x89, 0x8E, 0xA1, 0x37, 0x74, 0x6D, 0x67, 0xFC, 0x1F, 0xC0, 0xB1, 0x94, + 0x3B, 0x05, 0x56, 0x86, 0x00, 0x24, 0xF0, 0x17, 0x72, 0xA2, 0x3D, 0x1B, 0xE3, 0x17, 0xC5, 0x0B, + 0xB9, 0xE2, 0xBD, 0x58, 0x71, 0x1B, 0x2C, 0xFF, 0xE4, 0xC9, 0x4C, 0x5E, 0xC9, 0x55, 0x33, 0x45, + 0x7C, 0x3F, 0xB2, 0x51, 0xFE, 0x10, 0x7E, 0x75, 0x3C, 0x90, 0x8D, 0xDA, 0x94, 0x38, 0xC3, 0xE9, + 0x95, 0xEA, 0xCE, 0xA6, 0x06, 0xE0, 0x4F, 0x3F, 0x2A, 0xE3, 0x3A, 0xE4, 0x43, 0xBD, 0x7F, 0xDA, + 0x55, 0xF0, 0xEA, 0xCB, 0x2C, 0xA8, 0x47, 0x61, 0xA0, 0xEF, 0xCB, 0x13, 0x18, 0x20, 0xAF, 0x3E, + 0x4D, 0x9E, 0x1E, 0x77, 0x51, 0xC5, 0x51, 0x20, 0xCF, 0x21, 0xF9, 0x39, 0x94, 0xDE, 0xDD, 0x79, + 0x4E, 0x80, 0xC4, 0x9D, 0x94, 0xD5, 0x95, 0x01, 0x27, 0x27, 0xBD, 0x6D, 0x78, 0xB5, 0xD1, 0x31, + 0x6A, 0x65, 0x74, 0x74, 0x58, 0xB3, 0x7C, 0xC9, 0x5A, 0xED, 0x50, 0x03, 0xC4, 0xA2, 0x94, 0x4B, + 0xF0, 0x58, 0x09, 0x6F, 0x3E, 0x7D, 0xAE, 0x7D, 0x58, 0xA0, 0x2C, 0x91, 0xBB, 0xE1, 0x70, 0xEB, + 0x73, 0xA6, 0x9A, 0x44, 0x25, 0x90, 0x16, 0x62, 0x53, 0xAE, 0x08, 0xEB, 0xDC, 0xF0, 0xEE, 0x77, + 0xC2, 0xDE, 0x81, 0xE8, 0x30, 0x89, 0xDB, 0xFE, 0xBC, 0xC2, 0xDF, 0x26, 0xE9, 0x8B, 0xD6, 0x93, + 0xF0, 0xCB, 0x56, 0x90, 0xC0, 0x46, 0x68, 0x15, 0x43, 0xCB, 0xE9, 0x98, 0xE3, 0xAF, 0x31, 0x25, + 0x4D, 0x7B, 0xF3, 0xB1, 0x74, 0xE2, 0x64, 0xAC, 0xD9, 0xF6, 0xA0, 0xD5, 0x0B, 0x9B, 0x49, 0x52, + 0x69, 0x3B, 0x71, 0x00, 0x2F, 0xBB, 0xBA, 0x08, 0xB1, 0xAE, 0xBB, 0xB3, 0xE1, 0xC9, 0xA6, 0x7F, + 0x17, 0x97, 0x28, 0x72, 0x12, 0x6E, 0x91, 0xAE, 0x3A, 0xA2, 0x35, 0x46, 0x27, 0xF8, 0x12, 0x50 +}; +u8 v1_deadtable2[256] = { + 0xD8, 0x65, 0x04, 0xC2, 0x65, 0xD5, 0xB0, 0x0C, 0xDF, 0x9D, 0xF0, 0xC3, 0x9A, 0x17, 0xC9, 0xA6, + 0xE1, 0xAC, 0x0D, 0x14, 0x2F, 0x3C, 0x2C, 0x87, 0xA2, 0xBF, 0x4D, 0x5F, 0xAC, 0x2D, 0x9D, 0xE1, + 0x0C, 0x9C, 0xE7, 0x7F, 0xFC, 0xA8, 0x66, 0x59, 0xAC, 0x18, 0xD7, 0x05, 0xF0, 0xBF, 0xD1, 0x8B, + 0x35, 0x9F, 0x59, 0xB4, 0xBA, 0x55, 0xB2, 0x85, 0xFD, 0xB1, 0x72, 0x06, 0x73, 0xA4, 0xDB, 0x48, + 0x7B, 0x5F, 0x67, 0xA5, 0x95, 0xB9, 0xA5, 0x4A, 0xCF, 0xD1, 0x44, 0xF3, 0x81, 0xF5, 0x6D, 0xF6, + 0x3A, 0xC3, 0x57, 0x83, 0xFA, 0x8E, 0x15, 0x2A, 0xA2, 0x04, 0xB2, 0x9D, 0xA8, 0x0D, 0x7F, 0xB8, + 0x0F, 0xF6, 0xAC, 0xBE, 0x97, 0xCE, 0x16, 0xE6, 0x31, 0x10, 0x60, 0x16, 0xB5, 0x83, 0x45, 0xEE, + 0xD7, 0x5F, 0x2C, 0x08, 0x58, 0xB1, 0xFD, 0x7E, 0x79, 0x00, 0x34, 0xAD, 0xB5, 0x31, 0x34, 0x39, + 0xAF, 0xA8, 0xDD, 0x52, 0x6A, 0xB0, 0x60, 0x35, 0xB8, 0x1D, 0x52, 0xF5, 0xF5, 0x30, 0x00, 0x7B, + 0xF4, 0xBA, 0x03, 0xCB, 0x3A, 0x84, 0x14, 0x8A, 0x6A, 0xEF, 0x21, 0xBD, 0x01, 0xD8, 0xA0, 0xD4, + 0x43, 0xBE, 0x23, 0xE7, 0x76, 0x27, 0x2C, 0x3F, 0x4D, 0x3F, 0x43, 0x18, 0xA7, 0xC3, 0x47, 0xA5, + 0x7A, 0x1D, 0x02, 0x55, 0x09, 0xD1, 0xFF, 0x55, 0x5E, 0x17, 0xA0, 0x56, 0xF4, 0xC9, 0x6B, 0x90, + 0xB4, 0x80, 0xA5, 0x07, 0x22, 0xFB, 0x22, 0x0D, 0xD9, 0xC0, 0x5B, 0x08, 0x35, 0x05, 0xC1, 0x75, + 0x4F, 0xD0, 0x51, 0x2D, 0x2E, 0x5E, 0x69, 0xE7, 0x3B, 0xC2, 0xDA, 0xFF, 0xF6, 0xCE, 0x3E, 0x76, + 0xE8, 0x36, 0x8C, 0x39, 0xD8, 0xF3, 0xE9, 0xA6, 0x42, 0xE6, 0xC1, 0x4C, 0x05, 0xBE, 0x17, 0xF2, + 0x5C, 0x1B, 0x19, 0xDB, 0x0F, 0xF3, 0xF8, 0x49, 0xEB, 0x36, 0xF6, 0x40, 0x6F, 0xAD, 0xC1, 0x8C +}; + +//seed tables for AR v3 +u8 v3_deadtable1[256] = { + 0xD0, 0xFF, 0xBA, 0xE5, 0xC1, 0xC7, 0xDB, 0x5B, 0x16, 0xE3, 0x6E, 0x26, 0x62, 0x31, 0x2E, 0x2A, + 0xD1, 0xBB, 0x4A, 0xE6, 0xAE, 0x2F, 0x0A, 0x90, 0x29, 0x90, 0xB6, 0x67, 0x58, 0x2A, 0xB4, 0x45, + 0x7B, 0xCB, 0xF0, 0x73, 0x84, 0x30, 0x81, 0xC2, 0xD7, 0xBE, 0x89, 0xD7, 0x4E, 0x73, 0x5C, 0xC7, + 0x80, 0x1B, 0xE5, 0xE4, 0x43, 0xC7, 0x46, 0xD6, 0x6F, 0x7B, 0xBF, 0xED, 0xE5, 0x27, 0xD1, 0xB5, + 0xD0, 0xD8, 0xA3, 0xCB, 0x2B, 0x30, 0xA4, 0xF0, 0x84, 0x14, 0x72, 0x5C, 0xFF, 0xA4, 0xFB, 0x54, + 0x9D, 0x70, 0xE2, 0xFF, 0xBE, 0xE8, 0x24, 0x76, 0xE5, 0x15, 0xFB, 0x1A, 0xBC, 0x87, 0x02, 0x2A, + 0x58, 0x8F, 0x9A, 0x95, 0xBD, 0xAE, 0x8D, 0x0C, 0xA5, 0x4C, 0xF2, 0x5C, 0x7D, 0xAD, 0x51, 0xFB, + 0xB1, 0x22, 0x07, 0xE0, 0x29, 0x7C, 0xEB, 0x98, 0x14, 0xC6, 0x31, 0x97, 0xE4, 0x34, 0x8F, 0xCC, + 0x99, 0x56, 0x9F, 0x78, 0x43, 0x91, 0x85, 0x3F, 0xC2, 0xD0, 0xD1, 0x80, 0xD1, 0x77, 0xA7, 0xE2, + 0x43, 0x99, 0x1D, 0x2F, 0x8B, 0x6A, 0xE4, 0x66, 0x82, 0xF7, 0x2B, 0x0B, 0x65, 0x14, 0xC0, 0xC2, + 0x1D, 0x96, 0x78, 0x1C, 0xC4, 0xC3, 0xD2, 0xB1, 0x64, 0x07, 0xD7, 0x6F, 0x02, 0xE9, 0x44, 0x31, + 0xDB, 0x3C, 0xEB, 0x93, 0xED, 0x9A, 0x57, 0x05, 0xB9, 0x0E, 0xAF, 0x1F, 0x48, 0x11, 0xDC, 0x35, + 0x6C, 0xB8, 0xEE, 0x2A, 0x48, 0x2B, 0xBC, 0x89, 0x12, 0x59, 0xCB, 0xD1, 0x18, 0xEA, 0x72, 0x11, + 0x01, 0x75, 0x3B, 0xB5, 0x56, 0xF4, 0x8B, 0xA0, 0x41, 0x75, 0x86, 0x7B, 0x94, 0x12, 0x2D, 0x4C, + 0x0C, 0x22, 0xC9, 0x4A, 0xD8, 0xB1, 0x8D, 0xF0, 0x55, 0x2E, 0x77, 0x50, 0x1C, 0x64, 0x77, 0xAA, + 0x3E, 0xAC, 0xD3, 0x3D, 0xCE, 0x60, 0xCA, 0x5D, 0xA0, 0x92, 0x78, 0xC6, 0x51, 0xFE, 0xF9, 0x30 +}; +u8 v3_deadtable2[256] = { + 0xAA, 0xAF, 0xF0, 0x72, 0x90, 0xF7, 0x71, 0x27, 0x06, 0x11, 0xEB, 0x9C, 0x37, 0x12, 0x72, 0xAA, + 0x65, 0xBC, 0x0D, 0x4A, 0x76, 0xF6, 0x5C, 0xAA, 0xB0, 0x7A, 0x7D, 0x81, 0xC1, 0xCE, 0x2F, 0x9F, + 0x02, 0x75, 0x38, 0xC8, 0xFC, 0x66, 0x05, 0xC2, 0x2C, 0xBD, 0x91, 0xAD, 0x03, 0xB1, 0x88, 0x93, + 0x31, 0xC6, 0xAB, 0x40, 0x23, 0x43, 0x76, 0x54, 0xCA, 0xE7, 0x00, 0x96, 0x9F, 0xD8, 0x24, 0x8B, + 0xE4, 0xDC, 0xDE, 0x48, 0x2C, 0xCB, 0xF7, 0x84, 0x1D, 0x45, 0xE5, 0xF1, 0x75, 0xA0, 0xED, 0xCD, + 0x4B, 0x24, 0x8A, 0xB3, 0x98, 0x7B, 0x12, 0xB8, 0xF5, 0x63, 0x97, 0xB3, 0xA6, 0xA6, 0x0B, 0xDC, + 0xD8, 0x4C, 0xA8, 0x99, 0x27, 0x0F, 0x8F, 0x94, 0x63, 0x0F, 0xB0, 0x11, 0x94, 0xC7, 0xE9, 0x7F, + 0x3B, 0x40, 0x72, 0x4C, 0xDB, 0x84, 0x78, 0xFE, 0xB8, 0x56, 0x08, 0x80, 0xDF, 0x20, 0x2F, 0xB9, + 0x66, 0x2D, 0x60, 0x63, 0xF5, 0x18, 0x15, 0x1B, 0x86, 0x85, 0xB9, 0xB4, 0x68, 0x0E, 0xC6, 0xD1, + 0x8A, 0x81, 0x2B, 0xB3, 0xF6, 0x48, 0xF0, 0x4F, 0x9C, 0x28, 0x1C, 0xA4, 0x51, 0x2F, 0xD7, 0x4B, + 0x17, 0xE7, 0xCC, 0x50, 0x9F, 0xD0, 0xD1, 0x40, 0x0C, 0x0D, 0xCA, 0x83, 0xFA, 0x5E, 0xCA, 0xEC, + 0xBF, 0x4E, 0x7C, 0x8F, 0xF0, 0xAE, 0xC2, 0xD3, 0x28, 0x41, 0x9B, 0xC8, 0x04, 0xB9, 0x4A, 0xBA, + 0x72, 0xE2, 0xB5, 0x06, 0x2C, 0x1E, 0x0B, 0x2C, 0x7F, 0x11, 0xA9, 0x26, 0x51, 0x9D, 0x3F, 0xF8, + 0x62, 0x11, 0x2E, 0x89, 0xD2, 0x9D, 0x35, 0xB1, 0xE4, 0x0A, 0x4D, 0x93, 0x01, 0xA7, 0xD1, 0x2D, + 0x00, 0x87, 0xE2, 0x2D, 0xA4, 0xE9, 0x0A, 0x06, 0x66, 0xF8, 0x1F, 0x44, 0x75, 0xB5, 0x6B, 0x1C, + 0xFC, 0x31, 0x09, 0x48, 0xA3, 0xFF, 0x92, 0x12, 0x58, 0xE9, 0xFA, 0xAE, 0x4F, 0xE2, 0xB4, 0xCC +}; + +#define debuggerReadMemory(addr) \ + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadHalfWord(addr) \ + READ16LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadByte(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +#define debuggerWriteMemory(addr, value) \ + WRITE32LE(&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask], value) + +#define debuggerWriteHalfWord(addr, value) \ + WRITE16LE(&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask], value) + +#define debuggerWriteByte(addr, value) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] = (value) + + +#define CHEAT_IS_HEX(a) ( ((a)>='A' && (a) <='F') || ((a) >='0' && (a) <= '9')) + +#define CHEAT_PATCH_ROM_16BIT(a,v) \ + WRITE16LE(((u16 *)&rom[(a) & 0x1ffffff]), v); + +static bool isMultilineWithData(int i) +{ + // we consider it a multiline code if it has more than one line of data + // otherwise, it can still be considered a single code + // (Only CBA codes can be true multilines !!!) + if(i < cheatsNumber && i >= 0) + switch(cheatsList[i].size) { + case INT_8_BIT_WRITE: + case INT_16_BIT_WRITE: + case INT_32_BIT_WRITE: + case GSA_16_BIT_ROM_PATCH: + case GSA_8_BIT_GS_WRITE: + case GSA_16_BIT_GS_WRITE: + case GSA_32_BIT_GS_WRITE: + case CBA_AND: + case CBA_IF_KEYS_PRESSED: + case CBA_IF_TRUE: + case CBA_IF_FALSE: + case GSA_8_BIT_IF_TRUE: + case GSA_32_BIT_IF_TRUE: + case GSA_8_BIT_IF_FALSE: + case GSA_32_BIT_IF_FALSE: + case GSA_8_BIT_FILL: + case GSA_16_BIT_FILL: + case GSA_8_BIT_IF_TRUE2: + case GSA_16_BIT_IF_TRUE2: + case GSA_32_BIT_IF_TRUE2: + case GSA_8_BIT_IF_FALSE2: + case GSA_16_BIT_IF_FALSE2: + case GSA_32_BIT_IF_FALSE2: + case GSA_SLOWDOWN: + case CBA_ADD: + case CBA_OR: + case CBA_LT: + case CBA_GT: + case GSA_8_BIT_POINTER: + case GSA_16_BIT_POINTER: + case GSA_32_BIT_POINTER: + case GSA_8_BIT_ADD: + case GSA_16_BIT_ADD: + case GSA_32_BIT_ADD: + case GSA_8_BIT_IF_LOWER_U: + case GSA_16_BIT_IF_LOWER_U: + case GSA_32_BIT_IF_LOWER_U: + case GSA_8_BIT_IF_HIGHER_U: + case GSA_16_BIT_IF_HIGHER_U: + case GSA_32_BIT_IF_HIGHER_U: + case GSA_8_BIT_IF_AND: + case GSA_16_BIT_IF_AND: + case GSA_32_BIT_IF_AND: + case GSA_8_BIT_IF_LOWER_U2: + case GSA_16_BIT_IF_LOWER_U2: + case GSA_32_BIT_IF_LOWER_U2: + case GSA_8_BIT_IF_HIGHER_U2: + case GSA_16_BIT_IF_HIGHER_U2: + case GSA_32_BIT_IF_HIGHER_U2: + case GSA_8_BIT_IF_AND2: + case GSA_16_BIT_IF_AND2: + case GSA_32_BIT_IF_AND2: + case GSA_ALWAYS: + case GSA_ALWAYS2: + case GSA_8_BIT_IF_LOWER_S: + case GSA_16_BIT_IF_LOWER_S: + case GSA_32_BIT_IF_LOWER_S: + case GSA_8_BIT_IF_HIGHER_S: + case GSA_16_BIT_IF_HIGHER_S: + case GSA_32_BIT_IF_HIGHER_S: + case GSA_8_BIT_IF_LOWER_S2: + case GSA_16_BIT_IF_LOWER_S2: + case GSA_32_BIT_IF_LOWER_S2: + case GSA_8_BIT_IF_HIGHER_S2: + case GSA_16_BIT_IF_HIGHER_S2: + case GSA_32_BIT_IF_HIGHER_S2: + case GSA_16_BIT_WRITE_IOREGS: + case GSA_32_BIT_WRITE_IOREGS: + case GSA_CODES_ON: + case GSA_8_BIT_IF_TRUE3: + case GSA_16_BIT_IF_TRUE3: + case GSA_32_BIT_IF_TRUE3: + case GSA_8_BIT_IF_FALSE3: + case GSA_16_BIT_IF_FALSE3: + case GSA_32_BIT_IF_FALSE3: + case GSA_8_BIT_IF_LOWER_S3: + case GSA_16_BIT_IF_LOWER_S3: + case GSA_32_BIT_IF_LOWER_S3: + case GSA_8_BIT_IF_HIGHER_S3: + case GSA_16_BIT_IF_HIGHER_S3: + case GSA_32_BIT_IF_HIGHER_S3: + case GSA_8_BIT_IF_LOWER_U3: + case GSA_16_BIT_IF_LOWER_U3: + case GSA_32_BIT_IF_LOWER_U3: + case GSA_8_BIT_IF_HIGHER_U3: + case GSA_16_BIT_IF_HIGHER_U3: + case GSA_32_BIT_IF_HIGHER_U3: + case GSA_8_BIT_IF_AND3: + case GSA_16_BIT_IF_AND3: + case GSA_32_BIT_IF_AND3: + case GSA_ALWAYS3: + case GSA_8_BIT_GS_WRITE2: + case GSA_16_BIT_GS_WRITE2: + case GSA_32_BIT_GS_WRITE2: + case GSA_16_BIT_ROM_PATCH2C: + case GSA_16_BIT_ROM_PATCH2D: + case GSA_16_BIT_ROM_PATCH2E: + case GSA_16_BIT_ROM_PATCH2F: + case GSA_8_BIT_SLIDE: + case GSA_16_BIT_SLIDE: + case GSA_32_BIT_SLIDE: + case GSA_GROUP_WRITE: + case GSA_32_BIT_ADD2: + case GSA_32_BIT_SUB2: + case GSA_16_BIT_IF_LOWER_OR_EQ_U: + case GSA_16_BIT_IF_HIGHER_OR_EQ_U: + case GSA_16_BIT_MIF_TRUE: + case GSA_16_BIT_MIF_FALSE: + case GSA_16_BIT_MIF_LOWER_OR_EQ_U: + case GSA_16_BIT_MIF_HIGHER_OR_EQ_U: + return false; + // the codes below have two lines of data + case CBA_SLIDE_CODE: + case CBA_SUPER: + return true; + } + return false; +} + +static int getCodeLength(int num) +{ + if(num >= cheatsNumber || num < 0) + return 1; + + // this is for all the codes that are true multiline + switch(cheatsList[num].size) { + case INT_8_BIT_WRITE: + case INT_16_BIT_WRITE: + case INT_32_BIT_WRITE: + case GSA_16_BIT_ROM_PATCH: + case GSA_8_BIT_GS_WRITE: + case GSA_16_BIT_GS_WRITE: + case GSA_32_BIT_GS_WRITE: + case CBA_AND: + case GSA_8_BIT_FILL: + case GSA_16_BIT_FILL: + case GSA_SLOWDOWN: + case CBA_ADD: + case CBA_OR: + case GSA_8_BIT_POINTER: + case GSA_16_BIT_POINTER: + case GSA_32_BIT_POINTER: + case GSA_8_BIT_ADD: + case GSA_16_BIT_ADD: + case GSA_32_BIT_ADD: + case GSA_CODES_ON: + case GSA_8_BIT_IF_TRUE3: + case GSA_16_BIT_IF_TRUE3: + case GSA_32_BIT_IF_TRUE3: + case GSA_8_BIT_IF_FALSE3: + case GSA_16_BIT_IF_FALSE3: + case GSA_32_BIT_IF_FALSE3: + case GSA_8_BIT_IF_LOWER_S3: + case GSA_16_BIT_IF_LOWER_S3: + case GSA_32_BIT_IF_LOWER_S3: + case GSA_8_BIT_IF_HIGHER_S3: + case GSA_16_BIT_IF_HIGHER_S3: + case GSA_32_BIT_IF_HIGHER_S3: + case GSA_8_BIT_IF_LOWER_U3: + case GSA_16_BIT_IF_LOWER_U3: + case GSA_32_BIT_IF_LOWER_U3: + case GSA_8_BIT_IF_HIGHER_U3: + case GSA_16_BIT_IF_HIGHER_U3: + case GSA_32_BIT_IF_HIGHER_U3: + case GSA_8_BIT_IF_AND3: + case GSA_16_BIT_IF_AND3: + case GSA_32_BIT_IF_AND3: + case GSA_8_BIT_IF_LOWER_U: + case GSA_16_BIT_IF_LOWER_U: + case GSA_32_BIT_IF_LOWER_U: + case GSA_8_BIT_IF_HIGHER_U: + case GSA_16_BIT_IF_HIGHER_U: + case GSA_32_BIT_IF_HIGHER_U: + case GSA_8_BIT_IF_AND: + case GSA_16_BIT_IF_AND: + case GSA_32_BIT_IF_AND: + case GSA_ALWAYS: + case GSA_8_BIT_IF_LOWER_S: + case GSA_16_BIT_IF_LOWER_S: + case GSA_32_BIT_IF_LOWER_S: + case GSA_8_BIT_IF_HIGHER_S: + case GSA_16_BIT_IF_HIGHER_S: + case GSA_32_BIT_IF_HIGHER_S: + case GSA_16_BIT_WRITE_IOREGS: + case GSA_32_BIT_WRITE_IOREGS: + case GSA_8_BIT_GS_WRITE2: + case GSA_16_BIT_GS_WRITE2: + case GSA_32_BIT_GS_WRITE2: + case GSA_16_BIT_ROM_PATCH2C: + case GSA_16_BIT_ROM_PATCH2D: + case GSA_16_BIT_ROM_PATCH2E: + case GSA_16_BIT_ROM_PATCH2F: + case GSA_8_BIT_SLIDE: + case GSA_16_BIT_SLIDE: + case GSA_32_BIT_SLIDE: + case GSA_8_BIT_IF_TRUE: + case GSA_32_BIT_IF_TRUE: + case GSA_8_BIT_IF_FALSE: + case GSA_32_BIT_IF_FALSE: + case CBA_LT: + case CBA_GT: + case CBA_IF_TRUE: + case CBA_IF_FALSE: + case GSA_8_BIT_IF_TRUE2: + case GSA_16_BIT_IF_TRUE2: + case GSA_32_BIT_IF_TRUE2: + case GSA_8_BIT_IF_FALSE2: + case GSA_16_BIT_IF_FALSE2: + case GSA_32_BIT_IF_FALSE2: + case GSA_8_BIT_IF_LOWER_U2: + case GSA_16_BIT_IF_LOWER_U2: + case GSA_32_BIT_IF_LOWER_U2: + case GSA_8_BIT_IF_HIGHER_U2: + case GSA_16_BIT_IF_HIGHER_U2: + case GSA_32_BIT_IF_HIGHER_U2: + case GSA_8_BIT_IF_AND2: + case GSA_16_BIT_IF_AND2: + case GSA_32_BIT_IF_AND2: + case GSA_ALWAYS2: + case GSA_8_BIT_IF_LOWER_S2: + case GSA_16_BIT_IF_LOWER_S2: + case GSA_32_BIT_IF_LOWER_S2: + case GSA_8_BIT_IF_HIGHER_S2: + case GSA_16_BIT_IF_HIGHER_S2: + case GSA_32_BIT_IF_HIGHER_S2: + case GSA_GROUP_WRITE: + case GSA_32_BIT_ADD2: + case GSA_32_BIT_SUB2: + case GSA_16_BIT_IF_LOWER_OR_EQ_U: + case GSA_16_BIT_IF_HIGHER_OR_EQ_U: + case GSA_16_BIT_MIF_TRUE: + case GSA_16_BIT_MIF_FALSE: + case GSA_16_BIT_MIF_LOWER_OR_EQ_U: + case GSA_16_BIT_MIF_HIGHER_OR_EQ_U: + return 1; + case CBA_IF_KEYS_PRESSED: + case CBA_SLIDE_CODE: + return 2; + case CBA_SUPER: + return ((((cheatsList[num].value-1) & 0xFFFF)/3) + 1); + } + return 1; +} + +int cheatsCheckKeys(u32 keys, u32 extended) +{ + bool onoff = true; + int ticks = 1; + int i; + for (i = 0; i<4; i++) + if (rompatch2addr [i] != 0) { + CHEAT_PATCH_ROM_16BIT(rompatch2addr [i],rompatch2oldval [i]); + rompatch2addr [i] = 0; + } + + for (i = 0; i < cheatsNumber; i++) { + if(!cheatsList[i].enabled) { + // make sure we skip other lines in this code + i += getCodeLength(i)-1; + continue; + } + switch(cheatsList[i].size) { + case GSA_CODES_ON: + onoff = true; + break; + case GSA_SLOWDOWN: + // check if button was pressed and released, if so toggle our state + if((cheatsList[i].status & 4) && !(extended & 4)) + cheatsList[i].status ^= 1; + if(extended & 4) + cheatsList[i].status |= 4; + else + cheatsList[i].status &= ~4; + + if(cheatsList[i].status & 1) + ticks += ((cheatsList[i].value & 0xFFFF) * 7); + break; + case GSA_8_BIT_SLIDE: + i++; + if(i < cheatsNumber) { + u32 addr = cheatsList[i-1].value; + u8 value = cheatsList[i].rawaddress; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff); + while(count > 0) { + CPUWriteByte(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + i++; + break; + case GSA_16_BIT_SLIDE: + i++; + if(i < cheatsNumber) { + u32 addr = cheatsList[i-1].value; + u16 value = cheatsList[i].rawaddress; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff)*2; + while(count > 0) { + CPUWriteHalfWord(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + i++; + break; + case GSA_32_BIT_SLIDE: + i++; + if(i < cheatsNumber) { + u32 addr = cheatsList[i-1].value; + u32 value = cheatsList[i].rawaddress; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff)*4; + while(count > 0) { + CPUWriteMemory(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + i++; + break; + case GSA_8_BIT_GS_WRITE2: + i++; + if(i < cheatsNumber) { + if(extended & 4) { + CPUWriteByte(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_16_BIT_GS_WRITE2: + i++; + if(i < cheatsNumber) { + if(extended & 4) { + CPUWriteHalfWord(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_32_BIT_GS_WRITE2: + i++; + if(i < cheatsNumber) { + if(extended & 4) { + CPUWriteMemory(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_16_BIT_ROM_PATCH2C: + i++; + if((i < cheatsNumber) && (cheatsList[i].status & 1) == 0) { + rompatch2addr [0] = ((cheatsList[i-1].value & 0x00FFFFFF) << 1) + 0x8000000; + rompatch2oldval [0] = CPUReadHalfWord(rompatch2addr [0]); + rompatch2val [0] = cheatsList[i].rawaddress & 0xFFFF; + } + i++; + break; + case GSA_16_BIT_ROM_PATCH2D: + i++; + if((i < cheatsNumber) && (cheatsList[i].status & 1) == 0) { + rompatch2addr [1] = ((cheatsList[i-1].value & 0x00FFFFFF) << 1) + 0x8000000; + rompatch2oldval [1] = CPUReadHalfWord(rompatch2addr [1]); + rompatch2val [1] = cheatsList[i].rawaddress & 0xFFFF; + } + i++; + break; + case GSA_16_BIT_ROM_PATCH2E: + i++; + if((i < cheatsNumber) && (cheatsList[i].status & 1) == 0) { + rompatch2addr [2] = ((cheatsList[i-1].value & 0x00FFFFFF) << 1) + 0x8000000; + rompatch2oldval [2] = CPUReadHalfWord(rompatch2addr [2]); + rompatch2val [2] = cheatsList[i].rawaddress & 0xFFFF; + } + i++; + break; + case GSA_16_BIT_ROM_PATCH2F: + i++; + if((i < cheatsNumber) && (cheatsList[i].status & 1) == 0) { + rompatch2addr [3] = ((cheatsList[i-1].value & 0x00FFFFFF) << 1) + 0x8000000; + rompatch2oldval [3] = CPUReadHalfWord(rompatch2addr [3]); + rompatch2val [3] = cheatsList[i].rawaddress & 0xFFFF; + } + i++; + break; + } + if (onoff) { + switch(cheatsList[i].size) { + case INT_8_BIT_WRITE: + CPUWriteByte(cheatsList[i].address, cheatsList[i].value); + break; + case INT_16_BIT_WRITE: + CPUWriteHalfWord(cheatsList[i].address, cheatsList[i].value); + break; + case INT_32_BIT_WRITE: + CPUWriteMemory(cheatsList[i].address, cheatsList[i].value); + break; + case GSA_16_BIT_ROM_PATCH: + if((cheatsList[i].status & 1) == 0) { + if(CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) { + cheatsList[i].oldValue = CPUReadHalfWord(cheatsList[i].address); + cheatsList[i].status |= 1; + CHEAT_PATCH_ROM_16BIT(cheatsList[i].address, cheatsList[i].value); + } + } + break; + case GSA_8_BIT_GS_WRITE: + if(extended & 4) { + CPUWriteByte(cheatsList[i].address, cheatsList[i].value); + } + break; + case GSA_16_BIT_GS_WRITE: + if(extended & 4) { + CPUWriteHalfWord(cheatsList[i].address, cheatsList[i].value); + } + break; + case GSA_32_BIT_GS_WRITE: + if(extended & 4) { + CPUWriteMemory(cheatsList[i].address, cheatsList[i].value); + } + break; + case CBA_IF_KEYS_PRESSED: + { + u16 value = cheatsList[i].value; + u32 addr = cheatsList[i].address; + if((addr & 0xF0) == 0x20) { + if((keys & value) == 0) { + i++; + } + } else if((addr & 0xF0) == 0x10) { + if((keys & value) == value) { + i++; + } + } else if((addr & 0xF0) == 0x00) { + if(((~keys) & 0x3FF) == value) { + i++; + } + } + } + break; + case CBA_IF_TRUE: + if(CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) { + i++; + } + break; + case CBA_SLIDE_CODE: + { + u32 address = cheatsList[i].address; + u16 value = cheatsList[i].value; + i++; + if(i < cheatsNumber) { + int count = ((cheatsList[i].address - 1) & 0xFFFF); + u16 vinc = (cheatsList[i].address >> 16) & 0xFFFF; + int inc = cheatsList[i].value; + for(int x = 0; x <= count ; x++) { + CPUWriteHalfWord(address, value); + address += inc; + value += vinc; + } + } + } + break; + case CBA_IF_FALSE: + if(CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value){ + i++; + } + break; + case CBA_AND: + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) & + cheatsList[i].value); + break; + case GSA_8_BIT_IF_TRUE: + if(CPUReadByte(cheatsList[i].address) != cheatsList[i].value) { + i++; + } + break; + case GSA_32_BIT_IF_TRUE: + if(CPUReadMemory(cheatsList[i].address) != cheatsList[i].value) { + i++; + } + break; + case GSA_8_BIT_IF_FALSE: + if(CPUReadByte(cheatsList[i].address) == cheatsList[i].value) { + i++; + } + break; + case GSA_32_BIT_IF_FALSE: + if(CPUReadMemory(cheatsList[i].address) == cheatsList[i].value) { + i++; + } + break; + case GSA_8_BIT_FILL: + { + u32 addr = cheatsList[i].address; + u8 v = cheatsList[i].value & 0xff; + u32 end = addr + (cheatsList[i].value >> 8); + do { + CPUWriteByte(addr, v); + addr++; + } while (addr <= end); + } + break; + case GSA_16_BIT_FILL: + { + u32 addr = cheatsList[i].address; + u16 v = cheatsList[i].value & 0xffff; + u32 end = addr + ((cheatsList[i].value >> 16) << 1); + do { + CPUWriteHalfWord(addr, v); + addr+=2; + } while (addr <= end); + } + break; + case GSA_8_BIT_IF_TRUE2: + if(CPUReadByte(cheatsList[i].address) != cheatsList[i].value) { + i+=2; + } + break; + case GSA_16_BIT_IF_TRUE2: + if(CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) { + i+=2; + } + break; + case GSA_32_BIT_IF_TRUE2: + if(CPUReadMemory(cheatsList[i].address) != cheatsList[i].value) { + i+=2; + } + break; + case GSA_8_BIT_IF_FALSE2: + if(CPUReadByte(cheatsList[i].address) == cheatsList[i].value) { + i+=2; + } + break; + case GSA_16_BIT_IF_FALSE2: + if(CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value) { + i+=2; + } + break; + case GSA_32_BIT_IF_FALSE2: + if(CPUReadMemory(cheatsList[i].address) == cheatsList[i].value) { + i+=2; + } + break; + case CBA_ADD: + if ((cheatsList[i].address & 1) == 0) { + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) + + cheatsList[i].value); + } else { + CPUWriteMemory(cheatsList[i].address & 0x0FFFFFFE, + CPUReadMemory(cheatsList[i].address & 0x0FFFFFFE) + + cheatsList[i].value); + } + break; + case CBA_OR: + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) | + cheatsList[i].value); + break; + case CBA_GT: + if (!(CPUReadHalfWord(cheatsList[i].address) > cheatsList[i].value)){ + i++; + } + break; + case CBA_LT: + if (!(CPUReadHalfWord(cheatsList[i].address) < cheatsList[i].value)){ + i++; + } + break; + case CBA_SUPER: + { + int count = 2*((cheatsList[i].value -1) & 0xFFFF)+1; + u32 address = cheatsList[i].address; + for(int x = 0; x <= count; x++) { + u8 b; + int res = x % 6; + if (res==0) + i++; + if(res < 4) + b = (cheatsList[i].address >> (24-8*res)) & 0xFF; + else + b = (cheatsList[i].value >> (8 - 8*(res-4))) & 0xFF; + CPUWriteByte(address, b); + address++; + } + } + break; + case GSA_8_BIT_POINTER : + if ((CPUReadMemory(cheatsList[i].address)>=0x02000000) && (CPUReadMemory(cheatsList[i].address)<0x02040000) || + (CPUReadMemory(cheatsList[i].address)>=0x03000000) && (CPUReadMemory(cheatsList[i].address)<0x03008000)) + { + CPUWriteByte(CPUReadMemory(cheatsList[i].address)+((cheatsList[i].value & 0xFFFFFF00) >> 8), + cheatsList[i].value & 0xFF); + } + break; + case GSA_16_BIT_POINTER : + if ((CPUReadMemory(cheatsList[i].address)>=0x02000000) && (CPUReadMemory(cheatsList[i].address)<0x02040000) || + (CPUReadMemory(cheatsList[i].address)>=0x03000000) && (CPUReadMemory(cheatsList[i].address)<0x03008000)) + { + CPUWriteHalfWord(CPUReadMemory(cheatsList[i].address)+((cheatsList[i].value & 0xFFFF0000) >> 15), + cheatsList[i].value & 0xFFFF); + } + break; + case GSA_32_BIT_POINTER : + if ((CPUReadMemory(cheatsList[i].address)>=0x02000000) && (CPUReadMemory(cheatsList[i].address)<0x02040000) || + (CPUReadMemory(cheatsList[i].address)>=0x03000000) && (CPUReadMemory(cheatsList[i].address)<0x03008000)) + { + CPUWriteMemory(CPUReadMemory(cheatsList[i].address), + cheatsList[i].value); + } + break; + case GSA_8_BIT_ADD : + CPUWriteByte(cheatsList[i].address, + (cheatsList[i].value & 0xFF) + CPUReadMemory(cheatsList[i].address) & 0xFF); + break; + case GSA_16_BIT_ADD : + CPUWriteHalfWord(cheatsList[i].address, + (cheatsList[i].value & 0xFFFF) + CPUReadMemory(cheatsList[i].address) & 0xFFFF); + break; + case GSA_32_BIT_ADD : + CPUWriteMemory(cheatsList[i].address , + cheatsList[i].value + CPUReadMemory(cheatsList[i].address) & 0xFFFFFFFF); + break; + case GSA_8_BIT_IF_LOWER_U: + if (!(CPUReadByte(cheatsList[i].address) < (cheatsList[i].value & 0xFF))) { + i++; + } + break; + case GSA_16_BIT_IF_LOWER_U: + if (!(CPUReadHalfWord(cheatsList[i].address) < (cheatsList[i].value & 0xFFFF))) { + i++; + } + break; + case GSA_32_BIT_IF_LOWER_U: + if (!(CPUReadMemory(cheatsList[i].address) < cheatsList[i].value)) { + i++; + } + break; + case GSA_8_BIT_IF_HIGHER_U: + if (!(CPUReadByte(cheatsList[i].address) > (cheatsList[i].value & 0xFF))) { + i++; + } + break; + case GSA_16_BIT_IF_HIGHER_U: + if (!(CPUReadHalfWord(cheatsList[i].address) > (cheatsList[i].value & 0xFFFF))) { + i++; + } + break; + case GSA_32_BIT_IF_HIGHER_U: + if (!(CPUReadMemory(cheatsList[i].address) > cheatsList[i].value)) { + i++; + } + break; + case GSA_8_BIT_IF_AND: + if (!(CPUReadByte(cheatsList[i].address) & (cheatsList[i].value & 0xFF))) { + i++; + } + break; + case GSA_16_BIT_IF_AND: + if (!(CPUReadHalfWord(cheatsList[i].address) & (cheatsList[i].value & 0xFFFF))) { + i++; + } + break; + case GSA_32_BIT_IF_AND: + if (!(CPUReadMemory(cheatsList[i].address) & cheatsList[i].value)) { + i++; + } + break; + case GSA_8_BIT_IF_LOWER_U2: + if (!(CPUReadByte(cheatsList[i].address) < (cheatsList[i].value & 0xFF))) { + i+=2; + } + break; + case GSA_16_BIT_IF_LOWER_U2: + if (!(CPUReadHalfWord(cheatsList[i].address) < (cheatsList[i].value & 0xFFFF))) { + i+=2; + } + break; + case GSA_32_BIT_IF_LOWER_U2: + if (!(CPUReadMemory(cheatsList[i].address) < cheatsList[i].value)) { + i+=2; + } + break; + case GSA_8_BIT_IF_HIGHER_U2: + if (!(CPUReadByte(cheatsList[i].address) > (cheatsList[i].value & 0xFF))) { + i+=2; + } + break; + case GSA_16_BIT_IF_HIGHER_U2: + if (!(CPUReadHalfWord(cheatsList[i].address) > (cheatsList[i].value & 0xFFFF))) { + i+=2; + } + break; + case GSA_32_BIT_IF_HIGHER_U2: + if (!(CPUReadMemory(cheatsList[i].address) > cheatsList[i].value)) { + i+=2; + } + break; + case GSA_8_BIT_IF_AND2: + if (!(CPUReadByte(cheatsList[i].address) & (cheatsList[i].value & 0xFF))) { + i+=2; + } + break; + case GSA_16_BIT_IF_AND2: + if (!(CPUReadHalfWord(cheatsList[i].address) & (cheatsList[i].value & 0xFFFF))) { + i+=2; + } + break; + case GSA_32_BIT_IF_AND2: + if (!(CPUReadMemory(cheatsList[i].address) & cheatsList[i].value)) { + i+=2; + } + break; + case GSA_ALWAYS: + i++; + break; + case GSA_ALWAYS2: + i+=2; + break; + case GSA_8_BIT_IF_LOWER_S: + if (!((s8)CPUReadByte(cheatsList[i].address) < ((s8)cheatsList[i].value & 0xFF))) { + i++; + } + break; + case GSA_16_BIT_IF_LOWER_S: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) < ((s16)cheatsList[i].value & 0xFFFF))) { + i++; + } + break; + case GSA_32_BIT_IF_LOWER_S: + if (!((s32)CPUReadMemory(cheatsList[i].address) < (s32)cheatsList[i].value)) { + i++; + } + break; + case GSA_8_BIT_IF_HIGHER_S: + if (!((s8)CPUReadByte(cheatsList[i].address) > ((s8)cheatsList[i].value & 0xFF))) { + i++; + } + break; + case GSA_16_BIT_IF_HIGHER_S: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) > ((s16)cheatsList[i].value & 0xFFFF))) { + i++; + } + break; + case GSA_32_BIT_IF_HIGHER_S: + if (!((s32)CPUReadMemory(cheatsList[i].address) > (s32)cheatsList[i].value)) { + i++; + } + break; + case GSA_8_BIT_IF_LOWER_S2: + if (!((s8)CPUReadByte(cheatsList[i].address) < ((s8)cheatsList[i].value & 0xFF))) { + i+=2; + } + break; + case GSA_16_BIT_IF_LOWER_S2: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) < ((s16)cheatsList[i].value & 0xFFFF))) { + i+=2; + } + break; + case GSA_32_BIT_IF_LOWER_S2: + if (!((s32)CPUReadMemory(cheatsList[i].address) < (s32)cheatsList[i].value)) { + i+=2; + } + break; + case GSA_8_BIT_IF_HIGHER_S2: + if (!((s8)CPUReadByte(cheatsList[i].address) > ((s8)cheatsList[i].value & 0xFF))) { + i+=2; + } + break; + case GSA_16_BIT_IF_HIGHER_S2: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) > ((s16)cheatsList[i].value & 0xFFFF))) { + i+=2; + } + break; + case GSA_32_BIT_IF_HIGHER_S2: + if (!((s32)CPUReadMemory(cheatsList[i].address) > (s32)cheatsList[i].value)) { + i+=2; + } + break; + case GSA_16_BIT_WRITE_IOREGS: + if ((cheatsList[i].address <= 0x3FF) && (cheatsList[i].address != 0x6) && + (cheatsList[i].address != 0x130)) + ioMem[cheatsList[i].address & 0x3FE]=cheatsList[i].value & 0xFFFF; + break; + case GSA_32_BIT_WRITE_IOREGS: + if (cheatsList[i].address<=0x3FF) + { + if (((cheatsList[i].address & 0x3FC) != 0x6) && ((cheatsList[i].address & 0x3FC) != 0x130)) + ioMem[cheatsList[i].address & 0x3FC]= (cheatsList[i].value & 0xFFFF); + if ((((cheatsList[i].address & 0x3FC)+2) != 0x6) && ((cheatsList[i].address & 0x3FC) +2) != 0x130) + ioMem[(cheatsList[i].address & 0x3FC) + 2 ]= ((cheatsList[i].value>>16 ) & 0xFFFF); + } + break; + case GSA_8_BIT_IF_TRUE3: + if(CPUReadByte(cheatsList[i].address) != cheatsList[i].value) { + onoff=false; + } + break; + case GSA_16_BIT_IF_TRUE3: + if(CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) { + onoff=false; + } + break; + case GSA_32_BIT_IF_TRUE3: + if(CPUReadMemory(cheatsList[i].address) != cheatsList[i].value) { + onoff=false; + } + break; + case GSA_8_BIT_IF_FALSE3: + if(CPUReadByte(cheatsList[i].address) == cheatsList[i].value) { + onoff=false; + } + break; + case GSA_16_BIT_IF_FALSE3: + if(CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value) { + onoff=false; + } + break; + case GSA_32_BIT_IF_FALSE3: + if(CPUReadMemory(cheatsList[i].address) == cheatsList[i].value) { + onoff=false; + } + break; + case GSA_8_BIT_IF_LOWER_S3: + if (!((s8)CPUReadByte(cheatsList[i].address) < ((s8)cheatsList[i].value & 0xFF))) { + onoff=false; + } + break; + case GSA_16_BIT_IF_LOWER_S3: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) < ((s16)cheatsList[i].value & 0xFFFF))) { + onoff=false; + } + break; + case GSA_32_BIT_IF_LOWER_S3: + if (!((s32)CPUReadMemory(cheatsList[i].address) < (s32)cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_8_BIT_IF_HIGHER_S3: + if (!((s8)CPUReadByte(cheatsList[i].address) > ((s8)cheatsList[i].value & 0xFF))) { + onoff=false; + } + break; + case GSA_16_BIT_IF_HIGHER_S3: + if (!((s16)CPUReadHalfWord(cheatsList[i].address) > ((s16)cheatsList[i].value & 0xFFFF))) { + onoff=false; + } + break; + case GSA_32_BIT_IF_HIGHER_S3: + if (!((s32)CPUReadMemory(cheatsList[i].address) > (s32)cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_8_BIT_IF_LOWER_U3: + if (!(CPUReadByte(cheatsList[i].address) < (cheatsList[i].value & 0xFF))) { + onoff=false; + } + break; + case GSA_16_BIT_IF_LOWER_U3: + if (!(CPUReadHalfWord(cheatsList[i].address) < (cheatsList[i].value & 0xFFFF))) { + onoff=false; + } + break; + case GSA_32_BIT_IF_LOWER_U3: + if (!(CPUReadMemory(cheatsList[i].address) < cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_8_BIT_IF_HIGHER_U3: + if (!(CPUReadByte(cheatsList[i].address) > (cheatsList[i].value & 0xFF))) { + onoff=false; + } + break; + case GSA_16_BIT_IF_HIGHER_U3: + if (!(CPUReadHalfWord(cheatsList[i].address) > (cheatsList[i].value & 0xFFFF))) { + onoff=false; + } + break; + case GSA_32_BIT_IF_HIGHER_U3: + if (!(CPUReadMemory(cheatsList[i].address) > cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_8_BIT_IF_AND3: + if (!(CPUReadByte(cheatsList[i].address) & (cheatsList[i].value & 0xFF))) { + onoff=false; + } + break; + case GSA_16_BIT_IF_AND3: + if (!(CPUReadHalfWord(cheatsList[i].address) & (cheatsList[i].value & 0xFFFF))) { + onoff=false; + } + break; + case GSA_32_BIT_IF_AND3: + if (!(CPUReadMemory(cheatsList[i].address) & cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_ALWAYS3: + if (!(CPUReadMemory(cheatsList[i].address) & cheatsList[i].value)) { + onoff=false; + } + break; + case GSA_GROUP_WRITE: + { + int count = ((cheatsList[i].address) & 0xFFFE) +1; + u32 value = cheatsList[i].value; + if (count==0) + i++; + else + for (int x = 1; x <= count; x++) { + if ((x % 2) ==0){ + if (x cheatsList[i].value) { + i++; + } + break; + case GSA_16_BIT_IF_HIGHER_OR_EQ_U: + if(CPUReadHalfWord(cheatsList[i].address) < cheatsList[i].value) { + i++; + } + break; + case GSA_16_BIT_MIF_TRUE: + if(CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) { + i+=((cheatsList[i].rawaddress >> 0x10) & 0xFF); + } + break; + case GSA_16_BIT_MIF_FALSE: + if(CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value) { + i+=(cheatsList[i].rawaddress >> 0x10) & 0xFF; + } + break; + case GSA_16_BIT_MIF_LOWER_OR_EQ_U: + if(CPUReadHalfWord(cheatsList[i].address) > cheatsList[i].value) { + i+=(cheatsList[i].rawaddress >> 0x10) & 0xFF; + } + break; + case GSA_16_BIT_MIF_HIGHER_OR_EQ_U: + if(CPUReadHalfWord(cheatsList[i].address) < cheatsList[i].value) { + i+=(cheatsList[i].rawaddress >> 0x10) & 0xFF; + } + break; + } + } + } + for (i = 0; i<4; i++) + if (rompatch2addr [i] != 0) + CHEAT_PATCH_ROM_16BIT(rompatch2addr [i],rompatch2val [i]); + return ticks; +} + +void cheatsAdd(const char *codeStr, + const char *desc, + u32 rawaddress, + u32 address, + u32 value, + int code, + int size) +{ + if(cheatsNumber < 100) { + int x = cheatsNumber; + cheatsList[x].code = code; + cheatsList[x].size = size; + cheatsList[x].rawaddress = rawaddress; + cheatsList[x].address = address; + cheatsList[x].value = value; + strcpy(cheatsList[x].codestring, codeStr); + strcpy(cheatsList[x].desc, desc); + cheatsList[x].enabled = true; + cheatsList[x].status = 0; + + // we only store the old value for this simple codes. ROM patching + // is taken care when it actually patches the ROM + switch(cheatsList[x].size) { + case INT_8_BIT_WRITE: + cheatsList[x].oldValue = CPUReadByte(address); + break; + case INT_16_BIT_WRITE: + cheatsList[x].oldValue = CPUReadHalfWord(address); + break; + case INT_32_BIT_WRITE: + cheatsList[x].oldValue = CPUReadMemory(address); + break; + } + cheatsNumber++; + } +} + +void cheatsDelete(int number, bool restore) +{ + if(number < cheatsNumber && number >= 0) { + int x = number; + + if(restore) { + switch(cheatsList[x].size) { + case INT_8_BIT_WRITE: + CPUWriteByte(cheatsList[x].address, (u8)cheatsList[x].oldValue); + break; + case INT_16_BIT_WRITE: + CPUWriteHalfWord(cheatsList[x].address, (u16)cheatsList[x].oldValue); + break; + case INT_32_BIT_WRITE: + CPUWriteMemory(cheatsList[x].address, cheatsList[x].oldValue); + break; + case GSA_16_BIT_ROM_PATCH: + if(cheatsList[x].status & 1) { + cheatsList[x].status &= ~1; + CHEAT_PATCH_ROM_16BIT(cheatsList[x].address, + cheatsList[x].oldValue); + } + break; + case GSA_16_BIT_ROM_PATCH2C: + case GSA_16_BIT_ROM_PATCH2D: + case GSA_16_BIT_ROM_PATCH2E: + case GSA_16_BIT_ROM_PATCH2F: + if(cheatsList[x].status & 1) { + cheatsList[x].status &= ~1; + } + break; + } + } + if((x+1) < cheatsNumber) { + memcpy(&cheatsList[x], &cheatsList[x+1], sizeof(CheatsData)* + (cheatsNumber-x-1)); + } + cheatsNumber--; + } +} + +void cheatsDeleteAll(bool restore) +{ + for(int i = cheatsNumber-1; i >= 0; i--) { + cheatsDelete(i, restore); + } +} + +void cheatsEnable(int i) +{ + if(i >= 0 && i < cheatsNumber) { + cheatsList[i].enabled = true; + } +} + +void cheatsDisable(int i) +{ + if(i >= 0 && i < cheatsNumber) { + switch(cheatsList[i].size) { + case GSA_16_BIT_ROM_PATCH: + if(cheatsList[i].status & 1) { + cheatsList[i].status &= ~1; + CHEAT_PATCH_ROM_16BIT(cheatsList[i].address, + cheatsList[i].oldValue); + } + break; + case GSA_16_BIT_ROM_PATCH2C: + case GSA_16_BIT_ROM_PATCH2D: + case GSA_16_BIT_ROM_PATCH2E: + case GSA_16_BIT_ROM_PATCH2F: + if(cheatsList[i].status & 1) { + cheatsList[i].status &= ~1; + } + break; + } + cheatsList[i].enabled = false; + } +} + +bool cheatsVerifyCheatCode(const char *code, const char *desc) +{ + int len = strlen(code); + if(len != 11 && len != 13 && len != 17) { + systemMessage(MSG_INVALID_CHEAT_CODE, N_("Invalid cheat code '%s'"), code); + return false; + } + + if(code[8] != ':') { + systemMessage(MSG_INVALID_CHEAT_CODE, N_("Invalid cheat code '%s'"), code); + return false; + } + + int i; + for(i = 0; i < 8; i++) { + if(!CHEAT_IS_HEX(code[i])) { + // wrong cheat + systemMessage(MSG_INVALID_CHEAT_CODE, + N_("Invalid cheat code '%s'"), code); + return false; + } + } + for(i = 9; i < len; i++) { + if(!CHEAT_IS_HEX(code[i])) { + // wrong cheat + systemMessage(MSG_INVALID_CHEAT_CODE, + N_("Invalid cheat code '%s'"), code); + return false; + } + } + + u32 address = 0; + u32 value = 0; + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + sscanf(buffer, "%x", &address); + + switch(address >> 24) { + case 2: + case 3: + break; + default: + systemMessage(MSG_INVALID_CHEAT_CODE_ADDRESS, + N_("Invalid cheat code address: %08x"), + address); + return false; + } + + strncpy(buffer, &code[9], 8); + sscanf(buffer, "%x", &value); + int type = 0; + if(len == 13) + type = 1; + if(len == 17) + type = 2; + cheatsAdd(code, desc, address, address, value, type, type); + return true; +} + +void cheatsAddCheatCode(const char *code, const char *desc) +{ + cheatsVerifyCheatCode(code, desc); +} + +u16 cheatsGSAGetDeadface(bool v3) +{ + for(int i = cheatsNumber-1; i >= 0; i--) + if ((cheatsList[i].address == 0xDEADFACE) && (cheatsList[i].code == (v3 ? 257 : 256))) + return cheatsList[i].value & 0xFFFF; + return 0; +} + +void cheatsGSAChangeEncryption(u16 value, bool v3) { + int i; + u8 *deadtable1, *deadtable2; + + if (v3) { + deadtable1 = (u8*)(&v3_deadtable1); + deadtable2 = (u8*)(&v3_deadtable2); + for (i = 0; i < 4; i++) + seeds_v3[i] = seed_gen(((value & 0xFF00) >> 8), (value & 0xFF) + i, deadtable1, deadtable2); + } + else { + deadtable1 = (u8*)(&v1_deadtable1); + deadtable2 = (u8*)(&v1_deadtable2); + for (i = 0; i < 4; i++){ + seeds_v1[i] = seed_gen(((value & 0xFF00) >> 8), (value & 0xFF) + i, deadtable1, deadtable2); + } + } +} + +u32 seed_gen(u8 upper, u8 seed, u8 *deadtable1, u8 *deadtable2) { + int i; + u32 newseed = 0; + + for (i = 0; i < 4; i++) + newseed = ((newseed << 8) | ((deadtable1[(i + upper) & 0xFF] + deadtable2[seed]) & 0xFF)); + + return newseed; +} + +void cheatsDecryptGSACode(u32& address, u32& value, bool v3) +{ + u32 rollingseed = 0xC6EF3720; + u32 *seeds = v3 ? seeds_v3 : seeds_v1; + + int bitsleft = 32; + while (bitsleft > 0) { + value -= ((((address << 4) + seeds[2]) ^ (address + rollingseed)) ^ + ((address >> 5) + seeds[3])); + address -= ((((value << 4) + seeds[0]) ^ (value + rollingseed)) ^ + ((value >> 5) + seeds[1])); + rollingseed -= 0x9E3779B9; + bitsleft--; + } +} + +void cheatsAddGSACode(const char *code, const char *desc, bool v3) +{ + if(strlen(code) != 16) { + // wrong cheat + systemMessage(MSG_INVALID_GSA_CODE, + N_("Invalid GSA code. Format is XXXXXXXXYYYYYYYY")); + return; + } + + int i; + for(i = 0; i < 16; i++) { + if(!CHEAT_IS_HEX(code[i])) { + // wrong cheat + systemMessage(MSG_INVALID_GSA_CODE, + N_("Invalid GSA code. Format is XXXXXXXXYYYYYYYY")); + return; + } + } + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + strncpy(buffer, &code[8], 8); + buffer[8] = 0; + u32 value; + sscanf(buffer, "%x", &value); + cheatsGSAChangeEncryption(cheatsGSAGetDeadface (v3), v3); + cheatsDecryptGSACode(address, value, v3); + + if(value == 0x1DC0DE) { + u32 gamecode = READ32LE(((u32 *)&rom[0xac])); + if(gamecode != address) { + char buffer[5]; + *((u32 *)buffer) = address; + buffer[4] = 0; + char buffer2[5]; + *((u32 *)buffer2) = READ32LE(((u32 *)&rom[0xac])); + buffer2[4] = 0; + systemMessage(MSG_GBA_CODE_WARNING, N_("Warning: cheats are for game %s. Current game is %s.\nCodes may not work correctly."), + buffer, buffer2); + } + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, v3 ? 257 : 256, + UNKNOWN_CODE); + return; + } + if(isMultilineWithData(cheatsNumber-1)) { + cheatsAdd(code, desc, address, address, value, v3 ? 257 : 256, UNKNOWN_CODE); + return; + } + if(v3) { + int type = ((address >> 25) & 127) | ((address >> 17) & 0x80); + u32 addr = (address & 0x00F00000) << 4 | (address & 0x0003FFFF); + switch(type) { + case 0x00: + if(address == 0) { + type = (value >> 25) & 127; + addr = (value & 0x00F00000) << 4 | (value & 0x0003FFFF); + switch(type) { + case 0x04: + cheatsAdd(code, desc, address, 0, value & 0x00FFFFFF, 257, GSA_SLOWDOWN); + break; + case 0x08: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_8_BIT_GS_WRITE2); + break; + case 0x09: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_16_BIT_GS_WRITE2); + break; + case 0x0a: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_32_BIT_GS_WRITE2); + break; + case 0x0c: + cheatsAdd(code, desc, address, 0, value & 0x00FFFFFF, 257, GSA_16_BIT_ROM_PATCH2C); + break; + case 0x0d: + cheatsAdd(code, desc, address, 0, value & 0x00FFFFFF, 257, GSA_16_BIT_ROM_PATCH2D); + break; + case 0x0e: + cheatsAdd(code, desc, address, 0, value & 0x00FFFFFF, 257, GSA_16_BIT_ROM_PATCH2E); + break; + case 0x0f: + cheatsAdd(code, desc, address, 0, value & 0x00FFFFFF, 257, GSA_16_BIT_ROM_PATCH2F); + break; + case 0x20: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_CODES_ON); + break; + case 0x40: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_8_BIT_SLIDE); + break; + case 0x41: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_16_BIT_SLIDE); + break; + case 0x42: + cheatsAdd(code, desc, address, 0, addr, 257, GSA_32_BIT_SLIDE); + break; + default: + cheatsAdd(code, desc, address, address, value, 257, UNKNOWN_CODE); + break; + } + } else + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_FILL); + break; + case 0x01: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_FILL); + break; + case 0x02: + cheatsAdd(code, desc, address, addr, value, 257, INT_32_BIT_WRITE); + break; + case 0x04: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_TRUE); + break; + case 0x05: + cheatsAdd(code, desc, address, addr, value, 257, CBA_IF_TRUE); + break; + case 0x06: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_TRUE); + break; + case 0x07: + cheatsAdd(code, desc, address, addr, value, 257, GSA_ALWAYS); + break; + case 0x08: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_FALSE); + break; + case 0x09: + cheatsAdd(code, desc, address, addr, value, 257, CBA_IF_FALSE); + break; + case 0x0a: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_FALSE); + break; + case 0xc: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_S); + break; + case 0xd: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_S); + break; + case 0xe: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_S); + break; + case 0x10: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_S); + break; + case 0x11: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_S); + break; + case 0x12: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_S); + break; + case 0x14: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_U); + break; + case 0x15: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_U); + break; + case 0x16: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_U); + break; + case 0x18: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_U); + break; + case 0x19: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_U); + break; + case 0x1A: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_U); + break; + case 0x1C: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_AND); + break; + case 0x1D: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_AND); + break; + case 0x1E: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_AND); + break; + case 0x20: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_POINTER); + break; + case 0x21: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_POINTER); + break; + case 0x22: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_POINTER); + break; + case 0x24: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_TRUE2); + break; + case 0x25: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_TRUE2); + break; + case 0x26: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_TRUE2); + break; + case 0x27: + cheatsAdd(code, desc, address, addr, value, 257, GSA_ALWAYS2); + break; + case 0x28: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_FALSE2); + break; + case 0x29: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_FALSE2); + break; + case 0x2a: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_FALSE2); + break; + case 0x2c: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_S2); + break; + case 0x2d: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_S2); + break; + case 0x2e: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_S2); + break; + case 0x30: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_S2); + break; + case 0x31: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_S2); + break; + case 0x32: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_S2); + break; + case 0x34: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_U2); + break; + case 0x35: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_U2); + break; + case 0x36: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_U2); + break; + case 0x38: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_U2); + break; + case 0x39: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_U2); + break; + case 0x3A: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_U2); + break; + case 0x3C: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_AND2); + break; + case 0x3D: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_AND2); + break; + case 0x3E: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_AND2); + break; + case 0x40: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_ADD); + break; + case 0x41: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_ADD); + break; + case 0x42: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_ADD); + break; + case 0x44: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_TRUE3); + break; + case 0x45: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_TRUE3); + break; + case 0x46: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_TRUE3); + break; + case 0x47: + cheatsAdd(code, desc, address, addr, value, 257, GSA_ALWAYS3); + break; + case 0x48: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_FALSE3); + break; + case 0x49: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_FALSE3); + break; + case 0x4a: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_FALSE3); + break; + case 0x4c: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_S3); + break; + case 0x4d: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_S3); + break; + case 0x4e: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_S3); + break; + case 0x50: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_S3); + break; + case 0x51: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_S3); + break; + case 0x52: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_S3); + break; + case 0x54: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_LOWER_U3); + break; + case 0x55: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_LOWER_U3); + break; + case 0x56: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_LOWER_U3); + break; + case 0x58: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_HIGHER_U3); + break; + case 0x59: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_HIGHER_U3); + break; + case 0x5a: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_HIGHER_U3); + break; + case 0x5c: + cheatsAdd(code, desc, address, addr, value, 257, GSA_8_BIT_IF_AND3); + break; + case 0x5d: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_IF_AND3); + break; + case 0x5e: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_IF_AND3); + break; + case 0x63: + cheatsAdd(code, desc, address, addr, value, 257, GSA_16_BIT_WRITE_IOREGS); + break; + case 0xE3: + cheatsAdd(code, desc, address, addr, value, 257, GSA_32_BIT_WRITE_IOREGS); + break; + default: + cheatsAdd(code, desc, address, address, value, 257, UNKNOWN_CODE); + break; + } + } else { + int type = (address >> 28) & 15; + switch(type) { + case 0: + case 1: + case 2: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 256, type); + break; + case 3: + switch ((address >> 0x10) & 0xFF){ + case 0x00: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 256, GSA_GROUP_WRITE); + break; + case 0x10: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFF, 256, GSA_32_BIT_ADD ); + break; + case 0x20: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, (~(address & 0xFF)+1), 256, GSA_32_BIT_ADD ); + break; + case 0x30: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFFFF, 256, GSA_32_BIT_ADD ); + break; + case 0x40: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, (~(address & 0xFFFF)+1), 256, GSA_32_BIT_ADD ); + break; + case 0x50: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 256, GSA_32_BIT_ADD2); + break; + case 0x60: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 256, GSA_32_BIT_SUB2); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + } + break; + case 6: + address <<= 1; + type = (value >> 24) & 0xFF; + if(type == 0x00) { + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0xFFFF, 256, + GSA_16_BIT_ROM_PATCH); + break; + } + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + case 8: + switch((address >> 20) & 15) { + case 1: + cheatsAdd(code, desc, address, address & 0x0F0FFFFF, value, 256, + GSA_8_BIT_GS_WRITE); + break; + case 2: + cheatsAdd(code, desc, address, address & 0x0F0FFFFF, value, 256, + GSA_16_BIT_GS_WRITE); + break; + case 4: + // This code is buggy : the value is always set to 0 ! + cheatsAdd(code, desc, address, address & 0x0F0FFFFF, 0, 256, + GSA_32_BIT_GS_WRITE); + break; + case 15: + cheatsAdd(code, desc, address, 0, value & 0xFFFF, 256, GSA_SLOWDOWN); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + } + break; + case 0x0d: + if(address != 0xDEADFACE) { + switch((value >> 20) & 0xF) { + case 0: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0xFFFF, 256, + CBA_IF_TRUE); + break; + case 1: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0xFFFF, 256, + CBA_IF_FALSE); + break; + case 2: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0xFFFF, 256, + GSA_16_BIT_IF_LOWER_OR_EQ_U); + break; + case 3: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0xFFFF, 256, + GSA_16_BIT_IF_HIGHER_OR_EQ_U); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + } + } else + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + case 0x0e: + switch((value >> 28) & 0xF) { + case 0: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFFFF, 256, + GSA_16_BIT_MIF_TRUE); + break; + case 1: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFFFF, 256, + GSA_16_BIT_MIF_FALSE); + break; + case 2: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFFFF, 256, + GSA_16_BIT_MIF_LOWER_OR_EQ_U); + break; + case 3: + cheatsAdd(code, desc, address, value & 0x0FFFFFFF, address & 0xFFFF, 256, + GSA_16_BIT_MIF_HIGHER_OR_EQ_U); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + } + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address, value, 256, + UNKNOWN_CODE); + break; + } + } +} + +bool cheatsImportGSACodeFile(const char *name, int game, bool v3) +{ + FILE *f = fopen(name, "rb"); + if(!f) + return false; + + int games = 0; + int len = 0; + fseek(f, 0x1e, SEEK_CUR); + fread(&games, 1, 4, f); + bool found = false; + int g = 0; + while(games > 0) { + if(g == game) { + found = true; + break; + } + fread(&len, 1, 4, f); + fseek(f,len,SEEK_CUR); + int codes = 0; + fread(&codes, 1, 4, f); + while(codes > 0) { + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 8, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len*12, SEEK_CUR); + codes--; + } + games--; + g++; + } + if(found) { + char desc[256]; + char code[17]; + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + int codes = 0; + fread(&codes, 1, 4, f); + while(codes > 0) { + fread(&len, 1, 4, f); + fread(desc, 1, len, f); + desc[len] =0; + desc[31] = 0; + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 4, SEEK_CUR); + fread(&len, 1, 4, f); + while(len) { + fseek(f, 4, SEEK_CUR); + fread(code, 1, 8, f); + fseek(f, 4, SEEK_CUR); + fread(&code[8], 1, 8, f); + code[16] = 0; + cheatsAddGSACode(code, desc, v3); + len -= 2; + } + codes--; + } + } + fclose(f); + return false; +} + +void cheatsCBAReverseArray(u8 *array, u8 *dest) +{ + dest[0] = array[3]; + dest[1] = array[2]; + dest[2] = array[1]; + dest[3] = array[0]; + dest[4] = array[5]; + dest[5] = array[4]; +} + +void chatsCBAScramble(u8 *array, int count, u8 b) +{ + u8 *x = array + (count >> 3); + u8 *y = array + (b >> 3); + u32 z = *x & (1 << (count & 7)); + u32 x0 = (*x & (~(1 << (count & 7)))); + if (z != 0) + z = 1; + if ((*y & (1 << (b & 7))) != 0) + x0 |= (1 << (count & 7)); + *x = x0; + u32 temp = *y & (~(1 << (b & 7))); + if (z != 0) + temp |= (1 << (b & 7)); + *y = temp; +} + +u32 cheatsCBAGetValue(u8 *array) +{ + return array[0] | array[1]<<8 | array[2] << 16 | array[3]<<24; +} + +u16 cheatsCBAGetData(u8 *array) +{ + return array[4] | array[5]<<8; +} + +void cheatsCBAArrayToValue(u8 *array, u8 *dest) +{ + dest[0] = array[3]; + dest[1] = array[2]; + dest[2] = array[1]; + dest[3] = array[0]; + dest[4] = array[5]; + dest[5] = array[4]; +} + +void cheatsCBAParseSeedCode(u32 address, u32 value, u32 *array) +{ + array[0] = 1; + array[1] = value & 0xFF; + array[2] = (address >> 0x10) & 0xFF; + array[3] = (value >> 8) & 0xFF; + array[4] = (address >> 0x18) & 0x0F; + array[5] = address & 0xFFFF; + array[6] = address; + array[7] = value; +} + +u32 cheatsCBAEncWorker() +{ + u32 x = (cheatsCBATemporaryValue * 0x41c64e6d) + 0x3039; + u32 y = (x * 0x41c64e6d) + 0x3039; + u32 z = x >> 0x10; + x = ((y >> 0x10) & 0x7fff) << 0x0f; + z = (z << 0x1e) | x; + x = (y * 0x41c64e6d) + 0x3039; + cheatsCBATemporaryValue = x; + return z | ((x >> 0x10) & 0x7fff); +} + +#define ROR(v, s) \ + (((v) >> (s)) | (((v) & ((1 << (s))-1)) << (32 - (s)))) + +u32 cheatsCBACalcIndex(u32 x, u32 y) +{ + if(y != 0) { + if(y == 1) + x = 0; + else if(x == y) + x = 0; + if(y < 1) + return x; + else if(x < y) + return x; + u32 x0 = 1; + + while(y < 0x10000000) { + if(y < x) { + y = y << 4; + x0 = x0 << 4; + } else break; + } + + while(y < 0x80000000) { + if(y < x) { + y = y << 1; + x0 = x0 << 1; + } else break; + } + + loop: + u32 z = 0; + if(x >= y) + x -= y; + if(x >= (y >> 1)) { + x -= (y >> 1); + z |= ROR(x0, 1); + } + if(x >= (y >> 2)) { + x -= (y >> 2); + z |= ROR(x0, 2); + } + if(x >= (y >> 3)) { + x -= (y >> 3); + z |= ROR(x0, 3); + } + + u32 temp = x0; + + if(x != 0) { + x0 = x0 >> 4; + if(x0 != 0) { + y = y >> 4; + goto loop; + } + } + + z = z & 0xe0000000; + + if(z != 0) { + if((temp & 7) == 0) + return x; + } else + return x; + + if((z & ROR(temp, 3)) != 0) + x += y >> 3; + if((z & ROR(temp, 2)) != 0) + x += y >> 2; + if((z & ROR(temp, 1)) != 0) + x += y >> 1; + return x; + } else { + } + // should not happen in the current code + return 0; +} + +void cheatsCBAUpdateSeedBuffer(u32 a, u8 *buffer, int count) +{ + int i; + for(i = 0; i < count; i++) + buffer[i] = i; + for(i = 0; (u32)i < a; i++) { + u32 a = cheatsCBACalcIndex(cheatsCBAEncWorker(), count); + u32 b = cheatsCBACalcIndex(cheatsCBAEncWorker(), count); + u32 t = buffer[a]; + buffer[a] = buffer[b]; + buffer[b] = t; + } +} + +void cheatsCBAChangeEncryption(u32 *seed) +{ + int i; + + cheatsCBATemporaryValue = (seed[1] ^ 0x1111); + cheatsCBAUpdateSeedBuffer(0x50, cheatsCBASeedBuffer, 0x30); + cheatsCBATemporaryValue = 0x4efad1c3; + + for(i = 0; (u32)i < seed[4]; i++) { + cheatsCBATemporaryValue = cheatsCBAEncWorker(); + } + cheatsCBASeed[2] = cheatsCBAEncWorker(); + cheatsCBASeed[3] = cheatsCBAEncWorker(); + + cheatsCBATemporaryValue = seed[3] ^ 0xf254; + + for(i = 0; (u32)i < seed[3]; i++) { + cheatsCBATemporaryValue = cheatsCBAEncWorker(); + } + + cheatsCBASeed[0] = cheatsCBAEncWorker(); + cheatsCBASeed[1] = cheatsCBAEncWorker(); + + *((u32 *)&cheatsCBACurrentSeed[0]) = seed[6]; + *((u32 *)&cheatsCBACurrentSeed[4]) = seed[7]; + *((u32 *)&cheatsCBACurrentSeed[8]) = 0; +} + +u16 cheatsCBAGenValue(u32 x, u32 y, u32 z) +{ + y <<= 0x10; + z <<= 0x10; + x <<= 0x18; + u32 x0 = (int)y >> 0x10; + z = (int)z >> 0x10; + x = (int)x >> 0x10; + for(int i = 0; i < 8; i++) { + u32 temp = z ^ x; + if ((int)temp >= 0) { + temp = z << 0x11; + } + else { + temp = z << 0x01; + temp ^= x0; + temp = temp << 0x10; + } + z = (int)temp >> 0x10; + temp = x << 0x11; + x = (int)temp >> 0x10; + } + return z & 0xffff; +} + +void cheatsCBAGenTable() { + for (int i = 0; i < 0x100; i++) { + cheatsCBATable[i] = cheatsCBAGenValue(i, 0x1021, 0); + } + cheatsCBATableGenerated = true; +} + +u16 cheatsCBACalcCRC(u8 *rom, int count) +{ + u32 crc = 0xffffffff; + + if (count & 3) { + // 0x08000EAE + } else { + count = (count >> 2) - 1; + if(count != -1) { + while(count != -1) { + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + count--; + } + } + } + return crc & 0xffff; +} + +void cheatsCBADecrypt(u8 *decrypt) +{ + u8 buffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + u8 *array = &buffer[1]; + + cheatsCBAReverseArray(decrypt, array); + + for(int count = 0x2f; count >= 0; count--) { + chatsCBAScramble(array, count, cheatsCBASeedBuffer[count]); + } + cheatsCBAArrayToValue(array, decrypt); + *((u32 *)decrypt) = cheatsCBAGetValue(decrypt) ^ + cheatsCBASeed[0]; + *((u16 *)(decrypt+4)) = (cheatsCBAGetData(decrypt) ^ + cheatsCBASeed[1]) & 0xffff; + + cheatsCBAReverseArray(decrypt, array); + + u32 cs = cheatsCBAGetValue(cheatsCBACurrentSeed); + for(int i = 0; i <= 4; i++) { + array[i] = ((cs >> 8) ^ array[i+1]) ^ array[i] ; + } + + array[5] = (cs >> 8) ^ array[5]; + + for(int j = 5; j >=0; j--) { + array[j] = (cs ^ array[j-1]) ^ array[j]; + } + + cheatsCBAArrayToValue(array, decrypt); + + *((u32 *)decrypt) = cheatsCBAGetValue(decrypt) + ^ cheatsCBASeed[2]; + *((u16 *)(decrypt+4)) = (cheatsCBAGetData(decrypt) + ^ cheatsCBASeed[3]) & 0xffff; +} + +int cheatsCBAGetCount() +{ + int count = 0; + for(int i = 0; i < cheatsNumber; i++) { + if(cheatsList[i].code == 512) + count++; + } + return count; +} + +bool cheatsCBAShouldDecrypt() +{ + for(int i = 0; i < cheatsNumber; i++) { + if(cheatsList[i].code == 512) { + return (cheatsList[i].codestring[0] == '9'); + } + } + return false; +} + +void cheatsAddCBACode(const char *code, const char *desc) +{ + if(strlen(code) != 13) { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + + int i; + for(i = 0; i < 8; i++) { + if(!CHEAT_IS_HEX(code[i])) { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + } + + if(code[8] != ' ') { + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + + for(i = 9; i < 13; i++) { + if(!CHEAT_IS_HEX(code[i])) { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + } + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + strncpy(buffer, &code[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u8 array[8] = { + address & 255, + (address >> 8) & 255, + (address >> 16) & 255, + (address >> 24) & 255, + (value & 255), + (value >> 8) & 255, + 0, + 0 + }; + + if(cheatsCBAGetCount() == 0 && + (address >> 28) == 9) { + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 512, UNKNOWN_CODE); + } else { + if(cheatsCBAShouldDecrypt()) + cheatsCBADecrypt(array); + + address = READ32LE(((u32 *)array)); + value = READ16LE(((u16 *)&array[4])); + + int type = (address >> 28) & 15; + + if(isMultilineWithData(cheatsNumber-1) || (super>0)) { + cheatsAdd(code, desc, address, address, value, 512, UNKNOWN_CODE); + if (super>0) + super-= 1; + return; + } + + switch(type) { + case 0x00: + { + if(!cheatsCBATableGenerated) + cheatsCBAGenTable(); + u32 crc = cheatsCBACalcCRC(rom, 0x10000); + if(crc != address) { + systemMessage(MSG_CBA_CODE_WARNING, + N_("Warning: Codes seem to be for a different game.\nCodes may not work correctly.")); + } + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 512, + UNKNOWN_CODE); + } + break; + case 0x02: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_OR); + break; + case 0x03: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value, 512, + INT_8_BIT_WRITE); + break; + case 0x04: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_SLIDE_CODE); + break; + case 0x05: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_SUPER); + super = getCodeLength(cheatsNumber-1); + break; + case 0x06: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_AND); + break; + case 0x07: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_IF_TRUE); + break; + case 0x08: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + INT_16_BIT_WRITE); + break; + case 0x0a: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_IF_FALSE); + break; + case 0x0b: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_GT); + break; + case 0x0c: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + CBA_LT); + break; + case 0x0d: + if ((address & 0xF0)<0x30) + cheatsAdd(code, desc, address, address & 0xF0, value, 512, + CBA_IF_KEYS_PRESSED); + break; + case 0x0e: + cheatsAdd(code, desc, address, address & 0x0FFFFFFF, value & 0x8000 ? value | 0xFFFF0000 : value, 512, + CBA_ADD); + break; + case 0x0f: + cheatsAdd(code, desc, address, address & 0x0FFFFFFE, value, 512, + GSA_16_BIT_IF_AND); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, address & 0xFFFFFFFF, value, 512, + UNKNOWN_CODE); + break; + } + } +} + +void cheatsSaveGame(gzFile file) +{ + utilWriteInt(file, cheatsNumber); + + utilGzWrite(file, cheatsList, sizeof(cheatsList)); +} + +void cheatsReadGame(gzFile file) +{ + cheatsNumber = 0; + + cheatsNumber = utilReadInt(file); + + utilGzRead(file, cheatsList, sizeof(cheatsList)); + + bool firstCodeBreaker = true; + + for(int i = 0; i < cheatsNumber; i++) { + cheatsList[i].status = 0; + if(!cheatsList[i].codestring[0]) { + switch(cheatsList[i].size) { + case 0: + sprintf(cheatsList[i].codestring, "%08x:%02x", cheatsList[i].address, + cheatsList[i].value); + break; + case 1: + sprintf(cheatsList[i].codestring, "%08x:%04x", cheatsList[i].address, + cheatsList[i].value); + break; + case 2: + sprintf(cheatsList[i].codestring, "%08x:%08x", cheatsList[i].address, + cheatsList[i].value); + break; + } + } + + if(cheatsList[i].enabled) { + cheatsEnable(i); + } + + if(cheatsList[i].code == 512 && firstCodeBreaker) { + firstCodeBreaker = false; + char buffer[10]; + strncpy(buffer, cheatsList[i].codestring, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + if((address >> 28) == 9) { + strncpy(buffer, &cheatsList[i].codestring[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + } + } + } +} + +void cheatsSaveCheatList(const char *file) +{ + if(cheatsNumber == 0) + return; + FILE *f = fopen(file, "wb"); + if(f == NULL) + return; + int version = 1; + fwrite(&version, 1, sizeof(version), f); + int type = 0; + fwrite(&type, 1, sizeof(type), f); + fwrite(&cheatsNumber, 1, sizeof(cheatsNumber), f); + fwrite(cheatsList, 1, sizeof(cheatsList), f); + fclose(f); +} + +bool cheatsLoadCheatList(const char *file) +{ + cheatsNumber = 0; + + int count = 0; + + FILE *f = fopen(file, "rb"); + + if(f == NULL) + return false; + + int version = 0; + + if(fread(&version, 1, sizeof(version), f) != sizeof(version)) { + fclose(f); + return false; + } + + if(version != 1) { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_VERSION, + N_("Unsupported cheat list version %d"), version); + fclose(f); + return false; + } + + int type = 0; + if(fread(&type, 1, sizeof(type), f) != sizeof(type)) { + fclose(f); + return false; + } + + if(type != 0) { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_TYPE, + N_("Unsupported cheat list type %d"), type); + fclose(f); + return false; + } + + if(fread(&count, 1, sizeof(count), f) != sizeof(count)) { + fclose(f); + return false; + } + + if(fread(cheatsList, 1, sizeof(cheatsList), f) != sizeof(cheatsList)) { + fclose(f); + return false; + } + + bool firstCodeBreaker = true; + + for(int i = 0; i < count; i++) { + cheatsList[i].status = 0; // remove old status as it is not used + if(!cheatsList[i].codestring[0]) { + switch(cheatsList[i].size) { + case 0: + sprintf(cheatsList[i].codestring, "%08x:%02x", cheatsList[i].address, + cheatsList[i].value); + break; + case 1: + sprintf(cheatsList[i].codestring, "%08x:%04x", cheatsList[i].address, + cheatsList[i].value); + break; + case 2: + sprintf(cheatsList[i].codestring, "%08x:%08x", cheatsList[i].address, + cheatsList[i].value); + break; + } + } + + if(cheatsList[i].code == 512 && firstCodeBreaker) { + firstCodeBreaker = false; + char buffer[10]; + strncpy(buffer, cheatsList[i].codestring, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + if((address >> 28) == 9) { + strncpy(buffer, &cheatsList[i].codestring[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + } + } + } + cheatsNumber = count; + fclose(f); + return true; +} + +extern int cpuNextEvent; + +extern void debuggerBreakOnWrite(u32 , u32, u32, int, int); + +static u8 cheatsGetType(u32 address) +{ + switch(address >> 24) { + case 2: + return freezeWorkRAM[address & 0x3FFFF]; + case 3: + return freezeInternalRAM[address & 0x7FFF]; + } + return 0; +} + +void cheatsWriteMemory(u32 address, u32 value) +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if(cheatsNumber == 0) { + int type = cheatsGetType(address); + u32 oldValue = debuggerReadMemory(address); + if(type == 1 || (type == 2 && oldValue != value)) { + debuggerBreakOnWrite(address, oldValue, value, 2, type); + cpuNextEvent = 0; + } + debuggerWriteMemory(address, value); + } +#endif +#endif +} + +void cheatsWriteHalfWord(u32 address, u16 value) +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if(cheatsNumber == 0) { + int type = cheatsGetType(address); + u16 oldValue = debuggerReadHalfWord(address); + if(type == 1 || (type == 2 && oldValue != value)) { + debuggerBreakOnWrite(address, oldValue, value, 1, type); + cpuNextEvent = 0; + } + debuggerWriteHalfWord(address, value); + } +#endif +#endif +} + +#if defined BKPT_SUPPORT && defined SDL +void cheatsWriteByte(u32 address, u8 value) +#else +void cheatsWriteByte(u32, u8) +#endif +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if(cheatsNumber == 0) { + int type = cheatsGetType(address); + u8 oldValue = debuggerReadByte(address); + if(type == 1 || (type == 2 && oldValue != value)) { + debuggerBreakOnWrite(address, oldValue, value, 0, type); + cpuNextEvent = 0; + } + debuggerWriteByte(address, value); + } +#endif +#endif +} \ No newline at end of file diff --git a/src/Cheats.h b/src/Cheats.h new file mode 100644 index 00000000..ed828ee5 --- /dev/null +++ b/src/Cheats.h @@ -0,0 +1,55 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef GBA_CHEATS_H +#define GBA_CHEATS_H + +struct CheatsData { + int code; + int size; + int status; + bool enabled; + u32 rawaddress; + u32 address; + u32 value; + u32 oldValue; + char codestring[20]; + char desc[32]; +}; + +extern void cheatsAdd(const char *,const char *,u32, u32,u32,int,int); +extern void cheatsAddCheatCode(const char *code, const char *desc); +extern void cheatsAddGSACode(const char *code, const char *desc, bool v3); +extern void cheatsAddCBACode(const char *code, const char *desc); +extern bool cheatsImportGSACodeFile(const char *name, int game, bool v3); +extern void cheatsDelete(int number, bool restore); +extern void cheatsDeleteAll(bool restore); +extern void cheatsEnable(int number); +extern void cheatsDisable(int number); +extern void cheatsSaveGame(gzFile file); +extern void cheatsReadGame(gzFile file); +extern void cheatsSaveCheatList(const char *file); +extern bool cheatsLoadCheatList(const char *file); +extern void cheatsWriteMemory(u32, u32); +extern void cheatsWriteHalfWord(u32, u16); +extern void cheatsWriteByte(u32, u8); +extern int cheatsCheckKeys(u32,u32); +extern int cheatsNumber; +extern CheatsData cheatsList[100]; +#endif // GBA_CHEATS_H diff --git a/src/EEprom.cpp b/src/EEprom.cpp new file mode 100644 index 00000000..0992ae66 --- /dev/null +++ b/src/EEprom.cpp @@ -0,0 +1,191 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "EEprom.h" +#include "Util.h" + +extern int cpuDmaCount; + +int eepromMode = EEPROM_IDLE; +int eepromByte = 0; +int eepromBits = 0; +int eepromAddress = 0; +u8 eepromData[0x2000]; +u8 eepromBuffer[16]; +bool eepromInUse = false; +int eepromSize = 512; + +variable_desc eepromSaveData[] = { + { &eepromMode, sizeof(int) }, + { &eepromByte, sizeof(int) }, + { &eepromBits , sizeof(int) }, + { &eepromAddress , sizeof(int) }, + { &eepromInUse, sizeof(bool) }, + { &eepromData[0], 512 }, + { &eepromBuffer[0], 16 }, + { NULL, 0 } +}; + +void eepromReset() +{ + eepromMode = EEPROM_IDLE; + eepromByte = 0; + eepromBits = 0; + eepromAddress = 0; + eepromInUse = false; + eepromSize = 512; +} + +void eepromSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, eepromSaveData); + utilWriteInt(gzFile, eepromSize); + utilGzWrite(gzFile, eepromData, 0x2000); +} + +void eepromReadGame(gzFile gzFile, int version) +{ + utilReadData(gzFile, eepromSaveData); + if(version >= SAVE_GAME_VERSION_3) { + eepromSize = utilReadInt(gzFile); + utilGzRead(gzFile, eepromData, 0x2000); + } else { + // prior to 0.7.1, only 4K EEPROM was supported + eepromSize = 512; + } +} + + +int eepromRead(u32 /* address */) +{ + switch(eepromMode) { + case EEPROM_IDLE: + case EEPROM_READADDRESS: + case EEPROM_WRITEDATA: + return 1; + case EEPROM_READDATA: + { + eepromBits++; + if(eepromBits == 4) { + eepromMode = EEPROM_READDATA2; + eepromBits = 0; + eepromByte = 0; + } + return 0; + } + case EEPROM_READDATA2: + { + int data = 0; + int address = eepromAddress << 3; + int mask = 1 << (7 - (eepromBits & 7)); + data = (eepromData[address+eepromByte] & mask) ? 1 : 0; + eepromBits++; + if((eepromBits & 7) == 0) + eepromByte++; + if(eepromBits == 0x40) + eepromMode = EEPROM_IDLE; + return data; + } + default: + return 0; + } + return 1; +} + +void eepromWrite(u32 /* address */, u8 value) +{ + if(cpuDmaCount == 0) + return; + int bit = value & 1; + switch(eepromMode) { + case EEPROM_IDLE: + eepromByte = 0; + eepromBits = 1; + eepromBuffer[eepromByte] = bit; + eepromMode = EEPROM_READADDRESS; + break; + case EEPROM_READADDRESS: + eepromBuffer[eepromByte] <<= 1; + eepromBuffer[eepromByte] |= bit; + eepromBits++; + if((eepromBits & 7) == 0) { + eepromByte++; + } + if(cpuDmaCount == 0x11 || cpuDmaCount == 0x51) { + if(eepromBits == 0x11) { + eepromInUse = true; + eepromSize = 0x2000; + eepromAddress = ((eepromBuffer[0] & 0x3F) << 8) | + ((eepromBuffer[1] & 0xFF)); + if(!(eepromBuffer[0] & 0x40)) { + eepromBuffer[0] = bit; + eepromBits = 1; + eepromByte = 0; + eepromMode = EEPROM_WRITEDATA; + } else { + eepromMode = EEPROM_READDATA; + eepromByte = 0; + eepromBits = 0; + } + } + } else { + if(eepromBits == 9) { + eepromInUse = true; + eepromAddress = (eepromBuffer[0] & 0x3F); + if(!(eepromBuffer[0] & 0x40)) { + eepromBuffer[0] = bit; + eepromBits = 1; + eepromByte = 0; + eepromMode = EEPROM_WRITEDATA; + } else { + eepromMode = EEPROM_READDATA; + eepromByte = 0; + eepromBits = 0; + } + } + } + break; + case EEPROM_READDATA: + case EEPROM_READDATA2: + // should we reset here? + eepromMode = EEPROM_IDLE; + break; + case EEPROM_WRITEDATA: + eepromBuffer[eepromByte] <<= 1; + eepromBuffer[eepromByte] |= bit; + eepromBits++; + if((eepromBits & 7) == 0) { + eepromByte++; + } + if(eepromBits == 0x40) { + eepromInUse = true; + // write data; + for(int i = 0; i < 8; i++) { + eepromData[(eepromAddress << 3) + i] = eepromBuffer[i]; + } + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } else if(eepromBits == 0x41) { + eepromMode = EEPROM_IDLE; + eepromByte = 0; + eepromBits = 0; + } + break; + } +} + diff --git a/src/EEprom.h b/src/EEprom.h new file mode 100644 index 00000000..4bf23afc --- /dev/null +++ b/src/EEprom.h @@ -0,0 +1,38 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_EEPROM_H +#define VBA_EEPROM_H + +extern void eepromSaveGame(gzFile gzFile); +extern void eepromReadGame(gzFile gzFile, int version); +extern int eepromRead(u32 address); +extern void eepromWrite(u32 address, u8 value); +extern void eepromReset(); +extern u8 eepromData[0x2000]; +extern bool eepromInUse; +extern int eepromSize; + +#define EEPROM_IDLE 0 +#define EEPROM_READADDRESS 1 +#define EEPROM_READDATA 2 +#define EEPROM_READDATA2 3 +#define EEPROM_WRITEDATA 4 + +#endif // VBA_EEPROM_H diff --git a/src/Flash.cpp b/src/Flash.cpp new file mode 100644 index 00000000..0e9958a6 --- /dev/null +++ b/src/Flash.cpp @@ -0,0 +1,259 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include "GBA.h" +#include "Globals.h" +#include "Flash.h" +#include "Sram.h" +#include "Util.h" + +#define FLASH_READ_ARRAY 0 +#define FLASH_CMD_1 1 +#define FLASH_CMD_2 2 +#define FLASH_AUTOSELECT 3 +#define FLASH_CMD_3 4 +#define FLASH_CMD_4 5 +#define FLASH_CMD_5 6 +#define FLASH_ERASE_COMPLETE 7 +#define FLASH_PROGRAM 8 +#define FLASH_SETBANK 9 + +u8 flashSaveMemory[0x20000]; +int flashState = FLASH_READ_ARRAY; +int flashReadState = FLASH_READ_ARRAY; +int flashSize = 0x10000; +int flashDeviceID = 0x1b; +int flashManufacturerID = 0x32; +int flashBank = 0; + +static variable_desc flashSaveData[] = { + { &flashState, sizeof(int) }, + { &flashReadState, sizeof(int) }, + { &flashSaveMemory[0], 0x10000 }, + { NULL, 0 } +}; + +static variable_desc flashSaveData2[] = { + { &flashState, sizeof(int) }, + { &flashReadState, sizeof(int) }, + { &flashSize, sizeof(int) }, + { &flashSaveMemory[0], 0x20000 }, + { NULL, 0 } +}; + +static variable_desc flashSaveData3[] = { + { &flashState, sizeof(int) }, + { &flashReadState, sizeof(int) }, + { &flashSize, sizeof(int) }, + { &flashBank, sizeof(int) }, + { &flashSaveMemory[0], 0x20000 }, + { NULL, 0 } +}; + +void flashInit() +{ + memset(flashSaveMemory, 0xff, sizeof(flashSaveMemory)); +} + +void flashReset() +{ + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + flashBank = 0; +} + +void flashSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, flashSaveData3); +} + +void flashReadGame(gzFile gzFile, int version) +{ + if(version < SAVE_GAME_VERSION_5) + utilReadData(gzFile, flashSaveData); + else if(version < SAVE_GAME_VERSION_7) { + utilReadData(gzFile, flashSaveData2); + flashBank = 0; + flashSetSize(flashSize); + } else { + utilReadData(gzFile, flashSaveData3); + } +} + +void flashSetSize(int size) +{ + // log("Setting flash size to %d\n", size); + flashSize = size; + if(size == 0x10000) { + flashDeviceID = 0x1b; + flashManufacturerID = 0x32; + } else { + flashDeviceID = 0x13; //0x09; + flashManufacturerID = 0x62; //0xc2; + } +} + +u8 flashRead(u32 address) +{ + // log("Reading %08x from %08x\n", address, reg[15].I); + // log("Current read state is %d\n", flashReadState); + address &= 0xFFFF; + + switch(flashReadState) { + case FLASH_READ_ARRAY: + return flashSaveMemory[(flashBank << 16) + address]; + case FLASH_AUTOSELECT: + switch(address & 0xFF) { + case 0: + // manufacturer ID + return flashManufacturerID; + case 1: + // device ID + return flashDeviceID; + } + break; + case FLASH_ERASE_COMPLETE: + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + return 0xFF; + }; + return 0; +} + +void flashSaveDecide(u32 address, u8 byte) +{ + // log("Deciding save type %08x\n", address); + if(address == 0x0e005555) { + saveType = 2; + cpuSaveGameFunc = flashWrite; + } else { + saveType = 1; + cpuSaveGameFunc = sramWrite; + } + + (*cpuSaveGameFunc)(address, byte); +} + +void flashDelayedWrite(u32 address, u8 byte) +{ + saveType = 2; + cpuSaveGameFunc = flashWrite; + flashWrite(address, byte); +} + +void flashWrite(u32 address, u8 byte) +{ + // log("Writing %02x at %08x\n", byte, address); + // log("Current state is %d\n", flashState); + address &= 0xFFFF; + switch(flashState) { + case FLASH_READ_ARRAY: + if(address == 0x5555 && byte == 0xAA) + flashState = FLASH_CMD_1; + break; + case FLASH_CMD_1: + if(address == 0x2AAA && byte == 0x55) + flashState = FLASH_CMD_2; + else + flashState = FLASH_READ_ARRAY; + break; + case FLASH_CMD_2: + if(address == 0x5555) { + if(byte == 0x90) { + flashState = FLASH_AUTOSELECT; + flashReadState = FLASH_AUTOSELECT; + } else if(byte == 0x80) { + flashState = FLASH_CMD_3; + } else if(byte == 0xF0) { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } else if(byte == 0xA0) { + flashState = FLASH_PROGRAM; + } else if(byte == 0xB0 && flashSize == 0x20000) { + flashState = FLASH_SETBANK; + } else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + } else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_3: + if(address == 0x5555 && byte == 0xAA) { + flashState = FLASH_CMD_4; + } else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_4: + if(address == 0x2AAA && byte == 0x55) { + flashState = FLASH_CMD_5; + } else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_5: + if(byte == 0x30) { + // SECTOR ERASE + memset(&flashSaveMemory[(flashBank << 16) + (address & 0xF000)], + 0, + 0x1000); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashReadState = FLASH_ERASE_COMPLETE; + } else if(byte == 0x10) { + // CHIP ERASE + memset(flashSaveMemory, 0, flashSize); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashReadState = FLASH_ERASE_COMPLETE; + } else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_AUTOSELECT: + if(byte == 0xF0) { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } else if(address == 0x5555 && byte == 0xAA) + flashState = FLASH_CMD_1; + else { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_PROGRAM: + flashSaveMemory[(flashBank<<16)+address] = byte; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + break; + case FLASH_SETBANK: + if(address == 0) { + flashBank = (byte & 1); + } + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + break; + } +} \ No newline at end of file diff --git a/src/Flash.h b/src/Flash.h new file mode 100644 index 00000000..bde63997 --- /dev/null +++ b/src/Flash.h @@ -0,0 +1,35 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_FLASH_H +#define VBA_FLASH_H + +extern void flashSaveGame(gzFile gzFile); +extern void flashReadGame(gzFile gzFile, int version); +extern u8 flashRead(u32 address); +extern void flashWrite(u32 address, u8 byte); +extern void flashDelayedWrite(u32 address, u8 byte); +extern u8 flashSaveMemory[0x20000]; +extern void flashSaveDecide(u32 address, u8 byte); +extern void flashReset(); +extern void flashSetSize(int size); +extern void flashInit(); + +extern int flashSize; +#endif // VBA_FLASH_H \ No newline at end of file diff --git a/src/GBA.cpp b/src/GBA.cpp new file mode 100644 index 00000000..0b469aa0 --- /dev/null +++ b/src/GBA.cpp @@ -0,0 +1,4027 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include +#include + +#include "GBA.h" +#include "GBAinline.h" +#include "Globals.h" +#include "Gfx.h" +#include "EEprom.h" +#include "Flash.h" +#include "Sound.h" +#include "Sram.h" +#include "bios.h" +#include "unzip.h" +#include "Cheats.h" +#include "NLS.h" +#include "elf.h" +#include "Util.h" +#include "Port.h" +#include "agbprint.h" +#ifdef PROFILING +#include "prof/prof.h" +#endif + +#define UPDATE_REG(address, value) WRITE16LE(((u16 *)&ioMem[address]),value) + +#ifdef __GNUC__ +#define _stricmp strcasecmp +#endif + +extern int emulating; +extern int linktime; +extern void StartLink(u16); +extern void StartJOYLink(u16); +extern void StartGPLink(u16); +extern void LinkSSend(u16); +extern void LinkUpdate(int); +extern int linktime2; + +int cpuDmaTicksToUpdate = 0; +int cpuDmaCount = 0; +bool cpuDmaHack = false; +bool cpuDmaHack2 = false; +u32 cpuDmaLast = 0; +int dummyAddress = 0; + +bool cpuBreakLoop = true; +int cpuNextEvent = 0; +int cpuTotalTicks = 0; + +int gbaSaveType = 0; // used to remember the save type on reset +bool intState = false; +bool stopState = false; +bool holdState = false; +int holdType = 0; +bool cpuSramEnabled = true; +bool cpuFlashEnabled = true; +bool cpuEEPROMEnabled = true; +bool cpuEEPROMSensorEnabled = false; + +#ifdef PROFILING +int profilingTicks = 0; +int profilingTicksReload = 0; +static char *profilBuffer = NULL; +static int profilSize = 0; +static u32 profilLowPC = 0; +static int profilScale = 0; +#endif +u8 freezeWorkRAM[0x40000]; +u8 freezeInternalRAM[0x8000]; +int lcdTicks = 960; +bool timer0On = false; +int timer0Ticks = 0; +int timer0Reload = 0; +int timer0ClockReload = 0; +bool timer1On = false; +int timer1Ticks = 0; +int timer1Reload = 0; +int timer1ClockReload = 0; +bool timer2On = false; +int timer2Ticks = 0; +int timer2Reload = 0; +int timer2ClockReload = 0; +bool timer3On = false; +int timer3Ticks = 0; +int timer3Reload = 0; +int timer3ClockReload = 0; +u32 dma0Source = 0; +u32 dma0Dest = 0; +u32 dma1Source = 0; +u32 dma1Dest = 0; +u32 dma2Source = 0; +u32 dma2Dest = 0; +u32 dma3Source = 0; +u32 dma3Dest = 0; +void (*cpuSaveGameFunc)(u32,u8) = flashSaveDecide; +void (*renderLine)() = mode0RenderLine; +bool fxOn = false; +bool windowOn = false; +int frameCount = 0; +char buffer[1024]; +FILE *out = NULL; +u32 lastTime = 0; +int count = 0; + +int capture = 0; +int capturePrevious = 0; +int captureNumber = 0; + +const int TIMER_TICKS[4] = { + 1, + 64, + 256, + 1024 +}; + +const int thumbCycles[] = { +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 + 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 4 + 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 5 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 6 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 7 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 8 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 9 + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // a + 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 4, 1, 1, // b + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, // d + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // e + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // f +}; + +const int gamepakRamWaitState[4] = { 4, 3, 2, 8 }; +const int gamepakWaitState[8] = { 4, 3, 2, 8, 4, 3, 2, 8 }; +const int gamepakWaitState0[8] = { 2, 2, 2, 2, 1, 1, 1, 1 }; +const int gamepakWaitState1[8] = { 4, 4, 4, 4, 1, 1, 1, 1 }; +const int gamepakWaitState2[8] = { 8, 8, 8, 8, 1, 1, 1, 1 }; + +int memoryWait[16] = + { 0, 0, 2, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 0 }; +int memoryWait32[16] = + { 0, 0, 9, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 0 }; +int memoryWaitSeq[16] = + { 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 4, 4, 8, 8, 4, 0 }; +int memoryWaitSeq32[16] = + { 2, 0, 3, 0, 0, 2, 2, 0, 4, 4, 8, 8, 16, 16, 8, 0 }; +int memoryWaitFetch[16] = + { 3, 0, 3, 0, 0, 1, 1, 0, 4, 4, 4, 4, 4, 4, 4, 0 }; +int memoryWaitFetch32[16] = + { 6, 0, 6, 0, 0, 2, 2, 0, 8, 8, 8, 8, 8, 8, 8, 0 }; + +const int cpuMemoryWait[16] = { + 0, 0, 2, 0, 0, 0, 0, 0, + 2, 2, 2, 2, 2, 2, 0, 0 +}; +const int cpuMemoryWait32[16] = { + 0, 0, 3, 0, 0, 0, 0, 0, + 3, 3, 3, 3, 3, 3, 0, 0 +}; + +const bool memory32[16] = + { true, false, false, true, true, false, false, true, false, false, false, false, false, false, true, false}; + +u8 biosProtected[4]; + +#ifdef WORDS_BIGENDIAN +bool cpuBiosSwapped = false; +#endif + +u32 myROM[] = { +0xEA000006, +0xEA000093, +0xEA000006, +0x00000000, +0x00000000, +0x00000000, +0xEA000088, +0x00000000, +0xE3A00302, +0xE1A0F000, +0xE92D5800, +0xE55EC002, +0xE28FB03C, +0xE79BC10C, +0xE14FB000, +0xE92D0800, +0xE20BB080, +0xE38BB01F, +0xE129F00B, +0xE92D4004, +0xE1A0E00F, +0xE12FFF1C, +0xE8BD4004, +0xE3A0C0D3, +0xE129F00C, +0xE8BD0800, +0xE169F00B, +0xE8BD5800, +0xE1B0F00E, +0x0000009C, +0x0000009C, +0x0000009C, +0x0000009C, +0x000001F8, +0x000001F0, +0x000000AC, +0x000000A0, +0x000000FC, +0x00000168, +0xE12FFF1E, +0xE1A03000, +0xE1A00001, +0xE1A01003, +0xE2113102, +0x42611000, +0xE033C040, +0x22600000, +0xE1B02001, +0xE15200A0, +0x91A02082, +0x3AFFFFFC, +0xE1500002, +0xE0A33003, +0x20400002, +0xE1320001, +0x11A020A2, +0x1AFFFFF9, +0xE1A01000, +0xE1A00003, +0xE1B0C08C, +0x22600000, +0x42611000, +0xE12FFF1E, +0xE92D0010, +0xE1A0C000, +0xE3A01001, +0xE1500001, +0x81A000A0, +0x81A01081, +0x8AFFFFFB, +0xE1A0000C, +0xE1A04001, +0xE3A03000, +0xE1A02001, +0xE15200A0, +0x91A02082, +0x3AFFFFFC, +0xE1500002, +0xE0A33003, +0x20400002, +0xE1320001, +0x11A020A2, +0x1AFFFFF9, +0xE0811003, +0xE1B010A1, +0xE1510004, +0x3AFFFFEE, +0xE1A00004, +0xE8BD0010, +0xE12FFF1E, +0xE0010090, +0xE1A01741, +0xE2611000, +0xE3A030A9, +0xE0030391, +0xE1A03743, +0xE2833E39, +0xE0030391, +0xE1A03743, +0xE2833C09, +0xE283301C, +0xE0030391, +0xE1A03743, +0xE2833C0F, +0xE28330B6, +0xE0030391, +0xE1A03743, +0xE2833C16, +0xE28330AA, +0xE0030391, +0xE1A03743, +0xE2833A02, +0xE2833081, +0xE0030391, +0xE1A03743, +0xE2833C36, +0xE2833051, +0xE0030391, +0xE1A03743, +0xE2833CA2, +0xE28330F9, +0xE0000093, +0xE1A00840, +0xE12FFF1E, +0xE3A00001, +0xE3A01001, +0xE92D4010, +0xE3A03000, +0xE3A04001, +0xE3500000, +0x1B000004, +0xE5CC3301, +0xEB000002, +0x0AFFFFFC, +0xE8BD4010, +0xE12FFF1E, +0xE3A0C301, +0xE5CC3208, +0xE15C20B8, +0xE0110002, +0x10222000, +0x114C20B8, +0xE5CC4208, +0xE12FFF1E, +0xE92D500F, +0xE3A00301, +0xE1A0E00F, +0xE510F004, +0xE8BD500F, +0xE25EF004, +0xE59FD044, +0xE92D5000, +0xE14FC000, +0xE10FE000, +0xE92D5000, +0xE3A0C302, +0xE5DCE09C, +0xE35E00A5, +0x1A000004, +0x05DCE0B4, +0x021EE080, +0xE28FE004, +0x159FF018, +0x059FF018, +0xE59FD018, +0xE8BD5000, +0xE169F00C, +0xE8BD5000, +0xE25EF004, +0x03007FF0, +0x09FE2000, +0x09FFC000, +0x03007FE0 +}; + +variable_desc saveGameStruct[] = { + { &DISPCNT , sizeof(u16) }, + { &DISPSTAT , sizeof(u16) }, + { &VCOUNT , sizeof(u16) }, + { &BG0CNT , sizeof(u16) }, + { &BG1CNT , sizeof(u16) }, + { &BG2CNT , sizeof(u16) }, + { &BG3CNT , sizeof(u16) }, + { &BG0HOFS , sizeof(u16) }, + { &BG0VOFS , sizeof(u16) }, + { &BG1HOFS , sizeof(u16) }, + { &BG1VOFS , sizeof(u16) }, + { &BG2HOFS , sizeof(u16) }, + { &BG2VOFS , sizeof(u16) }, + { &BG3HOFS , sizeof(u16) }, + { &BG3VOFS , sizeof(u16) }, + { &BG2PA , sizeof(u16) }, + { &BG2PB , sizeof(u16) }, + { &BG2PC , sizeof(u16) }, + { &BG2PD , sizeof(u16) }, + { &BG2X_L , sizeof(u16) }, + { &BG2X_H , sizeof(u16) }, + { &BG2Y_L , sizeof(u16) }, + { &BG2Y_H , sizeof(u16) }, + { &BG3PA , sizeof(u16) }, + { &BG3PB , sizeof(u16) }, + { &BG3PC , sizeof(u16) }, + { &BG3PD , sizeof(u16) }, + { &BG3X_L , sizeof(u16) }, + { &BG3X_H , sizeof(u16) }, + { &BG3Y_L , sizeof(u16) }, + { &BG3Y_H , sizeof(u16) }, + { &WIN0H , sizeof(u16) }, + { &WIN1H , sizeof(u16) }, + { &WIN0V , sizeof(u16) }, + { &WIN1V , sizeof(u16) }, + { &WININ , sizeof(u16) }, + { &WINOUT , sizeof(u16) }, + { &MOSAIC , sizeof(u16) }, + { &BLDMOD , sizeof(u16) }, + { &COLEV , sizeof(u16) }, + { &COLY , sizeof(u16) }, + { &DM0SAD_L , sizeof(u16) }, + { &DM0SAD_H , sizeof(u16) }, + { &DM0DAD_L , sizeof(u16) }, + { &DM0DAD_H , sizeof(u16) }, + { &DM0CNT_L , sizeof(u16) }, + { &DM0CNT_H , sizeof(u16) }, + { &DM1SAD_L , sizeof(u16) }, + { &DM1SAD_H , sizeof(u16) }, + { &DM1DAD_L , sizeof(u16) }, + { &DM1DAD_H , sizeof(u16) }, + { &DM1CNT_L , sizeof(u16) }, + { &DM1CNT_H , sizeof(u16) }, + { &DM2SAD_L , sizeof(u16) }, + { &DM2SAD_H , sizeof(u16) }, + { &DM2DAD_L , sizeof(u16) }, + { &DM2DAD_H , sizeof(u16) }, + { &DM2CNT_L , sizeof(u16) }, + { &DM2CNT_H , sizeof(u16) }, + { &DM3SAD_L , sizeof(u16) }, + { &DM3SAD_H , sizeof(u16) }, + { &DM3DAD_L , sizeof(u16) }, + { &DM3DAD_H , sizeof(u16) }, + { &DM3CNT_L , sizeof(u16) }, + { &DM3CNT_H , sizeof(u16) }, + { &TM0D , sizeof(u16) }, + { &TM0CNT , sizeof(u16) }, + { &TM1D , sizeof(u16) }, + { &TM1CNT , sizeof(u16) }, + { &TM2D , sizeof(u16) }, + { &TM2CNT , sizeof(u16) }, + { &TM3D , sizeof(u16) }, + { &TM3CNT , sizeof(u16) }, + { &P1 , sizeof(u16) }, + { &IE , sizeof(u16) }, + { &IF , sizeof(u16) }, + { &IME , sizeof(u16) }, + { &holdState, sizeof(bool) }, + { &holdType, sizeof(int) }, + { &lcdTicks, sizeof(int) }, + { &timer0On , sizeof(bool) }, + { &timer0Ticks , sizeof(int) }, + { &timer0Reload , sizeof(int) }, + { &timer0ClockReload , sizeof(int) }, + { &timer1On , sizeof(bool) }, + { &timer1Ticks , sizeof(int) }, + { &timer1Reload , sizeof(int) }, + { &timer1ClockReload , sizeof(int) }, + { &timer2On , sizeof(bool) }, + { &timer2Ticks , sizeof(int) }, + { &timer2Reload , sizeof(int) }, + { &timer2ClockReload , sizeof(int) }, + { &timer3On , sizeof(bool) }, + { &timer3Ticks , sizeof(int) }, + { &timer3Reload , sizeof(int) }, + { &timer3ClockReload , sizeof(int) }, + { &dma0Source , sizeof(u32) }, + { &dma0Dest , sizeof(u32) }, + { &dma1Source , sizeof(u32) }, + { &dma1Dest , sizeof(u32) }, + { &dma2Source , sizeof(u32) }, + { &dma2Dest , sizeof(u32) }, + { &dma3Source , sizeof(u32) }, + { &dma3Dest , sizeof(u32) }, + { &fxOn, sizeof(bool) }, + { &windowOn, sizeof(bool) }, + { &N_FLAG , sizeof(bool) }, + { &C_FLAG , sizeof(bool) }, + { &Z_FLAG , sizeof(bool) }, + { &V_FLAG , sizeof(bool) }, + { &armState , sizeof(bool) }, + { &armIrqEnable , sizeof(bool) }, + { &armNextPC , sizeof(u32) }, + { &armMode , sizeof(int) }, + { &saveType , sizeof(int) }, + { NULL, 0 } +}; + +static int romSize = 0x2000000; + +#ifdef PROFILING +void cpuProfil(char *buf, int size, u32 lowPC, int scale) +{ + profilBuffer = buf; + profilSize = size; + profilLowPC = lowPC; + profilScale = scale; +} + +void cpuEnableProfiling(int hz) +{ + if(hz == 0) + hz = 100; + profilingTicks = profilingTicksReload = 16777216 / hz; + profSetHertz(hz); +} +#endif + +inline int CPUUpdateTicksAccess32(u32 address) +{ + return memoryWait32[(address>>24)&15]; +} + +inline int CPUUpdateTicksAccess16(u32 address) +{ + return memoryWait[(address>>24)&15]; +} + +inline int CPUUpdateTicksAccessSeq32(u32 address) +{ + return memoryWaitSeq32[(address>>24)&15]; +} + +inline int CPUUpdateTicksAccessSeq16(u32 address) +{ + return memoryWaitSeq[(address>>24)&15]; +} + +inline int CPUUpdateTicks() +{ + int cpuLoopTicks = lcdTicks; + + if(soundTicks < cpuLoopTicks) + cpuLoopTicks = soundTicks; + + if(timer0On && !(TM0CNT & 4) && (timer0Ticks < cpuLoopTicks)) { + cpuLoopTicks = timer0Ticks; + } + if(timer1On && !(TM1CNT & 4) && (timer1Ticks < cpuLoopTicks)) { + cpuLoopTicks = timer1Ticks; + } + if(timer2On && !(TM2CNT & 4) && (timer2Ticks < cpuLoopTicks)) { + cpuLoopTicks = timer2Ticks; + } + if(timer3On && !(TM3CNT & 4) && (timer3Ticks < cpuLoopTicks)) { + cpuLoopTicks = timer3Ticks; + } +#ifdef PROFILING + if(profilingTicksReload != 0) { + if(profilingTicks < cpuLoopTicks) { + cpuLoopTicks = profilingTicks; + } + } +#endif + + return cpuLoopTicks; +} + +void CPUUpdateWindow0() +{ + int x00 = WIN0H>>8; + int x01 = WIN0H & 255; + + if(x00 <= x01) { + for(int i = 0; i < 240; i++) { + gfxInWin0[i] = (i >= x00 && i < x01); + } + } else { + for(int i = 0; i < 240; i++) { + gfxInWin0[i] = (i >= x00 || i < x01); + } + } +} + +void CPUUpdateWindow1() +{ + int x00 = WIN1H>>8; + int x01 = WIN1H & 255; + + if(x00 <= x01) { + for(int i = 0; i < 240; i++) { + gfxInWin1[i] = (i >= x00 && i < x01); + } + } else { + for(int i = 0; i < 240; i++) { + gfxInWin1[i] = (i >= x00 || i < x01); + } + } +} + +extern u32 line0[240]; +extern u32 line1[240]; +extern u32 line2[240]; +extern u32 line3[240]; + +#define CLEAR_ARRAY(a) \ + {\ + u32 *array = (a);\ + for(int i = 0; i < 240; i++) {\ + *array++ = 0x80000000;\ + }\ + }\ + +void CPUUpdateRenderBuffers(bool force) +{ + if(!(layerEnable & 0x0100) || force) { + CLEAR_ARRAY(line0); + } + if(!(layerEnable & 0x0200) || force) { + CLEAR_ARRAY(line1); + } + if(!(layerEnable & 0x0400) || force) { + CLEAR_ARRAY(line2); + } + if(!(layerEnable & 0x0800) || force) { + CLEAR_ARRAY(line3); + } +} + +static bool CPUWriteState(gzFile gzFile) +{ + utilWriteInt(gzFile, SAVE_GAME_VERSION); + + utilGzWrite(gzFile, &rom[0xa0], 16); + + utilWriteInt(gzFile, useBios); + + utilGzWrite(gzFile, ®[0], sizeof(reg)); + + utilWriteData(gzFile, saveGameStruct); + + // new to version 0.7.1 + utilWriteInt(gzFile, stopState); + // new to version 0.8 + utilWriteInt(gzFile, intState); + + utilGzWrite(gzFile, internalRAM, 0x8000); + utilGzWrite(gzFile, paletteRAM, 0x400); + utilGzWrite(gzFile, workRAM, 0x40000); + utilGzWrite(gzFile, vram, 0x20000); + utilGzWrite(gzFile, oam, 0x400); + utilGzWrite(gzFile, pix, 4*241*162); + + for (int i = NR10; i <= 0x9F; i++) ioMem[i] = soundRead(i); + + utilGzWrite(gzFile, ioMem, 0x400); + + eepromSaveGame(gzFile); + flashSaveGame(gzFile); + soundSaveGame(gzFile); + + cheatsSaveGame(gzFile); + + // version 1.5 + rtcSaveGame(gzFile); + + return true; +} + +bool CPUWriteState(const char *file) +{ + gzFile gzFile = utilGzOpen(file, "wb"); + + if(gzFile == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), file); + return false; + } + + bool res = CPUWriteState(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool CPUWriteMemState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "w"); + + if(gzFile == NULL) { + return false; + } + + bool res = CPUWriteState(gzFile); + + long pos = utilGzMemTell(gzFile)+8; + + if(pos >= (available)) + res = false; + + utilGzClose(gzFile); + + return res; +} + +static bool CPUReadState(gzFile gzFile) +{ + int version = utilReadInt(gzFile); + + if(version > SAVE_GAME_VERSION || version < SAVE_GAME_VERSION_1) { + systemMessage(MSG_UNSUPPORTED_VBA_SGM, + N_("Unsupported VisualBoyAdvance save game version %d"), + version); + return false; + } + + u8 romname[17]; + + utilGzRead(gzFile, romname, 16); + + if(memcmp(&rom[0xa0], romname, 16) != 0) { + romname[16]=0; + for(int i = 0; i < 16; i++) + if(romname[i] < 32) + romname[i] = 32; + systemMessage(MSG_CANNOT_LOAD_SGM, N_("Cannot load save game for %s"), romname); + return false; + } + + bool ub = utilReadInt(gzFile) ? true : false; + + if(ub != useBios) { + if(useBios) + systemMessage(MSG_SAVE_GAME_NOT_USING_BIOS, + N_("Save game is not using the BIOS files")); + else + systemMessage(MSG_SAVE_GAME_USING_BIOS, + N_("Save game is using the BIOS file")); + return false; + } + + utilGzRead(gzFile, ®[0], sizeof(reg)); + + utilReadData(gzFile, saveGameStruct); + + if(version < SAVE_GAME_VERSION_3) + stopState = false; + else + stopState = utilReadInt(gzFile) ? true : false; + + if(version < SAVE_GAME_VERSION_4) + intState = false; + else + intState = utilReadInt(gzFile) ? true : false; + + utilGzRead(gzFile, internalRAM, 0x8000); + utilGzRead(gzFile, paletteRAM, 0x400); + utilGzRead(gzFile, workRAM, 0x40000); + utilGzRead(gzFile, vram, 0x20000); + utilGzRead(gzFile, oam, 0x400); + if(version < SAVE_GAME_VERSION_6) + utilGzRead(gzFile, pix, 4*240*160); + else + utilGzRead(gzFile, pix, 4*241*162); + utilGzRead(gzFile, ioMem, 0x400); + + eepromReadGame(gzFile, version); + flashReadGame(gzFile, version); + soundReadGame(gzFile, version); + + if(version > SAVE_GAME_VERSION_1) { + cheatsReadGame(gzFile); + } + if(version > SAVE_GAME_VERSION_6) { + rtcReadGame(gzFile); + } + + if(version <= SAVE_GAME_VERSION_7) { + u32 temp; +#define SWAP(a,b,c) \ + temp = (a);\ + (a) = (b)<<16|(c);\ + (b) = (temp) >> 16;\ + (c) = (temp) & 0xFFFF; + + SWAP(dma0Source, DM0SAD_H, DM0SAD_L); + SWAP(dma0Dest, DM0DAD_H, DM0DAD_L); + SWAP(dma1Source, DM1SAD_H, DM1SAD_L); + SWAP(dma1Dest, DM1DAD_H, DM1DAD_L); + SWAP(dma2Source, DM2SAD_H, DM2SAD_L); + SWAP(dma2Dest, DM2DAD_H, DM2DAD_L); + SWAP(dma3Source, DM3SAD_H, DM3SAD_L); + SWAP(dma3Dest, DM3DAD_H, DM3DAD_L); + } + + // set pointers! + layerEnable = layerSettings & DISPCNT; + + CPUUpdateRender(); + CPUUpdateRenderBuffers(true); + CPUUpdateWindow0(); + CPUUpdateWindow1(); + gbaSaveType = 0; + switch(saveType) { + case 0: + cpuSaveGameFunc = flashSaveDecide; + break; + case 1: + cpuSaveGameFunc = sramWrite; + gbaSaveType = 1; + break; + case 2: + cpuSaveGameFunc = flashWrite; + gbaSaveType = 2; + break; + default: + systemMessage(MSG_UNSUPPORTED_SAVE_TYPE, + N_("Unsupported save type %d"), saveType); + break; + } + if(eepromInUse) + gbaSaveType = 3; + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + return true; +} + +bool CPUReadMemState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "r"); + + bool res = CPUReadState(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool CPUReadState(const char * file) +{ + gzFile gzFile = utilGzOpen(file, "rb"); + + if(gzFile == NULL) + return false; + + bool res = CPUReadState(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool CPUExportEepromFile(const char *fileName) +{ + if(eepromInUse) { + FILE *file = fopen(fileName, "wb"); + + if(!file) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), + fileName); + return false; + } + + for(int i = 0; i < eepromSize;) { + for(int j = 0; j < 8; j++) { + if(fwrite(&eepromData[i+7-j], 1, 1, file) != 1) { + fclose(file); + return false; + } + } + i += 8; + } + fclose(file); + } + return true; +} + +bool CPUWriteBatteryFile(const char *fileName) +{ + if(gbaSaveType == 0) { + if(eepromInUse) + gbaSaveType = 3; + else switch(saveType) { + case 1: + gbaSaveType = 1; + break; + case 2: + gbaSaveType = 2; + break; + } + } + + if(gbaSaveType) { + FILE *file = fopen(fileName, "wb"); + + if(!file) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), + fileName); + return false; + } + + // only save if Flash/Sram in use or EEprom in use + if(gbaSaveType != 3) { + if(gbaSaveType == 2) { + if(fwrite(flashSaveMemory, 1, flashSize, file) != (size_t)flashSize) { + fclose(file); + return false; + } + } else { + if(fwrite(flashSaveMemory, 1, 0x10000, file) != 0x10000) { + fclose(file); + return false; + } + } + } else { + if(fwrite(eepromData, 1, eepromSize, file) != (size_t)eepromSize) { + fclose(file); + return false; + } + } + fclose(file); + } + return true; +} + +bool CPUReadGSASnapshot(const char *fileName) +{ + int i; + FILE *file = fopen(fileName, "rb"); + + if(!file) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + // long size = ftell(file); + fseek(file, 0x0, SEEK_SET); + fread(&i, 1, 4, file); + fseek(file, i, SEEK_CUR); // Skip SharkPortSave + fseek(file, 4, SEEK_CUR); // skip some sort of flag + fread(&i, 1, 4, file); // name length + fseek(file, i, SEEK_CUR); // skip name + fread(&i, 1, 4, file); // desc length + fseek(file, i, SEEK_CUR); // skip desc + fread(&i, 1, 4, file); // notes length + fseek(file, i, SEEK_CUR); // skip notes + int saveSize; + fread(&saveSize, 1, 4, file); // read length + saveSize -= 0x1c; // remove header size + char buffer[17]; + char buffer2[17]; + fread(buffer, 1, 16, file); + buffer[16] = 0; + for(i = 0; i < 16; i++) + if(buffer[i] < 32) + buffer[i] = 32; + memcpy(buffer2, &rom[0xa0], 16); + buffer2[16] = 0; + for(i = 0; i < 16; i++) + if(buffer2[i] < 32) + buffer2[i] = 32; + if(memcmp(buffer, buffer2, 16)) { + systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR, + N_("Cannot import snapshot for %s. Current game is %s"), + buffer, + buffer2); + fclose(file); + return false; + } + fseek(file, 12, SEEK_CUR); // skip some flags + if(saveSize >= 65536) { + if(fread(flashSaveMemory, 1, saveSize, file) != (size_t)saveSize) { + fclose(file); + return false; + } + } else { + systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE, + N_("Unsupported snapshot file %s"), + fileName); + fclose(file); + return false; + } + fclose(file); + CPUReset(); + return true; +} + +bool CPUWriteGSASnapshot(const char *fileName, + const char *title, + const char *desc, + const char *notes) +{ + FILE *file = fopen(fileName, "wb"); + + if(!file) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + u8 buffer[17]; + + utilPutDword(buffer, 0x0d); // SharkPortSave length + fwrite(buffer, 1, 4, file); + fwrite("SharkPortSave", 1, 0x0d, file); + utilPutDword(buffer, 0x000f0000); + fwrite(buffer, 1, 4, file); // save type 0x000f0000 = GBA save + utilPutDword(buffer, strlen(title)); + fwrite(buffer, 1, 4, file); // title length + fwrite(title, 1, strlen(title), file); + utilPutDword(buffer, strlen(desc)); + fwrite(buffer, 1, 4, file); // desc length + fwrite(desc, 1, strlen(desc), file); + utilPutDword(buffer, strlen(notes)); + fwrite(buffer, 1, 4, file); // notes length + fwrite(notes, 1, strlen(notes), file); + int saveSize = 0x10000; + if(gbaSaveType == 2) + saveSize = flashSize; + int totalSize = saveSize + 0x1c; + + utilPutDword(buffer, totalSize); // length of remainder of save - CRC + fwrite(buffer, 1, 4, file); + + char temp[0x2001c]; + memset(temp, 0, 28); + memcpy(temp, &rom[0xa0], 16); // copy internal name + temp[0x10] = rom[0xbe]; // reserved area (old checksum) + temp[0x11] = rom[0xbf]; // reserved area (old checksum) + temp[0x12] = rom[0xbd]; // complement check + temp[0x13] = rom[0xb0]; // maker + temp[0x14] = 1; // 1 save ? + memcpy(&temp[0x1c], flashSaveMemory, saveSize); // copy save + fwrite(temp, 1, totalSize, file); // write save + header + u32 crc = 0; + + for(int i = 0; i < totalSize; i++) { + crc += ((u32)temp[i] << (crc % 0x18)); + } + + utilPutDword(buffer, crc); + fwrite(buffer, 1, 4, file); // CRC? + + fclose(file); + return true; +} + +bool CPUImportEepromFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if(!file) + return false; + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + long size = ftell(file); + fseek(file, 0, SEEK_SET); + if(size == 512 || size == 0x2000) { + if(fread(eepromData, 1, size, file) != (size_t)size) { + fclose(file); + return false; + } + for(int i = 0; i < size;) { + u8 tmp = eepromData[i]; + eepromData[i] = eepromData[7-i]; + eepromData[7-i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7-i]; + eepromData[7-i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7-i]; + eepromData[7-i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7-i]; + eepromData[7-i] = tmp; + i++; + i += 4; + } + } else + return false; + fclose(file); + return true; +} + +bool CPUReadBatteryFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if(!file) + return false; + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + long size = ftell(file); + fseek(file, 0, SEEK_SET); + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + if(size == 512 || size == 0x2000) { + if(fread(eepromData, 1, size, file) != (size_t)size) { + fclose(file); + return false; + } + } else { + if(size == 0x20000) { + if(fread(flashSaveMemory, 1, 0x20000, file) != 0x20000) { + fclose(file); + return false; + } + flashSetSize(0x20000); + } else { + if(fread(flashSaveMemory, 1, 0x10000, file) != 0x10000) { + fclose(file); + return false; + } + flashSetSize(0x10000); + } + } + fclose(file); + return true; +} + +bool CPUWritePNGFile(const char *fileName) +{ + return utilWritePNGFile(fileName, 240, 160, pix); +} + +bool CPUWriteBMPFile(const char *fileName) +{ + return utilWriteBMPFile(fileName, 240, 160, pix); +} + +bool CPUIsZipFile(const char * file) +{ + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".zip") == 0) + return true; + } + } + + return false; +} + +bool CPUIsGBAImage(const char * file) +{ + cpuIsMultiBoot = false; + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gba") == 0) + return true; + if(_stricmp(p, ".agb") == 0) + return true; + if(_stricmp(p, ".bin") == 0) + return true; + if(_stricmp(p, ".elf") == 0) + return true; + if(_stricmp(p, ".mb") == 0) { + cpuIsMultiBoot = true; + return true; + } + } + } + + return false; +} + +bool CPUIsGBABios(const char * file) +{ + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gba") == 0) + return true; + if(_stricmp(p, ".agb") == 0) + return true; + if(_stricmp(p, ".bin") == 0) + return true; + if(_stricmp(p, ".bios") == 0) + return true; + } + } + + return false; +} + +bool CPUIsELF(const char *file) +{ + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".elf") == 0) + return true; + } + } + return false; +} + +void CPUCleanUp() +{ +#ifdef PROFILING + if(profilingTicksReload) { + profCleanup(); + } +#endif + + if(rom != NULL) { + free(rom); + rom = NULL; + } + + if(vram != NULL) { + free(vram); + vram = NULL; + } + + if(paletteRAM != NULL) { + free(paletteRAM); + paletteRAM = NULL; + } + + if(internalRAM != NULL) { + free(internalRAM); + internalRAM = NULL; + } + + if(workRAM != NULL) { + free(workRAM); + workRAM = NULL; + } + + if(bios != NULL) { + free(bios); + bios = NULL; + } + + if(pix != NULL) { + free(pix); + pix = NULL; + } + + if(oam != NULL) { + free(oam); + oam = NULL; + } + + if(ioMem != NULL) { + free(ioMem); + ioMem = NULL; + } + + elfCleanUp(); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + emulating = 0; +} + +int CPULoadRom(const char *szFile) +{ + romSize = 0x2000000; + + if(rom != NULL) { + CPUCleanUp(); + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + rom = (u8 *)malloc(0x2000000); + if(rom == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "ROM"); + return 0; + } + workRAM = (u8 *)calloc(1, 0x40000); + if(workRAM == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "WRAM"); + return 0; + } + + u8 *whereToLoad = rom; + if(cpuIsMultiBoot) + whereToLoad = workRAM; + + if(CPUIsELF(szFile)) { + FILE *f = fopen(szFile, "rb"); + if(!f) { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), + szFile); + free(rom); + rom = NULL; + free(workRAM); + workRAM = NULL; + return 0; + } + bool res = elfRead(szFile, romSize, f); + if(!res || romSize == 0) { + free(rom); + rom = NULL; + free(workRAM); + workRAM = NULL; + elfCleanUp(); + return 0; + } + } else if(!utilLoad(szFile, + utilIsGBAImage, + whereToLoad, + romSize)) { + free(rom); + rom = NULL; + free(workRAM); + workRAM = NULL; + return 0; + } + + u16 *temp = (u16 *)(rom+((romSize+1)&~1)); + int i; + for(i = (romSize+1)&~1; i < 0x2000000; i+=2) { + WRITE16LE(temp, (i >> 1) & 0xFFFF); + temp++; + } + + bios = (u8 *)calloc(1,0x4000); + if(bios == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "BIOS"); + CPUCleanUp(); + return 0; + } + internalRAM = (u8 *)calloc(1,0x8000); + if(internalRAM == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "IRAM"); + CPUCleanUp(); + return 0; + } + paletteRAM = (u8 *)calloc(1,0x400); + if(paletteRAM == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "PRAM"); + CPUCleanUp(); + return 0; + } + vram = (u8 *)calloc(1, 0x20000); + if(vram == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "VRAM"); + CPUCleanUp(); + return 0; + } + oam = (u8 *)calloc(1, 0x400); + if(oam == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "OAM"); + CPUCleanUp(); + return 0; + } + pix = (u8 *)calloc(1, 4 * 241 * 162); + if(pix == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "PIX"); + CPUCleanUp(); + return 0; + } + ioMem = (u8 *)calloc(1, 0x400); + if(ioMem == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "IO"); + CPUCleanUp(); + return 0; + } + + flashInit(); + + CPUUpdateRenderBuffers(true); + + return romSize; +} + +void CPUUpdateRender() +{ + switch(DISPCNT & 7) { + case 0: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode0RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode0RenderLineNoWindow; + else + renderLine = mode0RenderLineAll; + break; + case 1: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode1RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode1RenderLineNoWindow; + else + renderLine = mode1RenderLineAll; + break; + case 2: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode2RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode2RenderLineNoWindow; + else + renderLine = mode2RenderLineAll; + break; + case 3: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode3RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode3RenderLineNoWindow; + else + renderLine = mode3RenderLineAll; + break; + case 4: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode4RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode4RenderLineNoWindow; + else + renderLine = mode4RenderLineAll; + break; + case 5: + if((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode5RenderLine; + else if(fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode5RenderLineNoWindow; + else + renderLine = mode5RenderLineAll; + default: + break; + } +} + +void CPUUpdateCPSR() +{ + u32 CPSR = reg[16].I & 0x40; + if(N_FLAG) + CPSR |= 0x80000000; + if(Z_FLAG) + CPSR |= 0x40000000; + if(C_FLAG) + CPSR |= 0x20000000; + if(V_FLAG) + CPSR |= 0x10000000; + if(!armState) + CPSR |= 0x00000020; + if(!armIrqEnable) + CPSR |= 0x80; + CPSR |= (armMode & 0x1F); + reg[16].I = CPSR; +} + +void CPUUpdateFlags(bool breakLoop) +{ + u32 CPSR = reg[16].I; + + N_FLAG = (CPSR & 0x80000000) ? true: false; + Z_FLAG = (CPSR & 0x40000000) ? true: false; + C_FLAG = (CPSR & 0x20000000) ? true: false; + V_FLAG = (CPSR & 0x10000000) ? true: false; + armState = (CPSR & 0x20) ? false : true; + armIrqEnable = (CPSR & 0x80) ? false : true; + if(breakLoop) { + if(armIrqEnable && (IF & IE) && (IME & 1)) { + cpuNextEvent = 0; + } + } +} + +void CPUUpdateFlags() +{ + CPUUpdateFlags(true); +} + +#ifdef WORDS_BIGENDIAN +static void CPUSwap(volatile u32 *a, volatile u32 *b) +{ + volatile u32 c = *b; + *b = *a; + *a = c; +} +#else +static void CPUSwap(u32 *a, u32 *b) +{ + u32 c = *b; + *b = *a; + *a = c; +} +#endif + +void CPUSwitchMode(int mode, bool saveState, bool breakLoop) +{ + // if(armMode == mode) + // return; + + CPUUpdateCPSR(); + + switch(armMode) { + case 0x10: + case 0x1F: + reg[R13_USR].I = reg[13].I; + reg[R14_USR].I = reg[14].I; + reg[17].I = reg[16].I; + break; + case 0x11: + CPUSwap(®[R8_FIQ].I, ®[8].I); + CPUSwap(®[R9_FIQ].I, ®[9].I); + CPUSwap(®[R10_FIQ].I, ®[10].I); + CPUSwap(®[R11_FIQ].I, ®[11].I); + CPUSwap(®[R12_FIQ].I, ®[12].I); + reg[R13_FIQ].I = reg[13].I; + reg[R14_FIQ].I = reg[14].I; + reg[SPSR_FIQ].I = reg[17].I; + break; + case 0x12: + reg[R13_IRQ].I = reg[13].I; + reg[R14_IRQ].I = reg[14].I; + reg[SPSR_IRQ].I = reg[17].I; + break; + case 0x13: + reg[R13_SVC].I = reg[13].I; + reg[R14_SVC].I = reg[14].I; + reg[SPSR_SVC].I = reg[17].I; + break; + case 0x17: + reg[R13_ABT].I = reg[13].I; + reg[R14_ABT].I = reg[14].I; + reg[SPSR_ABT].I = reg[17].I; + break; + case 0x1b: + reg[R13_UND].I = reg[13].I; + reg[R14_UND].I = reg[14].I; + reg[SPSR_UND].I = reg[17].I; + break; + } + + u32 CPSR = reg[16].I; + u32 SPSR = reg[17].I; + + switch(mode) { + case 0x10: + case 0x1F: + reg[13].I = reg[R13_USR].I; + reg[14].I = reg[R14_USR].I; + reg[16].I = SPSR; + break; + case 0x11: + CPUSwap(®[8].I, ®[R8_FIQ].I); + CPUSwap(®[9].I, ®[R9_FIQ].I); + CPUSwap(®[10].I, ®[R10_FIQ].I); + CPUSwap(®[11].I, ®[R11_FIQ].I); + CPUSwap(®[12].I, ®[R12_FIQ].I); + reg[13].I = reg[R13_FIQ].I; + reg[14].I = reg[R14_FIQ].I; + if(saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_FIQ].I; + break; + case 0x12: + reg[13].I = reg[R13_IRQ].I; + reg[14].I = reg[R14_IRQ].I; + reg[16].I = SPSR; + if(saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_IRQ].I; + break; + case 0x13: + reg[13].I = reg[R13_SVC].I; + reg[14].I = reg[R14_SVC].I; + reg[16].I = SPSR; + if(saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_SVC].I; + break; + case 0x17: + reg[13].I = reg[R13_ABT].I; + reg[14].I = reg[R14_ABT].I; + reg[16].I = SPSR; + if(saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_ABT].I; + break; + case 0x1b: + reg[13].I = reg[R13_UND].I; + reg[14].I = reg[R14_UND].I; + reg[16].I = SPSR; + if(saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_UND].I; + break; + default: + systemMessage(MSG_UNSUPPORTED_ARM_MODE, N_("Unsupported ARM mode %02x"), mode); + break; + } + armMode = mode; + CPUUpdateFlags(breakLoop); + CPUUpdateCPSR(); +} + +void CPUSwitchMode(int mode, bool saveState) +{ + CPUSwitchMode(mode, saveState, true); +} + +void CPUUndefinedException() +{ + u32 PC = reg[15].I; + bool savedArmState = armState; + CPUSwitchMode(0x1b, true, false); + reg[14].I = PC - (savedArmState ? 4 : 2); + reg[15].I = 0x04; + armState = true; + armIrqEnable = false; + armNextPC = 0x04; + reg[15].I += 4; +} + +void CPUSoftwareInterrupt() +{ + u32 PC = reg[15].I; + bool savedArmState = armState; + CPUSwitchMode(0x13, true, false); + reg[14].I = PC - (savedArmState ? 4 : 2); + reg[15].I = 0x08; + armState = true; + armIrqEnable = false; + armNextPC = 0x08; + reg[15].I += 4; +} + +void CPUSoftwareInterrupt(int comment) +{ + static bool disableMessage = false; + if(armState) comment >>= 16; +#ifdef BKPT_SUPPORT + if(comment == 0xff) { + extern void (*dbgOutput)(char *, u32); + dbgOutput(NULL, reg[0].I); + return; + } +#endif +#ifdef PROFILING + if(comment == 0xfe) { + profStartup(reg[0].I, reg[1].I); + return; + } + if(comment == 0xfd) { + profControl(reg[0].I); + return; + } + if(comment == 0xfc) { + profCleanup(); + return; + } + if(comment == 0xfb) { + profCount(); + return; + } +#endif + if(comment == 0xfa) { + agbPrintFlush(); + return; + } +#ifdef SDL + if(comment == 0xf9) { + emulating = 0; + cpuNextEvent = 0; + cpuBreakLoop = true; + return; + } +#endif + if(useBios) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("SWI: %08x at %08x (0x%08x,0x%08x,0x%08x,VCOUNT = %2d)\n", comment, + armState ? armNextPC - 4: armNextPC -2, + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + return; + } + // This would be correct, but it causes problems if uncommented + // else { + // biosProtected = 0xe3a02004; + // } + + switch(comment) { + case 0x00: + BIOS_SoftReset(); + break; + case 0x01: + BIOS_RegisterRamReset(); + break; + case 0x02: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Halt: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + holdState = true; + holdType = -1; + cpuNextEvent = 0; + break; + case 0x03: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Stop: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + holdState = true; + holdType = -1; + stopState = true; + cpuNextEvent = 0; + break; + case 0x04: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("IntrWait: 0x%08x,0x%08x (VCOUNT = %2d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + break; + case 0x05: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("VBlankIntrWait: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + break; + case 0x06: + CPUSoftwareInterrupt(); + break; + case 0x07: + CPUSoftwareInterrupt(); + break; + case 0x08: + BIOS_Sqrt(); + break; + case 0x09: + BIOS_ArcTan(); + break; + case 0x0A: + BIOS_ArcTan2(); + break; + case 0x0B: + BIOS_CpuSet(); + break; + case 0x0C: + BIOS_CpuFastSet(); + break; + case 0x0E: + BIOS_BgAffineSet(); + break; + case 0x0F: + BIOS_ObjAffineSet(); + break; + case 0x10: + BIOS_BitUnPack(); + break; + case 0x11: + BIOS_LZ77UnCompWram(); + break; + case 0x12: + BIOS_LZ77UnCompVram(); + break; + case 0x13: + BIOS_HuffUnComp(); + break; + case 0x14: + BIOS_RLUnCompWram(); + break; + case 0x15: + BIOS_RLUnCompVram(); + break; + case 0x16: + BIOS_Diff8bitUnFilterWram(); + break; + case 0x17: + BIOS_Diff8bitUnFilterVram(); + break; + case 0x18: + BIOS_Diff16bitUnFilter(); + break; + case 0x19: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("SoundBiasSet: 0x%08x (VCOUNT = %2d)\n", + reg[0].I, + VCOUNT); + } +#endif + if(reg[0].I) + systemSoundPause(); + else + systemSoundResume(); + break; + case 0x1F: + BIOS_MidiKey2Freq(); + break; + case 0x2A: + BIOS_SndDriverJmpTableCopy(); + // let it go, because we don't really emulate this function + default: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("SWI: %08x at %08x (0x%08x,0x%08x,0x%08x,VCOUNT = %2d)\n", comment, + armState ? armNextPC - 4: armNextPC -2, + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + if(!disableMessage) { + systemMessage(MSG_UNSUPPORTED_BIOS_FUNCTION, + N_("Unsupported BIOS function %02x called from %08x. A BIOS file is needed in order to get correct behaviour."), + comment, + armMode ? armNextPC - 4: armNextPC - 2); + disableMessage = true; + } + break; + } +} + +void CPUCompareVCOUNT() +{ + if(VCOUNT == (DISPSTAT >> 8)) { + DISPSTAT |= 4; + UPDATE_REG(0x04, DISPSTAT); + + if(DISPSTAT & 0x20) { + IF |= 4; + UPDATE_REG(0x202, IF); + } + } else { + DISPSTAT &= 0xFFFB; + UPDATE_REG(0x4, DISPSTAT); + } +} + +void doDMA(u32 &s, u32 &d, u32 si, u32 di, u32 c, int transfer32) +{ + int sm = s >> 24; + int dm = d >> 24; + + int sc = c; + + cpuDmaCount = c; + + if(transfer32) { + s &= 0xFFFFFFFC; + if(s < 0x02000000 && (reg[15].I >> 24)) { + while(c != 0) { + CPUWriteMemory(d, 0); + d += di; + c--; + } + } else { + while(c != 0) { + cpuDmaLast = CPUReadMemory(s); + CPUWriteMemory(d, cpuDmaLast); + d += di; + s += si; + c--; + } + } + } else { + s &= 0xFFFFFFFE; + si = (int)si >> 1; + di = (int)di >> 1; + if(s < 0x02000000 && (reg[15].I >> 24)) { + while(c != 0) { + CPUWriteHalfWord(d, 0); + cpuDmaLast |= (cpuDmaLast<<16); + d += di; + c--; + } + } else { + while(c != 0) { + cpuDmaLast = CPUReadHalfWord(s); + CPUWriteHalfWord(d, cpuDmaLast); + d += di; + s += si; + c--; + } + } + } + + cpuDmaCount = 0; + + int sw = 1+memoryWaitSeq[sm & 15]; + int dw = 1+memoryWaitSeq[dm & 15]; + + int totalTicks = 0; + + if(transfer32) { + if(!memory32[sm & 15]) + sw <<= 1; + if(!memory32[dm & 15]) + dw <<= 1; + } + + totalTicks = (sw+dw)*sc; + + cpuDmaTicksToUpdate += totalTicks; + + cpuNextEvent = 0; +} + +bool CPUCheckDMA(int reason, int dmamask) +{ + bool res = false; + cpuDmaHack = 0; + // DMA 0 + if((DM0CNT_H & 0x8000) && (dmamask & 1)) { + if(((DM0CNT_H >> 12) & 3) == reason) { + res = true; + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch((DM0CNT_H >> 7) & 3) { + case 0: + break; + case 1: + sourceIncrement = (u32)-4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch((DM0CNT_H >> 5) & 3) { + case 0: + break; + case 1: + destIncrement = (u32)-4; + break; + case 2: + destIncrement = 0; + break; + } +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA0) { + int count = (DM0CNT_L ? DM0CNT_L : 0x4000) << 1; + if(DM0CNT_H & 0x0400) + count <<= 1; + log("DMA0: s=%08x d=%08x c=%04x count=%08x\n", dma0Source, dma0Dest, + DM0CNT_H, + count); + } +#endif + doDMA(dma0Source, dma0Dest, sourceIncrement, destIncrement, + DM0CNT_L ? DM0CNT_L : 0x4000, + DM0CNT_H & 0x0400); + cpuDmaHack = true; + if(DM0CNT_H & 0x4000) { + IF |= 0x0100; + UPDATE_REG(0x202, IF); + } + + if(((DM0CNT_H >> 5) & 3) == 3) { + dma0Dest = DM0DAD_L | (DM0DAD_H << 16); + } + + if(!(DM0CNT_H & 0x0200) || (reason == 0)) { + DM0CNT_H &= 0x7FFF; + UPDATE_REG(0xBA, DM0CNT_H); + } + } + } + + // DMA 1 + if((DM1CNT_H & 0x8000) && (dmamask & 2)) { + if(((DM1CNT_H >> 12) & 3) == reason) { + res = true; + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch((DM1CNT_H >> 7) & 3) { + case 0: + break; + case 1: + sourceIncrement = (u32)-4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch((DM1CNT_H >> 5) & 3) { + case 0: + break; + case 1: + destIncrement = (u32)-4; + break; + case 2: + destIncrement = 0; + break; + } + if(reason == 3) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA1) { + log("DMA1: s=%08x d=%08x c=%04x count=%08x\n", dma1Source, dma1Dest, + DM1CNT_H, + 16); + } +#endif + doDMA(dma1Source, dma1Dest, sourceIncrement, 0, 4, + 0x0400); + } else { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA1) { + int count = (DM1CNT_L ? DM1CNT_L : 0x4000) << 1; + if(DM1CNT_H & 0x0400) + count <<= 1; + log("DMA1: s=%08x d=%08x c=%04x count=%08x\n", dma1Source, dma1Dest, + DM1CNT_H, + count); + } +#endif + doDMA(dma1Source, dma1Dest, sourceIncrement, destIncrement, + DM1CNT_L ? DM1CNT_L : 0x4000, + DM1CNT_H & 0x0400); + } + cpuDmaHack = true; + + if(DM1CNT_H & 0x4000) { + IF |= 0x0200; + UPDATE_REG(0x202, IF); + } + + if(((DM1CNT_H >> 5) & 3) == 3) { + dma1Dest = DM1DAD_L | (DM1DAD_H << 16); + } + + if(!(DM1CNT_H & 0x0200) || (reason == 0)) { + DM1CNT_H &= 0x7FFF; + UPDATE_REG(0xC6, DM1CNT_H); + } + } + } + + // DMA 2 + if((DM2CNT_H & 0x8000) && (dmamask & 4)) { + if(((DM2CNT_H >> 12) & 3) == reason) { + res = true; + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch((DM2CNT_H >> 7) & 3) { + case 0: + break; + case 1: + sourceIncrement = (u32)-4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch((DM2CNT_H >> 5) & 3) { + case 0: + break; + case 1: + destIncrement = (u32)-4; + break; + case 2: + destIncrement = 0; + break; + } + if(reason == 3) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA2) { + int count = (4) << 2; + log("DMA2: s=%08x d=%08x c=%04x count=%08x\n", dma2Source, dma2Dest, + DM2CNT_H, + count); + } +#endif + doDMA(dma2Source, dma2Dest, sourceIncrement, 0, 4, + 0x0400); + } else { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA2) { + int count = (DM2CNT_L ? DM2CNT_L : 0x4000) << 1; + if(DM2CNT_H & 0x0400) + count <<= 1; + log("DMA2: s=%08x d=%08x c=%04x count=%08x\n", dma2Source, dma2Dest, + DM2CNT_H, + count); + } +#endif + doDMA(dma2Source, dma2Dest, sourceIncrement, destIncrement, + DM2CNT_L ? DM2CNT_L : 0x4000, + DM2CNT_H & 0x0400); + } + cpuDmaHack = true; + if(DM2CNT_H & 0x4000) { + IF |= 0x0400; + UPDATE_REG(0x202, IF); + } + + if(((DM2CNT_H >> 5) & 3) == 3) { + dma2Dest = DM2DAD_L | (DM2DAD_H << 16); + } + + if(!(DM2CNT_H & 0x0200) || (reason == 0)) { + DM2CNT_H &= 0x7FFF; + UPDATE_REG(0xD2, DM2CNT_H); + } + } + } + + // DMA 3 + if((DM3CNT_H & 0x8000) && (dmamask & 8)) { + if(((DM3CNT_H >> 12) & 3) == reason) { + res = true; + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch((DM3CNT_H >> 7) & 3) { + case 0: + break; + case 1: + sourceIncrement = (u32)-4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch((DM3CNT_H >> 5) & 3) { + case 0: + break; + case 1: + destIncrement = (u32)-4; + break; + case 2: + destIncrement = 0; + break; + } +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_DMA3) { + int count = (DM3CNT_L ? DM3CNT_L : 0x10000) << 1; + if(DM3CNT_H & 0x0400) + count <<= 1; + log("DMA3: s=%08x d=%08x c=%04x count=%08x\n", dma3Source, dma3Dest, + DM3CNT_H, + count); + } +#endif + doDMA(dma3Source, dma3Dest, sourceIncrement, destIncrement, + DM3CNT_L ? DM3CNT_L : 0x10000, + DM3CNT_H & 0x0400); + if(DM3CNT_H & 0x4000) { + IF |= 0x0800; + UPDATE_REG(0x202, IF); + } + + if(((DM3CNT_H >> 5) & 3) == 3) { + dma3Dest = DM3DAD_L | (DM3DAD_H << 16); + } + + if(!(DM3CNT_H & 0x0200) || (reason == 0)) { + DM3CNT_H &= 0x7FFF; + UPDATE_REG(0xDE, DM3CNT_H); + } + } + } + cpuDmaHack = false; + return res; +} + +void CPUUpdateRegister(u32 address, u16 value) +{ + switch(address) { + case 0x00: + { + bool change = ((DISPCNT ^ value) & 0x80) ? true : false; + bool changeBG = ((DISPCNT ^ value) & 0x0F00) ? true : false; + DISPCNT = (value & 0xFFF7); + UPDATE_REG(0x00, DISPCNT); + layerEnable = layerSettings & value; + windowOn = (layerEnable & 0x6000) ? true : false; + if(change && !((value & 0x80))) { + if(!(DISPSTAT & 1)) { + lcdTicks = 960; + // VCOUNT = 0; + // UPDATE_REG(0x06, VCOUNT); + DISPSTAT &= 0xFFFC; + UPDATE_REG(0x04, DISPSTAT); + CPUCompareVCOUNT(); + } + // (*renderLine)(); + } + CPUUpdateRender(); + // we only care about changes in BG0-BG3 + if(changeBG) + CPUUpdateRenderBuffers(false); + // CPUUpdateTicks(); + } + break; + case 0x04: + DISPSTAT = (value & 0xFF38) | (DISPSTAT & 7); + UPDATE_REG(0x04, DISPSTAT); + break; + case 0x06: + // not writable + break; + case 0x08: + BG0CNT = (value & 0xDFCF); + UPDATE_REG(0x08, BG0CNT); + break; + case 0x0A: + BG1CNT = (value & 0xDFCF); + UPDATE_REG(0x0A, BG1CNT); + break; + case 0x0C: + BG2CNT = (value & 0xFFCF); + UPDATE_REG(0x0C, BG2CNT); + break; + case 0x0E: + BG3CNT = (value & 0xFFCF); + UPDATE_REG(0x0E, BG3CNT); + break; + case 0x10: + BG0HOFS = value & 511; + UPDATE_REG(0x10, BG0HOFS); + break; + case 0x12: + BG0VOFS = value & 511; + UPDATE_REG(0x12, BG0VOFS); + break; + case 0x14: + BG1HOFS = value & 511; + UPDATE_REG(0x14, BG1HOFS); + break; + case 0x16: + BG1VOFS = value & 511; + UPDATE_REG(0x16, BG1VOFS); + break; + case 0x18: + BG2HOFS = value & 511; + UPDATE_REG(0x18, BG2HOFS); + break; + case 0x1A: + BG2VOFS = value & 511; + UPDATE_REG(0x1A, BG2VOFS); + break; + case 0x1C: + BG3HOFS = value & 511; + UPDATE_REG(0x1C, BG3HOFS); + break; + case 0x1E: + BG3VOFS = value & 511; + UPDATE_REG(0x1E, BG3VOFS); + break; + case 0x20: + BG2PA = value; + UPDATE_REG(0x20, BG2PA); + break; + case 0x22: + BG2PB = value; + UPDATE_REG(0x22, BG2PB); + break; + case 0x24: + BG2PC = value; + UPDATE_REG(0x24, BG2PC); + break; + case 0x26: + BG2PD = value; + UPDATE_REG(0x26, BG2PD); + break; + case 0x28: + BG2X_L = value; + UPDATE_REG(0x28, BG2X_L); + gfxBG2Changed |= 1; + break; + case 0x2A: + BG2X_H = (value & 0xFFF); + UPDATE_REG(0x2A, BG2X_H); + gfxBG2Changed |= 1; + break; + case 0x2C: + BG2Y_L = value; + UPDATE_REG(0x2C, BG2Y_L); + gfxBG2Changed |= 2; + break; + case 0x2E: + BG2Y_H = value & 0xFFF; + UPDATE_REG(0x2E, BG2Y_H); + gfxBG2Changed |= 2; + break; + case 0x30: + BG3PA = value; + UPDATE_REG(0x30, BG3PA); + break; + case 0x32: + BG3PB = value; + UPDATE_REG(0x32, BG3PB); + break; + case 0x34: + BG3PC = value; + UPDATE_REG(0x34, BG3PC); + break; + case 0x36: + BG3PD = value; + UPDATE_REG(0x36, BG3PD); + break; + case 0x38: + BG3X_L = value; + UPDATE_REG(0x38, BG3X_L); + gfxBG3Changed |= 1; + break; + case 0x3A: + BG3X_H = value & 0xFFF; + UPDATE_REG(0x3A, BG3X_H); + gfxBG3Changed |= 1; + break; + case 0x3C: + BG3Y_L = value; + UPDATE_REG(0x3C, BG3Y_L); + gfxBG3Changed |= 2; + break; + case 0x3E: + BG3Y_H = value & 0xFFF; + UPDATE_REG(0x3E, BG3Y_H); + gfxBG3Changed |= 2; + break; + case 0x40: + WIN0H = value; + UPDATE_REG(0x40, WIN0H); + CPUUpdateWindow0(); + break; + case 0x42: + WIN1H = value; + UPDATE_REG(0x42, WIN1H); + CPUUpdateWindow1(); + break; + case 0x44: + WIN0V = value; + UPDATE_REG(0x44, WIN0V); + break; + case 0x46: + WIN1V = value; + UPDATE_REG(0x46, WIN1V); + break; + case 0x48: + WININ = value & 0x3F3F; + UPDATE_REG(0x48, WININ); + break; + case 0x4A: + WINOUT = value & 0x3F3F; + UPDATE_REG(0x4A, WINOUT); + break; + case 0x4C: + MOSAIC = value; + UPDATE_REG(0x4C, MOSAIC); + break; + case 0x50: + BLDMOD = value & 0x3FFF; + UPDATE_REG(0x50, BLDMOD); + fxOn = ((BLDMOD>>6)&3) != 0; + CPUUpdateRender(); + break; + case 0x52: + COLEV = value & 0x1F1F; + UPDATE_REG(0x52, COLEV); + break; + case 0x54: + COLY = value & 0x1F; + UPDATE_REG(0x54, COLY); + break; + case 0x60: + case 0x62: + case 0x64: + case 0x68: + case 0x6c: + case 0x70: + case 0x72: + case 0x74: + case 0x78: + case 0x7c: + case 0x80: + case 0x84: + soundEvent(address&0xFF, (u8)(value & 0xFF)); + soundEvent((address&0xFF)+1, (u8)(value>>8)); + break; + case 0x82: + case 0x88: + case 0xa0: + case 0xa2: + case 0xa4: + case 0xa6: + case 0x90: + case 0x92: + case 0x94: + case 0x96: + case 0x98: + case 0x9a: + case 0x9c: + case 0x9e: + soundEvent(address&0xFF, value); + break; + case 0xB0: + DM0SAD_L = value; + UPDATE_REG(0xB0, DM0SAD_L); + break; + case 0xB2: + DM0SAD_H = value & 0x07FF; + UPDATE_REG(0xB2, DM0SAD_H); + break; + case 0xB4: + DM0DAD_L = value; + UPDATE_REG(0xB4, DM0DAD_L); + break; + case 0xB6: + DM0DAD_H = value & 0x07FF; + UPDATE_REG(0xB6, DM0DAD_H); + break; + case 0xB8: + DM0CNT_L = value & 0x3FFF; + UPDATE_REG(0xB8, 0); + break; + case 0xBA: + { + bool start = ((DM0CNT_H ^ value) & 0x8000) ? true : false; + value &= 0xF7E0; + + DM0CNT_H = value; + UPDATE_REG(0xBA, DM0CNT_H); + + if(start && (value & 0x8000)) { + dma0Source = DM0SAD_L | (DM0SAD_H << 16); + dma0Dest = DM0DAD_L | (DM0DAD_H << 16); + CPUCheckDMA(0, 1); + } + } + break; + case 0xBC: + DM1SAD_L = value; + UPDATE_REG(0xBC, DM1SAD_L); + break; + case 0xBE: + DM1SAD_H = value & 0x0FFF; + UPDATE_REG(0xBE, DM1SAD_H); + break; + case 0xC0: + DM1DAD_L = value; + UPDATE_REG(0xC0, DM1DAD_L); + break; + case 0xC2: + DM1DAD_H = value & 0x07FF; + UPDATE_REG(0xC2, DM1DAD_H); + break; + case 0xC4: + DM1CNT_L = value & 0x3FFF; + UPDATE_REG(0xC4, 0); + break; + case 0xC6: + { + bool start = ((DM1CNT_H ^ value) & 0x8000) ? true : false; + value &= 0xF7E0; + + DM1CNT_H = value; + UPDATE_REG(0xC6, DM1CNT_H); + + if(start && (value & 0x8000)) { + dma1Source = DM1SAD_L | (DM1SAD_H << 16); + dma1Dest = DM1DAD_L | (DM1DAD_H << 16); + CPUCheckDMA(0, 2); + } + } + break; + case 0xC8: + DM2SAD_L = value; + UPDATE_REG(0xC8, DM2SAD_L); + break; + case 0xCA: + DM2SAD_H = value & 0x0FFF; + UPDATE_REG(0xCA, DM2SAD_H); + break; + case 0xCC: + DM2DAD_L = value; + UPDATE_REG(0xCC, DM2DAD_L); + break; + case 0xCE: + DM2DAD_H = value & 0x07FF; + UPDATE_REG(0xCE, DM2DAD_H); + break; + case 0xD0: + DM2CNT_L = value & 0x3FFF; + UPDATE_REG(0xD0, 0); + break; + case 0xD2: + { + bool start = ((DM2CNT_H ^ value) & 0x8000) ? true : false; + + value &= 0xF7E0; + + DM2CNT_H = value; + UPDATE_REG(0xD2, DM2CNT_H); + + if(start && (value & 0x8000)) { + dma2Source = DM2SAD_L | (DM2SAD_H << 16); + dma2Dest = DM2DAD_L | (DM2DAD_H << 16); + + CPUCheckDMA(0, 4); + } + } + break; + case 0xD4: + DM3SAD_L = value; + UPDATE_REG(0xD4, DM3SAD_L); + break; + case 0xD6: + DM3SAD_H = value & 0x0FFF; + UPDATE_REG(0xD6, DM3SAD_H); + break; + case 0xD8: + DM3DAD_L = value; + UPDATE_REG(0xD8, DM3DAD_L); + break; + case 0xDA: + DM3DAD_H = value & 0x0FFF; + UPDATE_REG(0xDA, DM3DAD_H); + break; + case 0xDC: + DM3CNT_L = value; + UPDATE_REG(0xDC, 0); + break; + case 0xDE: + { + bool start = ((DM3CNT_H ^ value) & 0x8000) ? true : false; + + value &= 0xFFE0; + + DM3CNT_H = value; + UPDATE_REG(0xDE, DM3CNT_H); + + if(start && (value & 0x8000)) { + dma3Source = DM3SAD_L | (DM3SAD_H << 16); + dma3Dest = DM3DAD_L | (DM3DAD_H << 16); + CPUCheckDMA(0,8); + } + } + break; + case 0x100: + timer0Reload = value; + break; + case 0x102: + timer0Ticks = timer0ClockReload = TIMER_TICKS[value & 3]; + if(!timer0On && (value & 0x80)) { + // reload the counter + TM0D = timer0Reload; + if(timer0ClockReload == 1) + timer0Ticks = 0x10000 - TM0D; + UPDATE_REG(0x100, TM0D); + int event = cpuTotalTicks + timer0Ticks; + if(event < cpuNextEvent) + cpuNextEvent = event; + } + timer0On = value & 0x80 ? true : false; + TM0CNT = value & 0xC7; + UPDATE_REG(0x102, TM0CNT); + // CPUUpdateTicks(); + break; + case 0x104: + timer1Reload = value; + break; + case 0x106: + timer1Ticks = timer1ClockReload = TIMER_TICKS[value & 3]; + if(!timer1On && (value & 0x80)) { + // reload the counter + TM1D = timer1Reload; + if(timer1ClockReload == 1) + timer1Ticks = 0x10000 - TM1D; + UPDATE_REG(0x104, TM1D); + int event = cpuTotalTicks + timer1Ticks; + if(event < cpuNextEvent) + cpuNextEvent = event; + } + timer1On = value & 0x80 ? true : false; + TM1CNT = value & 0xC7; + UPDATE_REG(0x106, TM1CNT); + break; + case 0x108: + timer2Reload = value; + break; + case 0x10A: + timer2Ticks = timer2ClockReload = TIMER_TICKS[value & 3]; + if(!timer2On && (value & 0x80)) { + // reload the counter + TM2D = timer2Reload; + if(timer2ClockReload == 1) + timer2Ticks = 0x10000 - TM2D; + UPDATE_REG(0x108, TM2D); + int event = cpuTotalTicks + timer2Ticks; + if(event < cpuNextEvent) + cpuNextEvent = event; + } + timer2On = value & 0x80 ? true : false; + TM2CNT = value & 0xC7; + UPDATE_REG(0x10A, TM2CNT); + break; + case 0x10C: + timer3Reload = value; + break; + case 0x10E: + timer3Ticks = timer3ClockReload = TIMER_TICKS[value & 3]; + if(!timer3On && (value & 0x80)) { + // reload the counter + TM3D = timer3Reload; + if(timer3ClockReload == 1) + timer3Ticks = 0x10000 - TM3D; + UPDATE_REG(0x10C, TM3D); + int event = cpuTotalTicks + timer3Ticks; + if(event < cpuNextEvent) + cpuNextEvent = event; + } + timer3On = value & 0x80 ? true : false; + TM3CNT = value & 0xC7; + UPDATE_REG(0x10E, TM3CNT); + break; + case 0x128: + StartLink(value); // Link + if(value & 0x80) { + value &= 0xff7f; + if(value & 1 && (value & 0x4000)) { + UPDATE_REG(0x12a, 0xFF); + IF |= 0x80; + UPDATE_REG(0x202, IF); + value &= 0x7f7f; + } + } + UPDATE_REG(0x128, value); + break; +/* Link +------------------------------*/ + case 0x12a: + if(lspeed) + LinkSSend(value); + UPDATE_REG(0x12a, value); + break; +/* --------------------------- */ + case 0x130: + P1 |= (value & 0x3FF); + UPDATE_REG(0x130, P1); + break; + case 0x132: + UPDATE_REG(0x132, value & 0xC3FF); + break; + case 0x134: + StartGPLink(value); + break; + case 0x140: + StartJOYLink(value); + break; + case 0x200: + IE = value & 0x3FFF; + UPDATE_REG(0x200, IE); + if((IME & 1) && (IF & IE) && armIrqEnable) { + cpuNextEvent = 0; + } + break; + case 0x202: + IF ^= (value & IF); + UPDATE_REG(0x202, IF); + break; + case 0x204: + { + int i; + memoryWait[0x0e] = memoryWaitSeq[0x0e] = gamepakRamWaitState[value & 3]; + + if(!speedHack) { + memoryWait[0x08] = memoryWait[0x09] = gamepakWaitState[(value >> 2) & 7]; + memoryWaitSeq[0x08] = memoryWaitSeq[0x09] = + gamepakWaitState0[(value >> 2) & 7]; + + memoryWait[0x0a] = memoryWait[0x0b] = gamepakWaitState[(value >> 5) & 7]; + memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] = + gamepakWaitState1[(value >> 5) & 7]; + + memoryWait[0x0c] = memoryWait[0x0d] = gamepakWaitState[(value >> 8) & 7]; + memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] = + gamepakWaitState2[(value >> 8) & 7]; + } else { + memoryWait[0x08] = memoryWait[0x09] = 4; + memoryWaitSeq[0x08] = memoryWaitSeq[0x09] = 2; + + memoryWait[0x0a] = memoryWait[0x0b] = 4; + memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] = 4; + + memoryWait[0x0c] = memoryWait[0x0d] = 4; + memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] = 8; + } + for(i = 0; i < 16; i++) { + memoryWaitFetch32[i] = memoryWait32[i] = memoryWait[i] * + (memory32[i] ? 1 : 2); + memoryWaitFetch[i] = memoryWait[i]; + } + memoryWaitFetch32[3] += 1; + memoryWaitFetch32[2] += 3; + + if(value & 0x4000) { + for(i = 8; i < 16; i++) { + memoryWaitFetch32[i] = cpuMemoryWait32[i]; + memoryWaitFetch[i] = cpuMemoryWait[i]; + } + } + UPDATE_REG(0x204, value); + } + break; + case 0x208: + IME = value & 1; + UPDATE_REG(0x208, IME); + if((IME & 1) && (IF & IE) && armIrqEnable) { + cpuNextEvent = 0; + } + break; + case 0x300: + if(value != 0) + value &= 0xFFFE; + UPDATE_REG(0x300, value); + break; + default: + UPDATE_REG(address&0x3FE, value); + break; + } +} + +void CPUWriteHalfWord(u32 address, u16 value) +{ +#ifdef DEV_VERSION + if(address & 1) { + if(systemVerbose & VERBOSE_UNALIGNED_MEMORY) { + log("Unaligned halfword write: %04x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } + } +#endif + + switch(address >> 24) { + case 2: +#ifdef SDL + if(*((u16 *)&freezeWorkRAM[address & 0x3FFFE])) + cheatsWriteHalfWord(address & 0x203FFFE, + value); + else +#endif + WRITE16LE(((u16 *)&workRAM[address & 0x3FFFE]),value); + break; + case 3: +#ifdef SDL + if(*((u16 *)&freezeInternalRAM[address & 0x7ffe])) + cheatsWriteHalfWord(address & 0x3007ffe, + value); + else +#endif + WRITE16LE(((u16 *)&internalRAM[address & 0x7ffe]), value); + break; + case 4: + if(address < 0x4000400) + CPUUpdateRegister(address & 0x3fe, value); + else goto unwritable; + break; + case 5: + WRITE16LE(((u16 *)&paletteRAM[address & 0x3fe]), value); + break; + case 6: + if(address & 0x10000) + WRITE16LE(((u16 *)&vram[address & 0x17ffe]), value); + else + WRITE16LE(((u16 *)&vram[address & 0x1fffe]), value); + break; + case 7: + WRITE16LE(((u16 *)&oam[address & 0x3fe]), value); + break; + case 8: + case 9: + if(address == 0x80000c4 || address == 0x80000c6 || address == 0x80000c8) { + if(!rtcWrite(address, value)) + goto unwritable; + } else if(!agbPrintWrite(address, value)) goto unwritable; + break; + case 13: + if(cpuEEPROMEnabled) { + eepromWrite(address, (u8)value); + break; + } + goto unwritable; + case 14: + if(!eepromInUse | cpuSramEnabled | cpuFlashEnabled) { + (*cpuSaveGameFunc)(address, (u8)value); + break; + } + goto unwritable; + default: + unwritable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_WRITE) { + log("Illegal halfword write: %04x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } +#endif + break; + } +} + +void CPUWriteByte(u32 address, u8 b) +{ + switch(address >> 24) { + case 2: +#ifdef SDL + if(freezeWorkRAM[address & 0x3FFFF]) + cheatsWriteByte(address & 0x203FFFF, b); + else +#endif + workRAM[address & 0x3FFFF] = b; + break; + case 3: +#ifdef SDL + if(freezeInternalRAM[address & 0x7fff]) + cheatsWriteByte(address & 0x3007fff, b); + else +#endif + internalRAM[address & 0x7fff] = b; + break; + case 4: + if(address < 0x4000400) { + switch(address & 0x3FF) { + case 0x301: + if(b == 0x80) + stopState = true; + holdState = 1; + holdType = -1; + cpuNextEvent = 0; + break; + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x68: + case 0x69: + case 0x6c: + case 0x6d: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x78: + case 0x79: + case 0x7c: + case 0x7d: + case 0x80: + case 0x81: + case 0x84: + case 0x85: + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + case 0x98: + case 0x99: + case 0x9a: + case 0x9b: + case 0x9c: + case 0x9d: + case 0x9e: + case 0x9f: + soundEvent(address&0xFF, b); + break; + default: + if(address & 1) + CPUUpdateRegister(address & 0x3fe, + ((READ16LE(((u16 *)&ioMem[address & 0x3fe]))) + & 0x00FF) | + b<<8); + else + CPUUpdateRegister(address & 0x3fe, + ((READ16LE(((u16 *)&ioMem[address & 0x3fe])) & 0xFF00) | b)); + } + break; + } else goto unwritable; + break; + case 5: + // no need to switch + *((u16 *)&paletteRAM[address & 0x3FE]) = (b << 8) | b; + break; + case 6: + // no need to switch + // byte writes to OBJ VRAM are ignored + if(!(address & 0x10000)) + // *((u16 *)&vram[address & 0x17FFE]) = (b << 8) | b; + // else + *((u16 *)&vram[address & 0xFFFE]) = (b << 8) | b; + break; + case 7: + // no need to switch + // byte writes to OAM are ignored + // *((u16 *)&oam[address & 0x3FE]) = (b << 8) | b; + break; + case 13: + if(cpuEEPROMEnabled) { + eepromWrite(address, b); + break; + } + goto unwritable; + case 14: + if(!eepromInUse | cpuSramEnabled | cpuFlashEnabled) { + (*cpuSaveGameFunc)(address, b); + break; + } + // default + default: + unwritable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_WRITE) { + log("Illegal byte write: %02x to %08x from %08x\n", + b, + address, + armMode ? armNextPC - 4 : armNextPC -2 ); + } +#endif + break; + } +} +u8 cpuBitsSet[256]; +u8 cpuLowestBitSet[256]; + +void CPUInit(const char *biosFileName, bool useBiosFile) +{ +#ifdef WORDS_BIGENDIAN + if(!cpuBiosSwapped) { + for(unsigned int i = 0; i < sizeof(myROM)/4; i++) { + WRITE32LE(&myROM[i], myROM[i]); + } + cpuBiosSwapped = true; + } +#endif + gbaSaveType = 0; + eepromInUse = 0; + saveType = 0; + useBios = false; + + if(useBiosFile) { + int size = 0x4000; + if(utilLoad(biosFileName, + CPUIsGBABios, + bios, + size)) { + if(size == 0x4000) + useBios = true; + else + systemMessage(MSG_INVALID_BIOS_FILE_SIZE, N_("Invalid BIOS file size")); + } + } + + if(!useBios) { + memcpy(bios, myROM, sizeof(myROM)); + } + + int i = 0; + + biosProtected[0] = 0x00; + biosProtected[1] = 0xf0; + biosProtected[2] = 0x29; + biosProtected[3] = 0xe1; + + for(i = 0; i < 256; i++) { + int count = 0; + int j; + for(j = 0; j < 8; j++) + if(i & (1 << j)) + count++; + cpuBitsSet[i] = count; + + for(j = 0; j < 8; j++) + if(i & (1 << j)) + break; + cpuLowestBitSet[i] = j; + } + + for(i = 0; i < 0x400; i++) + ioReadable[i] = true; + for(i = 0x10; i < 0x48; i++) + ioReadable[i] = false; + for(i = 0x4c; i < 0x50; i++) + ioReadable[i] = false; + for(i = 0x54; i < 0x60; i++) + ioReadable[i] = false; + for(i = 0x8c; i < 0x90; i++) + ioReadable[i] = false; + for(i = 0xa0; i < 0xb8; i++) + ioReadable[i] = false; + for(i = 0xbc; i < 0xc4; i++) + ioReadable[i] = false; + for(i = 0xc8; i < 0xd0; i++) + ioReadable[i] = false; + for(i = 0xd4; i < 0xdc; i++) + ioReadable[i] = false; + for(i = 0xe0; i < 0x100; i++) + ioReadable[i] = false; + for(i = 0x110; i < 0x120; i++) + ioReadable[i] = false; + for(i = 0x12c; i < 0x130; i++) + ioReadable[i] = false; + for(i = 0x138; i < 0x140; i++) + ioReadable[i] = false; + for(i = 0x144; i < 0x150; i++) + ioReadable[i] = false; + for(i = 0x15c; i < 0x200; i++) + ioReadable[i] = false; + for(i = 0x20c; i < 0x300; i++) + ioReadable[i] = false; + for(i = 0x304; i < 0x400; i++) + ioReadable[i] = false; + + if(romSize < 0x1fe2000) { + *((u16 *)&rom[0x1fe209c]) = 0xdffa; // SWI 0xFA + *((u16 *)&rom[0x1fe209e]) = 0x4770; // BX LR + } else { + agbPrintEnable(false); + } +} + +void CPUReset() +{ + if(gbaSaveType == 0) { + if(eepromInUse) + gbaSaveType = 3; + else + switch(saveType) { + case 1: + gbaSaveType = 1; + break; + case 2: + gbaSaveType = 2; + break; + } + } + rtcReset(); + // clen registers + memset(®[0], 0, sizeof(reg)); + // clean OAM + memset(oam, 0, 0x400); + // clean palette + memset(paletteRAM, 0, 0x400); + // clean picture + memset(pix, 0, 4*160*240); + // clean vram + memset(vram, 0, 0x20000); + // clean io memory + memset(ioMem, 0, 0x400); + + DISPCNT = 0x0080; + DISPSTAT = 0x0000; + VCOUNT = 0x0000; + BG0CNT = 0x0000; + BG1CNT = 0x0000; + BG2CNT = 0x0000; + BG3CNT = 0x0000; + BG0HOFS = 0x0000; + BG0VOFS = 0x0000; + BG1HOFS = 0x0000; + BG1VOFS = 0x0000; + BG2HOFS = 0x0000; + BG2VOFS = 0x0000; + BG3HOFS = 0x0000; + BG3VOFS = 0x0000; + BG2PA = 0x0100; + BG2PB = 0x0000; + BG2PC = 0x0000; + BG2PD = 0x0100; + BG2X_L = 0x0000; + BG2X_H = 0x0000; + BG2Y_L = 0x0000; + BG2Y_H = 0x0000; + BG3PA = 0x0100; + BG3PB = 0x0000; + BG3PC = 0x0000; + BG3PD = 0x0100; + BG3X_L = 0x0000; + BG3X_H = 0x0000; + BG3Y_L = 0x0000; + BG3Y_H = 0x0000; + WIN0H = 0x0000; + WIN1H = 0x0000; + WIN0V = 0x0000; + WIN1V = 0x0000; + WININ = 0x0000; + WINOUT = 0x0000; + MOSAIC = 0x0000; + BLDMOD = 0x0000; + COLEV = 0x0000; + COLY = 0x0000; + DM0SAD_L = 0x0000; + DM0SAD_H = 0x0000; + DM0DAD_L = 0x0000; + DM0DAD_H = 0x0000; + DM0CNT_L = 0x0000; + DM0CNT_H = 0x0000; + DM1SAD_L = 0x0000; + DM1SAD_H = 0x0000; + DM1DAD_L = 0x0000; + DM1DAD_H = 0x0000; + DM1CNT_L = 0x0000; + DM1CNT_H = 0x0000; + DM2SAD_L = 0x0000; + DM2SAD_H = 0x0000; + DM2DAD_L = 0x0000; + DM2DAD_H = 0x0000; + DM2CNT_L = 0x0000; + DM2CNT_H = 0x0000; + DM3SAD_L = 0x0000; + DM3SAD_H = 0x0000; + DM3DAD_L = 0x0000; + DM3DAD_H = 0x0000; + DM3CNT_L = 0x0000; + DM3CNT_H = 0x0000; + TM0D = 0x0000; + TM0CNT = 0x0000; + TM1D = 0x0000; + TM1CNT = 0x0000; + TM2D = 0x0000; + TM2CNT = 0x0000; + TM3D = 0x0000; + TM3CNT = 0x0000; + P1 = 0x03FF; + IE = 0x0000; + IF = 0x0000; + IME = 0x0000; + + armMode = 0x1F; + + if(cpuIsMultiBoot) { + reg[13].I = 0x03007F00; + reg[15].I = 0x02000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R13_SVC].I = 0x03007FE0; + armIrqEnable = true; + } else { + if(useBios && !skipBios) { + reg[15].I = 0x00000000; + armMode = 0x13; + armIrqEnable = false; + } else { + reg[13].I = 0x03007F00; + reg[15].I = 0x08000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R13_SVC].I = 0x03007FE0; + armIrqEnable = true; + } + } + armState = true; + C_FLAG = V_FLAG = N_FLAG = Z_FLAG = false; + UPDATE_REG(0x00, DISPCNT); + UPDATE_REG(0x20, BG2PA); + UPDATE_REG(0x26, BG2PD); + UPDATE_REG(0x30, BG3PA); + UPDATE_REG(0x36, BG3PD); + UPDATE_REG(0x130, P1); + UPDATE_REG(0x88, 0x200); + + // disable FIQ + reg[16].I |= 0x40; + + CPUUpdateCPSR(); + + armNextPC = reg[15].I; + reg[15].I += 4; + + // reset internal state + holdState = false; + holdType = 0; + + biosProtected[0] = 0x00; + biosProtected[1] = 0xf0; + biosProtected[2] = 0x29; + biosProtected[3] = 0xe1; + + lcdTicks = 960; + timer0On = false; + timer0Ticks = 0; + timer0Reload = 0; + timer0ClockReload = 0; + timer1On = false; + timer1Ticks = 0; + timer1Reload = 0; + timer1ClockReload = 0; + timer2On = false; + timer2Ticks = 0; + timer2Reload = 0; + timer2ClockReload = 0; + timer3On = false; + timer3Ticks = 0; + timer3Reload = 0; + timer3ClockReload = 0; + dma0Source = 0; + dma0Dest = 0; + dma1Source = 0; + dma1Dest = 0; + dma2Source = 0; + dma2Dest = 0; + dma3Source = 0; + dma3Dest = 0; + cpuSaveGameFunc = flashSaveDecide; + renderLine = mode0RenderLine; + fxOn = false; + windowOn = false; + frameCount = 0; + saveType = 0; + layerEnable = DISPCNT & layerSettings; + + CPUUpdateRenderBuffers(true); + + for(int i = 0; i < 256; i++) { + map[i].address = (u8 *)&dummyAddress; + map[i].mask = 0; + } + + map[0].address = bios; + map[0].mask = 0x3FFF; + map[2].address = workRAM; + map[2].mask = 0x3FFFF; + map[3].address = internalRAM; + map[3].mask = 0x7FFF; + map[4].address = ioMem; + map[4].mask = 0x3FF; + map[5].address = paletteRAM; + map[5].mask = 0x3FF; + map[6].address = vram; + map[6].mask = 0x1FFFF; + map[7].address = oam; + map[7].mask = 0x3FF; + map[8].address = rom; + map[8].mask = 0x1FFFFFF; + map[9].address = rom; + map[9].mask = 0x1FFFFFF; + map[10].address = rom; + map[10].mask = 0x1FFFFFF; + map[12].address = rom; + map[12].mask = 0x1FFFFFF; + map[14].address = flashSaveMemory; + map[14].mask = 0xFFFF; + + eepromReset(); + flashReset(); + + soundReset(); + + CPUUpdateWindow0(); + CPUUpdateWindow1(); + + // make sure registers are correctly initialized if not using BIOS + if(!useBios) { + if(cpuIsMultiBoot) + BIOS_RegisterRamReset(0xfe); + else + BIOS_RegisterRamReset(0xff); + } else { + if(cpuIsMultiBoot) + BIOS_RegisterRamReset(0xfe); + } + + switch(cpuSaveType) { + case 0: // automatic + cpuSramEnabled = true; + cpuFlashEnabled = true; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = false; + break; + case 1: // EEPROM + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = false; + // EEPROM usage is automatically detected + break; + case 2: // SRAM + cpuSramEnabled = true; + cpuFlashEnabled = false; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + cpuSaveGameFunc = sramDelayedWrite; // to insure we detect the write + break; + case 3: // FLASH + cpuSramEnabled = false; + cpuFlashEnabled = true; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + cpuSaveGameFunc = flashDelayedWrite; // to insure we detect the write + break; + case 4: // EEPROM+Sensor + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = true; + // EEPROM usage is automatically detected + break; + case 5: // NONE + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + // no save at all + break; + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + cpuDmaHack2 = false; + + lastTime = systemGetClock(); +} + +void CPUInterrupt() +{ + u32 PC = reg[15].I; + bool savedState = armState; + CPUSwitchMode(0x12, true, false); + reg[14].I = PC; + if(!savedState) + reg[14].I += 2; + reg[15].I = 0x18; + armState = true; + armIrqEnable = false; + + armNextPC = reg[15].I; + reg[15].I += 4; + + // if(!holdState) + biosProtected[0] = 0x02; + biosProtected[1] = 0xc0; + biosProtected[2] = 0x5e; + biosProtected[3] = 0xe5; +} + +#ifdef SDL +void log(const char *defaultMsg, ...) +{ + char buffer[2048]; + va_list valist; + + va_start(valist, defaultMsg); + vsprintf(buffer, defaultMsg, valist); + + if(out == NULL) { + out = fopen("trace.log","w"); + } + + fputs(buffer, out); + + va_end(valist); +} +#else +extern void winlog(const char *, ...); +#endif + +void CPULoop(int ticks) +{ + int clockTicks; + int totalTicks = 0; + int timerOverflow = 0; + // variables used by the CPU core + cpuNextEvent = CPUUpdateTicks(); + if(cpuNextEvent > ticks) { + cpuNextEvent = ticks; + } + + if(intState) { + cpuNextEvent = 5; + } + if(cpuDmaHack2) + cpuNextEvent = 1; + cpuBreakLoop = false; + for(;;) { +#ifndef FINAL_VERSION + if(systemDebug) { + if(systemDebug >= 10 && !holdState) { + CPUUpdateCPSR(); + sprintf(buffer, "R00=%08x R01=%08x R02=%08x R03=%08x R04=%08x R05=%08x R06=%08x R07=%08x R08=%08x R09=%08x R10=%08x R11=%08x R12=%08x R13=%08x R14=%08x R15=%08x R16=%08x R17=%08x\n", + reg[0].I, reg[1].I, reg[2].I, reg[3].I, reg[4].I, reg[5].I, + reg[6].I, reg[7].I, reg[8].I, reg[9].I, reg[10].I, reg[11].I, + reg[12].I, reg[13].I, reg[14].I, reg[15].I, reg[16].I, + reg[17].I); +#ifdef SDL + log(buffer); +#else + winlog(buffer); +#endif + } else if(!holdState) { + sprintf(buffer, "PC=%08x\n", armNextPC); +#ifdef SDL + log(buffer); +#else + winlog(buffer); +#endif + } + } +#endif + + if(!holdState) { + if(armState) { +#include "arm-new.h" + } else { +#include "thumb.h" + } + } else { + clockTicks = lcdTicks; + + if(soundTicks < clockTicks) + clockTicks = soundTicks; + + if(timer0On && (timer0Ticks < clockTicks)) { + clockTicks = timer0Ticks; + } + if(timer1On && !(TM1CNT & 4) && (timer1Ticks < clockTicks)) { + clockTicks = timer1Ticks; + } + if(timer2On && !(TM2CNT & 4) && (timer2Ticks < clockTicks)) { + clockTicks = timer2Ticks; + } + if(timer3On && !(TM3CNT & 4) && (timer3Ticks < clockTicks)) { + clockTicks = timer3Ticks; + } +#ifdef PROFILING + if(profilingTicksReload != 0) { + if(profilingTicks < clockTicks) { + clockTicks = profilingTicks; + } + } +#endif + // if((clockTicks+totalTicks) > cpuNextEvent) { + // clockTicks = cpuNextEvent - totalTicks; + // } + } + totalTicks += clockTicks; + cpuTotalTicks = totalTicks; + if(totalTicks >= cpuNextEvent) { + cpuDmaHack2 = false; + int remainingTicks = totalTicks - cpuNextEvent; + clockTicks = cpuNextEvent; + int oldTotalTicks = totalTicks; + cpuTotalTicks = totalTicks = 0; + + updateLoop: + lcdTicks -= clockTicks; + + if(lcdTicks <= 0) { + if(DISPSTAT & 1) { // V-BLANK + // if in V-Blank mode, keep computing... + if(DISPSTAT & 2) { + lcdTicks += 960; + VCOUNT++; + UPDATE_REG(0x06, VCOUNT); + DISPSTAT &= 0xFFFD; + UPDATE_REG(0x04, DISPSTAT); + CPUCompareVCOUNT(); + } else { + lcdTicks += 272; + DISPSTAT |= 2; + UPDATE_REG(0x04, DISPSTAT); + if(DISPSTAT & 16) { + IF |= 2; + UPDATE_REG(0x202, IF); + } + } + + if(VCOUNT >= 228) { + DISPSTAT &= 0xFFFC; + UPDATE_REG(0x04, DISPSTAT); + VCOUNT = 0; + UPDATE_REG(0x06, VCOUNT); + CPUCompareVCOUNT(); + } + } else { + int framesToSkip = systemFrameSkip; + if(speedup) + framesToSkip = 9; // try 6 FPS during speedup + + if(DISPSTAT & 2) { + // if in H-Blank, leave it and move to drawing mode + VCOUNT++; + UPDATE_REG(0x06, VCOUNT); + + lcdTicks += (960); + DISPSTAT &= 0xFFFD; + if(VCOUNT == 160) { + count++; + systemFrame(); + + if((count % 10) == 0) { + system10Frames(60); + } + if(count == 60) { + u32 time = systemGetClock(); + if(time != lastTime) { + u32 t = 100000/(time - lastTime); + systemShowSpeed(t); + } else + systemShowSpeed(0); + lastTime = time; + count = 0; + } + u32 joy = 0; + // update joystick information + if(systemReadJoypads()) + // read default joystick + joy = systemReadJoypad(-1); + P1 = 0x03FF ^ (joy & 0x3FF); + if(cpuEEPROMSensorEnabled) + systemUpdateMotionSensor(); + UPDATE_REG(0x130, P1); + u16 P1CNT = READ16LE(((u16 *)&ioMem[0x132])); + // this seems wrong, but there are cases where the game + // can enter the stop state without requesting an IRQ from + // the joypad. + if((P1CNT & 0x4000) || stopState) { + u16 p1 = (0x3FF ^ P1) & 0x3FF; + if(P1CNT & 0x8000) { + if(p1 == (P1CNT & 0x3FF)) { + IF |= 0x1000; + UPDATE_REG(0x202, IF); + } + } else { + if(p1 & P1CNT) { + IF |= 0x1000; + UPDATE_REG(0x202, IF); + } + } + } + + u32 ext = (joy >> 10); + if(cheatsEnabled) + remainingTicks += cheatsCheckKeys(P1^0x3FF, ext); + speedup = (ext & 1) ? true : false; + capture = (ext & 2) ? true : false; + + if(capture && !capturePrevious) { + captureNumber++; + systemScreenCapture(captureNumber); + } + capturePrevious = capture; + + DISPSTAT |= 1; + DISPSTAT &= 0xFFFD; + UPDATE_REG(0x04, DISPSTAT); + if(DISPSTAT & 0x0008) { + IF |= 1; + UPDATE_REG(0x202, IF); + } + cpuDmaHack2 = CPUCheckDMA(1, 0x0f); + if(frameCount >= framesToSkip) { + systemDrawScreen(); + frameCount = 0; + } else + frameCount++; + if(systemPauseOnFrame()) + ticks = 0; + } + + UPDATE_REG(0x04, DISPSTAT); + + CPUCompareVCOUNT(); + } else { + if(frameCount >= framesToSkip) { + (*renderLine)(); + + switch(systemColorDepth) { + case 16: + { + u16 *dest = (u16 *)pix + 242 * (VCOUNT+1); + for(int x = 0; x < 240;) { + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++]&0xFFFF]; + } + // for filters that read past the screen + *dest++ = 0; + } + break; + case 24: + { + u8 *dest = (u8 *)pix + 240 * VCOUNT * 3; + for(int x = 0; x < 240;) { + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + } + } + break; + case 32: + { + u32 *dest = (u32 *)pix + 241 * (VCOUNT+1); + for(int x = 0; x < 240; ) { + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + } + } + break; + } + } + // entering H-Blank + DISPSTAT |= 2; + UPDATE_REG(0x04, DISPSTAT); + lcdTicks += 272; + cpuDmaHack2 = CPUCheckDMA(2, 0x0f); + if(DISPSTAT & 16) { + IF |= 2; + UPDATE_REG(0x202, IF); + } + } + } + } + + if(!stopState) { + if(timer0On) { + if(timer0ClockReload == 1) { + u32 tm0d = TM0D + clockTicks; + if(tm0d > 0xffff) { + tm0d += timer0Reload; + timerOverflow |= 1; + soundTimerOverflow(0); + if(TM0CNT & 0x40) { + IF |= 0x08; + UPDATE_REG(0x202, IF); + } + } + TM0D = tm0d; + if(TM0D < timer0Reload) + TM0D = timer0Reload; + timer0Ticks = 0x10000 - TM0D; + UPDATE_REG(0x100, TM0D); + } else { + timer0Ticks -= clockTicks; + if(timer0Ticks <= 0) { + timer0Ticks += timer0ClockReload; + TM0D++; + if(TM0D == 0) { + TM0D = timer0Reload; + timerOverflow |= 1; + soundTimerOverflow(0); + if(TM0CNT & 0x40) { + IF |= 0x08; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x100, TM0D); + } + } + } + + if(timer1On) { + if(TM1CNT & 4) { + if(timerOverflow & 1) { + TM1D++; + if(TM1D == 0) { + TM1D += timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if(TM1CNT & 0x40) { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x104, TM1D); + } + } else { + if(timer1ClockReload == 1) { + u32 tm1d = TM1D + clockTicks; + if(tm1d > 0xffff) { + tm1d += timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if(TM1CNT & 0x40) { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + TM1D = tm1d; + if(TM1D < timer1Reload) + TM1D = timer1Reload; + timer1Ticks = 0x10000 - TM1D; + UPDATE_REG(0x104, TM1D); + } else { + timer1Ticks -= clockTicks; + if(timer1Ticks <= 0) { + timer1Ticks += timer1ClockReload; + TM1D++; + + if(TM1D == 0) { + TM1D = timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if(TM1CNT & 0x40) { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x104, TM1D); + } + } + } + } + + if(timer2On) { + if(TM2CNT & 4) { + if(timerOverflow & 2) { + TM2D++; + if(TM2D == 0) { + TM2D += timer2Reload; + timerOverflow |= 4; + if(TM2CNT & 0x40) { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x108, TM2D); + } + } else { + if(timer2ClockReload == 1) { + u32 tm2d = TM2D + clockTicks; + if(tm2d > 0xffff) { + tm2d += timer2Reload; + timerOverflow |= 4; + if(TM2CNT & 0x40) { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + TM2D = tm2d; + if(TM2D < timer2Reload) + TM2D = timer2Reload; + timer2Ticks = 0x10000 - TM2D; + UPDATE_REG(0x108, TM2D); + } else { + timer2Ticks -= clockTicks; + if(timer2Ticks <= 0) { + timer2Ticks += timer2ClockReload; + TM2D++; + + if(TM2D == 0) { + TM2D = timer2Reload; + timerOverflow |= 4; + if(TM2CNT & 0x40) { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x108, TM2D); + } + } + } + } + + if(timer3On) { + if(TM3CNT & 4) { + if(timerOverflow & 4) { + TM3D++; + if(TM3D == 0) { + TM3D += timer3Reload; + if(TM3CNT & 0x40) { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x10c, TM3D); + } + } else { + if(timer3ClockReload == 1) { + u32 tm3d = TM3D + clockTicks; + if(tm3d > 0xffff) { + tm3d += timer3Reload; + if(TM3CNT & 0x40) { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + TM3D = tm3d; + if(TM3D < timer3Reload) + TM3D = timer3Reload; + timer3Ticks = 0x10000 - TM3D; + UPDATE_REG(0x10C, TM3D); + } else { + timer3Ticks -= clockTicks; + if(timer3Ticks <= 0) { + timer3Ticks += timer3ClockReload; + TM3D++; + + if(TM3D == 0) { + TM3D = timer3Reload; + if(TM3CNT & 0x40) { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x10C, TM3D); + } + } + } + } + } + // we shouldn't be doing sound in stop state, but we lose synchronization + // if sound is disabled, so in stop state, soundTick will just produce + // mute sound + soundTicks -= clockTicks; + if(soundTicks <= 0) { + soundTick(); + soundTicks += SOUND_CLOCK_TICKS; + } + timerOverflow = 0; + +#ifdef PROFILING + profilingTicks -= clockTicks; + if(profilingTicks <= 0) { + profilingTicks += profilingTicksReload; + if(profilBuffer && profilSize) { + u16 *b = (u16 *)profilBuffer; + int pc = ((reg[15].I - profilLowPC) * profilScale)/0x10000; + if(pc >= 0 && pc < profilSize) { + b[pc]++; + } + } + } +#endif + + ticks -= clockTicks; + + +/* Link +----------------------------------*/ + LinkUpdate(clockTicks); +/* ----------------------------- */ + + cpuNextEvent = CPUUpdateTicks(); + + if(cpuDmaTicksToUpdate > 0) { + if(cpuDmaTicksToUpdate < cpuNextEvent) + cpuNextEvent = cpuDmaTicksToUpdate; + cpuDmaTicksToUpdate -= cpuNextEvent; + clockTicks = cpuNextEvent; + if(cpuDmaTicksToUpdate < 0) + cpuDmaTicksToUpdate = 0; + goto updateLoop; + } + + if(cpuDmaHack2) + cpuNextEvent = 1; + + if(IF && (IME & 1) && armIrqEnable) { + int res = IF & IE; + if(stopState) + res &= 0x3080; + if(res) { + if(intState) { + CPUInterrupt(); + intState = false; + if(holdState) { + holdState = false; + stopState = false; + } + } else { + if(!holdState) { + intState = true; + cpuNextEvent = 5; + } else { + CPUInterrupt(); + if(holdState) { + holdState = false; + stopState = false; + } + } + } + } + } + + if(remainingTicks > 0) { + if(remainingTicks < cpuNextEvent) + cpuNextEvent = remainingTicks; + remainingTicks -= cpuNextEvent; + clockTicks = cpuNextEvent; + if(remainingTicks < 0) + remainingTicks = 0; + goto updateLoop; + } + + if(ticks <= 0 || cpuBreakLoop) + break; + } + } +} + +struct EmulatedSystem GBASystem = { + // emuMain + CPULoop, + // emuReset + CPUReset, + // emuCleanUp + CPUCleanUp, + // emuReadBattery + CPUReadBatteryFile, + // emuWriteBattery + CPUWriteBatteryFile, + // emuReadState + CPUReadState, + // emuWriteState + CPUWriteState, + // emuReadMemState + CPUReadMemState, + // emuWriteMemState + CPUWriteMemState, + // emuWritePNG + CPUWritePNGFile, + // emuWriteBMP + CPUWriteBMPFile, + // emuUpdateCPSR + CPUUpdateCPSR, + // emuHasDebugger + true, + // emuCount +#ifdef FINAL_VERSION + 250000 +#else + 5000 +#endif +}; diff --git a/src/GBA.h b/src/GBA.h new file mode 100644 index 00000000..d6b8fa39 --- /dev/null +++ b/src/GBA.h @@ -0,0 +1,147 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_GBA_H +#define VBA_GBA_H + +#include "System.h" + +#define SAVE_GAME_VERSION_1 1 +#define SAVE_GAME_VERSION_2 2 +#define SAVE_GAME_VERSION_3 3 +#define SAVE_GAME_VERSION_4 4 +#define SAVE_GAME_VERSION_5 5 +#define SAVE_GAME_VERSION_6 6 +#define SAVE_GAME_VERSION_7 7 +#define SAVE_GAME_VERSION_8 8 +#define SAVE_GAME_VERSION SAVE_GAME_VERSION_8 + +typedef struct { + u8 *address; + u32 mask; +} memoryMap; + +typedef union { + struct { +#ifdef WORDS_BIGENDIAN + u8 B3; + u8 B2; + u8 B1; + u8 B0; +#else + u8 B0; + u8 B1; + u8 B2; + u8 B3; +#endif + } B; + struct { +#ifdef WORDS_BIGENDIAN + u16 W1; + u16 W0; +#else + u16 W0; + u16 W1; +#endif + } W; +#ifdef WORDS_BIGENDIAN + volatile u32 I; +#else + u32 I; +#endif +} reg_pair; + +#ifndef NO_GBA_MAP +extern memoryMap map[256]; +#endif + +extern reg_pair reg[45]; +extern u8 biosProtected[4]; + +extern bool N_FLAG; +extern bool Z_FLAG; +extern bool C_FLAG; +extern bool V_FLAG; +extern bool armIrqEnable; +extern bool armState; +extern int armMode; +extern void (*cpuSaveGameFunc)(u32,u8); + +extern u8 freezeWorkRAM[0x40000]; +extern u8 freezeInternalRAM[0x8000]; +extern bool CPUReadGSASnapshot(const char *); +extern bool CPUWriteGSASnapshot(const char *, const char *, const char *, const char *); +extern bool CPUWriteBatteryFile(const char *); +extern bool CPUReadBatteryFile(const char *); +extern bool CPUExportEepromFile(const char *); +extern bool CPUImportEepromFile(const char *); +extern bool CPUWritePNGFile(const char *); +extern bool CPUWriteBMPFile(const char *); +extern void CPUCleanUp(); +extern void CPUUpdateRender(); +extern bool CPUReadMemState(char *, int); +extern bool CPUReadState(const char *); +extern bool CPUWriteMemState(char *, int); +extern bool CPUWriteState(const char *); +extern int CPULoadRom(const char *); +extern void CPUUpdateRegister(u32, u16); +extern void CPUWriteHalfWord(u32, u16); +extern void CPUWriteByte(u32, u8); +extern void CPUInit(const char *,bool); +extern void CPUReset(); +extern void CPULoop(int); +extern bool CPUCheckDMA(int,int); +extern bool CPUIsGBAImage(const char *); +extern bool CPUIsZipFile(const char *); +#ifdef PROFILING +extern void cpuProfil(char *buffer, int, u32, int); +extern void cpuEnableProfiling(int hz); +#endif + +extern struct EmulatedSystem GBASystem; + +#define R13_IRQ 18 +#define R14_IRQ 19 +#define SPSR_IRQ 20 +#define R13_USR 26 +#define R14_USR 27 +#define R13_SVC 28 +#define R14_SVC 29 +#define SPSR_SVC 30 +#define R13_ABT 31 +#define R14_ABT 32 +#define SPSR_ABT 33 +#define R13_UND 34 +#define R14_UND 35 +#define SPSR_UND 36 +#define R8_FIQ 37 +#define R9_FIQ 38 +#define R10_FIQ 39 +#define R11_FIQ 40 +#define R12_FIQ 41 +#define R13_FIQ 42 +#define R14_FIQ 43 +#define SPSR_FIQ 44 + +#include "Cheats.h" +#include "Globals.h" +#include "EEprom.h" +#include "Flash.h" + +#endif //VBA_GBA_H diff --git a/src/GBAinline.h b/src/GBAinline.h new file mode 100644 index 00000000..1c482c85 --- /dev/null +++ b/src/GBAinline.h @@ -0,0 +1,427 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_GBAinline_H +#define VBA_GBAinline_H + +#include "System.h" +#include "Port.h" +#include "RTC.h" +#include "Sound.h" + +extern bool cpuSramEnabled; +extern bool cpuFlashEnabled; +extern bool cpuEEPROMEnabled; +extern bool cpuEEPROMSensorEnabled; +extern bool cpuDmaHack; +extern bool cpuDmaHack2; +extern u32 cpuDmaLast; + +extern int lspeed; +extern void LinkSStop(void); + +#define CPUReadByteQuick(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +#define CPUReadHalfWordQuick(addr) \ + READ16LE(((u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define CPUReadMemoryQuick(addr) \ + READ32LE(((u32*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +inline u32 CPUReadMemory(u32 address) +{ + +#ifdef DEV_VERSION + if(address & 3) { + if(systemVerbose & VERBOSE_UNALIGNED_MEMORY) { + log("Unaligned word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } + } +#endif + + u32 value; + switch(address >> 24) { + case 0: + if(reg[15].I >> 24) { + if(address < 0x4000) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + + value = READ32LE(((u32 *)&biosProtected)); + } + else goto unreadable; + } else + value = READ32LE(((u32 *)&bios[address & 0x3FFC])); + break; + case 2: + value = READ32LE(((u32 *)&workRAM[address & 0x3FFFC])); + break; + case 3: + value = READ32LE(((u32 *)&internalRAM[address & 0x7ffC])); + break; + case 4: + if((address>=0x4000120||address<=0x4000126)&&lspeed) + LinkSStop(); + if((address < 0x4000400) && ioReadable[address & 0x3fc]) { + if(ioReadable[(address & 0x3fc) + 2]) + value = soundRead32(address & 0x3fC); + else + value = soundRead16(address & 0x3fc); + } else goto unreadable; + break; + case 5: + value = READ32LE(((u32 *)&paletteRAM[address & 0x3fC])); + break; + case 6: + value = READ32LE(((u32 *)&vram[address & 0x1fffc])); + break; + case 7: + value = READ32LE(((u32 *)&oam[address & 0x3FC])); + break; + case 8: + case 9: + case 10: + case 11: + case 12: + value = READ32LE(((u32 *)&rom[address&0x1FFFFFC])); + break; + case 13: + if(cpuEEPROMEnabled) + // no need to swap this + return eepromRead(address); + goto unreadable; + case 14: + if(cpuFlashEnabled | cpuSramEnabled) + // no need to swap this + return flashRead(address); + // default + default: + unreadable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + + if(cpuDmaHack || cpuDmaHack2) { + value = cpuDmaLast; + } else { + if(armState) { + value = CPUReadMemoryQuick(reg[15].I); + } else { + value = CPUReadHalfWordQuick(reg[15].I) | + CPUReadHalfWordQuick(reg[15].I) << 16; + } + } + } + + if(address & 3) { +#ifdef C_CORE + int shift = (address & 3) << 3; + value = (value >> shift) | (value << (32 - shift)); +#else +#ifdef __GNUC__ + asm("and $3, %%ecx;" + "shl $3 ,%%ecx;" + "ror %%cl, %0" + : "=r" (value) + : "r" (value), "c" (address)); +#else + __asm { + mov ecx, address; + and ecx, 3; + shl ecx, 3; + ror [dword ptr value], cl; + } +#endif +#endif + } + return value; +} + +extern u32 myROM[]; + +inline u32 CPUReadHalfWord(u32 address) +{ +#ifdef DEV_VERSION + if(address & 1) { + if(systemVerbose & VERBOSE_UNALIGNED_MEMORY) { + log("Unaligned halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } + } +#endif + + u32 value; + + switch(address >> 24) { + case 0: + if (reg[15].I >> 24) { + if(address < 0x4000) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + value = READ16LE(((u16 *)&biosProtected[address&2])); + } else goto unreadable; + } else + value = READ16LE(((u16 *)&bios[address & 0x3FFE])); + break; + case 2: + value = READ16LE(((u16 *)&workRAM[address & 0x3FFFE])); + break; + case 3: + value = READ16LE(((u16 *)&internalRAM[address & 0x7ffe])); + break; + case 4: +if((address>=0x4000120||address<=0x4000126)&&lspeed) + LinkSStop(); + if((address < 0x4000400) && ioReadable[address & 0x3fe]) + value = READ16LE(((u16 *)&ioMem[address & 0x3fe])); + else goto unreadable; + break; + case 5: + value = READ16LE(((u16 *)&paletteRAM[address & 0x3fe])); + break; + case 6: + value = READ16LE(((u16 *)&vram[address & 0x1fffe])); + break; + case 7: + value = READ16LE(((u16 *)&oam[address & 0x3fe])); + break; + case 8: + case 9: + case 10: + case 11: + case 12: + if(address == 0x80000c4 || address == 0x80000c6 || address == 0x80000c8) + value = rtcRead(address); + else + value = READ16LE(((u16 *)&rom[address & 0x1FFFFFE])); + break; + case 13: + if(cpuEEPROMEnabled) + // no need to swap this + return eepromRead(address); + goto unreadable; + case 14: + if(cpuFlashEnabled | cpuSramEnabled) + // no need to swap this + return flashRead(address); + // default + default: + unreadable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + if(cpuDmaHack2 || cpuDmaHack) { + value = cpuDmaLast & 0xFFFF; + } else { + if(armState) { + value = CPUReadHalfWordQuick(reg[15].I + (address & 2)); + } else { + value = CPUReadHalfWordQuick(reg[15].I); + } + } + break; + } + + if(address & 1) { + value = (value >> 8) | (value << 24); + } + + return value; +} + +inline u16 CPUReadHalfWordSigned(u32 address) +{ + u16 value = CPUReadHalfWord(address); + if((address & 1)) + value = (s8)value; + return value; +} + +inline u8 CPUReadByte(u32 address) +{ + switch(address >> 24) { + case 0: + if (reg[15].I >> 24) { + if(address < 0x4000) { +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal byte read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + return biosProtected[address & 3]; + } else goto unreadable; + } + return bios[address & 0x3FFF]; + case 2: + return workRAM[address & 0x3FFFF]; + case 3: + return internalRAM[address & 0x7fff]; + case 4: + if((address>=0x4000120||address<=0x4000126)&&lspeed) + LinkSStop(); + if((address < 0x4000400) && ioReadable[address & 0x3ff]) + return soundRead(address & 0x3ff); + else goto unreadable; + case 5: + return paletteRAM[address & 0x3ff]; + case 6: + return vram[address & 0x1ffff]; + case 7: + return oam[address & 0x3ff]; + case 8: + case 9: + case 10: + case 11: + case 12: + return rom[address & 0x1FFFFFF]; + case 13: + if(cpuEEPROMEnabled) + return eepromRead(address); + goto unreadable; + case 14: + if(cpuSramEnabled | cpuFlashEnabled) + return flashRead(address); + if(cpuEEPROMSensorEnabled) { + switch(address & 0x00008f00) { + case 0x8200: + return systemGetSensorX() & 255; + case 0x8300: + return (systemGetSensorX() >> 8)|0x80; + case 0x8400: + return systemGetSensorY() & 255; + case 0x8500: + return systemGetSensorY() >> 8; + } + } + // default + default: + unreadable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_READ) { + log("Illegal byte read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + if(cpuDmaHack || cpuDmaHack2) { + return cpuDmaLast & 0xFF; + } else { + if(armState) { + return CPUReadByteQuick(reg[15].I+(address & 3)); + } else { + return CPUReadByteQuick(reg[15].I+(address & 1)); + } + } + break; + } +} + +inline void CPUWriteMemory(u32 address, u32 value) +{ +#ifdef DEV_VERSION + if(address & 3) { + if(systemVerbose & VERBOSE_UNALIGNED_MEMORY) { + log("Unaliagned word write: %08x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } + } +#endif + + switch(address >> 24) { + case 0x02: +#ifdef SDL + if(*((u32 *)&freezeWorkRAM[address & 0x3FFFC])) + cheatsWriteMemory(address & 0x203FFFC, + value); + else +#endif + WRITE32LE(((u32 *)&workRAM[address & 0x3FFFC]), value); + break; + case 0x03: +#ifdef SDL + if(*((u32 *)&freezeInternalRAM[address & 0x7ffc])) + cheatsWriteMemory(address & 0x3007FFC, + value); + else +#endif + WRITE32LE(((u32 *)&internalRAM[address & 0x7ffC]), value); + break; + case 0x04: + if(address < 0x4000400) { + CPUUpdateRegister((address & 0x3FC), value & 0xFFFF); + CPUUpdateRegister((address & 0x3FC) + 2, (value >> 16)); + } else goto unwritable; + break; + case 0x05: + WRITE32LE(((u32 *)&paletteRAM[address & 0x3FC]), value); + break; + case 0x06: + if(address & 0x10000) + WRITE32LE(((u32 *)&vram[address & 0x17ffc]), value); + else + WRITE32LE(((u32 *)&vram[address & 0x1fffc]), value); + break; + case 0x07: + WRITE32LE(((u32 *)&oam[address & 0x3fc]), value); + break; + case 0x0D: + if(cpuEEPROMEnabled) { + eepromWrite(address, value); + break; + } + goto unwritable; + case 0x0E: + if(!eepromInUse | cpuSramEnabled | cpuFlashEnabled) { + (*cpuSaveGameFunc)(address, (u8)value); + break; + } + // default + default: + unwritable: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_ILLEGAL_WRITE) { + log("Illegal word write: %08x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } +#endif + break; + } +} + +#endif //VBA_GBAinline_H \ No newline at end of file diff --git a/src/Gb_Apu/Blip_Buffer.cpp b/src/Gb_Apu/Blip_Buffer.cpp new file mode 100644 index 00000000..81da8362 --- /dev/null +++ b/src/Gb_Apu/Blip_Buffer.cpp @@ -0,0 +1,406 @@ + +// Blip_Buffer 0.4.0. http://www.slack.net/~ant/ + +#include "Blip_Buffer.h" + +#include +#include +#include +#include +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +int const buffer_extra = blip_widest_impulse_ + 2; + +Blip_Buffer::Blip_Buffer() +{ + factor_ = LONG_MAX; + offset_ = 0; + buffer_ = 0; + buffer_size_ = 0; + sample_rate_ = 0; + reader_accum = 0; + bass_shift = 0; + clock_rate_ = 0; + bass_freq_ = 16; + length_ = 0; + + // assumptions code makes about implementation-defined features + #ifndef NDEBUG + // right shift of negative value preserves sign + buf_t_ i = -0x7FFFFFFE; + assert( (i >> 1) == -0x3FFFFFFF ); + + // casting to short truncates to 16 bits and sign-extends + i = 0x18000; + assert( (short) i == -0x8000 ); + #endif +} + +Blip_Buffer::~Blip_Buffer() +{ + free( buffer_ ); +} + +void Blip_Buffer::clear( int entire_buffer ) +{ + offset_ = 0; + reader_accum = 0; + if ( buffer_ ) + { + long count = (entire_buffer ? buffer_size_ : samples_avail()); + memset( buffer_, 0, (count + buffer_extra) * sizeof (buf_t_) ); + } +} + +Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) +{ + // start with maximum length that resampled time can represent + long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - buffer_extra - 64; + if ( msec != blip_max_length ) + { + long s = (new_rate * (msec + 1) + 999) / 1000; + if ( s < new_size ) + new_size = s; + else + assert( 0 ); // fails if requested buffer length exceeds limit + } + + if ( buffer_size_ != new_size ) + { + void* p = realloc( buffer_, (new_size + buffer_extra) * sizeof *buffer_ ); + if ( !p ) + return "Out of memory"; + buffer_ = (buf_t_*) p; + } + + buffer_size_ = new_size; + + // update things based on the sample rate + sample_rate_ = new_rate; + length_ = new_size * 1000 / new_rate - 1; + if ( msec ) + assert( length_ == msec ); // ensure length is same as that passed in + if ( clock_rate_ ) + clock_rate( clock_rate_ ); + bass_freq( bass_freq_ ); + + clear(); + + return 0; // success +} + +blip_resampled_time_t Blip_Buffer::clock_rate_factor( long clock_rate ) const +{ + double ratio = (double) sample_rate_ / clock_rate; + long factor = (long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 ); + assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large + return (blip_resampled_time_t) factor; +} + +void Blip_Buffer::bass_freq( int freq ) +{ + bass_freq_ = freq; + int shift = 31; + if ( freq > 0 ) + { + shift = 13; + long f = (freq << 16) / sample_rate_; + while ( (f >>= 1) && --shift ) { } + } + bass_shift = shift; +} + +void Blip_Buffer::end_frame( blip_time_t t ) +{ + offset_ += t * factor_; + assert( samples_avail() <= (long) buffer_size_ ); // time outside buffer length +} + +void Blip_Buffer::remove_silence( long count ) +{ + assert( count <= samples_avail() ); // tried to remove more samples than available + offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; +} + +long Blip_Buffer::count_samples( blip_time_t t ) const +{ + unsigned long last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; + unsigned long first_sample = offset_ >> BLIP_BUFFER_ACCURACY; + return (long) (last_sample - first_sample); +} + +blip_time_t Blip_Buffer::count_clocks( long count ) const +{ + if ( count > buffer_size_ ) + count = buffer_size_; + blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; + return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); +} + +void Blip_Buffer::remove_samples( long count ) +{ + if ( count ) + { + remove_silence( count ); + + // copy remaining samples to beginning and clear old samples + long remain = samples_avail() + buffer_extra; + memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); + memset( buffer_ + remain, 0, count * sizeof *buffer_ ); + } +} + +// Blip_Synth_ + +Blip_Synth_::Blip_Synth_( short* p, int w ) : + impulses( p ), + width( w ) +{ + volume_unit_ = 0.0; + kernel_unit = 0; + buf = 0; + last_amp = 0; + delta_factor = 0; +} + +static double const pi = 3.1415926535897932384626433832795029; + +static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff ) +{ + if ( cutoff >= 0.999 ) + cutoff = 0.999; + + if ( treble < -300.0 ) + treble = -300.0; + if ( treble > 5.0 ) + treble = 5.0; + + double const maxh = 4096.0; + double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) ); + double const pow_a_n = pow( rolloff, maxh - maxh * cutoff ); + double const to_angle = pi / 2 / maxh / oversample; + for ( int i = 0; i < count; i++ ) + { + double angle = ((i - count) * 2 + 1) * to_angle; + double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle ); + double cos_nc_angle = cos( maxh * cutoff * angle ); + double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle ); + double cos_angle = cos( angle ); + + c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle; + double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle); + double b = 2.0 - cos_angle - cos_angle; + double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle; + + out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d + } +} + +void blip_eq_t::generate( float* out, int count ) const +{ + // lower cutoff freq for narrow kernels with their wider transition band + // (8 points->1.49, 16 points->1.15) + double oversample = blip_res * 2.25 / count + 0.85; + double half_rate = sample_rate * 0.5; + if ( cutoff_freq ) + oversample = half_rate / cutoff_freq; + double cutoff = rolloff_freq * oversample / half_rate; + + gen_sinc( out, count, blip_res * oversample, treble, cutoff ); + + // apply (half of) hamming window + double to_fraction = pi / (count - 1); + for ( int i = count; i--; ) + out [i] *= 0.54 - 0.46 * cos( i * to_fraction ); +} + +void Blip_Synth_::adjust_impulse() +{ + // sum pairs for each phase and add error correction to end of first half + int const size = impulses_size(); + for ( int p = blip_res; p-- >= blip_res / 2; ) + { + int p2 = blip_res - 2 - p; + long error = kernel_unit; + for ( int i = 1; i < size; i += blip_res ) + { + error -= impulses [i + p ]; + error -= impulses [i + p2]; + } + if ( p == p2 ) + error /= 2; // phase = 0.5 impulse uses same half for both sides + impulses [size - blip_res + p] += error; + //printf( "error: %ld\n", error ); + } + + //for ( int i = blip_res; i--; printf( "\n" ) ) + // for ( int j = 0; j < width / 2; j++ ) + // printf( "%5ld,", impulses [j * blip_res + i + 1] ); +} + +void Blip_Synth_::treble_eq( blip_eq_t const& eq ) +{ + float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; + + int const half_size = blip_res / 2 * (width - 1); + eq.generate( &fimpulse [blip_res], half_size ); + + int i; + + // need mirror slightly past center for calculation + for ( i = blip_res; i--; ) + fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i]; + + // starts at 0 + for ( i = 0; i < blip_res; i++ ) + fimpulse [i] = 0.0f; + + // find rescale factor + double total = 0.0; + for ( i = 0; i < half_size; i++ ) + total += fimpulse [blip_res + i]; + + //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB + //double const base_unit = 37888.0; // allows treble to +5 dB + double const base_unit = 32768.0; // necessary for blip_unscaled to work + double rescale = base_unit / 2 / total; + kernel_unit = (long) base_unit; + + // integrate, first difference, rescale, convert to int + double sum = 0.0; + double next = 0.0; + int const impulses_size = this->impulses_size(); + for ( i = 0; i < impulses_size; i++ ) + { + impulses [i] = (short) floor( (next - sum) * rescale + 0.5 ); + sum += fimpulse [i]; + next += fimpulse [i + blip_res]; + } + adjust_impulse(); + + // volume might require rescaling + double vol = volume_unit_; + if ( vol ) + { + volume_unit_ = 0.0; + volume_unit( vol ); + } +} + +void Blip_Synth_::volume_unit( double new_unit ) +{ + if ( new_unit != volume_unit_ ) + { + // use default eq if it hasn't been set yet + if ( !kernel_unit ) + treble_eq( -8.0 ); + + volume_unit_ = new_unit; + double factor = new_unit * (1L << blip_sample_bits) / kernel_unit; + + if ( factor > 0.0 ) + { + int shift = 0; + + // if unit is really small, might need to attenuate kernel + while ( factor < 2.0 ) + { + shift++; + factor *= 2.0; + } + + if ( shift ) + { + kernel_unit >>= shift; + assert( kernel_unit > 0 ); // fails if volume unit is too low + + // keep values positive to avoid round-towards-zero of sign-preserving + // right shift for negative values + long offset = 0x8000 + (1 << (shift - 1)); + long offset2 = 0x8000 >> shift; + for ( int i = impulses_size(); i--; ) + impulses [i] = (short) (((impulses [i] + offset) >> shift) - offset2); + adjust_impulse(); + } + } + delta_factor = (int) floor( factor + 0.5 ); + //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); + } +} + +long Blip_Buffer::read_samples( blip_sample_t* out, long max_samples, int stereo ) +{ + long count = samples_avail(); + if ( count > max_samples ) + count = max_samples; + + if ( count ) + { + int const sample_shift = blip_sample_bits - 16; + int const bass_shift = this->bass_shift; + long accum = reader_accum; + buf_t_* in = buffer_; + + if ( !stereo ) + { + for ( long n = count; n--; ) + { + long s = accum >> sample_shift; + accum -= accum >> bass_shift; + accum += *in++; + *out++ = (blip_sample_t) s; + + // clamp sample + if ( (blip_sample_t) s != s ) + out [-1] = (blip_sample_t) (0x7FFF - (s >> 24)); + } + } + else + { + for ( long n = count; n--; ) + { + long s = accum >> sample_shift; + accum -= accum >> bass_shift; + accum += *in++; + *out = (blip_sample_t) s; + out += 2; + + // clamp sample + if ( (blip_sample_t) s != s ) + out [-2] = (blip_sample_t) (0x7FFF - (s >> 24)); + } + } + + reader_accum = accum; + remove_samples( count ); + } + return count; +} + +void Blip_Buffer::mix_samples( blip_sample_t const* in, long count ) +{ + buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2; + + int const sample_shift = blip_sample_bits - 16; + int prev = 0; + while ( count-- ) + { + long s = (long) *in++ << sample_shift; + *out += s - prev; + prev = s; + ++out; + } + *out -= prev; +} + diff --git a/src/Gb_Apu/Blip_Buffer.h b/src/Gb_Apu/Blip_Buffer.h new file mode 100644 index 00000000..49a156a3 --- /dev/null +++ b/src/Gb_Apu/Blip_Buffer.h @@ -0,0 +1,354 @@ + +// Band-limited sound synthesis and buffering + +// Blip_Buffer 0.4.0 + +#ifndef BLIP_BUFFER_H +#define BLIP_BUFFER_H + +// Time unit at source clock rate +typedef long blip_time_t; + +// Output samples are 16-bit signed, with a range of -32768 to 32767 +typedef short blip_sample_t; +enum { blip_sample_max = 32767 }; + +class Blip_Buffer { +public: + typedef const char* blargg_err_t; + + // Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults + // to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there + // isn't enough memory, returns error without affecting current buffer setup. + blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 ); + + // Set number of source time units per second + void clock_rate( long ); + + // End current time frame of specified duration and make its samples available + // (along with any still-unread samples) for reading with read_samples(). Begins + // a new time frame at the end of the current frame. + void end_frame( blip_time_t time ); + + // Read at most 'max_samples' out of buffer into 'dest', removing them from from + // the buffer. Returns number of samples actually read and removed. If stereo is + // true, increments 'dest' one extra time after writing each sample, to allow + // easy interleving of two channels into a stereo output buffer. + long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 ); + +// Additional optional features + + // Current output sample rate + long sample_rate() const; + + // Length of buffer, in milliseconds + int length() const; + + // Number of source time units per second + long clock_rate() const; + + // Set frequency high-pass filter frequency, where higher values reduce bass more + void bass_freq( int frequency ); + + // Number of samples delay from synthesis to samples read out + int output_latency() const; + + // Remove all available samples and clear buffer to silence. If 'entire_buffer' is + // false, just clears out any samples waiting rather than the entire buffer. + void clear( int entire_buffer = 1 ); + + // Number of samples available for reading with read_samples() + long samples_avail() const; + + // Remove 'count' samples from those waiting to be read + void remove_samples( long count ); + +// Experimental features + + // Number of raw samples that can be mixed within frame of specified duration. + long count_samples( blip_time_t duration ) const; + + // Mix 'count' samples from 'buf' into buffer. + void mix_samples( blip_sample_t const* buf, long count ); + + // Count number of clocks needed until 'count' samples will be available. + // If buffer can't even hold 'count' samples, returns number of clocks until + // buffer becomes full. + blip_time_t count_clocks( long count ) const; + + // not documented yet + typedef unsigned long blip_resampled_time_t; + void remove_silence( long count ); + blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; } + blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; } + blip_resampled_time_t clock_rate_factor( long clock_rate ) const; +public: + Blip_Buffer(); + ~Blip_Buffer(); + + // Deprecated + typedef blip_resampled_time_t resampled_time_t; + blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); } + blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); } +private: + // noncopyable + Blip_Buffer( const Blip_Buffer& ); + Blip_Buffer& operator = ( const Blip_Buffer& ); +public: + typedef long buf_t_; + unsigned long factor_; + blip_resampled_time_t offset_; + buf_t_* buffer_; + long buffer_size_; +private: + long reader_accum; + int bass_shift; + long sample_rate_; + long clock_rate_; + int bass_freq_; + int length_; + friend class Blip_Reader; +}; + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +// Number of bits in resample ratio fraction. Higher values give a more accurate ratio +// but reduce maximum buffer size. +#ifndef BLIP_BUFFER_ACCURACY + #define BLIP_BUFFER_ACCURACY 16 +#endif + +// Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in +// noticeable broadband noise when synthesizing high frequency square waves. +// Affects size of Blip_Synth objects since they store the waveform directly. +#ifndef BLIP_PHASE_BITS + #define BLIP_PHASE_BITS 6 +#endif + + // Internal + typedef unsigned long blip_resampled_time_t; + int const blip_widest_impulse_ = 16; + int const blip_res = 1 << BLIP_PHASE_BITS; + class blip_eq_t; + + class Blip_Synth_ { + double volume_unit_; + short* const impulses; + int const width; + long kernel_unit; + int impulses_size() const { return blip_res / 2 * width + 1; } + void adjust_impulse(); + public: + Blip_Buffer* buf; + int last_amp; + int delta_factor; + + Blip_Synth_( short* impulses, int width ); + void treble_eq( blip_eq_t const& ); + void volume_unit( double ); + }; + +// Quality level. Start with blip_good_quality. +const int blip_med_quality = 8; +const int blip_good_quality = 12; +const int blip_high_quality = 16; + +// Range specifies the greatest expected change in amplitude. Calculate it +// by finding the difference between the maximum and minimum expected +// amplitudes (max - min). +template +class Blip_Synth { +public: + // Set overall volume of waveform + void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); } + + // Configure low-pass filter (see notes.txt) + void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); } + + // Get/set Blip_Buffer used for output + Blip_Buffer* output() const { return impl.buf; } + void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; } + + // Update amplitude of waveform at given time. Using this requires a separate + // Blip_Synth for each waveform. + void update( blip_time_t time, int amplitude ); + +// Low-level interface + + // Add an amplitude transition of specified delta, optionally into specified buffer + // rather than the one set with output(). Delta can be positive or negative. + // The actual change in amplitude is delta * (volume / range) + void offset( blip_time_t, int delta, Blip_Buffer* ) const; + void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); } + + // Works directly in terms of fractional output samples. Contact author for more. + void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; + + // Same as offset(), except code is inlined for higher performance + void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const { + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); + } + void offset_inline( blip_time_t t, int delta ) const { + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); + } + +public: + Blip_Synth() : impl( impulses, quality ) { } +private: + typedef short imp_t; + imp_t impulses [blip_res * (quality / 2) + 1]; + Blip_Synth_ impl; +}; + +// Low-pass equalization parameters +class blip_eq_t { +public: + // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce + // treble, small positive values (0 to 5.0) increase treble. + blip_eq_t( double treble_db = 0 ); + + // See notes.txt + blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 ); + +private: + double treble; + long rolloff_freq; + long sample_rate; + long cutoff_freq; + void generate( float* out, int count ) const; + friend class Blip_Synth_; +}; + +int const blip_sample_bits = 30; + +// Optimized inline sample reader for custom sample formats and mixing of Blip_Buffer samples +class Blip_Reader { +public: + // Begin reading samples from buffer. Returns value to pass to next() (can + // be ignored if default bass_freq is acceptable). + int begin( Blip_Buffer& ); + + // Current sample + long read() const { return accum >> (blip_sample_bits - 16); } + + // Current raw sample in full internal resolution + long read_raw() const { return accum; } + + // Advance to next sample + void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); } + + // End reading samples from buffer. The number of samples read must now be removed + // using Blip_Buffer::remove_samples(). + void end( Blip_Buffer& b ) { b.reader_accum = accum; } + +private: + const Blip_Buffer::buf_t_* buf; + long accum; +}; + + +// End of public interface + + +#include + +// Compatibility with older version +const long blip_unscaled = 65535; +const int blip_low_quality = blip_med_quality; +const int blip_best_quality = blip_high_quality; + +#define BLIP_FWD( i ) { \ + long t0 = i0 * delta + buf [fwd + i]; \ + long t1 = imp [blip_res * (i + 1)] * delta + buf [fwd + 1 + i]; \ + i0 = imp [blip_res * (i + 2)]; \ + buf [fwd + i] = t0; \ + buf [fwd + 1 + i] = t1; } + +#define BLIP_REV( r ) { \ + long t0 = i0 * delta + buf [rev - r]; \ + long t1 = imp [blip_res * r] * delta + buf [rev + 1 - r]; \ + i0 = imp [blip_res * (r - 1)]; \ + buf [rev - r] = t0; \ + buf [rev + 1 - r] = t1; } + +template +inline void Blip_Synth::offset_resampled( blip_resampled_time_t time, + int delta, Blip_Buffer* blip_buf ) const +{ + // Fails if time is beyond end of Blip_Buffer, due to a bug in caller code or the + // need for a longer buffer as set by set_sample_rate(). + assert( (long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ ); + delta *= impl.delta_factor; + int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1)); + imp_t const* imp = impulses + blip_res - phase; + long* buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY); + long i0 = *imp; + + int const fwd = (blip_widest_impulse_ - quality) / 2; + int const rev = fwd + quality - 2; + + BLIP_FWD( 0 ) + if ( quality > 8 ) BLIP_FWD( 2 ) + if ( quality > 12 ) BLIP_FWD( 4 ) + { + int const mid = quality / 2 - 1; + long t0 = i0 * delta + buf [fwd + mid - 1]; + long t1 = imp [blip_res * mid] * delta + buf [fwd + mid]; + imp = impulses + phase; + i0 = imp [blip_res * mid]; + buf [fwd + mid - 1] = t0; + buf [fwd + mid] = t1; + } + if ( quality > 12 ) BLIP_REV( 6 ) + if ( quality > 8 ) BLIP_REV( 4 ) + BLIP_REV( 2 ) + + long t0 = i0 * delta + buf [rev]; + long t1 = *imp * delta + buf [rev + 1]; + buf [rev] = t0; + buf [rev + 1] = t1; +} + +#undef BLIP_FWD +#undef BLIP_REV + +template +void Blip_Synth::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const +{ + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); +} + +template +void Blip_Synth::update( blip_time_t t, int amp ) +{ + int delta = amp - impl.last_amp; + impl.last_amp = amp; + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); +} + +inline blip_eq_t::blip_eq_t( double t ) : + treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { } +inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) : + treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { } + +inline int Blip_Buffer::length() const { return length_; } +inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); } +inline long Blip_Buffer::sample_rate() const { return sample_rate_; } +inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; } +inline long Blip_Buffer::clock_rate() const { return clock_rate_; } +inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); } + +inline int Blip_Reader::begin( Blip_Buffer& blip_buf ) +{ + buf = blip_buf.buffer_; + accum = blip_buf.reader_accum; + return blip_buf.bass_shift; +} + +int const blip_max_length = 0; +int const blip_default_length = 250; + +#endif + diff --git a/src/Gb_Apu/Blip_Synth.h b/src/Gb_Apu/Blip_Synth.h new file mode 100644 index 00000000..9dadb972 --- /dev/null +++ b/src/Gb_Apu/Blip_Synth.h @@ -0,0 +1,204 @@ + +// Blip_Synth and Blip_Wave are waveform transition synthesizers for adding +// waveforms to a Blip_Buffer. + +// Blip_Buffer 0.3.3. Copyright (C) 2003-2005 Shay Green. GNU LGPL license. + +#ifndef BLIP_SYNTH_H +#define BLIP_SYNTH_H + +#ifndef BLIP_BUFFER_H + #include "Blip_Buffer.h" +#endif + +// Quality level. Higher levels are slower, and worse in a few cases. +// Use blip_good_quality as a starting point. +const int blip_low_quality = 1; +const int blip_med_quality = 2; +const int blip_good_quality = 3; +const int blip_high_quality = 4; + +// Blip_Synth is a transition waveform synthesizer which adds band-limited +// offsets (transitions) into a Blip_Buffer. For a simpler interface, use +// Blip_Wave (below). +// +// Range specifies the greatest expected offset that will occur. For a +// waveform that goes between +amp and -amp, range should be amp * 2 (half +// that if it only goes between +amp and 0). When range is large, a higher +// accuracy scheme is used; to force this even when range is small, pass +// the negative of range (i.e. -range). +template +class Blip_Synth { + BOOST_STATIC_ASSERT( 1 <= quality && quality <= 5 ); + BOOST_STATIC_ASSERT( -32768 <= range && range <= 32767 ); + enum { + abs_range = (range < 0) ? -range : range, + fine_mode = (range > 512 || range < 0), + width = (quality < 5 ? quality * 4 : Blip_Buffer::widest_impulse_), + res = 1 << blip_res_bits_, + impulse_size = width / 2 * (fine_mode + 1), + base_impulses_size = width / 2 * (res / 2 + 1), + fine_bits = (fine_mode ? (abs_range <= 64 ? 2 : abs_range <= 128 ? 3 : + abs_range <= 256 ? 4 : abs_range <= 512 ? 5 : abs_range <= 1024 ? 6 : + abs_range <= 2048 ? 7 : 8) : 0) + }; + blip_pair_t_ impulses [impulse_size * res * 2 + base_impulses_size]; + Blip_Impulse_ impulse; +public: + Blip_Synth() { impulse.init( impulses, width, res, fine_bits ); } + + // Configure low-pass filter (see notes.txt). Not optimized for real-time control + void treble_eq( const blip_eq_t& eq ) { impulse.treble_eq( eq ); } + + // Set volume of a transition at amplitude 'range' by setting volume_unit + // to v / range + void volume( double v ) { impulse.volume_unit( v * (1.0 / abs_range) ); } + + // Set base volume unit of transitions, where 1.0 is a full swing between the + // positive and negative extremes. Not optimized for real-time control. + void volume_unit( double unit ) { impulse.volume_unit( unit ); } + + // Default Blip_Buffer used for output when none is specified for a given call + Blip_Buffer* output() const { return impulse.buf; } + void output( Blip_Buffer* b ) { impulse.buf = b; } + + // Add an amplitude offset (transition) with an amplitude of delta * volume_unit + // into the specified buffer (default buffer if none specified) at the + // specified source time. Amplitude can be positive or negative. To increase + // performance by inlining code at the call site, use offset_inline(). + void offset( blip_time_t, int delta, Blip_Buffer* ) const; + + void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; + void offset_resampled( blip_resampled_time_t t, int o ) const { + offset_resampled( t, o, impulse.buf ); + } + void offset( blip_time_t t, int delta ) const { + offset( t, delta, impulse.buf ); + } + void offset_inline( blip_time_t time, int delta, Blip_Buffer* buf ) const { + offset_resampled( time * buf->factor_ + buf->offset_, delta, buf ); + } + void offset_inline( blip_time_t time, int delta ) const { + offset_inline( time, delta, impulse.buf ); + } +}; + +// Blip_Wave is a synthesizer for adding a *single* waveform to a Blip_Buffer. +// A wave is built from a series of delays and new amplitudes. This provides a +// simpler interface than Blip_Synth. +template +class Blip_Wave { + Blip_Synth synth; + blip_time_t time_; + int last_amp; +public: + // Start wave at time 0 and amplitude 0 + Blip_Wave() : time_( 0 ), last_amp( 0 ) { } + + // See Blip_Synth for description + void volume( double v ) { synth.volume( v ); } + void volume_unit( double v ) { synth.volume_unit( v ); } + void treble_eq( const blip_eq_t& eq){ synth.treble_eq( eq ); } + Blip_Buffer* output() const { return synth.output(); } + void output( Blip_Buffer* b ) { synth.output( b ); if ( !b ) time_ = last_amp = 0; } + + // Current time in frame + blip_time_t time() const { return time_; } + void time( blip_time_t t ) { time_ = t; } + + // Current amplitude of wave + int amplitude() const { return last_amp; } + void amplitude( int ); + + // Move forward by 't' time units + void delay( blip_time_t t ) { time_ += t; } + + // End time frame of specified duration. Localize time to new frame. + void end_frame( blip_time_t duration ) { + assert(( "Blip_Wave::end_frame(): Wave hadn't yet been run for entire frame", + duration <= time_ )); + time_ -= duration; + } +}; + + + +// End of public interface + + template + void Blip_Wave::amplitude( int amp ) { + int delta = amp - last_amp; + last_amp = amp; + synth.offset_inline( time_, delta ); + } + + template + inline void Blip_Synth::offset_resampled( blip_resampled_time_t time, + int delta, Blip_Buffer* blip_buf ) const + { + typedef blip_pair_t_ pair_t; + + unsigned sample_index = (time >> BLIP_BUFFER_ACCURACY) & ~1; + assert(( "Blip_Synth/Blip_wave: Went past end of buffer", + sample_index < blip_buf->buffer_size_ )); + enum { const_offset = Blip_Buffer::widest_impulse_ / 2 - width / 2 }; + pair_t* buf = (pair_t*) &blip_buf->buffer_ [const_offset + sample_index]; + + enum { shift = BLIP_BUFFER_ACCURACY - blip_res_bits_ }; + enum { mask = res * 2 - 1 }; + const pair_t* imp = &impulses [((time >> shift) & mask) * impulse_size]; + + pair_t offset = impulse.offset * delta; + + if ( !fine_bits ) + { + // normal mode + for ( int n = width / 4; n; --n ) + { + pair_t t0 = buf [0] - offset; + pair_t t1 = buf [1] - offset; + + t0 += imp [0] * delta; + t1 += imp [1] * delta; + imp += 2; + + buf [0] = t0; + buf [1] = t1; + buf += 2; + } + } + else + { + // fine mode + enum { sub_range = 1 << fine_bits }; + delta += sub_range / 2; + int delta2 = (delta & (sub_range - 1)) - sub_range / 2; + delta >>= fine_bits; + + for ( int n = width / 4; n; --n ) + { + pair_t t0 = buf [0] - offset; + pair_t t1 = buf [1] - offset; + + t0 += imp [0] * delta2; + t0 += imp [1] * delta; + + t1 += imp [2] * delta2; + t1 += imp [3] * delta; + + imp += 4; + + buf [0] = t0; + buf [1] = t1; + buf += 2; + } + } + } + + template + void Blip_Synth::offset( blip_time_t time, int delta, Blip_Buffer* buf ) const { + offset_resampled( time * buf->factor_ + buf->offset_, delta, buf ); + } + +#endif + diff --git a/src/Gb_Apu/COPYING b/src/Gb_Apu/COPYING new file mode 100644 index 00000000..54905a60 --- /dev/null +++ b/src/Gb_Apu/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/src/Gb_Apu/Gb_Apu.cpp b/src/Gb_Apu/Gb_Apu.cpp new file mode 100644 index 00000000..0dfd32ce --- /dev/null +++ b/src/Gb_Apu/Gb_Apu.cpp @@ -0,0 +1,318 @@ + +// Gb_Snd_Emu 0.1.4. http://www.slack.net/~ant/ + +#include "Gb_Apu.h" + +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +int const vol_reg = 0xFF24; +int const status_reg = 0xFF26; + +Gb_Apu::Gb_Apu() +{ + square1.synth = &square_synth; + square2.synth = &square_synth; + wave.synth = &other_synth; + noise.synth = &other_synth; + + oscs [0] = &square1; + oscs [1] = &square2; + oscs [2] = &wave; + oscs [3] = &noise; + + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + osc.regs = ®s [i * 5]; + osc.output = NULL; + osc.outputs [0] = NULL; + osc.outputs [1] = NULL; + osc.outputs [2] = NULL; + osc.outputs [3] = NULL; + } + + volume( 1.0 ); + reset(); +} + +Gb_Apu::~Gb_Apu() +{ +} + +void Gb_Apu::treble_eq( const blip_eq_t& eq ) +{ + square_synth.treble_eq( eq ); + other_synth.treble_eq( eq ); +} + +void Gb_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + require( (unsigned) index < osc_count ); + require( (center && left && right) || (!center && !left && !right) ); + Gb_Osc& osc = *oscs [index]; + osc.outputs [1] = right; + osc.outputs [2] = left; + osc.outputs [3] = center; + osc.output = osc.outputs [osc.output_select]; +} + +void Gb_Apu::output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) +{ + for ( int i = 0; i < osc_count; i++ ) + osc_output( i, center, left, right ); +} + +void Gb_Apu::update_volume() +{ + // to do: doesn't handle differing left/right global volume + int data = regs [vol_reg - start_addr]; + double vol = (max( data & 7, data >> 4 & 7 ) + 1) * volume_unit; + square_synth.volume( vol ); + other_synth.volume( vol ); +} + +static unsigned char const powerup_regs [0x30] = { + 0x80,0x3F,0x00,0xFF,0xBF, // square 1 + 0xFF,0x3F,0x00,0xFF,0xBF, // square 2 + 0x7F,0xFF,0x9F,0xFF,0xBF, // wave + 0xFF,0xFF,0x00,0x00,0xBF, // noise + 0x00, // left/right enables + 0x77, // master volume + 0x80, // power + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C, // wave table + 0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA +}; + +void Gb_Apu::reset(bool igba) +{ + next_frame_time = 0; + last_time = 0; + frame_count = 0; + stereo_found = false; + + square1.reset(); + square2.reset(); + wave.reset(gba = igba); + noise.reset(); + noise.bits = 1; + wave.wave_pos = 0; + + // avoid click at beginning + regs [vol_reg - start_addr] = 0x77; + update_volume(); + + regs [status_reg - start_addr] = 0x01; // force power + write_register( 0, status_reg, 0x00 ); +} + +// to do: remove +//static unsigned long abs_time; + +void Gb_Apu::run_until( gb_time_t end_time ) +{ + require( end_time >= last_time ); // end_time must not be before previous time + if ( end_time == last_time ) + return; + + while ( true ) + { + gb_time_t time = next_frame_time; + if ( time > end_time ) + time = end_time; + + // run oscillators + for ( int i = 0; i < osc_count; ++i ) + { + Gb_Osc& osc = *oscs [i]; + if ( osc.output ) + { + int playing = false; + if ( osc.enabled && osc.volume && + (!(osc.regs [4] & osc.len_enabled_mask) || osc.length) ) + playing = -1; + if ( osc.output != osc.outputs [3] ) + stereo_found = true; + switch ( i ) + { + case 0: square1.run( last_time, time, playing ); break; + case 1: square2.run( last_time, time, playing ); break; + case 2: wave .run( last_time, time, playing ); break; + case 3: noise .run( last_time, time, playing ); break; + } + } + } + last_time = time; + + if ( time == end_time ) + break; + + next_frame_time += 4194304 / 256; // 256 Hz + + // 256 Hz actions + square1.clock_length(); + square2.clock_length(); + wave.clock_length(); + noise.clock_length(); + + frame_count = (frame_count + 1) & 3; + if ( frame_count == 0 ) + { + // 64 Hz actions + square1.clock_envelope(); + square2.clock_envelope(); + noise.clock_envelope(); + } + + if ( frame_count & 1 ) + square1.clock_sweep(); // 128 Hz action + } +} + +bool Gb_Apu::end_frame( gb_time_t end_time ) +{ + if ( end_time > last_time ) + run_until( end_time ); + + //abs_time += end_time; + + assert( next_frame_time >= end_time ); + next_frame_time -= end_time; + + assert( last_time >= end_time ); + last_time -= end_time; + + bool result = stereo_found; + stereo_found = false; + return result; +} + +void Gb_Apu::write_register( gb_time_t time, gb_addr_t addr, int data ) +{ + require( (unsigned) data < 0x100 ); + + int reg = addr - start_addr; + if ( (unsigned) reg >= register_count ) + return; + + run_until( time ); + + int old_reg = regs [reg]; + regs [reg] = data; + + if ( addr < vol_reg ) + { + write_osc( reg / 5, reg, data ); + } + else if ( addr == vol_reg && data != old_reg ) // global volume + { + // return all oscs to 0 + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + int amp = osc.last_amp; + osc.last_amp = 0; + if ( amp && osc.enabled && osc.output ) + other_synth.offset( time, -amp, osc.output ); + } + + if ( wave.outputs [3] ) + other_synth.offset( time, 30, wave.outputs [3] ); + + update_volume(); + + if ( wave.outputs [3] ) + other_synth.offset( time, -30, wave.outputs [3] ); + + // oscs will update with new amplitude when next run + } + else if ( addr == 0xFF25 || addr == status_reg ) + { + int mask = (regs [status_reg - start_addr] & 0x80) ? ~0 : 0; + int flags = regs [0xFF25 - start_addr] & mask; + + // left/right assignments + for ( int i = 0; i < osc_count; i++ ) + { + Gb_Osc& osc = *oscs [i]; + osc.enabled &= mask; + int bits = flags >> i; + Blip_Buffer* old_output = osc.output; + osc.output_select = (bits >> 3 & 2) | (bits & 1); + osc.output = osc.outputs [osc.output_select]; + if ( osc.output != old_output ) + { + int amp = osc.last_amp; + osc.last_amp = 0; + if ( amp && old_output ) + other_synth.offset( time, -amp, old_output ); + } + } + + if ( addr == status_reg && data != old_reg ) + { + if ( !(data & 0x80) ) + { + for ( int i = 0; i < (int) sizeof powerup_regs; i++ ) + { + if ( i != status_reg - start_addr ) + write_register( time, i + start_addr, powerup_regs [i] ); + } + } + else + { + //dprintf( "APU powered on\n" ); + } + } + } + else if ( addr >= 0xFF30 ) + { + int bank; + if (gba) bank = (wave.wave_bank ^ 0x20); + else bank = 0; + int index = (addr & 0x0F) * 2 + bank; + wave.wave [index] = data >> 4; + wave.wave [index + 1] = data & 0x0F; + } +} + +int Gb_Apu::read_register( gb_time_t time, gb_addr_t addr ) +{ + run_until( time ); + + int index = addr - start_addr; + require( (unsigned) index < register_count ); + int data = regs [index]; + + if ( addr == status_reg ) + { + data = (data & 0x80) | 0x70; + for ( int i = 0; i < osc_count; i++ ) + { + const Gb_Osc& osc = *oscs [i]; + if ( osc.enabled && (osc.length || !(osc.regs [4] & osc.len_enabled_mask)) ) + data |= 1 << i; + } + } else if ( gba && addr >= 0xff30 ) { + int bank = (wave.wave_bank ^ 0x20); + int index = (addr & 0x0f) * 2; + data = wave.wave [bank + index] << 4; + data |= wave.wave [bank + index + 1]; + } + + return data; +} + diff --git a/src/Gb_Apu/Gb_Apu.h b/src/Gb_Apu/Gb_Apu.h new file mode 100644 index 00000000..6ae61580 --- /dev/null +++ b/src/Gb_Apu/Gb_Apu.h @@ -0,0 +1,99 @@ + +// Nintendo Game Boy PAPU sound chip emulator + +// Gb_Snd_Emu 0.1.4 + +#ifndef GB_APU_H +#define GB_APU_H + +typedef long gb_time_t; // clock cycle count +typedef unsigned gb_addr_t; // 16-bit address + +#include "Gb_Oscs.h" + +class Gb_Apu { +public: + + // Set overall volume of all oscillators, where 1.0 is full volume + void volume( double ); + + // Set treble equalization + void treble_eq( const blip_eq_t& ); + + // Outputs can be assigned to a single buffer for mono output, or to three + // buffers for stereo output (using Stereo_Buffer to do the mixing). + + // Assign all oscillator outputs to specified buffer(s). If buffer + // is NULL, silences all oscillators. + void output( Blip_Buffer* mono ); + void output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Assign single oscillator output to buffer(s). Valid indicies are 0 to 3, + // which refer to Square 1, Square 2, Wave, and Noise. If buffer is NULL, + // silences oscillator. + enum { osc_count = 4 }; + void osc_output( int index, Blip_Buffer* mono ); + void osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ); + + // Reset oscillators and internal state + void reset(bool gba = false); + + // Reads and writes at addr must satisfy start_addr <= addr <= end_addr + enum { start_addr = 0xFF10 }; + enum { end_addr = 0xFF3f }; + enum { register_count = end_addr - start_addr + 1 }; + + // Write 'data' to address at specified time + void write_register( gb_time_t, gb_addr_t, int data ); + + // Read from address at specified time + int read_register( gb_time_t, gb_addr_t ); + + // Run all oscillators up to specified time, end current time frame, then + // start a new frame at time 0. Returns true if any oscillators added + // sound to one of the left/right buffers, false if they only added + // to the center buffer. + bool end_frame( gb_time_t ); + +public: + Gb_Apu(); + ~Gb_Apu(); +private: + // noncopyable + Gb_Apu( const Gb_Apu& ); + Gb_Apu& operator = ( const Gb_Apu& ); + + Gb_Osc* oscs [osc_count]; + gb_time_t next_frame_time; + gb_time_t last_time; + double volume_unit; + int frame_count; + bool stereo_found; + + Gb_Square square1; + Gb_Square square2; + Gb_Wave wave; + Gb_Noise noise; + BOOST::uint8_t regs [register_count]; + Gb_Square::Synth square_synth; // used by squares + Gb_Wave::Synth other_synth; // used by wave and noise + + bool gba; // enable GBA extensions to wave channel + + void update_volume(); + void run_until( gb_time_t ); + void write_osc( int index, int reg, int data ); +}; + +inline void Gb_Apu::output( Blip_Buffer* b ) { output( b, b, b ); } + +inline void Gb_Apu::osc_output( int i, Blip_Buffer* b ) { osc_output( i, b, b, b ); } + +inline void Gb_Apu::volume( double vol ) +{ + volume_unit = 0.60 / osc_count / 15 /*steps*/ / 2 /*?*/ / 8 /*master vol range*/ * vol; + update_volume(); +} + +#endif + diff --git a/src/Gb_Apu/Gb_Oscs.cpp b/src/Gb_Apu/Gb_Oscs.cpp new file mode 100644 index 00000000..674c039a --- /dev/null +++ b/src/Gb_Apu/Gb_Oscs.cpp @@ -0,0 +1,360 @@ + +// Gb_Snd_Emu 0.1.4. http://www.slack.net/~ant/ + +#include "Gb_Apu.h" + +#include + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +// Gb_Osc + +void Gb_Osc::reset() +{ + delay = 0; + last_amp = 0; + length = 0; + output_select = 3; + output = outputs [output_select]; +} + +void Gb_Osc::clock_length() +{ + if ( (regs [4] & len_enabled_mask) && length ) + length--; +} + +// Gb_Env + +void Gb_Env::clock_envelope() +{ + if ( env_delay && !--env_delay ) + { + env_delay = regs [2] & 7; + int v = volume - 1 + (regs [2] >> 2 & 2); + if ( (unsigned) v < 15 ) + volume = v; + } +} + +bool Gb_Env::write_register( int reg, int data ) +{ + switch ( reg ) + { + case 1: + length = 64 - (regs [1] & 0x3f); + break; + + case 2: + if ( !(data >> 4) ) + enabled = false; + break; + + case 4: + if ( data & trigger ) + { + env_delay = regs [2] & 7; + volume = regs [2] >> 4; + enabled = true; + if ( length == 0 ) + length = 64; + return true; + } + } + return false; +} + +// Gb_Square + +void Gb_Square::reset() +{ + phase = 0; + sweep_freq = 0; + sweep_delay = 0; + Gb_Env::reset(); +} + +void Gb_Square::clock_sweep() +{ + int sweep_period = (regs [0] & period_mask) >> 4; + if ( sweep_period && sweep_delay && !--sweep_delay ) + { + sweep_delay = sweep_period; + regs [3] = sweep_freq & 0xFF; + regs [4] = (regs [4] & ~0x07) | (sweep_freq >> 8 & 0x07); + + int offset = sweep_freq >> (regs [0] & shift_mask); + if ( regs [0] & 0x08 ) + offset = -offset; + sweep_freq += offset; + + if ( sweep_freq < 0 ) + { + sweep_freq = 0; + } + else if ( sweep_freq >= 2048 ) + { + sweep_delay = 0; // don't modify channel frequency any further + sweep_freq = 2048; // silence sound immediately + } + } +} + +void Gb_Square::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + if ( sweep_freq == 2048 ) + playing = false; + + static unsigned char const table [4] = { 1, 2, 4, 6 }; + int const duty = table [regs [1] >> 6]; + int amp = volume & playing; + if ( phase >= duty ) + amp = -amp; + + int frequency = this->frequency(); + if ( unsigned (frequency - 1) > 2040 ) // frequency < 1 || frequency > 2041 + { + // really high frequency results in DC at half volume + amp = volume >> 1; + playing = false; + } + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + int const period = (2048 - frequency) * 4; + Blip_Buffer* const output = this->output; + int phase = this->phase; + int delta = amp * 2; + do + { + phase = (phase + 1) & 7; + if ( phase == 0 || phase == duty ) + { + delta = -delta; + synth->offset_inline( time, delta, output ); + } + time += period; + } + while ( time < end_time ); + + this->phase = phase; + last_amp = delta >> 1; + } + delay = time - end_time; +} + +// Gb_Noise + +#include BLARGG_ENABLE_OPTIMIZER + +void Gb_Noise::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + int amp = volume & playing; + int tap = 13 - (regs [3] & 8); + if ( bits >> tap & 2 ) + amp = -amp; + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + static unsigned char const table [8] = { 8, 16, 32, 48, 64, 80, 96, 112 }; + int period = table [regs [3] & 7] << (regs [3] >> 4); + + // keep parallel resampled time to eliminate time conversion in the loop + Blip_Buffer* const output = this->output; + const blip_resampled_time_t resampled_period = + output->resampled_duration( period ); + blip_resampled_time_t resampled_time = output->resampled_time( time ); + unsigned bits = this->bits; + int delta = amp * 2; + + do + { + unsigned changed = (bits >> tap) + 1; + time += period; + bits <<= 1; + if ( changed & 2 ) + { + delta = -delta; + bits |= 1; + synth->offset_resampled( resampled_time, delta, output ); + } + resampled_time += resampled_period; + } + while ( time < end_time ); + + this->bits = bits; + last_amp = delta >> 1; + } + delay = time - end_time; +} + +// Gb_Wave + +void Gb_Wave::reset(bool gba) +{ + volume_forced = 0; + wave_pos = 0; + wave_mode = gba; + wave_size = 32; + wave_bank = 0; + memset( wave, 0, sizeof wave ); + Gb_Osc::reset(); +} + +inline void Gb_Wave::write_register( int reg, int data ) +{ + switch ( reg ) + { + case 0: + if ( !(data & 0x80) ) + enabled = false; + if (wave_mode) + { + wave_bank = (data & 0x40) >> 1; + wave_size = (data & 0x20) + 32; + } + if (wave_pos > wave_size) wave_pos %= wave_size; + break; + + case 1: + length = 256 - regs [1]; + break; + + case 2: + volume = data >> 5 & 3; + if (wave_mode) volume_forced = data & 0x80; + if (volume_forced) volume = -1; + break; + + case 4: + if ( data & trigger & regs [0] ) + { + wave_pos = 0; + enabled = true; + if ( length == 0 ) + length = 256; + } + } +} + +void Gb_Wave::run( gb_time_t time, gb_time_t end_time, int playing ) +{ + int volume_shift = (volume - 1) & 7; // volume = 0 causes shift = 7 + int amp = (wave_size == 32) ? wave [wave_bank + wave_pos] : wave [wave_pos]; + if (volume_forced) amp = ((amp >> 1) + amp) >> 1; + else amp >>= volume_shift; + amp = (amp & playing) * 2; + + int frequency = this->frequency(); + if ( unsigned (frequency - 1) > 2044 ) // frequency < 1 || frequency > 2045 + { + if (volume_forced) amp = ((30 >> 1) + 30) >> 1; + else amp = 30 >> volume_shift; + amp &= playing; + playing = false; + } + + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset( time, delta, output ); + } + + time += delay; + if ( !playing ) + time = end_time; + + if ( time < end_time ) + { + Blip_Buffer* const output = this->output; + int const period = (2048 - frequency) * 2; + int wave_pos = (this->wave_pos + 1) & (wave_size - 1); + + do + { + int amp = (wave_size == 32) ? wave [wave_bank + wave_pos] : wave [wave_pos]; + if (volume_forced) amp = ((amp >> 1) + amp) >> 1; + else amp >>= volume_shift; + amp *= 2; + wave_pos = (wave_pos + 1) & (wave_size - 1); + int delta = amp - last_amp; + if ( delta ) + { + last_amp = amp; + synth->offset_inline( time, delta, output ); + } + time += period; + } + while ( time < end_time ); + + this->wave_pos = (wave_pos - 1) & (wave_size - 1); + } + delay = time - end_time; +} + +// Gb_Apu::write_osc + +void Gb_Apu::write_osc( int index, int reg, int data ) +{ + reg -= index * 5; + Gb_Square* sq = &square2; + switch ( index ) + { + case 0: + sq = &square1; + case 1: + if ( sq->write_register( reg, data ) && index == 0 ) + { + square1.sweep_freq = square1.frequency(); + if ( (regs [0] & sq->period_mask) && (regs [0] & sq->shift_mask) ) + { + square1.sweep_delay = 1; // cause sweep to recalculate now + square1.clock_sweep(); + } + } + break; + + case 2: + wave.write_register( reg, data ); + break; + + case 3: + if ( noise.write_register( reg, data ) ) + noise.bits = 0x7FFF; + } +} + diff --git a/src/Gb_Apu/Gb_Oscs.h b/src/Gb_Apu/Gb_Oscs.h new file mode 100644 index 00000000..40aa0d1a --- /dev/null +++ b/src/Gb_Apu/Gb_Oscs.h @@ -0,0 +1,90 @@ + +// Private oscillators used by Gb_Apu + +// Gb_Snd_Emu 0.1.4 + +#ifndef GB_OSCS_H +#define GB_OSCS_H + +#include "blargg_common.h" +#include "Blip_Buffer.h" + +struct Gb_Osc +{ + enum { trigger = 0x80 }; + enum { len_enabled_mask = 0x40 }; + + Blip_Buffer* outputs [4]; // NULL, right, left, center + Blip_Buffer* output; + int output_select; + BOOST::uint8_t* regs; // osc's 5 registers + + int delay; + int last_amp; + int volume; + int length; + bool enabled; + + void reset(); + void clock_length(); + int frequency() const { return (regs [4] & 7) * 0x100 + regs [3]; } +}; + +struct Gb_Env : Gb_Osc +{ + int env_delay; + + void reset(); + void clock_envelope(); + bool write_register( int, int ); +}; + +struct Gb_Square : Gb_Env +{ + enum { period_mask = 0x70 }; + enum { shift_mask = 0x07 }; + + typedef Blip_Synth Synth; + Synth const* synth; + int sweep_delay; + int sweep_freq; + int phase; + + void reset(); + void clock_sweep(); + void run( gb_time_t, gb_time_t, int playing ); +}; + +struct Gb_Noise : Gb_Env +{ + typedef Blip_Synth Synth; + Synth const* synth; + unsigned bits; + + void run( gb_time_t, gb_time_t, int playing ); +}; + +struct Gb_Wave : Gb_Osc +{ + typedef Blip_Synth Synth; + Synth const* synth; + int volume_forced; + int wave_pos; + unsigned wave_mode; + unsigned wave_size; + unsigned wave_bank; + BOOST::uint8_t wave [32 * 2]; + + void reset(bool gba = false); + void write_register( int, int ); + void run( gb_time_t, gb_time_t, int playing ); +}; + +inline void Gb_Env::reset() +{ + env_delay = 0; + Gb_Osc::reset(); +} + +#endif + diff --git a/src/Gb_Apu/Multi_Buffer.cpp b/src/Gb_Apu/Multi_Buffer.cpp new file mode 100644 index 00000000..37adb816 --- /dev/null +++ b/src/Gb_Apu/Multi_Buffer.cpp @@ -0,0 +1,215 @@ + +// Blip_Buffer 0.4.0. http://www.slack.net/~ant/ + +#include "Multi_Buffer.h" + +/* Copyright (C) 2003-2006 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. You should have received a copy of the GNU Lesser General +Public License along with this module; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include BLARGG_SOURCE_BEGIN + +Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf ) +{ + length_ = 0; + sample_rate_ = 0; + channels_changed_count_ = 1; +} + +blargg_err_t Multi_Buffer::set_channel_count( int ) +{ + return blargg_success; +} + +Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 ) +{ +} + +Mono_Buffer::~Mono_Buffer() +{ +} + +blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec ) +{ + BLARGG_RETURN_ERR( buf.set_sample_rate( rate, msec ) ); + return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() ); +} + +// Silent_Buffer + +Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse +{ + chan.left = NULL; + chan.center = NULL; + chan.right = NULL; +} + +// Mono_Buffer + +Mono_Buffer::channel_t Mono_Buffer::channel( int ) +{ + channel_t ch; + ch.center = &buf; + ch.left = &buf; + ch.right = &buf; + return ch; +} + +void Mono_Buffer::end_frame( blip_time_t t, bool ) +{ + buf.end_frame( t ); +} + +// Stereo_Buffer + +Stereo_Buffer::Stereo_Buffer() : Multi_Buffer( 2 ) +{ + chan.center = &bufs [0]; + chan.left = &bufs [1]; + chan.right = &bufs [2]; +} + +Stereo_Buffer::~Stereo_Buffer() +{ +} + +blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec ) +{ + for ( int i = 0; i < buf_count; i++ ) + BLARGG_RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) ); + return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() ); +} + +void Stereo_Buffer::clock_rate( long rate ) +{ + for ( int i = 0; i < buf_count; i++ ) + bufs [i].clock_rate( rate ); +} + +void Stereo_Buffer::bass_freq( int bass ) +{ + for ( unsigned i = 0; i < buf_count; i++ ) + bufs [i].bass_freq( bass ); +} + +void Stereo_Buffer::clear() +{ + stereo_added = false; + was_stereo = false; + for ( int i = 0; i < buf_count; i++ ) + bufs [i].clear(); +} + +void Stereo_Buffer::end_frame( blip_time_t clock_count, bool stereo ) +{ + for ( unsigned i = 0; i < buf_count; i++ ) + bufs [i].end_frame( clock_count ); + + stereo_added |= stereo; +} + +long Stereo_Buffer::read_samples( blip_sample_t* out, long count ) +{ + require( !(count & 1) ); // count must be even + count = (unsigned) count / 2; + + long avail = bufs [0].samples_avail(); + if ( count > avail ) + count = avail; + if ( count ) + { + if ( stereo_added || was_stereo ) + { + mix_stereo( out, count ); + + bufs [0].remove_samples( count ); + bufs [1].remove_samples( count ); + bufs [2].remove_samples( count ); + } + else + { + mix_mono( out, count ); + + bufs [0].remove_samples( count ); + + bufs [1].remove_silence( count ); + bufs [2].remove_silence( count ); + } + + // to do: this might miss opportunities for optimization + if ( !bufs [0].samples_avail() ) { + was_stereo = stereo_added; + stereo_added = false; + } + } + + return count * 2; +} + +#include BLARGG_ENABLE_OPTIMIZER + +void Stereo_Buffer::mix_stereo( blip_sample_t* out, long count ) +{ + Blip_Reader left; + Blip_Reader right; + Blip_Reader center; + + left.begin( bufs [1] ); + right.begin( bufs [2] ); + int bass = center.begin( bufs [0] ); + + while ( count-- ) + { + int c = center.read(); + long l = c + left.read(); + long r = c + right.read(); + center.next( bass ); + out [0] = l; + out [1] = r; + out += 2; + + if ( (BOOST::int16_t) l != l ) + out [-2] = 0x7FFF - (l >> 24); + + left.next( bass ); + right.next( bass ); + + if ( (BOOST::int16_t) r != r ) + out [-1] = 0x7FFF - (r >> 24); + } + + center.end( bufs [0] ); + right.end( bufs [2] ); + left.end( bufs [1] ); +} + +void Stereo_Buffer::mix_mono( blip_sample_t* out, long count ) +{ + Blip_Reader in; + int bass = in.begin( bufs [0] ); + + while ( count-- ) + { + long s = in.read(); + in.next( bass ); + out [0] = s; + out [1] = s; + out += 2; + + if ( (BOOST::int16_t) s != s ) { + s = 0x7FFF - (s >> 24); + out [-2] = s; + out [-1] = s; + } + } + + in.end( bufs [0] ); +} + diff --git a/src/Gb_Apu/Multi_Buffer.h b/src/Gb_Apu/Multi_Buffer.h new file mode 100644 index 00000000..155b6ec5 --- /dev/null +++ b/src/Gb_Apu/Multi_Buffer.h @@ -0,0 +1,175 @@ + +// Multi-channel sound buffer interface, and basic mono and stereo buffers + +// Blip_Buffer 0.4.0 + +#ifndef MULTI_BUFFER_H +#define MULTI_BUFFER_H + +#include "blargg_common.h" +#include "Blip_Buffer.h" + +// Interface to one or more Blip_Buffers mapped to one or more channels +// consisting of left, center, and right buffers. +class Multi_Buffer { +public: + Multi_Buffer( int samples_per_frame ); + virtual ~Multi_Buffer() { } + + // Set the number of channels available + virtual blargg_err_t set_channel_count( int ); + + // Get indexed channel, from 0 to channel count - 1 + struct channel_t { + Blip_Buffer* center; + Blip_Buffer* left; + Blip_Buffer* right; + }; + virtual channel_t channel( int index ) = 0; + + // See Blip_Buffer.h + virtual blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ) = 0; + virtual void clock_rate( long ) = 0; + virtual void bass_freq( int ) = 0; + virtual void clear() = 0; + long sample_rate() const; + + // Length of buffer, in milliseconds + int length() const; + + // See Blip_Buffer.h. For optimal operation, pass false for 'added_stereo' + // if nothing was added to the left and right buffers of any channel for + // this time frame. + virtual void end_frame( blip_time_t, bool added_stereo = true ) = 0; + + // Number of samples per output frame (1 = mono, 2 = stereo) + int samples_per_frame() const; + + // Count of changes to channel configuration. Incremented whenever + // a change is made to any of the Blip_Buffers for any channel. + unsigned channels_changed_count() { return channels_changed_count_; } + + // See Blip_Buffer.h + virtual long read_samples( blip_sample_t*, long ) = 0; + virtual long samples_avail() const = 0; + +protected: + void channels_changed() { channels_changed_count_++; } +private: + // noncopyable + Multi_Buffer( const Multi_Buffer& ); + Multi_Buffer& operator = ( const Multi_Buffer& ); + + unsigned channels_changed_count_; + long sample_rate_; + int length_; + int const samples_per_frame_; +}; + +// Uses a single buffer and outputs mono samples. +class Mono_Buffer : public Multi_Buffer { + Blip_Buffer buf; +public: + Mono_Buffer(); + ~Mono_Buffer(); + + // Buffer used for all channels + Blip_Buffer* center() { return &buf; } + + // See Multi_Buffer + blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ); + void clock_rate( long ); + void bass_freq( int ); + void clear(); + channel_t channel( int ); + void end_frame( blip_time_t, bool unused = true ); + long samples_avail() const; + long read_samples( blip_sample_t*, long ); +}; + +// Uses three buffers (one for center) and outputs stereo sample pairs. +class Stereo_Buffer : public Multi_Buffer { +public: + Stereo_Buffer(); + ~Stereo_Buffer(); + + // Buffers used for all channels + Blip_Buffer* center() { return &bufs [0]; } + Blip_Buffer* left() { return &bufs [1]; } + Blip_Buffer* right() { return &bufs [2]; } + + // See Multi_Buffer + blargg_err_t set_sample_rate( long, int msec = blip_default_length ); + void clock_rate( long ); + void bass_freq( int ); + void clear(); + channel_t channel( int index ); + void end_frame( blip_time_t, bool added_stereo = true ); + + long samples_avail() const; + long read_samples( blip_sample_t*, long ); + +private: + enum { buf_count = 3 }; + Blip_Buffer bufs [buf_count]; + channel_t chan; + bool stereo_added; + bool was_stereo; + + void mix_stereo( blip_sample_t*, long ); + void mix_mono( blip_sample_t*, long ); +}; + +// Silent_Buffer generates no samples, useful where no sound is wanted +class Silent_Buffer : public Multi_Buffer { + channel_t chan; +public: + Silent_Buffer(); + + blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ); + void clock_rate( long ) { } + void bass_freq( int ) { } + void clear() { } + channel_t channel( int ) { return chan; } + void end_frame( blip_time_t, bool unused = true ) { } + long samples_avail() const { return 0; } + long read_samples( blip_sample_t*, long ) { return 0; } +}; + + +// End of public interface + +inline blargg_err_t Multi_Buffer::set_sample_rate( long rate, int msec ) +{ + sample_rate_ = rate; + length_ = msec; + return blargg_success; +} + +inline blargg_err_t Silent_Buffer::set_sample_rate( long rate, int msec ) +{ + return Multi_Buffer::set_sample_rate( rate, msec ); +} + +inline int Multi_Buffer::samples_per_frame() const { return samples_per_frame_; } + +inline long Stereo_Buffer::samples_avail() const { return bufs [0].samples_avail() * 2; } + +inline Stereo_Buffer::channel_t Stereo_Buffer::channel( int ) { return chan; } + +inline long Multi_Buffer::sample_rate() const { return sample_rate_; } + +inline int Multi_Buffer::length() const { return length_; } + +inline void Mono_Buffer::clock_rate( long rate ) { buf.clock_rate( rate ); } + +inline void Mono_Buffer::clear() { buf.clear(); } + +inline void Mono_Buffer::bass_freq( int freq ) { buf.bass_freq( freq ); } + +inline long Mono_Buffer::read_samples( blip_sample_t* p, long s ) { return buf.read_samples( p, s ); } + +inline long Mono_Buffer::samples_avail() const { return buf.samples_avail(); } + +#endif + diff --git a/src/Gb_Apu/blargg_common.h b/src/Gb_Apu/blargg_common.h new file mode 100644 index 00000000..b7e9136e --- /dev/null +++ b/src/Gb_Apu/blargg_common.h @@ -0,0 +1,242 @@ + +// Sets up common environment for Shay Green's libraries. +// +// To change configuration options, modify blargg_config.h, not this file. + +#ifndef BLARGG_COMMON_H +#define BLARGG_COMMON_H + +// HAVE_CONFIG_H: If defined, include user's "config.h" first (which *can* +// re-include blargg_common.h if it needs to) +#ifdef HAVE_CONFIG_H + #undef BLARGG_COMMON_H + #include "config.h" + #define BLARGG_COMMON_H +#endif + +// BLARGG_NONPORTABLE: If defined to 1, platform-specific (and possibly non-portable) +// optimizations are used. Defaults to off. Report any problems that occur only when +// this is enabled. +#ifndef BLARGG_NONPORTABLE + #define BLARGG_NONPORTABLE 0 +#endif + +// BLARGG_BIG_ENDIAN, BLARGG_LITTLE_ENDIAN: Determined automatically, otherwise only +// one must be #defined to 1. Only needed if something actually depends on byte order. +#if !defined (BLARGG_BIG_ENDIAN) && !defined (BLARGG_LITTLE_ENDIAN) + #if defined (MSB_FIRST) || defined (__powerc) || defined (macintosh) || \ + defined (WORDS_BIGENDIAN) || defined (__BIG_ENDIAN__) + #define BLARGG_BIG_ENDIAN 1 + #else + #define BLARGG_LITTLE_ENDIAN 1 + #endif +#endif + +// Determine compiler's language support + +// Metrowerks CodeWarrior +#if defined (__MWERKS__) + #define BLARGG_COMPILER_HAS_NAMESPACE 1 + #if !__option(bool) + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + #define STATIC_CAST(T,expr) static_cast< T > (expr) + +// Microsoft Visual C++ +#elif defined (_MSC_VER) + #if _MSC_VER < 1100 + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + +// GNU C++ +#elif defined (__GNUC__) + #if __GNUC__ > 2 + #define BLARGG_COMPILER_HAS_NAMESPACE 1 + #endif + +// Mingw +#elif defined (__MINGW32__) + // empty + +// Pre-ISO C++ compiler +#elif __cplusplus < 199711 + #ifndef BLARGG_COMPILER_HAS_BOOL + #define BLARGG_COMPILER_HAS_BOOL 0 + #endif + +#endif + +/* BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compilers. + If errors occur here, add the following line to your config.h file: + #define BLARGG_COMPILER_HAS_BOOL 0 +*/ +#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL + typedef int bool; + const bool true = 1; + const bool false = 0; +#endif + +// BLARGG_USE_NAMESPACE: If 1, use headers rather than +#if BLARGG_USE_NAMESPACE || (!defined (BLARGG_USE_NAMESPACE) && BLARGG_COMPILER_HAS_NAMESPACE) + #include + #include + #include + #include + #define STD std +#else + #include + #include + #include + #include + #define STD +#endif + +// BLARGG_NEW is used in place of 'new' to create objects. By default, plain new is used. +// To prevent an exception if out of memory, #define BLARGG_NEW new (std::nothrow) +#ifndef BLARGG_NEW + #define BLARGG_NEW new +#endif + +// BOOST::int8_t etc. + +// HAVE_STDINT_H: If defined, use for int8_t etc. +#if defined (HAVE_STDINT_H) + #include + #define BOOST + +// HAVE_INTTYPES_H: If defined, use for int8_t etc. +#elif defined (HAVE_INTTYPES_H) + #include + #define BOOST + +#else + struct BOOST + { + #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F + typedef signed char int8_t; + typedef unsigned char uint8_t; + #else + // No suitable 8-bit type available + typedef struct see_blargg_common_h int8_t; + typedef struct see_blargg_common_h uint8_t; + #endif + + #if USHRT_MAX == 0xFFFF + typedef short int16_t; + typedef unsigned short uint16_t; + #else + // No suitable 16-bit type available + typedef struct see_blargg_common_h int16_t; + typedef struct see_blargg_common_h uint16_t; + #endif + + #if ULONG_MAX == 0xFFFFFFFF + typedef long int32_t; + typedef unsigned long uint32_t; + #elif UINT_MAX == 0xFFFFFFFF + typedef int int32_t; + typedef unsigned int uint32_t; + #else + // No suitable 32-bit type available + typedef struct see_blargg_common_h int32_t; + typedef struct see_blargg_common_h uint32_t; + #endif + }; +#endif + +// BLARGG_SOURCE_BEGIN: Library sources #include this after other #includes. +#ifndef BLARGG_SOURCE_BEGIN + #define BLARGG_SOURCE_BEGIN "blargg_source.h" +#endif + +// BLARGG_ENABLE_OPTIMIZER: Library sources #include this for speed-critical code +#ifndef BLARGG_ENABLE_OPTIMIZER + #define BLARGG_ENABLE_OPTIMIZER "blargg_common.h" +#endif + +// BLARGG_CPU_*: Used to select between some optimizations +#if !defined (BLARGG_CPU_POWERPC) && !defined (BLARGG_CPU_X86) + #if defined (__powerc) + #define BLARGG_CPU_POWERPC 1 + #elif defined (_MSC_VER) && defined (_M_IX86) + #define BLARGG_CPU_X86 1 + #endif +#endif + +// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0. +#ifndef BOOST_STATIC_ASSERT + #ifdef _MSC_VER + // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified + #define BOOST_STATIC_ASSERT( expr ) \ + void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] ) + #else + // Some other compilers fail when declaring same function multiple times in class, + // so differentiate them by line + #define BOOST_STATIC_ASSERT( expr ) \ + void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] [__LINE__] ) + #endif +#endif + +// STATIC_CAST(T,expr): Used in place of static_cast (expr) +#ifndef STATIC_CAST + #define STATIC_CAST(T,expr) ((T) (expr)) +#endif + +// blargg_err_t (NULL on success, otherwise error string) +#ifndef blargg_err_t + typedef const char* blargg_err_t; +#endif +const char* const blargg_success = 0; + +// blargg_vector: Simple array that does *not* work for types with a constructor (non-POD). +template +class blargg_vector { + T* begin_; + STD::size_t size_; +public: + blargg_vector() : begin_( 0 ), size_( 0 ) { } + ~blargg_vector() { STD::free( begin_ ); } + + typedef STD::size_t size_type; + + blargg_err_t resize( size_type n ) + { + void* p = STD::realloc( begin_, n * sizeof (T) ); + if ( !p && n ) + return "Out of memory"; + begin_ = (T*) p; + size_ = n; + return 0; + } + + void clear() + { + void* p = begin_; + begin_ = 0; + size_ = 0; + STD::free( p ); + } + + size_type size() const { return size_; } + + T* begin() { return begin_; } + T* end() { return begin_ + size_; } + + const T* begin() const { return begin_; } + const T* end() const { return begin_ + size_; } + + T& operator [] ( size_type n ) + { + assert( n <= size_ ); // allow for past-the-end value + return begin_ [n]; + } + + const T& operator [] ( size_type n ) const + { + assert( n <= size_ ); // allow for past-the-end value + return begin_ [n]; + } +}; + +#endif + diff --git a/src/Gb_Apu/blargg_endian.h b/src/Gb_Apu/blargg_endian.h new file mode 100644 index 00000000..af461b9d --- /dev/null +++ b/src/Gb_Apu/blargg_endian.h @@ -0,0 +1,156 @@ + +// CPU Byte Order Utilities + +// Game_Music_Emu 0.3.0 + +#ifndef BLARGG_ENDIAN +#define BLARGG_ENDIAN + +#include "blargg_common.h" + +#if 0 + // Read 16/32-bit little-endian integer from memory + unsigned GET_LE16( void const* ); + unsigned long GET_LE32( void const* ); + + // Read 16/32-bit big-endian integer from memory + unsigned GET_BE16( void const* ); + unsigned long GET_BE32( void const* ); + + // Write 16/32-bit integer to memory in little-endian format + void SET_LE16( void*, unsigned ); + void SET_LE32( void*, unsigned ); + + // Write 16/32-bit integer to memory in big-endian format + void SET_BE16( void*, unsigned long ); + void SET_BE32( void*, unsigned long ); +#endif + +inline unsigned get_le16( void const* p ) +{ + return ((unsigned char*) p) [1] * 0x100 + + ((unsigned char*) p) [0]; +} + +inline unsigned get_be16( void const* p ) +{ + return ((unsigned char*) p) [0] * 0x100 + + ((unsigned char*) p) [1]; +} + +inline unsigned long get_le32( void const* p ) +{ + return ((unsigned char*) p) [3] * 0x01000000 + + ((unsigned char*) p) [2] * 0x00010000 + + ((unsigned char*) p) [1] * 0x00000100 + + ((unsigned char*) p) [0]; +} + +inline unsigned long get_be32( void const* p ) +{ + return ((unsigned char*) p) [0] * 0x01000000 + + ((unsigned char*) p) [1] * 0x00010000 + + ((unsigned char*) p) [2] * 0x00000100 + + ((unsigned char*) p) [3]; +} + +inline void set_le16( void* p, unsigned n ) +{ + ((unsigned char*) p) [1] = (unsigned char) (n >> 8); + ((unsigned char*) p) [0] = (unsigned char) n; +} + +inline void set_be16( void* p, unsigned n ) +{ + ((unsigned char*) p) [0] = (unsigned char) (n >> 8); + ((unsigned char*) p) [1] = (unsigned char) n; +} + +inline void set_le32( void* p, unsigned long n ) +{ + ((unsigned char*) p) [3] = (unsigned char) (n >> 24); + ((unsigned char*) p) [2] = (unsigned char) (n >> 16); + ((unsigned char*) p) [1] = (unsigned char) (n >> 8); + ((unsigned char*) p) [0] = (unsigned char) n; +} + +inline void set_be32( void* p, unsigned long n ) +{ + ((unsigned char*) p) [0] = (unsigned char) (n >> 24); + ((unsigned char*) p) [1] = (unsigned char) (n >> 16); + ((unsigned char*) p) [2] = (unsigned char) (n >> 8); + ((unsigned char*) p) [3] = (unsigned char) n; +} + +#ifndef GET_LE16 + // Optimized implementation if byte order is known + #if BLARGG_NONPORTABLE && BLARGG_LITTLE_ENDIAN + #define GET_LE16( addr ) (*(BOOST::uint16_t*) (addr)) + #define GET_LE32( addr ) (*(BOOST::uint32_t*) (addr)) + #define SET_LE16( addr, data ) (void (*(BOOST::uint16_t*) (addr) = (data))) + #define SET_LE32( addr, data ) (void (*(BOOST::uint32_t*) (addr) = (data))) + + #elif BLARGG_NONPORTABLE && BLARGG_CPU_POWERPC + // PowerPC has special byte-reversed instructions + // to do: assumes that PowerPC is running in big-endian mode + #define GET_LE16( addr ) (__lhbrx( (addr), 0 )) + #define GET_LE32( addr ) (__lwbrx( (addr), 0 )) + #define SET_LE16( addr, data ) (__sthbrx( (data), (addr), 0 )) + #define SET_LE32( addr, data ) (__stwbrx( (data), (addr), 0 )) + + #define GET_BE16( addr ) (*(BOOST::uint16_t*) (addr)) + #define GET_BE32( addr ) (*(BOOST::uint32_t*) (addr)) + #define SET_BE16( addr, data ) (void (*(BOOST::uint16_t*) (addr) = (data))) + #define SET_BE32( addr, data ) (void (*(BOOST::uint32_t*) (addr) = (data))) + + #endif +#endif + +#ifndef GET_LE16 + #define GET_LE16( addr ) get_le16( addr ) +#endif + +#ifndef GET_LE32 + #define GET_LE32( addr ) get_le32( addr ) +#endif + +#ifndef SET_LE16 + #define SET_LE16( addr, data ) set_le16( addr, data ) +#endif + +#ifndef SET_LE32 + #define SET_LE32( addr, data ) set_le32( addr, data ) +#endif + +#ifndef GET_BE16 + #define GET_BE16( addr ) get_be16( addr ) +#endif + +#ifndef GET_BE32 + #define GET_BE32( addr ) get_be32( addr ) +#endif + +#ifndef SET_BE16 + #define SET_BE16( addr, data ) set_be16( addr, data ) +#endif + +#ifndef SET_BE32 + #define SET_BE32( addr, data ) set_be32( addr, data ) +#endif + +// auto-selecting versions + +inline void set_le( BOOST::uint16_t* p, unsigned n ) { SET_LE16( p, n ); } +inline void set_le( BOOST::uint32_t* p, unsigned long n ) { SET_LE32( p, n ); } + +inline void set_be( BOOST::uint16_t* p, unsigned n ) { SET_BE16( p, n ); } +inline void set_be( BOOST::uint32_t* p, unsigned long n ) { SET_BE32( p, n ); } + +inline unsigned get_le( BOOST::uint16_t* p ) { return GET_LE16( p ); } +inline unsigned long get_le( BOOST::uint32_t* p ) { return GET_LE32( p ); } + +inline unsigned get_be( BOOST::uint16_t* p ) { return GET_BE16( p ); } +inline unsigned long get_be( BOOST::uint32_t* p ) { return GET_BE32( p ); } + +#endif + diff --git a/src/Gb_Apu/blargg_source.h b/src/Gb_Apu/blargg_source.h new file mode 100644 index 00000000..34f17f8d --- /dev/null +++ b/src/Gb_Apu/blargg_source.h @@ -0,0 +1,76 @@ + +// By default, #included at beginning of library source files. +// Can be overridden by #defining BLARGG_SOURCE_BEGIN to path of alternate file. + +// Copyright (C) 2005 Shay Green. + +#ifndef BLARGG_SOURCE_H +#define BLARGG_SOURCE_H + +// If debugging is enabled, abort program if expr is false. Meant for checking +// internal state and consistency. A failed assertion indicates a bug in the module. +// void assert( bool expr ); +#include + +// If debugging is enabled and expr is false, abort program. Meant for checking +// caller-supplied parameters and operations that are outside the control of the +// module. A failed requirement indicates a bug outside the module. +// void require( bool expr ); +#undef require +#define require( expr ) assert( expr ) + +// Like printf() except output goes to debug log file. Might be defined to do +// nothing (not even evaluate its arguments). +// void dprintf( const char* format, ... ); +#undef dprintf +#ifdef BLARGG_DPRINTF + #define dprintf BLARGG_DPRINTF +#else + inline void blargg_dprintf_( const char*, ... ) { } + #define dprintf (1) ? (void) 0 : blargg_dprintf_ +#endif + +// If enabled, evaluate expr and if false, make debug log entry with source file +// and line. Meant for finding situations that should be examined further, but that +// don't indicate a problem. In all cases, execution continues normally. +#undef check +#ifdef BLARGG_CHECK + #define check( expr ) BLARGG_CHECK( expr ) +#else + #define check( expr ) ((void) 0) +#endif + +// If expr returns non-NULL error string, return it from current function, otherwise continue. +#define BLARGG_RETURN_ERR( expr ) do { \ + blargg_err_t blargg_return_err_ = (expr); \ + if ( blargg_return_err_ ) return blargg_return_err_; \ + } while ( 0 ) + +// If ptr is NULL, return out of memory error string. +#define BLARGG_CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 ) + +// Avoid any macros which evaluate their arguments multiple times +#undef min +#undef max + +// using const references generates crappy code, and I am currenly only using these +// for built-in types, so they take arguments by value + +template +inline T min( T x, T y ) +{ + if ( x < y ) + return x; + return y; +} + +template +inline T max( T x, T y ) +{ + if ( x < y ) + return y; + return x; +} + +#endif + diff --git a/src/Gb_Apu/boost/config.hpp b/src/Gb_Apu/boost/config.hpp new file mode 100644 index 00000000..76c2441f --- /dev/null +++ b/src/Gb_Apu/boost/config.hpp @@ -0,0 +1,13 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_CONFIG_HPP +#define BOOST_CONFIG_HPP + +#define BOOST_MINIMAL 1 + +#define BLARGG_BEGIN_NAMESPACE( name ) +#define BLARGG_END_NAMESPACE + +#endif + diff --git a/src/Gb_Apu/boost/cstdint.hpp b/src/Gb_Apu/boost/cstdint.hpp new file mode 100644 index 00000000..97f4feed --- /dev/null +++ b/src/Gb_Apu/boost/cstdint.hpp @@ -0,0 +1,42 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_CSTDINT_HPP +#define BOOST_CSTDINT_HPP + +#if BLARGG_USE_NAMESPACE + #include +#else + #include +#endif + +BLARGG_BEGIN_NAMESPACE( boost ) + +#if UCHAR_MAX != 0xFF || SCHAR_MAX != 0x7F +# error "No suitable 8-bit type available" +#endif + +typedef unsigned char uint8_t; +typedef signed char int8_t; + +#if USHRT_MAX != 0xFFFF +# error "No suitable 16-bit type available" +#endif + +typedef short int16_t; +typedef unsigned short uint16_t; + +#if ULONG_MAX == 0xFFFFFFFF + typedef long int32_t; + typedef unsigned long uint32_t; +#elif UINT_MAX == 0xFFFFFFFF + typedef int int32_t; + typedef unsigned int uint32_t; +#else +# error "No suitable 32-bit type available" +#endif + +BLARGG_END_NAMESPACE + +#endif + diff --git a/src/Gb_Apu/boost/static_assert.hpp b/src/Gb_Apu/boost/static_assert.hpp new file mode 100644 index 00000000..2f80ff05 --- /dev/null +++ b/src/Gb_Apu/boost/static_assert.hpp @@ -0,0 +1,22 @@ + +// Boost substitute. For full boost library see http://boost.org + +#ifndef BOOST_STATIC_ASSERT_HPP +#define BOOST_STATIC_ASSERT_HPP + +#if defined (_MSC_VER) && _MSC_VER <= 1200 + // MSVC6 can't handle the ##line concatenation + #define BOOST_STATIC_ASSERT( expr ) struct { int n [1 / ((expr) ? 1 : 0)]; } + +#else + #define BOOST_STATIC_ASSERT3( expr, line ) \ + typedef int boost_static_assert_##line [1 / ((expr) ? 1 : 0)] + + #define BOOST_STATIC_ASSERT2( expr, line ) BOOST_STATIC_ASSERT3( expr, line ) + + #define BOOST_STATIC_ASSERT( expr ) BOOST_STATIC_ASSERT2( expr, __LINE__ ) + +#endif + +#endif + diff --git a/src/Gfx.cpp b/src/Gfx.cpp new file mode 100644 index 00000000..26c15691 --- /dev/null +++ b/src/Gfx.cpp @@ -0,0 +1,47 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" + +int coeff[32] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}; + + +u32 line0[240]; +u32 line1[240]; +u32 line2[240]; +u32 line3[240]; +u32 lineOBJ[240]; +u32 lineOBJWin[240]; +u32 lineMix[240]; +bool gfxInWin0[240]; +bool gfxInWin1[240]; + +int gfxBG2Changed = 0; +int gfxBG3Changed = 0; + +int gfxBG2X = 0; +int gfxBG2Y = 0; +int gfxBG2LastX = 0; +int gfxBG2LastY = 0; +int gfxBG3X = 0; +int gfxBG3Y = 0; +int gfxBG3LastX = 0; +int gfxBG3LastY = 0; +int gfxLastVCOUNT = 0; diff --git a/src/Gfx.h b/src/Gfx.h new file mode 100644 index 00000000..40abe485 --- /dev/null +++ b/src/Gfx.h @@ -0,0 +1,1581 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_GFX_H +#define VBA_GFX_H + +#include "GBA.h" +#include "Gfx.h" +#include "Globals.h" + +#include "Port.h" + +//#define SPRITE_DEBUG + +void gfxDrawTextScreen(u16, u16, u16, u32 *); +void gfxDrawRotScreen(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32*); +void gfxDrawRotScreen16Bit(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32*); +void gfxDrawRotScreen256(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32*); +void gfxDrawRotScreen16Bit160(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32*); +void gfxDrawSprites(u32 *); +void gfxIncreaseBrightness(u32 *line, int coeff); +void gfxDecreaseBrightness(u32 *line, int coeff); +void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb); + +void mode0RenderLine(); +void mode0RenderLineNoWindow(); +void mode0RenderLineAll(); + +void mode1RenderLine(); +void mode1RenderLineNoWindow(); +void mode1RenderLineAll(); + +void mode2RenderLine(); +void mode2RenderLineNoWindow(); +void mode2RenderLineAll(); + +void mode3RenderLine(); +void mode3RenderLineNoWindow(); +void mode3RenderLineAll(); + +void mode4RenderLine(); +void mode4RenderLineNoWindow(); +void mode4RenderLineAll(); + +void mode5RenderLine(); +void mode5RenderLineNoWindow(); +void mode5RenderLineAll(); + +extern int coeff[32]; +extern u32 line0[240]; +extern u32 line1[240]; +extern u32 line2[240]; +extern u32 line3[240]; +extern u32 lineOBJ[240]; +extern u32 lineOBJWin[240]; +extern u32 lineMix[240]; +extern bool gfxInWin0[240]; +extern bool gfxInWin1[240]; + +extern int gfxBG2Changed; +extern int gfxBG3Changed; + +extern int gfxBG2X; +extern int gfxBG2Y; +extern int gfxBG2LastX; +extern int gfxBG2LastY; +extern int gfxBG3X; +extern int gfxBG3Y; +extern int gfxBG3LastX; +extern int gfxBG3LastY; +extern int gfxLastVCOUNT; + +inline void gfxClearArray(u32 *array) +{ + for(int i = 0; i < 240; i++) { + *array++ = 0x80000000; + } +} + +inline void gfxDrawTextScreen(u16 control, u16 hofs, u16 vofs, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 *charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u16 *screenBase = (u16 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u32 prio = ((control & 3)<<25) + 0x1000000; + int sizeX = 256; + int sizeY = 256; + switch((control >> 14) & 3) { + case 0: + break; + case 1: + sizeX = 512; + break; + case 2: + sizeY = 512; + break; + case 3: + sizeX = 512; + sizeY = 512; + break; + } + + int maskX = sizeX-1; + int maskY = sizeY-1; + + bool mosaicOn = (control & 0x40) ? true : false; + + int xxx = hofs & maskX; + int yyy = (vofs + VCOUNT) & maskY; + int mosaicX = (MOSAIC & 0x000F)+1; + int mosaicY = ((MOSAIC & 0x00F0)>>4)+1; + + if(mosaicOn) { + if((VCOUNT % mosaicY) != 0) { + mosaicY = (VCOUNT / mosaicY) * mosaicY; + yyy = (vofs + mosaicY) & maskY; + } + } + + if(yyy > 255 && sizeY > 256) { + yyy &= 255; + screenBase += 0x400; + if(sizeX > 256) + screenBase += 0x400; + } + + int yshift = ((yyy>>3)<<5); + if((control) & 0x80) { + u16 *screenSource = screenBase + 0x400 * (xxx>>8) + ((xxx & 255)>>3) + yshift; + for(int x = 0; x < 240; x++) { + u16 data = READ16LE(screenSource); + + int tile = data & 0x3FF; + int tileX = (xxx & 7); + int tileY = yyy & 7; + + if(data & 0x0400) + tileX = 7 - tileX; + if(data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + + line[x] = color ? (READ16LE(&palette[color]) | prio): 0x80000000; + + if(data & 0x0400) { + if(tileX == 0) + screenSource++; + } else if(tileX == 7) + screenSource++; + xxx++; + if(xxx == 256) { + if(sizeX > 256) + screenSource = screenBase + 0x400 + yshift; + else { + screenSource = screenBase + yshift; + xxx = 0; + } + } else if(xxx >= sizeX) { + xxx = 0; + screenSource = screenBase + yshift; + } + } + } else { + u16 *screenSource = screenBase + 0x400*(xxx>>8)+((xxx&255)>>3) + + yshift; + for(int x = 0; x < 240; x++) { + u16 data = READ16LE(screenSource); + + int tile = data & 0x3FF; + int tileX = (xxx & 7); + int tileY = yyy & 7; + + if(data & 0x0400) + tileX = 7 - tileX; + if(data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[(tile<<5) + (tileY<<2) + (tileX>>1)]; + + if(tileX & 1) { + color = (color >> 4); + } else { + color &= 0x0F; + } + + int pal = (READ16LE(screenSource)>>8) & 0xF0; + line[x] = color ? (READ16LE(&palette[pal + color])|prio): 0x80000000; + + if(data & 0x0400) { + if(tileX == 0) + screenSource++; + } else if(tileX == 7) + screenSource++; + xxx++; + if(xxx == 256) { + if(sizeX > 256) + screenSource = screenBase + 0x400 + yshift; + else { + screenSource = screenBase + yshift; + xxx = 0; + } + } else if(xxx >= sizeX) { + xxx = 0; + screenSource = screenBase + yshift; + } + } + } + if(mosaicOn) { + if(mosaicX > 1) { + int m = 1; + for(int i = 0; i < 239; i++) { + line[i+1] = line[i]; + m++; + if(m == mosaicX) { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 *charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u8 *screenBase = (u8 *)&vram[((control >> 8) & 0x1f) * 0x800]; + int prio = ((control & 3) << 25) + 0x1000000; + + int sizeX = 128; + int sizeY = 128; + switch((control >> 14) & 3) { + case 0: + break; + case 1: + sizeX = sizeY = 256; + break; + case 2: + sizeX = sizeY = 512; + break; + case 3: + sizeX = sizeY = 1024; + break; + } + + int dx = pa & 0x7FFF; + if(pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if(pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if(pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if(pd & 0x8000) + dmy |= 0xFFFF8000; + + if(VCOUNT == 0) + changed = 3; + + if(changed & 1) { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + currentX |= 0xF8000000; + } else { + currentX += dmx; + } + + if(changed & 2) { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + currentY |= 0xF8000000; + } else { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if(control & 0x40) { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT % mosaicY); + realX -= y*dmx; + realY -= y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + if(control & 0x2000) { + xxx %= sizeX; + yyy %= sizeY; + if(xxx < 0) + xxx += sizeX; + if(yyy < 0) + yyy += sizeY; + } + + if(control & 0x80) { + for(int x = 0; x < 240; x++) { + if(xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) { + line[x] = 0x80000000; + } else { + int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)]; + + int tileX = (xxx & 7); + int tileY = yyy & 7; + + u8 color = charBase[(tile<<6) + (tileY<<3) + tileX]; + + line[x] = color ? (READ16LE(&palette[color])|prio): 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + + if(control & 0x2000) { + xxx %= sizeX; + yyy %= sizeY; + if(xxx < 0) + xxx += sizeX; + if(yyy < 0) + yyy += sizeY; + } + } + } else { + for(int x = 0; x < 240; x++) { + if(xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) { + line[x] = 0x80000000; + } else { + int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)]; + + int tileX = (xxx & 7); + int tileY = yyy & 7; + + u8 color = charBase[(tile<<6) + (tileY<<3) + tileX]; + + line[x] = color ? (READ16LE(&palette[color])|prio): 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + + if(control & 0x2000) { + xxx %= sizeX; + yyy %= sizeY; + if(xxx < 0) + xxx += sizeX; + if(yyy < 0) + yyy += sizeY; + } + } + } + + if(control & 0x40) { + int mosaicX = (MOSAIC & 0xF) + 1; + if(mosaicX > 1) { + int m = 1; + for(int i = 0; i < 239; i++) { + line[i+1] = line[i]; + m++; + if(m == mosaicX) { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen16Bit(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *screenBase = (u16 *)&vram[0]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 240; + int sizeY = 160; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if(pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if(pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if(pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if(pd & 0x8000) + dmy |= 0xFFFF8000; + + if(VCOUNT == 0) + changed = 3; + + if(changed & 1) { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + currentX |= 0xF8000000; + } else + currentX += dmx; + + if(changed & 2) { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + currentY |= 0xF8000000; + } else { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if(control & 0x40) { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT % mosaicY); + realX -= y*dmx; + realY -= y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for(int x = 0; x < 240; x++) { + if(xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) { + line[x] = 0x80000000; + } else { + line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio); + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if(control & 0x40) { + int mosaicX = (MOSAIC & 0xF) + 1; + if(mosaicX > 1) { + int m = 1; + for(int i = 0; i < 239; i++) { + line[i+1] = line[i]; + m++; + if(m == mosaicX) { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen256(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int ¤tX, int& currentY, + int changed, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 *screenBase = (DISPCNT & 0x0010) ? &vram[0xA000] : &vram[0x0000]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 240; + int sizeY = 160; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if(pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if(pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if(pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if(pd & 0x8000) + dmy |= 0xFFFF8000; + + if(VCOUNT == 0) + changed = 3; + + if(changed & 1) { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + currentX |= 0xF8000000; + } else { + currentX += dmx; + } + + if(changed & 2) { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + currentY |= 0xF8000000; + } else { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if(control & 0x40) { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT / mosaicY) * mosaicY; + realX = startX + y*dmx; + realY = startY + y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for(int x = 0; x < 240; x++) { + if(xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) { + line[x] = 0x80000000; + } else { + u8 color = screenBase[yyy * 240 + xxx]; + + line[x] = color ? (READ16LE(&palette[color])|prio): 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if(control & 0x40) { + int mosaicX = (MOSAIC & 0xF) + 1; + if(mosaicX > 1) { + int m = 1; + for(int i = 0; i < 239; i++) { + line[i+1] = line[i]; + m++; + if(m == mosaicX) { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen16Bit160(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *screenBase = (DISPCNT & 0x0010) ? (u16 *)&vram[0xa000] : + (u16 *)&vram[0]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 160; + int sizeY = 128; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if(pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if(pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if(pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if(pd & 0x8000) + dmy |= 0xFFFF8000; + + if(VCOUNT == 0) + changed = 3; + + if(changed & 1) { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if(x_h & 0x0800) + currentX |= 0xF8000000; + } else { + currentX += dmx; + } + + if(changed & 2) { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if(y_h & 0x0800) + currentY |= 0xF8000000; + } else { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if(control & 0x40) { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT / mosaicY) * mosaicY; + realX = startX + y*dmx; + realY = startY + y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for(int x = 0; x < 240; x++) { + if(xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) { + line[x] = 0x80000000; + } else { + line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio); + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if(control & 0x40) { + int mosaicX = (MOSAIC & 0xF) + 1; + if(mosaicX > 1) { + int m = 1; + for(int i = 0; i < 239; i++) { + line[i+1] = line[i]; + m++; + if(m == mosaicX) { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawSprites(u32 *lineOBJ) +{ + int m=0; + gfxClearArray(lineOBJ); + if(layerEnable & 0x1000) { + u16 *sprites = (u16 *)oam; + u16 *spritePalette = &((u16 *)paletteRAM)[256]; + int mosaicY = ((MOSAIC & 0xF000)>>12) + 1; + int mosaicX = ((MOSAIC & 0xF00)>>8) + 1; + for(int x = 0; x < 128 ; x++) { + u16 a0 = READ16LE(sprites++); + u16 a1 = READ16LE(sprites++); + u16 a2 = READ16LE(sprites++); + sprites++; + + // ignore OBJ-WIN + if((a0 & 0x0c00) == 0x0800) + continue; + + int sizeY = 8; + int sizeX = 8; + + switch(((a0 >>12) & 0x0c)|(a1>>14)) { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + continue; + } + +#ifdef SPRITE_DEBUG + int maskX = sizeX-1; + int maskY = sizeY-1; +#endif + + int sy = (a0 & 255); + + if(sy > 160) + sy -= 256; + + if(a0 & 0x0100) { + int fieldX = sizeX; + int fieldY = sizeY; + if(a0 & 0x0200) { + fieldX <<= 1; + fieldY <<= 1; + } + + int t = VCOUNT - sy; + if((t >= 0) && (t < fieldY)) { + int sx = (a1 & 0x1FF); + if((sx < 240) || (((sx + fieldX) & 511) < 240)) { + // int t2 = t - (fieldY >> 1); + int rot = (a1 >> 9) & 0x1F; + u16 *OAM = (u16 *)oam; + int dx = READ16LE(&OAM[3 + (rot << 4)]); + if(dx & 0x8000) + dx |= 0xFFFF8000; + int dmx = READ16LE(&OAM[7 + (rot << 4)]); + if(dmx & 0x8000) + dmx |= 0xFFFF8000; + int dy = READ16LE(&OAM[11 + (rot << 4)]); + if(dy & 0x8000) + dy |= 0xFFFF8000; + int dmy = READ16LE(&OAM[15 + (rot << 4)]); + if(dmy & 0x8000) + dmy |= 0xFFFF8000; + + if(a0 & 0x1000) { + t -= (t % mosaicY); + } + + int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx + + t * dmx; + int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy + + t * dmy; + + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + if(a0 & 0x2000) { + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + for(int x = 0; x < fieldX; x++) { + int xxx = realX >> 8; + int yyy = realY >> 8; + + if(xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240); + else { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<3) + ((xxx >> 3)<<6) + + (xxx & 7))&0x7FFF)]; + if ((color==0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } else if((color) && (prio < (lineOBJ[sx]&0xFF000000))) { + lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + + if (a0 & 0x1000) { + m++; + if (m==mosaicX) + m=0; + } +#ifdef SPRITE_DEBUG + if(t == 0 || t == maskY || x == 0 || x == maskX) + lineOBJ[sx] = 0x001F; +#endif + } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } else { + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 3; + int palette = (a2 >> 8) & 0xF0; + for(int x = 0; x < fieldX; x++) { + int xxx = realX >> 8; + int yyy = realY >> 8; + if(xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240); + else { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<2) + ((xxx >> 3)<<5) + + ((xxx & 7)>>1))&0x7FFF)]; + if(xxx & 1) + color >>= 4; + else + color &= 0x0F; + + if ((color==0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } else if((color) && (prio < (lineOBJ[sx]&0xFF000000))) { + lineOBJ[sx] = READ16LE(&spritePalette[palette+color]) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + } + if((a0 & 0x1000) && m) { + m++; + if (m==mosaicX) + m=0; + } + +#ifdef SPRITE_DEBUG + if(t == 0 || t == maskY || x == 0 || x == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1)&511;; + realX += dx; + realY += dy; + + } + } + } + } + } else { + int t = VCOUNT - sy; + if((t >= 0) && (t < sizeY)) { + int sx = (a1 & 0x1FF); + if(((sx < 240)||(((sx+sizeX)&511)<240)) && !(a0 & 0x0200)) { + if(a0 & 0x2000) { + if(a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) { + inc = sizeX >> 2; + } else { + c &= 0x3FE; + } + int xxx = 0; + if(a1 & 0x1000) + xxx = sizeX-1; + + if(a0 & 0x1000) { + t -= (t % mosaicY); + } + + int address = 0x10000 + ((((c+ (t>>3) * inc) << 5) + + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7)) & 0x7FFF); + + if(a1 & 0x1000) + xxx = 7; + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + for(int xx = 0; xx < sizeX; xx++) { + if(sx < 240) { + u8 color = vram[address]; + if ((color==0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } else if((color) && (prio < (lineOBJ[sx]&0xFF000000))) { + lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + + if (a0 & 0x1000) { + m++; + if (m==mosaicX) + m=0; + } + +#ifdef SPRITE_DEBUG + if(t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + } + + sx = (sx+1) & 511; + if(a1 & 0x1000) { + xxx--; + address--; + if(xxx == -1) { + address -= 56; + xxx = 7; + } + if(address < 0x10000) + address += 0x8000; + } else { + xxx++; + address++; + if(xxx == 8) { + address += 56; + xxx = 0; + } + if(address > 0x17fff) + address -= 0x8000; + } + } + } else { + if(a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) { + inc = sizeX >> 3; + } + int xxx = 0; + if(a1 & 0x1000) + xxx = sizeX - 1; + + if(a0 & 0x1000) { + t -= (t % mosaicY); + } + + int address = 0x10000 + ((((c + (t>>3) * inc)<<5) + + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7FFF); + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + int palette = (a2 >> 8) & 0xF0; + if(a1 & 0x1000) { + xxx = 7; + for(int xx = sizeX - 1; xx >= 0; xx--) { + if(sx < 240) { + u8 color = vram[address]; + if(xx & 1) { + color = (color >> 4); + } else + color &= 0x0F; + + if ((color==0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } else if((color) && (prio < (lineOBJ[sx]&0xFF000000))) { + lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + } + if (a0 & 0x1000) { + m++; + if (m==mosaicX) + m=0; + } +#ifdef SPRITE_DEBUG + if(t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1) & 511; + xxx--; + if(!(xx & 1)) + address--; + if(xxx == -1) { + xxx = 7; + address -= 28; + } + if(address < 0x10000) + address += 0x8000; + } + } else { + for(int xx = 0; xx < sizeX; xx++) { + if(sx < 240) { + u8 color = vram[address]; + if(xx & 1) { + color = (color >> 4); + } else + color &= 0x0F; + + if ((color==0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } else if((color) && (prio < (lineOBJ[sx]&0xFF000000))) { + lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio; + if((a0 & 0x1000) && m) + lineOBJ[sx]=(lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + + } + } + if (a0 & 0x1000) { + m++; + if (m==mosaicX) + m=0; + } +#ifdef SPRITE_DEBUG + if(t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1) & 511; + xxx++; + if(xx & 1) + address++; + if(xxx == 8) { + address += 28; + xxx = 0; + } + if(address > 0x17fff) + address -= 0x8000; + } + } + } + } + } + } + } + } +} + +inline void gfxDrawOBJWin(u32 *lineOBJWin) +{ + gfxClearArray(lineOBJWin); + if(layerEnable & 0x8000) { + u16 *sprites = (u16 *)oam; + // u16 *spritePalette = &((u16 *)paletteRAM)[256]; + for(int x = 0; x < 128 ; x++) { + u16 a0 = READ16LE(sprites++); + u16 a1 = READ16LE(sprites++); + u16 a2 = READ16LE(sprites++); + sprites++; + + // ignore non OBJ-WIN + if((a0 & 0x0c00) != 0x0800) + continue; + + int sizeY = 8; + int sizeX = 8; + + switch(((a0 >>12) & 0x0c)|(a1>>14)) { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + continue; + } + + int sy = (a0 & 255); + + if(sy > 160) + sy -= 256; + + if(a0 & 0x0100) { + int fieldX = sizeX; + int fieldY = sizeY; + if(a0 & 0x0200) { + fieldX <<= 1; + fieldY <<= 1; + } + + int t = VCOUNT - sy; + if((t >= 0) && (t < fieldY)) { + int sx = (a1 & 0x1FF); + if((sx < 240) || (((sx + fieldX) & 511) < 240)) { + // int t2 = t - (fieldY >> 1); + int rot = (a1 >> 9) & 0x1F; + u16 *OAM = (u16 *)oam; + int dx = READ16LE(&OAM[3 + (rot << 4)]); + if(dx & 0x8000) + dx |= 0xFFFF8000; + int dmx = READ16LE(&OAM[7 + (rot << 4)]); + if(dmx & 0x8000) + dmx |= 0xFFFF8000; + int dy = READ16LE(&OAM[11 + (rot << 4)]); + if(dy & 0x8000) + dy |= 0xFFFF8000; + int dmy = READ16LE(&OAM[15 + (rot << 4)]); + if(dmy & 0x8000) + dmy |= 0xFFFF8000; + + int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx + + t * dmx; + int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy + + t * dmy; + + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + if(a0 & 0x2000) { + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + for(int x = 0; x < fieldX; x++) { + int xxx = realX >> 8; + int yyy = realY >> 8; + + if(xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240) { + } else { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<3) + ((xxx >> 3)<<6) + + (xxx & 7))&0x7fff)]; + if(color) { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } else { + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 3; + // int palette = (a2 >> 8) & 0xF0; + for(int x = 0; x < fieldX; x++) { + int xxx = realX >> 8; + int yyy = realY >> 8; + + // if(x == 0 || x == (sizeX-1) || + // t == 0 || t == (sizeY-1)) { + // lineOBJ[sx] = 0x001F | prio; + // } else { + if(xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240){ + } else { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<2) + ((xxx >> 3)<<5) + + ((xxx & 7)>>1))&0x7fff)]; + if(xxx & 1) + color >>= 4; + else + color &= 0x0F; + + if(color) { + lineOBJWin[sx] = 1; + } + } + // } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } + } + } + } else { + int t = VCOUNT - sy; + if((t >= 0) && (t < sizeY)) { + int sx = (a1 & 0x1FF); + if(((sx < 240)||(((sx+sizeX)&511)<240)) && !(a0 & 0x0200)) { + if(a0 & 0x2000) { + if(a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) { + inc = sizeX >> 2; + } else { + c &= 0x3FE; + } + int xxx = 0; + if(a1 & 0x1000) + xxx = sizeX-1; + int address = 0x10000 + ((((c+ (t>>3) * inc) << 5) + + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7))&0x7fff); + if(a1 & 0x1000) + xxx = 7; + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + for(int xx = 0; xx < sizeX; xx++) { + if(sx < 240) { + u8 color = vram[address]; + if(color) { + lineOBJWin[sx] = 1; + } + } + + sx = (sx+1) & 511; + if(a1 & 0x1000) { + xxx--; + address--; + if(xxx == -1) { + address -= 56; + xxx = 7; + } + if(address < 0x10000) + address += 0x8000; + } else { + xxx++; + address++; + if(xxx == 8) { + address += 56; + xxx = 0; + } + if(address > 0x17fff) + address -= 0x8000; + } + } + } else { + if(a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if(DISPCNT & 0x40) { + inc = sizeX >> 3; + } + int xxx = 0; + if(a1 & 0x1000) + xxx = sizeX - 1; + int address = 0x10000 + ((((c + (t>>3) * inc)<<5) + + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7fff); + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + // int palette = (a2 >> 8) & 0xF0; + if(a1 & 0x1000) { + xxx = 7; + for(int xx = sizeX - 1; xx >= 0; xx--) { + if(sx < 240) { + u8 color = vram[address]; + if(xx & 1) { + color = (color >> 4); + } else + color &= 0x0F; + + if(color) { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1) & 511; + xxx--; + if(!(xx & 1)) + address--; + if(xxx == -1) { + xxx = 7; + address -= 28; + } + if(address < 0x10000) + address += 0x8000; + } + } else { + for(int xx = 0; xx < sizeX; xx++) { + if(sx < 240) { + u8 color = vram[address]; + if(xx & 1) { + color = (color >> 4); + } else + color &= 0x0F; + + if(color) { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1) & 511; + xxx++; + if(xx & 1) + address++; + if(xxx == 8) { + address += 28; + xxx = 0; + } + if(address > 0x17fff) + address -= 0x8000; + } + } + } + } + } + } + } + } +} + +inline u32 gfxIncreaseBrightness(u32 color, int coeff) +{ + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r + (((31 - r) * coeff) >> 4); + g = g + (((31 - g) * coeff) >> 4); + b = b + (((31 - b) * coeff) >> 4); + if(r > 31) + r = 31; + if(g > 31) + g = 31; + if(b > 31) + b = 31; + color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + return color; +} + +inline void gfxIncreaseBrightness(u32 *line, int coeff) +{ + for(int x = 0; x < 240; x++) { + u32 color = *line; + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r + (((31 - r) * coeff) >> 4); + g = g + (((31 - g) * coeff) >> 4); + b = b + (((31 - b) * coeff) >> 4); + if(r > 31) + r = 31; + if(g > 31) + g = 31; + if(b > 31) + b = 31; + *line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } +} + +inline u32 gfxDecreaseBrightness(u32 color, int coeff) +{ + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r - ((r * coeff) >> 4); + g = g - ((g * coeff) >> 4); + b = b - ((b * coeff) >> 4); + if(r < 0) + r = 0; + if(g < 0) + g = 0; + if(b < 0) + b = 0; + color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + + return color; +} + +inline void gfxDecreaseBrightness(u32 *line, int coeff) +{ + for(int x = 0; x < 240; x++) { + u32 color = *line; + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r - ((r * coeff) >> 4); + g = g - ((g * coeff) >> 4); + b = b - ((b * coeff) >> 4); + if(r < 0) + r = 0; + if(g < 0) + g = 0; + if(b < 0) + b = 0; + *line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } +} + +inline u32 gfxAlphaBlend(u32 color, u32 color2, int ca, int cb) +{ + if(color < 0x80000000) { + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + int r0 = (color2 & 0x1F); + int g0 = ((color2 >> 5) & 0x1F); + int b0 = ((color2 >> 10) & 0x1F); + + r = ((r * ca) >> 4) + ((r0 * cb) >> 4); + g = ((g * ca) >> 4) + ((g0 * cb) >> 4); + b = ((b * ca) >> 4) + ((b0 * cb) >> 4); + + if(r > 31) + r = 31; + if(g > 31) + g = 31; + if(b > 31) + b = 31; + + return (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } + return color; +} + +inline void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb) +{ + for(int x = 0; x < 240; x++) { + u32 color = *ta; + if(color < 0x80000000) { + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + u32 color2 = (*tb++); + int r0 = (color2 & 0x1F); + int g0 = ((color2 >> 5) & 0x1F); + int b0 = ((color2 >> 10) & 0x1F); + + r = ((r * ca) >> 4) + ((r0 * cb) >> 4); + g = ((g * ca) >> 4) + ((g0 * cb) >> 4); + b = ((b * ca) >> 4) + ((b0 * cb) >> 4); + + if(r > 31) + r = 31; + if(g > 31) + g = 31; + if(b > 31) + b = 31; + + *ta++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } else { + ta++; + tb++; + } + } +} + +#endif // VBA_GFX_H \ No newline at end of file diff --git a/src/Globals.cpp b/src/Globals.cpp new file mode 100644 index 00000000..cb863c32 --- /dev/null +++ b/src/Globals.cpp @@ -0,0 +1,135 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "Globals.h" + +reg_pair reg[45]; +memoryMap map[256]; +bool ioReadable[0x400]; +bool N_FLAG = 0; +bool C_FLAG = 0; +bool Z_FLAG = 0; +bool V_FLAG = 0; +bool armState = true; +bool armIrqEnable = true; +u32 armNextPC = 0x00000000; +int armMode = 0x1f; +u32 stop = 0x08000568; +int saveType = 0; +bool useBios = false; +bool skipBios = false; +int frameSkip = 1; +bool speedup = false; +bool synchronize = true; +bool cpuDisableSfx = false; +bool cpuIsMultiBoot = false; +bool parseDebug = true; +int layerSettings = 0xff00; +int layerEnable = 0xff00; +bool speedHack = false; +int cpuSaveType = 0; +bool cpuEnhancedDetection = true; +bool cheatsEnabled = true; + +u8 *bios = NULL; +u8 *rom = NULL; +u8 *internalRAM = NULL; +u8 *workRAM = NULL; +u8 *paletteRAM = NULL; +u8 *vram = NULL; +u8 *pix = NULL; +u8 *oam = NULL; +u8 *ioMem = NULL; + +u16 DISPCNT = 0x0080; +u16 DISPSTAT = 0x0000; +u16 VCOUNT = 0x0000; +u16 BG0CNT = 0x0000; +u16 BG1CNT = 0x0000; +u16 BG2CNT = 0x0000; +u16 BG3CNT = 0x0000; +u16 BG0HOFS = 0x0000; +u16 BG0VOFS = 0x0000; +u16 BG1HOFS = 0x0000; +u16 BG1VOFS = 0x0000; +u16 BG2HOFS = 0x0000; +u16 BG2VOFS = 0x0000; +u16 BG3HOFS = 0x0000; +u16 BG3VOFS = 0x0000; +u16 BG2PA = 0x0100; +u16 BG2PB = 0x0000; +u16 BG2PC = 0x0000; +u16 BG2PD = 0x0100; +u16 BG2X_L = 0x0000; +u16 BG2X_H = 0x0000; +u16 BG2Y_L = 0x0000; +u16 BG2Y_H = 0x0000; +u16 BG3PA = 0x0100; +u16 BG3PB = 0x0000; +u16 BG3PC = 0x0000; +u16 BG3PD = 0x0100; +u16 BG3X_L = 0x0000; +u16 BG3X_H = 0x0000; +u16 BG3Y_L = 0x0000; +u16 BG3Y_H = 0x0000; +u16 WIN0H = 0x0000; +u16 WIN1H = 0x0000; +u16 WIN0V = 0x0000; +u16 WIN1V = 0x0000; +u16 WININ = 0x0000; +u16 WINOUT = 0x0000; +u16 MOSAIC = 0x0000; +u16 BLDMOD = 0x0000; +u16 COLEV = 0x0000; +u16 COLY = 0x0000; +u16 DM0SAD_L = 0x0000; +u16 DM0SAD_H = 0x0000; +u16 DM0DAD_L = 0x0000; +u16 DM0DAD_H = 0x0000; +u16 DM0CNT_L = 0x0000; +u16 DM0CNT_H = 0x0000; +u16 DM1SAD_L = 0x0000; +u16 DM1SAD_H = 0x0000; +u16 DM1DAD_L = 0x0000; +u16 DM1DAD_H = 0x0000; +u16 DM1CNT_L = 0x0000; +u16 DM1CNT_H = 0x0000; +u16 DM2SAD_L = 0x0000; +u16 DM2SAD_H = 0x0000; +u16 DM2DAD_L = 0x0000; +u16 DM2DAD_H = 0x0000; +u16 DM2CNT_L = 0x0000; +u16 DM2CNT_H = 0x0000; +u16 DM3SAD_L = 0x0000; +u16 DM3SAD_H = 0x0000; +u16 DM3DAD_L = 0x0000; +u16 DM3DAD_H = 0x0000; +u16 DM3CNT_L = 0x0000; +u16 DM3CNT_H = 0x0000; +u16 TM0D = 0x0000; +u16 TM0CNT = 0x0000; +u16 TM1D = 0x0000; +u16 TM1CNT = 0x0000; +u16 TM2D = 0x0000; +u16 TM2CNT = 0x0000; +u16 TM3D = 0x0000; +u16 TM3CNT = 0x0000; +u16 P1 = 0xFFFF; +u16 IE = 0x0000; +u16 IF = 0x0000; +u16 IME = 0x0000; diff --git a/src/Globals.h b/src/Globals.h new file mode 100644 index 00000000..2466a3f2 --- /dev/null +++ b/src/Globals.h @@ -0,0 +1,151 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_GLOBALS_H +#define VBA_GLOBALS_H + +#include "GBA.h" + +#define VERBOSE_SWI 1 +#define VERBOSE_UNALIGNED_MEMORY 2 +#define VERBOSE_ILLEGAL_WRITE 4 +#define VERBOSE_ILLEGAL_READ 8 +#define VERBOSE_DMA0 16 +#define VERBOSE_DMA1 32 +#define VERBOSE_DMA2 64 +#define VERBOSE_DMA3 128 +#define VERBOSE_UNDEFINED 256 +#define VERBOSE_AGBPRINT 512 + +extern reg_pair reg[45]; +extern bool ioReadable[0x400]; +extern bool N_FLAG; +extern bool C_FLAG; +extern bool Z_FLAG; +extern bool V_FLAG; +extern bool armState; +extern bool armIrqEnable; +extern u32 armNextPC; +extern int armMode; +extern u32 stop; +extern int saveType; +extern bool useBios; +extern bool skipBios; +extern int frameSkip; +extern bool speedup; +extern bool synchronize; +extern bool cpuDisableSfx; +extern bool cpuIsMultiBoot; +extern bool parseDebug; +extern int layerSettings; +extern int layerEnable; +extern bool speedHack; +extern int cpuSaveType; +extern bool cpuEnhancedDetection; +extern bool cheatsEnabled; + +extern u8 *bios; +extern u8 *rom; +extern u8 *internalRAM; +extern u8 *workRAM; +extern u8 *paletteRAM; +extern u8 *vram; +extern u8 *pix; +extern u8 *oam; +extern u8 *ioMem; + +extern u16 DISPCNT; +extern u16 DISPSTAT; +extern u16 VCOUNT; +extern u16 BG0CNT; +extern u16 BG1CNT; +extern u16 BG2CNT; +extern u16 BG3CNT; +extern u16 BG0HOFS; +extern u16 BG0VOFS; +extern u16 BG1HOFS; +extern u16 BG1VOFS; +extern u16 BG2HOFS; +extern u16 BG2VOFS; +extern u16 BG3HOFS; +extern u16 BG3VOFS; +extern u16 BG2PA; +extern u16 BG2PB; +extern u16 BG2PC; +extern u16 BG2PD; +extern u16 BG2X_L; +extern u16 BG2X_H; +extern u16 BG2Y_L; +extern u16 BG2Y_H; +extern u16 BG3PA; +extern u16 BG3PB; +extern u16 BG3PC; +extern u16 BG3PD; +extern u16 BG3X_L; +extern u16 BG3X_H; +extern u16 BG3Y_L; +extern u16 BG3Y_H; +extern u16 WIN0H; +extern u16 WIN1H; +extern u16 WIN0V; +extern u16 WIN1V; +extern u16 WININ; +extern u16 WINOUT; +extern u16 MOSAIC; +extern u16 BLDMOD; +extern u16 COLEV; +extern u16 COLY; +extern u16 DM0SAD_L; +extern u16 DM0SAD_H; +extern u16 DM0DAD_L; +extern u16 DM0DAD_H; +extern u16 DM0CNT_L; +extern u16 DM0CNT_H; +extern u16 DM1SAD_L; +extern u16 DM1SAD_H; +extern u16 DM1DAD_L; +extern u16 DM1DAD_H; +extern u16 DM1CNT_L; +extern u16 DM1CNT_H; +extern u16 DM2SAD_L; +extern u16 DM2SAD_H; +extern u16 DM2DAD_L; +extern u16 DM2DAD_H; +extern u16 DM2CNT_L; +extern u16 DM2CNT_H; +extern u16 DM3SAD_L; +extern u16 DM3SAD_H; +extern u16 DM3DAD_L; +extern u16 DM3DAD_H; +extern u16 DM3CNT_L; +extern u16 DM3CNT_H; +extern u16 TM0D; +extern u16 TM0CNT; +extern u16 TM1D; +extern u16 TM1CNT; +extern u16 TM2D; +extern u16 TM2CNT; +extern u16 TM3D; +extern u16 TM3CNT; +extern u16 P1; +extern u16 IE; +extern u16 IF; +extern u16 IME; + +#endif // VBA_GLOBALS_H diff --git a/src/Link.cpp b/src/Link.cpp new file mode 100644 index 00000000..c82d10b8 --- /dev/null +++ b/src/Link.cpp @@ -0,0 +1,1057 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team +// This file was written by denopqrihg + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Link.cpp : Emulation of GBA link accessories +// + +#include "GBA.h" +#include +#include "win32/stdafx.h" +#include "port.h" +#include "Link.h" +#include "win32/vba.h" +#include "win32/MainWnd.h" +#include "win32/LinkOptions.h" +#include "win32/Reg.h" + +#define UPDATE_REG(address, value) WRITE16LE(((u16 *)&ioMem[address]),value) +#define GBLINK_READY 8 + +int linktime = 0; +u8 tspeed=3; +u8 transfer=0; +LINKDATA *linkmem=NULL; +int linkid = 1, vbaid = 1; +HANDLE linksync[4]; +int savedlinktime=0; +char inifile[] = "vba1.ini"; +HANDLE mmf=NULL; +char linkevent[] = "VBA link event "; +static int i, j; +int linktimeout = 1000; +int linklog = 0; +FILE *jjj = NULL; +LANLINKDATA lanlink; +u16 linkdata[4]; +int lspeed = 0; +lserver ls; +lclient lc; +bool oncewait = false, after = false; +bool adapter = true; +u8 rfu_cmd, rfu_qsend, rfu_qrecv; +int rfu_state, rfu_polarity, linktime2, counter, rfu_masterq; +int transferend, numtransfers = 0; +u32 rfu_masterdata[32]; + +extern unsigned char *gbMemory; +extern int gbInterrupt; + +int trtimedata[4][4] = {{34080, 8520, 5680, 2840}, {65536, 16384, 10923, 5461}, {99609, 24903, 16602, 8301}, {133692, 33423, 22282, 11141}}; +int trtimeend[3][4] = {{72527, 18132, 12088, 6044}, {106608, 26652, 17768, 8884}, {133692, 33423, 22282, 11141}}; +int gbtime = 1024; + +DWORD WINAPI LinkClientThread(void *); +DWORD WINAPI LinkServerThread(void *); +int StartServer(void); +int GetSioMode(u16, u16); +u16 StartRFU(u16); + +void StartLink(WORD value){ + if(ioMem==NULL) return; + if(adapter){ + UPDATE_REG(0x128, StartRFU(value)); + return; + } + switch(GetSioMode(value, READ16LE(&ioMem[0x134]))){ + case MULTIPLAYER: + if(value & 0x80){ + if(!linkid){ + if(!transfer){ + if(lanlink.active){ + if(lanlink.connected){ + linkdata[0] = READ16LE(&ioMem[0x12a]); + savedlinktime = linktime; + tspeed = value & 3; + ls.Send(); + transfer = 1; + linktime = 0; + UPDATE_REG(0x120, linkdata[0]); + UPDATE_REG(0x122, 0xffff); + WRITE32LE(&ioMem[0x124], 0xffffffff); + if(lanlink.speed&&oncewait==false) ls.howmanytimes++; + after = false; + } + } else if(linkmem->numgbas>1){ + ResetEvent(linksync[0]); + linkmem->linkcmd[0] = ('M'<<8)+(value&3); + linkmem->linkdata[0] = READ16LE(&ioMem[0x12a]); + if(linkmem->numtransfers!=0) linkmem->lastlinktime = linktime; + else linkmem->lastlinktime = 0; + if((++linkmem->numtransfers)==0) linkmem->numtransfers=2; + transfer = 1; + linktime = 0; + tspeed = value & 3; + WRITE32LE(&ioMem[0x120], 0xffffffff); + WRITE32LE(&ioMem[0x124], 0xffffffff); + } + } + } + value &= 0xff7f; + value |= (transfer!=0)<<7; + } + value &= 0xff8b; + value |= (linkid ? 0xc : 8); + value |= linkid<<4; + UPDATE_REG(0x128, value); + if(linkid) UPDATE_REG(0x134, 7); + else UPDATE_REG(0x134, 3); + break; + case NORMAL8: + if(linklog) fprintf(jjj, "Attempt to use 8-bit Normal mode %04x\n", value); + UPDATE_REG(0x128, value); + break; + case NORMAL32: + if(linklog) fprintf(jjj, "Attempt to use 32-bit Normal mode %04x %x%x\n", value, READ16LE(&ioMem[0x122]), READ16LE(&ioMem[0x120])); + UPDATE_REG(0x128, value); + break; + case UART: + if(linklog) fprintf(jjj, "Attempt to use UART mode %04x\n", value); + UPDATE_REG(0x128, value); + break; + default: + UPDATE_REG(0x128, value); + break; + } +} + +void StartGPLink(u16 value){ + if(!value){ + UPDATE_REG(0x134, 0); + return; + } + switch(GetSioMode(READ16LE(&ioMem[0x128]), value)){ + case MULTIPLAYER: + value &= 0xc0f0; + value |= 3; + if(linkid) value |= 4; + UPDATE_REG(0x134, value); + UPDATE_REG(0x128, ((READ16LE(&ioMem[0x128])&0xff8b)|(linkid ? 0xc : 8)|(linkid<<4))); + return; + break; + case GP: + if(linklog){ + if(value==0x8000) fprintf(jjj, "Circuit reset\n"); + else if(!adapter) fprintf(jjj, "Attempt to use General-purpose mode %04x\n", value); + } + if(adapter) rfu_state = RFU_INIT; + break; + case JOYBUS: + UPDATE_REG(0x134, value); + break; + default: + UPDATE_REG(0x134, value); + break; + } + return; +} + +void StartJOYLink(u16 value){ + if(!value){ + UPDATE_REG(0x140, 0); + return; + } + if(GetSioMode(READ16LE(&ioMem[0x128]), READ16LE(&ioMem[0x134]))==JOYBUS&&linklog) fprintf(jjj, "Attempt to use JOY-BUS mode %04x\n", value); + return; +} + +void LinkUpdate(int ticks){ + linktime += ticks; + if(adapter){ + linktime2 += ticks; + transferend -= ticks; + if(transfer&&transferend<=0){ + transfer = 0; + if(READ16LE(&ioMem[0x128])&0x4000){ + IF |= 0x80; + UPDATE_REG(0x202, IF); + } + UPDATE_REG(0x128, READ16LE(&ioMem[0x128]) & 0xff7f); + } + return; + } + + if(lanlink.active){ + if(lanlink.connected){ + if(after){ + if(linkid&&linktime>6044){ + lc.Recv(); + oncewait = true; + } else return; + } + if(linkid&&!transfer&&lc.numtransfers>0&&linktime>=savedlinktime){ + linkdata[linkid] = READ16LE(&ioMem[0x12a]); + if(!lc.oncesend) lc.Send(); + lc.oncesend = false; + UPDATE_REG(0x120, linkdata[0]); + UPDATE_REG(0x128, READ16LE(&ioMem[0x128]) | 0x80); + transfer = 1; + if(lc.numtransfers==1) linktime = 0; + else linktime -= savedlinktime; + } + if(transfer&&linktime>=trtimeend[lanlink.numgbas-1][tspeed]){ + if(READ16LE(&ioMem[0x128]) & 0x4000){ + IF |= 0x80; + UPDATE_REG(0x202, IF); + } + UPDATE_REG(0x128, (READ16LE(&ioMem[0x128]) & 0xff0f) | (linkid << 4)); + transfer = 0; + linktime -= trtimeend[lanlink.numgbas-1][tspeed]; + oncewait = false; + if(!lanlink.speed){ + if(linkid) lc.Recv(); + else ls.Recv(); + UPDATE_REG(0x122, linkdata[1]); + UPDATE_REG(0x124, linkdata[2]); + UPDATE_REG(0x126, linkdata[3]); + if(linklog) fprintf(jjj, "%04x %04x %04x %04x %10u\n", linkdata[0], linkdata[1], linkdata[2], linkdata[3], savedlinktime); + oncewait = true; + } else { + after = true; + if(lanlink.numgbas==1){ + UPDATE_REG(0x122, linkdata[1]); + UPDATE_REG(0x124, linkdata[2]); + UPDATE_REG(0x126, linkdata[3]); + if(linklog) fprintf(jjj, "%04x %04x %04x %04x %10u\n", linkdata[0], linkdata[1], linkdata[2], linkdata[3], savedlinktime); + } + + } + } + } + return; + } + + if(linkid&&!transfer&&linktime>=linkmem->lastlinktime&&linkmem->numtransfers){ + linkmem->linkdata[linkid] = READ16LE(&ioMem[0x12a]); + + if(linkmem->numtransfers==1){ + linktime = 0; + if(WaitForSingleObject(linksync[linkid], linktimeout)==WAIT_TIMEOUT) linkmem->numtransfers=0; + } else linktime -= linkmem->lastlinktime; + + switch((linkmem->linkcmd[0])>>8){ + case 'M': + tspeed = (linkmem->linkcmd[0]) & 3; + transfer = 1; + WRITE32LE(&ioMem[0x120], 0xffffffff); + WRITE32LE(&ioMem[0x124], 0xffffffff); + UPDATE_REG(0x128, READ16LE(&ioMem[0x128]) | 0x80); + break; + } + } + + if(!transfer) return; + + if(transfer&&linktime>=trtimedata[transfer-1][tspeed]&&transfer<=linkmem->numgbas){ + if(transfer-linkid==2){ + SetEvent(linksync[linkid+1]); + if(WaitForSingleObject(linksync[linkid], linktimeout)==WAIT_TIMEOUT) + linkmem->numtransfers=0; + ResetEvent(linksync[linkid]); + if(linklog) fprintf(jjj, "%04x %04x %04x %04x %10u\n", + linkmem->linkdata[0], linkmem->linkdata[1], linkmem->linkdata[2], linkmem->linkdata[3], linkmem->lastlinktime); + } + + + UPDATE_REG(0x11e + (transfer<<1), linkmem->linkdata[transfer-1]); + transfer++; + } + + if(transfer&&linktime>=trtimeend[linkmem->numgbas-2][tspeed]){ + if(linkid==linkmem->numgbas-1){ + SetEvent(linksync[0]); + if(WaitForSingleObject(linksync[linkid], linktimeout)==WAIT_TIMEOUT) + linkmem->numtransfers=0; + ResetEvent(linksync[linkid]); + if(linklog) fprintf(jjj, "%04x %04x %04x %04x %10u\n", + linkmem->linkdata[0], linkmem->linkdata[1], linkmem->linkdata[2], linkmem->linkdata[3], linkmem->lastlinktime); + } + transfer = 0; + linktime -= trtimeend[0][tspeed]; + if(READ16LE(&ioMem[0x128]) & 0x4000){ + IF |= 0x80; + UPDATE_REG(0x202, IF); + } + UPDATE_REG(0x128, (READ16LE(&ioMem[0x128]) & 0xff0f) | (linkid << 4)); + linkmem->linkdata[linkid] = 0xffff; + } + + return; +} + +inline int GetSioMode(u16 reg1, u16 reg2){ + if(!(reg2&0x8000)){ + switch(reg1&0x3000){ + case 0x0000: + return NORMAL8; + case 0x1000: + return NORMAL32; + case 0x2000: + return MULTIPLAYER; + case 0x3000: + return UART; + } + } + if(reg2&0x4000) return JOYBUS; + return GP; +} + +u16 StartRFU(u16 value){ + switch(GetSioMode(value, READ16LE(&ioMem[0x134]))){ + case NORMAL8: + rfu_polarity = 0; + return value; + break; + case NORMAL32: + if(value&8) value &= 0xfffb; // A kind of acknowledge procedure + else value |= 4; + if(value&0x80){ + if((value&3)==1) transferend = 2048; + else transferend = 256; + u16 a = READ16LE(&ioMem[0x122]); + switch(rfu_state){ + case RFU_INIT: + if(READ32LE(&ioMem[0x120])==0xb0bb8001){ + rfu_state = RFU_COMM; // end of startup + } + UPDATE_REG(0x122, READ16LE(&ioMem[0x120])); + UPDATE_REG(0x120, a); + break; + case RFU_COMM: + if(a==0x9966){ + rfu_cmd = ioMem[0x120]; + if((rfu_qsend=ioMem[0x121])!=0){ + rfu_state = RFU_SEND; + counter = 0; + } + if(rfu_cmd==0x25||rfu_cmd==0x24){ + linkmem->rfu_q[vbaid] = rfu_qsend; + } + UPDATE_REG(0x120, 0); + UPDATE_REG(0x122, 0x8000); + } else if(a==0x8000){ + switch(rfu_cmd){ + case 0x1a: // check if someone joined + if(linkmem->rfu_request[vbaid]!=0){ + rfu_state = RFU_RECV; + rfu_qrecv = 1; + } + linkid = -1; + rfu_cmd |= 0x80; + break; + case 0x1e: // receive broadcast data + case 0x1d: // no visible difference + rfu_polarity = 0; + rfu_state = RFU_RECV; + rfu_qrecv = 7; + counter = 0; + rfu_cmd |= 0x80; + break; + case 0x30: + linkmem->rfu_request[vbaid] = 0; + linkmem->rfu_q[vbaid] = 0; + linkid = 0; + numtransfers = 0; + rfu_cmd |= 0x80; + if(linkmem->numgbas==2) SetEvent(linksync[1-vbaid]); + break; + case 0x11: // ? always receives 0xff - I suspect it's something for 3+ players + case 0x13: // unknown + case 0x20: // this has something to do with 0x1f + case 0x21: // this too + rfu_cmd |= 0x80; + rfu_polarity = 0; + rfu_state = 3; + rfu_qrecv = 1; + break; + case 0x26: + if(linkid>0){ + rfu_qrecv = rfu_masterq; + } + if((rfu_qrecv=linkmem->rfu_q[1-vbaid])!=0){ + rfu_state = RFU_RECV; + counter = 0; + } + rfu_cmd |= 0x80; + break; + case 0x24: // send data + if((numtransfers++)==0) linktime = 1; + linkmem->rfu_linktime[vbaid] = linktime; + if(linkmem->numgbas==2){ + SetEvent(linksync[1-vbaid]); + WaitForSingleObject(linksync[vbaid], linktimeout); + ResetEvent(linksync[vbaid]); + } + rfu_cmd |= 0x80; + linktime = 0; + linkid = -1; + break; + case 0x25: // send & wait for data + case 0x1f: // pick a server + case 0x10: // init + case 0x16: // send broadcast data + case 0x17: // setup or something ? + case 0x27: // wait for data ? + case 0x3d: // init + default: + rfu_cmd |= 0x80; + break; + case 0xa5: // 2nd part of send&wait function 0x25 + case 0xa7: // 2nd part of wait function 0x27 + if(linkid==-1){ + linkid++; + linkmem->rfu_linktime[vbaid] = 0; + } + if(linkid&&linkmem->rfu_request[1-vbaid]==0){ + linkmem->rfu_q[1-vbaid] = 0; + transferend = 256; + rfu_polarity = 1; + rfu_cmd = 0x29; + linktime = 0; + break; + } + if((numtransfers++)==0) linktime = 0; + linkmem->rfu_linktime[vbaid] = linktime; + if(linkmem->numgbas==2){ + if(!linkid||(linkid&&numtransfers)) SetEvent(linksync[1-vbaid]); + WaitForSingleObject(linksync[vbaid], linktimeout); + ResetEvent(linksync[vbaid]); + } + if(linkid>0){ + memcpy(rfu_masterdata, linkmem->rfu_data[1-vbaid], 128); + rfu_masterq = linkmem->rfu_q[1-vbaid]; + } + transferend = linkmem->rfu_linktime[1-vbaid] - linktime + 256; + if(transferend<256) transferend = 256; + linktime = -transferend; + rfu_polarity = 1; + rfu_cmd = 0x28; + break; + } + UPDATE_REG(0x122, 0x9966); + UPDATE_REG(0x120, (rfu_qrecv<<8) | rfu_cmd); + } else { + UPDATE_REG(0x120, 0); + UPDATE_REG(0x122, 0x8000); + } + break; + case RFU_SEND: + if(--rfu_qsend==0) rfu_state = RFU_COMM; + switch(rfu_cmd){ + case 0x16: + linkmem->rfu_bdata[vbaid][counter++] = READ32LE(&ioMem[0x120]); + break; + case 0x17: + linkid = 1; + break; + case 0x1f: + linkmem->rfu_request[1-vbaid] = 1; + break; + case 0x24: + case 0x25: + linkmem->rfu_data[vbaid][counter++] = READ32LE(&ioMem[0x120]); + break; + } + UPDATE_REG(0x120, 0); + UPDATE_REG(0x122, 0x8000); + break; + case RFU_RECV: + if(--rfu_qrecv==0) rfu_state = RFU_COMM; + switch(rfu_cmd){ + case 0x9d: + case 0x9e: + if(counter==0){ + UPDATE_REG(0x120, 0x61f1); + UPDATE_REG(0x122, 0); + counter++; + break; + } + UPDATE_REG(0x120, linkmem->rfu_bdata[1-vbaid][counter-1]&0xffff); + UPDATE_REG(0x122, linkmem->rfu_bdata[1-vbaid][counter-1]>>16); + counter++; + break; + case 0xa6: + if(linkid>0){ + UPDATE_REG(0x120, rfu_masterdata[counter]&0xffff); + UPDATE_REG(0x122, rfu_masterdata[counter++]>>16); + } else { + UPDATE_REG(0x120, linkmem->rfu_data[1-vbaid][counter]&0xffff); + UPDATE_REG(0x122, linkmem->rfu_data[1-vbaid][counter++]>>16); + } + break; + case 0x93: // it seems like the game doesn't care about this value + UPDATE_REG(0x120, 0x1234); // put anything in here + UPDATE_REG(0x122, 0x0200); // also here, but it should be 0200 + break; + case 0xa0: + case 0xa1: + UPDATE_REG(0x120, 0x641b); + UPDATE_REG(0x122, 0x0000); + break; + case 0x9a: + UPDATE_REG(0x120, 0x61f9); + UPDATE_REG(0x122, 0); + break; + case 0x91: + UPDATE_REG(0x120, 0x00ff); + UPDATE_REG(0x122, 0x0000); + break; + default: + UPDATE_REG(0x120, 0x0173); + UPDATE_REG(0x122, 0x0000); + break; + } + break; + } + transfer = 1; + } + if(rfu_polarity) value ^= 4; // sometimes it's the other way around + default: + return value; + } +} + +void gbLinkStart(u8 value){ +// Not in this version :-) +} + + +void gbLinkUpdate(void){ +} + +int InitLink(void){ + WSADATA wsadata; + BOOL disable = true; + + linkid = 0; + inifile[3]='1'; + + if(WSAStartup(MAKEWORD(1,1), &wsadata)!=0){ + WSACleanup(); + return 0; + } + + if((lanlink.tcpsocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET){ + MessageBox(NULL, "Couldn't create socket.", "Error!", MB_OK); + WSACleanup(); + return 0; + } + + setsockopt(lanlink.tcpsocket, IPPROTO_TCP, TCP_NODELAY, (char*)&disable, sizeof(BOOL)); + + if((mmf=CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READWRITE, 0, sizeof(LINKDATA), "VBA link memory"))==NULL){ + closesocket(lanlink.tcpsocket); + WSACleanup(); + MessageBox(NULL, "Error creating file mapping", "Error", MB_OK|MB_ICONEXCLAMATION); + return 0; + } + + if(GetLastError() == ERROR_ALREADY_EXISTS) + vbaid = 1; + else + vbaid = 0; + + if((linkmem=(LINKDATA *)MapViewOfFile(mmf, FILE_MAP_WRITE, 0, 0, sizeof(LINKDATA)))==NULL){ + closesocket(lanlink.tcpsocket); + WSACleanup(); + CloseHandle(mmf); + MessageBox(NULL, "Error mapping file", "Error", MB_OK|MB_ICONEXCLAMATION); + return 0; + } + + if(linkmem->linkflags&LINK_PARENTLOST) + vbaid = 0; + + if(vbaid==0){ + inifile[3]='1'; + linkid = 0; + if(linkmem->linkflags&LINK_PARENTLOST){ + linkmem->numgbas++; + linkmem->linkflags &= ~LINK_PARENTLOST; + } + else + linkmem->numgbas=1; + + for(i=0;i<4;i++){ + linkevent[15]=(char)i+'1'; + if((linksync[i]=CreateEvent(NULL, true, false, linkevent))==NULL){ + closesocket(lanlink.tcpsocket); + WSACleanup(); + UnmapViewOfFile(linkmem); + CloseHandle(mmf); + for(j=0;jnumgbas; + linkid = vbaid; + linkmem->numgbas++; + linklog = 0; + if(linkmem->numgbas>4){ + linkmem->numgbas=4; + closesocket(lanlink.tcpsocket); + WSACleanup(); + MessageBox(NULL, "5 or more GBAs not supported.", "Error!", MB_OK|MB_ICONEXCLAMATION); + UnmapViewOfFile(linkmem); + CloseHandle(mmf); + return 0; + } + inifile[3]=(char)linkmem->numgbas+'0'; + for(i=0;i<4;i++){ + linkevent[15]=(char)i+'1'; + if((linksync[i]=OpenEvent(EVENT_ALL_ACCESS, false, linkevent))==NULL){ + closesocket(lanlink.tcpsocket); + WSACleanup(); + CloseHandle(mmf); + UnmapViewOfFile(linkmem); + for(j=0;jlastlinktime=0xffffffff; + linkmem->numtransfers=0; + linkmem->linkflags=0; + lanlink.connected = false; + lanlink.thread = NULL; + lanlink.speed = false; + for(i=0;i<4;i++){ + linkmem->linkdata[i] = 0xffff; + linkdata[i] = 0xffff; + } +return 1; +} + +int openLinkLog(void){ + char filename[20]; + if(linklog){ + sprintf(filename, "vbalog%1d.txt", vbaid+1); + if((jjj=fopen(filename, "wt"))==NULL){ + return 0; + } + fprintf(jjj, "GBA0 GBA1 GBA2 GBA3 clocks between transfers\n"); + } + return 1; +} + +void CloseLink(void){ + if(lanlink.connected){ + if(linkid){ + char outbuffer[4]; + outbuffer[0] = 4; + outbuffer[1] = -32; + if(lanlink.type==0) send(lanlink.tcpsocket, outbuffer, 4, 0); + } else { + char outbuffer[12]; + int i; + outbuffer[0] = 12; + outbuffer[1] = -32; + for(i=1;i<=lanlink.numgbas;i++){ + if(lanlink.type==0){ + send(ls.tcpsocket[i], outbuffer, 12, 0); + } + closesocket(ls.tcpsocket[i]); + } + } + } + linkmem->numgbas--; + if(!linkid&&linkmem->numgbas!=0) + linkmem->linkflags|=LINK_PARENTLOST; + CloseHandle(mmf); + UnmapViewOfFile(linkmem); + + for(i=0;i<4;i++){ + if(linksync[i]!=NULL){ + PulseEvent(linksync[i]); + CloseHandle(linksync[i]); + } + } + regSetDwordValue("LAN", lanlink.active); + if(linklog) fclose(jjj); + closesocket(lanlink.tcpsocket); + WSACleanup(); +return; +} + +lserver::lserver(void){ + intinbuffer = (int*)inbuffer; + u16inbuffer = (u16*)inbuffer; + intoutbuffer = (int*)outbuffer; + u16outbuffer = (u16*)outbuffer; + oncewait = false; +} + +int lserver::Init(void *serverdlg){ + SOCKADDR_IN info; + DWORD nothing; + char str[100]; + + info.sin_family = AF_INET; + info.sin_addr.S_un.S_addr = INADDR_ANY; + info.sin_port = htons(5738); + + if(bind(lanlink.tcpsocket, (LPSOCKADDR)&info, sizeof(SOCKADDR_IN))==SOCKET_ERROR){ + closesocket(lanlink.tcpsocket); + if((lanlink.tcpsocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET) + return WSAGetLastError(); + if(bind(lanlink.tcpsocket, (LPSOCKADDR)&info, sizeof(SOCKADDR_IN))==SOCKET_ERROR) + return WSAGetLastError(); + } + + if(listen(lanlink.tcpsocket, lanlink.numgbas)==SOCKET_ERROR) + return WSAGetLastError(); + + if(lanlink.thread!=NULL){ + lanlink.terminate = true; + WaitForSingleObject(linksync[vbaid], 500); + lanlink.thread = NULL; + } + lanlink.terminate = false; + linkid = 0; + + gethostname(str, 100); + ((ServerWait*)serverdlg)->m_serveraddress.Format("Server IP address is: %s", inet_ntoa(*(LPIN_ADDR)(gethostbyname(str)->h_addr_list[0]))); + + lanlink.thread = CreateThread(NULL, 0, LinkServerThread, serverdlg, 0, ¬hing); + + return 0; + +} + +DWORD WINAPI LinkServerThread(void *serverdlg){ + fd_set fdset; + timeval wsocktimeout; + char inbuffer[256], outbuffer[256]; + int *intinbuffer = (int*)inbuffer; + u16 *u16inbuffer = (u16*)inbuffer; + int *intoutbuffer = (int*)outbuffer; + u16 *u16outbuffer = (u16*)outbuffer; + BOOL disable = true; + + wsocktimeout.tv_sec = 1; + wsocktimeout.tv_usec = 0; + i = 0; + + while(im_plconn[i].Format("Player %d connected", i+1); + ((ServerWait*)serverdlg)->UpdateData(false); + i++; + } + } + ((ServerWait*)serverdlg)->m_prgctrl.StepIt(); + } + MessageBox(NULL, "All players connected", "Link", MB_OK); + ((ServerWait*)serverdlg)->SendMessage(WM_CLOSE, 0, 0); + + for(i=1;i<=lanlink.numgbas;i++){ + outbuffer[0] = 4; + send(ls.tcpsocket[i], outbuffer, 4, 0); + } + + lanlink.connected = true; + + return 0; +} + +void lserver::Send(void){ + if(lanlink.type==0){ // TCP + if(savedlinktime==-1){ + outbuffer[0] = 4; + outbuffer[1] = -32; //0xe0 + for(i=1;i<=lanlink.numgbas;i++){ + send(tcpsocket[i], outbuffer, 4, 0); + recv(tcpsocket[i], inbuffer, 4, 0); + } + } + outbuffer[1] = tspeed; + u16outbuffer[1] = linkdata[0]; + intoutbuffer[1] = savedlinktime; + if(lanlink.numgbas==1){ + if(lanlink.type==0){ + outbuffer[0] = 8; + send(tcpsocket[1], outbuffer, 8, 0); + } + } + else if(lanlink.numgbas==2){ + u16outbuffer[4] = linkdata[2]; + if(lanlink.type==0){ + outbuffer[0] = 10; + send(tcpsocket[1], outbuffer, 10, 0); + u16outbuffer[4] = linkdata[1]; + send(tcpsocket[2], outbuffer, 10, 0); + } + } else { + if(lanlink.type==0){ + outbuffer[0] = 12; + u16outbuffer[4] = linkdata[2]; + u16outbuffer[5] = linkdata[3]; + send(tcpsocket[1], outbuffer, 12, 0); + u16outbuffer[4] = linkdata[1]; + send(tcpsocket[2], outbuffer, 12, 0); + u16outbuffer[5] = linkdata[2]; + send(tcpsocket[3], outbuffer, 12, 0); + } + } + } + return; +} + +void lserver::Recv(void){ + int numbytes; + if(lanlink.type==0){ // TCP + wsocktimeout.tv_usec = 0; + wsocktimeout.tv_sec = linktimeout / 1000; + fdset.fd_count = lanlink.numgbas; + for(i=0;i1) memcpy(inbuffer, inbuffer+inbuffer[0]*(howmanytimes-1), inbuffer[0]); + if(inbuffer[1]==-32){ + char message[30]; + lanlink.connected = false; + sprintf(message, "Player %d disconnected.", i+2); + MessageBox(NULL, message, "Link", MB_OK); + outbuffer[0] = 4; + outbuffer[1] = -32; + for(i=1;ih_addr_list); + + if(ioctlsocket(lanlink.tcpsocket, FIONBIO, ¬block)==SOCKET_ERROR) + return WSAGetLastError(); + + if(lanlink.thread!=NULL){ + lanlink.terminate = true; + WaitForSingleObject(linksync[vbaid], 500); + lanlink.thread = NULL; + } + + ((ServerWait*)waitdlg)->SetWindowText("Connecting..."); + lanlink.terminate = false; + lanlink.thread = CreateThread(NULL, 0, LinkClientThread, waitdlg, 0, ¬hing); + return 0; +} + +DWORD WINAPI LinkClientThread(void *waitdlg){ + fd_set fdset; + timeval wsocktimeout; + int numbytes; + char inbuffer[16]; + u16 *u16inbuffer = (u16*)inbuffer; + unsigned long block = 0; + + if(connect(lanlink.tcpsocket, (LPSOCKADDR)&lc.serverinfo, sizeof(SOCKADDR_IN))==SOCKET_ERROR){ + if(WSAGetLastError()!=WSAEWOULDBLOCK){ + MessageBox(NULL, "Couldn't connect to server.", "Link", MB_OK); + return 1; + } + wsocktimeout.tv_sec = 1; + wsocktimeout.tv_usec = 0; + do{ + if(lanlink.terminate) return 0; + fdset.fd_count = 1; + fdset.fd_array[0] = lanlink.tcpsocket; + ((ServerWait*)waitdlg)->m_prgctrl.StepIt(); + } while(select(0, NULL, &fdset, NULL, &wsocktimeout)!=1&&connect(lanlink.tcpsocket, (LPSOCKADDR)&lc.serverinfo, sizeof(SOCKADDR_IN))!=0); + } + + ioctlsocket(lanlink.tcpsocket, FIONBIO, &block); + + numbytes = 0; + while(numbytes<4) + numbytes += recv(lanlink.tcpsocket, inbuffer+numbytes, 16, 0); + linkid = (int)u16inbuffer[0]; + lanlink.numgbas = (int)u16inbuffer[1]; + + ((ServerWait*)waitdlg)->m_serveraddress.Format("Connected as #%d", linkid+1); + if(lanlink.numgbas!=linkid) ((ServerWait*)waitdlg)->m_plconn[0].Format("Waiting for %d players to join", lanlink.numgbas-linkid); + else ((ServerWait*)waitdlg)->m_plconn[0].Format("All players joined."); + + numbytes = 0; + inbuffer[0] = 1; + while(numbytesSendMessage(WM_CLOSE, 0, 0); + + block = 1; + + ioctlsocket(lanlink.tcpsocket, FIONBIO, &block); + + lanlink.connected = true; + return 0; +} + +void lclient::CheckConn(void){ + if((numbytes=recv(lanlink.tcpsocket, inbuffer, 256, 0))>0){ + while(numbytes + +#ifndef LINKH +#define LINKH +#define LINK_PARENTLOST 0x80 +#define UNSUPPORTED -1 +#define MULTIPLAYER 0 +#define NORMAL8 1 +#define NORMAL32 2 +#define UART 3 +#define JOYBUS 4 +#define GP 5 +#define RFU_INIT 0 +#define RFU_COMM 1 +#define RFU_SEND 2 +#define RFU_RECV 3 + +typedef struct { + WORD linkdata[4]; + WORD linkcmd[4]; + WORD numtransfers; + int lastlinktime; + unsigned char numgbas; + unsigned char linkflags; + int rfu_q[4]; + u8 rfu_request[4]; + int rfu_linktime[4]; + u32 rfu_bdata[4][7]; + u32 rfu_data[4][32]; +} LINKDATA; + +class lserver{ + int numbytes; + fd_set fdset; + timeval wsocktimeout; + //timeval udptimeout; + char inbuffer[256], outbuffer[256]; + int *intinbuffer; + u16 *u16inbuffer; + int *intoutbuffer; + u16 *u16outbuffer; + int counter; + int done; +public: + int howmanytimes; + SOCKET tcpsocket[4]; + SOCKADDR_IN udpaddr[4]; + lserver(void); + int Init(void*); + void Send(void); + void Recv(void); +}; + +class lclient{ + fd_set fdset; + timeval wsocktimeout; + char inbuffer[256], outbuffer[256]; + int *intinbuffer; + u16 *u16inbuffer; + int *intoutbuffer; + u16 *u16outbuffer; + int numbytes; +public: + bool oncesend; + SOCKADDR_IN serverinfo; + SOCKET noblock; + int numtransfers; + lclient(void); + int Init(LPHOSTENT, void*); + void Send(void); + void Recv(void); + void CheckConn(void); +}; + +typedef struct { + SOCKET tcpsocket; + //SOCKET udpsocket; + int numgbas; + HANDLE thread; + u8 type; + u8 server; + bool terminate; + bool connected; + bool speed; + bool active; +} LANLINKDATA; + +extern void LinkUpdate(void); +extern void LinkChildStop(void); +extern void LinkChildSend(u16); +extern int openLinkLog(void); +extern void CloseLanLink(void); + +extern LANLINKDATA lanlink; +extern FILE *jjj; +extern int vbaid; +extern int linklog; +extern bool adapter; +extern int linktimeout; +extern lclient lc; +extern int linkid; + +#endif diff --git a/src/Mode0.cpp b/src/Mode0.cpp new file mode 100644 index 00000000..5d30d7fc --- /dev/null +++ b/src/Mode0.cpp @@ -0,0 +1,552 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Gfx.h" + +void mode0RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + return; + } + + if(layerEnable & 0x0100) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if(layerEnable & 0x0200) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if(layerEnable & 0x0400) { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if(layerEnable & 0x0800) { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line0[x] < color) { + color = line0[x]; + top = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(color >> 24)) { + color = line1[x]; + top = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(color >> 24)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(color >> 24)) { + color = line3[x]; + top = 0x08; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line0[x]>>24) < (u8)(back >> 24)) { + back = line0[x]; + top2 = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(back >> 24)) { + back = line1[x]; + top2 = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(back >> 24)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} + +void mode0RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + return; + } + + if(layerEnable & 0x0100) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if(layerEnable & 0x0200) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if(layerEnable & 0x0400) { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if(layerEnable & 0x0800) { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + int effect = (BLDMOD >> 6) & 3; + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line0[x] < color) { + color = line0[x]; + top = 0x01; + } + + if(line1[x] < (color & 0xFF000000)) { + color = line1[x]; + top = 0x02; + } + + if(line2[x] < (color & 0xFF000000)) { + color = line2[x]; + top = 0x04; + } + + if(line3[x] < (color & 0xFF000000)) { + color = line3[x]; + top = 0x08; + } + + if(lineOBJ[x] < (color & 0xFF000000)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch(effect) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + if(line0[x] < back) { + if(top != 0x01) { + back = line0[x]; + top2 = 0x01; + } + } + + if(line1[x] < (back & 0xFF000000)) { + if(top != 0x02) { + back = line1[x]; + top2 = 0x02; + } + } + + if(line2[x] < (back & 0xFF000000)) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if(line3[x] < (back & 0xFF000000)) { + if(top != 0x08) { + back = line3[x]; + top2 = 0x08; + } + } + + if(lineOBJ[x] < (back & 0xFF000000)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if(line0[x] < back) { + back = line0[x]; + top2 = 0x01; + } + + if(line1[x] < (back & 0xFF000000)) { + back = line1[x]; + top2 = 0x02; + } + + if(line2[x] < (back & 0xFF000000)) { + back = line2[x]; + top2 = 0x04; + } + + if(line3[x] < (back & 0xFF000000)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} + +void mode0RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if((layerEnable & 0x0100)) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if((layerEnable & 0x0200)) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if((layerEnable & 0x0400)) { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if((layerEnable & 0x0800)) { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if((mask & 1) && (line0[x] < color)) { + color = line0[x]; + top = 0x01; + } + + if((mask & 2) && ((u8)(line1[x]>>24) < (u8)(color >> 24))) { + color = line1[x]; + top = 0x02; + } + + if((mask & 4) && ((u8)(line2[x]>>24) < (u8)(color >> 24))) { + color = line2[x]; + top = 0x04; + } + + if((mask & 8) && ((u8)(line3[x]>>24) < (u8)(color >> 24))) { + color = line3[x]; + top = 0x08; + } + + if((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24))) { + color = lineOBJ[x]; + top = 0x10; + } + + // special FX on in the window + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + if((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) { + if(top != 0x01) { + back = line0[x]; + top2 = 0x01; + } + } + + if((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) { + if(top != 0x02) { + back = line1[x]; + top2 = 0x02; + } + } + + if((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) { + if(top != 0x08) { + back = line3[x]; + top2 = 0x08; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 1) && ((u8)(line0[x]>>24) < (u8)(back >> 24))) { + back = line0[x]; + top2 = 0x01; + } + + if((mask & 2) && ((u8)(line1[x]>>24) < (u8)(back >> 24))) { + back = line1[x]; + top2 = 0x02; + } + + if((mask & 4) && ((u8)(line2[x]>>24) < (u8)(back >> 24))) { + back = line2[x]; + top2 = 0x04; + } + + if((mask & 8) && ((u8)(line3[x]>>24) < (u8)(back >> 24))) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 1) && ((u8)(line0[x]>>24) < (u8)(back >> 24))) { + back = line0[x]; + top2 = 0x01; + } + + if((mask & 2) && ((u8)(line1[x]>>24) < (u8)(back >> 24))) { + back = line1[x]; + top2 = 0x02; + } + + if((mask & 4) && ((u8)(line2[x]>>24) < (u8)(back >> 24))) { + back = line2[x]; + top2 = 0x04; + } + + if((mask & 8) && ((u8)(line3[x]>>24) < (u8)(back >> 24))) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} diff --git a/src/Mode1.cpp b/src/Mode1.cpp new file mode 100644 index 00000000..03491580 --- /dev/null +++ b/src/Mode1.cpp @@ -0,0 +1,513 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Gfx.h" + +void mode1RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0100) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if(layerEnable & 0x0200) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line0[x] < color) { + color = line0[x]; + top = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(color >> 24)) { + color = line1[x]; + top = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(color >> 24)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line0[x]>>24) < (u8)(back >> 24)) { + back = line0[x]; + top2 = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(back >> 24)) { + back = line1[x]; + top2 = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode1RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0100) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + + if(layerEnable & 0x0200) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line0[x] < color) { + color = line0[x]; + top = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(color >> 24)) { + color = line1[x]; + top = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(color >> 24)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + if((u8)(line0[x]>>24) < (u8)(back >> 24)) { + if(top != 0x01) { + back = line0[x]; + top2 = 0x01; + } + } + + if((u8)(line1[x]>>24) < (u8)(back >> 24)) { + if(top != 0x02) { + back = line1[x]; + top2 = 0x02; + } + } + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line0[x]>>24) < (u8)(back >> 24)) { + back = line0[x]; + top2 = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(back >> 24)) { + back = line1[x]; + top2 = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode1RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if(layerEnable & 0x0100) { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if(layerEnable & 0x0200) { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if(line0[x] < color && (mask & 1)) { + color = line0[x]; + top = 0x01; + } + + if((u8)(line1[x]>>24) < (u8)(color >> 24) && (mask & 2)) { + color = line1[x]; + top = 0x02; + } + + if((u8)(line2[x]>>24) < (u8)(color >> 24) && (mask & 4)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24) && (mask & 16)) { + color = lineOBJ[x]; + top = 0x10; + } + + // special FX on the window + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + if((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) { + if(top != 0x01) { + back = line0[x]; + top2 = 0x01; + } + } + + if((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) { + if(top != 0x02) { + back = line1[x]; + top2 = 0x02; + } + } + + if((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) { + back = line0[x]; + top2 = 0x01; + } + + if((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) { + back = line1[x]; + top2 = 0x02; + } + + if((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) { + back = line0[x]; + top2 = 0x01; + } + + if((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) { + back = line1[x]; + top2 = 0x02; + } + + if((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} diff --git a/src/Mode2.cpp b/src/Mode2.cpp new file mode 100644 index 00000000..462f2b00 --- /dev/null +++ b/src/Mode2.cpp @@ -0,0 +1,478 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Gfx.h" + +void mode2RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if(layerEnable & 0x0800) { + int changed = gfxBG3Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + + if((u8)(line2[x]>>24) < (u8)(color >> 24)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(color >> 24)) { + color = line3[x]; + top = 0x08; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(back >> 24)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode2RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if(layerEnable & 0x0800) { + int changed = gfxBG3Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + + if((u8)(line2[x]>>24) < (u8)(color >> 24)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(color >> 24)) { + color = line3[x]; + top = 0x08; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((u8)(line3[x]>>24) < (u8)(back >> 24)) { + if(top != 0x08) { + back = line3[x]; + top2 = 0x08; + } + } + + if((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((u8)(line2[x]>>24) < (u8)(back >> 24)) { + back = line2[x]; + top2 = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(back >> 24)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode2RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if(layerEnable & 0x0800) { + int changed = gfxBG3Changed; + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if(line2[x] < color && (mask & 4)) { + color = line2[x]; + top = 0x04; + } + + if((u8)(line3[x]>>24) < (u8)(color >> 24) && (mask & 8)) { + color = line3[x]; + top = 0x08; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24) && (mask & 16)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) { + if(top != 0x08) { + back = line3[x]; + top2 = 0x08; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) { + back = line3[x]; + top2 = 0x08; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} diff --git a/src/Mode3.cpp b/src/Mode3.cpp new file mode 100644 index 00000000..8a87308d --- /dev/null +++ b/src/Mode3.cpp @@ -0,0 +1,405 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Gfx.h" + +void mode3RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode3RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode3RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x80) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if((mask & 4) && (line2[x] < color)) { + color = line2[x]; + top = 0x04; + } + + if((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) { + color = lineOBJ[x]; + top = 0x10; + } + + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} diff --git a/src/Mode4.cpp b/src/Mode4.cpp new file mode 100644 index 00000000..8e815230 --- /dev/null +++ b/src/Mode4.cpp @@ -0,0 +1,402 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Gfx.h" +#include "Globals.h" + +void mode4RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode4RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if(layerEnable & 0x400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + + if(line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode4RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if(layerEnable & 0x400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for(int x = 0; x < 240; x++) { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if((mask & 4) && (line2[x] < color)) { + color = line2[x]; + top = 0x04; + } + + if((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) { + color = lineOBJ[x]; + top = 0x10; + } + + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} diff --git a/src/Mode5.cpp b/src/Mode5.cpp new file mode 100644 index 00000000..d3e97102 --- /dev/null +++ b/src/Mode5.cpp @@ -0,0 +1,405 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Gfx.h" + +void mode5RenderLine() +{ + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if((top & 0x10) && (color & 0x00010000)) { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode5RenderLineNoWindow() +{ + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = ( READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + + if(line2[x] < color) { + color = line2[x]; + top = 0x04; + } + + if((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) { + color = lineOBJ[x]; + top = 0x10; + } + + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if(line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode5RenderLineAll() +{ + if(DISPCNT & 0x0080) { + for(int x = 0; x < 240; x++) { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if(layerEnable & 0x0400) { + int changed = gfxBG2Changed; + + if(gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + bool inWindow0 = false; + bool inWindow1 = false; + + if(layerEnable & 0x2000) { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if(layerEnable & 0x4000) { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if(v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for(int x = 0; x < 240; x++) { + u32 color = background; + u8 top = 0x20; + u8 mask = outMask; + + if(!(lineOBJWin[x] & 0x80000000)) { + mask = WINOUT >> 8; + } + + if(inWindow1) { + if(gfxInWin1[x]) + mask = inWin1Mask; + } + + if(inWindow0) { + if(gfxInWin0[x]) { + mask = inWin0Mask; + } + } + + if((mask & 4) && (line2[x] < color)) { + color = line2[x]; + top = 0x04; + } + + if((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) { + color = lineOBJ[x]; + top = 0x10; + } + + if(mask & 32) { + if(!(color & 0x00010000)) { + switch((BLDMOD >> 6) & 3) { + case 0: + break; + case 1: + { + if(top & BLDMOD) { + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + if(top != 0x04) { + back = line2[x]; + top2 = 0x04; + } + } + + if((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) { + if(top != 0x10) { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + + } + } + break; + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } else { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } else if(color & 0x00010000) { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if((mask & 4) && line2[x] < back) { + back = line2[x]; + top2 = 0x04; + } + + if(top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else { + switch((BLDMOD >> 6) & 3) { + case 2: + if(BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if(BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} diff --git a/src/NLS.h b/src/NLS.h new file mode 100644 index 00000000..d243a863 --- /dev/null +++ b/src/NLS.h @@ -0,0 +1,62 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#define N_(String) (String) + +#define MSG_UNSUPPORTED_VBA_SGM 1 +#define MSG_CANNOT_LOAD_SGM 2 +#define MSG_SAVE_GAME_NOT_USING_BIOS 3 +#define MSG_SAVE_GAME_USING_BIOS 4 +#define MSG_UNSUPPORTED_SAVE_TYPE 5 +#define MSG_CANNOT_OPEN_FILE 6 +#define MSG_BAD_ARCHIVE_FILE 7 +#define MSG_NO_IMAGE_ON_ARCHIVE 8 +#define MSG_ERROR_OPENING_IMAGE 9 +#define MSG_ERROR_READING_IMAGE 10 +#define MSG_UNSUPPORTED_BIOS_FUNCTION 11 +#define MSG_INVALID_BIOS_FILE_SIZE 12 +#define MSG_INVALID_CHEAT_CODE 13 +#define MSG_UNKNOWN_ARM_OPCODE 14 +#define MSG_UNKNOWN_THUMB_OPCODE 15 +#define MSG_ERROR_CREATING_FILE 16 +#define MSG_FAILED_TO_READ_SGM 17 +#define MSG_FAILED_TO_READ_RTC 18 +#define MSG_UNSUPPORTED_VB_SGM 19 +#define MSG_CANNOT_LOAD_SGM_FOR 20 +#define MSG_ERROR_OPENING_IMAGE_FROM 21 +#define MSG_ERROR_READING_IMAGE_FROM 22 +#define MSG_UNSUPPORTED_ROM_SIZE 23 +#define MSG_UNSUPPORTED_RAM_SIZE 24 +#define MSG_UNKNOWN_CARTRIDGE_TYPE 25 +#define MSG_MAXIMUM_NUMBER_OF_CHEATS 26 +#define MSG_INVALID_GAMESHARK_CODE 27 +#define MSG_INVALID_GAMEGENIE_CODE 28 +#define MSG_INVALID_CHEAT_TO_REMOVE 29 +#define MSG_INVALID_CHEAT_CODE_ADDRESS 30 +#define MSG_UNSUPPORTED_CHEAT_LIST_VERSION 31 +#define MSG_UNSUPPORTED_CHEAT_LIST_TYPE 32 +#define MSG_INVALID_GSA_CODE 33 +#define MSG_CANNOT_IMPORT_SNAPSHOT_FOR 34 +#define MSG_UNSUPPORTED_SNAPSHOT_FILE 35 +#define MSG_UNSUPPORTED_ARM_MODE 36 +#define MSG_UNSUPPORTED_CODE_FILE 37 +#define MSG_GBA_CODE_WARNING 38 +#define MSG_INVALID_CBA_CODE 39 +#define MSG_CBA_CODE_WARNING 40 +#define MSG_OUT_OF_MEMORY 41 diff --git a/src/Port.h b/src/Port.h new file mode 100644 index 00000000..3716bf9c --- /dev/null +++ b/src/Port.h @@ -0,0 +1,75 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_PORT_H +#define VBA_PORT_H + +// swaps a 16-bit value +static inline u16 swap16(u16 v) +{ + return (v<<8)|(v>>8); +} + +// swaps a 32-bit value +static inline u32 swap32(u32 v) +{ + return (v<<24)|((v<<8)&0xff0000)|((v>>8)&0xff00)|(v>>24); +} + +#ifdef WORDS_BIGENDIAN +#if defined(__GNUC__) && defined(__ppc__) + +#define READ16LE(base) \ + ({ unsigned short lhbrxResult; \ + __asm__ ("lhbrx %0, 0, %1" : "=r" (lhbrxResult) : "r" (base) : "memory"); \ + lhbrxResult; }) + +#define READ32LE(base) \ + ({ unsigned long lwbrxResult; \ + __asm__ ("lwbrx %0, 0, %1" : "=r" (lwbrxResult) : "r" (base) : "memory"); \ + lwbrxResult; }) + +#define WRITE16LE(base, value) \ + __asm__ ("sthbrx %0, 0, %1" : : "r" (value), "r" (base) : "memory") + +#define WRITE32LE(base, value) \ + __asm__ ("stwbrx %0, 0, %1" : : "r" (value), "r" (base) : "memory") + +#else +#define READ16LE(x) \ + swap16(*((u16 *)(x))) +#define READ32LE(x) \ + swap32(*((u32 *)(x))) +#define WRITE16LE(x,v) \ + *((u16 *)x) = swap16((v)) +#define WRITE32LE(x,v) \ + *((u32 *)x) = swap32((v)) +#endif +#else +#define READ16LE(x) \ + *((u16 *)x) +#define READ32LE(x) \ + *((u32 *)x) +#define WRITE16LE(x,v) \ + *((u16 *)x) = (v) +#define WRITE32LE(x,v) \ + *((u32 *)x) = (v) +#endif + +#endif diff --git a/src/RTC.cpp b/src/RTC.cpp new file mode 100644 index 00000000..9a5b3098 --- /dev/null +++ b/src/RTC.cpp @@ -0,0 +1,220 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" +#include "GBA.h" +#include "Globals.h" +#include "Port.h" +#include "Util.h" +#include "NLS.h" + +#include +#include + +enum RTCSTATE { IDLE, COMMAND, DATA, READDATA }; + +typedef struct { + u8 byte0; + u8 byte1; + u8 byte2; + u8 command; + int dataLen; + int bits; + RTCSTATE state; + u8 data[12]; + // reserved variables for future + u8 reserved[12]; + bool reserved2; + u32 reserved3; +} RTCCLOCKDATA; + +static RTCCLOCKDATA rtcClockData; +static bool rtcEnabled = false; + +void rtcEnable(bool e) +{ + rtcEnabled = e; +} + +bool rtcIsEnabled() +{ + return rtcEnabled; +} + +u16 rtcRead(u32 address) +{ + if(rtcEnabled) { + if(address == 0x80000c8) + return rtcClockData.byte2; + else if(address == 0x80000c6) + return rtcClockData.byte1; + else if(address == 0x80000c4) { + return rtcClockData.byte0; + } + } + + return READ16LE((&rom[address & 0x1FFFFFE])); +} + +static u8 toBCD(u8 value) +{ + value = value % 100; + int l = value % 10; + int h = value / 10; + return h * 16 + l; +} + +bool rtcWrite(u32 address, u16 value) +{ + if(!rtcEnabled) + return false; + + if(address == 0x80000c8) { + rtcClockData.byte2 = (u8)value; // enable ? + } else if(address == 0x80000c6) { + rtcClockData.byte1 = (u8)value; // read/write + } else if(address == 0x80000c4) { + if(rtcClockData.byte2 & 1) { + if(rtcClockData.state == IDLE && rtcClockData.byte0 == 1 && value == 5) { + rtcClockData.state = COMMAND; + rtcClockData.bits = 0; + rtcClockData.command = 0; + } else if(!(rtcClockData.byte0 & 1) && (value & 1)) { // bit transfer + rtcClockData.byte0 = (u8)value; + switch(rtcClockData.state) { + case COMMAND: + rtcClockData.command |= ((value & 2) >> 1) << (7-rtcClockData.bits); + rtcClockData.bits++; + if(rtcClockData.bits == 8) { + rtcClockData.bits = 0; + switch(rtcClockData.command) { + case 0x60: + // not sure what this command does but it doesn't take parameters + // maybe it is a reset or stop + rtcClockData.state = IDLE; + rtcClockData.bits = 0; + break; + case 0x62: + // this sets the control state but not sure what those values are + rtcClockData.state = READDATA; + rtcClockData.dataLen = 1; + break; + case 0x63: + rtcClockData.dataLen = 1; + rtcClockData.data[0] = 0x40; + rtcClockData.state = DATA; + break; + case 0x65: + { + struct tm *newtime; + time_t long_time; + + time( &long_time ); /* Get time as long integer. */ + newtime = localtime( &long_time ); /* Convert to local time. */ + + rtcClockData.dataLen = 7; + rtcClockData.data[0] = toBCD(newtime->tm_year); + rtcClockData.data[1] = toBCD(newtime->tm_mon+1); + rtcClockData.data[2] = toBCD(newtime->tm_mday); + rtcClockData.data[3] = toBCD(newtime->tm_wday); + rtcClockData.data[4] = toBCD(newtime->tm_hour); + rtcClockData.data[5] = toBCD(newtime->tm_min); + rtcClockData.data[6] = toBCD(newtime->tm_sec); + rtcClockData.state = DATA; + } + break; + case 0x67: + { + struct tm *newtime; + time_t long_time; + + time( &long_time ); /* Get time as long integer. */ + newtime = localtime( &long_time ); /* Convert to local time. */ + + rtcClockData.dataLen = 3; + rtcClockData.data[0] = toBCD(newtime->tm_hour); + rtcClockData.data[1] = toBCD(newtime->tm_min); + rtcClockData.data[2] = toBCD(newtime->tm_sec); + rtcClockData.state = DATA; + } + break; + default: + systemMessage(0, N_("Unknown RTC command %02x"), rtcClockData.command); + rtcClockData.state = IDLE; + break; + } + } + break; + case DATA: + if(rtcClockData.byte1 & 2) { + } else { + rtcClockData.byte0 = (rtcClockData.byte0 & ~2) | + ((rtcClockData.data[rtcClockData.bits >> 3] >> + (rtcClockData.bits & 7)) & 1)*2; + rtcClockData.bits++; + if(rtcClockData.bits == 8*rtcClockData.dataLen) { + rtcClockData.bits = 0; + rtcClockData.state = IDLE; + } + } + break; + case READDATA: + if(!(rtcClockData.byte1 & 2)) { + } else { + rtcClockData.data[rtcClockData.bits >> 3] = + (rtcClockData.data[rtcClockData.bits >> 3] >> 1) | + ((value << 6) & 128); + rtcClockData.bits++; + if(rtcClockData.bits == 8*rtcClockData.dataLen) { + rtcClockData.bits = 0; + rtcClockData.state = IDLE; + } + } + break; + default: + break; + } + } else + rtcClockData.byte0 = (u8)value; + } + } + return true; +} + +void rtcReset() +{ + memset(&rtcClockData, 0, sizeof(rtcClockData)); + + rtcClockData.byte0 = 0; + rtcClockData.byte1 = 0; + rtcClockData.byte2 = 0; + rtcClockData.command = 0; + rtcClockData.dataLen = 0; + rtcClockData.bits = 0; + rtcClockData.state = IDLE; +} + +void rtcSaveGame(gzFile gzFile) +{ + utilGzWrite(gzFile, &rtcClockData, sizeof(rtcClockData)); +} + +void rtcReadGame(gzFile gzFile) +{ + utilGzRead(gzFile, &rtcClockData, sizeof(rtcClockData)); +} \ No newline at end of file diff --git a/src/RTC.h b/src/RTC.h new file mode 100644 index 00000000..acbd3f6a --- /dev/null +++ b/src/RTC.h @@ -0,0 +1,31 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_RTC_H +#define VBA_RTC_H +extern u16 rtcRead(u32 address); +extern bool rtcWrite(u32 address, u16 value); +extern void rtcEnable(bool); +extern bool rtcIsEnabled(); +extern void rtcReset(); + +extern void rtcReadGame(gzFile gzFile); +extern void rtcSaveGame(gzFile gzFile); + +#endif diff --git a/src/Sound.cpp b/src/Sound.cpp new file mode 100644 index 00000000..545d0a19 --- /dev/null +++ b/src/Sound.cpp @@ -0,0 +1,1177 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include + +#include "GBA.h" +#include "Globals.h" +#include "Sound.h" +#include "Util.h" + +#include "snd_interp.h" + +#include "Gb_Apu/Multi_Buffer.h" +#include "Gb_Apu/Gb_Apu.h" + +#ifdef _WIN32 +#include +#endif + +#define USE_TICKS_AS 380 +#define SOUND_MAGIC 0x60000000 +#define SOUND_MAGIC_2 0x30000000 +#define NOISE_MAGIC 5 + +extern bool stopState; +extern bool cpuDmaHack2; + +u8 soundWavePattern[4][32] = { + {0x01,0x01,0x01,0x01, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff}, + {0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff}, + {0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff}, + {0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01, + 0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff} +}; + +int soundFreqRatio[8] = { + 1048576, // 0 + 524288, // 1 + 262144, // 2 + 174763, // 3 + 131072, // 4 + 104858, // 5 + 87381, // 6 + 74898 // 7 +}; + +int soundShiftClock[16]= { + 2, // 0 + 4, // 1 + 8, // 2 + 16, // 3 + 32, // 4 + 64, // 5 + 128, // 6 + 256, // 7 + 512, // 8 + 1024, // 9 + 2048, // 10 + 4096, // 11 + 8192, // 12 + 16384, // 13 + 1, // 14 + 1 // 15 +}; + +int soundVolume = 0; + +u8 soundBuffer[6][735]; +u16 directBuffer[2][735]; +u16 soundFinalWave[1470]; + +int soundBufferLen = 1470; +int soundBufferTotalLen = 14700; +int soundQuality = 2; +int soundInterpolation = 0; +int soundPaused = 1; +int soundPlay = 0; +int soundTicks = soundQuality * USE_TICKS_AS; +int SOUND_CLOCK_TICKS = soundQuality * USE_TICKS_AS; +u32 soundNextPosition = 0; + +Multi_Buffer * apu_out = NULL; +Gb_Apu * apu = NULL; +bool apu_saved = false; + +int soundLevel1 = 0; +int soundLevel2 = 0; +int soundBalance = 0; +int soundMasterOn = 0; +int soundIndex = 0; +int soundBufferIndex = 0; +int soundDebug = 0; +bool soundOffFlag = false; + +int sound1On = 0; +int sound1ATL = 0; +int sound1Skip = 0; +int sound1Index = 0; +int sound1Continue = 0; +int sound1EnvelopeVolume = 0; +int sound1EnvelopeATL = 0; +int sound1EnvelopeUpDown = 0; +int sound1EnvelopeATLReload = 0; +int sound1SweepATL = 0; +int sound1SweepATLReload = 0; +int sound1SweepSteps = 0; +int sound1SweepUpDown = 0; +int sound1SweepStep = 0; +u8 *sound1Wave = soundWavePattern[2]; + +int sound2On = 0; +int sound2ATL = 0; +int sound2Skip = 0; +int sound2Index = 0; +int sound2Continue = 0; +int sound2EnvelopeVolume = 0; +int sound2EnvelopeATL = 0; +int sound2EnvelopeUpDown = 0; +int sound2EnvelopeATLReload = 0; +u8 *sound2Wave = soundWavePattern[2]; + +int sound3On = 0; +int sound3ATL = 0; +int sound3Skip = 0; +int sound3Index = 0; +int sound3Continue = 0; +int sound3OutputLevel = 0; +int sound3Last = 0; +u8 sound3WaveRam[0x20]; +int sound3Bank = 0; +int sound3DataSize = 0; +int sound3ForcedOutput = 0; + +int sound4On = 0; +int sound4Clock = 0; +int sound4ATL = 0; +int sound4Skip = 0; +int sound4Index = 0; +int sound4ShiftRight = 0x7f; +int sound4ShiftSkip = 0; +int sound4ShiftIndex = 0; +int sound4NSteps = 0; +int sound4CountDown = 0; +int sound4Continue = 0; +int sound4EnvelopeVolume = 0; +int sound4EnvelopeATL = 0; +int sound4EnvelopeUpDown = 0; +int sound4EnvelopeATLReload = 0; + +int soundControl = 0; + +int soundDSFifoAIndex = 0; +int soundDSFifoACount = 0; +int soundDSFifoAWriteIndex = 0; +bool soundDSAEnabled = false; +int soundDSATimer = 0; +u8 soundDSFifoA[32]; +u8 soundDSAValue = 0; + +int soundDSFifoBIndex = 0; +int soundDSFifoBCount = 0; +int soundDSFifoBWriteIndex = 0; +bool soundDSBEnabled = false; +int soundDSBTimer = 0; +u8 soundDSFifoB[32]; +u8 soundDSBValue = 0; + +int soundEnableFlag = 0x3ff; + +s16 soundFilter[4000]; +s16 soundRight[5] = { 0, 0, 0, 0, 0 }; +s16 soundLeft[5] = { 0, 0, 0, 0, 0 }; +int soundEchoIndex = 0; +bool soundEcho = false; +bool soundLowPass = false; +bool soundReverse = false; + +variable_desc soundSaveStruct[] = { + { &soundPaused, sizeof(int) }, + { &soundPlay, sizeof(int) }, + { &soundTicks, sizeof(int) }, + { &SOUND_CLOCK_TICKS, sizeof(int) }, + { &soundLevel1, sizeof(int) }, + { &soundLevel2, sizeof(int) }, + { &soundBalance, sizeof(int) }, + { &soundMasterOn, sizeof(int) }, + { &soundIndex, sizeof(int) }, + { &sound1On, sizeof(int) }, + { &sound1ATL, sizeof(int) }, + { &sound1Skip, sizeof(int) }, + { &sound1Index, sizeof(int) }, + { &sound1Continue, sizeof(int) }, + { &sound1EnvelopeVolume, sizeof(int) }, + { &sound1EnvelopeATL, sizeof(int) }, + { &sound1EnvelopeATLReload, sizeof(int) }, + { &sound1EnvelopeUpDown, sizeof(int) }, + { &sound1SweepATL, sizeof(int) }, + { &sound1SweepATLReload, sizeof(int) }, + { &sound1SweepSteps, sizeof(int) }, + { &sound1SweepUpDown, sizeof(int) }, + { &sound1SweepStep, sizeof(int) }, + { &sound2On, sizeof(int) }, + { &sound2ATL, sizeof(int) }, + { &sound2Skip, sizeof(int) }, + { &sound2Index, sizeof(int) }, + { &sound2Continue, sizeof(int) }, + { &sound2EnvelopeVolume, sizeof(int) }, + { &sound2EnvelopeATL, sizeof(int) }, + { &sound2EnvelopeATLReload, sizeof(int) }, + { &sound2EnvelopeUpDown, sizeof(int) }, + { &sound3On, sizeof(int) }, + { &sound3ATL, sizeof(int) }, + { &sound3Skip, sizeof(int) }, + { &sound3Index, sizeof(int) }, + { &sound3Continue, sizeof(int) }, + { &sound3OutputLevel, sizeof(int) }, + { &sound4On, sizeof(int) }, + { &sound4ATL, sizeof(int) }, + { &sound4Skip, sizeof(int) }, + { &sound4Index, sizeof(int) }, + { &sound4Clock, sizeof(int) }, + { &sound4ShiftRight, sizeof(int) }, + { &sound4ShiftSkip, sizeof(int) }, + { &sound4ShiftIndex, sizeof(int) }, + { &sound4NSteps, sizeof(int) }, + { &sound4CountDown, sizeof(int) }, + { &sound4Continue, sizeof(int) }, + { &sound4EnvelopeVolume, sizeof(int) }, + { &sound4EnvelopeATL, sizeof(int) }, + { &sound4EnvelopeATLReload, sizeof(int) }, + { &sound4EnvelopeUpDown, sizeof(int) }, + { &soundEnableFlag, sizeof(int) }, + { &soundControl, sizeof(int) }, + { &soundDSFifoAIndex, sizeof(int) }, + { &soundDSFifoACount, sizeof(int) }, + { &soundDSFifoAWriteIndex, sizeof(int) }, + { &soundDSAEnabled, sizeof(bool) }, + { &soundDSATimer, sizeof(int) }, + { &soundDSFifoA[0], 32 }, + { &soundDSAValue, sizeof(u8) }, + { &soundDSFifoBIndex, sizeof(int) }, + { &soundDSFifoBCount, sizeof(int) }, + { &soundDSFifoBWriteIndex, sizeof(int) }, + { &soundDSBEnabled, sizeof(int) }, + { &soundDSBTimer, sizeof(int) }, + { &soundDSFifoB[0], 32 }, + { &soundDSBValue, sizeof(int) }, + { &soundBuffer[0][0], 6*735 }, + { &soundFinalWave[0], 2*735 }, + { NULL, 0 } +}; + +variable_desc soundSaveStructV2[] = { + { &sound3WaveRam[0], 0x20 }, + { &sound3Bank, sizeof(int) }, + { &sound3DataSize, sizeof(int) }, + { &sound3ForcedOutput, sizeof(int) }, + { NULL, 0 } +}; + +static void soundEventGB(u32 address, u8 data) + { + if ( apu ) + { + int divisor = 4 * soundQuality; + int clock = (SOUND_CLOCK_TICKS - soundTicks + (divisor >> 1)) / divisor; + apu->write_register(clock, address, data); + } + else + ioMem[address] = data; +} + +u8 soundRead(u32 address) +{ + if (address < NR10 || address > 0x9F || !apu) return ioMem[address]; + if (address == NR51) return soundBalance; + + int divisor = 4 * soundQuality; + int clock = (SOUND_CLOCK_TICKS - soundTicks + (divisor >> 1)) / divisor; + switch (address) + { + case NR10: + return apu->read_register(clock, 0xFF10); + case NR11: + return apu->read_register(clock, 0xFF11); + case NR12: + return apu->read_register(clock, 0xFF12); + case NR13: + return apu->read_register(clock, 0xFF13); + case NR14: + return apu->read_register(clock, 0xFF14); + case NR21: + return apu->read_register(clock, 0xFF16); + case NR22: + return apu->read_register(clock, 0xFF17); + case NR23: + return apu->read_register(clock, 0xFF18); + case NR24: + return apu->read_register(clock, 0xFF19); + case NR30: + return apu->read_register(clock, 0xFF1A); + case NR31: + return apu->read_register(clock, 0xFF1B); + case NR32: + return apu->read_register(clock, 0xFF1C); + case NR33: + return apu->read_register(clock, 0xFF1D); + case NR34: + return apu->read_register(clock, 0xFF1E); + case NR41: + return apu->read_register(clock, 0xFF20); + case NR42: + return apu->read_register(clock, 0xFF21); + case NR43: + return apu->read_register(clock, 0xFF22); + case NR44: + return apu->read_register(clock, 0xFF23); + case NR50: + return apu->read_register(clock, 0xFF24); + case NR51: + return apu->read_register(clock, 0xFF25); + case NR52: + return apu->read_register(clock, 0xFF26); + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + case 0x98: + case 0x99: + case 0x9a: + case 0x9b: + case 0x9c: + case 0x9d: + case 0x9e: + case 0x9f: + return apu->read_register(clock, 0xFF30 - 0x90 + address); + } + + return ioMem[address]; + } + + u16 soundRead16(u32 address) + { + address &= ~1; + u16 temp = soundRead(address); + temp |= soundRead(address + 1) << 8; + return temp; + } + + u32 soundRead32(u32 address) + { + address &= ~3; + u32 temp = soundRead16(address); + temp |= soundRead16(address + 2) << 16; + return temp; + } + + + void soundEvent(u32 address, u8 data) + { + switch(address) { + case NR10: + soundEventGB(0xFF10, data); + break; + case NR11: + soundEventGB(0xFF11, data); + break; + case NR12: + soundEventGB(0xFF12, data); + break; + case NR13: + soundEventGB(0xFF13, data); + break; + case NR14: + soundEventGB(0xFF14, data); + break; + case NR21: + soundEventGB(0xFF16, data); + break; + case NR22: + soundEventGB(0xFF17, data); + break; + case NR23: + soundEventGB(0xFF18, data); + break; + case NR24: + soundEventGB(0xFF19, data); + break; + case NR30: + soundEventGB(0xFF1A, data); + break; + case NR31: + soundEventGB(0xFF1B, data); + break; + case NR32: + soundEventGB(0xFF1C, data); + break; + case NR33: + soundEventGB(0xFF1D, data); + break; + case NR34: + soundEventGB(0xFF1E, data); + break; + case NR41: + soundEventGB(0xFF20, data); + break; + case NR42: + soundEventGB(0xFF21, data); + break; + case NR43: + soundEventGB(0xFF22, data); + break; + case NR44: + soundEventGB(0xFF23, data); + break; + case NR50: + soundEventGB(0xFF24, data); + break; + case NR51: + soundBalance = data; + soundEventGB(0xFF25, data & soundEnableFlag); + break; + case NR52: + soundEventGB(0xFF26, data); + break; + + case 0x90: + case 0x92: + case 0x94: + case 0x96: + case 0x98: + case 0x9a: + case 0x9c: + case 0x9e: + soundEventGB(0xFF30 + (address & 14), data); + soundEventGB(0xFF31 + (address & 14), data >> 8); + break; + } +} + +void soundEvent(u32 address, u16 data) +{ + switch(address) { + case SGCNT0_H: + data &= 0xFF0F; + soundControl = data & 0x770F;; + if(data & 0x0800) { + interp_reset(0); + soundDSFifoAWriteIndex = 0; + soundDSFifoAIndex = 0; + soundDSFifoACount = 0; + soundDSAValue = 0; + memset(soundDSFifoA, 0, 32); + } + soundDSAEnabled = (data & 0x0300) ? true : false; + soundDSATimer = (data & 0x0400) ? 1 : 0; + if(data & 0x8000) { + interp_reset(1); + soundDSFifoBWriteIndex = 0; + soundDSFifoBIndex = 0; + soundDSFifoBCount = 0; + soundDSBValue = 0; + memset(soundDSFifoB, 0, 32); + } + soundDSBEnabled = (data & 0x3000) ? true : false; + soundDSBTimer = (data & 0x4000) ? 1 : 0; + *((u16 *)&ioMem[address]) = data; + break; + case FIFOA_L: + case FIFOA_H: + soundDSFifoA[soundDSFifoAWriteIndex++] = data & 0xFF; + soundDSFifoA[soundDSFifoAWriteIndex++] = data >> 8; + soundDSFifoACount += 2; + soundDSFifoAWriteIndex &= 31; + *((u16 *)&ioMem[address]) = data; + break; + case FIFOB_L: + case FIFOB_H: + soundDSFifoB[soundDSFifoBWriteIndex++] = data & 0xFF; + soundDSFifoB[soundDSFifoBWriteIndex++] = data >> 8; + soundDSFifoBCount += 2; + soundDSFifoBWriteIndex &= 31; + *((u16 *)&ioMem[address]) = data; + break; + case 0x88: + data &= 0xC3FF; + *((u16 *)&ioMem[address]) = data; + break; + case 0x90: + case 0x92: + case 0x94: + case 0x96: + case 0x98: + case 0x9a: + case 0x9c: + case 0x9e: + soundEventGB(0xFF30 + (address & 14), data); + soundEventGB(0xFF31 + (address & 14), data >> 8); + break; + } +} + +void soundChannel1() +{ +} + +void soundChannel2() +{ +} + +void soundChannel3() +{ +} + +void soundChannel4() +{ +} + +void soundDirectSoundA() +{ + directBuffer[0][soundIndex] = interp_pop(0, calc_rate(soundDSATimer)); //soundDSAValue; +} + +void soundDirectSoundATimer() +{ + if(soundDSAEnabled) { + if(soundDSFifoACount <= 16) { + cpuDmaHack2 = CPUCheckDMA(3, 2); + if(soundDSFifoACount <= 16) { + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + } + } + + soundDSAValue = (soundDSFifoA[soundDSFifoAIndex]); + interp_push(0, (s8)soundDSAValue << 8); + soundDSFifoAIndex = (++soundDSFifoAIndex) & 31; + soundDSFifoACount--; + } else + soundDSAValue = 0; +} + +void soundDirectSoundB() +{ + directBuffer[1][soundIndex] = interp_pop(1, calc_rate(soundDSBTimer)); //soundDSBValue; +} + +void soundDirectSoundBTimer() +{ + if(soundDSBEnabled) { + if(soundDSFifoBCount <= 16) { + cpuDmaHack2 = CPUCheckDMA(3, 4); + if(soundDSFifoBCount <= 16) { + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + } + } + + soundDSBValue = (soundDSFifoB[soundDSFifoBIndex]); + interp_push(1, (s8)soundDSBValue << 8); + soundDSFifoBIndex = (++soundDSFifoBIndex) & 31; + soundDSFifoBCount--; + } else { + soundDSBValue = 0; + } +} + +void soundTimerOverflow(int timer) +{ + if(soundDSAEnabled && (soundDSATimer == timer)) { + soundDirectSoundATimer(); + } + if(soundDSBEnabled && (soundDSBTimer == timer)) { + soundDirectSoundBTimer(); + } +} + +#ifndef max +#define max(a,b) (a)<(b)?(b):(a) +#endif + +void soundMix() +{ + int res = 0; + int cgbRes = 0; + int ratio = ioMem[0x82] & 3; + int dsaRatio = ioMem[0x82] & 4; + int dsbRatio = ioMem[0x82] & 8; + + blip_sample_t out[2] = {0, 0}; + + if ( ! apu_out ) return; + + while (!apu_out->read_samples(&out[0], 2)) + { + int ticks = SOUND_CLOCK_TICKS / (4 * soundQuality); + bool was_stereo = apu->end_frame( ticks ); + apu_out->end_frame( ticks, was_stereo ); + } + + cgbRes = out[0]; + + if((soundControl & 0x0200) && (soundEnableFlag & 0x100)){ + if(!dsaRatio) + res = ((s16)directBuffer[0][soundIndex])>>1; + else + res = ((s16)directBuffer[0][soundIndex]); + } + + if((soundControl & 0x2000) && (soundEnableFlag & 0x200)){ + if(!dsbRatio) + res += ((s16)directBuffer[1][soundIndex])>>1; + else + res += ((s16)directBuffer[1][soundIndex]); + } + + res = (res * 170) >> 8; + cgbRes = (cgbRes * 52 * 7) >> 8; + + switch(ratio) { + case 0: + case 3: // prohibited, but 25% + cgbRes >>= 2; + break; + case 1: + cgbRes >>= 1; + break; + case 2: + break; + } + + res += cgbRes; + + if(soundEcho) { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + } + + if(soundLowPass) { + soundLeft[4] = soundLeft[3]; + soundLeft[3] = soundLeft[2]; + soundLeft[2] = soundLeft[1]; + soundLeft[1] = soundLeft[0]; + soundLeft[0] = res; + res = (soundLeft[4] + 2*soundLeft[3] + 8*soundLeft[2] + 2*soundLeft[1] + + soundLeft[0])/14; + } + + switch(soundVolume) { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume+1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + + if(res > 32767) + res = 32767; + if(res < -32768) + res = -32768; + + if(soundReverse) + soundFinalWave[++soundBufferIndex] = res; + else + soundFinalWave[soundBufferIndex++] = res; + + res = 0; + cgbRes = out[1]; + + if((soundControl & 0x0100) && (soundEnableFlag & 0x100)){ + if(!dsaRatio) + res = ((s16)directBuffer[0][soundIndex])>>1; + else + res = ((s16)directBuffer[0][soundIndex]); + } + + if((soundControl & 0x1000) && (soundEnableFlag & 0x200)){ + if(!dsbRatio) + res += ((s16)directBuffer[1][soundIndex])>>1; + else + res += ((s16)directBuffer[1][soundIndex]); + } + + res = (res * 170) >> 8; + cgbRes = (cgbRes * 52 * 7) >> 8; + + switch(ratio) { + case 0: + case 3: // prohibited, but 25% + cgbRes >>= 2; + break; + case 1: + cgbRes >>= 1; + break; + case 2: + break; + } + + res += cgbRes; + + if(soundEcho) { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + + if(soundEchoIndex >= 4000) + soundEchoIndex = 0; + } + + if(soundLowPass) { + soundRight[4] = soundRight[3]; + soundRight[3] = soundRight[2]; + soundRight[2] = soundRight[1]; + soundRight[1] = soundRight[0]; + soundRight[0] = res; + res = (soundRight[4] + 2*soundRight[3] + 8*soundRight[2] + 2*soundRight[1] + + soundRight[0])/14; + } + + switch(soundVolume) { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume+1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + + if(res > 32767) + res = 32767; + if(res < -32768) + res = -32768; + + if(soundReverse) + soundFinalWave[-1+soundBufferIndex++] = res; + else + soundFinalWave[soundBufferIndex++] = res; +} + +void soundTick() +{ + if(systemSoundOn) { + if(soundMasterOn && !stopState) { + /*soundChannel1(); + soundChannel2(); + soundChannel3(); + soundChannel4();*/ + soundDirectSoundA(); + soundDirectSoundB(); + soundMix(); + } else { + soundFinalWave[soundBufferIndex++] = 0; + soundFinalWave[soundBufferIndex++] = 0; + } + + soundIndex++; + + if(2*soundBufferIndex >= soundBufferLen) { + if(systemSoundOn) { + if(soundPaused) { + soundResume(); + } + + systemWriteDataToSoundBuffer(); + } + soundIndex = 0; + soundBufferIndex = 0; + } + } +} + +void soundShutdown() +{ + systemSoundShutdown(); + + int i; + u8 j; + + if (apu) + { + j = soundRead(NR30); + if (!(j & 0x40)) soundEvent(NR30, u8(j | 0x40)); + for (i = 0; i < 16; i++) sound3WaveRam[i] = soundRead(i + 0x90); + soundEvent(NR30, u8(j & ~0x40)); + for (i = 16; i < 32; i++) sound3WaveRam[i] = soundRead(i - 16 + 0x90); + if (j & 0x40) soundEvent(NR30, j); + apu_saved = true; + + delete apu; apu = NULL; + } + if (apu_out) { delete apu_out; apu_out = NULL; } +} + +void soundPause() +{ + systemSoundPause(); + soundPaused = 1; +} + +void soundResume() +{ + systemSoundResume(); + soundPaused = 0; +} + +void soundEnable(int channels) +{ + int c = channels & 0x0f; + + soundEnableFlag |= ((channels & 0x30f) |c | (c << 4)); + if(apu) + soundEventGB(0xFF25, soundBalance & soundEnableFlag); +} + +void soundDisable(int channels) +{ + int c = channels & 0x0f; + + soundEnableFlag &= (~((channels & 0x30f)|c|(c<<4))); + if(apu) + soundEventGB(0xFF25, soundBalance & soundEnableFlag); +} + +int soundGetEnable() +{ + return (soundEnableFlag & 0x30f); +} + +const BOOST::uint8_t sound_data [Gb_Apu::register_count] = { + 0x80, 0xbf, 0x00, 0x00, 0xbf, // square 1 + 0x00, 0x3f, 0x00, 0x00, 0xbf, // square 2 + 0x40, 0xff, 0x9f, 0x00, 0xbf, // wave + 0x00, 0xff, 0x00, 0x00, 0xbf, // noise + + 0x77, 0xf3, 0xf1, // vin/volume, status, power mode + + 0, 0, 0, 0, 0, 0, 0, 0, 0, // unused + + 0xac, 0xdd, 0xda, 0x48, 0x36, 0x02, 0xcf, 0x16, // waveform data + 0x2c, 0x04, 0xe5, 0x2c, 0xac, 0xdd, 0xda, 0x48 +}; + +void soundReset() +{ + systemSoundReset(); + + soundPaused = 1; + soundPlay = 0; + SOUND_CLOCK_TICKS = soundQuality * USE_TICKS_AS; + soundTicks = SOUND_CLOCK_TICKS; + soundNextPosition = 0; + soundMasterOn = 1; + soundIndex = 0; + soundBufferIndex = 0; + soundLevel1 = 7; + soundLevel2 = 7; + + /* + sound1On = 0; + sound1ATL = 0; + sound1Skip = 0; + sound1Index = 0; + sound1Continue = 0; + sound1EnvelopeVolume = 0; + sound1EnvelopeATL = 0; + sound1EnvelopeUpDown = 0; + sound1EnvelopeATLReload = 0; + sound1SweepATL = 0; + sound1SweepATLReload = 0; + sound1SweepSteps = 0; + sound1SweepUpDown = 0; + sound1SweepStep = 0; + sound1Wave = soundWavePattern[2]; + + sound2On = 0; + sound2ATL = 0; + sound2Skip = 0; + sound2Index = 0; + sound2Continue = 0; + sound2EnvelopeVolume = 0; + sound2EnvelopeATL = 0; + sound2EnvelopeUpDown = 0; + sound2EnvelopeATLReload = 0; + sound2Wave = soundWavePattern[2]; + + sound3On = 0; + sound3ATL = 0; + sound3Skip = 0; + sound3Index = 0; + sound3Continue = 0; + sound3OutputLevel = 0; + sound3Last = 0; + sound3Bank = 0; + sound3DataSize = 0; + sound3ForcedOutput = 0; + + sound4On = 0; + sound4Clock = 0; + sound4ATL = 0; + sound4Skip = 0; + sound4Index = 0; + sound4ShiftRight = 0x7f; + sound4NSteps = 0; + sound4CountDown = 0; + sound4Continue = 0; + sound4EnvelopeVolume = 0; + sound4EnvelopeATL = 0; + sound4EnvelopeUpDown = 0; + sound4EnvelopeATLReload = 0; + + sound1On = 0; + sound2On = 0; + sound3On = 0; + sound4On = 0; + */ + + apu_out->clear(); + apu->reset(true); + + int addr = 0; + + while (addr < 0x30) { + switch (addr) + { + case 4: + case 9: + case 14: + case 19: + apu->write_register( 0, 0xFF10 + addr, 0 ); + break; + + default: + apu->write_register( 0, 0xFF10 + addr, sound_data [ addr ] ); + break; + } + addr++; + } + apu->write_register( 0, 0xFF1A, 0 ); + + addr = 0x20; + while (addr < 0x30) { + apu->write_register( 0, 0xFF10 + addr, sound_data [ addr ] ); + addr++; + } + + /*addr = 0x90; + + while(addr < 0xA0) { + ioMem[addr++] = 0x00; + ioMem[addr++] = 0xff; + } + + addr = 0; + while(addr < 0x20) { + sound3WaveRam[addr++] = 0x00; + sound3WaveRam[addr++] = 0xff; + } + */ + + memset(soundFinalWave, 0, soundBufferLen); + + memset(soundFilter, 0, sizeof(soundFilter)); + soundEchoIndex = 0; +} + +bool soundInit(bool gba) +{ + if(systemSoundInit()) { + memset(soundBuffer[0], 0, 735); + memset(soundBuffer[1], 0, 735); + memset(soundBuffer[2], 0, 735); + memset(soundBuffer[3], 0, 735); + + memset(soundFinalWave, 0, soundBufferLen); + +#if defined(_WIN32) && 0 + _fpreset(); // FUCKO, Direct3D display code does something dirty to FPU +#endif + + Stereo_Buffer * buf = new Stereo_Buffer; + apu = new Gb_Apu; + + buf->clock_rate( 4194304 ); + buf->set_sample_rate( 44100 / soundQuality , 1000 / 20 ); + buf->bass_freq( 120 ); + buf->set_channel_count(Gb_Apu::osc_count); + buf->clear(); + + apu_out = buf; + + apu->treble_eq( blip_eq_t( -1.0, 0, 44100 / soundQuality ) ); + apu->reset( gba ); + apu->volume( 1.0 ); + + for ( int i = apu->osc_count; i--; ) + { + Multi_Buffer::channel_t ch = apu_out->channel( i ); + apu->osc_output( i, ch.center, ch.left, ch.right ); + } + + int addr = 0; + + while (addr < 0x30) { + apu->write_register( 0, 0xFF10 + addr, sound_data [ addr ] ); + addr++; + } + + if (apu_saved) + { + int i; + apu->write_register( 0, 0xFF1A, 0x40); + for (i = 0; i < 16; i++) apu->write_register( 0, 0xFF30 + i, sound3WaveRam[i]); + apu->write_register( 0, 0xFF1A, 0); + for (i = 16; i < 32; i++) apu->write_register( 0, 0xFF30 - 16 + i, sound3WaveRam[i]); + apu->write_register( 0, 0xFF1A, 0x40); + } + else + { + apu->write_register( 0, 0xFF1A, 0 ); + + addr = 0x20; + while (addr < 0x30) { + apu->write_register( 0, 0xFF10 + addr, sound_data [ addr ] ); + addr++; + } + } + + soundPaused = true; + return true; + } + return false; +} + +void soundSetQuality(int quality) +{ + if(soundQuality != quality && systemCanChangeSoundQuality()) { + if(!soundOffFlag) + soundShutdown(); + soundQuality = quality; + soundNextPosition = 0; + if(!soundOffFlag) + soundInit(); + SOUND_CLOCK_TICKS = USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } else if(soundQuality != quality) { + soundNextPosition = 0; + SOUND_CLOCK_TICKS = USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } +} + +void soundSaveGame(gzFile gzFile) +{ + int i; + u8 j; + + j = soundRead(NR30); + if (!(j & 0x40)) soundEvent(NR30, u8(j | 0x40)); + for (i = 0; i < 16; i++) sound3WaveRam[i] = soundRead(i + 0x90); + soundEvent(NR30, u8(j & ~0x40)); + for (i = 16; i < 32; i++) sound3WaveRam[i] = soundRead(i - 16 + 0x90); + if (j & 0x40) soundEvent(NR30, j); + + utilWriteData(gzFile, soundSaveStruct); + utilWriteData(gzFile, soundSaveStructV2); + + utilGzWrite(gzFile, &soundQuality, sizeof(int)); +} + +void soundReadGame(gzFile gzFile, int version) +{ + utilReadData(gzFile, soundSaveStruct); + if(version >= SAVE_GAME_VERSION_3) { + utilReadData(gzFile, soundSaveStructV2); + } else { +/* sound3Bank = (ioMem[NR30] >> 6) & 1; + sound3DataSize = (ioMem[NR30] >> 5) & 1; + sound3ForcedOutput = (ioMem[NR32] >> 7) & 1;*/ + // nothing better to do here... + memcpy(&sound3WaveRam[0x00], &ioMem[0x90], 0x10); + memcpy(&sound3WaveRam[0x10], &ioMem[0x90], 0x10); + } + soundBufferIndex = soundIndex * 2; + + int quality = 1; + utilGzRead(gzFile, &quality, sizeof(int)); + soundSetQuality(quality); + apu->reset(true); + + int i; + u8 j = ioMem[NR30]; + for (i = NR10; i <= NR52; i++) soundEvent(i, ioMem[i]); + if (!(j & 0x40)) soundEventGB(0xFF1A, j | 0x40); + for (i = 0; i < 16; i++) soundEventGB(0xFF30 + i, sound3WaveRam[i]); + soundEventGB(0xFF1A, j & ~0x40); + for (i = 16; i < 32; i++) soundEventGB(0xFF30 - 16 + i, sound3WaveRam[i]); + if (j & 0x40) soundEventGB(0xFF1A, j); + + //sound1Wave = soundWavePattern[ioMem[NR11] >> 6]; + //sound2Wave = soundWavePattern[ioMem[NR21] >> 6]; +} \ No newline at end of file diff --git a/src/Sound.h b/src/Sound.h new file mode 100644 index 00000000..4b541073 --- /dev/null +++ b/src/Sound.h @@ -0,0 +1,95 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_SOUND_H +#define VBA_SOUND_H + +#define NR10 0x60 +#define NR11 0x62 +#define NR12 0x63 +#define NR13 0x64 +#define NR14 0x65 +#define NR21 0x68 +#define NR22 0x69 +#define NR23 0x6c +#define NR24 0x6d +#define NR30 0x70 +#define NR31 0x72 +#define NR32 0x73 +#define NR33 0x74 +#define NR34 0x75 +#define NR41 0x78 +#define NR42 0x79 +#define NR43 0x7c +#define NR44 0x7d +#define NR50 0x80 +#define NR51 0x81 +#define NR52 0x84 +#define SGCNT0_H 0x82 +#define FIFOA_L 0xa0 +#define FIFOA_H 0xa2 +#define FIFOB_L 0xa4 +#define FIFOB_H 0xa6 + +extern void soundTick(); +extern void soundShutdown(); +extern bool soundInit(bool gba=true); +extern void soundPause(); +extern void soundResume(); +extern void soundEnable(int); +extern void soundDisable(int); +extern int soundGetEnable(); +extern void soundReset(); +extern void soundSaveGame(gzFile); +extern void soundReadGame(gzFile, int); +extern void soundEvent(u32, u8); +extern void soundEvent(u32, u16); +extern void soundTimerOverflow(int); +extern void soundSetQuality(int); + +//extern int SOUND_TICKS; +extern int SOUND_CLOCK_TICKS; +extern u8 soundRead(u32); +extern u16 soundRead16(u32); +extern u32 soundRead32(u32); +extern int soundTicks; +extern int soundPaused; +extern bool soundOffFlag; +extern int soundQuality; +extern int soundBufferLen; +extern int soundBufferTotalLen; +extern u32 soundNextPosition; +extern u16 soundFinalWave[1470]; +extern int soundVolume; +extern int soundInterpolation; + +extern bool soundEcho; +extern bool soundLowPass; +extern bool soundReverse; + +#include "Gb_Apu/Multi_Buffer.h" +#include "Gb_Apu/Gb_Apu.h" + +extern Multi_Buffer * apu_out; +extern Gb_Apu * apu; + +extern const BOOST::uint8_t sound_data [Gb_Apu::register_count]; + + +#endif // VBA_SOUND_H diff --git a/src/Sram.cpp b/src/Sram.cpp new file mode 100644 index 00000000..df582240 --- /dev/null +++ b/src/Sram.cpp @@ -0,0 +1,39 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "GBA.h" +#include "Globals.h" +#include "Flash.h" +#include "Sram.h" + +u8 sramRead(u32 address) +{ + return flashSaveMemory[address & 0xFFFF]; +} +void sramDelayedWrite(u32 address, u8 byte) +{ + saveType = 1; + cpuSaveGameFunc = sramWrite; + sramWrite(address, byte); +} + +void sramWrite(u32 address, u8 byte) +{ + flashSaveMemory[address & 0xFFFF] = byte; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; +} \ No newline at end of file diff --git a/src/Sram.h b/src/Sram.h new file mode 100644 index 00000000..c8a90192 --- /dev/null +++ b/src/Sram.h @@ -0,0 +1,27 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_SRAM_H +#define VBA_SRAM_H + +extern u8 sramRead(u32 address); +extern void sramWrite(u32 address, u8 byte); +extern void sramDelayedWrite(u32 address, u8 byte); + +#endif // VBA_SRAM_H \ No newline at end of file diff --git a/src/System.h b/src/System.h new file mode 100644 index 00000000..2cc6d495 --- /dev/null +++ b/src/System.h @@ -0,0 +1,125 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_SYSTEM_H +#define VBA_SYSTEM_H + +#include "unzip.h" + +#ifndef NULL +#define NULL 0 +#endif + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; + +#ifdef _MSC_VER +typedef unsigned __int64 u64; +#else +typedef unsigned long long u64; +#endif + +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; + +#ifdef _MSC_VER +typedef signed __int64 s64; +#else +typedef signed long long s64; +#endif + +struct EmulatedSystem { + // main emulation function + void (*emuMain)(int); + // reset emulator + void (*emuReset)(); + // clean up memory + void (*emuCleanUp)(); + // load battery file + bool (*emuReadBattery)(const char *); + // write battery file + bool (*emuWriteBattery)(const char *); + // load state + bool (*emuReadState)(const char *); + // save state + bool (*emuWriteState)(const char *); + // load memory state (rewind) + bool (*emuReadMemState)(char *, int); + // write memory state (rewind) + bool (*emuWriteMemState)(char *, int); + // write PNG file + bool (*emuWritePNG)(const char *); + // write BMP file + bool (*emuWriteBMP)(const char *); + // emulator update CPSR (ARM only) + void (*emuUpdateCPSR)(); + // emulator has debugger + bool emuHasDebugger; + // clock ticks to emulate + int emuCount; +}; + +extern void log(const char *,...); + +extern bool systemPauseOnFrame(); +extern void systemGbPrint(u8 *,int,int,int,int); +extern void systemScreenCapture(int); +extern void systemDrawScreen(); +// updates the joystick data +extern bool systemReadJoypads(); +// return information about the given joystick, -1 for default joystick +extern u32 systemReadJoypad(int); +extern u32 systemGetClock(); +extern void systemMessage(int, const char *, ...); +extern void systemSetTitle(const char *); +extern void systemWriteDataToSoundBuffer(); +extern void systemSoundShutdown(); +extern void systemSoundPause(); +extern void systemSoundResume(); +extern void systemSoundReset(); +extern bool systemSoundInit(); +extern void systemScreenMessage(const char *); +extern void systemUpdateMotionSensor(); +extern int systemGetSensorX(); +extern int systemGetSensorY(); +extern bool systemCanChangeSoundQuality(); +extern void systemShowSpeed(int); +extern void system10Frames(int); +extern void systemFrame(); +extern void systemGbBorderOn(); + +extern bool systemSoundOn; +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; +extern u16 systemGbPalette[24]; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; +extern int systemColorDepth; +extern int systemDebug; +extern int systemVerbose; +extern int systemFrameSkip; +extern int systemSaveUpdateCounter; + +#define SYSTEM_SAVE_UPDATED 30 +#define SYSTEM_SAVE_NOT_UPDATED 0 + +#endif //VBA_SYSTEM_H diff --git a/src/Text.cpp b/src/Text.cpp new file mode 100644 index 00000000..811a12c3 --- /dev/null +++ b/src/Text.cpp @@ -0,0 +1,157 @@ +/* FCE Ultra - NES/Famicom Emulator + * + * Copyright notice for this file: + * Copyright (C) 2002 Ben Parnell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Code originally from fceu/drawing.h file, adapted by Forgotten + */ +#include "System.h" + +extern int RGB_LOW_BITS_MASK; + +static const u8 fontdata2[2048] = { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x81,0xa5,0x81,0xbd,0x99,0x81,0x7e,0x7e,0xff,0xdb,0xff,0xc3,0xe7,0xff,0x7e,0x36,0x7f,0x7f,0x7f,0x3e,0x1c,0x08,0x00,0x08,0x1c,0x3e,0x7f,0x3e,0x1c,0x08,0x00,0x1c,0x3e,0x1c,0x7f,0x7f,0x3e,0x1c,0x3e,0x08,0x08,0x1c,0x3e,0x7f,0x3e,0x1c,0x3e,0x00,0x00,0x18,0x3c,0x3c,0x18,0x00,0x00,0xff,0xff,0xe7,0xc3,0xc3,0xe7,0xff,0xff,0x00,0x3c,0x66,0x42,0x42,0x66,0x3c,0x00,0xff,0xc3,0x99,0xbd,0xbd,0x99,0xc3,0xff,0xf0,0xe0,0xf0,0xbe,0x33,0x33,0x33,0x1e,0x3c,0x66,0x66,0x66,0x3c,0x18,0x7e,0x18,0xfc,0xcc,0xfc,0x0c,0x0c,0x0e,0x0f,0x07,0xfe,0xc6,0xfe,0xc6,0xc6,0xe6,0x67,0x03,0x99,0x5a,0x3c,0xe7,0xe7,0x3c,0x5a,0x99,0x01,0x07,0x1f,0x7f,0x1f,0x07,0x01,0x00,0x40,0x70,0x7c,0x7f,0x7c,0x70,0x40,0x00,0x18,0x3c,0x7e,0x18,0x18,0x7e,0x3c,0x18,0x66,0x66,0x66,0x66,0x66,0x00,0x66,0x00,0xfe,0xdb,0xdb,0xde,0xd8,0xd8,0xd8,0x00,0x7c,0xc6,0x1c,0x36,0x36,0x1c,0x33,0x1e,0x00,0x00,0x00,0x00,0x7e,0x7e,0x7e,0x00,0x18,0x3c,0x7e,0x18,0x7e,0x3c,0x18,0xff,0x18,0x3c,0x7e,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x7e,0x3c,0x18,0x00,0x00,0x18,0x30,0x7f,0x30,0x18,0x00,0x00,0x00,0x0c,0x06,0x7f,0x06,0x0c,0x00,0x00,0x00,0x00,0x03,0x03,0x03,0x7f,0x00,0x00,0x00,0x24,0x66,0xff,0x66,0x24,0x00,0x00,0x00,0x18,0x3c,0x7e,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0x7e,0x3c,0x18,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x1e,0x1e,0x0c,0x0c,0x00,0x0c,0x00,0x36,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x36,0x36,0x7f,0x36,0x7f,0x36,0x36,0x00,0x0c,0x3e,0x03,0x1e,0x30,0x1f,0x0c,0x00,0x00,0x63,0x33,0x18,0x0c,0x66,0x63,0x00,0x1c,0x36,0x1c,0x6e,0x3b,0x33,0x6e,0x00,0x06,0x06,0x03,0x00,0x00,0x00,0x00,0x00,0x18,0x0c,0x06,0x06,0x06,0x0c,0x18,0x00,0x06,0x0c,0x18,0x18,0x18,0x0c,0x06,0x00,0x00,0x66,0x3c,0xff,0x3c,0x66,0x00,0x00,0x00,0x0c,0x0c,0x3f,0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x0c,0x06,0x00,0x00,0x00,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x0c,0x00,0x60,0x30,0x18,0x0c,0x06,0x03,0x01,0x00,0x3e,0x63,0x73,0x7b,0x6f,0x67,0x3e,0x00,0x0c,0x0e,0x0c,0x0c,0x0c,0x0c,0x3f,0x00,0x1e,0x33,0x30,0x1c,0x06,0x33,0x3f,0x00,0x1e,0x33,0x30,0x1c,0x30,0x33,0x1e,0x00,0x38,0x3c,0x36,0x33,0x7f,0x30,0x78,0x00,0x3f,0x03,0x1f,0x30,0x30,0x33,0x1e,0x00,0x1c,0x06,0x03,0x1f,0x33,0x33,0x1e,0x00,0x3f,0x33,0x30,0x18,0x0c,0x0c,0x0c,0x00,0x1e,0x33,0x33,0x1e,0x33,0x33,0x1e,0x00,0x1e,0x33,0x33,0x3e,0x30,0x18,0x0e,0x00,0x00,0x0c,0x0c,0x00,0x00,0x0c,0x0c,0x00,0x00,0x0c,0x0c,0x00,0x00,0x0c,0x0c,0x06,0x18,0x0c,0x06,0x03,0x06,0x0c,0x18,0x00,0x00,0x00,0x3f,0x00,0x00,0x3f,0x00,0x00,0x06,0x0c,0x18,0x30,0x18,0x0c,0x06,0x00,0x1e,0x33,0x30,0x18,0x0c,0x00,0x0c,0x00, + 0x3e,0x63,0x7b,0x7b,0x7b,0x03,0x1e,0x00,0x0c,0x1e,0x33,0x33,0x3f,0x33,0x33,0x00,0x3f,0x66,0x66,0x3e,0x66,0x66,0x3f,0x00,0x3c,0x66,0x03,0x03,0x03,0x66,0x3c,0x00,0x1f,0x36,0x66,0x66,0x66,0x36,0x1f,0x00,0x7f,0x46,0x16,0x1e,0x16,0x46,0x7f,0x00,0x7f,0x46,0x16,0x1e,0x16,0x06,0x0f,0x00,0x3c,0x66,0x03,0x03,0x73,0x66,0x7c,0x00,0x33,0x33,0x33,0x3f,0x33,0x33,0x33,0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0x0c,0x1e,0x00,0x78,0x30,0x30,0x30,0x33,0x33,0x1e,0x00,0x67,0x66,0x36,0x1e,0x36,0x66,0x67,0x00,0x0f,0x06,0x06,0x06,0x46,0x66,0x7f,0x00,0x63,0x77,0x7f,0x7f,0x6b,0x63,0x63,0x00,0x63,0x67,0x6f,0x7b,0x73,0x63,0x63,0x00,0x1c,0x36,0x63,0x63,0x63,0x36,0x1c,0x00,0x3f,0x66,0x66,0x3e,0x06,0x06,0x0f,0x00,0x1e,0x33,0x33,0x33,0x3b,0x1e,0x38,0x00,0x3f,0x66,0x66,0x3e,0x36,0x66,0x67,0x00,0x1e,0x33,0x07,0x0e,0x38,0x33,0x1e,0x00,0x3f,0x2d,0x0c,0x0c,0x0c,0x0c,0x1e,0x00,0x33,0x33,0x33,0x33,0x33,0x33,0x3f,0x00,0x33,0x33,0x33,0x33,0x33,0x1e,0x0c,0x00,0x63,0x63,0x63,0x6b,0x7f,0x77,0x63,0x00,0x63,0x63,0x36,0x1c,0x1c,0x36,0x63,0x00,0x33,0x33,0x33,0x1e,0x0c,0x0c,0x1e,0x00,0x7f,0x63,0x31,0x18,0x4c,0x66,0x7f,0x00,0x1e,0x06,0x06,0x06,0x06,0x06,0x1e,0x00,0x03,0x06,0x0c,0x18,0x30,0x60,0x40,0x00,0x1e,0x18,0x18,0x18,0x18,0x18,0x1e,0x00,0x08,0x1c,0x36,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff, + 0x0c,0x0c,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1e,0x30,0x3e,0x33,0x6e,0x00,0x07,0x06,0x06,0x3e,0x66,0x66,0x3b,0x00,0x00,0x00,0x1e,0x33,0x03,0x33,0x1e,0x00,0x38,0x30,0x30,0x3e,0x33,0x33,0x6e,0x00,0x00,0x00,0x1e,0x33,0x3f,0x03,0x1e,0x00,0x1c,0x36,0x06,0x0f,0x06,0x06,0x0f,0x00,0x00,0x00,0x6e,0x33,0x33,0x3e,0x30,0x1f,0x07,0x06,0x36,0x6e,0x66,0x66,0x67,0x00,0x0c,0x00,0x0e,0x0c,0x0c,0x0c,0x1e,0x00,0x30,0x00,0x30,0x30,0x30,0x33,0x33,0x1e,0x07,0x06,0x66,0x36,0x1e,0x36,0x67,0x00,0x0e,0x0c,0x0c,0x0c,0x0c,0x0c,0x1e,0x00,0x00,0x00,0x33,0x7f,0x7f,0x6b,0x63,0x00,0x00,0x00,0x1f,0x33,0x33,0x33,0x33,0x00,0x00,0x00,0x1e,0x33,0x33,0x33,0x1e,0x00,0x00,0x00,0x3b,0x66,0x66,0x3e,0x06,0x0f,0x00,0x00,0x6e,0x33,0x33,0x3e,0x30,0x78,0x00,0x00,0x3b,0x6e,0x66,0x06,0x0f,0x00,0x00,0x00,0x3e,0x03,0x1e,0x30,0x1f,0x00,0x08,0x0c,0x3e,0x0c,0x0c,0x2c,0x18,0x00,0x00,0x00,0x33,0x33,0x33,0x33,0x6e,0x00,0x00,0x00,0x33,0x33,0x33,0x1e,0x0c,0x00,0x00,0x00,0x63,0x6b,0x7f,0x7f,0x36,0x00,0x00,0x00,0x63,0x36,0x1c,0x36,0x63,0x00,0x00,0x00,0x33,0x33,0x33,0x3e,0x30,0x1f,0x00,0x00,0x3f,0x19,0x0c,0x26,0x3f,0x00,0x38,0x0c,0x0c,0x07,0x0c,0x0c,0x38,0x00,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00,0x07,0x0c,0x0c,0x38,0x0c,0x0c,0x07,0x00,0x6e,0x3b,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x1c,0x36,0x63,0x63,0x7f,0x00, + 0x1e,0x33,0x03,0x33,0x1e,0x18,0x30,0x1e,0x00,0x33,0x00,0x33,0x33,0x33,0x7e,0x00,0x38,0x00,0x1e,0x33,0x3f,0x03,0x1e,0x00,0x7e,0xc3,0x3c,0x60,0x7c,0x66,0xfc,0x00,0x33,0x00,0x1e,0x30,0x3e,0x33,0x7e,0x00,0x07,0x00,0x1e,0x30,0x3e,0x33,0x7e,0x00,0x0c,0x0c,0x1e,0x30,0x3e,0x33,0x7e,0x00,0x00,0x00,0x1e,0x03,0x03,0x1e,0x30,0x1c,0x7e,0xc3,0x3c,0x66,0x7e,0x06,0x3c,0x00,0x33,0x00,0x1e,0x33,0x3f,0x03,0x1e,0x00,0x07,0x00,0x1e,0x33,0x3f,0x03,0x1e,0x00,0x33,0x00,0x0e,0x0c,0x0c,0x0c,0x1e,0x00,0x3e,0x63,0x1c,0x18,0x18,0x18,0x3c,0x00,0x07,0x00,0x0e,0x0c,0x0c,0x0c,0x1e,0x00,0x63,0x1c,0x36,0x63,0x7f,0x63,0x63,0x00,0x0c,0x0c,0x00,0x1e,0x33,0x3f,0x33,0x00,0x38,0x00,0x3f,0x06,0x1e,0x06,0x3f,0x00,0x00,0x00,0xfe,0x30,0xfe,0x33,0xfe,0x00,0x7c,0x36,0x33,0x7f,0x33,0x33,0x73,0x00,0x1e,0x33,0x00,0x1e,0x33,0x33,0x1e,0x00,0x00,0x33,0x00,0x1e,0x33,0x33,0x1e,0x00,0x00,0x07,0x00,0x1e,0x33,0x33,0x1e,0x00,0x1e,0x33,0x00,0x33,0x33,0x33,0x7e,0x00,0x00,0x07,0x00,0x33,0x33,0x33,0x7e,0x00,0x00,0x33,0x00,0x33,0x33,0x3e,0x30,0x1f,0xc3,0x18,0x3c,0x66,0x66,0x3c,0x18,0x00,0x33,0x00,0x33,0x33,0x33,0x33,0x1e,0x00,0x18,0x18,0x7e,0x03,0x03,0x7e,0x18,0x18,0x1c,0x36,0x26,0x0f,0x06,0x67,0x3f,0x00,0x33,0x33,0x1e,0x3f,0x0c,0x3f,0x0c,0x0c,0x1f,0x33,0x33,0x5f,0x63,0xf3,0x63,0xe3,0x70,0xd8,0x18,0x3c,0x18,0x18,0x1b,0x0e, + 0x38,0x00,0x1e,0x30,0x3e,0x33,0x7e,0x00,0x1c,0x00,0x0e,0x0c,0x0c,0x0c,0x1e,0x00,0x00,0x38,0x00,0x1e,0x33,0x33,0x1e,0x00,0x00,0x38,0x00,0x33,0x33,0x33,0x7e,0x00,0x00,0x1f,0x00,0x1f,0x33,0x33,0x33,0x00,0x3f,0x00,0x33,0x37,0x3f,0x3b,0x33,0x00,0x3c,0x36,0x36,0x7c,0x00,0x7e,0x00,0x00,0x1c,0x36,0x36,0x1c,0x00,0x3e,0x00,0x00,0x0c,0x00,0x0c,0x06,0x03,0x33,0x1e,0x00,0x00,0x00,0x00,0x3f,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x3f,0x30,0x30,0x00,0x00,0xc3,0x63,0x33,0x7b,0xcc,0x66,0x33,0xf0,0xc3,0x63,0x33,0xdb,0xec,0xf6,0xf3,0xc0,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x00,0x00,0xcc,0x66,0x33,0x66,0xcc,0x00,0x00,0x00,0x33,0x66,0xcc,0x66,0x33,0x00,0x00,0x44,0x11,0x44,0x11,0x44,0x11,0x44,0x11,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xdb,0xee,0xdb,0x77,0xdb,0xee,0xdb,0x77,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1f,0x18,0x18,0x18,0x18,0x18,0x1f,0x18,0x1f,0x18,0x18,0x18,0x6c,0x6c,0x6c,0x6c,0x6f,0x6c,0x6c,0x6c,0x00,0x00,0x00,0x00,0x7f,0x6c,0x6c,0x6c,0x00,0x00,0x1f,0x18,0x1f,0x18,0x18,0x18,0x6c,0x6c,0x6f,0x60,0x6f,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x00,0x00,0x7f,0x60,0x6f,0x6c,0x6c,0x6c,0x6c,0x6c,0x6f,0x60,0x7f,0x00,0x00,0x00,0x6c,0x6c,0x6c,0x6c,0x7f,0x00,0x00,0x00,0x18,0x18,0x1f,0x18,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x18,0x18,0x18, + 0x18,0x18,0x18,0x18,0xf8,0x00,0x00,0x00,0x18,0x18,0x18,0x18,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xf8,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x18,0x18,0x18,0x18,0xff,0x18,0x18,0x18,0x18,0x18,0xf8,0x18,0xf8,0x18,0x18,0x18,0x6c,0x6c,0x6c,0x6c,0xec,0x6c,0x6c,0x6c,0x6c,0x6c,0xec,0x0c,0xfc,0x00,0x00,0x00,0x00,0x00,0xfc,0x0c,0xec,0x6c,0x6c,0x6c,0x6c,0x6c,0xef,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xef,0x6c,0x6c,0x6c,0x6c,0x6c,0xec,0x0c,0xec,0x6c,0x6c,0x6c,0x00,0x00,0xff,0x00,0xff,0x00,0x00,0x00,0x6c,0x6c,0xef,0x00,0xef,0x6c,0x6c,0x6c,0x18,0x18,0xff,0x00,0xff,0x00,0x00,0x00,0x6c,0x6c,0x6c,0x6c,0xff,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xff,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0xff,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0xfc,0x00,0x00,0x00,0x18,0x18,0xf8,0x18,0xf8,0x00,0x00,0x00,0x00,0x00,0xf8,0x18,0xf8,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0xfc,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0xff,0x6c,0x6c,0x6c,0x18,0x18,0xff,0x18,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x18,0x18,0x18,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00, + 0x00,0x00,0x6e,0x3b,0x13,0x3b,0x6e,0x00,0x00,0x1e,0x33,0x1f,0x33,0x1f,0x03,0x03,0x00,0x3f,0x33,0x03,0x03,0x03,0x03,0x00,0x00,0x7f,0x36,0x36,0x36,0x36,0x36,0x00,0x3f,0x33,0x06,0x0c,0x06,0x33,0x3f,0x00,0x00,0x00,0x7e,0x1b,0x1b,0x1b,0x0e,0x00,0x00,0x66,0x66,0x66,0x66,0x3e,0x06,0x03,0x00,0x6e,0x3b,0x18,0x18,0x18,0x18,0x00,0x3f,0x0c,0x1e,0x33,0x33,0x1e,0x0c,0x3f,0x1c,0x36,0x63,0x7f,0x63,0x36,0x1c,0x00,0x1c,0x36,0x63,0x63,0x36,0x36,0x77,0x00,0x38,0x0c,0x18,0x3e,0x33,0x33,0x1e,0x00,0x00,0x00,0x7e,0xdb,0xdb,0x7e,0x00,0x00,0x60,0x30,0x7e,0xdb,0xdb,0x7e,0x06,0x03,0x1c,0x06,0x03,0x1f,0x03,0x06,0x1c,0x00,0x1e,0x33,0x33,0x33,0x33,0x33,0x33,0x00,0x00,0x3f,0x00,0x3f,0x00,0x3f,0x00,0x00,0x0c,0x0c,0x3f,0x0c,0x0c,0x00,0x3f,0x00,0x06,0x0c,0x18,0x0c,0x06,0x00,0x3f,0x00,0x18,0x0c,0x06,0x0c,0x18,0x00,0x3f,0x00,0x70,0xd8,0xd8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1b,0x1b,0x0e,0x0c,0x0c,0x00,0x3f,0x00,0x0c,0x0c,0x00,0x00,0x6e,0x3b,0x00,0x6e,0x3b,0x00,0x00,0x1c,0x36,0x36,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0xf0,0x30,0x30,0x30,0x37,0x36,0x3c,0x38,0x1e,0x36,0x36,0x36,0x36,0x00,0x00,0x00,0x0e,0x18,0x0c,0x06,0x1e,0x00,0x00,0x00,0x00,0x00,0x3c,0x3c,0x3c,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 +}; + +static void drawTextInternal(u8 *screen, int pitch, int x, int y, + const char *string, bool trans) +{ + screen += y*pitch; + int inc = 2; + switch(systemColorDepth) { + case 24: + inc = 3; + break; + case 32: + inc = 4; + break; + } + screen += x*inc; + + switch(systemColorDepth) { + case 16: + { + while(*string) { + char c = *string++; + u8 *scr = screen; + + u16 mask = ~RGB_LOW_BITS_MASK; + int h, w; + u16 *s = (u16 *)scr; + for (h = 0; h < 8; h++) { + for (w = 0; w < 8; w++, s++) { + int on = (fontdata2[(c<<3)+h]>>w)&1; + + if(trans) { + if(on) + *s = ((0xf) << systemRedShift) + + ((*s & mask) >>1); + } else { + if(on) + *s = (0x1f) << systemRedShift; + } + } + scr += pitch; + s = (u16 *)scr; + } + screen += inc*8; + } + } + break; + case 24: + { + while(*string) { + char c = *string++; + u8 *scr = screen; + + int h, w; + u8 *s = (u8 *)scr; + for (h = 0; h < 8; h++) { + for (w = 0; w < 8; w++, s+=3) { + int on = (fontdata2[(c<<3)+h]>>w)&1; + + if(trans) { + if(on) { + u32 color = (0x1f) << systemRedShift; + *s = ((color & 255)>>1)+(*s>>1); + *(s+1) = (((color >> 8) & 255)>>1)+(*(s+1)>>1); + *(s+2) = (((color >> 16) & 255)>>1)+(*(s+2)>>1); + } + } else { + if(on) { + u32 color = (0x1f) << systemRedShift; + *s = (color & 255); + *(s+1) = (color >> 8) & 255; + *(s+2) = (color >> 16) & 255; + } + } + } + scr += pitch; + s = (u8 *)scr; + } + screen += inc*8; + } + } + break; + case 32: + { + while(*string) { + char c = *string++; + u8 *scr = screen; + + int h, w; + u32 mask = 0xfefefe; + u32 *s = (u32 *)scr; + for (h = 0; h < 8; h++) { + for (w = 0; w < 8; w++, s++) { + int on = (fontdata2[(c<<3)+h]>>w)&1; + + if(trans) { + if(on) + *s = ((0xf) << systemRedShift) + ((*s & mask)>>1); + } else { + if(on) + *s = (0x1f) << systemRedShift; + } + } + scr += pitch; + s = (u32 *)scr; + } + screen += inc*8; + } + } + break; + } +} + +void drawText(u8 *screen, int pitch, int x, int y, const char *string) +{ + drawTextInternal(screen, pitch, x, y, string, false); +} + +void drawTextTransp(u8 *screen, int pitch, int x, int y, const char *string) +{ + drawTextInternal(screen, pitch, x, y, string, true); +} diff --git a/src/Text.h b/src/Text.h new file mode 100644 index 00000000..cbfe8be8 --- /dev/null +++ b/src/Text.h @@ -0,0 +1,21 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern void drawText(u8 *, int, int, int, const char *); +extern void drawTextTransp(u8 *, int, int, int, const char *); diff --git a/src/Util.cpp b/src/Util.cpp new file mode 100644 index 00000000..328ed142 --- /dev/null +++ b/src/Util.cpp @@ -0,0 +1,1115 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include + +extern "C" { +#include +} + +#if 0 +#include "unrarlib.h" +#endif + +#include "System.h" +#include "NLS.h" +#include "Util.h" +#include "Flash.h" +#include "GBA.h" +#include "Globals.h" +#include "RTC.h" +#include "Port.h" +#include "memgzio.h" +#include "gbafilter.h" + +#ifndef _MSC_VER +#define _stricmp strcasecmp +#endif // ! _MSC_VER + +extern int systemColorDepth; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; + +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; + +static int (ZEXPORT *utilGzWriteFunc)(gzFile, const voidp, unsigned int) = NULL; +static int (ZEXPORT *utilGzReadFunc)(gzFile, voidp, unsigned int) = NULL; +static int (ZEXPORT *utilGzCloseFunc)(gzFile) = NULL; + +bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix) +{ + u8 writeBuffer[512 * 3]; + + FILE *fp = fopen(fileName,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); + return false; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return false; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return false; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return false; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + switch(systemColorDepth) { + case 16: + { + u16 *p = (u16 *)(pix+(w+2)*2); // skip first black line + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u16 v = *p++; + + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + } + break; + case 24: + { + u8 *pixU8 = (u8 *)pix; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + if(systemRedShift < systemBlueShift) { + *b++ = *pixU8++; // R + *b++ = *pixU8++; // G + *b++ = *pixU8++; // B + } else { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + } + break; + case 32: + { + u32 *pixU32 = (u32 *)(pix+4*(w+1)); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u32 v = *pixU32++; + + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + } + pixU32++; + + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + } + break; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); + + return true; +} + +void utilPutDword(u8 *p, u32 value) +{ + *p++ = value & 255; + *p++ = (value >> 8) & 255; + *p++ = (value >> 16) & 255; + *p = (value >> 24) & 255; +} + +void utilPutWord(u8 *p, u16 value) +{ + *p++ = value & 255; + *p = (value >> 8) & 255; +} + +void utilWriteBMP(char *buf, int w, int h, u8 *pix) +{ + u8 *b = (u8 *)buf; + + int sizeX = w; + int sizeY = h; + + switch(systemColorDepth) { + case 16: + { + u16 *p = (u16 *)(pix+(w+2)*(h)*2); // skip first black line + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u16 v = *p++; + + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + p -= 2*(w+2); + } + } + break; + case 24: + { + u8 *pixU8 = (u8 *)pix+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + if(systemRedShift > systemBlueShift) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } else { + int red = *pixU8++; + int green = *pixU8++; + int blue = *pixU8++; + + *b++ = blue; + *b++ = green; + *b++ = red; + } + } + pixU8 -= 2*3*w; + } + } + break; + case 32: + { + u32 *pixU32 = (u32 *)(pix+4*(w+1)*(h)); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u32 v = *pixU32++; + + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + pixU32++; + pixU32 -= 2*(w+1); + } + } + break; + } +} + +bool utilWriteBMPFile(const char *fileName, int w, int h, u8 *pix) +{ + u8 writeBuffer[512 * 3]; + + FILE *fp = fopen(fileName,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); + return false; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + // u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x36); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + switch(systemColorDepth) { + case 16: + { + u16 *p = (u16 *)(pix+(w+2)*(h)*2); // skip first black line + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u16 v = *p++; + + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + p -= 2*(w+2); + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + } + break; + case 24: + { + u8 *pixU8 = (u8 *)pix+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + if(systemRedShift > systemBlueShift) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } else { + int red = *pixU8++; + int green = *pixU8++; + int blue = *pixU8++; + + *b++ = blue; + *b++ = green; + *b++ = red; + } + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + } + break; + case 32: + { + u32 *pixU32 = (u32 *)(pix+4*(w+1)*(h)); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u32 v = *pixU32++; + + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + pixU32++; + pixU32 -= 2*(w+1); + + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + } + break; + } + + fclose(fp); + + return true; +} + +static int utilReadInt2(FILE *f) +{ + int res = 0; + int c = fgetc(f); + if(c == EOF) + return -1; + res = c; + c = fgetc(f); + if(c == EOF) + return -1; + return c + (res<<8); +} + +static int utilReadInt3(FILE *f) +{ + int res = 0; + int c = fgetc(f); + if(c == EOF) + return -1; + res = c; + c = fgetc(f); + if(c == EOF) + return -1; + res = c + (res<<8); + c = fgetc(f); + if(c == EOF) + return -1; + return c + (res<<8); +} + +void utilApplyIPS(const char *ips, u8 **r, int *s) +{ + // from the IPS spec at http://zerosoft.zophar.net/ips.htm + FILE *f = fopen(ips, "rb"); + if(!f) + return; + u8 *rom = *r; + int size = *s; + if(fgetc(f) == 'P' && + fgetc(f) == 'A' && + fgetc(f) == 'T' && + fgetc(f) == 'C' && + fgetc(f) == 'H') { + int b; + int offset; + int len; + for(;;) { + // read offset + offset = utilReadInt3(f); + // if offset == EOF, end of patch + if(offset == 0x454f46) + break; + // read length + len = utilReadInt2(f); + if(!len) { + // len == 0, RLE block + len = utilReadInt2(f); + // byte to fill + int c = fgetc(f); + if(c == -1) + break; + b = (u8)c; + } else + b= -1; + // check if we need to reallocate our ROM + if((offset + len) >= size) { + size *= 2; + rom = (u8 *)realloc(rom, size); + *r = rom; + *s = size; + } + if(b == -1) { + // normal block, just read the data + if(fread(&rom[offset], 1, len, f) != (size_t)len) + break; + } else { + // fill the region with the given byte + while(len--) { + rom[offset++] = b; + } + } + } + } + // close the file + fclose(f); +} + +extern bool cpuIsMultiBoot; + +bool utilIsGBAImage(const char * file) +{ + cpuIsMultiBoot = false; + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gba") == 0) + return true; + if(_stricmp(p, ".agb") == 0) + return true; + if(_stricmp(p, ".bin") == 0) + return true; + if(_stricmp(p, ".elf") == 0) + return true; + if(_stricmp(p, ".mb") == 0) { + cpuIsMultiBoot = true; + return true; + } + } + } + + return false; +} + +bool utilIsGBImage(const char * file) +{ + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gb") == 0) + return true; + if(_stricmp(p, ".gbc") == 0) + return true; + if(_stricmp(p, ".cgb") == 0) + return true; + if(_stricmp(p, ".sgb") == 0) + return true; + } + } + + return false; +} + +bool utilIsZipFile(const char *file) +{ + if(strlen(file) > 4) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".zip") == 0) + return true; + } + } + + return false; +} + +#if 0 +bool utilIsRarFile(const char *file) +{ + if(strlen(file) > 4) { + const char * p = strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".rar") == 0) + return true; + } + } + + return false; +} +#endif + +bool utilIs7ZipFile(const char *file) +{ + if(strlen(file) > 3) { + const char * p = strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".7z") == 0) + return true; + } + } + + return false; +} + +bool utilIsGzipFile(const char *file) +{ + if(strlen(file) > 3) { + char * p = (char *)strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gz") == 0) + return true; + if(_stricmp(p, ".z") == 0) + return true; + } + } + + return false; +} + +void utilGetBaseName(const char *file, char *buffer) +{ + strcpy(buffer, file); + + if(utilIsGzipFile(file)) { + char *p = strrchr(buffer, '.'); + + if(p) + *p = 0; + } +} + +IMAGE_TYPE utilFindType(const char *file) +{ + char buffer[2048]; + + if(utilIsZipFile(file)) { + unzFile unz = unzOpen(file); + + if(unz == NULL) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file); + return IMAGE_UNKNOWN; + } + + int r = unzGoToFirstFile(unz); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(MSG_BAD_ARCHIVE_FILE, N_("Bad ZIP file %s"), file); + return IMAGE_UNKNOWN; + } + + IMAGE_TYPE found = IMAGE_UNKNOWN; + + unz_file_info info; + + while(true) { + r = unzGetCurrentFileInfo(unz, + &info, + buffer, + sizeof(buffer), + NULL, + 0, + NULL, + 0); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(MSG_BAD_ARCHIVE_FILE, N_("Bad ZIP file %s"), file); + return IMAGE_UNKNOWN; + } + + if(utilIsGBAImage(buffer)) { + found = IMAGE_GBA; + break; + } + + if(utilIsGBImage(buffer)) { + found = IMAGE_GB; + break; + } + + r = unzGoToNextFile(unz); + + if(r != UNZ_OK) + break; + } + unzClose(unz); + + if(found == IMAGE_UNKNOWN) { + systemMessage(MSG_NO_IMAGE_ON_ARCHIVE, + N_("No image found on ZIP file %s"), file); + return found; + } + return found; +#if 0 + } else if(utilIsRarFile(file)) { + IMAGE_TYPE found = IMAGE_UNKNOWN; + + ArchiveList_struct *rarList = NULL; + if(urarlib_list((void *)file, (ArchiveList_struct *)&rarList)) { + ArchiveList_struct *p = rarList; + + while(p) { + if(utilIsGBAImage(p->item.Name)) { + found = IMAGE_GBA; + break; + } + + if(utilIsGBImage(p->item.Name)) { + found = IMAGE_GB; + break; + } + p = p->next; + } + + urarlib_freelist(rarList); + } + return found; +#endif + } else { + if(utilIsGzipFile(file)) + utilGetBaseName(file, buffer); + else + strcpy(buffer, file); + + if(utilIsGBAImage(buffer)) + return IMAGE_GBA; + if(utilIsGBImage(buffer)) + return IMAGE_GB; + } + return IMAGE_UNKNOWN; +} + +static int utilGetSize(int size) +{ + int res = 1; + while(res < size) + res <<= 1; + return res; +} + +static u8 *utilLoadFromZip(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + char buffer[2048]; + + unzFile unz = unzOpen(file); + + if(unz == NULL) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file); + return NULL; + } + int r = unzGoToFirstFile(unz); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(MSG_BAD_ARCHIVE_FILE, N_("Bad ZIP file %s"), file); + return NULL; + } + + bool found = false; + + unz_file_info info; + + while(true) { + r = unzGetCurrentFileInfo(unz, + &info, + buffer, + sizeof(buffer), + NULL, + 0, + NULL, + 0); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(MSG_BAD_ARCHIVE_FILE, N_("Bad ZIP file %s"), file); + return NULL; + } + + if(accept(buffer)) { + found = true; + break; + } + + r = unzGoToNextFile(unz); + + if(r != UNZ_OK) + break; + } + + if(!found) { + unzClose(unz); + systemMessage(MSG_NO_IMAGE_ON_ARCHIVE, + N_("No image found on ZIP file %s"), file); + return NULL; + } + + int fileSize = info.uncompressed_size; + if(size == 0) + size = fileSize; + r = unzOpenCurrentFile(unz); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), buffer); + return NULL; + } + + u8 *image = data; + + if(image == NULL) { + image = (u8 *)malloc(utilGetSize(size)); + if(image == NULL) { + unzCloseCurrentFile(unz); + unzClose(unz); + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + r = unzReadCurrentFile(unz, + image, + read); + + unzCloseCurrentFile(unz); + unzClose(unz); + + if(r != (int)read) { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), buffer); + if(data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +static u8 *utilLoadGzipFile(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + FILE *f = fopen(file, "rb"); + + if(f == NULL) { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + fseek(f, -4, SEEK_END); + int fileSize = fgetc(f) | (fgetc(f) << 8) | (fgetc(f) << 16) | (fgetc(f) << 24); + fclose(f); + if(size == 0) + size = fileSize; + + gzFile gz = gzopen(file, "rb"); + + if(gz == NULL) { + // should not happen, but who knows? + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + u8 *image = data; + + if(image == NULL) { + image = (u8 *)malloc(utilGetSize(size)); + if(image == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + fclose(f); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + int r = gzread(gz, image, read); + gzclose(gz); + + if(r != (int)read) { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), file); + if(data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +#if 0 +static u8 *utilLoadRarFile(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + char buffer[2048]; + + ArchiveList_struct *rarList = NULL; + if(urarlib_list((void *)file, (ArchiveList_struct *)&rarList)) { + ArchiveList_struct *p = rarList; + + bool found = false; + while(p) { + if(accept(p->item.Name)) { + strcpy(buffer, p->item.Name); + found = true; + break; + } + p = p->next; + } + if(found) { + void *memory = NULL; + unsigned long lsize = 0; + size = p->item.UnpSize; + int r = urarlib_get((void *)&memory, &lsize, buffer, (void *)file, ""); + if(!r) { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), buffer); + urarlib_freelist(rarList); + return NULL; + } + u8 *image = (u8 *)memory; + if(data != NULL) { + memcpy(image, data, size); + } + urarlib_freelist(rarList); + return image; + } + systemMessage(MSG_NO_IMAGE_ON_ZIP, + N_("No image found on RAR file %s"), file); + urarlib_freelist(rarList); + return NULL; + } + // nothing found + return NULL; +} +#endif + +u8 *utilLoad(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + if(utilIsZipFile(file)) { + return utilLoadFromZip(file, accept, data, size); + } + if(utilIsGzipFile(file)) { + return utilLoadGzipFile(file, accept, data, size); + } +#if 0 + if(utilIsRarFile(file)) { + return utilLoadRarFile(file, accept, data, size); + } +#endif + + u8 *image = data; + + FILE *f = fopen(file, "rb"); + + if(!f) { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + fseek(f,0,SEEK_END); + int fileSize = ftell(f); + fseek(f,0,SEEK_SET); + if(size == 0) + size = fileSize; + + if(image == NULL) { + image = (u8 *)malloc(utilGetSize(size)); + if(image == NULL) { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + fclose(f); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + int r = fread(image, 1, read, f); + fclose(f); + + if(r != (int)read) { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), file); + if(data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +void utilWriteInt(gzFile gzFile, int i) +{ + utilGzWrite(gzFile, &i, sizeof(int)); +} + +int utilReadInt(gzFile gzFile) +{ + int i = 0; + utilGzRead(gzFile, &i, sizeof(int)); + return i; +} + +void utilReadData(gzFile gzFile, variable_desc* data) +{ + while(data->address) { + utilGzRead(gzFile, data->address, data->size); + data++; + } +} + +void utilWriteData(gzFile gzFile, variable_desc *data) +{ + while(data->address) { + utilGzWrite(gzFile, data->address, data->size); + data++; + } +} + +gzFile utilGzOpen(const char *file, const char *mode) +{ + utilGzWriteFunc = (int (ZEXPORT *)(void *,void * const, unsigned int))gzwrite; + utilGzReadFunc = gzread; + utilGzCloseFunc = gzclose; + + return gzopen(file, mode); +} + +gzFile utilMemGzOpen(char *memory, int available, char *mode) +{ + utilGzWriteFunc = memgzwrite; + utilGzReadFunc = memgzread; + utilGzCloseFunc = memgzclose; + + return memgzopen(memory, available, mode); +} + +int utilGzWrite(gzFile file, const voidp buffer, unsigned int len) +{ + return utilGzWriteFunc(file, buffer, len); +} + +int utilGzRead(gzFile file, voidp buffer, unsigned int len) +{ + return utilGzReadFunc(file, buffer, len); +} + +int utilGzClose(gzFile file) +{ + return utilGzCloseFunc(file); +} + +long utilGzMemTell(gzFile file) +{ + return memtell(file); +} + +void utilGBAFindSave(const u8 *data, const int size) +{ + u32 *p = (u32 *)data; + u32 *end = (u32 *)(data + size); + int saveType = 0; + int flashSize = 0x10000; + bool rtcFound = false; + + while(p < end) { + u32 d = READ32LE(p); + + if(d == 0x52504545) { + if(memcmp(p, "EEPROM_", 7) == 0) { + if(saveType == 0) + saveType = 1; + } + } else if (d == 0x4D415253) { + if(memcmp(p, "SRAM_", 5) == 0) { + if(saveType == 0) + saveType = 2; + } + } else if (d == 0x53414C46) { + if(memcmp(p, "FLASH1M_", 8) == 0) { + if(saveType == 0) { + saveType = 3; + flashSize = 0x20000; + } + } else if(memcmp(p, "FLASH", 5) == 0) { + if(saveType == 0) { + saveType = 3; + flashSize = 0x10000; + } + } + } else if (d == 0x52494953) { + if(memcmp(p, "SIIRTC_V", 8) == 0) + rtcFound = true; + } + p++; + } + // if no matches found, then set it to NONE + if(saveType == 0) { + saveType = 5; + } + rtcEnable(rtcFound); + cpuSaveType = saveType; + flashSetSize(flashSize); +} + +void utilUpdateSystemColorMaps(int lcd) +{ + switch(systemColorDepth) { + case 16: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal(systemColorMap16, 0x10000); + } + break; + case 24: + case 32: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal32(systemColorMap32, 0x10000); + } + break; + } +} \ No newline at end of file diff --git a/src/Util.h b/src/Util.h new file mode 100644 index 00000000..37fa1f6a --- /dev/null +++ b/src/Util.h @@ -0,0 +1,65 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_UTIL_H +#define VBA_UTIL_H +enum IMAGE_TYPE { + IMAGE_UNKNOWN = -1, + IMAGE_GBA = 0, + IMAGE_GB = 1 +}; + +// save game + +typedef struct { + void *address; + int size; +} variable_desc; + +extern bool utilWritePNGFile(const char *, int, int, u8 *); +extern bool utilWriteBMPFile(const char *, int, int, u8 *); +extern void utilApplyIPS(const char *ips, u8 **rom, int *size); +extern void utilWriteBMP(char *, int, int, u8 *); +extern bool utilIsGBAImage(const char *); +extern bool utilIsGBImage(const char *); +extern bool utilIsZipFile(const char *); +extern bool utilIsGzipFile(const char *); +extern bool utilIsRarFile(const char *); +extern void utilGetBaseName(const char *, char *); +extern IMAGE_TYPE utilFindType(const char *); +extern u8 *utilLoad(const char *, + bool (*)(const char*), + u8 *, + int &); + +extern void utilPutDword(u8 *, u32); +extern void utilPutWord(u8 *, u16); +extern void utilWriteData(gzFile, variable_desc *); +extern void utilReadData(gzFile, variable_desc *); +extern int utilReadInt(gzFile); +extern void utilWriteInt(gzFile, int); +extern gzFile utilGzOpen(const char *file, const char *mode); +extern gzFile utilMemGzOpen(char *memory, int available, char *mode); +extern int utilGzWrite(gzFile file, const voidp buffer, unsigned int len); +extern int utilGzRead(gzFile file, voidp buffer, unsigned int len); +extern int utilGzClose(gzFile file); +extern long utilGzMemTell(gzFile file); +extern void utilGBAFindSave(const u8 *, const int); +extern void utilUpdateSystemColorMaps(int lcd); +#endif \ No newline at end of file diff --git a/src/admame.cpp b/src/admame.cpp new file mode 100644 index 00000000..bdde667f --- /dev/null +++ b/src/admame.cpp @@ -0,0 +1,1010 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 1999-2002 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * Alternatively at the previous license terms, you are allowed to use this + * code in your program with these conditions: + * - the program is not used in commercial activities. + * - the whole source code of the program is released with the binary. + */ + +#include "System.h" + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +static void internal_scale2x_16_def(u16 *dst, const u16* src0, const u16* src1, const u16* src2, unsigned count) { + /* first pixel */ + dst[0] = src1[0]; + if (src1[1] == src0[0] && src2[0] != src0[0]) + dst[1] = src0[0]; + else + dst[1] = src1[0]; + ++src0; + ++src1; + ++src2; + dst += 2; + + /* central pixels */ + count -= 2; + while (count) { + if (src0[0] != src2[0] && src1[-1] != src1[1]) { + dst[0] = src1[-1] == src0[0] ? src0[0] : src1[0]; + dst[1] = src1[1] == src0[0] ? src0[0] : src1[0]; + } else { + dst[0] = src1[0]; + dst[1] = src1[0]; + } + + ++src0; + ++src1; + ++src2; + dst += 2; + --count; + } + + /* last pixel */ + if (src1[-1] == src0[0] && src2[0] != src0[0]) + dst[0] = src0[0]; + else + dst[0] = src1[0]; + dst[1] = src1[0]; +} + +static void internal_scale2x_32_def(u32* dst, + const u32* src0, + const u32* src1, + const u32* src2, + unsigned count) +{ + /* first pixel */ + dst[0] = src1[0]; + if (src1[1] == src0[0] && src2[0] != src0[0]) + dst[1] = src0[0]; + else + dst[1] = src1[0]; + ++src0; + ++src1; + ++src2; + dst += 2; + + /* central pixels */ + count -= 2; + while (count) { + if (src0[0] != src2[0] && src1[-1] != src1[1]) { + dst[0] = src1[-1] == src0[0] ? src0[0] : src1[0]; + dst[1] = src1[1] == src0[0] ? src0[0] : src1[0]; + } else { + dst[0] = src1[0]; + dst[1] = src1[0]; + } + + ++src0; + ++src1; + ++src2; + dst += 2; + --count; + } + + /* last pixel */ + if (src1[-1] == src0[0] && src2[0] != src0[0]) + dst[0] = src0[0]; + else + dst[0] = src1[0]; + dst[1] = src1[0]; +} + +#ifdef MMX +static void internal_scale2x_16_mmx_single(u16* dst, const u16* src0, const u16* src1, const u16* src2, unsigned count) { + /* always do the first and last run */ + count -= 2*4; + +#ifdef __GNUC__ + __asm__ __volatile__( + /* first run */ + /* set the current, current_pre, current_next registers */ + "movq 0(%1), %%mm0\n" + "movq 0(%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psllq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "psrlq $48, %%mm0\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + /* central runs */ + "shrl $2,%4\n" + "jz 1f\n" + + "0:\n" + + /* set the current, current_pre, current_next registers */ + "movq -8(%1),%%mm0\n" + "movq (%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psrlq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + "decl %4\n" + "jnz 0b\n" + "1:\n" + + /* final run */ + /* set the current, current_pre, current_next registers */ + "movq (%1),%%mm1\n" + "movq (%1),%%mm7\n" + "movq -8(%1),%%mm0\n" + "psrlq $48,%%mm1\n" + "psrlq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + "emms\n" + + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (dst), "+r" (count) + : + : "cc" + ); +#else + __asm { + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, dst; + mov esi, count; + + /* first run */ + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx + 8]; + psllq mm0, 48; + psllq mm1, 48; + psrlq mm0, 48; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 16; + psrlq mm3, 16; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqw mm2, mm6; + pcmpeqw mm4, mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqw mm2,mm1; + pcmpeqw mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst0 */ + movq mm3,mm2; + punpcklwd mm2,mm4; + punpckhwd mm3,mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + /* next */ + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 16; + + /* central runs */ + shr esi, 2; + jz label1; + align 4; + label0: + + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx-8]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx+8]; + psrlq mm0,48; + psllq mm1,48; + movq mm2,mm7; + movq mm3,mm7; + psllq mm2,16; + psrlq mm3,16; + por mm0,mm2; + por mm1,mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2,mm0; + movq mm4,mm1; + movq mm3,mm0; + movq mm5,mm1; + pcmpeqw mm2,mm6; + pcmpeqw mm4,mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqw mm2,mm1; + pcmpeqw mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst */ + movq mm3,mm2; + punpcklwd mm2,mm4; + punpckhwd mm3,mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx+8], mm3; + + /* next */ + add eax,8; + add ebx,8; + add ecx,8; + add edx,16; + + dec esi; + jnz label0; + label1: + + /* final run */ + /* set the current, current_pre, current_next registers */ + movq mm1, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm0, qword ptr [ebx-8]; + psrlq mm1,48; + psrlq mm0,48; + psllq mm1,48; + movq mm2,mm7; + movq mm3,mm7; + psllq mm2,16; + psrlq mm3,16; + por mm0,mm2; + por mm1,mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2,mm0; + movq mm4,mm1; + movq mm3,mm0; + movq mm5,mm1; + pcmpeqw mm2,mm6; + pcmpeqw mm4,mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqw mm2,mm1; + pcmpeqw mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst */ + movq mm3,mm2; + punpcklwd mm2,mm4; + punpckhwd mm3,mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx+8], mm3; + + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov dst, edx; + mov count, esi; + + emms; + } +#endif +} + +static void internal_scale2x_32_mmx_single(u32* dst, const u32* src0, const u32* src1, const u32* src2, unsigned count) { + /* always do the first and last run */ + count -= 2*2; + +#ifdef __GNUC__ + __asm__ __volatile__( + /* first run */ + /* set the current, current_pre, current_next registers */ + "movq 0(%1),%%mm0\n" + "movq 0(%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psllq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "psrlq $32,%%mm0\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3, 8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + /* central runs */ + "shrl $1,%4\n" + "jz 1f\n" + + "0:\n" + + /* set the current, current_pre, current_next registers */ + "movq -8(%1),%%mm0\n" + "movq (%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psrlq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + "decl %4\n" + "jnz 0b\n" + "1:\n" + + /* final run */ + /* set the current, current_pre, current_next registers */ + "movq (%1),%%mm1\n" + "movq (%1),%%mm7\n" + "movq -8(%1), %%mm0\n" + "psrlq $32,%%mm1\n" + "psrlq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + "emms\n" + + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (dst), "+r" (count) + : + : "cc" + ); +#else + __asm { + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, dst; + mov esi, count; + + /* first run */ + /* set the current, current_pre, current_next registers */ + movq mm0,qword ptr [ebx]; + movq mm7,qword ptr [ebx]; + movq mm1,qword ptr [ebx + 8]; + psllq mm0,32; + psllq mm1,32; + psrlq mm0,32; + movq mm2,mm7; + movq mm3,mm7; + psllq mm2,32; + psrlq mm3,32; + por mm0,mm2; + por mm1,mm3; + + /* current_upper */ + movq mm6,qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2,mm0; + movq mm4,mm1; + movq mm3,mm0; + movq mm5,mm1; + pcmpeqd mm2,mm6; + pcmpeqd mm4,mm6; + pcmpeqd mm3,qword ptr [ecx]; + pcmpeqd mm5,qword ptr [ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqd mm2,mm1; + pcmpeqd mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst */ + movq mm3,mm2; + punpckldq mm2,mm4; + punpckhdq mm3,mm4; + movq qword ptr [edx],mm2; + movq qword ptr [edx+8],mm3; + + /* next */ + add eax,8; + add ebx,8; + add ecx,8; + add edx,16; + + /* central runs */ + shr esi,1; + jz label1; +label0: + + /* set the current, current_pre, current_next registers */ + movq mm0,qword ptr [ebx-8]; + movq mm7,qword ptr [ebx]; + movq mm1,qword ptr [ebx+8]; + psrlq mm0,32; + psllq mm1,32; + movq mm2,mm7; + movq mm3,mm7; + psllq mm2,32; + psrlq mm3,32; + por mm0,mm2; + por mm1,mm3; + + /* current_upper */ + movq mm6,qword ptr[eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2,mm0; + movq mm4,mm1; + movq mm3,mm0; + movq mm5,mm1; + pcmpeqd mm2,mm6; + pcmpeqd mm4,mm6; + pcmpeqd mm3,qword ptr[ecx]; + pcmpeqd mm5,qword ptr[ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqd mm2,mm1; + pcmpeqd mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst */ + movq mm3,mm2; + punpckldq mm2,mm4; + punpckhdq mm3,mm4; + movq qword ptr [edx],mm2; + movq qword ptr [edx+8],mm3; + + /* next */ + add eax,8; + add ebx,8; + add ecx,8; + add edx,16; + + dec esi; + jnz label0; +label1: + + /* final run */ + /* set the current, current_pre, current_next registers */ + movq mm1,qword ptr [ebx]; + movq mm7,qword ptr [ebx]; + movq mm0,qword ptr [ebx-8]; + psrlq mm1,32; + psrlq mm0,32; + psllq mm1,32; + movq mm2,mm7; + movq mm3,mm7; + psllq mm2,32; + psrlq mm3,32; + por mm0,mm2; + por mm1,mm3; + + /* current_upper */ + movq mm6,qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2,mm0; + movq mm4,mm1; + movq mm3,mm0; + movq mm5,mm1; + pcmpeqd mm2,mm6; + pcmpeqd mm4,mm6; + pcmpeqd mm3,qword ptr [ecx]; + pcmpeqd mm5,qword ptr [ecx]; + pandn mm3,mm2; + pandn mm5,mm4; + movq mm2,mm0; + movq mm4,mm1; + pcmpeqd mm2,mm1; + pcmpeqd mm4,mm0; + pandn mm2,mm3; + pandn mm4,mm5; + movq mm3,mm2; + movq mm5,mm4; + pand mm2,mm6; + pand mm4,mm6; + pandn mm3,mm7; + pandn mm5,mm7; + por mm2,mm3; + por mm4,mm5; + + /* set *dst */ + movq mm3,mm2; + punpckldq mm2,mm4; + punpckhdq mm3,mm4; + movq qword ptr [edx],mm2; + movq qword ptr [edx+8],mm3; + + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov dst, edx; + mov count, esi; + + emms; + } +#endif +} + +static void internal_scale2x_16_mmx(u16* dst0, u16* dst1, const u16* src0, const u16* src1, const u16* src2, unsigned count) { + // assert( count >= 2*4 ); + internal_scale2x_16_mmx_single(dst0, src0, src1, src2, count); + internal_scale2x_16_mmx_single(dst1, src2, src1, src0, count); +} + +static void internal_scale2x_32_mmx(u32* dst0, u32* dst1, const u32* src0, const u32* src1, const u32* src2, unsigned count) { + // assert( count >= 2*2 ); + internal_scale2x_32_mmx_single(dst0, src0, src1, src2, count); + internal_scale2x_32_mmx_single(dst1, src2, src1, src0, count); +} +#endif + +void AdMame2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); +#ifdef MMX + if(cpu_mmx) { + internal_scale2x_16_mmx(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_mmx(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_mmx(dst0, dst1, src0, src1, src1, width); + } else { +#endif + internal_scale2x_16_def(dst0, src0, src0, src1, width); + internal_scale2x_16_def(dst1, src1, src0, src0, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_def(dst0, src0, src1, src2, width); + internal_scale2x_16_def(dst1, src2, src1, src0, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_def(dst0, src0, src1, src1, width); + internal_scale2x_16_def(dst1, src1, src1, src0, width); +#ifdef MMX + } +#endif +} + +void AdMame2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); +#ifdef MMX + if(cpu_mmx) { + internal_scale2x_32_mmx(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_mmx(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_mmx(dst0, dst1, src0, src1, src1, width); + } else { +#endif + internal_scale2x_32_def(dst0, src0, src0, src1, width); + internal_scale2x_32_def(dst1, src1, src0, src0, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_def(dst0, src0, src1, src2, width); + internal_scale2x_32_def(dst1, src2, src1, src0, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_def(dst0, src0, src1, src1, width); + internal_scale2x_32_def(dst1, src1, src1, src0, width); +#ifdef MMX + } +#endif +} diff --git a/src/agbprint.cpp b/src/agbprint.cpp new file mode 100644 index 00000000..301c6718 --- /dev/null +++ b/src/agbprint.cpp @@ -0,0 +1,99 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include + +#include "GBA.h" +#include "Globals.h" +#include "Port.h" + +#define debuggerWriteHalfWord(addr, value) \ + WRITE16LE((u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask], (value)) + +#define debuggerReadHalfWord(addr) \ + READ16LE(((u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +static bool agbPrintEnabled = false; +static bool agbPrintProtect = false; + +bool agbPrintWrite(u32 address, u16 value) +{ + if(agbPrintEnabled) { + if(address == 0x9fe2ffe) { // protect + agbPrintProtect = (value != 0); + debuggerWriteHalfWord(address, value); + return true; + } else { + if(agbPrintProtect && + ((address >= 0x9fe20f8 && address <= 0x9fe20ff) // control structure + || (address >= 0x8fd0000 && address <= 0x8fdffff) + || (address >= 0x9fd0000 && address <= 0x9fdffff))) { + debuggerWriteHalfWord(address, value); + return true; + } + } + } + return false; +} + +void agbPrintReset() +{ + agbPrintProtect = false; +} + +void agbPrintEnable(bool enable) +{ + agbPrintEnabled = enable; +} + +bool agbPrintIsEnabled() +{ + return agbPrintEnabled; +} + +extern void (*dbgOutput)(char *, u32); + +void agbPrintFlush() +{ + u16 get = debuggerReadHalfWord(0x9fe20fc); + u16 put = debuggerReadHalfWord(0x9fe20fe); + + u32 address = (debuggerReadHalfWord(0x9fe20fa) << 16); + if(address != 0xfd0000 && address != 0x1fd0000) { + dbgOutput("Did you forget to call AGBPrintInit?\n", 0); + // get rid of the text otherwise we will continue to be called + debuggerWriteHalfWord(0x9fe20fc, put); + return; + } + + u8 *data = &rom[address]; + + while(get != put) { + char c = data[get++]; + char s[2]; + s[0] = c; + s[1] = 0; + + if(systemVerbose & VERBOSE_AGBPRINT) + dbgOutput(s, 0); + if(c == '\n') + break; + } + debuggerWriteHalfWord(0x9fe20fc, get); +} diff --git a/src/agbprint.h b/src/agbprint.h new file mode 100644 index 00000000..d7742dee --- /dev/null +++ b/src/agbprint.h @@ -0,0 +1,27 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_AGBPRINT_H +#define VBA_AGBPRINT_H +extern void agbPrintEnable(bool); +extern bool agbPrintIsEnabled(); +extern void agbPrintReset(); +extern bool agbPrintWrite(u32, u16); +extern void agbPrintFlush(); +#endif diff --git a/src/arm-new.h b/src/arm-new.h new file mode 100644 index 00000000..8c87f87c --- /dev/null +++ b/src/arm-new.h @@ -0,0 +1,7188 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifdef BKPT_SUPPORT +#define CONSOLE_OUTPUT(a,b) \ + extern void (*dbgOutput)(char *, u32);\ + if((opcode == 0xe0000000) && (reg[0].I == 0xC0DED00D)) {\ + dbgOutput((a), (b));\ + } +#else +#define CONSOLE_OUTPUT(a,b) +#endif + +#define OP_AND \ + reg[dest].I = reg[(opcode>>16)&15].I & value;\ + CONSOLE_OUTPUT(NULL,reg[2].I); + +#define OP_ANDS \ + reg[dest].I = reg[(opcode>>16)&15].I & value;\ + \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_EOR \ + reg[dest].I = reg[(opcode>>16)&15].I ^ value; + +#define OP_EORS \ + reg[dest].I = reg[(opcode>>16)&15].I ^ value;\ + \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; +#ifdef C_CORE +#define NEG(i) ((i) >> 31) +#define POS(i) ((~(i)) >> 31) +#define ADDCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & NEG(b)) |\ + (NEG(a) & POS(c)) |\ + (NEG(b) & POS(c))) ? true : false; +#define ADDOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & NEG(b) & POS(c)) |\ + (POS(a) & POS(b) & NEG(c))) ? true : false; +#define SUBCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & POS(b)) |\ + (NEG(a) & POS(c)) |\ + (POS(b) & POS(c))) ? true : false; +#define SUBOVERFLOW(a, b, c)\ + V_FLAG = ((NEG(a) & POS(b) & POS(c)) |\ + (POS(a) & NEG(b) & NEG(c))) ? true : false; +#define OP_SUB \ + {\ + reg[dest].I = reg[base].I - value;\ + } +#define OP_SUBS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define OP_RSB \ + {\ + reg[dest].I = value - reg[base].I;\ + } +#define OP_RSBS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = rhs - lhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(rhs, lhs, res);\ + SUBOVERFLOW(rhs, lhs, res);\ + } +#define OP_ADD \ + {\ + reg[dest].I = reg[base].I + value;\ + } +#define OP_ADDS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define OP_ADC \ + {\ + reg[dest].I = reg[base].I + value + (u32)C_FLAG;\ + } +#define OP_ADCS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs + (u32)C_FLAG;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define OP_SBC \ + {\ + reg[dest].I = reg[base].I - value - !((u32)C_FLAG);\ + } +#define OP_SBCS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs - !((u32)C_FLAG);\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define OP_RSC \ + {\ + reg[dest].I = value - reg[base].I - !((u32)C_FLAG);\ + } +#define OP_RSCS \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = rhs - lhs - !((u32)C_FLAG);\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(rhs, lhs, res);\ + SUBOVERFLOW(rhs, lhs, res);\ + } +#define OP_CMP \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define OP_CMN \ + {\ + u32 lhs = reg[base].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } + +#define LOGICAL_LSL_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (32 - shift)) & 1 ? true : false;\ + value = v << shift;\ + } +#define LOGICAL_LSR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = v >> shift;\ + } +#define LOGICAL_ASR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = ((s32)v >> (int)(shift - 1)) & 1 ? true : false;\ + value = (s32)v >> (int)shift;\ + } +#define LOGICAL_ROR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } +#define LOGICAL_RRX_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + shift = (int)C_FLAG;\ + C_OUT = (v & 1) ? true : false;\ + value = ((v >> 1) |\ + (shift << 31));\ + } +#define LOGICAL_ROR_IMM \ + {\ + u32 v = opcode & 0xff;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } +#define ARITHMETIC_LSL_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = v << shift;\ + } +#define ARITHMETIC_LSR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = v >> shift;\ + } +#define ARITHMETIC_ASR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = (s32)v >> (int)shift;\ + } +#define ARITHMETIC_ROR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } +#define ARITHMETIC_RRX_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + shift = (int)C_FLAG;\ + value = ((v >> 1) |\ + (shift << 31));\ + } +#define ARITHMETIC_ROR_IMM \ + {\ + u32 v = opcode & 0xff;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } +#define ROR_IMM_MSR \ + {\ + u32 v = opcode & 0xff;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } +#define ROR_VALUE \ + {\ + value = ((value << (32 - shift)) |\ + (value >> shift));\ + } +#define RCR_VALUE \ + {\ + shift = (int)C_FLAG;\ + value = ((value >> 1) |\ + (shift << 31));\ + } +#else +#ifdef __GNUC__ + #ifdef __POWERPC__ + #define OP_SUB \ + {\ + reg[dest].I = reg[base].I - value;\ + } + #define OP_SUBS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_RSB \ + {\ + reg[dest].I = value - reg[base].I;\ + } + #define OP_RSBS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subfco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_ADD \ + {\ + reg[dest].I = reg[base].I + value;\ + } + + #define OP_ADDS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_ADC \ + {\ + reg[dest].I = reg[base].I + value + (u32)C_FLAG;\ + } + #define OP_ADCS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("mtspr xer, %4\n" \ + "addeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_SBC \ + {\ + reg[dest].I = reg[base].I - value - (C_FLAG^1);\ + } + #define OP_SBCS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("mtspr xer, %4\n" \ + "subfeo. %0, %3, %2\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_RSC \ + {\ + reg[dest].I = value - reg[base].I - (C_FLAG^1);\ + } + #define OP_RSCS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("mtspr xer, %4\n" \ + "subfeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_CMP \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_CMN \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + + #define LOGICAL_LSL_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (32 - shift)) & 1 ? true : false;\ + value = v << shift;\ + } + #define LOGICAL_LSR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = v >> shift;\ + } + #define LOGICAL_ASR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = ((s32)v >> (int)(shift - 1)) & 1 ? true : false;\ + value = (s32)v >> (int)shift;\ + } + #define LOGICAL_ROR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } + #define LOGICAL_RRX_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + shift = (int)C_FLAG;\ + C_OUT = (v & 1) ? true : false;\ + value = ((v >> 1) |\ + (shift << 31));\ + } + #define LOGICAL_ROR_IMM \ + {\ + u32 v = opcode & 0xff;\ + C_OUT = (v >> (shift - 1)) & 1 ? true : false;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } + #define ARITHMETIC_LSL_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = v << shift;\ + } + #define ARITHMETIC_LSR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = v >> shift;\ + } + #define ARITHMETIC_ASR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = (s32)v >> (int)shift;\ + } + #define ARITHMETIC_ROR_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } + #define ARITHMETIC_RRX_REG \ + {\ + u32 v = reg[opcode & 0x0f].I;\ + shift = (int)C_FLAG;\ + value = ((v >> 1) |\ + (shift << 31));\ + } + #define ARITHMETIC_ROR_IMM \ + {\ + u32 v = opcode & 0xff;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } + #define ROR_IMM_MSR \ + {\ + u32 v = opcode & 0xff;\ + value = ((v << (32 - shift)) |\ + (v >> shift));\ + } + #define ROR_VALUE \ + {\ + value = ((value << (32 - shift)) |\ + (value >> shift));\ + } + #define RCR_VALUE \ + {\ + shift = (int)C_FLAG;\ + value = ((value >> 1) |\ + (shift << 31));\ + } +#else +#define OP_SUB \ + asm ("sub %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_SUBS \ + asm ("sub %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_RSB \ + asm ("sub %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (reg[base].I), "b" (value)); + +#define OP_RSBS \ + asm ("sub %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (reg[base].I), "b" (value)); + +#define OP_ADD \ + asm ("add %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADDS \ + asm ("add %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADC \ + asm ("bt $0, C_FLAG;"\ + "adc %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADCS \ + asm ("bt $0, C_FLAG;"\ + "adc %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_SBC \ + asm ("bt $0, C_FLAG;"\ + "cmc;"\ + "sbb %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); + +#define OP_SBCS \ + asm ("bt $0, C_FLAG;"\ + "cmc;"\ + "sbb %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[base].I)); +#define OP_RSC \ + asm ("bt $0, C_FLAG;"\ + "cmc;"\ + "sbb %1, %%ebx;"\ + : "=b" (reg[dest].I)\ + : "r" (reg[base].I), "b" (value)); + +#define OP_RSCS \ + asm ("bt $0, C_FLAG;"\ + "cmc;"\ + "sbb %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (reg[base].I), "b" (value)); +#define OP_CMP \ + asm ("sub %0, %1;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + :\ + : "r" (value), "r" (reg[base].I)); + +#define OP_CMN \ + asm ("add %0, %1;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : \ + : "r" (value), "r" (reg[base].I)); +#define LOGICAL_LSL_REG \ + asm("shl %%cl, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_LSR_REG \ + asm("shr %%cl, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_ASR_REG \ + asm("sar %%cl, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_ROR_REG \ + asm("ror %%cl, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_RRX_REG \ + asm("bt $0, C_FLAG;"\ + "rcr $1, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (reg[opcode & 0x0f].I)); + +#define LOGICAL_ROR_IMM \ + asm("ror %%cl, %%eax;"\ + "setcb %%cl;"\ + : "=a" (value), "=c" (C_OUT)\ + : "a" (opcode & 0xff), "c" (shift)); +#define ARITHMETIC_LSL_REG \ + asm("\ + shl %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_LSR_REG \ + asm("\ + shr %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_ASR_REG \ + asm("\ + sar %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_ROR_REG \ + asm("\ + ror %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_RRX_REG \ + asm("\ + bt $0, C_FLAG;\ + rcr $1, %%eax;"\ + : "=a" (value)\ + : "a" (reg[opcode & 0x0f].I)); + +#define ARITHMETIC_ROR_IMM \ + asm("\ + ror %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (opcode & 0xff), "c" (shift)); +#define ROR_IMM_MSR \ + asm ("ror %%cl, %%eax;"\ + : "=a" (value)\ + : "a" (opcode & 0xFF), "c" (shift)); +#define ROR_VALUE \ + asm("ror %%cl, %0"\ + : "=r" (value)\ + : "r" (value), "c" (shift)); +#define RCR_VALUE \ + asm("bt $0, C_FLAG;"\ + "rcr $1, %0"\ + : "=r" (value)\ + : "r" (value)); +#endif +#else +#define OP_SUB \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm sub ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + } + +#define OP_SUBS \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm sub ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } + +#define OP_RSB \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm mov eax, value\ + __asm sub eax, ebx\ + __asm mov ebx, dest\ + __asm mov dword ptr [OFFSET reg+4*ebx], eax\ + } + +#define OP_RSBS \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm mov eax, value\ + __asm sub eax, ebx\ + __asm mov ebx, dest\ + __asm mov dword ptr [OFFSET reg+4*ebx], eax\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } + +#define OP_ADD \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm add ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + } + +#define OP_ADDS \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm add ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } + +#define OP_ADC \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm bt word ptr C_FLAG, 0\ + __asm adc ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + } + +#define OP_ADCS \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm bt word ptr C_FLAG, 0\ + __asm adc ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } + +#define OP_SBC \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg + 4*ebx]\ + __asm mov eax, value\ + __asm bt word ptr C_FLAG, 0\ + __asm cmc\ + __asm sbb ebx, eax\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg + 4*eax], ebx\ + } + +#define OP_SBCS \ + {\ + __asm mov ebx, base\ + __asm mov ebx, dword ptr [OFFSET reg + 4*ebx]\ + __asm mov eax, value\ + __asm bt word ptr C_FLAG, 0\ + __asm cmc\ + __asm sbb ebx, eax\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg + 4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define OP_RSC \ + {\ + __asm mov ebx, value\ + __asm mov eax, base\ + __asm mov eax, dword ptr[OFFSET reg + 4*eax]\ + __asm bt word ptr C_FLAG, 0\ + __asm cmc\ + __asm sbb ebx, eax\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg + 4*eax], ebx\ + } + +#define OP_RSCS \ + {\ + __asm mov ebx, value\ + __asm mov eax, base\ + __asm mov eax, dword ptr[OFFSET reg + 4*eax]\ + __asm bt word ptr C_FLAG, 0\ + __asm cmc\ + __asm sbb ebx, eax\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg + 4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define OP_CMP \ + {\ + __asm mov eax, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm sub ebx, value\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } + +#define OP_CMN \ + {\ + __asm mov eax, base\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm add ebx, value\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define LOGICAL_LSL_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shl eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT + +#define LOGICAL_LSR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shr eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT + +#define LOGICAL_ASR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm sar eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT + +#define LOGICAL_ROR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0F\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm mov cl, byte ptr shift\ + __asm ror eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT + +#define LOGICAL_RRX_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0F\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm bt word ptr C_OUT, 0\ + __asm rcr eax, 1\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT + +#define LOGICAL_ROR_IMM \ + __asm mov eax, opcode\ + __asm and eax, 0xff\ + __asm mov cl, byte ptr shift\ + __asm ror eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_OUT +#define ARITHMETIC_LSL_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shl eax, cl\ + __asm mov value, eax + +#define ARITHMETIC_LSR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shr eax, cl\ + __asm mov value, eax + +#define ARITHMETIC_ASR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0f\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm sar eax, cl\ + __asm mov value, eax + +#define ARITHMETIC_ROR_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0F\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm mov cl, byte ptr shift\ + __asm ror eax, cl\ + __asm mov value, eax + +#define ARITHMETIC_RRX_REG \ + __asm mov eax, opcode\ + __asm and eax, 0x0F\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm bt word ptr C_FLAG, 0\ + __asm rcr eax, 1\ + __asm mov value, eax + +#define ARITHMETIC_ROR_IMM \ + __asm mov eax, opcode\ + __asm and eax, 0xff\ + __asm mov cl, byte ptr shift\ + __asm ror eax, cl\ + __asm mov value, eax +#define ROR_IMM_MSR \ + {\ + __asm mov eax, opcode\ + __asm and eax, 0xff\ + __asm mov cl, byte ptr shift\ + __asm ror eax, CL\ + __asm mov value, eax\ + } +#define ROR_VALUE \ + {\ + __asm mov cl, byte ptr shift\ + __asm ror dword ptr value, cl\ + } +#define RCR_VALUE \ + {\ + __asm mov cl, byte ptr shift\ + __asm bt word ptr C_FLAG, 0\ + __asm rcr dword ptr value, 1\ + } +#endif +#endif + +#define OP_TST \ + u32 res = reg[base].I & value;\ + N_FLAG = (res & 0x80000000) ? true : false;\ + Z_FLAG = (res) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_TEQ \ + u32 res = reg[base].I ^ value;\ + N_FLAG = (res & 0x80000000) ? true : false;\ + Z_FLAG = (res) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_ORR \ + reg[dest].I = reg[base].I | value; + +#define OP_ORRS \ + reg[dest].I = reg[base].I | value;\ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_MOV \ + reg[dest].I = value; + +#define OP_MOVS \ + reg[dest].I = value;\ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_BIC \ + reg[dest].I = reg[base].I & (~value); + +#define OP_BICS \ + reg[dest].I = reg[base].I & (~value);\ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; + +#define OP_MVN \ + reg[dest].I = ~value; + +#define OP_MVNS \ + reg[dest].I = ~value; \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false;\ + Z_FLAG = (reg[dest].I) ? false : true;\ + C_FLAG = C_OUT; + +#define CASE_16(BASE) \ + case BASE:\ + case BASE+1:\ + case BASE+2:\ + case BASE+3:\ + case BASE+4:\ + case BASE+5:\ + case BASE+6:\ + case BASE+7:\ + case BASE+8:\ + case BASE+9:\ + case BASE+10:\ + case BASE+11:\ + case BASE+12:\ + case BASE+13:\ + case BASE+14:\ + case BASE+15: + +#define CASE_256(BASE) \ + CASE_16(BASE)\ + CASE_16(BASE+0x10)\ + CASE_16(BASE+0x20)\ + CASE_16(BASE+0x30)\ + CASE_16(BASE+0x40)\ + CASE_16(BASE+0x50)\ + CASE_16(BASE+0x60)\ + CASE_16(BASE+0x70)\ + CASE_16(BASE+0x80)\ + CASE_16(BASE+0x90)\ + CASE_16(BASE+0xa0)\ + CASE_16(BASE+0xb0)\ + CASE_16(BASE+0xc0)\ + CASE_16(BASE+0xd0)\ + CASE_16(BASE+0xe0)\ + CASE_16(BASE+0xf0) + +#define LOGICAL_DATA_OPCODE(OPCODE, OPCODE2, BASE) \ + case BASE: \ + case BASE+8:\ + {\ + /* OP Rd,Rb,Rm LSL # */ \ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + \ + clockTicks++;\ + if(shift) {\ + LOGICAL_LSL_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+2:\ + case BASE+10:\ + {\ + /* OP Rd,Rb,Rm LSR # */ \ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_LSR_REG\ + } else {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000) ? true : false;\ + }\ + \ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+4:\ + case BASE+12:\ + {\ + /* OP Rd,Rb,Rm ASR # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ASR_REG\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + C_OUT = true;\ + } else {\ + value = 0;\ + C_OUT = false;\ + } \ + }\ + \ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+6:\ + case BASE+14:\ + {\ + /* OP Rd,Rb,Rm ROR # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ROR_REG\ + } else {\ + LOGICAL_RRX_REG\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+1:\ + {\ + /* OP Rd,Rb,Rm LSL Rs */\ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 1 ? true : false);\ + } else if(shift < 32) {\ + LOGICAL_LSL_REG\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+3:\ + {\ + /* OP Rd,Rb,Rm LSR Rs */ \ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000 ? true : false);\ + } else if(shift < 32) {\ + LOGICAL_LSR_REG\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+5:\ + {\ + /* OP Rd,Rb,Rm ASR Rs */ \ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift < 32) {\ + if(shift) {\ + LOGICAL_ASR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + C_OUT = true;\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+7:\ + {\ + /* OP Rd,Rb,Rm ROR Rs */\ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + shift &= 0x1f;\ + if(shift) {\ + LOGICAL_ROR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + C_OUT = (value & 0x80000000 ? true : false);\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + C_OUT = (value & 0x80000000 ? true : false);\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+0x200:\ + case BASE+0x201:\ + case BASE+0x202:\ + case BASE+0x203:\ + case BASE+0x204:\ + case BASE+0x205:\ + case BASE+0x206:\ + case BASE+0x207:\ + case BASE+0x208:\ + case BASE+0x209:\ + case BASE+0x20a:\ + case BASE+0x20b:\ + case BASE+0x20c:\ + case BASE+0x20d:\ + case BASE+0x20e:\ + case BASE+0x20f:\ + {\ + int shift = (opcode & 0xF00) >> 7;\ + int base = (opcode >> 16) & 0x0F;\ + int dest = (opcode >> 12) & 0x0F;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ROR_IMM\ + } else {\ + value = opcode & 0xff;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break; + +#define LOGICAL_DATA_OPCODE_WITHOUT_base(OPCODE, OPCODE2, BASE) \ + case BASE: \ + case BASE+8:\ + {\ + /* OP Rd,Rb,Rm LSL # */ \ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + \ + if(shift) {\ + LOGICAL_LSL_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+2:\ + case BASE+10:\ + {\ + /* OP Rd,Rb,Rm LSR # */ \ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_LSR_REG\ + } else {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000) ? true : false;\ + }\ + \ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+4:\ + case BASE+12:\ + {\ + /* OP Rd,Rb,Rm ASR # */\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ASR_REG\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + C_OUT = true;\ + } else {\ + value = 0;\ + C_OUT = false;\ + } \ + }\ + \ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+6:\ + case BASE+14:\ + {\ + /* OP Rd,Rb,Rm ROR # */\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ROR_REG\ + } else {\ + LOGICAL_RRX_REG\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+1:\ + {\ + /* OP Rd,Rb,Rm LSL Rs */\ + clockTicks+=2;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 1 ? true : false);\ + } else if(shift < 32) {\ + LOGICAL_LSL_REG\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+3:\ + {\ + /* OP Rd,Rb,Rm LSR Rs */ \ + clockTicks+=2;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000 ? true : false);\ + } else if(shift < 32) {\ + LOGICAL_LSR_REG\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+5:\ + {\ + /* OP Rd,Rb,Rm ASR Rs */ \ + clockTicks+=2;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift < 32) {\ + if(shift) {\ + LOGICAL_ASR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + C_OUT = true;\ + } else {\ + value = 0;\ + C_OUT = false;\ + }\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+7:\ + {\ + /* OP Rd,Rb,Rm ROR Rs */\ + clockTicks+=2;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + if(shift) {\ + shift &= 0x1f;\ + if(shift) {\ + LOGICAL_ROR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + C_OUT = (value & 0x80000000 ? true : false);\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + C_OUT = (value & 0x80000000 ? true : false);\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+0x200:\ + case BASE+0x201:\ + case BASE+0x202:\ + case BASE+0x203:\ + case BASE+0x204:\ + case BASE+0x205:\ + case BASE+0x206:\ + case BASE+0x207:\ + case BASE+0x208:\ + case BASE+0x209:\ + case BASE+0x20a:\ + case BASE+0x20b:\ + case BASE+0x20c:\ + case BASE+0x20d:\ + case BASE+0x20e:\ + case BASE+0x20f:\ + {\ + int shift = (opcode & 0xF00) >> 7;\ + int dest = (opcode >> 12) & 0x0F;\ + bool C_OUT = C_FLAG;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + LOGICAL_ROR_IMM\ + } else {\ + value = opcode & 0xff;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break; + +#define ARITHMETIC_DATA_OPCODE(OPCODE, OPCODE2, BASE) \ + case BASE:\ + case BASE+8:\ + {\ + /* OP Rd,Rb,Rm LSL # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + ARITHMETIC_LSL_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+2:\ + case BASE+10:\ + {\ + /* OP Rd,Rb,Rm LSR # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + ARITHMETIC_LSR_REG\ + } else {\ + value = 0;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+4:\ + case BASE+12:\ + {\ + /* OP Rd,Rb,Rm ASR # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + ARITHMETIC_ASR_REG\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + } else value = 0;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+6:\ + case BASE+14:\ + {\ + /* OP Rd,Rb,Rm ROR # */\ + int base = (opcode >> 16) & 0x0F;\ + int shift = (opcode >> 7) & 0x1F;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + ARITHMETIC_ROR_REG\ + } else {\ + ARITHMETIC_RRX_REG\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+1:\ + {\ + /* OP Rd,Rb,Rm LSL Rs */\ + clockTicks++;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + clockTicks++;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + } else if(shift < 32) {\ + ARITHMETIC_LSL_REG\ + } else value = 0;\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+3:\ + {\ + /* OP Rd,Rb,Rm LSR Rs */\ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + if(shift) {\ + if(shift == 32) {\ + value = 0;\ + } else if(shift < 32) {\ + ARITHMETIC_LSR_REG\ + } else value = 0;\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+5:\ + {\ + /* OP Rd,Rb,Rm ASR Rs */\ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + if(shift < 32) {\ + if(shift) {\ + ARITHMETIC_ASR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + } else {\ + if(reg[opcode & 0x0F].I & 0x80000000){\ + value = 0xFFFFFFFF;\ + } else value = 0;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+7:\ + {\ + /* OP Rd,Rb,Rm ROR Rs */\ + clockTicks+=2;\ + int base = (opcode >> 16) & 0x0F;\ + int shift = reg[(opcode >> 8)&15].B.B0;\ + int dest = (opcode>>12) & 15;\ + u32 value;\ + if(shift) {\ + shift &= 0x1f;\ + if(shift) {\ + ARITHMETIC_ROR_REG\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + } else {\ + value = reg[opcode & 0x0F].I;\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break;\ + case BASE+0x200:\ + case BASE+0x201:\ + case BASE+0x202:\ + case BASE+0x203:\ + case BASE+0x204:\ + case BASE+0x205:\ + case BASE+0x206:\ + case BASE+0x207:\ + case BASE+0x208:\ + case BASE+0x209:\ + case BASE+0x20a:\ + case BASE+0x20b:\ + case BASE+0x20c:\ + case BASE+0x20d:\ + case BASE+0x20e:\ + case BASE+0x20f:\ + {\ + int shift = (opcode & 0xF00) >> 7;\ + int base = (opcode >> 16) & 0x0F;\ + int dest = (opcode >> 12) & 0x0F;\ + u32 value;\ + clockTicks++;\ + {\ + ARITHMETIC_ROR_IMM\ + }\ + if(dest == 15) {\ + OPCODE2\ + /* todo */\ + if(opcode & 0x00100000) {\ + clockTicks++;\ + CPUSwitchMode(reg[17].I & 0x1f, false);\ + }\ + if(armState) {\ + reg[15].I &= 0xFFFFFFFC;\ + armNextPC = reg[15].I;\ + reg[15].I += 4;\ + } else {\ + reg[15].I &= 0xFFFFFFFE;\ + armNextPC = reg[15].I;\ + reg[15].I += 2;\ + }\ + } else {\ + OPCODE \ + }\ + }\ + break; + + u32 opcode = CPUReadMemoryQuick(armNextPC); + + clockTicks = memoryWaitFetch32[(armNextPC >> 24) & 15]; + +#ifndef FINAL_VERSION + if(armNextPC == stop) { + armNextPC++; + } +#endif + + armNextPC = reg[15].I; + reg[15].I += 4; + int cond = opcode >> 28; + // suggested optimization for frequent cases + bool cond_res; + if(cond == 0x0e) { + cond_res = true; + } else { + switch(cond) { + case 0x00: // EQ + cond_res = Z_FLAG; + break; + case 0x01: // NE + cond_res = !Z_FLAG; + break; + case 0x02: // CS + cond_res = C_FLAG; + break; + case 0x03: // CC + cond_res = !C_FLAG; + break; + case 0x04: // MI + cond_res = N_FLAG; + break; + case 0x05: // PL + cond_res = !N_FLAG; + break; + case 0x06: // VS + cond_res = V_FLAG; + break; + case 0x07: // VC + cond_res = !V_FLAG; + break; + case 0x08: // HI + cond_res = C_FLAG && !Z_FLAG; + break; + case 0x09: // LS + cond_res = !C_FLAG || Z_FLAG; + break; + case 0x0A: // GE + cond_res = N_FLAG == V_FLAG; + break; + case 0x0B: // LT + cond_res = N_FLAG != V_FLAG; + break; + case 0x0C: // GT + cond_res = !Z_FLAG &&(N_FLAG == V_FLAG); + break; + case 0x0D: // LE + cond_res = Z_FLAG || (N_FLAG != V_FLAG); + break; + case 0x0E: + cond_res = true; + break; + case 0x0F: + default: + // ??? + cond_res = false; + break; + } + } + +if(cond_res) { + switch(((opcode>>16)&0xFF0) | ((opcode>>4)&0x0F)) { + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_AND, OP_AND, 0x000); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_ANDS, OP_AND, 0x010); + case 0x009: + { + // MUL Rd, Rm, Rs + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs; + if(((s32)rs)<0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x019: + { + // MULS Rd, Rm, Rs + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs; + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; + Z_FLAG = (reg[dest].I) ? false : true; + if(((s32)rs)<0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x00b: + case 0x02b: + { + // STRH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address -= offset; + reg[base].I = address; + } + break; + case 0x04b: + case 0x06b: + { + // STRH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address -= offset; + reg[base].I = address; + } + break; + case 0x08b: + case 0x0ab: + { + // STRH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address += offset; + reg[base].I = address; + } + break; + case 0x0cb: + case 0x0eb: + { + // STRH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address += offset; + reg[base].I = address; + } + break; + case 0x10b: + { + // STRH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x12b: + { + // STRH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x14b: + { + // STRH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x16b: + { + // STRH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x18b: + { + // STRH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x1ab: + { + // STRH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x1cb: + { + // STRH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x1eb: + { + // STRH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x01b: + case 0x03b: + { + // LDRH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05b: + case 0x07b: + { + // LDRH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09b: + case 0x0bb: + { + // LDRH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0db: + case 0x0fb: + { + // LDRH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11b: + { + // LDRH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x13b: + { + // LDRH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x15b: + { + // LDRH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x17b: + { + // LDRH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x19b: + { + // LDRH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x1bb: + { + // LDRH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x1db: + { + // LDRH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x1fb: + { + // LDRH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x01d: + case 0x03d: + { + // LDRSB Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05d: + case 0x07d: + { + // LDRSB Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09d: + case 0x0bd: + { + // LDRSB Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0dd: + case 0x0fd: + { + // LDRSB Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11d: + { + // LDRSB Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x13d: + { + // LDRSB Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x15d: + { + // LDRSB Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x17d: + { + // LDRSB Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x19d: + { + // LDRSB Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x1bd: + { + // LDRSB Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x1dd: + { + // LDRSB Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x1fd: + { + // LDRSB Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x01f: + case 0x03f: + { + // LDRSH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05f: + case 0x07f: + { + // LDRSH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09f: + case 0x0bf: + { + // LDRSH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0df: + case 0x0ff: + { + // LDRSH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11f: + { + // LDRSH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x13f: + { + // LDRSH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x15f: + { + // LDRSH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x17f: + { + // LDRSH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x19f: + { + // LDRSH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x1bf: + { + // LDRSH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) + reg[base].I = address; + } + break; + case 0x1df: + { + // LDRSH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x1ff: + { + // LDRSH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F)|((opcode>>4)&0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if(dest != base) + reg[base].I = address; + } + break; + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_EOR, OP_EOR, 0x020); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_EORS, OP_EOR, 0x030); + case 0x029: + { + // MLA Rd, Rm, Rs, Rn + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs + reg[(opcode>>12)&0x0f].I; + if(((s32)rs)<0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x039: + { + // MLAS Rd, Rm, Rs, Rn + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs + reg[(opcode>>12)&0x0f].I; + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; + Z_FLAG = (reg[dest].I) ? false : true; + if(((s32)rs)<0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + ARITHMETIC_DATA_OPCODE(OP_SUB, OP_SUB, 0x040); + ARITHMETIC_DATA_OPCODE(OP_SUBS, OP_SUB, 0x050); + ARITHMETIC_DATA_OPCODE(OP_RSB, OP_RSB, 0x060); + ARITHMETIC_DATA_OPCODE(OP_RSBS, OP_RSB, 0x070); + ARITHMETIC_DATA_OPCODE(OP_ADD, OP_ADD, 0x080); + ARITHMETIC_DATA_OPCODE(OP_ADDS, OP_ADD, 0x090); + case 0x089: + { + // UMULL RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = ((u64)umult)*((u64)usource); + reg[destLo].I = (u32)(uTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(uTemp >> 32); + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((usource & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x099: + { + // UMULLS RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = ((u64)umult)*((u64)usource); + reg[destLo].I = (u32)uTemp; + reg[destHi].I = (u32)(uTemp >> 32); + Z_FLAG = (uTemp) ? false : true; + N_FLAG = (reg[destHi].I & 0x80000000) ? true : false; + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((usource & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + ARITHMETIC_DATA_OPCODE(OP_ADC, OP_ADC, 0x0a0); + ARITHMETIC_DATA_OPCODE(OP_ADCS, OP_ADC, 0x0b0); + case 0x0a9: + { + // UMLAL RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = (u64)reg[destHi].I; + uTemp <<= 32; + uTemp |= (u64)reg[destLo].I; + uTemp += ((u64)umult)*((u64)usource); + reg[destLo].I = (u32)uTemp; + reg[destHi].I = (u32)(uTemp >> 32); + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((usource & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x0b9: + { + // UMLALS RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = (u64)reg[destHi].I; + uTemp <<= 32; + uTemp |= (u64)reg[destLo].I; + uTemp += ((u64)umult)*((u64)usource); + reg[destLo].I = (u32)uTemp; + reg[destHi].I = (u32)(uTemp >> 32); + Z_FLAG = (uTemp) ? false : true; + N_FLAG = (reg[destHi].I & 0x80000000) ? true : false; + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((usource & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + ARITHMETIC_DATA_OPCODE(OP_SBC, OP_SBC, 0x0c0); + ARITHMETIC_DATA_OPCODE(OP_SBCS, OP_SBC, 0x0d0); + case 0x0c9: + { + // SMULL RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = m*s; + reg[destLo].I = (u32)sTemp; + reg[destHi].I = (u32)(sTemp >> 32); + if(((s32)rs) < 0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x0d9: + { + // SMULLS RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = m*s; + reg[destLo].I = (u32)sTemp; + reg[destHi].I = (u32)(sTemp >> 32); + Z_FLAG = (sTemp) ? false : true; + N_FLAG = (sTemp < 0) ? true : false; + if(((s32)rs) < 0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + ARITHMETIC_DATA_OPCODE(OP_RSC, OP_RSC, 0x0e0); + ARITHMETIC_DATA_OPCODE(OP_RSCS, OP_RSC, 0x0f0); + case 0x0e9: + { + // SMLAL RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = (u64)reg[destHi].I; + sTemp <<= 32; + sTemp |= (u64)reg[destLo].I; + sTemp += m*s; + reg[destLo].I = (u32)sTemp; + reg[destHi].I = (u32)(sTemp >> 32); + if(((s32)rs) < 0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x0f9: + { + // SMLALS RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = (u64)reg[destHi].I; + sTemp <<= 32; + sTemp |= (u64)reg[destLo].I; + sTemp += m*s; + reg[destLo].I = (u32)sTemp; + reg[destHi].I = (u32)(sTemp >> 32); + Z_FLAG = (sTemp) ? false : true; + N_FLAG = (sTemp < 0) ? true : false; + if(((s32)rs) < 0) + rs = ~rs; + if((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + LOGICAL_DATA_OPCODE(OP_TST, OP_TST, 0x110); + case 0x100: + // MRS Rd, CPSR + // TODO: check if right instruction.... + clockTicks++; + CPUUpdateCPSR(); + reg[(opcode >> 12) & 0x0F].I = reg[16].I; + break; + case 0x109: + { + // SWP Rd, Rm, [Rn] + u32 address = reg[(opcode >> 16) & 15].I; + u32 temp = CPUReadMemory(address); + CPUWriteMemory(address, reg[opcode&15].I); + reg[(opcode >> 12) & 15].I = temp; + clockTicks++; + } + break; + LOGICAL_DATA_OPCODE(OP_TEQ, OP_TEQ, 0x130); + case 0x120: + { + // MSR CPSR_fields, Rm + CPUUpdateCPSR(); + u32 value = reg[opcode & 15].I; + u32 newValue = reg[16].I; + if(armMode > 0x10) { + if(opcode & 0x00010000) + newValue = (newValue & 0xFFFFFF00) | (value & 0x000000FF); + if(opcode & 0x00020000) + newValue = (newValue & 0xFFFF00FF) | (value & 0x0000FF00); + if(opcode & 0x00040000) + newValue = (newValue & 0xFF00FFFF) | (value & 0x00FF0000); + } + if(opcode & 0x00080000) + newValue = (newValue & 0x00FFFFFF) | (value & 0xFF000000); + newValue |= 0x10; + CPUSwitchMode(newValue & 0x1f, false); + reg[16].I = newValue; + CPUUpdateFlags(); + clockTicks++; + } + break; + case 0x121: + { + // BX Rm + // TODO: check if right instruction... + clockTicks += 3; + int base = opcode & 0x0F; + armState = reg[base].I & 1 ? false : true; + if(armState) { + reg[15].I = reg[base].I & 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } else { + reg[15].I = reg[base].I & 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + } + } + break; + ARITHMETIC_DATA_OPCODE(OP_CMP, OP_CMP, 0x150); + case 0x140: + // MRS Rd, SPSR + // TODO: check if right instruction... + reg[(opcode >> 12) & 0x0F].I = reg[17].I; + clockTicks++; + break; + case 0x149: + { + // SWPB Rd, Rm, [Rn] + u32 address = reg[(opcode >> 16) & 15].I; + u32 temp = CPUReadByte(address); + CPUWriteByte(address, reg[opcode&15].B.B0); + reg[(opcode>>12)&15].I = temp; + clockTicks++; + } + break; + ARITHMETIC_DATA_OPCODE(OP_CMN, OP_CMN, 0x170); + case 0x160: + { + // MSR SPSR_fields, Rm + u32 value = reg[opcode & 15].I; + if(armMode > 0x10 && armMode < 0x1f) { + if(opcode & 0x00010000) + reg[17].I = (reg[17].I & 0xFFFFFF00) | (value & 0x000000FF); + if(opcode & 0x00020000) + reg[17].I = (reg[17].I & 0xFFFF00FF) | (value & 0x0000FF00); + if(opcode & 0x00040000) + reg[17].I = (reg[17].I & 0xFF00FFFF) | (value & 0x00FF0000); + if(opcode & 0x00080000) + reg[17].I = (reg[17].I & 0x00FFFFFF) | (value & 0xFF000000); + } + clockTicks++; + } + break; + LOGICAL_DATA_OPCODE (OP_ORR, OP_ORR, 0x180); + LOGICAL_DATA_OPCODE (OP_ORRS, OP_ORR, 0x190); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MOV, OP_MOV, 0x1a0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MOVS, OP_MOV, 0x1b0); + LOGICAL_DATA_OPCODE (OP_BIC, OP_BIC, 0x1c0); + LOGICAL_DATA_OPCODE (OP_BICS, OP_BIC, 0x1d0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MVN, OP_MVN, 0x1e0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MVNS, OP_MVN, 0x1f0); +#ifdef BKPT_SUPPORT + case 0x127: + case 0x7ff: // for GDB support + extern void (*dbgSignal)(int,int); + reg[15].I -= 4; + armNextPC -= 4; + dbgSignal(5, (opcode & 0x0f)|((opcode>>4) & 0xfff0)); + return; +#endif + case 0x320: + case 0x321: + case 0x322: + case 0x323: + case 0x324: + case 0x325: + case 0x326: + case 0x327: + case 0x328: + case 0x329: + case 0x32a: + case 0x32b: + case 0x32c: + case 0x32d: + case 0x32e: + case 0x32f: + { + // MSR CPSR_fields, # + CPUUpdateCPSR(); + u32 value = opcode & 0xFF; + int shift = (opcode & 0xF00) >> 7; + if(shift) { + ROR_IMM_MSR; + } + u32 newValue = reg[16].I; + if(armMode > 0x10) { + if(opcode & 0x00010000) + newValue = (newValue & 0xFFFFFF00) | (value & 0x000000FF); + if(opcode & 0x00020000) + newValue = (newValue & 0xFFFF00FF) | (value & 0x0000FF00); + if(opcode & 0x00040000) + newValue = (newValue & 0xFF00FFFF) | (value & 0x00FF0000); + } + if(opcode & 0x00080000) + newValue = (newValue & 0x00FFFFFF) | (value & 0xFF000000); + + newValue |= 0x10; + + CPUSwitchMode(newValue & 0x1f, false); + reg[16].I = newValue; + CPUUpdateFlags(); + clockTicks++; + } + break; + case 0x360: + case 0x361: + case 0x362: + case 0x363: + case 0x364: + case 0x365: + case 0x366: + case 0x367: + case 0x368: + case 0x369: + case 0x36a: + case 0x36b: + case 0x36c: + case 0x36d: + case 0x36e: + case 0x36f: + { + // MSR SPSR_fields, # + if(armMode > 0x10 && armMode < 0x1f) { + u32 value = opcode & 0xFF; + int shift = (opcode & 0xF00) >> 7; + if(shift) { + ROR_IMM_MSR; + } + if(opcode & 0x00010000) + reg[17].I = (reg[17].I & 0xFFFFFF00) | (value & 0x000000FF); + if(opcode & 0x00020000) + reg[17].I = (reg[17].I & 0xFFFF00FF) | (value & 0x0000FF00); + if(opcode & 0x00040000) + reg[17].I = (reg[17].I & 0xFF00FFFF) | (value & 0x00FF0000); + if(opcode & 0x00080000) + reg[17].I = (reg[17].I & 0x00FFFFFF) | (value & 0xFF000000); + } + clockTicks++; + } + break; + CASE_16(0x400) + // T versions shouldn't be different on GBA + CASE_16(0x420) + { + // STR Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x480) + // T versions shouldn't be different on GBA + CASE_16(0x4a0) + { + // STR Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x500) + { + // STR Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x520) + { + // STR Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x580) + { + // STR Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x5a0) + { + // STR Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x410) + { + // LDR Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x430) + { + // LDRT Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x490) + { + // LDR Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x4b0) + { + // LDRT Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x510) + { + // LDR Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x530) + { + // LDR Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x590) + { + // LDR Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x5b0) + { + // LDR Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x440) + // T versions shouldn't be different on GBA + CASE_16(0x460) + { + // STRB Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x4c0) + // T versions shouldn't be different on GBA + CASE_16(0x4e0) + // STRB Rd, [Rn], # + { + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x540) + { + // STRB Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x560) + { + // STRB Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5c0) + { + // STRB Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5e0) + { + // STRB Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x450) + // T versions shouldn't be different + CASE_16(0x470) + { + // LDRB Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x4d0) + CASE_16(0x4f0) // T versions should not be different + { + // LDRB Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x550) + { + // LDRB Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x570) + { + // LDRB Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5d0) + { + // LDRB Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5f0) + { + // LDRB Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x600: + case 0x608: + // T versions are the same + case 0x620: + case 0x628: + { + // STR Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x602: + case 0x60a: + // T versions are the same + case 0x622: + case 0x62a: + { + // STR Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x604: + case 0x60c: + // T versions are the same + case 0x624: + case 0x62c: + { + // STR Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x606: + case 0x60e: + // T versions are the same + case 0x626: + case 0x62e: + { + // STR Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - value; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x680: + case 0x688: + // T versions are the same + case 0x6a0: + case 0x6a8: + { + // STR Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x682: + case 0x68a: + // T versions are the same + case 0x6a2: + case 0x6aa: + { + // STR Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x684: + case 0x68c: + // T versions are the same + case 0x6a4: + case 0x6ac: + { + // STR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x686: + case 0x68e: + // T versions are the same + case 0x6a6: + case 0x6ae: + { + // STR Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + value; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x700: + case 0x708: + { + // STR Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x702: + case 0x70a: + { + // STR Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x704: + case 0x70c: + { + // STR Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x706: + case 0x70e: + { + // STR Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x720: + case 0x728: + { + // STR Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x722: + case 0x72a: + { + // STR Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x724: + case 0x72c: + { + // STR Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x726: + case 0x72e: + { + // STR Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x780: + case 0x788: + { + // STR Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x782: + case 0x78a: + { + // STR Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x784: + case 0x78c: + { + // STR Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x786: + case 0x78e: + { + // STR Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a0: + case 0x7a8: + { + // STR Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a2: + case 0x7aa: + { + // STR Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a4: + case 0x7ac: + { + // STR Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a6: + case 0x7ae: + { + // STR Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x610: + case 0x618: + // T versions are the same + case 0x630: + case 0x638: + { + // LDR Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x612: + case 0x61a: + // T versions are the same + case 0x632: + case 0x63a: + { + // LDR Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x614: + case 0x61c: + // T versions are the same + case 0x634: + case 0x63c: + { + // LDR Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x616: + case 0x61e: + // T versions are the same + case 0x636: + case 0x63e: + { + // LDR Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address - value; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x690: + case 0x698: + // T versions are the same + case 0x6b0: + case 0x6b8: + { + // LDR Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x692: + case 0x69a: + // T versions are the same + case 0x6b2: + case 0x6ba: + { + // LDR Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x694: + case 0x69c: + // T versions are the same + case 0x6b4: + case 0x6bc: + { + // LDR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x696: + case 0x69e: + // T versions are the same + case 0x6b6: + case 0x6be: + { + // LDR Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address + value; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x710: + case 0x718: + { + // LDR Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x712: + case 0x71a: + { + // LDR Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x714: + case 0x71c: + { + // LDR Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x716: + case 0x71e: + { + // LDR Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x730: + case 0x738: + { + // LDR Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x732: + case 0x73a: + { + // LDR Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x734: + case 0x73c: + { + // LDR Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x736: + case 0x73e: + { + // LDR Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x790: + case 0x798: + { + // LDR Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x792: + case 0x79a: + { + // LDR Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x794: + case 0x79c: + { + // LDR Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x796: + case 0x79e: + { + // LDR Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b0: + case 0x7b8: + { + // LDR Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b2: + case 0x7ba: + { + // LDR Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b4: + case 0x7bc: + { + // LDR Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b6: + case 0x7be: + { + // LDR Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadMemory(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if(dest == 15) { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x640: + case 0x648: + // T versions are the same + case 0x660: + case 0x668: + { + // STRB Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x642: + case 0x64a: + // T versions are the same + case 0x662: + case 0x66a: + { + // STRB Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x644: + case 0x64c: + // T versions are the same + case 0x664: + case 0x66c: + { + // STRB Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x646: + case 0x64e: + // T versions are the same + case 0x666: + case 0x66e: + { + // STRB Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - value; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c0: + case 0x6c8: + // T versions are the same + case 0x6e0: + case 0x6e8: + { + // STRB Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c2: + case 0x6ca: + // T versions are the same + case 0x6e2: + case 0x6ea: + { + // STRB Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c4: + case 0x6cc: + // T versions are the same + case 0x6e4: + case 0x6ec: + { + // STR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c6: + case 0x6ce: + // T versions are the same + case 0x6e6: + case 0x6ee: + { + // STRB Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + value; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x740: + case 0x748: + { + // STRB Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x742: + case 0x74a: + { + // STRB Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x744: + case 0x74c: + { + // STRB Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x746: + case 0x74e: + { + // STRB Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x760: + case 0x768: + { + // STRB Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x762: + case 0x76a: + { + // STRB Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x764: + case 0x76c: + { + // STRB Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x766: + case 0x76e: + { + // STRB Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c0: + case 0x7c8: + { + // STRB Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c2: + case 0x7ca: + { + // STRB Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c4: + case 0x7cc: + { + // STRB Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c6: + case 0x7ce: + { + // STRB Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e0: + case 0x7e8: + { + // STRB Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e2: + case 0x7ea: + { + // STRB Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e4: + case 0x7ec: + { + // STRB Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e6: + case 0x7ee: + { + // STRB Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x650: + case 0x658: + // T versions are the same + case 0x670: + case 0x678: + { + // LDRB Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x652: + case 0x65a: + // T versions are the same + case 0x672: + case 0x67a: + { + // LDRB Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x654: + case 0x65c: + // T versions are the same + case 0x674: + case 0x67c: + { + // LDRB Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x656: + case 0x65e: + // T versions are the same + case 0x676: + case 0x67e: + { + // LDRB Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address - value; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d0: + case 0x6d8: + // T versions are the same + case 0x6f0: + case 0x6f8: + { + // LDRB Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d2: + case 0x6da: + // T versions are the same + case 0x6f2: + case 0x6fa: + { + // LDRB Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d4: + case 0x6dc: + // T versions are the same + case 0x6f4: + case 0x6fc: + { + // LDRB Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d6: + case 0x6de: + // T versions are the same + case 0x6f6: + case 0x6fe: + { + // LDRB Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address + value; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x750: + case 0x758: + { + // LDRB Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x752: + case 0x75a: + { + // LDRB Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x754: + case 0x75c: + { + // LDRB Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x756: + case 0x75e: + { + // LDRB Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x770: + case 0x778: + { + // LDRB Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x772: + case 0x77a: + { + // LDRB Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x774: + case 0x77c: + { + // LDRB Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x776: + case 0x77e: + { + // LDRB Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d0: + case 0x7d8: + { + // LDRB Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d2: + case 0x7da: + { + // LDRB Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d4: + case 0x7dc: + { + // LDRB Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d6: + case 0x7de: + { + // LDRB Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f0: + case 0x7f8: + { + // LDRB Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode>>7)& 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f2: + case 0x7fa: + { + // LDRB Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f4: + case 0x7fc: + { + // LDRB Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if(shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if(reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f6: + case 0x7fe: + { + // LDRB Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if(shift) { + ROR_VALUE; + } else { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadByte(address); + if(dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; +#define STMW_REG(val,num) \ + if(opcode & (val)) {\ + CPUWriteMemory(address, reg[(num)].I);\ + if(!offset) {\ + reg[base].I = temp;\ + clockTicks += 1 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + } else {\ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address);\ + }\ + address += 4;\ + } +#define STM_REG(val,num) \ + if(opcode & (val)) {\ + CPUWriteMemory(address, reg[(num)].I);\ + if(!offset) {\ + clockTicks += 1 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + } else {\ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address);\ + }\ + address += 4;\ + } + + CASE_16(0x800) + // STMDA Rn, {Rlist} + { + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x820) + { + // STMDA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + CASE_16(0x840) + { + // STMDA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + + if(armMode == 0x11) { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } else { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } else { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x860) + { + // STMDA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + + if(armMode == 0x11) { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } else { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } else { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + + CASE_16(0x880) + { + // STMIA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8a0) + { + // STMIA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4*(cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8c0) + { + // STMIA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + if(armMode == 0x11) { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } else { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + if(armMode != 0x10 && armMode != 0x1f) { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } else { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8e0) + { + // STMIA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4*(cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + if(armMode == 0x11) { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } else { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + if(armMode != 0x10 && armMode != 0x1f) { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } else { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + + CASE_16(0x900) + { + // STMDB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x920) + { + // STMDB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + CASE_16(0x940) + { + // STMDB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + + if(armMode == 0x11) { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } else { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } else { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x960) + { + // STMDB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + + if(armMode == 0x11) { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } else { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } else { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + + CASE_16(0x980) + // STMIB Rn, {Rlist} + { + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9a0) + { + // STMIB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4*(cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9c0) + { + // STMIB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + if(armMode == 0x11) { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } else { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + if(armMode != 0x10 && armMode != 0x1f) { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } else { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9e0) + { + // STMIB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4*(cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + if(armMode == 0x11) { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } else { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + if(armMode != 0x10 && armMode != 0x1f) { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } else { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + if(opcode & 32768) { + CPUWriteMemory(address, reg[15].I+4); + if(!offset) { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + +#define LDM_REG(val,num) \ + if(opcode & (val)) {\ + reg[(num)].I = CPUReadMemory(address);\ + if(offset)\ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address);\ + else {\ + clockTicks += 1 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + }\ + address += 4;\ + } + + CASE_16(0x810) + { + // LDMDA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x830) + { + // LDMDA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x850) + { + // LDMDA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x870) + { + // LDMDA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if(!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if(!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x890) + { + // LDMIA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x8b0) + { + // LDMIA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x8d0) + { + // LDMIA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x8f0) + { + // LDMIA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if(!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if(!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x910) + { + // LDMDB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x930) + { + // LDMDB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x950) + { + // LDMDB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x970) + { + // LDMDB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if(!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if(!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x990) + { + // LDMIB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x9b0) + { + // LDMIB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if(opcode & 32768) { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x9d0) + { + // LDMIB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x9f0) + { + // LDMIB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (reg[base].I+4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if(opcode & 0x8000) { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if(!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if(!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if(armState) { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } else { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } else { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if(armMode == 0x11) { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } else { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if(armMode != 0x10 && armMode != 0x1f) { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } else { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if(!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + CASE_256(0xa00) + { + // B + clockTicks += 3; + int offset = opcode & 0x00FFFFFF; + if(offset & 0x00800000) { + offset |= 0xFF000000; + } + offset <<= 2; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + CASE_256(0xb00) + { + // BL + clockTicks += 3; + int offset = opcode & 0x00FFFFFF; + if(offset & 0x00800000) { + offset |= 0xFF000000; + } + offset <<= 2; + reg[14].I = reg[15].I - 4; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + CASE_256(0xf00) + // SWI + clockTicks += 3; + CPUSoftwareInterrupt(opcode & 0x00FFFFFF); + break; +#ifdef GP_SUPPORT + case 0xe11: + case 0xe13: + case 0xe15: + case 0xe17: + case 0xe19: + case 0xe1b: + case 0xe1d: + case 0xe1f: + // MRC + break; + case 0xe01: + case 0xe03: + case 0xe05: + case 0xe07: + case 0xe09: + case 0xe0b: + case 0xe0d: + case 0xe0f: + // MRC + break; +#endif + default: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_UNDEFINED) + log("Undefined ARM instruction %08x at %08x\n", opcode, + armNextPC-4); +#endif + CPUUndefinedException(); + break; + // END + } +} diff --git a/src/armdis.cpp b/src/armdis.cpp new file mode 100644 index 00000000..5162952f --- /dev/null +++ b/src/armdis.cpp @@ -0,0 +1,742 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/************************************************************************/ +/* Arm/Thumb command set disassembler */ +/************************************************************************/ +#include + +#include "System.h" +#include "Port.h" +#include "GBA.h" +#include "armdis.h" +#include "elf.h" + +struct Opcodes { + u32 mask; + u32 cval; + char *mnemonic; +}; + +#define debuggerReadMemory(addr) \ + READ32LE(((u32*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadHalfWord(addr) \ + READ16LE(((u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadByte(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +const char hdig[] = "0123456789abcdef"; + +const char *decVals[16] = { + "0","1","2","3","4","5","6","7","8", + "9","10","11","12","13","14","15" +}; + +const char *regs[16] = { + "r0","r1","r2","r3","r4","r5","r6","r7", + "r8","r9","r10","r11","r12","sp","lr","pc" +}; + +const char *conditions[16] = { + "eq","ne","cs","cc","mi","pl","vs","vc", + "hi","ls","ge","lt","gt","le","","nv" +}; + +const char *shifts[5] = { + "lsl","lsr","asr","ror","rrx" +}; + +const char *armMultLoadStore[12] = { + // non-stack + "da","ia","db","ib", + // stack store + "ed","ea","fd","fa", + // stack load + "fa","fd","ea","ed" +}; + +const Opcodes thumbOpcodes[] = { + // Format 1 + {0xf800, 0x0000, "lsl %r0, %r3, %o"}, + {0xf800, 0x0800, "lsr %r0, %r3, %o"}, + {0xf800, 0x1000, "asr %r0, %r3, %o"}, + // Format 2 + {0xfe00, 0x1800, "add %r0, %r3, %r6"}, + {0xfe00, 0x1a00, "sub %r0, %r3, %r6"}, + {0xfe00, 0x1c00, "add %r0, %r3, %i"}, + {0xfe00, 0x1e00, "sub %r0, %r3, %i"}, + // Format 3 + {0xf800, 0x2000, "mov %r8, %O"}, + {0xf800, 0x2800, "cmp %r8, %O"}, + {0xf800, 0x3000, "add %r8, %O"}, + {0xf800, 0x3800, "sub %r8, %O"}, + // Format 4 + {0xffc0, 0x4000, "and %r0, %r3"}, + {0xffc0, 0x4040, "eor %r0, %r3"}, + {0xffc0, 0x4080, "lsl %r0, %r3"}, + {0xffc0, 0x40c0, "lsr %r0, %r3"}, + {0xffc0, 0x4100, "asr %r0, %r3"}, + {0xffc0, 0x4140, "adc %r0, %r3"}, + {0xffc0, 0x4180, "sbc %r0, %r3"}, + {0xffc0, 0x41c0, "ror %r0, %r3"}, + {0xffc0, 0x4200, "tst %r0, %r3"}, + {0xffc0, 0x4240, "neg %r0, %r3"}, + {0xffc0, 0x4280, "cmp %r0, %r3"}, + {0xffc0, 0x42c0, "cmn %r0, %r3"}, + {0xffc0, 0x4300, "orr %r0, %r3"}, + {0xffc0, 0x4340, "mul %r0, %r3"}, + {0xffc0, 0x4380, "bic %r0, %r3"}, + {0xffc0, 0x43c0, "mvn %r0, %r3"}, + // Format 5 + {0xff80, 0x4700, "bx %h36"}, + {0xfcc0, 0x4400, "[ ??? ]"}, + {0xff00, 0x4400, "add %h07, %h36"}, + {0xff00, 0x4500, "cmp %h07, %h36"}, + {0xff00, 0x4600, "mov %h07, %h36"}, + // Format 6 + {0xf800, 0x4800, "ldr %r8, [%I] (=%J)"}, + // Format 7 + {0xfa00, 0x5000, "str%b %r0, [%r3, %r6]"}, + {0xfa00, 0x5800, "ldr%b %r0, [%r3, %r6]"}, + // Format 8 + {0xfe00, 0x5200, "strh %r0, [%r3, %r6]"}, + {0xfe00, 0x5600, "ldrh %r0, [%r3, %r6]"}, + {0xfe00, 0x5a00, "ldsb %r0, [%r3, %r6]"}, + {0xfe00, 0x5e00, "ldsh %r0, [%r3, %r6]"}, + // Format 9 + {0xe800, 0x6000, "str%B %r0, [%r3, %p]"}, + {0xe800, 0x6800, "ldr%B %r0, [%r3, %p]"}, + // Format 10 + {0xf800, 0x8000, "strh %r0, [%r3, %e]"}, + {0xf800, 0x8800, "ldrh %r0, [%r3, %e]"}, + // Format 11 + {0xf800, 0x9000, "str %r8, [sp, %w]"}, + {0xf800, 0x9800, "ldr %r8, [sp, %w]"}, + // Format 12 + {0xf800, 0xa000, "add %r8, pc, %w (=%K)"}, + {0xf800, 0xa800, "add %r8, sp, %w"}, + // Format 13 + {0xff00, 0xb000, "add sp, %s"}, + // Format 14 + {0xffff, 0xb500, "push {lr}"}, + {0xff00, 0xb400, "push {%l}"}, + {0xff00, 0xb500, "push {%l,lr}"}, + {0xffff, 0xbd00, "pop {pc}"}, + {0xff00, 0xbd00, "pop {%l,pc}"}, + {0xff00, 0xbc00, "pop {%l}"}, + // Format 15 + {0xf800, 0xc000, "stmia %r8!, {%l}"}, + {0xf800, 0xc800, "ldmia %r8!, {%l}"}, + // Format 17 + {0xff00, 0xdf00, "swi %m"}, + // Format 16 + {0xf000, 0xd000, "b%c %W"}, + // Format 18 + {0xf800, 0xe000, "b %a"}, + // Format 19 + {0xf800, 0xf000, "bl %A"}, + {0xf800, 0xf800, "blh %Z"}, + {0xff00, 0xbe00, "bkpt %O"}, + // Unknown + {0x0000, 0x0000, "[ ??? ]"} +}; + +const Opcodes armOpcodes[] = { + // Undefined + {0x0e000010, 0x06000010, "[ undefined ]"}, + // Branch instructions + {0x0ff000f0, 0x01200010, "bx%c %r0"}, + {0x0f000000, 0x0a000000, "b%c %o"}, + {0x0f000000, 0x0b000000, "bl%c %o"}, + {0x0f000000, 0x0f000000, "swi%c %q"}, + // PSR transfer + {0x0fbf0fff, 0x010f0000, "mrs%c %r3, %p"}, + {0x0db0f000, 0x0120f000, "msr%c %p, %i"}, + // Multiply instructions + {0x0fe000f0, 0x00000090, "mul%c%s %r4, %r0, %r2"}, + {0x0fe000f0, 0x00200090, "mla%c%s %r4, %r0, %r2, %r3"}, + {0x0fa000f0, 0x00800090, "%umull%c%s %r3, %r4, %r0, %r2"}, + {0x0fa000f0, 0x00a00090, "%umlal%c%s %r3, %r4, %r0, %r2"}, + // Load/Store instructions + {0x0fb00ff0, 0x01000090, "swp%c%b %r3, %r0, [%r4]"}, + {0x0fb000f0, 0x01000090, "[ ??? ]"}, + {0x0c100000, 0x04000000, "str%c%b%t %r3, %a"}, + {0x0c100000, 0x04100000, "ldr%c%b%t %r3, %a"}, + {0x0e100090, 0x00000090, "str%c%h %r3, %a"}, + {0x0e100090, 0x00100090, "ldr%c%h %r3, %a"}, + {0x0e100000, 0x08000000, "stm%c%m %r4%l"}, + {0x0e100000, 0x08100000, "ldm%c%m %r4%l"}, + // Data processing + {0x0de00000, 0x00000000, "and%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00200000, "eor%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00400000, "sub%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00600000, "rsb%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00800000, "add%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00a00000, "adc%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00c00000, "sbc%c%s %r3, %r4, %i"}, + {0x0de00000, 0x00e00000, "rsc%c%s %r3, %r4, %i"}, + {0x0de00000, 0x01000000, "tst%c%s %r4, %i"}, + {0x0de00000, 0x01200000, "teq%c%s %r4, %i"}, + {0x0de00000, 0x01400000, "cmp%c%s %r4, %i"}, + {0x0de00000, 0x01600000, "cmn%c%s %r4, %i"}, + {0x0de00000, 0x01800000, "orr%c%s %r3, %r4, %i"}, + {0x0de00000, 0x01a00000, "mov%c%s %r3, %i"}, + {0x0de00000, 0x01c00000, "bic%c%s %r3, %r4, %i"}, + {0x0de00000, 0x01e00000, "mvn%c%s %r3, %i"}, + // Coprocessor operations + {0x0f000010, 0x0e000000, "cdp%c %P, %N, %r3, %R4, %R0%V"}, + {0x0e100000, 0x0c000000, "stc%c%L %P, %r3, %A"}, + {0x0f100010, 0x0e000010, "mcr%c %P, %N, %r3, %R4, %R0%V"}, + {0x0f100010, 0x0e100010, "mrc%c %P, %N, %r3, %R4, %R0%V"}, + // Unknown + {0x00000000, 0x00000000, "[ ??? ]"} +}; + +char* addStr(char *dest, const char *src){ + while (*src){ + *dest++ = *src++; + } + return dest; +} + +char* addHex(char *dest, int siz, u32 val){ + if (siz==0){ + siz = 28; + while ( (((val>>siz)&15)==0) && (siz>=4) ) + siz -= 4; + siz += 4; + } + while (siz>0){ + siz -= 4; + *dest++ = hdig[(val>>siz)&15]; + } + return dest; +} + +int disArm(u32 offset, char *dest, int flags){ + u32 opcode = debuggerReadMemory(offset); + + const Opcodes *sp = armOpcodes; + while( sp->cval != (opcode & sp->mask) ) + sp++; + + if (flags&DIS_VIEW_ADDRESS){ + dest = addHex(dest, 32, offset); + *dest++ = ' '; + } + if (flags&DIS_VIEW_CODE){ + dest = addHex(dest, 32, opcode); + *dest++ = ' '; + } + + char *src = sp->mnemonic; + while (*src){ + if (*src!='%') + *dest++ = *src++; + else{ + src++; + switch (*src){ + case 'c': + dest = addStr(dest, conditions[opcode>>28]); + break; + case 'r': + dest = addStr(dest, regs[(opcode>>((*(++src)-'0')*4))&15]); + break; + case 'o': + { + *dest++ = '$'; + int off = opcode&0xffffff; + if (off&0x800000) + off |= 0xff000000; + off <<= 2; + dest = addHex(dest, 32, offset+8+off); + } + break; + case 'i': + if (opcode&(1<<25)){ + dest = addStr(dest, "#0x"); + int imm = opcode&0xff; + int rot = (opcode&0xf00)>>7; + int val = (imm<<(32-rot))|(imm>>rot); + dest = addHex(dest, 0, val); + } else{ + dest = addStr(dest, regs[opcode&0x0f]); + int shi = (opcode>>5)&3; + int sdw = (opcode>>7)&0x1f; + if ((sdw==0)&&(shi==3)) + shi = 4; + if ( (sdw) || (opcode&0x10) || (shi)) { + dest = addStr(dest, ", "); + dest = addStr(dest, shifts[shi]); + if (opcode&0x10){ + *dest++ = ' '; + dest = addStr(dest, regs[(opcode>>8)&15]); + } else { + if (sdw==0 && ( (shi==1) || (shi==2) )) + sdw = 32; + if(shi != 4) { + dest = addStr(dest, " #0x"); + dest = addHex(dest, 8, sdw); + } + } + } + } + break; + case 'p': + if (opcode&(1<<22)) + dest = addStr(dest, "spsr"); + else + dest = addStr(dest, "cpsr"); + if(opcode & 0x00F00000) { + *dest++ = '_'; + if(opcode & 0x00080000) + *dest++ = 'f'; + if(opcode & 0x00040000) + *dest++ = 's'; + if(opcode & 0x00020000) + *dest++ = 'x'; + if(opcode & 0x00010000) + *dest++ = 'c'; + } + break; + case 's': + if (opcode&(1<<20)) + *dest++ = 's'; + break; + case 'S': + if (opcode&(1<<22)) + *dest++ = 's'; + break; + case 'u': + if (opcode&(1<<22)) + *dest++ = 's'; + else + *dest++ = 'u'; + break; + case 'b': + if (opcode&(1<<22)) + *dest++ = 'b'; + break; + case 'a': + if ((opcode&0x076f0000)==0x004f0000){ + *dest++ = '['; + *dest++ = '$'; + int adr = offset+8; + int add = (opcode&15)|((opcode>>8)&0xf0); + if (opcode&(1<<23)) + adr += add; + else + adr -= add; + dest = addHex(dest, 32, adr); + *dest++ = ']'; + dest = addStr(dest, " (="); + *dest++ = '$'; + dest = addHex(dest ,32, debuggerReadMemory(adr)); + *dest++=')'; + } + if ((opcode&0x072f0000)==0x050f0000){ + *dest++ = '['; + *dest++ = '$'; + int adr = offset+8; + if (opcode&(1<<23)) + adr += opcode&0xfff; + else + adr -= opcode&0xfff; + dest = addHex(dest, 32, adr); + *dest++ = ']'; + dest = addStr(dest, " (="); + *dest++ = '$'; + dest = addHex(dest ,32, debuggerReadMemory(adr)); + *dest++=')'; + } else { + int reg = (opcode>>16)&15; + *dest++ = '['; + dest = addStr(dest, regs[reg]); + if (!(opcode&(1<<24))) + *dest++ = ']'; + if ( ((opcode&(1<<25))&&(opcode&(1<<26))) || (!(opcode&(1<<22))&&!(opcode&(1<<26))) ){ + dest = addStr(dest, ", "); + if (!(opcode&(1<<23))) + *dest++ = '-'; + dest = addStr(dest, regs[opcode&0x0f]); + int shi = (opcode>>5)&3; + if (opcode&(1<<26)){ + if ( ((opcode>>7)&0x1f) || (opcode&0x10) || (shi==1) || (shi==2)){ + dest = addStr(dest, ", "); + dest = addStr(dest, shifts[shi]); + if (opcode&0x10){ + *dest++ = ' '; + dest = addStr(dest, regs[(opcode>>8)&15]); + } else { + int sdw = (opcode>>7)&0x1f; + if (sdw==0 && ( (shi==1) || (shi==2) )) + sdw = 32; + dest = addStr(dest, " #0x"); + dest = addHex(dest, 8, sdw); + } + } + } + } else { + int off; + if (opcode&(1<<26)) + off = opcode&0xfff; + else + off = (opcode&15)|((opcode>>4)&0xf0); + if (off){ + dest = addStr(dest, ", "); + if (!(opcode&(1<<23))) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, off); + } + } + if (opcode&(1<<24)){ + *dest++ = ']'; + if (opcode&(1<<21)) + *dest++ = '!'; + } + } + break; + case 't': + if ((opcode&0x01200000)==0x01200000) + *dest++ = 't'; + break; + case 'h': + if (opcode&(1<<6)) + *dest++ = 's'; + if (opcode&(1<<5)) + *dest++ = 'h'; + else + *dest++ = 'b'; + break; + case 'm': + if (((opcode>>16)&15)==13) { + if(opcode & 0x00100000) + dest = addStr(dest, armMultLoadStore[8+((opcode>>23)&3)]); + else + dest = addStr(dest, armMultLoadStore[4+((opcode>>23)&3)]); + } else + dest = addStr(dest, armMultLoadStore[(opcode>>23)&3]); + break; + case 'l': + if (opcode&(1<<21)) + *dest++ = '!'; + dest = addStr(dest, ", {"); + { + int rlst = opcode&0xffff; + int msk = 0; + int not_first = 0; + while (msk<16){ + if (rlst&(1<>8)&15]); + break; + case 'N': + if (opcode&0x10) + dest = addStr(dest, decVals[(opcode>>21)&7]); + else + dest = addStr(dest, decVals[(opcode>>20)&15]); + break; + case 'R': + { + src++; + int reg = 4*(*src-'0'); + *dest++ = 'c'; + dest = addStr(dest, decVals[(opcode>>reg)&15]); + } + break; + case 'V': + { + int val = (opcode>>5)&7; + if (val){ + dest = addStr(dest, ", "); + dest = addStr(dest, decVals[val]); + } + } + break; + case 'L': + if (opcode&(1<<22)) + *dest++ = 'l'; + break; + case 'A': + if ((opcode&0x012f0000)==0x010f0000){ + int adr = offset+8; + int add = (opcode&0xff)<<2; + if (opcode&(1<<23)) + adr += add; + else + adr -= add; + *dest++ = '$'; + addHex(dest, 32, adr); + } else { + *dest++ = '['; + dest = addStr(dest, regs[(opcode>>16)&15]); + if (!(opcode&(1<<24))) + *dest++ = ']'; + int off = (opcode&0xff)<<2; + if (off){ + dest = addStr(dest, ", "); + if (!(opcode&(1<<23))) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, off); + } + if (opcode&(1<<24)){ + *dest++ = ']'; + if (opcode&(1<<21)) + *dest++ = '!'; + } + } + break; + } + src++; + } + } + *dest++ = 0; + + return 4; +} + +int disThumb(u32 offset, char *dest, int flags){ + u32 opcode = debuggerReadHalfWord(offset); + + const Opcodes *sp = thumbOpcodes; + int ret = 2; + while( sp->cval != (opcode & sp->mask) ) + sp++; + + if (flags&DIS_VIEW_ADDRESS){ + dest = addHex(dest, 32, offset); + *dest++ = ' '; + } + if (flags&DIS_VIEW_CODE){ + dest = addHex(dest, 16, opcode); + *dest++ = ' '; + } + + char *src = sp->mnemonic; + while (*src){ + if (*src!='%') + *dest++ = *src++; + else { + src++; + switch (*src){ + case 'r': + src++; + dest = addStr(dest, regs[(opcode>>(*src-'0'))&7]); + break; + case 'o': + dest = addStr(dest, "#0x"); + { + int val = (opcode>>6)&0x1f; + dest = addHex(dest, 8, val); + } + break; + case 'p': + dest = addStr(dest, "#0x"); + { + int val = (opcode>>6)&0x1f; + if (!(opcode&(1<<12))) + val <<= 2; + dest = addHex(dest, 0, val); + } + break; + case 'e': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, ((opcode>>6)&0x1f)<<1); + break; + case 'i': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode>>6)&7); + break; + case 'h': + { + src++; + int reg = (opcode>>(*src-'0'))&7; + src++; + if (opcode&(1<<(*src-'0'))) + reg += 8; + dest = addStr(dest, regs[reg]); + } + break; + case 'O': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode&0xff)); + break; + case 'I': + *dest++ = '$'; + dest = addHex(dest, 32, (offset&0xfffffffc)+4+((opcode&0xff)<<2)); + break; + case 'J': + { + u32 value = debuggerReadMemory((offset&0xfffffffc)+4+ + ((opcode & 0xff)<<2)); + *dest++ = '$'; + dest = addHex(dest, 32, value); + char *s = elfGetAddressSymbol(value); + if(*s) { + *dest++ = ' '; + dest = addStr(dest, s); + } + } + break; + case 'K': + { + u32 value = (offset&0xfffffffc)+4+((opcode & 0xff)<<2); + *dest++ = '$'; + dest = addHex(dest, 32, value); + char *s = elfGetAddressSymbol(value); + if(*s) { + *dest++ = ' '; + dest = addStr(dest, s); + } + } + break; + case 'b': + if (opcode&(1<<10)) + *dest++ = 'b'; + break; + case 'B': + if (opcode&(1<<12)) + *dest++ = 'b'; + break; + case 'w': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode&0xff)<<2); + break; + case 'W': + *dest++ = '$'; + { + int add = opcode&0xff; + if (add&0x80) + add |= 0xffffff00; + dest = addHex(dest, 32, (offset&0xfffffffe)+4+(add<<1)); + } + break; + case 'c': + dest = addStr(dest, conditions[(opcode>>8)&15]); + break; + case 's': + if (opcode&(1<<7)) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode&0x7f)<<2); + break; + case 'l': + { + int rlst = opcode&0xff; + int msk = 0; + int not_first = 0; + while (msk<8){ + if (rlst&(1<>3) << systemRedShift |\ + ((g) >> 3) << systemGreenShift |\ + ((b) >> 3) << systemBlueShift\ + +static void fill_rgb_row_16(u16 *from, int src_width, u8 *row, int width) +{ + u8 *copy_start = row + src_width*3; + u8 *all_stop = row + width*3; + while (row < copy_start) { + u16 color = *from++; + *row++ = ((color >> systemRedShift) & 0x1f) << 3; + *row++ = ((color >> systemGreenShift) & 0x1f) << 3; + *row++ = ((color >> systemBlueShift) & 0x1f) << 3; + } + // any remaining elements to be written to 'row' are a replica of the + // preceding pixel + u8 *p = row-3; + while (row < all_stop) { + // we're guaranteed three elements per pixel; could unroll the loop + // further, especially with a Duff's Device, but the gains would be + // probably limited (judging by profiler output) + *row++ = *p++; + *row++ = *p++; + *row++ = *p++; + } +} + +static void fill_rgb_row_32(u32 *from, int src_width, u8 *row, int width) +{ + u8 *copy_start = row + src_width*3; + u8 *all_stop = row + width*3; + while (row < copy_start) { + u32 color = *from++; + *row++ = ((color >> systemRedShift) & 0x1f) << 3; + *row++ = ((color >> systemGreenShift) & 0x1f) << 3; + *row++ = ((color >> systemBlueShift) & 0x1f) << 3; + } + // any remaining elements to be written to 'row' are a replica of the + // preceding pixel + u8 *p = row-3; + while (row < all_stop) { + // we're guaranteed three elements per pixel; could unroll the loop + // further, especially with a Duff's Device, but the gains would be + // probably limited (judging by profiler output) + *row++ = *p++; + *row++ = *p++; + *row++ = *p++; + } +} + +void Bilinear(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *to = (u16 *)dstPtr; + u16 *to_odd = (u16 *)(dstPtr + dstPitch); + + int from_width = width; + u16 *from = (u16 *)srcPtr; + fill_rgb_row_16(from, from_width, rgb_row_cur, width+1); + + for(int y = 0; y < height; y++) { + u16 *from_orig = from; + u16 *to_orig = to; + + if (y+1 < height) + fill_rgb_row_16(from+width+2, from_width, rgb_row_next, + width+1); + else + fill_rgb_row_16(from, from_width, rgb_row_next, width+1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for(int x=0; x < width; x++) { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + *to++ = RGB(*ar, *ag, *ab); + + // upper right + *to++ = RGB((*ar+*br)>>1, (*ag+*bg)>>1, (*ab+*bb)>>1); + + // lower left + *to_odd++ = RGB((*ar+*cr)>>1, (*ag+*cg)>>1, (*ab+*cb)>>1); + + // lower right + *to_odd++ = RGB((*ar+*br+*cr+*dr)>>2, + (*ag+*bg+*cg+*dg)>>2, + (*ab+*bb+*cb+*db)>>2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u16 *)((u8 *)from_orig + srcPitch); + to = (u16 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u16 *)((u8 *)to + dstPitch); + } +} + +void BilinearPlus(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *to = (u16 *)dstPtr; + u16 *to_odd = (u16 *)(dstPtr + dstPitch); + + int from_width = width; + u16 *from = (u16 *)srcPtr; + fill_rgb_row_16(from, from_width, rgb_row_cur, width+1); + + for(int y = 0; y < height; y++) { + u16 *from_orig = from; + u16 *to_orig = to; + + if (y+1 < height) + fill_rgb_row_16(from+width+2, from_width, rgb_row_next, + width+1); + else + fill_rgb_row_16(from, from_width, rgb_row_next, width+1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for(int x=0; x < width; x++) { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + //*to++ = manip.rgb(*ar, *ag, *ab); +#ifdef USE_ORIGINAL_BILINEAR_PLUS + *to++ = RGB( + (((*ar)<<2) +((*ar)) + (*cr+*br+*br) )>> 3, + (((*ag)<<2) +((*ag)) + (*cg+*bg+*bg) )>> 3, + (((*ab)<<2) +((*ab)) + (*cb+*bb+*bb) )>> 3); +#else + *to++ = RGB( + (((*ar)<<3) +((*ar)<<1) + (*cr+*br+*br+*cr) )>> 4, + (((*ag)<<3) +((*ag)<<1) + (*cg+*bg+*bg+*cg) )>> 4, + (((*ab)<<3) +((*ab)<<1) + (*cb+*bb+*bb+*cb) )>> 4); +#endif + + // upper right + *to++ = RGB((*ar+*br)>>1, (*ag+*bg)>>1, (*ab+*bb)>>1); + + // lower left + *to_odd++ = RGB((*ar+*cr)>>1, (*ag+*cg)>>1, (*ab+*cb)>>1); + + // lower right + *to_odd++ = RGB((*ar+*br+*cr+*dr)>>2, + (*ag+*bg+*cg+*dg)>>2, + (*ab+*bb+*cb+*db)>>2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u16 *)((u8 *)from_orig + srcPitch); + to = (u16 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u16 *)((u8 *)to + dstPitch); + } +} + +void Bilinear32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *to = (u32 *)dstPtr; + u32 *to_odd = (u32 *)(dstPtr + dstPitch); + + int from_width = width; + if(width+1 < from_width) + from_width = width+1; + u32 *from = (u32 *)srcPtr; + fill_rgb_row_32(from, from_width, rgb_row_cur, width+1); + + for(int y = 0; y < height; y++) { + u32 *from_orig = from; + u32 *to_orig = to; + + if (y+1 < height) + fill_rgb_row_32(from+width+1, from_width, rgb_row_next, + width+1); + else + fill_rgb_row_32(from, from_width, rgb_row_next, width+1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for(int x=0; x < width; x++) { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + *to++ = RGB(*ar, *ag, *ab); + + // upper right + *to++ = RGB((*ar+*br)>>1, (*ag+*bg)>>1, (*ab+*bb)>>1); + + // lower left + *to_odd++ = RGB((*ar+*cr)>>1, (*ag+*cg)>>1, (*ab+*cb)>>1); + + // lower right + *to_odd++ = RGB((*ar+*br+*cr+*dr)>>2, + (*ag+*bg+*cg+*dg)>>2, + (*ab+*bb+*cb+*db)>>2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u32 *)((u8 *)from_orig + srcPitch); + to = (u32 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u32 *)((u8 *)to + dstPitch); + } +} + +void BilinearPlus32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *to = (u32 *)dstPtr; + u32 *to_odd = (u32 *)(dstPtr + dstPitch); + + int from_width = width; + if(width+1 < from_width) + from_width = width+1; + u32 *from = (u32 *)srcPtr; + fill_rgb_row_32(from, from_width, rgb_row_cur, width+1); + + for(int y = 0; y < height; y++) { + u32 *from_orig = from; + u32 *to_orig = to; + + if (y+1 < height) + fill_rgb_row_32(from+width+1, from_width, rgb_row_next, + width+1); + else + fill_rgb_row_32(from, from_width, rgb_row_next, width+1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for(int x=0; x < width; x++) { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + //*to++ = manip.rgb(*ar, *ag, *ab); +#ifdef USE_ORIGINAL_BILINEAR_PLUS + *to++ = RGB( + (((*ar)<<2) +((*ar)) + (*cr+*br+*br) )>> 3, + (((*ag)<<2) +((*ag)) + (*cg+*bg+*bg) )>> 3, + (((*ab)<<2) +((*ab)) + (*cb+*bb+*bb) )>> 3); +#else + *to++ = RGB( + (((*ar)<<3) +((*ar)<<1) + (*cr+*br+*br+*cr) )>> 4, + (((*ag)<<3) +((*ag)<<1) + (*cg+*bg+*bg+*cg) )>> 4, + (((*ab)<<3) +((*ab)<<1) + (*cb+*bb+*bb+*cb) )>> 4); +#endif + + // upper right + *to++ = RGB((*ar+*br)>>1, (*ag+*bg)>>1, (*ab+*bb)>>1); + + // lower left + *to_odd++ = RGB((*ar+*cr)>>1, (*ag+*cg)>>1, (*ab+*cb)>>1); + + // lower right + *to_odd++ = RGB((*ar+*br+*cr+*dr)>>2, + (*ag+*bg+*cg+*dg)>>2, + (*ab+*bb+*cb+*db)>>2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u32 *)((u8 *)from_orig + srcPitch); + to = (u32 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u32 *)((u8 *)to + dstPitch); + } +} + diff --git a/src/bios.cpp b/src/bios.cpp new file mode 100644 index 00000000..7876493c --- /dev/null +++ b/src/bios.cpp @@ -0,0 +1,1156 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include + +#include "GBA.h" +#include "bios.h" +#include "GBAinline.h" +#include "Globals.h" + +s16 sineTable[256] = { + (s16)0x0000, (s16)0x0192, (s16)0x0323, (s16)0x04B5, (s16)0x0645, (s16)0x07D5, (s16)0x0964, (s16)0x0AF1, + (s16)0x0C7C, (s16)0x0E05, (s16)0x0F8C, (s16)0x1111, (s16)0x1294, (s16)0x1413, (s16)0x158F, (s16)0x1708, + (s16)0x187D, (s16)0x19EF, (s16)0x1B5D, (s16)0x1CC6, (s16)0x1E2B, (s16)0x1F8B, (s16)0x20E7, (s16)0x223D, + (s16)0x238E, (s16)0x24DA, (s16)0x261F, (s16)0x275F, (s16)0x2899, (s16)0x29CD, (s16)0x2AFA, (s16)0x2C21, + (s16)0x2D41, (s16)0x2E5A, (s16)0x2F6B, (s16)0x3076, (s16)0x3179, (s16)0x3274, (s16)0x3367, (s16)0x3453, + (s16)0x3536, (s16)0x3612, (s16)0x36E5, (s16)0x37AF, (s16)0x3871, (s16)0x392A, (s16)0x39DA, (s16)0x3A82, + (s16)0x3B20, (s16)0x3BB6, (s16)0x3C42, (s16)0x3CC5, (s16)0x3D3E, (s16)0x3DAE, (s16)0x3E14, (s16)0x3E71, + (s16)0x3EC5, (s16)0x3F0E, (s16)0x3F4E, (s16)0x3F84, (s16)0x3FB1, (s16)0x3FD3, (s16)0x3FEC, (s16)0x3FFB, + (s16)0x4000, (s16)0x3FFB, (s16)0x3FEC, (s16)0x3FD3, (s16)0x3FB1, (s16)0x3F84, (s16)0x3F4E, (s16)0x3F0E, + (s16)0x3EC5, (s16)0x3E71, (s16)0x3E14, (s16)0x3DAE, (s16)0x3D3E, (s16)0x3CC5, (s16)0x3C42, (s16)0x3BB6, + (s16)0x3B20, (s16)0x3A82, (s16)0x39DA, (s16)0x392A, (s16)0x3871, (s16)0x37AF, (s16)0x36E5, (s16)0x3612, + (s16)0x3536, (s16)0x3453, (s16)0x3367, (s16)0x3274, (s16)0x3179, (s16)0x3076, (s16)0x2F6B, (s16)0x2E5A, + (s16)0x2D41, (s16)0x2C21, (s16)0x2AFA, (s16)0x29CD, (s16)0x2899, (s16)0x275F, (s16)0x261F, (s16)0x24DA, + (s16)0x238E, (s16)0x223D, (s16)0x20E7, (s16)0x1F8B, (s16)0x1E2B, (s16)0x1CC6, (s16)0x1B5D, (s16)0x19EF, + (s16)0x187D, (s16)0x1708, (s16)0x158F, (s16)0x1413, (s16)0x1294, (s16)0x1111, (s16)0x0F8C, (s16)0x0E05, + (s16)0x0C7C, (s16)0x0AF1, (s16)0x0964, (s16)0x07D5, (s16)0x0645, (s16)0x04B5, (s16)0x0323, (s16)0x0192, + (s16)0x0000, (s16)0xFE6E, (s16)0xFCDD, (s16)0xFB4B, (s16)0xF9BB, (s16)0xF82B, (s16)0xF69C, (s16)0xF50F, + (s16)0xF384, (s16)0xF1FB, (s16)0xF074, (s16)0xEEEF, (s16)0xED6C, (s16)0xEBED, (s16)0xEA71, (s16)0xE8F8, + (s16)0xE783, (s16)0xE611, (s16)0xE4A3, (s16)0xE33A, (s16)0xE1D5, (s16)0xE075, (s16)0xDF19, (s16)0xDDC3, + (s16)0xDC72, (s16)0xDB26, (s16)0xD9E1, (s16)0xD8A1, (s16)0xD767, (s16)0xD633, (s16)0xD506, (s16)0xD3DF, + (s16)0xD2BF, (s16)0xD1A6, (s16)0xD095, (s16)0xCF8A, (s16)0xCE87, (s16)0xCD8C, (s16)0xCC99, (s16)0xCBAD, + (s16)0xCACA, (s16)0xC9EE, (s16)0xC91B, (s16)0xC851, (s16)0xC78F, (s16)0xC6D6, (s16)0xC626, (s16)0xC57E, + (s16)0xC4E0, (s16)0xC44A, (s16)0xC3BE, (s16)0xC33B, (s16)0xC2C2, (s16)0xC252, (s16)0xC1EC, (s16)0xC18F, + (s16)0xC13B, (s16)0xC0F2, (s16)0xC0B2, (s16)0xC07C, (s16)0xC04F, (s16)0xC02D, (s16)0xC014, (s16)0xC005, + (s16)0xC000, (s16)0xC005, (s16)0xC014, (s16)0xC02D, (s16)0xC04F, (s16)0xC07C, (s16)0xC0B2, (s16)0xC0F2, + (s16)0xC13B, (s16)0xC18F, (s16)0xC1EC, (s16)0xC252, (s16)0xC2C2, (s16)0xC33B, (s16)0xC3BE, (s16)0xC44A, + (s16)0xC4E0, (s16)0xC57E, (s16)0xC626, (s16)0xC6D6, (s16)0xC78F, (s16)0xC851, (s16)0xC91B, (s16)0xC9EE, + (s16)0xCACA, (s16)0xCBAD, (s16)0xCC99, (s16)0xCD8C, (s16)0xCE87, (s16)0xCF8A, (s16)0xD095, (s16)0xD1A6, + (s16)0xD2BF, (s16)0xD3DF, (s16)0xD506, (s16)0xD633, (s16)0xD767, (s16)0xD8A1, (s16)0xD9E1, (s16)0xDB26, + (s16)0xDC72, (s16)0xDDC3, (s16)0xDF19, (s16)0xE075, (s16)0xE1D5, (s16)0xE33A, (s16)0xE4A3, (s16)0xE611, + (s16)0xE783, (s16)0xE8F8, (s16)0xEA71, (s16)0xEBED, (s16)0xED6C, (s16)0xEEEF, (s16)0xF074, (s16)0xF1FB, + (s16)0xF384, (s16)0xF50F, (s16)0xF69C, (s16)0xF82B, (s16)0xF9BB, (s16)0xFB4B, (s16)0xFCDD, (s16)0xFE6E +}; + +void BIOS_ArcTan() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("ArcTan: %08x (VCOUNT=%2d)\n", + reg[0].I, + VCOUNT); + } +#endif + + s32 a = -(((s32)(reg[0].I*reg[0].I)) >> 14); + s32 b = ((0xA9 * a) >> 14) + 0x390; + b = ((b * a) >> 14) + 0x91C; + b = ((b * a) >> 14) + 0xFB6; + b = ((b * a) >> 14) + 0x16AA; + b = ((b * a) >> 14) + 0x2081; + b = ((b * a) >> 14) + 0x3651; + b = ((b * a) >> 14) + 0xA2F9; + a = ((s32)reg[0].I * b) >> 16; + reg[0].I = a; + +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("ArcTan: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_ArcTan2() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("ArcTan2: %08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + s16 x = reg[0].I; + s16 y = reg[1].I; + s16 res = 0; + if (y == 0) { + res = 0x8000 & x; + } else { + if (x == 0) { + res = (0x8000 & y) + 0x4000; + } else { + if (abs(x) > abs(y)) { + reg[1].I = x; + reg[0].I = y << 14; + BIOS_Div(); + BIOS_ArcTan(); + if (x < 0) + res = 0x8000 + reg[0].I; + else + res = ((y & 0x8000) << 1 ) + reg[0].I; + } else { + reg[0].I = x << 14; + BIOS_Div(); + BIOS_ArcTan(); + res = (0x4000 + (y & 0x8000)) - reg[0].I; + } + } + } + reg[0].I = ((u32)res) & 0xffff; + +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("ArcTan2: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_BitUnPack() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("BitUnPack: %08x,%08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 header = reg[2].I; + + int len = CPUReadHalfWord(header); + // check address + int bits = CPUReadByte(header+2); + int revbits = 8 - bits; + // u32 value = 0; + u32 base = CPUReadMemory(header+4); + bool addBase = (base & 0x80000000) ? true : false; + base &= 0x7fffffff; + int dataSize = CPUReadByte(header+3); + + int data = 0; + int bitwritecount = 0; + while(1) { + len -= 1; + if(len < 0) + break; + int mask = 0xff >> revbits; + u8 b = CPUReadByte(source); + source++; + int bitcount = 0; + while(1) { + if(bitcount >= 8) + break; + u32 d = b & mask; + u32 temp = d >> bitcount; + if(!temp && addBase) { + temp += base; + } + data |= temp << bitwritecount; + bitwritecount += dataSize; + if(bitwritecount >= 32) { + CPUWriteMemory(dest, data); + dest += 4; + data = 0; + bitwritecount = 0; + } + mask <<= bits; + bitcount += bits; + } + } +} + +void BIOS_BgAffineSet() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("BgAffineSet: %08x,%08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + u32 src = reg[0].I; + u32 dest = reg[1].I; + int num = reg[2].I; + + for(int i = 0; i < num; i++) { + s32 cx = CPUReadMemory(src); + src+=4; + s32 cy = CPUReadMemory(src); + src+=4; + s16 dispx = CPUReadHalfWord(src); + src+=2; + s16 dispy = CPUReadHalfWord(src); + src+=2; + s16 rx = CPUReadHalfWord(src); + src+=2; + s16 ry = CPUReadHalfWord(src); + src+=2; + u16 theta = CPUReadHalfWord(src)>>8; + src+=4; // keep structure alignment + s32 a = sineTable[(theta+0x40)&255]; + s32 b = sineTable[theta]; + + s16 dx = (rx * a)>>14; + s16 dmx = (rx * b)>>14; + s16 dy = (ry * b)>>14; + s16 dmy = (ry * a)>>14; + + CPUWriteHalfWord(dest, dx); + dest += 2; + CPUWriteHalfWord(dest, -dmx); + dest += 2; + CPUWriteHalfWord(dest, dy); + dest += 2; + CPUWriteHalfWord(dest, dmy); + dest += 2; + + s32 startx = cx - dx * dispx + dmx * dispy; + s32 starty = cy - dy * dispx - dmy * dispy; + + CPUWriteMemory(dest, startx); + dest += 4; + CPUWriteMemory(dest, starty); + dest += 4; + } +} + +void BIOS_CpuSet() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("CpuSet: 0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + reg[2].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 cnt = reg[2].I; + + if(((source & 0xe000000) == 0) || + ((source + (((cnt << 11)>>9) & 0x1fffff)) & 0xe000000) == 0) + return; + + int count = cnt & 0x1FFFFF; + + // 32-bit ? + if((cnt >> 26) & 1) { + // needed for 32-bit mode! + source &= 0xFFFFFFFC; + dest &= 0xFFFFFFFC; + // fill ? + if((cnt >> 24) & 1) { + u32 value = CPUReadMemory(source); + while(count) { + CPUWriteMemory(dest, value); + dest += 4; + count--; + } + } else { + // copy + while(count) { + CPUWriteMemory(dest, CPUReadMemory(source)); + source += 4; + dest += 4; + count--; + } + } + } else { + // 16-bit fill? + if((cnt >> 24) & 1) { + u16 value = CPUReadHalfWord(source); + while(count) { + CPUWriteHalfWord(dest, value); + dest += 2; + count--; + } + } else { + // copy + while(count) { + CPUWriteHalfWord(dest, CPUReadHalfWord(source)); + source += 2; + dest += 2; + count--; + } + } + } +} + +void BIOS_CpuFastSet() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("CpuFastSet: 0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + reg[2].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 cnt = reg[2].I; + + if(((source & 0xe000000) == 0) || + ((source + (((cnt << 11)>>9) & 0x1fffff)) & 0xe000000) == 0) + return; + + // needed for 32-bit mode! + source &= 0xFFFFFFFC; + dest &= 0xFFFFFFFC; + + int count = cnt & 0x1FFFFF; + + // fill? + if((cnt >> 24) & 1) { + while(count > 0) { + // BIOS always transfers 32 bytes at a time + u32 value = CPUReadMemory(source); + for(int i = 0; i < 8; i++) { + CPUWriteMemory(dest, value); + dest += 4; + } + count -= 8; + } + } else { + // copy + while(count > 0) { + // BIOS always transfers 32 bytes at a time + for(int i = 0; i < 8; i++) { + CPUWriteMemory(dest, CPUReadMemory(source)); + source += 4; + dest += 4; + } + count -= 8; + } + } +} + +void BIOS_Diff8bitUnFilterWram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Diff8bitUnFilterWram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff) & 0xe000000) == 0)) + return; + + int len = header >> 8; + + u8 data = CPUReadByte(source++); + CPUWriteByte(dest++, data); + len--; + + while(len > 0) { + u8 diff = CPUReadByte(source++); + data += diff; + CPUWriteByte(dest++, data); + len--; + } +} + +void BIOS_Diff8bitUnFilterVram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Diff8bitUnFilterVram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + u8 data = CPUReadByte(source++); + u16 writeData = data; + int shift = 8; + int bytes = 1; + + while(len >= 2) { + u8 diff = CPUReadByte(source++); + data += diff; + writeData |= (data << shift); + bytes++; + shift += 8; + if(bytes == 2) { + CPUWriteHalfWord(dest, writeData); + dest += 2; + len -= 2; + bytes = 0; + writeData = 0; + shift = 0; + } + } +} + +void BIOS_Diff16bitUnFilter() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Diff16bitUnFilter: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + u16 data = CPUReadHalfWord(source); + source += 2; + CPUWriteHalfWord(dest, data); + dest += 2; + len -= 2; + + while(len >= 2) { + u16 diff = CPUReadHalfWord(source); + source += 2; + data += diff; + CPUWriteHalfWord(dest, data); + dest += 2; + len -= 2; + } +} + +void BIOS_Div() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Div: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + int number = reg[0].I; + int denom = reg[1].I; + + if(denom != 0) { + reg[0].I = number / denom; + reg[1].I = number % denom; + s32 temp = (s32)reg[0].I; + reg[3].I = temp < 0 ? (u32)-temp : (u32)temp; + } +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Div: return=0x%08x,0x%08x,0x%08x\n", + reg[0].I, + reg[1].I, + reg[3].I); + } +#endif +} + +void BIOS_DivARM() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("DivARM: 0x%08x, (VCOUNT=%d)\n", + reg[0].I, + VCOUNT); + } +#endif + + u32 temp = reg[0].I; + reg[0].I = reg[1].I; + reg[1].I = temp; + BIOS_Div(); +} + +void BIOS_HuffUnComp() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("HuffUnComp: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + u8 treeSize = CPUReadByte(source++); + + u32 treeStart = source; + + source += ((treeSize+1)<<1)-1; // minus because we already skipped one byte + + int len = header >> 8; + + u32 mask = 0x80000000; + u32 data = CPUReadMemory(source); + source += 4; + + int pos = 0; + u8 rootNode = CPUReadByte(treeStart); + u8 currentNode = rootNode; + bool writeData = false; + int byteShift = 0; + int byteCount = 0; + u32 writeValue = 0; + + if((header & 0x0F) == 8) { + while(len > 0) { + // take left + if(pos == 0) + pos++; + else + pos += (((currentNode & 0x3F)+1)<<1); + + if(data & mask) { + // right + if(currentNode & 0x40) + writeData = true; + currentNode = CPUReadByte(treeStart+pos+1); + } else { + // left + if(currentNode & 0x80) + writeData = true; + currentNode = CPUReadByte(treeStart+pos); + } + + if(writeData) { + writeValue |= (currentNode << byteShift); + byteCount++; + byteShift += 8; + + pos = 0; + currentNode = rootNode; + writeData = false; + + if(byteCount == 4) { + byteCount = 0; + byteShift = 0; + CPUWriteMemory(dest, writeValue); + writeValue = 0; + dest += 4; + len -= 4; + } + } + mask >>= 1; + if(mask == 0) { + mask = 0x80000000; + data = CPUReadMemory(source); + source += 4; + } + } + } else { + int halfLen = 0; + int value = 0; + while(len > 0) { + // take left + if(pos == 0) + pos++; + else + pos += (((currentNode & 0x3F)+1)<<1); + + if((data & mask)) { + // right + if(currentNode & 0x40) + writeData = true; + currentNode = CPUReadByte(treeStart+pos+1); + } else { + // left + if(currentNode & 0x80) + writeData = true; + currentNode = CPUReadByte(treeStart+pos); + } + + if(writeData) { + if(halfLen == 0) + value |= currentNode; + else + value |= (currentNode<<4); + + halfLen += 4; + if(halfLen == 8) { + writeValue |= (value << byteShift); + byteCount++; + byteShift += 8; + + halfLen = 0; + value = 0; + + if(byteCount == 4) { + byteCount = 0; + byteShift = 0; + CPUWriteMemory(dest, writeValue); + dest += 4; + writeValue = 0; + len -= 4; + } + } + pos = 0; + currentNode = rootNode; + writeData = false; + } + mask >>= 1; + if(mask == 0) { + mask = 0x80000000; + data = CPUReadMemory(source); + source += 4; + } + } + } +} + +void BIOS_LZ77UnCompVram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("LZ77UnCompVram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int byteCount = 0; + int byteShift = 0; + u32 writeValue = 0; + + int len = header >> 8; + + while(len > 0) { + u8 d = CPUReadByte(source++); + + if(d) { + for(int i = 0; i < 8; i++) { + if(d & 0x80) { + u16 data = CPUReadByte(source++) << 8; + data |= CPUReadByte(source++); + int length = (data >> 12) + 3; + int offset = (data & 0x0FFF); + u32 windowOffset = dest + byteCount - offset - 1; + for(int i = 0; i < length; i++) { + writeValue |= (CPUReadByte(windowOffset++) << byteShift); + byteShift += 8; + byteCount++; + + if(byteCount == 2) { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if(len == 0) + return; + } + } else { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if(byteCount == 2) { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if(len == 0) + return; + } + d <<= 1; + } + } else { + for(int i = 0; i < 8; i++) { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if(byteCount == 2) { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteShift = 0; + byteCount = 0; + writeValue = 0; + } + len--; + if(len == 0) + return; + } + } + } +} + +void BIOS_LZ77UnCompWram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("LZ77UnCompWram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + while(len > 0) { + u8 d = CPUReadByte(source++); + + if(d) { + for(int i = 0; i < 8; i++) { + if(d & 0x80) { + u16 data = CPUReadByte(source++) << 8; + data |= CPUReadByte(source++); + int length = (data >> 12) + 3; + int offset = (data & 0x0FFF); + u32 windowOffset = dest - offset - 1; + for(int i = 0; i < length; i++) { + CPUWriteByte(dest++, CPUReadByte(windowOffset++)); + len--; + if(len == 0) + return; + } + } else { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if(len == 0) + return; + } + d <<= 1; + } + } else { + for(int i = 0; i < 8; i++) { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if(len == 0) + return; + } + } + } +} + +void BIOS_ObjAffineSet() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("ObjAffineSet: 0x%08x,0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + reg[3].I, + VCOUNT); + } +#endif + + u32 src = reg[0].I; + u32 dest = reg[1].I; + int num = reg[2].I; + int offset = reg[3].I; + + for(int i = 0; i < num; i++) { + s16 rx = CPUReadHalfWord(src); + src+=2; + s16 ry = CPUReadHalfWord(src); + src+=2; + u16 theta = CPUReadHalfWord(src)>>8; + src+=4; // keep structure alignment + + s32 a = (s32)sineTable[(theta+0x40)&255]; + s32 b = (s32)sineTable[theta]; + + s16 dx = ((s32)rx * a)>>14; + s16 dmx = ((s32)rx * b)>>14; + s16 dy = ((s32)ry * b)>>14; + s16 dmy = ((s32)ry * a)>>14; + + CPUWriteHalfWord(dest, dx); + dest += offset; + CPUWriteHalfWord(dest, -dmx); + dest += offset; + CPUWriteHalfWord(dest, dy); + dest += offset; + CPUWriteHalfWord(dest, dmy); + dest += offset; + } +} + +void BIOS_RegisterRamReset(u32 flags) +{ + // no need to trace here. this is only called directly from GBA.cpp + // to emulate bios initialization + + if(flags) { + if(flags & 0x01) { + // clear work RAM + memset(workRAM, 0, 0x40000); + } + if(flags & 0x02) { + // clear internal RAM + memset(internalRAM, 0, 0x7e00); // don't clear 0x7e00-0x7fff + } + if(flags & 0x04) { + // clear palette RAM + memset(paletteRAM, 0, 0x400); + } + if(flags & 0x08) { + // clear VRAM + memset(vram, 0, 0x18000); + } + if(flags & 0x10) { + // clean OAM + memset(oam, 0, 0x400); + } + + if(flags & 0x80) { + int i; + for(i = 0; i < 8; i++) + CPUUpdateRegister(0x200+i*2, 0); + + CPUUpdateRegister(0x202, 0xFFFF); + + for(i = 0; i < 8; i++) + CPUUpdateRegister(0x4+i*2, 0); + + for(i = 0; i < 16; i++) + CPUUpdateRegister(0x20+i*2, 0); + + for(i = 0; i < 24; i++) + CPUUpdateRegister(0xb0+i*2, 0); + + CPUUpdateRegister(0x130, 0); + CPUUpdateRegister(0x20, 0x100); + CPUUpdateRegister(0x30, 0x100); + CPUUpdateRegister(0x26, 0x100); + CPUUpdateRegister(0x36, 0x100); + } + + if(flags & 0x20) { + int i; + for(i = 0; i < 8; i++) + CPUUpdateRegister(0x110+i*2, 0); + CPUUpdateRegister(0x134, 0x8000); + for(i = 0; i < 7; i++) + CPUUpdateRegister(0x140+i*2, 0); + } + + if(flags & 0x40) { + int i; + CPUWriteByte(0x4000084, 0); + CPUWriteByte(0x4000084, 0x80); + CPUWriteMemory(0x4000080, 0x880e0000); + CPUUpdateRegister(0x88, CPUReadHalfWord(0x4000088)&0x3ff); + CPUWriteByte(0x4000070, 0x70); + for(i = 0; i < 8; i++) + CPUUpdateRegister(0x90+i*2, 0); + CPUWriteByte(0x4000070, 0); + for(i = 0; i < 8; i++) + CPUUpdateRegister(0x90+i*2, 0); + CPUWriteByte(0x4000084, 0); + } + } +} + +void BIOS_RegisterRamReset() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("RegisterRamReset: 0x%08x (VCOUNT=%d)\n", + reg[0].I, + VCOUNT); + } +#endif + + BIOS_RegisterRamReset(reg[0].I); +} + +void BIOS_RLUnCompVram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("RLUnCompVram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + int byteCount = 0; + int byteShift = 0; + u32 writeValue = 0; + + while(len > 0) { + u8 d = CPUReadByte(source++); + int l = d & 0x7F; + if(d & 0x80) { + u8 data = CPUReadByte(source++); + l += 3; + for(int i = 0;i < l; i++) { + writeValue |= (data << byteShift); + byteShift += 8; + byteCount++; + + if(byteCount == 2) { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if(len == 0) + return; + } + } else { + l++; + for(int i = 0; i < l; i++) { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if(byteCount == 2) { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if(len == 0) + return; + } + } + } +} + +void BIOS_RLUnCompWram() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("RLUnCompWram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if(((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + while(len > 0) { + u8 d = CPUReadByte(source++); + int l = d & 0x7F; + if(d & 0x80) { + u8 data = CPUReadByte(source++); + l += 3; + for(int i = 0;i < l; i++) { + CPUWriteByte(dest++, data); + len--; + if(len == 0) + return; + } + } else { + l++; + for(int i = 0; i < l; i++) { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if(len == 0) + return; + } + } + } +} + +void BIOS_SoftReset() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("SoftReset: (VCOUNT=%d)\n", VCOUNT); + } +#endif + + armState = true; + armMode = 0x1F; + armIrqEnable = false; + C_FLAG = V_FLAG = N_FLAG = Z_FLAG = false; + reg[13].I = 0x03007F00; + reg[14].I = 0x00000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R14_IRQ].I = 0x00000000; + reg[SPSR_IRQ].I = 0x00000000; + reg[R13_SVC].I = 0x03007FE0; + reg[R14_SVC].I = 0x00000000; + reg[SPSR_SVC].I = 0x00000000; + u8 b = internalRAM[0x7ffa]; + + memset(&internalRAM[0x7e00], 0, 0x200); + + if(b) { + armNextPC = 0x02000000; + reg[15].I = 0x02000004; + } else { + armNextPC = 0x08000000; + reg[15].I = 0x08000004; + } +} + +void BIOS_Sqrt() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Sqrt: %08x (VCOUNT=%2d)\n", + reg[0].I, + VCOUNT); + } +#endif + reg[0].I = (u32)sqrt((double)reg[0].I); +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("Sqrt: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_MidiKey2Freq() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("MidiKey2Freq: WaveData=%08x mk=%08x fp=%08x\n", + reg[0].I, + reg[1].I, + reg[2].I); + } +#endif + int freq = CPUReadMemory(reg[0].I+4); + double tmp; + tmp = ((double)(180 - reg[1].I)) - ((double)reg[2].I / 256.f); + tmp = pow((double)2.f, tmp / 12.f); + reg[0].I = (int)((double)freq / tmp); + +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("MidiKey2Freq: return %08x\n", + reg[0].I); + } +#endif +} + +void BIOS_SndDriverJmpTableCopy() +{ +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_SWI) { + log("SndDriverJmpTableCopy: dest=%08x\n", + reg[0].I); + } +#endif + for(int i = 0; i < 0x24; i++) { + CPUWriteMemory(reg[0].I, 0x9c); + reg[0].I += 4; + } +} diff --git a/src/bios.h b/src/bios.h new file mode 100644 index 00000000..58b3417f --- /dev/null +++ b/src/bios.h @@ -0,0 +1,46 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_BIOS_H +#define VBA_BIOS_H + +extern void BIOS_ArcTan(); +extern void BIOS_ArcTan2(); +extern void BIOS_BitUnPack(); +extern void BIOS_BgAffineSet(); +extern void BIOS_CpuSet(); +extern void BIOS_CpuFastSet(); +extern void BIOS_Diff8bitUnFilterWram(); +extern void BIOS_Diff8bitUnFilterVram(); +extern void BIOS_Diff16bitUnFilter(); +extern void BIOS_Div(); +extern void BIOS_DivARM(); +extern void BIOS_HuffUnComp(); +extern void BIOS_LZ77UnCompVram(); +extern void BIOS_LZ77UnCompWram(); +extern void BIOS_ObjAffineSet(); +extern void BIOS_RegisterRamReset(); +extern void BIOS_RegisterRamReset(u32); +extern void BIOS_RLUnCompVram(); +extern void BIOS_RLUnCompWram(); +extern void BIOS_SoftReset(); +extern void BIOS_Sqrt(); +extern void BIOS_MidiKey2Freq(); +extern void BIOS_SndDriverJmpTableCopy(); +#endif // VBA_BIOS_H diff --git a/src/elf.cpp b/src/elf.cpp new file mode 100644 index 00000000..ce6c314e --- /dev/null +++ b/src/elf.cpp @@ -0,0 +1,2999 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include + +#include "GBA.h" +#include "Port.h" +#include "elf.h" +#include "NLS.h" + +#define elfReadMemory(addr) \ + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define DW_TAG_array_type 0x01 +#define DW_TAG_enumeration_type 0x04 +#define DW_TAG_formal_parameter 0x05 +#define DW_TAG_label 0x0a +#define DW_TAG_lexical_block 0x0b +#define DW_TAG_member 0x0d +#define DW_TAG_pointer_type 0x0f +#define DW_TAG_reference_type 0x10 +#define DW_TAG_compile_unit 0x11 +#define DW_TAG_structure_type 0x13 +#define DW_TAG_subroutine_type 0x15 +#define DW_TAG_typedef 0x16 +#define DW_TAG_union_type 0x17 +#define DW_TAG_unspecified_parameters 0x18 +#define DW_TAG_inheritance 0x1c +#define DW_TAG_inlined_subroutine 0x1d +#define DW_TAG_subrange_type 0x21 +#define DW_TAG_base_type 0x24 +#define DW_TAG_const_type 0x26 +#define DW_TAG_enumerator 0x28 +#define DW_TAG_subprogram 0x2e +#define DW_TAG_variable 0x34 +#define DW_TAG_volatile_type 0x35 + +#define DW_AT_sibling 0x01 +#define DW_AT_location 0x02 +#define DW_AT_name 0x03 +#define DW_AT_byte_size 0x0b +#define DW_AT_bit_offset 0x0c +#define DW_AT_bit_size 0x0d +#define DW_AT_stmt_list 0x10 +#define DW_AT_low_pc 0x11 +#define DW_AT_high_pc 0x12 +#define DW_AT_language 0x13 +#define DW_AT_compdir 0x1b +#define DW_AT_const_value 0x1c +#define DW_AT_containing_type 0x1d +#define DW_AT_inline 0x20 +#define DW_AT_producer 0x25 +#define DW_AT_prototyped 0x27 +#define DW_AT_upper_bound 0x2f +#define DW_AT_abstract_origin 0x31 +#define DW_AT_accessibility 0x32 +#define DW_AT_artificial 0x34 +#define DW_AT_data_member_location 0x38 +#define DW_AT_decl_file 0x3a +#define DW_AT_decl_line 0x3b +#define DW_AT_declaration 0x3c +#define DW_AT_encoding 0x3e +#define DW_AT_external 0x3f +#define DW_AT_frame_base 0x40 +#define DW_AT_macro_info 0x43 +#define DW_AT_specification 0x47 +#define DW_AT_type 0x49 +#define DW_AT_virtuality 0x4c +#define DW_AT_vtable_elem_location 0x4d +// DWARF 2.1/3.0 extensions +#define DW_AT_entry_pc 0x52 +#define DW_AT_ranges 0x55 +// ARM Compiler extensions +#define DW_AT_proc_body 0x2000 +#define DW_AT_save_offset 0x2001 +#define DW_AT_user_2002 0x2002 +// MIPS extensions +#define DW_AT_MIPS_linkage_name 0x2007 + +#define DW_FORM_addr 0x01 +#define DW_FORM_data2 0x05 +#define DW_FORM_data4 0x06 +#define DW_FORM_string 0x08 +#define DW_FORM_block 0x09 +#define DW_FORM_block1 0x0a +#define DW_FORM_data1 0x0b +#define DW_FORM_flag 0x0c +#define DW_FORM_sdata 0x0d +#define DW_FORM_strp 0x0e +#define DW_FORM_udata 0x0f +#define DW_FORM_ref_addr 0x10 +#define DW_FORM_ref4 0x13 +#define DW_FORM_ref_udata 0x15 +#define DW_FORM_indirect 0x16 + +#define DW_OP_addr 0x03 +#define DW_OP_plus_uconst 0x23 +#define DW_OP_reg0 0x50 +#define DW_OP_reg1 0x51 +#define DW_OP_reg2 0x52 +#define DW_OP_reg3 0x53 +#define DW_OP_reg4 0x54 +#define DW_OP_reg5 0x55 +#define DW_OP_reg6 0x56 +#define DW_OP_reg7 0x57 +#define DW_OP_reg8 0x58 +#define DW_OP_reg9 0x59 +#define DW_OP_reg10 0x5a +#define DW_OP_reg11 0x5b +#define DW_OP_reg12 0x5c +#define DW_OP_reg13 0x5d +#define DW_OP_reg14 0x5e +#define DW_OP_reg15 0x5f +#define DW_OP_fbreg 0x91 + +#define DW_LNS_extended_op 0x00 +#define DW_LNS_copy 0x01 +#define DW_LNS_advance_pc 0x02 +#define DW_LNS_advance_line 0x03 +#define DW_LNS_set_file 0x04 +#define DW_LNS_set_column 0x05 +#define DW_LNS_negate_stmt 0x06 +#define DW_LNS_set_basic_block 0x07 +#define DW_LNS_const_add_pc 0x08 +#define DW_LNS_fixed_advance_pc 0x09 + +#define DW_LNE_end_sequence 0x01 +#define DW_LNE_set_address 0x02 +#define DW_LNE_define_file 0x03 + +#define DW_CFA_advance_loc 0x01 +#define DW_CFA_offset 0x02 +#define DW_CFA_restore 0x03 +#define DW_CFA_set_loc 0x01 +#define DW_CFA_advance_loc1 0x02 +#define DW_CFA_advance_loc2 0x03 +#define DW_CFA_advance_loc4 0x04 +#define DW_CFA_offset_extended 0x05 +#define DW_CFA_restore_extended 0x06 +#define DW_CFA_undefined 0x07 +#define DW_CFA_same_value 0x08 +#define DW_CFA_register 0x09 +#define DW_CFA_remember_state 0x0a +#define DW_CFA_restore_state 0x0b +#define DW_CFA_def_cfa 0x0c +#define DW_CFA_def_cfa_register 0x0d +#define DW_CFA_def_cfa_offset 0x0e +#define DW_CFA_nop 0x00 + +#define CASE_TYPE_TAG \ + case DW_TAG_const_type:\ + case DW_TAG_volatile_type:\ + case DW_TAG_pointer_type:\ + case DW_TAG_base_type:\ + case DW_TAG_array_type:\ + case DW_TAG_structure_type:\ + case DW_TAG_union_type:\ + case DW_TAG_typedef:\ + case DW_TAG_subroutine_type:\ + case DW_TAG_enumeration_type:\ + case DW_TAG_enumerator:\ + case DW_TAG_reference_type + +struct ELFcie { + ELFcie *next; + u32 offset; + u8 *augmentation; + u32 codeAlign; + s32 dataAlign; + int returnAddress; + u8 *data; + u32 dataLen; +}; + +struct ELFfde { + ELFcie *cie; + u32 address; + u32 end; + u8 *data; + u32 dataLen; +}; + +enum ELFRegMode { + REG_NOT_SET, + REG_OFFSET, + REG_REGISTER +}; + + +struct ELFFrameStateRegister { + ELFRegMode mode; + int reg; + s32 offset; +}; + +struct ELFFrameStateRegisters { + ELFFrameStateRegister regs[16]; + ELFFrameStateRegisters *previous; +}; + +enum ELFCfaMode { + CFA_NOT_SET, + CFA_REG_OFFSET +}; + +struct ELFFrameState { + ELFFrameStateRegisters registers; + + ELFCfaMode cfaMode; + int cfaRegister; + s32 cfaOffset; + + u32 pc; + + int dataAlign; + int codeAlign; + int returnAddress; +}; + +extern bool cpuIsMultiBoot; + +Symbol *elfSymbols = NULL; +char *elfSymbolsStrTab = NULL; +int elfSymbolsCount = 0; + +ELFSectionHeader **elfSectionHeaders = NULL; +char *elfSectionHeadersStringTable = NULL; +int elfSectionHeadersCount = 0; +u8 *elfFileData = NULL; + +CompileUnit *elfCompileUnits = NULL; +DebugInfo *elfDebugInfo = NULL; +char *elfDebugStrings = NULL; + +ELFcie *elfCies = NULL; +ELFfde **elfFdes = NULL; +int elfFdeCount = 0; + +CompileUnit *elfCurrentUnit = NULL; + +u32 elfRead4Bytes(u8 *); +u16 elfRead2Bytes(u8 *); + +CompileUnit *elfGetCompileUnit(u32 addr) +{ + if(elfCompileUnits) { + CompileUnit *unit = elfCompileUnits; + while(unit) { + if(unit->lowPC) { + if(addr >= unit->lowPC && addr < unit->highPC) + return unit; + } else { + ARanges *r = unit->ranges; + if(r) { + int count = r->count; + for(int j = 0; j < count; j++) { + if(addr >= r->ranges[j].lowPC && addr < r->ranges[j].highPC) + return unit; + } + } + } + unit = unit->next; + } + } + return NULL; +} + +char *elfGetAddressSymbol(u32 addr) +{ + static char buffer[256]; + + CompileUnit *unit = elfGetCompileUnit(addr); + // found unit, need to find function + if(unit) { + Function *func = unit->functions; + while(func) { + if(addr >= func->lowPC && addr < func->highPC) { + int offset = addr - func->lowPC; + char *name = func->name; + if(!name) + name = ""; + if(offset) + sprintf(buffer, "%s+%d", name, offset); + else + strcpy(buffer, name); + return buffer; + } + func = func->next; + } + } + + if(elfSymbolsCount) { + for(int i = 0; i < elfSymbolsCount; i++) { + Symbol *s = &elfSymbols[i]; + if((addr >= s->value) && addr < (s->value+s->size)) { + int offset = addr-s->value; + char *name = s->name; + if(name == NULL) + name = ""; + if(offset) + sprintf(buffer, "%s+%d", name, addr-s->value); + else + strcpy(buffer, name); + return buffer; + } else if(addr == s->value) { + if(s->name) + strcpy(buffer, s->name); + else + strcpy(buffer, ""); + return buffer; + } + } + } + + return ""; +} + +bool elfFindLineInModule(u32 *addr, char *name, int line) +{ + CompileUnit *unit = elfCompileUnits; + + while(unit) { + if(unit->lineInfoTable) { + int i; + int count = unit->lineInfoTable->fileCount; + char *found = NULL; + for(i = 0; i < count; i++) { + if(strcmp(name, unit->lineInfoTable->files[i]) == 0) { + found = unit->lineInfoTable->files[i]; + break; + } + } + // found a matching filename... try to find line now + if(found) { + LineInfoItem *table = unit->lineInfoTable->lines; + count = unit->lineInfoTable->number; + for(i = 0; i < count; i++) { + if(table[i].file == found && table[i].line == line) { + *addr = table[i].address; + return true; + } + } + // we can only find a single match + return false; + } + } + unit = unit->next; + } + return false; +} + +int elfFindLine(CompileUnit *unit, Function * /* func */, u32 addr, char **f) +{ + int currentLine = -1; + if(unit->hasLineInfo) { + int count = unit->lineInfoTable->number; + LineInfoItem *table = unit->lineInfoTable->lines; + int i; + for(i = 0; i < count; i++) { + if(addr <= table[i].address) + break; + } + if(i == count) + i--; + *f = table[i].file; + currentLine = table[i].line; + } + return currentLine; +} + +bool elfFindLineInUnit(u32 *addr, CompileUnit *unit, int line) +{ + if(unit->hasLineInfo) { + int count = unit->lineInfoTable->number; + LineInfoItem *table = unit->lineInfoTable->lines; + int i; + for(i = 0; i < count; i++) { + if(line == table[i].line) { + *addr = table[i].address; + return true; + } + } + } + return false; +} + +bool elfGetCurrentFunction(u32 addr, Function **f, CompileUnit **u) +{ + CompileUnit *unit = elfGetCompileUnit(addr); + // found unit, need to find function + if(unit) { + Function *func = unit->functions; + while(func) { + if(addr >= func->lowPC && addr < func->highPC) { + *f = func; + *u = unit; + return true; + } + func = func->next; + } + } + return false; +} + +bool elfGetObject(char *name, Function *f, CompileUnit *u, Object **o) +{ + if(f && u) { + Object *v = f->variables; + + while(v) { + if(strcmp(name, v->name) == 0) { + *o = v; + return true; + } + v = v->next; + } + v = f->parameters; + while(v) { + if(strcmp(name, v->name) == 0) { + *o = v; + return true; + } + v = v->next; + } + v = u->variables; + while(v) { + if(strcmp(name, v->name) == 0) { + *o = v; + return true; + } + v = v->next; + } + } + + CompileUnit *c = elfCompileUnits; + + while(c) { + if(c != u) { + Object *v = c->variables; + while(v) { + if(strcmp(name, v->name) == 0) { + *o = v; + return true; + } + v = v->next; + } + } + c = c->next; + } + + return false; +} + +char *elfGetSymbol(int i, u32 *value, u32 *size, int *type) +{ + if(i < elfSymbolsCount) { + Symbol *s = &elfSymbols[i]; + *value = s->value; + *size = s->size; + *type = s->type; + return s->name; + } + return NULL; +} + +bool elfGetSymbolAddress(char *sym, u32 *addr, u32 *size, int *type) +{ + if(elfSymbolsCount) { + for(int i = 0; i < elfSymbolsCount; i++) { + Symbol *s = &elfSymbols[i]; + if(strcmp(sym, s->name) == 0) { + *addr = s->value; + *size = s->size; + *type = s->type; + return true; + } + } + } + return false; +} + +ELFfde *elfGetFde(u32 address) +{ + if(elfFdes) { + int i; + for(i = 0; i < elfFdeCount; i++) { + if(address >= elfFdes[i]->address && + address < elfFdes[i]->end) { + return elfFdes[i]; + } + } + } + + return NULL; +} + +void elfExecuteCFAInstructions(ELFFrameState *state, u8 *data, u32 len, + u32 pc) +{ + u8 *end = data + len; + int bytes; + int reg; + ELFFrameStateRegisters *fs; + + while(data < end && state->pc < pc) { + u8 op = *data++; + + switch(op >> 6) { + case DW_CFA_advance_loc: + state->pc += (op & 0x3f) * state->codeAlign; + break; + case DW_CFA_offset: + reg = op & 0x3f; + state->registers.regs[reg].mode = REG_OFFSET; + state->registers.regs[reg].offset = state->dataAlign * + (s32)elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_restore: + // we don't care much about the other possible settings, + // so just setting to unset is enough for now + state->registers.regs[op & 0x3f].mode = REG_NOT_SET; + break; + case 0: + switch(op & 0x3f) { + case DW_CFA_nop: + break; + case DW_CFA_advance_loc1: + state->pc += state->codeAlign * (*data++); + break; + case DW_CFA_advance_loc2: + state->pc += state->codeAlign * elfRead2Bytes(data); + data += 2; + break; + case DW_CFA_advance_loc4: + state->pc += state->codeAlign * elfRead4Bytes(data); + data += 4; + break; + case DW_CFA_offset_extended: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_OFFSET; + state->registers.regs[reg].offset = state->dataAlign * + (s32)elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_restore_extended: + case DW_CFA_undefined: + case DW_CFA_same_value: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_NOT_SET; + break; + case DW_CFA_register: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_REGISTER; + state->registers.regs[reg].reg = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_remember_state: + fs = (ELFFrameStateRegisters *)calloc(1, + sizeof(ELFFrameStateRegisters)); + memcpy(fs, &state->registers, sizeof(ELFFrameStateRegisters)); + state->registers.previous = fs; + break; + case DW_CFA_restore_state: + if(state->registers.previous == NULL) { + printf("Error: previous frame state is NULL.\n"); + return; + } + fs = state->registers.previous; + memcpy(&state->registers, fs, sizeof(ELFFrameStateRegisters)); + free(fs); + break; + case DW_CFA_def_cfa: + state->cfaRegister = elfReadLEB128(data, &bytes); + data += bytes; + state->cfaOffset = (s32)elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + case DW_CFA_def_cfa_register: + state->cfaRegister = elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + case DW_CFA_def_cfa_offset: + state->cfaOffset = (s32)elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + default: + printf("Unknown CFA opcode %08x\n", op); + return; + } + break; + default: + printf("Unknown CFA opcode %08x\n", op); + return; + } + } +} + +ELFFrameState *elfGetFrameState(ELFfde *fde, u32 address) +{ + ELFFrameState *state = (ELFFrameState *)calloc(1, sizeof(ELFFrameState)); + state->pc = fde->address; + state->dataAlign = fde->cie->dataAlign; + state->codeAlign = fde->cie->codeAlign; + state->returnAddress = fde->cie->returnAddress; + + elfExecuteCFAInstructions(state, + fde->cie->data, + fde->cie->dataLen, + 0xffffffff); + elfExecuteCFAInstructions(state, + fde->data, + fde->dataLen, + address); + + return state; +} + +void elfPrintCallChain(u32 address) +{ + int count = 1; + + reg_pair regs[15]; + reg_pair newRegs[15]; + + memcpy(®s[0], ®[0], sizeof(reg_pair) * 15); + + while(count < 20) { + char *addr = elfGetAddressSymbol(address); + if(*addr == 0) + addr = "???"; + + printf("%08x %s\n", address, addr); + + ELFfde *fde = elfGetFde(address); + + if(fde == NULL) { + break; + } + + ELFFrameState *state = elfGetFrameState(fde, address); + + if(!state) { + break; + } + + if(state->cfaMode == CFA_REG_OFFSET) { + memcpy(&newRegs[0], ®s[0], sizeof(reg_pair) * 15); + u32 addr = 0; + for(int i = 0; i < 15; i++) { + ELFFrameStateRegister *r = &state->registers. + regs[i]; + + switch(r->mode) { + case REG_NOT_SET: + newRegs[i].I = regs[i].I; + break; + case REG_OFFSET: + newRegs[i].I = elfReadMemory(regs[state->cfaRegister].I + + state->cfaOffset + + r->offset); + break; + case REG_REGISTER: + newRegs[i].I = regs[r->reg].I; + break; + default: + printf("Unknown register mode: %d\n", r->mode); + break; + } + } + memcpy(regs, newRegs, sizeof(reg_pair)*15); + addr = newRegs[14].I; + addr &= 0xfffffffe; + address = addr; + count++; + } else { + printf("CFA not set\n"); + break; + } + if(state->registers.previous) { + ELFFrameStateRegisters *prev = state->registers.previous; + + while(prev) { + ELFFrameStateRegisters *p = prev->previous; + free(prev); + prev = p; + } + } + free(state); + } +} + +u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type, u32 base) +{ + u32 framebase = 0; + if(f && f->frameBase) { + ELFBlock *b = f->frameBase; + switch(*b->data) { + case DW_OP_reg0: + case DW_OP_reg1: + case DW_OP_reg2: + case DW_OP_reg3: + case DW_OP_reg4: + case DW_OP_reg5: + case DW_OP_reg6: + case DW_OP_reg7: + case DW_OP_reg8: + case DW_OP_reg9: + case DW_OP_reg10: + case DW_OP_reg11: + case DW_OP_reg12: + case DW_OP_reg13: + case DW_OP_reg14: + case DW_OP_reg15: + framebase = reg[*b->data-0x50].I; + break; + default: + fprintf(stderr, "Unknown frameBase %02x\n", *b->data); + break; + } + } + + ELFBlock *loc = o; + u32 location = 0; + int bytes = 0; + if(loc) { + switch(*loc->data) { + case DW_OP_addr: + location = elfRead4Bytes(loc->data+1); + *type = LOCATION_memory; + break; + case DW_OP_plus_uconst: + location = base + elfReadLEB128(loc->data+1, &bytes); + *type = LOCATION_memory; + break; + case DW_OP_reg0: + case DW_OP_reg1: + case DW_OP_reg2: + case DW_OP_reg3: + case DW_OP_reg4: + case DW_OP_reg5: + case DW_OP_reg6: + case DW_OP_reg7: + case DW_OP_reg8: + case DW_OP_reg9: + case DW_OP_reg10: + case DW_OP_reg11: + case DW_OP_reg12: + case DW_OP_reg13: + case DW_OP_reg14: + case DW_OP_reg15: + location = *loc->data - 0x50; + *type = LOCATION_register; + break; + case DW_OP_fbreg: + { + int bytes; + s32 off = elfReadSignedLEB128(loc->data+1, &bytes); + location = framebase + off; + *type = LOCATION_memory; + } + break; + default: + fprintf(stderr, "Unknown location %02x\n", *loc->data); + break; + } + } + return location; +} + +u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type) +{ + return elfDecodeLocation(f, o, type, 0); +} + +// reading function + +u32 elfRead4Bytes(u8 *data) +{ + u32 value = *data++; + value |= (*data++ << 8); + value |= (*data++ << 16); + value |= (*data << 24); + return value; +} + +u16 elfRead2Bytes(u8 *data) +{ + u16 value = *data++; + value |= (*data << 8); + return value; +} + +char *elfReadString(u8 *data, int *bytesRead) +{ + if(*data == 0) { + *bytesRead = 1; + return NULL; + } + *bytesRead = strlen((char *)data) + 1; + return (char *)data; +} + +s32 elfReadSignedLEB128(u8 *data, int *bytesRead) +{ + s32 result = 0; + int shift = 0; + int count = 0; + + u8 byte; + do { + byte = *data++; + count++; + result |= (byte & 0x7f) << shift; + shift += 7; + } while(byte & 0x80); + if((shift < 32) && (byte & 0x40)) + result |= -(1 << shift); + *bytesRead = count; + return result; +} + +u32 elfReadLEB128(u8 *data, int *bytesRead) +{ + u32 result = 0; + int shift = 0; + int count = 0; + u8 byte; + do { + byte = *data++; + count++; + result |= (byte & 0x7f) << shift; + shift += 7; + } while(byte & 0x80); + *bytesRead = count; + return result; +} + +u8 *elfReadSection(u8 *data, ELFSectionHeader *sh) +{ + return data + READ32LE(&sh->offset); +} + +ELFSectionHeader *elfGetSectionByName(char *name) +{ + for(int i = 0; i < elfSectionHeadersCount; i++) { + if(strcmp(name, + &elfSectionHeadersStringTable[READ32LE(&elfSectionHeaders[i]-> + name)]) == 0) { + return elfSectionHeaders[i]; + } + } + return NULL; +} + +ELFSectionHeader *elfGetSectionByNumber(int number) +{ + if(number < elfSectionHeadersCount) { + return elfSectionHeaders[number]; + } + return NULL; +} + +CompileUnit *elfGetCompileUnitForData(u8 *data) +{ + u8 *end = elfCurrentUnit->top + 4 + elfCurrentUnit->length; + + if(data >= elfCurrentUnit->top && data < end) + return elfCurrentUnit; + + CompileUnit *unit = elfCompileUnits; + + while(unit) { + end = unit->top + 4 + unit->length; + + if(data >= unit->top && data < end) + return unit; + + unit = unit->next; + } + + printf("Error: cannot find reference to compile unit at offset %08x\n", + (int)(data - elfDebugInfo->infodata)); + exit(-1); +} + +u8 *elfReadAttribute(u8 *data, ELFAttr *attr) +{ + int bytes; + int form = attr->form; + start: + switch(form) { + case DW_FORM_addr: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_data2: + attr->value = elfRead2Bytes(data); + data += 2; + break; + case DW_FORM_data4: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_string: + attr->string = (char *)data; + data += strlen(attr->string)+1; + break; + case DW_FORM_strp: + attr->string = elfDebugStrings + elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_block: + attr->block = (ELFBlock *)malloc(sizeof(ELFBlock)); + attr->block->length = elfReadLEB128(data, &bytes); + data += bytes; + attr->block->data = data; + data += attr->block->length; + break; + case DW_FORM_block1: + attr->block = (ELFBlock *)malloc(sizeof(ELFBlock)); + attr->block->length = *data++; + attr->block->data = data; + data += attr->block->length; + break; + case DW_FORM_data1: + attr->value = *data++; + break; + case DW_FORM_flag: + attr->flag = (*data++) ? true : false; + break; + case DW_FORM_sdata: + attr->value = elfReadSignedLEB128(data, &bytes); + data += bytes; + break; + case DW_FORM_udata: + attr->value = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_FORM_ref_addr: + attr->value = (elfDebugInfo->infodata + elfRead4Bytes(data)) - + elfGetCompileUnitForData(data)->top; + data += 4; + break; + case DW_FORM_ref4: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_ref_udata: + attr->value = (elfDebugInfo->infodata + + (elfGetCompileUnitForData(data)->top - + elfDebugInfo->infodata) + + elfReadLEB128(data, &bytes)) - + elfCurrentUnit->top; + data += bytes; + break; + case DW_FORM_indirect: + form = elfReadLEB128(data, &bytes); + data += bytes; + goto start; + default: + fprintf(stderr, "Unsupported FORM %02x\n", form); + exit(-1); + } + return data; +} + +ELFAbbrev *elfGetAbbrev(ELFAbbrev **table, u32 number) +{ + int hash = number % 121; + + ELFAbbrev *abbrev = table[hash]; + + while(abbrev) { + if(abbrev->number == number) + return abbrev; + abbrev = abbrev->next; + } + return NULL; +} + +ELFAbbrev **elfReadAbbrevs(u8 *data, u32 offset) +{ + data += offset; + ELFAbbrev **abbrevs = (ELFAbbrev **)calloc(sizeof(ELFAbbrev *)*121,1); + int bytes = 0; + u32 number = elfReadLEB128(data, &bytes); + data += bytes; + while(number) { + ELFAbbrev *abbrev = (ELFAbbrev *)calloc(sizeof(ELFAbbrev),1); + + // read tag information + abbrev->number = number; + abbrev->tag = elfReadLEB128(data, &bytes); + data += bytes; + abbrev->hasChildren = *data++ ? true: false; + + // read attributes + int name = elfReadLEB128(data, &bytes); + data += bytes; + int form = elfReadLEB128(data, &bytes); + data += bytes; + + while(name) { + if((abbrev->numAttrs % 4) == 0) { + abbrev->attrs = (ELFAttr *)realloc(abbrev->attrs, + (abbrev->numAttrs + 4) * + sizeof(ELFAttr)); + } + abbrev->attrs[abbrev->numAttrs].name = name; + abbrev->attrs[abbrev->numAttrs++].form = form; + + name = elfReadLEB128(data, &bytes); + data += bytes; + form = elfReadLEB128(data, &bytes); + data += bytes; + } + + int hash = number % 121; + abbrev->next = abbrevs[hash]; + abbrevs[hash] = abbrev; + + number = elfReadLEB128(data, &bytes); + data += bytes; + + if(elfGetAbbrev(abbrevs, number) != NULL) + break; + } + + return abbrevs; +} + +void elfParseCFA(u8 *top) +{ + ELFSectionHeader *h = elfGetSectionByName(".debug_frame"); + + if(h == NULL) { + return; + } + + u8 *data = elfReadSection(top, h); + + u8 *topOffset = data; + + u8 *end = data + READ32LE(&h->size); + + ELFcie *cies = NULL; + + while(data < end) { + u32 offset = data - topOffset; + u32 len = elfRead4Bytes(data); + data += 4; + + u8 *dataEnd = data + len; + + u32 id = elfRead4Bytes(data); + data += 4; + + if(id == 0xffffffff) { + // skip version + *data++; + + ELFcie *cie = (ELFcie *)calloc(1, sizeof(ELFcie)); + + cie->next = cies; + cies = cie; + + cie->offset = offset; + + cie->augmentation = data; + while(*data) + data++; + data++; + + if(*cie->augmentation) { + fprintf(stderr, "Error: augmentation not supported\n"); + exit(-1); + } + + int bytes; + cie->codeAlign = elfReadLEB128(data, &bytes); + data += bytes; + + cie->dataAlign = elfReadSignedLEB128(data, &bytes); + data += bytes; + + cie->returnAddress = *data++; + + cie->data = data; + cie->dataLen = dataEnd - data; + } else { + ELFfde *fde = (ELFfde *)calloc(1, sizeof(ELFfde)); + + ELFcie *cie = cies; + + while(cie != NULL) { + if(cie->offset == id) + break; + cie = cie->next; + } + + if(!cie) { + fprintf(stderr, "Cannot find CIE %08x\n", id); + exit(-1); + } + + fde->cie = cie; + + fde->address = elfRead4Bytes(data); + data += 4; + + fde->end = fde->address + elfRead4Bytes(data); + data += 4; + + fde->data = data; + fde->dataLen = dataEnd - data; + + if((elfFdeCount %10) == 0) { + elfFdes = (ELFfde **)realloc(elfFdes, (elfFdeCount+10) * + sizeof(ELFfde *)); + } + elfFdes[elfFdeCount++] = fde; + } + data = dataEnd; + } + + elfCies = cies; +} + +void elfAddLine(LineInfo *l, u32 a, int file, int line, int *max) +{ + if(l->number == *max) { + *max += 1000; + l->lines = (LineInfoItem *)realloc(l->lines, *max*sizeof(LineInfoItem)); + } + LineInfoItem *li = &l->lines[l->number]; + li->file = l->files[file-1]; + li->address = a; + li->line = line; + l->number++; +} + +void elfParseLineInfo(CompileUnit *unit, u8 *top) +{ + ELFSectionHeader *h = elfGetSectionByName(".debug_line"); + if(h == NULL) { + fprintf(stderr, "No line information found\n"); + return; + } + LineInfo *l = unit->lineInfoTable = (LineInfo *)calloc(1, sizeof(LineInfo)); + l->number = 0; + int max = 1000; + l->lines = (LineInfoItem *)malloc(1000*sizeof(LineInfoItem)); + + u8 *data = elfReadSection(top, h); + data += unit->lineInfo; + u32 totalLen = elfRead4Bytes(data); + data += 4; + u8 *end = data + totalLen; + // u16 version = elfRead2Bytes(data); + data += 2; + // u32 offset = elfRead4Bytes(data); + data += 4; + int minInstrSize = *data++; + int defaultIsStmt = *data++; + int lineBase = (s8)*data++; + int lineRange = *data++; + int opcodeBase = *data++; + u8 *stdOpLen = (u8 *)malloc(opcodeBase * sizeof(u8)); + stdOpLen[0] = 1; + int i; + for(i = 1; i < opcodeBase; i++) + stdOpLen[i] = *data++; + + free(stdOpLen);// todo + int bytes = 0; + + char *s; + while((s = elfReadString(data, &bytes)) != NULL) { + data += bytes; + // fprintf(stderr, "Directory is %s\n", s); + } + data += bytes; + int count = 4; + int index = 0; + l->files = (char **)malloc(sizeof(char *)*count); + + while((s = elfReadString(data, &bytes)) != NULL) { + l->files[index++] = s; + + data += bytes; + // directory + elfReadLEB128(data, &bytes); + data += bytes; + // time + elfReadLEB128(data, &bytes); + data += bytes; + // size + elfReadLEB128(data, &bytes); + data += bytes; + // fprintf(stderr, "File is %s\n", s); + if(index == count) { + count += 4; + l->files = (char **)realloc(l->files, sizeof(char *)*count); + } + } + l->fileCount = index; + data += bytes; + + while(data < end) { + u32 address = 0; + int file = 1; + int line = 1; + int col = 0; + int isStmt = defaultIsStmt; + int basicBlock = 0; + int endSeq = 0; + + while(!endSeq) { + int op = *data++; + switch(op) { + case DW_LNS_extended_op: + { + data++; + op = *data++; + switch(op) { + case DW_LNE_end_sequence: + endSeq = 1; + break; + case DW_LNE_set_address: + address = elfRead4Bytes(data); + data += 4; + break; + default: + fprintf(stderr, "Unknown extended LINE opcode %02x\n", op); + exit(-1); + } + } + break; + case DW_LNS_copy: + // fprintf(stderr, "Address %08x line %d (%d)\n", address, line, file); + elfAddLine(l, address, file, line, &max); + basicBlock = 0; + break; + case DW_LNS_advance_pc: + address += minInstrSize * elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_advance_line: + line += elfReadSignedLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_set_file: + file = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_set_column: + col = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_negate_stmt: + isStmt = !isStmt; + break; + case DW_LNS_set_basic_block: + basicBlock = 1; + break; + case DW_LNS_const_add_pc: + address += (minInstrSize *((255 - opcodeBase)/lineRange)); + break; + case DW_LNS_fixed_advance_pc: + address += elfRead2Bytes(data); + data += 2; + break; + default: + op = op - opcodeBase; + address += (op / lineRange) * minInstrSize; + line += lineBase + (op % lineRange); + elfAddLine(l, address, file, line, &max); + // fprintf(stderr, "Address %08x line %d (%d)\n", address, line,file); + basicBlock = 1; + break; + } + } + } + l->lines = (LineInfoItem *)realloc(l->lines, l->number*sizeof(LineInfoItem)); +} + +u8 *elfSkipData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs) +{ + int i; + int bytes; + + for(i = 0; i < abbrev->numAttrs; i++) { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if(abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if(abbrev->hasChildren) { + int nesting = 1; + while(nesting) { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + for(i = 0; i < abbrev->numAttrs; i++) { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if(abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if(abbrev->hasChildren) { + nesting++; + } + } + } + return data; +} + +Type *elfParseType(CompileUnit *unit, u32); +u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Object **object); +u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function **function); +void elfCleanUp(Function *); + +void elfAddType(Type *type, CompileUnit *unit, u32 offset) +{ + if(type->next == NULL) { + if(unit->types != type && type->offset == 0) { + type->offset = offset; + type->next = unit->types; + unit->types = type; + } + } +} + +void elfParseType(u8 *data, u32 offset, ELFAbbrev *abbrev, CompileUnit *unit, + Type **type) +{ + switch(abbrev->tag) { + case DW_TAG_typedef: + { + u32 typeref = 0; + char *name = NULL; + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + name = attr->string; + break; + case DW_AT_type: + typeref = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + break; + default: + fprintf(stderr, "Unknown attribute for typedef %02x\n", attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for typedef\n"); + *type = elfParseType(unit, typeref); + if(name) + (*type)->name = name; + return; + } + break; + case DW_TAG_union_type: + case DW_TAG_structure_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + if(abbrev->tag == DW_TAG_structure_type) + t->type = TYPE_struct; + else + t->type = TYPE_union; + + Struct *s = (Struct *)calloc(sizeof(Struct), 1); + t->structure = s; + elfAddType(t, unit, offset); + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + case DW_AT_sibling: + case DW_AT_containing_type: // todo? + case DW_AT_declaration: + case DW_AT_specification: // TODO: + break; + default: + fprintf(stderr, "Unknown attribute for struct %02x\n", attr->name); + break; + } + } + if(abbrev->hasChildren) { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + int index = 0; + while(num) { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch(abbr->tag) { + case DW_TAG_member: + { + if((index % 4) == 0) + s->members = (Member *)realloc(s->members, + sizeof(Member)*(index+4)); + Member *m = &s->members[index]; + m->location = NULL; + m->bitOffset = 0; + m->bitSize = 0; + m->byteSize = 0; + for(int i = 0; i < abbr->numAttrs; i++) { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + m->name = attr->string; + break; + case DW_AT_type: + m->type = elfParseType(unit, attr->value); + break; + case DW_AT_data_member_location: + m->location = attr->block; + break; + case DW_AT_byte_size: + m->byteSize = attr->value; + break; + case DW_AT_bit_offset: + m->bitOffset = attr->value; + break; + case DW_AT_bit_size: + m->bitSize = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + case DW_AT_accessibility: + case DW_AT_artificial: // todo? + break; + default: + fprintf(stderr, "Unknown member attribute %02x\n", + attr->name); + } + } + index++; + } + break; + case DW_TAG_subprogram: + { + Function *fnc = NULL; + data = elfParseFunction(data, abbr, unit, &fnc); + if(fnc != NULL) { + if(unit->lastFunction) + unit->lastFunction->next = fnc; + else + unit->functions = fnc; + unit->lastFunction = fnc; + } + } + break; + case DW_TAG_inheritance: + // TODO: add support + data = elfSkipData(data, abbr, unit->abbrevs); + break; + CASE_TYPE_TAG: + // skip types... parsed only when used + data = elfSkipData(data, abbr, unit->abbrevs); + break; + case DW_TAG_variable: + data = elfSkipData(data, abbr, unit->abbrevs); + break; + default: + fprintf(stderr, "Unknown struct tag %02x %s\n", abbr->tag, t->name); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + s->memberCount = index; + } + *type = t; + return; + } + break; + case DW_TAG_base_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_base; + elfAddType(t, unit, offset); + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_encoding: + t->encoding = attr->value; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_bit_size: + t->bitSize = attr->value; + break; + default: + fprintf(stderr, "Unknown attribute for base type %02x\n", + attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for base type\n"); + *type = t; + return; + } + break; + case DW_TAG_pointer_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_pointer; + + elfAddType(t, unit, offset); + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data =elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_type: + t->pointer = elfParseType(unit, attr->value); + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + default: + fprintf(stderr, "Unknown pointer type attribute %02x\n", attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for pointer type\n"); + *type = t; + return; + } + break; + case DW_TAG_reference_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_reference; + + elfAddType(t, unit, offset); + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data =elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_type: + t->pointer = elfParseType(unit, attr->value); + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + default: + fprintf(stderr, "Unknown ref type attribute %02x\n", attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for ref type\n"); + *type = t; + return; + } + break; + case DW_TAG_volatile_type: + { + u32 typeref = 0; + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_type: + typeref = attr->value; + break; + default: + fprintf(stderr, "Unknown volatile attribute for type %02x\n", + attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for volatile type\n"); + *type = elfParseType(unit, typeref); + return; + } + break; + case DW_TAG_const_type: + { + u32 typeref = 0; + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_type: + typeref = attr->value; + break; + default: + fprintf(stderr, "Unknown const attribute for type %02x\n", + attr->name); + break; + } + } + if(abbrev->hasChildren) + fprintf(stderr, "Unexpected children for const type\n"); + *type = elfParseType(unit, typeref); + return; + } + break; + case DW_TAG_enumeration_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_enum; + Enum *e = (Enum *)calloc(sizeof(Enum), 1); + t->enumeration = e; + elfAddType(t, unit, offset); + int count = 0; + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_sibling: + case DW_AT_decl_file: + case DW_AT_decl_line: + break; + default: + fprintf(stderr, "Unknown enum attribute %02x\n", attr->name); + } + } + if(abbrev->hasChildren) { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + while(num) { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch(abbr->tag) { + case DW_TAG_enumerator: + { + count++; + e->members = (EnumMember *)realloc(e->members, + count*sizeof(EnumMember)); + EnumMember *m = &e->members[count-1]; + for(int i = 0; i < abbr->numAttrs; i++) { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_name: + m->name = attr->string; + break; + case DW_AT_const_value: + m->value = attr->value; + break; + default: + fprintf(stderr, "Unknown sub param attribute %02x\n", + attr->name); + } + } + } + break; + default: + fprintf(stderr, "Unknown enum tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + } + e->count = count; + *type = t; + return; + } + break; + case DW_TAG_subroutine_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_function; + FunctionType *f = (FunctionType *)calloc(sizeof(FunctionType), 1); + t->function = f; + elfAddType(t, unit, offset); + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_prototyped: + case DW_AT_sibling: + break; + case DW_AT_type: + f->returnType = elfParseType(unit, attr->value); + break; + default: + fprintf(stderr, "Unknown subroutine attribute %02x\n", attr->name); + } + } + if(abbrev->hasChildren) { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + Object *lastVar = NULL; + while(num) { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch(abbr->tag) { + case DW_TAG_formal_parameter: + { + Object *o; + data = elfParseObject(data, abbr, unit, &o); + if(f->args) + lastVar->next = o; + else + f->args = o; + lastVar = o; + } + break; + case DW_TAG_unspecified_parameters: + // no use in the debugger yet + data = elfSkipData(data, abbr, unit->abbrevs); + break; + CASE_TYPE_TAG: + // skip types... parsed only when used + data = elfSkipData(data, abbr, unit->abbrevs); + break; + default: + fprintf(stderr, "Unknown subroutine tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + } + *type = t; + return; + } + break; + case DW_TAG_array_type: + { + u32 typeref = 0; + int i; + Array *array = (Array *)calloc(sizeof(Array), 1); + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_array; + elfAddType(t, unit, offset); + + for(i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_sibling: + break; + case DW_AT_type: + typeref = attr->value; + array->type = elfParseType(unit, typeref); + break; + default: + fprintf(stderr, "Unknown array attribute %02x\n", attr->name); + } + } + if(abbrev->hasChildren) { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + int index = 0; + int maxBounds = 0; + while(num) { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch(abbr->tag) { + case DW_TAG_subrange_type: + { + if(maxBounds == index) { + maxBounds += 4; + array->bounds = (int *)realloc(array->bounds, + sizeof(int)*maxBounds); + } + for(int i = 0; i < abbr->numAttrs; i++) { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_upper_bound: + array->bounds[index] = attr->value+1; + break; + case DW_AT_type: // ignore + break; + default: + fprintf(stderr, "Unknown subrange attribute %02x\n", + attr->name); + } + } + index++; + } + break; + default: + fprintf(stderr, "Unknown array tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + array->maxBounds = index; + } + t->size = array->type->size; + for(i = 0; i < array->maxBounds; i++) + t->size *= array->bounds[i]; + t->array = array; + *type = t; + return; + } + break; + default: + fprintf(stderr, "Unknown type TAG %02x\n", abbrev->tag); + exit(-1); + } +} + +Type *elfParseType(CompileUnit *unit, u32 offset) +{ + Type *t = unit->types; + + while(t) { + if(t->offset == offset) + return t; + t = t->next; + } + if(offset == 0) { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_void; + t->offset = 0; + elfAddType(t, unit, 0); + return t; + } + u8 *data = unit->top + offset; + int bytes; + int abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + Type *type = NULL; + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + elfParseType(data, offset, abbrev, unit, &type); + return type; +} + +void elfGetObjectAttributes(CompileUnit *unit, u32 offset, Object *o) +{ + u8 *data = unit->top + offset; + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + return; + } + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_location: + o->location = attr->block; + break; + case DW_AT_name: + if(o->name == NULL) + o->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + o->name = attr->string; + break; + case DW_AT_decl_file: + o->file = attr->value; + break; + case DW_AT_decl_line: + o->line = attr->value; + break; + case DW_AT_type: + o->type = elfParseType(unit, attr->value); + break; + case DW_AT_external: + o->external = attr->flag; + break; + case DW_AT_const_value: + case DW_AT_abstract_origin: + case DW_AT_declaration: + case DW_AT_artificial: + // todo + break; + case DW_AT_specification: + // TODO: + break; + default: + fprintf(stderr, "Unknown object attribute %02x\n", attr->name); + break; + } + } +} + +u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Object **object) +{ + Object *o = (Object *)calloc(sizeof(Object), 1); + + o->next = NULL; + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_location: + o->location = attr->block; + break; + case DW_AT_name: + if(o->name == NULL) + o->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + o->name = attr->string; + break; + case DW_AT_decl_file: + o->file = attr->value; + break; + case DW_AT_decl_line: + o->line = attr->value; + break; + case DW_AT_type: + o->type = elfParseType(unit, attr->value); + break; + case DW_AT_external: + o->external = attr->flag; + break; + case DW_AT_abstract_origin: + elfGetObjectAttributes(unit, attr->value, o); + break; + case DW_AT_const_value: + case DW_AT_declaration: + case DW_AT_artificial: + break; + case DW_AT_specification: + // TODO: + break; + default: + fprintf(stderr, "Unknown object attribute %02x\n", attr->name); + break; + } + } + *object = o; + return data; +} + +u8 *elfParseBlock(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function *func, Object **lastVar) +{ + int bytes; + u32 start = func->lowPC; + u32 end = func->highPC; + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_sibling: + break; + case DW_AT_low_pc: + start = attr->value; + break; + case DW_AT_high_pc: + end = attr->value; + break; + case DW_AT_ranges: // ignore for now + break; + default: + fprintf(stderr, "Unknown block attribute %02x\n", attr->name); + break; + } + } + + if(abbrev->hasChildren) { + int nesting = 1; + + while(nesting) { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + switch(abbrev->tag) { + CASE_TYPE_TAG: // types only parsed when used + case DW_TAG_label: // not needed + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_lexical_block: + data = elfParseBlock(data, abbrev, unit, func, lastVar); + break; + case DW_TAG_subprogram: + { + Function *f = NULL; + data = elfParseFunction(data, abbrev, unit, &f); + if(f != NULL) { + if(unit->lastFunction) + unit->lastFunction->next = f; + else + unit->functions = f; + unit->lastFunction = f; + } + } + break; + case DW_TAG_variable: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if(o->startScope == 0) + o->startScope = start; + if(o->endScope == 0) + o->endScope = 0; + if(func->variables) + (*lastVar)->next = o; + else + func->variables = o; + *lastVar = o; + } + break; + case DW_TAG_inlined_subroutine: + // TODO: + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + default: + { + fprintf(stderr, "Unknown block TAG %02x\n", abbrev->tag); + data = elfSkipData(data, abbrev, unit->abbrevs); + } + break; + } + } + } + return data; +} + +void elfGetFunctionAttributes(CompileUnit *unit, u32 offset, Function *func) +{ + u8 *data = unit->top + offset; + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + return; + } + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + + switch(attr->name) { + case DW_AT_sibling: + break; + case DW_AT_name: + if(func->name == NULL) + func->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + func->name = attr->string; + break; + case DW_AT_low_pc: + func->lowPC = attr->value; + break; + case DW_AT_high_pc: + func->highPC = attr->value; + break; + case DW_AT_decl_file: + func->file = attr->value; + break; + case DW_AT_decl_line: + func->line = attr->value; + break; + case DW_AT_external: + func->external = attr->flag; + break; + case DW_AT_frame_base: + func->frameBase = attr->block; + break; + case DW_AT_type: + func->returnType = elfParseType(unit, attr->value); + break; + case DW_AT_inline: + case DW_AT_specification: + case DW_AT_declaration: + case DW_AT_artificial: + case DW_AT_prototyped: + case DW_AT_proc_body: + case DW_AT_save_offset: + case DW_AT_user_2002: + case DW_AT_virtuality: + case DW_AT_containing_type: + case DW_AT_accessibility: + // todo; + break; + case DW_AT_vtable_elem_location: + free(attr->block); + break; + default: + fprintf(stderr, "Unknown function attribute %02x\n", attr->name); + break; + } + } + + return; +} + +u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function **f) +{ + Function *func = (Function *)calloc(sizeof(Function), 1); + *f = func; + + int bytes; + bool mangled = false; + bool declaration = false; + for(int i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch(attr->name) { + case DW_AT_sibling: + break; + case DW_AT_name: + if(func->name == NULL) + func->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + func->name = attr->string; + mangled = true; + break; + case DW_AT_low_pc: + func->lowPC = attr->value; + break; + case DW_AT_high_pc: + func->highPC = attr->value; + break; + case DW_AT_prototyped: + break; + case DW_AT_decl_file: + func->file = attr->value; + break; + case DW_AT_decl_line: + func->line = attr->value; + break; + case DW_AT_external: + func->external = attr->flag; + break; + case DW_AT_frame_base: + func->frameBase = attr->block; + break; + case DW_AT_type: + func->returnType = elfParseType(unit, attr->value); + break; + case DW_AT_abstract_origin: + elfGetFunctionAttributes(unit, attr->value, func); + break; + case DW_AT_declaration: + declaration = attr->flag; + break; + case DW_AT_inline: + case DW_AT_specification: + case DW_AT_artificial: + case DW_AT_proc_body: + case DW_AT_save_offset: + case DW_AT_user_2002: + case DW_AT_virtuality: + case DW_AT_containing_type: + case DW_AT_accessibility: + // todo; + break; + case DW_AT_vtable_elem_location: + free(attr->block); + break; + default: + fprintf(stderr, "Unknown function attribute %02x\n", attr->name); + break; + } + } + + if(declaration) { + elfCleanUp(func); + free(func); + *f = NULL; + + while(1) { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + return data; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + data = elfSkipData(data, abbrev, unit->abbrevs); + } + } + + if(abbrev->hasChildren) { + int nesting = 1; + Object *lastParam = NULL; + Object *lastVar = NULL; + + while(nesting) { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + switch(abbrev->tag) { + CASE_TYPE_TAG: // no need to parse types. only parsed when used + case DW_TAG_label: // not needed + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_subprogram: + { + Function *fnc=NULL; + data = elfParseFunction(data, abbrev, unit, &fnc); + if(fnc != NULL) { + if(unit->lastFunction == NULL) + unit->functions = fnc; + else + unit->lastFunction->next = fnc; + unit->lastFunction = fnc; + } + } + break; + case DW_TAG_lexical_block: + { + data = elfParseBlock(data, abbrev, unit, func, &lastVar); + } + break; + case DW_TAG_formal_parameter: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if(func->parameters) + lastParam->next = o; + else + func->parameters = o; + lastParam = o; + } + break; + case DW_TAG_variable: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if(func->variables) + lastVar->next = o; + else + func->variables = o; + lastVar = o; + } + break; + case DW_TAG_unspecified_parameters: + case DW_TAG_inlined_subroutine: + { + // todo + for(int i = 0; i < abbrev->numAttrs; i++) { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if(abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if(abbrev->hasChildren) + nesting++; + } + break; + default: + { + fprintf(stderr, "Unknown function TAG %02x\n", abbrev->tag); + data = elfSkipData(data, abbrev, unit->abbrevs); + } + break; + } + } + } + return data; +} + +u8 *elfParseUnknownData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs) +{ + int i; + int bytes; + // switch(abbrev->tag) { + // default: + fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag); + + for(i = 0; i < abbrev->numAttrs; i++) { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if(abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if(abbrev->hasChildren) { + int nesting = 1; + while(nesting) { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if(!abbrevNum) { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag); + + for(i = 0; i < abbrev->numAttrs; i++) { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if(abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if(abbrev->hasChildren) { + nesting++; + } + } + } + // } + return data; +} + +u8 *elfParseCompileUnitChildren(u8 *data, CompileUnit *unit) +{ + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + Object *lastObj = NULL; + while(abbrevNum) { + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + switch(abbrev->tag) { + case DW_TAG_subprogram: + { + Function *func = NULL; + data = elfParseFunction(data, abbrev, unit, &func); + if(func != NULL) { + if(unit->lastFunction) + unit->lastFunction->next = func; + else + unit->functions = func; + unit->lastFunction = func; + } + } + break; + CASE_TYPE_TAG: + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_variable: + { + Object *var = NULL; + data = elfParseObject(data, abbrev, unit, &var); + if(lastObj) + lastObj->next = var; + else + unit->variables = var; + lastObj = var; + } + break; + default: + data = elfParseUnknownData(data, abbrev, unit->abbrevs); + break; + } + + abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + } + return data; +} + + +CompileUnit *elfParseCompUnit(u8 *data, u8 *abbrevData) +{ + int bytes; + u8 *top = data; + + u32 length = elfRead4Bytes(data); + data += 4; + + u16 version = elfRead2Bytes(data); + data += 2; + + u32 offset = elfRead4Bytes(data); + data += 4; + + u8 addrSize = *data++; + + if(version != 2) { + fprintf(stderr, "Unsupported debugging information version %d\n", version); + return NULL; + } + + if(addrSize != 4) { + fprintf(stderr, "Unsupported address size %d\n", addrSize); + return NULL; + } + + ELFAbbrev **abbrevs = elfReadAbbrevs(abbrevData, offset); + + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + ELFAbbrev *abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + CompileUnit *unit = (CompileUnit *)calloc(sizeof(CompileUnit), 1); + unit->top = top; + unit->length = length; + unit->abbrevs = abbrevs; + unit->next = NULL; + + elfCurrentUnit = unit; + + int i; + + for(i = 0; i < abbrev->numAttrs; i++) { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + + switch(attr->name) { + case DW_AT_name: + unit->name = attr->string; + break; + case DW_AT_stmt_list: + unit->hasLineInfo = true; + unit->lineInfo = attr->value; + break; + case DW_AT_low_pc: + unit->lowPC = attr->value; + break; + case DW_AT_high_pc: + unit->highPC = attr->value; + break; + case DW_AT_compdir: + unit->compdir = attr->string; + break; + // ignore + case DW_AT_language: + case DW_AT_producer: + case DW_AT_macro_info: + case DW_AT_entry_pc: + break; + default: + fprintf(stderr, "Unknown attribute %02x\n", attr->name); + break; + } + } + + if(abbrev->hasChildren) + elfParseCompileUnitChildren(data, unit); + + return unit; +} + +void elfParseAranges(u8 *data) +{ + ELFSectionHeader *sh = elfGetSectionByName(".debug_aranges"); + if(sh == NULL) { + fprintf(stderr, "No aranges found\n"); + return; + } + + data = elfReadSection(data, sh); + u8 *end = data + READ32LE(&sh->size); + + int max = 4; + ARanges *ranges = (ARanges *)calloc(sizeof(ARanges), 4); + + int index = 0; + + while(data < end) { + u32 len = elfRead4Bytes(data); + data += 4; + // u16 version = elfRead2Bytes(data); + data += 2; + u32 offset = elfRead4Bytes(data); + data += 4; + // u8 addrSize = *data++; + // u8 segSize = *data++; + data += 2; // remove if uncommenting above + data += 4; + ranges[index].count = (len-20)/8; + ranges[index].offset = offset; + ranges[index].ranges = (ARange *)calloc(sizeof(ARange), (len-20)/8); + int i = 0; + while(true) { + u32 addr = elfRead4Bytes(data); + data += 4; + u32 len = elfRead4Bytes(data); + data += 4; + if(addr == 0 && len == 0) + break; + ranges[index].ranges[i].lowPC = addr; + ranges[index].ranges[i].highPC = addr+len; + i++; + } + index++; + if(index == max) { + max += 4; + ranges = (ARanges *)realloc(ranges, max*sizeof(ARanges)); + } + } + elfDebugInfo->numRanges = index; + elfDebugInfo->ranges = ranges; +} + +void elfReadSymtab(u8 *data) +{ + ELFSectionHeader *sh = elfGetSectionByName(".symtab"); + int table = READ32LE(&sh->link); + + char *strtable = (char *)elfReadSection(data, elfGetSectionByNumber(table)); + + ELFSymbol *symtab = (ELFSymbol *)elfReadSection(data, sh); + + int count = READ32LE(&sh->size) / sizeof(ELFSymbol); + elfSymbolsCount = 0; + + elfSymbols = (Symbol *)malloc(sizeof(Symbol)*count); + + int i; + + for(i = 0; i < count; i++) { + ELFSymbol *s = &symtab[i]; + int type = s->info & 15; + int binding = s->info >> 4; + + if(binding) { + Symbol *sym = &elfSymbols[elfSymbolsCount]; + sym->name = &strtable[READ32LE(&s->name)]; + sym->binding = binding; + sym->type = type; + sym->value = READ32LE(&s->value); + sym->size = READ32LE(&s->size); + elfSymbolsCount++; + } + } + for(i = 0; i < count; i++) { + ELFSymbol *s = &symtab[i]; + int bind = s->info>>4; + int type = s->info & 15; + + if(!bind) { + Symbol *sym = &elfSymbols[elfSymbolsCount]; + sym->name = &strtable[READ32LE(&s->name)]; + sym->binding = (s->info >> 4); + sym->type = type; + sym->value = READ32LE(&s->value); + sym->size = READ32LE(&s->size); + elfSymbolsCount++; + } + } + elfSymbolsStrTab = strtable; + // free(symtab); +} + +bool elfReadProgram(ELFHeader *eh, u8 *data, int& size, bool parseDebug) +{ + int count = READ16LE(&eh->e_phnum); + int i; + + if(READ32LE(&eh->e_entry) == 0x2000000) + cpuIsMultiBoot = true; + + // read program headers... should probably move this code down + u8 *p = data + READ32LE(&eh->e_phoff); + size = 0; + for(i = 0; i < count; i++) { + ELFProgramHeader *ph = (ELFProgramHeader *)p; + p += sizeof(ELFProgramHeader); + if(READ16LE(&eh->e_phentsize) != sizeof(ELFProgramHeader)) { + p += READ16LE(&eh->e_phentsize) - sizeof(ELFProgramHeader); + } + + // printf("PH %d %08x %08x %08x %08x %08x %08x %08x %08x\n", + // i, ph->type, ph->offset, ph->vaddr, ph->paddr, + // ph->filesz, ph->memsz, ph->flags, ph->align); + if(cpuIsMultiBoot) { + if(READ32LE(&ph->paddr) >= 0x2000000 && + READ32LE(&ph->paddr) <= 0x203ffff) { + memcpy(&workRAM[READ32LE(&ph->paddr) & 0x3ffff], + data + READ32LE(&ph->offset), + READ32LE(&ph->filesz)); + size += READ32LE(&ph->filesz); + } + } else { + if(READ32LE(&ph->paddr) >= 0x8000000 && + READ32LE(&ph->paddr) <= 0x9ffffff) { + memcpy(&rom[READ32LE(&ph->paddr) & 0x1ffffff], + data + READ32LE(&ph->offset), + READ32LE(&ph->filesz)); + size += READ32LE(&ph->filesz); + } + } + } + + char *stringTable = NULL; + + // read section headers + p = data + READ32LE(&eh->e_shoff); + count = READ16LE(&eh->e_shnum); + + ELFSectionHeader **sh = (ELFSectionHeader **) + malloc(sizeof(ELFSectionHeader *) * count); + + for(i = 0; i < count; i++) { + sh[i] = (ELFSectionHeader *)p; + p += sizeof(ELFSectionHeader); + if(READ16LE(&eh->e_shentsize) != sizeof(ELFSectionHeader)) + p += READ16LE(&eh->e_shentsize) - sizeof(ELFSectionHeader); + } + + if(READ16LE(&eh->e_shstrndx) != 0) { + stringTable = (char *)elfReadSection(data, + sh[READ16LE(&eh->e_shstrndx)]); + } + + elfSectionHeaders = sh; + elfSectionHeadersStringTable = stringTable; + elfSectionHeadersCount = count; + + for(i = 0; i < count; i++) { + // printf("SH %d %-20s %08x %08x %08x %08x %08x %08x %08x %08x\n", + // i, &stringTable[sh[i]->name], sh[i]->name, sh[i]->type, + // sh[i]->flags, sh[i]->addr, sh[i]->offset, sh[i]->size, + // sh[i]->link, sh[i]->info); + if(READ32LE(&sh[i]->flags) & 2) { // load section + if(cpuIsMultiBoot) { + if(READ32LE(&sh[i]->addr) >= 0x2000000 && + READ32LE(&sh[i]->addr) <= 0x203ffff) { + memcpy(&workRAM[READ32LE(&sh[i]->addr) & 0x3ffff], data + + READ32LE(&sh[i]->offset), + READ32LE(&sh[i]->size)); + size += READ32LE(&sh[i]->size); + } + } else { + if(READ32LE(&sh[i]->addr) >= 0x8000000 && + READ32LE(&sh[i]->addr) <= 0x9ffffff) { + memcpy(&rom[READ32LE(&sh[i]->addr) & 0x1ffffff], + data + READ32LE(&sh[i]->offset), + READ32LE(&sh[i]->size)); + size += READ32LE(&sh[i]->size); + } + } + } + } + + if(parseDebug) { + fprintf(stderr, "Parsing debug info\n"); + + ELFSectionHeader *dbgHeader = elfGetSectionByName(".debug_info"); + if(dbgHeader == NULL) { + fprintf(stderr, "Cannot find debug information\n"); + goto end; + } + + ELFSectionHeader *h = elfGetSectionByName(".debug_abbrev"); + if(h == NULL) { + fprintf(stderr, "Cannot find abbreviation table\n"); + goto end; + } + + elfDebugInfo = (DebugInfo *)calloc(sizeof(DebugInfo), 1); + u8 *abbrevdata = elfReadSection(data, h); + + h = elfGetSectionByName(".debug_str"); + + if(h == NULL) + elfDebugStrings = NULL; + else + elfDebugStrings = (char *)elfReadSection(data, h); + + u8 *debugdata = elfReadSection(data, dbgHeader); + + elfDebugInfo->debugdata = data; + elfDebugInfo->infodata = debugdata; + + u32 total = READ32LE(&dbgHeader->size); + u8 *end = debugdata + total; + u8 *ddata = debugdata; + + CompileUnit *last = NULL; + CompileUnit *unit = NULL; + + while(ddata < end) { + unit = elfParseCompUnit(ddata, abbrevdata); + unit->offset = ddata-debugdata; + elfParseLineInfo(unit, data); + if(last == NULL) + elfCompileUnits = unit; + else + last->next = unit; + last = unit; + ddata += 4 + unit->length; + } + elfParseAranges(data); + CompileUnit *comp = elfCompileUnits; + while(comp) { + ARanges *r = elfDebugInfo->ranges; + for(int i = 0; i < elfDebugInfo->numRanges; i++) + if(r[i].offset == comp->offset) { + comp->ranges = &r[i]; + break; + } + comp = comp->next; + } + elfParseCFA(data); + elfReadSymtab(data); + } + end: + if(sh) { + free(sh); + } + + elfSectionHeaders = NULL; + elfSectionHeadersStringTable = NULL; + elfSectionHeadersCount = 0; + + return true; +} + +extern bool parseDebug; + +bool elfRead(const char *name, int& siz, FILE *f) +{ + fseek(f, 0, SEEK_END); + long size = ftell(f); + elfFileData = (u8 *)malloc(size); + fseek(f, 0, SEEK_SET); + fread(elfFileData, 1, size, f); + fclose(f); + + ELFHeader *header = (ELFHeader *)elfFileData; + + if(READ32LE(&header->magic) != 0x464C457F || + READ16LE(&header->e_machine) != 40 || + header->clazz != 1) { + systemMessage(0, N_("Not a valid ELF file %s"), name); + free(elfFileData); + elfFileData = NULL; + return false; + } + + if(!elfReadProgram(header, elfFileData, siz, parseDebug)) { + free(elfFileData); + elfFileData = NULL; + return false; + } + + return true; +} + +void elfCleanUp(Object *o) +{ + free(o->location); +} + +void elfCleanUp(Function *func) +{ + Object *o = func->parameters; + while(o) { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + + o = func->variables; + while(o) { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + free(func->frameBase); +} + +void elfCleanUp(ELFAbbrev **abbrevs) +{ + for(int i = 0; i < 121; i++) { + ELFAbbrev *abbrev = abbrevs[i]; + + while(abbrev) { + free(abbrev->attrs); + ELFAbbrev *next = abbrev->next; + free(abbrev); + + abbrev = next; + } + } +} + +void elfCleanUp(Type *t) +{ + switch(t->type) { + case TYPE_function: + if(t->function) { + Object *o = t->function->args; + while(o) { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + free(t->function); + } + break; + case TYPE_array: + if(t->array) { + free(t->array->bounds); + free(t->array); + } + break; + case TYPE_struct: + case TYPE_union: + if(t->structure) { + for(int i = 0; i < t->structure->memberCount; i++) { + free(t->structure->members[i].location); + } + free(t->structure->members); + free(t->structure); + } + break; + case TYPE_enum: + if(t->enumeration) { + free(t->enumeration->members); + free(t->enumeration); + } + break; + case TYPE_base: + case TYPE_pointer: + case TYPE_void: + case TYPE_reference: + break; // nothing to do + } +} + +void elfCleanUp(CompileUnit *comp) +{ + elfCleanUp(comp->abbrevs); + free(comp->abbrevs); + Function *func = comp->functions; + while(func) { + elfCleanUp(func); + Function *next = func->next; + free(func); + func = next; + } + Type *t = comp->types; + while(t) { + elfCleanUp(t); + Type *next = t->next; + free(t); + t = next; + } + Object *o = comp->variables; + while(o) { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + if(comp->lineInfoTable) { + free(comp->lineInfoTable->lines); + free(comp->lineInfoTable->files); + free(comp->lineInfoTable); + } +} + +void elfCleanUp() +{ + CompileUnit *comp = elfCompileUnits; + + while(comp) { + elfCleanUp(comp); + CompileUnit *next = comp->next; + free(comp); + comp = next; + } + elfCompileUnits = NULL; + free(elfSymbols); + elfSymbols = NULL; + // free(elfSymbolsStrTab); + elfSymbolsStrTab = NULL; + + elfDebugStrings = NULL; + if(elfDebugInfo) { + int num = elfDebugInfo->numRanges; + int i; + for(i = 0; i < num; i++) { + free(elfDebugInfo->ranges[i].ranges); + } + free(elfDebugInfo->ranges); + free(elfDebugInfo); + elfDebugInfo = NULL; + } + + if(elfFdes) { + if(elfFdeCount) { + for(int i = 0; i < elfFdeCount; i++) + free(elfFdes[i]); + } + free(elfFdes); + + elfFdes = NULL; + elfFdeCount = 0; + } + + ELFcie *cie = elfCies; + while(cie) { + ELFcie *next = cie->next; + free(cie); + cie = next; + } + elfCies = NULL; + + if(elfFileData) { + free(elfFileData); + elfFileData = NULL; + } +} \ No newline at end of file diff --git a/src/elf.h b/src/elf.h new file mode 100644 index 00000000..8ce4379c --- /dev/null +++ b/src/elf.h @@ -0,0 +1,283 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_ELF_H +#define VBA_ELF_H + +enum LocationType { + LOCATION_register, + LOCATION_memory, + LOCATION_value +}; + +#define DW_ATE_boolean 0x02 +#define DW_ATE_signed 0x05 +#define DW_ATE_unsigned 0x07 +#define DW_ATE_unsigned_char 0x08 + +struct ELFHeader { + u32 magic; + u8 clazz; + u8 data; + u8 version; + u8 pad[9]; + u16 e_type; + u16 e_machine; + u32 e_version; + u32 e_entry; + u32 e_phoff; + u32 e_shoff; + u32 e_flags; + u16 e_ehsize; + u16 e_phentsize; + u16 e_phnum; + u16 e_shentsize; + u16 e_shnum; + u16 e_shstrndx; +}; + +struct ELFProgramHeader { + u32 type; + u32 offset; + u32 vaddr; + u32 paddr; + u32 filesz; + u32 memsz; + u32 flags; + u32 align; +}; + +struct ELFSectionHeader { + u32 name; + u32 type; + u32 flags; + u32 addr; + u32 offset; + u32 size; + u32 link; + u32 info; + u32 addralign; + u32 entsize; +}; + +struct ELFSymbol { + u32 name; + u32 value; + u32 size; + u8 info; + u8 other; + u16 shndx; +}; + +struct ELFBlock { + int length; + u8 *data; +}; + +struct ELFAttr { + u32 name; + u32 form; + union { + u32 value; + char *string; + u8 *data; + bool flag; + ELFBlock *block; + }; +}; + +struct ELFAbbrev { + u32 number; + u32 tag; + bool hasChildren; + int numAttrs; + ELFAttr *attrs; + ELFAbbrev *next; +}; + +enum TypeEnum { + TYPE_base, + TYPE_pointer, + TYPE_function, + TYPE_void, + TYPE_array, + TYPE_struct, + TYPE_reference, + TYPE_enum, + TYPE_union +}; + +struct Type; +struct Object; + +struct FunctionType { + Type *returnType; + Object *args; +}; + +struct Member { + char *name; + Type *type; + int bitSize; + int bitOffset; + int byteSize; + ELFBlock *location; +}; + +struct Struct { + int memberCount; + Member *members; +}; + +struct Array { + Type *type; + int maxBounds; + int *bounds; +}; + +struct EnumMember { + char *name; + u32 value; +}; + +struct Enum { + int count; + EnumMember *members; +}; + +struct Type { + u32 offset; + TypeEnum type; + char *name; + int encoding; + int size; + int bitSize; + union { + Type *pointer; + FunctionType *function; + Array *array; + Struct *structure; + Enum *enumeration; + }; + Type *next; +}; + +struct Object { + char *name; + int file; + int line; + bool external; + Type *type; + ELFBlock *location; + u32 startScope; + u32 endScope; + Object *next; +}; + +struct Function { + char *name; + u32 lowPC; + u32 highPC; + int file; + int line; + bool external; + Type *returnType; + Object *parameters; + Object *variables; + ELFBlock *frameBase; + Function *next; +}; + +struct LineInfoItem { + u32 address; + char *file; + int line; +}; + +struct LineInfo { + int fileCount; + char **files; + int number; + LineInfoItem *lines; +}; + +struct ARange { + u32 lowPC; + u32 highPC; +}; + +struct ARanges { + u32 offset; + int count; + ARange *ranges; +}; + +struct CompileUnit { + u32 length; + u8 *top; + u32 offset; + ELFAbbrev **abbrevs; + ARanges *ranges; + char *name; + char *compdir; + u32 lowPC; + u32 highPC; + bool hasLineInfo; + u32 lineInfo; + LineInfo *lineInfoTable; + Function *functions; + Function *lastFunction; + Object *variables; + Type *types; + CompileUnit *next; +}; + +struct DebugInfo { + u8 *debugfile; + u8 *abbrevdata; + u8 *debugdata; + u8 *infodata; + int numRanges; + ARanges *ranges; +}; + +struct Symbol { + char *name; + int type; + int binding; + u32 address; + u32 value; + u32 size; +}; + +extern u32 elfReadLEB128(u8 *, int *); +extern s32 elfReadSignedLEB128(u8 *, int *); +extern bool elfRead(const char *, int &, FILE *f); +extern bool elfGetSymbolAddress(char *,u32 *, u32 *, int *); +extern char *elfGetAddressSymbol(u32); +extern char *elfGetSymbol(int, u32 *, u32 *, int *); +extern void elfCleanUp(); +extern bool elfGetCurrentFunction(u32, Function **, CompileUnit **c); +extern bool elfGetObject(char *, Function *, CompileUnit *, Object **); +extern bool elfFindLineInUnit(u32 *, CompileUnit *, int); +extern bool elfFindLineInModule(u32 *, char *, int); +u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *); +u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *, u32); +int elfFindLine(CompileUnit *unit, Function *func, u32 addr, char **); +#endif diff --git a/src/gb/GB.cpp b/src/gb/GB.cpp new file mode 100644 index 00000000..55563e56 --- /dev/null +++ b/src/gb/GB.cpp @@ -0,0 +1,3217 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include + +#include "../System.h" +#include "../NLS.h" +#include "GB.h" +#include "gbCheats.h" +#include "gbGlobals.h" +#include "gbMemory.h" +#include "gbSGB.h" +#include "gbSound.h" +#include "../unzip.h" +#include "../Util.h" + +#ifdef __GNUC__ +#define _stricmp strcasecmp +#endif + +extern u8 *pix; +extern bool speedup; + +bool gbUpdateSizes(); + +// debugging +bool memorydebug = false; +char gbBuffer[2048]; + +extern u16 gbLineMix[160]; + +// mappers +void (*mapper)(u16,u8) = NULL; +void (*mapperRAM)(u16,u8) = NULL; +u8 (*mapperReadRAM)(u16) = NULL; + +// registers +gbRegister PC; +gbRegister SP; +gbRegister AF; +gbRegister BC; +gbRegister DE; +gbRegister HL; +u16 IFF; +// 0xff04 +u8 register_DIV = 0; +// 0xff05 +u8 register_TIMA = 0; +// 0xff06 +u8 register_TMA = 0; +// 0xff07 +u8 register_TAC = 0; +// 0xff0f +u8 register_IF = 0; +// 0xff40 +u8 register_LCDC = 0; +// 0xff41 +u8 register_STAT = 0; +// 0xff42 +u8 register_SCY = 0; +// 0xff43 +u8 register_SCX = 0; +// 0xff44 +u8 register_LY = 0; +// 0xff45 +u8 register_LYC = 0; +// 0xff46 +u8 register_DMA = 0; +// 0xff4a +u8 register_WY = 0; +// 0xff4b +u8 register_WX = 0; +// 0xff4f +u8 register_VBK = 0; +// 0xff51 +u8 register_HDMA1 = 0; +// 0xff52 +u8 register_HDMA2 = 0; +// 0xff53 +u8 register_HDMA3 = 0; +// 0xff54 +u8 register_HDMA4 = 0; +// 0xff55 +u8 register_HDMA5 = 0; +// 0xff70 +u8 register_SVBK = 0; +// 0xffff +u8 register_IE = 0; + +// ticks definition +int GBDIV_CLOCK_TICKS = 64; +int GBLCD_MODE_0_CLOCK_TICKS = 51; +int GBLCD_MODE_1_CLOCK_TICKS = 1140; +int GBLCD_MODE_2_CLOCK_TICKS = 20; +int GBLCD_MODE_3_CLOCK_TICKS = 43; +int GBLY_INCREMENT_CLOCK_TICKS = 114; +int GBTIMER_MODE_0_CLOCK_TICKS = 256; +int GBTIMER_MODE_1_CLOCK_TICKS = 4; +int GBTIMER_MODE_2_CLOCK_TICKS = 16; +int GBTIMER_MODE_3_CLOCK_TICKS = 64; +int GBSERIAL_CLOCK_TICKS = 128; +int GBSYNCHRONIZE_CLOCK_TICKS = 52920; + +// state variables + +// interrupt +int gbInterrupt = 0; +int gbInterruptWait = 0; +// serial +int gbSerialOn = 0; +int gbSerialTicks = 0; +int gbSerialBits = 0; +// timer +int gbTimerOn = 0; +int gbTimerTicks = 0; +int gbTimerClockTicks = 0; +int gbTimerMode = 0; +// lcd +int gbLcdMode = 2; +int gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS; +int gbLcdLYIncrementTicks = 0; +// div +int gbDivTicks = GBDIV_CLOCK_TICKS; +// cgb +int gbVramBank = 0; +int gbWramBank = 1; +int gbHdmaSource = 0x0000; +int gbHdmaDestination = 0x8000; +int gbHdmaBytes = 0x0000; +int gbHdmaOn = 0; +int gbSpeed = 0; +// frame counting +int gbFrameCount = 0; +int gbFrameSkip = 0; +int gbFrameSkipCount = 0; +// timing +u32 gbLastTime = 0; +u32 gbElapsedTime = 0; +u32 gbTimeNow = 0; +int gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS; +// emulator features +int gbBattery = 0; +int gbCaptureNumber = 0; +bool gbCapture = false; +bool gbCapturePrevious = false; +int gbJoymask[4] = { 0, 0, 0, 0 }; + +int gbRomSizes[] = { 0x00008000, // 32K + 0x00010000, // 64K + 0x00020000, // 128K + 0x00040000, // 256K + 0x00080000, // 512K + 0x00100000, // 1024K + 0x00200000, // 2048K + 0x00400000, // 4096K + 0x00800000 // 8192K +}; +int gbRomSizesMasks[] = { 0x00007fff, + 0x0000ffff, + 0x0001ffff, + 0x0003ffff, + 0x0007ffff, + 0x000fffff, + 0x001fffff, + 0x003fffff, + 0x007fffff +}; + +int gbRamSizes[6] = { 0x00000000, // 0K + 0x00000800, // 2K + 0x00002000, // 8K + 0x00008000, // 32K + 0x00020000, // 128K + 0x00010000 // 64K +}; + +int gbRamSizesMasks[6] = { 0x00000000, + 0x000007ff, + 0x00001fff, + 0x00007fff, + 0x0001ffff, + 0x0000ffff +}; + +int gbCycles[] = { +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, // 0 + 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, // 1 + 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 2 + 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 3 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 4 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 5 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 6 + 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, // 7 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 8 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 9 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // a + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // b + 2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4, // c + 2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4, // d + 3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4, // e + 3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4 // f +}; + +int gbCyclesCB[] = { +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 1 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 2 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 3 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 4 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 5 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 6 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 7 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 8 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 9 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // a + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // b + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // c + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // d + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // e + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2 // f +}; + +u16 DAATable[] = { + 0x0080,0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700, + 0x0800,0x0900,0x1020,0x1120,0x1220,0x1320,0x1420,0x1520, + 0x1000,0x1100,0x1200,0x1300,0x1400,0x1500,0x1600,0x1700, + 0x1800,0x1900,0x2020,0x2120,0x2220,0x2320,0x2420,0x2520, + 0x2000,0x2100,0x2200,0x2300,0x2400,0x2500,0x2600,0x2700, + 0x2800,0x2900,0x3020,0x3120,0x3220,0x3320,0x3420,0x3520, + 0x3000,0x3100,0x3200,0x3300,0x3400,0x3500,0x3600,0x3700, + 0x3800,0x3900,0x4020,0x4120,0x4220,0x4320,0x4420,0x4520, + 0x4000,0x4100,0x4200,0x4300,0x4400,0x4500,0x4600,0x4700, + 0x4800,0x4900,0x5020,0x5120,0x5220,0x5320,0x5420,0x5520, + 0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,0x5600,0x5700, + 0x5800,0x5900,0x6020,0x6120,0x6220,0x6320,0x6420,0x6520, + 0x6000,0x6100,0x6200,0x6300,0x6400,0x6500,0x6600,0x6700, + 0x6800,0x6900,0x7020,0x7120,0x7220,0x7320,0x7420,0x7520, + 0x7000,0x7100,0x7200,0x7300,0x7400,0x7500,0x7600,0x7700, + 0x7800,0x7900,0x8020,0x8120,0x8220,0x8320,0x8420,0x8520, + 0x8000,0x8100,0x8200,0x8300,0x8400,0x8500,0x8600,0x8700, + 0x8800,0x8900,0x9020,0x9120,0x9220,0x9320,0x9420,0x9520, + 0x9000,0x9100,0x9200,0x9300,0x9400,0x9500,0x9600,0x9700, + 0x9800,0x9900,0x00B0,0x0130,0x0230,0x0330,0x0430,0x0530, + 0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,0x0610,0x0710, + 0x0810,0x0910,0x1030,0x1130,0x1230,0x1330,0x1430,0x1530, + 0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,0x1610,0x1710, + 0x1810,0x1910,0x2030,0x2130,0x2230,0x2330,0x2430,0x2530, + 0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,0x2610,0x2710, + 0x2810,0x2910,0x3030,0x3130,0x3230,0x3330,0x3430,0x3530, + 0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,0x3610,0x3710, + 0x3810,0x3910,0x4030,0x4130,0x4230,0x4330,0x4430,0x4530, + 0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,0x4610,0x4710, + 0x4810,0x4910,0x5030,0x5130,0x5230,0x5330,0x5430,0x5530, + 0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,0x5610,0x5710, + 0x5810,0x5910,0x6030,0x6130,0x6230,0x6330,0x6430,0x6530, + 0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,0x6610,0x6710, + 0x6810,0x6910,0x7030,0x7130,0x7230,0x7330,0x7430,0x7530, + 0x7010,0x7110,0x7210,0x7310,0x7410,0x7510,0x7610,0x7710, + 0x7810,0x7910,0x8030,0x8130,0x8230,0x8330,0x8430,0x8530, + 0x8010,0x8110,0x8210,0x8310,0x8410,0x8510,0x8610,0x8710, + 0x8810,0x8910,0x9030,0x9130,0x9230,0x9330,0x9430,0x9530, + 0x9010,0x9110,0x9210,0x9310,0x9410,0x9510,0x9610,0x9710, + 0x9810,0x9910,0xA030,0xA130,0xA230,0xA330,0xA430,0xA530, + 0xA010,0xA110,0xA210,0xA310,0xA410,0xA510,0xA610,0xA710, + 0xA810,0xA910,0xB030,0xB130,0xB230,0xB330,0xB430,0xB530, + 0xB010,0xB110,0xB210,0xB310,0xB410,0xB510,0xB610,0xB710, + 0xB810,0xB910,0xC030,0xC130,0xC230,0xC330,0xC430,0xC530, + 0xC010,0xC110,0xC210,0xC310,0xC410,0xC510,0xC610,0xC710, + 0xC810,0xC910,0xD030,0xD130,0xD230,0xD330,0xD430,0xD530, + 0xD010,0xD110,0xD210,0xD310,0xD410,0xD510,0xD610,0xD710, + 0xD810,0xD910,0xE030,0xE130,0xE230,0xE330,0xE430,0xE530, + 0xE010,0xE110,0xE210,0xE310,0xE410,0xE510,0xE610,0xE710, + 0xE810,0xE910,0xF030,0xF130,0xF230,0xF330,0xF430,0xF530, + 0xF010,0xF110,0xF210,0xF310,0xF410,0xF510,0xF610,0xF710, + 0xF810,0xF910,0x00B0,0x0130,0x0230,0x0330,0x0430,0x0530, + 0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,0x0610,0x0710, + 0x0810,0x0910,0x1030,0x1130,0x1230,0x1330,0x1430,0x1530, + 0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,0x1610,0x1710, + 0x1810,0x1910,0x2030,0x2130,0x2230,0x2330,0x2430,0x2530, + 0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,0x2610,0x2710, + 0x2810,0x2910,0x3030,0x3130,0x3230,0x3330,0x3430,0x3530, + 0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,0x3610,0x3710, + 0x3810,0x3910,0x4030,0x4130,0x4230,0x4330,0x4430,0x4530, + 0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,0x4610,0x4710, + 0x4810,0x4910,0x5030,0x5130,0x5230,0x5330,0x5430,0x5530, + 0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,0x5610,0x5710, + 0x5810,0x5910,0x6030,0x6130,0x6230,0x6330,0x6430,0x6530, + 0x0600,0x0700,0x0800,0x0900,0x0A00,0x0B00,0x0C00,0x0D00, + 0x0E00,0x0F00,0x1020,0x1120,0x1220,0x1320,0x1420,0x1520, + 0x1600,0x1700,0x1800,0x1900,0x1A00,0x1B00,0x1C00,0x1D00, + 0x1E00,0x1F00,0x2020,0x2120,0x2220,0x2320,0x2420,0x2520, + 0x2600,0x2700,0x2800,0x2900,0x2A00,0x2B00,0x2C00,0x2D00, + 0x2E00,0x2F00,0x3020,0x3120,0x3220,0x3320,0x3420,0x3520, + 0x3600,0x3700,0x3800,0x3900,0x3A00,0x3B00,0x3C00,0x3D00, + 0x3E00,0x3F00,0x4020,0x4120,0x4220,0x4320,0x4420,0x4520, + 0x4600,0x4700,0x4800,0x4900,0x4A00,0x4B00,0x4C00,0x4D00, + 0x4E00,0x4F00,0x5020,0x5120,0x5220,0x5320,0x5420,0x5520, + 0x5600,0x5700,0x5800,0x5900,0x5A00,0x5B00,0x5C00,0x5D00, + 0x5E00,0x5F00,0x6020,0x6120,0x6220,0x6320,0x6420,0x6520, + 0x6600,0x6700,0x6800,0x6900,0x6A00,0x6B00,0x6C00,0x6D00, + 0x6E00,0x6F00,0x7020,0x7120,0x7220,0x7320,0x7420,0x7520, + 0x7600,0x7700,0x7800,0x7900,0x7A00,0x7B00,0x7C00,0x7D00, + 0x7E00,0x7F00,0x8020,0x8120,0x8220,0x8320,0x8420,0x8520, + 0x8600,0x8700,0x8800,0x8900,0x8A00,0x8B00,0x8C00,0x8D00, + 0x8E00,0x8F00,0x9020,0x9120,0x9220,0x9320,0x9420,0x9520, + 0x9600,0x9700,0x9800,0x9900,0x9A00,0x9B00,0x9C00,0x9D00, + 0x9E00,0x9F00,0x00B0,0x0130,0x0230,0x0330,0x0430,0x0530, + 0x0610,0x0710,0x0810,0x0910,0x0A10,0x0B10,0x0C10,0x0D10, + 0x0E10,0x0F10,0x1030,0x1130,0x1230,0x1330,0x1430,0x1530, + 0x1610,0x1710,0x1810,0x1910,0x1A10,0x1B10,0x1C10,0x1D10, + 0x1E10,0x1F10,0x2030,0x2130,0x2230,0x2330,0x2430,0x2530, + 0x2610,0x2710,0x2810,0x2910,0x2A10,0x2B10,0x2C10,0x2D10, + 0x2E10,0x2F10,0x3030,0x3130,0x3230,0x3330,0x3430,0x3530, + 0x3610,0x3710,0x3810,0x3910,0x3A10,0x3B10,0x3C10,0x3D10, + 0x3E10,0x3F10,0x4030,0x4130,0x4230,0x4330,0x4430,0x4530, + 0x4610,0x4710,0x4810,0x4910,0x4A10,0x4B10,0x4C10,0x4D10, + 0x4E10,0x4F10,0x5030,0x5130,0x5230,0x5330,0x5430,0x5530, + 0x5610,0x5710,0x5810,0x5910,0x5A10,0x5B10,0x5C10,0x5D10, + 0x5E10,0x5F10,0x6030,0x6130,0x6230,0x6330,0x6430,0x6530, + 0x6610,0x6710,0x6810,0x6910,0x6A10,0x6B10,0x6C10,0x6D10, + 0x6E10,0x6F10,0x7030,0x7130,0x7230,0x7330,0x7430,0x7530, + 0x7610,0x7710,0x7810,0x7910,0x7A10,0x7B10,0x7C10,0x7D10, + 0x7E10,0x7F10,0x8030,0x8130,0x8230,0x8330,0x8430,0x8530, + 0x8610,0x8710,0x8810,0x8910,0x8A10,0x8B10,0x8C10,0x8D10, + 0x8E10,0x8F10,0x9030,0x9130,0x9230,0x9330,0x9430,0x9530, + 0x9610,0x9710,0x9810,0x9910,0x9A10,0x9B10,0x9C10,0x9D10, + 0x9E10,0x9F10,0xA030,0xA130,0xA230,0xA330,0xA430,0xA530, + 0xA610,0xA710,0xA810,0xA910,0xAA10,0xAB10,0xAC10,0xAD10, + 0xAE10,0xAF10,0xB030,0xB130,0xB230,0xB330,0xB430,0xB530, + 0xB610,0xB710,0xB810,0xB910,0xBA10,0xBB10,0xBC10,0xBD10, + 0xBE10,0xBF10,0xC030,0xC130,0xC230,0xC330,0xC430,0xC530, + 0xC610,0xC710,0xC810,0xC910,0xCA10,0xCB10,0xCC10,0xCD10, + 0xCE10,0xCF10,0xD030,0xD130,0xD230,0xD330,0xD430,0xD530, + 0xD610,0xD710,0xD810,0xD910,0xDA10,0xDB10,0xDC10,0xDD10, + 0xDE10,0xDF10,0xE030,0xE130,0xE230,0xE330,0xE430,0xE530, + 0xE610,0xE710,0xE810,0xE910,0xEA10,0xEB10,0xEC10,0xED10, + 0xEE10,0xEF10,0xF030,0xF130,0xF230,0xF330,0xF430,0xF530, + 0xF610,0xF710,0xF810,0xF910,0xFA10,0xFB10,0xFC10,0xFD10, + 0xFE10,0xFF10,0x00B0,0x0130,0x0230,0x0330,0x0430,0x0530, + 0x0610,0x0710,0x0810,0x0910,0x0A10,0x0B10,0x0C10,0x0D10, + 0x0E10,0x0F10,0x1030,0x1130,0x1230,0x1330,0x1430,0x1530, + 0x1610,0x1710,0x1810,0x1910,0x1A10,0x1B10,0x1C10,0x1D10, + 0x1E10,0x1F10,0x2030,0x2130,0x2230,0x2330,0x2430,0x2530, + 0x2610,0x2710,0x2810,0x2910,0x2A10,0x2B10,0x2C10,0x2D10, + 0x2E10,0x2F10,0x3030,0x3130,0x3230,0x3330,0x3430,0x3530, + 0x3610,0x3710,0x3810,0x3910,0x3A10,0x3B10,0x3C10,0x3D10, + 0x3E10,0x3F10,0x4030,0x4130,0x4230,0x4330,0x4430,0x4530, + 0x4610,0x4710,0x4810,0x4910,0x4A10,0x4B10,0x4C10,0x4D10, + 0x4E10,0x4F10,0x5030,0x5130,0x5230,0x5330,0x5430,0x5530, + 0x5610,0x5710,0x5810,0x5910,0x5A10,0x5B10,0x5C10,0x5D10, + 0x5E10,0x5F10,0x6030,0x6130,0x6230,0x6330,0x6430,0x6530, + 0x00C0,0x0140,0x0240,0x0340,0x0440,0x0540,0x0640,0x0740, + 0x0840,0x0940,0x0440,0x0540,0x0640,0x0740,0x0840,0x0940, + 0x1040,0x1140,0x1240,0x1340,0x1440,0x1540,0x1640,0x1740, + 0x1840,0x1940,0x1440,0x1540,0x1640,0x1740,0x1840,0x1940, + 0x2040,0x2140,0x2240,0x2340,0x2440,0x2540,0x2640,0x2740, + 0x2840,0x2940,0x2440,0x2540,0x2640,0x2740,0x2840,0x2940, + 0x3040,0x3140,0x3240,0x3340,0x3440,0x3540,0x3640,0x3740, + 0x3840,0x3940,0x3440,0x3540,0x3640,0x3740,0x3840,0x3940, + 0x4040,0x4140,0x4240,0x4340,0x4440,0x4540,0x4640,0x4740, + 0x4840,0x4940,0x4440,0x4540,0x4640,0x4740,0x4840,0x4940, + 0x5040,0x5140,0x5240,0x5340,0x5440,0x5540,0x5640,0x5740, + 0x5840,0x5940,0x5440,0x5540,0x5640,0x5740,0x5840,0x5940, + 0x6040,0x6140,0x6240,0x6340,0x6440,0x6540,0x6640,0x6740, + 0x6840,0x6940,0x6440,0x6540,0x6640,0x6740,0x6840,0x6940, + 0x7040,0x7140,0x7240,0x7340,0x7440,0x7540,0x7640,0x7740, + 0x7840,0x7940,0x7440,0x7540,0x7640,0x7740,0x7840,0x7940, + 0x8040,0x8140,0x8240,0x8340,0x8440,0x8540,0x8640,0x8740, + 0x8840,0x8940,0x8440,0x8540,0x8640,0x8740,0x8840,0x8940, + 0x9040,0x9140,0x9240,0x9340,0x9440,0x9540,0x9640,0x9740, + 0x9840,0x9940,0x3450,0x3550,0x3650,0x3750,0x3850,0x3950, + 0x4050,0x4150,0x4250,0x4350,0x4450,0x4550,0x4650,0x4750, + 0x4850,0x4950,0x4450,0x4550,0x4650,0x4750,0x4850,0x4950, + 0x5050,0x5150,0x5250,0x5350,0x5450,0x5550,0x5650,0x5750, + 0x5850,0x5950,0x5450,0x5550,0x5650,0x5750,0x5850,0x5950, + 0x6050,0x6150,0x6250,0x6350,0x6450,0x6550,0x6650,0x6750, + 0x6850,0x6950,0x6450,0x6550,0x6650,0x6750,0x6850,0x6950, + 0x7050,0x7150,0x7250,0x7350,0x7450,0x7550,0x7650,0x7750, + 0x7850,0x7950,0x7450,0x7550,0x7650,0x7750,0x7850,0x7950, + 0x8050,0x8150,0x8250,0x8350,0x8450,0x8550,0x8650,0x8750, + 0x8850,0x8950,0x8450,0x8550,0x8650,0x8750,0x8850,0x8950, + 0x9050,0x9150,0x9250,0x9350,0x9450,0x9550,0x9650,0x9750, + 0x9850,0x9950,0x9450,0x9550,0x9650,0x9750,0x9850,0x9950, + 0xA050,0xA150,0xA250,0xA350,0xA450,0xA550,0xA650,0xA750, + 0xA850,0xA950,0xA450,0xA550,0xA650,0xA750,0xA850,0xA950, + 0xB050,0xB150,0xB250,0xB350,0xB450,0xB550,0xB650,0xB750, + 0xB850,0xB950,0xB450,0xB550,0xB650,0xB750,0xB850,0xB950, + 0xC050,0xC150,0xC250,0xC350,0xC450,0xC550,0xC650,0xC750, + 0xC850,0xC950,0xC450,0xC550,0xC650,0xC750,0xC850,0xC950, + 0xD050,0xD150,0xD250,0xD350,0xD450,0xD550,0xD650,0xD750, + 0xD850,0xD950,0xD450,0xD550,0xD650,0xD750,0xD850,0xD950, + 0xE050,0xE150,0xE250,0xE350,0xE450,0xE550,0xE650,0xE750, + 0xE850,0xE950,0xE450,0xE550,0xE650,0xE750,0xE850,0xE950, + 0xF050,0xF150,0xF250,0xF350,0xF450,0xF550,0xF650,0xF750, + 0xF850,0xF950,0xF450,0xF550,0xF650,0xF750,0xF850,0xF950, + 0x00D0,0x0150,0x0250,0x0350,0x0450,0x0550,0x0650,0x0750, + 0x0850,0x0950,0x0450,0x0550,0x0650,0x0750,0x0850,0x0950, + 0x1050,0x1150,0x1250,0x1350,0x1450,0x1550,0x1650,0x1750, + 0x1850,0x1950,0x1450,0x1550,0x1650,0x1750,0x1850,0x1950, + 0x2050,0x2150,0x2250,0x2350,0x2450,0x2550,0x2650,0x2750, + 0x2850,0x2950,0x2450,0x2550,0x2650,0x2750,0x2850,0x2950, + 0x3050,0x3150,0x3250,0x3350,0x3450,0x3550,0x3650,0x3750, + 0x3850,0x3950,0x3450,0x3550,0x3650,0x3750,0x3850,0x3950, + 0x4050,0x4150,0x4250,0x4350,0x4450,0x4550,0x4650,0x4750, + 0x4850,0x4950,0x4450,0x4550,0x4650,0x4750,0x4850,0x4950, + 0x5050,0x5150,0x5250,0x5350,0x5450,0x5550,0x5650,0x5750, + 0x5850,0x5950,0x5450,0x5550,0x5650,0x5750,0x5850,0x5950, + 0x6050,0x6150,0x6250,0x6350,0x6450,0x6550,0x6650,0x6750, + 0x6850,0x6950,0x6450,0x6550,0x6650,0x6750,0x6850,0x6950, + 0x7050,0x7150,0x7250,0x7350,0x7450,0x7550,0x7650,0x7750, + 0x7850,0x7950,0x7450,0x7550,0x7650,0x7750,0x7850,0x7950, + 0x8050,0x8150,0x8250,0x8350,0x8450,0x8550,0x8650,0x8750, + 0x8850,0x8950,0x8450,0x8550,0x8650,0x8750,0x8850,0x8950, + 0x9050,0x9150,0x9250,0x9350,0x9450,0x9550,0x9650,0x9750, + 0x9850,0x9950,0x9450,0x9550,0x9650,0x9750,0x9850,0x9950, + 0xFA60,0xFB60,0xFC60,0xFD60,0xFE60,0xFF60,0x00C0,0x0140, + 0x0240,0x0340,0x0440,0x0540,0x0640,0x0740,0x0840,0x0940, + 0x0A60,0x0B60,0x0C60,0x0D60,0x0E60,0x0F60,0x1040,0x1140, + 0x1240,0x1340,0x1440,0x1540,0x1640,0x1740,0x1840,0x1940, + 0x1A60,0x1B60,0x1C60,0x1D60,0x1E60,0x1F60,0x2040,0x2140, + 0x2240,0x2340,0x2440,0x2540,0x2640,0x2740,0x2840,0x2940, + 0x2A60,0x2B60,0x2C60,0x2D60,0x2E60,0x2F60,0x3040,0x3140, + 0x3240,0x3340,0x3440,0x3540,0x3640,0x3740,0x3840,0x3940, + 0x3A60,0x3B60,0x3C60,0x3D60,0x3E60,0x3F60,0x4040,0x4140, + 0x4240,0x4340,0x4440,0x4540,0x4640,0x4740,0x4840,0x4940, + 0x4A60,0x4B60,0x4C60,0x4D60,0x4E60,0x4F60,0x5040,0x5140, + 0x5240,0x5340,0x5440,0x5540,0x5640,0x5740,0x5840,0x5940, + 0x5A60,0x5B60,0x5C60,0x5D60,0x5E60,0x5F60,0x6040,0x6140, + 0x6240,0x6340,0x6440,0x6540,0x6640,0x6740,0x6840,0x6940, + 0x6A60,0x6B60,0x6C60,0x6D60,0x6E60,0x6F60,0x7040,0x7140, + 0x7240,0x7340,0x7440,0x7540,0x7640,0x7740,0x7840,0x7940, + 0x7A60,0x7B60,0x7C60,0x7D60,0x7E60,0x7F60,0x8040,0x8140, + 0x8240,0x8340,0x8440,0x8540,0x8640,0x8740,0x8840,0x8940, + 0x8A60,0x8B60,0x8C60,0x8D60,0x8E60,0x8F60,0x9040,0x9140, + 0x9240,0x9340,0x3450,0x3550,0x3650,0x3750,0x3850,0x3950, + 0x3A70,0x3B70,0x3C70,0x3D70,0x3E70,0x3F70,0x4050,0x4150, + 0x4250,0x4350,0x4450,0x4550,0x4650,0x4750,0x4850,0x4950, + 0x4A70,0x4B70,0x4C70,0x4D70,0x4E70,0x4F70,0x5050,0x5150, + 0x5250,0x5350,0x5450,0x5550,0x5650,0x5750,0x5850,0x5950, + 0x5A70,0x5B70,0x5C70,0x5D70,0x5E70,0x5F70,0x6050,0x6150, + 0x6250,0x6350,0x6450,0x6550,0x6650,0x6750,0x6850,0x6950, + 0x6A70,0x6B70,0x6C70,0x6D70,0x6E70,0x6F70,0x7050,0x7150, + 0x7250,0x7350,0x7450,0x7550,0x7650,0x7750,0x7850,0x7950, + 0x7A70,0x7B70,0x7C70,0x7D70,0x7E70,0x7F70,0x8050,0x8150, + 0x8250,0x8350,0x8450,0x8550,0x8650,0x8750,0x8850,0x8950, + 0x8A70,0x8B70,0x8C70,0x8D70,0x8E70,0x8F70,0x9050,0x9150, + 0x9250,0x9350,0x9450,0x9550,0x9650,0x9750,0x9850,0x9950, + 0x9A70,0x9B70,0x9C70,0x9D70,0x9E70,0x9F70,0xA050,0xA150, + 0xA250,0xA350,0xA450,0xA550,0xA650,0xA750,0xA850,0xA950, + 0xAA70,0xAB70,0xAC70,0xAD70,0xAE70,0xAF70,0xB050,0xB150, + 0xB250,0xB350,0xB450,0xB550,0xB650,0xB750,0xB850,0xB950, + 0xBA70,0xBB70,0xBC70,0xBD70,0xBE70,0xBF70,0xC050,0xC150, + 0xC250,0xC350,0xC450,0xC550,0xC650,0xC750,0xC850,0xC950, + 0xCA70,0xCB70,0xCC70,0xCD70,0xCE70,0xCF70,0xD050,0xD150, + 0xD250,0xD350,0xD450,0xD550,0xD650,0xD750,0xD850,0xD950, + 0xDA70,0xDB70,0xDC70,0xDD70,0xDE70,0xDF70,0xE050,0xE150, + 0xE250,0xE350,0xE450,0xE550,0xE650,0xE750,0xE850,0xE950, + 0xEA70,0xEB70,0xEC70,0xED70,0xEE70,0xEF70,0xF050,0xF150, + 0xF250,0xF350,0xF450,0xF550,0xF650,0xF750,0xF850,0xF950, + 0xFA70,0xFB70,0xFC70,0xFD70,0xFE70,0xFF70,0x00D0,0x0150, + 0x0250,0x0350,0x0450,0x0550,0x0650,0x0750,0x0850,0x0950, + 0x0A70,0x0B70,0x0C70,0x0D70,0x0E70,0x0F70,0x1050,0x1150, + 0x1250,0x1350,0x1450,0x1550,0x1650,0x1750,0x1850,0x1950, + 0x1A70,0x1B70,0x1C70,0x1D70,0x1E70,0x1F70,0x2050,0x2150, + 0x2250,0x2350,0x2450,0x2550,0x2650,0x2750,0x2850,0x2950, + 0x2A70,0x2B70,0x2C70,0x2D70,0x2E70,0x2F70,0x3050,0x3150, + 0x3250,0x3350,0x3450,0x3550,0x3650,0x3750,0x3850,0x3950, + 0x3A70,0x3B70,0x3C70,0x3D70,0x3E70,0x3F70,0x4050,0x4150, + 0x4250,0x4350,0x4450,0x4550,0x4650,0x4750,0x4850,0x4950, + 0x4A70,0x4B70,0x4C70,0x4D70,0x4E70,0x4F70,0x5050,0x5150, + 0x5250,0x5350,0x5450,0x5550,0x5650,0x5750,0x5850,0x5950, + 0x5A70,0x5B70,0x5C70,0x5D70,0x5E70,0x5F70,0x6050,0x6150, + 0x6250,0x6350,0x6450,0x6550,0x6650,0x6750,0x6850,0x6950, + 0x6A70,0x6B70,0x6C70,0x6D70,0x6E70,0x6F70,0x7050,0x7150, + 0x7250,0x7350,0x7450,0x7550,0x7650,0x7750,0x7850,0x7950, + 0x7A70,0x7B70,0x7C70,0x7D70,0x7E70,0x7F70,0x8050,0x8150, + 0x8250,0x8350,0x8450,0x8550,0x8650,0x8750,0x8850,0x8950, + 0x8A70,0x8B70,0x8C70,0x8D70,0x8E70,0x8F70,0x9050,0x9150, + 0x9250,0x9350,0x9450,0x9550,0x9650,0x9750,0x9850,0x9950, +}; + +u8 ZeroTable[] = { + 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0 +}; + +#define GBSAVE_GAME_VERSION_1 1 +#define GBSAVE_GAME_VERSION_2 2 +#define GBSAVE_GAME_VERSION_3 3 +#define GBSAVE_GAME_VERSION_4 4 +#define GBSAVE_GAME_VERSION_5 5 +#define GBSAVE_GAME_VERSION_6 6 +#define GBSAVE_GAME_VERSION_7 7 +#define GBSAVE_GAME_VERSION_8 8 +#define GBSAVE_GAME_VERSION_9 9 +#define GBSAVE_GAME_VERSION_10 10 +#define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_10 + +int inline gbGetValue(int min,int max,int v) +{ + return (int)(min+(float)(max-min)*(2.0*(v/31.0)-(v/31.0)*(v/31.0))); +} + +void gbGenFilter() +{ + for (int r=0;r<32;r++) { + for (int g=0;g<32;g++) { + for (int b=0;b<32;b++) { + int nr=gbGetValue(gbGetValue(4,14,g), + gbGetValue(24,29,g),r)-4; + int ng=gbGetValue(gbGetValue(4+gbGetValue(0,5,r), + 14+gbGetValue(0,3,r),b), + gbGetValue(24+gbGetValue(0,3,r), + 29+gbGetValue(0,1,r),b),g)-4; + int nb=gbGetValue(gbGetValue(4+gbGetValue(0,5,r), + 14+gbGetValue(0,3,r),g), + gbGetValue(24+gbGetValue(0,3,r), + 29+gbGetValue(0,1,r),g),b)-4; + gbColorFilter[(b<<10)|(g<<5)|r]=(nb<<10)|(ng<<5)|nr; + } + } + } +} + +bool gbIsGameboyRom(char * file) +{ + if(strlen(file) > 4) { + char * p = strrchr(file,'.'); + + if(p != NULL) { + if(_stricmp(p, ".gb") == 0) + return true; + if(_stricmp(p, ".gbc") == 0) + return true; + if(_stricmp(p, ".cgb") == 0) + return true; + if(_stricmp(p, ".sgb") == 0) + return true; + } + } + + return false; +} + +void gbCopyMemory(u16 d, u16 s, int count) +{ + while(count) { + gbMemoryMap[d>>12][d & 0x0fff] = gbMemoryMap[s>>12][s & 0x0fff]; + s++; + d++; + count--; + } +} + +void gbDoHdma() +{ + gbCopyMemory(gbHdmaDestination, + gbHdmaSource, + 0x10); + + gbHdmaDestination += 0x10; + gbHdmaSource += 0x10; + + register_HDMA2 += 0x10; + if(register_HDMA2 == 0x00) + register_HDMA1++; + + register_HDMA4 += 0x10; + if(register_HDMA4 == 0x00) + register_HDMA3++; + + if(gbHdmaDestination == 0x96b0) + gbHdmaBytes = gbHdmaBytes; + gbHdmaBytes -= 0x10; + register_HDMA5--; + if(register_HDMA5 == 0xff) + gbHdmaOn = 0; +} + +// fix for Harley and Lego Racers +void gbCompareLYToLYC() +{ + if(register_LY == register_LYC) { + // mark that we have a match + register_STAT |= 4; + + // check if we need an interrupt + if((register_STAT & 0x40) && (register_IE & 2)) + gbInterrupt |= 2; + } else // no match + register_STAT &= 0xfb; +} + +void gbWriteMemory(register u16 address, register u8 value) +{ + if(address < 0x8000) { +#ifndef FINAL_VERSION + if(memorydebug && (address>0x3fff || address < 0x2000)) { + log("Memory register write %04x=%02x PC=%04x\n", + address, + value, + PC.W); + } +#endif + if(mapper) + (*mapper)(address, value); + return; + } + + if(address < 0xa000) { + gbMemoryMap[address>>12][address&0x0fff] = value; + return; + } + + if(address < 0xc000) { +#ifndef FINAL_VERSION + if(memorydebug) { + log("Memory register write %04x=%02x PC=%04x\n", + address, + value, + PC.W); + } +#endif + + if(mapper) + (*mapperRAM)(address, value); + return; + } + + if(address < 0xfe00) { + gbMemoryMap[address>>12][address & 0x0fff] = value; + return; + } + + if(address < 0xff00) { + gbMemory[address] = value; + return; + } + + switch(address & 0x00ff) { + case 0x00: { + gbMemory[0xff00] = ((gbMemory[0xff00] & 0xcf) | + (value & 0x30)); + if(gbSgbMode) { + gbSgbDoBitTransfer(value); + } + + return; + } + + case 0x01: { + gbMemory[0xff01] = value; + return; + } + + // serial control + case 0x02: { + gbSerialOn = (value & 0x80); + gbMemory[0xff02] = value; + if(gbSerialOn) { + gbSerialTicks = GBSERIAL_CLOCK_TICKS; +#ifdef LINK_EMULATION + if(linkConnected) { + if(value & 1) { + linkSendByte(0x100|gbMemory[0xFF01]); + Sleep(5); + } + } +#endif + } + + gbSerialBits = 0; + return; + } + + // DIV register resets on any write + case 0x04: { + register_DIV = 0; + return; + } + case 0x05: + register_TIMA = value; + return; + + case 0x06: + register_TMA = value; + return; + + // TIMER control + case 0x07: { + register_TAC = value; + + gbTimerOn = (value & 4); + gbTimerMode = value & 3; + // register_TIMA = register_TMA; + switch(gbTimerMode) { + case 0: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_0_CLOCK_TICKS; + break; + case 1: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_1_CLOCK_TICKS; + break; + case 2: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_2_CLOCK_TICKS; + break; + case 3: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_3_CLOCK_TICKS; + break; + } + return; + } + + case 0x0f: { + register_IF = value; + gbInterrupt = value; + return; + } + + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + case 0x18: + case 0x19: + case 0x1a: + case 0x1b: + case 0x1c: + case 0x1d: + case 0x1e: + case 0x1f: + case 0x20: + case 0x21: + case 0x22: + case 0x23: + case 0x24: + case 0x25: + case 0x26: + case 0x30: + case 0x31: + case 0x32: + case 0x33: + case 0x34: + case 0x35: + case 0x36: + case 0x37: + case 0x38: + case 0x39: + case 0x3a: + case 0x3b: + case 0x3c: + case 0x3d: + case 0x3e: + case 0x3f: { + SOUND_EVENT(address,value); + return; + } + case 0x40: { + int lcdChange = (register_LCDC & 0x80) ^ (value & 0x80); + + if(lcdChange) { + if(value & 0x80) { + gbLcdTicks = GBLCD_MODE_1_CLOCK_TICKS; + gbLcdMode = 0; + register_STAT &= 0xfc; + register_LY = 0x00; + } else { + gbLcdTicks = 0; + gbLcdMode = 0; + register_STAT &= 0xfc; + register_LY = 0x00; + } + // compareLYToLYC(); + } + // don't draw the window if it was not enabled and not being drawn before + if(!(register_LCDC & 0x20) && (value & 0x20) && gbWindowLine == -1 && + register_LY > register_WY) + gbWindowLine = 144; + + register_LCDC = value; + + return; + } + + // STAT + case 0x41: { + //register_STAT = (register_STAT & 0x87) | + // (value & 0x7c); + register_STAT = (value & 0xf8) | (register_STAT & 0x07); // fix ? + // GB bug from Devrs FAQ + if(!gbCgbMode && (register_LCDC & 0x80) && gbLcdMode < 2) + gbInterrupt |= 2; + return; + } + + // SCY + case 0x42: { + register_SCY = value; + return; + } + + // SCX + case 0x43: { + register_SCX = value; + return; + } + + // LY + case 0x44: { + // read only + return; + } + + // LYC + case 0x45: { + register_LYC = value; + if((register_LCDC & 0x80)) { + gbCompareLYToLYC(); + } + return; + } + + // DMA! + case 0x46: { + int source = value * 0x0100; + + gbCopyMemory(0xfe00, + source, + 0xa0); + register_DMA = value; + return; + } + + // BGP + case 0x47: { + gbBgp[0] = value & 0x03; + gbBgp[1] = (value & 0x0c)>>2; + gbBgp[2] = (value & 0x30)>>4; + gbBgp[3] = (value & 0xc0)>>6; + break; + } + + // OBP0 + case 0x48: { + gbObp0[0] = value & 0x03; + gbObp0[1] = (value & 0x0c)>>2; + gbObp0[2] = (value & 0x30)>>4; + gbObp0[3] = (value & 0xc0)>>6; + break; + } + + // OBP1 + case 0x49: { + gbObp1[0] = value & 0x03; + gbObp1[1] = (value & 0x0c)>>2; + gbObp1[2] = (value & 0x30)>>4; + gbObp1[3] = (value & 0xc0)>>6; + break; + } + + case 0x4a: + register_WY = value; + return; + + case 0x4b: + register_WX = value; + return; + + // KEY1 + case 0x4d: { + if(gbCgbMode) { + gbMemory[0xff4d] = (gbMemory[0xff4d] & 0x80) | (value & 1); + return; + } + } + break; + + // VBK + case 0x4f: { + if(gbCgbMode) { + value = value & 1; + if(value == gbVramBank) + return; + + int vramAddress = value * 0x2000; + gbMemoryMap[0x08] = &gbVram[vramAddress]; + gbMemoryMap[0x09] = &gbVram[vramAddress + 0x1000]; + + gbVramBank = value; + register_VBK = value; + } + return; + } + break; + + // HDMA1 + case 0x51: { + if(gbCgbMode) { + if(value > 0x7f && value < 0xa0) + value = 0; + + gbHdmaSource = (value << 8) | (register_HDMA2 & 0xf0); + + register_HDMA1 = value; + return; + } + } + break; + + // HDMA2 + case 0x52: { + if(gbCgbMode) { + value = value & 0xf0; + + gbHdmaSource = (register_HDMA1 << 8) | (value); + + register_HDMA2 = value; + return; + } + } + break; + + // HDMA3 + case 0x53: { + if(gbCgbMode) { + value = value & 0x1f; + gbHdmaDestination = (value << 8) | (register_HDMA4 & 0xf0); + gbHdmaDestination += 0x8000; + register_HDMA3 = value; + return; + } + } + break; + + // HDMA4 + case 0x54: { + if(gbCgbMode) { + value = value & 0xf0; + gbHdmaDestination = ((register_HDMA3 & 0x1f) << 8) | value; + gbHdmaDestination += 0x8000; + register_HDMA4 = value; + return; + } + } + break; + + // HDMA5 + case 0x55: { + if(gbCgbMode) { + gbHdmaBytes = 16 + (value & 0x7f) * 16; + if(gbHdmaOn) { + if(value & 0x80) { + register_HDMA5 = (value & 0x7f); + } else { + register_HDMA5 = 0xff; + gbHdmaOn = 0; + } + } else { + if(value & 0x80) { + gbHdmaOn = 1; + register_HDMA5 = value & 0x7f; + if(gbLcdMode == 0) + gbDoHdma(); + } else { + // we need to take the time it takes to complete the transfer into + // account... according to GB DEV FAQs, the setup time is the same + // for single and double speed, but the actual transfer takes the + // same time + if(gbSpeed) + gbDmaTicks = 231 + 16 * (value & 0x7f); + else + gbDmaTicks = 231 + 8 * (value & 0x7f); + gbCopyMemory(gbHdmaDestination, + gbHdmaSource, + gbHdmaBytes); + gbHdmaDestination += gbHdmaBytes; + gbHdmaSource += gbHdmaBytes; + + register_HDMA3 = ((gbHdmaDestination - 0x8000) >> 8) & 0x1f; + register_HDMA4 = gbHdmaDestination & 0xf0; + register_HDMA1 = (gbHdmaSource >> 8) & 0xff; + register_HDMA2 = gbHdmaSource & 0xf0; + } + } + return; + } + } + break; + + // BCPS + case 0x68: { + if(gbCgbMode) { + int paletteIndex = (value & 0x3f) >> 1; + int paletteHiLo = (value & 0x01); + + gbMemory[0xff68] = value; + gbMemory[0xff69] = (paletteHiLo ? + (gbPalette[paletteIndex] >> 8) : + (gbPalette[paletteIndex] & 0x00ff)); + return; + } + } + break; + + // BCPD + case 0x69: { + if(gbCgbMode) { + int v = gbMemory[0xff68]; + int paletteIndex = (v & 0x3f) >> 1; + int paletteHiLo = (v & 0x01); + gbMemory[0xff69] = value; + gbPalette[paletteIndex] = (paletteHiLo ? + ((value << 8) | (gbPalette[paletteIndex] & 0xff)) : + ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff; + + if(gbMemory[0xff68] & 0x80) { + int index = ((gbMemory[0xff68] & 0x3f) + 1) & 0x3f; + + gbMemory[0xff68] = (gbMemory[0xff68] & 0x80) | index; + + gbMemory[0xff69] = (index & 1 ? + (gbPalette[index>>1] >> 8) : + (gbPalette[index>>1] & 0x00ff)); + + } + return; + } + } + break; + + // OCPS + case 0x6a: { + if(gbCgbMode) { + int paletteIndex = (value & 0x3f) >> 1; + int paletteHiLo = (value & 0x01); + + paletteIndex += 32; + + gbMemory[0xff6a] = value; + gbMemory[0xff6b] = (paletteHiLo ? + (gbPalette[paletteIndex] >> 8) : + (gbPalette[paletteIndex] & 0x00ff)); + return; + } + } + break; + + // OCPD + case 0x6b: { + if(gbCgbMode) { + int v = gbMemory[0xff6a]; + int paletteIndex = (v & 0x3f) >> 1; + int paletteHiLo = (v & 0x01); + + paletteIndex += 32; + + gbMemory[0xff6b] = value; + gbPalette[paletteIndex] = (paletteHiLo ? + ((value << 8) | (gbPalette[paletteIndex] & 0xff)) : + ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff; + if(gbMemory[0xff6a] & 0x80) { + int index = ((gbMemory[0xff6a] & 0x3f) + 1) & 0x3f; + + gbMemory[0xff6a] = (gbMemory[0xff6a] & 0x80) | index; + + gbMemory[0xff6b] = (index & 1 ? + (gbPalette[(index>>1) + 32] >> 8) : + (gbPalette[(index>>1) + 32] & 0x00ff)); + + } + return; + } + } + break; + + // SVBK + case 0x70: { + if(gbCgbMode) { + value = value & 7; + + int bank = value; + if(value == 0) + bank = 1; + + if(bank == gbWramBank) + return; + + int wramAddress = bank * 0x1000; + gbMemoryMap[0x0d] = &gbWram[wramAddress]; + + gbWramBank = bank; + register_SVBK = value; + return; + } + } + break; + + case 0xff: { + register_IE = value; + register_IF &= value; + return; + } + } + + gbMemory[address] = value; +} + +u8 gbReadOpcode(register u16 address) +{ + if(gbCheatMap[address]) + return gbCheatRead(address); + + switch(address & 0xf000) { + case 0x0a: + case 0x0b: + if(mapperReadRAM) + return mapperReadRAM(address); + break; + case 0x0f: + if(address > 0xff00) { + if (address >= 0xFF10 && address <= 0xFF3F) + { + return gbSoundRead(address); + } + switch(address & 0x00ff) { + case 0x04: + return register_DIV; + case 0x05: + return register_TIMA; + case 0x06: + return register_TMA; + case 0x07: + return (0xf8 | register_TAC); + case 0x0f: + return (0xe0 | register_IF); + case 0x40: + return register_LCDC; + case 0x41: + return (0x80 | register_STAT); + case 0x42: + return register_SCY; + case 0x43: + return register_SCX; + case 0x44: + return register_LY; + case 0x45: + return register_LYC; + case 0x46: + return register_DMA; + case 0x4a: + return register_WY; + case 0x4b: + return register_WX; + case 0x4f: + return (0xfe | register_VBK); + case 0x51: + return register_HDMA1; + case 0x52: + return register_HDMA2; + case 0x53: + return register_HDMA3; + case 0x54: + return register_HDMA4; + case 0x55: + return register_HDMA5; + case 0x70: + return (0xf8 | register_SVBK); + case 0xff: + return register_IE; + } + } + break; + } + return gbMemoryMap[address>>12][address & 0x0fff]; +} + +u8 gbReadMemory(register u16 address) +{ + if(gbCheatMap[address]) + return gbCheatRead(address); + + if(address < 0xa000) + return gbMemoryMap[address>>12][address&0x0fff]; + + if(address < 0xc000) { +#ifndef FINAL_VERSION + if(memorydebug) { + log("Memory register read %04x PC=%04x\n", + address, + PC.W); + } +#endif + + if(mapperReadRAM) + return mapperReadRAM(address); + return gbMemoryMap[address>>12][address & 0x0fff]; + } + + if(address >= 0xff00) { + + if (address >= 0xFF10 && address <= 0xFF3F) { + return gbSoundRead(address); + } + + switch(address & 0x00ff) { + case 0x00: + { + if(gbSgbMode) { + gbSgbReadingController |= 4; + gbSgbResetPacketState(); + } + + int b = gbMemory[0xff00]; + + if((b & 0x30) == 0x20) { + b &= 0xf0; + + int joy = 0; + if(gbSgbMode && gbSgbMultiplayer) { + switch(gbSgbNextController) { + case 0x0f: + joy = 0; + break; + case 0x0e: + joy = 1; + break; + case 0x0d: + joy = 2; + break; + case 0x0c: + joy = 3; + break; + default: + joy = 0; + } + } + int joystate = gbJoymask[joy]; + if(!(joystate & 128)) + b |= 0x08; + if(!(joystate & 64)) + b |= 0x04; + if(!(joystate & 32)) + b |= 0x02; + if(!(joystate & 16)) + b |= 0x01; + + gbMemory[0xff00] = b; + } else if((b & 0x30) == 0x10) { + b &= 0xf0; + + int joy = 0; + if(gbSgbMode && gbSgbMultiplayer) { + switch(gbSgbNextController) { + case 0x0f: + joy = 0; + break; + case 0x0e: + joy = 1; + break; + case 0x0d: + joy = 2; + break; + case 0x0c: + joy = 3; + break; + default: + joy = 0; + } + } + int joystate = gbJoymask[joy]; + if(!(joystate & 8)) + b |= 0x08; + if(!(joystate & 4)) + b |= 0x04; + if(!(joystate & 2)) + b |= 0x02; + if(!(joystate & 1)) + b |= 0x01; + + gbMemory[0xff00] = b; + } else { + if(gbSgbMode && gbSgbMultiplayer) { + gbMemory[0xff00] = 0xf0 | gbSgbNextController; + } else { + gbMemory[0xff00] = 0xff; + } + } + } + return gbMemory[0xff00]; + break; + case 0x01: + return gbMemory[0xff01]; + case 0x04: + return register_DIV; + case 0x05: + return register_TIMA; + case 0x06: + return register_TMA; + case 0x07: + return (0xf8 | register_TAC); + case 0x0f: + return (0xe0 | register_IF); + case 0x40: + return register_LCDC; + case 0x41: + return (0x80 | register_STAT); + case 0x42: + return register_SCY; + case 0x43: + return register_SCX; + case 0x44: + return register_LY; + case 0x45: + return register_LYC; + case 0x46: + return register_DMA; + case 0x4a: + return register_WY; + case 0x4b: + return register_WX; + case 0x4f: + return (0xfe | register_VBK); + case 0x51: + return register_HDMA1; + case 0x52: + return register_HDMA2; + case 0x53: + return register_HDMA3; + case 0x54: + return register_HDMA4; + case 0x55: + return register_HDMA5; + case 0x70: + return (0xf8 | register_SVBK); + case 0xff: + return register_IE; + } + } + + return gbMemoryMap[address>>12][address & 0x0fff]; +} + +void gbVblank_interrupt() +{ + if(IFF & 0x80) { + PC.W++; + IFF &= 0x7f; + } + gbInterrupt &= 0xfe; + + IFF &= 0x7e; + register_IF &= 0xfe; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + PC.W = 0x40; +} + +void gbLcd_interrupt() +{ + if(IFF & 0x80) { + PC.W++; + IFF &= 0x7f; + } + gbInterrupt &= 0xfd; + IFF &= 0x7e; + register_IF &= 0xfd; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x48; +} + +void gbTimer_interrupt() +{ + if(IFF & 0x80) { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xfb; + register_IF &= 0xfb; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x50; +} + +void gbSerial_interrupt() +{ + if(IFF & 0x80) { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xf7; + register_IF &= 0xf7; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x58; +} + +void gbJoypad_interrupt() +{ + if(IFF & 0x80) { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xef; + register_IF &= 0xef; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x60; +} + +void gbSpeedSwitch() +{ + if(gbSpeed == 0) { + gbSpeed = 1; + GBLCD_MODE_0_CLOCK_TICKS = 51 * 2; //127; //51 * 2; + GBLCD_MODE_1_CLOCK_TICKS = 1140 * 2; + GBLCD_MODE_2_CLOCK_TICKS = 20 * 2; //52; //20 * 2; + GBLCD_MODE_3_CLOCK_TICKS = 43 * 2; //99; //43 * 2; + GBDIV_CLOCK_TICKS = 64 * 2; + GBLY_INCREMENT_CLOCK_TICKS = 114 * 2; + GBTIMER_MODE_0_CLOCK_TICKS = 256; //256*2; + GBTIMER_MODE_1_CLOCK_TICKS = 4; //4*2; + GBTIMER_MODE_2_CLOCK_TICKS = 16; //16*2; + GBTIMER_MODE_3_CLOCK_TICKS = 64; //64*2; + GBSERIAL_CLOCK_TICKS = 128 * 2; + gbDivTicks *= 2; + gbLcdTicks *= 2; + gbLcdLYIncrementTicks *= 2; + // timerTicks *= 2; + // timerClockTicks *= 2; + gbSerialTicks *= 2; + SOUND_CLOCK_TICKS = soundQuality * 24 * 2; + soundTicks *= 2; + // synchronizeTicks *= 2; + // SYNCHRONIZE_CLOCK_TICKS *= 2; + } else { + gbSpeed = 0; + GBLCD_MODE_0_CLOCK_TICKS = 51; + GBLCD_MODE_1_CLOCK_TICKS = 1140; + GBLCD_MODE_2_CLOCK_TICKS = 20; + GBLCD_MODE_3_CLOCK_TICKS = 43; + GBDIV_CLOCK_TICKS = 64; + GBLY_INCREMENT_CLOCK_TICKS = 114; + GBTIMER_MODE_0_CLOCK_TICKS = 256; + GBTIMER_MODE_1_CLOCK_TICKS = 4; + GBTIMER_MODE_2_CLOCK_TICKS = 16; + GBTIMER_MODE_3_CLOCK_TICKS = 64; + GBSERIAL_CLOCK_TICKS = 128; + gbDivTicks /= 2; + gbLcdTicks /= 2; + gbLcdLYIncrementTicks /= 2; + // timerTicks /= 2; + // timerClockTicks /= 2; + gbSerialTicks /= 2; + SOUND_CLOCK_TICKS = soundQuality * 24; + soundTicks /= 2; + // synchronizeTicks /= 2; + // SYNCHRONIZE_CLOCK_TICKS /= 2; + } +} + +void gbReset() +{ + SP.W = 0xfffe; + AF.W = 0x01b0; + BC.W = 0x0013; + DE.W = 0x00d8; + HL.W = 0x014d; + PC.W = 0x0100; + IFF = 0; + gbInterrupt = 1; + gbInterruptWait = 0; + + register_DIV = 0; + register_TIMA = 0; + register_TMA = 0; + register_TAC = 0; + register_IF = 1; + register_LCDC = 0x91; + register_STAT = 0; + register_SCY = 0; + register_SCX = 0; + register_LY = 0; + register_LYC = 0; + register_DMA = 0; + register_WY = 0; + register_WX = 0; + register_VBK = 0; + register_HDMA1 = 0; + register_HDMA2 = 0; + register_HDMA3 = 0; + register_HDMA4 = 0; + register_HDMA5 = 0; + register_SVBK = 0; + register_IE = 0; + + if(gbCgbMode) { + if(gbSgbMode) { + if(gbEmulatorType == 5) + AF.W = 0xffb0; + else + AF.W = 0x01b0; + BC.W = 0x0013; + DE.W = 0x00d8; + HL.W = 0x014d; + for(int i = 0; i < 8; i++) + gbPalette[i] = systemGbPalette[gbPaletteOption*8+i]; + } else { + AF.W = 0x11b0; + BC.W = 0x0000; + DE.W = 0xff56; + HL.W = 0x000d; + } + if(gbEmulatorType == 4) + BC.B.B1 |= 0x01; + + register_HDMA5 = 0xff; + gbMemory[0xff68] = 0xc0; + gbMemory[0xff6a] = 0xc0; + } else { + for(int i = 0; i < 8; i++) + gbPalette[i] = systemGbPalette[gbPaletteOption*8+i]; + } + + if(gbSpeed) { + gbSpeedSwitch(); + gbMemory[0xff4d] = 0; + } + + gbDivTicks = GBDIV_CLOCK_TICKS; + gbLcdMode = 2; + gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS; + gbLcdLYIncrementTicks = 0; + gbTimerTicks = 0; + gbTimerClockTicks = 0; + gbSerialTicks = 0; + gbSerialBits = 0; + gbSerialOn = 0; + gbWindowLine = -1; + gbTimerOn = 0; + gbTimerMode = 0; + // gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS; + gbSpeed = 0; + gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0; + + if(gbCgbMode) { + gbSpeed = 0; + gbHdmaOn = 0; + gbHdmaSource = 0x0000; + gbHdmaDestination = 0x8000; + gbVramBank = 0; + gbWramBank = 1; + register_LY = 0x90; + gbLcdMode = 1; + for(int i = 0; i < 64; i++) + gbPalette[i] = 0x7fff; + } + + if(gbSgbMode) { + gbSgbReset(); + } + + for(int i =0; i < 4; i++) + gbBgp[i] = gbObp0[i] = gbObp1[i] = i; + + memset(&gbDataMBC1,0, sizeof(gbDataMBC1)); + gbDataMBC1.mapperROMBank = 1; + + gbDataMBC2.mapperRAMEnable = 0; + gbDataMBC2.mapperROMBank = 1; + + memset(&gbDataMBC3,0, 6 * sizeof(int)); + gbDataMBC3.mapperROMBank = 1; + + memset(&gbDataMBC5, 0, sizeof(gbDataMBC5)); + gbDataMBC5.mapperROMBank = 1; + + memset(&gbDataHuC1, 0, sizeof(gbDataHuC1)); + gbDataHuC1.mapperROMBank = 1; + + memset(&gbDataHuC3, 0, sizeof(gbDataHuC3)); + gbDataHuC3.mapperROMBank = 1; + + gbMemoryMap[0x00] = &gbRom[0x0000]; + gbMemoryMap[0x01] = &gbRom[0x1000]; + gbMemoryMap[0x02] = &gbRom[0x2000]; + gbMemoryMap[0x03] = &gbRom[0x3000]; + gbMemoryMap[0x04] = &gbRom[0x4000]; + gbMemoryMap[0x05] = &gbRom[0x5000]; + gbMemoryMap[0x06] = &gbRom[0x6000]; + gbMemoryMap[0x07] = &gbRom[0x7000]; + if(gbCgbMode) { + gbMemoryMap[0x08] = &gbVram[0x0000]; + gbMemoryMap[0x09] = &gbVram[0x1000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbWram[0x1000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + } else { + gbMemoryMap[0x08] = &gbMemory[0x8000]; + gbMemoryMap[0x09] = &gbMemory[0x9000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbMemory[0xd000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + } + + if(gbRam) { + gbMemoryMap[0x0a] = &gbRam[0x0000]; + gbMemoryMap[0x0b] = &gbRam[0x1000]; + } + + gbSoundReset(); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + gbLastTime = systemGetClock(); + gbFrameCount = 0; +} + +void gbWriteSaveMBC1(const char * name) +{ + FILE *gzFile = fopen(name,"wb"); + + if(gzFile == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + fclose(gzFile); +} + +void gbWriteSaveMBC2(const char * name) +{ + FILE *file = fopen(name, "wb"); + + if(file == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(&gbMemory[0xa000], + 1, + 256, + file); + + fclose(file); +} + +void gbWriteSaveMBC3(const char * name, bool extendedSave) +{ + FILE *gzFile = fopen(name,"wb"); + + if(gzFile == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + if(extendedSave) + fwrite(&gbDataMBC3.mapperSeconds, + 1, + 10*sizeof(int) + sizeof(time_t), + gzFile); + + fclose(gzFile); +} + +void gbWriteSaveMBC5(const char * name) +{ + FILE *gzFile = fopen(name,"wb"); + + if(gzFile == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + fclose(gzFile); +} + +void gbWriteSaveMBC7(const char * name) +{ + FILE *file = fopen(name, "wb"); + + if(file == NULL) { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(&gbMemory[0xa000], + 1, + 256, + file); + + fclose(file); +} + +bool gbReadSaveMBC1(const char * name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if(gzFile == NULL) { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + if(read != gbRamSize) { + systemMessage(MSG_FAILED_TO_READ_SGM, N_("Failed to read complete save game %s (%d)"), name, read); + gzclose(gzFile); + return false; + } + + gzclose(gzFile); + return true; +} + +bool gbReadSaveMBC2(const char * name) +{ + FILE *file = fopen(name, "rb"); + + if(file == NULL) { + return false; + } + + int read = fread(&gbMemory[0xa000], + 1, + 256, + file); + + if(read != 256) { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + fclose(file); + return false; + } + + fclose(file); + return true; +} + +bool gbReadSaveMBC3(const char * name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if(gzFile == NULL) { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + bool res = true; + + if(read != gbRamSize) { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + } else { + read = gzread(gzFile, + &gbDataMBC3.mapperSeconds, + sizeof(int)*10 + sizeof(time_t)); + + if(read != (sizeof(int)*10 + sizeof(time_t)) && read != 0) { + systemMessage(MSG_FAILED_TO_READ_RTC, + N_("Failed to read RTC from save game %s (continuing)"), + name); + res = false; + } + } + + gzclose(gzFile); + return res; +} + +bool gbReadSaveMBC5(const char * name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if(gzFile == NULL) { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + if(read != gbRamSize) { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + gzclose(gzFile); + return false; + } + + gzclose(gzFile); + return true; +} + +bool gbReadSaveMBC7(const char * name) +{ + FILE *file = fopen(name, "rb"); + + if(file == NULL) { + return false; + } + + int read = fread(&gbMemory[0xa000], + 1, + 256, + file); + + if(read != 256) { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + fclose(file); + return false; + } + + fclose(file); + return true; +} + +void gbInit() +{ + gbGenFilter(); + gbSgbInit(); + + gbMemory = (u8 *)malloc(65536); + memset(gbMemory,0, 65536); + + pix = (u8 *)calloc(1,4*257*226); + + gbLineBuffer = (u16 *)malloc(160 * sizeof(u16)); +} + +bool gbWriteBatteryFile(const char *file, bool extendedSave) +{ + if(gbBattery) { + int type = gbRom[0x147]; + + switch(type) { + case 0x03: + gbWriteSaveMBC1(file); + break; + case 0x06: + gbWriteSaveMBC2(file); + break; + case 0x0f: + case 0x10: + case 0x13: + gbWriteSaveMBC3(file, extendedSave); + break; + case 0x1b: + case 0x1e: + gbWriteSaveMBC5(file); + break; + case 0x22: + gbWriteSaveMBC7(file); + break; + case 0xff: + gbWriteSaveMBC1(file); + break; + } + } + return true; +} + +bool gbWriteBatteryFile(const char *file) +{ + gbWriteBatteryFile(file, true); + return true; +} + +bool gbReadBatteryFile(const char *file) +{ + bool res = false; + if(gbBattery) { + int type = gbRom[0x147]; + + switch(type) { + case 0x03: + res = gbReadSaveMBC1(file); + break; + case 0x06: + res = gbReadSaveMBC2(file); + break; + case 0x0f: + case 0x10: + case 0x13: + if(!gbReadSaveMBC3(file)) { + time(&gbDataMBC3.mapperLastTime); + struct tm *lt; + lt = localtime(&gbDataMBC3.mapperLastTime); + gbDataMBC3.mapperSeconds = lt->tm_sec; + gbDataMBC3.mapperMinutes = lt->tm_min; + gbDataMBC3.mapperHours = lt->tm_hour; + gbDataMBC3.mapperDays = lt->tm_yday & 255; + gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) | + (lt->tm_yday > 255 ? 1: 0); + res = false; + break; + } + res = true; + break; + case 0x1b: + case 0x1e: + res = gbReadSaveMBC5(file); + break; + case 0x22: + res = gbReadSaveMBC7(file); + case 0xff: + res = gbReadSaveMBC1(file); + break; + } + } + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + return res; +} + +bool gbReadGSASnapshot(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if(!file) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + // long size = ftell(file); + fseek(file, 0x4, SEEK_SET); + char buffer[16]; + char buffer2[16]; + fread(buffer, 1, 15, file); + buffer[15] = 0; + memcpy(buffer2, &gbRom[0x134], 15); + buffer2[15] = 0; + if(memcmp(buffer, buffer2, 15)) { + systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR, + N_("Cannot import snapshot for %s. Current game is %s"), + buffer, + buffer2); + fclose(file); + return false; + } + fseek(file, 0x13, SEEK_SET); + int read = 0; + int toRead = 0; + switch(gbRom[0x147]) { + case 0x03: + case 0x0f: + case 0x10: + case 0x13: + case 0x1b: + case 0x1e: + case 0xff: + read = fread(gbRam, 1, gbRamSize, file); + toRead = gbRamSize; + break; + case 0x06: + case 0x22: + read = fread(&gbMemory[0xa000],1,256,file); + toRead = 256; + break; + default: + systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE, + N_("Unsupported snapshot file %s"), + fileName); + fclose(file); + return false; + } + fclose(file); + gbReset(); + return true; +} + +variable_desc gbSaveGameStruct[] = { + { &PC.W, sizeof(u16) }, + { &SP.W, sizeof(u16) }, + { &AF.W, sizeof(u16) }, + { &BC.W, sizeof(u16) }, + { &DE.W, sizeof(u16) }, + { &HL.W, sizeof(u16) }, + { &IFF, sizeof(u8) }, + { &GBLCD_MODE_0_CLOCK_TICKS, sizeof(int) }, + { &GBLCD_MODE_1_CLOCK_TICKS, sizeof(int) }, + { &GBLCD_MODE_2_CLOCK_TICKS, sizeof(int) }, + { &GBLCD_MODE_3_CLOCK_TICKS, sizeof(int) }, + { &GBDIV_CLOCK_TICKS, sizeof(int) }, + { &GBLY_INCREMENT_CLOCK_TICKS, sizeof(int) }, + { &GBTIMER_MODE_0_CLOCK_TICKS, sizeof(int) }, + { &GBTIMER_MODE_1_CLOCK_TICKS, sizeof(int) }, + { &GBTIMER_MODE_2_CLOCK_TICKS, sizeof(int) }, + { &GBTIMER_MODE_3_CLOCK_TICKS, sizeof(int) }, + { &GBSERIAL_CLOCK_TICKS, sizeof(int) }, + { &GBSYNCHRONIZE_CLOCK_TICKS, sizeof(int) }, + { &gbDivTicks, sizeof(int) }, + { &gbLcdMode, sizeof(int) }, + { &gbLcdTicks, sizeof(int) }, + { &gbLcdLYIncrementTicks, sizeof(int) }, + { &gbTimerTicks, sizeof(int) }, + { &gbTimerClockTicks, sizeof(int) }, + { &gbSerialTicks, sizeof(int) }, + { &gbSerialBits, sizeof(int) }, + { &gbInterrupt, sizeof(int) }, + { &gbInterruptWait, sizeof(int) }, + { &gbSynchronizeTicks, sizeof(int) }, + { &gbTimerOn, sizeof(int) }, + { &gbTimerMode, sizeof(int) }, + { &gbSerialOn, sizeof(int) }, + { &gbWindowLine, sizeof(int) }, + { &gbCgbMode, sizeof(int) }, + { &gbVramBank, sizeof(int) }, + { &gbWramBank, sizeof(int) }, + { &gbHdmaSource, sizeof(int) }, + { &gbHdmaDestination, sizeof(int) }, + { &gbHdmaBytes, sizeof(int) }, + { &gbHdmaOn, sizeof(int) }, + { &gbSpeed, sizeof(int) }, + { &gbSgbMode, sizeof(int) }, + { ®ister_DIV, sizeof(u8) }, + { ®ister_TIMA, sizeof(u8) }, + { ®ister_TMA, sizeof(u8) }, + { ®ister_TAC, sizeof(u8) }, + { ®ister_IF, sizeof(u8) }, + { ®ister_LCDC, sizeof(u8) }, + { ®ister_STAT, sizeof(u8) }, + { ®ister_SCY, sizeof(u8) }, + { ®ister_SCX, sizeof(u8) }, + { ®ister_LY, sizeof(u8) }, + { ®ister_LYC, sizeof(u8) }, + { ®ister_DMA, sizeof(u8) }, + { ®ister_WY, sizeof(u8) }, + { ®ister_WX, sizeof(u8) }, + { ®ister_VBK, sizeof(u8) }, + { ®ister_HDMA1, sizeof(u8) }, + { ®ister_HDMA2, sizeof(u8) }, + { ®ister_HDMA3, sizeof(u8) }, + { ®ister_HDMA4, sizeof(u8) }, + { ®ister_HDMA5, sizeof(u8) }, + { ®ister_SVBK, sizeof(u8) }, + { ®ister_IE , sizeof(u8) }, + { &gbBgp[0], sizeof(u8) }, + { &gbBgp[1], sizeof(u8) }, + { &gbBgp[2], sizeof(u8) }, + { &gbBgp[3], sizeof(u8) }, + { &gbObp0[0], sizeof(u8) }, + { &gbObp0[1], sizeof(u8) }, + { &gbObp0[2], sizeof(u8) }, + { &gbObp0[3], sizeof(u8) }, + { &gbObp1[0], sizeof(u8) }, + { &gbObp1[1], sizeof(u8) }, + { &gbObp1[2], sizeof(u8) }, + { &gbObp1[3], sizeof(u8) }, + { NULL, 0 } +}; + + +static bool gbWriteSaveState(gzFile gzFile) +{ + utilWriteInt(gzFile, GBSAVE_GAME_VERSION); + + utilGzWrite(gzFile, &gbRom[0x134], 15); + + utilWriteData(gzFile, gbSaveGameStruct); + + utilGzWrite(gzFile, &IFF, 2); + + if(gbSgbMode) { + gbSgbSaveGame(gzFile); + } + + utilGzWrite(gzFile, &gbDataMBC1, sizeof(gbDataMBC1)); + utilGzWrite(gzFile, &gbDataMBC2, sizeof(gbDataMBC2)); + utilGzWrite(gzFile, &gbDataMBC3, sizeof(gbDataMBC3)); + utilGzWrite(gzFile, &gbDataMBC5, sizeof(gbDataMBC5)); + utilGzWrite(gzFile, &gbDataHuC1, sizeof(gbDataHuC1)); + utilGzWrite(gzFile, &gbDataHuC3, sizeof(gbDataHuC3)); + + // not saved anymore + // gzwrite(gzFile, pix, 256*224*sizeof(u16)); + + utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16)); + // todo: remove + utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16)); + + for (int i = 0xFF10; i <= 0xFF3F; i++) gbMemory[i] = gbReadMemory(i); + + utilGzWrite(gzFile, &gbMemory[0x8000], 0x8000); + + if(gbRamSize && gbRam) { + utilGzWrite(gzFile, gbRam, gbRamSize); + } + + if(gbCgbMode) { + utilGzWrite(gzFile, gbVram, 0x4000); + utilGzWrite(gzFile, gbWram, 0x8000); + } + + gbSoundSaveGame(gzFile); + + gbCheatsSaveGame(gzFile); + + return true; +} + +bool gbWriteMemSaveState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "w"); + + if(gzFile == NULL) { + return false; + } + + bool res = gbWriteSaveState(gzFile); + + long pos = utilGzMemTell(gzFile)+8; + + if(pos >= (available)) + res = false; + + utilGzClose(gzFile); + + return res; +} + +bool gbWriteSaveState(const char *name) +{ + gzFile gzFile = utilGzOpen(name,"wb"); + + if(gzFile == NULL) + return false; + + bool res = gbWriteSaveState(gzFile); + + utilGzClose(gzFile); + return res; +} + +static bool gbReadSaveState(gzFile gzFile) +{ + int version = utilReadInt(gzFile); + + if(version > GBSAVE_GAME_VERSION || version < 0) { + systemMessage(MSG_UNSUPPORTED_VB_SGM, + N_("Unsupported VisualBoy save game version %d"), version); + return false; + } + + u8 romname[20]; + + utilGzRead(gzFile, romname, 15); + + if(memcmp(&gbRom[0x134], romname, 15) != 0) { + systemMessage(MSG_CANNOT_LOAD_SGM_FOR, + N_("Cannot load save game for %s. Playing %s"), + romname, &gbRom[0x134]); + return false; + } + + utilReadData(gzFile, gbSaveGameStruct); + + if(version >= GBSAVE_GAME_VERSION_7) { + utilGzRead(gzFile, &IFF, 2); + } + + if(gbSgbMode) { + gbSgbReadGame(gzFile, version); + } else { + gbSgbMask = 0; // loading a game at the wrong time causes no display + } + + utilGzRead(gzFile, &gbDataMBC1, sizeof(gbDataMBC1)); + utilGzRead(gzFile, &gbDataMBC2, sizeof(gbDataMBC2)); + if(version < GBSAVE_GAME_VERSION_4) + // prior to version 4, there was no adjustment for the time the game + // was last played, so we have less to read. This needs update if the + // structure changes again. + utilGzRead(gzFile, &gbDataMBC3, sizeof(gbDataMBC3)-sizeof(time_t)); + else + utilGzRead(gzFile, &gbDataMBC3, sizeof(gbDataMBC3)); + utilGzRead(gzFile, &gbDataMBC5, sizeof(gbDataMBC5)); + utilGzRead(gzFile, &gbDataHuC1, sizeof(gbDataHuC1)); + utilGzRead(gzFile, &gbDataHuC3, sizeof(gbDataHuC3)); + + if(version < GBSAVE_GAME_VERSION_5) { + utilGzRead(gzFile, pix, 256*224*sizeof(u16)); + } + memset(pix, 0, 257*226*sizeof(u32)); + + if(version < GBSAVE_GAME_VERSION_6) { + utilGzRead(gzFile, gbPalette, 64 * sizeof(u16)); + } else + utilGzRead(gzFile, gbPalette, 128 * sizeof(u16)); + + // todo: remove + utilGzRead(gzFile, gbPalette, 128 * sizeof(u16)); + + if(version < GBSAVE_GAME_VERSION_10) { + if(!gbCgbMode && !gbSgbMode) { + for(int i = 0; i < 8; i++) + gbPalette[i] = systemGbPalette[gbPaletteOption*8+i]; + } + } + + utilGzRead(gzFile, &gbMemory[0x8000], 0x8000); + + for (int i = 0xFF10; i <= 0xFF3F; i++) gbSoundEvent(i, gbMemory[i]); + + if(gbRamSize && gbRam) { + utilGzRead(gzFile, gbRam, gbRamSize); + } + + gbMemoryMap[0x00] = &gbRom[0x0000]; + gbMemoryMap[0x01] = &gbRom[0x1000]; + gbMemoryMap[0x02] = &gbRom[0x2000]; + gbMemoryMap[0x03] = &gbRom[0x3000]; + gbMemoryMap[0x04] = &gbRom[0x4000]; + gbMemoryMap[0x05] = &gbRom[0x5000]; + gbMemoryMap[0x06] = &gbRom[0x6000]; + gbMemoryMap[0x07] = &gbRom[0x7000]; + gbMemoryMap[0x08] = &gbMemory[0x8000]; + gbMemoryMap[0x09] = &gbMemory[0x9000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbMemory[0xd000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + + int type = gbRom[0x147]; + + switch(type) { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + // MBC 1 + memoryUpdateMapMBC1(); + break; + case 0x05: + case 0x06: + // MBC2 + memoryUpdateMapMBC2(); + break; + case 0x0f: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + // MBC 3 + memoryUpdateMapMBC3(); + break; + case 0x19: + case 0x1a: + case 0x1b: + // MBC5 + memoryUpdateMapMBC5(); + break; + case 0x1c: + case 0x1d: + case 0x1e: + // MBC 5 Rumble + memoryUpdateMapMBC5(); + break; + case 0x22: + // MBC 7 + memoryUpdateMapMBC7(); + break; + case 0xfe: + // HuC3 + memoryUpdateMapHuC3(); + break; + case 0xff: + // HuC1 + memoryUpdateMapHuC1(); + break; + } + + if(gbCgbMode) { + utilGzRead(gzFile, gbVram, 0x4000); + utilGzRead(gzFile, gbWram, 0x8000); + + int value = register_SVBK; + if(value == 0) + value = 1; + + gbMemoryMap[0x08] = &gbVram[register_VBK * 0x2000]; + gbMemoryMap[0x09] = &gbVram[register_VBK * 0x2000 + 0x1000]; + gbMemoryMap[0x0d] = &gbWram[value * 0x1000]; + } + + gbSoundReadGame(version, gzFile); + + if(gbBorderOn) { + gbSgbRenderBorder(); + } + + systemDrawScreen(); + + if(version > GBSAVE_GAME_VERSION_1) + gbCheatsReadGame(gzFile, version); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + return true; +} + +bool gbReadMemSaveState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "r"); + + bool res = gbReadSaveState(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool gbReadSaveState(const char *name) +{ + gzFile gzFile = utilGzOpen(name,"rb"); + + if(gzFile == NULL) { + return false; + } + + bool res = gbReadSaveState(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool gbWritePNGFile(const char *fileName) +{ + if(gbBorderOn) + return utilWritePNGFile(fileName, 256, 224, pix); + return utilWritePNGFile(fileName, 160, 144, pix); +} + +bool gbWriteBMPFile(const char *fileName) +{ + if(gbBorderOn) + return utilWriteBMPFile(fileName, 256, 224, pix); + return utilWriteBMPFile(fileName, 160, 144, pix); +} + +void gbCleanUp() +{ + if(gbRam != NULL) { + free(gbRam); + gbRam = NULL; + } + + if(gbRom != NULL) { + free(gbRom); + gbRom = NULL; + } + + if(gbMemory != NULL) { + free(gbMemory); + gbMemory = NULL; + } + + if(gbLineBuffer != NULL) { + free(gbLineBuffer); + gbLineBuffer = NULL; + } + + if(pix != NULL) { + free(pix); + pix = NULL; + } + + gbSgbShutdown(); + + if(gbVram != NULL) { + free(gbVram); + gbVram = NULL; + } + + if(gbWram != NULL) { + free(gbWram); + gbWram = NULL; + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; +} + +bool gbLoadRom(const char *szFile) +{ + int size = 0; + + if(gbRom != NULL) { + gbCleanUp(); + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + gbRom = utilLoad(szFile, + utilIsGBImage, + NULL, + size); + if(!gbRom) + return false; + + gbRomSize = size; + + return gbUpdateSizes(); +} + +bool gbUpdateSizes() +{ + if(gbRom[0x148] > 8) { + systemMessage(MSG_UNSUPPORTED_ROM_SIZE, + N_("Unsupported rom size %02x"), gbRom[0x148]); + return false; + } + + if(gbRomSize < gbRomSizes[gbRom[0x148]]) { + gbRom = (u8 *)realloc(gbRom, gbRomSizes[gbRom[0x148]]); + } + gbRomSize = gbRomSizes[gbRom[0x148]]; + gbRomSizeMask = gbRomSizesMasks[gbRom[0x148]]; + + if(gbRom[0x149] > 5) { + systemMessage(MSG_UNSUPPORTED_RAM_SIZE, + N_("Unsupported ram size %02x"), gbRom[0x149]); + return false; + } + + gbRamSize = gbRamSizes[gbRom[0x149]]; + gbRamSizeMask = gbRamSizesMasks[gbRom[0x149]]; + + if(gbRamSize) { + gbRam = (u8 *)malloc(gbRamSize); + memset(gbRam, 0xFF, gbRamSize); + } + + int type = gbRom[0x147]; + + mapperReadRAM = NULL; + + switch(type) { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + // MBC 1 + mapper = mapperMBC1ROM; + mapperRAM = mapperMBC1RAM; + break; + case 0x05: + case 0x06: + // MBC2 + mapper = mapperMBC2ROM; + mapperRAM = mapperMBC2RAM; + gbRamSize = 0x200; + gbRamSizeMask = 0x1ff; + break; + case 0x0f: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + // MBC 3 + mapper = mapperMBC3ROM; + mapperRAM = mapperMBC3RAM; + mapperReadRAM = mapperMBC3ReadRAM; + break; + case 0x19: + case 0x1a: + case 0x1b: + // MBC5 + mapper = mapperMBC5ROM; + mapperRAM = mapperMBC5RAM; + break; + case 0x1c: + case 0x1d: + case 0x1e: + // MBC 5 Rumble + mapper = mapperMBC5ROM; + mapperRAM = mapperMBC5RAM; + break; + case 0x22: + // MBC 7 + mapper = mapperMBC7ROM; + mapperRAM = mapperMBC7RAM; + mapperReadRAM = mapperMBC7ReadRAM; + break; + case 0xfe: + // HuC3 + mapper = mapperHuC3ROM; + mapperRAM = mapperHuC3RAM; + mapperReadRAM = mapperHuC3ReadRAM; + break; + case 0xff: + // HuC1 + mapper = mapperHuC1ROM; + mapperRAM = mapperHuC1RAM; + break; + default: + systemMessage(MSG_UNKNOWN_CARTRIDGE_TYPE, + N_("Unknown cartridge type %02x"), type); + return false; + } + + switch(type) { + case 0x03: + case 0x06: + case 0x0f: + case 0x10: + case 0x13: + case 0x1b: + case 0x1d: + case 0x1e: + case 0x22: + case 0xff: + gbBattery = 1; + break; + } + + if(gbRom[0x146] == 0x03) { + if(gbEmulatorType == 0 || + gbEmulatorType == 2 || + gbEmulatorType == 5) + gbSgbMode = 1; + else + gbSgbMode = 0; + } else + gbSgbMode = 0; + + if(gbRom[0x143] & 0x80) { + if(gbEmulatorType == 0 || + gbEmulatorType == 1 || + gbEmulatorType == 4 || + gbEmulatorType == 5) { + gbCgbMode = 1; + gbVram = (u8 *)malloc(0x4000); + gbWram = (u8 *)malloc(0x8000); + memset(gbVram,0,0x4000); + memset(gbWram,0,0x8000); + memset(gbPalette,0, 2*128); + } else { + gbCgbMode = 0; + } + } else + gbCgbMode = 0; + + gbInit(); + gbReset(); + + switch(type) { + case 0x1c: + case 0x1d: + case 0x1e: + gbDataMBC5.isRumbleCartridge = 1; + } + + return true; +} + +void gbEmulate(int ticksToStop) +{ + gbRegister tempRegister; + u8 tempValue; + s8 offset; + + int clockTicks = 0; + gbDmaTicks = 0; + + register int opcode = 0; + + while(1) { +#ifndef FINAL_VERSION + if(systemDebug) { + if(!(IFF & 0x80)) { + if(systemDebug > 1) { + sprintf(gbBuffer,"PC=%04x AF=%04x BC=%04x DE=%04x HL=%04x SP=%04x I=%04x\n", + PC.W, AF.W, BC.W, DE.W,HL.W,SP.W,IFF); + } else { + sprintf(gbBuffer,"PC=%04x I=%02x\n", PC.W, IFF); + } + log(gbBuffer); + } + } +#endif + if(IFF & 0x80) { + if(register_LCDC & 0x80) { + clockTicks = gbLcdTicks; + } else + clockTicks = 100; + + if(gbLcdMode == 1 && (gbLcdLYIncrementTicks < clockTicks)) + clockTicks = gbLcdLYIncrementTicks; + + if(gbSerialOn && (gbSerialTicks < clockTicks)) + clockTicks = gbSerialTicks; + + if(gbTimerOn && (gbTimerTicks < clockTicks)) + clockTicks = gbTimerTicks; + + if(soundTicks && (soundTicks < clockTicks)) + clockTicks = soundTicks; + } else { + opcode = gbReadOpcode(PC.W++); + + if(IFF & 0x100) { + IFF &= 0xff; + PC.W--; + } + + clockTicks = gbCycles[opcode]; + + switch(opcode) { + case 0xCB: + // extended opcode + opcode = gbReadOpcode(PC.W++); + clockTicks = gbCyclesCB[opcode]; + switch(opcode) { +#include "gbCodesCB.h" + } + break; +#include "gbCodes.h" + } + } + + if(!emulating) + return; + + if(gbDmaTicks) { + clockTicks += gbDmaTicks; + gbDmaTicks = 0; + } + + if(gbSgbMode) { + if(gbSgbPacketTimeout) { + gbSgbPacketTimeout -= clockTicks; + + if(gbSgbPacketTimeout <= 0) + gbSgbResetPacketState(); + } + } + + ticksToStop -= clockTicks; + + // DIV register emulation + gbDivTicks -= clockTicks; + while(gbDivTicks <= 0) { + register_DIV++; + gbDivTicks += GBDIV_CLOCK_TICKS; + } + + if(register_LCDC & 0x80) { + // LCD stuff + gbLcdTicks -= clockTicks; + if(gbLcdMode == 1) { + // during V-BLANK,we need to increment LY at the same rate! + gbLcdLYIncrementTicks -= clockTicks; + while(gbLcdLYIncrementTicks <= 0) { + gbLcdLYIncrementTicks += GBLY_INCREMENT_CLOCK_TICKS; + + if(register_LY < 153) { + register_LY++; + + gbCompareLYToLYC(); + + if(register_LY >= 153) + gbLcdLYIncrementTicks = 6; + } else { + register_LY = 0x00; + // reset the window line + gbWindowLine = -1; + gbLcdLYIncrementTicks = GBLY_INCREMENT_CLOCK_TICKS * 2; + gbCompareLYToLYC(); + } + } + } + + // our counter is off, see what we need to do + while(gbLcdTicks <= 0) { + int framesToSkip = systemFrameSkip; + if(speedup) + framesToSkip = 9; // try 6 FPS during speedup + switch(gbLcdMode) { + case 0: + // H-Blank + register_LY++; + + gbCompareLYToLYC(); + + // check if we reached the V-Blank period + if(register_LY == 144) { + // Yes, V-Blank + // set the LY increment counter + gbLcdLYIncrementTicks = gbLcdTicks + GBLY_INCREMENT_CLOCK_TICKS; + gbLcdTicks += GBLCD_MODE_1_CLOCK_TICKS; + gbLcdMode = 1; + if(register_LCDC & 0x80) { + gbInterrupt |= 1; // V-Blank interrupt + gbInterruptWait = 6; + if(register_STAT & 0x10) + gbInterrupt |= 2; + } + + gbFrameCount++; + + systemFrame(); + + if((gbFrameCount % 10) == 0) + system10Frames(60); + + if(gbFrameCount >= 60) { + u32 currentTime = systemGetClock(); + if(currentTime != gbLastTime) + systemShowSpeed(100000/(currentTime - gbLastTime)); + else + systemShowSpeed(0); + gbLastTime = currentTime; + gbFrameCount = 0; + } + + if(systemReadJoypads()) { + // read joystick + if(gbSgbMode && gbSgbMultiplayer) { + if(gbSgbFourPlayers) { + gbJoymask[0] = systemReadJoypad(0); + gbJoymask[1] = systemReadJoypad(1); + gbJoymask[2] = systemReadJoypad(2); + gbJoymask[3] = systemReadJoypad(3); + } else { + gbJoymask[0] = systemReadJoypad(0); + gbJoymask[1] = systemReadJoypad(1); + } + } else { + gbJoymask[0] = systemReadJoypad(-1); + } + } + int newmask = gbJoymask[0] & 255; + + if(gbRom[0x147] == 0x22) { + systemUpdateMotionSensor(); + } + + if(newmask) { + gbInterrupt |= 16; + } + + newmask = (gbJoymask[0] >> 10); + + speedup = (newmask & 1) ? true : false; + gbCapture = (newmask & 2) ? true : false; + + if(gbCapture && !gbCapturePrevious) { + gbCaptureNumber++; + systemScreenCapture(gbCaptureNumber); + } + gbCapturePrevious = gbCapture; + + if(gbFrameSkipCount >= framesToSkip) { + systemDrawScreen(); + gbFrameSkipCount = 0; + } else + gbFrameSkipCount++; + } else { + // go the the OAM being accessed mode + gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS; + gbLcdMode = 2; + + // only one LCD interrupt per line. may need to generalize... + if(!(register_STAT & 0x40) || + (register_LY != register_LYC)) { + if((register_STAT & 0x28) == 0x20) + gbInterrupt |= 2; + } + } + break; + case 1: + // V-Blank + // next mode is OAM being accessed mode + gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS; + gbLcdMode = 2; + if(!(register_STAT & 0x40) || + (register_LY != register_LYC)) { + if((register_STAT & 0x28) == 0x20) + gbInterrupt |= 2; + } + break; + case 2: + // OAM being accessed mode + + // next mode is OAM and VRAM in use + gbLcdTicks += GBLCD_MODE_3_CLOCK_TICKS; + gbLcdMode = 3; + break; + case 3: + // OAM and VRAM in use + // next mode is H-Blank + if(register_LY < 144) { + if(!gbSgbMask) { + if(gbFrameSkipCount >= framesToSkip) { + gbRenderLine(); + gbDrawSprites(); + + switch(systemColorDepth) { + case 16: + { + u16 * dest = (u16 *)pix + + (gbBorderLineSkip+2) * (register_LY + gbBorderRowSkip+1) + + gbBorderColumnSkip; + for(int x = 0; x < 160; ) { + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + } + if(gbBorderOn) + dest += gbBorderColumnSkip; + *dest++ = 0; // for filters that read one pixel more + } + break; + case 24: + { + u8 *dest = (u8 *)pix + + 3*(gbBorderLineSkip * (register_LY + gbBorderRowSkip) + + gbBorderColumnSkip); + for(int x = 0; x < 160;) { + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest+= 3; + } + } + break; + case 32: + { + u32 * dest = (u32 *)pix + + (gbBorderLineSkip+1) * (register_LY + gbBorderRowSkip+1) + + gbBorderColumnSkip; + for(int x = 0; x < 160;) { + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + } + } + break; + } + } + } + } + gbLcdTicks += GBLCD_MODE_0_CLOCK_TICKS; + gbLcdMode = 0; + // only one LCD interrupt per line. may need to generalize... + if(!(register_STAT & 0x40) || + (register_LY != register_LYC)) { + if(register_STAT & 0x08) + gbInterrupt |= 2; + } + if(gbHdmaOn) { + gbDoHdma(); + } + break; + } + // mark the correct lcd mode on STAT register + register_STAT = (register_STAT & 0xfc) | gbLcdMode; + } + } + + // serial emulation + if(gbSerialOn) { +#ifdef LINK_EMULATION + if(linkConnected) { + gbSerialTicks -= clockTicks; + + while(gbSerialTicks <= 0) { + // increment number of shifted bits + gbSerialBits++; + linkProc(); + if(gbSerialOn && (gbMemory[0xff02] & 1)) { + if(gbSerialBits == 8) { + gbSerialBits = 0; + gbMemory[0xff01] = 0xff; + gbMemory[0xff02] &= 0x7f; + gbSerialOn = 0; + gbInterrupt |= 8; + gbSerialTicks = 0; + } + } + gbSerialTicks += GBSERIAL_CLOCK_TICKS; + } + } else { +#endif + if(gbMemory[0xff02] & 1) { + gbSerialTicks -= clockTicks; + + // overflow + while(gbSerialTicks <= 0) { + // shift serial byte to right and put a 1 bit in its place + // gbMemory[0xff01] = 0x80 | (gbMemory[0xff01]>>1); + // increment number of shifted bits + gbSerialBits++; + if(gbSerialBits == 8) { + // end of transmission + if(gbSerialFunction) // external device + gbMemory[0xff01] = gbSerialFunction(gbMemory[0xff01]); + else + gbMemory[0xff01] = 0xff; + gbSerialTicks = 0; + gbMemory[0xff02] &= 0x7f; + gbSerialOn = 0; + gbInterrupt |= 8; + gbSerialBits = 0; + } else + gbSerialTicks += GBSERIAL_CLOCK_TICKS; + } + } +#ifdef LINK_EMULATION + } +#endif + } + + // timer emulation + if(gbTimerOn) { + gbTimerTicks -= clockTicks; + + while(gbTimerTicks <= 0) { + register_TIMA++; + + if(register_TIMA == 0) { + // timer overflow! + + // reload timer modulo + register_TIMA = register_TMA; + + // flag interrupt + gbInterrupt |= 4; + } + + gbTimerTicks += gbTimerClockTicks; + } + } + + /* + if(soundOffFlag) { + if(synchronize && !speedup) { + synchronizeTicks -= clockTicks; + + while(synchronizeTicks < 0) { + synchronizeTicks += SYNCHRONIZE_CLOCK_TICKS; + + DWORD now = timeGetTime(); + gbElapsedTime += (now - timeNow); + + if(gbElapsedTime < 50) { + DWORD diff = 50 - gbElapsedTime; + Sleep(diff); + timeNow = timeGetTime(); + elapsedTime = timeNow - now - diff; + if((int)elapsedTime < 0) + elapsedTime = 0; + } else { + timeNow = timeGetTime(); + elapsedTime = 0; + } + } + } + } + */ + + soundTicks -= clockTicks; + + while(soundTicks < 0) { + soundTicks += SOUND_CLOCK_TICKS; + + gbSoundTick(); + } + + register_IF = gbInterrupt; + + if(IFF & 0x20) { + IFF &= 0xdf; + IFF |= 0x01; + gbInterruptWait = 0; + } else if(gbInterrupt) { + if(gbInterruptWait == 0) { + // gbInterruptWait = 0; + + if(IFF & 0x01) { + if((gbInterrupt & 1) && (register_IE & 1)) { + gbVblank_interrupt(); + continue; + } + + if((gbInterrupt & 2) && (register_IE & 2)) { + gbLcd_interrupt(); + continue; + } + + if((gbInterrupt & 4) && (register_IE & 4)) { + gbTimer_interrupt(); + continue; + } + + if((gbInterrupt & 8) && (register_IE & 8)) { + gbSerial_interrupt(); + continue; + } + + if((gbInterrupt & 16) && (register_IE & 16)) { + gbJoypad_interrupt(); + continue; + } + } + } else { + gbInterruptWait -= clockTicks; + if(gbInterruptWait < 0) + gbInterruptWait = 0; + } + } + + if(ticksToStop <= 0) { + if(!(register_LCDC & 0x80)) { + if(systemReadJoypads()) { + // read joystick + if(gbSgbMode && gbSgbMultiplayer) { + if(gbSgbFourPlayers) { + gbJoymask[0] = systemReadJoypad(0); + gbJoymask[1] = systemReadJoypad(1); + gbJoymask[2] = systemReadJoypad(2); + gbJoymask[3] = systemReadJoypad(3); + } else { + gbJoymask[0] = systemReadJoypad(0); + gbJoymask[1] = systemReadJoypad(1); + } + } else { + gbJoymask[0] = systemReadJoypad(-1); + } + } + } + return; + } + } +} + +struct EmulatedSystem GBSystem = { + // emuMain + gbEmulate, + // emuReset + gbReset, + // emuCleanUp + gbCleanUp, + // emuReadBattery + gbReadBatteryFile, + // emuWriteBattery + gbWriteBatteryFile, + // emuReadState + gbReadSaveState, + // emuWriteState + gbWriteSaveState, + // emuReadMemState + gbReadMemSaveState, + // emuWriteMemState + gbWriteMemSaveState, + // emuWritePNG + gbWritePNGFile, + // emuWriteBMP + gbWriteBMPFile, + // emuUpdateCPSR + NULL, + // emuHasDebugger + false, + // emuCount +#ifdef FINAL_VERSION + 70000/4, +#else + 1000, +#endif +}; diff --git a/src/gb/gb.h b/src/gb/gb.h new file mode 100644 index 00000000..1527c77a --- /dev/null +++ b/src/gb/gb.h @@ -0,0 +1,60 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_GB_GB_H +#define VBA_GB_GB_H + +#define C_FLAG 0x10 +#define H_FLAG 0x20 +#define N_FLAG 0x40 +#define Z_FLAG 0x80 + +typedef union { + struct { +#ifdef WORDS_BIGENDIAN + u8 B1, B0; +#else + u8 B0,B1; +#endif + } B; + u16 W; +} gbRegister; + +extern bool gbLoadRom(const char *); +extern void gbEmulate(int); +extern bool gbIsGameboyRom(const char *); +extern void gbSoundReset(); +extern void gbSoundSetQuality(int); +extern void gbReset(); +extern void gbCleanUp(); +extern bool gbWriteBatteryFile(const char *); +extern bool gbWriteBatteryFile(const char *, bool); +extern bool gbReadBatteryFile(const char *); +extern bool gbWriteSaveState(const char *); +extern bool gbWriteMemSaveState(char *, int); +extern bool gbReadSaveState(const char *); +extern bool gbReadMemSaveState(char *, int); +extern void gbSgbRenderBorder(); +extern bool gbWritePNGFile(const char *); +extern bool gbWriteBMPFile(const char *); +extern bool gbReadGSASnapshot(const char *); + +extern struct EmulatedSystem GBSystem; + +#endif diff --git a/src/gb/gbCheats.cpp b/src/gb/gbCheats.cpp new file mode 100644 index 00000000..941bb166 --- /dev/null +++ b/src/gb/gbCheats.cpp @@ -0,0 +1,462 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include + +#include "../System.h" +#include "../NLS.h" +#include "../Util.h" + +#include "gbCheats.h" +#include "gbGlobals.h" + +gbCheat gbCheatList[100]; +int gbCheatNumber = 0; +bool gbCheatMap[0x10000]; + +extern bool cheatsEnabled; + +#define GBCHEAT_IS_HEX(a) ( ((a)>='A' && (a) <='F') || ((a) >='0' && (a) <= '9')) +#define GBCHEAT_HEX_VALUE(a) ( (a) >= 'A' ? (a) - 'A' + 10 : (a) - '0') + +void gbCheatUpdateMap() +{ + memset(gbCheatMap, 0, 0x10000); + + for(int i = 0; i < gbCheatNumber; i++) { + if(gbCheatList[i].enabled) + gbCheatMap[gbCheatList[i].address] = true; + } +} + +void gbCheatsSaveGame(gzFile gzFile) +{ + utilWriteInt(gzFile, gbCheatNumber); + if(gbCheatNumber) + utilGzWrite(gzFile, &gbCheatList[0], sizeof(gbCheat)*gbCheatNumber); +} + +void gbCheatsReadGame(gzFile gzFile, int version) +{ + if(version <= 8) { + int gbGgOn = utilReadInt(gzFile); + + if(gbGgOn) { + int n = utilReadInt(gzFile); + gbXxCheat tmpCheat; + for(int i = 0; i < n; i++) { + utilGzRead(gzFile,&tmpCheat, sizeof(gbXxCheat)); + gbAddGgCheat(tmpCheat.cheatCode, tmpCheat.cheatDesc); + } + } + + int gbGsOn = utilReadInt(gzFile); + + if(gbGsOn) { + int n = utilReadInt(gzFile); + gbXxCheat tmpCheat; + for(int i = 0; i < n; i++) { + utilGzRead(gzFile,&tmpCheat, sizeof(gbXxCheat)); + gbAddGsCheat(tmpCheat.cheatCode, tmpCheat.cheatDesc); + } + } + } else { + gbCheatNumber = utilReadInt(gzFile); + + if(gbCheatNumber) { + utilGzRead(gzFile, &gbCheatList[0], sizeof(gbCheat)*gbCheatNumber); + } + } + + gbCheatUpdateMap(); +} + +void gbCheatsSaveCheatList(const char *file) +{ + if(gbCheatNumber == 0) + return; + FILE *f = fopen(file, "wb"); + if(f == NULL) + return; + int version = 1; + fwrite(&version, 1, sizeof(version), f); + int type = 1; + fwrite(&type, 1, sizeof(type), f); + fwrite(&gbCheatNumber, 1, sizeof(gbCheatNumber), f); + fwrite(gbCheatList, 1, sizeof(gbCheatList), f); + fclose(f); +} + +bool gbCheatsLoadCheatList(const char *file) +{ + gbCheatNumber = 0; + + gbCheatUpdateMap(); + + int count = 0; + + FILE *f = fopen(file, "rb"); + + if(f == NULL) + return false; + + int version = 0; + + if(fread(&version, 1, sizeof(version), f) != sizeof(version)) { + fclose(f); + return false; + } + + if(version != 1) { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_VERSION, + N_("Unsupported cheat list version %d"), version); + fclose(f); + return false; + } + + int type = 0; + if(fread(&type, 1, sizeof(type), f) != sizeof(type)) { + fclose(f); + return false; + } + + if(type != 1) { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_TYPE, + N_("Unsupported cheat list type %d"), type); + fclose(f); + return false; + } + + if(fread(&count, 1, sizeof(count), f) != sizeof(count)) { + fclose(f); + return false; + } + + if(fread(gbCheatList, 1, sizeof(gbCheatList), f) != sizeof(gbCheatList)) { + fclose(f); + return false; + } + + gbCheatNumber = count; + gbCheatUpdateMap(); + + return true; +} + +bool gbVerifyGsCode(const char *code) +{ + int len = strlen(code); + + if(len == 0) + return true; + + if(len != 8) + return false; + + for(int i = 0; i < 8; i++) + if(!GBCHEAT_IS_HEX(code[i])) + return false; + + int address = GBCHEAT_HEX_VALUE(code[6]) << 12 | + GBCHEAT_HEX_VALUE(code[7]) << 8 | + GBCHEAT_HEX_VALUE(code[4]) << 4 | + GBCHEAT_HEX_VALUE(code[5]); + + if(address < 0xa000 || + address > 0xdfff) + return false; + + return true; +} + +void gbAddGsCheat(const char *code, const char *desc) +{ + if(gbCheatNumber > 99) { + systemMessage(MSG_MAXIMUM_NUMBER_OF_CHEATS, + N_("Maximum number of cheats reached.")); + return; + } + + if(!gbVerifyGsCode(code)) { + systemMessage(MSG_INVALID_GAMESHARK_CODE, + N_("Invalid GameShark code: %s"), code); + return; + } + + int i = gbCheatNumber; + + strcpy(gbCheatList[i].cheatCode, code); + strcpy(gbCheatList[i].cheatDesc, desc); + + gbCheatList[i].code = GBCHEAT_HEX_VALUE(code[0]) << 4 | + GBCHEAT_HEX_VALUE(code[1]); + + gbCheatList[i].value = GBCHEAT_HEX_VALUE(code[2]) << 4 | + GBCHEAT_HEX_VALUE(code[3]); + + gbCheatList[i].address = GBCHEAT_HEX_VALUE(code[6]) << 12 | + GBCHEAT_HEX_VALUE(code[7]) << 8 | + GBCHEAT_HEX_VALUE(code[4]) << 4 | + GBCHEAT_HEX_VALUE(code[5]); + + gbCheatList[i].compare = 0; + + gbCheatList[i].enabled = true; + + gbCheatMap[gbCheatList[i].address] = true; + + gbCheatNumber++; +} + +bool gbVerifyGgCode(const char *code) +{ + int len = strlen(code); + + if(len != 11 && + len != 7 && + len != 6 && + len != 0) + return false; + + if(len == 0) + return true; + + if(!GBCHEAT_IS_HEX(code[0])) + return false; + if(!GBCHEAT_IS_HEX(code[1])) + return false; + if(!GBCHEAT_IS_HEX(code[2])) + return false; + if(code[3] != '-') + return false; + if(!GBCHEAT_IS_HEX(code[4])) + return false; + if(!GBCHEAT_IS_HEX(code[5])) + return false; + if(!GBCHEAT_IS_HEX(code[6])) + return false; + if(code[7] != 0) { + if(code[7] != '-') + return false; + if(code[8] != 0) { + if(!GBCHEAT_IS_HEX(code[8])) + return false; + if(!GBCHEAT_IS_HEX(code[9])) + return false; + if(!GBCHEAT_IS_HEX(code[10])) + return false; + } + } + + // int replace = (GBCHEAT_HEX_VALUE(code[0]) << 4) + + // GBCHEAT_HEX_VALUE(code[1]); + + int address = (GBCHEAT_HEX_VALUE(code[2]) << 8) + + (GBCHEAT_HEX_VALUE(code[4]) << 4) + + (GBCHEAT_HEX_VALUE(code[5])) + + ((GBCHEAT_HEX_VALUE(code[6]) ^ 0x0f) << 12); + + if(address >= 0x8000 && address <= 0x9fff) + return false; + + if(address >= 0xc000) + return false; + + if(code[7] == 0 || code[8] == '0') + return true; + + int compare = (GBCHEAT_HEX_VALUE(code[8]) << 4) + + (GBCHEAT_HEX_VALUE(code[10])); + compare = compare ^ 0xff; + compare = (compare >> 2) | ( (compare << 6) & 0xc0); + compare ^= 0x45; + + int cloak = (GBCHEAT_HEX_VALUE(code[8])) ^ (GBCHEAT_HEX_VALUE(code[9])); + + if(cloak >=1 && cloak <= 7) + return false; + + return true; +} + +void gbAddGgCheat(const char *code, const char *desc) +{ + if(gbCheatNumber > 99) { + systemMessage(MSG_MAXIMUM_NUMBER_OF_CHEATS, + N_("Maximum number of cheats reached.")); + return; + } + + if(!gbVerifyGgCode(code)) { + systemMessage(MSG_INVALID_GAMEGENIE_CODE, + N_("Invalid GameGenie code: %s"), code); + return; + } + + int i = gbCheatNumber; + + int len = strlen(code); + + strcpy(gbCheatList[i].cheatCode, code); + strcpy(gbCheatList[i].cheatDesc, desc); + + gbCheatList[i].code = 1; + gbCheatList[i].value = (GBCHEAT_HEX_VALUE(code[0]) << 4) + + GBCHEAT_HEX_VALUE(code[1]); + + gbCheatList[i].address = (GBCHEAT_HEX_VALUE(code[2]) << 8) + + (GBCHEAT_HEX_VALUE(code[4]) << 4) + + (GBCHEAT_HEX_VALUE(code[5])) + + ((GBCHEAT_HEX_VALUE(code[6]) ^ 0x0f) << 12); + + gbCheatList[i].compare = 0; + + if(len != 7 && len != 8) { + + int compare = (GBCHEAT_HEX_VALUE(code[8]) << 4) + + (GBCHEAT_HEX_VALUE(code[10])); + compare = compare ^ 0xff; + compare = (compare >> 2) | ( (compare << 6) & 0xc0); + compare ^= 0x45; + + gbCheatList[i].compare = compare; + gbCheatList[i].code = 0; + } + + gbCheatList[i].enabled = true; + + gbCheatMap[gbCheatList[i].address] = true; + + gbCheatNumber++; +} + +void gbCheatRemove(int i) +{ + if(i < 0 || i >= gbCheatNumber) { + systemMessage(MSG_INVALID_CHEAT_TO_REMOVE, + N_("Invalid cheat to remove %d"), i); + return; + } + + if((i+1) < gbCheatNumber) { + memcpy(&gbCheatList[i], &gbCheatList[i+1], sizeof(gbCheat)* + (gbCheatNumber-i-1)); + } + + gbCheatNumber--; + + gbCheatUpdateMap(); +} + +void gbCheatRemoveAll() +{ + gbCheatNumber = 0; + gbCheatUpdateMap(); +} + +void gbCheatEnable(int i) +{ + if(i >=0 && i < gbCheatNumber) { + if(!gbCheatList[i].enabled) { + gbCheatList[i].enabled = true; + gbCheatUpdateMap(); + } + } +} + +void gbCheatDisable(int i) +{ + if(i >=0 && i < gbCheatNumber) { + if(gbCheatList[i].enabled) { + gbCheatList[i].enabled = false; + gbCheatUpdateMap(); + } + } +} + +bool gbCheatReadGSCodeFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if(!file) { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + fseek(file, 0x18, SEEK_SET); + int count = 0; + fread(&count, 1, 2, file); + int dummy = 0; + gbCheatRemoveAll(); + char desc[13]; + char code[9]; + int i; + for(i = 0; i < count; i++) { + fread(&dummy, 1, 2, file); + fread(desc, 1, 12, file); + desc[12] = 0; + fread(code, 1, 8, file); + code[8] = 0; + gbAddGsCheat(code, desc); + } + + for(i = 0; i < gbCheatNumber; i++) + gbCheatDisable(i); + + fclose(file); + return true; +} + +u8 gbCheatRead(u16 address) +{ + if(!cheatsEnabled) + return gbMemoryMap[address>>12][address & 0xFFF]; + + for(int i = 0; i < gbCheatNumber; i++) { + if(gbCheatList[i].enabled && gbCheatList[i].address == address) { + switch(gbCheatList[i].code) { + case 0x100: // GameGenie support + if(gbMemoryMap[address>>12][address&0xFFF] == gbCheatList[i].compare) + return gbCheatList[i].value; + break; + case 0x00: + case 0x01: + case 0x80: + return gbCheatList[i].value; + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + if(address >= 0xd000 && address < 0xe000) { + if(((gbMemoryMap[0x0d] - gbWram)/0x1000) == + (gbCheatList[i].code - 0x90)) + return gbCheatList[i].value; + } else + return gbCheatList[i].value; + } + } + } + return gbMemoryMap[address>>12][address&0xFFF]; +} diff --git a/src/gb/gbCheats.h b/src/gb/gbCheats.h new file mode 100644 index 00000000..3845a53c --- /dev/null +++ b/src/gb/gbCheats.h @@ -0,0 +1,58 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_GB_GBCHEATS_H +#define __VBA_GB_GBCHEATS_H + +#include "../System.h" + +struct gbXxCheat { + char cheatDesc[100]; + char cheatCode[20]; +}; + +struct gbCheat { + char cheatCode[20]; + char cheatDesc[32]; + u16 address; + int code; + u8 compare; + u8 value; + bool enabled; +}; + +extern void gbCheatsSaveGame(gzFile); +extern void gbCheatsReadGame(gzFile, int); +extern void gbCheatsSaveCheatList(const char *); +extern bool gbCheatsLoadCheatList(const char *); +extern bool gbCheatReadGSCodeFile(const char *); + +extern void gbAddGsCheat(const char *, const char*); +extern void gbAddGgCheat(const char *, const char*); +extern void gbCheatRemove(int); +extern void gbCheatRemoveAll(); +extern void gbCheatEnable(int); +extern void gbCheatDisable(int); +extern u8 gbCheatRead(u16); + +extern int gbCheatNumber; +extern gbCheat gbCheatList[100]; +extern bool gbCheatMap[0x10000]; +#endif + diff --git a/src/gb/gbCodes.h b/src/gb/gbCodes.h new file mode 100644 index 00000000..43c45199 --- /dev/null +++ b/src/gb/gbCodes.h @@ -0,0 +1,1407 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + case 0x00: + // NOP + break; + case 0x01: + // LD BC, NNNN + BC.B.B0=gbReadMemory(PC.W++); + BC.B.B1=gbReadMemory(PC.W++); + break; + case 0x02: + // LD (BC),A + gbWriteMemory(BC.W,AF.B.B1); + break; + case 0x03: + // INC BC + BC.W++; + break; + case 0x04: + // INC B + BC.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B1]| (BC.B.B1&0x0F? 0:H_FLAG); + break; + case 0x05: + // DEC B + BC.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B1]| + ((BC.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x06: + // LD B, NN + BC.B.B1=gbReadOpcode(PC.W++); + break; + case 0x07: + // RLCA + tempValue=AF.B.B1&0x80? C_FLAG:0; + AF.B.B1=(AF.B.B1<<1)|(AF.B.B1>>7); + AF.B.B0=tempValue; + break; + case 0x08: + // LD (NNNN), SP + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + gbWriteMemory(tempRegister.W++,SP.B.B0); + gbWriteMemory(tempRegister.W,SP.B.B1); + break; + case 0x09: + // ADD HL,BC + tempRegister.W=(HL.W+BC.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^BC.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)BC.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x0a: + // LD A,(BC) + AF.B.B1=gbReadMemory(BC.W); + break; + case 0x0b: + // DEC BC + BC.W--; + break; + case 0x0c: + // INC C + BC.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B0]| (BC.B.B0&0x0F? 0:H_FLAG); + break; + case 0x0d: + // DEC C + BC.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B0]| + ((BC.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x0e: + // LD C, NN + BC.B.B0=gbReadOpcode(PC.W++); + break; + case 0x0f: + // RRCA + tempValue=AF.B.B1&0x01; + AF.B.B1=(AF.B.B1>>1)|(tempValue? 0x80:0); + AF.B.B0=(tempValue<<4); + break; + case 0x10: + // STOP + opcode = gbReadOpcode(PC.W++); + if(gbCgbMode) { + if(gbMemory[0xff4d] & 1) { + gbSpeedSwitch(); + + if(gbSpeed == 0) + gbMemory[0xff4d] = 0x00; + else + gbMemory[0xff4d] = 0x80; + } + } + break; + case 0x11: + // LD DE, NNNN + DE.B.B0=gbReadMemory(PC.W++); + DE.B.B1=gbReadMemory(PC.W++); + break; + case 0x12: + // LD (DE),A + gbWriteMemory(DE.W,AF.B.B1); + break; + case 0x13: + // INC DE + DE.W++; + break; + case 0x14: + // INC D + DE.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B1]| (DE.B.B1&0x0F? 0:H_FLAG); + break; + case 0x15: + // DEC D + DE.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B1]| + ((DE.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x16: + // LD D,NN + DE.B.B1=gbReadOpcode(PC.W++); + break; + case 0x17: + // RLA + tempValue=AF.B.B1&0x80? C_FLAG:0; + AF.B.B1=(AF.B.B1<<1)|((AF.B.B0&C_FLAG)>>4); + AF.B.B0=tempValue; + break; + case 0x18: + // JR NN + PC.W+=(s8)gbReadMemory(PC.W)+1; + break; + case 0x19: + // ADD HL,DE + tempRegister.W=(HL.W+DE.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^DE.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)DE.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x1a: + // LD A,(DE) + AF.B.B1=gbReadMemory(DE.W); + break; + case 0x1b: + // DEC DE + DE.W--; + break; + case 0x1c: + // INC E + DE.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B0]| (DE.B.B0&0x0F? 0:H_FLAG); + break; + case 0x1d: + // DEC E + DE.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B0]| + ((DE.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x1e: + // LD E,NN + DE.B.B0=gbReadOpcode(PC.W++); + break; + case 0x1f: + // RRA + tempValue=AF.B.B1&0x01; + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0&C_FLAG? 0x80:0); + AF.B.B0=(tempValue<<4); + break; + case 0x20: + // JR NZ,NN + if(AF.B.B0&Z_FLAG) + PC.W++; + else { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } + break; + case 0x21: + // LD HL,NNNN + HL.B.B0=gbReadMemory(PC.W++); + HL.B.B1=gbReadMemory(PC.W++); + break; + case 0x22: + // LDI (HL),A + gbWriteMemory(HL.W++,AF.B.B1); + break; + case 0x23: + // INC HL + HL.W++; + break; + case 0x24: + // INC H + HL.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B1]| (HL.B.B1&0x0F? 0:H_FLAG); + break; + case 0x25: + // DEC H + HL.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B1]| + ((HL.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x26: + // LD H,NN + HL.B.B1=gbReadOpcode(PC.W++); + break; + case 0x27: + // DAA + tempRegister.W=AF.B.B1; + if(AF.B.B0&C_FLAG) tempRegister.W|=256; + if(AF.B.B0&H_FLAG) tempRegister.W|=512; + if(AF.B.B0&N_FLAG) tempRegister.W|=1024; + AF.W=DAATable[tempRegister.W]; + break; + case 0x28: + // JR Z,NN + if(AF.B.B0&Z_FLAG) { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } else + PC.W++; + break; + case 0x29: + // ADD HL,HL + tempRegister.W=(HL.W+HL.W)&0xFFFF; AF.B.B0= (AF.B.B0 & Z_FLAG)| + ((HL.W^HL.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)HL.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x2a: + // LDI A,(HL) + AF.B.B1 = gbReadMemory(HL.W++); + break; + case 0x2b: + // DEC HL + HL.W--; + break; + case 0x2c: + // INC L + HL.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B0]| (HL.B.B0&0x0F? 0:H_FLAG); + break; + case 0x2d: + // DEC L + HL.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B0]| + ((HL.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x2e: + // LD L,NN + HL.B.B0=gbReadOpcode(PC.W++); + break; + case 0x2f: + // CPL + AF.B.B1 ^= 255; + AF.B.B0|=N_FLAG|H_FLAG; + break; + case 0x30: + // JR NC,NN + if(AF.B.B0&C_FLAG) + PC.W++; + else { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } + break; + case 0x31: + // LD SP,NNNN + SP.B.B0=gbReadMemory(PC.W++); + SP.B.B1=gbReadMemory(PC.W++); + break; + case 0x32: + // LDD (HL),A + gbWriteMemory(HL.W--,AF.B.B1); + break; + case 0x33: + // INC SP + SP.W++; + break; + case 0x34: + // INC (HL) + tempValue=gbReadMemory(HL.W)+1; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[tempValue]| (tempValue&0x0F? 0:H_FLAG); + gbWriteMemory(HL.W,tempValue); + break; + case 0x35: + // DEC (HL) + tempValue=gbReadMemory(HL.W)-1; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[tempValue]| + ((tempValue&0x0F)==0x0F? H_FLAG:0);gbWriteMemory(HL.W,tempValue); + break; + case 0x36: + // LD (HL),NN + gbWriteMemory(HL.W,gbReadOpcode(PC.W++)); + break; + case 0x37: + // SCF + AF.B.B0 = AF.B.B0 & Z_FLAG | C_FLAG; + break; +case 0x38: + // JR C,NN + if(AF.B.B0&C_FLAG) { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks ++; + } else + PC.W++; + break; + case 0x39: + // ADD HL,SP + tempRegister.W=(HL.W+SP.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^SP.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)SP.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x3a: + // LDD A,(HL) + AF.B.B1 = gbReadMemory(HL.W--); + break; + case 0x3b: + // DEC SP + SP.W--; + break; + case 0x3c: + // INC A + AF.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[AF.B.B1]| (AF.B.B1&0x0F? 0:H_FLAG); + break; + case 0x3d: + // DEC A + AF.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[AF.B.B1]| + ((AF.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x3e: + // LD A,NN + AF.B.B1=gbReadOpcode(PC.W++); + break; + case 0x3f: + // CCF + AF.B.B0^=C_FLAG;AF.B.B0&=~(N_FLAG|H_FLAG); + break; + case 0x40: + // LD B,B + BC.B.B1=BC.B.B1; + break; + case 0x41: + // LD B,C + BC.B.B1=BC.B.B0; + break; + case 0x42: + // LD B,D + BC.B.B1=DE.B.B1; + break; + case 0x43: + // LD B,E + BC.B.B1=DE.B.B0; + break; + case 0x44: + // LD B,H + BC.B.B1=HL.B.B1; + break; + case 0x45: + // LD B,L + BC.B.B1=HL.B.B0; + break; + case 0x46: + // LD B,(HL) + BC.B.B1=gbReadMemory(HL.W); + break; + case 0x47: + // LD B,A + BC.B.B1=AF.B.B1; + break; + case 0x48: + // LD C,B + BC.B.B0=BC.B.B1; + break; + case 0x49: + // LD C,C + BC.B.B0=BC.B.B0; + break; + case 0x4a: + // LD C,D + BC.B.B0=DE.B.B1; + break; + case 0x4b: + // LD C,E + BC.B.B0=DE.B.B0; + break; + case 0x4c: + // LD C,H + BC.B.B0=HL.B.B1; + break; + case 0x4d: + // LD C,L + BC.B.B0=HL.B.B0; + break; + case 0x4e: + // LD C,(HL) + BC.B.B0=gbReadMemory(HL.W); + break; + case 0x4f: + // LD C,A + BC.B.B0=AF.B.B1; + break; + case 0x50: + // LD D,B + DE.B.B1=BC.B.B1; + break; + case 0x51: + // LD D,C + DE.B.B1=BC.B.B0; + break; + case 0x52: + // LD D,D + DE.B.B1=DE.B.B1; + break; + case 0x53: + // LD D,E + DE.B.B1=DE.B.B0; + break; + case 0x54: + // LD D,H + DE.B.B1=HL.B.B1; + break; + case 0x55: + // LD D,L + DE.B.B1=HL.B.B0; + break; + case 0x56: + // LD D,(HL) + DE.B.B1=gbReadMemory(HL.W); + break; + case 0x57: + // LD D,A + DE.B.B1=AF.B.B1; + break; + case 0x58: + // LD E,B + DE.B.B0=BC.B.B1; + break; + case 0x59: + // LD E,C + DE.B.B0=BC.B.B0; + break; + case 0x5a: + // LD E,D + DE.B.B0=DE.B.B1; + break; + case 0x5b: + // LD E,E + DE.B.B0=DE.B.B0; + break; + case 0x5c: + // LD E,H + DE.B.B0=HL.B.B1; + break; + case 0x5d: + // LD E,L + DE.B.B0=HL.B.B0; + break; + case 0x5e: + // LD E,(HL) + DE.B.B0=gbReadMemory(HL.W); + break; + case 0x5f: + // LD E,A + DE.B.B0=AF.B.B1; + break; + case 0x60: + // LD H,B + HL.B.B1=BC.B.B1; + break; + case 0x61: + // LD H,C + HL.B.B1=BC.B.B0; + break; + case 0x62: + // LD H,D + HL.B.B1=DE.B.B1; + break; + case 0x63: + // LD H,E + HL.B.B1=DE.B.B0; + break; + case 0x64: + // LD H,H + HL.B.B1=HL.B.B1; + break; + case 0x65: + // LD H,L + HL.B.B1=HL.B.B0; + break; + case 0x66: + // LD H,(HL) + HL.B.B1=gbReadMemory(HL.W); + break; + case 0x67: + // LD H,A + HL.B.B1=AF.B.B1; + break; + case 0x68: + // LD L,B + HL.B.B0=BC.B.B1; + break; + case 0x69: + // LD L,C + HL.B.B0=BC.B.B0; + break; + case 0x6a: + // LD L,D + HL.B.B0=DE.B.B1; + break; + case 0x6b: + // LD L,E + HL.B.B0=DE.B.B0; + break; + case 0x6c: + // LD L,H + HL.B.B0=HL.B.B1; + break; + case 0x6d: + // LD L,L + HL.B.B0=HL.B.B0; + break; + case 0x6e: + // LD L,(HL) + HL.B.B0=gbReadMemory(HL.W); + break; + case 0x6f: + // LD L,A + HL.B.B0=AF.B.B1; + break; + case 0x70: + // LD (HL),B + gbWriteMemory(HL.W,BC.B.B1); + break; + case 0x71: + // LD (HL),C + gbWriteMemory(HL.W,BC.B.B0); + break; + case 0x72: + // LD (HL),D + gbWriteMemory(HL.W,DE.B.B1); + break; + case 0x73: + // LD (HL),E + gbWriteMemory(HL.W,DE.B.B0); + break; + case 0x74: + // LD (HL),H + gbWriteMemory(HL.W,HL.B.B1); + break; + case 0x75: + // LD (HL),L + gbWriteMemory(HL.W,HL.B.B0); + break; + case 0x76: + // HALT + if(IFF & 1) { + PC.W--; + IFF |= 0x80; + } else { + if((register_IE & register_IF) > 0) + IFF |= 0x100; + else { + PC.W--; + IFF |= 0x81; + } + } + break; + case 0x77: + // LD (HL),A + gbWriteMemory(HL.W,AF.B.B1); + break; + case 0x78: + // LD A,B + AF.B.B1=BC.B.B1; + break; + case 0x79: + // LD A,C + AF.B.B1=BC.B.B0; + break; + case 0x7a: + // LD A,D + AF.B.B1=DE.B.B1; + break; + case 0x7b: + // LD A,E + AF.B.B1=DE.B.B0; + break; + case 0x7c: + // LD A,H + AF.B.B1=HL.B.B1; + break; + case 0x7d: + // LD A,L + AF.B.B1=HL.B.B0; + break; + case 0x7e: + // LD A,(HL) + AF.B.B1=gbReadMemory(HL.W); + break; + case 0x7f: + // LD A,A + AF.B.B1=AF.B.B1; + break; + case 0x80: + // ADD B + tempRegister.W=AF.B.B1+BC.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x81: + // ADD C + tempRegister.W=AF.B.B1+BC.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x82: + // ADD D + tempRegister.W=AF.B.B1+DE.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x83: + // ADD E + tempRegister.W=AF.B.B1+DE.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x84: + // ADD H + tempRegister.W=AF.B.B1+HL.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x85: + // ADD L + tempRegister.W=AF.B.B1+HL.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x86: + // ADD (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1+tempValue; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x87: + // ADD A + tempRegister.W=AF.B.B1+AF.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x88: + // ADC B: + tempRegister.W=AF.B.B1+BC.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x89: + // ADC C + tempRegister.W=AF.B.B1+BC.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8a: + // ADC D + tempRegister.W=AF.B.B1+DE.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8b: + // ADC E + tempRegister.W=AF.B.B1+DE.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8c: + // ADC H + tempRegister.W=AF.B.B1+HL.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); AF.B.B1=tempRegister.B.B0; + break; + case 0x8d: + // ADC L + tempRegister.W=AF.B.B1+HL.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8e: + // ADC (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1+tempValue+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8f: + // ADC A + tempRegister.W=AF.B.B1+AF.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x90: + // SUB B + tempRegister.W=AF.B.B1-BC.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x91: + // SUB C + tempRegister.W=AF.B.B1-BC.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x92: + // SUB D + tempRegister.W=AF.B.B1-DE.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x93: + // SUB E + tempRegister.W=AF.B.B1-DE.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x94: + // SUB H + tempRegister.W=AF.B.B1-HL.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x95: + // SUB L + tempRegister.W=AF.B.B1-HL.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x96: + // SUB (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x97: + // SUB A + AF.B.B1=0; + AF.B.B0=N_FLAG|Z_FLAG; + break; + case 0x98: + // SBC B + tempRegister.W=AF.B.B1-BC.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x99: + // SBC C + tempRegister.W=AF.B.B1-BC.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9a: + // SBC D + tempRegister.W=AF.B.B1-DE.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9b: + // SBC E + tempRegister.W=AF.B.B1-DE.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9c: + // SBC H + tempRegister.W=AF.B.B1-HL.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9d: + // SBC L + tempRegister.W=AF.B.B1-HL.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9e: + // SBC (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9f: + // SBC A + tempRegister.W=AF.B.B1-AF.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xa0: + // AND B + AF.B.B1&=BC.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa1: + // AND C + AF.B.B1&=BC.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa2: + // AND_D + AF.B.B1&=DE.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa3: + // AND E + AF.B.B1&=DE.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa4: + // AND H + AF.B.B1&=HL.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa5: + // AND L + AF.B.B1&=HL.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa6: + // AND (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1&=tempValue; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa7: + // AND A + AF.B.B1&=AF.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa8: + // XOR B + AF.B.B1^=BC.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xa9: + // XOR C + AF.B.B1^=BC.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xaa: + // XOR D + AF.B.B1^=DE.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xab: + // XOR E + AF.B.B1^=DE.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xac: + // XOR H + AF.B.B1^=HL.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xad: + // XOR L + AF.B.B1^=HL.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xae: + // XOR (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1^=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xaf: + // XOR A + AF.B.B1=0; + AF.B.B0=Z_FLAG; + break; + case 0xb0: + // OR B + AF.B.B1|=BC.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb1: + // OR C + AF.B.B1|=BC.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb2: + // OR D + AF.B.B1|=DE.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb3: + // OR E + AF.B.B1|=DE.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb4: + // OR H + AF.B.B1|=HL.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb5: + // OR L + AF.B.B1|=HL.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb6: + // OR (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1|=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb7: + // OR A + AF.B.B1|=AF.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb8: + // CP B: + tempRegister.W=AF.B.B1-BC.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xb9: + // CP C + tempRegister.W=AF.B.B1-BC.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xba: + // CP D + tempRegister.W=AF.B.B1-DE.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbb: + // CP E + tempRegister.W=AF.B.B1-DE.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbc: + // CP H + tempRegister.W=AF.B.B1-HL.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbd: + // CP L + tempRegister.W=AF.B.B1-HL.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbe: + // CP (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbf: + // CP A + AF.B.B0=N_FLAG|Z_FLAG; + break; + case 0xc0: + // RET NZ + if(!(AF.B.B0&Z_FLAG)) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xc1: + // POP BC + BC.B.B0=gbReadMemory(SP.W++); + BC.B.B1=gbReadMemory(SP.W++); + break; + case 0xc2: + // JP NZ,NNNN + if(AF.B.B0&Z_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } + break; + case 0xc3: + // JP NNNN + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + break; + case 0xc4: + // CALL NZ,NNNN + if(AF.B.B0&Z_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } + break; + case 0xc5: + // PUSH BC + gbWriteMemory(--SP.W,BC.B.B1); + gbWriteMemory(--SP.W,BC.B.B0); + break; + case 0xc6: + // ADD NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1+tempValue; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xc7: + // RST 00 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0000; + break; + case 0xc8: + // RET Z + if(AF.B.B0&Z_FLAG) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xc9: + // RET + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + break; + case 0xca: + // JP Z,NNNN + if(AF.B.B0&Z_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } else + PC.W+=2; + break; + // CB done outside + case 0xcc: + // CALL Z,NNNN + if(AF.B.B0&Z_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } else + PC.W+=2; + break; + case 0xcd: + // CALL NNNN + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + break; + case 0xce: + // ADC NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1+tempValue+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xcf: + // RST 08 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0008; + break; + case 0xd0: + // RET NC + if(!(AF.B.B0&C_FLAG)) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xd1: + // POP DE + DE.B.B0=gbReadMemory(SP.W++); + DE.B.B1=gbReadMemory(SP.W++); + break; + case 0xd2: + // JP NC,NNNN + if(AF.B.B0&C_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } + break; + // D3 illegal + case 0xd4: + // CALL NC,NNNN + if(AF.B.B0&C_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } + break; + case 0xd5: + // PUSH DE + gbWriteMemory(--SP.W,DE.B.B1); + gbWriteMemory(--SP.W,DE.B.B0); + break; + case 0xd6: + // SUB NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xd7: + // RST 10 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0010; + break; + case 0xd8: + // RET C + if(AF.B.B0&C_FLAG) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 4; + } + break; + case 0xd9: + // RETI + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + IFF |= 0x01; + break; + case 0xda: + // JP C,NNNN + if(AF.B.B0&C_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } else + PC.W+=2; + break; + // DB illegal + case 0xdc: + // CALL C,NNNN + if(AF.B.B0&C_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } else + PC.W+=2; + break; + // DD illegal + case 0xde: + // SBC NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xdf: + // RST 18 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0018; + break; + case 0xe0: + // LD (FF00+NN),A + gbWriteMemory(0xff00 + gbReadOpcode(PC.W++),AF.B.B1); + break; + case 0xe1: + // POP HL + HL.B.B0=gbReadMemory(SP.W++); + HL.B.B1=gbReadMemory(SP.W++); + break; + case 0xe2: + // LD (FF00+C),A + gbWriteMemory(0xff00 + BC.B.B0,AF.B.B1); + break; + // E3 illegal + // E4 illegal + case 0xe5: + // PUSH HL + gbWriteMemory(--SP.W,HL.B.B1); + gbWriteMemory(--SP.W,HL.B.B0); + break; + case 0xe6: + // AND NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1&=tempValue; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xe7: + // RST 20 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0020; + break; + case 0xe8: + // ADD SP,NN + offset = (s8)gbReadOpcode(PC.W++); + + if(offset >= 0) { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W > tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000? H_FLAG:0); + SP.W = tempRegister.W; + } else { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W < tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000?H_FLAG:0); + SP.W = tempRegister.W; + } + break; + case 0xe9: + // LD PC,HL + PC.W=HL.W; + break; + case 0xea: + // LD (NNNN),A + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + gbWriteMemory(tempRegister.W,AF.B.B1); + break; + // EB illegal + // EC illegal + // ED illegal + case 0xee: + // XOR NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1^=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xef: + // RST 28 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0028; + break; + case 0xf0: + // LD A,(FF00+NN) + AF.B.B1 = gbReadMemory(0xff00+gbReadOpcode(PC.W++)); + break; + case 0xf1: + // POP AF + AF.B.B0=gbReadMemory(SP.W++); + AF.B.B1=gbReadMemory(SP.W++); + break; + case 0xf2: + // LD A,(FF00+C) + AF.B.B1 = gbReadMemory(0xff00+BC.B.B0); + break; + case 0xf3: + // DI + // IFF&=0xFE; + IFF&=(~0x21); + break; + // F4 illegal + case 0xf5: + // PUSH AF + gbWriteMemory(--SP.W,AF.B.B1); + gbWriteMemory(--SP.W,AF.B.B0); + break; + case 0xf6: + // OR NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1|=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xf7: + // RST 30 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0030; + break; + case 0xf8: + // LD HL,SP+NN + offset = (s8)gbReadOpcode(PC.W++); + if(offset >= 0) { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W > tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000? H_FLAG:0); + HL.W = tempRegister.W; + } else { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W < tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000?H_FLAG:0); + HL.W = tempRegister.W; + } + break; + case 0xf9: + // LD SP,HL + SP.W=HL.W; + break; + case 0xfa: + // LD A,(NNNN) + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + AF.B.B1=gbReadMemory(tempRegister.W); + break; + case 0xfb: + // EI + IFF|=0x20; + break; + // FC illegal + // FD illegal + case 0xfe: + // CP NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xff: + // RST 38 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0038; + break; + default: + systemMessage(0, N_("Unknown opcode %02x at %04x"), + gbReadOpcode(PC.W-1),PC.W-1); + emulating = false; + return; diff --git a/src/gb/gbCodesCB.h b/src/gb/gbCodesCB.h new file mode 100644 index 00000000..05dce6e6 --- /dev/null +++ b/src/gb/gbCodesCB.h @@ -0,0 +1,1288 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + case 0x00: + // RLC B + AF.B.B0 = (BC.B.B1 & 0x80)?C_FLAG:0; + BC.B.B1 = (BC.B.B1<<1) | (BC.B.B1>>7); + AF.B.B0 |= ZeroTable[BC.B.B1]; + break; + case 0x01: + // RLC C + AF.B.B0 = (BC.B.B0 & 0x80)?C_FLAG:0; + BC.B.B0 = (BC.B.B0<<1) | (BC.B.B0>>7); + AF.B.B0 |= ZeroTable[BC.B.B0]; + break; + case 0x02: + // RLC D + AF.B.B0 = (DE.B.B1 & 0x80)?C_FLAG:0; + DE.B.B1 = (DE.B.B1<<1) | (DE.B.B1>>7); + AF.B.B0 |= ZeroTable[DE.B.B1]; + break; + case 0x03: + // RLC E + AF.B.B0 = (DE.B.B0 & 0x80)?C_FLAG:0; + DE.B.B0 = (DE.B.B0<<1) | (DE.B.B0>>7); + AF.B.B0 |= ZeroTable[DE.B.B0]; + break; + case 0x04: + // RLC H + AF.B.B0 = (HL.B.B1 & 0x80)?C_FLAG:0; + HL.B.B1 = (HL.B.B1<<1) | (HL.B.B1>>7); + AF.B.B0 |= ZeroTable[HL.B.B1]; + break; + case 0x05: + // RLC L + AF.B.B0 = (HL.B.B0 & 0x80)?C_FLAG:0; + HL.B.B0 = (HL.B.B0<<1) | (HL.B.B0>>7); + AF.B.B0 |= ZeroTable[HL.B.B0]; + break; + case 0x06: + // RLC (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0 = (tempValue & 0x80)?C_FLAG:0; + tempValue = (tempValue<<1) | (tempValue>>7); + AF.B.B0 |= ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x07: + // RLC A + AF.B.B0 = (AF.B.B1 & 0x80)?C_FLAG:0; + AF.B.B1 = (AF.B.B1<<1) | (AF.B.B1>>7); + AF.B.B0 |= ZeroTable[AF.B.B1]; + break; + case 0x08: + // RRC B + AF.B.B0=(BC.B.B1&0x01 ? C_FLAG : 0); + BC.B.B1=(BC.B.B1>>1)|(BC.B.B1<<7); + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x09: + // RRC C + AF.B.B0=(BC.B.B0&0x01 ? C_FLAG : 0); + BC.B.B0=(BC.B.B0>>1)|(BC.B.B0<<7); + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x0a: + // RRC D + AF.B.B0=(DE.B.B1&0x01 ? C_FLAG : 0); + DE.B.B1=(DE.B.B1>>1)|(DE.B.B1<<7); + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x0b: + // RRC E + AF.B.B0=(DE.B.B0&0x01 ? C_FLAG : 0); + DE.B.B0=(DE.B.B0>>1)|(DE.B.B0<<7); + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x0c: + // RRC H + AF.B.B0=(HL.B.B1&0x01 ? C_FLAG : 0); + HL.B.B1=(HL.B.B1>>1)|(HL.B.B1<<7); + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x0d: + // RRC L + AF.B.B0=(HL.B.B0&0x01 ? C_FLAG : 0); + HL.B.B0=(HL.B.B0>>1)|(HL.B.B0<<7); + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x0e: + // RRC (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01 ? C_FLAG : 0); + tempValue=(tempValue>>1)|(tempValue<<7); + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x0f: + // RRC A + AF.B.B0=(AF.B.B1&0x01 ? C_FLAG : 0); + AF.B.B1=(AF.B.B1>>1)|(AF.B.B1<<7); + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x10: + // RL B + if(BC.B.B1&0x80) { + BC.B.B1=(BC.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[BC.B.B1]|C_FLAG; + } else { + BC.B.B1=(BC.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[BC.B.B1]; + } + break; + case 0x11: + // RL C + if(BC.B.B0&0x80) { + BC.B.B0=(BC.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[BC.B.B0]|C_FLAG; + } else { + BC.B.B0=(BC.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[BC.B.B0]; + } + break; + case 0x12: + // RL D + if(DE.B.B1&0x80) { + DE.B.B1=(DE.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[DE.B.B1]|C_FLAG; + } else { + DE.B.B1=(DE.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[DE.B.B1]; + } + break; + case 0x13: + // RL E + if(DE.B.B0&0x80) { + DE.B.B0=(DE.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[DE.B.B0]|C_FLAG; + } else { + DE.B.B0=(DE.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[DE.B.B0]; + } + break; + case 0x14: + // RL H + if(HL.B.B1&0x80) { + HL.B.B1=(HL.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[HL.B.B1]|C_FLAG; + } else { + HL.B.B1=(HL.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[HL.B.B1]; + } + break; + case 0x15: + // RL L + if(HL.B.B0&0x80) { + HL.B.B0=(HL.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[HL.B.B0]|C_FLAG; + } else { + HL.B.B0=(HL.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[HL.B.B0]; + } + break; + case 0x16: + // RL (HL) + tempValue=gbReadMemory(HL.W); + if(tempValue&0x80) { + tempValue=(tempValue<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[tempValue]|C_FLAG; + } else { + tempValue=(tempValue<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[tempValue]; + } + gbWriteMemory(HL.W,tempValue); + break; + case 0x17: + // RL A + if(AF.B.B1&0x80) { + AF.B.B1=(AF.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[AF.B.B1]|C_FLAG; + } else { + AF.B.B1=(AF.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0=ZeroTable[AF.B.B1]; + } + break; + case 0x18: + // RR B + if(BC.B.B1&0x01) { + BC.B.B1=(BC.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B1]|C_FLAG; + } else { + BC.B.B1=(BC.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B1]; + } + break; + case 0x19: + // RR C + if(BC.B.B0&0x01) { + BC.B.B0=(BC.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B0]|C_FLAG; + } else { + BC.B.B0=(BC.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B0]; + } + break; + case 0x1a: + // RR D + if(DE.B.B1&0x01) { + DE.B.B1=(DE.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B1]|C_FLAG; + } else { + DE.B.B1=(DE.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B1]; + } + break; + case 0x1b: + // RR E + if(DE.B.B0&0x01) { + DE.B.B0=(DE.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B0]|C_FLAG; + } else { + DE.B.B0=(DE.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B0]; + } + break; + case 0x1c: + // RR H + if(HL.B.B1&0x01) { + HL.B.B1=(HL.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B1]|C_FLAG; + } else { + HL.B.B1=(HL.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B1]; + } + break; + case 0x1d: + // RR L + if(HL.B.B0&0x01) { + HL.B.B0=(HL.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B0]|C_FLAG; + } else { + HL.B.B0=(HL.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B0]; + } + break; + case 0x1e: + // RR (HL) + tempValue=gbReadMemory(HL.W); + if(tempValue&0x01) { + tempValue=(tempValue>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[tempValue]|C_FLAG; + } else { + tempValue=(tempValue>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[tempValue]; + } + gbWriteMemory(HL.W,tempValue); + break; + case 0x1f: + // RR A + if(AF.B.B1&0x01) { + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[AF.B.B1]|C_FLAG; + } else { + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[AF.B.B1]; + } + break; + case 0x20: + // SLA B + AF.B.B0=(BC.B.B1&0x80?C_FLAG : 0); + BC.B.B1<<=1; + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x21: + // SLA C + AF.B.B0=(BC.B.B0&0x80?C_FLAG : 0); + BC.B.B0<<=1; + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x22: + // SLA D + AF.B.B0=(DE.B.B1&0x80?C_FLAG : 0); + DE.B.B1<<=1; + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x23: + // SLA E + AF.B.B0=(DE.B.B0&0x80?C_FLAG : 0); + DE.B.B0<<=1; + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x24: + // SLA H + AF.B.B0=(HL.B.B1&0x80?C_FLAG : 0); + HL.B.B1<<=1; + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x25: + // SLA L + AF.B.B0=(HL.B.B0&0x80?C_FLAG : 0); + HL.B.B0<<=1; + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x26: + // SLA (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x80?C_FLAG : 0); + tempValue<<=1; + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x27: + // SLA A + AF.B.B0=(AF.B.B1&0x80?C_FLAG : 0); + AF.B.B1<<=1; + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x28: + // SRA B + AF.B.B0=(BC.B.B1&0x01 ? C_FLAG: 0); + BC.B.B1=(BC.B.B1>>1)|(BC.B.B1&0x80); + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x29: + // SRA C + AF.B.B0=(BC.B.B0&0x01 ? C_FLAG: 0); + BC.B.B0=(BC.B.B0>>1)|(BC.B.B0&0x80); + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x2a: + // SRA D + AF.B.B0=(DE.B.B1&0x01 ? C_FLAG: 0); + DE.B.B1=(DE.B.B1>>1)|(DE.B.B1&0x80); + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x2b: + // SRA E + AF.B.B0=(DE.B.B0&0x01 ? C_FLAG: 0); + DE.B.B0=(DE.B.B0>>1)|(DE.B.B0&0x80); + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x2c: + // SRA H + AF.B.B0=(HL.B.B1&0x01 ? C_FLAG: 0); + HL.B.B1=(HL.B.B1>>1)|(HL.B.B1&0x80); + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x2d: + // SRA L + AF.B.B0=(HL.B.B0&0x01 ? C_FLAG: 0); + HL.B.B0=(HL.B.B0>>1)|(HL.B.B0&0x80); + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x2e: + // SRA (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01 ? C_FLAG: 0); + tempValue=(tempValue>>1)|(tempValue&0x80); + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x2f: + // SRA A + AF.B.B0=(AF.B.B1&0x01 ? C_FLAG: 0); + AF.B.B1=(AF.B.B1>>1)|(AF.B.B1&0x80); + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x30: + // SWAP B + BC.B.B1 = (BC.B.B1&0xf0)>>4 | (BC.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[BC.B.B1]; + break; + case 0x31: + // SWAP C + BC.B.B0 = (BC.B.B0&0xf0)>>4 | (BC.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[BC.B.B0]; + break; + case 0x32: + // SWAP D + DE.B.B1 = (DE.B.B1&0xf0)>>4 | (DE.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[DE.B.B1]; + break; + case 0x33: + // SWAP E + DE.B.B0 = (DE.B.B0&0xf0)>>4 | (DE.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[DE.B.B0]; + break; + case 0x34: + // SWAP H + HL.B.B1 = (HL.B.B1&0xf0)>>4 | (HL.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[HL.B.B1]; + break; + case 0x35: + // SWAP L + HL.B.B0 = (HL.B.B0&0xf0)>>4 | (HL.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[HL.B.B0]; + break; + case 0x36: + // SWAP (HL) + tempValue=gbReadMemory(HL.W); + tempValue = (tempValue&0xf0)>>4 | (tempValue&0x0f)<<4; + AF.B.B0 = ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x37: + // SWAP A + AF.B.B1 = (AF.B.B1&0xf0)>>4 | (AF.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[AF.B.B1]; + break; + case 0x38: + // SRL B + AF.B.B0=(BC.B.B1&0x01)?C_FLAG:0; + BC.B.B1>>=1; + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x39: + // SRL C + AF.B.B0=(BC.B.B0&0x01)?C_FLAG:0; + BC.B.B0>>=1; + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x3a: + // SRL D + AF.B.B0=(DE.B.B1&0x01)?C_FLAG:0; + DE.B.B1>>=1; + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x3b: + // SRL E + AF.B.B0=(DE.B.B0&0x01)?C_FLAG:0; + DE.B.B0>>=1; + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x3c: + // SRL H + AF.B.B0=(HL.B.B1&0x01)?C_FLAG:0; + HL.B.B1>>=1; + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x3d: + // SRL L + AF.B.B0=(HL.B.B0&0x01)?C_FLAG:0; + HL.B.B0>>=1; + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x3e: + // SRL (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01)?C_FLAG:0; + tempValue>>=1; + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x3f: + // SRL A + AF.B.B0=(AF.B.B1&0x01)?C_FLAG:0; + AF.B.B1>>=1; + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x40: + // BIT 0,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x41: + // BIT 0,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x42: + // BIT 0,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x43: + // BIT 0,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x44: + // BIT 0,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x45: + // BIT 0,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x46: + // BIT 0,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<0)? 0:Z_FLAG); + break; + case 0x47: + // BIT 0,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x48: + // BIT 1,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x49: + // BIT 1,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4a: + // BIT 1,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x4b: + // BIT 1,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4c: + // BIT 1,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x4d: + // BIT 1,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4e: + // BIT 1,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<1)? 0:Z_FLAG); + break; + case 0x4f: + // BIT 1,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x50: + // BIT 2,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x51: + // BIT 2,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x52: + // BIT 2,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x53: + // BIT 2,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x54: + // BIT 2,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x55: + // BIT 2,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x56: + // BIT 2,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<2)? 0:Z_FLAG); + break; + case 0x57: + // BIT 2,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x58: + // BIT 3,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x59: + // BIT 3,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5a: + // BIT 3,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x5b: + // BIT 3,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5c: + // BIT 3,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x5d: + // BIT 3,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5e: + // BIT 3,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<3)? 0:Z_FLAG); + break; + case 0x5f: + // BIT 3,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x60: + // BIT 4,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x61: + // BIT 4,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x62: + // BIT 4,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x63: + // BIT 4,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x64: + // BIT 4,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x65: + // BIT 4,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x66: + // BIT 4,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<4)? 0:Z_FLAG); + break; + case 0x67: + // BIT 4,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x68: + // BIT 5,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x69: + // BIT 5,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6a: + // BIT 5,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x6b: + // BIT 5,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6c: + // BIT 5,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x6d: + // BIT 5,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6e: + // BIT 5,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<5)? 0:Z_FLAG); + break; + case 0x6f: + // BIT 5,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x70: + // BIT 6,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x71: + // BIT 6,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x72: + // BIT 6,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x73: + // BIT 6,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x74: + // BIT 6,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x75: + // BIT 6,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x76: + // BIT 6,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<6)? 0:Z_FLAG); + break; + case 0x77: + // BIT 6,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x78: + // BIT 7,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x79: + // BIT 7,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7a: + // BIT 7,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x7b: + // BIT 7,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7c: + // BIT 7,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x7d: + // BIT 7,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7e: + // BIT 7,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<7)? 0:Z_FLAG); + break; + case 0x7f: + // BIT 7,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x80: + // RES 0,B + BC.B.B1&=~(1<<0); + break; + case 0x81: + // RES 0,C + BC.B.B0&=~(1<<0); + break; + case 0x82: + // RES 0,D + DE.B.B1&=~(1<<0); + break; + case 0x83: + // RES 0,E + DE.B.B0&=~(1<<0); + break; + case 0x84: + // RES 0,H + HL.B.B1&=~(1<<0); + break; + case 0x85: + // RES 0,L + HL.B.B0&=~(1<<0); + break; + case 0x86: + // RES 0,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<0); + gbWriteMemory(HL.W,tempValue); + break; + case 0x87: + // RES 0,A + AF.B.B1&=~(1<<0); + break; + case 0x88: + // RES 1,B + BC.B.B1&=~(1<<1); + break; + case 0x89: + // RES 1,C + BC.B.B0&=~(1<<1); + break; + case 0x8a: + // RES 1,D + DE.B.B1&=~(1<<1); + break; + case 0x8b: + // RES 1,E + DE.B.B0&=~(1<<1); + break; + case 0x8c: + // RES 1,H + HL.B.B1&=~(1<<1); + break; + case 0x8d: + // RES 1,L + HL.B.B0&=~(1<<1); + break; + case 0x8e: + // RES 1,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<1); + gbWriteMemory(HL.W,tempValue); + break; + case 0x8f: + // RES 1,A + AF.B.B1&=~(1<<1); + break; + case 0x90: + // RES 2,B + BC.B.B1&=~(1<<2); + break; + case 0x91: + // RES 2,C + BC.B.B0&=~(1<<2); + break; + case 0x92: + // RES 2,D + DE.B.B1&=~(1<<2); + break; + case 0x93: + // RES 2,E + DE.B.B0&=~(1<<2); + break; + case 0x94: + // RES 2,H + HL.B.B1&=~(1<<2); + break; + case 0x95: + // RES 2,L + HL.B.B0&=~(1<<2); + break; + case 0x96: + // RES 2,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<2); + gbWriteMemory(HL.W,tempValue); + break; + case 0x97: + // RES 2,A + AF.B.B1&=~(1<<2); + break; + case 0x98: + // RES 3,B + BC.B.B1&=~(1<<3); + break; + case 0x99: + // RES 3,C + BC.B.B0&=~(1<<3); + break; + case 0x9a: + // RES 3,D + DE.B.B1&=~(1<<3); + break; + case 0x9b: + // RES 3,E + DE.B.B0&=~(1<<3); + break; + case 0x9c: + // RES 3,H + HL.B.B1&=~(1<<3); + break; + case 0x9d: + // RES 3,L + HL.B.B0&=~(1<<3); + break; + case 0x9e: + // RES 3,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<3); + gbWriteMemory(HL.W,tempValue); + break; + case 0x9f: + // RES 3,A + AF.B.B1&=~(1<<3); + break; + case 0xa0: + // RES 4,B + BC.B.B1&=~(1<<4); + break; + case 0xa1: + // RES 4,C + BC.B.B0&=~(1<<4); + break; + case 0xa2: + // RES 4,D + DE.B.B1&=~(1<<4); + break; + case 0xa3: + // RES 4,E + DE.B.B0&=~(1<<4); + break; + case 0xa4: + // RES 4,H + HL.B.B1&=~(1<<4); + break; + case 0xa5: + // RES 4,L + HL.B.B0&=~(1<<4); + break; + case 0xa6: + // RES 4,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<4); + gbWriteMemory(HL.W,tempValue); + break; + case 0xa7: + // RES 4,A + AF.B.B1&=~(1<<4); + break; + case 0xa8: + // RES 5,B + BC.B.B1&=~(1<<5); + break; + case 0xa9: + // RES 5,C + BC.B.B0&=~(1<<5); + break; + case 0xaa: + // RES 5,D + DE.B.B1&=~(1<<5); + break; + case 0xab: + // RES 5,E + DE.B.B0&=~(1<<5); + break; + case 0xac: + // RES 5,H + HL.B.B1&=~(1<<5); + break; + case 0xad: + // RES 5,L + HL.B.B0&=~(1<<5); + break; + case 0xae: + // RES 5,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<5); + gbWriteMemory(HL.W,tempValue); + break; + case 0xaf: + // RES 5,A + AF.B.B1&=~(1<<5); + break; + case 0xb0: + // RES 6,B + BC.B.B1&=~(1<<6); + break; + case 0xb1: + // RES 6,C + BC.B.B0&=~(1<<6); + break; + case 0xb2: + // RES 6,D + DE.B.B1&=~(1<<6); + break; + case 0xb3: + // RES 6,E + DE.B.B0&=~(1<<6); + break; + case 0xb4: + // RES 6,H + HL.B.B1&=~(1<<6); + break; + case 0xb5: + // RES 6,L + HL.B.B0&=~(1<<6); + break; + case 0xb6: + // RES 6,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<6); + gbWriteMemory(HL.W,tempValue); + break; + case 0xb7: + // RES 6,A + AF.B.B1&=~(1<<6); + break; + case 0xb8: + // RES 7,B + BC.B.B1&=~(1<<7); + break; + case 0xb9: + // RES 7,C + BC.B.B0&=~(1<<7); + break; + case 0xba: + // RES 7,D + DE.B.B1&=~(1<<7); + break; + case 0xbb: + // RES 7,E + DE.B.B0&=~(1<<7); + break; + case 0xbc: + // RES 7,H + HL.B.B1&=~(1<<7); + break; + case 0xbd: + // RES 7,L + HL.B.B0&=~(1<<7); + break; + case 0xbe: + // RES 7,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<7); + gbWriteMemory(HL.W,tempValue); + break; + case 0xbf: + // RES 7,A + AF.B.B1&=~(1<<7); + break; + case 0xc0: + // SET 0,B + BC.B.B1|=1<<0; + break; + case 0xc1: + // SET 0,C + BC.B.B0|=1<<0; + break; + case 0xc2: + // SET 0,D + DE.B.B1|=1<<0; + break; + case 0xc3: + // SET 0,E + DE.B.B0|=1<<0; + break; + case 0xc4: + // SET 0,H + HL.B.B1|=1<<0; + break; + case 0xc5: + // SET 0,L + HL.B.B0|=1<<0; + break; + case 0xc6: + // SET 0,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<0; + gbWriteMemory(HL.W,tempValue); + break; + case 0xc7: + // SET 0,A + AF.B.B1|=1<<0; + break; + case 0xc8: + // SET 1,B + BC.B.B1|=1<<1; + break; + case 0xc9: + // SET 1,C + BC.B.B0|=1<<1; + break; + case 0xca: + // SET 1,D + DE.B.B1|=1<<1; + break; + case 0xcb: + // SET 1,E + DE.B.B0|=1<<1; + break; + case 0xcc: + // SET 1,H + HL.B.B1|=1<<1; + break; + case 0xcd: + // SET 1,L + HL.B.B0|=1<<1; + break; + case 0xce: + // SET 1,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<1; + gbWriteMemory(HL.W,tempValue); + break; + case 0xcf: + // SET 1,A + AF.B.B1|=1<<1; + break; + case 0xd0: + // SET 2,B + BC.B.B1|=1<<2; + break; + case 0xd1: + // SET 2,C + BC.B.B0|=1<<2; + break; + case 0xd2: + // SET 2,D + DE.B.B1|=1<<2; + break; + case 0xd3: + // SET 2,E + DE.B.B0|=1<<2; + break; + case 0xd4: + // SET 2,H + HL.B.B1|=1<<2; + break; + case 0xd5: + // SET 2,L + HL.B.B0|=1<<2; + break; + case 0xd6: + // SET 2,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<2; + gbWriteMemory(HL.W,tempValue); + break; + case 0xd7: + // SET 2,A + AF.B.B1|=1<<2; + break; + case 0xd8: + // SET 3,B + BC.B.B1|=1<<3; + break; + case 0xd9: + // SET 3,C + BC.B.B0|=1<<3; + break; + case 0xda: + // SET 3,D + DE.B.B1|=1<<3; + break; + case 0xdb: + // SET 3,E + DE.B.B0|=1<<3; + break; + case 0xdc: + // SET 3,H + HL.B.B1|=1<<3; + break; + case 0xdd: + // SET 3,L + HL.B.B0|=1<<3; + break; + case 0xde: + // SET 3,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<3; + gbWriteMemory(HL.W,tempValue); + break; + case 0xdf: + // SET 3,A + AF.B.B1|=1<<3; + break; + case 0xe0: + // SET 4,B + BC.B.B1|=1<<4; + break; + case 0xe1: + // SET 4,C + BC.B.B0|=1<<4; + break; + case 0xe2: + // SET 4,D + DE.B.B1|=1<<4; + break; + case 0xe3: + // SET 4,E + DE.B.B0|=1<<4; + break; + case 0xe4: + // SET 4,H + HL.B.B1|=1<<4; + break; + case 0xe5: + // SET 4,L + HL.B.B0|=1<<4; + break; + case 0xe6: + // SET 4,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<4; + gbWriteMemory(HL.W,tempValue); + break; + case 0xe7: + // SET 4,A + AF.B.B1|=1<<4; + break; + case 0xe8: + // SET 5,B + BC.B.B1|=1<<5; + break; + case 0xe9: + // SET 5,C + BC.B.B0|=1<<5; + break; + case 0xea: + // SET 5,D + DE.B.B1|=1<<5; + break; + case 0xeb: + // SET 5,E + DE.B.B0|=1<<5; + break; + case 0xec: + // SET 5,H + HL.B.B1|=1<<5; + break; + case 0xed: + // SET 5,L + HL.B.B0|=1<<5; + break; + case 0xee: + // SET 5,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<5; + gbWriteMemory(HL.W,tempValue); + break; + case 0xef: + // SET 5,A + AF.B.B1|=1<<5; + break; + case 0xf0: + // SET 6,B + BC.B.B1|=1<<6; + break; + case 0xf1: + // SET 6,C + BC.B.B0|=1<<6; + break; + case 0xf2: + // SET 6,D + DE.B.B1|=1<<6; + break; + case 0xf3: + // SET 6,E + DE.B.B0|=1<<6; + break; + case 0xf4: + // SET 6,H + HL.B.B1|=1<<6; + break; + case 0xf5: + // SET 6,L + HL.B.B0|=1<<6; + break; + case 0xf6: + // SET 6,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<6; + gbWriteMemory(HL.W,tempValue); + break; + case 0xf7: + // SET 6,A + AF.B.B1|=1<<6; + break; + case 0xf8: + // SET 7,B + BC.B.B1|=1<<7; + break; + case 0xf9: + // SET 7,C + BC.B.B0|=1<<7; + break; + case 0xfa: + // SET 7,D + DE.B.B1|=1<<7; + break; + case 0xfb: + // SET 7,E + DE.B.B0|=1<<7; + break; + case 0xfc: + // SET 7,H + HL.B.B1|=1<<7; + break; + case 0xfd: + // SET 7,L + HL.B.B0|=1<<7; + break; + case 0xfe: + // SET 7,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<7; + gbWriteMemory(HL.W,tempValue); + break; + case 0xff: + // SET 7,A + AF.B.B1|=1<<7; + break; + default: + systemMessage(0, N_("Unknown opcode %02x at %04x"), + gbReadOpcode(PC.W-1),PC.W-1); + emulating = false; + return; diff --git a/src/gb/gbDis.cpp b/src/gb/gbDis.cpp new file mode 100644 index 00000000..bf85f3a8 --- /dev/null +++ b/src/gb/gbDis.cpp @@ -0,0 +1,249 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include + +#include "../System.h" +#include "gbGlobals.h" + +typedef struct { + u8 mask; + u8 value; + char *mnen; +} GBOPCODE; + +#define GB_READ(x) gbMemoryMap[(x)>>12][(x)&0xfff] + +static char *registers[] = + { "B", "C", "D", "E", "H", "L", "(HL)", "A" }; + +static char *registers16[] = + { "BC", "DE", "HL", "SP", // for some operations + "BC", "DE", "HL", "AF" }; // for push/pop + +static char *cond[] = + { "NZ", "Z", "NC", "C" }; + +static char hexDigits[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +}; + +static GBOPCODE opcodes[] = { + { 0xff, 0x00, "NOP" }, + { 0xcf, 0x01, "LD %R4,%W" }, + { 0xff, 0x02, "LD (BC),A" }, + { 0xcf, 0x03, "INC %R4" }, + { 0xc7, 0x04, "INC %r3" }, + { 0xc7, 0x05, "DEC %r3" }, + { 0xc7, 0x06, "LD %r3,%B" }, + { 0xff, 0x07, "RLCA" }, + { 0xff, 0x08, "LD (%W),SP" }, + { 0xcf, 0x09, "ADD HL,%R4" }, + { 0xff, 0x0a, "LD A,(BC)" }, + { 0xcf, 0x0b, "DEC %R4" }, + { 0xff, 0x0f, "RRCA" }, + { 0xff, 0x10, "STOP" }, + { 0xff, 0x12, "LD (DE),A" }, + { 0xff, 0x17, "RLA" }, + { 0xff, 0x18, "JR %d" }, + { 0xff, 0x1a, "LD A,(DE)" }, + { 0xff, 0x1f, "RRA" }, + { 0xe7, 0x20, "JR %c3,%d" }, + { 0xff, 0x22, "LDI (HL),A" }, + { 0xff, 0x27, "DAA" }, + { 0xff, 0x2a, "LDI A,(HL)" }, + { 0xff, 0x2f, "CPL" }, + { 0xff, 0x32, "LDD (HL),A" }, + { 0xff, 0x37, "SCF" }, + { 0xff, 0x3a, "LDD A,(HL)" }, + { 0xff, 0x3f, "CCF" }, + { 0xff, 0x76, "HALT" }, + { 0xc0, 0x40, "LD %r3,%r0" }, + { 0xf8, 0x80, "ADD A,%r0" }, + { 0xf8, 0x88, "ADC A,%r0" }, + { 0xf8, 0x90, "SUB %r0" }, + { 0xf8, 0x98, "SBC A,%r0" }, + { 0xf8, 0xa0, "AND %r0" }, + { 0xf8, 0xa8, "XOR %r0" }, + { 0xf8, 0xb0, "OR %r0" }, + { 0xf8, 0xb8, "CP %r0" }, + { 0xe7, 0xc0, "RET %c3" }, + { 0xcf, 0xc1, "POP %t4" }, + { 0xe7, 0xc2, "JP %c3,%W" }, + { 0xff, 0xc3, "JP %W" }, + { 0xe7, 0xc4, "CALL %c3,%W" }, + { 0xcf, 0xc5, "PUSH %t4" }, + { 0xff, 0xc6, "ADD A,%B" }, + { 0xc7, 0xc7, "RST %P" }, + { 0xff, 0xc9, "RET" }, + { 0xff, 0xcd, "CALL %W" }, + { 0xff, 0xce, "ADC %B" }, + { 0xff, 0xd6, "SUB %B" }, + { 0xff, 0xd9, "RETI" }, + { 0xff, 0xde, "SBC %B" }, + { 0xff, 0xe0, "LD (FF%B),A" }, + { 0xff, 0xe2, "LD (FF00h+C),A" }, + { 0xff, 0xe6, "AND %B" }, + { 0xff, 0xe8, "ADD SP,%D" }, + { 0xff, 0xe9, "LD PC,HL" }, + { 0xff, 0xea, "LD (%W),A" }, + { 0xff, 0xee, "XOR %B" }, + { 0xff, 0xf0, "LD A,(FF%B)" }, + { 0xff, 0xf2, "LD A,(FF00h+C)" }, + { 0xff, 0xf3, "DI" }, + { 0xff, 0xf6, "OR %B" }, + { 0xff, 0xf8, "LD HL,SP%D" }, + { 0xff, 0xf9, "LD SP,HL" }, + { 0xff, 0xfa, "LD A,(%W)" }, + { 0xff, 0xfb, "EI" }, + { 0xff, 0xfe, "CP %B" }, + { 0x00, 0x00, "DB %B" } +}; + +static GBOPCODE cbOpcodes[] = { + { 0xf8, 0x00, "RLC %r0" }, + { 0xf8, 0x08, "RRC %r0" }, + { 0xf8, 0x10, "RL %r0" }, + { 0xf8, 0x18, "RR %r0" }, + { 0xf8, 0x20, "SLA %r0" }, + { 0xf8, 0x28, "SRA %r0" }, + { 0xf8, 0x30, "SWAP %r0" }, + { 0xf8, 0x38, "SRL %r0" }, + { 0xc0, 0x40, "BIT %b,%r0" }, + { 0xc0, 0x80, "RES %b,%r0" }, + { 0xc0, 0xc0, "SET %b,%r0" }, + { 0x00, 0x00, "DB CBh,%B" } +}; + +static char *addHex(char *p, u8 value) +{ + *p++ = hexDigits[value >> 4]; + *p++ = hexDigits[value & 15]; + return p; +} + +static char *addHex16(char *p, u16 value) +{ + p = addHex(p, value>>8); + return addHex(p, value & 255); +} + +static char *addStr(char *p, char *s) +{ + while(*s) { + *p++ = *s++; + } + return p; +} + +int gbDis(char *buffer, u16 address) +{ + char *p = buffer; + int instr = 1; + u16 addr = address; + sprintf(p, "%04x ", address); + p += 12; + + u8 opcode = GB_READ(address); + address++; + char *mnen; + GBOPCODE *op; + if(opcode == 0xcb) { + opcode = GB_READ(address); + address++; + instr++; + op = cbOpcodes; + } else { + op = opcodes; + } + while(op->value != (opcode & op->mask)) op++; + mnen = op->mnen; + + u8 b0, b1; + s8 disp; + int shift; + + while(*mnen) { + if(*mnen == '%') { + mnen++; + switch(*mnen++) { + case 'W': + b0 = GB_READ(address); + address++; + b1 = GB_READ(address); + address++; + p = addHex16(p, b0|b1<<8); + instr += 2; + *p++ = 'h'; + break; + case 'B': + p = addHex(p, GB_READ(address)); + *p++ = 'h'; + address++; + instr++; + break; + case 'D': + disp = GB_READ(address); + if(disp >= 0) + *p++ = '+'; + p += sprintf(p, "%d", disp); + instr++; + break; + case 'd': + disp = GB_READ(address); + address++; + p = addHex16(p, address+disp); + *p++ = 'h'; + instr++; + break; + case 'b': + // kind of a hack, but it works :-) + *p++ = hexDigits[(opcode >> 3) & 7]; + break; + case 'r': + shift = *mnen++ - '0'; + p = addStr(p, registers[(opcode >> shift) & 7]); + break; + case 'R': + shift = *mnen++ - '0'; + p = addStr(p, registers16[(opcode >> shift) & 3]); + break; + case 't': + shift = *mnen++ - '0'; + p = addStr(p, registers16[4+((opcode >> shift) & 3)]); + break; + case 'P': + p = addHex(p, ((opcode >> 3) & 7) * 8); + break; + case 'c': + shift = *mnen++ - '0'; + p = addStr(p, cond[(opcode >> shift) & 3]); + break; + } + } else + *p++ = *mnen++; + } + for(int i = 0; i < instr; i++) { + u16 a = addr + i; + addHex(buffer+5+i*2, GB_READ(a)); + } + *p = 0; + return instr; +} diff --git a/src/gb/gbGfx.cpp b/src/gb/gbGfx.cpp new file mode 100644 index 00000000..aa639664 --- /dev/null +++ b/src/gb/gbGfx.cpp @@ -0,0 +1,490 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include + +#include "../GBA.h" +#include "gbGlobals.h" +#include "gbSGB.h" + +u8 gbInvertTab[256] = { + 0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0, + 0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0, + 0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8, + 0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8, + 0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4, + 0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4, + 0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec, + 0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc, + 0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2, + 0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2, + 0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea, + 0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa, + 0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6, + 0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6, + 0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee, + 0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe, + 0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1, + 0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1, + 0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9, + 0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9, + 0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5, + 0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5, + 0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed, + 0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd, + 0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3, + 0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3, + 0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb, + 0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb, + 0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7, + 0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7, + 0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef, + 0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff +}; + +u16 gbLineMix[160]; + +void gbRenderLine() +{ + u8 * bank0; + u8 * bank1; + if(gbCgbMode) { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } else { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int tile_map = 0x1800; + if((register_LCDC & 8) != 0) + tile_map = 0x1c00; + + int tile_pattern = 0x0800; + + if((register_LCDC & 16) != 0) + tile_pattern = 0x0000; + + int x = 0; + int y = register_LY; + + if(y >= 144) + return; + + // int yLine = (y + gbBorderRowSkip) * gbBorderLineSkip; + + int sx = register_SCX; + int sy = register_SCY; + + sy+=y; + + sy &= 255; + + int tx = sx >> 3; + int ty = sy >> 3; + + int bx = 1 << (7 - (sx & 7)); + int by = sy & 7; + + int tile_map_line_y = tile_map + ty * 32; + + int tile_map_address = tile_map_line_y + tx; + + u8 attrs = 0; + if(bank1 != NULL) + attrs = bank1[tile_map_address]; + + u8 tile = bank0[tile_map_address]; + + tile_map_address++; + + if((register_LCDC & 16) == 0) { + if(tile < 128) tile += 128; + else tile -= 128; + } + + int tile_pattern_address = tile_pattern + tile * 16 + by*2; + + if(register_LCDC & 0x80) { + if((register_LCDC & 0x01 || gbCgbMode) && (layerSettings & 0x0100)) { + while(x < 160) { + u8 tile_a = 0; + u8 tile_b = 0; + + if(attrs & 0x40) { + tile_pattern_address = tile_pattern + tile * 16 + (7-by)*2; + } + + if(attrs & 0x08) { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } else { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if(attrs & 0x20) { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + while(bx > 0) { + u8 c = (tile_a & bx) ? 1 : 0; + c += ((tile_b & bx) ? 2 : 0); + + gbLineBuffer[x] = c; // mark the gbLineBuffer color + + if(attrs & 0x80) + gbLineBuffer[x] |= 0x300; + + if(gbCgbMode) { + c = c + (attrs & 7)*4; + } else { + c = gbBgp[c]; + if(gbSgbMode && !gbCgbMode) { + int dx = x >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if(c == 0) + palette = 0; + + c = c + 4*palette; + } + } + gbLineMix[x] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + x++; + if(x >= 160) + break; + bx >>= 1; + } + tx++; + if(tx == 32) + tx = 0; + bx = 128; + + if(bank1) + attrs = bank1[tile_map_line_y + tx]; + + tile = bank0[tile_map_line_y + tx]; + + if((register_LCDC & 16) == 0) { + if(tile < 128) tile += 128; + else tile -= 128; + } + tile_pattern_address = tile_pattern + tile * 16 + by * 2; + } + } else { + for(int i = 0; i < 160; i++) { + gbLineMix[i] = gbPalette[0]; + gbLineBuffer[i] = 0; + } + } + + // do the window display + if((register_LCDC & 0x20) && (layerSettings & 0x2000)) { + int wy = register_WY; + + if(y >= wy) { + int wx = register_WX; + wx -= 7; + + if( wx <= 159 && gbWindowLine <= 143) { + + tile_map = 0x1800; + + if((register_LCDC & 0x40) != 0) + tile_map = 0x1c00; + + if(gbWindowLine == -1) { + gbWindowLine = 0; + } + + tx = 0; + ty = gbWindowLine >> 3; + + bx = 128; + by = gbWindowLine & 7; + + if(wx < 0) { + bx >>= (-wx); + wx = 0; + } + + tile_map_line_y = tile_map + ty * 32; + + tile_map_address = tile_map_line_y + tx; + + x = wx; + + tile = bank0[tile_map_address]; + u8 attrs = 0; + if(bank1) + attrs = bank1[tile_map_address]; + tile_map_address++; + + if((register_LCDC & 16) == 0) { + if(tile < 128) tile += 128; + else tile -= 128; + } + + tile_pattern_address = tile_pattern + tile * 16 + by*2; + + while(x < 160) { + u8 tile_a = 0; + u8 tile_b = 0; + + if(attrs & 0x40) { + tile_pattern_address = tile_pattern + tile * 16 + (7-by)*2; + } + + if(attrs & 0x08) { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } else { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if(attrs & 0x20) { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + while(bx > 0) { + u8 c = (tile_a & bx) != 0 ? 1 : 0; + c += ((tile_b & bx) != 0 ? 2 : 0); + + if(attrs & 0x80) + gbLineBuffer[x] = 0x300 + c; + else + gbLineBuffer[x] = 0x100 + c; + + if(gbCgbMode) { + c = c + (attrs & 7) * 4; + } else { + c = gbBgp[c]; + if(gbSgbMode && ! gbCgbMode) { + int dx = x >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if(c == 0) + palette = 0; + + c = c + 4*palette; + } + } + gbLineMix[x] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + x++; + if(x >= 160) + break; + bx >>= 1; + } + tx++; + if(tx == 32) + tx = 0; + bx = 128; + tile = bank0[tile_map_line_y + tx]; + if(bank1) + attrs = bank1[tile_map_line_y + tx]; + + if((register_LCDC & 16) == 0) { + if(tile < 128) tile += 128; + else tile -= 128; + } + tile_pattern_address = tile_pattern + tile * 16 + by * 2; + } + gbWindowLine++; + } + } + } + } else { + for(int i = 0; i < 160; i++) { + gbLineMix[i] = gbPalette[0]; + gbLineBuffer[i] = 0; + } + } +} + +void gbDrawSpriteTile(int tile, int x,int y,int t, int flags, + int size,int spriteNumber) +{ + u8 * bank0; + u8 * bank1; + if(gbCgbMode) { + if(register_VBK & 1) { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } else { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + } else { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int init = 0x0000; + + // int yLine = (y+gbBorderRowSkip) * gbBorderLineSkip; + + u8 *pal = gbObp0; + + int flipx = (flags & 0x20); + int flipy = (flags & 0x40); + + if((flags & 0x10)) + pal = gbObp1; + + if(flipy) { + t = (size ? 15 : 7) - t; + } + + int prio = flags & 0x80; + + int address = init + tile * 16 + 2*t; + int a = 0; + int b = 0; + + if(gbCgbMode && flags & 0x08) { + a = bank1[address++]; + b = bank1[address++]; + } else { + a = bank0[address++]; + b = bank0[address++]; + } + + for(int xx = 0; xx < 8; xx++) { + u8 mask = 1 << (7-xx); + u8 c = 0; + if( (a & mask)) + c++; + if( (b & mask)) + c+=2; + + if(c==0) continue; + + int xxx = xx+x; + if(flipx) + xxx = (7-xx+x); + + if(xxx < 0 || xxx > 159) + continue; + + u16 color = gbLineBuffer[xxx]; + + if(prio) { + if(color < 0x200 && ((color & 0xFF) != 0)) + continue; + } + if(color >= 0x300 && color != 0x300) + continue; + else if(color >= 0x200 && color < 0x300) { + int sprite = color & 0xff; + + int spriteX = gbMemory[0xfe00 + 4 * sprite + 1] - 8; + + if(spriteX == x) { + if(sprite < spriteNumber) + continue; + } else { + if(gbCgbMode) { + if(sprite < spriteNumber) + continue; + } else { + if(spriteX < x+8) + continue; + } + } + } + + + gbLineBuffer[xxx] = 0x200 + spriteNumber; + + // make sure that sprites will work even in CGB mode + if(gbCgbMode) { + c = c + (flags & 0x07)*4 + 32; + } else { + c = pal[c]; + + if(gbSgbMode && !gbCgbMode) { + int dx = xxx >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if(c == 0) + palette = 0; + + c = c + 4*palette; + } else { + c += 4; + } + } + + gbLineMix[xxx] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + } +} + +void gbDrawSprites() +{ + int x = 0; + int y = 0; + int count = 0; + + int size = (register_LCDC & 4); + + if(!(register_LCDC & 0x80)) + return; + + if((register_LCDC & 2) && (layerSettings & 0x1000)) { + int yc = register_LY; + + int address = 0xfe00; + for(int i = 0; i < 40; i++) { + y = gbMemory[address++]; + x = gbMemory[address++]; + int tile = gbMemory[address++]; + if(size) + tile &= 254; + int flags = gbMemory[address++]; + + if(x > 0 && y > 0 && x < 168 && y < 160) { + // check if sprite intersects current line + int t = yc -y + 16; + if(size && t >=0 && t < 16) { + gbDrawSpriteTile(tile,x-8,yc,t,flags,size,i); + count++; + } else if(!size && t >= 0 && t < 8) { + gbDrawSpriteTile(tile, x-8, yc, t, flags,size,i); + count++; + } + } + // sprite limit reached! + if(count >= 10) + break; + } + } +} + diff --git a/src/gb/gbGlobals.cpp b/src/gb/gbGlobals.cpp new file mode 100644 index 00000000..90b1af15 --- /dev/null +++ b/src/gb/gbGlobals.cpp @@ -0,0 +1,54 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "../GBA.h" + +u8 *gbMemoryMap[16]; + +int gbRomSizeMask = 0; +int gbRomSize = 0; +int gbRamSizeMask = 0; +int gbRamSize = 0; + +u8 *gbMemory = NULL; +u8 *gbVram = NULL; +u8 *gbRom = NULL; +u8 *gbRam = NULL; +u8 *gbWram = NULL; +u16 *gbLineBuffer = NULL; + +u16 gbPalette[128]; +u8 gbBgp[4] = { 0, 1, 2, 3}; +u8 gbObp0[4] = { 0, 1, 2, 3}; +u8 gbObp1[4] = { 0, 1, 2, 3}; +int gbWindowLine = -1; + +int gbCgbMode = 0; + +u16 gbColorFilter[32768]; +int gbColorOption = 0; +int gbPaletteOption = 0; +int gbEmulatorType = 0; +int gbBorderOn = 1; +int gbBorderAutomatic = 0; +int gbBorderLineSkip = 160; +int gbBorderRowSkip = 0; +int gbBorderColumnSkip = 0; +int gbDmaTicks = 0; + +u8 (*gbSerialFunction)(u8) = NULL; diff --git a/src/gb/gbGlobals.h b/src/gb/gbGlobals.h new file mode 100644 index 00000000..380f9331 --- /dev/null +++ b/src/gb/gbGlobals.h @@ -0,0 +1,68 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern int gbRomSizeMask; +extern int gbRomSize; +extern int gbRamSize; +extern int gbRamSizeMask; + +extern u8 *gbRom; +extern u8 *gbRam; +extern u8 *gbVram; +extern u8 *gbWram; +extern u8 *gbMemory; +extern u16 *gbLineBuffer; + +extern u8 *gbMemoryMap[16]; + +extern int gbFrameSkip; +extern u16 gbColorFilter[32768]; +extern int gbColorOption; +extern int gbPaletteOption; +extern int gbEmulatorType; +extern int gbBorderOn; +extern int gbBorderAutomatic; +extern int gbCgbMode; +extern int gbSgbMode; +extern int gbWindowLine; +extern int gbSpeed; +extern u8 gbBgp[4]; +extern u8 gbObp0[4]; +extern u8 gbObp1[4]; +extern u16 gbPalette[128]; + +extern u8 register_LCDC; +extern u8 register_LY; +extern u8 register_SCY; +extern u8 register_SCX; +extern u8 register_WY; +extern u8 register_WX; +extern u8 register_VBK; + +extern int emulating; + +extern int gbBorderLineSkip; +extern int gbBorderRowSkip; +extern int gbBorderColumnSkip; +extern int gbDmaTicks; + +extern void gbRenderLine(); +extern void gbDrawSprites(); + +extern u8 (*gbSerialFunction)(u8); diff --git a/src/gb/gbMemory.cpp b/src/gb/gbMemory.cpp new file mode 100644 index 00000000..be379d3e --- /dev/null +++ b/src/gb/gbMemory.cpp @@ -0,0 +1,967 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "../GBA.h" +#include "../Port.h" +#include "gbGlobals.h" +#include "gbMemory.h" + +mapperMBC1 gbDataMBC1 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // memory model + 0, // ROM high address + 0 // RAM address +}; + +// MBC1 ROM write registers +void mapperMBC1ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: // RAM enable register + gbDataMBC1.mapperRAMEnable = ( ( value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + // value = value & 0x1f; + if(value == 0) + value = 1; + if(value == gbDataMBC1.mapperROMBank) + break; + + tmpAddress = value << 14; + + // check current model + if(gbDataMBC1.mapperMemoryModel == 0) { + // model is 16/8, so we have a high address in use + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + } + + tmpAddress &= gbRomSizeMask; + gbDataMBC1.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + if(gbDataMBC1.mapperMemoryModel == 1) { + // 4/32 model, RAM bank switching provided + value = value & 0x03; + if(value == gbDataMBC1.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataMBC1.mapperRAMBank = value; + gbDataMBC1.mapperRAMAddress = tmpAddress; + } else { + // 16/8, set the high address + gbDataMBC1.mapperROMHighAddress = value & 0x03; + tmpAddress = gbDataMBC1.mapperROMBank << 14; + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x6000: // memory model select + gbDataMBC1.mapperMemoryModel = value & 1; + break; + } +} + +// MBC1 RAM write +void mapperMBC1RAM(u16 address, u8 value) +{ + if(gbDataMBC1.mapperRAMEnable) { + if(gbRamSize) { + gbMemoryMap[address >> 12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC1() +{ + int tmpAddress = gbDataMBC1.mapperROMBank << 14; + + // check current model + if(gbDataMBC1.mapperMemoryModel == 1) { + // model is 16/8, so we have a high address in use + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + } + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if(gbRamSize) { + gbMemoryMap[0x0a] = &gbRam[gbDataMBC1.mapperRAMAddress]; + gbMemoryMap[0x0b] = &gbRam[gbDataMBC1.mapperRAMAddress + 0x1000]; + } +} + +mapperMBC2 gbDataMBC2 = { + 0, // RAM enable + 1 // ROM bank +}; + +// MBC2 ROM write registers +void mapperMBC2ROM(u16 address, u8 value) +{ + switch(address & 0x6000) { + case 0x0000: // RAM enable + if(!(address & 0x0100)) { + gbDataMBC2.mapperRAMEnable = (value & 0x0f) == 0x0a; + } + break; + case 0x2000: // ROM bank select + if(address & 0x0100) { + value &= 0x0f; + + if(value == 0) + value = 1; + if(gbDataMBC2.mapperROMBank != value) { + gbDataMBC2.mapperROMBank = value; + + int tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + } + break; + } +} + +// MBC2 RAM write +void mapperMBC2RAM(u16 address, u8 value) +{ + if(gbDataMBC2.mapperRAMEnable) { + if(gbRamSize && address < 0xa200) { + gbMemoryMap[address >> 12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC2() +{ + int tmpAddress = gbDataMBC2.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; +} + +mapperMBC3 gbDataMBC3 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // timer clock latch + 0, // timer clock register + 0, // timer seconds + 0, // timer minutes + 0, // timer hours + 0, // timer days + 0, // timer control + 0, // timer latched seconds + 0, // timer latched minutes + 0, // timer latched hours + 0, // timer latched days + 0, // timer latched control + (time_t)-1 // last time +}; + +void memoryUpdateMBC3Clock() +{ + time_t now = time(NULL); + time_t diff = now - gbDataMBC3.mapperLastTime; + if(diff > 0) { + // update the clock according to the last update time + gbDataMBC3.mapperSeconds += diff % 60; + if(gbDataMBC3.mapperSeconds > 59) { + gbDataMBC3.mapperSeconds -= 60; + gbDataMBC3.mapperMinutes++; + } + + diff /= 60; + + gbDataMBC3.mapperMinutes += diff % 60; + if(gbDataMBC3.mapperMinutes > 60) { + gbDataMBC3.mapperMinutes -= 60; + gbDataMBC3.mapperHours++; + } + + diff /= 60; + + gbDataMBC3.mapperHours += diff % 24; + if(gbDataMBC3.mapperHours > 24) { + gbDataMBC3.mapperHours -= 24; + gbDataMBC3.mapperDays++; + } + diff /= 24; + + gbDataMBC3.mapperDays += diff; + if(gbDataMBC3.mapperDays > 255) { + if(gbDataMBC3.mapperDays > 511) { + gbDataMBC3.mapperDays %= 512; + gbDataMBC3.mapperControl |= 0x80; + } + gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) | + (gbDataMBC3.mapperDays>255 ? 1 : 0); + } + } + gbDataMBC3.mapperLastTime = now; +} + +// MBC3 ROM write registers +void mapperMBC3ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: // RAM enable register + gbDataMBC3.mapperRAMEnable = ( ( value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if(value == 0) + value = 1; + if(value == gbDataMBC3.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataMBC3.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + break; + case 0x4000: // RAM bank select + if(value < 8) { + if(value == gbDataMBC3.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataMBC3.mapperRAMBank = value; + gbDataMBC3.mapperRAMAddress = tmpAddress; + } else { + if(gbDataMBC3.mapperRAMEnable) { + gbDataMBC3.mapperRAMBank = -1; + + gbDataMBC3.mapperClockRegister = value; + } + } + break; + case 0x6000: // clock latch + if(gbDataMBC3.mapperClockLatch == 0 && value == 1) { + memoryUpdateMBC3Clock(); + gbDataMBC3.mapperLSeconds = gbDataMBC3.mapperSeconds; + gbDataMBC3.mapperLMinutes = gbDataMBC3.mapperMinutes; + gbDataMBC3.mapperLHours = gbDataMBC3.mapperHours; + gbDataMBC3.mapperLDays = gbDataMBC3.mapperDays; + gbDataMBC3.mapperLControl = gbDataMBC3.mapperControl; + } + if(value == 0x00 || value == 0x01) + gbDataMBC3.mapperClockLatch = value; + break; + } +} + +// MBC3 RAM write +void mapperMBC3RAM(u16 address, u8 value) +{ + if(gbDataMBC3.mapperRAMEnable) { + if(gbDataMBC3.mapperRAMBank != -1) { + if(gbRamSize) { + gbMemoryMap[address>>12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } else { + time(&gbDataMBC3.mapperLastTime); + switch(gbDataMBC3.mapperClockRegister) { + case 0x08: + gbDataMBC3.mapperSeconds = value; + break; + case 0x09: + gbDataMBC3.mapperMinutes = value; + break; + case 0x0a: + gbDataMBC3.mapperHours = value; + break; + case 0x0b: + gbDataMBC3.mapperDays = value; + break; + case 0x0c: + if(gbDataMBC3.mapperControl & 0x80) + gbDataMBC3.mapperControl = 0x80 | value; + else + gbDataMBC3.mapperControl = value; + break; + } + } + } +} + +// MBC3 read RAM +u8 mapperMBC3ReadRAM(u16 address) +{ + if(gbDataMBC3.mapperRAMEnable) { + if(gbDataMBC3.mapperRAMBank != -1) { + return gbMemoryMap[address>>12][address & 0x0fff]; + } + + switch(gbDataMBC3.mapperClockRegister) { + case 0x08: + return gbDataMBC3.mapperLSeconds; + break; + case 0x09: + return gbDataMBC3.mapperLMinutes; + break; + case 0x0a: + return gbDataMBC3.mapperLHours; + break; + case 0x0b: + return gbDataMBC3.mapperLDays; + break; + case 0x0c: + return gbDataMBC3.mapperLControl; + } + } + return 0; +} + +void memoryUpdateMapMBC3() +{ + int tmpAddress = gbDataMBC3.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if(gbDataMBC3.mapperRAMBank >= 0 && gbRamSize) { + tmpAddress = gbDataMBC3.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperMBC5 gbDataMBC5 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // ROM high address + 0, // RAM address + 0 // is rumble cartridge? +}; + +// MBC5 ROM write registers +void mapperMBC5ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: // RAM enable register + gbDataMBC5.mapperRAMEnable = ( ( value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + if(address < 0x3000) { + value = value & 0xff; + if(value == gbDataMBC5.mapperROMBank) + break; + + tmpAddress = (value << 14) | (gbDataMBC5.mapperROMHighAddress << 22) ; + + tmpAddress &= gbRomSizeMask; + gbDataMBC5.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + } else { + value = value & 1; + if(value == gbDataMBC5.mapperROMHighAddress) + break; + + tmpAddress = (gbDataMBC5.mapperROMBank << 14) | (value << 22); + + tmpAddress &= gbRomSizeMask; + gbDataMBC5.mapperROMHighAddress = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x4000: // RAM bank select + if(gbDataMBC5.isRumbleCartridge) + value &= 0x07; + else + value &= 0x0f; + if(value == gbDataMBC5.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + if(gbRamSize) { + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + + gbDataMBC5.mapperRAMBank = value; + gbDataMBC5.mapperRAMAddress = tmpAddress; + } + break; + } +} + +// MBC5 RAM write +void mapperMBC5RAM(u16 address, u8 value) +{ + if(gbDataMBC5.mapperRAMEnable) { + if(gbRamSize) { + gbMemoryMap[address >> 12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC5() +{ + int tmpAddress = (gbDataMBC5.mapperROMBank << 14) | + (gbDataMBC5.mapperROMHighAddress << 22) ; + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if(gbRamSize) { + tmpAddress = gbDataMBC5.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperMBC7 gbDataMBC7 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // chip select + 0, // ?? + 0, // mapper state + 0, // buffer for receiving serial data + 0, // idle state + 0, // count of bits received + 0, // command received + 0, // address received + 0, // write enable + 0, // value to return on ram +}; + +// MBC7 ROM write registers +void mapperMBC7ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if(value == 0) + value = 1; + + if(value == gbDataMBC7.mapperROMBank) + break; + + tmpAddress = (value << 14); + + tmpAddress &= gbRomSizeMask; + gbDataMBC7.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select/enable + if(value < 8) { + tmpAddress = (value&3) << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + + gbDataMBC7.mapperRAMBank = value; + gbDataMBC7.mapperRAMAddress = tmpAddress; + gbDataMBC7.mapperRAMEnable = 0; + } else { + gbDataMBC7.mapperRAMEnable = 0; + } + break; + } +} + +// MBC7 read RAM +u8 mapperMBC7ReadRAM(u16 address) +{ + switch(address & 0xa0f0) { + case 0xa000: + case 0xa010: + case 0xa060: + case 0xa070: + return 0; + case 0xa020: + // sensor X low byte + return systemGetSensorX() & 255; + case 0xa030: + // sensor X high byte + return systemGetSensorX() >> 8; + case 0xa040: + // sensor Y low byte + return systemGetSensorY() & 255; + case 0xa050: + // sensor Y high byte + return systemGetSensorY() >> 8; + case 0xa080: + return gbDataMBC7.value; + } + return 0xff; +} + +// MBC7 RAM write +void mapperMBC7RAM(u16 address, u8 value) +{ + if(address == 0xa080) { + // special processing needed + int oldCs = gbDataMBC7.cs,oldSk=gbDataMBC7.sk; + + gbDataMBC7.cs=value>>7; + gbDataMBC7.sk=(value>>6)&1; + + if(!oldCs && gbDataMBC7.cs) { + if(gbDataMBC7.state==5) { + if(gbDataMBC7.writeEnable) { + gbMemory[0xa000+gbDataMBC7.address*2]=gbDataMBC7.buffer>>8; + gbMemory[0xa000+gbDataMBC7.address*2+1]=gbDataMBC7.buffer&0xff; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + gbDataMBC7.state=0; + gbDataMBC7.value=1; + } else { + gbDataMBC7.idle=true; + gbDataMBC7.state=0; + } + } + + if(!oldSk && gbDataMBC7.sk) { + if(gbDataMBC7.idle) { + if(value & 0x02) { + gbDataMBC7.idle=false; + gbDataMBC7.count=0; + gbDataMBC7.state=1; + } + } else { + switch(gbDataMBC7.state) { + case 1: + // receiving command + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value & 0x02)?1:0; + gbDataMBC7.count++; + if(gbDataMBC7.count==2) { + // finished receiving command + gbDataMBC7.state=2; + gbDataMBC7.count=0; + gbDataMBC7.code=gbDataMBC7.buffer & 3; + } + break; + case 2: + // receive address + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value&0x02)?1:0; + gbDataMBC7.count++; + if(gbDataMBC7.count==8) { + // finish receiving + gbDataMBC7.state=3; + gbDataMBC7.count=0; + gbDataMBC7.address=gbDataMBC7.buffer&0xff; + if(gbDataMBC7.code==0) { + if((gbDataMBC7.address>>6)==0) { + gbDataMBC7.writeEnable=0; + gbDataMBC7.state=0; + } else if((gbDataMBC7.address>>6) == 3) { + gbDataMBC7.writeEnable=1; + gbDataMBC7.state=0; + } + } + } + break; + case 3: + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value&0x02)?1:0; + gbDataMBC7.count++; + + switch(gbDataMBC7.code) { + case 0: + if(gbDataMBC7.count==16) { + if((gbDataMBC7.address>>6)==0) { + gbDataMBC7.writeEnable = 0; + gbDataMBC7.state=0; + } else if((gbDataMBC7.address>>6)==1) { + if (gbDataMBC7.writeEnable) { + for(int i=0;i<256;i++) { + gbMemory[0xa000+i*2] = gbDataMBC7.buffer >> 8; + gbMemory[0xa000+i*2+1] = gbDataMBC7.buffer & 0xff; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } + gbDataMBC7.state=5; + } else if((gbDataMBC7.address>>6) == 2) { + if (gbDataMBC7.writeEnable) { + for(int i=0;i<256;i++) + WRITE16LE((u16 *)&gbMemory[0xa000+i*2], 0xffff); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + gbDataMBC7.state=5; + } else if((gbDataMBC7.address>>6)==3) { + gbDataMBC7.writeEnable = 1; + gbDataMBC7.state=0; + } + gbDataMBC7.count=0; + } + break; + case 1: + if(gbDataMBC7.count==16) { + gbDataMBC7.count=0; + gbDataMBC7.state=5; + gbDataMBC7.value=0; + } + break; + case 2: + if(gbDataMBC7.count==1) { + gbDataMBC7.state=4; + gbDataMBC7.count=0; + gbDataMBC7.buffer = (gbMemory[0xa000+gbDataMBC7.address*2]<<8)| + (gbMemory[0xa000+gbDataMBC7.address*2+1]); + } + break; + case 3: + if(gbDataMBC7.count==16) { + gbDataMBC7.count=0; + gbDataMBC7.state=5; + gbDataMBC7.value=0; + gbDataMBC7.buffer=0xffff; + } + break; + } + break; + } + } + } + + if (oldSk && !gbDataMBC7.sk) { + if (gbDataMBC7.state==4) { + gbDataMBC7.value = (gbDataMBC7.buffer & 0x8000)?1:0; + gbDataMBC7.buffer <<= 1; + gbDataMBC7.count++; + if (gbDataMBC7.count==16) { + gbDataMBC7.count=0; + gbDataMBC7.state=0; + } + } + } + } +} + +void memoryUpdateMapMBC7() +{ + int tmpAddress = (gbDataMBC5.mapperROMBank << 14); + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; +} + +mapperHuC1 gbDataHuC1 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // memory model + 0, // ROM high address + 0 // RAM address +}; + +// HuC1 ROM write registers +void mapperHuC1ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: // RAM enable register + gbDataHuC1.mapperRAMEnable = ( ( value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + value = value & 0x3f; + if(value == 0) + value = 1; + if(value == gbDataHuC1.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataHuC1.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + if(gbDataHuC1.mapperMemoryModel == 1) { + // 4/32 model, RAM bank switching provided + value = value & 0x03; + if(value == gbDataHuC1.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataHuC1.mapperRAMBank = value; + gbDataHuC1.mapperRAMAddress = tmpAddress; + } else { + // 16/8, set the high address + gbDataHuC1.mapperROMHighAddress = value & 0x03; + tmpAddress = gbDataHuC1.mapperROMBank << 14; + tmpAddress |= (gbDataHuC1.mapperROMHighAddress) << 19; + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x6000: // memory model select + gbDataHuC1.mapperMemoryModel = value & 1; + break; + } +} + +// HuC1 RAM write +void mapperHuC1RAM(u16 address, u8 value) +{ + if(gbDataHuC1.mapperRAMEnable) { + if(gbRamSize) { + gbMemoryMap[address >> 12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapHuC1() +{ + int tmpAddress = gbDataHuC1.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if(gbRamSize) { + tmpAddress = gbDataHuC1.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperHuC3 gbDataHuC3 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // RAM flag + 0 // RAM read value +}; + + +// HuC3 ROM write registers +void mapperHuC3ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch(address & 0x6000) { + case 0x0000: // RAM enable register + gbDataHuC3.mapperRAMEnable = ( value == 0x0a ? 1 : 0); + gbDataHuC3.mapperRAMFlag = value; + if(gbDataHuC3.mapperRAMFlag != 0x0a) + gbDataHuC3.mapperRAMBank = -1; + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if(value == 0) + value = 1; + if(value == gbDataHuC3.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataHuC3.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + value = value & 0x03; + if(value == gbDataHuC3.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataHuC3.mapperRAMBank = value; + gbDataHuC3.mapperRAMAddress = tmpAddress; + break; + case 0x6000: // nothing to do! + break; + } +} + +// HuC3 read RAM +u8 mapperHuC3ReadRAM(u16 address) +{ + if(gbDataHuC3.mapperRAMFlag > 0x0b && + gbDataHuC3.mapperRAMFlag < 0x0e) { + if(gbDataHuC3.mapperRAMFlag != 0x0c) + return 1; + return gbDataHuC3.mapperRAMValue; + } else + return gbMemoryMap[address >> 12][address & 0x0fff]; +} + +// HuC3 RAM write +void mapperHuC3RAM(u16 address, u8 value) +{ + int *p; + + if(gbDataHuC3.mapperRAMFlag < 0x0b || + gbDataHuC3.mapperRAMFlag > 0x0e) { + if(gbDataHuC3.mapperRAMEnable) { + if(gbRamSize) { + gbMemoryMap[address >> 12][address & 0x0fff] = value; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } + } else { + if(gbDataHuC3.mapperRAMFlag == 0x0b) { + if(value == 0x62) { + gbDataHuC3.mapperRAMValue = 1; + } else { + switch(value & 0xf0) { + case 0x10: + p = &gbDataHuC3.mapperRegister2; + gbDataHuC3.mapperRAMValue = *(p+gbDataHuC3.mapperRegister1++); + if(gbDataHuC3.mapperRegister1 > 6) + gbDataHuC3.mapperRegister1 = 0; + break; + case 0x30: + p = &gbDataHuC3.mapperRegister2; + *(p+gbDataHuC3.mapperRegister1++) = value & 0x0f; + if(gbDataHuC3.mapperRegister1 > 6) + gbDataHuC3.mapperRegister1 = 0; + gbDataHuC3.mapperAddress = + (gbDataHuC3.mapperRegister6 << 24) | + (gbDataHuC3.mapperRegister5 << 16) | + (gbDataHuC3.mapperRegister4 << 8) | + (gbDataHuC3.mapperRegister3 << 4) | + (gbDataHuC3.mapperRegister2); + break; + case 0x40: + gbDataHuC3.mapperRegister1 = (gbDataHuC3.mapperRegister1 & 0xf0) | + (value & 0x0f); + gbDataHuC3.mapperRegister2 = (gbDataHuC3.mapperAddress & 0x0f); + gbDataHuC3.mapperRegister3 = ((gbDataHuC3.mapperAddress>>4)&0x0f); + gbDataHuC3.mapperRegister4 = ((gbDataHuC3.mapperAddress>>8)&0x0f); + gbDataHuC3.mapperRegister5 = ((gbDataHuC3.mapperAddress>>16)&0x0f); + gbDataHuC3.mapperRegister6 = ((gbDataHuC3.mapperAddress>>24)&0x0f); + gbDataHuC3.mapperRegister7 = 0; + gbDataHuC3.mapperRegister8 = 0; + gbDataHuC3.mapperRAMValue = 0; + break; + case 0x50: + gbDataHuC3.mapperRegister1 = (gbDataHuC3.mapperRegister1 & 0x0f) | + ((value << 4)&0x0f); + break; + default: + gbDataHuC3.mapperRAMValue = 1; + break; + } + } + } + } +} + +void memoryUpdateMapHuC3() +{ + int tmpAddress = gbDataHuC3.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if(gbRamSize) { + tmpAddress = gbDataHuC3.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} diff --git a/src/gb/gbMemory.h b/src/gb/gbMemory.h new file mode 100644 index 00000000..9ce19edd --- /dev/null +++ b/src/gb/gbMemory.h @@ -0,0 +1,149 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include + +struct mapperMBC1 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperMemoryModel; + int mapperROMHighAddress; + int mapperRAMAddress; +}; + +struct mapperMBC2 { + int mapperRAMEnable; + int mapperROMBank; +}; + +struct mapperMBC3 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperRAMAddress; + int mapperClockLatch; + int mapperClockRegister; + int mapperSeconds; + int mapperMinutes; + int mapperHours; + int mapperDays; + int mapperControl; + int mapperLSeconds; + int mapperLMinutes; + int mapperLHours; + int mapperLDays; + int mapperLControl; + time_t mapperLastTime; +}; + +struct mapperMBC5 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperROMHighAddress; + int mapperRAMAddress; + int isRumbleCartridge; +}; + +struct mapperMBC7 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperRAMAddress; + int cs; + int sk; + int state; + int buffer; + int idle; + int count; + int code; + int address; + int writeEnable; + int value; +}; + +struct mapperHuC1 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperMemoryModel; + int mapperROMHighAddress; + int mapperRAMAddress; +}; + +struct mapperHuC3 { + int mapperRAMEnable; + int mapperROMBank; + int mapperRAMBank; + int mapperRAMAddress; + int mapperAddress; + int mapperRAMFlag; + int mapperRAMValue; + int mapperRegister1; + int mapperRegister2; + int mapperRegister3; + int mapperRegister4; + int mapperRegister5; + int mapperRegister6; + int mapperRegister7; + int mapperRegister8; +}; + +extern mapperMBC1 gbDataMBC1; +extern mapperMBC2 gbDataMBC2; +extern mapperMBC3 gbDataMBC3; +extern mapperMBC5 gbDataMBC5; +extern mapperHuC1 gbDataHuC1; +extern mapperHuC3 gbDataHuC3; + +void mapperMBC1ROM(u16,u8); +void mapperMBC1RAM(u16,u8); +void mapperMBC2ROM(u16,u8); +void mapperMBC2RAM(u16,u8); +void mapperMBC3ROM(u16,u8); +void mapperMBC3RAM(u16,u8); +u8 mapperMBC3ReadRAM(u16); +void mapperMBC5ROM(u16,u8); +void mapperMBC5RAM(u16,u8); +void mapperMBC7ROM(u16,u8); +void mapperMBC7RAM(u16,u8); +u8 mapperMBC7ReadRAM(u16); +void mapperHuC1ROM(u16,u8); +void mapperHuC1RAM(u16,u8); +void mapperHuC3ROM(u16,u8); +void mapperHuC3RAM(u16,u8); +u8 mapperHuC3ReadRAM(u16); + +//extern void (*mapper)(u16,u8); +//extern void (*mapperRAM)(u16,u8); +//extern u8 (*mapperReadRAM)(u16); + +extern void memoryUpdateMapMBC1(); +extern void memoryUpdateMapMBC2(); +extern void memoryUpdateMapMBC3(); +extern void memoryUpdateMapMBC5(); +extern void memoryUpdateMapMBC7(); +extern void memoryUpdateMapHuC1(); +extern void memoryUpdateMapHuC3(); + + + + + diff --git a/src/gb/gbPrinter.cpp b/src/gb/gbPrinter.cpp new file mode 100644 index 00000000..cf676c9b --- /dev/null +++ b/src/gb/gbPrinter.cpp @@ -0,0 +1,229 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include "../GBA.h" + +u8 gbPrinterStatus = 0; +int gbPrinterState = 0; +u8 gbPrinterData[0x280*9]; +u8 gbPrinterPacket[0x400]; +int gbPrinterCount = 0; +int gbPrinterDataCount = 0; +int gbPrinterDataSize = 0; +int gbPrinterResult = 0; + +bool gbPrinterCheckCRC() +{ + u16 crc = 0; + + for(int i = 2; i < (6+gbPrinterDataSize); i++) { + crc += gbPrinterPacket[i]; + } + + int msgCrc = gbPrinterPacket[6+gbPrinterDataSize] + + (gbPrinterPacket[7+gbPrinterDataSize]<<8); + + return msgCrc == crc; +} + +void gbPrinterReset() +{ + gbPrinterState = 0; + gbPrinterDataSize = 0; + gbPrinterDataCount = 0; + gbPrinterCount = 0; + gbPrinterStatus = 0; + gbPrinterResult = 0; +} + +void gbPrinterShowData() +{ + systemGbPrint(gbPrinterData, + gbPrinterPacket[6], + gbPrinterPacket[7], + gbPrinterPacket[8], + gbPrinterPacket[9]); + /* + allegro_init(); + install_keyboard(); + set_gfx_mode(GFX_AUTODETECT, 160, 144, 0, 0); + PALETTE pal; + pal[0].r = 255; + pal[0].g = 255; + pal[0].b = 255; + pal[1].r = 168; + pal[1].g = 168; + pal[1].b = 168; + pal[2].r = 96; + pal[2].g = 96; + pal[2].b = 96; + pal[3].r = 0; + pal[3].g = 0; + pal[3].b = 0; + set_palette(pal); + acquire_screen(); + u8 *data = gbPrinterData; + for(int y = 0; y < 0x12; y++) { + for(int x = 0; x < 0x14; x++) { + for(int k = 0; k < 8; k++) { + int a = *data++; + int b = *data++; + for(int j = 0; j < 8; j++) { + int mask = 1 << (7-j); + int c = 0; + if(a & mask) + c++; + if(b & mask) + c+=2; + putpixel(screen, x*8+j, y*8+k, c); + } + } + } + } + release_screen(); + while(!keypressed()) { + } + */ +} + +void gbPrinterReceiveData() +{ + if(gbPrinterPacket[3]) { // compressed + u8 *data = &gbPrinterPacket[6]; + u8 *dest = &gbPrinterData[gbPrinterDataCount]; + int len = 0; + while(len < gbPrinterDataSize) { + u8 control = *data++; + if(control & 0x80) { // repeated data + control &= 0x7f; + control += 2; + memset(dest, *data++, control); + len += control; + dest += control; + } else { // raw data + control++; + memcpy(dest, data, control); + dest += control; + data += control; + len += control; + } + } + } else { + memcpy(&gbPrinterData[gbPrinterDataCount], + &gbPrinterPacket[6], + gbPrinterDataSize); + gbPrinterDataCount += gbPrinterDataSize; + } +} + +void gbPrinterCommand() +{ + switch(gbPrinterPacket[2]) { + case 0x01: + // reset/initialize packet + gbPrinterDataCount = 0; + gbPrinterStatus = 0; + break; + case 0x02: + // print packet + gbPrinterShowData(); + break; + case 0x04: + // data packet + gbPrinterReceiveData(); + break; + case 0x0f: + // NUL packet + break; + } +} + +u8 gbPrinterSend(u8 b) +{ + switch(gbPrinterState) { + case 0: + gbPrinterCount = 0; + // receiving preamble + if(b == 0x88) { + gbPrinterPacket[gbPrinterCount++] = b; + gbPrinterState++; + } else { + // todo: handle failure + gbPrinterReset(); + } + break; + case 1: + // receiving preamble + if(b == 0x33) { + gbPrinterPacket[gbPrinterCount++] = b; + gbPrinterState++; + } else { + // todo: handle failure + gbPrinterReset(); + } + break; + case 2: + // receiving header + gbPrinterPacket[gbPrinterCount++] = b; + if(gbPrinterCount == 6) { + gbPrinterState++; + gbPrinterDataSize = gbPrinterPacket[4] + (gbPrinterPacket[5]<<8); + } + break; + case 3: + // receiving data + if(gbPrinterDataSize) { + gbPrinterPacket[gbPrinterCount++] = b; + if(gbPrinterCount == (6+gbPrinterDataSize)) { + gbPrinterState++; + } + break; + } + gbPrinterState++; + // intentionally move to next if no data to receive + case 4: + // receiving CRC + gbPrinterPacket[gbPrinterCount++] = b; + gbPrinterState++; + break; + case 5: + // receiving CRC-2 + gbPrinterPacket[gbPrinterCount++] = b; + if(gbPrinterCheckCRC()) { + gbPrinterCommand(); + } + gbPrinterState++; + break; + case 6: + // receiving dummy 1 + gbPrinterPacket[gbPrinterCount++] = b; + gbPrinterResult = 0x81; + gbPrinterState++; + break; + case 7: + // receiving dummy 2 + gbPrinterPacket[gbPrinterCount++] = b; + gbPrinterResult = gbPrinterStatus; + gbPrinterState = 0; + gbPrinterCount = 0; + break; + } + return gbPrinterResult; +} diff --git a/src/gb/gbPrinter.h b/src/gb/gbPrinter.h new file mode 100644 index 00000000..9bebd4e3 --- /dev/null +++ b/src/gb/gbPrinter.h @@ -0,0 +1,20 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern u8 gbPrinterSend(u8 b); diff --git a/src/gb/gbSGB.cpp b/src/gb/gbSGB.cpp new file mode 100644 index 00000000..dc7a1d80 --- /dev/null +++ b/src/gb/gbSGB.cpp @@ -0,0 +1,917 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include + +#include "../win32/stdafx.h" +#include "../win32/VBA.h" +#include "../System.h" +#include "../Port.h" +#include "../Util.h" +#include "GB.h" +#include "gbGlobals.h" + +extern u8 *pix; +extern bool speedup; + +#define GBSGB_NONE 0 +#define GBSGB_RESET 1 +#define GBSGB_PACKET_TRANSMIT 2 + +u8 *gbSgbBorderChar = NULL; +u8 *gbSgbBorder = NULL; + +int gbSgbCGBSupport = 0; +int gbSgbMask = 0; +int gbSgbMode = 0; +int gbSgbPacketState = GBSGB_NONE; +int gbSgbBit = 0; +int gbSgbPacketTimeout = 0; +int GBSGB_PACKET_TIMEOUT = 66666; +u8 gbSgbPacket[16*7]; +int gbSgbPacketNBits = 0; +int gbSgbPacketByte = 0; +int gbSgbPacketNumber = 0; +int gbSgbMultiplayer = 0; +int gbSgbFourPlayers = 0; +u8 gbSgbNextController = 0x0f; +u8 gbSgbReadingController = 0; +u16 gbSgbSCPPalette[4*512]; +u8 gbSgbATF[20 * 18]; +u8 gbSgbATFList[45 * 20 * 18]; +u8 gbSgbScreenBuffer[4160]; + +inline void gbSgbDraw24Bit(u8 *p, u16 v) +{ + *((u32*) p) = systemColorMap32[v]; +} + +inline void gbSgbDraw32Bit(u32 *p, u16 v) +{ + *p = systemColorMap32[v]; +} + +inline void gbSgbDraw16Bit(u16 *p, u16 v) +{ + *p = systemColorMap16[v]; +} + +void gbSgbReset() +{ + gbSgbPacketTimeout = 0; + gbSgbCGBSupport = 0; + gbSgbMask = 0; + gbSgbPacketState = GBSGB_NONE; + gbSgbBit = 0; + gbSgbPacketNBits = 0; + gbSgbPacketNumber = 0; + gbSgbMultiplayer = 0; + gbSgbFourPlayers = 0; + gbSgbNextController = 0x0f; + gbSgbReadingController = 0; + + memset(gbSgbSCPPalette, 0, 512*4); + memset(gbSgbATF, 0, 20*18); + memset(gbSgbATFList, 0, 45 * 20 * 18); + memset(gbSgbPacket, 0, 16 * 7); + memset(gbSgbBorderChar, 0, 32*256); + memset(gbSgbBorder, 0, 2048); + + int i; + for(i = 1; i < 2048; i+=2) { + gbSgbBorder[i] = 1 << 2; + } + + for(i = 0; i < 4; i++) { + gbPalette[i*4] = (0x1f) | (0x1f << 5) | (0x1f << 10); + gbPalette[i*4+1] = (0x15) | (0x15 << 5) | (0x15 << 10); + gbPalette[i*4+2] = (0x0c) | (0x0c << 5) | (0x0c << 10); + gbPalette[i*4+3] = 0; + } +} + +void gbSgbInit() +{ + gbSgbBorderChar = (u8 *)malloc(32 * 256); + gbSgbBorder = (u8 *)malloc(2048); + + gbSgbReset(); +} + +void gbSgbShutdown() +{ + if(gbSgbBorderChar != NULL) { + free(gbSgbBorderChar); + gbSgbBorderChar = NULL; + } + + if(gbSgbBorder != NULL) { + free(gbSgbBorder); + gbSgbBorder = NULL; + } +} + +void gbSgbFillScreen(u16 color) +{ + switch(systemColorDepth) { + case 16: + { + for(int y = 0; y < 144; y++) { + int yLine = (y+gbBorderRowSkip+1)*(gbBorderLineSkip+2) + + gbBorderColumnSkip; + u16 *dest = (u16*)pix + yLine; + for(register int x = 0; x < 160; x++) + gbSgbDraw16Bit(dest++, color); + } + } + break; + case 24: + { + for(int y = 0; y < 144; y++) { + int yLine = (y+gbBorderRowSkip)*gbBorderLineSkip + gbBorderColumnSkip; + u8 *dest = (u8 *)pix + yLine*3; + for(register int x = 0; x < 160; x++) { + gbSgbDraw24Bit(dest, color); + dest += 3; + } + } + } + break; + case 32: + { + for(int y = 0; y < 144; y++) { + int yLine = (y+gbBorderRowSkip+1)*(gbBorderLineSkip+1) + gbBorderColumnSkip; + u32 *dest = (u32 *)pix + yLine; + for(register int x = 0; x < 160; x++) { + gbSgbDraw32Bit(dest++, color); + } + } + } + break; + } +} + +#define getmem(x) gbMemoryMap[(x) >> 12][(x) & 0xfff] + +void gbSgbRenderScreenToBuffer() +{ + u16 mapAddress = 0x9800; + + if(register_LCDC & 0x08) + mapAddress = 0x9c00; + + u16 patternAddress = 0x8800; + + int flag = 1; + + if(register_LCDC & 0x10) { + patternAddress = 0x8000; + flag = 0; + } + + u8 *toAddress = gbSgbScreenBuffer; + + for(int i = 0; i < 13; i++) { + for(int j = 0; j < 20; j++) { + int tile = getmem(mapAddress); + mapAddress++; + + if(flag) { + if(tile > 127) + tile -= 128; + else + tile += 128; + } + for(int k = 0; k < 16; k++) + *toAddress++ = getmem(patternAddress + tile*16 + k); + } + mapAddress += 12; + } +} + +void gbSgbDrawBorderTile(int x, int y, int tile, int attr) +{ + u16 *dest = (u16*)pix + ((y+1) * (256+2)) + x; + u8 *dest8 = (u8*)pix + ((y*256)+x)*3; + u32 *dest32 = (u32*)pix + ((y+1)*257) + x; + + u8 *tileAddress = &gbSgbBorderChar[tile * 32]; + u8 *tileAddress2 = &gbSgbBorderChar[tile * 32 + 16]; + + u8 l = 8; + + u8 palette = ((attr >> 2 ) & 7); + + if(palette < 4) + palette += 4; + + palette *= 16; + + u8 xx = 0; + u8 yy = 0; + + int flipX = attr & 0x40; + int flipY = attr & 0x80; + + while(l > 0) { + u8 mask = 0x80; + u8 a = *tileAddress++; + u8 b = *tileAddress++; + u8 c = *tileAddress2++; + u8 d = *tileAddress2++; + + while(mask > 0) { + + u8 color = 0; + if(a & mask) + color++; + if(b & mask) + color+=2; + if(c & mask) + color+=4; + if(d & mask) + color+=8; + + u8 xxx = xx; + u8 yyy = yy; + + if(flipX) + xxx = 7 - xx; + if(flipY) + yyy = 7 - yy; + + u16 c = gbPalette[palette + color]; + if(!color) + c = gbPalette[0]; + if((yy < 40 || yy >= 184) || (xx < 48 || xx >= 208)) { + switch(systemColorDepth) { + case 16: + gbSgbDraw16Bit(dest + yyy*(256+2) + xxx, c); + break; + case 24: + gbSgbDraw24Bit(dest8 + (yyy*256+xxx)*3, c); + break; + case 32: + gbSgbDraw32Bit(dest32 + yyy*(256+1)+xxx, c); + break; + } + } + + mask >>= 1; + + xx++; + } + yy++; + xx = 0; + l--; + mask = 0x80; + } +} + +void gbSgbRenderBorder() +{ + if(gbBorderOn) { + if (theApp.filterLCD) utilUpdateSystemColorMaps(0); + u8 *fromAddress = gbSgbBorder; + + for(u8 y = 0; y < 28; y++) { + for(u8 x = 0; x< 32; x++) { + u8 tile = *fromAddress++; + u8 attr = *fromAddress++; + + gbSgbDrawBorderTile(x*8,y*8,tile,attr); + } + } + if (theApp.filterLCD) utilUpdateSystemColorMaps(1); + } +} + +void gbSgbPicture() +{ + gbSgbRenderScreenToBuffer(); + + memcpy(gbSgbBorder, gbSgbScreenBuffer, 2048); + + u16 *paletteAddr = (u16 *)&gbSgbScreenBuffer[2048]; + + for(int i = 64; i < 128; i++) { + gbPalette[i] = READ16LE(paletteAddr++); + } + + gbSgbCGBSupport |= 4; + + if(gbBorderAutomatic && !gbBorderOn && gbSgbCGBSupport > 4) { + gbBorderOn = 1; + systemGbBorderOn(); + } + + if(gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); + + if(gbSgbMode && gbCgbMode && gbSgbCGBSupport > 4) { + gbSgbCGBSupport = 0; + gbSgbMode = 0; + gbSgbMask = 0; + gbSgbRenderBorder(); + gbReset(); + } + + if(gbSgbCGBSupport > 4) + gbSgbCGBSupport = 0; +} + +void gbSgbSetPalette(int a,int b,u16 *p) +{ + u16 bit00 = READ16LE(p++); + int i; + + for(i = 1; i < 4; i++) { + gbPalette[a*4+i] = READ16LE(p++); + } + + for(i = 1; i < 4; i++) { + gbPalette[b*4+i] = READ16LE(p++); + } + + gbPalette[0] = gbPalette[4] = gbPalette[8] = gbPalette[12] = bit00; + if(gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); +} + +void gbSgbScpPalette() +{ + gbSgbRenderScreenToBuffer(); + + u16 *fromAddress = (u16 *)gbSgbScreenBuffer; + + for(int i = 0; i < 512*4; i++) { + gbSgbSCPPalette[i] = READ16LE(fromAddress++); + } +} + +void gbSgbSetATF(int n) +{ + if(n < 0) + n = 0; + if(n > 44) + n = 44; + memcpy(gbSgbATF,&gbSgbATFList[n * 20 * 18], 20 * 18); + + if(gbSgbPacket[1] & 0x40) { + gbSgbMask = 0; + if(gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbSetPalette() +{ + u16 pal = READ16LE((((u16 *)&gbSgbPacket[1])))&511; + memcpy(&gbPalette[0], &gbSgbSCPPalette[pal*4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[3])))&511; + memcpy(&gbPalette[4], &gbSgbSCPPalette[pal*4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[5])))&511; + memcpy(&gbPalette[8], &gbSgbSCPPalette[pal*4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[7])))&511; + memcpy(&gbPalette[12], &gbSgbSCPPalette[pal*4], 4 * sizeof(u16)); + + u8 atf = gbSgbPacket[9]; + + if(atf & 0x80) { + gbSgbSetATF(atf & 0x3f); + } + + if(atf & 0x40) { + gbSgbMask = 0; + if(gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbAttributeBlock() +{ + u8 *fromAddress = &gbSgbPacket[1]; + + u8 nDataSet = *fromAddress++; + if(nDataSet > 12) + nDataSet = 12; + if(nDataSet == 0) + nDataSet = 1; + + while(nDataSet) { + u8 controlCode = (*fromAddress++) & 7; + u8 paletteDesignation = (*fromAddress++) & 0x3f; + u8 startH = (*fromAddress++) & 0x1f; + u8 startV = (*fromAddress++) & 0x1f; + u8 endH = (*fromAddress++) & 0x1f; + u8 endV = (*fromAddress++) & 0x1f; + + u8 * toAddress = gbSgbATF; + + for(u8 y = 0; y < 18; y++) { + for(u8 x = 0; x < 20; x++) { + if(x < startH || y < startV || + x > endH || y > endV) { + // outside + if(controlCode & 0x04) + *toAddress = (paletteDesignation >> 4) & 0x03; + } else if(x > startH && x < endH && + y > startV && y < endV) { + // inside + if(controlCode & 0x01) + *toAddress = paletteDesignation & 0x03; + } else { + // surrounding line + if(controlCode & 0x02) + *toAddress = (paletteDesignation>>2) & 0x03; + else if(controlCode == 0x01) + *toAddress = paletteDesignation & 0x03; + } + toAddress++; + } + } + nDataSet--; + } +} + +void gbSgbSetColumnPalette(u8 col, u8 p) +{ + // if(col < 0) + // col = 0; + if(col > 19) + col = 19; + + p &= 3; + + u8 *toAddress = &gbSgbATF[col]; + + for(u8 y = 0; y < 18; y++) { + *toAddress = p; + toAddress += 20; + } +} + +void gbSgbSetRowPalette(u8 row, u8 p) +{ + // if(row < 0) + // row = 0; + if(row > 17) + row = 17; + + p &= 3; + + u8 *toAddress = &gbSgbATF[row*20]; + + for(u8 x = 0; x < 20; x++) { + *toAddress++ = p; + } +} + +void gbSgbAttributeDivide() +{ + u8 control = gbSgbPacket[1]; + u8 coord = gbSgbPacket[2]; + u8 colorBR = control & 3; + u8 colorAL = (control >> 2) & 3; + u8 colorOL = (control >> 4) & 3; + + if(control & 0x40) { + if(coord > 17) + coord = 17; + + for(u8 i = 0; i < 18; i++) { + if(i < coord) + gbSgbSetRowPalette(i, colorAL); + else if ( i > coord) + gbSgbSetRowPalette(i, colorBR); + else + gbSgbSetRowPalette(i, colorOL); + } + } else { + if(coord > 19) + coord = 19; + + for(u8 i = 0; i < 20; i++) { + if(i < coord) + gbSgbSetColumnPalette(i, colorAL); + else if ( i > coord) + gbSgbSetColumnPalette(i, colorBR); + else + gbSgbSetColumnPalette(i, colorOL); + } + } +} + +void gbSgbAttributeLine() +{ + u8 *fromAddress = &gbSgbPacket[1]; + + u8 nDataSet = *fromAddress++; + + if(nDataSet > 0x6e) + nDataSet = 0x6e; + + while(nDataSet) { + u8 line = *fromAddress++; + u8 num = line & 0x1f; + u8 pal = (line >> 5) & 0x03; + if(line & 0x80) { + if(num > 17) + num = 17; + gbSgbSetRowPalette(num,pal); + } else { + if(num > 19) + num = 19; + gbSgbSetColumnPalette(num,pal); + } + nDataSet--; + } +} + +void gbSgbAttributeCharacter() +{ + u8 startH = gbSgbPacket[1] & 0x1f; + u8 startV = gbSgbPacket[2] & 0x1f; + int nDataSet = READ16LE(((u16 *)&gbSgbPacket[3])); + int style = gbSgbPacket[5] & 1; + if(startH > 19) + startH = 19; + if(startV > 17) + startV = 17; + + u8 s = 6; + u8 *fromAddress = &gbSgbPacket[6]; + u8 v = *fromAddress++; + + if(style) { + while(nDataSet) { + u8 p = (v >> s) & 3; + gbSgbATF[startV * 20 + startH] = p; + startV++; + if(startV == 18) { + startV = 0; + startH++; + if(startH == 20) + break; + } + + if(s) + s -= 2; + else { + s = 6; + v = *fromAddress++; + nDataSet--; + } + } + } else { + while(nDataSet) { + u8 p = (v >> s) & 3; + gbSgbATF[startV * 20 + startH] = p; + startH++; + if(startH == 20) { + startH = 0; + startV++; + if(startV == 18) + break; + } + + if(s) + s -= 2; + else { + s = 6; + v = *fromAddress++; + nDataSet--; + } + } + } +} + +void gbSgbSetATFList() +{ + gbSgbRenderScreenToBuffer(); + + u8 *fromAddress = gbSgbScreenBuffer; + u8 *toAddress = gbSgbATFList; + + for(int i = 0; i < 45; i++) { + for(int j = 0; j < 90; j++) { + u8 v = *fromAddress++; + u8 s = 6; + if(i == 2) + s = 6; + for(int k = 0; k < 4; k++) { + *toAddress++ = (v >> s) & 0x03; + s -= 2; + } + } + } +} + +void gbSgbMaskEnable() +{ + int gbSgbMaskFlag = gbSgbPacket[1] & 3; + + gbSgbMask = gbSgbMaskFlag; + + switch(gbSgbMaskFlag) { + case 1: + break; + case 2: + gbSgbFillScreen(0x0000); + // memset(&gbPalette[0], 0, 128*sizeof(u16)); + break; + case 3: + gbSgbFillScreen(gbPalette[0]); + break; + } + if(!gbSgbMask) { + if(gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbChrTransfer() +{ + gbSgbRenderScreenToBuffer(); + + int address = (gbSgbPacket[1] & 1) * (128*32); + + if(gbSgbPacket[1] & 1) + gbSgbCGBSupport |= 2; + else + gbSgbCGBSupport |= 1; + + memcpy(&gbSgbBorderChar[address], gbSgbScreenBuffer, 128 * 32); + + if(gbBorderAutomatic && !gbBorderOn && gbSgbCGBSupport > 4) { + gbBorderOn = 1; + systemGbBorderOn(); + } + + if(gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); + + if(gbSgbMode && gbCgbMode && gbSgbCGBSupport == 7) { + gbSgbCGBSupport = 0; + gbSgbMode = 0; + gbSgbMask = 0; + gbSgbRenderBorder(); + gbReset(); + } + + if(gbSgbCGBSupport > 4) + gbSgbCGBSupport = 0; +} + +void gbSgbMultiRequest() +{ + if(gbSgbPacket[1] & 1) { + gbSgbMultiplayer = 1; + if(gbSgbPacket[1] & 2) + gbSgbFourPlayers = 1; + else + gbSgbFourPlayers = 0; + gbSgbNextController = 0x0e; + } else { + gbSgbFourPlayers = 0; + gbSgbMultiplayer = 0; + gbSgbNextController = 0x0f; + } +} + +void gbSgbCommand() +{ + int command = gbSgbPacket[0] >> 3; + // int nPacket = gbSgbPacket[0] & 7; + + switch(command) { + case 0x00: + gbSgbSetPalette(0,1,(u16 *)&gbSgbPacket[1]); + break; + case 0x01: + gbSgbSetPalette(2,3,(u16 *)&gbSgbPacket[1]); + break; + case 0x02: + gbSgbSetPalette(0,3,(u16 *)&gbSgbPacket[1]); + break; + case 0x03: + gbSgbSetPalette(1,2,(u16 *)&gbSgbPacket[1]); + break; + case 0x04: + gbSgbAttributeBlock(); + break; + case 0x05: + gbSgbAttributeLine(); + break; + case 0x06: + gbSgbAttributeDivide(); + break; + case 0x07: + gbSgbAttributeCharacter(); + break; + case 0x0a: + gbSgbSetPalette(); + break; + case 0x0b: + gbSgbScpPalette(); + break; + case 0x11: + gbSgbMultiRequest(); + break; + case 0x13: + gbSgbChrTransfer(); + break; + case 0x14: + gbSgbPicture(); + break; + case 0x15: + gbSgbSetATFList(); + break; + case 0x16: + gbSgbSetATF(gbSgbPacket[1] & 0x3f); + break; + case 0x17: + gbSgbMaskEnable(); + break; + } +} + +void gbSgbResetPacketState() +{ + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; +} + +void gbSgbDoBitTransfer(u8 value) +{ + value = value & 0x30; + switch(gbSgbPacketState) { + case GBSGB_NONE: + if(value == 0) { + gbSgbPacketState = GBSGB_RESET; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } else if (value == 0x30) { + if(gbSgbMultiplayer) { + if((gbSgbReadingController & 7) == 7) { + gbSgbReadingController = 0; + if(gbSgbMultiplayer) { + gbSgbNextController--; + if(gbSgbFourPlayers) { + if(gbSgbNextController == 0x0b) + gbSgbNextController = 0x0f; + } else { + if(gbSgbNextController == 0x0d) + gbSgbNextController = 0x0f; + } + } + } else { + gbSgbReadingController &= 3; + } + } + gbSgbPacketTimeout = 0; + } else { + if(value == 0x10) + gbSgbReadingController |= 0x2; + else if(value == 0x20) + gbSgbReadingController |= 0x01; + gbSgbPacketTimeout = 0; + } + gbSgbPacketTimeout = 0; + break; + case GBSGB_RESET: + if(value == 0x30) { + gbSgbPacketState = GBSGB_PACKET_TRANSMIT; + gbSgbPacketByte = 0; + gbSgbPacketNBits = 0; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } else if(value == 0x00) { + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + gbSgbPacketState = GBSGB_RESET; + } else { + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + } + break; + case GBSGB_PACKET_TRANSMIT: + if(value == 0) { + gbSgbPacketState = GBSGB_RESET; + gbSgbPacketTimeout = 0; + } else if (value == 0x30){ + if(gbSgbPacketNBits == 128) { + gbSgbPacketNBits = 0; + gbSgbPacketByte = 0; + gbSgbPacketNumber++; + gbSgbPacketTimeout = 0; + if(gbSgbPacketNumber == (gbSgbPacket[0] & 7)) { + gbSgbCommand(); + gbSgbPacketNumber = 0; + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + } + } else { + if(gbSgbPacketNBits < 128) { + gbSgbPacket[gbSgbPacketNumber * 16 + gbSgbPacketByte] >>= 1; + gbSgbPacket[gbSgbPacketNumber * 16 + gbSgbPacketByte] |= gbSgbBit; + gbSgbPacketNBits++; + if(!(gbSgbPacketNBits & 7)) { + gbSgbPacketByte++; + } + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + } + } else { + if(value == 0x20) + gbSgbBit = 0x00; + else + gbSgbBit = 0x80; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + gbSgbReadingController = 0; + break; + default: + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + break; + } +} + +variable_desc gbSgbSaveStruct[] = { + { &gbSgbMask, sizeof(int) }, + { &gbSgbPacketState, sizeof(int) }, + { &gbSgbBit, sizeof(int) }, + { &gbSgbPacketNBits, sizeof(int) }, + { &gbSgbPacketByte, sizeof(int) }, + { &gbSgbPacketNumber, sizeof(int) }, + { &gbSgbMultiplayer, sizeof(int) }, + { &gbSgbNextController, sizeof(u8) }, + { &gbSgbReadingController, sizeof(u8) }, + { NULL, 0 } +}; + +variable_desc gbSgbSaveStructV3[] = { + { &gbSgbMask, sizeof(int) }, + { &gbSgbPacketState, sizeof(int) }, + { &gbSgbBit, sizeof(int) }, + { &gbSgbPacketNBits, sizeof(int) }, + { &gbSgbPacketByte, sizeof(int) }, + { &gbSgbPacketNumber, sizeof(int) }, + { &gbSgbMultiplayer, sizeof(int) }, + { &gbSgbNextController, sizeof(u8) }, + { &gbSgbReadingController, sizeof(u8) }, + { &gbSgbFourPlayers, sizeof(int) }, + { NULL, 0 } +}; + +void gbSgbSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, gbSgbSaveStructV3); + + utilGzWrite(gzFile, gbSgbBorder, 2048); + utilGzWrite(gzFile, gbSgbBorderChar, 32*256); + + utilGzWrite(gzFile, gbSgbPacket, 16*7); + + utilGzWrite(gzFile, gbSgbSCPPalette, 4 * 512 * sizeof(u16)); + utilGzWrite(gzFile, gbSgbATF, 20 * 18); + utilGzWrite(gzFile, gbSgbATFList, 45 * 20 * 18); +} + +void gbSgbReadGame(gzFile gzFile, int version) +{ + if(version >= 3) + utilReadData(gzFile, gbSgbSaveStructV3); + else { + utilReadData(gzFile, gbSgbSaveStruct); + gbSgbFourPlayers = 0; + } + + if(version >= 8) { + utilGzRead(gzFile, gbSgbBorder, 2048); + utilGzRead(gzFile, gbSgbBorderChar, 32*256); + } + + utilGzRead(gzFile, gbSgbPacket, 16*7); + + utilGzRead(gzFile, gbSgbSCPPalette, 4 * 512 * sizeof(u16)); + utilGzRead(gzFile, gbSgbATF, 20 * 18); + utilGzRead(gzFile, gbSgbATFList, 45 * 20 * 18); +} diff --git a/src/gb/gbSGB.h b/src/gb/gbSGB.h new file mode 100644 index 00000000..95afb2c1 --- /dev/null +++ b/src/gb/gbSGB.h @@ -0,0 +1,39 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +void gbSgbInit(); +void gbSgbShutdown(); +void gbSgbCommand(); +void gbSgbResetPacketState(); +void gbSgbReset(); +void gbSgbDoBitTransfer(u8); +void gbSgbSaveGame(gzFile); +void gbSgbReadGame(gzFile, int version); +void gbSgbRenderBorder(); + +extern u8 gbSgbATF[20*18]; +extern int gbSgbMode; +extern int gbSgbMask; +extern int gbSgbMultiplayer; +extern u8 gbSgbNextController; +extern int gbSgbPacketTimeout; +extern u8 gbSgbReadingController; +extern int gbSgbFourPlayers; + + diff --git a/src/gb/gbSound.cpp b/src/gb/gbSound.cpp new file mode 100644 index 00000000..22f83150 --- /dev/null +++ b/src/gb/gbSound.cpp @@ -0,0 +1,525 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include + +#include "../System.h" +#include "../Util.h" +#include "gbGlobals.h" +#include "gbSound.h" + +#include "../Gb_Apu/Multi_Buffer.h" +#include "../Gb_Apu/Gb_Apu.h" + +extern u8 soundBuffer[6][735]; +extern u16 soundFinalWave[1470]; +extern int soundVolume; + +#define SOUND_MAGIC 0x60000000 +#define SOUND_MAGIC_2 0x30000000 +#define NOISE_MAGIC 5 + +extern int speed; + +extern void soundResume(); + +extern u8 soundWavePattern[4][32]; + +extern int soundBufferLen; +extern int soundBufferTotalLen; +extern int soundQuality; +extern int soundPaused; +extern int soundPlay; +extern int soundTicks; +extern int SOUND_CLOCK_TICKS; +extern u32 soundNextPosition; + +extern int soundLevel1; +extern int soundLevel2; +extern int soundBalance; +extern int soundMasterOn; +extern int soundIndex; +extern int soundBufferIndex; +int soundVIN = 0; +extern int soundDebug; + +extern Multi_Buffer * apu_out; +extern Gb_Apu * apu; + +extern int sound1On; +extern int sound1ATL; +extern int sound1Skip; +extern int sound1Index; +extern int sound1Continue; +extern int sound1EnvelopeVolume; +extern int sound1EnvelopeATL; +extern int sound1EnvelopeUpDown; +extern int sound1EnvelopeATLReload; +extern int sound1SweepATL; +extern int sound1SweepATLReload; +extern int sound1SweepSteps; +extern int sound1SweepUpDown; +extern int sound1SweepStep; +extern u8 *sound1Wave; + +extern int sound2On; +extern int sound2ATL; +extern int sound2Skip; +extern int sound2Index; +extern int sound2Continue; +extern int sound2EnvelopeVolume; +extern int sound2EnvelopeATL; +extern int sound2EnvelopeUpDown; +extern int sound2EnvelopeATLReload; +extern u8 *sound2Wave; + +extern int sound3On; +extern int sound3ATL; +extern int sound3Skip; +extern int sound3Index; +extern int sound3Continue; +extern int sound3OutputLevel; +extern int sound3Last; + +extern int sound4On; +extern int sound4Clock; +extern int sound4ATL; +extern int sound4Skip; +extern int sound4Index; +extern int sound4ShiftRight; +extern int sound4ShiftSkip; +extern int sound4ShiftIndex; +extern int sound4NSteps; +extern int sound4CountDown; +extern int sound4Continue; +extern int sound4EnvelopeVolume; +extern int sound4EnvelopeATL; +extern int sound4EnvelopeUpDown; +extern int sound4EnvelopeATLReload; + +extern int soundEnableFlag; + +extern int soundFreqRatio[8]; +extern int soundShiftClock[16]; + +extern s16 soundFilter[4000]; +extern s16 soundLeft[5]; +extern s16 soundRight[5]; +extern int soundEchoIndex; +extern bool soundEcho; +extern bool soundLowPass; +extern bool soundReverse; +extern bool soundOffFlag; + +u8 gbSoundRead(u16 address) +{ + if (address < NR10 || address > 0xFF3F || !apu) return gbMemory[address]; + if (address == NR51) return soundBalance; + + int clock = (SOUND_CLOCK_TICKS - soundTicks) * 95 / (24 * (gbSpeed ? 2 : 1)); + + int ret = apu->read_register(clock, address); + + switch ( address ) + { + case NR10: + ret |= 0x80; break; + + case NR11: + case NR21: + ret |= 0x3F; break; + + case NR13: + case NR23: + case NR31: + case NR33: + ret = 0xFF; break; + + case NR14: + case NR24: + case NR34: + case NR44: + ret |= 0xBF; break; + + case NR30: + ret |= 0x7F; break; + + case NR32: + ret |= 0x9F; break; + } + + return ret; +} + +void gbSoundEvent(register u16 address, register int data) +{ + int freq = 0; + + gbMemory[address] = data; + +#ifndef FINAL_VERSION + if(soundDebug) { + // don't translate. debug only + log("Sound event: %08lx %02x\n", address, data); + } +#endif + if (apu && address >= NR10 && address <= 0xFF3F) + { + int clock = (SOUND_CLOCK_TICKS - soundTicks) * 95 / (24 * (gbSpeed ? 2 : 1)); + if (address == NR50) + { + apu->write_register(clock, address, data); + } + else if (address == NR51) + { + soundBalance = data; + apu->write_register(clock, address, data & soundEnableFlag); + } + else + apu->write_register(clock, address, data); + } +} + + void gbSoundChannel1() +{ +} + +void gbSoundChannel2() +{ +} + +void gbSoundChannel3() +{ +} + +void gbSoundChannel4() +{ +} + +void gbSoundMix() +{ + int res = 0; + + blip_sample_t out[2] = {0, 0}; + + if ( ! apu_out ) return; + + while (!apu_out->read_samples(&out[0], 2)) + { + int ticks = SOUND_CLOCK_TICKS * 95 / (24 * (gbSpeed ? 2 : 1)); + bool was_stereo = apu->end_frame( ticks ); + apu_out->end_frame( ticks, was_stereo ); + } + + res = out[0]; + + //res = (res * 7 * 60) >> 8; + + + if(soundEcho) { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + } + + if(soundLowPass) { + soundLeft[4] = soundLeft[3]; + soundLeft[3] = soundLeft[2]; + soundLeft[2] = soundLeft[1]; + soundLeft[1] = soundLeft[0]; + soundLeft[0] = res; + res = (soundLeft[4] + 2*soundLeft[3] + 8*soundLeft[2] + 2*soundLeft[1] + + soundLeft[0])/14; + } + + switch(soundVolume) { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume+1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + + if(res > 32767) + res = 32767; + if(res < -32768) + res = -32768; + + if(soundReverse) + soundFinalWave[++soundBufferIndex] = res; + else + soundFinalWave[soundBufferIndex++] = res; + + res = out[1]; + + if(soundEcho) { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + + if(soundEchoIndex >= 4000) + soundEchoIndex = 0; + } + + if(soundLowPass) { + soundRight[4] = soundRight[3]; + soundRight[3] = soundRight[2]; + soundRight[2] = soundRight[1]; + soundRight[1] = soundRight[0]; + soundRight[0] = res; + res = (soundRight[4] + 2*soundRight[3] + 8*soundRight[2] + 2*soundRight[1] + + soundRight[0])/14; + } + + switch(soundVolume) { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume+1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + + if(res > 32767) + res = 32767; + if(res < -32768) + res = -32768; + + if(soundReverse) + soundFinalWave[-1+soundBufferIndex++] = res; + else + soundFinalWave[soundBufferIndex++] = res; +} + +void gbSoundTick() +{ + if(systemSoundOn) { + if(soundMasterOn) { + /*gbSoundChannel1(); + gbSoundChannel2(); + gbSoundChannel3(); + gbSoundChannel4();*/ + + gbSoundMix(); + } else { + soundFinalWave[soundBufferIndex++] = 0; + soundFinalWave[soundBufferIndex++] = 0; + } + + soundIndex++; + + if(2*soundBufferIndex >= soundBufferLen) { + if(systemSoundOn) { + if(soundPaused) { + soundResume(); + } + + systemWriteDataToSoundBuffer(); + } + soundIndex = 0; + soundBufferIndex = 0; + } + } +} + +void gbSoundReset() +{ + soundPaused = 1; + soundPlay = 0; + SOUND_CLOCK_TICKS = soundQuality * 24; + soundTicks = SOUND_CLOCK_TICKS; + soundNextPosition = 0; + soundMasterOn = 1; + soundIndex = 0; + soundBufferIndex = 0; + soundLevel1 = 7; + soundLevel2 = 7; + soundVIN = 0; + + + // don't translate + if(soundDebug) { + log("*** Sound Init ***\n"); + } + + + if (apu_out) + { + apu_out->clear(); + apu->reset(false); + + extern const BOOST::uint8_t sound_data[Gb_Apu::end_addr - Gb_Apu::start_addr + 1]; + + int addr = 0; + + while (addr < 0x30) { + apu->write_register( 0, 0xFF10 + addr, sound_data [ addr ] ); + addr++; + } + } + // don't translate + if(soundDebug) { + log("*** Sound Init Complete ***\n"); + } + + if (apu) + { + int addr = 0xff30; + + while(addr < 0xff40) { + /*gbMemory[addr++] = 0x00; + gbMemory[addr++] = 0xff;*/ + gbSoundEvent(addr++, 0x00); + gbSoundEvent(addr++, 0xFF); + } + } + + memset(soundFinalWave, 0x00, soundBufferLen); + + + memset(soundFilter, 0, sizeof(soundFilter)); + soundEchoIndex = 0; +} + +extern bool soundInit(bool gba = true); +extern void soundShutdown(); + +void gbSoundSetQuality(int quality) +{ + if(soundQuality != quality && systemCanChangeSoundQuality()) { + if(!soundOffFlag) + soundShutdown(); + soundQuality = quality; + soundNextPosition = 0; + if(!soundOffFlag) + soundInit(false); + SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * 24 * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } else { + soundNextPosition = 0; + SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * 24 * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } +} + +variable_desc gbSoundSaveStruct[] = { + { &soundPaused, sizeof(int) }, + { &soundPlay, sizeof(int) }, + { &soundTicks, sizeof(int) }, + { &SOUND_CLOCK_TICKS, sizeof(int) }, + { &soundLevel1, sizeof(int) }, + { &soundLevel2, sizeof(int) }, + { &soundBalance, sizeof(int) }, + { &soundMasterOn, sizeof(int) }, + { &soundIndex, sizeof(int) }, + { &soundVIN, sizeof(int) }, + { &sound1On, sizeof(int) }, + { &sound1ATL, sizeof(int) }, + { &sound1Skip, sizeof(int) }, + { &sound1Index, sizeof(int) }, + { &sound1Continue, sizeof(int) }, + { &sound1EnvelopeVolume, sizeof(int) }, + { &sound1EnvelopeATL, sizeof(int) }, + { &sound1EnvelopeATLReload, sizeof(int) }, + { &sound1EnvelopeUpDown, sizeof(int) }, + { &sound1SweepATL, sizeof(int) }, + { &sound1SweepATLReload, sizeof(int) }, + { &sound1SweepSteps, sizeof(int) }, + { &sound1SweepUpDown, sizeof(int) }, + { &sound1SweepStep, sizeof(int) }, + { &sound2On, sizeof(int) }, + { &sound2ATL, sizeof(int) }, + { &sound2Skip, sizeof(int) }, + { &sound2Index, sizeof(int) }, + { &sound2Continue, sizeof(int) }, + { &sound2EnvelopeVolume, sizeof(int) }, + { &sound2EnvelopeATL, sizeof(int) }, + { &sound2EnvelopeATLReload, sizeof(int) }, + { &sound2EnvelopeUpDown, sizeof(int) }, + { &sound3On, sizeof(int) }, + { &sound3ATL, sizeof(int) }, + { &sound3Skip, sizeof(int) }, + { &sound3Index, sizeof(int) }, + { &sound3Continue, sizeof(int) }, + { &sound3OutputLevel, sizeof(int) }, + { &sound4On, sizeof(int) }, + { &sound4ATL, sizeof(int) }, + { &sound4Skip, sizeof(int) }, + { &sound4Index, sizeof(int) }, + { &sound4Clock, sizeof(int) }, + { &sound4ShiftRight, sizeof(int) }, + { &sound4ShiftSkip, sizeof(int) }, + { &sound4ShiftIndex, sizeof(int) }, + { &sound4NSteps, sizeof(int) }, + { &sound4CountDown, sizeof(int) }, + { &sound4Continue, sizeof(int) }, + { &sound4EnvelopeVolume, sizeof(int) }, + { &sound4EnvelopeATL, sizeof(int) }, + { &sound4EnvelopeATLReload, sizeof(int) }, + { &sound4EnvelopeUpDown, sizeof(int) }, + { &soundEnableFlag, sizeof(int) }, + { NULL, 0 } +}; + +void gbSoundSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, gbSoundSaveStruct); + + utilGzWrite(gzFile, soundBuffer, 4*735); + utilGzWrite(gzFile, soundFinalWave, 2*735); + utilGzWrite(gzFile, &soundQuality, sizeof(int)); +} + +void gbSoundReadGame(int version,gzFile gzFile) +{ + utilReadData(gzFile, gbSoundSaveStruct); + + soundBufferIndex = soundIndex * 2; + + utilGzRead(gzFile, soundBuffer, 4*735); + utilGzRead(gzFile, soundFinalWave, 2*735); + + if(version >=7) { + int quality = 1; + utilGzRead(gzFile, &quality, sizeof(int)); + gbSoundSetQuality(quality); + } else { + soundQuality = -1; + gbSoundSetQuality(1); + } + + sound1Wave = soundWavePattern[gbMemory[NR11] >> 6]; + sound2Wave = soundWavePattern[gbMemory[NR21] >> 6]; +} diff --git a/src/gb/gbSound.h b/src/gb/gbSound.h new file mode 100644 index 00000000..8509a107 --- /dev/null +++ b/src/gb/gbSound.h @@ -0,0 +1,61 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#define NR10 0xff10 +#define NR11 0xff11 +#define NR12 0xff12 +#define NR13 0xff13 +#define NR14 0xff14 +#define NR21 0xff16 +#define NR22 0xff17 +#define NR23 0xff18 +#define NR24 0xff19 +#define NR30 0xff1a +#define NR31 0xff1b +#define NR32 0xff1c +#define NR33 0xff1d +#define NR34 0xff1e +#define NR41 0xff20 +#define NR42 0xff21 +#define NR43 0xff22 +#define NR44 0xff23 +#define NR50 0xff24 +#define NR51 0xff25 +#define NR52 0xff26 + +#define SOUND_EVENT(address,value) \ + gbSoundEvent(address,value) + +extern void gbSoundTick(); +extern void gbSoundPause(); +extern void gbSoundResume(); +extern void gbSoundEnable(int); +extern void gbSoundDisable(int); +extern int gbSoundGetEnable(); +extern void gbSoundReset(); +extern void gbSoundSaveGame(gzFile); +extern void gbSoundReadGame(int,gzFile); +extern void gbSoundEvent(register u16, register int); +extern void gbSoundSetQuality(int); + +extern u8 gbSoundRead(u16 address); + +extern int soundTicks; +extern int soundQuality; +extern int SOUND_CLOCK_TICKS; diff --git a/src/gbafilter.cpp b/src/gbafilter.cpp new file mode 100644 index 00000000..f6df4ef8 --- /dev/null +++ b/src/gbafilter.cpp @@ -0,0 +1,227 @@ +#include "gbafilter.h" + +#include + +extern int systemColorDepth; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; + +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; + +static const unsigned char curve[32] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x10, 0x12, + 0x14, 0x16, 0x18, 0x1c, 0x20, 0x28, 0x30, 0x38, + 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x80, + 0x88, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}; + +// output R G B +static const unsigned char influence[3 * 3] = { 16, 4, 4, // red + 8, 16, 8, // green + 0, 8, 16};// blue + +inline void swap(short & a, short & b) +{ + short temp = a; + a = b; + b = temp; +} + +void gbafilter_pal(u16 * buf, int count) +{ + short temp[3 * 3], s; + unsigned pix; + u8 red, green, blue; + + while (count--) + { + pix = *buf; + + s = curve[(pix >> systemGreenShift) & 0x1f]; + temp[3] = s * influence[3]; + temp[4] = s * influence[4]; + temp[5] = s * influence[5]; + + s = curve[(pix >> systemRedShift) & 0x1f]; + temp[0] = s * influence[0]; + temp[1] = s * influence[1]; + temp[2] = s * influence[2]; + + s = curve[(pix >> systemBlueShift) & 0x1f]; + temp[6] = s * influence[6]; + temp[7] = s * influence[7]; + temp[8] = s * influence[8]; + + if (temp[0] < temp[3]) swap(temp[0], temp[3]); + if (temp[0] < temp[6]) swap(temp[0], temp[6]); + if (temp[3] < temp[6]) swap(temp[3], temp[6]); + temp[3] <<= 1; + temp[0] <<= 2; + temp[0] += temp[3] + temp[6]; + + red = ((int(temp[0]) * 160) >> 17) + 4; + if (red > 31) red = 31; + + if (temp[2] < temp[5]) swap(temp[2], temp[5]); + if (temp[2] < temp[8]) swap(temp[2], temp[8]); + if (temp[5] < temp[8]) swap(temp[5], temp[8]); + temp[5] <<= 1; + temp[2] <<= 2; + temp[2] += temp[5] + temp[8]; + + blue = ((int(temp[2]) * 160) >> 17) + 4; + if (blue > 31) blue = 31; + + if (temp[1] < temp[4]) swap(temp[1], temp[4]); + if (temp[1] < temp[7]) swap(temp[1], temp[7]); + if (temp[4] < temp[7]) swap(temp[4], temp[7]); + temp[4] <<= 1; + temp[1] <<= 2; + temp[1] += temp[4] + temp[7]; + + green = ((int(temp[1]) * 160) >> 17) + 4; + if (green > 31) green = 31; + + pix = red << systemRedShift; + pix += green << systemGreenShift; + pix += blue << systemBlueShift; + + *buf++ = pix; + } +} + +void gbafilter_pal32(u32 * buf, int count) +{ + short temp[3 * 3], s; + unsigned pix; + u8 red, green, blue; + + while (count--) + { + pix = *buf; + + s = curve[(pix >> systemGreenShift) & 0x1f]; + temp[3] = s * influence[3]; + temp[4] = s * influence[4]; + temp[5] = s * influence[5]; + + s = curve[(pix >> systemRedShift) & 0x1f]; + temp[0] = s * influence[0]; + temp[1] = s * influence[1]; + temp[2] = s * influence[2]; + + s = curve[(pix >> systemBlueShift) & 0x1f]; + temp[6] = s * influence[6]; + temp[7] = s * influence[7]; + temp[8] = s * influence[8]; + + if (temp[0] < temp[3]) swap(temp[0], temp[3]); + if (temp[0] < temp[6]) swap(temp[0], temp[6]); + if (temp[3] < temp[6]) swap(temp[3], temp[6]); + temp[3] <<= 1; + temp[0] <<= 2; + temp[0] += temp[3] + temp[6]; + + //red = ((int(temp[0]) * 160) >> 17) + 4; + red = ((int(temp[0]) * 160) >> 14) + 32; + + if (temp[2] < temp[5]) swap(temp[2], temp[5]); + if (temp[2] < temp[8]) swap(temp[2], temp[8]); + if (temp[5] < temp[8]) swap(temp[5], temp[8]); + temp[5] <<= 1; + temp[2] <<= 2; + temp[2] += temp[5] + temp[8]; + + //blue = ((int(temp[2]) * 160) >> 17) + 4; + blue = ((int(temp[2]) * 160) >> 14) + 32; + + if (temp[1] < temp[4]) swap(temp[1], temp[4]); + if (temp[1] < temp[7]) swap(temp[1], temp[7]); + if (temp[4] < temp[7]) swap(temp[4], temp[7]); + temp[4] <<= 1; + temp[1] <<= 2; + temp[1] += temp[4] + temp[7]; + + //green = ((int(temp[1]) * 160) >> 17) + 4; + green = ((int(temp[1]) * 160) >> 14) + 32; + + //pix = red << redshift; + //pix += green << greenshift; + //pix += blue << blueshift; + + pix = red << (systemRedShift - 3); + pix += green << (systemGreenShift - 3); + pix += blue << (systemBlueShift - 3); + + *buf++ = pix; + } +} + +// for palette mode to work with the three spoony filters in 32bpp depth + +void gbafilter_pad(u8 * buf, int count) +{ + union + { + struct + { + u8 r; + u8 g; + u8 b; + u8 a; + } part; + unsigned whole; + } + mask; + + mask.whole = 0x1f << systemRedShift; + mask.whole += 0x1f << systemGreenShift; + mask.whole += 0x1f << systemBlueShift; + + switch (systemColorDepth) + { + case 24: + while (count--) + { + *buf++ &= mask.part.r; + *buf++ &= mask.part.g; + *buf++ &= mask.part.b; + } + break; + case 32: + while (count--) + { + *((u32*)buf) &= mask.whole; + buf += 4; + } + } +} + +/* +void UpdateSystemColorMaps(int lcd) +{ + switch(systemColorDepth) { + case 16: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal(systemColorMap16, 0x10000); + } + break; + case 24: + case 32: + { + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if (lcd == 1) gbafilter_pal32(systemColorMap32, 0x10000); + } + break; + } +} +*/ \ No newline at end of file diff --git a/src/gbafilter.h b/src/gbafilter.h new file mode 100644 index 00000000..3c171e07 --- /dev/null +++ b/src/gbafilter.h @@ -0,0 +1,5 @@ +#include "System.h" + +void gbafilter_pal(u16 * buf, int count); +void gbafilter_pal32(u32 * buf, int count); +void gbafilter_pad(u8 * buf, int count); diff --git a/src/getopt.cpp b/src/getopt.cpp new file mode 100644 index 00000000..9a127443 --- /dev/null +++ b/src/getopt.cpp @@ -0,0 +1,1048 @@ +#include + +/* Getopt for GNU. + NOTE: getopt is now part of the C library, so if you don't know what + "Keep this file name-space clean" means, talk to drepper@gnu.org + before changing it! + + Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98 + Free Software Foundation, Inc. + + NOTE: This source is derived from an old version taken from the GNU C + Library (glibc). + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +/* This tells Alpha OSF/1 not to define a getopt prototype in . + Ditto for AIX 3.2 and . */ +#ifndef _NO_PROTO +# define _NO_PROTO +#endif + +#ifdef HAVE_CONFIG_H +# include +#endif + +#if !defined __STDC__ || !__STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +# ifndef const +# define const +# endif +#endif + +#include + +/* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#define GETOPT_INTERFACE_VERSION 2 +#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 +# include +# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION +# define ELIDE_CODE +# endif +#endif + +#ifndef ELIDE_CODE + + +/* This needs to come after some library #include + to get __GNU_LIBRARY__ defined. */ +#ifdef __GNU_LIBRARY__ +/* Don't include stdlib.h for non-GNU C libraries because some of them + contain conflicting prototypes for getopt. */ +# include +# include +#endif /* GNU C library. */ + +#ifdef VMS +# include +# if HAVE_STRING_H - 0 +# include +# endif +#endif + +#ifndef _ +/* This is for other GNU distributions with internationalized messages. + When compiling libc, the _ macro is predefined. */ +# ifdef HAVE_LIBINTL_H +# include +# define _(msgid) gettext (msgid) +# else +# define _(msgid) (msgid) +# endif +#endif + +#ifdef _MSC_VER +#include +#include +#endif + +/* This version of `getopt' appears to the caller like standard Unix `getopt' + but it behaves differently for the user, since it allows the user + to intersperse the options with the other arguments. + + As `getopt' works, it permutes the elements of ARGV so that, + when it is done, all the options precede everything else. Thus + all application programs are extended to handle flexible argument order. + + Setting the environment variable POSIXLY_CORRECT disables permutation. + Then the behavior is completely standard. + + GNU application programs can use a third alternative mode in which + they can distinguish the relative order of options and other arguments. */ + +#include "getopt.h" + +/* For communication from `getopt' to the caller. + When `getopt' finds an option that takes an argument, + the argument value is returned here. + Also, when `ordering' is RETURN_IN_ORDER, + each non-option ARGV-element is returned here. */ + +char *optarg = NULL; + +/* Index in ARGV of the next element to be scanned. + This is used for communication to and from the caller + and for communication between successive calls to `getopt'. + + On entry to `getopt', zero means this is the first call; initialize. + + When `getopt' returns -1, this is the index of the first of the + non-option elements that the caller should itself scan. + + Otherwise, `optind' communicates from one call to the next + how much of ARGV has been scanned so far. */ + +/* 1003.2 says this must be 1 before any call. */ +int optind = 1; + +/* Formerly, initialization of getopt depended on optind==0, which + causes problems with re-calling getopt as programs generally don't + know that. */ + +int __getopt_initialized = 0; + +/* The next char to be scanned in the option-element + in which the last option character we returned was found. + This allows us to pick up the scan where we left off. + + If this is zero, or a null string, it means resume the scan + by advancing to the next ARGV-element. */ + +static char *nextchar; + +/* Callers store zero here to inhibit the error message + for unrecognized options. */ + +int opterr = 1; + +/* Set to an option character which was unrecognized. + This must be initialized on some systems to avoid linking in the + system's own getopt implementation. */ + +int optopt = '?'; + +/* Describe how to deal with options that follow non-option ARGV-elements. + + If the caller did not specify anything, + the default is REQUIRE_ORDER if the environment variable + POSIXLY_CORRECT is defined, PERMUTE otherwise. + + REQUIRE_ORDER means don't recognize them as options; + stop option processing when the first non-option is seen. + This is what Unix does. + This mode of operation is selected by either setting the environment + variable POSIXLY_CORRECT, or using `+' as the first character + of the list of option characters. + + PERMUTE is the default. We permute the contents of ARGV as we scan, + so that eventually all the non-options are at the end. This allows options + to be given in any order, even with programs that were not written to + expect this. + + RETURN_IN_ORDER is an option available to programs that were written + to expect options and other ARGV-elements in any order and that care about + the ordering of the two. We describe each non-option ARGV-element + as if it were the argument of an option with character code 1. + Using `-' as the first character of the list of option characters + selects this mode of operation. + + The special argument `--' forces an end of option-scanning regardless + of the value of `ordering'. In the case of RETURN_IN_ORDER, only + `--' can cause `getopt' to return -1 with `optind' != ARGC. */ + +static enum +{ + REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER +} ordering; + +/* Value of POSIXLY_CORRECT environment variable. */ +static char *posixly_correct; + +#ifdef __GNU_LIBRARY__ +/* We want to avoid inclusion of string.h with non-GNU libraries + because there are many ways it can cause trouble. + On some systems, it contains special magic macros that don't work + in GCC. */ +# include +# define my_index strchr +#else + +# if HAVE_STRING_H +# include +# else +# if HAVE_STRINGS_H +# include +# endif +# endif + +/* Avoid depending on library functions or files + whose names are inconsistent. */ + +#ifndef getenv +extern char *getenv (); +#endif + +static char * +my_index (const char *str, int chr) +{ + while (*str) + { + if (*str == chr) + return (char *) str; + str++; + } + return 0; +} + +/* If using GCC, we can safely declare strlen this way. + If not using GCC, it is ok not to declare it. */ +#ifdef __GNUC__ +/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. + That was relevant to code that was here before. */ +# if (!defined __STDC__ || !__STDC__) && !defined strlen +/* gcc with -traditional declares the built-in strlen to return int, + and has done so at least since version 2.4.5. -- rms. */ +extern int strlen (const char *); +# endif /* not __STDC__ */ +#endif /* __GNUC__ */ + +#endif /* not __GNU_LIBRARY__ */ + +/* Handle permutation of arguments. */ + +/* Describe the part of ARGV that contains non-options that have + been skipped. `first_nonopt' is the index in ARGV of the first of them; + `last_nonopt' is the index after the last of them. */ + +static int first_nonopt; +static int last_nonopt; + +#ifdef _LIBC +/* Bash 2.0 gives us an environment variable containing flags + indicating ARGV elements that should not be considered arguments. */ + +/* Defined in getopt_init.c */ +extern char *__getopt_nonoption_flags; + +static int nonoption_flags_max_len; +static int nonoption_flags_len; + +static int original_argc; +static char *const *original_argv; + +/* Make sure the environment variable bash 2.0 puts in the environment + is valid for the getopt call we must make sure that the ARGV passed + to getopt is that one passed to the process. */ +static void +__attribute__ ((unused)) +store_args_and_env (int argc, char *const *argv) +{ + /* XXX This is no good solution. We should rather copy the args so + that we can compare them later. But we must not use malloc(3). */ + original_argc = argc; + original_argv = argv; +} +# ifdef text_set_element +text_set_element (__libc_subinit, store_args_and_env); +# endif /* text_set_element */ + +# define SWAP_FLAGS(ch1, ch2) \ + if (nonoption_flags_len > 0) \ + { \ + char __tmp = __getopt_nonoption_flags[ch1]; \ + __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ + __getopt_nonoption_flags[ch2] = __tmp; \ + } +#else /* !_LIBC */ +# define SWAP_FLAGS(ch1, ch2) +#endif /* _LIBC */ + +/* Exchange two adjacent subsequences of ARGV. + One subsequence is elements [first_nonopt,last_nonopt) + which contains all the non-options that have been skipped so far. + The other is elements [last_nonopt,optind), which contains all + the options processed since those non-options were skipped. + + `first_nonopt' and `last_nonopt' are relocated so that they describe + the new indices of the non-options in ARGV after they are moved. */ + +#if defined __STDC__ && __STDC__ +static void exchange (char **); +#endif + +static void +exchange (char **argv) +{ + int bottom = first_nonopt; + int middle = last_nonopt; + int top = optind; + char *tem; + + /* Exchange the shorter segment with the far end of the longer segment. + That puts the shorter segment into the right place. + It leaves the longer segment in the right place overall, + but it consists of two parts that need to be swapped next. */ + +#ifdef _LIBC + /* First make sure the handling of the `__getopt_nonoption_flags' + string can work normally. Our top argument must be in the range + of the string. */ + if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) + { + /* We must extend the array. The user plays games with us and + presents new arguments. */ + char *new_str = malloc (top + 1); + if (new_str == NULL) + nonoption_flags_len = nonoption_flags_max_len = 0; + else + { + memset (__mempcpy (new_str, __getopt_nonoption_flags, + nonoption_flags_max_len), + '\0', top + 1 - nonoption_flags_max_len); + nonoption_flags_max_len = top + 1; + __getopt_nonoption_flags = new_str; + } + } +#endif + + while (top > middle && middle > bottom) + { + if (top - middle > middle - bottom) + { + /* Bottom segment is the short one. */ + int len = middle - bottom; + register int i; + + /* Swap it with the top part of the top segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[top - (middle - bottom) + i]; + argv[top - (middle - bottom) + i] = tem; + SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); + } + /* Exclude the moved bottom segment from further swapping. */ + top -= len; + } + else + { + /* Top segment is the short one. */ + int len = top - middle; + register int i; + + /* Swap it with the bottom part of the bottom segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[middle + i]; + argv[middle + i] = tem; + SWAP_FLAGS (bottom + i, middle + i); + } + /* Exclude the moved top segment from further swapping. */ + bottom += len; + } + } + + /* Update records for the slots the non-options now occupy. */ + + first_nonopt += (optind - last_nonopt); + last_nonopt = optind; +} + +/* Initialize the internal data when the first call is made. */ + +#if defined __STDC__ && __STDC__ +static const char *_getopt_initialize (int, char *const *, const char *); +#endif +static const char * +_getopt_initialize (int argc, char *const *argv, const char *optstring) +{ + /* Start processing options with ARGV-element 1 (since ARGV-element 0 + is the program name); the sequence of previously skipped + non-option ARGV-elements is empty. */ + + first_nonopt = last_nonopt = optind; + + nextchar = NULL; + + posixly_correct = getenv ("POSIXLY_CORRECT"); + + /* Determine how to handle the ordering of options and nonoptions. */ + + if (optstring[0] == '-') + { + ordering = RETURN_IN_ORDER; + ++optstring; + } + else if (optstring[0] == '+') + { + ordering = REQUIRE_ORDER; + ++optstring; + } + else if (posixly_correct != NULL) + ordering = REQUIRE_ORDER; + else + ordering = PERMUTE; + +#ifdef _LIBC + if (posixly_correct == NULL + && argc == original_argc && argv == original_argv) + { + if (nonoption_flags_max_len == 0) + { + if (__getopt_nonoption_flags == NULL + || __getopt_nonoption_flags[0] == '\0') + nonoption_flags_max_len = -1; + else + { + const char *orig_str = __getopt_nonoption_flags; + int len = nonoption_flags_max_len = strlen (orig_str); + if (nonoption_flags_max_len < argc) + nonoption_flags_max_len = argc; + __getopt_nonoption_flags = + (char *) malloc (nonoption_flags_max_len); + if (__getopt_nonoption_flags == NULL) + nonoption_flags_max_len = -1; + else + memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), + '\0', nonoption_flags_max_len - len); + } + } + nonoption_flags_len = nonoption_flags_max_len; + } + else + nonoption_flags_len = 0; +#endif + + return optstring; +} + +/* Scan elements of ARGV (whose length is ARGC) for option characters + given in OPTSTRING. + + If an element of ARGV starts with '-', and is not exactly "-" or "--", + then it is an option element. The characters of this element + (aside from the initial '-') are option characters. If `getopt' + is called repeatedly, it returns successively each of the option characters + from each of the option elements. + + If `getopt' finds another option character, it returns that character, + updating `optind' and `nextchar' so that the next call to `getopt' can + resume the scan with the following option character or ARGV-element. + + If there are no more option characters, `getopt' returns -1. + Then `optind' is the index in ARGV of the first ARGV-element + that is not an option. (The ARGV-elements have been permuted + so that those that are not options now come last.) + + OPTSTRING is a string containing the legitimate option characters. + If an option character is seen that is not listed in OPTSTRING, + return '?' after printing an error message. If you set `opterr' to + zero, the error message is suppressed but we still return '?'. + + If a char in OPTSTRING is followed by a colon, that means it wants an arg, + so the following text in the same ARGV-element, or the text of the following + ARGV-element, is returned in `optarg'. Two colons mean an option that + wants an optional arg; if there is text in the current ARGV-element, + it is returned in `optarg', otherwise `optarg' is set to zero. + + If OPTSTRING starts with `-' or `+', it requests different methods of + handling the non-option ARGV-elements. + See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. + + Long-named options begin with `--' instead of `-'. + Their names may be abbreviated as long as the abbreviation is unique + or is an exact match for some defined option. If they have an + argument, it follows the option name in the same ARGV-element, separated + from the option name by a `=', or else the in next ARGV-element. + When `getopt' finds a long-named option, it returns 0 if that option's + `flag' field is nonzero, the value of the option's `val' field + if the `flag' field is zero. + + The elements of ARGV aren't really const, because we permute them. + But we pretend they're const in the prototype to be compatible + with other systems. + + LONGOPTS is a vector of `struct option' terminated by an + element containing a name which is zero. + + LONGIND returns the index in LONGOPT of the long-named option found. + It is only valid when a long-named option has been found by the most + recent call. + + If LONG_ONLY is nonzero, '-' as well as '--' can introduce + long-named options. */ + +int +_getopt_internal (int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only) +{ + optarg = NULL; + + if (optind == 0 || !__getopt_initialized) + { + if (optind == 0) + optind = 1; /* Don't scan ARGV[0], the program name. */ + optstring = _getopt_initialize (argc, argv, optstring); + __getopt_initialized = 1; + } + + /* Test whether ARGV[optind] points to a non-option argument. + Either it does not have option syntax, or there is an environment flag + from the shell indicating it is not an option. The later information + is only used when the used in the GNU libc. */ +#ifdef _LIBC +# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ + || (optind < nonoption_flags_len \ + && __getopt_nonoption_flags[optind] == '1')) +#else +# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') +#endif + + if (nextchar == NULL || *nextchar == '\0') + { + /* Advance to the next ARGV-element. */ + + /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been + moved back by the user (who may also have changed the arguments). */ + if (last_nonopt > optind) + last_nonopt = optind; + if (first_nonopt > optind) + first_nonopt = optind; + + if (ordering == PERMUTE) + { + /* If we have just processed some options following some non-options, + exchange them so that the options come first. */ + + if (first_nonopt != last_nonopt && last_nonopt != optind) + exchange ((char **) argv); + else if (last_nonopt != optind) + first_nonopt = optind; + + /* Skip any additional non-options + and extend the range of non-options previously skipped. */ + + while (optind < argc && NONOPTION_P) + optind++; + last_nonopt = optind; + } + + /* The special ARGV-element `--' means premature end of options. + Skip it like a null option, + then exchange with previous non-options as if it were an option, + then skip everything else like a non-option. */ + + if (optind != argc && !strcmp (argv[optind], "--")) + { + optind++; + + if (first_nonopt != last_nonopt && last_nonopt != optind) + exchange ((char **) argv); + else if (first_nonopt == last_nonopt) + first_nonopt = optind; + last_nonopt = argc; + + optind = argc; + } + + /* If we have done all the ARGV-elements, stop the scan + and back over any non-options that we skipped and permuted. */ + + if (optind == argc) + { + /* Set the next-arg-index to point at the non-options + that we previously skipped, so the caller will digest them. */ + if (first_nonopt != last_nonopt) + optind = first_nonopt; + return -1; + } + + /* If we have come to a non-option and did not permute it, + either stop the scan or describe it to the caller and pass it by. */ + + if (NONOPTION_P) + { + if (ordering == REQUIRE_ORDER) + return -1; + optarg = argv[optind++]; + return 1; + } + + /* We have found another option-ARGV-element. + Skip the initial punctuation. */ + + nextchar = (argv[optind] + 1 + + (longopts != NULL && argv[optind][1] == '-')); + } + + /* Decode the current option-ARGV-element. */ + + /* Check whether the ARGV-element is a long option. + + If long_only and the ARGV-element has the form "-f", where f is + a valid short option, don't consider it an abbreviated form of + a long option that starts with f. Otherwise there would be no + way to give the -f short option. + + On the other hand, if there's a long option "fubar" and + the ARGV-element is "-fu", do consider that an abbreviation of + the long option, just like "--fu", and not "-f" with arg "u". + + This distinction seems to be the most useful approach. */ + + if (longopts != NULL + && (argv[optind][1] == '-' + || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) + { + char *nameend; + const struct option *p; + const struct option *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = -1; + int option_index; + + for (nameend = nextchar; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; + + /* Test all long options for either exact match + or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) + == (unsigned int) strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + + if (ambig && !exact) + { + if (opterr) + fprintf (stderr, _("%s: option `%s' is ambiguous\n"), + argv[0], argv[optind]); + nextchar += strlen (nextchar); + optind++; + optopt = 0; + return '?'; + } + + if (pfound != NULL) + { + option_index = indfound; + optind++; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + allow it to be used on enums. */ + if (pfound->has_arg) + optarg = nameend + 1; + else + { + if (opterr) + { + if (argv[optind - 1][1] == '-') + /* --option */ + fprintf (stderr, + _("%s: option `--%s' doesn't allow an argument\n"), + argv[0], pfound->name); + else + /* +option or -option */ + fprintf (stderr, + _("%s: option `%c%s' doesn't allow an argument\n"), + argv[0], argv[optind - 1][0], pfound->name); + + nextchar += strlen (nextchar); + + optopt = pfound->val; + return '?'; + } + } + } + else if (pfound->has_arg == 1) + { + if (optind < argc) + optarg = argv[optind++]; + else + { + if (opterr) + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[optind - 1]); + nextchar += strlen (nextchar); + optopt = pfound->val; + return optstring[0] == ':' ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + + /* Can't find it as a long option. If this is not getopt_long_only, + or the option starts with '--' or is not a valid short + option, then it's an error. + Otherwise interpret it as a short option. */ + if (!long_only || argv[optind][1] == '-' + || my_index (optstring, *nextchar) == NULL) + { + if (opterr) + { + if (argv[optind][1] == '-') + /* --option */ + fprintf (stderr, _("%s: unrecognized option `--%s'\n"), + argv[0], nextchar); + else + /* +option or -option */ + fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), + argv[0], argv[optind][0], nextchar); + } + nextchar = (char *) ""; + optind++; + optopt = 0; + return '?'; + } + } + + /* Look at and handle the next short option-character. */ + + { + char c = *nextchar++; + char *temp = my_index (optstring, c); + + /* Increment `optind' when we start to process its last character. */ + if (*nextchar == '\0') + ++optind; + + if (temp == NULL || c == ':') + { + if (opterr) + { + if (posixly_correct) + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: illegal option -- %c\n"), + argv[0], c); + else + fprintf (stderr, _("%s: invalid option -- %c\n"), + argv[0], c); + } + optopt = c; + return '?'; + } + /* Convenience. Treat POSIX -W foo same as long option --foo */ + if (temp[0] == 'W' && temp[1] == ';') + { + char *nameend; + const struct option *p; + const struct option *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = 0; + int option_index; + + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + optarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + we must advance to the next element now. */ + optind++; + } + else if (optind == argc) + { + if (opterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + optopt = c; + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + return c; + } + else + /* We already incremented `optind' once; + increment it again when taking next ARGV-elt as argument. */ + optarg = argv[optind++]; + + /* optarg is now the argument, see if it's in the + table of longopts. */ + + for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; + + /* Test all long options for either exact match + or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) == strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + if (ambig && !exact) + { + if (opterr) + fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), + argv[0], argv[optind]); + nextchar += strlen (nextchar); + optind++; + return '?'; + } + if (pfound != NULL) + { + option_index = indfound; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + allow it to be used on enums. */ + if (pfound->has_arg) + optarg = nameend + 1; + else + { + if (opterr) + fprintf (stderr, _("\ +%s: option `-W %s' doesn't allow an argument\n"), + argv[0], pfound->name); + + nextchar += strlen (nextchar); + return '?'; + } + } + else if (pfound->has_arg == 1) + { + if (optind < argc) + optarg = argv[optind++]; + else + { + if (opterr) + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[optind - 1]); + nextchar += strlen (nextchar); + return optstring[0] == ':' ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + nextchar = NULL; + return 'W'; /* Let the application handle it. */ + } + if (temp[1] == ':') + { + if (temp[2] == ':') + { + /* This is an option that accepts an argument optionally. */ + if (*nextchar != '\0') + { + optarg = nextchar; + optind++; + } + else + optarg = NULL; + nextchar = NULL; + } + else + { + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + optarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + we must advance to the next element now. */ + optind++; + } + else if (optind == argc) + { + if (opterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, + _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + optopt = c; + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + } + else + /* We already incremented `optind' once; + increment it again when taking next ARGV-elt as argument. */ + optarg = argv[optind++]; + nextchar = NULL; + } + } + return c; + } +} + +int +getopt (int argc, char *const *argv, const char *optstring) +{ + return _getopt_internal (argc, argv, optstring, + (const struct option *) 0, + (int *) 0, + 0); +} + +#endif /* Not ELIDE_CODE. */ + +#ifdef TEST + +/* Compile with -DTEST to make an executable for use in testing + the above definition of `getopt'. */ + +int +main (argc, argv) + int argc; + char **argv; +{ + int c; + int digit_optind = 0; + + while (1) + { + int this_option_optind = optind ? optind : 1; + + c = getopt (argc, argv, "abc:d:0123456789"); + if (c == -1) + break; + + switch (c) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf ("digits occur in two different argv-elements.\n"); + digit_optind = this_option_optind; + printf ("option %c\n", c); + break; + + case 'a': + printf ("option a\n"); + break; + + case 'b': + printf ("option b\n"); + break; + + case 'c': + printf ("option c with value `%s'\n", optarg); + break; + + case '?': + break; + + default: + printf ("?? getopt returned character code 0%o ??\n", c); + } + } + + if (optind < argc) + { + printf ("non-option ARGV-elements: "); + while (optind < argc) + printf ("%s ", argv[optind++]); + printf ("\n"); + } + + exit (0); +} + +#endif /* TEST */ diff --git a/src/getopt.h b/src/getopt.h new file mode 100644 index 00000000..9268121e --- /dev/null +++ b/src/getopt.h @@ -0,0 +1,144 @@ +/* Declarations for getopt. + Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000 + Free Software Foundation, Inc. + + NOTE: The canonical source of this file is maintained with the GNU C Library. + Bugs can be reported to bug-glibc@gnu.org. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifndef _GETOPT_H +#define _GETOPT_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +/* For communication from `getopt' to the caller. + When `getopt' finds an option that takes an argument, + the argument value is returned here. + Also, when `ordering' is RETURN_IN_ORDER, + each non-option ARGV-element is returned here. */ + +extern char *optarg; + +/* Index in ARGV of the next element to be scanned. + This is used for communication to and from the caller + and for communication between successive calls to `getopt'. + + On entry to `getopt', zero means this is the first call; initialize. + + When `getopt' returns -1, this is the index of the first of the + non-option elements that the caller should itself scan. + + Otherwise, `optind' communicates from one call to the next + how much of ARGV has been scanned so far. */ + +extern int optind; + +/* Callers store zero here to inhibit the error message `getopt' prints + for unrecognized options. */ + +extern int opterr; + +/* Set to an option character which was unrecognized. */ + +extern int optopt; + +/* Describe the long-named options requested by the application. + The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector + of `struct option' terminated by an element containing a name which is + zero. + + The field `has_arg' is: + no_argument (or 0) if the option does not take an argument, + required_argument (or 1) if the option requires an argument, + optional_argument (or 2) if the option takes an optional argument. + + If the field `flag' is not NULL, it points to a variable that is set + to the value given in the field `val' when the option is found, but + left unchanged if the option is not found. + + To have a long-named option do something other than set an `int' to + a compiled-in constant, such as set a value from `optarg', set the + option's `flag' field to zero and its `val' field to a nonzero + value (the equivalent single-letter option character, if there is + one). For long options that have a zero `flag' field, `getopt' + returns the contents of the `val' field. */ + +struct option +{ +#if defined (__STDC__) && __STDC__ + const char *name; +#else + char *name; +#endif + /* has_arg can't be an enum because some compilers complain about + type mismatches in all the code that assumes it is an int. */ + int has_arg; + int *flag; + int val; +}; + +/* Names for the values of the `has_arg' field of `struct option'. */ + +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 + +#if defined (__STDC__) && __STDC__ +/* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is + undefined, we haven't run the autoconf check so provide the + declaration without arguments. If it is 0, we checked and failed + to find the declaration so provide a fully prototyped one. If it + is 1, we found it so don't provide any declaration at all. */ +#if defined (__GNU_LIBRARY__) || (defined (HAVE_DECL_GETOPT) && !HAVE_DECL_GETOPT) +/* Many other libraries have conflicting prototypes for getopt, with + differences in the consts, in stdlib.h. To avoid compilation + errors, only prototype getopt for the GNU C library. */ +extern int getopt (int argc, char *const *argv, const char *shortopts); +#else /* not __GNU_LIBRARY__ */ +# if !defined (HAVE_DECL_GETOPT) +extern int getopt (); +# endif +#endif /* __GNU_LIBRARY__ */ +extern int getopt_long (int argc, char *const *argv, const char *shortopts, + const struct option *longopts, int *longind); +extern int getopt_long_only (int argc, char *const *argv, + const char *shortopts, + const struct option *longopts, int *longind); + +/* Internal only. Users should not call this directly. */ +extern int _getopt_internal (int argc, char *const *argv, + const char *shortopts, + const struct option *longopts, int *longind, + int long_only); +#else /* not __STDC__ */ +extern int getopt (); +extern int getopt_long (); +extern int getopt_long_only (); + +extern int _getopt_internal (int argc, char *const *argv, + const char *shortopts, + const struct option *longopts, int *longind, + int long_only); +#endif /* __STDC__ */ + +#ifdef __cplusplus +} +#endif + +#endif /* getopt.h */ diff --git a/src/getopt1.cpp b/src/getopt1.cpp new file mode 100644 index 00000000..b32d85bc --- /dev/null +++ b/src/getopt1.cpp @@ -0,0 +1,180 @@ +/* getopt_long and getopt_long_only entry points for GNU getopt. + Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 + Free Software Foundation, Inc. + + NOTE: This source is derived from an old version taken from the GNU C + Library (glibc). + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "getopt.h" + +#if !defined __STDC__ || !__STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +#ifndef const +#define const +#endif +#endif + +#include + +/* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#define GETOPT_INTERFACE_VERSION 2 +#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 +#include +#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION +#define ELIDE_CODE +#endif +#endif + +#ifndef ELIDE_CODE + + +/* This needs to come after some library #include + to get __GNU_LIBRARY__ defined. */ +#ifdef __GNU_LIBRARY__ +#include +#endif + +#ifndef NULL +#define NULL 0 +#endif + +int +getopt_long (int argc, char *const *argv, const char *options, const struct option *long_options, int *opt_index) +{ + return _getopt_internal (argc, argv, options, long_options, opt_index, 0); +} + +/* Like getopt_long, but '-' as well as '--' can indicate a long option. + If an option that starts with '-' (not '--') doesn't match a long option, + but does match a short option, it is parsed as a short option + instead. */ + +int +getopt_long_only (int argc, char *const *argv, const char *options, const struct option *long_options, int *opt_index) +{ + return _getopt_internal (argc, argv, options, long_options, opt_index, 1); +} + + +#endif /* Not ELIDE_CODE. */ + +#ifdef TEST + +#include + +int +main (argc, argv) + int argc; + char **argv; +{ + int c; + int digit_optind = 0; + + while (1) + { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = + { + {"add", 1, 0, 0}, + {"append", 0, 0, 0}, + {"delete", 1, 0, 0}, + {"verbose", 0, 0, 0}, + {"create", 0, 0, 0}, + {"file", 1, 0, 0}, + {0, 0, 0, 0} + }; + + c = getopt_long (argc, argv, "abc:d:0123456789", + long_options, &option_index); + if (c == -1) + break; + + switch (c) + { + case 0: + printf ("option %s", long_options[option_index].name); + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf ("digits occur in two different argv-elements.\n"); + digit_optind = this_option_optind; + printf ("option %c\n", c); + break; + + case 'a': + printf ("option a\n"); + break; + + case 'b': + printf ("option b\n"); + break; + + case 'c': + printf ("option c with value `%s'\n", optarg); + break; + + case 'd': + printf ("option d with value `%s'\n", optarg); + break; + + case '?': + break; + + default: + printf ("?? getopt returned character code 0%o ??\n", c); + } + } + + if (optind < argc) + { + printf ("non-option ARGV-elements: "); + while (optind < argc) + printf ("%s ", argv[optind++]); + printf ("\n"); + } + + exit (0); +} + +#endif /* TEST */ diff --git a/src/hq2x.cpp b/src/hq2x.cpp new file mode 100644 index 00000000..e5cac32f --- /dev/null +++ b/src/hq2x.cpp @@ -0,0 +1,604 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ +#include "System.h" +#include "interp.h" + +/***************************************************************************/ +/* HQ2x C implementation */ + +/* + * This effect is a rewritten implementation of the hq2x effect made by Maxim Stepin + */ + +static void hq2x_16_def(u16* dst0, u16* dst1, const u16* src0, const u16* src1, const u16* src2, unsigned count) +{ + unsigned i; + + for(i=0;i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); + + hq2x_16_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch; + dst1 += dstPitch; + hq2x_16_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + hq2x_16_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); + hq2x_32_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2x_32_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2x_32_def(dst0, dst1, src0, src1, src1, width); +} + +void lq2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); + + lq2x_16_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch; + dst1 += dstPitch; + lq2x_16_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + lq2x_16_def(dst0, dst1, src0, src1, src1, width); +} + +void lq2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); + lq2x_32_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while(count) { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + lq2x_32_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + lq2x_32_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2x_init(unsigned bits_per_pixel) +{ + interp_set(bits_per_pixel); +} diff --git a/src/hq2x.dat b/src/hq2x.dat new file mode 100644 index 00000000..a9cba209 --- /dev/null +++ b/src/hq2x.dat @@ -0,0 +1,1960 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is a rewritten implementation of the hq effect made by Maxim Stepin + */ + +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 10 : +case 138 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 11 : +case 139 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 14 : +case 142 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); + P(1, 0) = I2(3, 1, 4, 5); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(1, 0) = I3(5, 2, 1, 4, 1, 5); +} +} break; +case 15 : +case 143 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(3, 1, 4, 5); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(1, 0) = I3(5, 2, 1, 4, 1, 5); +} +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 18 : +case 50 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(1, 0) = I3(3, 3, 2, 1, 5, 4); +} +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 22 : +case 54 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 23 : +case 55 : +{ +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(1, 0) = I3(3, 3, 2, 1, 5, 4); +} +} break; +case 24 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 25 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 26 : +case 31 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 27 : +{ +P(1, 0) = I2(3, 1, 4, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 28 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 29 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 30 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} break; +case 42 : +case 170 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); + P(0, 1) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(0, 1) = I3(5, 2, 1, 4, 3, 7); +} +} break; +case 43 : +case 171 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(0, 1) = I3(5, 2, 1, 4, 3, 7); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 56 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 57 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 58 : +{ +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 59 : +{ +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 60 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 61 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +} break; +case 62 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 63 : +{ +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I3(2, 1, 1, 4, 7, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 66 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 67 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 70 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 71 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 72 : +case 76 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 73 : +case 77 : +{ +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 0) = I3(5, 2, 1, 4, 3, 1); + P(0, 1) = I3(3, 3, 2, 3, 7, 4); +} +} break; +case 74 : +case 107 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 75 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 6); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 78 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 79 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 80 : +case 81 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 82 : +case 214 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 83 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 84 : +case 85 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 0) = I3(5, 2, 1, 4, 5, 1); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 86 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 87 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 88 : +case 248 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 89 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 90 : +{ +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 91 : +{ +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 92 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 93 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 94 : +{ +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 95 : +{ +P(0, 1) = I2(3, 1, 4, 6); +P(1, 1) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 98 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 99 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 102 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 103 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +} break; +case 104 : +case 108 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 105 : +case 109 : +{ +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(5, 2, 1, 4, 3, 1); + P(0, 1) = I3(3, 3, 2, 3, 7, 4); +} +} break; +case 106 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 110 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 111 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I3(2, 1, 1, 4, 5, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 112 : +case 113 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDR) { + P(0, 1) = I2(3, 1, 4, 3); + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(0, 1) = I3(5, 2, 1, 4, 7, 3); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 114 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 115 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 116 : +case 117 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 118 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 119 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(1, 0) = I3(3, 3, 2, 1, 5, 4); +} +} break; +case 120 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 121 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 122 : +{ +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I2(3, 1, 4, 8); +} else { + P(1, 1) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 123 : +{ +P(1, 0) = I2(3, 1, 4, 2); +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 124 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 125 : +{ +P(1, 0) = I2(3, 1, 4, 1); +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(5, 2, 1, 4, 3, 1); + P(0, 1) = I3(3, 3, 2, 3, 7, 4); +} +} break; +case 126 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 127 : +{ +P(1, 1) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 146 : +case 178 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); + P(1, 1) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I3(3, 3, 2, 1, 5, 4); + P(1, 1) = I3(5, 2, 1, 4, 5, 7); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 150 : +case 182 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I3(3, 3, 2, 1, 5, 4); + P(1, 1) = I3(5, 2, 1, 4, 5, 7); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 152 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 153 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 154 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 155 : +{ +P(1, 0) = I2(3, 1, 4, 2); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 156 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 157 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 158 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 159 : +{ +P(0, 1) = I3(2, 1, 1, 4, 6, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 184 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 185 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 186 : +{ +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 187 : +{ +P(1, 0) = I2(3, 1, 4, 2); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(0, 1) = I3(5, 2, 1, 4, 3, 7); +} +} break; +case 188 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 189 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +} break; +case 190 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I3(3, 3, 2, 1, 5, 4); + P(1, 1) = I3(5, 2, 1, 4, 5, 7); +} +} break; +case 191 : +{ +P(0, 1) = I2(3, 1, 4, 7); +P(1, 1) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 194 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 195 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 198 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 199 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 200 : +case 204 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); + P(1, 1) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I3(3, 3, 2, 3, 7, 4); + P(1, 1) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +} break; +case 202 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 203 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 6); +P(1, 1) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 206 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 207 : +{ +P(0, 1) = I2(3, 1, 4, 6); +P(1, 1) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(3, 1, 4, 5); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(1, 0) = I3(5, 2, 1, 4, 1, 5); +} +} break; +case 208 : +case 209 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 210 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 211 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 2); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 212 : +case 213 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(5, 2, 1, 4, 5, 1); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 215 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I3(2, 1, 1, 4, 3, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 216 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 217 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 218 : +{ +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 219 : +{ +P(1, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 220 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +if (MDL) { + P(0, 1) = I2(3, 1, 4, 6); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 221 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(5, 2, 1, 4, 5, 1); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 222 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 223 : +{ +P(0, 1) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 226 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 227 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 230 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 231 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +} break; +case 232 : +case 236 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +if (MDL) { + P(0, 1) = I1(4); + P(1, 1) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I3(3, 3, 2, 3, 7, 4); + P(1, 1) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 235 : +{ +P(1, 0) = I3(2, 1, 1, 4, 2, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 238 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(1, 1) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I3(3, 3, 2, 3, 7, 4); + P(1, 1) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 239 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 240 : +case 241 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDR) { + P(0, 1) = I2(3, 1, 4, 3); + P(1, 1) = I1(4); +} else { + P(0, 1) = I3(5, 2, 1, 4, 7, 3); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 242 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(3, 1, 4, 2); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 243 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 2); +if (MDR) { + P(0, 1) = I2(3, 1, 4, 3); + P(1, 1) = I1(4); +} else { + P(0, 1) = I3(5, 2, 1, 4, 7, 3); + P(1, 1) = I3(3, 3, 2, 5, 7, 4); +} +} break; +case 244 : +case 245 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +} break; +case 246 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 3); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 247 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(2, 1, 1, 4, 1, 2); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 250 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 2); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 251 : +{ +P(1, 0) = I2(3, 1, 4, 2); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 252 : +{ +P(0, 0) = I3(2, 1, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +} break; +case 254 : +{ +P(0, 0) = I2(3, 1, 4, 0); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 255 : +{ +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; diff --git a/src/hq2x.h b/src/hq2x.h new file mode 100644 index 00000000..64c5778b --- /dev/null +++ b/src/hq2x.h @@ -0,0 +1,1824 @@ +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); +} break; +case 10 : +case 138 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 11 : +case 139 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 14 : +case 142 : +{ + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 15 : +case 143 : +{ + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); +} break; +case 18 : +case 50 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 19 : +case 51 : +{ + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = I31(4, 2); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); +} break; +case 22 : +case 54 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 23 : +case 55 : +{ + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = IC(4); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 24 : +case 66 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 25 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 26 : +case 31 : +case 95 : +{ + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 27 : +case 75 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 28 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 29 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 30 : +case 86 : +{ + P0 = I31(4, 0); + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); +} break; +case 42 : +case 170 : +{ + P1 = I31(4, 2); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 43 : +case 171 : +{ + P1 = I31(4, 2); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 46 : +case 174 : +{ + P1 = I31(4, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 47 : +case 175 : +{ + P1 = I31(4, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 56 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 57 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 58 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 59 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 60 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 61 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 62 : +{ + P0 = I31(4, 0); + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 63 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 67 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 70 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 71 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 72 : +case 76 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 73 : +case 77 : +{ + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = I31(4, 6); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 74 : +case 107 : +case 123 : +{ + P1 = I31(4, 2); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 78 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 79 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 80 : +case 81 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 82 : +case 214 : +case 222 : +{ + P0 = I31(4, 0); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 83 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 84 : +case 85 : +{ + P0 = I211(4, 1, 3); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = I31(4, 8); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 87 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 88 : +case 248 : +case 250 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 89 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 90 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 91 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 92 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 93 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 94 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 98 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 99 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 102 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 103 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 104 : +case 108 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 105 : +case 109 : +{ + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = IC(4); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 106 : +case 120 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 110 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 111 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 112 : +case 113 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = I31(4, 8); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 114 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 115 : +{ + P0 = I31(4, 3); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 116 : +case 117 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 118 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 119 : +{ + P2 = I31(4, 3); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = IC(4); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 121 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 122 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 124 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 125 : +{ + P1 = I31(4, 1); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = IC(4); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 126 : +{ + P0 = I31(4, 0); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 127 : +{ + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); +} break; +case 146 : +case 178 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + if (MUR) { + P1 = I31(4, 2); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 147 : +case 179 : +{ + P0 = I31(4, 3); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); +} break; +case 150 : +case 182 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + if (MUR) { + P1 = IC(4); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 151 : +case 183 : +{ + P0 = I31(4, 3); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 152 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 153 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 154 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 155 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 156 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 157 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 158 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 159 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 184 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 185 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 186 : +{ + P2 = I31(4, 7); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 187 : +{ + P1 = I31(4, 2); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 188 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 189 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 190 : +{ + P0 = I31(4, 0); + P2 = I31(4, 7); + if (MUR) { + P1 = IC(4); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 191 : +{ + P2 = I31(4, 7); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 194 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 195 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 198 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 199 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 200 : +case 204 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + if (MDL) { + P2 = I31(4, 6); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 201 : +case 205 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } +} break; +case 202 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 203 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 206 : +{ + P1 = I31(4, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 207 : +{ + P2 = I31(4, 6); + P3 = I31(4, 5); + if (MUL) { + P0 = IC(4); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 208 : +case 209 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 210 : +case 216 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 211 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 212 : +case 213 : +{ + P0 = I211(4, 1, 3); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = IC(4); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 215 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 217 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 218 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 219 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 220 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 221 : +{ + P0 = I31(4, 1); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = IC(4); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 223 : +{ + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 226 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 227 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 230 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 231 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 232 : +case 236 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + if (MDL) { + P2 = IC(4); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 233 : +case 237 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } +} break; +case 234 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 235 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 238 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + if (MDL) { + P2 = IC(4); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 239 : +{ + P1 = I31(4, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 240 : +case 241 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = IC(4); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 242 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 243 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = IC(4); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 244 : +case 245 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 246 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 247 : +{ + P0 = I31(4, 3); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 249 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 251 : +{ + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 252 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 253 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 254 : +{ + P0 = I31(4, 0); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 255 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; diff --git a/src/hq2x3.dat b/src/hq2x3.dat new file mode 100644 index 00000000..4b893c7e --- /dev/null +++ b/src/hq2x3.dat @@ -0,0 +1,2478 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is derived from the hq effect made by Maxim Stepin + */ + +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 10 : +case 138 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 11 : +case 139 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 14 : +case 142 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I2(13, 3, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 5, 1, 1, 3, 4); + P(1, 0) = I3(7, 6, 3, 4, 1, 5); + P(0, 1) = I2(13, 3, 4, 3); +} +} break; +case 15 : +case 143 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 5, 1, 1, 3, 4); + P(1, 0) = I3(7, 6, 3, 4, 1, 5); + P(0, 1) = I2(13, 3, 4, 3); +} +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 18 : +case 50 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I2(13, 3, 4, 2); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 3); + P(1, 0) = I2(13, 3, 4, 2); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(7, 6, 3, 4, 1, 3); + P(1, 0) = I3(10, 5, 1, 1, 5, 4); + P(1, 1) = I2(13, 3, 4, 5); +} +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 22 : +case 54 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 23 : +case 55 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 3); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(7, 6, 3, 4, 1, 3); + P(1, 0) = I3(10, 5, 1, 1, 5, 4); + P(1, 1) = I2(13, 3, 4, 5); +} +} break; +case 24 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 25 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 26 : +case 31 : +{ +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 27 : +{ +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 28 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 29 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 30 : +{ +P(0, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} break; +case 42 : +case 170 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(7, 5, 4, 1, 3, 4); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(9, 4, 3, 4, 7, 3); +} +} break; +case 43 : +case 171 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(7, 5, 4, 1, 3, 4); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(9, 4, 3, 4, 7, 3); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 56 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 57 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 58 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 59 : +{ +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (!MUR) { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I2(13, 3, 4, 2); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 60 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 61 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} break; +case 62 : +{ +P(0, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 63 : +{ +P(0, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I3(12, 3, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 66 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 67 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 70 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 71 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 72 : +case 76 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 73 : +case 77 : +{ +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 0) = I3(9, 4, 3, 4, 1, 3); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(7, 5, 4, 7, 3, 4); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 74 : +case 107 : +{ +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +} break; +case 75 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 78 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 8); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 79 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 8); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 5, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 80 : +case 81 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 82 : +case 214 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +if (MDR) { + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +} break; +case 83 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 6); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 84 : +case 85 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 0) = I3(9, 4, 3, 4, 1, 5); + P(1, 1) = I2(1, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(7, 5, 4, 7, 5, 4); +} +} break; +case 86 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 87 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I2(13, 3, 4, 6); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 3); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 3, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 88 : +case 248 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 89 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 90 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 91 : +{ +P(1, 1) = I1(4); +if (!MUR) { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I2(13, 3, 4, 2); +} +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 92 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 93 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 94 : +{ +P(0, 1) = I1(4); +if (!MUL) { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I2(13, 3, 4, 0); +} +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 95 : +{ +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 98 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 99 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 102 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 103 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +} break; +case 104 : +case 108 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 105 : +case 109 : +{ +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 0) = I3(9, 4, 3, 4, 1, 3); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(7, 5, 4, 7, 3, 4); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 106 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 110 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 111 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 112 : +case 113 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 3); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 1) = I2(13, 3, 4, 5); + P(0, 2) = I3(7, 6, 3, 7, 4, 3); + P(1, 2) = I3(10, 5, 1, 7, 5, 4); +} +} break; +case 114 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 115 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 116 : +case 117 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 118 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 119 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 3); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(7, 6, 3, 4, 1, 3); + P(1, 0) = I3(10, 5, 1, 1, 5, 4); + P(1, 1) = I2(13, 3, 4, 5); +} +} break; +case 120 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 121 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(1, 1) = I1(4); +if (!MDR) { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I2(13, 3, 4, 8); +} +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +} break; +case 122 : +{ +P(1, 1) = I1(4); +if (!MDR) { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I2(13, 3, 4, 8); +} +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 123 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +} break; +case 124 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 125 : +{ +P(1, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 0) = I3(9, 4, 3, 4, 1, 3); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(7, 5, 4, 7, 3, 4); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +} break; +case 126 : +{ +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 127 : +{ +if (!MUR) { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I2(15, 1, 4, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I1(4); +} +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 8); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 8, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 146 : +case 178 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I2(13, 3, 4, 2); + P(1, 1) = I1(4); + P(1, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(7, 5, 4, 1, 5, 4); + P(1, 1) = I2(1, 1, 4, 5); + P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 150 : +case 182 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); + P(1, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(7, 5, 4, 1, 5, 4); + P(1, 1) = I2(1, 1, 4, 5); + P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(9, 4, 3, 4, 7, 3); +P(1, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 152 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 153 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 154 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 155 : +{ +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 156 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 157 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 158 : +{ +P(0, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (!MUL) { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I2(13, 3, 4, 0); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 159 : +{ +P(1, 1) = I1(4); +P(0, 2) = I3(12, 3, 1, 4, 6, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 184 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 185 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 186 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 187 : +{ +P(1, 1) = I1(4); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(7, 5, 4, 1, 3, 4); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(1, 1, 3, 4); + P(0, 2) = I3(9, 4, 3, 4, 7, 3); +} +} break; +case 188 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 189 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +} break; +case 190 : +{ +P(0, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); + P(1, 1) = I1(4); + P(1, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(7, 5, 4, 1, 5, 4); + P(1, 1) = I2(1, 1, 4, 5); + P(1, 2) = I3(9, 4, 3, 4, 7, 5); +} +} break; +case 191 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 194 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 195 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 198 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 199 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 200 : +case 204 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I2(13, 3, 4, 5); +} else { + P(0, 1) = I2(13, 3, 4, 3); + P(0, 2) = I3(10, 5, 1, 7, 3, 4); + P(1, 2) = I3(7, 6, 3, 7, 4, 5); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +} break; +case 202 : +{ +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 203 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 206 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 207 : +{ +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 5, 1, 1, 3, 4); + P(1, 0) = I3(7, 6, 3, 4, 1, 5); + P(0, 1) = I2(13, 3, 4, 3); +} +} break; +case 208 : +case 209 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 210 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 211 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 212 : +case 213 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 0) = I3(9, 4, 3, 4, 1, 5); + P(1, 1) = I2(1, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(7, 5, 4, 7, 5, 4); +} +} break; +case 215 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +if (MDR) { + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 216 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 217 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 218 : +{ +P(0, 1) = I1(4); +if (!MDL) { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I2(13, 3, 4, 6); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 219 : +{ +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 220 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +if (!MDL) { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I2(13, 3, 4, 6); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 221 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +if (MDR) { + P(1, 0) = I2(3, 1, 4, 1); + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(1, 0) = I3(9, 4, 3, 4, 1, 5); + P(1, 1) = I2(1, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(7, 5, 4, 7, 5, 4); +} +} break; +case 222 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDR) { + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +} break; +case 223 : +{ +if (!MUL) { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I2(15, 1, 4, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I1(4); +} +if (MDR) { + P(0, 2) = I2(13, 3, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(12, 3, 1, 4, 6, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 226 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 227 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 230 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 231 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I2(13, 3, 4, 5); +P(0, 2) = I2(13, 3, 4, 3); +P(1, 2) = I2(13, 3, 4, 5); +} break; +case 232 : +case 236 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 5); +} else { + P(0, 1) = I2(13, 3, 4, 3); + P(0, 2) = I3(10, 5, 1, 7, 3, 4); + P(1, 2) = I3(7, 6, 3, 7, 4, 5); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I2(13, 3, 4, 2); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 5); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I3(12, 3, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 235 : +{ +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +} break; +case 238 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 5); +P(1, 1) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I2(13, 3, 4, 5); +} else { + P(0, 1) = I2(13, 3, 4, 3); + P(0, 2) = I3(10, 5, 1, 7, 3, 4); + P(1, 2) = I3(7, 6, 3, 7, 4, 5); +} +} break; +case 239 : +{ +P(1, 0) = I2(13, 3, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(13, 3, 4, 5); +P(1, 2) = I2(13, 3, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 240 : +case 241 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 3); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(13, 3, 4, 5); + P(0, 2) = I3(7, 6, 3, 7, 4, 3); + P(1, 2) = I3(10, 5, 1, 7, 5, 4); +} +} break; +case 242 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 3); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(0, 2) = I3(12, 3, 1, 4, 3, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 243 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(1, 0) = I2(13, 3, 4, 2); +P(0, 1) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 1) = I1(4); + P(0, 2) = I2(13, 3, 4, 3); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(13, 3, 4, 5); + P(0, 2) = I3(7, 6, 3, 7, 4, 3); + P(1, 2) = I3(10, 5, 1, 7, 5, 4); +} +} break; +case 244 : +case 245 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 246 : +{ +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +} break; +case 247 : +{ +P(0, 0) = I2(13, 3, 4, 3); +P(0, 1) = I2(13, 3, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 3); +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I3(12, 3, 1, 4, 2, 1); +P(0, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 250 : +{ +P(0, 0) = I2(13, 3, 4, 0); +P(1, 0) = I2(13, 3, 4, 2); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +} break; +case 251 : +{ +if (!MDR) { + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I2(15, 1, 4, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I1(4); +} +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(13, 3, 4, 2); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I3(12, 3, 1, 4, 2, 1); +} +} break; +case 252 : +{ +P(0, 0) = I3(12, 3, 1, 4, 0, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +} break; +case 254 : +{ +if (!MDL) { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I2(15, 1, 4, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I1(4); +} +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); +} +if (MDR) { + P(1, 1) = I1(4); + P(1, 2) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUR) { + P(0, 0) = I2(13, 3, 4, 0); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(12, 3, 1, 4, 0, 1); + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +} break; +case 255 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; diff --git a/src/hq2x4.dat b/src/hq2x4.dat new file mode 100644 index 00000000..96982038 --- /dev/null +++ b/src/hq2x4.dat @@ -0,0 +1,2748 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is derived from the hq effect made by Maxim Stepin + */ + +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 10 : +case 138 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 11 : +case 139 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 14 : +case 142 : +{ +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(1, 0) = I2(3, 1, 4, 5); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I2(9, 7, 1, 3); + P(1, 0) = I2(1, 1, 1, 4); + P(0, 1) = I3(8, 5, 3, 4, 3, 1); +} +} break; +case 15 : +case 143 : +{ +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(3, 1, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(9, 7, 1, 3); + P(1, 0) = I2(1, 1, 1, 4); + P(0, 1) = I3(8, 5, 3, 4, 3, 1); +} +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 18 : +case 50 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(0, 0) = I2(1, 1, 1, 4); + P(1, 0) = I2(9, 7, 1, 5); + P(1, 1) = I3(8, 5, 3, 4, 5, 1); +} +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 22 : +case 54 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 23 : +case 55 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I2(1, 1, 1, 4); + P(1, 0) = I2(9, 7, 1, 5); + P(1, 1) = I3(8, 5, 3, 4, 5, 1); +} +} break; +case 24 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 25 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 26 : +case 31 : +{ +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 27 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 28 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 29 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 30 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +} break; +case 42 : +case 170 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); + P(0, 2) = I2(7, 1, 4, 7); + P(0, 3) = I2(5, 3, 4, 7); +} else { + P(0, 0) = I3(4, 3, 1, 1, 3, 4); + P(0, 1) = I3(3, 3, 2, 3, 4, 1); + P(0, 2) = I3(9, 6, 1, 4, 3, 7); + P(0, 3) = I3(11, 3, 2, 4, 7, 3); +} +} break; +case 43 : +case 171 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); + P(0, 2) = I2(7, 1, 4, 7); + P(0, 3) = I2(5, 3, 4, 7); +} else { + P(0, 0) = I3(4, 3, 1, 1, 3, 4); + P(0, 1) = I3(3, 3, 2, 3, 4, 1); + P(0, 2) = I3(9, 6, 1, 4, 3, 7); + P(0, 3) = I3(11, 3, 2, 4, 7, 3); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I3(11, 3, 2, 4, 5, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(9, 4, 3, 4, 7, 5); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 56 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 57 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 58 : +{ +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 59 : +{ +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 60 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 61 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +} break; +case 62 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 63 : +{ +P(0, 1) = I1(4); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I3(5, 2, 1, 4, 8, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 66 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 67 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 70 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 71 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 72 : +case 76 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 73 : +case 77 : +{ +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 0) = I2(5, 3, 4, 1); + P(0, 1) = I2(7, 1, 4, 1); + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 0) = I3(11, 3, 2, 4, 1, 3); + P(0, 1) = I3(9, 6, 1, 4, 3, 1); + P(0, 2) = I3(3, 3, 2, 3, 4, 7); + P(0, 3) = I3(4, 3, 1, 7, 3, 4); +} +} break; +case 74 : +case 107 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 75 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 78 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 79 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 80 : +case 81 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 82 : +case 214 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 83 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 84 : +case 85 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 0) = I2(5, 3, 4, 1); + P(1, 1) = I2(7, 1, 4, 1); + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 0) = I3(11, 3, 2, 4, 1, 5); + P(1, 1) = I3(9, 6, 1, 4, 5, 1); + P(1, 2) = I3(3, 3, 2, 4, 5, 7); + P(1, 3) = I3(4, 3, 1, 7, 5, 4); +} +} break; +case 86 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 87 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 88 : +case 248 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 89 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 90 : +{ +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 91 : +{ +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 92 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 93 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 94 : +{ +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 95 : +{ +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(11, 5, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 98 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 99 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 102 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 103 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +} break; +case 104 : +case 108 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 105 : +case 109 : +{ +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 0) = I2(5, 3, 4, 1); + P(0, 1) = I2(7, 1, 4, 1); + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 0) = I3(11, 3, 2, 4, 1, 3); + P(0, 1) = I3(9, 6, 1, 4, 3, 1); + P(0, 2) = I3(3, 3, 2, 3, 4, 7); + P(0, 3) = I3(4, 3, 1, 7, 3, 4); +} +} break; +case 106 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 110 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 111 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I3(6, 1, 1, 4, 5, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 112 : +case 113 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(0, 3) = I2(3, 1, 4, 3); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I3(8, 5, 3, 4, 5, 7); + P(0, 3) = I2(1, 1, 4, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 114 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 115 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 116 : +case 117 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(3, 1, 4, 3); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 118 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 119 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(13, 3, 4, 8); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(11, 5, 4, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(0, 0) = I2(1, 1, 1, 4); + P(1, 0) = I2(9, 7, 1, 5); + P(1, 1) = I3(8, 5, 3, 4, 5, 1); +} +} break; +case 120 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 121 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +} break; +case 122 : +{ +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 2) = I2(13, 3, 4, 8); + P(1, 3) = I2(11, 5, 4, 8); +} else { + P(1, 2) = I2(7, 1, 4, 5); + P(1, 3) = I3(5, 2, 1, 4, 7, 5); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 123 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 124 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +} break; +case 125 : +{ +P(1, 0) = I2(5, 3, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 0) = I2(5, 3, 4, 1); + P(0, 1) = I2(7, 1, 4, 1); + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 0) = I3(11, 3, 2, 4, 1, 3); + P(0, 1) = I3(9, 6, 1, 4, 3, 1); + P(0, 2) = I3(3, 3, 2, 3, 4, 7); + P(0, 3) = I3(4, 3, 1, 7, 3, 4); +} +} break; +case 126 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 127 : +{ +P(0, 1) = I1(4); +P(1, 2) = I2(13, 3, 4, 8); +P(1, 3) = I2(11, 5, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 146 : +case 178 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); + P(1, 2) = I2(7, 1, 4, 7); + P(1, 3) = I2(5, 3, 4, 7); +} else { + P(1, 0) = I3(4, 3, 1, 1, 5, 4); + P(1, 1) = I3(3, 3, 2, 4, 5, 1); + P(1, 2) = I3(9, 6, 1, 4, 5, 7); + P(1, 3) = I3(11, 3, 2, 4, 7, 5); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I2(5, 3, 4, 7); +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 150 : +case 182 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); + P(1, 2) = I2(7, 1, 4, 7); + P(1, 3) = I2(5, 3, 4, 7); +} else { + P(1, 0) = I3(4, 3, 1, 1, 5, 4); + P(1, 1) = I3(3, 3, 2, 4, 5, 1); + P(1, 2) = I3(9, 6, 1, 4, 5, 7); + P(1, 3) = I3(11, 3, 2, 4, 7, 5); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(11, 3, 2, 4, 3, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(9, 4, 3, 4, 7, 3); +P(1, 3) = I2(5, 3, 4, 7); +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 152 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 153 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 154 : +{ +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 155 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 156 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 157 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 158 : +{ +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 159 : +{ +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I3(5, 2, 1, 4, 6, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 184 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 185 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 186 : +{ +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 187 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(1, 2) = I2(7, 1, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); + P(0, 2) = I2(7, 1, 4, 7); + P(0, 3) = I2(5, 3, 4, 7); +} else { + P(0, 0) = I3(4, 3, 1, 1, 3, 4); + P(0, 1) = I3(3, 3, 2, 3, 4, 1); + P(0, 2) = I3(9, 6, 1, 4, 3, 7); + P(0, 3) = I3(11, 3, 2, 4, 7, 3); +} +} break; +case 188 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 189 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +} break; +case 190 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(0, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); + P(1, 2) = I2(7, 1, 4, 7); + P(1, 3) = I2(5, 3, 4, 7); +} else { + P(1, 0) = I3(4, 3, 1, 1, 5, 4); + P(1, 1) = I3(3, 3, 2, 4, 5, 1); + P(1, 2) = I3(9, 6, 1, 4, 5, 7); + P(1, 3) = I3(11, 3, 2, 4, 7, 5); +} +} break; +case 191 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(7, 1, 4, 7); +P(1, 2) = I2(7, 1, 4, 7); +P(0, 3) = I2(5, 3, 4, 7); +P(1, 3) = I2(5, 3, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 194 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 195 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 198 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 199 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 200 : +case 204 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); + P(1, 3) = I2(3, 1, 4, 5); +} else { + P(0, 2) = I3(8, 5, 3, 4, 3, 7); + P(0, 3) = I2(9, 7, 7, 3); + P(1, 3) = I2(1, 1, 4, 7); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +} break; +case 202 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 203 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 206 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 207 : +{ +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(13, 3, 4, 6); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(11, 5, 4, 6); +P(1, 3) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I2(3, 1, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(9, 7, 1, 3); + P(1, 0) = I2(1, 1, 1, 4); + P(0, 1) = I3(8, 5, 3, 4, 3, 1); +} +} break; +case 208 : +case 209 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 210 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 211 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 212 : +case 213 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 0) = I2(5, 3, 4, 1); + P(1, 1) = I2(7, 1, 4, 1); + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 0) = I3(11, 3, 2, 4, 1, 5); + P(1, 1) = I3(9, 6, 1, 4, 5, 1); + P(1, 2) = I3(3, 3, 2, 4, 5, 7); + P(1, 3) = I3(4, 3, 1, 7, 5, 4); +} +} break; +case 215 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(6, 1, 1, 4, 3, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 216 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 217 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 218 : +{ +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 219 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 220 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +if (MDL) { + P(0, 2) = I2(13, 3, 4, 6); + P(0, 3) = I2(11, 5, 4, 6); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 221 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 0) = I2(5, 3, 4, 1); + P(1, 1) = I2(7, 1, 4, 1); + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 0) = I3(11, 3, 2, 4, 1, 5); + P(1, 1) = I3(9, 6, 1, 4, 5, 1); + P(1, 2) = I3(3, 3, 2, 4, 5, 7); + P(1, 3) = I3(4, 3, 1, 7, 5, 4); +} +} break; +case 222 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 223 : +{ +P(1, 1) = I1(4); +P(0, 2) = I2(13, 3, 4, 6); +P(0, 3) = I2(11, 5, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 226 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 227 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 230 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 231 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I2(3, 1, 4, 5); +P(0, 3) = I2(3, 1, 4, 3); +P(1, 3) = I2(3, 1, 4, 5); +} break; +case 232 : +case 236 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(1, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); + P(1, 3) = I2(3, 1, 4, 5); +} else { + P(0, 2) = I3(8, 5, 3, 4, 3, 7); + P(0, 3) = I2(9, 7, 7, 3); + P(1, 3) = I2(1, 1, 4, 7); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(9, 4, 3, 4, 1, 5); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I3(11, 3, 2, 4, 5, 1); +P(0, 2) = I1(4); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I2(11, 5, 4, 0); + P(0, 1) = I2(13, 3, 4, 0); +} else { + P(0, 0) = I3(5, 2, 1, 4, 1, 3); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 235 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I3(6, 1, 1, 4, 2, 5); +P(0, 2) = I1(4); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 238 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(3, 1, 4, 5); +P(1, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); + P(1, 3) = I2(3, 1, 4, 5); +} else { + P(0, 2) = I3(8, 5, 3, 4, 3, 7); + P(0, 3) = I2(9, 7, 7, 3); + P(1, 3) = I2(1, 1, 4, 7); +} +} break; +case 239 : +{ +P(1, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I2(3, 1, 4, 5); +P(0, 2) = I1(4); +P(1, 2) = I2(3, 1, 4, 5); +P(1, 3) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 240 : +case 241 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I1(4); + P(0, 3) = I2(3, 1, 4, 3); + P(1, 3) = I1(4); +} else { + P(1, 2) = I3(8, 5, 3, 4, 5, 7); + P(0, 3) = I2(1, 1, 4, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 242 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUR) { + P(1, 0) = I2(11, 5, 4, 2); + P(1, 1) = I2(13, 3, 4, 2); +} else { + P(1, 0) = I3(5, 2, 1, 4, 1, 5); + P(1, 1) = I2(7, 1, 4, 5); +} +} break; +case 243 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 2) = I1(4); + P(0, 3) = I2(3, 1, 4, 3); + P(1, 3) = I1(4); +} else { + P(1, 2) = I3(8, 5, 3, 4, 5, 7); + P(0, 3) = I2(1, 1, 4, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 244 : +case 245 : +{ +P(0, 0) = I3(9, 4, 3, 4, 1, 3); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I3(11, 3, 2, 4, 3, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 246 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I3(6, 1, 1, 4, 0, 3); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 247 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(0, 3) = I2(3, 1, 4, 3); +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I3(5, 2, 1, 4, 2, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 250 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(1, 0) = I2(11, 5, 4, 2); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(13, 3, 4, 2); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +} break; +case 251 : +{ +P(1, 0) = I2(11, 5, 4, 2); +P(1, 1) = I2(13, 3, 4, 2); +P(0, 2) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 252 : +{ +P(0, 0) = I3(5, 2, 1, 4, 0, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 1) = I2(7, 1, 4, 1); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I2(5, 3, 4, 1); +P(1, 0) = I2(5, 3, 4, 1); +P(0, 1) = I2(7, 1, 4, 1); +P(1, 1) = I2(7, 1, 4, 1); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +} break; +case 254 : +{ +P(0, 0) = I2(11, 5, 4, 0); +P(0, 1) = I2(13, 3, 4, 0); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 255 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; diff --git a/src/hq3x.dat b/src/hq3x.dat new file mode 100644 index 00000000..04168fba --- /dev/null +++ b/src/hq3x.dat @@ -0,0 +1,2913 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is a rewritten implementation of the hq effect made by Maxim Stepin + */ + +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 10 : +case 138 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 11 : +case 139 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 14 : +case 142 : +{ +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 15 : +case 143 : +{ +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 18 : +case 50 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 2); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 2); + P(2, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 4, 5); +} +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 22 : +case 54 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 23 : +case 55 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 4, 5); +} +} break; +case 24 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 25 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 26 : +case 31 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 27 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 28 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 29 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 30 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} break; +case 42 : +case 170 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); + P(1, 0) = I1(4); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 4, 1); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 43 : +case 171 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 4, 1); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I3(2, 1, 1, 4, 5, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 56 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 57 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 58 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 59 : +{ +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 60 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 61 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 62 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 63 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 66 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 67 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 70 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 71 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 72 : +case 76 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 73 : +case 77 : +{ +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 6); + P(1, 2) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 4, 7); +} +} break; +case 74 : +case 107 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); +} +} break; +case 75 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 78 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 79 : +{ +P(2, 0) = I2(3, 1, 4, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 80 : +case 81 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 82 : +case 214 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); +} +} break; +case 83 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 84 : +case 85 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 0) = I2(3, 1, 4, 1); + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(1, 2) = I2(3, 1, 4, 7); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 86 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 87 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 88 : +case 248 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); +} +if (MDR) { + P(2, 1) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 89 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 90 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 91 : +{ +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 92 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 93 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 94 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 95 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 98 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 99 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 102 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 103 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +} break; +case 104 : +case 108 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 105 : +case 109 : +{ +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 4, 7); +} +} break; +case 106 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 110 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 111 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 112 : +case 113 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +if (MDR) { + P(2, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 3); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 1) = I2(3, 1, 4, 5); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 114 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 115 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 116 : +case 117 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 118 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 119 : +{ +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MUR) { + P(0, 0) = I2(3, 1, 4, 3); + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 4, 5); +} +} break; +case 120 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 121 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 122 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MDR) { + P(2, 2) = I2(3, 1, 4, 8); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 123 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); +} +} break; +case 124 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +} break; +case 125 : +{ +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 0) = I2(3, 1, 4, 1); + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 4, 7); +} +} break; +case 126 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 127 : +{ +P(1, 1) = I1(4); +P(2, 2) = I2(3, 1, 4, 8); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 146 : +case 178 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 2); + P(2, 1) = I1(4); + P(2, 2) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I2(3, 1, 4, 1); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 150 : +case 182 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); + P(2, 2) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I2(3, 1, 4, 1); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I3(2, 1, 1, 4, 3, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 152 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 153 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 154 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 155 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 156 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 157 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 158 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 159 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 184 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 185 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 186 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 187 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 7); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 4, 1); + P(0, 1) = I2(3, 1, 3, 4); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 188 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 189 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +} break; +case 190 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); + P(2, 2) = I2(3, 1, 4, 7); +} else { + P(1, 0) = I2(3, 1, 4, 1); + P(2, 0) = I2(1, 1, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 191 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 7); +P(1, 2) = I2(3, 1, 4, 7); +P(2, 2) = I2(3, 1, 4, 7); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 194 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 195 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 198 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 199 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 200 : +case 204 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 6); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I2(3, 1, 4, 3); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 202 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 203 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 206 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 207 : +{ +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 6); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(2, 0) = I2(3, 1, 4, 5); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(3, 1, 1, 4); + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 208 : +case 209 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 210 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 211 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 212 : +case 213 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 0) = I2(3, 1, 4, 1); + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(1, 2) = I2(3, 1, 4, 7); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 215 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 216 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 217 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 218 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 219 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); + P(0, 1) = I2(7, 1, 4, 3); +} +} break; +case 220 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I2(3, 1, 4, 6); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 221 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(2, 0) = I2(3, 1, 4, 1); + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(2, 1) = I2(3, 1, 5, 4); + P(1, 2) = I2(3, 1, 4, 7); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 222 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); +} +} break; +case 223 : +{ +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 6); +if (MDR) { + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(0, 1) = I2(7, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); + P(2, 1) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(2, 1, 1, 4, 1, 5); + P(2, 1) = I2(7, 1, 4, 5); +} +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 226 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 227 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 230 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 231 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +} break; +case 232 : +case 236 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I2(3, 1, 4, 3); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I3(2, 1, 1, 4, 1, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I2(3, 1, 4, 0); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 235 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); +} +} break; +case 238 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I2(3, 1, 4, 5); +} else { + P(0, 1) = I2(3, 1, 4, 3); + P(0, 2) = I2(1, 1, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 239 : +{ +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 5); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I2(3, 1, 4, 5); +P(1, 2) = I1(4); +P(2, 2) = I2(3, 1, 4, 5); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 240 : +case 241 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +if (MDR) { + P(2, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 3); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(3, 1, 4, 5); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 242 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUR) { + P(2, 0) = I2(3, 1, 4, 2); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 243 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +if (MDR) { + P(2, 1) = I1(4); + P(0, 2) = I2(3, 1, 4, 3); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(3, 1, 4, 5); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); + P(1, 2) = I2(3, 1, 7, 4); + P(2, 2) = I2(1, 1, 5, 7); +} +} break; +case 244 : +case 245 : +{ +P(0, 0) = I3(2, 1, 1, 4, 1, 3); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 246 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); +} +} break; +case 247 : +{ +P(0, 0) = I2(3, 1, 4, 3); +P(1, 0) = I1(4); +P(0, 1) = I2(3, 1, 4, 3); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(0, 2) = I2(3, 1, 4, 3); +P(1, 2) = I1(4); +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 2); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 1) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 250 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I1(4); +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); +} +if (MDR) { + P(2, 1) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +} break; +case 251 : +{ +P(2, 0) = I2(3, 1, 4, 2); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(2, 1, 1, 4, 3, 7); + P(1, 2) = I2(7, 1, 4, 7); +} +if (MDR) { + P(2, 1) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(2, 2) = I3(7, 7, 2, 5, 7, 4); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(7, 7, 2, 1, 3, 4); + P(1, 0) = I2(7, 1, 4, 1); +} +} break; +case 252 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); +} +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I2(3, 1, 4, 1); +P(1, 0) = I2(3, 1, 4, 1); +P(2, 0) = I2(3, 1, 4, 1); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +} break; +case 254 : +{ +P(0, 0) = I2(3, 1, 4, 0); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); + P(0, 2) = I1(4); +} else { + P(0, 1) = I2(7, 1, 4, 3); + P(0, 2) = I3(7, 7, 2, 3, 7, 4); +} +if (MDR) { + P(2, 1) = I1(4); + P(1, 2) = I1(4); + P(2, 2) = I1(4); +} else { + P(2, 1) = I2(7, 1, 4, 5); + P(1, 2) = I2(7, 1, 4, 7); + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUR) { + P(1, 0) = I1(4); + P(2, 0) = I1(4); +} else { + P(1, 0) = I2(7, 1, 4, 1); + P(2, 0) = I3(7, 7, 2, 1, 5, 4); +} +} break; +case 255 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(2, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(2, 1, 1, 4, 3, 7); +} +if (MDR) { + P(2, 2) = I1(4); +} else { + P(2, 2) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(2, 0) = I1(4); +} else { + P(2, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; diff --git a/src/hq3x32.cpp b/src/hq3x32.cpp new file mode 100644 index 00000000..4b05eff1 --- /dev/null +++ b/src/hq3x32.cpp @@ -0,0 +1,96 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include "win32/stdafx.h" +#include "win32/VBA.h" +#include "Util.h" + +#include + +extern "C" +{ +void hq3x_16(unsigned char*, unsigned char*, DWORD, DWORD, DWORD, DWORD); +void hq3x_32(unsigned char*, unsigned char*, DWORD, DWORD, DWORD, DWORD); +unsigned int LUT16to32[65536]; +unsigned int RGBtoYUV[65536]; +} + +int InitLUTs(void) +{ + int i, j, k, r, g, b, Y, u, v; + + for (i=0; i<65536; i++) + LUT16to32[i] = ((i & 0xF800) << 8) + ((i & 0x07E0) << 5) + ((i & 0x001F) << 3); + + for (i=0; i<32; i++) + for (j=0; j<64; j++) + for (k=0; k<32; k++) + { + r = i << 3; + g = j << 2; + b = k << 3; + Y = (r + g + b) >> 2; + u = 128 + ((r - b) >> 2); + v = 128 + ((-r + 2*g -b)>>3); + RGBtoYUV[ (i << 11) + (j << 5) + k ] = (Y<<16) + (u<<8) + v; + } + + int nMMXsupport = 0; + + __asm + { + mov eax, 1 + cpuid + and edx, 0x00800000 + mov nMMXsupport, edx + } + + return nMMXsupport; +} + +int hq3xinited=0; + +#include + +void hq3x32(unsigned char * pIn, unsigned int srcPitch, + unsigned char *, + unsigned char * pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + // NOTICE! This driver wants 16 bit, not 32 bit input! + + if (!hq3xinited) + { + InitLUTs(); + hq3xinited=1; + } + hq3x_32( pIn, pOut, Xres, Yres, dstPitch, srcPitch - (Xres *2) ); +} +#include + +void hq3x16(unsigned char * pIn, unsigned int srcPitch, + unsigned char *, + unsigned char * pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + if (!hq3xinited) + { + InitLUTs(); + hq3xinited=1; + } + hq3x_16( pIn, pOut, Xres, Yres, dstPitch, srcPitch - (Xres *2)); +} diff --git a/src/hq3x32.h b/src/hq3x32.h new file mode 100644 index 00000000..6d1533c0 --- /dev/null +++ b/src/hq3x32.h @@ -0,0 +1,3634 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + switch (pattern) + { + case 0: + case 1: + case 4: + case 32: + case 128: + case 5: + case 132: + case 160: + case 33: + case 129: + case 36: + case 133: + case 164: + case 161: + case 37: + case 165: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 2: + case 34: + case 130: + case 162: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 16: + case 17: + case 48: + case 49: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 64: + case 65: + case 68: + case 69: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 8: + case 12: + case 136: + case 140: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 3: + case 35: + case 131: + case 163: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 6: + case 38: + case 134: + case 166: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 20: + case 21: + case 52: + case 53: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 144: + case 145: + case 176: + case 177: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 192: + case 193: + case 196: + case 197: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 96: + case 97: + case 100: + case 101: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 40: + case 44: + case 168: + case 172: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 9: + case 13: + case 137: + case 141: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 18: + case 50: + { + PIXEL00_1M + + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_1M + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 80: + case 81: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 72: + case 76: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_1M + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 10: + case 138: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 66: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 24: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 7: + case 39: + case 135: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 148: + case 149: + case 180: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 224: + case 228: + case 225: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 41: + case 169: + case 45: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 22: + case 54: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 208: + case 209: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 104: + case 108: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 11: + case 139: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 19: + case 51: + { + if (Diff(c[2], c[6])) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 146: + case 178: + { + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_1M + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + break; + } + case 84: + case 85: + { + if (Diff(c[6], c[8])) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + break; + } + case 112: + case 113: + { + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 200: + case 204: + { + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + break; + } + case 73: + case 77: + { + if (Diff(c[8], c[4])) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_1M + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + break; + } + case 42: + case 170: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + break; + } + case 14: + case 142: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 67: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 70: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 28: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 152: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 194: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 98: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 56: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 25: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 26: + case 31: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 82: + case 214: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 88: + case 248: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 74: + case 107: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 27: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 86: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 216: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 106: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 30: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 210: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 120: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 75: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 29: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 198: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 184: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 99: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 57: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 71: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 156: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 226: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 60: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 195: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 102: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 153: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 58: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 83: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 92: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 202: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 78: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1M + break; + } + case 154: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 114: + { + PIXEL00_1M + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 89: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 90: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 55: + case 23: + { + if (Diff(c[2], c[6])) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 182: + case 150: + { + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + break; + } + case 213: + case 212: + { + if (Diff(c[6], c[8])) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + break; + } + case 241: + case 240: + { + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 236: + case 232: + { + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + break; + } + case 109: + case 105: + { + if (Diff(c[8], c[4])) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + break; + } + case 171: + case 43: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + break; + } + case 143: + case 15: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 124: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 203: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 62: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 211: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 118: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 217: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 110: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 155: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 188: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 185: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 61: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 157: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 103: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 227: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 230: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 199: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 220: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 158: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 234: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1R + break; + } + case 242: + { + PIXEL00_1M + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 59: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 121: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 87: + { + PIXEL00_1L + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 79: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1R + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1M + break; + } + case 122: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 94: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 218: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 91: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 229: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 167: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 173: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 181: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 186: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 115: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 93: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 206: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 205: + case 201: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 174: + case 46: + { + if (Diff(c[4], c[2])) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 179: + case 147: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 117: + case 116: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 189: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 231: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 126: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 219: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 125: + { + if (Diff(c[8], c[4])) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + PIXEL22_1M + break; + } + case 221: + { + if (Diff(c[6], c[8])) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_1U + PIXEL01_1 + PIXEL10_C + PIXEL11 + PIXEL20_1M + break; + } + case 207: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 238: + { + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + break; + } + case 190: + { + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + break; + } + case 187: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL21_1 + PIXEL22_1D + break; + } + case 243: + { + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 119: + { + if (Diff(c[2], c[6])) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 237: + case 233: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 175: + case 47: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 183: + case 151: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 245: + case 244: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 250: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 123: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 95: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 222: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 252: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 249: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 235: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 111: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 63: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 159: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 215: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 246: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 254: + { + PIXEL00_1M + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_2 + } + break; + } + case 253: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 251: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_2 + PIXEL21_3 + } + if (Diff(c[6], c[8])) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 239: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 127: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_2 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(c[2], c[6])) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + if (Diff(c[8], c[4])) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 191: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 223: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + if (Diff(c[2], c[6])) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_2 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + if (Diff(c[6], c[8])) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 247: + { + PIXEL00_1L + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 255: + { + if (Diff(c[4], c[2])) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(c[2], c[6])) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(c[8], c[4])) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(c[6], c[8])) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + } diff --git a/src/hq3x_16.asm b/src/hq3x_16.asm new file mode 100644 index 00000000..a42e5ac8 --- /dev/null +++ b/src/hq3x_16.asm @@ -0,0 +1,2527 @@ +;hq3x filter +;16bpp output +;---------------------------------------------------------- +;Copyright (C) 2003 MaxSt ( maxst@hiend3d.com ) +; +;This program is free software; you can redistribute it and/or +;modify it under the terms of the GNU General Public License +;as published by the Free Software Foundation; either +;version 2 of the License, or (at your option) any later +;version. +; +;This program is distributed in the hope that it will be useful, +;but WITHOUT ANY WARRANTY; without even the implied warranty of +;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;GNU General Public License for more details. +; +;You should have received a copy of the GNU General Public License +;along with this program; if not, write to the Free Software +;Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +GLOBAL _hq3x_16 + +EXTERN _LUT16to32 +EXTERN _RGBtoYUV + +SECTION .bss +linesleft resd 1 +xcounter resd 1 +cross resd 1 +nextline resd 1 +prevline resd 1 +w1 resd 1 +w2 resd 1 +w3 resd 1 +w4 resd 1 +w5 resd 1 +w6 resd 1 +w7 resd 1 +w8 resd 1 +w9 resd 1 + +SECTION .data + +reg_blank dd 0,0 +const7 dd 0x00070007,0x00000007 +threshold dd 0x00300706,0x00000000 +zerolowbits dd 0xF7DEF7DE + +SECTION .text + +%macro TestDiff 2 + xor ecx,ecx + mov edx,[%1] + cmp edx,[%2] + je %%fin + mov ecx,_RGBtoYUV + movd mm1,[ecx+edx*4] + movq mm5,mm1 + mov edx,[%2] + movd mm2,[ecx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd ecx,mm1 +%%fin: +%endmacro + +%macro DiffOrNot 4 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + jmp %%fin +%%same: + %4 +%%fin +%endmacro + +%macro DiffOrNot 6 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + jmp %%fin +%%same: + %5 + %6 +%%fin +%endmacro + +%macro DiffOrNot 8 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + %5 + jmp %%fin +%%same: + %6 + %7 + %8 +%%fin +%endmacro + +%macro DiffOrNot 10 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + %5 + %6 + jmp %%fin +%%same: + %7 + %8 + %9 + %10 +%%fin +%endmacro + +%macro Interp1 3 + mov edx,%2 + mov ecx,%3 + cmp edx,ecx + je %%fin + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 +%%fin + mov %1,dx +%endmacro + +%macro Interp2 4 + mov edx,%3 + mov ecx,%4 + cmp edx,ecx + je %%fin1 + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 +%%fin1 + mov edx,%2 + cmp edx,ecx + je %%fin2 + and ecx,[zerolowbits] + and edx,[zerolowbits] + add edx,ecx + shr edx,1 +%%fin2 + mov %1,dx +%endmacro + +%macro Interp3 2 + mov ecx, _LUT16to32 + movd mm1, [ecx+eax*4] + mov edx, %2 + movd mm2, [ecx+edx*4] + punpcklbw mm1, [reg_blank] + punpcklbw mm2, [reg_blank] + pmullw mm1, [const7] + paddw mm1, mm2 + psrlw mm1, 5 + packuswb mm1, [reg_blank] + movd edx, mm1 + shl dl, 2 + shr edx, 1 + shl dx, 3 + shr edx, 5 + mov %1, dx +%endmacro + +%macro Interp4 3 + mov ecx, _LUT16to32 + movd mm1, [ecx+eax*4] + mov edx, %2 + movd mm2, [ecx+edx*4] + mov edx, %3 + movd mm3, [ecx+edx*4] + punpcklbw mm1, [reg_blank] + punpcklbw mm2, [reg_blank] + punpcklbw mm3, [reg_blank] + psllw mm1, 1 + paddw mm2, mm3 + pmullw mm2, [const7] + paddw mm1, mm2 + psrlw mm1, 6 + packuswb mm1, [reg_blank] + movd edx, mm1 + shl dl, 2 + shr edx, 1 + shl dx, 3 + shr edx, 5 + mov %1, dx +%endmacro + +%macro Interp5 3 + mov edx,%2 + mov ecx,%3 + cmp edx,ecx + je %%fin + and edx,[zerolowbits] + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 +%%fin + mov %1,dx +%endmacro + +%macro PIXEL00_1M 0 + Interp1 [edi],eax,[w1] +%endmacro + +%macro PIXEL00_1U 0 + Interp1 [edi],eax,[w2] +%endmacro + +%macro PIXEL00_1L 0 + Interp1 [edi],eax,[w4] +%endmacro + +%macro PIXEL00_2 0 + Interp2 [edi],eax,[w4],[w2] +%endmacro + +%macro PIXEL00_4 0 + Interp4 [edi],[w4],[w2] +%endmacro + +%macro PIXEL00_5 0 + Interp5 [edi],[w4],[w2] +%endmacro + +%macro PIXEL00_C 0 + mov [edi],ax +%endmacro + +%macro PIXEL01_1 0 + Interp1 [edi+2],eax,[w2] +%endmacro + +%macro PIXEL01_3 0 + Interp3 [edi+2],[w2] +%endmacro + +%macro PIXEL01_6 0 + Interp1 [edi+2],[w2],eax +%endmacro + +%macro PIXEL01_C 0 + mov [edi+2],ax +%endmacro + +%macro PIXEL02_1M 0 + Interp1 [edi+4],eax,[w3] +%endmacro + +%macro PIXEL02_1U 0 + Interp1 [edi+4],eax,[w2] +%endmacro + +%macro PIXEL02_1R 0 + Interp1 [edi+4],eax,[w6] +%endmacro + +%macro PIXEL02_2 0 + Interp2 [edi+4],eax,[w2],[w6] +%endmacro + +%macro PIXEL02_4 0 + Interp4 [edi+4],[w2],[w6] +%endmacro + +%macro PIXEL02_5 0 + Interp5 [edi+4],[w2],[w6] +%endmacro + +%macro PIXEL02_C 0 + mov [edi+4],ax +%endmacro + +%macro PIXEL10_1 0 + Interp1 [edi+ebx],eax,[w4] +%endmacro + +%macro PIXEL10_3 0 + Interp3 [edi+ebx],[w4] +%endmacro + +%macro PIXEL10_6 0 + Interp1 [edi+ebx],[w4],eax +%endmacro + +%macro PIXEL10_C 0 + mov [edi+ebx],ax +%endmacro + +%macro PIXEL11 0 + mov [edi+ebx+2],ax +%endmacro + +%macro PIXEL12_1 0 + Interp1 [edi+ebx+4],eax,[w6] +%endmacro + +%macro PIXEL12_3 0 + Interp3 [edi+ebx+4],[w6] +%endmacro + +%macro PIXEL12_6 0 + Interp1 [edi+ebx+4],[w6],eax +%endmacro + +%macro PIXEL12_C 0 + mov [edi+ebx+4],ax +%endmacro + +%macro PIXEL20_1M 0 + Interp1 [edi+ebx*2],eax,[w7] +%endmacro + +%macro PIXEL20_1D 0 + Interp1 [edi+ebx*2],eax,[w8] +%endmacro + +%macro PIXEL20_1L 0 + Interp1 [edi+ebx*2],eax,[w4] +%endmacro + +%macro PIXEL20_2 0 + Interp2 [edi+ebx*2],eax,[w8],[w4] +%endmacro + +%macro PIXEL20_4 0 + Interp4 [edi+ebx*2],[w8],[w4] +%endmacro + +%macro PIXEL20_5 0 + Interp5 [edi+ebx*2],[w8],[w4] +%endmacro + +%macro PIXEL20_C 0 + mov [edi+ebx*2],ax +%endmacro + +%macro PIXEL21_1 0 + Interp1 [edi+ebx*2+2],eax,[w8] +%endmacro + +%macro PIXEL21_3 0 + Interp3 [edi+ebx*2+2],[w8] +%endmacro + +%macro PIXEL21_6 0 + Interp1 [edi+ebx*2+2],[w8],eax +%endmacro + +%macro PIXEL21_C 0 + mov [edi+ebx*2+2],ax +%endmacro + +%macro PIXEL22_1M 0 + Interp1 [edi+ebx*2+4],eax,[w9] +%endmacro + +%macro PIXEL22_1D 0 + Interp1 [edi+ebx*2+4],eax,[w8] +%endmacro + +%macro PIXEL22_1R 0 + Interp1 [edi+ebx*2+4],eax,[w6] +%endmacro + +%macro PIXEL22_2 0 + Interp2 [edi+ebx*2+4],eax,[w6],[w8] +%endmacro + +%macro PIXEL22_4 0 + Interp4 [edi+ebx*2+4],[w6],[w8] +%endmacro + +%macro PIXEL22_5 0 + Interp5 [edi+ebx*2+4],[w6],[w8] +%endmacro + +%macro PIXEL22_C 0 + mov [edi+ebx*2+4],ax +%endmacro + +inbuffer equ 8 +outbuffer equ 12 +Xres equ 16 +Yres equ 20 +pitch equ 24 +offset equ 28 + +_hq3x_16: + push ebp + mov ebp,esp + pushad + + mov esi,[ebp+inbuffer] + mov edi,[ebp+outbuffer] + mov edx,[ebp+Yres] + mov [linesleft],edx + mov ebx,[ebp+Xres] + shl ebx,1 + mov dword[prevline],0 + mov dword[nextline],ebx +.loopy + mov ecx,[ebp+Xres] + sub ecx,2 ; x={Xres-2, Xres-1} are special cases. + mov dword[xcounter],ecx + ; x=0 - special case + mov ebx,[prevline] + movq mm5,[esi+ebx] + movq mm6,[esi] + mov ebx,[nextline] + movq mm7,[esi+ebx] + movd eax,mm5 + movzx edx,ax + mov [w1],edx + mov [w2],edx + shr eax,16 + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + mov [w5],edx + shr eax,16 + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + mov [w8],edx + shr eax,16 + mov [w9],eax + jmp .flags +.loopx + mov ebx,[prevline] + movq mm5,[esi+ebx-2] + movq mm6,[esi-2] + mov ebx,[nextline] + movq mm7,[esi+ebx-2] + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + psrlq mm5,32 + movd eax,mm5 + movzx edx,ax + mov [w3],edx + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + psrlq mm6,32 + movd eax,mm6 + movzx edx,ax + mov [w6],edx + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + psrlq mm7,32 + movd eax,mm7 + movzx edx,ax + mov [w9],edx +.flags + mov ebx,_RGBtoYUV + mov eax,[w5] + xor ecx,ecx + movd mm5,[ebx+eax*4] + mov dword[cross],0 + + mov edx,[w2] + cmp eax,edx + je .noflag2 + or dword[cross],1 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag2 + or ecx,2 +.noflag2 + mov edx,[w4] + cmp eax,edx + je .noflag4 + or dword[cross],2 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag4 + or ecx,8 +.noflag4 + mov edx,[w6] + cmp eax,edx + je .noflag6 + or dword[cross],4 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag6 + or ecx,16 +.noflag6 + mov edx,[w8] + cmp eax,edx + je .noflag8 + or dword[cross],8 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag8 + or ecx,64 +.noflag8 + test ecx,ecx + jnz .testflag1 + mov ecx,[cross] + mov ebx,[ebp+pitch] + jmp [FuncTable2+ecx*4] +.testflag1 + mov edx,[w1] + cmp eax,edx + je .noflag1 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag1 + or ecx,1 +.noflag1 + mov edx,[w3] + cmp eax,edx + je .noflag3 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag3 + or ecx,4 +.noflag3 + mov edx,[w7] + cmp eax,edx + je .noflag7 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag7 + or ecx,32 +.noflag7 + mov edx,[w9] + cmp eax,edx + je .noflag9 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag9 + or ecx,128 +.noflag9 + mov ebx,[ebp+pitch] + jmp [FuncTable+ecx*4] + +..@flag0 +..@flag1 +..@flag4 +..@flag32 +..@flag128 +..@flag5 +..@flag132 +..@flag160 +..@flag33 +..@flag129 +..@flag36 +..@flag133 +..@flag164 +..@flag161 +..@flag37 +..@flag165 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag2 +..@flag34 +..@flag130 +..@flag162 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag16 +..@flag17 +..@flag48 +..@flag49 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag64 +..@flag65 +..@flag68 +..@flag69 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag8 +..@flag12 +..@flag136 +..@flag140 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag3 +..@flag35 +..@flag131 +..@flag163 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag6 +..@flag38 +..@flag134 +..@flag166 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag20 +..@flag21 +..@flag52 +..@flag53 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag144 +..@flag145 +..@flag176 +..@flag177 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag192 +..@flag193 +..@flag196 +..@flag197 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag96 +..@flag97 +..@flag100 +..@flag101 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag40 +..@flag44 +..@flag168 +..@flag172 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag9 +..@flag13 +..@flag137 +..@flag141 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag18 +..@flag50 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag80 +..@flag81 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_1M,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag72 +..@flag76 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag10 +..@flag138 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag66 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag24 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag7 +..@flag39 +..@flag135 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag148 +..@flag149 +..@flag180 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag224 +..@flag228 +..@flag225 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag41 +..@flag169 +..@flag45 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag22 +..@flag54 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag208 +..@flag209 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag104 +..@flag108 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag11 +..@flag139 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag19 +..@flag51 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag146 +..@flag178 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + jmp .loopx_end +..@flag84 +..@flag85 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_1M,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag112 +..@flag113 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_1M,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag200 +..@flag204 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag73 +..@flag77 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + jmp .loopx_end +..@flag42 +..@flag170 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag14 +..@flag142 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag67 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag70 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag28 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag152 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag194 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag98 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag56 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag25 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag26 +..@flag31 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag82 +..@flag214 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag88 +..@flag248 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag74 +..@flag107 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag27 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag86 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag216 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag106 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag30 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag210 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag120 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag75 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag29 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag198 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag184 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag99 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag57 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag71 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag156 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag226 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag60 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag195 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag102 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag153 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag58 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag83 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag92 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag202 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag78 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag154 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag114 + PIXEL00_1M + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag89 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag90 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag55 +..@flag23 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag182 +..@flag150 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + jmp .loopx_end +..@flag213 +..@flag212 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag241 +..@flag240 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_C,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag236 +..@flag232 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag109 +..@flag105 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + jmp .loopx_end +..@flag171 +..@flag43 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag143 +..@flag15 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag124 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag203 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag62 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag211 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag118 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag217 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag110 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag155 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag188 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag185 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag61 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag157 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag103 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag227 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag230 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag199 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag220 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag158 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag234 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1R + jmp .loopx_end +..@flag242 + PIXEL00_1M + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag59 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag121 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag87 + PIXEL00_1L + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag79 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1R + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag122 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag94 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag218 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag91 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag229 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag167 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag173 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag181 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag186 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag115 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag93 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag206 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag205 +..@flag201 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag174 +..@flag46 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag179 +..@flag147 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag117 +..@flag116 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag189 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag231 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag126 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag219 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag125 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + PIXEL22_1M + jmp .loopx_end +..@flag221 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_1U + PIXEL01_1 + PIXEL10_C + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag207 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag238 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag190 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + jmp .loopx_end +..@flag187 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag243 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_C,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag119 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag237 +..@flag233 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag175 +..@flag47 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag183 +..@flag151 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag245 +..@flag244 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag250 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag123 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag95 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag222 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag252 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag249 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag235 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag111 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag63 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag159 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag215 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag246 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag254 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_2 + jmp .loopx_end +..@flag253 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag251 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_2,PIXEL21_3 + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag239 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag127 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_2,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag191 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag223 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_2,PIXEL12_3 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag247 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag255 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end + +..@cross0 + mov edx,eax + shl eax,16 + or eax,edx + mov [edi],eax + mov [edi+4],ax + mov [edi+ebx],eax + mov [edi+ebx+4],ax + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],ax + jmp .loopx_end +..@cross1 + mov edx,eax + shl eax,16 + or eax,edx + mov ecx,[w2] + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 + mov [edi],dx + mov [edi+2],dx + mov [edi+4],dx + mov [edi+ebx],eax + mov [edi+ebx+4],ax + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],ax + jmp .loopx_end +..@cross2 + mov edx,eax + shl eax,16 + or eax,edx + mov ecx,[w4] + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 + mov [edi],dx + mov [edi+2],eax + mov [edi+ebx],dx + mov [edi+ebx+2],eax + mov [edi+ebx*2],dx + mov [edi+ebx*2+2],eax + jmp .loopx_end +..@cross4 + mov edx,eax + shl eax,16 + or eax,edx + mov ecx,[w6] + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 + mov [edi],eax + mov [edi+4],dx + mov [edi+ebx],eax + mov [edi+ebx+4],dx + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],dx + jmp .loopx_end +..@cross8 + mov edx,eax + shl eax,16 + or eax,edx + mov ecx,[w8] + and edx,[zerolowbits] + and ecx,[zerolowbits] + add ecx,edx + shr ecx,1 + add ecx,0x0821 + and ecx,[zerolowbits] + add edx,ecx + shr edx,1 + mov [edi],eax + mov [edi+4],ax + mov [edi+ebx],eax + mov [edi+ebx+4],ax + mov [edi+ebx*2],dx + mov [edi+ebx*2+2],dx + mov [edi+ebx*2+4],dx + jmp .loopx_end + +.loopx_end + add esi,2 + add edi,6 + dec dword[xcounter] + jle .xres_2 + jmp .loopx +.xres_2 + ; x=Xres-2 - special case + jl .xres_1 + mov ebx,[prevline] + movq mm5,[esi+ebx-4] + movq mm6,[esi-4] + mov ebx,[nextline] + movq mm7,[esi+ebx-4] + psrlq mm5,16 + psrlq mm6,16 + psrlq mm7,16 + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + psrlq mm5,32 + movd eax,mm5 + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + psrlq mm6,32 + movd eax,mm6 + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + psrlq mm7,32 + movd eax,mm7 + mov [w9],eax + jmp .flags +.xres_1 + cmp dword[xcounter],-1 + jl .nexty + ; x=Xres-1 - special case + mov ebx,[prevline] + movq mm5,[esi+ebx-6] + movq mm6,[esi-6] + mov ebx,[nextline] + movq mm7,[esi+ebx-6] + psrlq mm5,32 + psrlq mm6,32 + psrlq mm7,32 + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + mov [w9],eax + jmp .flags +.nexty + add esi,[ebp+offset] ; added - move source pointer past end-of-line blanks + add edi,ebx + add edi,ebx + add edi,ebx + mov ebx, [ebp+Xres] ; added, bug - need to add to destination offset + shl ebx, 1 + sub edi, ebx + sub edi, ebx + sub edi, ebx + dec dword[linesleft] + jz .fin + add ebx, [ebp+offset]; + cmp dword[linesleft],1 + je .lastline + mov dword[nextline],ebx + neg ebx + mov dword[prevline],ebx + jmp .loopy +.lastline + mov dword[nextline],0 + neg ebx + mov dword[prevline],ebx + jmp .loopy +.fin + emms + popad + mov esp,ebp + pop ebp + ret + +SECTION .data +FuncTable + dd ..@flag0, ..@flag1, ..@flag2, ..@flag3, ..@flag4, ..@flag5, ..@flag6, ..@flag7 + dd ..@flag8, ..@flag9, ..@flag10, ..@flag11, ..@flag12, ..@flag13, ..@flag14, ..@flag15 + dd ..@flag16, ..@flag17, ..@flag18, ..@flag19, ..@flag20, ..@flag21, ..@flag22, ..@flag23 + dd ..@flag24, ..@flag25, ..@flag26, ..@flag27, ..@flag28, ..@flag29, ..@flag30, ..@flag31 + dd ..@flag32, ..@flag33, ..@flag34, ..@flag35, ..@flag36, ..@flag37, ..@flag38, ..@flag39 + dd ..@flag40, ..@flag41, ..@flag42, ..@flag43, ..@flag44, ..@flag45, ..@flag46, ..@flag47 + dd ..@flag48, ..@flag49, ..@flag50, ..@flag51, ..@flag52, ..@flag53, ..@flag54, ..@flag55 + dd ..@flag56, ..@flag57, ..@flag58, ..@flag59, ..@flag60, ..@flag61, ..@flag62, ..@flag63 + dd ..@flag64, ..@flag65, ..@flag66, ..@flag67, ..@flag68, ..@flag69, ..@flag70, ..@flag71 + dd ..@flag72, ..@flag73, ..@flag74, ..@flag75, ..@flag76, ..@flag77, ..@flag78, ..@flag79 + dd ..@flag80, ..@flag81, ..@flag82, ..@flag83, ..@flag84, ..@flag85, ..@flag86, ..@flag87 + dd ..@flag88, ..@flag89, ..@flag90, ..@flag91, ..@flag92, ..@flag93, ..@flag94, ..@flag95 + dd ..@flag96, ..@flag97, ..@flag98, ..@flag99, ..@flag100, ..@flag101, ..@flag102, ..@flag103 + dd ..@flag104, ..@flag105, ..@flag106, ..@flag107, ..@flag108, ..@flag109, ..@flag110, ..@flag111 + dd ..@flag112, ..@flag113, ..@flag114, ..@flag115, ..@flag116, ..@flag117, ..@flag118, ..@flag119 + dd ..@flag120, ..@flag121, ..@flag122, ..@flag123, ..@flag124, ..@flag125, ..@flag126, ..@flag127 + dd ..@flag128, ..@flag129, ..@flag130, ..@flag131, ..@flag132, ..@flag133, ..@flag134, ..@flag135 + dd ..@flag136, ..@flag137, ..@flag138, ..@flag139, ..@flag140, ..@flag141, ..@flag142, ..@flag143 + dd ..@flag144, ..@flag145, ..@flag146, ..@flag147, ..@flag148, ..@flag149, ..@flag150, ..@flag151 + dd ..@flag152, ..@flag153, ..@flag154, ..@flag155, ..@flag156, ..@flag157, ..@flag158, ..@flag159 + dd ..@flag160, ..@flag161, ..@flag162, ..@flag163, ..@flag164, ..@flag165, ..@flag166, ..@flag167 + dd ..@flag168, ..@flag169, ..@flag170, ..@flag171, ..@flag172, ..@flag173, ..@flag174, ..@flag175 + dd ..@flag176, ..@flag177, ..@flag178, ..@flag179, ..@flag180, ..@flag181, ..@flag182, ..@flag183 + dd ..@flag184, ..@flag185, ..@flag186, ..@flag187, ..@flag188, ..@flag189, ..@flag190, ..@flag191 + dd ..@flag192, ..@flag193, ..@flag194, ..@flag195, ..@flag196, ..@flag197, ..@flag198, ..@flag199 + dd ..@flag200, ..@flag201, ..@flag202, ..@flag203, ..@flag204, ..@flag205, ..@flag206, ..@flag207 + dd ..@flag208, ..@flag209, ..@flag210, ..@flag211, ..@flag212, ..@flag213, ..@flag214, ..@flag215 + dd ..@flag216, ..@flag217, ..@flag218, ..@flag219, ..@flag220, ..@flag221, ..@flag222, ..@flag223 + dd ..@flag224, ..@flag225, ..@flag226, ..@flag227, ..@flag228, ..@flag229, ..@flag230, ..@flag231 + dd ..@flag232, ..@flag233, ..@flag234, ..@flag235, ..@flag236, ..@flag237, ..@flag238, ..@flag239 + dd ..@flag240, ..@flag241, ..@flag242, ..@flag243, ..@flag244, ..@flag245, ..@flag246, ..@flag247 + dd ..@flag248, ..@flag249, ..@flag250, ..@flag251, ..@flag252, ..@flag253, ..@flag254, ..@flag255 + +FuncTable2 + dd ..@cross0, ..@cross1, ..@cross2, ..@flag0, + dd ..@cross4, ..@flag0, ..@flag0, ..@flag0, + dd ..@cross8, ..@flag0, ..@flag0, ..@flag0, + dd ..@flag0, ..@flag0, ..@flag0, ..@flag0 + diff --git a/src/hq3x_32.asm b/src/hq3x_32.asm new file mode 100644 index 00000000..38d0145f --- /dev/null +++ b/src/hq3x_32.asm @@ -0,0 +1,2571 @@ +;hq3x filter +;32bpp output +;---------------------------------------------------------- +;Copyright (C) 2003 MaxSt ( maxst@hiend3d.com ) +; +;This program is free software; you can redistribute it and/or +;modify it under the terms of the GNU General Public License +;as published by the Free Software Foundation; either +;version 2 of the License, or (at your option) any later +;version. +; +;This program is distributed in the hope that it will be useful, +;but WITHOUT ANY WARRANTY; without even the implied warranty of +;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;GNU General Public License for more details. +; +;You should have received a copy of the GNU General Public License +;along with this program; if not, write to the Free Software +;Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +GLOBAL _hq3x_32 + +EXTERN _LUT16to32 +EXTERN _RGBtoYUV + +SECTION .bss +linesleft resd 1 +xcounter resd 1 +cross resd 1 +nextline resd 1 +prevline resd 1 +w1 resd 1 +w2 resd 1 +w3 resd 1 +w4 resd 1 +w5 resd 1 +w6 resd 1 +w7 resd 1 +w8 resd 1 +w9 resd 1 +c1 resd 1 +c2 resd 1 +c3 resd 1 +c4 resd 1 +c5 resd 1 +c6 resd 1 +c7 resd 1 +c8 resd 1 +c9 resd 1 + +SECTION .data + +reg_blank dd 0,0 +const7 dd 0x00070007,0x00000007 +threshold dd 0x00300706,0x00000000 + +SECTION .text + +%macro TestDiff 2 + xor ecx,ecx + mov edx,[%1] + cmp edx,[%2] + je %%fin + mov ecx,_RGBtoYUV + movd mm1,[ecx+edx*4] + movq mm5,mm1 + mov edx,[%2] + movd mm2,[ecx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd ecx,mm1 +%%fin: +%endmacro + +%macro DiffOrNot 4 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + jmp %%fin +%%same: + %4 +%%fin +%endmacro + +%macro DiffOrNot 6 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + jmp %%fin +%%same: + %5 + %6 +%%fin +%endmacro + +%macro DiffOrNot 8 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + %5 + jmp %%fin +%%same: + %6 + %7 + %8 +%%fin +%endmacro + +%macro DiffOrNot 10 + TestDiff %1,%2 + test ecx,ecx + jz %%same + %3 + %4 + %5 + %6 + jmp %%fin +%%same: + %7 + %8 + %9 + %10 +%%fin +%endmacro + +%macro Interp1 3 + mov edx,%2 + shl edx,2 + add edx,%3 + sub edx,%2 + shr edx,2 + mov %1,edx +%endmacro + +%macro Interp2 4 + mov edx,%2 + shl edx,1 + add edx,%3 + add edx,%4 + shr edx,2 + mov %1,edx +%endmacro + +%macro Interp3 2 + movd mm1, eax + movd mm2, %2 + punpcklbw mm1, [reg_blank] + punpcklbw mm2, [reg_blank] + pmullw mm1, [const7] + paddw mm1, mm2 + psrlw mm1, 3 + packuswb mm1, [reg_blank] + movd %1, mm1 +%endmacro + +%macro Interp4 3 + movd mm1, eax + movd mm2, %2 + movd mm3, %3 + punpcklbw mm1, [reg_blank] + punpcklbw mm2, [reg_blank] + punpcklbw mm3, [reg_blank] + psllw mm1, 1 + paddw mm2, mm3 + pmullw mm2, [const7] + paddw mm1, mm2 + psrlw mm1, 4 + packuswb mm1, [reg_blank] + movd %1, mm1 +%endmacro + +%macro Interp5 3 + mov edx,%2 + add edx,%3 + shr edx,1 + mov %1,edx +%endmacro + +%macro PIXEL00_1M 0 + Interp1 [edi],eax,[c1] +%endmacro + +%macro PIXEL00_1U 0 + Interp1 [edi],eax,[c2] +%endmacro + +%macro PIXEL00_1L 0 + Interp1 [edi],eax,[c4] +%endmacro + +%macro PIXEL00_2 0 + Interp2 [edi],eax,[c4],[c2] +%endmacro + +%macro PIXEL00_4 0 + Interp4 [edi],[c4],[c2] +%endmacro + +%macro PIXEL00_5 0 + Interp5 [edi],[c4],[c2] +%endmacro + +%macro PIXEL00_C 0 + mov [edi],eax +%endmacro + +%macro PIXEL01_1 0 + Interp1 [edi+4],eax,[c2] +%endmacro + +%macro PIXEL01_3 0 + Interp3 [edi+4],[c2] +%endmacro + +%macro PIXEL01_6 0 + Interp1 [edi+4],[c2],eax +%endmacro + +%macro PIXEL01_C 0 + mov [edi+4],eax +%endmacro + +%macro PIXEL02_1M 0 + Interp1 [edi+8],eax,[c3] +%endmacro + +%macro PIXEL02_1U 0 + Interp1 [edi+8],eax,[c2] +%endmacro + +%macro PIXEL02_1R 0 + Interp1 [edi+8],eax,[c6] +%endmacro + +%macro PIXEL02_2 0 + Interp2 [edi+8],eax,[c2],[c6] +%endmacro + +%macro PIXEL02_4 0 + Interp4 [edi+8],[c2],[c6] +%endmacro + +%macro PIXEL02_5 0 + Interp5 [edi+8],[c2],[c6] +%endmacro + +%macro PIXEL02_C 0 + mov [edi+8],eax +%endmacro + +%macro PIXEL10_1 0 + Interp1 [edi+ebx],eax,[c4] +%endmacro + +%macro PIXEL10_3 0 + Interp3 [edi+ebx],[c4] +%endmacro + +%macro PIXEL10_6 0 + Interp1 [edi+ebx],[c4],eax +%endmacro + +%macro PIXEL10_C 0 + mov [edi+ebx],eax +%endmacro + +%macro PIXEL11 0 + mov [edi+ebx+4],eax +%endmacro + +%macro PIXEL12_1 0 + Interp1 [edi+ebx+8],eax,[c6] +%endmacro + +%macro PIXEL12_3 0 + Interp3 [edi+ebx+8],[c6] +%endmacro + +%macro PIXEL12_6 0 + Interp1 [edi+ebx+8],[c6],eax +%endmacro + +%macro PIXEL12_C 0 + mov [edi+ebx+8],eax +%endmacro + +%macro PIXEL20_1M 0 + Interp1 [edi+ebx*2],eax,[c7] +%endmacro + +%macro PIXEL20_1D 0 + Interp1 [edi+ebx*2],eax,[c8] +%endmacro + +%macro PIXEL20_1L 0 + Interp1 [edi+ebx*2],eax,[c4] +%endmacro + +%macro PIXEL20_2 0 + Interp2 [edi+ebx*2],eax,[c8],[c4] +%endmacro + +%macro PIXEL20_4 0 + Interp4 [edi+ebx*2],[c8],[c4] +%endmacro + +%macro PIXEL20_5 0 + Interp5 [edi+ebx*2],[c8],[c4] +%endmacro + +%macro PIXEL20_C 0 + mov [edi+ebx*2],eax +%endmacro + +%macro PIXEL21_1 0 + Interp1 [edi+ebx*2+4],eax,[c8] +%endmacro + +%macro PIXEL21_3 0 + Interp3 [edi+ebx*2+4],[c8] +%endmacro + +%macro PIXEL21_6 0 + Interp1 [edi+ebx*2+4],[c8],eax +%endmacro + +%macro PIXEL21_C 0 + mov [edi+ebx*2+4],eax +%endmacro + +%macro PIXEL22_1M 0 + Interp1 [edi+ebx*2+8],eax,[c9] +%endmacro + +%macro PIXEL22_1D 0 + Interp1 [edi+ebx*2+8],eax,[c8] +%endmacro + +%macro PIXEL22_1R 0 + Interp1 [edi+ebx*2+8],eax,[c6] +%endmacro + +%macro PIXEL22_2 0 + Interp2 [edi+ebx*2+8],eax,[c6],[c8] +%endmacro + +%macro PIXEL22_4 0 + Interp4 [edi+ebx*2+8],[c6],[c8] +%endmacro + +%macro PIXEL22_5 0 + Interp5 [edi+ebx*2+8],[c6],[c8] +%endmacro + +%macro PIXEL22_C 0 + mov [edi+ebx*2+8],eax +%endmacro + +inbuffer equ 8 +outbuffer equ 12 +Xres equ 16 +Yres equ 20 +pitch equ 24 +offset equ 28 + +_hq3x_32: + push ebp + mov ebp,esp + pushad + + mov esi,[ebp+inbuffer] + mov edi,[ebp+outbuffer] + mov edx,[ebp+Yres] + mov [linesleft],edx + mov ebx,[ebp+Xres] + shl ebx,1 + mov dword[prevline],0 + mov dword[nextline],ebx +.loopy + mov ecx,[ebp+Xres] + sub ecx,2 ; x={Xres-2, Xres-1} are special cases. + mov dword[xcounter],ecx + ; x=0 - special case + mov ebx,[prevline] + movq mm5,[esi+ebx] + movq mm6,[esi] + mov ebx,[nextline] + movq mm7,[esi+ebx] + movd eax,mm5 + movzx edx,ax + mov [w1],edx + mov [w2],edx + shr eax,16 + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + mov [w5],edx + shr eax,16 + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + mov [w8],edx + shr eax,16 + mov [w9],eax + jmp .flags +.loopx + mov ebx,[prevline] + movq mm5,[esi+ebx-2] + movq mm6,[esi-2] + mov ebx,[nextline] + movq mm7,[esi+ebx-2] + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + psrlq mm5,32 + movd eax,mm5 + movzx edx,ax + mov [w3],edx + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + psrlq mm6,32 + movd eax,mm6 + movzx edx,ax + mov [w6],edx + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + psrlq mm7,32 + movd eax,mm7 + movzx edx,ax + mov [w9],edx +.flags + mov ebx,_RGBtoYUV + mov eax,[w5] + xor ecx,ecx + movd mm5,[ebx+eax*4] + mov dword[cross],0 + + mov edx,[w2] + cmp eax,edx + je .noflag2 + or dword[cross],1 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag2 + or ecx,2 +.noflag2 + mov edx,[w4] + cmp eax,edx + je .noflag4 + or dword[cross],2 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag4 + or ecx,8 +.noflag4 + mov edx,[w6] + cmp eax,edx + je .noflag6 + or dword[cross],4 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag6 + or ecx,16 +.noflag6 + mov edx,[w8] + cmp eax,edx + je .noflag8 + or dword[cross],8 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag8 + or ecx,64 +.noflag8 + test ecx,ecx + jnz .testflag1 + mov ecx,[cross] + mov ebx,_LUT16to32 + mov eax,[ebx+eax*4] + jmp [FuncTable2+ecx*4] +.testflag1 + mov edx,[w1] + cmp eax,edx + je .noflag1 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag1 + or ecx,1 +.noflag1 + mov edx,[w3] + cmp eax,edx + je .noflag3 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag3 + or ecx,4 +.noflag3 + mov edx,[w7] + cmp eax,edx + je .noflag7 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag7 + or ecx,32 +.noflag7 + mov edx,[w9] + cmp eax,edx + je .noflag9 + movq mm1,mm5 + movd mm2,[ebx+edx*4] + psubusb mm1,mm2 + psubusb mm2,mm5 + por mm1,mm2 + psubusb mm1,[threshold] + movd edx,mm1 + test edx,edx + jz .noflag9 + or ecx,128 +.noflag9 + mov ebx,_LUT16to32 + mov eax,[ebx+eax*4] + mov edx,[w2] + mov edx,[ebx+edx*4] + mov [c2],edx + mov edx,[w4] + mov edx,[ebx+edx*4] + mov [c4],edx + mov edx,[w6] + mov edx,[ebx+edx*4] + mov [c6],edx + mov edx,[w8] + mov edx,[ebx+edx*4] + mov [c8],edx + test ecx,0x005A + jz .switch + mov edx,[w1] + mov edx,[ebx+edx*4] + mov [c1],edx + mov edx,[w3] + mov edx,[ebx+edx*4] + mov [c3],edx + mov edx,[w7] + mov edx,[ebx+edx*4] + mov [c7],edx + mov edx,[w9] + mov edx,[ebx+edx*4] + mov [c9],edx +.switch + mov ebx,[ebp+pitch] + jmp [FuncTable+ecx*4] + +..@flag0 +..@flag1 +..@flag4 +..@flag32 +..@flag128 +..@flag5 +..@flag132 +..@flag160 +..@flag33 +..@flag129 +..@flag36 +..@flag133 +..@flag164 +..@flag161 +..@flag37 +..@flag165 +; PIXEL00_2 +; PIXEL01_1 +; PIXEL02_2 +; PIXEL10_1 +; PIXEL11 +; PIXEL12_1 +; PIXEL20_2 +; PIXEL21_1 +; PIXEL22_2 + +; the same, only optimized + mov ecx,eax + shl ecx,1 + add ecx,[c2] + mov edx,ecx + add edx,[c4] + shr edx,2 + mov [edi],edx + mov edx,ecx + add edx,eax + shr edx,2 + mov [edi+4],edx + add ecx,[c6] + shr ecx,2 + mov [edi+8],ecx + mov ecx,eax + shl ecx,2 + sub ecx,eax + mov edx,ecx + add edx,[c4] + shr edx,2 + mov [edi+ebx],edx + mov [edi+ebx+4],eax + add ecx,[c6] + shr ecx,2 + mov [edi+ebx+8],ecx + mov ecx,eax + shl ecx,1 + add ecx,[c8] + mov edx,ecx + add edx,[c4] + shr edx,2 + mov [edi+ebx*2],edx + mov edx,ecx + add edx,eax + shr edx,2 + mov [edi+ebx*2+4],edx + add ecx,[c6] + shr ecx,2 + mov [edi+ebx*2+8],ecx + jmp .loopx_end +..@flag2 +..@flag34 +..@flag130 +..@flag162 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag16 +..@flag17 +..@flag48 +..@flag49 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag64 +..@flag65 +..@flag68 +..@flag69 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag8 +..@flag12 +..@flag136 +..@flag140 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag3 +..@flag35 +..@flag131 +..@flag163 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag6 +..@flag38 +..@flag134 +..@flag166 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag20 +..@flag21 +..@flag52 +..@flag53 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag144 +..@flag145 +..@flag176 +..@flag177 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag192 +..@flag193 +..@flag196 +..@flag197 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag96 +..@flag97 +..@flag100 +..@flag101 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag40 +..@flag44 +..@flag168 +..@flag172 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag9 +..@flag13 +..@flag137 +..@flag141 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag18 +..@flag50 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag80 +..@flag81 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_1M,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag72 +..@flag76 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag10 +..@flag138 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag66 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag24 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag7 +..@flag39 +..@flag135 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag148 +..@flag149 +..@flag180 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag224 +..@flag228 +..@flag225 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag41 +..@flag169 +..@flag45 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag22 +..@flag54 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag208 +..@flag209 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag104 +..@flag108 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag11 +..@flag139 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag19 +..@flag51 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag146 +..@flag178 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_1M,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + jmp .loopx_end +..@flag84 +..@flag85 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_1M,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag112 +..@flag113 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_1M,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag200 +..@flag204 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag73 +..@flag77 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_1M,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + jmp .loopx_end +..@flag42 +..@flag170 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag14 +..@flag142 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag67 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag70 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag28 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag152 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag194 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag98 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag56 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag25 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag26 +..@flag31 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag82 +..@flag214 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag88 +..@flag248 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag74 +..@flag107 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag27 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag86 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag216 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag106 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag30 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag210 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag120 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag75 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag29 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag198 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag184 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag99 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag57 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag71 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag156 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag226 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag60 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag195 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag102 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag153 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag58 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag83 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag92 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag202 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag78 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag154 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag114 + PIXEL00_1M + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag89 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag90 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag55 +..@flag23 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag182 +..@flag150 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + jmp .loopx_end +..@flag213 +..@flag212 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag241 +..@flag240 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_C,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag236 +..@flag232 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag109 +..@flag105 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + jmp .loopx_end +..@flag171 +..@flag43 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag143 +..@flag15 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag124 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag203 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag62 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag211 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag118 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag217 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag110 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag155 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag188 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag185 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag61 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag157 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag103 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag227 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag230 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag199 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag220 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag158 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag234 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1R + jmp .loopx_end +..@flag242 + PIXEL00_1M + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag59 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag121 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag87 + PIXEL00_1L + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag79 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1R + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag122 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag94 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag218 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag91 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag229 + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag167 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag173 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag181 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag186 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag115 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag93 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag206 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag205 +..@flag201 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_1M,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag174 +..@flag46 + DiffOrNot w4,w2,PIXEL00_1M,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag179 +..@flag147 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_1M,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag117 +..@flag116 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_1M,PIXEL22_2 + jmp .loopx_end +..@flag189 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag231 + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag126 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_4,PIXEL12_3 + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag219 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_4,PIXEL01_3,PIXEL10_3 + PIXEL02_1M + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag125 + DiffOrNot w8,w4,PIXEL00_1U,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL00_2,PIXEL10_6,PIXEL20_5,PIXEL21_1 + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + PIXEL22_1M + jmp .loopx_end +..@flag221 + DiffOrNot w6,w8,PIXEL02_1U,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL02_2,PIXEL12_6,PIXEL21_1,PIXEL22_5 + PIXEL00_1U + PIXEL01_1 + PIXEL10_C + PIXEL11 + PIXEL20_1M + jmp .loopx_end +..@flag207 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL02_1R,PIXEL10_C,PIXEL00_5,PIXEL01_6,PIXEL02_2,PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag238 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL22_1R,PIXEL10_1,PIXEL20_5,PIXEL21_6,PIXEL22_2 + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + jmp .loopx_end +..@flag190 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL22_1D,PIXEL01_1,PIXEL02_5,PIXEL12_6,PIXEL22_2 + PIXEL00_1M + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + jmp .loopx_end +..@flag187 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL20_1D,PIXEL00_5,PIXEL01_1,PIXEL10_6,PIXEL20_2 + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag243 + DiffOrNot w6,w8,PIXEL12_C,PIXEL20_1L,PIXEL21_C,PIXEL22_C,PIXEL12_1,PIXEL20_2,PIXEL21_6,PIXEL22_5 + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + jmp .loopx_end +..@flag119 + DiffOrNot w2,w6,PIXEL00_1L,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL00_2,PIXEL01_6,PIXEL02_5,PIXEL12_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag237 +..@flag233 + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag175 +..@flag47 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + jmp .loopx_end +..@flag183 +..@flag151 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag245 +..@flag244 + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag250 + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag123 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag95 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + jmp .loopx_end +..@flag222 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag252 + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag249 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag235 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag111 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag63 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + jmp .loopx_end +..@flag159 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag215 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag246 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag254 + PIXEL00_1M + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL01_3,PIXEL02_4 + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL10_3,PIXEL20_4 + DiffOrNot w6,w8,PIXEL12_C,PIXEL21_C,PIXEL22_C,PIXEL12_3,PIXEL21_3,PIXEL22_2 + jmp .loopx_end +..@flag253 + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag251 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL00_4,PIXEL01_3 + PIXEL02_1M + PIXEL11 + DiffOrNot w8,w4,PIXEL10_C,PIXEL20_C,PIXEL21_C,PIXEL10_3,PIXEL20_2,PIXEL21_3 + DiffOrNot w6,w8,PIXEL12_C,PIXEL22_C,PIXEL12_3,PIXEL22_4 + jmp .loopx_end +..@flag239 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + PIXEL22_1R + jmp .loopx_end +..@flag127 + DiffOrNot w4,w2,PIXEL00_C,PIXEL01_C,PIXEL10_C,PIXEL00_2,PIXEL01_3,PIXEL10_3 + DiffOrNot w2,w6,PIXEL02_C,PIXEL12_C,PIXEL02_4,PIXEL12_3 + PIXEL11 + DiffOrNot w8,w4,PIXEL20_C,PIXEL21_C,PIXEL20_4,PIXEL21_3 + PIXEL22_1M + jmp .loopx_end +..@flag191 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + jmp .loopx_end +..@flag223 + DiffOrNot w4,w2,PIXEL00_C,PIXEL10_C,PIXEL00_4,PIXEL10_3 + DiffOrNot w2,w6,PIXEL01_C,PIXEL02_C,PIXEL12_C,PIXEL01_3,PIXEL02_2,PIXEL12_3 + PIXEL11 + PIXEL20_1M + DiffOrNot w6,w8,PIXEL21_C,PIXEL22_C,PIXEL21_3,PIXEL22_4 + jmp .loopx_end +..@flag247 + PIXEL00_1L + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end +..@flag255 + DiffOrNot w4,w2,PIXEL00_C,PIXEL00_2 + PIXEL01_C + DiffOrNot w2,w6,PIXEL02_C,PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_C + DiffOrNot w8,w4,PIXEL20_C,PIXEL20_2 + PIXEL21_C + DiffOrNot w6,w8,PIXEL22_C,PIXEL22_2 + jmp .loopx_end + +..@cross0 + mov ebx,[ebp+pitch] + mov [edi],eax + mov [edi+4],eax + mov [edi+8],eax + mov [edi+ebx],eax + mov [edi+ebx+4],eax + mov [edi+ebx+8],eax + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],eax + mov [edi+ebx*2+8],eax + jmp .loopx_end +..@cross1 + mov ecx,[w2] + mov edx,eax + shl edx,2 + add edx,[ebx+ecx*4] + sub edx,eax + shr edx,2 + mov ebx,[ebp+pitch] + mov [edi],edx + mov [edi+4],edx + mov [edi+8],edx + mov [edi+ebx],eax + mov [edi+ebx+4],eax + mov [edi+ebx+8],eax + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],eax + mov [edi+ebx*2+8],eax + jmp .loopx_end +..@cross2 + mov ecx,[w4] + mov edx,eax + shl edx,2 + add edx,[ebx+ecx*4] + sub edx,eax + shr edx,2 + mov ebx,[ebp+pitch] + mov [edi],edx + mov [edi+4],eax + mov [edi+8],eax + mov [edi+ebx],edx + mov [edi+ebx+4],eax + mov [edi+ebx+8],eax + mov [edi+ebx*2],edx + mov [edi+ebx*2+4],eax + mov [edi+ebx*2+8],eax + jmp .loopx_end +..@cross4 + mov ecx,[w6] + mov edx,eax + shl edx,2 + add edx,[ebx+ecx*4] + sub edx,eax + shr edx,2 + mov ebx,[ebp+pitch] + mov [edi],eax + mov [edi+4],eax + mov [edi+8],edx + mov [edi+ebx],eax + mov [edi+ebx+4],eax + mov [edi+ebx+8],edx + mov [edi+ebx*2],eax + mov [edi+ebx*2+4],eax + mov [edi+ebx*2+8],edx + jmp .loopx_end +..@cross8 + mov ecx,[w8] + mov edx,eax + shl edx,2 + add edx,[ebx+ecx*4] + sub edx,eax + shr edx,2 + mov ebx,[ebp+pitch] + mov [edi],eax + mov [edi+4],eax + mov [edi+8],eax + mov [edi+ebx],eax + mov [edi+ebx+4],eax + mov [edi+ebx+8],eax + mov [edi+ebx*2],edx + mov [edi+ebx*2+4],edx + mov [edi+ebx*2+8],edx + jmp .loopx_end +..@crossN + mov edx,[w2] + mov ecx,[ebx+edx*4] + mov [c2],ecx + mov edx,[w4] + mov ecx,[ebx+edx*4] + mov [c4],ecx + mov edx,[w6] + mov ecx,[ebx+edx*4] + mov [c6],ecx + mov edx,[w8] + mov ecx,[ebx+edx*4] + mov [c8],ecx + mov ebx,[ebp+pitch] + jmp ..@flag0 + +.loopx_end + add esi,2 + add edi,12 + dec dword[xcounter] + jle .xres_2 + jmp .loopx +.xres_2 + ; x=Xres-2 - special case + jl .xres_1 + mov ebx,[prevline] + movq mm5,[esi+ebx-4] + movq mm6,[esi-4] + mov ebx,[nextline] + movq mm7,[esi+ebx-4] + psrlq mm5,16 + psrlq mm6,16 + psrlq mm7,16 + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + psrlq mm5,32 + movd eax,mm5 + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + psrlq mm6,32 + movd eax,mm6 + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + psrlq mm7,32 + movd eax,mm7 + mov [w9],eax + jmp .flags +.xres_1 + cmp dword[xcounter],-1 + jl .nexty + ; x=Xres-1 - special case + mov ebx,[prevline] + movq mm5,[esi+ebx-6] + movq mm6,[esi-6] + mov ebx,[nextline] + movq mm7,[esi+ebx-6] + psrlq mm5,32 + psrlq mm6,32 + psrlq mm7,32 + movd eax,mm5 + movzx edx,ax + mov [w1],edx + shr eax,16 + mov [w2],eax + mov [w3],eax + movd eax,mm6 + movzx edx,ax + mov [w4],edx + shr eax,16 + mov [w5],eax + mov [w6],eax + movd eax,mm7 + movzx edx,ax + mov [w7],edx + shr eax,16 + mov [w8],eax + mov [w9],eax + jmp .flags +.nexty + add esi,[ebp+offset] ; added - move source pointer past end-of-line blanks + add edi,ebx + add edi,ebx + add edi,ebx + mov ebx, [ebp+Xres] ; added, bug - need to add to destination offset + shl ebx, 2 + sub edi, ebx + sub edi, ebx + sub edi, ebx + shr ebx, 1 + dec dword[linesleft] + jz .fin + add ebx, [ebp+offset]; + cmp dword[linesleft],1 + je .lastline + mov dword[nextline],ebx + neg ebx + mov dword[prevline],ebx + jmp .loopy +.lastline + mov dword[nextline],0 + neg ebx + mov dword[prevline],ebx + jmp .loopy +.fin + emms + popad + mov esp,ebp + pop ebp + ret + +SECTION .data +FuncTable + dd ..@flag0, ..@flag1, ..@flag2, ..@flag3, ..@flag4, ..@flag5, ..@flag6, ..@flag7 + dd ..@flag8, ..@flag9, ..@flag10, ..@flag11, ..@flag12, ..@flag13, ..@flag14, ..@flag15 + dd ..@flag16, ..@flag17, ..@flag18, ..@flag19, ..@flag20, ..@flag21, ..@flag22, ..@flag23 + dd ..@flag24, ..@flag25, ..@flag26, ..@flag27, ..@flag28, ..@flag29, ..@flag30, ..@flag31 + dd ..@flag32, ..@flag33, ..@flag34, ..@flag35, ..@flag36, ..@flag37, ..@flag38, ..@flag39 + dd ..@flag40, ..@flag41, ..@flag42, ..@flag43, ..@flag44, ..@flag45, ..@flag46, ..@flag47 + dd ..@flag48, ..@flag49, ..@flag50, ..@flag51, ..@flag52, ..@flag53, ..@flag54, ..@flag55 + dd ..@flag56, ..@flag57, ..@flag58, ..@flag59, ..@flag60, ..@flag61, ..@flag62, ..@flag63 + dd ..@flag64, ..@flag65, ..@flag66, ..@flag67, ..@flag68, ..@flag69, ..@flag70, ..@flag71 + dd ..@flag72, ..@flag73, ..@flag74, ..@flag75, ..@flag76, ..@flag77, ..@flag78, ..@flag79 + dd ..@flag80, ..@flag81, ..@flag82, ..@flag83, ..@flag84, ..@flag85, ..@flag86, ..@flag87 + dd ..@flag88, ..@flag89, ..@flag90, ..@flag91, ..@flag92, ..@flag93, ..@flag94, ..@flag95 + dd ..@flag96, ..@flag97, ..@flag98, ..@flag99, ..@flag100, ..@flag101, ..@flag102, ..@flag103 + dd ..@flag104, ..@flag105, ..@flag106, ..@flag107, ..@flag108, ..@flag109, ..@flag110, ..@flag111 + dd ..@flag112, ..@flag113, ..@flag114, ..@flag115, ..@flag116, ..@flag117, ..@flag118, ..@flag119 + dd ..@flag120, ..@flag121, ..@flag122, ..@flag123, ..@flag124, ..@flag125, ..@flag126, ..@flag127 + dd ..@flag128, ..@flag129, ..@flag130, ..@flag131, ..@flag132, ..@flag133, ..@flag134, ..@flag135 + dd ..@flag136, ..@flag137, ..@flag138, ..@flag139, ..@flag140, ..@flag141, ..@flag142, ..@flag143 + dd ..@flag144, ..@flag145, ..@flag146, ..@flag147, ..@flag148, ..@flag149, ..@flag150, ..@flag151 + dd ..@flag152, ..@flag153, ..@flag154, ..@flag155, ..@flag156, ..@flag157, ..@flag158, ..@flag159 + dd ..@flag160, ..@flag161, ..@flag162, ..@flag163, ..@flag164, ..@flag165, ..@flag166, ..@flag167 + dd ..@flag168, ..@flag169, ..@flag170, ..@flag171, ..@flag172, ..@flag173, ..@flag174, ..@flag175 + dd ..@flag176, ..@flag177, ..@flag178, ..@flag179, ..@flag180, ..@flag181, ..@flag182, ..@flag183 + dd ..@flag184, ..@flag185, ..@flag186, ..@flag187, ..@flag188, ..@flag189, ..@flag190, ..@flag191 + dd ..@flag192, ..@flag193, ..@flag194, ..@flag195, ..@flag196, ..@flag197, ..@flag198, ..@flag199 + dd ..@flag200, ..@flag201, ..@flag202, ..@flag203, ..@flag204, ..@flag205, ..@flag206, ..@flag207 + dd ..@flag208, ..@flag209, ..@flag210, ..@flag211, ..@flag212, ..@flag213, ..@flag214, ..@flag215 + dd ..@flag216, ..@flag217, ..@flag218, ..@flag219, ..@flag220, ..@flag221, ..@flag222, ..@flag223 + dd ..@flag224, ..@flag225, ..@flag226, ..@flag227, ..@flag228, ..@flag229, ..@flag230, ..@flag231 + dd ..@flag232, ..@flag233, ..@flag234, ..@flag235, ..@flag236, ..@flag237, ..@flag238, ..@flag239 + dd ..@flag240, ..@flag241, ..@flag242, ..@flag243, ..@flag244, ..@flag245, ..@flag246, ..@flag247 + dd ..@flag248, ..@flag249, ..@flag250, ..@flag251, ..@flag252, ..@flag253, ..@flag254, ..@flag255 + +FuncTable2 + dd ..@cross0, ..@cross1, ..@cross2, ..@crossN, + dd ..@cross4, ..@crossN, ..@crossN, ..@crossN, + dd ..@cross8, ..@crossN, ..@crossN, ..@crossN, + dd ..@crossN, ..@crossN, ..@crossN, ..@crossN + diff --git a/src/hq4x.cpp b/src/hq4x.cpp new file mode 100644 index 00000000..c7a1b8da --- /dev/null +++ b/src/hq4x.cpp @@ -0,0 +1,212 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +#include "System.h" +#include "portable.h" + +#include "hq4x.h" + +#include + +/***************************************************************************/ +/* HQ4x C implementation */ + +/* + * This effect is a rewritten implementation of the hq4x effect made by Maxim Stepin + */ + +void hq4x_16_def(interp_uint16* restrict dst0, interp_uint16* restrict dst1, interp_uint16* restrict dst2, interp_uint16* restrict dst3, const interp_uint16* restrict src0, const interp_uint16* restrict src1, const interp_uint16* restrict src2, unsigned count) +{ + unsigned i; + + for(i=0;i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i> 3; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm1, c1 + movd mm2, c2 + punpcklbw mm1, reg_blank + punpcklbw mm2, reg_blank + pmullw mm1, const7 + paddw mm1, mm2 + psrlw mm1, 3 + packuswb mm1, reg_blank + movd [eax], mm1 + EMMS + } +#else + __asm + { + mov eax, c1 + mov ebx, c2 + mov ecx, eax + shl ecx, 3 + sub ecx, eax + add ecx, ebx + shr ecx, 3 + mov eax, pc + mov [eax], ecx + } +#endif +} + +void Interp4(unsigned char * pc, unsigned int c1, unsigned int c2, unsigned int c3) +{ + //*((int*)pc) = (c1*2+(c2+c3)*7)/16; + //*((int*)pc) = ((((c1 & 0x00FF00)*2 + ((c2 & 0x00FF00) + (c3 & 0x00FF00))*7 ) & 0x000FF000) + + // (((c1 & 0xFF00FF)*2 + ((c2 & 0xFF00FF) + (c3 & 0xFF00FF))*7 ) & 0x0FF00FF0)) >> 4; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm1, c1 + movd mm2, c2 + movd mm3, c3 + punpcklbw mm1, reg_blank + punpcklbw mm2, reg_blank + punpcklbw mm3, reg_blank + psllw mm1, 1 + paddw mm2, mm3 + pmullw mm2, const7 + paddw mm1, mm2 + psrlw mm1, 4 + packuswb mm1, reg_blank + movd [eax], mm1 + EMMS + } +#else + + __asm + { + mov eax, [c1] + and eax, 0FF00h + shl eax, 1 + mov ecx, [c2] + and ecx, 0FF00h + mov edx, [c3] + and edx, 0FF00h + add ecx, edx + imul ecx, ecx,7 + add eax, ecx + and eax, 0FF000h + + mov ebx, [c1] + and ebx, 0FF00FFh + shl ebx, 1 + mov ecx, [c2] + and ecx, 0FF00FFh + mov edx, [c3] + and edx, 0FF00FFh + add ecx, edx + imul ecx, ecx,7 + add ebx, ecx + and ebx, 0FF00FF0h + + add eax, ebx + shr eax, 4 + + mov ebx, pc + mov [ebx], eax + } +#endif +} + +void Interp5(unsigned char * pc, unsigned int c1, unsigned int c2) +{ + //*((int*)pc) = (c1+c2)/2; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm0, c1 + movd mm1, c2 + paddd mm0, mm1 + psrad mm0, 1 + movd [eax], mm0 + EMMS + } +#else + __asm + { + mov eax, pc + mov edx, c1 + add edx, c2 + shr edx, 1 + mov [eax], edx + } +#endif +} + + +bool Diff(unsigned int c1, unsigned int c2) +{ + unsigned int + YUV1 = RGBtoYUV(c1), + YUV2 = RGBtoYUV(c2); + + if (YUV1 == YUV2) return false; // Save some processing power + +#ifdef MMX + unsigned int retval; + __asm + { + mov eax, 0x7FFFFFFF + movd mm7, eax ;mm7 = ABS_MASK = 0x7FFFFFFF + + ; Copy source colors in first reg + movd mm0, YUV1 + movd mm1, YUV2 + + mov eax, 0x00FF0000 + movd mm6, eax ;mm6 = Ymask = 0x00FF0000 + + ; Calculate color Y difference + movq mm2, mm0 + movq mm3, mm1 + pand mm2, mm6 + pand mm3, mm6 + psubd mm2, mm3 + pand mm2, mm7 + + mov eax, 0x0000FF00 + movd mm6, eax ;mm6 = Umask = 0x0000FF00 + + ; Calculate color U difference + movq mm3, mm0 + movq mm4, mm1 + pand mm3, mm6 + pand mm4, mm6 + psubd mm3, mm4 + pand mm3, mm7 + + mov eax, 0x000000FF + movd mm6, eax ;mm6 = Vmask = 0x000000FF + + ; Calculate color V difference + movq mm4, mm0 + movq mm5, mm1 + pand mm4, mm6 + pand mm5, mm6 + psubd mm4, mm5 + pand mm4, mm7 + + mov eax, 0x00300000 + movd mm5, eax ;mm5 = trY = 0x00300000 + mov eax, 0x00000700 + movd mm6, eax ;mm6 = trU = 0x00000700 + mov eax, 0x00000006 + movd mm7, eax ;mm7 = trV = 0x00000006 + + ; Compare the results + pcmpgtd mm2, trY + pcmpgtd mm3, trU + pcmpgtd mm4, trV + por mm2, mm3 + por mm2, mm4 + + movd retval, mm2 + + EMMS + } + return (retval != 0); +#else + return + ( abs32((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) || + ( abs32((YUV1 & Umask) - (YUV2 & Umask)) > trU ) || + ( abs32((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ); +#endif +} + + +unsigned int RGBtoYUV(unsigned int c) +{ // Division through 3 slows down the emulation about 10% !!! +#ifdef MMX + unsigned int retval; + __asm + { + movd mm0, c + movq mm1, mm0 + movq mm2, mm0 ;mm0=mm1=mm2=c + + mov eax, 0x000000FF + movd mm5, eax ;mm5 = REDMASK = 0x000000FF + mov eax, 0x0000FF00 + movd mm6, eax ;mm6 = GREENMASK = 0x0000FF00 + mov eax, 0x00FF0000 + movd mm7, eax ;mm7 = BLUEMASK = 0x00FF0000 + + + pand mm0, mm5 + pand mm1, mm6 + pand mm2, mm7 ;mm0=R mm1=G mm2=B + + movq mm3, mm0 + paddd mm3, mm1 + paddd mm3, mm2 +; psrld mm3, 2 ;mm3=Y +; pslld mm3, 16 + pslld mm3, 14 ;mm3=Y<<16 + + mov eax, 512 + movd mm7, eax ;mm7 = 128 << 2 = 512 + + movq mm4, mm0 + psubd mm4, mm2 +; psrld mm4, 2 +; paddd mm4, mm7 ;mm4=U +; pslld mm4, 8 ;mm4=U<<8 + paddd mm4, mm7 + pslld mm4, 6 + + mov eax, 128 + movd mm7, eax ;mm7 = 128 + + movq mm5, mm1 + pslld mm5, 1 + psubd mm5, mm0 + psubd mm5, mm2 + psrld mm5, 3 + paddd mm5, mm7 ;mm5=V + + paddd mm5, mm4 + paddd mm5, mm3 + + movd retval, mm5 + + EMMS + } + return retval; +#else + unsigned char r, g, b, Y, u, v; + r = (c & 0x000000FF); + g = (c & 0x0000FF00) >> 8; + b = (c & 0x00FF0000) >> 16; + Y = (r + g + b) >> 2; + u = 128 + ((r - b) >> 2); + v = 128 + ((-r + 2*g -b)>>3); + return (Y<<16) + (u<<8) + v; + + // Extremely High Quality Code + //unsigned char r, g, b; + //r = c & 0xFF; + //g = (c >> 8) & 0xFF; + //b = (c >> 16) & 0xFF; + //unsigned char y, u, v; + //y = (0.256788 * r + 0.504129 * g + 0.097906 * b) + 16; + //u = (-0.148223 * r - 0.290993 * g + 0.439216 * b) + 128; + //v = (0.439216 * r - 0.367788 * g - 0.071427 * b) + 128; + //return (y << 16) + (u << 8) + v; +#endif +} \ No newline at end of file diff --git a/src/hq_shared32.h b/src/hq_shared32.h new file mode 100644 index 00000000..764d35e4 --- /dev/null +++ b/src/hq_shared32.h @@ -0,0 +1,90 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#define SIZE_PIXEL 4 // 32bit = 4 bytes + +#define abs32(value) (value & 0x7FFFFFFF) + +#define PIXEL00_1M Interp1( pOut, c[5], c[1] ); +#define PIXEL00_1U Interp1( pOut, c[5], c[2] ); +#define PIXEL00_1L Interp1( pOut, c[5], c[4] ); +#define PIXEL00_2 Interp2( pOut, c[5], c[4], c[2] ); +#define PIXEL00_4 Interp4( pOut, c[5], c[4], c[2] ); +#define PIXEL00_5 Interp5( pOut, c[4], c[2] ); +#define PIXEL00_C *((unsigned int*)(pOut)) = c[5]; + +#define PIXEL01_1 Interp1( pOut+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL01_3 Interp3( pOut+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL01_6 Interp1( pOut+SIZE_PIXEL, c[2], c[5] ); +#define PIXEL01_C *((unsigned int*)(pOut+4)) = c[5]; + +#define PIXEL02_1M Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[3] ); +#define PIXEL02_1U Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL02_1R Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL02_2 Interp2( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2], c[6] ); +#define PIXEL02_4 Interp4( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2], c[6] ); +#define PIXEL02_5 Interp5( pOut+SIZE_PIXEL+SIZE_PIXEL, c[2], c[6] ); +#define PIXEL02_C *((unsigned int*)(pOut+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +#define PIXEL10_1 Interp1( pOut+dstPitch, c[5], c[4] ); +#define PIXEL10_3 Interp3( pOut+dstPitch, c[5], c[4] ); +#define PIXEL10_6 Interp1( pOut+dstPitch, c[4], c[5] ); +#define PIXEL10_C *((unsigned int*)(pOut+dstPitch)) = c[5]; + +#define PIXEL11 *((unsigned int*)(pOut+dstPitch+SIZE_PIXEL)) = c[5]; + +#define PIXEL12_1 Interp1( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL12_3 Interp3( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL12_6 Interp1( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[6], c[5] ); +#define PIXEL12_C *((unsigned int*)(pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +#define PIXEL20_1M Interp1( pOut+dstPitch+dstPitch, c[5], c[7] ); +#define PIXEL20_1D Interp1( pOut+dstPitch+dstPitch, c[5], c[8] ); +#define PIXEL20_1L Interp1( pOut+dstPitch+dstPitch, c[5], c[4] ); +#define PIXEL20_2 Interp2( pOut+dstPitch+dstPitch, c[5], c[8], c[4] ); +#define PIXEL20_4 Interp4( pOut+dstPitch+dstPitch, c[5], c[8], c[4] ); +#define PIXEL20_5 Interp5( pOut+dstPitch+dstPitch, c[8], c[4] ); +#define PIXEL20_C *((unsigned int*)(pOut+dstPitch+dstPitch)) = c[5]; + +#define PIXEL21_1 Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL21_3 Interp3( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL21_6 Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[8], c[5] ); +#define PIXEL21_C *((unsigned int*)(pOut+dstPitch+dstPitch+SIZE_PIXEL)) = c[5]; + +#define PIXEL22_1M Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[9] ); +#define PIXEL22_1D Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL22_1R Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL22_2 Interp2( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6], c[8] ); +#define PIXEL22_4 Interp4( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6], c[8] ); +#define PIXEL22_5 Interp5( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[6], c[8] ); +#define PIXEL22_C *((unsigned int*)(pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +const int Ymask = 0x00FF0000; +const int Umask = 0x0000FF00; +const int Vmask = 0x000000FF; +const int trY = 0x00300000; +const int trU = 0x00000700; +const int trV = 0x00000006; + +void Interp1(unsigned char * pc, unsigned int c1, unsigned int c2); +void Interp2(unsigned char * pc, unsigned int c1, unsigned int c2, unsigned int c3); +void Interp3(unsigned char * pc, unsigned int c1, unsigned int c2); +void Interp4(unsigned char * pc, unsigned int c1, unsigned int c2, unsigned int c3); +void Interp5(unsigned char * pc, unsigned int c1, unsigned int c2); +bool Diff(unsigned int c1, unsigned int c2); +unsigned int RGBtoYUV(unsigned int c); \ No newline at end of file diff --git a/src/interframe.cpp b/src/interframe.cpp new file mode 100644 index 00000000..78ea76fd --- /dev/null +++ b/src/interframe.cpp @@ -0,0 +1,648 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" +#include +#include + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +/* + * Thanks to Kawaks' Mr. K for the code + + Incorporated into vba by Anthony Di Franco +*/ + +static u8 *frm1 = NULL; +static u8 *frm2 = NULL; +static u8 *frm3 = NULL; + +extern int RGB_LOW_BITS_MASK; +extern u32 qRGB_COLOR_MASK[2]; + +static void Init() +{ + frm1 = (u8 *)calloc(322*242,4); + // 1 frame ago + frm2 = (u8 *)calloc(322*242,4); + // 2 frames ago + frm3 = (u8 *)calloc(322*242,4); + // 3 frames ago +} + +void InterframeCleanup() +{ + if(frm1) + free(frm1); + if(frm2) + free(frm2); + if(frm3) + free(frm3); + frm1 = frm2 = frm3 = NULL; +} + +#ifdef MMX +static void SmartIB_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + u16 *src2 = (u16 *)frm2; + u16 *src3 = (u16 *)frm3; + + int sPitch = srcPitch >> 1; + if (width > sPitch) width = sPitch; + sPitch -= width; + int count = width >> 2; + + for(int i = 0; i < height; i++) { +#ifdef __GNUC__ + asm volatile ( + "push %4\n" + "movq 0(%5), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq 0(%2), %%mm2\n" // src2 + "movq 0(%3), %%mm3\n" // src3 + "movq %%mm0, 0(%3)\n" // src3 = src0 + "movq %%mm0, %%mm4\n" + "movq %%mm1, %%mm5\n" + "pcmpeqw %%mm2, %%mm5\n" // src1 == src2 (A) + "pcmpeqw %%mm3, %%mm4\n" // src3 == src0 (B) + "por %%mm5, %%mm4\n" // A | B + "movq %%mm2, %%mm5\n" + "pcmpeqw %%mm0, %%mm5\n" // src0 == src2 (C) + "pcmpeqw %%mm1, %%mm3\n" // src1 == src3 (D) + "por %%mm3, %%mm5\n" // C|D + "pandn %%mm5, %%mm4\n" // (!(A|B))&(C|D) + "movq %%mm0, %%mm2\n" + "pand %%mm7, %%mm2\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrlw $1, %%mm2\n" // (color & colorMask) >> 1 (E) + "psrlw $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddw %%mm2, %%mm1\n" // E+F + "pand %%mm4, %%mm1\n" // (E+F) & res + "pandn %%mm0, %%mm4\n" // color& !res + + "por %%mm1, %%mm4\n" + "movq %%mm4, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + "addl $8, %2\n" + "addl $8, %3\n" + + "decl %4\n" + "jnz 0b\n" + "pop %4\n" + "emms\n" + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (src3) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, src3; + mov edi, count; + label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq mm2, qword ptr [ecx]; // src2 + movq mm3, qword ptr [edx]; // src3 + movq qword ptr [edx], mm0; // src3 = src0 + movq mm4, mm0; + movq mm5, mm1; + pcmpeqw mm5, mm2; // src1 == src2 (A) + pcmpeqw mm4, mm3; // src3 == src0 (B) + por mm4, mm5; // A | B + movq mm5, mm2; + pcmpeqw mm5, mm0; // src0 == src2 (C) + pcmpeqw mm3, mm1; // src1 == src3 (D) + por mm5, mm3; // C|D + pandn mm4, mm5; // (!(A|B))&(C|D) + movq mm2, mm0; + pand mm2, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrlw mm2, 1; // (color & colorMask) >> 1 (E) + psrlw mm1, 1; // (src & colorMask) >> 1 (F) + paddw mm1, mm2; // E+F + pand mm1, mm4; // (E+F) & res + pandn mm4, mm0; // color & !res + + por mm4, mm1; + movq qword ptr [eax], mm4; // src0 = res + + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov src3, edx; + emms; + } +#endif + src0+= sPitch; + src1+= sPitch; + src2+= sPitch; + src3+= sPitch; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} +#endif + +void SmartIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if(frm1 == NULL) { + Init(); + } +#ifdef MMX + if(cpu_mmx) { + SmartIB_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + u16 *src2 = (u16 *)frm2; + u16 *src3 = (u16 *)frm3; + + int sPitch = srcPitch >> 1; + if (width > sPitch) width = sPitch; + sPitch -= width; + + int pos = 0; + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + u16 color = src0[pos]; + src0[pos] = + (src1[pos] != src2[pos]) && + (src3[pos] != color) && + ((color == src2[pos]) || (src1[pos] == src3[pos])) + ? (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)) : + color; + src3[pos] = color; /* oldest buffer now holds newest frame */ + pos++; + } + pos += sPitch; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#ifdef MMX +static void SmartIB32_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + u32 *src2 = (u32 *)frm2; + u32 *src3 = (u32 *)frm3; + + int sPitch = srcPitch >> 2; + if (width > sPitch) width = sPitch; + sPitch -= width; + int count = width >> 1; + + for(int i = 0; i < height; i++) { +#ifdef __GNUC__ + asm volatile ( + "push %4\n" + "movq 0(%5), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq 0(%2), %%mm2\n" // src2 + "movq 0(%3), %%mm3\n" // src3 + "movq %%mm0, 0(%3)\n" // src3 = src0 + "movq %%mm0, %%mm4\n" + "movq %%mm1, %%mm5\n" + "pcmpeqd %%mm2, %%mm5\n" // src1 == src2 (A) + "pcmpeqd %%mm3, %%mm4\n" // src3 == src0 (B) + "por %%mm5, %%mm4\n" // A | B + "movq %%mm2, %%mm5\n" + "pcmpeqd %%mm0, %%mm5\n" // src0 == src2 (C) + "pcmpeqd %%mm1, %%mm3\n" // src1 == src3 (D) + "por %%mm3, %%mm5\n" // C|D + "pandn %%mm5, %%mm4\n" // (!(A|B))&(C|D) + "movq %%mm0, %%mm2\n" + "pand %%mm7, %%mm2\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrld $1, %%mm2\n" // (color & colorMask) >> 1 (E) + "psrld $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddd %%mm2, %%mm1\n" // E+F + "pand %%mm4, %%mm1\n" // (E+F) & res + "pandn %%mm0, %%mm4\n" // color& !res + + "por %%mm1, %%mm4\n" + "movq %%mm4, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + "addl $8, %2\n" + "addl $8, %3\n" + + "decl %4\n" + "jnz 0b\n" + "pop %4\n" + "emms\n" + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (src3) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, src3; + mov edi, count; + label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq mm2, qword ptr [ecx]; // src2 + movq mm3, qword ptr [edx]; // src3 + movq qword ptr [edx], mm0; // src3 = src0 + movq mm4, mm0; + movq mm5, mm1; + pcmpeqd mm5, mm2; // src1 == src2 (A) + pcmpeqd mm4, mm3; // src3 == src0 (B) + por mm4, mm5; // A | B + movq mm5, mm2; + pcmpeqd mm5, mm0; // src0 == src2 (C) + pcmpeqd mm3, mm1; // src1 == src3 (D) + por mm5, mm3; // C|D + pandn mm4, mm5; // (!(A|B))&(C|D) + movq mm2, mm0; + pand mm2, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrld mm2, 1; // (color & colorMask) >> 1 (E) + psrld mm1, 1; // (src & colorMask) >> 1 (F) + paddd mm1, mm2; // E+F + pand mm1, mm4; // (E+F) & res + pandn mm4, mm0; // color & !res + + por mm4, mm1; + movq qword ptr [eax], mm4; // src0 = res + + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov src3, edx; + emms; + } +#endif + + src0 += sPitch; + src1 += sPitch; + src2 += sPitch; + src3 += sPitch; + } + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} +#endif + +void SmartIB32(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if(frm1 == NULL) { + Init(); + } +#ifdef MMX + if(cpu_mmx) { + SmartIB32_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + u32 *src2 = (u32 *)frm2; + u32 *src3 = (u32 *)frm3; + + u32 colorMask = 0xfefefe; + + int sPitch = srcPitch >> 2; + if (width > sPitch) width = sPitch; + sPitch -= width; + int pos = 0; + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + u32 color = src0[pos]; + src0[pos] = + (src1[pos] != src2[pos]) && + (src3[pos] != color) && + ((color == src2[pos]) || (src1[pos] == src3[pos])) + ? (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)) : + color; + src3[pos] = color; /* oldest buffer now holds newest frame */ + pos++; + } + pos += sPitch; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#ifdef MMX +static void MotionBlurIB_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int sPitch = srcPitch >> 1; + if (width > sPitch) width = sPitch; + sPitch -= width; + int count = width >> 2; + + for(int i = 0; i < height; i++) { +#ifdef __GNUC__ + asm volatile ( + "push %2\n" + "movq 0(%3), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq %%mm0, 0(%1)\n" // src1 = src0 + "pand %%mm7, %%mm0\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrlw $1, %%mm0\n" // (color & colorMask) >> 1 (E) + "psrlw $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddw %%mm1, %%mm0\n" // E+F + + "movq %%mm0, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + + "decl %2\n" + "jnz 0b\n" + "pop %2\n" + "emms\n" + : "+r" (src0), "+r" (src1) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov edi, count; + label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq qword ptr [ebx], mm0; // src1 = src0 + pand mm0, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrlw mm0, 1; // (color & colorMask) >> 1 (E) + psrlw mm1, 1; // (src & colorMask) >> 1 (F) + paddw mm0, mm1; // E+F + + movq qword ptr [eax], mm0; // src0 = res + + add eax, 8; + add ebx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + emms; + } +#endif + src0 += sPitch; + src1 += sPitch; + } +} +#endif + +void MotionBlurIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if(frm1 == NULL) { + Init(); + } + +#ifdef MMX + if(cpu_mmx) { + MotionBlurIB_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int sPitch = srcPitch >> 1; + if (width > sPitch) width = sPitch; + sPitch -= width; + + int pos = 0; + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + u16 color = src0[pos]; + src0[pos] = + (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + pos += sPitch; + } +} + +#ifdef MMX +static void MotionBlurIB32_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + + int sPitch = srcPitch >> 2; + if (width > sPitch) width = sPitch; + sPitch -= width; + int count = width >> 1; + + for(int i = 0; i < height; i++) { +#ifdef __GNUC__ + asm volatile ( + "push %2\n" + "movq 0(%3), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq %%mm0, 0(%1)\n" // src1 = src0 + "pand %%mm7, %%mm0\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrld $1, %%mm0\n" // (color & colorMask) >> 1 (E) + "psrld $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddd %%mm1, %%mm0\n" // E+F + + "movq %%mm0, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + + "decl %2\n" + "jnz 0b\n" + "pop %2\n" + "emms\n" + : "+r" (src0), "+r" (src1) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov edi, count; + label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq qword ptr [ebx], mm0; // src1 = src0 + pand mm0, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrld mm0, 1; // (color & colorMask) >> 1 (E) + psrld mm1, 1; // (src & colorMask) >> 1 (F) + paddd mm0, mm1; // E+F + + movq qword ptr [eax], mm0; // src0 = res + + add eax, 8; + add ebx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + emms; + } +#endif + src0 += sPitch; + src1 += sPitch; + } +} +#endif + +void MotionBlurIB32(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if(frm1 == NULL) { + Init(); + } + +#ifdef MMX + if(cpu_mmx) { + MotionBlurIB32_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + + u32 colorMask = 0xfefefe; + + int sPitch = srcPitch >> 2; + if (width > sPitch) width = sPitch; + sPitch -= width; + int pos = 0; + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + u32 color = src0[pos]; + src0[pos] = (((color & colorMask) >> 1) + + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + pos += sPitch; + } +} + +static int count = 0; + +void InterlaceIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if(frm1 == NULL) { + Init(); + } + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int sPitch = srcPitch >> 1; + + int pos = 0; + for (int j = 0; j < height; j++) { + bool render = count ? (j & 1) != 0 : (j & 1) == 0; + if(render) { + for (int i = 0; i < sPitch; i++) { + u16 color = src0[pos]; + src0[pos] = + (((color & colorMask) >> 1) + ((((src1[pos] & colorMask) >> 1) & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + } else { + for (int i = 0; i < sPitch; i++) { + u16 color = src0[pos]; + src0[pos] = + (((((color & colorMask) >> 1) & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + } + } + count = count ^ 1; +} diff --git a/src/interp.h b/src/interp.h new file mode 100644 index 00000000..b899ea73 --- /dev/null +++ b/src/interp.h @@ -0,0 +1,299 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +#ifndef __INTERP_H +#define __INTERP_H + +/***************************************************************************/ +/* Basic types */ + +/***************************************************************************/ +/* interpolation */ + +static unsigned interp_mask[2]; +static unsigned interp_bits_per_pixel; + +typedef unsigned short interp_uint16; +typedef unsigned int interp_uint32; + +#define restrict + +#define INTERP_16_MASK_1(v) (v & interp_mask[0]) +#define INTERP_16_MASK_2(v) (v & interp_mask[1]) + +static inline u16 interp_16_521(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*5 + INTERP_16_MASK_1(p2)*2 + INTERP_16_MASK_1(p3)*1) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*5 + INTERP_16_MASK_2(p2)*2 + INTERP_16_MASK_2(p3)*1) / 8); +} + +static inline u16 interp_16_332(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*3 + INTERP_16_MASK_1(p2)*3 + INTERP_16_MASK_1(p3)*2) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*3 + INTERP_16_MASK_2(p2)*3 + INTERP_16_MASK_2(p3)*2) / 8); +} + +static inline u16 interp_16_611(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*6 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*6 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 8); +} + +static inline u16 interp_16_71(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*7 + INTERP_16_MASK_1(p2)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*7 + INTERP_16_MASK_2(p2)) / 8); +} + +static inline u16 interp_16_211(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*2 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 4) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*2 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 4); +} + +static inline u16 interp_16_772(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1(((INTERP_16_MASK_1(p1) + INTERP_16_MASK_1(p2))*7 + INTERP_16_MASK_1(p3)*2) / 16) + | INTERP_16_MASK_2(((INTERP_16_MASK_2(p1) + INTERP_16_MASK_2(p2))*7 + INTERP_16_MASK_2(p3)*2) / 16); +} + +static inline u16 interp_16_11(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1) + INTERP_16_MASK_1(p2)) / 2) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1) + INTERP_16_MASK_2(p2)) / 2); +} + +static inline u16 interp_16_31(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*3 + INTERP_16_MASK_1(p2)) / 4) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*3 + INTERP_16_MASK_2(p2)) / 4); +} + +static inline u16 interp_16_1411(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*14 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*14 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 16); +} + +static inline u16 interp_16_431(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*4 + INTERP_16_MASK_1(p2)*3 + INTERP_16_MASK_1(p3)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*4 + INTERP_16_MASK_2(p2)*3 + INTERP_16_MASK_2(p3)) / 8); +} + +static inline u16 interp_16_53(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*5 + INTERP_16_MASK_1(p2)*3) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*5 + INTERP_16_MASK_2(p2)*3) / 8); +} + +static inline u16 interp_16_151(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*15 + INTERP_16_MASK_1(p2)) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*15 + INTERP_16_MASK_2(p2)) / 16); +} + +static inline u16 interp_16_97(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*9 + INTERP_16_MASK_1(p2)*7) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*9 + INTERP_16_MASK_2(p2)*7) / 16); +} + +#define INTERP_32_MASK_1(v) (v & 0xFF00FF) +#define INTERP_32_MASK_2(v) (v & 0x00FF00) + +static inline u32 interp_32_521(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*5 + INTERP_32_MASK_1(p2)*2 + INTERP_32_MASK_1(p3)*1) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*5 + INTERP_32_MASK_2(p2)*2 + INTERP_32_MASK_2(p3)*1) / 8); +} + +static inline u32 interp_32_332(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*3 + INTERP_32_MASK_1(p2)*3 + INTERP_32_MASK_1(p3)*2) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*3 + INTERP_32_MASK_2(p2)*3 + INTERP_32_MASK_2(p3)*2) / 8); +} + +static inline u32 interp_32_211(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*2 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 4) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*2 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 4); +} + +static inline u32 interp_32_611(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*6 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*6 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 8); +} + +static inline u32 interp_32_71(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*7 + INTERP_32_MASK_1(p2)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*7 + INTERP_32_MASK_2(p2)) / 8); +} + +static inline u32 interp_32_772(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1(((INTERP_32_MASK_1(p1) + INTERP_32_MASK_1(p2))*7 + INTERP_32_MASK_1(p3)*2) / 16) + | INTERP_32_MASK_2(((INTERP_32_MASK_2(p1) + INTERP_32_MASK_2(p2))*7 + INTERP_32_MASK_2(p3)*2) / 16); +} + +static inline u32 interp_32_11(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1) + INTERP_32_MASK_1(p2)) / 2) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1) + INTERP_32_MASK_2(p2)) / 2); +} + +static inline u32 interp_32_31(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*3 + INTERP_32_MASK_1(p2)) / 4) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*3 + INTERP_32_MASK_2(p2)) / 4); +} + +static inline u32 interp_32_1411(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*14 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*14 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 16); +} + +static inline u32 interp_32_431(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*4 + INTERP_32_MASK_1(p2)*3 + INTERP_32_MASK_1(p3)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*4 + INTERP_32_MASK_2(p2)*3 + INTERP_32_MASK_2(p3)) / 8); +} + +static inline u32 interp_32_53(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*5 + INTERP_32_MASK_1(p2)*3) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*5 + INTERP_32_MASK_2(p2)*3) / 8); +} + +static inline u32 interp_32_151(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*15 + INTERP_32_MASK_1(p2)) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*15 + INTERP_32_MASK_2(p2)) / 16); +} + +static inline u32 interp_32_97(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*9 + INTERP_32_MASK_1(p2)*7) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*9 + INTERP_32_MASK_2(p2)*7) / 16); +} + +/***************************************************************************/ +/* diff */ + +#define INTERP_Y_LIMIT (0x30*4) +#define INTERP_U_LIMIT (0x07*4) +#define INTERP_V_LIMIT (0x06*8) + +static int interp_16_diff(u16 p1, u16 p2) +{ + int r, g, b; + int y, u, v; + + if (p1 == p2) + return 0; + + if (interp_bits_per_pixel == 16) { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x7E0) - (p2 & 0x7E0)) >> 3; + r = (int)((p1 & 0xF800) - (p2 & 0xF800)) >> 8; + } else { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x3E0) - (p2 & 0x3E0)) >> 2; + r = (int)((p1 & 0x7C00) - (p2 & 0x7C00)) >> 7; + } + + y = r + g + b; + u = r - b; + v = -r + 2*g - b; + + if (y < -INTERP_Y_LIMIT || y > INTERP_Y_LIMIT) + return 1; + + if (u < -INTERP_U_LIMIT || u > INTERP_U_LIMIT) + return 1; + + if (v < -INTERP_V_LIMIT || v > INTERP_V_LIMIT) + return 1; + + return 0; +} + +static int interp_32_diff(u32 p1, u32 p2) +{ + int r, g, b; + int y, u, v; + + if ((p1 & 0xF8F8F8) == (p2 & 0xF8F8F8)) + return 0; + + b = (int)((p1 & 0xFF) - (p2 & 0xFF)); + g = (int)((p1 & 0xFF00) - (p2 & 0xFF00)) >> 8; + r = (int)((p1 & 0xFF0000) - (p2 & 0xFF0000)) >> 16; + + y = r + g + b; + u = r - b; + v = -r + 2*g - b; + + if (y < -INTERP_Y_LIMIT || y > INTERP_Y_LIMIT) + return 1; + + if (u < -INTERP_U_LIMIT || u > INTERP_U_LIMIT) + return 1; + + if (v < -INTERP_V_LIMIT || v > INTERP_V_LIMIT) + return 1; + + return 0; +} + +static void interp_set(unsigned bits_per_pixel) +{ + interp_bits_per_pixel = bits_per_pixel; + + switch (bits_per_pixel) { + case 15 : + interp_mask[0] = 0x7C1F; + interp_mask[1] = 0x03E0; + break; + case 16 : + interp_mask[0] = 0xF81F; + interp_mask[1] = 0x07E0; + break; + case 32 : + interp_mask[0] = 0xFF00FF; + interp_mask[1] = 0x00FF00; + break; + } +} + +#endif diff --git a/src/lq2x.dat b/src/lq2x.dat new file mode 100644 index 00000000..4f7dd184 --- /dev/null +++ b/src/lq2x.dat @@ -0,0 +1,1318 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is derived from the hq effect made by Maxim Stepin + */ + +case 0 : +case 2 : +case 4 : +case 6 : +case 8 : +case 12 : +case 16 : +case 20 : +case 24 : +case 28 : +case 32 : +case 34 : +case 36 : +case 38 : +case 40 : +case 44 : +case 48 : +case 52 : +case 56 : +case 60 : +case 64 : +case 66 : +case 68 : +case 70 : +case 96 : +case 98 : +case 100 : +case 102 : +case 128 : +case 130 : +case 132 : +case 134 : +case 136 : +case 140 : +case 144 : +case 148 : +case 152 : +case 156 : +case 160 : +case 162 : +case 164 : +case 166 : +case 168 : +case 172 : +case 176 : +case 180 : +case 184 : +case 188 : +case 192 : +case 194 : +case 196 : +case 198 : +case 224 : +case 226 : +case 228 : +case 230 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +} break; +case 1 : +case 5 : +case 9 : +case 13 : +case 17 : +case 21 : +case 25 : +case 29 : +case 33 : +case 37 : +case 41 : +case 45 : +case 49 : +case 53 : +case 57 : +case 61 : +case 65 : +case 69 : +case 97 : +case 101 : +case 129 : +case 133 : +case 137 : +case 141 : +case 145 : +case 149 : +case 153 : +case 157 : +case 161 : +case 165 : +case 169 : +case 173 : +case 177 : +case 181 : +case 185 : +case 189 : +case 193 : +case 197 : +case 225 : +case 229 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +} break; +case 3 : +case 35 : +case 67 : +case 99 : +case 131 : +case 163 : +case 195 : +case 227 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +} break; +case 7 : +case 39 : +case 71 : +case 103 : +case 135 : +case 167 : +case 199 : +case 231 : +{ +P(0, 0) = I1(3); +P(1, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +} break; +case 10 : +case 138 : +{ +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 0, 1, 3); +} +} break; +case 11 : +case 27 : +case 75 : +case 139 : +case 155 : +case 203 : +{ +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +} break; +case 14 : +case 142 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 0); + P(1, 0) = I2(3, 1, 0, 1); +} +} break; +case 15 : +case 143 : +case 207 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 4); + P(1, 0) = I2(3, 1, 4, 1); +} +} break; +case 18 : +case 22 : +case 30 : +case 50 : +case 54 : +case 62 : +case 86 : +case 118 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I1(2); +P(1, 1) = I1(2); +if (MUR) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); +} else { + P(0, 0) = I2(3, 1, 2, 1); + P(1, 0) = I3(3, 3, 2, 1, 5, 2); +} +} break; +case 23 : +case 55 : +case 119 : +{ +P(0, 1) = I1(3); +P(1, 1) = I1(3); +if (MUR) { + P(0, 0) = I1(3); + P(1, 0) = I1(3); +} else { + P(0, 0) = I2(3, 1, 3, 1); + P(1, 0) = I3(3, 3, 2, 1, 5, 3); +} +} break; +case 26 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 31 : +case 95 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 42 : +case 170 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 0); + P(0, 1) = I2(3, 1, 0, 3); +} +} break; +case 43 : +case 171 : +case 187 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(3, 3, 2, 1, 3, 2); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 58 : +case 154 : +case 186 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 59 : +{ +P(0, 1) = I1(2); +P(1, 1) = I1(2); +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(6, 1, 1, 2, 1, 5); +} +} break; +case 63 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 72 : +case 76 : +case 104 : +case 106 : +case 108 : +case 110 : +case 120 : +case 124 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +} break; +case 73 : +case 77 : +case 105 : +case 109 : +case 125 : +{ +P(1, 0) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 0) = I1(1); + P(0, 1) = I1(1); +} else { + P(0, 0) = I2(3, 1, 1, 3); + P(0, 1) = I3(3, 3, 2, 3, 7, 1); +} +} break; +case 74 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 0, 1, 3); +} +} break; +case 78 : +case 202 : +case 206 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +} break; +case 79 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +} break; +case 80 : +case 208 : +case 210 : +case 216 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +} break; +case 81 : +case 209 : +case 217 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(2, 1, 1, 1, 5, 7); +} +} break; +case 82 : +case 214 : +case 222 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 83 : +case 115 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I3(6, 1, 1, 2, 5, 7); +} +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(6, 1, 1, 2, 1, 5); +} +} break; +case 84 : +case 212 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I2(3, 1, 0, 5); + P(1, 1) = I3(3, 3, 2, 5, 7, 0); +} +} break; +case 85 : +case 213 : +case 221 : +{ +P(0, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 0) = I1(1); + P(1, 1) = I1(1); +} else { + P(1, 0) = I2(3, 1, 1, 5); + P(1, 1) = I3(3, 3, 2, 5, 7, 1); +} +} break; +case 87 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +if (MDR) { + P(1, 1) = I1(3); +} else { + P(1, 1) = I3(6, 1, 1, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(2, 1, 1, 3, 1, 5); +} +} break; +case 88 : +case 248 : +case 250 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +} break; +case 89 : +case 93 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(6, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(6, 1, 1, 1, 5, 7); +} +} break; +case 90 : +{ +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 91 : +{ +if (MDL) { + P(0, 1) = I1(2); +} else { + P(0, 1) = I3(6, 1, 1, 2, 3, 7); +} +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I3(6, 1, 1, 2, 5, 7); +} +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(6, 1, 1, 2, 1, 5); +} +} break; +case 92 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +} break; +case 94 : +{ +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 107 : +case 123 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +if (MDL) { + P(0, 1) = I1(2); +} else { + P(0, 1) = I3(2, 1, 1, 2, 3, 7); +} +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +} break; +case 111 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 112 : +case 240 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDR) { + P(0, 1) = I1(0); + P(1, 1) = I1(0); +} else { + P(0, 1) = I2(3, 1, 0, 7); + P(1, 1) = I3(3, 3, 2, 5, 7, 0); +} +} break; +case 113 : +case 241 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +if (MDR) { + P(0, 1) = I1(1); + P(1, 1) = I1(1); +} else { + P(0, 1) = I2(3, 1, 1, 7); + P(1, 1) = I3(3, 3, 2, 5, 7, 1); +} +} break; +case 114 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 116 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +} break; +case 117 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(6, 1, 1, 1, 5, 7); +} +} break; +case 121 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(2, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(6, 1, 1, 1, 5, 7); +} +} break; +case 122 : +{ +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(6, 1, 1, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 126 : +{ +P(0, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 127 : +{ +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(2, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 4, 1, 5); +} +} break; +case 146 : +case 150 : +case 178 : +case 182 : +case 190 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(3, 3, 2, 1, 5, 0); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(6, 1, 1, 2, 1, 5); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(14, 1, 1, 3, 1, 5); +} +} break; +case 158 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 159 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 191 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 200 : +case 204 : +case 232 : +case 236 : +case 238 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(1, 1) = I1(0); +} else { + P(0, 1) = I3(3, 3, 2, 3, 7, 0); + P(1, 1) = I2(3, 1, 0, 7); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(6, 1, 1, 1, 3, 7); +} +} break; +case 211 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I3(2, 1, 1, 2, 5, 7); +} +} break; +case 215 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +if (MDR) { + P(1, 1) = I1(3); +} else { + P(1, 1) = I3(2, 1, 1, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(14, 1, 1, 3, 1, 5); +} +} break; +case 218 : +{ +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 219 : +{ +P(1, 0) = I1(2); +P(0, 1) = I1(2); +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I3(2, 1, 1, 2, 5, 7); +} +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +} break; +case 220 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(6, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +} break; +case 223 : +{ +P(0, 1) = I1(4); +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(2, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(14, 1, 1, 1, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(6, 1, 1, 0, 1, 3); +} +} break; +case 235 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +if (MDL) { + P(0, 1) = I1(2); +} else { + P(0, 1) = I3(14, 1, 1, 2, 3, 7); +} +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +} break; +case 239 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +} break; +case 242 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(2, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(6, 1, 1, 0, 1, 5); +} +} break; +case 243 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +if (MDR) { + P(0, 1) = I1(2); + P(1, 1) = I1(2); +} else { + P(0, 1) = I2(3, 1, 2, 7); + P(1, 1) = I3(3, 3, 2, 5, 7, 2); +} +} break; +case 244 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(14, 1, 1, 0, 5, 7); +} +} break; +case 245 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(14, 1, 1, 1, 5, 7); +} +} break; +case 246 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(14, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 247 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +if (MDR) { + P(1, 1) = I1(3); +} else { + P(1, 1) = I3(14, 1, 1, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(14, 1, 1, 3, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(14, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(2, 1, 1, 1, 5, 7); +} +} break; +case 251 : +{ +P(1, 0) = I1(2); +if (MDL) { + P(0, 1) = I1(2); +} else { + P(0, 1) = I3(14, 1, 1, 2, 3, 7); +} +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I3(2, 1, 1, 2, 5, 7); +} +if (MUL) { + P(0, 0) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 2, 1, 3); +} +} break; +case 252 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(14, 1, 1, 0, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +if (MDL) { + P(0, 1) = I1(1); +} else { + P(0, 1) = I3(14, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 1) = I1(1); +} else { + P(1, 1) = I3(14, 1, 1, 1, 5, 7); +} +} break; +case 254 : +{ +P(0, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I3(2, 1, 1, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); +} else { + P(1, 1) = I3(14, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 0, 1, 5); +} +} break; +case 255 : +{ +if (MDL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I3(14, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I3(14, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(14, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(14, 1, 1, 4, 1, 5); +} +} break; diff --git a/src/lq2x.h b/src/lq2x.h new file mode 100644 index 00000000..b0053556 --- /dev/null +++ b/src/lq2x.h @@ -0,0 +1,1284 @@ +case 0 : +case 2 : +case 4 : +case 6 : +case 8 : +case 12 : +case 16 : +case 20 : +case 24 : +case 28 : +case 32 : +case 34 : +case 36 : +case 38 : +case 40 : +case 44 : +case 48 : +case 52 : +case 56 : +case 60 : +case 64 : +case 66 : +case 68 : +case 70 : +case 96 : +case 98 : +case 100 : +case 102 : +case 128 : +case 130 : +case 132 : +case 134 : +case 136 : +case 140 : +case 144 : +case 148 : +case 152 : +case 156 : +case 160 : +case 162 : +case 164 : +case 166 : +case 168 : +case 172 : +case 176 : +case 180 : +case 184 : +case 188 : +case 192 : +case 194 : +case 196 : +case 198 : +case 224 : +case 226 : +case 228 : +case 230 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); +} break; +case 1 : +case 5 : +case 9 : +case 13 : +case 17 : +case 21 : +case 25 : +case 29 : +case 33 : +case 37 : +case 41 : +case 45 : +case 49 : +case 53 : +case 57 : +case 61 : +case 65 : +case 69 : +case 97 : +case 101 : +case 129 : +case 133 : +case 137 : +case 141 : +case 145 : +case 149 : +case 153 : +case 157 : +case 161 : +case 165 : +case 169 : +case 173 : +case 177 : +case 181 : +case 185 : +case 189 : +case 193 : +case 197 : +case 225 : +case 229 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + P3 = IC(1); +} break; +case 3 : +case 35 : +case 67 : +case 99 : +case 131 : +case 163 : +case 195 : +case 227 : +{ + P0 = IC(2); + P1 = IC(2); + P2 = IC(2); + P3 = IC(2); +} break; +case 7 : +case 39 : +case 71 : +case 103 : +case 135 : +case 167 : +case 199 : +case 231 : +{ + P0 = IC(3); + P1 = IC(3); + P2 = IC(3); + P3 = IC(3); +} break; +case 10 : +case 138 : +{ + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } +} break; +case 11 : +case 27 : +case 75 : +case 139 : +case 155 : +case 203 : +{ + P1 = IC(2); + P2 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 14 : +case 142 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + P1 = IC(0); + } else { + P0 = I332(1, 3, 0); + P1 = I31(0, 1); + } +} break; +case 15 : +case 143 : +case 207 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + P1 = IC(4); + } else { + P0 = I332(1, 3, 4); + P1 = I31(4, 1); + } +} break; +case 18 : +case 22 : +case 30 : +case 50 : +case 54 : +case 62 : +case 86 : +case 118 : +{ + P0 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 19 : +case 51 : +{ + P2 = IC(2); + P3 = IC(2); + if (MUR) { + P0 = IC(2); + P1 = IC(2); + } else { + P0 = I31(2, 1); + P1 = I332(1, 5, 2); + } +} break; +case 23 : +case 55 : +case 119 : +{ + P2 = IC(3); + P3 = IC(3); + if (MUR) { + P0 = IC(3); + P1 = IC(3); + } else { + P0 = I31(3, 1); + P1 = I332(1, 5, 3); + } +} break; +case 26 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 31 : +case 95 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 42 : +case 170 : +{ + P1 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + P2 = IC(0); + } else { + P0 = I332(1, 3, 0); + P2 = I31(0, 3); + } +} break; +case 43 : +case 171 : +case 187 : +{ + P1 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + P2 = IC(2); + } else { + P0 = I332(1, 3, 2); + P2 = I31(2, 3); + } +} break; +case 46 : +case 174 : +{ + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 47 : +case 175 : +{ + P1 = IC(4); + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 58 : +case 154 : +case 186 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 59 : +{ + P2 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 63 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 72 : +case 76 : +case 104 : +case 106 : +case 108 : +case 110 : +case 120 : +case 124 : +{ + P0 = IC(0); + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } +} break; +case 73 : +case 77 : +case 105 : +case 109 : +case 125 : +{ + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P0 = IC(1); + P2 = IC(1); + } else { + P0 = I31(1, 3); + P2 = I332(3, 7, 1); + } +} break; +case 74 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } +} break; +case 78 : +case 202 : +case 206 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 79 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 80 : +case 208 : +case 210 : +case 216 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 81 : +case 209 : +case 217 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I211(1, 5, 7); + } +} break; +case 82 : +case 214 : +case 222 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 83 : +case 115 : +{ + P0 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I611(2, 5, 7); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 84 : +case 212 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P1 = IC(0); + P3 = IC(0); + } else { + P1 = I31(0, 5); + P3 = I332(5, 7, 0); + } +} break; +case 85 : +case 213 : +case 221 : +{ + P0 = IC(1); + P2 = IC(1); + if (MDR) { + P1 = IC(1); + P3 = IC(1); + } else { + P1 = I31(1, 5); + P3 = I332(5, 7, 1); + } +} break; +case 87 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I611(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I211(3, 1, 5); + } +} break; +case 88 : +case 248 : +case 250 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 89 : +case 93 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I611(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 90 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 91 : +{ + if (MDL) { + P2 = IC(2); + } else { + P2 = I611(2, 3, 7); + } + if (MDR) { + P3 = IC(2); + } else { + P3 = I611(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 92 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } +} break; +case 94 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 107 : +case 123 : +{ + P1 = IC(2); + P3 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I211(2, 3, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 111 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 112 : +case 240 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDR) { + P2 = IC(0); + P3 = IC(0); + } else { + P2 = I31(0, 7); + P3 = I332(5, 7, 0); + } +} break; +case 113 : +case 241 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDR) { + P2 = IC(1); + P3 = IC(1); + } else { + P2 = I31(1, 7); + P3 = I332(5, 7, 1); + } +} break; +case 114 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 116 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } +} break; +case 117 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 121 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I211(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 122 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 126 : +{ + P0 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 127 : +{ + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 146 : +case 150 : +case 178 : +case 182 : +case 190 : +{ + P0 = IC(0); + P2 = IC(0); + if (MUR) { + P1 = IC(0); + P3 = IC(0); + } else { + P1 = I332(1, 5, 0); + P3 = I31(0, 5); + } +} break; +case 147 : +case 179 : +{ + P0 = IC(2); + P2 = IC(2); + P3 = IC(2); + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 151 : +case 183 : +{ + P0 = IC(3); + P2 = IC(3); + P3 = IC(3); + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 158 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 159 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 191 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 200 : +case 204 : +case 232 : +case 236 : +case 238 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + P3 = IC(0); + } else { + P2 = I332(3, 7, 0); + P3 = I31(0, 7); + } +} break; +case 201 : +case 205 : +{ + P0 = IC(1); + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I611(1, 3, 7); + } +} break; +case 211 : +{ + P0 = IC(2); + P1 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } +} break; +case 215 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I211(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 218 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 219 : +{ + P1 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 220 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 223 : +{ + P2 = IC(4); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 233 : +case 237 : +{ + P0 = IC(1); + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } +} break; +case 234 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 235 : +{ + P1 = IC(2); + P3 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I1411(2, 3, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 239 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 242 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 243 : +{ + P0 = IC(2); + P1 = IC(2); + if (MDR) { + P2 = IC(2); + P3 = IC(2); + } else { + P2 = I31(2, 7); + P3 = I332(5, 7, 2); + } +} break; +case 244 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } +} break; +case 245 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I1411(1, 5, 7); + } +} break; +case 246 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 247 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I1411(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 249 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I211(1, 5, 7); + } +} break; +case 251 : +{ + P1 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I1411(2, 3, 7); + } + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 252 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } +} break; +case 253 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I1411(1, 5, 7); + } +} break; +case 254 : +{ + P0 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 255 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; diff --git a/src/lq2x3.dat b/src/lq2x3.dat new file mode 100644 index 00000000..a31a2d1f --- /dev/null +++ b/src/lq2x3.dat @@ -0,0 +1,1574 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is derived from the hq effect made by Maxim Stepin + */ + +case 0 : +case 2 : +case 4 : +case 6 : +case 8 : +case 12 : +case 16 : +case 20 : +case 24 : +case 28 : +case 32 : +case 34 : +case 36 : +case 38 : +case 40 : +case 44 : +case 48 : +case 52 : +case 56 : +case 60 : +case 64 : +case 66 : +case 68 : +case 70 : +case 96 : +case 98 : +case 100 : +case 102 : +case 128 : +case 130 : +case 132 : +case 134 : +case 136 : +case 140 : +case 144 : +case 148 : +case 152 : +case 156 : +case 160 : +case 162 : +case 164 : +case 166 : +case 168 : +case 172 : +case 176 : +case 180 : +case 184 : +case 188 : +case 192 : +case 194 : +case 196 : +case 198 : +case 224 : +case 226 : +case 228 : +case 230 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +} break; +case 1 : +case 5 : +case 9 : +case 13 : +case 17 : +case 21 : +case 25 : +case 29 : +case 33 : +case 37 : +case 41 : +case 45 : +case 49 : +case 53 : +case 57 : +case 61 : +case 65 : +case 69 : +case 97 : +case 101 : +case 129 : +case 133 : +case 137 : +case 141 : +case 145 : +case 149 : +case 153 : +case 157 : +case 161 : +case 165 : +case 169 : +case 173 : +case 177 : +case 181 : +case 185 : +case 189 : +case 193 : +case 197 : +case 225 : +case 229 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(1, 2) = I1(1); +} break; +case 3 : +case 35 : +case 67 : +case 99 : +case 131 : +case 163 : +case 195 : +case 227 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +} break; +case 7 : +case 39 : +case 71 : +case 103 : +case 135 : +case 167 : +case 199 : +case 231 : +{ +P(0, 0) = I1(3); +P(1, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +} break; +case 10 : +case 138 : +{ +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(6, 5, 5, 0, 1, 3); + P(1, 0) = I2(15, 1, 0, 1); + P(0, 1) = I2(15, 1, 0, 3); +} +} break; +case 11 : +case 27 : +case 75 : +case 139 : +case 155 : +case 203 : +{ +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(1, 0) = I2(15, 1, 2, 1); + P(0, 1) = I2(15, 1, 2, 3); +} +} break; +case 14 : +case 142 : +{ +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(10, 5, 1, 1, 3, 0); + P(1, 0) = I2(5, 3, 0, 1); + P(0, 1) = I2(13, 3, 0, 3); +} +} break; +case 15 : +case 143 : +case 207 : +{ +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 5, 1, 1, 3, 4); + P(1, 0) = I2(5, 3, 4, 1); + P(0, 1) = I2(13, 3, 4, 3); +} +} break; +case 18 : +case 22 : +case 30 : +case 50 : +case 54 : +case 62 : +case 86 : +case 118 : +{ +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(6, 5, 5, 0, 1, 5); + P(1, 1) = I2(15, 1, 0, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +if (MUR) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(0, 0) = I2(5, 3, 2, 1); + P(1, 0) = I3(10, 5, 1, 1, 5, 2); + P(1, 1) = I2(13, 3, 2, 5); +} +} break; +case 23 : +case 55 : +case 119 : +{ +P(0, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +if (MUR) { + P(0, 0) = I1(3); + P(1, 0) = I1(3); + P(1, 1) = I1(3); +} else { + P(0, 0) = I2(5, 3, 3, 1); + P(1, 0) = I3(10, 5, 1, 1, 5, 3); + P(1, 1) = I2(13, 3, 3, 5); +} +} break; +case 26 : +{ +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(6, 5, 5, 0, 1, 3); + P(0, 1) = I2(15, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(6, 5, 5, 0, 1, 5); + P(1, 1) = I2(15, 1, 0, 5); +} +} break; +case 31 : +case 95 : +{ +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 42 : +case 170 : +{ +P(1, 1) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(0, 1) = I1(0); + P(0, 2) = I1(0); +} else { + P(0, 0) = I3(7, 5, 4, 1, 3, 0); + P(1, 0) = I2(15, 1, 0, 1); + P(0, 1) = I2(1, 1, 0, 3); + P(0, 2) = I2(13, 3, 0, 3); +} +} break; +case 43 : +case 171 : +case 187 : +{ +P(1, 1) = I1(2); +P(1, 2) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); + P(0, 1) = I1(2); + P(0, 2) = I1(2); +} else { + P(0, 0) = I3(7, 5, 4, 1, 3, 2); + P(1, 0) = I2(15, 1, 2, 1); + P(0, 1) = I2(1, 1, 2, 3); + P(0, 2) = I2(13, 3, 2, 3); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 58 : +case 154 : +case 186 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 59 : +{ +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +if (!MUR) { + P(1, 0) = I3(10, 3, 3, 2, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I2(15, 1, 2, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I1(2); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(0, 1) = I2(15, 1, 2, 3); +} +} break; +case 63 : +{ +P(0, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 72 : +case 76 : +case 104 : +case 106 : +case 108 : +case 110 : +case 120 : +case 124 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); + P(1, 2) = I2(15, 1, 0, 7); +} +} break; +case 73 : +case 77 : +case 105 : +case 109 : +case 125 : +{ +P(1, 0) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 0) = I1(1); + P(0, 1) = I1(1); + P(0, 2) = I1(1); + P(1, 2) = I1(1); +} else { + P(0, 0) = I2(13, 3, 1, 3); + P(0, 1) = I2(1, 1, 1, 3); + P(0, 2) = I3(7, 5, 4, 7, 3, 1); + P(1, 2) = I2(15, 1, 1, 7); +} +} break; +case 74 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 2) = I3(6, 5, 5, 0, 3, 7); + P(1, 2) = I2(15, 1, 0, 7); +} +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); +} else { + P(0, 0) = I3(6, 5, 5, 0, 1, 3); + P(1, 0) = I2(15, 1, 0, 1); +} +} break; +case 78 : +case 202 : +case 206 : +{ +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +if (MDL) { + P(0, 2) = I1(0); +} else { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +} break; +case 79 : +{ +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(1, 0) = I2(15, 1, 4, 1); + P(0, 1) = I2(15, 1, 4, 3); +} +} break; +case 80 : +case 208 : +case 210 : +case 216 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(0, 2) = I2(15, 1, 0, 7); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +} break; +case 81 : +case 209 : +case 217 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 1) = I1(1); + P(0, 2) = I1(1); + P(1, 2) = I1(1); +} else { + P(1, 1) = I2(15, 1, 1, 5); + P(0, 2) = I2(15, 1, 1, 7); + P(1, 2) = I3(6, 5, 5, 1, 5, 7); +} +} break; +case 82 : +case 214 : +case 222 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDR) { + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 2) = I2(15, 1, 0, 7); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(6, 5, 5, 0, 1, 5); +} +} break; +case 83 : +case 115 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +if (MDR) { + P(1, 2) = I1(2); +} else { + P(1, 2) = I3(10, 3, 3, 2, 5, 7); +} +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(10, 3, 3, 2, 1, 5); +} +} break; +case 84 : +case 212 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 0) = I2(13, 3, 0, 5); + P(1, 1) = I2(1, 1, 0, 5); + P(0, 2) = I2(15, 1, 0, 7); + P(1, 2) = I3(7, 5, 4, 7, 5, 0); +} +} break; +case 85 : +case 213 : +case 221 : +{ +P(0, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 0) = I1(1); + P(1, 1) = I1(1); + P(0, 2) = I1(1); + P(1, 2) = I1(1); +} else { + P(1, 0) = I2(13, 3, 1, 5); + P(1, 1) = I2(1, 1, 1, 5); + P(0, 2) = I2(15, 1, 1, 7); + P(1, 2) = I3(7, 5, 4, 7, 5, 1); +} +} break; +case 87 : +{ +P(0, 1) = I1(3); +P(0, 2) = I1(3); +if (MDR) { + P(1, 2) = I1(3); +} else { + P(1, 2) = I3(10, 3, 3, 3, 5, 7); +} +if (MUR) { + P(0, 0) = I1(3); + P(1, 0) = I1(3); + P(1, 1) = I1(3); +} else { + P(0, 0) = I2(15, 1, 3, 1); + P(1, 0) = I3(6, 5, 5, 3, 1, 5); + P(1, 1) = I2(15, 1, 3, 5); +} +} break; +case 88 : +case 248 : +case 250 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); +} +if (MDR) { + P(1, 1) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +} break; +case 89 : +case 93 : +case 253 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 2) = I1(1); +} else { + P(0, 2) = I3(10, 3, 3, 1, 3, 7); +} +if (MDR) { + P(1, 2) = I1(1); +} else { + P(1, 2) = I3(10, 3, 3, 1, 5, 7); +} +} break; +case 90 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); +} else { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 91 : +{ +P(1, 1) = I1(2); +if (!MUR) { + P(1, 0) = I3(10, 3, 3, 2, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I2(15, 1, 2, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I1(2); +} +if (MDL) { + P(0, 2) = I1(2); +} else { + P(0, 2) = I3(10, 3, 3, 2, 3, 7); +} +if (MDR) { + P(1, 2) = I1(2); +} else { + P(1, 2) = I3(10, 3, 3, 2, 5, 7); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(0, 1) = I2(15, 1, 2, 3); +} +} break; +case 92 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); +} else { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +} break; +case 94 : +{ +P(0, 1) = I1(0); +if (!MUL) { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I2(15, 1, 0, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I1(0); +} +if (MDL) { + P(0, 2) = I1(0); +} else { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(6, 5, 5, 0, 1, 5); + P(1, 1) = I2(15, 1, 0, 5); +} +} break; +case 107 : +case 123 : +{ +P(0, 1) = I1(2); +P(1, 1) = I1(2); +if (MDL) { + P(0, 2) = I1(2); + P(1, 2) = I1(2); +} else { + P(0, 2) = I3(6, 5, 5, 2, 3, 7); + P(1, 2) = I2(15, 1, 2, 7); +} +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(1, 0) = I2(15, 1, 2, 1); +} +} break; +case 111 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I2(15, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 112 : +case 240 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(13, 3, 0, 5); + P(0, 2) = I2(9, 7, 0, 7); + P(1, 2) = I3(10, 5, 1, 7, 5, 0); +} +} break; +case 113 : +case 241 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDR) { + P(1, 1) = I1(1); + P(0, 2) = I1(1); + P(1, 2) = I1(1); +} else { + P(1, 1) = I2(13, 3, 1, 5); + P(0, 2) = I2(9, 7, 1, 7); + P(1, 2) = I3(10, 5, 1, 7, 5, 1); +} +} break; +case 114 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 116 : +case 244 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +} break; +case 117 : +case 245 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +if (MDR) { + P(1, 2) = I1(1); +} else { + P(1, 2) = I3(10, 3, 3, 1, 5, 7); +} +} break; +case 121 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(1, 1) = I1(1); +if (!MDR) { + P(1, 2) = I3(10, 3, 3, 1, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I2(15, 1, 1, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I1(1); +} +if (MDL) { + P(0, 1) = I1(1); + P(0, 2) = I1(1); +} else { + P(0, 1) = I2(15, 1, 1, 3); + P(0, 2) = I3(6, 5, 5, 1, 3, 7); +} +} break; +case 122 : +{ +P(1, 1) = I1(0); +if (!MDR) { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I2(15, 1, 0, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I1(0); +} +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 126 : +{ +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); + P(1, 2) = I2(15, 1, 0, 7); +} +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(6, 5, 5, 0, 1, 5); + P(1, 1) = I2(15, 1, 0, 5); +} +} break; +case 127 : +{ +if (!MUR) { + P(1, 0) = I3(6, 5, 5, 4, 1, 5); +} +if ((MUR && !MUL)) { + P(1, 0) = I2(15, 1, 4, 1); +} +if ((MUR && MUL)) { + P(1, 0) = I1(4); +} +if (MDL) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I3(6, 5, 5, 4, 3, 7); + P(1, 2) = I2(15, 1, 4, 7); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 1) = I1(4); +} else { + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 146 : +case 150 : +case 178 : +case 182 : +case 190 : +{ +P(0, 1) = I1(0); +P(0, 2) = I1(0); +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(1, 1) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(7, 5, 4, 1, 5, 0); + P(1, 1) = I2(1, 1, 0, 5); + P(1, 2) = I2(13, 3, 0, 5); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +if (MUR) { + P(1, 0) = I1(2); +} else { + P(1, 0) = I3(10, 3, 3, 2, 1, 5); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(10, 3, 3, 3, 1, 5); +} +} break; +case 158 : +{ +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +if (!MUL) { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I2(15, 1, 0, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I1(0); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(6, 5, 5, 0, 1, 5); + P(1, 1) = I2(15, 1, 0, 5); +} +} break; +case 159 : +{ +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 191 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; +case 200 : +case 204 : +case 232 : +case 236 : +case 238 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 1) = I2(13, 3, 0, 3); + P(0, 2) = I3(10, 5, 1, 7, 3, 0); + P(1, 2) = I2(9, 7, 0, 7); +} +} break; +case 201 : +case 205 : +case 233 : +case 237 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(1, 2) = I1(1); +if (MDL) { + P(0, 2) = I1(1); +} else { + P(0, 2) = I3(10, 3, 3, 1, 3, 7); +} +} break; +case 211 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +if (MDR) { + P(1, 1) = I1(2); + P(0, 2) = I1(2); + P(1, 2) = I1(2); +} else { + P(1, 1) = I2(15, 1, 2, 5); + P(0, 2) = I2(15, 1, 2, 7); + P(1, 2) = I3(6, 5, 5, 2, 5, 7); +} +} break; +case 215 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +if (MDR) { + P(0, 2) = I1(3); + P(1, 2) = I1(3); +} else { + P(0, 2) = I2(15, 1, 3, 7); + P(1, 2) = I3(6, 5, 5, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(10, 3, 3, 3, 1, 5); +} +} break; +case 218 : +{ +P(0, 1) = I1(0); +if (!MDL) { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I2(15, 1, 0, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I1(0); +} +if (MDR) { + P(1, 1) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 219 : +{ +if (MDR) { + P(1, 1) = I1(2); + P(0, 2) = I1(2); + P(1, 2) = I1(2); +} else { + P(1, 1) = I2(15, 1, 2, 5); + P(0, 2) = I2(15, 1, 2, 7); + P(1, 2) = I3(6, 5, 5, 2, 5, 7); +} +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(1, 0) = I2(15, 1, 2, 1); + P(0, 1) = I2(15, 1, 2, 3); +} +} break; +case 220 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +if (!MDL) { + P(0, 2) = I3(10, 3, 3, 0, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I2(15, 1, 0, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I1(0); +} +if (MDR) { + P(1, 1) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +} break; +case 223 : +{ +if (!MUL) { + P(0, 0) = I3(6, 5, 5, 4, 1, 3); +} +if ((!MUR && MUL)) { + P(0, 0) = I2(15, 1, 4, 1); +} +if ((MUR && MUL)) { + P(0, 0) = I1(4); +} +if (MDR) { + P(0, 2) = I1(4); + P(1, 2) = I1(4); +} else { + P(0, 2) = I2(15, 1, 4, 7); + P(1, 2) = I3(6, 5, 5, 4, 5, 7); +} +if (MUL) { + P(0, 1) = I1(4); +} else { + P(0, 1) = I2(15, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); + P(1, 1) = I2(15, 1, 4, 5); +} +} break; +case 234 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); + P(1, 2) = I2(15, 1, 0, 7); +} +if (MUL) { + P(0, 0) = I1(0); +} else { + P(0, 0) = I3(10, 3, 3, 0, 1, 3); +} +} break; +case 235 : +{ +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(1, 2) = I1(2); +if (MDL) { + P(0, 2) = I1(2); +} else { + P(0, 2) = I3(10, 3, 3, 2, 3, 7); +} +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(1, 0) = I2(15, 1, 2, 1); +} +} break; +case 239 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +} break; +case 242 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +if (MDR) { + P(1, 1) = I1(0); + P(0, 2) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(0, 2) = I2(15, 1, 0, 7); + P(1, 2) = I3(6, 5, 5, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); +} else { + P(1, 0) = I3(10, 3, 3, 0, 1, 5); +} +} break; +case 243 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +if (MDR) { + P(1, 1) = I1(2); + P(0, 2) = I1(2); + P(1, 2) = I1(2); +} else { + P(1, 1) = I2(13, 3, 2, 5); + P(0, 2) = I2(9, 7, 2, 7); + P(1, 2) = I3(10, 5, 1, 7, 5, 2); +} +} break; +case 246 : +{ +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(6, 5, 5, 0, 1, 5); +} +} break; +case 247 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +if (MDR) { + P(1, 2) = I1(3); +} else { + P(1, 2) = I3(10, 3, 3, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(10, 3, 3, 3, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +if (MDL) { + P(0, 2) = I1(1); +} else { + P(0, 2) = I3(10, 3, 3, 1, 3, 7); +} +if (MDR) { + P(1, 1) = I1(1); + P(1, 2) = I1(1); +} else { + P(1, 1) = I2(15, 1, 1, 5); + P(1, 2) = I3(6, 5, 5, 1, 5, 7); +} +} break; +case 251 : +{ +if (!MDR) { + P(1, 2) = I3(6, 5, 5, 2, 5, 7); +} +if ((MDR && !MDL)) { + P(1, 2) = I2(15, 1, 2, 7); +} +if ((MDR && MDL)) { + P(1, 2) = I1(2); +} +if (MDL) { + P(0, 1) = I1(2); + P(0, 2) = I1(2); +} else { + P(0, 1) = I2(15, 1, 2, 3); + P(0, 2) = I3(10, 3, 3, 2, 3, 7); +} +if (MDR) { + P(1, 1) = I1(2); +} else { + P(1, 1) = I2(15, 1, 2, 5); +} +if (MUL) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); +} else { + P(0, 0) = I3(6, 5, 5, 2, 1, 3); + P(1, 0) = I2(15, 1, 2, 1); +} +} break; +case 252 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 1) = I1(0); + P(0, 2) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); + P(0, 2) = I3(6, 5, 5, 0, 3, 7); +} +if (MDR) { + P(1, 2) = I1(0); +} else { + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +} break; +case 254 : +{ +if (!MDL) { + P(0, 2) = I3(6, 5, 5, 0, 3, 7); +} +if ((!MDR && MDL)) { + P(0, 2) = I2(15, 1, 0, 7); +} +if ((MDR && MDL)) { + P(0, 2) = I1(0); +} +if (MDL) { + P(0, 1) = I1(0); +} else { + P(0, 1) = I2(15, 1, 0, 3); +} +if (MDR) { + P(1, 1) = I1(0); + P(1, 2) = I1(0); +} else { + P(1, 1) = I2(15, 1, 0, 5); + P(1, 2) = I3(10, 3, 3, 0, 5, 7); +} +if (MUR) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); +} else { + P(0, 0) = I2(15, 1, 0, 1); + P(1, 0) = I3(6, 5, 5, 0, 1, 5); +} +} break; +case 255 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +if (MDL) { + P(0, 2) = I1(4); +} else { + P(0, 2) = I3(10, 3, 3, 4, 3, 7); +} +if (MDR) { + P(1, 2) = I1(4); +} else { + P(1, 2) = I3(10, 3, 3, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(10, 3, 3, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(10, 3, 3, 4, 1, 5); +} +} break; diff --git a/src/lq2x4.dat b/src/lq2x4.dat new file mode 100644 index 00000000..d34c5d55 --- /dev/null +++ b/src/lq2x4.dat @@ -0,0 +1,1746 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * This effect is derived from the hq effect made by Maxim Stepin + */ + +case 0 : +case 2 : +case 4 : +case 6 : +case 8 : +case 12 : +case 16 : +case 20 : +case 24 : +case 28 : +case 32 : +case 34 : +case 36 : +case 38 : +case 40 : +case 44 : +case 48 : +case 52 : +case 56 : +case 60 : +case 64 : +case 66 : +case 68 : +case 70 : +case 96 : +case 98 : +case 100 : +case 102 : +case 128 : +case 130 : +case 132 : +case 134 : +case 136 : +case 140 : +case 144 : +case 148 : +case 152 : +case 156 : +case 160 : +case 162 : +case 164 : +case 166 : +case 168 : +case 172 : +case 176 : +case 180 : +case 184 : +case 188 : +case 192 : +case 194 : +case 196 : +case 198 : +case 224 : +case 226 : +case 228 : +case 230 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +} break; +case 1 : +case 5 : +case 9 : +case 13 : +case 17 : +case 21 : +case 25 : +case 29 : +case 33 : +case 37 : +case 41 : +case 45 : +case 49 : +case 53 : +case 57 : +case 61 : +case 65 : +case 69 : +case 97 : +case 101 : +case 129 : +case 133 : +case 137 : +case 141 : +case 145 : +case 149 : +case 153 : +case 157 : +case 161 : +case 165 : +case 169 : +case 173 : +case 177 : +case 181 : +case 185 : +case 189 : +case 193 : +case 197 : +case 225 : +case 229 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(1, 2) = I1(1); +P(0, 3) = I1(1); +P(1, 3) = I1(1); +} break; +case 3 : +case 35 : +case 67 : +case 99 : +case 131 : +case 163 : +case 195 : +case 227 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(0, 3) = I1(2); +P(1, 3) = I1(2); +} break; +case 7 : +case 39 : +case 71 : +case 103 : +case 135 : +case 167 : +case 199 : +case 231 : +{ +P(0, 0) = I1(3); +P(1, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +P(0, 3) = I1(3); +P(1, 3) = I1(3); +} break; +case 10 : +case 138 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 1, 0, 3); + P(0, 1) = I2(3, 1, 0, 3); +} +} break; +case 11 : +case 27 : +case 75 : +case 139 : +case 155 : +case 203 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(0, 3) = I1(2); +P(1, 3) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 14 : +case 142 : +{ +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(1, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I2(9, 7, 1, 3); + P(1, 0) = I2(1, 1, 0, 1); + P(0, 1) = I3(8, 5, 3, 0, 3, 1); +} +} break; +case 15 : +case 143 : +case 207 : +{ +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(1, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I2(9, 7, 1, 3); + P(1, 0) = I2(1, 1, 1, 4); + P(0, 1) = I3(8, 5, 3, 4, 3, 1); +} +} break; +case 18 : +case 22 : +case 30 : +case 50 : +case 54 : +case 62 : +case 86 : +case 118 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 19 : +case 51 : +{ +P(0, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(0, 3) = I1(2); +P(1, 3) = I1(2); +if (MUR) { + P(0, 0) = I1(2); + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(0, 0) = I2(1, 1, 1, 2); + P(1, 0) = I2(9, 7, 1, 5); + P(1, 1) = I3(8, 5, 3, 2, 5, 1); +} +} break; +case 23 : +case 55 : +case 119 : +{ +P(0, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +P(0, 3) = I1(3); +P(1, 3) = I1(3); +if (MUR) { + P(0, 0) = I1(3); + P(1, 0) = I1(3); + P(1, 1) = I1(3); +} else { + P(0, 0) = I2(1, 1, 1, 3); + P(1, 0) = I2(9, 7, 1, 5); + P(1, 1) = I3(8, 5, 3, 3, 5, 1); +} +} break; +case 26 : +{ +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 1, 0, 3); + P(0, 1) = I2(3, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 31 : +case 95 : +{ +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 42 : +case 170 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 0) = I3(4, 3, 1, 1, 3, 0); + P(0, 1) = I3(3, 3, 2, 0, 3, 1); + P(0, 2) = I2(5, 3, 0, 3); + P(0, 3) = I2(7, 1, 0, 3); +} +} break; +case 43 : +case 171 : +case 187 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(1, 2) = I1(2); +P(1, 3) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); + P(0, 2) = I1(2); + P(0, 3) = I1(2); +} else { + P(0, 0) = I3(4, 3, 1, 1, 3, 2); + P(0, 1) = I3(3, 3, 2, 2, 3, 1); + P(0, 2) = I2(5, 3, 2, 3); + P(0, 3) = I2(7, 1, 2, 3); +} +} break; +case 46 : +case 174 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +} break; +case 47 : +case 175 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 58 : +case 154 : +case 186 : +{ +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 59 : +{ +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(0, 3) = I1(2); +P(1, 3) = I1(2); +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +if (MUR) { + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(1, 0) = I3(5, 2, 1, 2, 1, 5); + P(1, 1) = I2(7, 1, 2, 5); +} +} break; +case 63 : +{ +P(0, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 72 : +case 76 : +case 104 : +case 106 : +case 108 : +case 110 : +case 120 : +case 124 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +} break; +case 73 : +case 77 : +case 105 : +case 109 : +case 125 : +{ +P(1, 0) = I1(1); +P(1, 1) = I1(1); +P(1, 2) = I1(1); +P(1, 3) = I1(1); +if (MDL) { + P(0, 0) = I1(1); + P(0, 1) = I1(1); + P(0, 2) = I1(1); + P(0, 3) = I1(1); +} else { + P(0, 0) = I2(7, 1, 1, 3); + P(0, 1) = I2(5, 3, 1, 3); + P(0, 2) = I3(3, 3, 2, 1, 3, 7); + P(0, 3) = I3(4, 3, 1, 7, 3, 1); +} +} break; +case 74 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(2, 1, 1, 1, 0, 3); + P(0, 1) = I2(3, 1, 0, 3); +} +} break; +case 78 : +case 202 : +case 206 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +} break; +case 79 : +{ +P(1, 0) = I1(4); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +P(1, 3) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(7, 1, 4, 3); + P(0, 3) = I3(5, 2, 1, 4, 7, 3); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +} break; +case 80 : +case 208 : +case 210 : +case 216 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +} break; +case 81 : +case 209 : +case 217 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(0, 3) = I1(1); +if (MDR) { + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I2(3, 1, 1, 5); + P(1, 3) = I3(2, 1, 1, 7, 1, 5); +} +} break; +case 82 : +case 214 : +case 222 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 83 : +case 115 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +P(0, 2) = I1(2); +P(0, 3) = I1(2); +if (MDR) { + P(1, 2) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I2(7, 1, 2, 5); + P(1, 3) = I3(5, 2, 1, 2, 7, 5); +} +if (MUR) { + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(1, 0) = I3(5, 2, 1, 2, 1, 5); + P(1, 1) = I2(7, 1, 2, 5); +} +} break; +case 84 : +case 212 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 0) = I2(7, 1, 0, 5); + P(1, 1) = I2(5, 3, 0, 5); + P(1, 2) = I3(3, 3, 2, 0, 5, 7); + P(1, 3) = I3(4, 3, 1, 7, 5, 0); +} +} break; +case 85 : +case 213 : +case 221 : +{ +P(0, 0) = I1(1); +P(0, 1) = I1(1); +P(0, 2) = I1(1); +P(0, 3) = I1(1); +if (MDR) { + P(1, 0) = I1(1); + P(1, 1) = I1(1); + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 0) = I2(7, 1, 1, 5); + P(1, 1) = I2(5, 3, 1, 5); + P(1, 2) = I3(3, 3, 2, 1, 5, 7); + P(1, 3) = I3(4, 3, 1, 7, 5, 1); +} +} break; +case 87 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(0, 2) = I1(3); +P(0, 3) = I1(3); +if (MDR) { + P(1, 2) = I1(3); + P(1, 3) = I1(3); +} else { + P(1, 2) = I2(7, 1, 3, 5); + P(1, 3) = I3(5, 2, 1, 3, 7, 5); +} +if (MUR) { + P(1, 0) = I1(3); + P(1, 1) = I1(3); +} else { + P(1, 0) = I3(2, 1, 1, 1, 3, 5); + P(1, 1) = I2(3, 1, 3, 5); +} +} break; +case 88 : +case 248 : +case 250 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +} break; +case 89 : +case 93 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 2) = I1(1); + P(0, 3) = I1(1); +} else { + P(0, 2) = I2(7, 1, 1, 3); + P(0, 3) = I3(5, 2, 1, 1, 7, 3); +} +if (MDR) { + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I2(7, 1, 1, 5); + P(1, 3) = I3(5, 2, 1, 1, 7, 5); +} +} break; +case 90 : +{ +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 91 : +{ +if (MDL) { + P(0, 2) = I1(2); + P(0, 3) = I1(2); +} else { + P(0, 2) = I2(7, 1, 2, 3); + P(0, 3) = I3(5, 2, 1, 2, 7, 3); +} +if (MDR) { + P(1, 2) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I2(7, 1, 2, 5); + P(1, 3) = I3(5, 2, 1, 2, 7, 5); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +if (MUR) { + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(1, 0) = I3(5, 2, 1, 2, 1, 5); + P(1, 1) = I2(7, 1, 2, 5); +} +} break; +case 92 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +} break; +case 94 : +{ +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 107 : +case 123 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(1, 2) = I1(2); +P(1, 3) = I1(2); +if (MDL) { + P(0, 2) = I1(2); + P(0, 3) = I1(2); +} else { + P(0, 2) = I2(3, 1, 2, 3); + P(0, 3) = I3(2, 1, 1, 7, 2, 3); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 111 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(1, 2) = I1(4); +P(1, 3) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 112 : +case 240 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(0, 3) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I3(8, 5, 3, 0, 5, 7); + P(0, 3) = I2(1, 1, 0, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 113 : +case 241 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +if (MDR) { + P(1, 2) = I1(1); + P(0, 3) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I3(8, 5, 3, 1, 5, 7); + P(0, 3) = I2(1, 1, 1, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 114 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 116 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +} break; +case 117 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(0, 3) = I1(1); +if (MDR) { + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I2(7, 1, 1, 5); + P(1, 3) = I3(5, 2, 1, 1, 7, 5); +} +} break; +case 121 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +if (MDL) { + P(0, 2) = I1(1); + P(0, 3) = I1(1); +} else { + P(0, 2) = I2(3, 1, 1, 3); + P(0, 3) = I3(2, 1, 1, 7, 1, 3); +} +if (MDR) { + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I2(7, 1, 1, 5); + P(1, 3) = I3(5, 2, 1, 1, 7, 5); +} +} break; +case 122 : +{ +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(7, 1, 0, 5); + P(1, 3) = I3(5, 2, 1, 0, 7, 5); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 126 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 127 : +{ +P(0, 1) = I1(4); +P(1, 2) = I1(4); +P(1, 3) = I1(4); +if (MDL) { + P(0, 2) = I1(4); + P(0, 3) = I1(4); +} else { + P(0, 2) = I2(3, 1, 4, 3); + P(0, 3) = I3(2, 1, 1, 7, 3, 4); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); + P(1, 1) = I1(4); +} else { + P(1, 0) = I3(2, 1, 1, 1, 4, 5); + P(1, 1) = I2(3, 1, 4, 5); +} +} break; +case 146 : +case 150 : +case 178 : +case 182 : +case 190 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 0) = I3(4, 3, 1, 1, 5, 0); + P(1, 1) = I3(3, 3, 2, 0, 5, 1); + P(1, 2) = I2(5, 3, 0, 5); + P(1, 3) = I2(7, 1, 0, 5); +} +} break; +case 147 : +case 179 : +{ +P(0, 0) = I1(2); +P(0, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(0, 3) = I1(2); +P(1, 3) = I1(2); +if (MUR) { + P(1, 0) = I1(2); + P(1, 1) = I1(2); +} else { + P(1, 0) = I3(5, 2, 1, 2, 1, 5); + P(1, 1) = I2(7, 1, 2, 5); +} +} break; +case 151 : +case 183 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +P(0, 3) = I1(3); +P(1, 3) = I1(3); +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(6, 1, 1, 3, 1, 5); +} +} break; +case 158 : +{ +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +P(1, 3) = I1(0); +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 159 : +{ +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 191 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(0, 3) = I1(4); +P(1, 3) = I1(4); +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 200 : +case 204 : +case 232 : +case 236 : +case 238 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); + P(1, 3) = I1(0); +} else { + P(0, 2) = I3(8, 5, 3, 0, 3, 7); + P(0, 3) = I2(9, 7, 7, 3); + P(1, 3) = I2(1, 1, 0, 7); +} +} break; +case 201 : +case 205 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(1, 2) = I1(1); +P(1, 3) = I1(1); +if (MDL) { + P(0, 2) = I1(1); + P(0, 3) = I1(1); +} else { + P(0, 2) = I2(7, 1, 1, 3); + P(0, 3) = I3(5, 2, 1, 1, 7, 3); +} +} break; +case 211 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(0, 3) = I1(2); +if (MDR) { + P(1, 2) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I2(3, 1, 2, 5); + P(1, 3) = I3(2, 1, 1, 7, 2, 5); +} +} break; +case 215 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(0, 3) = I1(3); +if (MDR) { + P(1, 2) = I1(3); + P(1, 3) = I1(3); +} else { + P(1, 2) = I2(3, 1, 3, 5); + P(1, 3) = I3(2, 1, 1, 7, 3, 5); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(6, 1, 1, 3, 1, 5); +} +} break; +case 218 : +{ +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 219 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(0, 3) = I1(2); +if (MDR) { + P(1, 2) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I2(3, 1, 2, 5); + P(1, 3) = I3(2, 1, 1, 7, 2, 5); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 220 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(7, 1, 0, 3); + P(0, 3) = I3(5, 2, 1, 0, 7, 3); +} +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +} break; +case 223 : +{ +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(0, 3) = I1(4); +if (MDR) { + P(1, 2) = I1(4); + P(1, 3) = I1(4); +} else { + P(1, 2) = I2(3, 1, 4, 5); + P(1, 3) = I3(2, 1, 1, 7, 4, 5); +} +if (MUL) { + P(0, 0) = I1(4); + P(0, 1) = I1(4); +} else { + P(0, 0) = I3(2, 1, 1, 1, 3, 4); + P(0, 1) = I2(3, 1, 4, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; +case 233 : +case 237 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(1, 2) = I1(1); +P(1, 3) = I1(1); +if (MDL) { + P(0, 3) = I1(1); +} else { + P(0, 3) = I3(6, 1, 1, 1, 3, 7); +} +} break; +case 234 : +{ +P(1, 0) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +P(1, 3) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MUL) { + P(0, 0) = I1(0); + P(0, 1) = I1(0); +} else { + P(0, 0) = I3(5, 2, 1, 0, 1, 3); + P(0, 1) = I2(7, 1, 0, 3); +} +} break; +case 235 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +P(1, 2) = I1(2); +P(1, 3) = I1(2); +if (MDL) { + P(0, 3) = I1(2); +} else { + P(0, 3) = I3(6, 1, 1, 2, 3, 7); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 239 : +{ +P(1, 0) = I1(4); +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +P(1, 3) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +} break; +case 242 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 2) = I1(0); + P(1, 3) = I1(0); +} else { + P(1, 2) = I2(3, 1, 0, 5); + P(1, 3) = I3(2, 1, 1, 7, 0, 5); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(5, 2, 1, 0, 1, 5); + P(1, 1) = I2(7, 1, 0, 5); +} +} break; +case 243 : +{ +P(0, 0) = I1(2); +P(1, 0) = I1(2); +P(0, 1) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +if (MDR) { + P(1, 2) = I1(2); + P(0, 3) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I3(8, 5, 3, 2, 5, 7); + P(0, 3) = I2(1, 1, 2, 7); + P(1, 3) = I2(9, 7, 7, 5); +} +} break; +case 244 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 3) = I1(0); +} else { + P(1, 3) = I3(6, 1, 1, 0, 5, 7); +} +} break; +case 245 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(1, 2) = I1(1); +P(0, 3) = I1(1); +if (MDR) { + P(1, 3) = I1(1); +} else { + P(1, 3) = I3(6, 1, 1, 1, 5, 7); +} +} break; +case 246 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(0, 2) = I1(0); +P(1, 2) = I1(0); +P(0, 3) = I1(0); +if (MDR) { + P(1, 3) = I1(0); +} else { + P(1, 3) = I3(6, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 247 : +{ +P(0, 0) = I1(3); +P(0, 1) = I1(3); +P(1, 1) = I1(3); +P(0, 2) = I1(3); +P(1, 2) = I1(3); +P(0, 3) = I1(3); +if (MDR) { + P(1, 3) = I1(3); +} else { + P(1, 3) = I3(6, 1, 1, 3, 5, 7); +} +if (MUR) { + P(1, 0) = I1(3); +} else { + P(1, 0) = I3(6, 1, 1, 3, 1, 5); +} +} break; +case 249 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +if (MDL) { + P(0, 3) = I1(1); +} else { + P(0, 3) = I3(6, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 2) = I1(1); + P(1, 3) = I1(1); +} else { + P(1, 2) = I2(3, 1, 1, 5); + P(1, 3) = I3(2, 1, 1, 7, 1, 5); +} +} break; +case 251 : +{ +P(1, 0) = I1(2); +P(1, 1) = I1(2); +P(0, 2) = I1(2); +if (MDL) { + P(0, 3) = I1(2); +} else { + P(0, 3) = I3(6, 1, 1, 2, 3, 7); +} +if (MDR) { + P(1, 2) = I1(2); + P(1, 3) = I1(2); +} else { + P(1, 2) = I2(3, 1, 2, 5); + P(1, 3) = I3(2, 1, 1, 7, 2, 5); +} +if (MUL) { + P(0, 0) = I1(2); + P(0, 1) = I1(2); +} else { + P(0, 0) = I3(2, 1, 1, 1, 2, 3); + P(0, 1) = I2(3, 1, 2, 3); +} +} break; +case 252 : +{ +P(0, 0) = I1(0); +P(1, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 1) = I1(0); +P(1, 2) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MDR) { + P(1, 3) = I1(0); +} else { + P(1, 3) = I3(6, 1, 1, 0, 5, 7); +} +} break; +case 253 : +{ +P(0, 0) = I1(1); +P(1, 0) = I1(1); +P(0, 1) = I1(1); +P(1, 1) = I1(1); +P(0, 2) = I1(1); +P(1, 2) = I1(1); +if (MDL) { + P(0, 3) = I1(1); +} else { + P(0, 3) = I3(6, 1, 1, 1, 3, 7); +} +if (MDR) { + P(1, 3) = I1(1); +} else { + P(1, 3) = I3(6, 1, 1, 1, 5, 7); +} +} break; +case 254 : +{ +P(0, 0) = I1(0); +P(0, 1) = I1(0); +P(1, 2) = I1(0); +if (MDL) { + P(0, 2) = I1(0); + P(0, 3) = I1(0); +} else { + P(0, 2) = I2(3, 1, 0, 3); + P(0, 3) = I3(2, 1, 1, 7, 0, 3); +} +if (MDR) { + P(1, 3) = I1(0); +} else { + P(1, 3) = I3(6, 1, 1, 0, 5, 7); +} +if (MUR) { + P(1, 0) = I1(0); + P(1, 1) = I1(0); +} else { + P(1, 0) = I3(2, 1, 1, 1, 0, 5); + P(1, 1) = I2(3, 1, 0, 5); +} +} break; +case 255 : +{ +P(0, 1) = I1(4); +P(1, 1) = I1(4); +P(0, 2) = I1(4); +P(1, 2) = I1(4); +if (MDL) { + P(0, 3) = I1(4); +} else { + P(0, 3) = I3(6, 1, 1, 4, 3, 7); +} +if (MDR) { + P(1, 3) = I1(4); +} else { + P(1, 3) = I3(6, 1, 1, 4, 5, 7); +} +if (MUL) { + P(0, 0) = I1(4); +} else { + P(0, 0) = I3(6, 1, 1, 4, 1, 3); +} +if (MUR) { + P(1, 0) = I1(4); +} else { + P(1, 0) = I3(6, 1, 1, 4, 1, 5); +} +} break; diff --git a/src/lq3x.cpp b/src/lq3x.cpp new file mode 100644 index 00000000..1ddd572a --- /dev/null +++ b/src/lq3x.cpp @@ -0,0 +1,209 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +#include "portable.h" +#include "System.h" +#include "lq3x.h" + +#include + +/***************************************************************************/ +/* LQ3x C implementation */ + +/* + * This effect is derived from the hq3x effect made by Maxim Stepin + */ + +void lq3x_16_def(interp_uint16* restrict dst0, interp_uint16* restrict dst1, interp_uint16* restrict dst2, const interp_uint16* restrict src0, const interp_uint16* restrict src1, const interp_uint16* restrict src2, unsigned count) +{ + unsigned i; + + for(i=0;i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i + +/***************************************************************************/ +/* LQ4x C implementation */ + +/* + * This effect is derived from the hq4x effect made by Maxim Stepin + */ + +void lq4x_16_def(interp_uint16* restrict dst0, interp_uint16* restrict dst1, interp_uint16* restrict dst2, interp_uint16* restrict dst3, const interp_uint16* restrict src0, const interp_uint16* restrict src1, const interp_uint16* restrict src2, unsigned count) +{ + unsigned i; + + for(i=0;i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i0) { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } else { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i +#include +#include + +#include "memgzio.h" + +/*struct internal_state {int dummy;};*/ /* for buggy compilers */ + +#ifndef Z_BUFSIZE +# ifdef MAXSEG_64K +# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */ +# else +# define Z_BUFSIZE 16384 +# endif +#endif +#ifndef Z_PRINTF_BUFSIZE +# define Z_PRINTF_BUFSIZE 4096 +#endif + +#define ALLOC(size) malloc(size) +#define TRYFREE(p) {if (p) free(p);} + +static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ + +/* gzip flag byte */ +#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ +#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ +#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ +#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ +#define COMMENT 0x10 /* bit 4 set: file comment present */ +#define RESERVED 0xE0 /* bits 5..7: reserved */ + +typedef struct _MemFile { + char *memory; + char *next; + int available; + int error; + char mode; +} MEMFILE; + +typedef struct mem_stream { + z_stream stream; + int z_err; /* error code for last stream operation */ + int z_eof; /* set if end of input file */ + MEMFILE *file; /* memoru file */ + Byte *inbuf; /* input buffer */ + Byte *outbuf; /* output buffer */ + uLong crc; /* crc32 of uncompressed data */ + char *msg; /* error message */ + int transparent; /* 1 if input file is not a .gz file */ + char mode; /* 'w' or 'r' */ + long startpos; /* start of compressed data in file (header skipped) */ +} mem_stream; + + +local gzFile gz_open OF((char *memory, const int available, const char *mode)); +local int do_flush OF((gzFile file, int flush)); +local int get_byte OF((mem_stream *s)); +local void check_header OF((mem_stream *s)); +local int destroy OF((mem_stream *s)); +local void putLong OF((MEMFILE *file, uLong x)); +local uLong getLong OF((mem_stream *s)); + +local MEMFILE *memOpen(char *memory, int available, char mode) +{ + MEMFILE *f; + + if(available <= 8) + return NULL; + + if(mode != 'w' && mode != 'r') + return NULL; + + f = (MEMFILE *)malloc(sizeof(MEMFILE)); + + f->memory = memory; + f->mode = mode; + f->error = 0; + + if(mode == 'w') { + f->available = available - 8; + f->next = memory + 8; + memory[0] = 'V'; + memory[1] = 'B'; + memory[2] = 'A'; + memory[3] = ' '; + *((int *)(memory+4)) = 0; + } else { + if(memory[0] != 'V' || memory[1] != 'B' || memory[2] != 'A' || + memory[3] != ' ') { + free(f); + return NULL; + } + f->available = *((int *)(memory+4)); + f->next = memory+8; + } + + return f; +} + +local size_t memWrite(const void *buffer, size_t size, size_t count, + MEMFILE *file) +{ + size_t total = size*count; + + if(file->mode != 'w') { + file->error = 1; + return 0; + } + + if(total > (size_t)file->available) { + total = file->available; + } + memcpy(file->next, buffer, total); + file->available -= total; + file->next += total; + return total; +} + +local size_t memRead(void *buffer, size_t size, size_t count, + MEMFILE *file) +{ + size_t total = size*count; + + if(file->mode != 'r') { + file->error = 1; + return 0; + } + + if(file->available == 0) + return -1; + + if(total > (size_t)file->available) { + total = file->available; + } + memcpy(buffer, file->next, total); + file->available -= total; + file->next += total; + return total; +} + +local int memPutc(int c, MEMFILE *file) +{ + if(file->mode != 'w') { + file->error = 1; + return -1; + } + + if(file->available >= 1) { + *file->next++ = c; + file->available--; + } else + return -1; + + return c; +} + +local long memTell(MEMFILE *f) +{ + return (f->next - f->memory) - 8; +} + +local int memError(MEMFILE *f) +{ + return f->error; +} + +local int memClose(MEMFILE *f) +{ + if(f->mode == 'w') { + *((int *)(f->memory+4)) = memTell(f); + } + free(f); + return 0; +} + +local int memPrintf(MEMFILE *f, const char *format, ...) +{ + char buffer[80]; + va_list list; + int len; + + va_start(list, format); + len = vsprintf(buffer, format, list); + va_end(list); + + return memWrite(buffer, 1, len, f); +} + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. The mode parameter + is as in fopen ("rb" or "wb"). The file is given either by file descriptor + or path name (if fd == -1). + gz_open return NULL if the file could not be opened or if there was + insufficient memory to allocate the (de)compression state; errno + can be checked to distinguish the two cases (if errno is zero, the + zlib error is Z_MEM_ERROR). +*/ +local gzFile gz_open (char *memory, const int available, const char *mode) +{ + int err; + int level = Z_DEFAULT_COMPRESSION; /* compression level */ + int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */ + char *p = (char*)mode; + mem_stream *s; + char fmode[80]; /* copy of mode, without the compression level */ + char *m = fmode; + + s = (mem_stream *)ALLOC(sizeof(mem_stream)); + if (!s) return Z_NULL; + + s->stream.zalloc = (alloc_func)0; + s->stream.zfree = (free_func)0; + s->stream.opaque = (voidpf)0; + s->stream.next_in = s->inbuf = Z_NULL; + s->stream.next_out = s->outbuf = Z_NULL; + s->stream.avail_in = s->stream.avail_out = 0; + s->z_err = Z_OK; + s->z_eof = 0; + s->crc = crc32(0L, Z_NULL, 0); + s->msg = NULL; + s->transparent = 0; + s->file = NULL; + + s->mode = '\0'; + do { + if (*p == 'r') s->mode = 'r'; + if (*p == 'w' || *p == 'a') s->mode = 'w'; + if (*p >= '0' && *p <= '9') { + level = *p - '0'; + } else if (*p == 'f') { + strategy = Z_FILTERED; + } else if (*p == 'h') { + strategy = Z_HUFFMAN_ONLY; + } else { + *m++ = *p; /* copy the mode */ + } + } while (*p++ && m != fmode + sizeof(fmode)); + if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; + + if (s->mode == 'w') { +#ifdef NO_DEFLATE + err = Z_STREAM_ERROR; +#else + err = deflateInit2(&(s->stream), level, + Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy); + /* windowBits is passed < 0 to suppress zlib header */ + + s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); +#endif + if (err != Z_OK || s->outbuf == Z_NULL) { + return destroy(s), (gzFile)Z_NULL; + } + } else { + s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); + + err = inflateInit2(&(s->stream), -MAX_WBITS); + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are + * present after the compressed stream. + */ + if (err != Z_OK || s->inbuf == Z_NULL) { + return destroy(s), (gzFile)Z_NULL; + } + } + s->stream.avail_out = Z_BUFSIZE; + + errno = 0; + s->file = memOpen(memory, available, s->mode); + + if (s->file == NULL) { + return destroy(s), (gzFile)Z_NULL; + } + + if (s->mode == 'w') { + /* Write a very simple .gz header: + */ + memPrintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], + Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); + s->startpos = 10L; + /* We use 10L instead of ftell(s->file) to because ftell causes an + * fflush on some systems. This version of the library doesn't use + * startpos anyway in write mode, so this initialization is not + * necessary. + */ + } else { + check_header(s); /* skip the .gz header */ + s->startpos = (memTell(s->file) - s->stream.avail_in); + } + + return (gzFile)s; +} + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. +*/ +gzFile ZEXPORT memgzopen (char *memory, int available, const char *mode) +{ + return gz_open (memory, available, mode); +} + +/* =========================================================================== + Read a byte from a mem_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. +*/ +local int get_byte(mem_stream *s) +{ + if (s->z_eof) return EOF; + if (s->stream.avail_in == 0) { + errno = 0; + s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) { + s->z_eof = 1; + if (memError(s->file)) s->z_err = Z_ERRNO; + return EOF; + } + s->stream.next_in = s->inbuf; + } + s->stream.avail_in--; + return *(s->stream.next_in)++; +} + +/* =========================================================================== + Check the gzip header of a mem_stream opened for reading. Set the stream + mode to transparent if the gzip magic header is not present; set s->err + to Z_DATA_ERROR if the magic header is present but the rest of the header + is incorrect. + IN assertion: the stream s has already been created sucessfully; + s->stream.avail_in is zero for the first time, but may be non-zero + for concatenated .gz files. +*/ +local void check_header(mem_stream *s) +{ + int method; /* method byte */ + int flags; /* flags byte */ + uInt len; + int c; + + /* Check the gzip magic header */ + for (len = 0; len < 2; len++) { + c = get_byte(s); + if (c != gz_magic[len]) { + if (len != 0) s->stream.avail_in++, s->stream.next_in--; + if (c != EOF) { + s->stream.avail_in++, s->stream.next_in--; + s->transparent = 1; + } + s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END; + return; + } + } + method = get_byte(s); + flags = get_byte(s); + if (method != Z_DEFLATED || (flags & RESERVED) != 0) { + s->z_err = Z_DATA_ERROR; + return; + } + + /* Discard time, xflags and OS code: */ + for (len = 0; len < 6; len++) (void)get_byte(s); + + if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ + len = (uInt)get_byte(s); + len += ((uInt)get_byte(s))<<8; + /* len is garbage if EOF but the loop below will quit anyway */ + while (len-- != 0 && get_byte(s) != EOF) ; + } + if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ + while ((c = get_byte(s)) != 0 && c != EOF) ; + } + if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ + while ((c = get_byte(s)) != 0 && c != EOF) ; + } + if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ + for (len = 0; len < 2; len++) (void)get_byte(s); + } + s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK; +} + + /* =========================================================================== + * Cleanup then free the given mem_stream. Return a zlib error code. + Try freeing in the reverse order of allocations. + */ +local int destroy (mem_stream *s) +{ + int err = Z_OK; + + if (!s) return Z_STREAM_ERROR; + + TRYFREE(s->msg); + + if (s->stream.state != NULL) { + if (s->mode == 'w') { +#ifdef NO_DEFLATE + err = Z_STREAM_ERROR; +#else + err = deflateEnd(&(s->stream)); +#endif + } else if (s->mode == 'r') { + err = inflateEnd(&(s->stream)); + } + } + if (s->file != NULL && memClose(s->file)) { +#ifdef ESPIPE + if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ +#endif + err = Z_ERRNO; + } + if (s->z_err < 0) err = s->z_err; + + TRYFREE(s->inbuf); + TRYFREE(s->outbuf); + TRYFREE(s); + return err; +} + +/* =========================================================================== + Reads the given number of uncompressed bytes from the compressed file. + gzread returns the number of bytes actually read (0 for end of file). +*/ +int ZEXPORT memgzread (gzFile file, voidp buf, unsigned len) +{ + mem_stream *s = (mem_stream*)file; + Bytef *start = (Bytef*)buf; /* starting point for crc computation */ + Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */ + + if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; + + if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1; + if (s->z_err == Z_STREAM_END) return 0; /* EOF */ + + next_out = (Byte*)buf; + s->stream.next_out = (Bytef*)buf; + s->stream.avail_out = len; + + while (s->stream.avail_out != 0) { + + if (s->transparent) { + /* Copy first the lookahead bytes: */ + uInt n = s->stream.avail_in; + if (n > s->stream.avail_out) n = s->stream.avail_out; + if (n > 0) { + zmemcpy(s->stream.next_out, s->stream.next_in, n); + next_out += n; + s->stream.next_out = next_out; + s->stream.next_in += n; + s->stream.avail_out -= n; + s->stream.avail_in -= n; + } + if (s->stream.avail_out > 0) { + s->stream.avail_out -= memRead(next_out, 1, s->stream.avail_out, + s->file); + } + len -= s->stream.avail_out; + s->stream.total_in += (uLong)len; + s->stream.total_out += (uLong)len; + if (len == 0) s->z_eof = 1; + return (int)len; + } + if (s->stream.avail_in == 0 && !s->z_eof) { + + errno = 0; + s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) { + s->z_eof = 1; + if (memError(s->file)) { + s->z_err = Z_ERRNO; + break; + } + } + s->stream.next_in = s->inbuf; + } + s->z_err = inflate(&(s->stream), Z_NO_FLUSH); + + if (s->z_err == Z_STREAM_END) { + /* Check CRC and original size */ + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + start = s->stream.next_out; + + if (getLong(s) != s->crc) { + s->z_err = Z_DATA_ERROR; + } else { + (void)getLong(s); + /* The uncompressed length returned by above getlong() may + * be different from s->stream.total_out) in case of + * concatenated .gz files. Check for such files: + */ + check_header(s); + if (s->z_err == Z_OK) { + uLong total_in = s->stream.total_in; + uLong total_out = s->stream.total_out; + + inflateReset(&(s->stream)); + s->stream.total_in = total_in; + s->stream.total_out = total_out; + s->crc = crc32(0L, Z_NULL, 0); + } + } + } + if (s->z_err != Z_OK || s->z_eof) break; + } + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + + return (int)(len - s->stream.avail_out); +} + + +#ifndef NO_DEFLATE +/* =========================================================================== + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of bytes actually written (0 in case of error). +*/ +int ZEXPORT memgzwrite (gzFile file, const voidp buf, unsigned len) +{ + mem_stream *s = (mem_stream*)file; + + if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; + + s->stream.next_in = (Bytef*)buf; + s->stream.avail_in = len; + + while (s->stream.avail_in != 0) { + + if (s->stream.avail_out == 0) { + + s->stream.next_out = s->outbuf; + if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { + s->z_err = Z_ERRNO; + break; + } + s->stream.avail_out = Z_BUFSIZE; + } + s->z_err = deflate(&(s->stream), Z_NO_FLUSH); + if (s->z_err != Z_OK) break; + } + s->crc = crc32(s->crc, (const Bytef *)buf, len); + + return (int)(len - s->stream.avail_in); +} +#endif +/* =========================================================================== + Flushes all pending output into the compressed file. The parameter + flush is as in the deflate() function. +*/ +local int do_flush (gzFile file, int flush) +{ + uInt len; + int done = 0; + mem_stream *s = (mem_stream*)file; + + if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; + + s->stream.avail_in = 0; /* should be zero already anyway */ + + for (;;) { + len = Z_BUFSIZE - s->stream.avail_out; + + if (len != 0) { + if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len) { + s->z_err = Z_ERRNO; + return Z_ERRNO; + } + s->stream.next_out = s->outbuf; + s->stream.avail_out = Z_BUFSIZE; + } + if (done) break; + s->z_err = deflate(&(s->stream), flush); + + /* Ignore the second of two consecutive flushes: */ + if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK; + + /* deflate has finished flushing only when it hasn't used up + * all the available space in the output buffer: + */ + done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); + + if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; + } + return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; +} + +/* =========================================================================== + Outputs a long in LSB order to the given file +*/ +local void putLong (MEMFILE *file, uLong x) +{ + int n; + for (n = 0; n < 4; n++) { + memPutc((int)(x & 0xff), file); + x >>= 8; + } +} + +/* =========================================================================== + Reads a long in LSB order from the given mem_stream. Sets z_err in case + of error. +*/ +local uLong getLong (mem_stream *s) +{ + uLong x = (uLong)get_byte(s); + int c; + + x += ((uLong)get_byte(s))<<8; + x += ((uLong)get_byte(s))<<16; + c = get_byte(s); + if (c == EOF) s->z_err = Z_DATA_ERROR; + x += ((uLong)c)<<24; + return x; +} + +/* =========================================================================== + Flushes all pending output if necessary, closes the compressed file + and deallocates all the (de)compression state. +*/ +int ZEXPORT memgzclose (gzFile file) +{ + int err; + mem_stream *s = (mem_stream*)file; + + if (s == NULL) return Z_STREAM_ERROR; + + if (s->mode == 'w') { +#ifdef NO_DEFLATE + return Z_STREAM_ERROR; +#else + err = do_flush (file, Z_FINISH); + if (err != Z_OK) return destroy((mem_stream*)file); + + putLong (s->file, s->crc); + putLong (s->file, s->stream.total_in); +#endif + } + return destroy((mem_stream*)file); +} + +long ZEXPORT memtell(gzFile file) +{ + mem_stream *s = (mem_stream*)file; + + if (s == NULL) return Z_STREAM_ERROR; + + return memTell(s->file); +} diff --git a/src/memgzio.h b/src/memgzio.h new file mode 100644 index 00000000..3f761411 --- /dev/null +++ b/src/memgzio.h @@ -0,0 +1,17 @@ +/* gzio.c -- IO on .gz files + * Copyright (C) 1995-2002 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Compile this file with -DNO_DEFLATE to avoid the compression code. + */ + +/* memgzio.c - IO on .gz files in memory + * Adapted from original gzio.c from zlib library by Forgotten + */ +#include + +gzFile ZEXPORT memgzopen(char *memory, int, const char *); +int ZEXPORT memgzread(gzFile, voidp, unsigned); +int ZEXPORT memgzwrite(gzFile, const voidp, unsigned); +int ZEXPORT memgzclose(gzFile); +long ZEXPORT memtell(gzFile); diff --git a/src/motionblur.cpp b/src/motionblur.cpp new file mode 100644 index 00000000..35beb888 --- /dev/null +++ b/src/motionblur.cpp @@ -0,0 +1,192 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" + +extern int RGB_LOW_BITS_MASK; + +void MotionBlur(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + u32 lowPixelMask = RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width+2) << 1); + nextPixel = *bP++; + nextDelta = *xP++; + + do { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + if(currentPixel != currentDelta) { + u32 colorA, product, colorB; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentDelta >> 16; +#else + colorA = currentPixel & 0xffff; + colorB = currentDelta & 0xffff; +#endif + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP) = product | product << 16; + *(nL) = product | product << 16; + +#ifdef WORDS_BIGENDIAN + colorA = (currentPixel & 0xffff); + colorB = (currentDelta & 0xffff); +#else + colorA = currentPixel >> 16; + colorB = currentDelta >> 16; +#endif + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP + 1) = product | product << 16; + *(nL + 1) = product | product << 16; + } else { + u32 colorA, product; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; +#else + colorA = currentPixel & 0xffff; +#endif + + product = colorA; + + *(dP) = product | product << 16; + *(nL) = product | product << 16; +#ifdef WORDS_BIGENDIAN + colorA = (currentPixel & 0xffff); +#else + colorA = currentPixel >> 16; +#endif + product = colorA; + + *(dP + 1) = product | product << 16; + *(nL + 1) = product | product << 16; + } + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void MotionBlur32(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~RGB_LOW_BITS_MASK; + u32 lowPixelMask = RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width+1) << 2); + nextPixel = *bP++; + nextDelta = *xP++; + + do { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + u32 colorA, product, colorB; + + *(xP - 2) = currentPixel; + colorA = currentPixel; + colorB = currentDelta; + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP) = product; + *(dP+1) = product; + *(nL) = product; + *(nL+1) = product; + + *(xP - 1) = nextPixel; + + colorA = nextPixel; + colorB = nextDelta; + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP + 2) = product; + *(dP + 3) = product; + *(nL + 2) = product; + *(nL + 3) = product; + + nextPixel = *bP++; + nextDelta = *xP++; + + dP += 4; + nL += 4; + } while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} diff --git a/src/pixel.cpp b/src/pixel.cpp new file mode 100644 index 00000000..5673ed24 --- /dev/null +++ b/src/pixel.cpp @@ -0,0 +1,150 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" + +extern int RGB_LOW_BITS_MASK; + +void Pixelate(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width+2) << 1); + nextPixel = *bP++; + nextDelta = *xP++; + + do { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + if ((nextPixel != nextDelta) || (currentPixel != currentDelta)) { + u32 colorA, colorB, product; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xffff; +#else + colorA = currentPixel & 0xffff; + colorB = currentPixel >> 16; +#endif + product = (((colorA & colorMask) >> 1) & colorMask) >> 1; + +#ifdef WORDS_BIGENDIAN + *(nL) = (product << 16) | (product); + *(dP) = (colorA << 16) | product; +#else + *(nL) = product | (product << 16); + *(dP) = colorA | (product << 16); +#endif + +#ifdef WORDS_BIGENDIAN + colorA = nextPixel >> 16; +#else + colorA = nextPixel & 0xffff; +#endif + product = (((colorB & colorMask) >> 1) & colorMask) >> 1; +#ifdef WORDS_BIGENDIAN + *(nL + 1) = (product << 16) | (product); + *(dP + 1) = (colorB << 16) | (product); +#else + *(nL + 1) = (product) | (product << 16); + *(dP + 1) = (colorB) | (product << 16); +#endif + } + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Pixelate32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + // u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+1) << 2); + nextPixel = *bP++; + + do { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB, product; + + colorA = currentPixel; + colorB = nextPixel; + + product = (((colorA & colorMask) >> 1) & colorMask) >> 1; + *(nL) = product; + *(nL+1) = product; + *(dP) = colorA; + *(dP+1) = product; + + nextPixel = *bP++; + colorA = nextPixel; + product = (((colorB & colorMask) >> 1) & colorMask) >> 1; + *(nL + 2) = product; + *(nL + 3) = product; + *(dP + 2) = colorB; + *(dP + 3) = product; + + dP += 4; + nL += 4; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} diff --git a/src/portable.cpp b/src/portable.cpp new file mode 100644 index 00000000..b9932353 --- /dev/null +++ b/src/portable.cpp @@ -0,0 +1,81 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 1999, 2000, 2001, 2002, 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +#include "portable.h" + +#ifdef __MSDOS__ +int rpl_snprintf(char* str, size_t count, const char* fmt, ...) +{ + int r; + + /* Note that the snprintf implementation of "Patrick Powell 1995" has */ + /* various bugs on %f, %g and %e for example snprintf("%f",1.01) -> 1.1 */ + va_list arg; + va_start(arg, fmt); + r = vsprintf(str, fmt, arg); + va_end(arg); + + return r; +} + +int rpl_vsnprintf(char* str, size_t count, const char* fmt, va_list arg) +{ + return vsprintf(str, fmt, arg); +} +#endif + +#ifdef __WIN32__ +double rpl_asinh(double x) +{ + return log(x + sqrt(x*x + 1)); +} + +double rpl_acosh(double x) +{ + return log(x + sqrt(x*x - 1)); +} + +double rpl_logb(double x) +{ + return floor(log(x) / 0.69314718055994530942); +} + +int rpl_isnan(double x) +{ + /* NaNs are the only values unordered */ + return x != x; +} + +int rpl_isunordered(double x, double y) +{ + return rpl_isnan(x) || rpl_isnan(y); +} +#endif + diff --git a/src/portable.h b/src/portable.h new file mode 100644 index 00000000..94cdb749 --- /dev/null +++ b/src/portable.h @@ -0,0 +1,220 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/** \file + * Functions and defines required for portability. + */ + +#ifndef __PORTABLE_H +#define __PORTABLE_H + +#if HAVE_CONFIG_H +#include "config.h" /* Use " to include first in the same directory of this file */ +#endif + +/***************************************************************************/ +/* Config */ + +/* Customize for MSDOS DJGPP */ +#ifdef __MSDOS__ +#define TIME_WITH_SYS_TIME 1 +#define HAVE_SYS_TIME_H 1 +#define HAVE_SYS_TYPES_H 1 +#define HAVE_SYS_STAT_H 1 +#define HAVE_UNISTD_H 1 +#define HAVE_DIRENT_H 1 +#define HAVE_SYS_WAIT_H 1 +#define restrict __restrict +#endif + +/* Customize for Windows Mingw/Cygwin */ +#ifdef __WIN32__ +#define TIME_WITH_SYS_TIME 1 +#define HAVE_SYS_TIME_H 1 +#define HAVE_SYS_TYPES_H 1 +#define HAVE_SYS_STAT_H 1 +#define HAVE_UNISTD_H 1 +#define HAVE_DIRENT_H 1 +#define restrict __restrict +#endif + +/* Include some standard headers */ +#include +#include /* On many systems (e.g., Darwin), `stdio.h' is a prerequisite. */ +#include +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_UNISTD_H +#include +#endif + +#if TIME_WITH_SYS_TIME +#include +#include +#else +#if HAVE_SYS_TIME_H +#include +#else +#include +#endif +#endif + +#if HAVE_DIRENT_H +#include +#define NAMLEN(dirent) strlen((dirent)->d_name) +#else +#define dirent direct +#define NAMLEN(dirent) (dirent)->d_namlen +#if HAVE_SYS_NDIR_H +#include +#endif +#if HAVE_SYS_DIR_H +#include +#endif +#if HAVE_NDIR_H +#include +#endif +#endif + +#if HAVE_SYS_TYPES_H +#include +#endif + +#if HAVE_SYS_WAIT_H +#include +#endif + +#if HAVE_SYS_STAT_H +#include +#endif + +#ifdef __WIN32__ +#ifndef WEXITSTATUS +#define WEXITSTATUS(r) (r) +#endif +#ifndef WIFEXITED +#define WIFEXITED(r) 1 +#endif +#else +#ifndef WEXITSTATUS +#define WEXITSTATUS(r) ((unsigned)(r) >> 8) +#endif +#ifndef WIFEXITED +#define WIFEXITED(r) (((r) & 255) == 0) +#endif +#endif + +#ifndef WIFSTOPPED +#define WIFSTOPPED(r) 0 +#endif +#ifndef WIFSIGNALED +#define WIFSIGNALED(r) 0 +#endif +#ifndef WTERMSIG +#define WTERMSIG(r) 0 +#endif +#ifndef WSTOPSIG +#define WSTOPSIG(r) 0 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __MSDOS__ +int rpl_snprintf(char* str, size_t count, const char* fmt, ...); +int rpl_vsnprintf(char* str, size_t count, const char* fmt, va_list arg); +#define snprintf rpl_snprintf +#define vsnprintf rpl_vsnprintf +#endif + +#ifdef __WIN32__ +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#endif + +#ifdef __WIN32__ +#if (__GNUC__ == 2) +/* math functions for gcc for Windows */ +int rpl_isnan(double x); +int rpl_isunordered(double x, double y); +#define isnan rpl_isnan +#define isunordered rpl_isunordered +#endif + +#if (__GNUC__ == 2) || (__GNUC__ == 3 && __GNUC_MINOR__ <= 2) +/* math functions for gcc for Windows */ +double rpl_asinh(double x); +double rpl_acosh(double x); +double rpl_alogb(double x); +#define asinh rpl_asinh +#define acosh rpl_acosh +#define logb rpl_logb +#endif +#endif + +/* M_PI isn't a POSIX standard */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +/* faster lrint implementation */ +#if defined(__GNUC__) && defined(__i386__) +static inline int rpl_lrint(double x) +{ + int r; + __asm__ __volatile__ ( + "fistpl %0" + : "=m" (r) + : "t" (x) + : "st" + ); + return r; +} +#else +static inline int rpl_lrint(double x) +{ + return (int)x; +} +#endif +#define lrint rpl_lrint + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/remote.cpp b/src/remote.cpp new file mode 100644 index 00000000..40fdb3df --- /dev/null +++ b/src/remote.cpp @@ -0,0 +1,698 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include + +#ifndef WIN32 +# include +# include +# include +# ifdef HAVE_NETINET_IN_H +# include +# endif // HAVE_NETINET_IN_H +# ifdef HAVE_ARPA_INET_H +# include +# else // ! HAVE_ARPA_INET_H +# define socklen_t int +# endif // ! HAVE_ARPA_INET_H +#else // WIN32 +# include +# include +# define socklen_t int +# define close closesocket +# define read _read +# define write _write +#endif // WIN32 + +#include "GBA.h" + +extern bool debugger; +extern void CPUUpdateCPSR(); +#ifdef SDL +extern void (*dbgMain)(); +extern void (*dbgSignal)(int,int); +extern void debuggerMain(); +extern void debuggerSignal(int,int); +#endif + +int remotePort = 55555; +int remoteSignal = 5; +int remoteSocket = -1; +int remoteListenSocket = -1; +bool remoteConnected = false; +bool remoteResumed = false; + +int (*remoteSendFnc)(char *, int) = NULL; +int (*remoteRecvFnc)(char *, int) = NULL; +bool (*remoteInitFnc)() = NULL; +void (*remoteCleanUpFnc)() = NULL; + +#ifndef SDL +void remoteSetSockets(SOCKET l, SOCKET r) +{ + remoteSocket = r; + remoteListenSocket = l; +} +#endif + +int remoteTcpSend(char *data, int len) +{ + return send(remoteSocket, data, len, 0); +} + +int remoteTcpRecv(char *data, int len) +{ + return recv(remoteSocket, data, len, 0); +} + +bool remoteTcpInit() +{ + if(remoteSocket == -1) { +#ifdef WIN32 + WSADATA wsaData; + int error = WSAStartup(MAKEWORD(1,1),&wsaData); +#endif // WIN32 + int s = socket(PF_INET, SOCK_STREAM, 0); + + remoteListenSocket = s; + + if(s < 0) { + fprintf(stderr,"Error opening socket\n"); + exit(-1); + } + int tmp = 1; + setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp)); + + // char hostname[256]; + // gethostname(hostname, 256); + + // hostent *ent = gethostbyname(hostname); + // unsigned long a = *((unsigned long *)ent->h_addr); + + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(remotePort); + addr.sin_addr.s_addr = htonl(0); + int count = 0; + while(count < 3) { + if(bind(s, (sockaddr *)&addr, sizeof(addr))) { + addr.sin_port = htons(ntohs(addr.sin_port)+1); + } else + break; + } + if(count == 3) { + fprintf(stderr,"Error binding \n"); + exit(-1); + } + + fprintf(stderr,"Listening for a connection at port %d\n", + ntohs(addr.sin_port)); + + if(listen(s, 1)) { + fprintf(stderr, "Error listening\n"); + exit(-1); + } + socklen_t len = sizeof(addr); + +#ifdef WIN32 + int flag = 0; + ioctlsocket(s, FIONBIO, (unsigned long *)&flag); +#endif // WIN32 + int s2 = accept(s, (sockaddr *)&addr, &len); + if(s2 > 0) { + fprintf(stderr, "Got a connection from %s %d\n", + inet_ntoa((in_addr)addr.sin_addr), + ntohs(addr.sin_port)); + } else { +#ifdef WIN32 + int error = WSAGetLastError(); +#endif // WIN32 + } + char dummy; + recv(s2, &dummy, 1, 0); + if(dummy != '+') { + fprintf(stderr, "ACK not received\n"); + exit(-1); + } + remoteSocket = s2; + // close(s); + } + return true; +} + +void remoteTcpCleanUp() +{ + if(remoteSocket > 0) { + fprintf(stderr, "Closing remote socket\n"); + close(remoteSocket); + remoteSocket = -1; + } + if(remoteListenSocket > 0) { + fprintf(stderr, "Closing listen socket\n"); + close(remoteListenSocket); + remoteListenSocket = -1; + } +} + +int remotePipeSend(char *data, int len) +{ + int res = write(1, data, len); + return res; +} + +int remotePipeRecv(char *data, int len) +{ + int res = read(0, data, len); + return res; +} + +bool remotePipeInit() +{ + char dummy; + read(0, &dummy, 1); + if(dummy != '+') { + fprintf(stderr, "ACK not received\n"); + exit(-1); + } + + return true; +} + +void remotePipeCleanUp() +{ +} + +void remoteSetPort(int port) +{ + remotePort = port; +} + +void remoteSetProtocol(int p) +{ + if(p == 0) { + remoteSendFnc = remoteTcpSend; + remoteRecvFnc = remoteTcpRecv; + remoteInitFnc = remoteTcpInit; + remoteCleanUpFnc = remoteTcpCleanUp; + } else { + remoteSendFnc = remotePipeSend; + remoteRecvFnc = remotePipeRecv; + remoteInitFnc = remotePipeInit; + remoteCleanUpFnc = remotePipeCleanUp; + } +} + +void remoteInit() +{ + if(remoteInitFnc) + remoteInitFnc(); +} + +void remotePutPacket(char *packet) +{ + char *hex = "0123456789abcdef"; + char buffer[1024]; + + int count = strlen(packet); + + unsigned char csum = 0; + + char *p = buffer; + *p++ = '$'; + + for(int i = 0 ;i < count; i++) { + csum += packet[i]; + *p++ = packet[i]; + } + *p++ = '#'; + *p++ = hex[csum>>4]; + *p++ = hex[csum & 15]; + *p++ = 0; + // printf("Sending %s\n", buffer); + remoteSendFnc(buffer, count + 4); + + char c = 0; + remoteRecvFnc(&c, 1); + /* + if(c == '+') + printf("ACK\n"); + else if(c=='-') + printf("NACK\n"); + */ +} + +#define debuggerReadMemory(addr) \ + (*(u32*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]) + +#define debuggerReadHalfWord(addr) \ + (*(u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]) + +#define debuggerReadByte(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +#define debuggerWriteMemory(addr, value) \ + *(u32*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] = (value) + +#define debuggerWriteHalfWord(addr, value) \ + *(u16*)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] = (value) + +#define debuggerWriteByte(addr, value) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] = (value) + +void remoteOutput(char *s, u32 addr) +{ + char buffer[16384]; + + char *d = buffer; + *d++ = 'O'; + + if(s) { + char c = *s++; + while(c) { + sprintf(d, "%02x", c); + d += 2; + c = *s++; + } + } else { + char c= debuggerReadByte(addr); + addr++; + while(c) { + sprintf(d, "%02x", c); + d += 2; + c = debuggerReadByte(addr); + addr++; + } + } + remotePutPacket(buffer); + // fprintf(stderr, "Output sent %s\n", buffer); +} + +void remoteSendSignal() +{ + char buffer[1024]; + sprintf(buffer, "S%02x", remoteSignal); + remotePutPacket(buffer); +} + +void remoteSendStatus() +{ + char buffer[1024]; + sprintf(buffer, "T%02x", remoteSignal); + char *s = buffer; + s += 3; + for(int i = 0; i < 15; i++) { + u32 v = reg[i].I; + sprintf(s, "%02x:%02x%02x%02x%02x;",i, + (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + } + u32 v = armNextPC; + sprintf(s, "0f:%02x%02x%02x%02x;", (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + CPUUpdateCPSR(); + v = reg[16].I; + sprintf(s, "19:%02x%02x%02x%02x;", (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + *s = 0; + // printf("Sending %s\n", buffer); + remotePutPacket(buffer); +} + +void remoteBinaryWrite(char *p) +{ + u32 address; + int count; + sscanf(p,"%x,%x:", &address, &count); + // printf("Binary write for %08x %d\n", address, count); + + p = strchr(p, ':'); + p++; + for(int i = 0; i < count; i++) { + u8 b = *p++; + switch(b) { + case 0x7d: + b = *p++; + debuggerWriteByte(address, (b^0x20)); + address++; + break; + default: + debuggerWriteByte(address, b); + address++; + break; + } + } + // printf("ROM is %08x\n", debuggerReadMemory(0x8000254)); + remotePutPacket("OK"); +} + +void remoteMemoryWrite(char *p) +{ + u32 address; + int count; + sscanf(p,"%x,%x:", &address, &count); + // printf("Memory write for %08x %d\n", address, count); + + p = strchr(p, ':'); + p++; + for(int i = 0; i < count; i++) { + u8 v = 0; + char c = *p++; + if(c <= '9') + v = (c - '0') << 4; + else + v = (c + 10 - 'a') << 4; + c = *p++; + if(c <= '9') + v += (c - '0'); + else + v += (c + 10 - 'a'); + debuggerWriteByte(address, v); + address++; + } + // printf("ROM is %08x\n", debuggerReadMemory(0x8000254)); + remotePutPacket("OK"); +} + +void remoteMemoryRead(char *p) +{ + u32 address; + int count; + sscanf(p,"%x,%x:", &address, &count); + // printf("Memory read for %08x %d\n", address, count); + + char buffer[1024]; + + char *s = buffer; + for(int i = 0; i < count; i++) { + u8 b = debuggerReadByte(address); + sprintf(s, "%02x", b); + address++; + s += 2; + } + *s = 0; + remotePutPacket(buffer); +} + +void remoteStepOverRange(char *p) +{ + u32 address; + u32 final; + sscanf(p, "%x,%x", &address, &final); + + remotePutPacket("OK"); + + remoteResumed = true; + do { + CPULoop(1); + if(debugger) + break; + } while(armNextPC >= address && armNextPC < final); + + remoteResumed = false; + + remoteSendStatus(); +} + +void remoteWriteWatch(char *p, bool active) +{ + u32 address; + int count; + sscanf(p, ",%x,%x#", &address, &count); + + fprintf(stderr, "Write watch for %08x %d\n", address, count); + + if(address < 0x2000000 || address > 0x3007fff) { + remotePutPacket("E01"); + return; + } + + if(address > 0x203ffff && address < 0x3000000) { + remotePutPacket("E01"); + return; + } + + u32 final = address + count; + + if(address < 0x2040000 && final > 0x2040000) { + remotePutPacket("E01"); + return; + } else if(address < 0x3008000 && final > 0x3008000) { + remotePutPacket("E01"); + return; + } + + for(int i = 0; i < count; i++) { + if((address >> 24) == 2) + freezeWorkRAM[address & 0x3ffff] = active; + else + freezeInternalRAM[address & 0x7fff] = active; + address++; + } + + remotePutPacket("OK"); +} + +void remoteReadRegisters(char *p) +{ + char buffer[1024]; + + char *s = buffer; + int i; + // regular registers + for(i = 0; i < 15; i++) { + u32 v = reg[i].I; + sprintf(s, "%02x%02x%02x%02x", v & 255, (v >> 8) & 255, + (v >> 16) & 255, (v >> 24) & 255); + s += 8; + } + // PC + u32 pc = armNextPC; + sprintf(s, "%02x%02x%02x%02x", pc & 255, (pc >> 8) & 255, + (pc >> 16) & 255, (pc >> 24) & 255); + s += 8; + + // floating point registers (24-bit) + for(i = 0; i < 8; i++) { + sprintf(s, "000000000000000000000000"); + s += 24; + } + + // FP status register + sprintf(s, "00000000"); + s += 8; + // CPSR + CPUUpdateCPSR(); + u32 v = reg[16].I; + sprintf(s, "%02x%02x%02x%02x", v & 255, (v >> 8) & 255, + (v >> 16) & 255, (v >> 24) & 255); + s += 8; + *s = 0; + remotePutPacket(buffer); +} + +void remoteWriteRegister(char *p) +{ + int r; + + sscanf(p, "%x=", &r); + + p = strchr(p, '='); + p++; + + char c = *p++; + + u32 v = 0; + + u8 data[4] = {0,0,0,0}; + + int i = 0; + + while(c != '#') { + u8 b = 0; + if(c <= '9') + b = (c - '0') << 4; + else + b = (c + 10 - 'a') << 4; + c = *p++; + if(c <= '9') + b += (c - '0'); + else + b += (c + 10 - 'a'); + data[i++] = b; + c = *p++; + } + + v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); + + // printf("Write register %d=%08x\n", r, v); + reg[r].I = v; + if(r == 15) { + armNextPC = v; + if(armState) + reg[15].I = v + 4; + else + reg[15].I = v + 2; + } + remotePutPacket("OK"); +} + +extern int emulating; + +void remoteStubMain() +{ + if(!debugger) + return; + + if(remoteResumed) { + remoteSendStatus(); + remoteResumed = false; + } + + while(1) { + char buffer[1024]; + int res = remoteRecvFnc(buffer, 1024); + + if(res == -1) { + fprintf(stderr, "GDB connection lost\n"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + debugger = false; + break; + } + + // fprintf(stderr, "Received %s\n", buffer); + char *p = buffer; + char c = *p++; + char pp = '+'; + remoteSendFnc(&pp, 1); + + if(c != '$') + continue; + c= *p++; + switch(c) { + case '?': + remoteSendSignal(); + break; + case 'D': + remotePutPacket("OK"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + remoteResumed = true; + debugger = false; + return; + case 'e': + remoteStepOverRange(p); + break; + case 'k': + remotePutPacket("OK"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + debugger = false; + emulating = false; + return; + case 'C': + remoteResumed = true; + debugger = false; + return; + case 'c': + remoteResumed = true; + debugger = false; + return; + case 's': + remoteResumed = true; + remoteSignal = 5; + CPULoop(1); + if(remoteResumed) { + remoteResumed = false; + remoteSendStatus(); + } + break; + case 'g': + remoteReadRegisters(p); + break; + case 'P': + remoteWriteRegister(p); + break; + case 'M': + remoteMemoryWrite(p); + break; + case 'm': + remoteMemoryRead(p); + break; + case 'X': + remoteBinaryWrite(p); + break; + case 'H': + remotePutPacket("OK"); + break; + case 'q': + remotePutPacket(""); + break; + case 'Z': + if(*p++ == '2') { + remoteWriteWatch(p, true); + } else + remotePutPacket(""); + break; + case 'z': + if(*p++ == '2') { + remoteWriteWatch(p, false); + } else + remotePutPacket(""); + break; + default: + { + *(strchr(p, '#') + 3) = 0; + fprintf(stderr, "Unknown packet %s\n", --p); + remotePutPacket(""); + } + break; + } + } +} + +void remoteStubSignal(int sig, int number) +{ + remoteSignal = sig; + remoteResumed = false; + remoteSendStatus(); + debugger = true; +} + +void remoteCleanUp() +{ + if(remoteCleanUpFnc) + remoteCleanUpFnc(); +} diff --git a/src/scanline.cpp b/src/scanline.cpp new file mode 100644 index 00000000..21610a8a --- /dev/null +++ b/src/scanline.cpp @@ -0,0 +1,230 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" + +extern int RGB_LOW_BITS_MASK; + +void Scanlines (u8 *srcPtr, u32 srcPitch, u8 *, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+2) << 1); + nextPixel = *bP++; + + do { + currentPixel = nextPixel; + nextPixel = *bP++; + u32 colorA, colorB; + +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xffff; +#else + colorA = currentPixel & 0xffff; + colorB = currentPixel >> 16; +#endif + + *(dP) = colorA | colorA<<16; + *(nL) = 0; + +#ifdef WORDS_BIGENDIAN + colorA = nextPixel >> 16; +#else + colorA = nextPixel & 0xffff; +#endif + + *(dP + 1) = colorB | (colorB << 16); + *(nL + 1) = 0; + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Scanlines32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+1) << 2); + nextPixel = *bP++; + + do { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB; + + colorA = currentPixel; + colorB = nextPixel; + + *(dP) = colorA; + *(dP+1) = colorA; + *(nL) = 0; + *(nL+1) = 0; + + *(dP + 2) = colorB; + *(dP + 3) = colorB; + *(nL+2) = 0; + *(nL+3) = 0; + + nextPixel = *bP++; + + dP += 4; + nL += 4; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void ScanlinesTV(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+2) << 1); + nextPixel = *bP++; + + do { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB; + +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xFFFF; +#else + colorA = currentPixel & 0xFFFF; + colorB = currentPixel >> 16; +#endif + + *(dP) = colorA = colorA | ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1))) << 16; + colorA = ((colorA & colorMask) >> 1); + colorA += ((colorA & colorMask) >> 1); + *(nL) = colorA; + + colorA = nextPixel & 0xFFFF; + + *(dP + 1) = colorB = colorB | ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1))) << 16; + colorB = ((colorB & colorMask) >> 1); + colorB += ((colorB & colorMask) >> 1); + + *(nL + 1) = colorB; + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void ScanlinesTV32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+1) << 2); + nextPixel = *bP++; + + do { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB, temp; + + colorA = currentPixel; + colorB = nextPixel; + + *(dP) = colorA; + *(dP+1) = temp = ((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1); + temp = ((temp & colorMask) >> 1); + temp += ((temp & colorMask) >> 1); + colorA = ((colorA & colorMask) >> 1); + colorA += ((colorA & colorMask) >> 1); + + *(nL) = colorA; + *(nL+1) = temp; + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} diff --git a/src/simpleFilter.cpp b/src/simpleFilter.cpp new file mode 100644 index 00000000..15e0e78c --- /dev/null +++ b/src/simpleFilter.cpp @@ -0,0 +1,303 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "System.h" + +void Simple2x16(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + + finish = (u8 *) bP + ((width+2) << 1); + currentPixel = *bP++; + + do { +#ifdef WORDS_BIGENDIAN + u32 color = currentPixel >> 16; +#else + u32 color = currentPixel & 0xffff; +#endif + + color = color | (color << 16); + + *(dP) = color; + *(nL) = color; + +#ifdef WORDS_BIGENDIAN + color = currentPixel & 0xffff; +#else + color = currentPixel >> 16; +#endif + color = color| (color << 16); + *(dP + 1) = color; + *(nL + 1) = color; + + currentPixel = *bP++; + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Simple2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + + finish = (u8 *) bP + ((width+1) << 2); + currentPixel = *bP++; + + do { + u32 color = currentPixel; + + *(dP) = color; + *(dP+1) = color; + *(nL) = color; + *(nL + 1) = color; + + currentPixel = *bP++; + + dP += 2; + nL += 2; + } while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + + +void Simple3x16(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ +#define magnification 3 +#define colorBytes 2 // 16 bit colors = 2 byte colors + + // Generic Simple magnification filter + int x, y; // Source Position Counter + unsigned int dx, dy; // Destination pixel's pixels + unsigned short col; // Source color + + srcPitch = (srcPitch / colorBytes) - width; // This is the part of the source pitch in pixels that is more than the source image + dstPitch = dstPitch / colorBytes; + + unsigned short *src, *dst, *dst2; + src = (unsigned short *)srcPtr; // Since everything is time-critical this should be better than converting the pointers x*y times + dst = (unsigned short *)dstPtr; + + for (y = 0; y < height; y++) // Line + { + for (x = 0; x < width; x++) // Pixel in Line + { + col = *src; + + dst2 = dst; + *dst2 = col; + for (dy = 0; dy < magnification; dy++) + { + for (dx = 0; dx < magnification; dx++) + { + *dst2 = col; + dst2++; + } + dst2+=dstPitch; + dst2-=magnification; + } + + src++; + dst+=magnification; + } + src+=srcPitch; + dst+=dstPitch * magnification; + dst-=width * magnification; + } +#undef magnification +#undef colorBytes +} + + + +void Simple3x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ +#define magnification 3 +#define colorBytes 4 // 32 bit colors = 4 byte colors + + // Generic Simple magnification filter + int x, y; // Source Position Counter + unsigned int dx, dy; // Destination pixel's pixels + unsigned int col; // Source color + + srcPitch = (srcPitch / colorBytes) - width; // This is the part of the source pitch in pixels that is more than the source image + dstPitch = dstPitch / colorBytes; + + unsigned int *src, *dst, *dst2; + src = (unsigned int *)srcPtr; // Since everything is time-critical this should be better than converting the pointers x*y times + dst = (unsigned int *)dstPtr; + + for (y = 0; y < height; y++) // Line + { + for (x = 0; x < width; x++) // Pixel in Line + { + col = *src; + + dst2 = dst; + *dst2 = col; + for (dy = 0; dy < magnification; dy++) + { + for (dx = 0; dx < magnification; dx++) + { + *dst2 = col; + dst2++; + } + dst2+=dstPitch; + dst2-=magnification; + } + + src++; + dst+=magnification; + } + src+=srcPitch; + dst+=dstPitch * magnification; + dst-=width * magnification; + } +#undef magnification +#undef colorBytes +} + +void Simple4x16(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ +#define magnification 4 +#define colorBytes 2 // 16 bit colors = 2 byte colors + + // Generic Simple magnification filter + int x, y; // Source Position Counter + unsigned int dx, dy; // Destination pixel's pixels + unsigned short col; // Source color + + srcPitch = (srcPitch / colorBytes) - width; // This is the part of the source pitch in pixels that is more than the source image + dstPitch = dstPitch / colorBytes; + + unsigned short *src, *dst, *dst2; + src = (unsigned short *)srcPtr; // Since everything is time-critical this should be better than converting the pointers x*y times + dst = (unsigned short *)dstPtr; + + for (y = 0; y < height; y++) // Line + { + for (x = 0; x < width; x++) // Pixel in Line + { + col = *src; + + dst2 = dst; + *dst2 = col; + for (dy = 0; dy < magnification; dy++) + { + for (dx = 0; dx < magnification; dx++) + { + *dst2 = col; + dst2++; + } + dst2+=dstPitch; + dst2-=magnification; + } + + src++; + dst+=magnification; + } + src+=srcPitch; + dst+=dstPitch * magnification; + dst-=width * magnification; + } +#undef magnification +#undef colorBytes +} + + + +void Simple4x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ +#define magnification 4 +#define colorBytes 4 // 32 bit colors = 4 byte colors + + // Generic Simple magnification filter + int x, y; // Source Position Counter + unsigned int dx, dy; // Destination pixel's pixels + unsigned int col; // Source color + + srcPitch = (srcPitch / colorBytes) - width; // This is the part of the source pitch in pixels that is more than the source image + dstPitch = dstPitch / colorBytes; + + unsigned int *src, *dst, *dst2; + src = (unsigned int *)srcPtr; // Since everything is time-critical this should be better than converting the pointers x*y times + dst = (unsigned int *)dstPtr; + + for (y = 0; y < height; y++) // Line + { + for (x = 0; x < width; x++) // Pixel in Line + { + col = *src; + + dst2 = dst; + *dst2 = col; + for (dy = 0; dy < magnification; dy++) + { + for (dx = 0; dx < magnification; dx++) + { + *dst2 = col; + dst2++; + } + dst2+=dstPitch; + dst2-=magnification; + } + + src++; + dst+=magnification; + } + src+=srcPitch; + dst+=dstPitch * magnification; + dst-=width * magnification; + } +#undef magnification +#undef colorBytes +} diff --git a/src/snd_interp.cpp b/src/snd_interp.cpp new file mode 100644 index 00000000..17510980 --- /dev/null +++ b/src/snd_interp.cpp @@ -0,0 +1,619 @@ +#include + +#define WIN32_LEAN_AND_MEAN +#include + +#include "../libresample-0.1.3/include/libresample.h" + +#include "snd_interp.h" + +// this was once borrowed from libmodplug, and was also used to generate the FIR coefficient +// tables that ZSNES uses for its "FIR" interpolation mode + +/* + ------------------------------------------------------------------------------------------------ + fir interpolation doc, + (derived from "an engineer's guide to fir digital filters", n.j. loy) + + calculate coefficients for ideal lowpass filter (with cutoff = fc in 0..1 (mapped to 0..nyquist)) + c[-N..N] = (i==0) ? fc : sin(fc*pi*i)/(pi*i) + + then apply selected window to coefficients + c[-N..N] *= w(0..N) + with n in 2*N and w(n) being a window function (see loy) + + then calculate gain and scale filter coefs to have unity gain. + ------------------------------------------------------------------------------------------------ +*/ +// quantizer scale of window coefs +#define WFIR_QUANTBITS 14 +#define WFIR_QUANTSCALE (1L<>1) +// cutoff (1.0 == pi/2) +#define WFIR_CUTOFF 0.95f +// wfir type +#define WFIR_HANN 0 +#define WFIR_HAMMING 1 +#define WFIR_BLACKMANEXACT 2 +#define WFIR_BLACKMAN3T61 3 +#define WFIR_BLACKMAN3T67 4 +#define WFIR_BLACKMAN4T92 5 +#define WFIR_BLACKMAN4T74 6 +#define WFIR_KAISER4T 7 +#define WFIR_LANCZOS 8 +#define WFIR_TYPE WFIR_LANCZOS +// wfir help +#ifndef M_zPI +#define M_zPI 3.1415926535897932384626433832795 +#endif +#define M_zEPS 1e-8 +#define M_zBESSELEPS 1e-21 + +class CzWINDOWEDFIR +{ public: + CzWINDOWEDFIR( ); + ~CzWINDOWEDFIR( ); + float coef( int _PCnr, float _POfs, float _PCut, int _PWidth, int _PType ) //float _PPos, float _PFc, int _PLen ) + { double _LWidthM1 = _PWidth-1; + double _LWidthM1Half = 0.5*_LWidthM1; + double _LPosU = ((double)_PCnr - _POfs); + double _LPos = _LPosU-_LWidthM1Half; + double _LPIdl = 2.0*M_zPI/_LWidthM1; + double _LWc,_LSi; + if( fabs(_LPos)_LScale)?_LScale:_LCoef) ); + } + } +} + +CzWINDOWEDFIR::~CzWINDOWEDFIR() +{ // nothing todo +} + +CzWINDOWEDFIR sfir; + +template +class sample_buffer +{ + int ptr, filled; + T * buffer; + +public: + sample_buffer() : ptr(0), filled(0), buffer(0) {} + ~sample_buffer() + { + if (buffer) delete [] buffer; + } + + void clear() + { + if (buffer) + { + delete [] buffer; + buffer = 0; + } + ptr = filled = 0; + } + + inline int size() const + { + return filled; + } + + void push_back(T sample) + { + if (!buffer) buffer = new T[buffer_size]; + buffer[ptr] = sample; + if (++ptr >= buffer_size) ptr = 0; + if (filled < buffer_size) filled++; + } + + void erase(int count) + { + if (count > filled) filled = 0; + else filled -= count; + } + + T operator[] (int index) const + { + index += ptr - filled; + if (index < 0) index += buffer_size; + else if (index > buffer_size) index -= buffer_size; + return buffer[index]; + } +}; + +class foo_null : public foo_interpolate +{ + int sample; + +public: + foo_null() : sample(0) {} + ~foo_null() {} + + void reset() {} + + void push(int psample) + { + sample = psample; + } + + int pop(double rate) + { + return sample; + } +}; + +class foo_linear : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_linear() + { + position = 0; + } + + ~foo_linear() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 2) return 0; + + ret = smp(0) * (0x8000 - position); + ret += smp(1) * position; + ret >>= 15; + + // wahoo, takes care of drifting + if (samples.size() > 2) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +// and this integer cubic interpolation implementation was kind of borrowed from either TiMidity +// or the P.E.Op.S. SPU project, or is in use in both, or something... + +class foo_cubic : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_cubic() + { + position = 0; + } + + ~foo_cubic() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 4) return 0; + + ret = smp(3) - 3 * smp(2) + 3 * smp(1) - smp(0); + ret *= (position - (2 << 15)) / 6; + ret >>= 15; + ret += smp(2) - 2 * smp(1) + smp(0); + ret *= (position - (1 << 15)) >> 1; + ret >>= 15; + ret += smp(1) - smp(0); + ret *= position; + ret >>= 15; + ret += smp(0); + + if (ret > 32767) ret = 32767; + else if (ret < -32768) ret = -32768; + + // wahoo, takes care of drifting + if (samples.size() > 8) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +class foo_fir : public foo_interpolate +{ + sample_buffer samples; + + int position; + + inline int smp(int index) + { + return samples[index]; + } + +public: + foo_fir() + { + position = 0; + } + + ~foo_fir() {} + + void reset() + { + position = 0; + samples.clear(); + } + + void push(int sample) + { + samples.push_back(sample); + } + + int pop(double rate) + { + int ret, lrate; + + if (position > 0x7fff) + { + int howmany = position >> 15; + position &= 0x7fff; + samples.erase(howmany); + } + + if (samples.size() < 8) return 0; + + ret = smp(0) * CzWINDOWEDFIR::lut[(position & ~7) ]; + ret += smp(1) * CzWINDOWEDFIR::lut[(position & ~7) + 1]; + ret += smp(2) * CzWINDOWEDFIR::lut[(position & ~7) + 2]; + ret += smp(3) * CzWINDOWEDFIR::lut[(position & ~7) + 3]; + ret += smp(4) * CzWINDOWEDFIR::lut[(position & ~7) + 4]; + ret += smp(5) * CzWINDOWEDFIR::lut[(position & ~7) + 5]; + ret += smp(6) * CzWINDOWEDFIR::lut[(position & ~7) + 6]; + ret += smp(7) * CzWINDOWEDFIR::lut[(position & ~7) + 7]; + ret >>= WFIR_QUANTBITS; + + if (ret > 32767) ret = 32767; + else if (ret < -32768) ret = -32768; + + // wahoo, takes care of drifting + if (samples.size() > 16) + { + rate += (.5 / 32768.); + } + + lrate = (int)(32768. * rate); + position += lrate; + + return ret; + } +}; + +class foo_libresample : public foo_interpolate +{ + sample_buffer samples; + + void * resampler; + +public: + foo_libresample() + { + resampler = 0; + } + + ~foo_libresample() + { + reset(); + } + + void reset() + { + samples.clear(); + if (resampler) + { + resample_close(resampler); + resampler = 0; + } + } + + void push(int sample) + { + samples.push_back(float(sample)); + } + + int pop(double rate) + { + int ret; + + if (!resampler) + { + resampler = resample_open(0, .25, 44100. / 4000.); + } + + { + int count = samples.size(); + float * in = new float[count]; + float out; + int used, returned; + + for (used = 0; used < count; used++) + { + in[used] = samples[used]; + } + + returned = resample_process(resampler, 1. / rate, in, count, 0, &used, &out, 1); + + if (used) + { + samples.erase(used); + } + + delete [] in; + + if (returned < 1) return 0; + + ret = (int)out; + } + + if (ret > 32767) ret = 32767; + else if (ret < -32768) ret = -32768; + + return ret; + } +}; + +foo_interpolate * get_filter(int which) +{ + switch (which) + { + default: + return new foo_null; + case 1: + return new foo_linear; + case 2: + return new foo_cubic; + case 3: + return new foo_fir; + case 4: + return new foo_libresample; + } +} + +// and here is the implementation specific code, in a messier state than the stuff above + +extern bool timer0On; +extern int timer0Reload; +extern int timer0ClockReload; +extern bool timer1On; +extern int timer1Reload; +extern int timer1ClockReload; + +extern int SOUND_CLOCK_TICKS; +extern int soundInterpolation; + +double calc_rate(int timer) +{ + if (timer ? timer1On : timer0On) + { + return double(SOUND_CLOCK_TICKS) / + double((0x10000 - (timer ? timer1Reload : timer0Reload)) * + (timer ? timer1ClockReload : timer0ClockReload)); + } + else + { + return 1.; + } +} + +static foo_interpolate * interp[2]; + +class foo_interpolate_setup +{ +public: + foo_interpolate_setup() + { + for (int i = 0; i < 2; i++) + { + interp[i] = get_filter(0); + } + } + + ~foo_interpolate_setup() + { + for (int i = 0; i < 2; i++) + { + delete interp[i]; + } + } +}; + +static foo_interpolate_setup blah; + +class critical_section +{ + CRITICAL_SECTION cs; + +public: + critical_section() { InitializeCriticalSection(&cs); } + ~critical_section() { DeleteCriticalSection(&cs); } + + void enter() { EnterCriticalSection(&cs); } + void leave() { LeaveCriticalSection(&cs); } +}; + +static critical_section interp_sync; +static int interpolation = 0; + +class scopelock +{ + critical_section * cs; + +public: + scopelock(critical_section & pcs) { cs = &pcs; cs->enter(); } + ~scopelock() { cs->leave(); } +}; + +void interp_switch(int which) +{ + scopelock sl(interp_sync); + + for (int i = 0; i < 2; i++) + { + delete interp[i]; + interp[i] = get_filter(which); + } + + interpolation = which; +} + +void interp_reset(int ch) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + interp[ch]->reset(); +} + +void interp_push(int ch, int sample) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + interp[ch]->push(sample); +} + +int interp_pop(int ch, double rate) +{ + scopelock sl(interp_sync); + if (soundInterpolation != interpolation) interp_switch(soundInterpolation); + + return interp[ch]->pop(rate); +} diff --git a/src/snd_interp.h b/src/snd_interp.h new file mode 100644 index 00000000..60e2a83b --- /dev/null +++ b/src/snd_interp.h @@ -0,0 +1,30 @@ +#ifndef __SND_INTERP_H__ +#define __SND_INTERP_H__ + +// simple interface that could easily be recycled + +class foo_interpolate +{ +public: + foo_interpolate() {} + virtual ~foo_interpolate() {}; + + virtual void reset() = 0; + + virtual void push(int sample) = 0; + virtual int pop(double rate) = 0; +}; + +foo_interpolate * get_filter(int which); + + +// complicated, synced interface, specific to this implementation + +double calc_rate(int timer); + +void interp_switch(int which); +void interp_reset(int ch); +void interp_push(int ch, int sample); +int interp_pop(int ch, double rate); + +#endif \ No newline at end of file diff --git a/src/thumb.h b/src/thumb.h new file mode 100644 index 00000000..51502583 --- /dev/null +++ b/src/thumb.h @@ -0,0 +1,2474 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifdef C_CORE +#define NEG(i) ((i) >> 31) +#define POS(i) ((~(i)) >> 31) +#define ADDCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & NEG(b)) |\ + (NEG(a) & POS(c)) |\ + (NEG(b) & POS(c))) ? true : false; +#define ADDOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & NEG(b) & POS(c)) |\ + (POS(a) & POS(b) & NEG(c))) ? true : false; +#define SUBCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & POS(b)) |\ + (NEG(a) & POS(c)) |\ + (POS(b) & POS(c))) ? true : false; +#define SUBOVERFLOW(a, b, c)\ + V_FLAG = ((NEG(a) & POS(b) & POS(c)) |\ + (POS(a) & NEG(b) & NEG(c))) ? true : false; +#define ADD_RD_RS_RN \ + {\ + u32 lhs = reg[source].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define ADD_RD_RS_O3 \ + {\ + u32 lhs = reg[source].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define ADD_RN_O8(d) \ + {\ + u32 lhs = reg[(d)].I;\ + u32 rhs = (opcode & 255);\ + u32 res = lhs + rhs;\ + reg[(d)].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define CMN_RD_RS \ + {\ + u32 lhs = reg[dest].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define ADC_RD_RS \ + {\ + u32 lhs = reg[dest].I;\ + u32 rhs = value;\ + u32 res = lhs + rhs + (u32)C_FLAG;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + ADDCARRY(lhs, rhs, res);\ + ADDOVERFLOW(lhs, rhs, res);\ + } +#define SUB_RD_RS_RN \ + {\ + u32 lhs = reg[source].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define SUB_RD_RS_O3 \ + {\ + u32 lhs = reg[source].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define SUB_RN_O8(d) \ + {\ + u32 lhs = reg[(d)].I;\ + u32 rhs = (opcode & 255);\ + u32 res = lhs - rhs;\ + reg[(d)].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define CMP_RN_O8(d) \ + {\ + u32 lhs = reg[(d)].I;\ + u32 rhs = (opcode & 255);\ + u32 res = lhs - rhs;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define SBC_RD_RS \ + {\ + u32 lhs = reg[dest].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs - !((u32)C_FLAG);\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#define LSL_RD_RM_I5 \ + {\ + C_FLAG = (reg[source].I >> (32 - shift)) & 1 ? true : false;\ + value = reg[source].I << shift;\ + } +#define LSL_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (32 - value)) & 1 ? true : false;\ + value = reg[dest].I << value;\ + } +#define LSR_RD_RM_I5 \ + {\ + C_FLAG = (reg[source].I >> (shift - 1)) & 1 ? true : false;\ + value = reg[source].I >> shift;\ + } +#define LSR_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false;\ + value = reg[dest].I >> value;\ + } +#define ASR_RD_RM_I5 \ + {\ + C_FLAG = ((s32)reg[source].I >> (int)(shift - 1)) & 1 ? true : false;\ + value = (s32)reg[source].I >> (int)shift;\ + } +#define ASR_RD_RS \ + {\ + C_FLAG = ((s32)reg[dest].I >> (int)(value - 1)) & 1 ? true : false;\ + value = (s32)reg[dest].I >> (int)value;\ + } +#define ROR_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false;\ + value = ((reg[dest].I << (32 - value)) |\ + (reg[dest].I >> value));\ + } +#define NEG_RD_RS \ + {\ + u32 lhs = reg[source].I;\ + u32 rhs = 0;\ + u32 res = rhs - lhs;\ + reg[dest].I = res;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(rhs, lhs, res);\ + SUBOVERFLOW(rhs, lhs, res);\ + } +#define CMP_RD_RS \ + {\ + u32 lhs = reg[dest].I;\ + u32 rhs = value;\ + u32 res = lhs - rhs;\ + Z_FLAG = (res == 0) ? true : false;\ + N_FLAG = NEG(res) ? true : false;\ + SUBCARRY(lhs, rhs, res);\ + SUBOVERFLOW(lhs, rhs, res);\ + } +#else +#ifdef __GNUC__ +#ifdef __POWERPC__ + #define ADD_RD_RS_RN \ + { \ + register int Flags; \ + register int Result; \ + asm volatile("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define ADD_RD_RS_O3 ADD_RD_RS_RN + #define ADD_RN_O8(d) \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + reg[(d)].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMN_RD_RS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define ADC_RD_RS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("mtspr xer, %4\n" \ + "addeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SUB_RD_RS_RN \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SUB_RD_RS_O3 SUB_RD_RS_RN + #define SUB_RN_O8(d) \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + reg[(d)].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMP_RN_O8(d) \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SBC_RD_RS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("mtspr xer, %4\n" \ + "subfeo. %0, %3, %2\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define LSL_RD_RM_I5 \ + {\ + C_FLAG = (reg[source].I >> (32 - shift)) & 1 ? true : false;\ + value = reg[source].I << shift;\ + } + #define LSL_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (32 - value)) & 1 ? true : false;\ + value = reg[dest].I << value;\ + } + #define LSR_RD_RM_I5 \ + {\ + C_FLAG = (reg[source].I >> (shift - 1)) & 1 ? true : false;\ + value = reg[source].I >> shift;\ + } + #define LSR_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false;\ + value = reg[dest].I >> value;\ + } + #define ASR_RD_RM_I5 \ + {\ + C_FLAG = ((s32)reg[source].I >> (int)(shift - 1)) & 1 ? true : false;\ + value = (s32)reg[source].I >> (int)shift;\ + } + #define ASR_RD_RS \ + {\ + C_FLAG = ((s32)reg[dest].I >> (int)(value - 1)) & 1 ? true : false;\ + value = (s32)reg[dest].I >> (int)value;\ + } + #define ROR_RD_RS \ + {\ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false;\ + value = ((reg[dest].I << (32 - value)) |\ + (reg[dest].I >> value));\ + } + #define NEG_RD_RS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subfco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (0) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMP_RD_RS \ + {\ + register int Flags; \ + register int Result; \ + asm volatile("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } +#else +#define ADD_RD_RS_RN \ + asm ("add %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[source].I)); +#define ADD_RD_RS_O3 \ + asm ("add %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[source].I)); +#define ADD_RN_O8(d) \ + asm ("add %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[(d)].I)\ + : "r" (opcode & 255), "b" (reg[(d)].I)); +#define CMN_RD_RS \ + asm ("add %0, %1;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : \ + : "r" (value), "r" (reg[dest].I):"1"); +#define ADC_RD_RS \ + asm ("bt $0, C_FLAG;"\ + "adc %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setcb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[dest].I)); +#define SUB_RD_RS_RN \ + asm ("sub %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[source].I)); +#define SUB_RD_RS_O3 \ + asm ("sub %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[source].I)); +#define SUB_RN_O8(d) \ + asm ("sub %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[(d)].I)\ + : "r" (opcode & 255), "b" (reg[(d)].I)); +#define CMP_RN_O8(d) \ + asm ("sub %0, %1;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : \ + : "r" (opcode & 255), "r" (reg[(d)].I) : "1"); +#define SBC_RD_RS \ + asm volatile ("bt $0, C_FLAG;"\ + "cmc;"\ + "sbb %1, %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "r" (value), "b" (reg[dest].I) : "cc", "memory"); +#define LSL_RD_RM_I5 \ + asm ("shl %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[source].I), "c" (shift)); +#define LSL_RD_RS \ + asm ("shl %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[dest].I), "c" (value)); +#define LSR_RD_RM_I5 \ + asm ("shr %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[source].I), "c" (shift)); +#define LSR_RD_RS \ + asm ("shr %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[dest].I), "c" (value)); +#define ASR_RD_RM_I5 \ + asm ("sar %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[source].I), "c" (shift)); +#define ASR_RD_RS \ + asm ("sar %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[dest].I), "c" (value)); +#define ROR_RD_RS \ + asm ("ror %%cl, %%eax;"\ + "setcb C_FLAG;"\ + : "=a" (value)\ + : "a" (reg[dest].I), "c" (value)); +#define NEG_RD_RS \ + asm ("neg %%ebx;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : "=b" (reg[dest].I)\ + : "b" (reg[source].I)); +#define CMP_RD_RS \ + asm ("sub %0, %1;"\ + "setsb N_FLAG;"\ + "setzb Z_FLAG;"\ + "setncb C_FLAG;"\ + "setob V_FLAG;"\ + : \ + : "r" (value), "r" (reg[dest].I):"1"); +#endif +#else +#define ADD_RD_RS_RN \ + {\ + __asm mov eax, source\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm add ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define ADD_RD_RS_O3 \ + {\ + __asm mov eax, source\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm add ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define ADD_RN_O8(d) \ + {\ + __asm mov ebx, opcode\ + __asm and ebx, 255\ + __asm add dword ptr [OFFSET reg+4*(d)], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define CMN_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm add ebx, value\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define ADC_RD_RS \ + {\ + __asm mov ebx, dest\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm bt word ptr C_FLAG, 0\ + __asm adc ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define SUB_RD_RS_RN \ + {\ + __asm mov eax, source\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm sub ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define SUB_RD_RS_O3 \ + {\ + __asm mov eax, source\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm sub ebx, value\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define SUB_RN_O8(d) \ + {\ + __asm mov ebx, opcode\ + __asm and ebx, 255\ + __asm sub dword ptr [OFFSET reg + 4*(d)], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define CMP_RN_O8(d) \ + {\ + __asm mov eax, dword ptr [OFFSET reg+4*(d)]\ + __asm mov ebx, opcode\ + __asm and ebx, 255\ + __asm sub eax, ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define SBC_RD_RS \ + {\ + __asm mov ebx, dest\ + __asm mov ebx, dword ptr [OFFSET reg + 4*ebx]\ + __asm mov eax, value\ + __asm bt word ptr C_FLAG, 0\ + __asm cmc\ + __asm sbb ebx, eax\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg + 4*eax], ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define LSL_RD_RM_I5 \ + {\ + __asm mov eax, source\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shl eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define LSL_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr value\ + __asm shl eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define LSR_RD_RM_I5 \ + {\ + __asm mov eax, source\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr shift\ + __asm shr eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define LSR_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax]\ + __asm mov cl, byte ptr value\ + __asm shr eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define ASR_RD_RM_I5 \ + {\ + __asm mov eax, source\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm mov cl, byte ptr shift\ + __asm sar eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define ASR_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm mov cl, byte ptr value\ + __asm sar eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define ROR_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov eax, dword ptr [OFFSET reg + 4*eax]\ + __asm mov cl, byte ptr value\ + __asm ror eax, cl\ + __asm mov value, eax\ + __asm setc byte ptr C_FLAG\ + } +#define NEG_RD_RS \ + {\ + __asm mov ebx, source\ + __asm mov ebx, dword ptr [OFFSET reg+4*ebx]\ + __asm neg ebx\ + __asm mov eax, dest\ + __asm mov dword ptr [OFFSET reg+4*eax],ebx\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#define CMP_RD_RS \ + {\ + __asm mov eax, dest\ + __asm mov ebx, dword ptr [OFFSET reg+4*eax]\ + __asm sub ebx, value\ + __asm sets byte ptr N_FLAG\ + __asm setz byte ptr Z_FLAG\ + __asm setnc byte ptr C_FLAG\ + __asm seto byte ptr V_FLAG\ + } +#endif +#endif + +u32 opcode = CPUReadHalfWordQuick(armNextPC); +clockTicks = thumbCycles[opcode >> 8] + memoryWaitFetch[(armNextPC >> 24) & 15]; +#ifndef FINAL_VERSION +if(armNextPC == stop) { + armNextPC = armNextPC++; +} +#endif + +armNextPC = reg[15].I; +reg[15].I += 2; + +switch(opcode >> 8) { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + case 0x04: + case 0x05: + case 0x06: + case 0x07: + { + // LSL Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if(shift) { + LSL_RD_RM_I5; + } else { + value = reg[source].I; + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false : true); + } + break; + case 0x08: + case 0x09: + case 0x0a: + case 0x0b: + case 0x0c: + case 0x0d: + case 0x0e: + case 0x0f: + { + // LSR Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if(shift) { + LSR_RD_RM_I5; + } else { + C_FLAG = reg[source].I & 0x80000000 ? true : false; + value = 0; + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false : true); + } + break; + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + { + // ASR Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if(shift) { + ASR_RD_RM_I5; + } else { + if(reg[source].I & 0x80000000) { + value = 0xFFFFFFFF; + C_FLAG = true; + } else { + value = 0; + C_FLAG = false; + } + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false :true); + } + break; + case 0x18: + case 0x19: + { + // ADD Rd, Rs, Rn + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = reg[(opcode>>6)& 0x07].I; + ADD_RD_RS_RN; + } + break; + case 0x1a: + case 0x1b: + { + // SUB Rd, Rs, Rn + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = reg[(opcode>>6)& 0x07].I; + SUB_RD_RS_RN; + } + break; + case 0x1c: + case 0x1d: + { + // ADD Rd, Rs, #Offset3 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = (opcode >> 6) & 7; + ADD_RD_RS_O3; + } + break; + case 0x1e: + case 0x1f: + { + // SUB Rd, Rs, #Offset3 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = (opcode >> 6) & 7; + SUB_RD_RS_O3; + } + break; + case 0x20: + // MOV R0, #Offset8 + reg[0].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[0].I ? false : true); + break; + case 0x21: + // MOV R1, #Offset8 + reg[1].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[1].I ? false : true); + break; + case 0x22: + // MOV R2, #Offset8 + reg[2].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[2].I ? false : true); + break; + case 0x23: + // MOV R3, #Offset8 + reg[3].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[3].I ? false : true); + break; + case 0x24: + // MOV R4, #Offset8 + reg[4].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[4].I ? false : true); + break; + case 0x25: + // MOV R5, #Offset8 + reg[5].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[5].I ? false : true); + break; + case 0x26: + // MOV R6, #Offset8 + reg[6].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[6].I ? false : true); + break; + case 0x27: + // MOV R7, #Offset8 + reg[7].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[7].I ? false : true); + break; + case 0x28: + // CMP R0, #Offset8 + CMP_RN_O8(0); + break; + case 0x29: + // CMP R1, #Offset8 + CMP_RN_O8(1); + break; + case 0x2a: + // CMP R2, #Offset8 + CMP_RN_O8(2); + break; + case 0x2b: + // CMP R3, #Offset8 + CMP_RN_O8(3); + break; + case 0x2c: + // CMP R4, #Offset8 + CMP_RN_O8(4); + break; + case 0x2d: + // CMP R5, #Offset8 + CMP_RN_O8(5); + break; + case 0x2e: + // CMP R6, #Offset8 + CMP_RN_O8(6); + break; + case 0x2f: + // CMP R7, #Offset8 + CMP_RN_O8(7); + break; + case 0x30: + // ADD R0,#Offset8 + ADD_RN_O8(0); + break; + case 0x31: + // ADD R1,#Offset8 + ADD_RN_O8(1); + break; + case 0x32: + // ADD R2,#Offset8 + ADD_RN_O8(2); + break; + case 0x33: + // ADD R3,#Offset8 + ADD_RN_O8(3); + break; + case 0x34: + // ADD R4,#Offset8 + ADD_RN_O8(4); + break; + case 0x35: + // ADD R5,#Offset8 + ADD_RN_O8(5); + break; + case 0x36: + // ADD R6,#Offset8 + ADD_RN_O8(6); + break; + case 0x37: + // ADD R7,#Offset8 + ADD_RN_O8(7); + break; + case 0x38: + // SUB R0,#Offset8 + SUB_RN_O8(0); + break; + case 0x39: + // SUB R1,#Offset8 + SUB_RN_O8(1); + break; + case 0x3a: + // SUB R2,#Offset8 + SUB_RN_O8(2); + break; + case 0x3b: + // SUB R3,#Offset8 + SUB_RN_O8(3); + break; + case 0x3c: + // SUB R4,#Offset8 + SUB_RN_O8(4); + break; + case 0x3d: + // SUB R5,#Offset8 + SUB_RN_O8(5); + break; + case 0x3e: + // SUB R6,#Offset8 + SUB_RN_O8(6); + break; + case 0x3f: + // SUB R7,#Offset8 + SUB_RN_O8(7); + break; + case 0x40: + switch((opcode >> 6) & 3) { + case 0x00: + { + // AND Rd, Rs + int dest = opcode & 7; + reg[dest].I &= reg[(opcode >> 3)&7].I; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; +#ifdef BKPT_SUPPORT +#define THUMB_CONSOLE_OUTPUT(a,b) \ + if((opcode == 0x4000) && (reg[0].I == 0xC0DED00D)) {\ + extern void (*dbgOutput)(char *, u32);\ + dbgOutput((a), (b));\ + } +#else +#define THUMB_CONSOLE_OUTPUT(a,b) +#endif + THUMB_CONSOLE_OUTPUT(NULL, reg[2].I); + } + break; + case 0x01: + // EOR Rd, Rs + { + int dest = opcode & 7; + reg[dest].I ^= reg[(opcode >> 3)&7].I; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + } + break; + case 0x02: + // LSL Rd, Rs + { + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].B.B0; + if(value) { + if(value == 32) { + value = 0; + C_FLAG = (reg[dest].I & 1 ? true : false); + } else if(value < 32) { + LSL_RD_RS; + } else { + value = 0; + C_FLAG = false; + } + reg[dest].I = value; + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + case 0x03: + { + // LSR Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].B.B0; + if(value) { + if(value == 32) { + value = 0; + C_FLAG = (reg[dest].I & 0x80000000 ? true : false); + } else if(value < 32) { + LSR_RD_RS; + } else { + value = 0; + C_FLAG = false; + } + reg[dest].I = value; + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + } + break; + case 0x41: + switch((opcode >> 6) & 3) { + case 0x00: + { + // ASR Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].B.B0; + // ASR + if(value) { + if(value < 32) { + ASR_RD_RS; + reg[dest].I = value; + } else { + if(reg[dest].I & 0x80000000){ + reg[dest].I = 0xFFFFFFFF; + C_FLAG = true; + } else { + reg[dest].I = 0x00000000; + C_FLAG = false; + } + } + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + case 0x01: + { + // ADC Rd, Rs + int dest = opcode & 0x07; + u32 value = reg[(opcode >> 3)&7].I; + // ADC + ADC_RD_RS; + } + break; + case 0x02: + { + // SBC Rd, Rs + int dest = opcode & 0x07; + u32 value = reg[(opcode >> 3)&7].I; + + // SBC + SBC_RD_RS; + } + break; + case 0x03: + // ROR Rd, Rs + { + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].B.B0; + + if(value) { + value = value & 0x1f; + if(value == 0) { + C_FLAG = (reg[dest].I & 0x80000000 ? true : false); + } else { + ROR_RD_RS; + reg[dest].I = value; + } + } + clockTicks++; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + } + break; + } + break; + case 0x42: + switch((opcode >> 6) & 3) { + case 0x00: + { + // TST Rd, Rs + u32 value = reg[opcode & 7].I & reg[(opcode >> 3) & 7].I; + N_FLAG = value & 0x80000000 ? true : false; + Z_FLAG = value ? false : true; + } + break; + case 0x01: + { + // NEG Rd, Rs + int dest = opcode & 7; + int source = (opcode >> 3) & 7; + NEG_RD_RS; + } + break; + case 0x02: + { + // CMP Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].I; + CMP_RD_RS; + } + break; + case 0x03: + { + // CMN Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3)&7].I; + // CMN + CMN_RD_RS; + } + break; + } + break; + case 0x43: + switch((opcode >> 6) & 3) { + case 0x00: + { + // ORR Rd, Rs + int dest = opcode & 7; + reg[dest].I |= reg[(opcode >> 3) & 7].I; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x01: + { + // MUL Rd, Rs + int dest = opcode & 7; + u32 rm = reg[(opcode >> 3) & 7].I; + reg[dest].I = reg[dest].I * rm; + if (((s32)rm) < 0) + rm = ~rm; + if ((rm & 0xFFFFFF00) == 0) + clockTicks += 1; + else if ((rm & 0xFFFF0000) == 0) + clockTicks += 2; + else if ((rm & 0xFF000000) == 0) + clockTicks += 3; + else + clockTicks += 4; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x02: + { + // BIC Rd, Rs + int dest = opcode & 7; + reg[dest].I &= (~reg[(opcode >> 3) & 7].I); + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x03: + { + // MVN Rd, Rs + int dest = opcode & 7; + reg[dest].I = ~reg[(opcode >> 3) & 7].I; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + } + break; + case 0x44: + { + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + switch((opcode >> 6)& 3) { + default: + goto unknown_thumb; + case 1: + // ADD Rd, Hs + reg[dest].I += reg[base+8].I; + break; + case 2: + // ADD Hd, Rs + reg[dest+8].I += reg[base].I; + if(dest == 7) { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + case 3: + // ADD Hd, Hs + reg[dest+8].I += reg[base+8].I; + if(dest == 7) { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + } + } + break; + case 0x45: + { + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + u32 value; + switch((opcode >> 6) & 3) { + case 0: + // CMP Rd, Hs + value = reg[base].I; + CMP_RD_RS; + break; + case 1: + // CMP Rd, Hs + value = reg[base+8].I; + CMP_RD_RS; + break; + case 2: + // CMP Hd, Rs + value = reg[base].I; + dest += 8; + CMP_RD_RS; + break; + case 3: + // CMP Hd, Hs + value = reg[base+8].I; + dest += 8; + CMP_RD_RS; + break; + } + } + break; + case 0x46: + { + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + switch((opcode >> 6) & 3) { + case 0: + // this form should not be used... + // MOV Rd, Rs + reg[dest].I = reg[base].I; + break; + case 1: + // MOV Rd, Hs + reg[dest].I = reg[base+8].I; + break; + case 2: + // MOV Hd, Rs + reg[dest+8].I = reg[base].I; + if(dest == 7) { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + case 3: + // MOV Hd, Hs + reg[dest+8].I = reg[base+8].I; + if(dest == 7) { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + } + } + break; + case 0x47: + { + int base = (opcode >> 3) & 7; + switch((opcode >>6) & 3) { + case 0: + // BX Rs + reg[15].I = (reg[base].I) & 0xFFFFFFFE; + if(reg[base].I & 1) { + armState = false; + armNextPC = reg[15].I; + reg[15].I += 2; + } else { + armState = true; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + case 1: + // BX Hs + reg[15].I = (reg[8+base].I) & 0xFFFFFFFE; + if(reg[8+base].I & 1) { + armState = false; + armNextPC = reg[15].I; + reg[15].I += 2; + } else { + armState = true; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + default: + goto unknown_thumb; + } + } + break; + case 0x48: + // LDR R0,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[0].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x49: + // LDR R1,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[1].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4a: + // LDR R2,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[2].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4b: + // LDR R3,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[3].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4c: + // LDR R4,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[4].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4d: + // LDR R5,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[5].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4e: + // LDR R6,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[6].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x4f: + // LDR R7,[PC, #Imm] + { + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[7].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x50: + case 0x51: + // STR Rd, [Rs, Rn] + { + u32 + address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + CPUWriteMemory(address, + reg[opcode & 7].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x52: + case 0x53: + // STRH Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + CPUWriteHalfWord(address, + reg[opcode&7].W.W0); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x54: + case 0x55: + // STRB Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode >>6)&7].I; + CPUWriteByte(address, + reg[opcode & 7].B.B0); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x56: + case 0x57: + // LDSB Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + reg[opcode&7].I = (s8)CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x58: + case 0x59: + // LDR Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + reg[opcode&7].I = CPUReadMemory(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x5a: + case 0x5b: + // LDRH Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + reg[opcode&7].I = CPUReadHalfWord(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x5c: + case 0x5d: + // LDRB Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + reg[opcode&7].I = CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x5e: + case 0x5f: + // LDSH Rd, [Rs, Rn] + { + u32 address = reg[(opcode>>3)&7].I + reg[(opcode>>6)&7].I; + reg[opcode&7].I = (s16)CPUReadHalfWordSigned(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x66: + case 0x67: + // STR Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)<<2); + CPUWriteMemory(address, + reg[opcode&7].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x68: + case 0x69: + case 0x6a: + case 0x6b: + case 0x6c: + case 0x6d: + case 0x6e: + case 0x6f: + // LDR Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)<<2); + reg[opcode&7].I = CPUReadMemory(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x76: + case 0x77: + // STRB Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)); + CPUWriteByte(address, + reg[opcode&7].B.B0); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x78: + case 0x79: + case 0x7a: + case 0x7b: + case 0x7c: + case 0x7d: + case 0x7e: + case 0x7f: + // LDRB Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)); + reg[opcode&7].I = CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x80: + case 0x81: + case 0x82: + case 0x83: + case 0x84: + case 0x85: + case 0x86: + case 0x87: + // STRH Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)<<1); + CPUWriteHalfWord(address, + reg[opcode&7].W.W0); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x88: + case 0x89: + case 0x8a: + case 0x8b: + case 0x8c: + case 0x8d: + case 0x8e: + case 0x8f: + // LDRH Rd, [Rs, #Imm] + { + u32 address = reg[(opcode>>3)&7].I + (((opcode>>6)&31)<<1); + reg[opcode&7].I = CPUReadHalfWord(address); + clockTicks += CPUUpdateTicksAccess16(address); + } + break; + case 0x90: + // STR R0, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[0].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x91: + // STR R1, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[1].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x92: + // STR R2, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[2].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x93: + // STR R3, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[3].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x94: + // STR R4, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[4].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x95: + // STR R5, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[5].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x96: + // STR R6, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[6].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x97: + // STR R7, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + CPUWriteMemory(address, reg[7].I); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x98: + // LDR R0, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[0].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x99: + // LDR R1, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[1].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9a: + // LDR R2, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[2].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9b: + // LDR R3, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[3].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9c: + // LDR R4, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[4].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9d: + // LDR R5, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[5].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9e: + // LDR R6, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[6].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0x9f: + // LDR R7, [SP, #Imm] + { + u32 address = reg[13].I + ((opcode&255)<<2); + reg[7].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); + } + break; + case 0xa0: + // ADD R0, PC, Imm + reg[0].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa1: + // ADD R1, PC, Imm + reg[1].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa2: + // ADD R2, PC, Imm + reg[2].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa3: + // ADD R3, PC, Imm + reg[3].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa4: + // ADD R4, PC, Imm + reg[4].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa5: + // ADD R5, PC, Imm + reg[5].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa6: + // ADD R6, PC, Imm + reg[6].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa7: + // ADD R7, PC, Imm + reg[7].I = (reg[15].I & 0xFFFFFFFC) + ((opcode&255)<<2); + break; + case 0xa8: + // ADD R0, SP, Imm + reg[0].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xa9: + // ADD R1, SP, Imm + reg[1].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xaa: + // ADD R2, SP, Imm + reg[2].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xab: + // ADD R3, SP, Imm + reg[3].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xac: + // ADD R4, SP, Imm + reg[4].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xad: + // ADD R5, SP, Imm + reg[5].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xae: + // ADD R6, SP, Imm + reg[6].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xaf: + // ADD R7, SP, Imm + reg[7].I = reg[13].I + ((opcode&255)<<2); + break; + case 0xb0: + { + // ADD SP, Imm + int offset = (opcode & 127) << 2; + if(opcode & 0x80) + offset = -offset; + reg[13].I += offset; + } + break; +#define PUSH_REG(val, r) \ + if(opcode & (val)) {\ + CPUWriteMemory(address, reg[(r)].I);\ + if(offset)\ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address);\ + else\ + clockTicks += 1 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + address += 4;\ + } + case 0xb4: + // PUSH {Rlist} + { + int offset = 0; + u32 temp = reg[13].I - 4 * cpuBitsSet[opcode & 0xff]; + u32 address = temp & 0xFFFFFFFC; + PUSH_REG(1, 0); + PUSH_REG(2, 1); + PUSH_REG(4, 2); + PUSH_REG(8, 3); + PUSH_REG(16, 4); + PUSH_REG(32, 5); + PUSH_REG(64, 6); + PUSH_REG(128, 7); + reg[13].I = temp; + } + break; + case 0xb5: + // PUSH {Rlist, LR} + { + int offset = 0; + u32 temp = reg[13].I - 4 - 4 * cpuBitsSet[opcode & 0xff]; + u32 address = temp & 0xFFFFFFFC; + PUSH_REG(1, 0); + PUSH_REG(2, 1); + PUSH_REG(4, 2); + PUSH_REG(8, 3); + PUSH_REG(16, 4); + PUSH_REG(32, 5); + PUSH_REG(64, 6); + PUSH_REG(128, 7); + PUSH_REG(256, 14); + reg[13].I = temp; + } + break; +#define POP_REG(val, r) \ + if(opcode & (val)) {\ + reg[(r)].I = CPUReadMemory(address);\ + if(offset)\ + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address);\ + else\ + clockTicks += 2 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + address += 4;\ + } + case 0xbc: + // POP {Rlist} + { + int offset = 0; + u32 address = reg[13].I & 0xFFFFFFFC; + u32 temp = reg[13].I + 4*cpuBitsSet[opcode & 0xFF]; + POP_REG(1, 0); + POP_REG(2, 1); + POP_REG(4, 2); + POP_REG(8, 3); + POP_REG(16, 4); + POP_REG(32, 5); + POP_REG(64, 6); + POP_REG(128, 7); + reg[13].I = temp; + } + break; + case 0xbd: + // POP {Rlist, PC} + { + int offset = 0; + u32 address = reg[13].I & 0xFFFFFFFC; + u32 temp = reg[13].I + 4 + 4*cpuBitsSet[opcode & 0xFF]; + POP_REG(1, 0); + POP_REG(2, 1); + POP_REG(4, 2); + POP_REG(8, 3); + POP_REG(16, 4); + POP_REG(32, 5); + POP_REG(64, 6); + POP_REG(128, 7); + reg[15].I = (CPUReadMemory(address) & 0xFFFFFFFE); + if(offset) + clockTicks += CPUUpdateTicksAccessSeq32(address); + else + clockTicks += CPUUpdateTicksAccess32(address); + armNextPC = reg[15].I; + reg[15].I += 2; + reg[13].I = temp; + } + break; +#define THUMB_STM_REG(val,r,b) \ + if(opcode & (val)) {\ + CPUWriteMemory(address, reg[(r)].I);\ + if(!offset) {\ + reg[(b)].I = temp;\ + clockTicks += 1 + CPUUpdateTicksAccess32(address);\ + } else \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address);\ + offset = 1;\ + address += 4;\ + } + case 0xc0: + { + // STM R0!, {Rlist} + u32 address = reg[0].I & 0xFFFFFFFC; + u32 temp = reg[0].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 0); + THUMB_STM_REG(2, 1, 0); + THUMB_STM_REG(4, 2, 0); + THUMB_STM_REG(8, 3, 0); + THUMB_STM_REG(16, 4, 0); + THUMB_STM_REG(32, 5, 0); + THUMB_STM_REG(64, 6, 0); + THUMB_STM_REG(128, 7, 0); + } + break; + case 0xc1: + { + // STM R1!, {Rlist} + u32 address = reg[1].I & 0xFFFFFFFC; + u32 temp = reg[1].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 1); + THUMB_STM_REG(2, 1, 1); + THUMB_STM_REG(4, 2, 1); + THUMB_STM_REG(8, 3, 1); + THUMB_STM_REG(16, 4, 1); + THUMB_STM_REG(32, 5, 1); + THUMB_STM_REG(64, 6, 1); + THUMB_STM_REG(128, 7, 1); + } + break; + case 0xc2: + { + // STM R2!, {Rlist} + u32 address = reg[2].I & 0xFFFFFFFC; + u32 temp = reg[2].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 2); + THUMB_STM_REG(2, 1, 2); + THUMB_STM_REG(4, 2, 2); + THUMB_STM_REG(8, 3, 2); + THUMB_STM_REG(16, 4, 2); + THUMB_STM_REG(32, 5, 2); + THUMB_STM_REG(64, 6, 2); + THUMB_STM_REG(128, 7, 2); + } + break; + case 0xc3: + { + // STM R3!, {Rlist} + u32 address = reg[3].I & 0xFFFFFFFC; + u32 temp = reg[3].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 3); + THUMB_STM_REG(2, 1, 3); + THUMB_STM_REG(4, 2, 3); + THUMB_STM_REG(8, 3, 3); + THUMB_STM_REG(16, 4, 3); + THUMB_STM_REG(32, 5, 3); + THUMB_STM_REG(64, 6, 3); + THUMB_STM_REG(128, 7, 3); + } + break; + case 0xc4: + { + // STM R4!, {Rlist} + u32 address = reg[4].I & 0xFFFFFFFC; + u32 temp = reg[4].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 4); + THUMB_STM_REG(2, 1, 4); + THUMB_STM_REG(4, 2, 4); + THUMB_STM_REG(8, 3, 4); + THUMB_STM_REG(16, 4, 4); + THUMB_STM_REG(32, 5, 4); + THUMB_STM_REG(64, 6, 4); + THUMB_STM_REG(128, 7, 4); + } + break; + case 0xc5: + { + // STM R5!, {Rlist} + u32 address = reg[5].I & 0xFFFFFFFC; + u32 temp = reg[5].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 5); + THUMB_STM_REG(2, 1, 5); + THUMB_STM_REG(4, 2, 5); + THUMB_STM_REG(8, 3, 5); + THUMB_STM_REG(16, 4, 5); + THUMB_STM_REG(32, 5, 5); + THUMB_STM_REG(64, 6, 5); + THUMB_STM_REG(128, 7, 5); + } + break; + case 0xc6: + { + // STM R6!, {Rlist} + u32 address = reg[6].I & 0xFFFFFFFC; + u32 temp = reg[6].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 6); + THUMB_STM_REG(2, 1, 6); + THUMB_STM_REG(4, 2, 6); + THUMB_STM_REG(8, 3, 6); + THUMB_STM_REG(16, 4, 6); + THUMB_STM_REG(32, 5, 6); + THUMB_STM_REG(64, 6, 6); + THUMB_STM_REG(128, 7, 6); + } + break; + case 0xc7: + { + // STM R7!, {Rlist} + u32 address = reg[7].I & 0xFFFFFFFC; + u32 temp = reg[7].I + 4*cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 7); + THUMB_STM_REG(2, 1, 7); + THUMB_STM_REG(4, 2, 7); + THUMB_STM_REG(8, 3, 7); + THUMB_STM_REG(16, 4, 7); + THUMB_STM_REG(32, 5, 7); + THUMB_STM_REG(64, 6, 7); + THUMB_STM_REG(128, 7, 7); + } + break; +#define THUMB_LDM_REG(val,r) \ + if(opcode & (val)) {\ + reg[(r)].I = CPUReadMemory(address);\ + if(offset)\ + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address);\ + else\ + clockTicks += 2 + CPUUpdateTicksAccess32(address);\ + offset = 1;\ + address += 4;\ + } + case 0xc8: + { + // LDM R0!, {Rlist} + u32 address = reg[0].I & 0xFFFFFFFC; + u32 temp = reg[0].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 1)) + reg[0].I = temp; + } + break; + case 0xc9: + { + // LDM R1!, {Rlist} + u32 address = reg[1].I & 0xFFFFFFFC; + u32 temp = reg[1].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 2)) + reg[1].I = temp; + } + break; + case 0xca: + { + // LDM R2!, {Rlist} + u32 address = reg[2].I & 0xFFFFFFFC; + u32 temp = reg[2].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 4)) + reg[2].I = temp; + } + break; + case 0xcb: + { + // LDM R3!, {Rlist} + u32 address = reg[3].I & 0xFFFFFFFC; + u32 temp = reg[3].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 8)) + reg[3].I = temp; + } + break; + case 0xcc: + { + // LDM R4!, {Rlist} + u32 address = reg[4].I & 0xFFFFFFFC; + u32 temp = reg[4].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 16)) + reg[4].I = temp; + } + break; + case 0xcd: + { + // LDM R5!, {Rlist} + u32 address = reg[5].I & 0xFFFFFFFC; + u32 temp = reg[5].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 32)) + reg[5].I = temp; + } + break; + case 0xce: + { + // LDM R6!, {Rlist} + u32 address = reg[6].I & 0xFFFFFFFC; + u32 temp = reg[6].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 64)) + reg[6].I = temp; + } + break; + case 0xcf: + { + // LDM R7!, {Rlist} + u32 address = reg[7].I & 0xFFFFFFFC; + u32 temp = reg[7].I + 4*cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if(!(opcode & 128)) + reg[7].I = temp; + } + break; + case 0xd0: + // BEQ offset + if(Z_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd1: + // BNE offset + if(!Z_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd2: + // BCS offset + if(C_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd3: + // BCC offset + if(!C_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd4: + // BMI offset + if(N_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd5: + // BPL offset + if(!N_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd6: + // BVS offset + if(V_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd7: + // BVC offset + if(!V_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd8: + // BHI offset + if(C_FLAG && !Z_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xd9: + // BLS offset + if(!C_FLAG || Z_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xda: + // BGE offset + if(N_FLAG == V_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xdb: + // BLT offset + if(N_FLAG != V_FLAG) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xdc: + // BGT offset + if(!Z_FLAG && (N_FLAG == V_FLAG)) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xdd: + // BLE offset + if(Z_FLAG || (N_FLAG != V_FLAG)) { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks += 2; + } + break; + case 0xdf: + // SWI #comment + CPUSoftwareInterrupt(opcode & 0xFF); + break; + case 0xe0: + case 0xe1: + case 0xe2: + case 0xe3: + case 0xe4: + case 0xe5: + case 0xe6: + case 0xe7: + { + // B offset + int offset = (opcode & 0x3FF) << 1; + if(opcode & 0x0400) + offset |= 0xFFFFF800; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 2; + } + break; + case 0xf0: + case 0xf1: + case 0xf2: + case 0xf3: + { + // BLL #offset + int offset = (opcode & 0x7FF); + reg[14].I = reg[15].I + (offset << 12); + } + break; + case 0xf4: + case 0xf5: + case 0xf6: + case 0xf7: + { + // BLL #offset + int offset = (opcode & 0x7FF); + reg[14].I = reg[15].I + ((offset << 12) | 0xFF800000); + } + break; + case 0xf8: + case 0xf9: + case 0xfa: + case 0xfb: + case 0xfc: + case 0xfd: + case 0xfe: + case 0xff: + { + // BLH #offset + int offset = (opcode & 0x7FF); + u32 temp = reg[15].I-2; + reg[15].I = (reg[14].I + (offset<<1))&0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + reg[14].I = temp|1; + } + break; +#ifdef BKPT_SUPPORT + case 0xbe: + // BKPT #comment + extern void (*dbgSignal)(int,int); + reg[15].I -= 2; + armNextPC -= 2; + dbgSignal(5, opcode & 255); + return; +#endif + case 0xb1: + case 0xb2: + case 0xb3: + case 0xb6: + case 0xb7: + case 0xb8: + case 0xb9: + case 0xba: + case 0xbb: +#ifndef BKPT_SUPPORT + case 0xbe: +#endif + case 0xbf: + case 0xde: + default: + unknown_thumb: +#ifdef DEV_VERSION + if(systemVerbose & VERBOSE_UNDEFINED) + log("Undefined THUMB instruction %04x at %08x\n", opcode, armNextPC-2); +#endif + CPUUndefinedException(); + break; +} diff --git a/src/unzip.cpp b/src/unzip.cpp new file mode 100644 index 00000000..98c50e94 --- /dev/null +++ b/src/unzip.cpp @@ -0,0 +1,1270 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/* unzip.c -- IO on .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Read unzip.h for more info +*/ + + +#include +#include +#include +#include "zlib.h" +#include "unzip.h" + +#ifdef STDC +# include +# include +# include +#endif +#ifdef NO_ERRNO_H + extern int errno; +#else +# include +#endif + + +#ifndef local +# define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + + + +#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ + !defined(CASESENSITIVITYDEFAULT_NO) +#define CASESENSITIVITYDEFAULT_NO +#endif + + +#ifndef UNZ_BUFSIZE +#define UNZ_BUFSIZE (16384) +#endif + +#ifndef UNZ_MAXFILENAMEINZIP +#define UNZ_MAXFILENAMEINZIP (256) +#endif + +#ifndef ALLOC +# define ALLOC(size) (malloc(size)) +#endif +#ifndef TRYFREE +# define TRYFREE(p) {if (p) free(p);} +#endif + +#define SIZECENTRALDIRITEM (0x2e) +#define SIZEZIPLOCALHEADER (0x1e) + + +/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ + +#ifndef SEEK_CUR +#define SEEK_CUR 1 +#endif + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +#ifndef SEEK_SET +#define SEEK_SET 0 +#endif + +const char unz_copyright[] = + " unzip 0.15 Copyright 1998 Gilles Vollant "; + +/* unz_file_info_interntal contain internal info about a file in zipfile*/ +typedef struct unz_file_info_internal_s +{ + uLong offset_curfile;/* relative offset of local header 4 bytes */ +} unz_file_info_internal; + + +/* file_in_zip_read_info_s contain internal information about a file in zipfile, + when reading and decompress it */ +typedef struct +{ + char *read_buffer; /* internal buffer for compressed data */ + z_stream stream; /* zLib stream structure for inflate */ + + uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ + uLong stream_initialised; /* flag set if stream structure is initialised*/ + + uLong offset_local_extrafield;/* offset of the local extra field */ + uInt size_local_extrafield;/* size of the local extra field */ + uLong pos_local_extrafield; /* position in the local extra field in read*/ + + uLong crc32; /* crc32 of all data uncompressed */ + uLong crc32_wait; /* crc32 we must obtain after decompress all */ + uLong rest_read_compressed; /* number of byte to be decompressed */ + uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ + FILE* file; /* io structore of the zipfile */ + uLong compression_method; /* compression method (0==store) */ + uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ +} file_in_zip_read_info_s; + + +/* unz_s contain internal information about the zipfile +*/ +typedef struct +{ + FILE* file; /* io structore of the zipfile */ + unz_global_info gi; /* public global information */ + uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ + uLong num_file; /* number of the current file in the zipfile*/ + uLong pos_in_central_dir; /* pos of the current file in the central dir*/ + uLong current_file_ok; /* flag about the usability of the current file*/ + uLong central_pos; /* position of the beginning of the central dir*/ + + uLong size_central_dir; /* size of the central directory */ + uLong offset_central_dir; /* offset of start of central directory with + respect to the starting disk number */ + + unz_file_info cur_file_info; /* public info about the current file in zip*/ + unz_file_info_internal cur_file_info_internal; /* private info about it*/ + file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current + file if we are decompressing it */ +} unz_s; + + +/* =========================================================================== + Read a byte from a gz_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. +*/ + + +local int unzlocal_getByte(FILE *fin,int *pi) +{ + unsigned char c; + int err = fread(&c, 1, 1, fin); + if (err==1) + { + *pi = (int)c; + return UNZ_OK; + } + else + { + if (ferror(fin)) + return UNZ_ERRNO; + else + return UNZ_EOF; + } +} + + +/* =========================================================================== + Reads a long in LSB order from the given gz_stream. Sets +*/ +local int unzlocal_getShort (FILE *fin,uLong *pX) +{ + uLong x ; + int i; + int err; + + err = unzlocal_getByte(fin,&i); + x = (uLong)i; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<8; + + if (err==UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + +local int unzlocal_getLong (FILE *fin,uLong *pX) +{ + uLong x ; + int i; + int err; + + err = unzlocal_getByte(fin,&i); + x = (uLong)i; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<8; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<16; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<24; + + if (err==UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + + +/* My own strcmpi / strcasecmp */ +local int strcmpcasenosensitive_internal (const char *fileName1, + const char *fileName2) +{ + for (;;) + { + char c1=*(fileName1++); + char c2=*(fileName2++); + if ((c1>='a') && (c1<='z')) + c1 -= 0x20; + if ((c2>='a') && (c2<='z')) + c2 -= 0x20; + if (c1=='\0') + return ((c2=='\0') ? 0 : -1); + if (c2=='\0') + return 1; + if (c1c2) + return 1; + } +} + + +#ifdef CASESENSITIVITYDEFAULT_NO +#define CASESENSITIVITYDEFAULTVALUE 2 +#else +#define CASESENSITIVITYDEFAULTVALUE 1 +#endif + +#ifndef STRCMPCASENOSENTIVEFUNCTION +#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal +#endif + +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) + +*/ +extern int ZEXPORT unzStringFileNameCompare (const char *fileName1, + const char *fileName2, + int iCaseSensitivity) +{ + if (iCaseSensitivity==0) + iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; + + if (iCaseSensitivity==1) + return strcmp(fileName1,fileName2); + + return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); +} + +#define BUFREADCOMMENT (0x400) + +/* + Locate the Central directory of a zipfile (at the end, just before + the global comment) +*/ +local uLong unzlocal_SearchCentralDir(FILE *fin) +{ + unsigned char* buf; + uLong uSizeFile; + uLong uBackRead; + uLong uMaxBack=0xffff; /* maximum size of global comment */ + uLong uPosFound=0; + + if (fseek(fin,0,SEEK_END) != 0) + return 0; + + + uSizeFile = ftell( fin ); + + if (uMaxBack>uSizeFile) + uMaxBack = uSizeFile; + + buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); + if (buf==NULL) + return 0; + + uBackRead = 4; + while (uBackReaduMaxBack) + uBackRead = uMaxBack; + else + uBackRead+=BUFREADCOMMENT; + uReadPos = uSizeFile-uBackRead ; + + uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? + (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); + if (fseek(fin,uReadPos,SEEK_SET)!=0) + break; + + if (fread(buf,(uInt)uReadSize,1,fin)!=1) + break; + + for (i=(int)uReadSize-3; (i--)>0;) + if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && + ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) + { + uPosFound = uReadPos+i; + break; + } + + if (uPosFound!=0) + break; + } + TRYFREE(buf); + return uPosFound; +} + +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer + "zlib/zlib109.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ +extern unzFile ZEXPORT unzOpen (const char *path) +{ + unz_s us; + unz_s *s; + uLong central_pos,uL; + FILE * fin ; + + uLong number_disk; /* number of the current dist, used for + spaning ZIP, unsupported, always 0*/ + uLong number_disk_with_CD; /* number the the disk with central dir, used + for spaning ZIP, unsupported, always 0*/ + uLong number_entry_CD; /* total number of entries in + the central dir + (same than number_entry on nospan) */ + + int err=UNZ_OK; + + if (unz_copyright[0]!=' ') + return NULL; + + fin=fopen(path,"rb"); + if (fin==NULL) + return NULL; + + central_pos = unzlocal_SearchCentralDir(fin); + if (central_pos==0) + err=UNZ_ERRNO; + + if (fseek(fin,central_pos,SEEK_SET)!=0) + err=UNZ_ERRNO; + + /* the signature, already checked */ + if (unzlocal_getLong(fin,&uL)!=UNZ_OK) + err=UNZ_ERRNO; + + /* number of this disk */ + if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK) + err=UNZ_ERRNO; + + /* number of the disk with the start of the central directory */ + if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK) + err=UNZ_ERRNO; + + /* total number of entries in the central dir on this disk */ + if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK) + err=UNZ_ERRNO; + + /* total number of entries in the central dir */ + if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK) + err=UNZ_ERRNO; + + if ((number_entry_CD!=us.gi.number_entry) || + (number_disk_with_CD!=0) || + (number_disk!=0)) + err=UNZ_BADZIPFILE; + + /* size of the central directory */ + if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK) + err=UNZ_ERRNO; + + /* offset of start of central directory with respect to the + starting disk number */ + if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK) + err=UNZ_ERRNO; + + /* zipfile comment length */ + if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK) + err=UNZ_ERRNO; + + if ((central_pospfile_in_zip_read!=NULL) + unzCloseCurrentFile(file); + + fclose(s->file); + TRYFREE(s); + return UNZ_OK; +} + + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ +extern int ZEXPORT unzGetGlobalInfo (unzFile file, + unz_global_info *pglobal_info) +{ + unz_s* s; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + *pglobal_info=s->gi; + return UNZ_OK; +} + + +/* + Translate date/time from Dos format to tm_unz (readable more easilty) +*/ +local void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz *ptm) +{ + uLong uDate; + uDate = (uLong)(ulDosDate>>16); + ptm->tm_mday = (uInt)(uDate&0x1f) ; + ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; + ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; + + ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); + ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; + ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; +} + +/* + Get Info about the current file in the zipfile, with internal only info +*/ +local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, + unz_file_info *pfile_info, + unz_file_info_internal + *pfile_info_internal, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); + +local int unzlocal_GetCurrentFileInfoInternal (unzFile file, + unz_file_info *pfile_info, + unz_file_info_internal *pfile_info_internal, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize) +{ + unz_s* s; + unz_file_info file_info; + unz_file_info_internal file_info_internal; + int err=UNZ_OK; + uLong uMagic; + long lSeek=0; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0) + err=UNZ_ERRNO; + + + /* we check the magic */ + if (err==UNZ_OK) + if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) + err=UNZ_ERRNO; + else if (uMagic!=0x02014b50) + err=UNZ_BADZIPFILE; + + if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK) + err=UNZ_ERRNO; + + unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); + + if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK) + err=UNZ_ERRNO; + + lSeek+=file_info.size_filename; + if ((err==UNZ_OK) && (szFileName!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_filename0) && (fileNameBufferSize>0)) + if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek -= uSizeRead; + } + + + if ((err==UNZ_OK) && (extraField!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_extrafile,lSeek,SEEK_CUR)==0) + lSeek=0; + else + err=UNZ_ERRNO; + if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) + if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek += file_info.size_file_extra - uSizeRead; + } + else + lSeek+=file_info.size_file_extra; + + + if ((err==UNZ_OK) && (szComment!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_commentfile,lSeek,SEEK_CUR)==0) + lSeek=0; + else + err=UNZ_ERRNO; + if ((file_info.size_file_comment>0) && (commentBufferSize>0)) + if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek+=file_info.size_file_comment - uSizeRead; + } + else + lSeek+=file_info.size_file_comment; + + if ((err==UNZ_OK) && (pfile_info!=NULL)) + *pfile_info=file_info; + + if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) + *pfile_info_internal=file_info_internal; + + return err; +} + + + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. +*/ +extern int ZEXPORT unzGetCurrentFileInfo (unzFile file, + unz_file_info *pfile_info, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize) +{ + return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, + szFileName,fileNameBufferSize, + extraField,extraFieldBufferSize, + szComment,commentBufferSize); +} + +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem +*/ +extern int ZEXPORT unzGoToFirstFile (unzFile file) +{ + int err=UNZ_OK; + unz_s* s; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + s->pos_in_central_dir=s->offset_central_dir; + s->num_file=0; + err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, + &s->cur_file_info_internal, + NULL,0,NULL,0,NULL,0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + + +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. +*/ +extern int ZEXPORT unzGoToNextFile (unzFile file) +{ + unz_s* s; + int err; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + if (s->num_file+1==s->gi.number_entry) + return UNZ_END_OF_LIST_OF_FILE; + + s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + + s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; + s->num_file++; + err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, + &s->cur_file_info_internal, + NULL,0,NULL,0,NULL,0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + + +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzipStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found +*/ +extern int ZEXPORT unzLocateFile (unzFile file, + const char *szFileName, + int iCaseSensitivity) +{ + unz_s* s; + int err; + + + uLong num_fileSaved; + uLong pos_in_central_dirSaved; + + + if (file==NULL) + return UNZ_PARAMERROR; + + if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) + return UNZ_PARAMERROR; + + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + + num_fileSaved = s->num_file; + pos_in_central_dirSaved = s->pos_in_central_dir; + + err = unzGoToFirstFile(file); + + while (err == UNZ_OK) + { + char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; + unzGetCurrentFileInfo(file,NULL, + szCurrentFileName,sizeof(szCurrentFileName)-1, + NULL,0,NULL,0); + if (unzStringFileNameCompare(szCurrentFileName, + szFileName,iCaseSensitivity)==0) + return UNZ_OK; + err = unzGoToNextFile(file); + } + + s->num_file = num_fileSaved ; + s->pos_in_central_dir = pos_in_central_dirSaved ; + return err; +} + + +/* + Read the local header of the current zipfile + Check the coherency of the local header and info in the end of central + directory about this file + store in *piSizeVar the size of extra info in local header + (filename and size of extra field data) +*/ +local int unzlocal_CheckCurrentFileCoherencyHeader (unz_s *s, + uInt *piSizeVar, + uLong *poffset_local_extrafield, + uInt *psize_local_extrafield) +{ + uLong uMagic,uData,uFlags; + uLong size_filename; + uLong size_extra_field; + int err=UNZ_OK; + + *piSizeVar = 0; + *poffset_local_extrafield = 0; + *psize_local_extrafield = 0; + + if (fseek(s->file,s->cur_file_info_internal.offset_curfile + + s->byte_before_the_zipfile,SEEK_SET)!=0) + return UNZ_ERRNO; + + + if (err==UNZ_OK) + if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) + err=UNZ_ERRNO; + else if (uMagic!=0x04034b50) + err=UNZ_BADZIPFILE; + + if (unzlocal_getShort(s->file,&uData) != UNZ_OK) + err=UNZ_ERRNO; +/* + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) + err=UNZ_BADZIPFILE; +*/ + if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&uData) != UNZ_OK) + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) + err=UNZ_BADZIPFILE; + + if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && + (s->cur_file_info.compression_method!=Z_DEFLATED)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */ + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + + if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK) + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) + err=UNZ_BADZIPFILE; + + *piSizeVar += (uInt)size_filename; + + if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK) + err=UNZ_ERRNO; + *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + + SIZEZIPLOCALHEADER + size_filename; + *psize_local_extrafield = (uInt)size_extra_field; + + *piSizeVar += (uInt)size_extra_field; + + return err; +} + +/* + Open for reading data the current file in the zipfile. + If there is no error and the file is opened, the return value is UNZ_OK. +*/ +extern int ZEXPORT unzOpenCurrentFile (unzFile file) +{ + int err=UNZ_OK; + int Store; + uInt iSizeVar; + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + uLong offset_local_extrafield; /* offset of the local extra field */ + uInt size_local_extrafield; /* size of the local extra field */ + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_PARAMERROR; + + if (s->pfile_in_zip_read != NULL) + unzCloseCurrentFile(file); + + if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, + &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) + return UNZ_BADZIPFILE; + + pfile_in_zip_read_info = (file_in_zip_read_info_s*) + ALLOC(sizeof(file_in_zip_read_info_s)); + if (pfile_in_zip_read_info==NULL) + return UNZ_INTERNALERROR; + + pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); + pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; + pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; + pfile_in_zip_read_info->pos_local_extrafield=0; + + if (pfile_in_zip_read_info->read_buffer==NULL) + { + TRYFREE(pfile_in_zip_read_info); + return UNZ_INTERNALERROR; + } + + pfile_in_zip_read_info->stream_initialised=0; + + if ((s->cur_file_info.compression_method!=0) && + (s->cur_file_info.compression_method!=Z_DEFLATED)) + err=UNZ_BADZIPFILE; + Store = s->cur_file_info.compression_method==0; + + pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; + pfile_in_zip_read_info->crc32=0; + pfile_in_zip_read_info->compression_method = + s->cur_file_info.compression_method; + pfile_in_zip_read_info->file=s->file; + pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; + + pfile_in_zip_read_info->stream.total_out = 0; + + if (!Store) + { + pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; + pfile_in_zip_read_info->stream.zfree = (free_func)0; + pfile_in_zip_read_info->stream.opaque = (voidpf)0; + + err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); + if (err == Z_OK) + pfile_in_zip_read_info->stream_initialised=1; + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. + * In unzip, i don't wait absolutely Z_STREAM_END because I known the + * size of both compressed and uncompressed data + */ + } + pfile_in_zip_read_info->rest_read_compressed = + s->cur_file_info.compressed_size ; + pfile_in_zip_read_info->rest_read_uncompressed = + s->cur_file_info.uncompressed_size ; + + + pfile_in_zip_read_info->pos_in_zipfile = + s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + + iSizeVar; + + pfile_in_zip_read_info->stream.avail_in = (uInt)0; + + + s->pfile_in_zip_read = pfile_in_zip_read_info; + return UNZ_OK; +} + + +/* + Read bytes from the current file. + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) +*/ +extern int ZEXPORT unzReadCurrentFile (unzFile file, voidp buf, unsigned len) +{ + int err=UNZ_OK; + uInt iRead = 0; + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + + if ((pfile_in_zip_read_info->read_buffer == NULL)) + return UNZ_END_OF_LIST_OF_FILE; + if (len==0) + return 0; + + pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; + + pfile_in_zip_read_info->stream.avail_out = (uInt)len; + + if (len>pfile_in_zip_read_info->rest_read_uncompressed) + pfile_in_zip_read_info->stream.avail_out = + (uInt)pfile_in_zip_read_info->rest_read_uncompressed; + + while (pfile_in_zip_read_info->stream.avail_out>0) + { + if ((pfile_in_zip_read_info->stream.avail_in==0) && + (pfile_in_zip_read_info->rest_read_compressed>0)) + { + uInt uReadThis = UNZ_BUFSIZE; + if (pfile_in_zip_read_info->rest_read_compressedrest_read_compressed; + if (uReadThis == 0) + return UNZ_EOF; + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->pos_in_zipfile + + pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0) + return UNZ_ERRNO; + if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1, + pfile_in_zip_read_info->file)!=1) + return UNZ_ERRNO; + pfile_in_zip_read_info->pos_in_zipfile += uReadThis; + + pfile_in_zip_read_info->rest_read_compressed-=uReadThis; + + pfile_in_zip_read_info->stream.next_in = + (Bytef*)pfile_in_zip_read_info->read_buffer; + pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; + } + + if (pfile_in_zip_read_info->compression_method==0) + { + uInt uDoCopy,i ; + if (pfile_in_zip_read_info->stream.avail_out < + pfile_in_zip_read_info->stream.avail_in) + uDoCopy = pfile_in_zip_read_info->stream.avail_out ; + else + uDoCopy = pfile_in_zip_read_info->stream.avail_in ; + + for (i=0;istream.next_out+i) = + *(pfile_in_zip_read_info->stream.next_in+i); + + pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, + pfile_in_zip_read_info->stream.next_out, + uDoCopy); + pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; + pfile_in_zip_read_info->stream.avail_in -= uDoCopy; + pfile_in_zip_read_info->stream.avail_out -= uDoCopy; + pfile_in_zip_read_info->stream.next_out += uDoCopy; + pfile_in_zip_read_info->stream.next_in += uDoCopy; + pfile_in_zip_read_info->stream.total_out += uDoCopy; + iRead += uDoCopy; + } + else + { + uLong uTotalOutBefore,uTotalOutAfter; + const Bytef *bufBefore; + uLong uOutThis; + int flush=Z_SYNC_FLUSH; + + uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; + bufBefore = pfile_in_zip_read_info->stream.next_out; + + /* + if ((pfile_in_zip_read_info->rest_read_uncompressed == + pfile_in_zip_read_info->stream.avail_out) && + (pfile_in_zip_read_info->rest_read_compressed == 0)) + flush = Z_FINISH; + */ + err=inflate(&pfile_in_zip_read_info->stream,flush); + + uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; + uOutThis = uTotalOutAfter-uTotalOutBefore; + + pfile_in_zip_read_info->crc32 = + crc32(pfile_in_zip_read_info->crc32,bufBefore, + (uInt)(uOutThis)); + + pfile_in_zip_read_info->rest_read_uncompressed -= + uOutThis; + + iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); + + if (err==Z_STREAM_END) + return (iRead==0) ? UNZ_EOF : iRead; + if (err!=Z_OK) + break; + } + } + + if (err==Z_OK) + return iRead; + return err; +} + + +/* + Give the current position in uncompressed data +*/ +extern z_off_t ZEXPORT unztell (unzFile file) +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + return (z_off_t)pfile_in_zip_read_info->stream.total_out; +} + + +/* + return 1 if the end of file was reached, 0 elsewhere +*/ +extern int ZEXPORT unzeof (unzFile file) +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + return 1; + else + return 0; +} + + + +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field that can be read + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code +*/ +extern int ZEXPORT unzGetLocalExtrafield (unzFile file,voidp buf,unsigned len) +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + uInt read_now; + uLong size_to_read; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + size_to_read = (pfile_in_zip_read_info->size_local_extrafield - + pfile_in_zip_read_info->pos_local_extrafield); + + if (buf==NULL) + return (int)size_to_read; + + if (len>size_to_read) + read_now = (uInt)size_to_read; + else + read_now = (uInt)len ; + + if (read_now==0) + return 0; + + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->offset_local_extrafield + + pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0) + return UNZ_ERRNO; + + if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1) + return UNZ_ERRNO; + + return (int)read_now; +} + +/* + Close the file in zip opened with unzipOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good +*/ +extern int ZEXPORT unzCloseCurrentFile (unzFile file) +{ + int err=UNZ_OK; + + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + { + if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) + err=UNZ_CRCERROR; + } + + + TRYFREE(pfile_in_zip_read_info->read_buffer); + pfile_in_zip_read_info->read_buffer = NULL; + if (pfile_in_zip_read_info->stream_initialised) + inflateEnd(&pfile_in_zip_read_info->stream); + + pfile_in_zip_read_info->stream_initialised = 0; + TRYFREE(pfile_in_zip_read_info); + + s->pfile_in_zip_read=NULL; + + return err; +} + + +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 +*/ +extern int ZEXPORT unzGetGlobalComment (unzFile file, + char *szComment, + uLong uSizeBuf) +{ + //int err=UNZ_OK; + unz_s* s; + uLong uReadThis ; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + + uReadThis = uSizeBuf; + if (uReadThis>s->gi.size_comment) + uReadThis = s->gi.size_comment; + + if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0) + return UNZ_ERRNO; + + if (uReadThis>0) + { + *szComment='\0'; + if (fread(szComment,(uInt)uReadThis,1,s->file)!=1) + return UNZ_ERRNO; + } + + if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) + *(szComment+s->gi.size_comment)='\0'; + return (int)uReadThis; +} diff --git a/src/unzip.h b/src/unzip.h new file mode 100644 index 00000000..003e7381 --- /dev/null +++ b/src/unzip.h @@ -0,0 +1,294 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/* unzip.h -- IO for uncompress .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Copyright (C) 1998 Gilles Vollant + + This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g + WinZip, InfoZip tools and compatible. + Encryption and multi volume ZipFile (span) are not supported. + Old compressions used by old PKZip 1.x are not supported + + THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE + CAN CHANGE IN FUTURE VERSION !! + I WAIT FEEDBACK at mail info@winimage.com + Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution + + Condition of use and distribution are the same than zlib : + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + +*/ +/* for more info about .ZIP format, see + ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip + PkWare has also a specification at : + ftp://ftp.pkware.com/probdesc.zip */ + +#ifndef _unz_H +#define _unz_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _ZLIB_H +#include "zlib.h" +#endif + +#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) +/* like the STRICT of WIN32, we define a pointer that cannot be converted + from (void*) without cast */ +typedef struct TagunzFile__ { int unused; } unzFile__; +typedef unzFile__ *unzFile; +#else +typedef voidp unzFile; +#endif + + +#define UNZ_OK (0) +#define UNZ_END_OF_LIST_OF_FILE (-100) +#define UNZ_ERRNO (Z_ERRNO) +#define UNZ_EOF (0) +#define UNZ_PARAMERROR (-102) +#define UNZ_BADZIPFILE (-103) +#define UNZ_INTERNALERROR (-104) +#define UNZ_CRCERROR (-105) + +/* tm_unz contain date/time info */ +typedef struct tm_unz_s +{ + uInt tm_sec; /* seconds after the minute - [0,59] */ + uInt tm_min; /* minutes after the hour - [0,59] */ + uInt tm_hour; /* hours since midnight - [0,23] */ + uInt tm_mday; /* day of the month - [1,31] */ + uInt tm_mon; /* months since January - [0,11] */ + uInt tm_year; /* years - [1980..2044] */ +} tm_unz; + +/* unz_global_info structure contain global data about the ZIPfile + These data comes from the end of central dir */ +typedef struct unz_global_info_s +{ + uLong number_entry; /* total number of entries in + the central dir on this disk */ + uLong size_comment; /* size of the global comment of the zipfile */ +} unz_global_info; + + +/* unz_file_info contain information about a file in the zipfile */ +typedef struct unz_file_info_s +{ + uLong version; /* version made by 2 bytes */ + uLong version_needed; /* version needed to extract 2 bytes */ + uLong flag; /* general purpose bit flag 2 bytes */ + uLong compression_method; /* compression method 2 bytes */ + uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ + uLong crc; /* crc-32 4 bytes */ + uLong compressed_size; /* compressed size 4 bytes */ + uLong uncompressed_size; /* uncompressed size 4 bytes */ + uLong size_filename; /* filename length 2 bytes */ + uLong size_file_extra; /* extra field length 2 bytes */ + uLong size_file_comment; /* file comment length 2 bytes */ + + uLong disk_num_start; /* disk number start 2 bytes */ + uLong internal_fa; /* internal file attributes 2 bytes */ + uLong external_fa; /* external file attributes 4 bytes */ + + tm_unz tmu_date; +} unz_file_info; + +extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1, + const char* fileName2, + int iCaseSensitivity)); +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) +*/ + + +extern unzFile ZEXPORT unzOpen OF((const char *path)); +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer + "zlib/zlib111.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ + +extern int ZEXPORT unzClose OF((unzFile file)); +/* + Close a ZipFile opened with unzipOpen. + If there is files inside the .Zip opened with unzOpenCurrentFile (see later), + these files MUST be closed with unzipCloseCurrentFile before call unzipClose. + return UNZ_OK if there is no problem. */ + +extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, + unz_global_info *pglobal_info)); +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ + + +extern int ZEXPORT unzGetGlobalComment OF((unzFile file, + char *szComment, + uLong uSizeBuf)); +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 +*/ + + +/***************************************************************************/ +/* Unzip package allow you browse the directory of the zipfile */ + +extern int ZEXPORT unzGoToFirstFile OF((unzFile file)); +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem +*/ + +extern int ZEXPORT unzGoToNextFile OF((unzFile file)); +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. +*/ + +extern int ZEXPORT unzLocateFile OF((unzFile file, + const char *szFileName, + int iCaseSensitivity)); +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found +*/ + + +extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, + unz_file_info *pfile_info, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); +/* + Get Info about the current file + if pfile_info!=NULL, the *pfile_info structure will contain somes info about + the current file + if szFileName!=NULL, the filemane string will be copied in szFileName + (fileNameBufferSize is the size of the buffer) + if extraField!=NULL, the extra field information will be copied in extraField + (extraFieldBufferSize is the size of the buffer). + This is the Central-header version of the extra field + if szComment!=NULL, the comment string of the file will be copied in szComment + (commentBufferSize is the size of the buffer) +*/ + +/***************************************************************************/ +/* for reading the content of the current zipfile, you can open it, read data + from it, and close it (you can close it before reading all the file) + */ + +extern int ZEXPORT unzOpenCurrentFile OF((unzFile file)); +/* + Open for reading data the current file in the zipfile. + If there is no error, the return value is UNZ_OK. +*/ + +extern int ZEXPORT unzCloseCurrentFile OF((unzFile file)); +/* + Close the file in zip opened with unzOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good +*/ + + +extern int ZEXPORT unzReadCurrentFile OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read bytes from the current file (opened by unzOpenCurrentFile) + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) +*/ + +extern z_off_t ZEXPORT unztell OF((unzFile file)); +/* + Give the current position in uncompressed data +*/ + +extern int ZEXPORT unzeof OF((unzFile file)); +/* + return 1 if the end of file was reached, 0 elsewhere +*/ + +extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* _unz_H */ diff --git a/src/win32/AVIWrite.cpp b/src/win32/AVIWrite.cpp new file mode 100644 index 00000000..4943224e --- /dev/null +++ b/src/win32/AVIWrite.cpp @@ -0,0 +1,187 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "AVIWrite.h" + +AVIWrite::AVIWrite() +{ + m_failed = false; + m_file = NULL; + m_stream = NULL; + m_streamCompressed = NULL; + m_streamSound = NULL; + m_samplesSound = 0; + + AVIFileInit(); +} + +AVIWrite::~AVIWrite() +{ + if(m_streamSound) + AVIStreamClose(m_streamSound); + + if(m_streamCompressed) + AVIStreamClose(m_streamCompressed); + + if(m_stream) + AVIStreamClose(m_stream); + + if(m_file) + AVIFileClose(m_file); + + AVIFileExit(); +} + +void AVIWrite::SetVideoFormat(BITMAPINFOHEADER *bh) +{ + // force size to 0x28 to avoid extra fields + memcpy(&m_bitmap, bh, 0x28); +} + +void AVIWrite::SetSoundFormat(WAVEFORMATEX *format) +{ + memcpy(&m_soundFormat, format, sizeof(WAVEFORMATEX)); + ZeroMemory(&m_soundHeader, sizeof(AVISTREAMINFO)); + // setup the sound stream header + m_soundHeader.fccType = streamtypeAUDIO; + m_soundHeader.dwQuality = (DWORD)-1; + m_soundHeader.dwScale = format->nBlockAlign; + m_soundHeader.dwInitialFrames = 1; + m_soundHeader.dwRate = format->nAvgBytesPerSec; + m_soundHeader.dwSampleSize = format->nBlockAlign; + + // create the sound stream + if(FAILED(AVIFileCreateStream(m_file, &m_streamSound, &m_soundHeader))) { + m_failed = true; + return; + } + + // setup the sound stream format + if(FAILED(AVIStreamSetFormat(m_streamSound, 0 , (void *)&m_soundFormat, + sizeof(WAVEFORMATEX)))) { + m_failed = true; + return; + } +} + +bool AVIWrite::Open(const char *filename) +{ + // create the AVI file + if(FAILED(AVIFileOpen(&m_file, + filename, + OF_WRITE | OF_CREATE, + NULL))) { + m_failed = true; + return false; + } + // setup the video stream information + ZeroMemory(&m_header, sizeof(AVISTREAMINFO)); + m_header.fccType = streamtypeVIDEO; + m_header.dwScale = 1; + m_header.dwRate = m_fps; + m_header.dwSuggestedBufferSize = m_bitmap.biSizeImage; + + // create the video stream + if(FAILED(AVIFileCreateStream(m_file, + &m_stream, + &m_header))) { + m_failed = true; + return false; + } + + ZeroMemory(&m_options, sizeof(AVICOMPRESSOPTIONS)); + m_arrayOptions[0] = &m_options; + + // call the dialog to setup the compress options to be used + if(!AVISaveOptions(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), 0, 1, &m_stream, m_arrayOptions)) { + m_failed = true; + return false; + } + + // create the compressed stream + if(FAILED(AVIMakeCompressedStream(&m_streamCompressed, m_stream, &m_options, NULL))) { + m_failed = true; + return false; + } + + // setup the video stream format + if(FAILED( AVIStreamSetFormat(m_streamCompressed, 0, + &m_bitmap, + m_bitmap.biSize + + m_bitmap.biClrUsed * sizeof(RGBQUAD)))) { + m_failed = true; + return false; + } + + return true; +} + +bool AVIWrite::AddSound(const char *sound, int len) +{ + // return if we failed somewhere already + if(m_failed) + return false; + + int samples = len / m_soundFormat.nBlockAlign; + + if(FAILED(AVIStreamWrite(m_streamSound, + m_samplesSound, + samples, + (LPVOID)sound, + len, + 0, + NULL, + NULL))) { + m_failed = true; + return false; + } + m_samplesSound += samples; + + return true; +} + +bool AVIWrite::AddFrame(const int frame, const char *bmp) +{ + if (m_failed) + return false; + + // write the frame to the video stream + if(FAILED(AVIStreamWrite(m_streamCompressed, + frame, + 1, + (LPVOID)bmp, + m_bitmap.biSizeImage, + AVIIF_KEYFRAME, + NULL, + NULL))) { + m_failed = true; + return false; + } + return true; +} + +bool AVIWrite::IsSoundAdded() +{ + return m_streamSound != NULL; +} + +void AVIWrite::SetFPS(int f) +{ + m_fps = f; +} diff --git a/src/win32/AVIWrite.h b/src/win32/AVIWrite.h new file mode 100644 index 00000000..74daab98 --- /dev/null +++ b/src/win32/AVIWrite.h @@ -0,0 +1,49 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include + +class AVIWrite { + public: + AVIWrite(); + virtual ~AVIWrite(); + + bool Open(const char *filename); + virtual bool AddFrame(const int number, const char * bmp); + void SetFPS(int fps); + void SetVideoFormat(BITMAPINFOHEADER *); + bool IsSoundAdded(); + void SetSoundFormat(WAVEFORMATEX *); + bool AddSound(const char *sound, int len); + + private: + int m_fps; + WAVEFORMATEX m_soundFormat; + BITMAPINFOHEADER m_bitmap; + AVISTREAMINFO m_header; + AVISTREAMINFO m_soundHeader; + PAVIFILE m_file; + PAVISTREAM m_stream; + PAVISTREAM m_streamCompressed; + PAVISTREAM m_streamSound; + AVICOMPRESSOPTIONS m_options; + AVICOMPRESSOPTIONS *m_arrayOptions[1]; + int m_samplesSound; + bool m_failed; +}; diff --git a/src/win32/AboutDialog.cpp b/src/win32/AboutDialog.cpp new file mode 100644 index 00000000..0d634e2b --- /dev/null +++ b/src/win32/AboutDialog.cpp @@ -0,0 +1,77 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// AboutDialog.cpp : implementation file +// + +#include "AboutDialog.h" +#include "..\..\res\resource.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// AboutDialog dialog + + +AboutDialog::AboutDialog(CWnd* pParent /*=NULL*/) + : CDialog(AboutDialog::IDD, pParent) +{ + //{{AFX_DATA_INIT(AboutDialog) + m_version = _T(VERSION); + //}}AFX_DATA_INIT +} + + +void AboutDialog::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AboutDialog) + DDX_Text(pDX, IDC_VERSION, m_version); + DDX_Control(pDX, IDC_URL, m_link); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AboutDialog, CDialog) + //{{AFX_MSG_MAP(AboutDialog) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AboutDialog message handlers + +BOOL AboutDialog::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_link.SetWindowText("http://vba.ngemu.com"); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AboutDialog::OnOK() +{ + // TODO: Add extra validation here + + CDialog::OnOK(); +} diff --git a/src/win32/AboutDialog.h b/src/win32/AboutDialog.h new file mode 100644 index 00000000..fdb48d3d --- /dev/null +++ b/src/win32/AboutDialog.h @@ -0,0 +1,73 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_) +#define AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// AboutDialog.h : header file +// +#include "stdafx.h" +#include "Hyperlink.h" +#include "..\..\res\resource.h" + +///////////////////////////////////////////////////////////////////////////// +// AboutDialog dialog + +class AboutDialog : public CDialog +{ + Hyperlink m_link; + Hyperlink m_translator; + // Construction + public: + AboutDialog(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AboutDialog) + enum { IDD = IDD_ABOUT }; + CString m_version; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AboutDialog) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AboutDialog) + virtual BOOL OnInitDialog(); + virtual void OnOK(); + //}}AFX_MSG + + + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_) diff --git a/src/win32/AccelEditor.cpp b/src/win32/AccelEditor.cpp new file mode 100644 index 00000000..19076e95 --- /dev/null +++ b/src/win32/AccelEditor.cpp @@ -0,0 +1,289 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// AccelEditor.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "AccelEditor.h" +#include "CmdAccelOb.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// AccelEditor dialog + + +AccelEditor::AccelEditor(CWnd* pParent /*=NULL*/) + : ResizeDlg(AccelEditor::IDD, pParent) +{ + //{{AFX_DATA_INIT(AccelEditor) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + mgr = theApp.winAccelMgr; +} + + +void AccelEditor::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AccelEditor) + DDX_Control(pDX, IDC_CURRENTS, m_currents); + DDX_Control(pDX, IDC_ALREADY_AFFECTED, m_alreadyAffected); + DDX_Control(pDX, IDC_COMMANDS, m_commands); + DDX_Control(pDX, IDC_EDIT_KEY, m_key); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AccelEditor, CDialog) + //{{AFX_MSG_MAP(AccelEditor) + ON_BN_CLICKED(ID_OK, OnOk) + ON_LBN_SELCHANGE(IDC_COMMANDS, OnSelchangeCommands) + ON_BN_CLICKED(IDC_RESET, OnReset) + ON_BN_CLICKED(IDC_ASSIGN, OnAssign) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(IDC_REMOVE, OnRemove) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AccelEditor message handlers + +BOOL AccelEditor::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_STATIC1, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_STATIC2, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_STATIC3, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_ALREADY_AFFECTED, DS_MoveY) + DIALOG_SIZER_ENTRY( ID_OK, DS_MoveX) + DIALOG_SIZER_ENTRY( ID_CANCEL, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_ASSIGN, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_REMOVE, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_RESET, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_COMMANDS, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY( IDC_CURRENTS, DS_MoveX | DS_SizeY) + DIALOG_SIZER_ENTRY( IDC_EDIT_KEY, DS_MoveX | DS_MoveY) + DIALOG_SIZER_END() + + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\AccelEditor", + NULL); + + InitCommands(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AccelEditor::InitCommands() +{ + m_commands.ResetContent(); + m_alreadyAffected.SetWindowText(""); + + POSITION pos = mgr.m_mapAccelString.GetStartPosition(); + + while(pos != NULL) { + CString command; + WORD wID; + mgr.m_mapAccelString.GetNextAssoc(pos, command, wID); + + int index = m_commands.AddString(command); + m_commands.SetItemData(index, wID); + } + + // Update the currents accels associated with the selected command + if (m_commands.SetCurSel(0) != LB_ERR) + OnSelchangeCommands(); +} + +void AccelEditor::OnCancel() +{ + EndDialog(FALSE); +} + +void AccelEditor::OnOk() +{ + EndDialog(TRUE); +} + +void AccelEditor::OnSelchangeCommands() +{ + // Check if some commands exist. + int index = m_commands.GetCurSel(); + if (index == LB_ERR) + return; + + WORD wIDCommand = LOWORD(m_commands.GetItemData(index)); + m_currents.ResetContent(); + + CCmdAccelOb* pCmdAccel; + + if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel)) { + CAccelsOb* pAccel; + CString szBuffer; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + + // Add the keys to the 'currents keys' listbox. + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + pAccel->GetString(szBuffer); + index = m_currents.AddString(szBuffer); + // and a pointer to the accel object. + m_currents.SetItemData(index, (DWORD)pAccel); + } + } + // Init the key editor + // m_pKey->ResetKey(); + +} + +void AccelEditor::OnReset() +{ + mgr.Default(); + InitCommands(); // update the listboxes. +} + +void AccelEditor::OnAssign() +{ + // Control if it's not already affected + CCmdAccelOb* pCmdAccel; + CAccelsOb* pAccel; + WORD wIDCommand; + POSITION pos; + + WORD wKey; + bool bCtrl, bAlt, bShift; + + if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift)) + return; // no valid key, abort + + int count = m_commands.GetCount(); + for (int index = 0; index < count; index++) { + + wIDCommand = LOWORD(m_commands.GetItemData(index)); + mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel); + + pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift)) { + // the key is already affected (in the same or other command) + m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand); + m_key.SetSel(0, -1); + return; // abort + } + } + } + + // OK, we can add the accel key in the currently selected group + index = m_commands.GetCurSel(); + if (index == LB_ERR) + return; + + // Get the object who manage the accels list, associated to the command. + wIDCommand = LOWORD(m_commands.GetItemData(index)); + + if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) != TRUE) + return; + + BYTE cVirt = 0; + if (bCtrl) + cVirt |= FCONTROL; + if (bAlt) + cVirt |= FALT; + if (bShift) + cVirt |= FSHIFT; + + cVirt |= FVIRTKEY; + + // Create the new key... + pAccel = new CAccelsOb(cVirt, wKey, false); + ASSERT(pAccel != NULL); + // ...and add in the list. + pCmdAccel->m_Accels.AddTail(pAccel); + + // Update the listbox. + CString szBuffer; + pAccel->GetString(szBuffer); + + index = m_currents.AddString(szBuffer); + m_currents.SetItemData(index, (DWORD)pAccel); + + // Reset the key editor. + m_key.ResetKey(); +} + +void AccelEditor::OnRemove() +{ + // Some controls + int indexCurrent = m_currents.GetCurSel(); + if (indexCurrent == LB_ERR) + return; + + // 2nd part. + int indexCmd = m_commands.GetCurSel(); + if (indexCmd == LB_ERR) + return; + + // Ref to the ID command + WORD wIDCommand = LOWORD(m_commands.GetItemData(indexCmd)); + + // Run through the accels,and control if it can be deleted. + CCmdAccelOb* pCmdAccel; + if (mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) { + CAccelsOb* pAccel; + CAccelsOb* pAccelCurrent = (CAccelsOb*)(m_currents.GetItemData(indexCurrent)); + CString szBuffer; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + POSITION PrevPos; + while (pos != NULL) { + PrevPos = pos; + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel == pAccelCurrent) { + if (!pAccel->m_bLocked) { + // not locked, so we delete the key + pCmdAccel->m_Accels.RemoveAt(PrevPos); + delete pAccel; + // and update the listboxes/key editor/static text + m_currents.DeleteString(indexCurrent); + m_key.ResetKey(); + m_alreadyAffected.SetWindowText(""); + return; + } else { + systemMessage(0,"Unable to remove this\naccelerator (Locked)"); + return; + } + } + } + systemMessage(0,"internal error (CAccelDlgHelper::Remove : pAccel unavailable)"); + return; + } + systemMessage(0,"internal error (CAccelDlgHelper::Remove : Lookup failed)"); +} diff --git a/src/win32/AccelEditor.h b/src/win32/AccelEditor.h new file mode 100644 index 00000000..e515b1fa --- /dev/null +++ b/src/win32/AccelEditor.h @@ -0,0 +1,79 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_) +#define AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// AccelEditor.h : header file +// +#include "AcceleratorManager.h" +#include "KeyboardEdit.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// AccelEditor dialog + +class AccelEditor : public ResizeDlg +{ + // Construction + public: + CAcceleratorManager mgr; + void InitCommands(); + AccelEditor(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AccelEditor) + enum { IDD = IDD_ACCEL_EDITOR }; + CListBox m_currents; + CStatic m_alreadyAffected; + CListBox m_commands; + CKeyboardEdit m_key; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AccelEditor) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AccelEditor) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnSelchangeCommands(); + afx_msg void OnReset(); + afx_msg void OnAssign(); + afx_msg void OnRemove(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_) diff --git a/src/win32/AcceleratorManager.cpp b/src/win32/AcceleratorManager.cpp new file mode 100644 index 00000000..ad4d4cfb --- /dev/null +++ b/src/win32/AcceleratorManager.cpp @@ -0,0 +1,784 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : AcceleratorManager.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : implementation of the CAcceleratorManager class. +// +//////////////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +//#include "..\..\res\resource.h" +#include "../System.h" + +#include "AcceleratorManager.h" +#include "Reg.h" + +#ifdef _DEBUG +#undef THIS_FILE +static char THIS_FILE[]=__FILE__; +#define new DEBUG_NEW +#endif + + + +////////////////////////////////////////////////////////////////////// +// Constructor/Destructor +////////////////////////////////////////////////////////////////////// +// +// +CAcceleratorManager::CAcceleratorManager() +{ + m_hRegKey = HKEY_CURRENT_USER; + m_szRegKey = ""; + m_bAutoSave = FALSE; + m_pWndConnected = NULL; + + m_bDefaultTable = false; +} + + +////////////////////////////////////////////////////////////////////// +// +// +CAcceleratorManager::~CAcceleratorManager() +{ + if ((m_bAutoSave == true) && (m_szRegKey.IsEmpty() != FALSE)) { + // bool bRet = Write(); + // if (!bRet) + // systemMessage(0, "CAcceleratorManager::~CAcceleratorManager\nError in CAcceleratorManager::Write..."); + } + + Reset(); +} + + +////////////////////////////////////////////////////////////////////// +// Internal fcts +////////////////////////////////////////////////////////////////////// +// +// +void CAcceleratorManager::Reset() +{ + CCmdAccelOb* pCmdAccel; + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + delete pCmdAccel; + } + m_mapAccelTable.RemoveAll(); + m_mapAccelString.RemoveAll(); + + pos = m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + delete pCmdAccel; + } + m_mapAccelTableSaved.RemoveAll(); +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + WORD wIDCmd; + if (m_mapAccelString.Lookup(szCommand, wIDCmd) == TRUE) { + if (wIDCmd != wIDCommand) + return false; + } + + CCmdAccelOb* pCmdAccel = NULL; + if (m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) { + if (pCmdAccel->m_szCommand != szCommand) { + return false; + } + CAccelsOb* pAccel; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_cVirt == cVirt && + pAccel->m_wKey == wKey) + return FALSE; + } + // Adding the accelerator + pCmdAccel->Add(cVirt, wKey, bLocked); + + } else { + pCmdAccel = new CCmdAccelOb(cVirt, wIDCommand, wKey, szCommand, bLocked); + ASSERT(pCmdAccel != NULL); + m_mapAccelTable.SetAt(wIDCommand, pCmdAccel); + } + // 2nd table + m_mapAccelString.SetAt(szCommand, wIDCommand); + return true; +} + + +////////////////////////////////////////////////////////////////////// +// Debug fcts +////////////////////////////////////////////////////////////////////// +// +// +#ifdef _DEBUG +void CAcceleratorManager::AssertValid() const +{ +} + +////////////////////////////////////////////////////////////////////// +// +// +void CAcceleratorManager::Dump(CDumpContext& dc) const +{ + CCmdAccelOb* pCmdAccel; + WORD wKey; + dc << "CAcceleratorManager::Dump :\n"; + dc << "m_mapAccelTable :\n"; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + dc << "a CCmdAccelOb at 0x" << (void*)pCmdAccel << " = {\n"; + dc << pCmdAccel; + dc << "}\n"; + } + dc << "\nm_mapAccelTableSaved\n"; + pos = m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + dc << "a CCmdAccelOb at 0x" << (void*)pCmdAccel << " = {\n"; + dc << pCmdAccel; + dc << "}\n"; + } +} +#endif + + +////////////////////////////////////////////////////////////////////// +// +////////////////////////////////////////////////////////////////////// +// +// +void CAcceleratorManager::Connect(CWnd* pWnd, bool bAutoSave) +{ + ASSERT(m_pWndConnected == NULL); + m_pWndConnected = pWnd; + m_bAutoSave = bAutoSave; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::GetRegKey(HKEY& hRegKey, CString& szRegKey) +{ + if (m_szRegKey.IsEmpty()) + return false; + + hRegKey = m_hRegKey; + szRegKey = m_szRegKey; + return true; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::SetRegKey(HKEY hRegKey, LPCTSTR szRegKey) +{ + ASSERT(hRegKey != NULL); + ASSERT(szRegKey != NULL); + + m_szRegKey = szRegKey; + m_hRegKey = hRegKey; + return true; +} + + +////////////////////////////////////////////////////////////////////// +// Update the application's ACCELs table +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::UpdateWndTable() +{ + int iLoop = 0; + CTypedPtrArray arrayACCEL; + + CCmdAccelOb* pCmdAccel; + WORD wKey; + LPACCEL pACCEL; + CAccelsOb* pAccelOb; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccelOb = pCmdAccel->m_Accels.GetNext(pos); + + pACCEL = new ACCEL; + ASSERT(pACCEL != NULL); + pACCEL->fVirt = pAccelOb->m_cVirt; + pACCEL->key = pAccelOb->m_wKey; + pACCEL->cmd = pCmdAccel->m_wIDCommand; + arrayACCEL.Add(pACCEL); + } + } + + int nAccel = arrayACCEL.GetSize(); + LPACCEL lpAccel = (LPACCEL)LocalAlloc(LPTR, nAccel * sizeof(ACCEL)); + if (!lpAccel) { + for (iLoop = 0; iLoop < nAccel; iLoop++) + delete arrayACCEL.GetAt(iLoop); + arrayACCEL.RemoveAll(); + + return false; + } + + for (iLoop = 0; iLoop < nAccel; iLoop++) { + + pACCEL = arrayACCEL.GetAt(iLoop); + lpAccel[iLoop].fVirt = pACCEL->fVirt; + lpAccel[iLoop].key = pACCEL->key; + lpAccel[iLoop].cmd = pACCEL->cmd; + + delete pACCEL; + } + arrayACCEL.RemoveAll(); + + HACCEL hNewTable = CreateAcceleratorTable(lpAccel, nAccel); + if (!hNewTable) { + ::LocalFree(lpAccel); + return false; + } + HACCEL hOldTable = theApp.hAccel; + if (!::DestroyAcceleratorTable(hOldTable)) { + ::LocalFree(lpAccel); + return false; + } + theApp.hAccel = hNewTable; + ::LocalFree(lpAccel); + + UpdateMenu(GetMenu(*AfxGetApp()->m_pMainWnd)); + + return true; +} + + +////////////////////////////////////////////////////////////////////// +// Create/Destroy accelerators +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::DeleteAccel(BYTE cVirt, WORD wIDCommand, WORD wKey) +{ + CCmdAccelOb* pCmdAccel = NULL; + if (m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) { + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + POSITION PrevPos; + CAccelsOb* pAccel = NULL; + while (pos != NULL) { + PrevPos = pos; + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_bLocked == true) + return false; + + if (pAccel->m_cVirt == cVirt && pAccel->m_wKey == wKey) { + pCmdAccel->m_Accels.RemoveAt(PrevPos); + delete pAccel; + return true; + } + } + } + return false; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::DeleteEntry(WORD wIDCommand) +{ + CCmdAccelOb* pCmdAccel = NULL; + VERIFY(m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE); + + CAccelsOb* pAccel; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_bLocked == true) + return false; + } + m_mapAccelString.RemoveKey(pCmdAccel->m_szCommand); + m_mapAccelTable.RemoveKey(wIDCommand); + delete pCmdAccel; + + return true; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::DeleteEntry(LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + WORD wIDCommand; + if (m_mapAccelString.Lookup(szCommand, wIDCommand) == TRUE) { + return DeleteEntry(wIDCommand); + } + return true; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::SetAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + return AddAccel(cVirt, wIDCommand, wKey, szCommand, bLocked); +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::AddCommandAccel(WORD wIDCommand, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + ASSERT(m_pWndConnected != NULL); + HACCEL hOriginalTable = theApp.hAccel; + + int nAccel = ::CopyAcceleratorTable(hOriginalTable, NULL, 0); + LPACCEL lpAccel = (LPACCEL)LocalAlloc(LPTR, (nAccel) * sizeof(ACCEL)); + if (!lpAccel) + return false; + ::CopyAcceleratorTable(hOriginalTable, lpAccel, nAccel); + + bool bRet = false; + for (int i = 0; i < nAccel; i++) { + if (lpAccel[i].cmd == wIDCommand) + bRet = AddAccel(lpAccel[i].fVirt, wIDCommand, lpAccel[i].key, szCommand, bLocked); + } + ::LocalFree(lpAccel); + return bRet; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::CreateEntry(WORD wIDCommand, LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + WORD wIDDummy; + if (m_mapAccelString.Lookup(szCommand, wIDDummy) == TRUE) + return false; + + CCmdAccelOb* pCmdAccel = new CCmdAccelOb(wIDCommand, szCommand); + ASSERT(pCmdAccel != NULL); + m_mapAccelTable.SetAt(wIDCommand, pCmdAccel); + m_mapAccelString.SetAt(szCommand, wIDCommand); + + return false; +} + + +////////////////////////////////////////////////////////////////////// +// Get a string from the ACCEL definition +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::GetStringFromACCEL(ACCEL* pACCEL, CString& szAccel) +{ + ASSERT(pACCEL != NULL); + + CAccelsOb accel(pACCEL); + accel.GetString(szAccel); + + if (szAccel.IsEmpty()) + return false; + else + return true; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::GetStringFromACCEL(BYTE cVirt, WORD nCode, CString& szAccel) +{ + CAccelsOb accel(cVirt, nCode); + accel.GetString(szAccel); + + if (szAccel.IsEmpty()) + return false; + else + return true; +} + + +////////////////////////////////////////////////////////////////////// +// Copy function +// +CAcceleratorManager& CAcceleratorManager::operator=(const CAcceleratorManager& accelmgr) +{ + Reset(); + + CCmdAccelOb* pCmdAccel; + CCmdAccelOb* pNewCmdAccel; + WORD wKey; + // Copy the 2 tables : normal accel table... + POSITION pos = accelmgr.m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + accelmgr.m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + *pNewCmdAccel = *pCmdAccel; + m_mapAccelTable.SetAt(wKey, pNewCmdAccel); + } + // ... and saved accel table. + pos = accelmgr.m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) { + accelmgr.m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + *pNewCmdAccel = *pCmdAccel; + m_mapAccelTableSaved.SetAt(wKey, pNewCmdAccel); + } + + // The Strings-ID table + CString szKey; + pos = accelmgr.m_mapAccelString.GetStartPosition(); + while (pos != NULL) { + accelmgr.m_mapAccelString.GetNextAssoc(pos, szKey, wKey); + m_mapAccelString.SetAt(szKey, wKey); + } + m_bDefaultTable = accelmgr.m_bDefaultTable; + + return *this; +} + +void CAcceleratorManager::UpdateMenu(HMENU menu) +{ + int count = GetMenuItemCount(menu); + + OSVERSIONINFO info; + info.dwOSVersionInfoSize = sizeof(info); + GetVersionEx(&info); + + if(info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { + MENUITEMINFO info; + char ss[128]; + ZeroMemory(&info, sizeof(info)); + info.cbSize = sizeof(info) - sizeof(HBITMAP); + info.fMask = MIIM_ID | MIIM_SUBMENU; + for(int i = 0; i < count; i++) { + GetMenuItemInfo(menu, i, TRUE, &info); + + if(info.hSubMenu != NULL) { + UpdateMenu(info.hSubMenu); + } else { + if(info.wID != (UINT)-1) { + MENUITEMINFO info2; + ZeroMemory(&info2, sizeof(info2)); + info2.cbSize = sizeof(info2) - sizeof(HBITMAP); + info2.fMask = MIIM_STRING; + info2.dwTypeData = ss; + info2.cch = 128; + GetMenuItemInfo(menu, i, MF_BYPOSITION, &info2); + CString str = ss; + int index = str.Find('\t'); + if(index != -1) + str = str.Left(index); + + WORD command = info.wID; + + CCmdAccelOb *o; + if(m_mapAccelTable.Lookup(command, o)) { + if(o->m_Accels.GetCount()) { + POSITION pos = o->m_Accels.GetHeadPosition(); + CAccelsOb *accel = o->m_Accels.GetNext(pos); + + CString s; + accel->GetString(s); + str += "\t"; + str += s; + } + } + if(str != ss) + ModifyMenu(menu, i, MF_BYPOSITION | MF_STRING, info.wID, str); + } + } + } + } else { + MENUITEMINFO info; + wchar_t ss[128]; + wchar_t str[512]; + + ZeroMemory(&info, sizeof(info)); + info.cbSize = sizeof(info); + info.fMask = MIIM_ID | MIIM_SUBMENU; + for(int i = 0; i < count; i++) { + GetMenuItemInfo(menu, i, TRUE, &info); + + if(info.hSubMenu != NULL) { + UpdateMenu(info.hSubMenu); + } else { + if(info.wID != (WORD)-1) { + MENUITEMINFOW info2; + ZeroMemory(&info2, sizeof(info2)); + info2.cbSize = sizeof(info2); + info2.fMask = MIIM_STRING; + info2.dwTypeData = ss; + info2.cch = 128; + GetMenuItemInfoW(menu, i, MF_BYPOSITION, &info2); + + wcscpy(str, ss); + + wchar_t *p = wcschr(str, '\t'); + if(p) + *p = 0; + + CCmdAccelOb *o; + WORD command = info.wID; + if(m_mapAccelTable.Lookup(command, o)) { + if(o->m_Accels.GetCount()) { + POSITION pos = o->m_Accels.GetHeadPosition(); + + CAccelsOb *accel = o->m_Accels.GetNext(pos); + + CString s; + accel->GetString(s); + + wchar_t temp[128]; + temp[0] = '\t'; + temp[1] = 0; + wcscat(str, temp); + p = temp; + for(const char *sp = s; *sp; sp++) + *p++ = *sp; + *p = 0; + wcscat(str, temp); + } + } + if(wcscmp(str,ss)) + ModifyMenuW(menu, i, MF_BYPOSITION | MF_STRING, info.wID, str); + } + } + } + } +} + +////////////////////////////////////////////////////////////////////// +// In/Out to the registry +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::Load(HKEY hRegKey, LPCTSTR szRegKey) +{ + ASSERT(szRegKey != NULL); + + m_hRegKey = hRegKey; + m_szRegKey = szRegKey; + + DWORD data[2048/sizeof(DWORD)]; + + DWORD len = sizeof(data); + if(regQueryBinaryValue("keyboard", (char *)data, len)) { + int count = len/sizeof(DWORD); + + CCmdAccelOb* pCmdAccel; + CAccelsOb* pAccel; + DWORD dwIDAccelData, dwAccelData; + BOOL bExistID; + int iIndex = 0; + if(count) { + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + + while(pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pCmdAccel->DeleteUserAccels(); + } + + while(iIndex < count) { + dwIDAccelData = data[iIndex++]; + + WORD wIDCommand = LOWORD(dwIDAccelData); + bExistID = m_mapAccelTable.Lookup(wIDCommand, pCmdAccel); + + if (bExistID) { + pCmdAccel->DeleteUserAccels(); + } + for (int j = 0; j < HIWORD(dwIDAccelData) && iIndex < count; j++) { + dwAccelData = data[iIndex++]; + if (bExistID) { + pAccel = new CAccelsOb; + ASSERT(pAccel != NULL); + pAccel->SetData(dwAccelData); + pCmdAccel->Add(pAccel); + } + } + } + } + UpdateWndTable(); + return true; + } + return false; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::Load() +{ + BOOL bRet = FALSE; + if (!m_szRegKey.IsEmpty()) + bRet = Load(m_hRegKey, m_szRegKey); + + if (bRet == TRUE) + return true; + else + return false; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::Write() +{ + CDWordArray AccelsDatasArray; + CDWordArray CmdDatasArray; + + int iCount = 0; + CCmdAccelOb* pCmdAccel; + CAccelsOb* pAccel; + DWORD dwAccelData; + + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + CmdDatasArray.RemoveAll(); + + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + // if (!pAccel->m_bLocked) { + dwAccelData = pAccel->GetData(); + CmdDatasArray.Add(dwAccelData); + // } + } + + if (CmdDatasArray.GetSize() > 0) { + CmdDatasArray.InsertAt(0, MAKELONG(pCmdAccel->m_wIDCommand, CmdDatasArray.GetSize())); + + AccelsDatasArray.Append(CmdDatasArray); + iCount++; + } + } + // AccelsDatasArray.InsertAt(0, MAKELONG(65535, iCount)); + + int count = AccelsDatasArray.GetSize(); + DWORD *data = (DWORD *)malloc(count * sizeof(DWORD)); + ASSERT(data != NULL); + + for(int index = 0; index < count; index++) + data[index] = AccelsDatasArray[index]; + + regSetBinaryValue("keyboard", (char *)data, count*sizeof(DWORD)); + + AccelsDatasArray.RemoveAll(); + CmdDatasArray.RemoveAll(); + + free(data); + + return true; +} + + +////////////////////////////////////////////////////////////////////// +// Defaults values management. +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::CreateDefaultTable() +{ + if (m_bDefaultTable) + return false; + + CCmdAccelOb* pCmdAccel; + CCmdAccelOb* pNewCmdAccel; + + CAccelsOb* pAccel; + CAccelsOb* pNewAccel; + + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (!pAccel->m_bLocked) { + pNewAccel = new CAccelsOb; + ASSERT(pNewAccel != NULL); + + *pNewAccel = *pAccel; + pNewCmdAccel->m_Accels.AddTail(pNewAccel); + } + } + if (pNewCmdAccel->m_Accels.GetCount() != 0) { + pNewCmdAccel->m_wIDCommand = pCmdAccel->m_wIDCommand; + pNewCmdAccel->m_szCommand = pCmdAccel->m_szCommand; + + m_mapAccelTableSaved.SetAt(wKey, pNewCmdAccel); + } else + delete pNewCmdAccel; + } + + m_bDefaultTable = true; + return true; +} + + +////////////////////////////////////////////////////////////////////// +// +// +bool CAcceleratorManager::Default() +{ + return true; +} diff --git a/src/win32/AcceleratorManager.h b/src/win32/AcceleratorManager.h new file mode 100644 index 00000000..c0b12885 --- /dev/null +++ b/src/win32/AcceleratorManager.h @@ -0,0 +1,142 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : AcceleratorManager.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : interface for the CAcceleratorManager class. +// +//////////////////////////////////////////////////////////////////////////////// +#if !defined(AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_) +#define AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + + +#include "CmdAccelOb.h" + + +// Helper map +#include // MFC Templates extension +#ifndef CMapStringToWord +typedef CMap< CString, LPCSTR, WORD, WORD& > CMapStringToWord; +#endif + +#ifndef CMapWordToCCmdAccelOb +typedef CMap< WORD, WORD&, CCmdAccelOb*, CCmdAccelOb*& > CMapWordToCCmdAccelOb; +#endif + + +////////////////////////////////////////////////////////////////////// +// +// +class CAcceleratorManager : public CObject { + friend class AccelEditor; + public: + CAcceleratorManager(); + virtual ~CAcceleratorManager(); + + // Operations + public: + void UpdateMenu(HMENU menu); + void UpdateMenu(); + // Connection to the main application wnd + void Connect(CWnd *pWnd, bool bAutoSave = true); + // In/Out with the registry + bool Load(HKEY hRegKey, LPCTSTR szRegKey); + bool Load(); + bool Write(); + // Get the initials accels, not the user's + bool Default(); + // Save a copy in the 2 maps called xxxSaved, which are used in case + // of Default(), to reload the defaults accels. + bool CreateDefaultTable(); + bool IsDefaultTableAvailable() {return m_bDefaultTable;} + bool IsMapStringCommandsEmpty() { + if (m_mapAccelString.IsEmpty()) + return true; + else + return false; + } + + // Registry access configuration + bool GetRegKey(HKEY& hRegKey, CString &szRegKey); + bool SetRegKey(HKEY hRegKey, LPCTSTR szRegKey); + bool IsAutoSave() {return m_bAutoSave;} + void SetAutoSave(bool bAutoSave) {m_bAutoSave = bAutoSave;} + + // Helper fct, used for new menus strings + bool GetStringFromACCEL(ACCEL* pACCEL, CString& szAccel); + bool GetStringFromACCEL(BYTE cVirt, WORD nCode, CString& szAccel); + + // Update the ACCELS table in the application, from the specified + // datas in the manager. + bool UpdateWndTable(); + + // Modification helper fcts + bool SetAccel(BYTE cVirt, WORD wIDCommand, WORD wNewCaract, + LPCTSTR szCommand, bool bLocked = false); + bool AddCommandAccel(WORD wIDCommand, LPCTSTR szCommand, bool bLocked = true); + bool CreateEntry(WORD wIDCommand, LPCTSTR szCommand); + + bool DeleteEntry(LPCTSTR szCommand); + bool DeleteEntry(WORD wIDCommand); + bool DeleteAccel(BYTE cVirt, WORD wIDCommand, WORD wNewCaract); + + // Affectation operator + CAcceleratorManager& operator=(const CAcceleratorManager& accelmgr); + + public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + + protected: + // Erase all the datas + void Reset(); + // Internal affect fct. + bool AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, + LPCTSTR szCommand, bool bLocked); + + // Attributes + protected: + CWnd *m_pWndConnected; + + // User datas + CMapStringToWord m_mapAccelString; + CMapWordToCCmdAccelOb m_mapAccelTable; + // Default datas + CMapWordToCCmdAccelOb m_mapAccelTableSaved; + bool m_bDefaultTable; + + // Where the users datas will be saved in the registry + HKEY m_hRegKey; + CString m_szRegKey; + // if true, there is an auto-save in the registry, when the destructor is called + bool m_bAutoSave; + +}; + + +#endif // !defined(AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_) diff --git a/src/win32/Associate.cpp b/src/win32/Associate.cpp new file mode 100644 index 00000000..7eca0f84 --- /dev/null +++ b/src/win32/Associate.cpp @@ -0,0 +1,128 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Associate.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Associate.h" +#include "Reg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// Associate dialog + + +Associate::Associate(CWnd* pParent /*=NULL*/) + : CDialog(Associate::IDD, pParent) +{ + //{{AFX_DATA_INIT(Associate) + m_agb = FALSE; + m_bin = FALSE; + m_cgb = FALSE; + m_gb = FALSE; + m_gba = FALSE; + m_gbc = FALSE; + m_sgb = FALSE; + //}}AFX_DATA_INIT +} + + +void Associate::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Associate) + DDX_Check(pDX, IDC_AGB, m_agb); + DDX_Check(pDX, IDC_BIN, m_bin); + DDX_Check(pDX, IDC_CGB, m_cgb); + DDX_Check(pDX, IDC_GB, m_gb); + DDX_Check(pDX, IDC_GBA, m_gba); + DDX_Check(pDX, IDC_GBC, m_gbc); + DDX_Check(pDX, IDC_SGB, m_sgb); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(Associate, CDialog) + //{{AFX_MSG_MAP(Associate) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Associate message handlers + +BOOL Associate::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Associate::OnCancel() +{ + EndDialog(FALSE); +} + +void Associate::OnOk() +{ + UpdateData(); + + int mask = 0; + if(m_gb) + mask |= 1; + if(m_sgb) + mask |= 2; + if(m_cgb) + mask |= 4; + if(m_gbc) + mask |= 8; + if(m_gba) + mask |= 16; + if(m_agb) + mask |= 32; + if(m_bin) + mask |= 64; + if(mask) { + char applicationPath[2048]; + CString commandPath; + LPCTSTR types[] = { ".gb", ".sgb", ".cgb", ".gbc", ".gba", ".agb", ".bin" }; + GetModuleFileName(NULL, applicationPath, 2048); + commandPath.Format("\"%s\" \"%%1\"", applicationPath); + regAssociateType("VisualBoyAdvance.Binary", + "Binary", + commandPath); + + for(int i = 0; i < 7; i++) { + if(mask & (1< 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Associate.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Associate dialog + +class Associate : public CDialog +{ + // Construction + public: + Associate(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Associate) + enum { IDD = IDD_ASSOCIATIONS }; + BOOL m_agb; + BOOL m_bin; + BOOL m_cgb; + BOOL m_gb; + BOOL m_gba; + BOOL m_gbc; + BOOL m_sgb; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Associate) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(Associate) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ASSOCIATE_H__3326525B_B405_40A7_82C4_B2594669A930__INCLUDED_) diff --git a/src/win32/BitmapControl.cpp b/src/win32/BitmapControl.cpp new file mode 100644 index 00000000..0aacc578 --- /dev/null +++ b/src/win32/BitmapControl.cpp @@ -0,0 +1,287 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// BitmapControl.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "BitmapControl.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +bool BitmapControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl + +IMPLEMENT_DYNCREATE(BitmapControl, CScrollView) + + BitmapControl::BitmapControl() +{ + w = 0; + h = 0; + data = NULL; + bmpInfo = NULL; + stretch = false; + registerClass(); + CSize sizeTotal; + sizeTotal.cx = sizeTotal.cy = 0; + SetScrollSizes(MM_TEXT, sizeTotal); +} + +BitmapControl::~BitmapControl() +{ +} + + +BEGIN_MESSAGE_MAP(BitmapControl, CScrollView) + //{{AFX_MSG_MAP(BitmapControl) + ON_WM_ERASEBKGND() + ON_WM_SIZE() + ON_WM_LBUTTONDOWN() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// BitmapControl drawing + +void BitmapControl::OnInitialUpdate() +{ + CScrollView::OnInitialUpdate(); + + CSize sizeTotal; + // TODO: calculate the total size of this view + sizeTotal.cx = sizeTotal.cy = 100; + SetScrollSizes(MM_TEXT, sizeTotal); +} + +void BitmapControl::OnDraw(CDC* dc) +{ + RECT r; + GetClientRect(&r); + int w1 = r.right - r.left; + int h1 = r.bottom - r.top; + CDC memDC; + memDC.CreateCompatibleDC(dc); + if(!stretch) { + if(w > w1) + w1 = w; + if(h > h1) + h1 = h; + } + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(dc, w1, h1); + pOldBitmap = memDC.SelectObject(&bitmap); + if(stretch) { + bmpInfo->bmiHeader.biWidth = w; + bmpInfo->bmiHeader.biHeight = -h; + + StretchDIBits(memDC.GetSafeHdc(), + 0, + 0, + w1, + h1, + 0, + 0, + w, + h, + data, + bmpInfo, + DIB_RGB_COLORS, + SRCCOPY); + } else { + FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE))); + + bmpInfo->bmiHeader.biWidth = w; + bmpInfo->bmiHeader.biHeight = -h; + SetDIBitsToDevice(memDC.GetSafeHdc(), + 0, + 0, + w, + h, + 0, + 0, + 0, + h, + data, + bmpInfo, + DIB_RGB_COLORS); + } + + dc->BitBlt(0,0,w1,h1, + &memDC,0,0,SRCCOPY); + memDC.SelectObject(pOldBitmap); + + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl diagnostics + +#ifdef _DEBUG +void BitmapControl::AssertValid() const +{ + CScrollView::AssertValid(); +} + +void BitmapControl::Dump(CDumpContext& dc) const +{ + CScrollView::Dump(dc); +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl message handlers + +BOOL BitmapControl::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + +void BitmapControl::OnSize(UINT nType, int cx, int cy) +{ + if(!stretch) + CScrollView::OnSize(nType, cx, cy); +} + +void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt) +{ + if(!data) + return; + int x = pt.x; + int y = pt.y; + + WPARAM point; + + if(stretch) { + RECT rect; + GetClientRect(&rect); + + int height = rect.bottom - rect.top; + int width = rect.right - rect.left; + + int xx = (x * w) / width; + int yy = (y * h) / height; + + point = xx | (yy<<16); + + int xxx = xx / 8; + int yyy = yy / 8; + + for(int i = 0; i < 8; i++) { + memcpy(&colors[i*3*8], &data[xxx * 8 * 3 + + w * yyy * 8 * 3 + + i * w * 3], 8 * 3); + } + } else { + POINT p; + p.x = GetScrollPos(SB_HORZ); + p.y = GetScrollPos(SB_VERT); + + p.x += x; + p.y += y; + + if(p.x >= w || + p.y >= h) + return; + + point = p.x | (p.y<<16); + + int xxx = p.x / 8; + int yyy = p.y / 8; + + for(int i = 0; i < 8; i++) { + memcpy(&colors[i*3*8], &data[xxx * 8 * 3 + + w * yyy * 8 * 3 + + i * w * 3], 8 * 3); + } + } + + GetParent()->SendMessage(WM_MAPINFO, + point, + (LPARAM)colors); +} + +void BitmapControl::setBmpInfo(BITMAPINFO *info) +{ + bmpInfo = info; +} + +void BitmapControl::setData(u8 *d) +{ + data = d; +} + +void BitmapControl::setSize(int w1, int h1) +{ + if(w != w1 || h != h1) { + w = w1; + h = h1; + SIZE s; + s.cx = w; + s.cy = h; + SetScrollSizes(MM_TEXT, s); + } +} + +void BitmapControl::refresh() +{ + Invalidate(); +} + + +void BitmapControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaBitmapControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void BitmapControl::setStretch(bool b) +{ + if(b != stretch) { + stretch = b; + Invalidate(); + } +} + +bool BitmapControl::getStretch() +{ + return stretch; +} + +void BitmapControl::PostNcDestroy() +{ +} diff --git a/src/win32/BitmapControl.h b/src/win32/BitmapControl.h new file mode 100644 index 00000000..f2f0c4d8 --- /dev/null +++ b/src/win32/BitmapControl.h @@ -0,0 +1,96 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_) +#define AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// BitmapControl.h : header file +// +#ifndef WM_MAPINFO +#define WM_MAPINFO WM_APP+101 +#endif + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl view + +class BitmapControl : public CScrollView +{ + public: + BitmapControl(); // protected constructor used by dynamic creation + protected: + DECLARE_DYNCREATE(BitmapControl) + + // Attributes + public: + + // Operations + public: + void setStretch(bool b); + void refresh(); + void setSize(int w1, int h1); + void setData(u8 *d); + void setBmpInfo(BITMAPINFO *info); + static bool isRegistered; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(BitmapControl) + protected: + virtual void OnDraw(CDC* pDC); // overridden to draw this view + virtual void OnInitialUpdate(); // first time after construct + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + public: + bool getStretch(); + virtual ~BitmapControl(); + protected: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + + // Generated message map functions + //{{AFX_MSG(BitmapControl) + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + void registerClass(); + bool stretch; + u8 colors[3*64]; + BITMAPINFO *bmpInfo; + u8 * data; + int h; + int w; +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_) diff --git a/src/win32/BugReport.cpp b/src/win32/BugReport.cpp new file mode 100644 index 00000000..ad7c31f9 --- /dev/null +++ b/src/win32/BugReport.cpp @@ -0,0 +1,248 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// BugReport.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "BugReport.h" + +#include "../agbprint.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Port.h" +#include "../RTC.h" +#include "../Sound.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// BugReport dialog + + +BugReport::BugReport(CWnd* pParent /*=NULL*/) + : CDialog(BugReport::IDD, pParent) +{ + //{{AFX_DATA_INIT(BugReport) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void BugReport::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(BugReport) + DDX_Control(pDX, IDC_BUG_REPORT, m_report); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(BugReport, CDialog) + //{{AFX_MSG_MAP(BugReport) + ON_BN_CLICKED(IDC_COPY, OnCopy) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// BugReport message handlers + +void BugReport::OnCopy() +{ + OpenClipboard(); + + EmptyClipboard(); + CString report; + m_report.GetWindowText(report); + + HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, + (report.GetLength() + 1) * sizeof(CHAR)); + if (hglbCopy == NULL) { + CloseClipboard(); + return; + } + + // Lock the handle and copy the text to the buffer. + + LPSTR lptstrCopy = (LPSTR)GlobalLock(hglbCopy); + memcpy(lptstrCopy, (const char *)report, + report.GetLength() * sizeof(CHAR)); + lptstrCopy[report.GetLength()] = (TCHAR) 0; // null character + GlobalUnlock(hglbCopy); + + // Place the handle on the clipboard. + + SetClipboardData(CF_TEXT, hglbCopy); + CloseClipboard(); + + systemMessage(IDS_BUG_REPORT, "Bug report has been copied to the Clipboard"); +} + +void BugReport::OnOk() +{ + EndDialog(TRUE); +} + +BOOL BugReport::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + CString report = createReport(); + + m_report.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + m_report.SetWindowText(report); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + + +static void AppendFormat(CString& report, const char *format, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, format); + buffer.FormatV(format, valist); + va_end(valist); + report += buffer; +} + +CString BugReport::createReport() +{ + theApp.winCheckFullscreen(); + + CString report = ""; + AppendFormat(report, "Emu version : %s\r\n", VERSION); + AppendFormat(report, "Emu Type : %s\r\n", +#ifdef FINAL_VERSION +#ifdef DEV_VERSION + "Development Version" +#else + "Normal Version" +#endif +#else + "Debug Version" +#endif + ); + + if(emulating) { + AppendFormat(report, "File : %s\r\n", theApp.szFile); + + char buffer[20]; + if(theApp.cartridgeType == 0) { + u32 check = 0; + for(int i = 0; i < 0x4000; i += 4) { + check += *((u32 *)&bios[i]); + } + AppendFormat(report, "BIOS Checksum: %08X\r\n", check); + + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + AppendFormat(report, "Internal name: %s\r\n", buffer); + + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + AppendFormat(report, "Game code : %s\r\n", buffer); + + CString res = ""; + u32 *p = (u32 *)rom; + u32 *end = (u32 *)((char *)rom+theApp.romSize); + while(p < end) { + u32 d = READ32LE(p); + + if(d == 0x52504545) { + if(memcmp(p, "EEPROM_", 7) == 0) { + res += (const char *)p; + res += ' '; + } + } else if (d == 0x4D415253) { + if(memcmp(p, "SRAM_", 5) == 0) { + res += (const char *)p; + res += ' '; + } + } else if (d == 0x53414C46) { + if(memcmp(p, "FLASH1M_", 8) == 0) { + res += (const char *)p; + res += ' '; + } + } else if(memcmp(p, "FLASH", 5) == 0) { + res += (const char *)p; + res += ' '; + } else if (d == 0x52494953) { + if(memcmp(p, "SIIRTC_V", 8) == 0) { + res += (const char *)p; + res += ' '; + } + } + p++; + } + if(res.GetLength() > 0) + AppendFormat(report, "Cart Save : %s\r\n", res); + } else if(theApp.cartridgeType == 1) { + strncpy(buffer, (const char *)&gbRom[0x134], 15); + buffer[15] = 0; + AppendFormat(report, "Game title : %s\r\n", buffer); + } + } + + AppendFormat(report, "Using BIOS : %d\r\n", theApp.useBiosFile); + AppendFormat(report, "Skip BIOS : %d\r\n", theApp.skipBiosFile); + AppendFormat(report, "Disable SFX : %d\r\n", cpuDisableSfx); + AppendFormat(report, "Skip intro : %d\r\n", theApp.removeIntros); + AppendFormat(report, "Throttle : %d\r\n", theApp.throttle); + AppendFormat(report, "Rewind : %d\r\n", theApp.rewindTimer); + AppendFormat(report, "Auto frame : %d\r\n", theApp.autoFrameSkip); + AppendFormat(report, "Video option : %d\r\n", theApp.videoOption); + AppendFormat(report, "Render type : %d\r\n", theApp.renderMethod); + AppendFormat(report, "Color depth : %d\r\n", systemColorDepth); + AppendFormat(report, "Red shift : %08x\r\n", systemRedShift); + AppendFormat(report, "Green shift : %08x\r\n", systemGreenShift); + AppendFormat(report, "Blue shift : %08x\r\n", systemBlueShift); + AppendFormat(report, "Layer setting: %04X\r\n", layerSettings); + AppendFormat(report, "Save type : %d (%d)\r\n", + theApp.winSaveType, cpuSaveType); + AppendFormat(report, "Flash size : %08X (%08x)\r\n", + theApp.winFlashSize, flashSize); + AppendFormat(report, "RTC : %d (%d)\r\n", theApp.winRtcEnable, + rtcIsEnabled()); + AppendFormat(report, "AGBPrint : %d\r\n", agbPrintIsEnabled()); + AppendFormat(report, "Speed toggle : %d\r\n", theApp.speedupToggle); + AppendFormat(report, "Synchronize : %d\r\n", synchronize); + AppendFormat(report, "Sound OFF : %d\r\n", soundOffFlag); + AppendFormat(report, "Channels : %04x\r\n", soundGetEnable() & 0x30f); + AppendFormat(report, "Old Sync : %d\r\n", theApp.useOldSync); + AppendFormat(report, "Priority : %d\r\n", theApp.threadPriority); + AppendFormat(report, "Filters : %d (%d)\r\n", theApp.filterType, theApp.ifbType); + AppendFormat(report, "Cheats : %d\r\n", cheatsNumber); + AppendFormat(report, "GB Cheats : %d\r\n", gbCheatNumber); + AppendFormat(report, "GB Emu Type : %d\r\n", gbEmulatorType); + + return report; +} diff --git a/src/win32/BugReport.h b/src/win32/BugReport.h new file mode 100644 index 00000000..6d5a0cff --- /dev/null +++ b/src/win32/BugReport.h @@ -0,0 +1,68 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_) +#define AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// BugReport.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// BugReport dialog + +class BugReport : public CDialog +{ + // Construction + public: + BugReport(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(BugReport) + enum { IDD = IDD_BUG_REPORT }; + CEdit m_report; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(BugReport) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + CString createReport(); + + // Generated message map functions + //{{AFX_MSG(BugReport) + afx_msg void OnCopy(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_) diff --git a/src/win32/CmdAccelOb.cpp b/src/win32/CmdAccelOb.cpp new file mode 100644 index 00000000..4172b726 --- /dev/null +++ b/src/win32/CmdAccelOb.cpp @@ -0,0 +1,526 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : CmdAccelOb.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "CmdAccelOb.h" + +//////////////////////////////////////////////////////////////////////// +// +// +MAPVIRTKEYS mapVirtKeys[] = { + {VK_LBUTTON, "VK_LBUTTON"}, + {VK_RBUTTON, "VK_RBUTTON"}, + {VK_CANCEL, "VK_CANCEL"}, + {VK_MBUTTON, "VK_MBUTTON"}, + {VK_BACK, "BACK"}, + {VK_TAB, "TAB"}, + {VK_CLEAR, "VK_CLEAR"}, + {VK_RETURN, "RETURN"}, + {VK_SHIFT, "SHIFT"}, + {VK_CONTROL, "CONTROL"}, + {VK_MENU, "MENU"}, + {VK_PAUSE, "PAUSE"}, + {VK_CAPITAL, "CAPITAL"}, + {VK_ESCAPE, "ESCAPE"}, + {VK_SPACE, "SPACE"}, + {VK_PRIOR, "PRIOR"}, + {VK_NEXT, "NEXT"}, + {VK_END, "END"}, + {VK_HOME, "HOME"}, + {VK_LEFT, "LEFT"}, + {VK_UP, "UP"}, + {VK_RIGHT, "RIGHT"}, + {VK_DOWN, "DOWN"}, + {VK_SELECT, "VK_SELECT"}, + {VK_PRINT, "PRINT"}, + {VK_EXECUTE, "EXECUTE"}, + {VK_SNAPSHOT, "SNAPSHOT"}, + {VK_INSERT, "INSERT"}, + {VK_DELETE, "DELETE"}, + {VK_HELP, "VK_HELP"}, + {WORD('0'), "0"}, + {WORD('1'), "1"}, + {WORD('2'), "2"}, + {WORD('3'), "3"}, + {WORD('4'), "4"}, + {WORD('5'), "5"}, + {WORD('6'), "6"}, + {WORD('7'), "7"}, + {WORD('8'), "8"}, + {WORD('9'), "9"}, + {WORD('A'), "A"}, + {WORD('B'), "B"}, + {WORD('C'), "C"}, + {WORD('D'), "D"}, + {WORD('E'), "E"}, + {WORD('F'), "F"}, + {WORD('G'), "G"}, + {WORD('H'), "H"}, + {WORD('I'), "I"}, + {WORD('J'), "J"}, + {WORD('K'), "K"}, + {WORD('L'), "L"}, + {WORD('M'), "M"}, + {WORD('N'), "N"}, + {WORD('O'), "O"}, + {WORD('P'), "P"}, + {WORD('Q'), "Q"}, + {WORD('R'), "R"}, + {WORD('S'), "S"}, + {WORD('T'), "T"}, + {WORD('U'), "U"}, + {WORD('V'), "V"}, + {WORD('W'), "W"}, + {WORD('X'), "X"}, + {WORD('Y'), "Y"}, + {WORD('Z'), "Z"}, + {VK_LWIN, "VK_LWIN"}, + {VK_RWIN, "VK_RWIN"}, + {VK_APPS, "VK_APPS"}, + {VK_NUMPAD0, "NUMPAD0"}, + {VK_NUMPAD1, "NUMPAD1"}, + {VK_NUMPAD2, "NUMPAD2"}, + {VK_NUMPAD3, "NUMPAD3"}, + {VK_NUMPAD4, "NUMPAD4"}, + {VK_NUMPAD5, "NUMPAD5"}, + {VK_NUMPAD6, "NUMPAD6"}, + {VK_NUMPAD7, "NUMPAD7"}, + {VK_NUMPAD8, "NUMPAD8"}, + {VK_NUMPAD9, "NUMPAD9"}, + {VK_MULTIPLY, "MULTIPLY"}, + {VK_ADD, "ADD"}, + {VK_SEPARATOR, "SEPARATOR"}, + {VK_SUBTRACT, "SUBTRACT"}, + {VK_DECIMAL, "DECIMAL"}, + {VK_DIVIDE, "DIVIDE"}, + {VK_F1, "F1"}, + {VK_F2, "F2"}, + {VK_F3, "F3"}, + {VK_F4, "F4"}, + {VK_F5, "F5"}, + {VK_F6, "F6"}, + {VK_F7, "F7"}, + {VK_F8, "F8"}, + {VK_F9, "F9"}, + {VK_F10, "F10"}, + {VK_F11, "F11"}, + {VK_F12, "F12"}, + {VK_F13, "F13"}, + {VK_F14, "F14"}, + {VK_F15, "F15"}, + {VK_F16, "F16"}, + {VK_F17, "F17"}, + {VK_F18, "F18"}, + {VK_F19, "F19"}, + {VK_F20, "F20"}, + {VK_F21, "F21"}, + {VK_F22, "F22"}, + {VK_F23, "F23"}, + {VK_F24, "F24"}, + {VK_NUMLOCK, "NUMLOCK"}, + {VK_SCROLL, "VK_SCROLL"}, + {VK_ATTN, "VK_ATTN"}, + {VK_CRSEL, "VK_CRSEL"}, + {VK_EXSEL, "VK_EXSEL"}, + {VK_EREOF, "VK_EREOF"}, + {VK_PLAY, "VK_PLAY"}, + {VK_ZOOM, "VK_ZOOM"}, + {VK_NONAME, "VK_NONAME"}, + {VK_PA1, "VK_PA1"}, + {VK_OEM_CLEAR, "VK_OEM_CLEAR"}, +}; + + +//////////////////////////////////////////////////////////////////////// +// +// +MAPVIRTKEYS mapVirtSysKeys[] = { + {FCONTROL, "Ctrl"}, + {FALT, "Alt"}, + {FSHIFT, "Shift"}, +}; + + +//////////////////////////////////////////////////////////////////////// +// helper fct for external access +//////////////////////////////////////////////////////////////////////// +// +// +TCHAR* mapVirtKeysStringFromWORD(WORD wKey) +{ + for (int index = 0; index < sizeof(mapVirtKeys)/sizeof(mapVirtKeys[0]); index++) { + if (mapVirtKeys[index].wKey == wKey) + return mapVirtKeys[index].szKey; + } + return NULL; +} + + + +//////////////////////////////////////////////////////////////////////// +// +#define DEFAULT_ACCEL 0x01 +#define USER_ACCEL 0x02 + + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb() +{ + m_cVirt = 0; + m_wKey = 0; + m_bLocked = false; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(CAccelsOb* pFrom) +{ + ASSERT(pFrom != NULL); + + m_cVirt = pFrom->m_cVirt; + m_wKey = pFrom->m_wKey; + m_bLocked = pFrom->m_bLocked; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(BYTE cVirt, WORD wKey, bool bLocked) +{ + m_cVirt = cVirt; + m_wKey = wKey; + m_bLocked = bLocked; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(LPACCEL pACCEL) +{ + ASSERT(pACCEL != NULL); + + m_cVirt = pACCEL->fVirt; + m_wKey = pACCEL->key; + m_bLocked = false; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb& CAccelsOb::operator=(const CAccelsOb& from) +{ + m_cVirt = from.m_cVirt; + m_wKey = from.m_wKey; + m_bLocked = from.m_bLocked; + + return *this; +} + + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::GetString(CString& szBuffer) +{ + szBuffer = ""; + // in case of the object is not assigned, we avoid error messages + if (m_wKey == 0) + return; + + // modifiers part + for (int i = 0; i < sizetable(mapVirtSysKeys); i++) { + if (m_cVirt & mapVirtSysKeys[i].wKey) { + szBuffer += mapVirtSysKeys[i].szKey; + szBuffer += "+"; + } + } + // and virtual key part + if (1) for (int i = 0; i < sizetable(mapVirtKeys); i++) { + if (m_wKey == mapVirtKeys[i].wKey) { + szBuffer += mapVirtKeys[i].szKey; + return; + } + } + AfxMessageBox("Internal error : (CAccelsOb::GetString) m_wKey invalid"); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +bool CAccelsOb::IsEqual(WORD wKey, bool bCtrl, bool bAlt, bool bShift) +{ + // CString szTemp; + // GetString(szTemp); + + + bool m_bCtrl = (m_cVirt & FCONTROL) ? true : false; + bool bRet = (bCtrl == m_bCtrl); + + bool m_bAlt = (m_cVirt & FALT) ? true : false; + bRet &= (bAlt == m_bAlt); + + bool m_bShift = (m_cVirt & FSHIFT) ? true : false; + bRet &= (bShift == m_bShift); + + bRet &= static_cast(m_wKey == wKey); + + return bRet; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +DWORD CAccelsOb::GetData() +{ + BYTE cLocalCodes = 0; + if (m_bLocked) + cLocalCodes = DEFAULT_ACCEL; + else + cLocalCodes = USER_ACCEL; + + WORD bCodes = MAKEWORD(m_cVirt, cLocalCodes); + return MAKELONG(m_wKey, bCodes); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +bool CAccelsOb::SetData(DWORD dwDatas) +{ + m_wKey = LOWORD(dwDatas); + + WORD bCodes = HIWORD(dwDatas); + m_cVirt = LOBYTE(bCodes); + + BYTE cLocalCodes = HIBYTE(bCodes); + m_bLocked = static_cast(cLocalCodes == DEFAULT_ACCEL); + return true; +} + +//////////////////////////////////////////////////////////////////////// +// +#ifdef _DEBUG +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::AssertValid() const +{ + CObject::AssertValid(); +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::Dump(CDumpContext& dc) const +{ + dc << "\t\t"; + CObject::Dump(dc); + dc << "\t\tlocked=" << m_bLocked << ", cVirt=" << m_cVirt << ", wKey=" << m_wKey << "\n\n"; + +} +#endif + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb() +{ +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb(WORD wIDCommand, LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + m_wIDCommand = wIDCommand; + m_szCommand = szCommand; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + m_wIDCommand = wIDCommand; + m_szCommand = szCommand; + + CAccelsOb* pAccel = DEBUG_NEW CAccelsOb(cVirt, wKey, bLocked); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::~CCmdAccelOb() +{ + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) + delete m_Accels.GetNext(pos); + m_Accels.RemoveAll(); +} + + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Add(BYTE cVirt, WORD wKey, bool bLocked) +{ + CAccelsOb* pAccel = DEBUG_NEW CAccelsOb(cVirt, wKey, bLocked); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Add(CAccelsOb* pAccel) +{ + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb& CCmdAccelOb::operator=(const CCmdAccelOb& from) +{ + Reset(); + + m_wIDCommand = from.m_wIDCommand; + m_szCommand = from.m_szCommand; + + CAccelsOb* pAccel; + POSITION pos = from.m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = DEBUG_NEW CAccelsOb(from.m_Accels.GetNext(pos)); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); + } + return *this; +} + + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::DeleteUserAccels() +{ + CAccelsOb* pAccel; + POSITION prevPos; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) { + prevPos = pos; + pAccel = m_Accels.GetNext(pos); + if (!pAccel->m_bLocked) { + delete pAccel; + m_Accels.RemoveAt(prevPos); + } + } +} + + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Reset() +{ + m_wIDCommand = 0; + m_szCommand = "Empty command"; + + CAccelsOb* pAccel; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = m_Accels.GetNext(pos); + delete pAccel; + } +} + +//////////////////////////////////////////////////////////////////////// +// +#ifdef _DEBUG +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::AssertValid() const +{ + // call base class function first + CObject::AssertValid(); +} + + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Dump( CDumpContext& dc ) const +{ + // call base class function first + dc << "\t"; + CObject::Dump( dc ); + + // now do the stuff for our specific class + dc << "\tIDCommand = " << m_wIDCommand; + dc << "\n\tszCommand = " << m_szCommand; + dc << "\n\tAccelerators = {\n"; + + CAccelsOb* pAccel; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) { + pAccel = m_Accels.GetNext(pos); + dc << pAccel; + } + dc << "\t}\n"; +} +#endif diff --git a/src/win32/CmdAccelOb.h b/src/win32/CmdAccelOb.h new file mode 100644 index 00000000..c8e47ac1 --- /dev/null +++ b/src/win32/CmdAccelOb.h @@ -0,0 +1,113 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : CmdAccelOb.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef __CMDACCEL_OB_INCLUDE +#define __CMDACCEL_OB_INCLUDE + +#include // MFC Templates extension + +//////////////////////////////////////////////////////////////////////// +// +// +typedef struct tagMAPVIRTKEYS { + WORD wKey; + TCHAR szKey[15]; +} MAPVIRTKEYS, *PMAPVIRTKEYS; + + +//////////////////////////////////////////////////////////////////////// +// +// +#define sizetable(table) (sizeof(table)/sizeof(table[0])) + + +//////////////////////////////////////////////////////////////////////// +// +// +class CAccelsOb : public CObject { + public: + CAccelsOb(); + CAccelsOb(CAccelsOb* pFrom); + CAccelsOb(BYTE cVirt, WORD wKey, bool bLocked = false); + CAccelsOb(LPACCEL pACCEL); + + public: + CAccelsOb& operator=(const CAccelsOb& from); + + void GetString(CString& szBuffer); + bool IsEqual(WORD wKey, bool bCtrl, bool bAlt, bool bShift); + DWORD GetData(); + bool SetData(DWORD dwDatas); + + public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + + public: + BYTE m_cVirt; + WORD m_wKey; + bool m_bLocked; +}; + + +////////////////////////////////////////////////////////////////////// +// +// +class CCmdAccelOb : public CObject { + public: + CCmdAccelOb(); + CCmdAccelOb(WORD wIDCommand, LPCTSTR szCommand); + CCmdAccelOb(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked = false); + ~CCmdAccelOb(); + + public: + void Add(CAccelsOb* pAccel); + void Add(BYTE cVirt, WORD wKey, bool bLocked = false); + void Reset(); + void DeleteUserAccels(); + + CCmdAccelOb& operator=(const CCmdAccelOb& from); + public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + + public: + WORD m_wIDCommand; + CString m_szCommand; + + CList< CAccelsOb*, CAccelsOb*& > m_Accels; +}; + + +//////////////////////////////////////////////////////////////////////// +#endif // __CMDACCEL_OB_INCLUDE + + diff --git a/src/win32/ColorButton.cpp b/src/win32/ColorButton.cpp new file mode 100644 index 00000000..b6de51fa --- /dev/null +++ b/src/win32/ColorButton.cpp @@ -0,0 +1,120 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// ColorButton.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "ColorButton.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +bool ColorButton::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ColorButton + +ColorButton::ColorButton() +{ + color = 0; + registerClass(); +} + +ColorButton::~ColorButton() +{ +} + + +BEGIN_MESSAGE_MAP(ColorButton, CButton) + //{{AFX_MSG_MAP(ColorButton) + // NOTE - the ClassWizard will add and remove mapping macros here. + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// ColorButton message handlers + +void ColorButton::PreSubclassWindow() +{ + SetWindowLong(m_hWnd, GWL_STYLE, GetStyle() | BS_OWNERDRAW); + CWnd::PreSubclassWindow(); +} + +void ColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) +{ + ASSERT(lpDrawItemStruct); + + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + HDC dc = lpDrawItemStruct->hDC; + UINT state = lpDrawItemStruct->itemState; + RECT rect = lpDrawItemStruct->rcItem; + + SIZE margins; + margins.cx = ::GetSystemMetrics(SM_CXEDGE); + margins.cy = ::GetSystemMetrics(SM_CYEDGE); + + if(GetState() & BST_PUSHED) + DrawEdge(dc, &rect, EDGE_SUNKEN, BF_RECT); + else + DrawEdge(dc, &rect, EDGE_RAISED, BF_RECT); + + InflateRect(&rect, -margins.cx, -margins.cy); + + HBRUSH br = CreateSolidBrush((state & ODS_DISABLED) ? + ::GetSysColor(COLOR_3DFACE) : RGB(r,g,b)); + + FillRect(dc, &rect, br); + + if(state & ODS_FOCUS) { + InflateRect(&rect, -1, -1); + DrawFocusRect(dc, &rect); + } + + DeleteObject(br); +} + +void ColorButton::setColor(u16 c) +{ + color = c; + Invalidate(); +} + +void ColorButton::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaColorButton"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff --git a/src/win32/ColorButton.h b/src/win32/ColorButton.h new file mode 100644 index 00000000..3d12f74b --- /dev/null +++ b/src/win32/ColorButton.h @@ -0,0 +1,74 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_) +#define AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ColorButton.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ColorButton window + +class ColorButton : public CButton +{ + // Construction + public: + ColorButton(); + + // Attributes + public: + // Operations + static bool isRegistered; + public: + void PreSubclassWindow(); + void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ColorButton) + //}}AFX_VIRTUAL + + // Implementation + public: + void setColor(u16 c); + u16 color; + virtual ~ColorButton(); + + void registerClass(); + + // Generated message map functions + protected: + //{{AFX_MSG(ColorButton) + // NOTE - the ClassWizard will add and remove member functions here. + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_) diff --git a/src/win32/ColorControl.cpp b/src/win32/ColorControl.cpp new file mode 100644 index 00000000..b61d135e --- /dev/null +++ b/src/win32/ColorControl.cpp @@ -0,0 +1,103 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// ColorControl.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "ColorControl.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +bool ColorControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ColorControl + +ColorControl::ColorControl() +{ + color = 0; + registerClass(); +} + +ColorControl::~ColorControl() +{ +} + + +BEGIN_MESSAGE_MAP(ColorControl, CWnd) + //{{AFX_MSG_MAP(ColorControl) + ON_WM_PAINT() + ON_WM_ERASEBKGND() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// ColorControl message handlers + +void ColorControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting +} + +BOOL ColorControl::OnEraseBkgnd(CDC* pDC) +{ + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + CBrush br; + br.CreateSolidBrush(RGB(r,g,b)); + + RECT rect; + GetClientRect(&rect); + pDC->FillRect(&rect,&br); + pDC->DrawEdge(&rect, EDGE_SUNKEN, BF_RECT); + br.DeleteObject(); + return TRUE; +} + +void ColorControl::setColor(u16 c) +{ + color = c; + Invalidate(); +} + +void ColorControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaColorControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff --git a/src/win32/ColorControl.h b/src/win32/ColorControl.h new file mode 100644 index 00000000..e66832c6 --- /dev/null +++ b/src/win32/ColorControl.h @@ -0,0 +1,74 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_) +#define AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ColorControl.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ColorControl window + +class ColorControl : public CWnd +{ + // Construction + public: + ColorControl(); + + // Attributes + public: + // Operations + static bool isRegistered; + + // Operations + public: + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ColorControl) + //}}AFX_VIRTUAL + + // Implementation + public: + void setColor(u16 c); + u16 color; + virtual ~ColorControl(); + + // Generated message map functions + protected: + //{{AFX_MSG(ColorControl) + afx_msg void OnPaint(); + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + void registerClass(); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_) diff --git a/src/win32/Commands.cpp b/src/win32/Commands.cpp new file mode 100644 index 00000000..3e9ae39d --- /dev/null +++ b/src/win32/Commands.cpp @@ -0,0 +1,253 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "AcceleratorManager.h" +#include "..\..\res\resource.h" +#include + +#include // MFC Templates extension +#ifndef CMapStringToWord +typedef CMap< CString, LPCSTR, WORD, WORD& > CMapStringToWord; +#endif + +static CMapStringToWord winAccelStrings; +static bool initialized = false; + +struct { + const char *command; + WORD id; +} winAccelCommands[] = { + { "FileOpen", ID_FILE_OPEN }, + { "FileOpenGameboy", ID_FILE_OPENGAMEBOY }, + { "FileLoad", ID_FILE_LOAD }, + { "FileSave", ID_FILE_SAVE }, + { "FileLoadGame01", ID_FILE_LOADGAME_SLOT1 }, + { "FileLoadGame02", ID_FILE_LOADGAME_SLOT2 }, + { "FileLoadGame03", ID_FILE_LOADGAME_SLOT3 }, + { "FileLoadGame04", ID_FILE_LOADGAME_SLOT4 }, + { "FileLoadGame05", ID_FILE_LOADGAME_SLOT5 }, + { "FileLoadGame06", ID_FILE_LOADGAME_SLOT6 }, + { "FileLoadGame07", ID_FILE_LOADGAME_SLOT7 }, + { "FileLoadGame08", ID_FILE_LOADGAME_SLOT8 }, + { "FileLoadGame09", ID_FILE_LOADGAME_SLOT9 }, + { "FileLoadGame10", ID_FILE_LOADGAME_SLOT10 }, + { "FileLoadGameAutoLoad", ID_FILE_LOADGAME_AUTOLOADMOSTRECENT }, + { "FileLoadGameRecent", ID_FILE_LOADGAME_MOSTRECENT }, + { "FileSaveGame01", ID_FILE_SAVEGAME_SLOT1 }, + { "FileSaveGame02", ID_FILE_SAVEGAME_SLOT2 }, + { "FileSaveGame03", ID_FILE_SAVEGAME_SLOT3 }, + { "FileSaveGame04", ID_FILE_SAVEGAME_SLOT4 }, + { "FileSaveGame05", ID_FILE_SAVEGAME_SLOT5 }, + { "FileSaveGame06", ID_FILE_SAVEGAME_SLOT6 }, + { "FileSaveGame07", ID_FILE_SAVEGAME_SLOT7 }, + { "FileSaveGame08", ID_FILE_SAVEGAME_SLOT8 }, + { "FileSaveGame09", ID_FILE_SAVEGAME_SLOT9 }, + { "FileSaveGame10", ID_FILE_SAVEGAME_SLOT10 }, + { "FileSaveGameOldest", ID_FILE_SAVEGAME_OLDESTSLOT }, + { "FileRecentReset", ID_FILE_RECENT_RESET }, + { "FileRecentFreeze", ID_FILE_RECENT_FREEZE }, + { "FileRecent01", ID_FILE_MRU_FILE1 }, + { "FileRecent02", ID_FILE_MRU_FILE2 }, + { "FileRecent03", ID_FILE_MRU_FILE3 }, + { "FileRecent04", ID_FILE_MRU_FILE4 }, + { "FileRecent05", ID_FILE_MRU_FILE5 }, + { "FileRecent06", ID_FILE_MRU_FILE6 }, + { "FileRecent07", ID_FILE_MRU_FILE7 }, + { "FileRecent08", ID_FILE_MRU_FILE8 }, + { "FileRecent09", ID_FILE_MRU_FILE9 }, + { "FileRecent10", ID_FILE_MRU_FILE10 }, + { "FilePause", ID_FILE_PAUSE }, + { "FileReset", ID_FILE_RESET }, + { "FileImportBatteryFile", ID_FILE_IMPORT_BATTERYFILE }, + { "FileImportGamesharkCodeFile", ID_FILE_IMPORT_GAMESHARKCODEFILE }, + { "FileImportGamesharkSnapshot", ID_FILE_IMPORT_GAMESHARKSNAPSHOT }, + { "FileExportBatteryFile", ID_FILE_EXPORT_BATTERYFILE }, + { "FileExportGamesharkSnapshot", ID_FILE_EXPORT_GAMESHARKSNAPSHOT }, + { "FileScreenCapture", ID_FILE_SCREENCAPTURE }, + { "FileRomInformation", ID_FILE_ROMINFORMATION }, + { "FileToggleMenu", ID_FILE_TOGGLEMENU }, + { "FileClose", ID_FILE_CLOSE }, + { "FileExit", ID_FILE_EXIT }, + { "OptionsFrameSkip0", ID_OPTIONS_VIDEO_FRAMESKIP_0 }, + { "OptionsFrameSkip1", ID_OPTIONS_VIDEO_FRAMESKIP_1 }, + { "OptionsFrameSkip2", ID_OPTIONS_VIDEO_FRAMESKIP_2 }, + { "OptionsFrameSkip3", ID_OPTIONS_VIDEO_FRAMESKIP_3 }, + { "OptionsFrameSkip4", ID_OPTIONS_VIDEO_FRAMESKIP_4 }, + { "OptionsFrameSkip5", ID_OPTIONS_VIDEO_FRAMESKIP_5 }, + { "OptionsFrameSkip6", ID_OPTIONS_VIDEO_FRAMESKIP_6 }, + { "OptionsFrameSkip7", ID_OPTIONS_VIDEO_FRAMESKIP_7 }, + { "OptionsFrameSkip8", ID_OPTIONS_VIDEO_FRAMESKIP_8 }, + { "OptionsFrameSkip9", ID_OPTIONS_VIDEO_FRAMESKIP_9 }, + { "OptionsThrottleNone", ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE }, + { "OptionsThrottle025%", ID_OPTIONS_FRAMESKIP_THROTTLE_25 }, + { "OptionsThrottle050%", ID_OPTIONS_FRAMESKIP_THROTTLE_50 }, + { "OptionsThrottle100%", ID_OPTIONS_FRAMESKIP_THROTTLE_100 }, + { "OptionsThrottle150%", ID_OPTIONS_FRAMESKIP_THROTTLE_150 }, + { "OptionsThrottle200%", ID_OPTIONS_FRAMESKIP_THROTTLE_200 }, + { "OptionsThrottleOther", ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER }, + { "OptionsVideoRenderGDI", ID_OPTIONS_VIDEO_RENDERMETHOD_GDI }, + { "OptionsVideoRenderDDRAW", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW }, + { "OptionsVideoRenderD3D", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D }, + { "OptionsVideoRenderOGL", ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL }, + { "OptionsVideoVsync", ID_OPTIONS_VIDEO_VSYNC }, + { "OptionsVideoX1", ID_OPTIONS_VIDEO_X1 }, + { "OptionsVideoX2", ID_OPTIONS_VIDEO_X2 }, + { "OptionsVideoX3", ID_OPTIONS_VIDEO_X3 }, + { "OptionsVideoX4", ID_OPTIONS_VIDEO_X4 }, + { "OptionsVideo320x240", ID_OPTIONS_VIDEO_FULLSCREEN320X240 }, + { "OptionsVideo640x480", ID_OPTIONS_VIDEO_FULLSCREEN640X480 }, + { "OptionsVideo800x600", ID_OPTIONS_VIDEO_FULLSCREEN800X600 }, + { "OptionsVideoFullscreen", ID_OPTIONS_VIDEO_FULLSCREEN }, + { "OptionsVideoFullscreenMaxScale", ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE }, + { "OptionsVideoLayersBg0", ID_OPTIONS_VIDEO_LAYERS_BG0 }, + { "OptionsVideoLayersBg1", ID_OPTIONS_VIDEO_LAYERS_BG1 }, + { "OptionsVideoLayersBg2", ID_OPTIONS_VIDEO_LAYERS_BG2 }, + { "OptionsVideoLayersBg3", ID_OPTIONS_VIDEO_LAYERS_BG3 }, + { "OptionsVideoLayersOBJ", ID_OPTIONS_VIDEO_LAYERS_OBJ }, + { "OptionsVideoLayersWIN0", ID_OPTIONS_VIDEO_LAYERS_WIN0 }, + { "OptionsVideoLayersWIN1", ID_OPTIONS_VIDEO_LAYERS_WIN1 }, + { "OptionsVideoLayersOBJWIN", ID_OPTIONS_VIDEO_LAYERS_OBJWIN }, + { "OptionsEmulatorAssociate", ID_OPTIONS_EMULATOR_ASSOCIATE }, + { "OptionsEmulatorDirectories", ID_OPTIONS_EMULATOR_DIRECTORIES }, + { "OptionsEmulatorSelectBIOS", ID_OPTIONS_EMULATOR_SELECTBIOSFILE }, + { "OptionsEmulatorUseBIOS", ID_OPTIONS_EMULATOR_USEBIOSFILE }, + { "OptionsEmulatorSkipBIOS", ID_OPTIONS_EMULATOR_SKIPBIOS }, + { "OptionsEmulatorShowSpeedNone", ID_OPTIONS_EMULATOR_SHOWSPEED_NONE }, + { "OptionsEmulatorShowSpeedPercentage", ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE }, + { "OptionsEmulatorShowSpeedDetailed", ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED }, + { "OptionsEmulatorShowSpeedTransparent", ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT }, + { "OptionsEmulatorSpeedupToggle", ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE }, + { "OptionsEmulatorRemoveIntros", ID_OPTIONS_EMULATOR_REMOVEINTROSGBA }, + { "OptionsEmulatorAutoHideMenu", ID_OPTIONS_EMULATOR_AUTOHIDEMENU }, + { "OptionsEmulatorSaveAuto", ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC }, + { "OptionsEmulatorSaveEEPROM", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM }, + { "OptionsEmulatorSaveSRAM", ID_OPTIONS_EMULATOR_SAVETYPE_SRAM }, + { "OptionsEmulatorSaveFLASH", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH }, + { "OptionsEmulatorSaveEEPROMSensor", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR }, + { "OptionsEmulatorSaveFlash64K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K }, + { "OptionsEmulatorSaveFlash128K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M }, + { "OptionsEmulatorAutoIPSPatch", ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH }, + { "OptionsEmulatorAGBPrint", ID_OPTIONS_EMULATOR_AGBPRINT }, + { "OptionsEmulatorRTC", ID_OPTIONS_EMULATOR_REALTIMECLOCK }, + { "OptionsEmulatorRewindInterval", ID_OPTIONS_EMULATOR_REWINDINTERVAL }, + { "OptionsSoundOff", ID_OPTIONS_SOUND_OFF }, + { "OptionsSoundMute", ID_OPTIONS_SOUND_OFF }, /* mute hax */ + { "OptionsSoundOn", ID_OPTIONS_SOUND_ON }, + { "OptionsSoundChannel1", ID_OPTIONS_SOUND_CHANNEL1 }, + { "OptionsSoundChannel2", ID_OPTIONS_SOUND_CHANNEL2 }, + { "OptionsSoundChannel3", ID_OPTIONS_SOUND_CHANNEL3 }, + { "OptionsSoundChannel4", ID_OPTIONS_SOUND_CHANNEL4 }, + { "OptionsSoundDirectSoundA", ID_OPTIONS_SOUND_DIRECTSOUNDA }, + { "OptionsSoundDirectSoundB", ID_OPTIONS_SOUND_DIRECTSOUNDB }, + { "OptionsSound11Khz", ID_OPTIONS_SOUND_11KHZ }, + { "OptionsSound22Khz", ID_OPTIONS_SOUND_22KHZ }, + { "OptionsSound44Khz", ID_OPTIONS_SOUND_44KHZ }, + { "OptionsSoundEcho", ID_OPTIONS_SOUND_ECHO }, + { "OptionsSoundLowPassFilter", ID_OPTIONS_SOUND_LOWPASSFILTER }, + { "OptionsSoundReverseStereo", ID_OPTIONS_SOUND_REVERSESTEREO }, + { "OptionsSoundVolume1x", ID_OPTIONS_SOUND_VOLUME_1X }, + { "OptionsSoundVolume2x", ID_OPTIONS_SOUND_VOLUME_2X }, + { "OptionsSoundVolume3x", ID_OPTIONS_SOUND_VOLUME_3X }, + { "OptionsSoundVolume4x", ID_OPTIONS_SOUND_VOLUME_4X }, + { "OptionsGameboyBorder", ID_OPTIONS_GAMEBOY_BORDER }, + { "OptionsGameboyBorderAutomatic", ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC }, + { "OptionsGameboyColors", ID_OPTIONS_GAMEBOY_COLORS }, + { "OptionsFilterNormal", ID_OPTIONS_FILTER_NORMAL }, + { "OptionsFilterTVMode", ID_OPTIONS_FILTER_TVMODE }, + { "OptionsFilter2xSaI", ID_OPTIONS_FILTER_2XSAI }, + { "OptionsFilterSuper2xSaI", ID_OPTIONS_FILTER_SUPER2XSAI }, + { "OptionsFilterSuperEagle", ID_OPTIONS_FILTER_SUPEREAGLE }, + { "OptionsFilterPixelate", ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL }, + { "OptionsFilterAdMameScale2x", ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X }, + { "OptionsFilterSimple2x", ID_OPTIONS_FILTER16BIT_SIMPLE2X }, + { "OptionsFilterBilinear", ID_OPTIONS_FILTER_BILINEAR }, + { "OptionsFilterBilinearPlus", ID_OPTIONS_FILTER_BILINEARPLUS }, + { "OptionsFilterScanlines", ID_OPTIONS_FILTER_SCANLINES }, + { "OptionsFilterHq2x", ID_OPTIONS_FILTER_HQ2X }, + { "OptionsFilterLq2x", ID_OPTIONS_FILTER_LQ2X }, + { "OptionsFilterIFBNone", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE }, + { "OptionsFilterIFBMotionBlur", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR }, + { "OptionsFilterIFBSmart", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART }, + { "OptionsFilterDisableMMX", ID_OPTIONS_FILTER_DISABLEMMX }, + { "OptionsJoypadConfigure1", ID_OPTIONS_JOYPAD_CONFIGURE_1 }, + { "OptionsJoypadConfigure2", ID_OPTIONS_JOYPAD_CONFIGURE_2 }, + { "OptionsJoypadConfigure3", ID_OPTIONS_JOYPAD_CONFIGURE_3 }, + { "OptionsJoypadConfigure4", ID_OPTIONS_JOYPAD_CONFIGURE_4 }, + { "OptionsJoypadMotionConfigure", ID_OPTIONS_JOYPAD_MOTIONCONFIGURE }, + { "OptionsJoypadAutofireA", ID_OPTIONS_JOYPAD_AUTOFIRE_A }, + { "OptionsJoypadAutofireB", ID_OPTIONS_JOYPAD_AUTOFIRE_B }, + { "OptionsJoypadAutofireL", ID_OPTIONS_JOYPAD_AUTOFIRE_L }, + { "OptionsJoypadAutofireR", ID_OPTIONS_JOYPAD_AUTOFIRE_R }, + { "CheatsSearch", ID_CHEATS_SEARCHFORCHEATS }, + { "CheatsList", ID_CHEATS_CHEATLIST }, + { "CheatsLoad", ID_CHEATS_LOADCHEATLIST }, + { "CheatsSave", ID_CHEATS_SAVECHEATLIST }, + { "CheatsDisable", ID_CHEATS_DISABLECHEATS }, + { "ToolsDebugGDB", ID_TOOLS_DEBUG_GDB }, + { "ToolsDebugGDBLoad", ID_TOOLS_DEBUG_LOADANDWAIT }, + { "ToolsDebugGDBBreak", ID_TOOLS_DEBUG_BREAK }, + { "ToolsDebugGDBDisconnect", ID_TOOLS_DEBUG_DISCONNECT }, + { "ToolsDisassemble", ID_TOOLS_DISASSEMBLE }, + { "ToolsIOViewer", ID_TOOLS_IOVIEWER }, + { "ToolsLogging", ID_TOOLS_LOGGING }, + { "ToolsMapViewer", ID_TOOLS_MAPVIEW }, + { "ToolsMemoryViewer", ID_TOOLS_MEMORYVIEWER }, + { "ToolsOAMViewer", ID_TOOLS_OAMVIEWER }, + { "ToolsPaletteViewer", ID_TOOLS_PALETTEVIEW }, + { "ToolsTileViewer", ID_TOOLS_TILEVIEWER }, + { "ToolsNextFrame", ID_DEBUG_NEXTFRAME }, + { "ToolsRecordSoundStartRecording", ID_OPTIONS_SOUND_STARTRECORDING }, + { "ToolsRecordSoundStopRecording", ID_OPTIONS_SOUND_STOPRECORDING }, + { "ToolsRecordAVIStartRecording", ID_TOOLS_RECORD_STARTAVIRECORDING }, + { "ToolsRecordAVIStopRecording", ID_TOOLS_RECORD_STOPAVIRECORDING }, + { "ToolsRecordMovieStartRecording", ID_TOOLS_RECORD_STARTMOVIERECORDING }, + { "ToolsRecordMovieStopRecording", ID_TOOLS_RECORD_STOPMOVIERECORDING }, + { "ToolsPlayMovieStartPlaying", ID_TOOLS_PLAY_STARTMOVIEPLAYING }, + { "ToolsPlayMovieStopPlaying", ID_TOOLS_PLAY_STOPMOVIEPLAYING }, + { "ToolsRewind", ID_TOOLS_REWIND }, + { "ToolsCustomize", ID_TOOLS_CUSTOMIZE }, + { "HelpBugReport", ID_HELP_BUGREPORT }, + { "HelpFAQ", ID_HELP_FAQ }, + { "HelpAbout", ID_HELP_ABOUT } +}; + +bool winAccelGetID(const char *command, WORD& id) +{ + if(!initialized) { + int count = sizeof(winAccelCommands)/sizeof(winAccelCommands[0]); + + for(int i = 0; i < count; i++) { + winAccelStrings.SetAt(winAccelCommands[i].command, winAccelCommands[i].id); + } + initialized = true; + } + + return winAccelStrings.Lookup(command, id) ? true : false; +} + +void winAccelAddCommands(CAcceleratorManager& mgr) +{ + int count = sizeof(winAccelCommands)/sizeof(winAccelCommands[0]); + + for(int i = 0; i < count; i++) { + if(!mgr.AddCommandAccel(winAccelCommands[i].id, winAccelCommands[i].command, false)) + mgr.CreateEntry(winAccelCommands[i].id, winAccelCommands[i].command); + } + +} diff --git a/src/win32/Direct3D.cpp b/src/win32/Direct3D.cpp new file mode 100644 index 00000000..5d53eee8 --- /dev/null +++ b/src/win32/Direct3D.cpp @@ -0,0 +1,950 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include +#include +#include "vba.h" +#include "MainWnd.h" +#include "UniVideoModeDlg.h" +#include "../Util.h" +#include "../Globals.h" +#include "../Util.h" +#include "../gb/gbGlobals.h" + +#include "../gbafilter.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#endif + +#ifdef MMX +extern "C" bool cpu_mmx; +extern bool detectMMX(); +#endif + +extern int Init_2xSaI(u32); +extern void winlog(const char *,...); +extern int systemSpeed; + + +// Textured Vertex +typedef struct _D3DTLVERTEX { + float sx; /* Screen coordinates */ + float sy; + float sz; + float rhw; /* Reciprocal of homogeneous w */ + D3DCOLOR color; /* Vertex color */ + float tu; /* Texture coordinates */ + float tv; + _D3DTLVERTEX() { } + _D3DTLVERTEX( + const D3DVECTOR& v, + float _rhw, + D3DCOLOR _color, + float _tu, float _tv) + { sx = v.x; sy = v.y; sz = v.z; + rhw = _rhw; + color = _color; + tu = _tu; tv = _tv; } +} D3DTLVERTEX, *LPD3DTLVERTEX; +#define D3DFVF_TLVERTEX D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1 + + +// Simple Vertex +struct D3DVERTEX_SIMPLE +{ + FLOAT x, y, z, rhw; + D3DCOLOR color; +}; +#define D3DFVF_SIMPLE D3DFVF_XYZRHW | D3DFVF_DIFFUSE + + +class Direct3DDisplay : public IDisplay +{ +private: + HINSTANCE d3dDLL; + LPDIRECT3D9 pD3D; + LPDIRECT3DDEVICE9 pDevice; + LPDIRECT3DTEXTURE9 pTexture; + D3DPRESENT_PARAMETERS dpp; + D3DFORMAT screenFormat; + int width, height; // Size of the source image to display + bool filterDisabled; + ID3DXFont *pFont; + bool failed; + D3DTLVERTEX verts[4]; // The coordinates for our texture + D3DVERTEX_SIMPLE msgBox[4]; + int textureWidth; // Size of the texture, + int textureHeight; // where the source image is copied to + int SelectedFreq, SelectedAdapter; + bool fullscreen; + + void restoreDeviceObjects(); + void invalidateDeviceObjects(); + bool initializeOffscreen(unsigned int w, unsigned int h); + void updateFiltering(int); + void updateVSync(void); + +public: + Direct3DDisplay(); + virtual ~Direct3DDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual void resize(int w, int h); + virtual DISPLAY_TYPE getType() { return DIRECT_3D; }; + virtual void setOption(const char *, int); + virtual int selectFullScreenMode(GUID **); + virtual int selectFullScreenMode2(); +}; + +Direct3DDisplay::Direct3DDisplay() +{ + d3dDLL = NULL; + pD3D = NULL; + pDevice = NULL; + pTexture = NULL; + pFont = NULL; + screenFormat = D3DFMT_R5G6B5; + width = 0; + height = 0; + filterDisabled = false; + failed = false; +} + +Direct3DDisplay::~Direct3DDisplay() +{ + cleanup(); +} + +void Direct3DDisplay::cleanup() +{ + if(pD3D != NULL) { + if(pFont) { + pFont->Release(); + pFont = NULL; + } + + if(pTexture) + { + pTexture->Release(); + pTexture = NULL; + } + + if(pDevice) { + pDevice->Release(); + pDevice = NULL; + } + + pD3D->Release(); + pD3D = NULL; + + if(d3dDLL != NULL) { + FreeLibrary(d3dDLL); + d3dDLL = NULL; + } + } +} + +bool Direct3DDisplay::initialize() +{ + // Get emulated image's dimensions + switch (theApp.cartridgeType) + { + case IMAGE_GBA: + theApp.sizeX = 240; + theApp.sizeY = 160; + break; + case IMAGE_GB: + if (gbBorderOn) + { + theApp.sizeX = 256; + theApp.sizeY = 224; + } + else + { + theApp.sizeX = 160; + theApp.sizeY = 144; + } + break; + } + + theApp.rect.left = 0; + theApp.rect.top = 0; + theApp.rect.right = theApp.sizeX; + theApp.rect.bottom = theApp.sizeY; + + + switch(theApp.videoOption) + { + case VIDEO_1X: + theApp.surfaceSizeX = theApp.sizeX; + theApp.surfaceSizeY = theApp.sizeY; + fullscreen = false; + break; + case VIDEO_2X: + theApp.surfaceSizeX = theApp.sizeX * 2; + theApp.surfaceSizeY = theApp.sizeY * 2; + fullscreen = false; + break; + case VIDEO_3X: + theApp.surfaceSizeX = theApp.sizeX * 3; + theApp.surfaceSizeY = theApp.sizeY * 3; + fullscreen = false; + break; + case VIDEO_4X: + theApp.surfaceSizeX = theApp.sizeX * 4; + theApp.surfaceSizeY = theApp.sizeY * 4; + fullscreen = false; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + float scaleX = ((float)theApp.fsWidth / theApp.sizeX); + float scaleY = ((float)theApp.fsHeight / theApp.sizeY); + float min = scaleX < scaleY ? scaleX : scaleY; + if(theApp.fsMaxScale) + min = min > theApp.fsMaxScale ? theApp.fsMaxScale : min; + if(theApp.fullScreenStretch) + { + theApp.surfaceSizeX = theApp.fsWidth; + theApp.surfaceSizeY = theApp.fsHeight; + } + else + { + theApp.surfaceSizeX = (int)(theApp.sizeX * min); + theApp.surfaceSizeY = (int)(theApp.sizeY * min); + } + fullscreen = true; + break; + } + + + theApp.dest.left = 0; + theApp.dest.top = 0; + theApp.dest.right = theApp.surfaceSizeX; + theApp.dest.bottom = theApp.surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + style |= WS_OVERLAPPEDWINDOW; + else + styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + AdjustWindowRectEx(&theApp.dest, style, TRUE, styleEx); + else + AdjustWindowRectEx(&theApp.dest, style, FALSE, styleEx); + + int winSizeX = theApp.dest.right-theApp.dest.left; + int winSizeY = theApp.dest.bottom-theApp.dest.top; + + if(theApp.videoOption > VIDEO_4X) { + winSizeX = theApp.fsWidth; + winSizeY = theApp.fsHeight; + } + int x = 0; + int y = 0; + + if(theApp.videoOption <= VIDEO_4X) { + x = theApp.windowPositionX; + y = theApp.windowPositionY; + } + + // Create a window + MainWnd *pWnd = new MainWnd; + theApp.m_pMainWnd = pWnd; + + pWnd->CreateEx( + styleEx, + theApp.wndClass, + "VisualBoyAdvance", + style, + x, y, + winSizeX, winSizeY, + NULL, + 0); + + if (!(HWND)*pWnd) + { + winlog("Error creating Window %08x\n", GetLastError()); + return FALSE; + } + + theApp.updateMenuBar(); + + theApp.adjustDestRect(); + + + // Load DirectX DLL + d3dDLL = LoadLibrary("D3D9.DLL"); + LPDIRECT3D9 (WINAPI *D3DCreate)(UINT); + if(d3dDLL != NULL) + { + D3DCreate = (LPDIRECT3D9 (WINAPI *)(UINT)) + GetProcAddress(d3dDLL, "Direct3DCreate9"); + + if(D3DCreate == NULL) + { + theApp.directXMessage("Direct3DCreate9"); + return FALSE; + } + } + else + { + theApp.directXMessage("D3D9.DLL"); + return FALSE; + } + + pD3D = D3DCreate(D3D_SDK_VERSION); + + if(pD3D == NULL) + { + winlog("Error creating Direct3D object\n"); + return FALSE; + } + + + + + // Display resolution + D3DDISPLAYMODE mode; + pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode); + + switch(mode.Format) { + case D3DFMT_R8G8B8: + systemColorDepth = 24; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + break; + case D3DFMT_X8R8G8B8: + systemColorDepth = 32; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + Init_2xSaI(32); + break; + case D3DFMT_R5G6B5: + systemColorDepth = 16; + systemRedShift = 11; + systemGreenShift = 6; + systemBlueShift = 0; + Init_2xSaI(565); + break; + case D3DFMT_X1R5G5B5: + systemColorDepth = 16; + systemRedShift = 10; + systemGreenShift = 5; + systemBlueShift = 0; + Init_2xSaI(555); + break; + default: + systemMessage(0,"Unsupported D3D format %d", mode.Format); + return false; + } + theApp.fsColorDepth = systemColorDepth; + + // Check the available fullscreen modes and enable menu items + unsigned int nModes, i; + D3DDISPLAYMODE dm; + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + + nModes = pD3D->GetAdapterModeCount(theApp.fsAdapter, D3DFMT_R5G6B5); + for (i = 0; iEnumAdapterModes(theApp.fsAdapter, D3DFMT_R5G6B5, i, &dm) ) + { + if ( (dm.Width == 320) && (dm.Height == 240) ) + theApp.mode320Available = true; + if ( (dm.Width == 640) && (dm.Height == 480) ) + theApp.mode640Available = true; + if ( (dm.Width == 800) && (dm.Height == 600) ) + theApp.mode800Available = true; + } + } + + +#ifdef MMX + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + screenFormat = mode.Format; + + // Change display mode + ZeroMemory(&dpp, sizeof(dpp)); + dpp.Windowed = !fullscreen; + if (fullscreen) + dpp.BackBufferFormat = + (theApp.fsColorDepth == 32) ? D3DFMT_X8R8G8B8 : D3DFMT_R5G6B5; + else + dpp.BackBufferFormat = mode.Format; + dpp.BackBufferCount = 1; + dpp.MultiSampleType = D3DMULTISAMPLE_NONE; + dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; + dpp.BackBufferWidth = fullscreen ? theApp.fsWidth : theApp.surfaceSizeX; + dpp.BackBufferHeight = fullscreen ? theApp.fsHeight : theApp.surfaceSizeY; + dpp.hDeviceWindow = pWnd->GetSafeHwnd(); + dpp.FullScreen_RefreshRateInHz = fullscreen ? theApp.fsFrequency : 0; + dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; + if (theApp.vsync) + dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // VSync + else + dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // No Sync + + HRESULT hret = pD3D->CreateDevice(theApp.fsAdapter, + D3DDEVTYPE_HAL, + pWnd->GetSafeHwnd(), + D3DCREATE_SOFTWARE_VERTEXPROCESSING, + &dpp, + &pDevice); + if(hret != D3D_OK) + { + winlog("Error creating Direct3DDevice %08x\n", hret); + return false; + } + pDevice->SetDialogBoxMode(TRUE); // !!! Enable menu and windows !!! + + restoreDeviceObjects(); + + + // Set the status message's background vertex information, that does not need to be changed in realtime + msgBox[0].z = 0.5f; + msgBox[0].rhw = 1.0f; + msgBox[0].color = 0x7fffffff; + msgBox[1].z = 0.5f; + msgBox[1].rhw = 1.0f; + msgBox[1].color = 0x7f7f7f7f; + msgBox[2].z = 0.5f; + msgBox[2].rhw = 1.0f; + msgBox[2].color = 0x7f7f7fff; + msgBox[3].z = 0.5f; + msgBox[3].rhw = 1.0f; + msgBox[3].color = 0x7f7f7f7f; + + utilUpdateSystemColorMaps(theApp.filterLCD ); + theApp.updateFilter(); + theApp.updateIFB(); + + if(failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + +bool Direct3DDisplay::initializeOffscreen(unsigned int w, unsigned int h) +{ + D3DFORMAT format = screenFormat; + unsigned int correctedWidth=w, correctedHeight=h; + + // This function corrects the texture size automaticly + if(D3D_OK == D3DXCheckTextureRequirements( + pDevice, + &correctedWidth, + &correctedHeight, + NULL, + 0, + &format, + D3DPOOL_MANAGED)) + { + if( (correctedWidth < w) || (correctedHeight < h) ) + { + if(theApp.filterFunction) + { + filterDisabled = true; + theApp.filterFunction = NULL; + systemMessage(0, "3D card cannot support needed texture size for filter function. Disabling it"); + } + else + systemMessage(0, "Graphics card doesn't support needed texture size for emulation."); + } + else filterDisabled = false; + + if(D3D_OK == D3DXCreateTexture( + pDevice, + correctedWidth, + correctedHeight, + D3DX_DEFAULT, + 0, + format, + D3DPOOL_MANAGED, + &pTexture) ) + { + width = w; + height = h; + textureWidth = correctedWidth; + textureHeight = correctedHeight; + return true; + } + else systemMessage(0, "Texture creation failed"); + } + return false; +} + + +void Direct3DDisplay::updateFiltering(int filter) +{ + switch(filter) { + default: + case 0: + // point filtering + pDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); + pDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); + pDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT ); + break; + case 1: + // bilinear + pDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); + pDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); + pDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT ); + break; + } +} + +void Direct3DDisplay::clear() +{ + if (pDevice) + pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, +#ifdef _DEBUG + 0xffff00ff //pink +#else + 0xff000000 //black +#endif + , 1.0f, 0L ); +} + +void Direct3DDisplay::renderMenu() +{ + checkFullScreen(); + if(theApp.m_pMainWnd) + theApp.m_pMainWnd->DrawMenuBar(); +} + +void Direct3DDisplay::checkFullScreen() +{ + //if(tripleBuffering) + //pDirect3D->FlipToGDISurface(); +} + +void Direct3DDisplay::render() +{ + unsigned int nBytesPerPixel = systemColorDepth / 8; //This is the byte count of a Pixel + unsigned int pitch = (theApp.filterWidth * nBytesPerPixel) + 4; + HRESULT hr; + + if(!pDevice) return; + + + // Test the cooperative level to see if it's okay to render + hr = pDevice->TestCooperativeLevel(); + if(hr != D3D_OK) + { + switch (hr) + { + case D3DERR_DEVICELOST: + break; + case D3DERR_DEVICENOTRESET: + invalidateDeviceObjects(); + hr = pDevice->Reset(&dpp); + if( hr == D3D_OK ) + { + restoreDeviceObjects(); + } +#ifdef _DEBUG + else + switch (hr) + { + case D3DERR_DEVICELOST: + winlog("Render_DeviceLost: D3DERR_DEVICELOST\n"); + break; + case D3DERR_DRIVERINTERNALERROR: + winlog("Render_DeviceLost: D3DERR_DRIVERINTERNALERROR\n"); + break; + case D3DERR_INVALIDCALL: + winlog("Render_DeviceLost: D3DERR_INVALIDCALL\n"); + break; + case D3DERR_OUTOFVIDEOMEMORY: + winlog("Render_DeviceLost: D3DERR_OUTOFVIDEOMEMORY\n"); + break; + case E_OUTOFMEMORY: + winlog("Render_DeviceLost: E_OUTOFMEMORY\n"); + break; + } +#endif + break; + case D3DERR_DRIVERINTERNALERROR: + winlog("Render: D3DERR_DRIVERINTERNALERROR\n"); + theApp.ExitInstance(); + break; + } + return; + } + + // Clear the screen + if (pDevice) + pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, +#ifdef _DEBUG + 0xffff00ff //pink +#else + 0xff000000 //black +#endif + , 1.0f, 0L ); + + + if(SUCCEEDED(pDevice->BeginScene())) + { + D3DLOCKED_RECT locked; + + if( D3D_OK == pTexture->LockRect(0, &locked, NULL, D3DLOCK_DISCARD) ) + { + if(theApp.filterFunction) + { + theApp.filterFunction( + pix + pitch, + pitch, + (u8*)theApp.delta, + (u8*)locked.pBits, + locked.Pitch, + theApp.filterWidth, + theApp.filterHeight); + } + else + { + // Copy the image at [pix] to the locked Direct3D texture + __asm + { + mov eax, theApp.sizeX ; Initialize + mov ebx, theApp.sizeY ; + mov edi, locked.pBits ; + mov edx, locked.Pitch ; + + cmp systemColorDepth, 16 ; Check systemColorDepth==16bit + jnz gbaOtherColor ; + sub edx, eax ; + sub edx, eax ; + mov esi, pix ; + lea esi,[esi+2*eax+4] ; + shr eax, 1 ; +gbaLoop16bit: + mov ecx, eax ; + rep movsd ; + add esi, 4 ; + add edi, edx ; + dec ebx ; + jnz gbaLoop16bit ; + jmp gbaLoopEnd ; +gbaOtherColor: + cmp systemColorDepth, 32 ; Check systemColorDepth==32bit + jnz gbaOtherColor2 ; + + lea esi, [eax*4] ; + sub edx, esi ; + mov esi, pix ; + lea esi, [esi+4*eax+4] ; +gbaLoop32bit: + mov ecx, eax ; + rep movsd ; ECX times: Move DWORD at [ESI] to [EDI] | ESI++ EDI++ + add esi, 4 ; + add edi, edx ; + dec ebx ; + jnz gbaLoop32bit ; + jmp gbaLoopEnd ; +gbaOtherColor2: + lea eax, [eax+2*eax] ; Work like systemColorDepth==24bit + sub edx, eax ; +gbaLoop24bit: + mov ecx, eax ; + shr ecx, 2 ; + rep movsd ; + add edi, edx ; + dec ebx ; + jnz gbaLoop24bit ; +gbaLoopEnd: + } + + // C Version of the code above + //int x,y,i; + //int srcPitch = (theApp.sizeX+1) * nBytesPerPixel; + //unsigned char * src = ((unsigned char*)pix)+srcPitch; + //unsigned char * dst = (unsigned char*)locked.pBits; + //for (y=0;yUnlockRect(0); + + // Set the edges of the texture + POINT p1, p2; + p1.x = theApp.dest.left; + p1.y = theApp.dest.top; + p2.x = theApp.dest.right; + p2.y = theApp.dest.bottom; + theApp.m_pMainWnd->ScreenToClient(&p1); + theApp.m_pMainWnd->ScreenToClient(&p2); + + FLOAT left, right, top, bottom; + left = (FLOAT)(p1.x); + top = (FLOAT)(p1.y); + right = (FLOAT)(p2.x); + bottom = (FLOAT)(p2.y); + + right *= (FLOAT)textureWidth/theApp.rect.right; + bottom *= (FLOAT)textureHeight/theApp.rect.bottom; + + + verts[0] = D3DTLVERTEX(D3DXVECTOR3( left, top, 0.0f), 1.0f, 0xffffffff, 0.0f, 0.0f ); + verts[1] = D3DTLVERTEX(D3DXVECTOR3( right, top, 0.0f), 1.0f, 0xffffffff, 1.0f, 0.0f ); + verts[2] = D3DTLVERTEX(D3DXVECTOR3( right, bottom, 0.0f), 1.0f, 0xffffffff, 1.0f, 1.0f ); + verts[3] = D3DTLVERTEX(D3DXVECTOR3( left, bottom, 0.0f), 1.0f, 0xffffffff, 0.0f, 1.0f ); + + pDevice->SetFVF( D3DFVF_TLVERTEX ); + pDevice->SetTexture( 0, pTexture ); + pDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, verts, sizeof(D3DTLVERTEX) ); + pTexture->UnlockRect(0); + } // SUCCEEDED(pTexture->LockRect... + else + { + systemMessage(0,"Rendering error"); + } + + + // Draw the screen message + if(theApp.screenMessage) + { + if( ((GetTickCount() - theApp.screenMessageTime) < 3000) && + !theApp.disableStatusMessage && pFont ) + { + CRect msgRect( + 64, + dpp.BackBufferHeight - 32, + dpp.BackBufferWidth - 64, + dpp.BackBufferHeight); + + msgBox[0].x = (FLOAT)msgRect.left; + msgBox[0].y = (FLOAT)msgRect.top; + msgBox[1].x = (FLOAT)msgRect.right; + msgBox[1].y = (FLOAT)msgRect.top; + msgBox[2].x = (FLOAT)msgRect.right; + msgBox[2].y = (FLOAT)msgRect.bottom; + msgBox[3].x = (FLOAT)msgRect.left; + msgBox[3].y = (FLOAT)msgRect.bottom; + + pDevice->SetFVF( D3DFVF_SIMPLE ); + pDevice->SetTexture( 0, NULL ); + pDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, msgBox, sizeof(D3DVERTEX_SIMPLE)); + + pFont->DrawText(NULL, theApp.screenMessageBuffer, -1, msgRect, DT_CENTER | DT_VCENTER, 0x7fff0000); + } + else theApp.screenMessage = false; + } + + + // Draw the speed + if( (theApp.videoOption > VIDEO_4X) && theApp.showSpeed ) + { + // Create the string text + char buffer[30]; + if(theApp.showSpeed == TRUE) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + + //Draw the string + D3DCOLOR speedColor; + theApp.showSpeedTransparent==TRUE ? speedColor = 0x7fffffff : speedColor = 0xffffffff; + + CRect speedRect( + 64, + 0, + dpp.BackBufferWidth - 64, + 32); + + pFont->DrawText(NULL, buffer, -1, speedRect, DT_CENTER | DT_VCENTER, speedColor); + } + + pDevice->EndScene(); + + pDevice->Present( NULL, NULL, NULL, NULL ); //Draw everything to the screen + } +} + + +void Direct3DDisplay::invalidateDeviceObjects() +{ + if(pFont) + pFont->Release(); + pFont = NULL; +} + +void Direct3DDisplay::restoreDeviceObjects() +{ + updateFiltering(theApp.d3dFilter); + + // Enable transparent vectors + pDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE ); + pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); + pDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); + pDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); + pDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); + + + // Create the font + D3DXCreateFont( pDevice, 24, 0, FW_BOLD, 1, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE, "Arial", &pFont ); +} + + +void Direct3DDisplay::resize(int w, int h) +{ + if(pDevice) + { + dpp.BackBufferWidth = w; + dpp.BackBufferHeight = h; + invalidateDeviceObjects(); + HRESULT hr = pDevice->Reset(&dpp); + if( hr == D3D_OK ) + restoreDeviceObjects(); + else + systemMessage(0, "Failed device reset %08x", hr); + } +} + + +bool Direct3DDisplay::changeRenderSize(int w, int h) +{ + // w and h is the size of the filtered image (So this could be 3xGBASize) + if(w != width || h != height) + { + if(pTexture) + { + pTexture->Release(); + pTexture = NULL; + } + + if(!initializeOffscreen(w, h)) { + failed = true; + return false; + } + } + + if(filterDisabled && theApp.filterFunction) + theApp.filterFunction = NULL; + + return true; +} + +void Direct3DDisplay::setOption(const char *option, int value) +{ + if(!strcmp(option, "d3dFilter")) + updateFiltering(value); + + if(!strcmp(option, "d3dVSync")) + updateVSync(); +} + +int Direct3DDisplay::selectFullScreenMode(GUID **) +{ + //int newScreenWidth, newScreenHeight; + //int newScreenBitsPerPixel; + + //D3DDISPLAYMODE dm; + //pDevice->GetDisplayMode( 0, &dm ); + + //newScreenWidth = dm.Width; + //newScreenHeight = dm.Height; + + //switch (dm.Format) + //{ + //case D3DFMT_A2R10G10B10: + //case D3DFMT_A8R8G8B8: + //case D3DFMT_X8R8G8B8: + // newScreenBitsPerPixel = 32; + // break; + //case D3DFMT_A1R5G5B5: + //case D3DFMT_X1R5G5B5: + //case D3DFMT_R5G6B5: + // newScreenBitsPerPixel = 16; + // break; + //} + // + //return (newScreenBitsPerPixel << 24) | (newScreenWidth << 12) | newScreenHeight; + int w, h, b; + UniVideoModeDlg dlg(0, &w, &h, &b, &SelectedFreq, &SelectedAdapter); + + if (0 == dlg.DoModal()) + { + return (b<<24) + (w<<12) + h; + // Bits<<24 | Width<<12 | Height + } + else + { + return -1; + } +} + + +int Direct3DDisplay::selectFullScreenMode2() +{ + return (SelectedAdapter<<16) + SelectedFreq; +} + + +void Direct3DDisplay::updateVSync(void) +{ + if (pDevice) + { + if (theApp.vsync) + dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // VSync + else + dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // No Sync + + invalidateDeviceObjects(); + HRESULT hr = pDevice->Reset(&dpp); + if (hr == D3D_OK) + restoreDeviceObjects(); + else + systemMessage(0, "Failed to change VSync option %08x", hr); + } +} + +IDisplay *newDirect3DDisplay() +{ + return new Direct3DDisplay(); +} \ No newline at end of file diff --git a/src/win32/DirectDraw.cpp b/src/win32/DirectDraw.cpp new file mode 100644 index 00000000..df652491 --- /dev/null +++ b/src/win32/DirectDraw.cpp @@ -0,0 +1,960 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" + +#define DIRECTDRAW_VERSION 0x0700 +#include + +#include + +#include "../System.h" +#include "../gb/gbGlobals.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Text.h" +#include "../Util.h" + +#include "VBA.h" +#include "MainWnd.h" +#include "Reg.h" +#include "..\..\res\resource.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern int Init_2xSaI(u32); +extern void winlog(const char *,...); +extern int systemSpeed; +extern int winVideoModeSelect(CWnd *, GUID **); + +class DirectDrawDisplay : public IDisplay { +private: + HINSTANCE ddrawDLL; + LPDIRECTDRAW7 pDirectDraw; + LPDIRECTDRAWSURFACE7 ddsPrimary; + LPDIRECTDRAWSURFACE7 ddsOffscreen; + LPDIRECTDRAWSURFACE7 ddsFlip; + LPDIRECTDRAWCLIPPER ddsClipper; + int width; + int height; + bool failed; + + volatile unsigned wait_lastscanline; + volatile unsigned wait_screenheight; + volatile unsigned wait_maxheight; + volatile unsigned wait_firstline; + HANDLE wait_event; + UINT wait_timerres; + UINT wait_timerid; + + bool initializeOffscreen(int w, int h); + bool StartTimer(); + void StopTimer(); + static void CALLBACK g_timer_proc( UINT id, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2 ); +public: + DirectDrawDisplay(); + virtual ~DirectDrawDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual DISPLAY_TYPE getType() { return DIRECT_DRAW; }; + virtual void setOption(const char *, int) {} + virtual int selectFullScreenMode(GUID **); + + void timer_proc( UINT id, UINT msg, DWORD_PTR dw1, DWORD_PTR dw2 ); +}; + +static HRESULT WINAPI checkModesAvailable(LPDDSURFACEDESC2 surf, LPVOID lpContext) +{ + if(surf->dwWidth == 320 && + surf->dwHeight == 240 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode320Available = TRUE; + } + if(surf->dwWidth == 640 && + surf->dwHeight == 480 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode640Available = TRUE; + } + if(surf->dwWidth == 800 && + surf->dwHeight == 600 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode800Available = TRUE; + } + return DDENUMRET_OK; +} + +static int ffs(UINT mask) +{ + int m = 0; + if (mask) { + while (!(mask & (1 << m))) + m++; + + return (m); + } + + return (0); +} + +DirectDrawDisplay::DirectDrawDisplay() +{ + pDirectDraw = NULL; + ddsPrimary = NULL; + ddsOffscreen = NULL; + ddsFlip = NULL; + ddsClipper = NULL; + ddrawDLL = NULL; + width = 0; + height = 0; + failed = false; + wait_screenheight = 0; + wait_event = 0; + wait_timerres = 0; + wait_timerid = 0; +} + +DirectDrawDisplay::~DirectDrawDisplay() +{ + cleanup(); +} + +void DirectDrawDisplay::cleanup() +{ + if(pDirectDraw != NULL) { + if ( wait_timerid ) { + StopTimer(); + } + + if ( wait_event ) { + CloseHandle( wait_event ); + wait_event = 0; + } + + if(ddsClipper != NULL) { + ddsClipper->Release(); + ddsClipper = NULL; + } + + if(ddsFlip != NULL) { + ddsFlip->Release(); + ddsFlip = NULL; + } + + if(ddsOffscreen != NULL) { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + + if(ddsPrimary != NULL) { + ddsPrimary->Release(); + ddsPrimary = NULL; + } + + pDirectDraw->Release(); + pDirectDraw = NULL; + } + + if(ddrawDLL != NULL) { + FreeLibrary(ddrawDLL); + ddrawDLL = NULL; + } + width = 0; + height = 0; +} + +bool DirectDrawDisplay::initialize() +{ + theApp.sizeX = 240; + theApp.sizeY = 160; + + switch(theApp.videoOption) { + case VIDEO_1X: + theApp.surfaceSizeX = theApp.sizeX; + theApp.surfaceSizeY = theApp.sizeY; + break; + case VIDEO_2X: + theApp.surfaceSizeX = theApp.sizeX * 2; + theApp.surfaceSizeY = theApp.sizeY * 2; + break; + case VIDEO_3X: + theApp.surfaceSizeX = theApp.sizeX * 3; + theApp.surfaceSizeY = theApp.sizeY * 3; + break; + case VIDEO_4X: + theApp.surfaceSizeX = theApp.sizeX * 4; + theApp.surfaceSizeY = theApp.sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + { + float scaleX = ((float)theApp.fsWidth / (float)theApp.sizeX); + float scaleY = ((float)theApp.fsHeight / (float)theApp.sizeY); + float min = scaleX < scaleY ? scaleX : scaleY; + if(theApp.fsMaxScale) + min = min > theApp.fsMaxScale ? theApp.fsMaxScale : min; + theApp.surfaceSizeX = (int)(theApp.sizeX * min); + theApp.surfaceSizeY = (int)(theApp.sizeY * min); + if(theApp.fullScreenStretch) { + theApp.surfaceSizeX = theApp.fsWidth; + theApp.surfaceSizeY = theApp.fsHeight; + } + } + break; + } + + theApp.rect.left = 0; + theApp.rect.top = 0; + theApp.rect.right = theApp.sizeX; + theApp.rect.bottom = theApp.sizeY; + + theApp.dest.left = 0; + theApp.dest.top = 0; + theApp.dest.right = theApp.surfaceSizeX; + theApp.dest.bottom = theApp.surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + style |= WS_OVERLAPPEDWINDOW; + else + styleEx = WS_EX_TOPMOST; + + if(theApp.videoOption <= VIDEO_4X) + AdjustWindowRectEx(&theApp.dest, style, TRUE, styleEx); + else + AdjustWindowRectEx(&theApp.dest, style, FALSE, styleEx); + + int winSizeX = theApp.dest.right-theApp.dest.left; + int winSizeY = theApp.dest.bottom-theApp.dest.top; + + int x = 0; + int y = 0; + + if(theApp.videoOption <= VIDEO_4X) { + x = theApp.windowPositionX; + y = theApp.windowPositionY; + } + + // Create a window + MainWnd *pWnd = new MainWnd; + theApp.m_pMainWnd = pWnd; + + pWnd->CreateEx(styleEx, + theApp.wndClass, + "VisualBoyAdvance", + style, + x,y,winSizeX,winSizeY, + NULL, + 0); + + if (!(HWND)*pWnd) { + winlog("Error creating Window %08x\n", GetLastError()); + return FALSE; + } + + + theApp.updateMenuBar(); + + theApp.adjustDestRect(); + + GUID *guid = NULL; + if(theApp.ddrawEmulationOnly) + guid = (GUID *)DDCREATE_EMULATIONONLY; + + if(theApp.pVideoDriverGUID) + guid = theApp.pVideoDriverGUID; + + ddrawDLL = LoadLibrary("DDRAW.DLL"); + HRESULT (WINAPI *DDrawCreateEx)(GUID *,LPVOID *,REFIID,IUnknown *); + if(ddrawDLL != NULL) { + DDrawCreateEx = (HRESULT (WINAPI *)(GUID *,LPVOID *,REFIID,IUnknown *)) + GetProcAddress(ddrawDLL, "DirectDrawCreateEx"); + + if(DDrawCreateEx == NULL) { + theApp.directXMessage("DirectDrawCreateEx"); + return FALSE; + } + } else { + theApp.directXMessage("DDRAW.DLL"); + return FALSE; + } + + theApp.ddrawUsingEmulationOnly = theApp.ddrawEmulationOnly; + + HRESULT hret = DDrawCreateEx(guid, + (void **)&pDirectDraw, + IID_IDirectDraw7, + NULL); + + if(hret != DD_OK) { + winlog("Error creating DirectDraw object %08x\n", hret); + if(theApp.ddrawEmulationOnly) { + // disable emulation only setting in case of failure + regSetDwordValue("ddrawEmulationOnly", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWCREATE), hret); + return FALSE; + } + + if(theApp.ddrawDebug) { + DDCAPS driver; + DDCAPS hel; + ZeroMemory(&driver, sizeof(driver)); + ZeroMemory(&hel, sizeof(hel)); + driver.dwSize = sizeof(driver); + hel.dwSize = sizeof(hel); + pDirectDraw->GetCaps(&driver, &hel); + int i; + DWORD *p = (DWORD *)&driver; + for(i = 0; i < (int)driver.dwSize; i+=4) + winlog("Driver CAPS %2d: %08x\n", i>>2, *p++); + p = (DWORD *)&hel; + for(i = 0; i < (int)hel.dwSize; i+=4) + winlog("HEL CAPS %2d: %08x\n", i>>2, *p++); + } + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + // check for available fullscreen modes + pDirectDraw->EnumDisplayModes(DDEDM_STANDARDVGAMODES, NULL, NULL, + checkModesAvailable); + + DWORD flags = DDSCL_NORMAL; + + if(theApp.videoOption >= VIDEO_320x240) + flags = DDSCL_ALLOWMODEX | + DDSCL_ALLOWREBOOT | + DDSCL_EXCLUSIVE | + DDSCL_FULLSCREEN; + + hret = pDirectDraw->SetCooperativeLevel(pWnd->m_hWnd, + flags); + + if(hret != DD_OK) { + winlog("Error SetCooperativeLevel %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWLEVEL), hret); + return FALSE; + } + + if(theApp.videoOption > VIDEO_4X) { + hret = pDirectDraw->SetDisplayMode(theApp.fsWidth, + theApp.fsHeight, + theApp.fsColorDepth, + 60, + 0); + if(hret != DD_OK) { + winlog("Error SetDisplayMode %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSET), hret); + return FALSE; + } + } + + DDSURFACEDESC2 ddsd; + ZeroMemory(&ddsd,sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + if(theApp.videoOption > VIDEO_4X) { + if(theApp.tripleBuffering) { + // setup triple buffering + ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT; + ddsd.ddsCaps.dwCaps |= DDSCAPS_COMPLEX | DDSCAPS_FLIP; + ddsd.dwBackBufferCount = 2; + } + } + + hret = pDirectDraw->CreateSurface(&ddsd, &ddsPrimary, NULL); + if(hret != DD_OK) { + winlog("Error primary CreateSurface %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE), hret); + return FALSE; + } + + if(theApp.ddrawDebug) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsPrimary->GetCaps(&caps); + + winlog("Primary CAPS 1: %08x\n", caps.dwCaps); + winlog("Primary CAPS 2: %08x\n", caps.dwCaps2); + winlog("Primary CAPS 3: %08x\n", caps.dwCaps3); + winlog("Primary CAPS 4: %08x\n", caps.dwCaps4); + } + + if(theApp.videoOption > VIDEO_4X && theApp.tripleBuffering) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + // this gets the third surface. The front one is the primary, + // the second is the backbuffer and the third is the flip + // surface + caps.dwCaps = DDSCAPS_BACKBUFFER; + + hret = ddsPrimary->GetAttachedSurface(&caps, &ddsFlip); + if(hret != DD_OK) { + winlog("Failed to get attached surface %08x", hret); + return FALSE; + } + + ddsFlip->AddRef(); + clear(); + } + + // create clipper in all modes to avoid paint problems + // if(videoOption <= VIDEO_4X) { + hret = pDirectDraw->CreateClipper(0, &ddsClipper, NULL); + if(hret == DD_OK) { + ddsClipper->SetHWnd(0, pWnd->m_hWnd); + if(theApp.videoOption > VIDEO_4X) { + if(theApp.tripleBuffering) + ddsFlip->SetClipper(ddsClipper); + else + ddsPrimary->SetClipper(ddsClipper); + } else + ddsPrimary->SetClipper(ddsClipper); + } + // } + + wait_event = CreateEvent( NULL, FALSE, FALSE, NULL ); + + if ( ! StartTimer() ) { + return FALSE; + } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsPrimary->GetPixelFormat(&px); + + switch(px.dwRGBBitCount) { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage(IDS_ERROR_DISP_COLOR, "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.",px.dwRGBBitCount); + return FALSE; + } + theApp.updateFilter(); + theApp.updateIFB(); + + if(failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return true; +} + +bool DirectDrawDisplay::changeRenderSize(int w, int h) +{ + if(w != width || h != height) { + if(ddsOffscreen) { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + if(!initializeOffscreen(w, h)) { + failed = true; + return false; + } + } + return true; +} + +bool DirectDrawDisplay::initializeOffscreen(int w, int h) +{ + DDSURFACEDESC2 ddsd; + + ZeroMemory(&ddsd, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; + ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; + if(theApp.ddrawUseVideoMemory) + ddsd.ddsCaps.dwCaps |= (DDSCAPS_LOCALVIDMEM|DDSCAPS_VIDEOMEMORY); + ddsd.dwWidth = w; + ddsd.dwHeight = h; + + HRESULT hret = pDirectDraw->CreateSurface(&ddsd, &ddsOffscreen, NULL); + + if(hret != DD_OK) { + winlog("Error offscreen CreateSurface %08x\n", hret); + if(theApp.ddrawUseVideoMemory) { + regSetDwordValue("ddrawUseVideoMemory", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE2), hret); + return false; + } + + if(theApp.ddrawDebug) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsOffscreen->GetCaps(&caps); + + winlog("Offscreen CAPS 1: %08x\n", caps.dwCaps); + winlog("Offscreen CAPS 2: %08x\n", caps.dwCaps2); + winlog("Offscreen CAPS 3: %08x\n", caps.dwCaps3); + winlog("Offscreen CAPS 4: %08x\n", caps.dwCaps4); + } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsOffscreen->GetPixelFormat(&px); + + if(theApp.ddrawDebug) { + DWORD *pdword = (DWORD *)&px; + for(int ii = 0; ii < 8; ii++) { + winlog("Pixel format %d %08x\n", ii, pdword[ii]); + } + } + + switch(px.dwRGBBitCount) { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage(IDS_ERROR_DISP_COLOR, "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.",px.dwRGBBitCount); + return FALSE; + } + if(theApp.ddrawDebug) { + winlog("R Mask: %08x\n", px.dwRBitMask); + winlog("G Mask: %08x\n", px.dwGBitMask); + winlog("B Mask: %08x\n", px.dwBBitMask); + } + + systemRedShift = ffs(px.dwRBitMask); + systemGreenShift = ffs(px.dwGBitMask); + systemBlueShift = ffs(px.dwBBitMask); + +#ifdef MMX + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0xF800 && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0x001F) { + systemGreenShift++; + Init_2xSaI(565); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x7C00 && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x001F) { + Init_2xSaI(555); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0xF800) { + systemGreenShift++; + Init_2xSaI(565); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x7C00) { + Init_2xSaI(555); + } else { + // 32-bit or 24-bit + if(systemColorDepth == 32 || systemColorDepth == 24) { + systemRedShift += 3; + systemGreenShift += 3; + systemBlueShift += 3; + if(systemColorDepth == 32) + Init_2xSaI(32); + } + } + + if(theApp.ddrawDebug) { + winlog("R shift: %d\n", systemRedShift); + winlog("G shift: %d\n", systemGreenShift); + winlog("B shift: %d\n", systemBlueShift); + } + + utilUpdateSystemColorMaps(theApp.filterLCD); + width = w; + height = h; + return true; +} + +void DirectDrawDisplay::clear() +{ + if(theApp.videoOption <= VIDEO_4X || !theApp.tripleBuffering || ddsFlip == NULL) + return; + + DDBLTFX fx; + ZeroMemory(&fx, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFillColor = 0; + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); +} + +void DirectDrawDisplay::renderMenu() +{ + checkFullScreen(); + theApp.m_pMainWnd->DrawMenuBar(); +} + +void DirectDrawDisplay::checkFullScreen() +{ + if(theApp.tripleBuffering) + pDirectDraw->FlipToGDISurface(); +} + +void DirectDrawDisplay::render() +{ +// hack - some systems seem to render audio too +// fast. This means the audio buffer will slowly drain +// if vsync is on. If we drop one out of 800 frames +// it will keep us "ahead" of the sound. Better +// way to handle this is to skip frames when the sound +// buffer is low. + static int ctr = 0; + ctr++; + if (ctr == 800) + { + ctr = 0; + return; + } +// end hack + HRESULT hret; + + if(pDirectDraw == NULL || + ddsOffscreen == NULL || + ddsPrimary == NULL) + return; + + DDSURFACEDESC2 ddsDesc; + + ZeroMemory(&ddsDesc, sizeof(ddsDesc)); + + ddsDesc.dwSize = sizeof(ddsDesc); + + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, + DDLOCK_WRITEONLY| +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK| +#endif + DDLOCK_SURFACEMEMORYPTR, + NULL); + + if(hret == DDERR_SURFACELOST) { + hret = ddsPrimary->Restore(); + if(hret == DD_OK) { + hret = ddsOffscreen->Restore(); + + if(hret == DD_OK) { + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, + DDLOCK_WRITEONLY| +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK| +#endif + DDLOCK_SURFACEMEMORYPTR, + NULL); + + } + } + } + + if(hret == DD_OK) { + if(theApp.filterFunction) { + if(systemColorDepth == 16) + (*theApp.filterFunction)(pix+theApp.filterWidth*2+4, + theApp.filterWidth*2+4, + (u8*)theApp.delta, + (u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + else + (*theApp.filterFunction)(pix+theApp.filterWidth*4+4, + theApp.filterWidth*4+4, + (u8*)theApp.delta, + (u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + + } else { + int copyX = 240; + int copyY = 160; + + if(theApp.cartridgeType == 1) { + if(gbBorderOn) { + copyX = 256; + copyY = 224; + } else { + copyX = 160; + copyY = 144; + } + } + // MMX doesn't seem to be faster to copy the data + __asm { + mov eax, copyX; + mov ebx, copyY; + + mov esi, pix; + mov edi, ddsDesc.lpSurface; + mov edx, ddsDesc.lPitch; + cmp systemColorDepth, 16; + jnz gbaOtherColor; + sub edx, eax; + sub edx, eax; + lea esi,[esi+2*eax+4]; + shr eax, 1; + gbaLoop16bit: + mov ecx, eax; + repz movsd; + inc esi; + inc esi; + inc esi; + inc esi; + add edi, edx; + dec ebx; + jnz gbaLoop16bit; + jmp gbaLoopEnd; + gbaOtherColor: + cmp systemColorDepth, 32; + jnz gbaOtherColor2; + + sub edx, eax; + sub edx, eax; + sub edx, eax; + sub edx, eax; + lea esi, [esi+4*eax+4]; + gbaLoop32bit: + mov ecx, eax; + repz movsd; + add esi, 4; + add edi, edx; + dec ebx; + jnz gbaLoop32bit; + jmp gbaLoopEnd; + gbaOtherColor2: + lea eax, [eax+2*eax]; + sub edx, eax; + gbaLoop24bit: + mov ecx, eax; + shr ecx, 2; + repz movsd; + add edi, edx; + dec ebx; + jnz gbaLoop24bit; + gbaLoopEnd: + } + } + if(theApp.showSpeed && theApp.videoOption > VIDEO_4X) { + char buffer[30]; + if(theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + if(theApp.showSpeedTransparent) + drawTextTransp((u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + } else if(theApp.ddrawDebug) + winlog("Error during lock: %08x\n", hret); + + hret = ddsOffscreen->Unlock(NULL); + + if(hret == DD_OK) { + if(theApp.vsync && !(theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) && !speedup) { // isn't the Flip() call synced unless a certain flag is passed to it? + //hret = pDirectDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); + WaitForSingleObject( wait_event, 100 ); + } + ddsOffscreen->PageLock(0); + if(theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) { + hret = ddsFlip->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_WAIT, NULL); + if(hret == DD_OK) { + if(theApp.menuToggle || !theApp.active) { + pDirectDraw->FlipToGDISurface(); + ddsPrimary->SetClipper(ddsClipper); + hret = ddsPrimary->Blt(&theApp.dest, ddsFlip, NULL, DDBLT_ASYNC, NULL); + // if using emulation only, then we have to redraw the menu + // everytime. It seems like a bug in DirectDraw to me as we not + // overwritting the menu area at all. + if(theApp.ddrawUsingEmulationOnly) + theApp.m_pMainWnd->DrawMenuBar(); + } else + hret = ddsPrimary->Flip(NULL, 0); + } + } else { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL,DDBLT_ASYNC,NULL); + + if(hret == DDERR_SURFACELOST) { + hret = ddsPrimary->Restore(); + + if(hret == DD_OK) { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_ASYNC, NULL); + } + } + } + ddsOffscreen->PageUnlock(0); + } else if(theApp.ddrawDebug) + winlog("Error during unlock: %08x\n", hret); + + if(theApp.screenMessage) { + if(((GetTickCount() - theApp.screenMessageTime) < 3000) && + !theApp.disableStatusMessage) { + ddsPrimary->SetClipper(ddsClipper); + HDC hdc; + ddsPrimary->GetDC(&hdc); + SetTextColor(hdc, RGB(255,0,0)); + SetBkMode(hdc,TRANSPARENT); + TextOut(hdc, theApp.dest.left+10, theApp.dest.bottom - 20, theApp.screenMessageBuffer, + strlen(theApp.screenMessageBuffer)); + ddsPrimary->ReleaseDC(hdc); + } else { + theApp.screenMessage = false; + } + } + + if(hret != DD_OK) { + if(theApp.ddrawDebug) + winlog("Error on update screen: %08x\n", hret); + } +} + +int DirectDrawDisplay::selectFullScreenMode(GUID **pGUID) +{ + return winVideoModeSelect(theApp.m_pMainWnd, pGUID); +} + +bool DirectDrawDisplay::StartTimer() +{ + MMRESULT result; + + TIMECAPS tc; + + if ( TIMERR_NOERROR == timeGetDevCaps( & tc, sizeof( TIMECAPS ) ) ) { + wait_timerres = min( max( tc.wPeriodMin, 1 ), tc.wPeriodMax ); + timeBeginPeriod( wait_timerres ); + } else { + return false; + } + + result = timeSetEvent( wait_timerres, wait_timerres, & DirectDrawDisplay::g_timer_proc, ( DWORD_PTR ) this, TIME_PERIODIC ); + + if (NULL != result) { + wait_timerid = (UINT)result; + return true; + } + + return false; +} + +void DirectDrawDisplay::StopTimer() +{ + MMRESULT result; + + result = timeKillEvent( wait_timerid ); + if ( TIMERR_NOERROR == result ) { + wait_timerid = 0; + } + + if ( 0 != wait_timerres ) { + timeEndPeriod( wait_timerres ); + wait_timerres = 0; + } +} + +void CALLBACK DirectDrawDisplay::g_timer_proc( UINT id, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2 ) +{ + DirectDrawDisplay * p_this = reinterpret_cast< DirectDrawDisplay * >( dwUser ); + if ( p_this ) { + p_this->timer_proc( id, msg, dw1, dw2 ); + } +} + +void DirectDrawDisplay::timer_proc( UINT id, UINT msg, DWORD_PTR dw1, DWORD_PTR dw2 ) +{ + DWORD scanline; + if ( pDirectDraw->GetScanLine( & scanline ) == DD_OK ) { + unsigned height = GetSystemMetrics( SM_CYSCREEN ); + if ( wait_screenheight != height ) { + wait_screenheight = height; + wait_maxheight = height; + } + if ( scanline >= wait_maxheight ) wait_maxheight = scanline + 1; + + scanline = ( scanline + wait_maxheight - min( theApp.dest.bottom, wait_screenheight ) ) % wait_maxheight; + + if ( scanline < wait_lastscanline ) { + PulseEvent( wait_event ); + } + + wait_lastscanline = scanline; + } +} + +IDisplay *newDirectDrawDisplay() +{ + return new DirectDrawDisplay(); +} diff --git a/src/win32/DirectDraw.cpp.orig b/src/win32/DirectDraw.cpp.orig new file mode 100644 index 00000000..177dedf4 --- /dev/null +++ b/src/win32/DirectDraw.cpp.orig @@ -0,0 +1,845 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" + +#define DIRECTDRAW_VERSION 0x0700 +#include + +#include "../System.h" +#include "../gb/gbGlobals.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Text.h" +#include "../Util.h" + +#include "VBA.h" +#include "MainWnd.h" +#include "Reg.h" +#include "..\..\res\resource.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern int Init_2xSaI(u32); +extern void winlog(const char *,...); +extern int systemSpeed; +extern int winVideoModeSelect(CWnd *, GUID **); + +class DirectDrawDisplay : public IDisplay { +private: + HINSTANCE ddrawDLL; + LPDIRECTDRAW7 pDirectDraw; + LPDIRECTDRAWSURFACE7 ddsPrimary; + LPDIRECTDRAWSURFACE7 ddsOffscreen; + LPDIRECTDRAWSURFACE7 ddsFlip; + LPDIRECTDRAWCLIPPER ddsClipper; + int width; + int height; + bool failed; + + bool initializeOffscreen(int w, int h); +public: + DirectDrawDisplay(); + virtual ~DirectDrawDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual DISPLAY_TYPE getType() { return DIRECT_DRAW; }; + virtual void setOption(const char *, int) {} + virtual int selectFullScreenMode(GUID **); +}; + +static HRESULT WINAPI checkModesAvailable(LPDDSURFACEDESC2 surf, LPVOID lpContext) +{ + if(surf->dwWidth == 320 && + surf->dwHeight == 240 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode320Available = TRUE; + } + if(surf->dwWidth == 640 && + surf->dwHeight == 480 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode640Available = TRUE; + } + if(surf->dwWidth == 800 && + surf->dwHeight == 600 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) { + theApp.mode800Available = TRUE; + } + return DDENUMRET_OK; +} + +static int ffs(UINT mask) +{ + int m = 0; + if (mask) { + while (!(mask & (1 << m))) + m++; + + return (m); + } + + return (0); +} + +DirectDrawDisplay::DirectDrawDisplay() +{ + pDirectDraw = NULL; + ddsPrimary = NULL; + ddsOffscreen = NULL; + ddsFlip = NULL; + ddsClipper = NULL; + ddrawDLL = NULL; + width = 0; + height = 0; + failed = false; +} + +DirectDrawDisplay::~DirectDrawDisplay() +{ + cleanup(); +} + +void DirectDrawDisplay::cleanup() +{ + if(pDirectDraw != NULL) { + if(ddsClipper != NULL) { + ddsClipper->Release(); + ddsClipper = NULL; + } + + if(ddsFlip != NULL) { + ddsFlip->Release(); + ddsFlip = NULL; + } + + if(ddsOffscreen != NULL) { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + + if(ddsPrimary != NULL) { + ddsPrimary->Release(); + ddsPrimary = NULL; + } + + pDirectDraw->Release(); + pDirectDraw = NULL; + } + + if(ddrawDLL != NULL) { + FreeLibrary(ddrawDLL); + ddrawDLL = NULL; + } + width = 0; + height = 0; +} + +bool DirectDrawDisplay::initialize() +{ + theApp.sizeX = 240; + theApp.sizeY = 160; + + switch(theApp.videoOption) { + case VIDEO_1X: + theApp.surfaceSizeX = theApp.sizeX; + theApp.surfaceSizeY = theApp.sizeY; + break; + case VIDEO_2X: + theApp.surfaceSizeX = theApp.sizeX * 2; + theApp.surfaceSizeY = theApp.sizeY * 2; + break; + case VIDEO_3X: + theApp.surfaceSizeX = theApp.sizeX * 3; + theApp.surfaceSizeY = theApp.sizeY * 3; + break; + case VIDEO_4X: + theApp.surfaceSizeX = theApp.sizeX * 4; + theApp.surfaceSizeY = theApp.sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + { + float scaleX = ((float)theApp.fsWidth / (float)theApp.sizeX); + float scaleY = ((float)theApp.fsHeight / (float)theApp.sizeY); + float min = scaleX < scaleY ? scaleX : scaleY; + if(theApp.fsMaxScale) + min = min > theApp.fsMaxScale ? theApp.fsMaxScale : min; + theApp.surfaceSizeX = (int)(theApp.sizeX * min); + theApp.surfaceSizeY = (int)(theApp.sizeY * min); + if(theApp.fullScreenStretch) { + theApp.surfaceSizeX = theApp.fsWidth; + theApp.surfaceSizeY = theApp.fsHeight; + } + } + break; + } + + theApp.rect.left = 0; + theApp.rect.top = 0; + theApp.rect.right = theApp.sizeX; + theApp.rect.bottom = theApp.sizeY; + + theApp.dest.left = 0; + theApp.dest.top = 0; + theApp.dest.right = theApp.surfaceSizeX; + theApp.dest.bottom = theApp.surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + style |= WS_OVERLAPPEDWINDOW; + else + styleEx = WS_EX_TOPMOST; + + if(theApp.videoOption <= VIDEO_4X) + AdjustWindowRectEx(&theApp.dest, style, TRUE, styleEx); + else + AdjustWindowRectEx(&theApp.dest, style, FALSE, styleEx); + + int winSizeX = theApp.dest.right-theApp.dest.left; + int winSizeY = theApp.dest.bottom-theApp.dest.top; + + int x = 0; + int y = 0; + + if(theApp.videoOption <= VIDEO_4X) { + x = theApp.windowPositionX; + y = theApp.windowPositionY; + } + + // Create a window + MainWnd *pWnd = new MainWnd; + theApp.m_pMainWnd = pWnd; + + pWnd->CreateEx(styleEx, + theApp.wndClass, + "VisualBoyAdvance", + style, + x,y,winSizeX,winSizeY, + NULL, + 0); + + if (!(HWND)*pWnd) { + winlog("Error creating Window %08x\n", GetLastError()); + return FALSE; + } + + + theApp.updateMenuBar(); + + theApp.adjustDestRect(); + + GUID *guid = NULL; + if(theApp.ddrawEmulationOnly) + guid = (GUID *)DDCREATE_EMULATIONONLY; + + if(theApp.pVideoDriverGUID) + guid = theApp.pVideoDriverGUID; + + ddrawDLL = LoadLibrary("DDRAW.DLL"); + HRESULT (WINAPI *DDrawCreateEx)(GUID *,LPVOID *,REFIID,IUnknown *); + if(ddrawDLL != NULL) { + DDrawCreateEx = (HRESULT (WINAPI *)(GUID *,LPVOID *,REFIID,IUnknown *)) + GetProcAddress(ddrawDLL, "DirectDrawCreateEx"); + + if(DDrawCreateEx == NULL) { + theApp.directXMessage("DirectDrawCreateEx"); + return FALSE; + } + } else { + theApp.directXMessage("DDRAW.DLL"); + return FALSE; + } + + theApp.ddrawUsingEmulationOnly = theApp.ddrawEmulationOnly; + + HRESULT hret = DDrawCreateEx(guid, + (void **)&pDirectDraw, + IID_IDirectDraw7, + NULL); + + if(hret != DD_OK) { + winlog("Error creating DirectDraw object %08x\n", hret); + if(theApp.ddrawEmulationOnly) { + // disable emulation only setting in case of failure + regSetDwordValue("ddrawEmulationOnly", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWCREATE), hret); + return FALSE; + } + + if(theApp.ddrawDebug) { + DDCAPS driver; + DDCAPS hel; + ZeroMemory(&driver, sizeof(driver)); + ZeroMemory(&hel, sizeof(hel)); + driver.dwSize = sizeof(driver); + hel.dwSize = sizeof(hel); + pDirectDraw->GetCaps(&driver, &hel); + int i; + DWORD *p = (DWORD *)&driver; + for(i = 0; i < (int)driver.dwSize; i+=4) + winlog("Driver CAPS %2d: %08x\n", i>>2, *p++); + p = (DWORD *)&hel; + for(i = 0; i < (int)hel.dwSize; i+=4) + winlog("HEL CAPS %2d: %08x\n", i>>2, *p++); + } + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + // check for available fullscreen modes + pDirectDraw->EnumDisplayModes(DDEDM_STANDARDVGAMODES, NULL, NULL, + checkModesAvailable); + + DWORD flags = DDSCL_NORMAL; + + if(theApp.videoOption >= VIDEO_320x240) + flags = DDSCL_ALLOWMODEX | + DDSCL_ALLOWREBOOT | + DDSCL_EXCLUSIVE | + DDSCL_FULLSCREEN; + + hret = pDirectDraw->SetCooperativeLevel(pWnd->m_hWnd, + flags); + + if(hret != DD_OK) { + winlog("Error SetCooperativeLevel %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWLEVEL), hret); + return FALSE; + } + + if(theApp.videoOption > VIDEO_4X) { + hret = pDirectDraw->SetDisplayMode(theApp.fsWidth, + theApp.fsHeight, + theApp.fsColorDepth, + 60, + 0); + if(hret != DD_OK) { + winlog("Error SetDisplayMode %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSET), hret); + return FALSE; + } + } + + DDSURFACEDESC2 ddsd; + ZeroMemory(&ddsd,sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + if(theApp.videoOption > VIDEO_4X) { + if(theApp.tripleBuffering) { + // setup triple buffering + ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT; + ddsd.ddsCaps.dwCaps |= DDSCAPS_COMPLEX | DDSCAPS_FLIP; + ddsd.dwBackBufferCount = 2; + } + } + + hret = pDirectDraw->CreateSurface(&ddsd, &ddsPrimary, NULL); + if(hret != DD_OK) { + winlog("Error primary CreateSurface %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE), hret); + return FALSE; + } + + if(theApp.ddrawDebug) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsPrimary->GetCaps(&caps); + + winlog("Primary CAPS 1: %08x\n", caps.dwCaps); + winlog("Primary CAPS 2: %08x\n", caps.dwCaps2); + winlog("Primary CAPS 3: %08x\n", caps.dwCaps3); + winlog("Primary CAPS 4: %08x\n", caps.dwCaps4); + } + + if(theApp.videoOption > VIDEO_4X && theApp.tripleBuffering) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + // this gets the third surface. The front one is the primary, + // the second is the backbuffer and the third is the flip + // surface + caps.dwCaps = DDSCAPS_BACKBUFFER; + + hret = ddsPrimary->GetAttachedSurface(&caps, &ddsFlip); + if(hret != DD_OK) { + winlog("Failed to get attached surface %08x", hret); + return FALSE; + } + + ddsFlip->AddRef(); + clear(); + } + + // create clipper in all modes to avoid paint problems + // if(videoOption <= VIDEO_4X) { + hret = pDirectDraw->CreateClipper(0, &ddsClipper, NULL); + if(hret == DD_OK) { + ddsClipper->SetHWnd(0, pWnd->m_hWnd); + if(theApp.videoOption > VIDEO_4X) { + if(theApp.tripleBuffering) + ddsFlip->SetClipper(ddsClipper); + else + ddsPrimary->SetClipper(ddsClipper); + } else + ddsPrimary->SetClipper(ddsClipper); + } + // } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsPrimary->GetPixelFormat(&px); + + switch(px.dwRGBBitCount) { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage(IDS_ERROR_DISP_COLOR, "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.",px.dwRGBBitCount); + return FALSE; + } + theApp.updateFilter(); + theApp.updateIFB(); + + if(failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return true; +} + +bool DirectDrawDisplay::changeRenderSize(int w, int h) +{ + if(w != width || h != height) { + if(ddsOffscreen) { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + if(!initializeOffscreen(w, h)) { + failed = true; + return false; + } + } + return true; +} + +bool DirectDrawDisplay::initializeOffscreen(int w, int h) +{ + DDSURFACEDESC2 ddsd; + + ZeroMemory(&ddsd, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; + ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; + if(theApp.ddrawUseVideoMemory) + ddsd.ddsCaps.dwCaps |= (DDSCAPS_LOCALVIDMEM|DDSCAPS_VIDEOMEMORY); + ddsd.dwWidth = w; + ddsd.dwHeight = h; + + HRESULT hret = pDirectDraw->CreateSurface(&ddsd, &ddsOffscreen, NULL); + + if(hret != DD_OK) { + winlog("Error offscreen CreateSurface %08x\n", hret); + if(theApp.ddrawUseVideoMemory) { + regSetDwordValue("ddrawUseVideoMemory", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE2), hret); + return false; + } + + if(theApp.ddrawDebug) { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsOffscreen->GetCaps(&caps); + + winlog("Offscreen CAPS 1: %08x\n", caps.dwCaps); + winlog("Offscreen CAPS 2: %08x\n", caps.dwCaps2); + winlog("Offscreen CAPS 3: %08x\n", caps.dwCaps3); + winlog("Offscreen CAPS 4: %08x\n", caps.dwCaps4); + } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsOffscreen->GetPixelFormat(&px); + + if(theApp.ddrawDebug) { + DWORD *pdword = (DWORD *)&px; + for(int ii = 0; ii < 8; ii++) { + winlog("Pixel format %d %08x\n", ii, pdword[ii]); + } + } + + switch(px.dwRGBBitCount) { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage(IDS_ERROR_DISP_COLOR, "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.",px.dwRGBBitCount); + return FALSE; + } + if(theApp.ddrawDebug) { + winlog("R Mask: %08x\n", px.dwRBitMask); + winlog("G Mask: %08x\n", px.dwGBitMask); + winlog("B Mask: %08x\n", px.dwBBitMask); + } + + systemRedShift = ffs(px.dwRBitMask); + systemGreenShift = ffs(px.dwGBitMask); + systemBlueShift = ffs(px.dwBBitMask); + +#ifdef MMX + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0xF800 && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0x001F) { + systemGreenShift++; + Init_2xSaI(565); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x7C00 && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x001F) { + Init_2xSaI(555); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0xF800) { + systemGreenShift++; + Init_2xSaI(565); + } else if((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x7C00) { + Init_2xSaI(555); + } else { + // 32-bit or 24-bit + if(systemColorDepth == 32 || systemColorDepth == 24) { + systemRedShift += 3; + systemGreenShift += 3; + systemBlueShift += 3; + if(systemColorDepth == 32) + Init_2xSaI(32); + } + } + + if(theApp.ddrawDebug) { + winlog("R shift: %d\n", systemRedShift); + winlog("G shift: %d\n", systemGreenShift); + winlog("B shift: %d\n", systemBlueShift); + } + + utilUpdateSystemColorMaps(); + width = w; + height = h; + return true; +} + +void DirectDrawDisplay::clear() +{ + if(theApp.videoOption <= VIDEO_4X || !theApp.tripleBuffering || ddsFlip == NULL) + return; + + DDBLTFX fx; + ZeroMemory(&fx, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFillColor = 0; + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); +} + +void DirectDrawDisplay::renderMenu() +{ + checkFullScreen(); + theApp.m_pMainWnd->DrawMenuBar(); +} + +void DirectDrawDisplay::checkFullScreen() +{ + if(theApp.tripleBuffering) + pDirectDraw->FlipToGDISurface(); +} + +void DirectDrawDisplay::render() +{ + HRESULT hret; + + if(pDirectDraw == NULL || + ddsOffscreen == NULL || + ddsPrimary == NULL) + return; + + DDSURFACEDESC2 ddsDesc; + + ZeroMemory(&ddsDesc, sizeof(ddsDesc)); + + ddsDesc.dwSize = sizeof(ddsDesc); + + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, + DDLOCK_WRITEONLY| +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK| +#endif + DDLOCK_SURFACEMEMORYPTR, + NULL); + + if(hret == DDERR_SURFACELOST) { + hret = ddsPrimary->Restore(); + if(hret == DD_OK) { + hret = ddsOffscreen->Restore(); + + if(hret == DD_OK) { + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, + DDLOCK_WRITEONLY| +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK| +#endif + DDLOCK_SURFACEMEMORYPTR, + NULL); + + } + } + } + + if(hret == DD_OK) { + if(theApp.filterFunction) { + if(systemColorDepth == 16) + (*theApp.filterFunction)(pix+theApp.filterWidth*2+4, + theApp.filterWidth*2+4, + (u8*)theApp.delta, + (u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + else + (*theApp.filterFunction)(pix+theApp.filterWidth*4+4, + theApp.filterWidth*4+4, + (u8*)theApp.delta, + (u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + + } else { + int copyX = 240; + int copyY = 160; + + if(theApp.cartridgeType == 1) { + if(gbBorderOn) { + copyX = 256; + copyY = 224; + } else { + copyX = 160; + copyY = 144; + } + } + // MMX doesn't seem to be faster to copy the data + __asm { + mov eax, copyX; + mov ebx, copyY; + + mov esi, pix; + mov edi, ddsDesc.lpSurface; + mov edx, ddsDesc.lPitch; + cmp systemColorDepth, 16; + jnz gbaOtherColor; + sub edx, eax; + sub edx, eax; + lea esi,[esi+2*eax+4]; + shr eax, 1; + gbaLoop16bit: + mov ecx, eax; + repz movsd; + inc esi; + inc esi; + inc esi; + inc esi; + add edi, edx; + dec ebx; + jnz gbaLoop16bit; + jmp gbaLoopEnd; + gbaOtherColor: + cmp systemColorDepth, 32; + jnz gbaOtherColor2; + + sub edx, eax; + sub edx, eax; + sub edx, eax; + sub edx, eax; + lea esi, [esi+4*eax+4]; + gbaLoop32bit: + mov ecx, eax; + repz movsd; + add esi, 4; + add edi, edx; + dec ebx; + jnz gbaLoop32bit; + jmp gbaLoopEnd; + gbaOtherColor2: + lea eax, [eax+2*eax]; + sub edx, eax; + gbaLoop24bit: + mov ecx, eax; + shr ecx, 2; + repz movsd; + add edi, edx; + dec ebx; + jnz gbaLoop24bit; + gbaLoopEnd: + } + } + if(theApp.showSpeed && theApp.videoOption > VIDEO_4X) { + char buffer[30]; + if(theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + if(theApp.showSpeedTransparent) + drawTextTransp((u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8*)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + } else if(theApp.ddrawDebug) + winlog("Error during lock: %08x\n", hret); + + hret = ddsOffscreen->Unlock(NULL); + + if(hret == DD_OK) { + if(theApp.vsync && !(theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) && !speedup) { // isn't the Flip() call synced unless a certain flag is passed to it? + hret = pDirectDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); + } + ddsOffscreen->PageLock(0); + if(theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) { + hret = ddsFlip->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_WAIT, NULL); + if(hret == DD_OK) { + if(theApp.menuToggle || !theApp.active) { + pDirectDraw->FlipToGDISurface(); + ddsPrimary->SetClipper(ddsClipper); + hret = ddsPrimary->Blt(&theApp.dest, ddsFlip, NULL, DDBLT_ASYNC, NULL); + // if using emulation only, then we have to redraw the menu + // everytime. It seems like a bug in DirectDraw to me as we not + // overwritting the menu area at all. + if(theApp.ddrawUsingEmulationOnly) + theApp.m_pMainWnd->DrawMenuBar(); + } else + hret = ddsPrimary->Flip(NULL, 0); + } + } else { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL,DDBLT_ASYNC,NULL); + + if(hret == DDERR_SURFACELOST) { + hret = ddsPrimary->Restore(); + + if(hret == DD_OK) { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_ASYNC, NULL); + } + } + } + ddsOffscreen->PageUnlock(0); + } else if(theApp.ddrawDebug) + winlog("Error during unlock: %08x\n", hret); + + if(theApp.screenMessage) { + if(((GetTickCount() - theApp.screenMessageTime) < 3000) && + !theApp.disableStatusMessage) { + ddsPrimary->SetClipper(ddsClipper); + HDC hdc; + ddsPrimary->GetDC(&hdc); + SetTextColor(hdc, RGB(255,0,0)); + SetBkMode(hdc,TRANSPARENT); + TextOut(hdc, theApp.dest.left+10, theApp.dest.bottom - 20, theApp.screenMessageBuffer, + strlen(theApp.screenMessageBuffer)); + ddsPrimary->ReleaseDC(hdc); + } else { + theApp.screenMessage = false; + } + } + + if(hret != DD_OK) { + if(theApp.ddrawDebug) + winlog("Error on update screen: %08x\n", hret); + } +} + +int DirectDrawDisplay::selectFullScreenMode(GUID **pGUID) +{ + return winVideoModeSelect(theApp.m_pMainWnd, pGUID); +} + +IDisplay *newDirectDrawDisplay() +{ + return new DirectDrawDisplay(); +} + diff --git a/src/win32/DirectInput.cpp b/src/win32/DirectInput.cpp new file mode 100644 index 00000000..374d57e0 --- /dev/null +++ b/src/win32/DirectInput.cpp @@ -0,0 +1,1060 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "Reg.h" +#include "WinResUtil.h" + +#define DIRECTINPUT_VERSION 0x0500 +#include + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern void directXMessage(const char *); +extern void winlog(const char *msg,...); + +#define POV_UP 1 +#define POV_DOWN 2 +#define POV_RIGHT 4 +#define POV_LEFT 8 + +class DirectInput : public Input { +private: + HINSTANCE dinputDLL; + +public: + virtual void checkDevices(); + DirectInput(); + virtual ~DirectInput(); + + virtual bool initialize(); + virtual bool readDevices(); + virtual u32 readDevice(int which); + virtual CString getKeyName(int key); + virtual void checkKeys(); + virtual void checkMotionKeys(); + virtual void activate(); + virtual void loadSettings(); + virtual void saveSettings(); +}; + +struct deviceInfo { + LPDIRECTINPUTDEVICE device; + BOOL isPolled; + int nButtons; + int nAxes; + int nPovs; + BOOL first; + struct { + DWORD offset; + LONG center; + LONG negative; + LONG positive; + } axis[8]; + int needed; + union { + UCHAR data[256]; + DIJOYSTATE state; + }; +}; + +static deviceInfo *currentDevice = NULL; +static int numDevices = 1; +static deviceInfo *pDevices = NULL; +static LPDIRECTINPUT pDirectInput = NULL; +static int joyDebug = 0; +static int axisNumber = 0; + +USHORT joypad[4][13] = { + { + DIK_LEFT, DIK_RIGHT, + DIK_UP, DIK_DOWN, + DIK_Z, DIK_X, + DIK_RETURN,DIK_BACK, + DIK_A, DIK_S, + DIK_SPACE, DIK_F12, + DIK_C + }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +USHORT motion[4] = { + DIK_NUMPAD4, DIK_NUMPAD6, DIK_NUMPAD8, DIK_NUMPAD2 +}; + +static int winReadKey(char *name, int num) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + return regQueryDwordValue(buffer, (DWORD)-1); +} + +void winReadKeys() +{ + int key = -1; + + for(int i = 0; i < 4; i++) { + key = winReadKey("Left", i); + if(key != -1) + joypad[i][KEY_LEFT] = key; + key = winReadKey("Right", i); + if(key != -1) + joypad[i][KEY_RIGHT] = key; + key = winReadKey("Up", i); + if(key != -1) + joypad[i][KEY_UP] = key; + key = winReadKey("Down", i); + if(key != -1) + joypad[i][KEY_DOWN] = key; + key = winReadKey("A", i); + if(key != -1) + joypad[i][KEY_BUTTON_A] = key; + key = winReadKey("B", i); + if(key != -1) + joypad[i][KEY_BUTTON_B] = key; + key = winReadKey("L", i); + if(key != -1) + joypad[i][KEY_BUTTON_L] = key; + key = winReadKey("R", i); + if(key != -1) + joypad[i][KEY_BUTTON_R] = key; + key = winReadKey("Start", i); + if(key != -1) + joypad[i][KEY_BUTTON_START] = key; + key = winReadKey("Select", i); + if(key != -1) + joypad[i][KEY_BUTTON_SELECT] = key; + key = winReadKey("Speed", i); + if(key != -1) + joypad[i][KEY_BUTTON_SPEED] = key; + key = winReadKey("Capture", i); + if(key != -1) + joypad[i][KEY_BUTTON_CAPTURE] = key; + key = winReadKey("GS", i); + if(key != -1) + joypad[i][KEY_BUTTON_GS] = key; + } + key = regQueryDwordValue("Motion_Left", (DWORD)-1); + if(key != -1) + motion[KEY_LEFT] = key; + key = regQueryDwordValue("Motion_Right", (DWORD)-1); + if(key != -1) + motion[KEY_RIGHT] = key; + key = regQueryDwordValue("Motion_Up", (DWORD)-1); + if(key != -1) + motion[KEY_UP] = key; + key = regQueryDwordValue("Motion_Down", (DWORD)-1); + if(key != -1) + motion[KEY_DOWN] = key; +} + +static void winSaveKey(char *name, int num, USHORT value) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + regSetDwordValue(buffer, value); +} + +void winSaveKeys() +{ + for(int i = 0; i < 4; i++) { + winSaveKey("Left", i, joypad[i][KEY_LEFT]); + winSaveKey("Right", i, joypad[i][KEY_RIGHT]); + winSaveKey("Up", i, joypad[i][KEY_UP]); + winSaveKey("Speed", i, joypad[i][KEY_BUTTON_SPEED]); + winSaveKey("Capture", i, joypad[i][KEY_BUTTON_CAPTURE]); + winSaveKey("GS", i, joypad[i][KEY_BUTTON_GS]); + winSaveKey("Down", i, joypad[i][KEY_DOWN]); + winSaveKey("A", i, joypad[i][KEY_BUTTON_A]); + winSaveKey("B", i, joypad[i][KEY_BUTTON_B]); + winSaveKey("L", i, joypad[i][KEY_BUTTON_L]); + winSaveKey("R", i, joypad[i][KEY_BUTTON_R]); + winSaveKey("Start", i, joypad[i][KEY_BUTTON_START]); + winSaveKey("Select", i, joypad[i][KEY_BUTTON_SELECT]); + } + regSetDwordValue("joyVersion", 1); + + regSetDwordValue("Motion_Left", + motion[KEY_LEFT]); + regSetDwordValue("Motion_Right", + motion[KEY_RIGHT]); + regSetDwordValue("Motion_Up", + motion[KEY_UP]); + regSetDwordValue("Motion_Down", + motion[KEY_DOWN]); +} + +static BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, + VOID* pContext ) +{ + DIPROPRANGE diprg; + diprg.diph.dwSize = sizeof(DIPROPRANGE); + diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); + diprg.diph.dwHow = DIPH_BYOFFSET; + diprg.diph.dwObj = pdidoi->dwOfs; // Specify the enumerated axis + + diprg.lMin = -32768; + diprg.lMax = 32767; + // try to set the range + if(FAILED(currentDevice->device->SetProperty(DIPROP_RANGE, &diprg.diph))) { + // Get the range for the axis + if( FAILED(currentDevice->device-> + GetProperty( DIPROP_RANGE, &diprg.diph ) ) ) { + return DIENUM_STOP; + } + } + + DIPROPDWORD didz; + + didz.diph.dwSize = sizeof(didz); + didz.diph.dwHeaderSize = sizeof(DIPROPHEADER); + didz.diph.dwHow = DIPH_BYOFFSET; + didz.diph.dwObj = pdidoi->dwOfs; + + didz.dwData = 5000; + + currentDevice->device->SetProperty(DIPROP_DEADZONE, &didz.diph); + + LONG center = (diprg.lMin + diprg.lMax)/2; + LONG threshold = (diprg.lMax - center)/2; + + // only 8 axis supported + if(axisNumber < 8) { + currentDevice->axis[axisNumber].center = center; + currentDevice->axis[axisNumber].negative = center - threshold; + currentDevice->axis[axisNumber].positive = center + threshold; + currentDevice->axis[axisNumber].offset = pdidoi->dwOfs; + } + axisNumber++; + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK EnumPovsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, + VOID* pContext ) +{ + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + ZeroMemory(&pDevices[numDevices],sizeof(deviceInfo)); + + HRESULT hRet = pDirectInput->CreateDevice(pInst->guidInstance, + &pDevices[numDevices].device, + NULL); + + if(hRet != DI_OK) + return DIENUM_STOP; + + DIDEVCAPS caps; + caps.dwSize=sizeof(DIDEVCAPS); + + hRet = pDevices[numDevices].device->GetCapabilities(&caps); + + if(hRet == DI_OK) { + if(caps.dwFlags & DIDC_POLLEDDATAFORMAT || + caps.dwFlags & DIDC_POLLEDDEVICE) + pDevices[numDevices].isPolled = TRUE; + + pDevices[numDevices].nButtons = caps.dwButtons; + pDevices[numDevices].nAxes = caps.dwAxes; + pDevices[numDevices].nPovs = caps.dwPOVs; + + for(int i = 0; i < 6; i++) { + pDevices[numDevices].axis[i].center = 0x8000; + pDevices[numDevices].axis[i].negative = 0x4000; + pDevices[numDevices].axis[i].positive = 0xc000; + } + } else if(joyDebug) + winlog("Failed to get device capabilities %08x\n", hRet); + + if(joyDebug) { + // don't translate. debug only + winlog("******************************\n"); + winlog("Joystick %2d name : %s\n", numDevices, pInst->tszProductName); + } + + numDevices++; + + + return DIENUM_CONTINUE; +} + +BOOL CALLBACK DIEnumDevicesCallback2(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + numDevices++; + + return DIENUM_CONTINUE; +} + +static int getPovState(DWORD value) +{ + int state = 0; + if(LOWORD(value) != 0xFFFF) { + if(value < 9000 || value > 27000) + state |= POV_UP; + if(value > 0 && value < 18000) + state |= POV_RIGHT; + if(value > 9000 && value < 27000) + state |= POV_DOWN; + if(value > 18000) + state |= POV_LEFT; + } + return state; +} + +static void checkKeys() +{ + int dev = 0; + int i; + + for(i = 0; i < numDevices; i++) + pDevices[i].needed = 0; + + for(i = 0; i < 4; i++) { + dev = joypad[i][KEY_LEFT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_LEFT] = DIK_LEFT; + + dev = joypad[i][KEY_RIGHT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_RIGHT] = DIK_RIGHT; + + dev = joypad[i][KEY_UP] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_UP] = DIK_UP; + + dev = joypad[i][KEY_DOWN] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_DOWN] = DIK_DOWN; + + dev = joypad[i][KEY_BUTTON_A] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_A] = DIK_Z; + + dev = joypad[i][KEY_BUTTON_B] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_B] = DIK_X; + + dev = joypad[i][KEY_BUTTON_L] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_L] = DIK_A; + + dev = joypad[i][KEY_BUTTON_R] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_R] = DIK_S; + + dev = joypad[i][KEY_BUTTON_START] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_START] = DIK_RETURN; + + dev = joypad[i][KEY_BUTTON_SELECT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SELECT] = DIK_BACK; + + dev = joypad[i][KEY_BUTTON_SPEED] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SPEED] = DIK_SPACE; + + dev = joypad[i][KEY_BUTTON_CAPTURE] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_CAPTURE] = DIK_F12; + + dev = joypad[i][KEY_BUTTON_GS] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_GS] = DIK_C; + } + + dev = motion[KEY_UP] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_UP] = DIK_NUMPAD8; + + dev = motion[KEY_DOWN] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_DOWN] = DIK_NUMPAD2; + + dev = motion[KEY_LEFT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_LEFT] = DIK_NUMPAD4; + + dev = motion[KEY_RIGHT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_RIGHT] = DIK_NUMPAD6; +} + +#define KEYDOWN(buffer,key) (buffer[key] & 0x80) + +static bool readKeyboard() +{ + if(pDevices[0].needed) { + if (!theApp.dinputKeyFocus) { + memset(pDevices[0].data, 0, 256); + return true; + } + HRESULT hret = pDevices[0].device-> + GetDeviceState(256, + (LPVOID)pDevices[0].data); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + hret = pDevices[0].device->Acquire(); + if(hret != DI_OK) + return false; + hret = pDevices[0].device->GetDeviceState(256,(LPVOID)pDevices[0].data); + } + + return hret == DI_OK; + } + return true; +} + +static bool readJoystick(int joy) +{ + if(pDevices[joy].needed) { + if(pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + HRESULT hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + hret = pDevices[joy].device->Acquire(); + + if(hret == DI_OK) { + + if(pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + } + } + + return hret == DI_OK; + } + + return true; +} + +static void checkKeyboard() +{ + HRESULT hret = pDevices[0].device->Acquire(); + hret = pDevices[0].device-> + GetDeviceState(256, + (LPVOID)pDevices[0].data); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + return; + } + + if(hret == DI_OK) { + for(int i = 0; i < 256; i++) { + if(KEYDOWN(pDevices[0].data, i)) { + SendMessage(GetFocus(), JOYCONFIG_MESSAGE,0,i); + break; + } + } + } +} + +static void checkJoypads() +{ + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di,sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + int i =0; + + DIJOYSTATE joystick; + + for(i = 1; i < numDevices; i++) { + HRESULT hret = pDevices[i].device->Acquire(); + + + if(pDevices[i].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[i].device)->Poll(); + + hret = pDevices[i].device->GetDeviceState(sizeof(joystick), &joystick); + + int j; + + if(pDevices[i].first) { + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + pDevices[i].first = FALSE; + continue; + } + + for(j = 0; j < pDevices[i].nButtons; j++) { + if(((pDevices[i].state.rgbButtons[j] ^ joystick.rgbButtons[j]) + & joystick.rgbButtons[j]) & 0x80) { + HWND focus = GetFocus(); + + SendMessage(focus, JOYCONFIG_MESSAGE, i,j+128); + } + } + + for(j = 0; j < pDevices[i].nAxes && j < 8; j++) { + LONG value = pDevices[i].axis[j].center; + LONG old = 0; + switch(pDevices[i].axis[j].offset) { + case DIJOFS_X: + value = joystick.lX; + old = pDevices[i].state.lX; + break; + case DIJOFS_Y: + value = joystick.lY; + old = pDevices[i].state.lY; + break; + case DIJOFS_Z: + value = joystick.lZ; + old = pDevices[i].state.lZ; + break; + case DIJOFS_RX: + value = joystick.lRx; + old = pDevices[i].state.lRx; + break; + case DIJOFS_RY: + value = joystick.lRy; + old = pDevices[i].state.lRy; + break; + case DIJOFS_RZ: + value = joystick.lRz; + old = pDevices[i].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = joystick.rglSlider[0]; + old = pDevices[i].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = joystick.rglSlider[1]; + old = pDevices[i].state.rglSlider[1]; + break; + } + if(value != old) { + if(value < pDevices[i].axis[j].negative) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)); + else if (value > pDevices[i].axis[j].positive) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)+1); + } + } + + for(j = 0;j < 4 && j < pDevices[i].nPovs; j++) { + if(LOWORD(pDevices[i].state.rgdwPOV[j]) != LOWORD(joystick.rgdwPOV[j])) { + int state = getPovState(joystick.rgdwPOV[j]); + + if(state & POV_UP) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x20); + else if(state & POV_DOWN) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x21); + else if(state & POV_RIGHT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x22); + else if(state & POV_LEFT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x23); + } + } + + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + } +} + +BOOL checkKey(int key) +{ + int dev = (key >> 8); + + int k = (key & 255); + + if(dev == 0) { + return KEYDOWN(pDevices[0].data,k); + } else { + if(k < 16) { + int axis = k >> 1; + LONG value = pDevices[dev].axis[axis].center; + switch(pDevices[dev].axis[axis].offset) { + case DIJOFS_X: + value = pDevices[dev].state.lX; + break; + case DIJOFS_Y: + value = pDevices[dev].state.lY; + break; + case DIJOFS_Z: + value = pDevices[dev].state.lZ; + break; + case DIJOFS_RX: + value = pDevices[dev].state.lRx; + break; + case DIJOFS_RY: + value = pDevices[dev].state.lRy; + break; + case DIJOFS_RZ: + value = pDevices[dev].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = pDevices[dev].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = pDevices[dev].state.rglSlider[1]; + break; + } + + if(k & 1) + return value > pDevices[dev].axis[axis].positive; + return value < pDevices[dev].axis[axis].negative; + } else if(k < 48) { + int hat = (k >> 2) & 3; + int state = getPovState(pDevices[dev].state.rgdwPOV[hat]); + BOOL res = FALSE; + switch(k & 3) { + case 0: + res = state & POV_UP; + break; + case 1: + res = state & POV_DOWN; + break; + case 2: + res = state & POV_RIGHT; + break; + case 3: + res = state & POV_LEFT; + break; + } + return res; + } else if(k >= 128) { + return pDevices[dev].state.rgbButtons[k-128] & 0x80; + } + } + + return FALSE; +} + +DirectInput::DirectInput() +{ + dinputDLL = NULL; +} + +DirectInput::~DirectInput() +{ + saveSettings(); + if(pDirectInput != NULL) { + if(pDevices) { + for(int i = 0; i < numDevices ; i++) { + if(pDevices[i].device) { + pDevices[i].device->Unacquire(); + pDevices[i].device->Release(); + pDevices[i].device = NULL; + } + } + free(pDevices); + pDevices = NULL; + } + + pDirectInput->Release(); + pDirectInput = NULL; + } + + if(dinputDLL) { + FreeLibrary(dinputDLL); + dinputDLL = NULL; + } +} + +bool DirectInput::initialize() +{ + joyDebug = GetPrivateProfileInt("config", + "joyDebug", + 0, + "VBA.ini"); + dinputDLL = LoadLibrary("DINPUT.DLL"); + HRESULT (WINAPI *DInputCreate)(HINSTANCE,DWORD,LPDIRECTINPUT *,IUnknown *); + if(dinputDLL != NULL) { + DInputCreate = (HRESULT (WINAPI *)(HINSTANCE,DWORD,LPDIRECTINPUT *,IUnknown *)) + GetProcAddress(dinputDLL, "DirectInputCreateA"); + + if(DInputCreate == NULL) { + directXMessage("DirectInputCreateA"); + return false; + } + } else { + directXMessage("DINPUT.DLL"); + return false; + } + + HRESULT hret = DInputCreate(AfxGetInstanceHandle(), + DIRECTINPUT_VERSION, + &pDirectInput, + NULL); + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATE), hret); + return false; + } + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback2, + NULL, + DIEDFL_ATTACHEDONLY); + + + + pDevices = (deviceInfo *)calloc(numDevices, sizeof(deviceInfo)); + + hret = pDirectInput->CreateDevice(GUID_SysKeyboard,&pDevices[0].device,NULL); + pDevices[0].isPolled = false; + pDevices[0].needed = true; + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATEDEVICE), hret); + return false; + } + + + numDevices = 1; + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback, + NULL, + DIEDFL_ATTACHEDONLY); + + // hret = pDevices[0].device->SetCooperativeLevel(hWindow, + // DISCL_FOREGROUND| + // DISCL_NONEXCLUSIVE); + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_LEVEL), hret); + return false; + } + + hret = pDevices[0].device->SetDataFormat(&c_dfDIKeyboard); + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_DATAFORMAT), hret); + return false; + } + + for(int i = 1; i < numDevices; i++) { + pDevices[i].device->SetDataFormat(&c_dfDIJoystick); + pDevices[i].needed = false; + currentDevice = &pDevices[i]; + axisNumber = 0; + currentDevice->device->EnumObjects(EnumAxesCallback, NULL, DIDFT_AXIS); + currentDevice->device->EnumObjects(EnumPovsCallback, NULL, DIDFT_POV); + if(joyDebug) { + // don't translate. debug only + winlog("Joystick %2d polled : %d\n", i, currentDevice->isPolled); + winlog("Joystick %2d buttons : %d\n", i, currentDevice->nButtons); + winlog("Joystick %2d povs : %d\n", i, currentDevice->nPovs); + winlog("Joystick %2d axes : %d\n", i, currentDevice->nAxes); + for(int j = 0; j < currentDevice->nAxes; j++) { + winlog("Axis %2d offset : %08lx\n", j, currentDevice->axis[j]. + offset); + winlog("Axis %2d center : %08lx\n", j, currentDevice->axis[j]. + center); + winlog("Axis %2d negative : %08lx\n", j, currentDevice->axis[j]. + negative); + winlog("Axis %2d positive : %08lx\n", j, currentDevice->axis[j]. + positive); + } + } + + currentDevice = NULL; + } + + if (1) for(int i = 0; i < numDevices; i++) + pDevices[i].device->Acquire(); + + return true; +} + +bool DirectInput::readDevices() +{ + bool ok = true; + for(int i = 0; i < numDevices; i++) { + if(pDevices[i].needed) { + if(i) { + ok = readJoystick(i); + } else + ok = readKeyboard(); + } + } + return ok; +} + +u32 DirectInput::readDevice(int which) +{ + u32 res = 0; + int i = theApp.joypadDefault; + if(which >= 0 && which <= 3) + i = which; + + if(checkKey(joypad[i][KEY_BUTTON_A])) + res |= 1; + if(checkKey(joypad[i][KEY_BUTTON_B])) + res |= 2; + if(checkKey(joypad[i][KEY_BUTTON_SELECT])) + res |= 4; + if(checkKey(joypad[i][KEY_BUTTON_START])) + res |= 8; + if(checkKey(joypad[i][KEY_RIGHT])) + res |= 16; + if(checkKey(joypad[i][KEY_LEFT])) + res |= 32; + if(checkKey(joypad[i][KEY_UP])) + res |= 64; + if(checkKey(joypad[i][KEY_DOWN])) + res |= 128; + if(checkKey(joypad[i][KEY_BUTTON_R])) + res |= 256; + if(checkKey(joypad[i][KEY_BUTTON_L])) + res |= 512; + + if(checkKey(joypad[i][KEY_BUTTON_GS])) + res |= 4096; + + if(theApp.autoFire) { + res &= (~theApp.autoFire); + if(theApp.autoFireToggle) + res |= theApp.autoFire; + theApp.autoFireToggle = !theApp.autoFireToggle; + } + + // disallow L+R or U+D of being pressed at the same time + if((res & 48) == 48) + res &= ~16; + if((res & 192) == 192) + res &= ~128; + + if(theApp.movieRecording) { + if(i == theApp.joypadDefault) { + if(res != theApp.movieLastJoypad) { + fwrite(&theApp.movieFrame, 1, sizeof(theApp.movieFrame), theApp.movieFile); + fwrite(&res, 1, sizeof(res), theApp.movieFile); + theApp.movieLastJoypad = res; + } + } + } + if(theApp.moviePlaying) { + if(theApp.movieFrame == theApp.moviePlayFrame) { + theApp.movieLastJoypad = theApp.movieNextJoypad; + theApp.movieReadNext(); + } + res = theApp.movieLastJoypad; + } + // we don't record speed up or screen capture buttons + if(checkKey(joypad[i][KEY_BUTTON_SPEED]) || theApp.speedupToggle) + res |= 1024; + if(checkKey(joypad[i][KEY_BUTTON_CAPTURE])) + res |= 2048; + + return res; +} + +CString DirectInput::getKeyName(int key) +{ + int d = (key >> 8); + int k = key & 255; + + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di,sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + CString winBuffer = winResLoadString(IDS_ERROR); + + if(d == 0) { + pDevices[0].device->GetObjectInfo(&di,key,DIPH_BYOFFSET); + winBuffer = di.tszName; + } else { + if(k < 16) { + if(k < 4) { + switch(k) { + case 0: + winBuffer.Format(winResLoadString(IDS_JOY_LEFT), d); + break; + case 1: + winBuffer.Format(winResLoadString(IDS_JOY_RIGHT), d); + break; + case 2: + winBuffer.Format(winResLoadString(IDS_JOY_UP), d); + break; + case 3: + winBuffer.Format(winResLoadString(IDS_JOY_DOWN), d); + break; + } + } else { + pDevices[d].device->GetObjectInfo(&di, + pDevices[d].axis[k>>1].offset, + DIPH_BYOFFSET); + if(k & 1) + winBuffer.Format("Joy %d %s +", d, di.tszName); + else + winBuffer.Format("Joy %d %s -", d, di.tszName); + } + } else if(k < 48) { + int hat = (k >> 2) & 3; + pDevices[d].device->GetObjectInfo(&di, + DIJOFS_POV(hat), + DIPH_BYOFFSET); + char *dir = "up"; + int dd = k & 3; + if(dd == 1) + dir = "down"; + else if(dd == 2) + dir = "right"; + else if(dd == 3) + dir = "left"; + winBuffer.Format("Joy %d %s %s", d, di.tszName, dir); + } else { + pDevices[d].device->GetObjectInfo(&di, + DIJOFS_BUTTON(k-128), + DIPH_BYOFFSET); + winBuffer.Format(winResLoadString(IDS_JOY_BUTTON),d,di.tszName); + } + } + + return winBuffer; +} + +void DirectInput::checkKeys() +{ + ::checkKeys(); +} + +void DirectInput::checkMotionKeys() +{ + if(checkKey(motion[KEY_LEFT])) { + theApp.sensorX += 3; + if(theApp.sensorX > 2197) + theApp.sensorX = 2197; + if(theApp.sensorX < 2047) + theApp.sensorX = 2057; + } else if(checkKey(motion[KEY_RIGHT])) { + theApp.sensorX -= 3; + if(theApp.sensorX < 1897) + theApp.sensorX = 1897; + if(theApp.sensorX > 2047) + theApp.sensorX = 2037; + } else if(theApp.sensorX > 2047) { + theApp.sensorX -= 2; + if(theApp.sensorX < 2047) + theApp.sensorX = 2047; + } else { + theApp.sensorX += 2; + if(theApp.sensorX > 2047) + theApp.sensorX = 2047; + } + + if(checkKey(motion[KEY_UP])) { + theApp.sensorY += 3; + if(theApp.sensorY > 2197) + theApp.sensorY = 2197; + if(theApp.sensorY < 2047) + theApp.sensorY = 2057; + } else if(checkKey(motion[KEY_DOWN])) { + theApp.sensorY -= 3; + if(theApp.sensorY < 1897) + theApp.sensorY = 1897; + if(theApp.sensorY > 2047) + theApp.sensorY = 2037; + } else if(theApp.sensorY > 2047) { + theApp.sensorY -= 2; + if(theApp.sensorY < 2047) + theApp.sensorY = 2047; + } else { + theApp.sensorY += 2; + if(theApp.sensorY > 2047) + theApp.sensorY = 2047; + } +} + +Input *newDirectInput() +{ + return new DirectInput; +} + + +void DirectInput::checkDevices() +{ + checkJoypads(); + checkKeyboard(); +} + +void DirectInput::activate() +{ + for(int i = 0; i < numDevices; i++) { + if(pDevices != NULL && pDevices[i].device != NULL) + pDevices[i].device->Acquire(); + } +} + +void DirectInput::loadSettings() +{ + winReadKeys(); +} + +void DirectInput::saveSettings() +{ + winSaveKeys(); +} diff --git a/src/win32/DirectInput.cpp.orig b/src/win32/DirectInput.cpp.orig new file mode 100644 index 00000000..de6c3b29 --- /dev/null +++ b/src/win32/DirectInput.cpp.orig @@ -0,0 +1,1056 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "Reg.h" +#include "WinResUtil.h" + +#define DIRECTINPUT_VERSION 0x0500 +#include + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern void directXMessage(const char *); +extern void winlog(const char *msg,...); + +#define POV_UP 1 +#define POV_DOWN 2 +#define POV_RIGHT 4 +#define POV_LEFT 8 + +class DirectInput : public Input { +private: + HINSTANCE dinputDLL; + +public: + virtual void checkDevices(); + DirectInput(); + virtual ~DirectInput(); + + virtual bool initialize(); + virtual bool readDevices(); + virtual u32 readDevice(int which); + virtual CString getKeyName(int key); + virtual void checkKeys(); + virtual void checkMotionKeys(); + virtual void activate(); + virtual void loadSettings(); + virtual void saveSettings(); +}; + +struct deviceInfo { + LPDIRECTINPUTDEVICE device; + BOOL isPolled; + int nButtons; + int nAxes; + int nPovs; + BOOL first; + struct { + DWORD offset; + LONG center; + LONG negative; + LONG positive; + } axis[8]; + int needed; + union { + UCHAR data[256]; + DIJOYSTATE state; + }; +}; + +static deviceInfo *currentDevice = NULL; +static int numDevices = 1; +static deviceInfo *pDevices = NULL; +static LPDIRECTINPUT pDirectInput = NULL; +static int joyDebug = 0; +static int axisNumber = 0; + +USHORT joypad[4][13] = { + { + DIK_LEFT, DIK_RIGHT, + DIK_UP, DIK_DOWN, + DIK_Z, DIK_X, + DIK_RETURN,DIK_BACK, + DIK_A, DIK_S, + DIK_SPACE, DIK_F12, + DIK_C + }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +USHORT motion[4] = { + DIK_NUMPAD4, DIK_NUMPAD6, DIK_NUMPAD8, DIK_NUMPAD2 +}; + +static int winReadKey(char *name, int num) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + return regQueryDwordValue(buffer, (DWORD)-1); +} + +void winReadKeys() +{ + int key = -1; + + for(int i = 0; i < 4; i++) { + key = winReadKey("Left", i); + if(key != -1) + joypad[i][KEY_LEFT] = key; + key = winReadKey("Right", i); + if(key != -1) + joypad[i][KEY_RIGHT] = key; + key = winReadKey("Up", i); + if(key != -1) + joypad[i][KEY_UP] = key; + key = winReadKey("Down", i); + if(key != -1) + joypad[i][KEY_DOWN] = key; + key = winReadKey("A", i); + if(key != -1) + joypad[i][KEY_BUTTON_A] = key; + key = winReadKey("B", i); + if(key != -1) + joypad[i][KEY_BUTTON_B] = key; + key = winReadKey("L", i); + if(key != -1) + joypad[i][KEY_BUTTON_L] = key; + key = winReadKey("R", i); + if(key != -1) + joypad[i][KEY_BUTTON_R] = key; + key = winReadKey("Start", i); + if(key != -1) + joypad[i][KEY_BUTTON_START] = key; + key = winReadKey("Select", i); + if(key != -1) + joypad[i][KEY_BUTTON_SELECT] = key; + key = winReadKey("Speed", i); + if(key != -1) + joypad[i][KEY_BUTTON_SPEED] = key; + key = winReadKey("Capture", i); + if(key != -1) + joypad[i][KEY_BUTTON_CAPTURE] = key; + key = winReadKey("GS", i); + if(key != -1) + joypad[i][KEY_BUTTON_GS] = key; + } + key = regQueryDwordValue("Motion_Left", (DWORD)-1); + if(key != -1) + motion[KEY_LEFT] = key; + key = regQueryDwordValue("Motion_Right", (DWORD)-1); + if(key != -1) + motion[KEY_RIGHT] = key; + key = regQueryDwordValue("Motion_Up", (DWORD)-1); + if(key != -1) + motion[KEY_UP] = key; + key = regQueryDwordValue("Motion_Down", (DWORD)-1); + if(key != -1) + motion[KEY_DOWN] = key; +} + +static void winSaveKey(char *name, int num, USHORT value) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + regSetDwordValue(buffer, value); +} + +void winSaveKeys() +{ + for(int i = 0; i < 4; i++) { + winSaveKey("Left", i, joypad[i][KEY_LEFT]); + winSaveKey("Right", i, joypad[i][KEY_RIGHT]); + winSaveKey("Up", i, joypad[i][KEY_UP]); + winSaveKey("Speed", i, joypad[i][KEY_BUTTON_SPEED]); + winSaveKey("Capture", i, joypad[i][KEY_BUTTON_CAPTURE]); + winSaveKey("GS", i, joypad[i][KEY_BUTTON_GS]); + winSaveKey("Down", i, joypad[i][KEY_DOWN]); + winSaveKey("A", i, joypad[i][KEY_BUTTON_A]); + winSaveKey("B", i, joypad[i][KEY_BUTTON_B]); + winSaveKey("L", i, joypad[i][KEY_BUTTON_L]); + winSaveKey("R", i, joypad[i][KEY_BUTTON_R]); + winSaveKey("Start", i, joypad[i][KEY_BUTTON_START]); + winSaveKey("Select", i, joypad[i][KEY_BUTTON_SELECT]); + } + regSetDwordValue("joyVersion", 1); + + regSetDwordValue("Motion_Left", + motion[KEY_LEFT]); + regSetDwordValue("Motion_Right", + motion[KEY_RIGHT]); + regSetDwordValue("Motion_Up", + motion[KEY_UP]); + regSetDwordValue("Motion_Down", + motion[KEY_DOWN]); +} + +static BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, + VOID* pContext ) +{ + DIPROPRANGE diprg; + diprg.diph.dwSize = sizeof(DIPROPRANGE); + diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); + diprg.diph.dwHow = DIPH_BYOFFSET; + diprg.diph.dwObj = pdidoi->dwOfs; // Specify the enumerated axis + + diprg.lMin = -32768; + diprg.lMax = 32767; + // try to set the range + if(FAILED(currentDevice->device->SetProperty(DIPROP_RANGE, &diprg.diph))) { + // Get the range for the axis + if( FAILED(currentDevice->device-> + GetProperty( DIPROP_RANGE, &diprg.diph ) ) ) { + return DIENUM_STOP; + } + } + + DIPROPDWORD didz; + + didz.diph.dwSize = sizeof(didz); + didz.diph.dwHeaderSize = sizeof(DIPROPHEADER); + didz.diph.dwHow = DIPH_BYOFFSET; + didz.diph.dwObj = pdidoi->dwOfs; + + didz.dwData = 5000; + + currentDevice->device->SetProperty(DIPROP_DEADZONE, &didz.diph); + + LONG center = (diprg.lMin + diprg.lMax)/2; + LONG threshold = (diprg.lMax - center)/2; + + // only 8 axis supported + if(axisNumber < 8) { + currentDevice->axis[axisNumber].center = center; + currentDevice->axis[axisNumber].negative = center - threshold; + currentDevice->axis[axisNumber].positive = center + threshold; + currentDevice->axis[axisNumber].offset = pdidoi->dwOfs; + } + axisNumber++; + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK EnumPovsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, + VOID* pContext ) +{ + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + ZeroMemory(&pDevices[numDevices],sizeof(deviceInfo)); + + HRESULT hRet = pDirectInput->CreateDevice(pInst->guidInstance, + &pDevices[numDevices].device, + NULL); + + if(hRet != DI_OK) + return DIENUM_STOP; + + DIDEVCAPS caps; + caps.dwSize=sizeof(DIDEVCAPS); + + hRet = pDevices[numDevices].device->GetCapabilities(&caps); + + if(hRet == DI_OK) { + if(caps.dwFlags & DIDC_POLLEDDATAFORMAT || + caps.dwFlags & DIDC_POLLEDDEVICE) + pDevices[numDevices].isPolled = TRUE; + + pDevices[numDevices].nButtons = caps.dwButtons; + pDevices[numDevices].nAxes = caps.dwAxes; + pDevices[numDevices].nPovs = caps.dwPOVs; + + for(int i = 0; i < 6; i++) { + pDevices[numDevices].axis[i].center = 0x8000; + pDevices[numDevices].axis[i].negative = 0x4000; + pDevices[numDevices].axis[i].positive = 0xc000; + } + } else if(joyDebug) + winlog("Failed to get device capabilities %08x\n", hRet); + + if(joyDebug) { + // don't translate. debug only + winlog("******************************\n"); + winlog("Joystick %2d name : %s\n", numDevices, pInst->tszProductName); + } + + numDevices++; + + + return DIENUM_CONTINUE; +} + +BOOL CALLBACK DIEnumDevicesCallback2(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + numDevices++; + + return DIENUM_CONTINUE; +} + +static int getPovState(DWORD value) +{ + int state = 0; + if(LOWORD(value) != 0xFFFF) { + if(value < 9000 || value > 27000) + state |= POV_UP; + if(value > 0 && value < 18000) + state |= POV_RIGHT; + if(value > 9000 && value < 27000) + state |= POV_DOWN; + if(value > 18000) + state |= POV_LEFT; + } + return state; +} + +static void checkKeys() +{ + int dev = 0; + int i; + + for(i = 0; i < numDevices; i++) + pDevices[i].needed = 0; + + for(i = 0; i < 4; i++) { + dev = joypad[i][KEY_LEFT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_LEFT] = DIK_LEFT; + + dev = joypad[i][KEY_RIGHT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_RIGHT] = DIK_RIGHT; + + dev = joypad[i][KEY_UP] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_UP] = DIK_UP; + + dev = joypad[i][KEY_DOWN] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_DOWN] = DIK_DOWN; + + dev = joypad[i][KEY_BUTTON_A] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_A] = DIK_Z; + + dev = joypad[i][KEY_BUTTON_B] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_B] = DIK_X; + + dev = joypad[i][KEY_BUTTON_L] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_L] = DIK_A; + + dev = joypad[i][KEY_BUTTON_R] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_R] = DIK_S; + + dev = joypad[i][KEY_BUTTON_START] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_START] = DIK_RETURN; + + dev = joypad[i][KEY_BUTTON_SELECT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SELECT] = DIK_BACK; + + dev = joypad[i][KEY_BUTTON_SPEED] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SPEED] = DIK_SPACE; + + dev = joypad[i][KEY_BUTTON_CAPTURE] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_CAPTURE] = DIK_F12; + + dev = joypad[i][KEY_BUTTON_GS] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_GS] = DIK_C; + } + + dev = motion[KEY_UP] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_UP] = DIK_NUMPAD8; + + dev = motion[KEY_DOWN] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_DOWN] = DIK_NUMPAD2; + + dev = motion[KEY_LEFT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_LEFT] = DIK_NUMPAD4; + + dev = motion[KEY_RIGHT] >> 8; + if(dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_RIGHT] = DIK_NUMPAD6; +} + +#define KEYDOWN(buffer,key) (buffer[key] & 0x80) + +static bool readKeyboard() +{ + if(pDevices[0].needed) { + HRESULT hret = pDevices[0].device-> + GetDeviceState(256, + (LPVOID)pDevices[0].data); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + hret = pDevices[0].device->Acquire(); + if(hret != DI_OK) + return false; + hret = pDevices[0].device->GetDeviceState(256,(LPVOID)pDevices[0].data); + } + + return hret == DI_OK; + } + return true; +} + +static bool readJoystick(int joy) +{ + if(pDevices[joy].needed) { + if(pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + HRESULT hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + hret = pDevices[joy].device->Acquire(); + + if(hret == DI_OK) { + + if(pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + } + } + + return hret == DI_OK; + } + + return true; +} + +static void checkKeyboard() +{ + HRESULT hret = pDevices[0].device->Acquire(); + hret = pDevices[0].device-> + GetDeviceState(256, + (LPVOID)pDevices[0].data); + + if(hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) { + return; + } + + if(hret == DI_OK) { + for(int i = 0; i < 256; i++) { + if(KEYDOWN(pDevices[0].data, i)) { + SendMessage(GetFocus(), JOYCONFIG_MESSAGE,0,i); + break; + } + } + } +} + +static void checkJoypads() +{ + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di,sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + int i =0; + + DIJOYSTATE joystick; + + for(i = 1; i < numDevices; i++) { + HRESULT hret = pDevices[i].device->Acquire(); + + + if(pDevices[i].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[i].device)->Poll(); + + hret = pDevices[i].device->GetDeviceState(sizeof(joystick), &joystick); + + int j; + + if(pDevices[i].first) { + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + pDevices[i].first = FALSE; + continue; + } + + for(j = 0; j < pDevices[i].nButtons; j++) { + if(((pDevices[i].state.rgbButtons[j] ^ joystick.rgbButtons[j]) + & joystick.rgbButtons[j]) & 0x80) { + HWND focus = GetFocus(); + + SendMessage(focus, JOYCONFIG_MESSAGE, i,j+128); + } + } + + for(j = 0; j < pDevices[i].nAxes && j < 8; j++) { + LONG value = pDevices[i].axis[j].center; + LONG old = 0; + switch(pDevices[i].axis[j].offset) { + case DIJOFS_X: + value = joystick.lX; + old = pDevices[i].state.lX; + break; + case DIJOFS_Y: + value = joystick.lY; + old = pDevices[i].state.lY; + break; + case DIJOFS_Z: + value = joystick.lZ; + old = pDevices[i].state.lZ; + break; + case DIJOFS_RX: + value = joystick.lRx; + old = pDevices[i].state.lRx; + break; + case DIJOFS_RY: + value = joystick.lRy; + old = pDevices[i].state.lRy; + break; + case DIJOFS_RZ: + value = joystick.lRz; + old = pDevices[i].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = joystick.rglSlider[0]; + old = pDevices[i].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = joystick.rglSlider[1]; + old = pDevices[i].state.rglSlider[1]; + break; + } + if(value != old) { + if(value < pDevices[i].axis[j].negative) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)); + else if (value > pDevices[i].axis[j].positive) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)+1); + } + } + + for(j = 0;j < 4 && j < pDevices[i].nPovs; j++) { + if(LOWORD(pDevices[i].state.rgdwPOV[j]) != LOWORD(joystick.rgdwPOV[j])) { + int state = getPovState(joystick.rgdwPOV[j]); + + if(state & POV_UP) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x20); + else if(state & POV_DOWN) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x21); + else if(state & POV_RIGHT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x22); + else if(state & POV_LEFT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x23); + } + } + + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + } +} + +BOOL checkKey(int key) +{ + int dev = (key >> 8); + + int k = (key & 255); + + if(dev == 0) { + return KEYDOWN(pDevices[0].data,k); + } else { + if(k < 16) { + int axis = k >> 1; + LONG value = pDevices[dev].axis[axis].center; + switch(pDevices[dev].axis[axis].offset) { + case DIJOFS_X: + value = pDevices[dev].state.lX; + break; + case DIJOFS_Y: + value = pDevices[dev].state.lY; + break; + case DIJOFS_Z: + value = pDevices[dev].state.lZ; + break; + case DIJOFS_RX: + value = pDevices[dev].state.lRx; + break; + case DIJOFS_RY: + value = pDevices[dev].state.lRy; + break; + case DIJOFS_RZ: + value = pDevices[dev].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = pDevices[dev].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = pDevices[dev].state.rglSlider[1]; + break; + } + + if(k & 1) + return value > pDevices[dev].axis[axis].positive; + return value < pDevices[dev].axis[axis].negative; + } else if(k < 48) { + int hat = (k >> 2) & 3; + int state = getPovState(pDevices[dev].state.rgdwPOV[hat]); + BOOL res = FALSE; + switch(k & 3) { + case 0: + res = state & POV_UP; + break; + case 1: + res = state & POV_DOWN; + break; + case 2: + res = state & POV_RIGHT; + break; + case 3: + res = state & POV_LEFT; + break; + } + return res; + } else if(k >= 128) { + return pDevices[dev].state.rgbButtons[k-128] & 0x80; + } + } + + return FALSE; +} + +DirectInput::DirectInput() +{ + dinputDLL = NULL; +} + +DirectInput::~DirectInput() +{ + saveSettings(); + if(pDirectInput != NULL) { + if(pDevices) { + for(int i = 0; i < numDevices ; i++) { + if(pDevices[i].device) { + pDevices[i].device->Unacquire(); + pDevices[i].device->Release(); + pDevices[i].device = NULL; + } + } + free(pDevices); + pDevices = NULL; + } + + pDirectInput->Release(); + pDirectInput = NULL; + } + + if(dinputDLL) { + FreeLibrary(dinputDLL); + dinputDLL = NULL; + } +} + +bool DirectInput::initialize() +{ + joyDebug = GetPrivateProfileInt("config", + "joyDebug", + 0, + "VBA.ini"); + dinputDLL = LoadLibrary("DINPUT.DLL"); + HRESULT (WINAPI *DInputCreate)(HINSTANCE,DWORD,LPDIRECTINPUT *,IUnknown *); + if(dinputDLL != NULL) { + DInputCreate = (HRESULT (WINAPI *)(HINSTANCE,DWORD,LPDIRECTINPUT *,IUnknown *)) + GetProcAddress(dinputDLL, "DirectInputCreateA"); + + if(DInputCreate == NULL) { + directXMessage("DirectInputCreateA"); + return false; + } + } else { + directXMessage("DINPUT.DLL"); + return false; + } + + HRESULT hret = DInputCreate(AfxGetInstanceHandle(), + DIRECTINPUT_VERSION, + &pDirectInput, + NULL); + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATE), hret); + return false; + } + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback2, + NULL, + DIEDFL_ATTACHEDONLY); + + + + pDevices = (deviceInfo *)calloc(numDevices, sizeof(deviceInfo)); + + hret = pDirectInput->CreateDevice(GUID_SysKeyboard,&pDevices[0].device,NULL); + pDevices[0].isPolled = false; + pDevices[0].needed = true; + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATEDEVICE), hret); + return false; + } + + + numDevices = 1; + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback, + NULL, + DIEDFL_ATTACHEDONLY); + + // hret = pDevices[0].device->SetCooperativeLevel(hWindow, + // DISCL_FOREGROUND| + // DISCL_NONEXCLUSIVE); + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_LEVEL), hret); + return false; + } + + hret = pDevices[0].device->SetDataFormat(&c_dfDIKeyboard); + + if(hret != DI_OK) { + // errorMessage(myLoadString(IDS_ERROR_DISP_DATAFORMAT), hret); + return false; + } + + for(int i = 1; i < numDevices; i++) { + pDevices[i].device->SetDataFormat(&c_dfDIJoystick); + pDevices[i].needed = false; + currentDevice = &pDevices[i]; + axisNumber = 0; + currentDevice->device->EnumObjects(EnumAxesCallback, NULL, DIDFT_AXIS); + currentDevice->device->EnumObjects(EnumPovsCallback, NULL, DIDFT_POV); + if(joyDebug) { + // don't translate. debug only + winlog("Joystick %2d polled : %d\n", i, currentDevice->isPolled); + winlog("Joystick %2d buttons : %d\n", i, currentDevice->nButtons); + winlog("Joystick %2d povs : %d\n", i, currentDevice->nPovs); + winlog("Joystick %2d axes : %d\n", i, currentDevice->nAxes); + for(int j = 0; j < currentDevice->nAxes; j++) { + winlog("Axis %2d offset : %08lx\n", j, currentDevice->axis[j]. + offset); + winlog("Axis %2d center : %08lx\n", j, currentDevice->axis[j]. + center); + winlog("Axis %2d negative : %08lx\n", j, currentDevice->axis[j]. + negative); + winlog("Axis %2d positive : %08lx\n", j, currentDevice->axis[j]. + positive); + } + } + + currentDevice = NULL; + } + + for(i = 0; i < numDevices; i++) + pDevices[i].device->Acquire(); + + return true; +} + +bool DirectInput::readDevices() +{ + bool ok = true; + for(int i = 0; i < numDevices; i++) { + if(pDevices[i].needed) { + if(i) { + ok = readJoystick(i); + } else + ok = readKeyboard(); + } + } + return ok; +} + +u32 DirectInput::readDevice(int which) +{ + u32 res = 0; + int i = theApp.joypadDefault; + if(which >= 0 && which <= 3) + i = which; + + if(checkKey(joypad[i][KEY_BUTTON_A])) + res |= 1; + if(checkKey(joypad[i][KEY_BUTTON_B])) + res |= 2; + if(checkKey(joypad[i][KEY_BUTTON_SELECT])) + res |= 4; + if(checkKey(joypad[i][KEY_BUTTON_START])) + res |= 8; + if(checkKey(joypad[i][KEY_RIGHT])) + res |= 16; + if(checkKey(joypad[i][KEY_LEFT])) + res |= 32; + if(checkKey(joypad[i][KEY_UP])) + res |= 64; + if(checkKey(joypad[i][KEY_DOWN])) + res |= 128; + if(checkKey(joypad[i][KEY_BUTTON_R])) + res |= 256; + if(checkKey(joypad[i][KEY_BUTTON_L])) + res |= 512; + + if(checkKey(joypad[i][KEY_BUTTON_GS])) + res |= 4096; + + if(theApp.autoFire) { + res &= (~theApp.autoFire); + if(theApp.autoFireToggle) + res |= theApp.autoFire; + theApp.autoFireToggle = !theApp.autoFireToggle; + } + + // disallow L+R or U+D of being pressed at the same time + if((res & 48) == 48) + res &= ~16; + if((res & 192) == 192) + res &= ~128; + + if(theApp.movieRecording) { + if(i == theApp.joypadDefault) { + if(res != theApp.movieLastJoypad) { + fwrite(&theApp.movieFrame, 1, sizeof(theApp.movieFrame), theApp.movieFile); + fwrite(&res, 1, sizeof(res), theApp.movieFile); + theApp.movieLastJoypad = res; + } + } + } + if(theApp.moviePlaying) { + if(theApp.movieFrame == theApp.moviePlayFrame) { + theApp.movieLastJoypad = theApp.movieNextJoypad; + theApp.movieReadNext(); + } + res = theApp.movieLastJoypad; + } + // we don't record speed up or screen capture buttons + if(checkKey(joypad[i][KEY_BUTTON_SPEED]) || theApp.speedupToggle) + res |= 1024; + if(checkKey(joypad[i][KEY_BUTTON_CAPTURE])) + res |= 2048; + + return res; +} + +CString DirectInput::getKeyName(int key) +{ + int d = (key >> 8); + int k = key & 255; + + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di,sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + CString winBuffer = winResLoadString(IDS_ERROR); + + if(d == 0) { + pDevices[0].device->GetObjectInfo(&di,key,DIPH_BYOFFSET); + winBuffer = di.tszName; + } else { + if(k < 16) { + if(k < 4) { + switch(k) { + case 0: + winBuffer.Format(winResLoadString(IDS_JOY_LEFT), d); + break; + case 1: + winBuffer.Format(winResLoadString(IDS_JOY_RIGHT), d); + break; + case 2: + winBuffer.Format(winResLoadString(IDS_JOY_UP), d); + break; + case 3: + winBuffer.Format(winResLoadString(IDS_JOY_DOWN), d); + break; + } + } else { + pDevices[d].device->GetObjectInfo(&di, + pDevices[d].axis[k>>1].offset, + DIPH_BYOFFSET); + if(k & 1) + winBuffer.Format("Joy %d %s +", d, di.tszName); + else + winBuffer.Format("Joy %d %s -", d, di.tszName); + } + } else if(k < 48) { + int hat = (k >> 2) & 3; + pDevices[d].device->GetObjectInfo(&di, + DIJOFS_POV(hat), + DIPH_BYOFFSET); + char *dir = "up"; + int dd = k & 3; + if(dd == 1) + dir = "down"; + else if(dd == 2) + dir = "right"; + else if(dd == 3) + dir = "left"; + winBuffer.Format("Joy %d %s %s", d, di.tszName, dir); + } else { + pDevices[d].device->GetObjectInfo(&di, + DIJOFS_BUTTON(k-128), + DIPH_BYOFFSET); + winBuffer.Format(winResLoadString(IDS_JOY_BUTTON),d,di.tszName); + } + } + + return winBuffer; +} + +void DirectInput::checkKeys() +{ + ::checkKeys(); +} + +void DirectInput::checkMotionKeys() +{ + if(checkKey(motion[KEY_LEFT])) { + theApp.sensorX += 3; + if(theApp.sensorX > 2197) + theApp.sensorX = 2197; + if(theApp.sensorX < 2047) + theApp.sensorX = 2057; + } else if(checkKey(motion[KEY_RIGHT])) { + theApp.sensorX -= 3; + if(theApp.sensorX < 1897) + theApp.sensorX = 1897; + if(theApp.sensorX > 2047) + theApp.sensorX = 2037; + } else if(theApp.sensorX > 2047) { + theApp.sensorX -= 2; + if(theApp.sensorX < 2047) + theApp.sensorX = 2047; + } else { + theApp.sensorX += 2; + if(theApp.sensorX > 2047) + theApp.sensorX = 2047; + } + + if(checkKey(motion[KEY_UP])) { + theApp.sensorY += 3; + if(theApp.sensorY > 2197) + theApp.sensorY = 2197; + if(theApp.sensorY < 2047) + theApp.sensorY = 2057; + } else if(checkKey(motion[KEY_DOWN])) { + theApp.sensorY -= 3; + if(theApp.sensorY < 1897) + theApp.sensorY = 1897; + if(theApp.sensorY > 2047) + theApp.sensorY = 2037; + } else if(theApp.sensorY > 2047) { + theApp.sensorY -= 2; + if(theApp.sensorY < 2047) + theApp.sensorY = 2047; + } else { + theApp.sensorY += 2; + if(theApp.sensorY > 2047) + theApp.sensorY = 2047; + } +} + +Input *newDirectInput() +{ + return new DirectInput; +} + + +void DirectInput::checkDevices() +{ + checkJoypads(); + checkKeyboard(); +} + +void DirectInput::activate() +{ + for(int i = 0; i < numDevices; i++) { + if(pDevices != NULL && pDevices[i].device != NULL) + pDevices[i].device->Acquire(); + } +} + +void DirectInput::loadSettings() +{ + winReadKeys(); +} + +void DirectInput::saveSettings() +{ + winSaveKeys(); +} diff --git a/src/win32/DirectSound.cpp b/src/win32/DirectSound.cpp new file mode 100644 index 00000000..87b462df --- /dev/null +++ b/src/win32/DirectSound.cpp @@ -0,0 +1,373 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004-2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "VBA.h" +#include "AVIWrite.h" +#include "Sound.h" +#include "WavWriter.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Sound.h" + +#include +#include //DirectSound + +class DirectSound : public ISound +{ +private: + HINSTANCE dsoundDLL; + LPDIRECTSOUND pDirectSound; + LPDIRECTSOUNDBUFFER dsbPrimary; + LPDIRECTSOUNDBUFFER dsbSecondary; + LPDIRECTSOUNDNOTIFY dsbNotify; + HANDLE dsbEvent; + WAVEFORMATEX wfx; + +public: + DirectSound(); + virtual ~DirectSound(); + + bool init(); + void pause(); + void reset(); + void resume(); + void write(); +}; + +DirectSound::DirectSound() +{ + dsoundDLL = NULL; + pDirectSound = NULL; + dsbPrimary = NULL; + dsbSecondary = NULL; + dsbNotify = NULL; + dsbEvent = NULL; +} + +DirectSound::~DirectSound() +{ + if(theApp.aviRecorder != NULL) { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + theApp.aviFrameNumber = 0; + } + + if(theApp.soundRecording) { + if(theApp.soundRecorder != NULL) { + delete theApp.soundRecorder; + theApp.soundRecorder = NULL; + } + theApp.soundRecording = false; + } + + if(dsbNotify != NULL) { + dsbNotify->Release(); + dsbNotify = NULL; + } + + if(dsbEvent != NULL) { + CloseHandle(dsbEvent); + dsbEvent = NULL; + } + + if(pDirectSound != NULL) { + if(dsbPrimary != NULL) { + dsbPrimary->Release(); + dsbPrimary = NULL; + } + + if(dsbSecondary != NULL) { + dsbSecondary->Release(); + dsbSecondary = NULL; + } + + pDirectSound->Release(); + pDirectSound = NULL; + } + + if(dsoundDLL != NULL) { + FreeLibrary(dsoundDLL); + dsoundDLL = NULL; + } +} + +bool DirectSound::init() +{ + HRESULT hr; + + dsoundDLL = LoadLibrary("dsound.dll"); + HRESULT (WINAPI *DSoundCreate)(LPCGUID,LPDIRECTSOUND *,IUnknown *); + if(dsoundDLL != NULL) { + DSoundCreate = (HRESULT (WINAPI *)(LPCGUID,LPDIRECTSOUND *,IUnknown *)) + GetProcAddress(dsoundDLL, "DirectSoundCreate8"); + + if(DSoundCreate == NULL) { + theApp.directXMessage("DirectSoundCreate8"); + return false; + } + } else { + theApp.directXMessage("dsound.dll"); + return false; + } + + if((hr = DSoundCreate(NULL,&pDirectSound,NULL) != DS_OK)) { + // errorMessage(myLoadString(IDS_ERROR_SOUND_CREATE), hr); + systemMessage(IDS_CANNOT_CREATE_DIRECTSOUND, + "Cannot create DirectSound %08x", hr); + pDirectSound = NULL; + dsbSecondary = NULL; + return false; + } + + if((hr=pDirectSound->SetCooperativeLevel((HWND)*theApp.m_pMainWnd, + DSSCL_EXCLUSIVE)) != DS_OK) { + // errorMessage(myLoadString(IDS_ERROR_SOUND_LEVEL), hr); + systemMessage(IDS_CANNOT_SETCOOPERATIVELEVEL, + "Cannot SetCooperativeLevel %08x", hr); + return false; + } + + DSBUFFERDESC dsbdesc; + ZeroMemory(&dsbdesc,sizeof(DSBUFFERDESC)); + dsbdesc.dwSize=sizeof(DSBUFFERDESC); + dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER; + + if((hr=pDirectSound->CreateSoundBuffer(&dsbdesc, + &dsbPrimary, + NULL) != DS_OK)) { + // errorMessage(myLoadString(IDS_ERROR_SOUND_BUFFER),hr); + systemMessage(IDS_CANNOT_CREATESOUNDBUFFER, + "Cannot CreateSoundBuffer %08x", hr); + return false; + } + + // Set primary buffer format + + memset(&wfx, 0, sizeof(WAVEFORMATEX)); + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 2; + switch(soundQuality) { + case 2: + wfx.nSamplesPerSec = 22050; + soundBufferLen = 736*2; + soundBufferTotalLen = 7360*2; + break; + case 4: + wfx.nSamplesPerSec = 11025; + soundBufferLen = 368*2; + soundBufferTotalLen = 3680*2; + break; + default: + soundQuality = 1; + wfx.nSamplesPerSec = 44100; + soundBufferLen = 1470*2; + soundBufferTotalLen = 14700*2; + } + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.wBitsPerSample / 8) * wfx.nChannels; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + + if((hr = dsbPrimary->SetFormat(&wfx)) != DS_OK) { + // errorMessage(myLoadString(IDS_ERROR_SOUND_PRIMARY),hr); + systemMessage(IDS_CANNOT_SETFORMAT_PRIMARY, + "Cannot SetFormat for primary %08x", hr); + return false; + } + + ZeroMemory(&dsbdesc,sizeof(DSBUFFERDESC)); + dsbdesc.dwSize = sizeof(DSBUFFERDESC); + dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLPOSITIONNOTIFY; + dsbdesc.dwBufferBytes = soundBufferTotalLen; + dsbdesc.lpwfxFormat = &wfx; + + if((hr = pDirectSound->CreateSoundBuffer(&dsbdesc, &dsbSecondary, NULL)) + != DS_OK) { + dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2; + if((hr = pDirectSound->CreateSoundBuffer(&dsbdesc, &dsbSecondary, NULL)) + != DS_OK) { + systemMessage(IDS_CANNOT_CREATESOUNDBUFFER_SEC, + "Cannot CreateSoundBuffer secondary %08x", hr); + return false; + } + } + + dsbSecondary->SetCurrentPosition(0); + + if(!theApp.useOldSync) { + hr = dsbSecondary->QueryInterface(IID_IDirectSoundNotify, + (void **)&dsbNotify); + if(!FAILED(hr)) { + dsbEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + + DSBPOSITIONNOTIFY notify[10]; + + for(int i = 0; i < 10; i++) { + notify[i].dwOffset = i*soundBufferLen; + notify[i].hEventNotify = dsbEvent; + } + if(FAILED(dsbNotify->SetNotificationPositions(10, notify))) { + dsbNotify->Release(); + dsbNotify = NULL; + CloseHandle(dsbEvent); + dsbEvent = NULL; + } + } + } + + hr = dsbPrimary->Play(0,0,DSBPLAY_LOOPING); + + if(hr != DS_OK) { + // errorMessage(myLoadString(IDS_ERROR_SOUND_PLAYPRIM), hr); + systemMessage(IDS_CANNOT_PLAY_PRIMARY, "Cannot Play primary %08x", hr); + return false; + } + + systemSoundOn = true; + + return true; +} + +void DirectSound::pause() +{ + if(dsbSecondary != NULL) { + DWORD status = 0; + dsbSecondary->GetStatus(&status); + + if(status & DSBSTATUS_PLAYING) { + dsbSecondary->Stop(); + } + } +} + +void DirectSound::reset() +{ + if(dsbSecondary) { + dsbSecondary->Stop(); + dsbSecondary->SetCurrentPosition(0); + } +} + +void DirectSound::resume() +{ + if(dsbSecondary != NULL) { + dsbSecondary->Play(0,0,DSBPLAY_LOOPING); + } +} + +void DirectSound::write() +{ + int len = soundBufferLen; + LPVOID lpvPtr1; + DWORD dwBytes1; + LPVOID lpvPtr2; + DWORD dwBytes2; + + if(!pDirectSound) + return; + + if(theApp.soundRecording) { + if(dsbSecondary) { + if(theApp.soundRecorder == NULL) { + theApp.soundRecorder = new WavWriter; + WAVEFORMATEX format; + dsbSecondary->GetFormat(&format, sizeof(format), NULL); + if(theApp.soundRecorder->Open(theApp.soundRecordName)) + theApp.soundRecorder->SetFormat(&format); + } + } + + if(theApp.soundRecorder) { + theApp.soundRecorder->AddSound((u8 *)soundFinalWave, len); + } + } + + if(theApp.aviRecording) { + if(theApp.aviRecorder) { + if(dsbSecondary) { + if(!theApp.aviRecorder->IsSoundAdded()) { + WAVEFORMATEX format; + dsbSecondary->GetFormat(&format, sizeof(format), NULL); + theApp.aviRecorder->SetSoundFormat(&format); + } + } + + theApp.aviRecorder->AddSound((const char *)soundFinalWave, len); + } + } + + HRESULT hr; + + if(!speedup && synchronize && !theApp.throttle) { + DWORD status=0; + hr = dsbSecondary->GetStatus(&status); + if(status && DSBSTATUS_PLAYING) { + if(!soundPaused) { + DWORD play; + while(true) { + dsbSecondary->GetCurrentPosition(&play, NULL); + + if(play < soundNextPosition || + play > soundNextPosition+soundBufferLen) { + break; + } + + if(dsbEvent) { + WaitForSingleObject(dsbEvent, 50); + } + } + } + } else { + soundPaused = 1; + } + } + // Obtain memory address of write block. This will be in two parts + // if the block wraps around. + hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen, &lpvPtr1, + &dwBytes1, &lpvPtr2, &dwBytes2, + 0); + + // If DSERR_BUFFERLOST is returned, restore and retry lock. + if (DSERR_BUFFERLOST == hr) { + dsbSecondary->Restore(); + hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen,&lpvPtr1, + &dwBytes1, &lpvPtr2, &dwBytes2, + 0); + } + + soundNextPosition += soundBufferLen; + soundNextPosition = soundNextPosition % soundBufferTotalLen; + + if SUCCEEDED(hr) { + // Write to pointers. + CopyMemory(lpvPtr1, soundFinalWave, dwBytes1); + if (NULL != lpvPtr2) { + CopyMemory(lpvPtr2, soundFinalWave+dwBytes1, dwBytes2); + } + // Release the data back to DirectSound. + hr = dsbSecondary->Unlock(lpvPtr1, dwBytes1, lpvPtr2, + dwBytes2); + } +} + +ISound *newDirectSound() +{ + return new DirectSound(); +} diff --git a/src/win32/Directories.cpp b/src/win32/Directories.cpp new file mode 100644 index 00000000..4a1bec69 --- /dev/null +++ b/src/win32/Directories.cpp @@ -0,0 +1,257 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Directories.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Directories.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// Directories dialog + +static int CALLBACK browseCallbackProc(HWND hWnd, UINT msg, + LPARAM l, LPARAM data) +{ + char *buffer = (char *)data; + switch(msg) { + case BFFM_INITIALIZED: + if(buffer[0]) + SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)buffer); + break; + default: + break; + } + return 0; +} + +Directories::Directories(CWnd* pParent /*=NULL*/) + : CDialog(Directories::IDD, pParent) +{ + //{{AFX_DATA_INIT(Directories) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void Directories::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Directories) + DDX_Control(pDX, IDC_SAVE_PATH, m_savePath); + DDX_Control(pDX, IDC_ROM_PATH, m_romPath); + DDX_Control(pDX, IDC_GBROM_PATH, m_gbromPath); + DDX_Control(pDX, IDC_CAPTURE_PATH, m_capturePath); + DDX_Control(pDX, IDC_BATTERY_PATH, m_batteryPath); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(Directories, CDialog) + //{{AFX_MSG_MAP(Directories) + ON_BN_CLICKED(IDC_BATTERY_DIR, OnBatteryDir) + ON_BN_CLICKED(IDC_BATTERY_DIR_RESET, OnBatteryDirReset) + ON_BN_CLICKED(IDC_CAPTURE_DIR, OnCaptureDir) + ON_BN_CLICKED(IDC_CAPTURE_DIR_RESET, OnCaptureDirReset) + ON_BN_CLICKED(IDC_GBROM_DIR, OnGbromDir) + ON_BN_CLICKED(IDC_GBROM_DIR_RESET, OnGbromDirReset) + ON_BN_CLICKED(IDC_ROM_DIR, OnRomDir) + ON_BN_CLICKED(IDC_ROM_DIR_RESET, OnRomDirReset) + ON_BN_CLICKED(IDC_SAVE_DIR, OnSaveDir) + ON_BN_CLICKED(IDC_SAVE_DIR_RESET, OnSaveDirReset) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Directories message handlers + +BOOL Directories::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString p = regQueryStringValue("romdir", NULL); + if(!p.IsEmpty()) { + int len = p.GetLength(); + if(len > 0) + if(p[len-1] == '\\') + p = p.Left(len-1); + GetDlgItem(IDC_ROM_PATH)->SetWindowText(p); + } + + p = regQueryStringValue("gbromdir", NULL); + if(!p.IsEmpty()) { + int len = p.GetLength(); + if(len > 0) + if(p[len-1] == '\\') + p = p.Left(len-1); + GetDlgItem(IDC_GBROM_PATH)->SetWindowText(p); + } + + p = regQueryStringValue("batteryDir", NULL); + if(!p.IsEmpty()) + GetDlgItem(IDC_BATTERY_PATH)->SetWindowText( p); + p = regQueryStringValue("saveDir", NULL); + if(!p.IsEmpty()) + GetDlgItem(IDC_SAVE_PATH)->SetWindowText(p); + p = regQueryStringValue("captureDir", NULL); + if(!p.IsEmpty()) + GetDlgItem(IDC_CAPTURE_PATH)->SetWindowText(p); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Directories::OnBatteryDir() +{ + m_batteryPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_BATTERY_DIR)); + if(!p.IsEmpty()) + m_batteryPath.SetWindowText(p); +} + +void Directories::OnBatteryDirReset() +{ + regDeleteValue("batteryDir"); + m_batteryPath.SetWindowText(""); +} + +void Directories::OnCaptureDir() +{ + m_capturePath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_CAPTURE_DIR)); + if(!p.IsEmpty()) + m_capturePath.SetWindowText(p); +} + +void Directories::OnCaptureDirReset() +{ + regDeleteValue("captureDir"); + m_capturePath.SetWindowText(""); +} + +void Directories::OnGbromDir() +{ + m_gbromPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_ROM_DIR)); + if(!p.IsEmpty()) + m_gbromPath.SetWindowText(p); +} + +void Directories::OnGbromDirReset() +{ + regDeleteValue("gbromdir"); + m_gbromPath.SetWindowText(""); +} + +void Directories::OnRomDir() +{ + m_romPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_ROM_DIR)); + if(!p.IsEmpty()) + m_romPath.SetWindowText(p); +} + +void Directories::OnRomDirReset() +{ + regDeleteValue("romdir"); + m_romPath.SetWindowText(""); +} + +void Directories::OnSaveDir() +{ + m_savePath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_SAVE_DIR)); + if(!p.IsEmpty()) + m_savePath.SetWindowText(p); +} + +void Directories::OnSaveDirReset() +{ + regDeleteValue("saveDir"); + m_savePath.SetWindowText(""); +} + +void Directories::OnCancel() +{ + EndDialog(FALSE); +} + +void Directories::OnOK() +{ + CString buffer; + m_romPath.GetWindowText(buffer); + if(!buffer.IsEmpty()) + regSetStringValue("romdir", buffer); + m_gbromPath.GetWindowText(buffer); + if(!buffer.IsEmpty()) + regSetStringValue("gbromdir", buffer); + m_batteryPath.GetWindowText(buffer); + if(!buffer.IsEmpty()) + regSetStringValue("batteryDir", buffer); + m_savePath.GetWindowText(buffer); + if(!buffer.IsEmpty()) + regSetStringValue("saveDir", buffer); + m_capturePath.GetWindowText(buffer); + if(!buffer.IsEmpty()) + regSetStringValue("captureDir", buffer); + EndDialog(TRUE); +} + +CString Directories::browseForDir(CString title) +{ + static char buffer[1024]; + LPMALLOC pMalloc; + LPITEMIDLIST pidl; + + CString res; + + if(SUCCEEDED(SHGetMalloc(&pMalloc))) { + BROWSEINFO bi; + ZeroMemory(&bi, sizeof(bi)); + bi.hwndOwner = m_hWnd; + bi.lpszTitle = title; + bi.pidlRoot = 0; + bi.ulFlags = BIF_RETURNONLYFSDIRS; + bi.lpfn = browseCallbackProc; + bi.lParam = (LPARAM)(LPCTSTR)initialFolderDir; + + pidl = SHBrowseForFolder(&bi); + + if(pidl) { + if(SHGetPathFromIDList(pidl, buffer)) { + res = buffer; + } + pMalloc->Free(pidl); + pMalloc->Release(); + } + } + return res; +} diff --git a/src/win32/Directories.h b/src/win32/Directories.h new file mode 100644 index 00000000..50087355 --- /dev/null +++ b/src/win32/Directories.h @@ -0,0 +1,83 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_) +#define AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Directories.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Directories dialog + +class Directories : public CDialog +{ + // Construction + public: + CString initialFolderDir; + CString browseForDir(CString title); + Directories(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Directories) + enum { IDD = IDD_DIRECTORIES }; + CEdit m_savePath; + CEdit m_romPath; + CEdit m_gbromPath; + CEdit m_capturePath; + CEdit m_batteryPath; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Directories) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(Directories) + virtual BOOL OnInitDialog(); + afx_msg void OnBatteryDir(); + afx_msg void OnBatteryDirReset(); + afx_msg void OnCaptureDir(); + afx_msg void OnCaptureDirReset(); + afx_msg void OnGbromDir(); + afx_msg void OnGbromDirReset(); + afx_msg void OnRomDir(); + afx_msg void OnRomDirReset(); + afx_msg void OnSaveDir(); + afx_msg void OnSaveDirReset(); + virtual void OnCancel(); + virtual void OnOK(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_) diff --git a/src/win32/Disassemble.cpp b/src/win32/Disassemble.cpp new file mode 100644 index 00000000..fab2d533 --- /dev/null +++ b/src/win32/Disassemble.cpp @@ -0,0 +1,349 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Disassemble.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Disassemble.h" + +#include "../System.h" +#include "../armdis.h" +#include "../GBA.h" +#include "../Globals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern int emulating; + +extern void CPUUpdateCPSR(); + + +///////////////////////////////////////////////////////////////////////////// +// Disassemble dialog + + +Disassemble::Disassemble(CWnd* pParent /*=NULL*/) + : ResizeDlg(Disassemble::IDD, pParent) +{ + //{{AFX_DATA_INIT(Disassemble) + m_c = FALSE; + m_f = FALSE; + m_i = FALSE; + m_n = FALSE; + m_t = FALSE; + m_v = FALSE; + m_z = FALSE; + mode = -1; + //}}AFX_DATA_INIT + mode = 0; + address = 0; + autoUpdate = false; + count = 1; +} + + +void Disassemble::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Disassemble) + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DISASSEMBLE, m_list); + DDX_Check(pDX, IDC_C, m_c); + DDX_Check(pDX, IDC_F, m_f); + DDX_Check(pDX, IDC_I, m_i); + DDX_Check(pDX, IDC_N, m_n); + DDX_Check(pDX, IDC_T, m_t); + DDX_Check(pDX, IDC_V, m_v); + DDX_Check(pDX, IDC_Z, m_z); + DDX_Radio(pDX, IDC_AUTOMATIC, mode); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(Disassemble, CDialog) + //{{AFX_MSG_MAP(Disassemble) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_AUTOMATIC, OnAutomatic) + ON_BN_CLICKED(IDC_ARM, OnArm) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_GO, OnGo) + ON_BN_CLICKED(IDC_GOPC, OnGopc) + ON_BN_CLICKED(IDC_NEXT, OnNext) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_THUMB, OnThumb) + ON_WM_VSCROLL() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Disassemble message handlers + +void Disassemble::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void Disassemble::OnAutomatic() +{ + mode = 0; + refresh(); +} + +void Disassemble::OnArm() +{ + mode = 1; + refresh(); +} + +void Disassemble::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void Disassemble::OnGo() +{ + CString buffer; + m_address.GetWindowText(buffer); + sscanf(buffer, "%x", &address); + refresh(); +} + +void Disassemble::OnGopc() +{ + if(armState) + address = armNextPC - 16; + else + address = armNextPC - 8; + + refresh(); +} + +void Disassemble::OnNext() +{ + CPULoop(1); + if(armState) { + u32 total = address+count*4; + if(armNextPC >= address && armNextPC < total) { + } else { + OnGopc(); + } + } else { + u32 total = address+count*2; + if(armNextPC >= address && armNextPC < total) { + } else { + OnGopc(); + } + } + refresh(); +} + +void Disassemble::OnRefresh() +{ + refresh(); +} + +void Disassemble::OnThumb() +{ + mode = 2; + refresh(); +} + +BOOL Disassemble::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_DISASSEMBLE, DS_SizeY) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_NEXT, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_GOPC, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_VSCROLL, DS_SizeY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\DisassembleView", + NULL); + + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; + si.nMin = 0; + si.nMax = 100; + si.nPos = 50; + si.nPage = 0; + GetDlgItem(IDC_VSCROLL)->SetScrollInfo(SB_CTL, &si, TRUE); + + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + + m_list.SetFont(font, FALSE); + for(int i = 0; i < 17; i++) + GetDlgItem(IDC_R0+i)->SetFont(font, FALSE); + + GetDlgItem(IDC_MODE)->SetFont(font, FALSE); + + + m_address.LimitText(8); + refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Disassemble::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + switch(nSBCode) { + case SB_LINEDOWN: + if(mode == 0) { + if(armState) + address += 4; + else + address += 2; + } else if(mode == 1) + address += 4; + else + address += 2; + break; + case SB_LINEUP: + if(mode == 0) { + if(armState) + address -= 4; + else + address -= 2; + } else if(mode == 1) + address -= 4; + else + address -= 2; + break; + case SB_PAGEDOWN: + if(mode == 0) { + if(armState) + address += count*4; + else + address += count*2; + } else if(mode == 1) + address += count*4; + else + address += count*2; + break; + case SB_PAGEUP: + if(mode == 0) { + if(armState) + address -= count*4; + else + address -= count*2; + } else if(mode == 1) + address -= count*4; + else + address -= count*2; + break; + } + refresh(); + + CDialog::OnVScroll(nSBCode, nPos, pScrollBar); +} + +void Disassemble::refresh() +{ + if(rom == NULL) + return; + + bool arm = armState; + + if(mode != 0) { + if(mode == 1) + arm = true; + else + arm = false; + } + + int h = m_list.GetItemHeight(0); + RECT r; + m_list.GetClientRect(&r); + count = (r.bottom - r.top+1)/h; + + m_list.ResetContent(); + if(!emulating && theApp.cartridgeType == 0) + return; + + char buffer[80]; + u32 addr = address; + int i; + int sel = -1; + for(i = 0; i < count; i++) { + if(addr == armNextPC) + sel = i; + if(arm) { + addr += disArm(addr, buffer, 3); + } else { + addr += disThumb(addr, buffer, 3); + } + m_list.InsertString(-1, buffer); + } + + if(sel != -1) + m_list.SetCurSel(sel); + + CPUUpdateCPSR(); + + for(i = 0; i < 17; i++) { + sprintf(buffer, "%08x", reg[i].I); + GetDlgItem(IDC_R0+i)->SetWindowText(buffer); + } + + m_n = (reg[16].I & 0x80000000) != 0; + m_z = (reg[16].I & 0x40000000) != 0; + m_c = (reg[16].I & 0x20000000) != 0; + m_v = (reg[16].I & 0x10000000) != 0; + m_i = (reg[16].I & 0x80) != 0; + m_f = (reg[16].I & 0x40) != 0; + m_t = (reg[16].I & 0x20) != 0; + + UpdateData(FALSE); + + int v = reg[16].I & 0x1f; + sprintf(buffer, "%02x", v); + GetDlgItem(IDC_MODE)->SetWindowText(buffer); +} + +void Disassemble::update() +{ + OnGopc(); + refresh(); +} + +void Disassemble::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/Disassemble.h b/src/win32/Disassemble.h new file mode 100644 index 00000000..259538d8 --- /dev/null +++ b/src/win32/Disassemble.h @@ -0,0 +1,94 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_) +#define AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Disassemble.h : header file +// + +#include "IUpdate.h" +#include "ResizeDlg.h" +#include "../System.h" // Added by ClassView + +///////////////////////////////////////////////////////////////////////////// +// Disassemble dialog + +class Disassemble : public ResizeDlg, IUpdateListener +{ + // Construction + public: + virtual void update(); + void refresh(); + int count; + bool autoUpdate; + u32 address; + Disassemble(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Disassemble) + enum { IDD = IDD_DISASSEMBLE }; + CEdit m_address; + CListBox m_list; + BOOL m_c; + BOOL m_f; + BOOL m_i; + BOOL m_n; + BOOL m_t; + BOOL m_v; + BOOL m_z; + int mode; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Disassemble) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(Disassemble) + afx_msg void OnAutoUpdate(); + afx_msg void OnAutomatic(); + afx_msg void OnArm(); + afx_msg void OnClose(); + afx_msg void OnGo(); + afx_msg void OnGopc(); + afx_msg void OnNext(); + afx_msg void OnRefresh(); + afx_msg void OnThumb(); + virtual BOOL OnInitDialog(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_) diff --git a/src/win32/Display.h b/src/win32/Display.h new file mode 100644 index 00000000..b63005e9 --- /dev/null +++ b/src/win32/Display.h @@ -0,0 +1,44 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +enum DISPLAY_TYPE { + GDI = 0, + DIRECT_DRAW = 1, + DIRECT_3D = 2, + OPENGL = 3 +}; + +class IDisplay { + public: + IDisplay() {}; + virtual ~IDisplay() {}; + + virtual bool initialize() = 0; + virtual void cleanup() = 0; + virtual void render() = 0; + virtual void checkFullScreen() { }; + virtual void renderMenu() { }; + virtual void clear() = 0; + virtual bool changeRenderSize(int w, int h) { return true; }; + virtual void resize(int w, int h) {}; + virtual void setOption(const char *option, int value) = 0; + virtual DISPLAY_TYPE getType() = 0; + virtual int selectFullScreenMode(GUID **) = 0; + virtual int selectFullScreenMode2() { return 0; }; +}; diff --git a/src/win32/ExportGSASnapshot.cpp b/src/win32/ExportGSASnapshot.cpp new file mode 100644 index 00000000..c6143d40 --- /dev/null +++ b/src/win32/ExportGSASnapshot.cpp @@ -0,0 +1,117 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// ExportGSASnapshot.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "ExportGSASnapshot.h" + +#include "../GBA.h" +#include "../NLS.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot dialog + + +ExportGSASnapshot::ExportGSASnapshot(CString filename, CString title, CWnd* pParent /*=NULL*/) + : CDialog(ExportGSASnapshot::IDD, pParent) +{ + //{{AFX_DATA_INIT(ExportGSASnapshot) + m_desc = _T(""); + m_notes = _T(""); + m_title = _T(""); + //}}AFX_DATA_INIT + m_title = title; + m_filename = filename; + char date[100]; + char time[100]; + + GetDateFormat(LOCALE_USER_DEFAULT, + DATE_SHORTDATE, + NULL, + NULL, + date, + 100); + GetTimeFormat(LOCALE_USER_DEFAULT, + 0, + NULL, + NULL, + time, + 100); + m_desc.Format("%s %s", date, time); +} + + +void ExportGSASnapshot::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(ExportGSASnapshot) + DDX_Text(pDX, IDC_DESC, m_desc); + DDV_MaxChars(pDX, m_desc, 100); + DDX_Text(pDX, IDC_NOTES, m_notes); + DDV_MaxChars(pDX, m_notes, 512); + DDX_Text(pDX, IDC_TITLE, m_title); + DDV_MaxChars(pDX, m_title, 100); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(ExportGSASnapshot, CDialog) + //{{AFX_MSG_MAP(ExportGSASnapshot) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot message handlers + +BOOL ExportGSASnapshot::OnInitDialog() +{ + CDialog::OnInitDialog(); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void ExportGSASnapshot::OnCancel() +{ + EndDialog(FALSE); +} + +void ExportGSASnapshot::OnOk() +{ + UpdateData(TRUE); + + bool result = CPUWriteGSASnapshot(m_filename, m_title, m_desc, m_notes); + + if(!result) + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + m_filename); + + EndDialog(TRUE); +} diff --git a/src/win32/ExportGSASnapshot.h b/src/win32/ExportGSASnapshot.h new file mode 100644 index 00000000..390908fa --- /dev/null +++ b/src/win32/ExportGSASnapshot.h @@ -0,0 +1,70 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_) +#define AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ExportGSASnapshot.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot dialog + +class ExportGSASnapshot : public CDialog +{ + // Construction + public: + ExportGSASnapshot(CString filename, CString title,CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(ExportGSASnapshot) + enum { IDD = IDD_EXPORT_SPS }; + CString m_desc; + CString m_notes; + CString m_title; + //}}AFX_DATA + CString m_filename; + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ExportGSASnapshot) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(ExportGSASnapshot) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_) diff --git a/src/win32/FileDlg.cpp b/src/win32/FileDlg.cpp new file mode 100644 index 00000000..9cee23e1 --- /dev/null +++ b/src/win32/FileDlg.cpp @@ -0,0 +1,197 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// FileDlg.cpp: implementation of the FileDlg class. +// +////////////////////////////////////////////////////////////////////// +#include "stdafx.h" +#include +#include + +#include "VBA.h" +#include "FileDlg.h" +#include "../System.h" +#include "..\..\res\resource.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +static FileDlg *instance = NULL; + +static UINT_PTR CALLBACK HookFunc(HWND hwnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) +{ + if(instance) { + if(msg == WM_NOTIFY) { + OFNOTIFY *notify = (OFNOTIFY *)lParam; + if(notify) { + if(notify->hdr.code == CDN_TYPECHANGE) { + instance->OnTypeChange(hwnd); + return 1; + } + } + } + } + return 0; +} + +static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) +{ + if(instance) { + if(msg == WM_COMMAND) { + if(HIWORD(wParam) == CBN_SELCHANGE) { + if(LOWORD(wParam) == cmb1) { + // call method with combobox handle to keep + // behaviour there + instance->OnTypeChange((HWND)lParam); + return 1; + } + } + } + } + return 0; +} + +///////////////////////////////////////////////////////////////////////////// +// FileDlg + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter, + int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir, + LPCTSTR title, bool save) +{ + OSVERSIONINFO info; + info.dwOSVersionInfoSize = sizeof(info); + GetVersionEx(&info); + m_file = file; + int size = sizeof(OPENFILENAME); + + // avoid problems if OPENFILENAME is already defined with the extended fields + // needed for the enhanced open/save dialog +#if _WIN32_WINNT < 0x0500 + if(info.dwPlatformId == VER_PLATFORM_WIN32_NT) { + if(info.dwMajorVersion >= 5) + size = sizeof(OPENFILENAMEEX); + } +#endif + + ZeroMemory(&m_ofn, sizeof(m_ofn)); + m_ofn.lpstrFile = m_file.GetBuffer(MAX_PATH); + m_ofn.nMaxFile = MAX_PATH; + m_ofn.lStructSize = size; + m_ofn.hwndOwner = parent ? parent->GetSafeHwnd() : NULL; + m_ofn.nFilterIndex = filterIndex; + m_ofn.lpstrInitialDir = initialDir; + m_ofn.lpstrTitle = title; + m_ofn.lpstrDefExt = ext; + m_ofn.lpfnHook = HookFunc; + m_ofn.Flags = OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_ENABLEHOOK; + m_ofn.Flags |= OFN_EXPLORER; + m_filter = filter; + + char *p = m_filter.GetBuffer(0); + + while ((p = strchr(p, '|')) != NULL) + *p++ = 0; + m_ofn.lpstrFilter = m_filter; + + if(theApp.videoOption == VIDEO_320x240) { + m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENDLG); + m_ofn.lpfnHook = HookFuncOldStyle; + m_ofn.Flags |= OFN_ENABLETEMPLATE; + m_ofn.Flags &= ~OFN_EXPLORER; + } + + isSave = save; + extensions = exts; + + instance = this; +} + +FileDlg::~FileDlg() +{ + instance = NULL; +} + +void FileDlg::OnTypeChange(HWND hwnd) +{ + HWND parent = GetParent(hwnd); + + HWND fileNameControl = ::GetDlgItem(parent, cmb13); + + if(fileNameControl == NULL) + fileNameControl = ::GetDlgItem(parent, edt1); + + if(fileNameControl == NULL) + return; + + CString filename; + GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH); + filename.ReleaseBuffer(); + + HWND typeControl = ::GetDlgItem(parent, cmb1); + + ASSERT(typeControl != NULL); + + int sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0); + + ASSERT(sel != -1); + + LPCTSTR typeName = extensions[sel]; + + if(filename.GetLength() == 0) { + filename.Format("*%s", typeName); + } else { + int index = filename.Find('.'); + if (index == -1) { + filename = filename + typeName; + } else { + filename = filename.Left(index) + typeName; + } + } + SetWindowText(fileNameControl, filename); +} + +int FileDlg::getFilterIndex() +{ + return m_ofn.nFilterIndex; +} + +int FileDlg::DoModal() +{ + BOOL res = isSave ? GetSaveFileName(&m_ofn) : + GetOpenFileName(&m_ofn); + + return res ? IDOK : IDCANCEL; +} + +LPCTSTR FileDlg::GetPathName() +{ + return (LPCTSTR)m_file; +} diff --git a/src/win32/FileDlg.h b/src/win32/FileDlg.h new file mode 100644 index 00000000..2712f666 --- /dev/null +++ b/src/win32/FileDlg.h @@ -0,0 +1,66 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// FileDlg.h: interface for the FileDlg class. +// +////////////////////////////////////////////////////////////////////// +#if !defined(AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_) +#define AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// FileDlg.h : header file +// + +struct OPENFILENAMEEX : public OPENFILENAME { + void * pvReserved; + DWORD dwReserved; + DWORD FlagsEx; +}; + +///////////////////////////////////////////////////////////////////////////// +// FileDlg dialog + +class FileDlg +{ + private: + CString m_file; + CString m_filter; + public: + OPENFILENAMEEX m_ofn; + int DoModal(); + LPCTSTR GetPathName(); + virtual int getFilterIndex(); + virtual void OnTypeChange(HWND hwnd); + FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter, + int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir, + LPCTSTR title, bool save); + virtual ~FileDlg(); + + protected: + bool isSave; + LPCTSTR *extensions; + + protected: + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. +}; + +#endif // !defined(AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_) diff --git a/src/win32/GBACheats.cpp b/src/win32/GBACheats.cpp new file mode 100644 index 00000000..f36b3101 --- /dev/null +++ b/src/win32/GBACheats.cpp @@ -0,0 +1,1172 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBACheats.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GBACheats.h" + +#include "../System.h" +#include "../Cheats.h" +#include "../CheatSearch.h" +#include "../GBA.h" +#include "../Globals.h" + +#include "Reg.h" +#include "StringTokenizer.h" +#include "WinResUtil.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch dialog + +GBACheatSearch::GBACheatSearch(CWnd* pParent /*=NULL*/) + : CDialog(GBACheatSearch::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBACheatSearch) + valueType = -1; + sizeType = -1; + searchType = -1; + numberType = -1; + updateValues = FALSE; + //}}AFX_DATA_INIT + data = NULL; +} + +GBACheatSearch::~GBACheatSearch() +{ + if(data) + free(data); +} + +void GBACheatSearch::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBACheatSearch) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + DDX_Radio(pDX, IDC_OLD_VALUE, valueType); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_EQ, searchType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + DDX_Check(pDX, IDC_UPDATE, updateValues); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBACheatSearch, CDialog) + //{{AFX_MSG_MAP(GBACheatSearch) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(IDC_START, OnStart) + ON_BN_CLICKED(IDC_SEARCH, OnSearch) + ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) + ON_BN_CLICKED(IDC_UPDATE, OnUpdate) + ON_NOTIFY(LVN_GETDISPINFO, IDC_CHEAT_LIST, OnGetdispinfoCheatList) + ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) + ON_CONTROL_RANGE(BN_CLICKED, IDC_OLD_VALUE, IDC_SPECIFIC_VALUE, OnValueType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_EQ, IDC_GE, OnSearchType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch message handlers + +void GBACheatSearch::OnOk() +{ + EndDialog(TRUE); +} + +void GBACheatSearch::OnStart() +{ + if(cheatSearchData.count == 0) { + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + block->size = 0x40000; + block->offset = 0x2000000; + block->bits = (u8 *)malloc(0x40000>>3); + block->data = workRAM; + block->saved = (u8 *)malloc(0x40000); + + block = &cheatSearchData.blocks[1]; + block->size = 0x8000; + block->offset = 0x3000000; + block->bits = (u8 *)malloc(0x8000>>3); + block->data = internalRAM; + block->saved = (u8 *)malloc(0x8000); + + cheatSearchData.count = 2; + } + + cheatSearchStart(&cheatSearchData); + GetDlgItem(IDC_SEARCH)->EnableWindow(TRUE); +} + +void GBACheatSearch::OnSearch() +{ + CString buffer; + + if(valueType == 0) + cheatSearch(&cheatSearchData, + searchType, + sizeType, + numberType == 0); + else { + m_value.GetWindowText(buffer); + if(buffer.IsEmpty()) { + systemMessage(IDS_NUMBER_CANNOT_BE_EMPTY, "Number cannot be empty"); + return; + } + int value = 0; + switch(numberType) { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + cheatSearchValue(&cheatSearchData, + searchType, + sizeType, + numberType == 0, + value); + } + + addChanges(true); + + if(updateValues) + cheatSearchUpdateValues(&cheatSearchData); +} + +void GBACheatSearch::OnAddCheat() +{ + int mark = m_list.GetSelectionMark(); + + if(mark != -1) { + LVITEM item; + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if(m_list.GetItem(&item)) { + AddCheat dlg((u32)item.lParam); + dlg.DoModal(); + } + } +} + +void GBACheatSearch::OnUpdate() +{ + if(GetDlgItem(IDC_UPDATE)->SendMessage(BM_GETCHECK, + 0, + 0) & BST_CHECKED) + updateValues = true; + else + updateValues = false; + regSetDwordValue("cheatsUpdate", updateValues); +} + +void GBACheatSearch::OnGetdispinfoCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + LV_DISPINFO* info = (LV_DISPINFO*)pNMHDR; + if(info->item.mask & LVIF_TEXT) { + int index = info->item.iItem; + int col = info->item.iSubItem; + + switch(col) { + case 0: + strcpy(info->item.pszText, data[index].address); + break; + case 1: + strcpy(info->item.pszText, data[index].oldValue); + break; + case 2: + strcpy(info->item.pszText, data[index].newValue); + break; + } + } + *pResult = TRUE; + +} + +void GBACheatSearch::OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(m_list.GetSelectionMark() != -1); + *pResult = TRUE; +} + +BOOL GBACheatSearch::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_ADDRESS); + + m_list.InsertColumn(0, temp, LVCFMT_CENTER, 125, 0); + + temp = winResLoadString(IDS_OLD_VALUE); + m_list.InsertColumn(1, temp, LVCFMT_CENTER, 125, 1); + + temp = winResLoadString(IDS_NEW_VALUE); + m_list.InsertColumn(2, temp, LVCFMT_CENTER, 125, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT); + + if(!cheatSearchData.count) { + GetDlgItem(IDC_SEARCH)->EnableWindow(FALSE); + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(FALSE); + } + + valueType = regQueryDwordValue("cheatsValueType", 0); + if(valueType < 0 || valueType > 1) + valueType = 0; + + searchType = regQueryDwordValue("cheatsSearchType", SEARCH_EQ); + if(searchType > 5 || searchType < 0) + searchType = 0; + + numberType = regQueryDwordValue("cheatsNumberType", 2); + if(numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("cheatsSizeType", 0); + if(sizeType < 0 || sizeType > 2) + sizeType = 0; + + updateValues = regQueryDwordValue("cheatsUpdate", 0) ? + true : false; + + UpdateData(FALSE); + + if(valueType == 0) + m_value.EnableWindow(FALSE); + CenterWindow(); + + if(cheatSearchData.count) { + addChanges(false); + } + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBACheatSearch::addChanges(bool showMsgs) +{ + int count = cheatSearchGetCount(&cheatSearchData, sizeType); + + m_list.DeleteAllItems(); + + if(count > 1000) { + if(showMsgs) + systemMessage(IDS_SEARCH_PRODUCED_TOO_MANY, + "Search produced %d results. Please refine better", + count); + return; + } + + if(count == 0) { + if(showMsgs) + systemMessage(IDS_SEARCH_PRODUCED_NO_RESULTS, + "Search produced no results."); + return; + } + + m_list.SetItemCount(count); + if(data) + free(data); + + data = (WinCheatsData *)calloc(count,sizeof(WinCheatsData)); + + int inc = 1; + switch(sizeType) { + case 1: + inc = 2; + break; + case 2: + inc = 4; + break; + } + + int index = 0; + if(numberType == 0) { + for(int i = 0; i < cheatSearchData.count; i++) { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for(int j = 0; j < block->size; j+= inc) { + if(IS_BIT_SET(block->bits, j)) { + addChange(index++, + block->offset | j, + cheatSearchSignedRead(block->saved, + j, + sizeType), + cheatSearchSignedRead(block->data, + j, + sizeType)); + } + } + } + } else { + for(int i = 0; i < cheatSearchData.count; i++) { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for(int j = 0; j < block->size; j+= inc) { + if(IS_BIT_SET(block->bits, j)) { + addChange(index++, + block->offset | j, + cheatSearchRead(block->saved, + j, + sizeType), + cheatSearchRead(block->data, + j, + sizeType)); + } + } + } + } + + for(int i = 0; i < count; i++) { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = data[i].addr; + item.state = 0; + item.stateMask = 0; + item.pszText = LPSTR_TEXTCALLBACK; + m_list.InsertItem(&item); + + m_list.SetItemText(i, 1, LPSTR_TEXTCALLBACK); + m_list.SetItemText(i, 2, LPSTR_TEXTCALLBACK); + } +} + +void GBACheatSearch::addChange(int index, u32 address, u32 oldValue, u32 newValue) +{ + data[index].addr = address; + sprintf(data[index].address, "%08x",address); + switch(numberType) { + case 0: + sprintf(data[index].oldValue, "%d", oldValue); + sprintf(data[index].newValue, "%d", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%u", oldValue); + sprintf(data[index].newValue, "%u", newValue); + break; + case 2: + switch(sizeType) { + case 0: + sprintf(data[index].oldValue, "%02x", oldValue); + sprintf(data[index].newValue, "%02x", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%04x", oldValue); + sprintf(data[index].newValue, "%04x", newValue); + break; + case 2: + sprintf(data[index].oldValue, "%08x", oldValue); + sprintf(data[index].newValue, "%08x", newValue); + break; + } + } +} + +void GBACheatSearch::OnValueType(UINT id) +{ + switch(id) { + case IDC_OLD_VALUE: + valueType = 0; + m_value.EnableWindow(FALSE); + regSetDwordValue("cheatsValueType", 0); + break; + case IDC_SPECIFIC_VALUE: + valueType = 1; + m_value.EnableWindow(TRUE); + regSetDwordValue("cheatsValueType", 1); + break; + } +} + +void GBACheatSearch::OnSearchType(UINT id) +{ + switch(id) { + case IDC_EQ: + searchType = SEARCH_EQ; + regSetDwordValue("cheatsSearchType", 0); + break; + case IDC_NE: + searchType = SEARCH_NE; + regSetDwordValue("cheatsSearchType", 1); + break; + case IDC_LT: + searchType = SEARCH_LT; + regSetDwordValue("cheatsSearchType", 2); + break; + case IDC_LE: + searchType = SEARCH_LE; + regSetDwordValue("cheatsSearchType", 3); + break; + case IDC_GT: + searchType = SEARCH_GT; + regSetDwordValue("cheatsSearchType", 4); + break; + case IDC_GE: + searchType = SEARCH_GE; + regSetDwordValue("cheatsSearchType", 5); + break; + } +} + +void GBACheatSearch::OnNumberType(UINT id) +{ + switch(id) { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("cheatsNumberType", 0); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("cheatsNumberType", 1); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("cheatsNumberType", 2); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + } +} + +void GBACheatSearch::OnSizeType(UINT id) +{ + switch(id) { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("cheatsSizeType", 0); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("cheatsSizeType", 1); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("cheatsSizeType", 2); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + } +} +///////////////////////////////////////////////////////////////////////////// +// AddCheat dialog + + +AddCheat::AddCheat(u32 address, CWnd* pParent /*=NULL*/) + : CDialog(AddCheat::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCheat) + sizeType = -1; + numberType = -1; + //}}AFX_DATA_INIT + this->address = address; +} + + +void AddCheat::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCheat) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddCheat, CDialog) + //{{AFX_MSG_MAP(AddCheat) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddCheat message handlers + +void AddCheat::OnOk() +{ + // add cheat + if(addCheat()) { + EndDialog(TRUE); + } +} + +void AddCheat::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCheat::OnInitDialog() +{ + CDialog::OnInitDialog(); + + if(address != 0) { + CString buffer; + buffer.Format("%08x", address); + m_address.SetWindowText(buffer); + m_address.EnableWindow(FALSE); + } + + numberType = regQueryDwordValue("cheatsNumberType", 2); + if(numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("cheatsSizeType", 0); + if(sizeType < 0 || sizeType > 2) + sizeType = 0; + + UpdateData(FALSE); + + GetDlgItem(IDC_DESC)->SendMessage(EM_LIMITTEXT, + 32, + 0); + if(address != 0) { + GetDlgItem(IDC_SIZE_8)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_16)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_32)->EnableWindow(FALSE); + GetDlgItem(IDC_HEXADECIMAL)->EnableWindow(FALSE); + GetDlgItem(IDC_UNSIGNED)->EnableWindow(FALSE); + GetDlgItem(IDC_SIGNED)->EnableWindow(FALSE); + } + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AddCheat::OnNumberType(UINT id) +{ + switch(id) { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("cheatsNumberType", 0); + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("cheatsNumberType", 1); + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("cheatsNumberType", 2); + break; + } +} + +void AddCheat::OnSizeType(UINT id) +{ + switch(id) { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("cheatsSizeType", 0); + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("cheatsSizeType", 1); + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("cheatsSizeType", 2); + break; + } +} + +bool AddCheat::addCheat() +{ + CString buffer; + CString code; + + m_address.GetWindowText(buffer); + u32 address = 0; + sscanf(buffer, "%x", &address); + if((address >= 0x02000000 && address < 0x02040000) || + (address >= 0x03000000 && address < 0x03008000)) { + } else { + systemMessage(IDS_INVALID_ADDRESS, "Invalid address: %08x", address); + return false; + } + if(sizeType != 0) { + if(sizeType == 1 && address & 1) { + systemMessage(IDS_MISALIGNED_HALFWORD, + "Misaligned half-word address: %08x", address); + return false; + } + if(sizeType == 2 && address & 3) { + systemMessage(IDS_MISALIGNED_WORD, + "Misaligned word address: %08x", address); + return false; + } + } + u32 value; + m_value.GetWindowText(buffer); + + if(buffer.IsEmpty()) { + systemMessage(IDS_VALUE_CANNOT_BE_EMPTY, "Value cannot be empty"); + return false; + } + + switch(numberType) { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + + m_desc.GetWindowText(buffer); + + switch(sizeType) { + case 0: + code.Format("%08x:%02x", address, value); + break; + case 1: + code.Format("%08x:%04x", address, value); + break; + case 2: + code.Format("%08x:%08x", address, value); + break; + } + + cheatsAdd(code, buffer, address ,address, value,-1, sizeType); + return true; +} +///////////////////////////////////////////////////////////////////////////// +// GBACheatList dialog + + +GBACheatList::GBACheatList(CWnd* pParent /*=NULL*/) + : CDialog(GBACheatList::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBACheatList) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + duringRefresh = false; +} + + +void GBACheatList::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBACheatList) + DDX_Control(pDX, IDC_RESTORE, m_restore); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBACheatList, CDialog) + //{{AFX_MSG_MAP(GBACheatList) + ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) + ON_BN_CLICKED(IDC_ADD_CODE, OnAddCode) + ON_BN_CLICKED(IDC_ADD_CODEBREAKER, OnAddCodebreaker) + ON_BN_CLICKED(IDC_ADD_GAMESHARK, OnAddGameshark) + ON_BN_CLICKED(IDC_ENABLE, OnEnable) + ON_BN_CLICKED(IDC_REMOVE, OnRemove) + ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll) + ON_BN_CLICKED(IDC_RESTORE, OnRestore) + ON_BN_CLICKED(ID_OK, OnOk) + ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBACheatList message handlers + +void GBACheatList::OnAddCheat() +{ + AddCheat dlg(0); + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddCode() +{ + AddCheatCode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddCodebreaker() +{ + AddCBACode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddGameshark() +{ + AddGSACode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnEnable() +{ + int mark = m_list.GetSelectionMark(); + int count = m_list.GetItemCount(); + + if(mark != -1) { + LVITEM item; + for(int i = 0; i < count; i++) { + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM|LVIF_STATE; + item.stateMask = LVIS_SELECTED; + item.iItem = i; + if(m_list.GetItem(&item)) { + if(item.state & LVIS_SELECTED) { + if(cheatsList[item.lParam].enabled) + cheatsDisable(item.lParam); + else + cheatsEnable(item.lParam); + } + } + } + refresh(); + } +} + +void GBACheatList::OnRemove() +{ + int mark = m_list.GetSelectionMark(); + int count = m_list.GetItemCount(); + + if(mark != -1) { + for(int i = count - 1; i >= 0; i--) { + LVITEM item; + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM|LVIF_STATE; + item.iItem = i; + item.stateMask = LVIS_SELECTED; + if(m_list.GetItem(&item)) { + if(item.state & LVIS_SELECTED) { + cheatsDelete(item.lParam, restoreValues); + } + } + } + refresh(); + } +} + +void GBACheatList::OnRemoveAll() +{ + cheatsDeleteAll(restoreValues); + refresh(); +} + + +void GBACheatList::OnRestore() +{ + restoreValues = !restoreValues; + regSetDwordValue("cheatsRestore", restoreValues); +} + +void GBACheatList::OnOk() +{ + EndDialog(TRUE); +} + +void GBACheatList::OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + if(m_list.GetSelectionMark() != -1) { + GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE); + GetDlgItem(IDC_ENABLE)->EnableWindow(TRUE); + } else { + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + } + + if(!duringRefresh) { + LPNMLISTVIEW l = (LPNMLISTVIEW)pNMHDR; + if(l->uChanged & LVIF_STATE) { + if(((l->uOldState & LVIS_STATEIMAGEMASK)>>12) != + (((l->uNewState & LVIS_STATEIMAGEMASK)>>12))) { + if(m_list.GetCheck(l->iItem)) + cheatsEnable(l->lParam); + else + cheatsDisable(l->lParam); + refresh(); + } + } + } + + *pResult = 0; +} + +BOOL GBACheatList::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_CODE); + m_list.InsertColumn(0, temp, LVCFMT_LEFT, 170, 0); + temp = winResLoadString(IDS_DESCRIPTION); + m_list.InsertColumn(1, temp, LVCFMT_LEFT, 150, 1); + temp = winResLoadString(IDS_STATUS); + m_list.InsertColumn(2, temp, LVCFMT_LEFT, 80, 1); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT); + + restoreValues = regQueryDwordValue("cheatsRestore", 0) ? + true : false; + + m_restore.SetCheck(restoreValues); + + refresh(); + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBACheatList::refresh() +{ + duringRefresh = true; + m_list.DeleteAllItems(); + + CString buffer; + + for(int i = 0; i < cheatsNumber; i++) { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = i; + item.state = 0; + item.stateMask = 0; + item.pszText = cheatsList[i].codestring; + m_list.InsertItem(&item); + + m_list.SetCheck(i, (cheatsList[i].enabled) ? TRUE : FALSE); + + m_list.SetItemText(i, 1, cheatsList[i].desc); + + buffer = (cheatsList[i].enabled) ? 'E' : 'D'; + m_list.SetItemText(i, 2, buffer); + } + duringRefresh = false; +} +///////////////////////////////////////////////////////////////////////////// +// AddGSACode dialog + + +AddGSACode::AddGSACode(CWnd* pParent /*=NULL*/) + : CDialog(AddGSACode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGSACode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void AddGSACode::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGSACode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddGSACode, CDialog) + //{{AFX_MSG_MAP(AddGSACode) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddGSACode message handlers + +void AddGSACode::OnOk() +{ + CString desc; + CString buffer; + CString part1; + CString code; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + part1.Empty(); + const char *t = st.next(); + while(t) { + token = t; + token.MakeUpper(); + if(token.GetLength() == 16) + cheatsAddGSACode(token, desc, false); + else if(token.GetLength() == 12) { + code = token.Left(8); + code += " "; + code += token.Right(4); + cheatsAddCBACode(code, desc); + } else if(part1.IsEmpty()) + part1 = token; + else { + if(token.GetLength() == 4) { + code = part1; + code += " "; + code += token; + cheatsAddCBACode(code, desc); + } else { + code = part1 + token; + cheatsAddGSACode(code, desc, true); + } + part1.Empty(); + } + + t = st.next(); + } + EndDialog(TRUE); +} + +void AddGSACode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddGSACode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_GSA_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// AddCBACode dialog + + +AddCBACode::AddCBACode(CWnd* pParent /*=NULL*/) + : CDialog(AddCBACode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCBACode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void AddCBACode::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCBACode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddCBACode, CDialog) + //{{AFX_MSG_MAP(AddCBACode) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddCBACode message handlers + +void AddCBACode::OnOk() +{ + CString desc; + CString buffer; + CString part1; + CString code; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + part1.Empty(); + const char *t = st.next(); + while(t) { + token = t; + token.MakeUpper(); + if(token.GetLength() == 16) + cheatsAddGSACode(token, desc, false); + else if(token.GetLength() == 12) { + code = token.Left(8); + code += " "; + code += token.Right(4); + cheatsAddCBACode(code, desc); + } else if(part1.IsEmpty()) + part1 = token; + else { + if(token.GetLength() == 4) { + code = part1; + code += " "; + code += token; + cheatsAddCBACode(code, desc); + } else { + code = part1 + token; + cheatsAddGSACode(code, desc, true); + } + part1.Empty(); + } + + t = st.next(); + } + EndDialog(TRUE); +} + +void AddCBACode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCBACode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_CBA_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// AddCheatCode dialog + + +AddCheatCode::AddCheatCode(CWnd* pParent /*=NULL*/) + : CDialog(AddCheatCode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCheatCode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void AddCheatCode::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCheatCode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddCheatCode, CDialog) + //{{AFX_MSG_MAP(AddCheatCode) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddCheatCode message handlers + +void AddCheatCode::OnOk() +{ + CString desc; + CString buffer; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + const char *t = st.next(); + while(t) { + token = t; + token.MakeUpper(); + cheatsAddCheatCode(token, desc); + t = st.next(); + } + EndDialog(TRUE); +} + +void AddCheatCode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCheatCode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_CHEAT_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/GBACheats.h b/src/win32/GBACheats.h new file mode 100644 index 00000000..79a6f9ae --- /dev/null +++ b/src/win32/GBACheats.h @@ -0,0 +1,291 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_) +#define AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBACheats.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch dialog + +struct WinCheatsData { + u32 addr; + char address[9]; + char oldValue[12]; + char newValue[12]; +}; + +class GBACheatSearch : public CDialog +{ + // Construction + public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + afx_msg void OnSearchType(UINT id); + afx_msg void OnValueType(UINT id); + void addChange(int index, u32 address, u32 oldValue, u32 newValue); + GBACheatSearch(CWnd* pParent = NULL); // standard constructor + ~GBACheatSearch(); + + // Dialog Data + //{{AFX_DATA(GBACheatSearch) + enum { IDD = IDD_CHEATS }; + CEdit m_value; + CListCtrl m_list; + int valueType; + int sizeType; + int searchType; + int numberType; + BOOL updateValues; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBACheatSearch) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBACheatSearch) + afx_msg void OnOk(); + afx_msg void OnStart(); + afx_msg void OnSearch(); + afx_msg void OnAddCheat(); + afx_msg void OnUpdate(); + afx_msg void OnGetdispinfoCheatList(NMHDR* pNMHDR, LRESULT* pResult); + afx_msg void OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + void addChanges(bool showMsgs); + WinCheatsData *data; +}; + +///////////////////////////////////////////////////////////////////////////// +// AddCheat dialog + +class AddCheat : public CDialog +{ + // Construction + public: + bool addCheat(); + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + u32 address; + AddCheat(u32 address, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCheat) + enum { IDD = IDD_ADD_CHEAT }; + CEdit m_value; + CEdit m_desc; + CEdit m_address; + int sizeType; + int numberType; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCheat) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AddCheat) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + ///////////////////////////////////////////////////////////////////////////// +// GBACheatList dialog + +class GBACheatList : public CDialog +{ + // Construction + public: + void refresh(); + bool duringRefresh; + bool restoreValues; + + GBACheatList(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBACheatList) + enum { IDD = IDD_CHEAT_LIST }; + CButton m_restore; + CListCtrl m_list; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBACheatList) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBACheatList) + afx_msg void OnAddCheat(); + afx_msg void OnAddCode(); + afx_msg void OnAddCodebreaker(); + afx_msg void OnAddGameshark(); + afx_msg void OnEnable(); + afx_msg void OnRemove(); + afx_msg void OnRemoveAll(); + afx_msg void OnRestore(); + afx_msg void OnOk(); + afx_msg void OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + ///////////////////////////////////////////////////////////////////////////// +// AddGSACode dialog + +class AddGSACode : public CDialog +{ + // Construction + public: + AddGSACode(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGSACode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGSACode) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AddGSACode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// +// AddCBACode dialog + +class AddCBACode : public CDialog +{ + // Construction + public: + AddCBACode(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCBACode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCBACode) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AddCBACode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// +// AddCheatCode dialog + +class AddCheatCode : public CDialog +{ + // Construction + public: + AddCheatCode(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCheatCode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCheatCode) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AddCheatCode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_) diff --git a/src/win32/GBCheatsDlg.cpp b/src/win32/GBCheatsDlg.cpp new file mode 100644 index 00000000..dc8c164f --- /dev/null +++ b/src/win32/GBCheatsDlg.cpp @@ -0,0 +1,1009 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBCheats.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GBCheatsDlg.h" +#include "Reg.h" +#include "StringTokenizer.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../CheatSearch.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +static bool winGbCheatAddVerifyGs(const char *code, const char *desc) +{ + gbAddGsCheat(code, desc); + return true; +} + +static bool winGbCheatAddVerifyGg(const char *code, const char *desc) +{ + gbAddGgCheat(code, desc); + return true; +} + +///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch dialog + +GBCheatSearch::GBCheatSearch(CWnd* pParent /*=NULL*/) + : CDialog(GBCheatSearch::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBCheatSearch) + searchType = -1; + numberType = -1; + sizeType = -1; + updateValues = FALSE; + valueType = -1; + //}}AFX_DATA_INIT + data = NULL; +} + +GBCheatSearch::~GBCheatSearch() +{ + if(data) + free(data); +} + +void GBCheatSearch::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBCheatSearch) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + DDX_Radio(pDX, IDC_EQ, searchType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Check(pDX, IDC_UPDATE, updateValues); + DDX_Radio(pDX, IDC_OLD_VALUE, valueType); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBCheatSearch, CDialog) + //{{AFX_MSG_MAP(GBCheatSearch) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) + ON_BN_CLICKED(IDC_SEARCH, OnSearch) + ON_BN_CLICKED(IDC_START, OnStart) + ON_BN_CLICKED(IDC_UPDATE, OnUpdate) + ON_NOTIFY(LVN_GETDISPINFO, IDC_CHEAT_LIST, OnGetdispinfoCheatList) + ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) + ON_CONTROL_RANGE(BN_CLICKED, IDC_OLD_VALUE, IDC_SPECIFIC_VALUE, OnValueType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_EQ, IDC_GE, OnSearchType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch message handlers + +void GBCheatSearch::OnOk() +{ + if(data) + free(data); + data = NULL; + EndDialog(TRUE); +} + +void GBCheatSearch::OnAddCheat() +{ + int mark = m_list.GetSelectionMark(); + + if(mark != -1) { + LVITEM item; + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if(m_list.GetItem(&item)) { + AddGBCheat dlg((u32)item.lParam); + dlg.DoModal(); + } + } +} + +void GBCheatSearch::OnSearch() +{ + CString buffer; + if(valueType == 0) + cheatSearch(&cheatSearchData, + searchType, + sizeType, + numberType == 0); + else { + m_value.GetWindowText(buffer); + if(buffer.IsEmpty()) { + systemMessage(IDS_NUMBER_CANNOT_BE_EMPTY, "Number cannot be empty"); + return; + } + int value = 0; + switch(numberType) { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + cheatSearchValue(&cheatSearchData, + searchType, + sizeType, + numberType == 0, + value); + } + + addChanges(true); + + if(updateValues) + cheatSearchUpdateValues(&cheatSearchData); +} + +void GBCheatSearch::OnStart() +{ + if(cheatSearchData.count == 0) { + int i = 0; + + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + + if(gbRamSize) { + block->offset = 0xa000; + if(gbRam) + block->data = gbRam; + else + block->data = &gbMemory[0xa000]; + block->saved = (u8*)malloc(gbRamSize); + block->size = gbRamSize; + block->bits = (u8 *)malloc(gbRamSize >> 3); + i++; + } + block = &cheatSearchData.blocks[i]; + if(gbCgbMode) { + block->offset = 0xc000; + block->data = &gbMemory[0xc000]; + block->saved = (u8*)malloc(0x1000); + block->size = 0x1000; + block->bits = (u8 *)malloc(0x1000 >> 3); + i++; + block =&cheatSearchData.blocks[i]; + block->offset = 0xd000; + block->data = gbWram; + block->saved = (u8*)malloc(0x8000); + block->size = 0x8000; + block->bits = (u8 *)malloc(0x8000 >> 3); + i++; + } else { + block->offset = 0xc000; + block->data = &gbMemory[0xc000]; + block->saved = (u8*)malloc(0x2000); + block->size = 0x2000; + block->bits = (u8 *)malloc(0x2000 >> 3); + i++; + } + cheatSearchData.count = i; + } + + cheatSearchStart(&cheatSearchData); + GetDlgItem(IDC_SEARCH)->EnableWindow(TRUE); +} + +void GBCheatSearch::OnUpdate() +{ + if(GetDlgItem(IDC_UPDATE)->SendMessage(BM_GETCHECK, + 0, + 0) & BST_CHECKED) + updateValues = true; + else + updateValues = false; + regSetDwordValue("gbCheatsUpdate", updateValues); +} + +BOOL GBCheatSearch::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_ADDRESS); + + m_list.InsertColumn(0, temp, LVCFMT_CENTER, 125, 0); + + temp = winResLoadString(IDS_OLD_VALUE); + m_list.InsertColumn(1, temp, LVCFMT_CENTER, 125, 1); + + temp = winResLoadString(IDS_NEW_VALUE); + m_list.InsertColumn(2, temp, LVCFMT_CENTER, 125, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT); + + if(!cheatSearchData.count) { + GetDlgItem(IDC_SEARCH)->EnableWindow(FALSE); + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(FALSE); + } + + valueType = regQueryDwordValue("gbCheatsValueType", 0); + if(valueType < 0 || valueType > 1) + valueType = 2; + + searchType = regQueryDwordValue("gbCheatsSearchType", + SEARCH_EQ); + if(searchType < 0 || searchType > 5) + searchType = 0; + + numberType = regQueryDwordValue("gbCheatsNumberType", 2); + if(numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("gbCheatsSizeType", 0); + if(sizeType < 0 || sizeType > 2) + sizeType = 0; + + updateValues = regQueryDwordValue("gbCheatsUpdate", 0) ? + true : false; + + UpdateData(FALSE); + + if(valueType == 0) + m_value.EnableWindow(FALSE); + + CenterWindow(); + + if(cheatSearchData.count) { + addChanges(false); + } + + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBCheatSearch::OnGetdispinfoCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + LV_DISPINFO* info = (LV_DISPINFO*)pNMHDR; + if(info->item.mask & LVIF_TEXT) { + int index = info->item.iItem; + int col = info->item.iSubItem; + + switch(col) { + case 0: + strcpy(info->item.pszText, data[index].address); + break; + case 1: + strcpy(info->item.pszText, data[index].oldValue); + break; + case 2: + strcpy(info->item.pszText, data[index].newValue); + break; + } + } + *pResult = TRUE; +} + +void GBCheatSearch::OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(m_list.GetSelectionMark() != -1); +} + +int GBCheatSearch::getBank(u16 addr, int j) +{ + switch(addr >> 12) { + case 0x0a: + return j / 0x2000; + case 0x0d: + return j / 0x1000; + } + return 0; +} + +void GBCheatSearch::addChange(int index, int bank, u16 address, int offset, u32 oldValue, u32 newValue) +{ + data[index].bank = bank; + if(bank) { + if(address == 0xa000) + address |= offset & 0x1fff; + else + address |= offset & 0xfff; + } else + address |= offset; + data[index].addr = address; + sprintf(data[index].address, "%02x:%04x",bank,address); + switch(numberType) { + case 0: + sprintf(data[index].oldValue, "%d", oldValue); + sprintf(data[index].newValue, "%d", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%u", oldValue); + sprintf(data[index].newValue, "%u", newValue); + break; + case 2: + switch(sizeType) { + case 0: + sprintf(data[index].oldValue, "%02x", oldValue); + sprintf(data[index].newValue, "%02x", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%04x", oldValue); + sprintf(data[index].newValue, "%04x", newValue); + break; + case 2: + sprintf(data[index].oldValue, "%08x", oldValue); + sprintf(data[index].newValue, "%08x", newValue); + break; + } + } +} + +void GBCheatSearch::addChanges(bool showMsg) +{ + int count = cheatSearchGetCount(&cheatSearchData, sizeType); + + m_list.DeleteAllItems(); + + if(count > 1000) { + if(showMsg) + systemMessage(IDS_SEARCH_PRODUCED_TOO_MANY, + "Search produced %d results. Please refine better", + count); + return; + } + + if(count == 0) { + if(showMsg) + systemMessage(IDS_SEARCH_PRODUCED_NO_RESULTS, + "Search produced no results"); + return; + } + + m_list.SetItemCount(count); + if(data) + free(data); + + data = (WinGbCheatsData *)calloc(count, sizeof(WinGbCheatsData)); + + int inc = 1; + switch(sizeType) { + case 1: + inc = 2; + break; + case 2: + inc = 4; + break; + } + + int index = 0; + if(numberType == 0) { + for(int i = 0; i < cheatSearchData.count; i++) { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for(int j = 0; j < block->size; j+= inc) { + if(IS_BIT_SET(block->bits, j)) { + addChange(index++, + getBank(block->offset|j, j), + block->offset, + j, + cheatSearchSignedRead(block->saved, + j, + sizeType), + cheatSearchSignedRead(block->data, + j, + sizeType)); + } + } + } + } else { + for(int i = 0; i < cheatSearchData.count; i++) { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for(int j = 0; j < block->size; j+= inc) { + if(IS_BIT_SET(block->bits, j)) { + addChange(index++, + getBank(block->offset|j, j), + block->offset, + j, + cheatSearchRead(block->saved, + j, + sizeType), + cheatSearchRead(block->data, + j, + sizeType)); + } + } + } + } + + for(int i = 0; i < count; i++) { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = data[i].addr| + (data[i].bank << 16); + item.state = 0; + item.stateMask = 0; + item.pszText = LPSTR_TEXTCALLBACK; + m_list.InsertItem(&item); + + m_list.SetItemText(i, 1, LPSTR_TEXTCALLBACK); + m_list.SetItemText(i, 2, LPSTR_TEXTCALLBACK); + } +} + +void GBCheatSearch::OnValueType(UINT id) +{ + switch(id) { + case IDC_OLD_VALUE: + valueType = 0; + m_value.EnableWindow(FALSE); + regSetDwordValue("gbCheatsValueType", 0); + break; + case IDC_SPECIFIC_VALUE: + valueType = 1; + m_value.EnableWindow(TRUE); + regSetDwordValue("gbCheatsValueType", 1); + break; + } +} + +void GBCheatSearch::OnSearchType(UINT id) +{ + switch(id) { + case IDC_EQ: + searchType = SEARCH_EQ; + regSetDwordValue("gbCheatsSearchType", 0); + break; + case IDC_NE: + searchType = SEARCH_NE; + regSetDwordValue("gbCheatsSearchType", 1); + break; + case IDC_LT: + searchType = SEARCH_LT; + regSetDwordValue("gbCheatsSearchType", 2); + break; + case IDC_LE: + searchType = SEARCH_LE; + regSetDwordValue("gbCheatsSearchType", 3); + break; + case IDC_GT: + searchType = SEARCH_GT; + regSetDwordValue("gbCheatsSearchType", 4); + break; + case IDC_GE: + searchType = SEARCH_GE; + regSetDwordValue("gbCheatsSearchType", 5); + break; + } +} + +void GBCheatSearch::OnNumberType(UINT id) +{ + switch(id) { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("gbCheatsNumberType", 0); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("gbCheatsNumberType", 1); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("gbCheatsNumberType", 2); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + } +} + +void GBCheatSearch::OnSizeType(UINT id) +{ + switch(id) { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("gbCheatsSizeType", 0); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("gbCheatsSizeType", 1); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("gbCheatsSizeType", 2); + if(m_list.GetItemCount()) { + addChanges(false); + } + break; + } +} +///////////////////////////////////////////////////////////////////////////// +// AddGBCheat dialog + + +AddGBCheat::AddGBCheat(u32 addr, CWnd* pParent /*=NULL*/) + : CDialog(AddGBCheat::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGBCheat) + sizeType = -1; + numberType = -1; + //}}AFX_DATA_INIT + address = addr; +} + + +void AddGBCheat::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGBCheat) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddGBCheat, CDialog) + //{{AFX_MSG_MAP(AddGBCheat) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) + ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddGBCheat message handlers + +void AddGBCheat::OnCancel() +{ + EndDialog(FALSE); +} + +void AddGBCheat::OnOk() +{ + // add cheat + if(addCheat()) { + EndDialog(TRUE); + } +} + +bool AddGBCheat::addCheat() +{ + CString buffer; + CString code; + + u32 value; + m_value.GetWindowText(buffer); + + if(buffer.IsEmpty()) { + systemMessage(IDS_VALUE_CANNOT_BE_EMPTY, "Value cannot be empty"); + return false; + } + + switch(numberType) { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + + m_desc.GetWindowText(buffer); + + int bank = (address >> 16); + address &= 0xFFFF; + + if(address >= 0xd000) + bank += 0x90; + else + bank = 0x01; + + switch(sizeType) { + case 0: + code.Format("%02X%02X%02X%02X", bank, value, address&0xFF, address>>8); + gbAddGsCheat(code, buffer); + break; + case 1: + code.Format("%02X%02X%02X%02X", bank, value&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, value>>8, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + break; + case 2: + code.Format("%02X%02X%02X%02X", bank, value&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, (value>>8) & 0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, (value>>16)&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, value>>24, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + break; + } + + return true; +} + +BOOL AddGBCheat::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + buffer.Format("%02x:%08x", (address>>16), address&0xFFFF); + m_address.SetWindowText(buffer); + m_address.EnableWindow(FALSE); + ::SetWindowLong(m_address, + GWL_USERDATA, + address); + + numberType = regQueryDwordValue("gbCheatsNumberType", 2); + if(numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("gbCheatsSizeType", 0); + if(sizeType < 0 || sizeType > 2) + sizeType = 0; + + UpdateData(FALSE); + + m_desc.LimitText(32); + + if(address != 0) { + GetDlgItem(IDC_SIZE_8)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_16)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_32)->EnableWindow(FALSE); + GetDlgItem(IDC_HEXADECIMAL)->EnableWindow(FALSE); + GetDlgItem(IDC_UNSIGNED)->EnableWindow(FALSE); + GetDlgItem(IDC_SIGNED)->EnableWindow(FALSE); + } + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AddGBCheat::OnNumberType(UINT id) +{ + switch(id) { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("gbCheatsNumberType", 0); + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("gbCheatsNumberType", 1); + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("gbCheatsNumberType", 2); + break; + } +} + +void AddGBCheat::OnSizeType(UINT id) +{ + switch(id) { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("gbCheatsSizeType", 0); + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("gbCheatsSizeType", 1); + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("gbCheatsSizeType", 2); + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBCheatList dialog + + +GBCheatList::GBCheatList(CWnd* pParent /*=NULL*/) + : CDialog(GBCheatList::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBCheatList) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + duringRefresh = false; +} + + +void GBCheatList::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBCheatList) + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBCheatList, CDialog) + //{{AFX_MSG_MAP(GBCheatList) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(IDC_ADD_GG_CHEAT, OnAddGgCheat) + ON_BN_CLICKED(IDC_ADD_GS_CHEAT, OnAddGsCheat) + ON_BN_CLICKED(IDC_ENABLE, OnEnable) + ON_BN_CLICKED(IDC_REMOVE, OnRemove) + ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll) + ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBCheatList message handlers + +void GBCheatList::OnOk() +{ + EndDialog(TRUE); +} + +void GBCheatList::OnAddGgCheat() +{ + CString temp = winResLoadString(IDS_ADD_GG_CODE); + AddGBCode dlg(winGbCheatAddVerifyGg, 11, temp); + dlg.DoModal(); + refresh(); +} + +void GBCheatList::OnAddGsCheat() +{ + CString temp = winResLoadString(IDS_ADD_GS_CODE); + + AddGBCode dlg(winGbCheatAddVerifyGs, 8, temp); + dlg.DoModal(); + refresh(); +} + +void GBCheatList::OnEnable() +{ + int mark = m_list.GetSelectionMark(); + + if(mark != -1) { + LVITEM item; + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if(m_list.GetItem(&item)) { + if(gbCheatList[item.lParam].enabled) + gbCheatDisable(item.lParam); + else + gbCheatEnable(item.lParam); + refresh(); + } + } +} + +void GBCheatList::OnRemove() +{ + int mark = m_list.GetSelectionMark(); + + if(mark != -1) { + LVITEM item; + memset(&item,0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if(m_list.GetItem(&item)) { + gbCheatRemove(item.lParam); + refresh(); + } + } +} + +void GBCheatList::OnRemoveAll() +{ + gbCheatRemoveAll(); + refresh(); +} + +void GBCheatList::OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult) +{ + if(m_list.GetSelectionMark() != -1) { + GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE); + GetDlgItem(IDC_ENABLE)->EnableWindow(TRUE); + } else { + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + } + + if(!duringRefresh) { + LPNMLISTVIEW l = (LPNMLISTVIEW)pNMHDR; + if(l->uChanged & LVIF_STATE) { + if(((l->uOldState & LVIS_STATEIMAGEMASK)>>12) != + (((l->uNewState & LVIS_STATEIMAGEMASK)>>12))) { + if(m_list.GetCheck(l->iItem)) + gbCheatEnable(l->lParam); + else + gbCheatDisable(l->lParam); + refresh(); + } + } + } +} + +BOOL GBCheatList::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_CODE); + m_list.InsertColumn(0, temp, LVCFMT_LEFT, 120, 0); + temp = winResLoadString(IDS_DESCRIPTION); + m_list.InsertColumn(1, temp, LVCFMT_LEFT, 200, 1); + temp = winResLoadString(IDS_STATUS); + m_list.InsertColumn(2, temp, LVCFMT_LEFT, 80, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | + LVS_EX_FULLROWSELECT); + + refresh(); + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBCheatList::refresh() +{ + duringRefresh = true; + + m_list.DeleteAllItems(); + + char buffer[2]; + + for(int i = 0; i < gbCheatNumber; i++) { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = i; + item.state = 0; + item.stateMask = 0; + item.pszText = gbCheatList[i].cheatCode; + m_list.InsertItem(&item); + + m_list.SetCheck(i, (gbCheatList[i].enabled ? TRUE : FALSE)); + + m_list.SetItemText(i, 1, gbCheatList[i].cheatDesc); + + buffer[0] = (gbCheatList[i].enabled) ? 'E' : 'D'; + buffer[1] = 0; + m_list.SetItemText(i, 2, buffer); + } + duringRefresh = false; +} + +///////////////////////////////////////////////////////////////////////////// +// AddGBCode dialog + + +AddGBCode::AddGBCode(bool (*verify)(const char *,const char*), int len, const char *title, CWnd* pParent /*=NULL*/) + : CDialog(AddGBCode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGBCode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + addVerify = verify; + addLength = len; + addTitle = title; +} + + +void AddGBCode::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGBCode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(AddGBCode, CDialog) + //{{AFX_MSG_MAP(AddGBCode) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// AddGBCode message handlers + +void AddGBCode::OnOk() +{ + CString desc; + CString buffer; + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + const char *t = st.next(); + while(t) { + addVerify(t, desc); + t = st.next(); + } + EndDialog(TRUE); +} + +void AddGBCode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddGBCode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + SetWindowText(addTitle); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/GBCheatsDlg.h b/src/win32/GBCheatsDlg.h new file mode 100644 index 00000000..0f3f1d43 --- /dev/null +++ b/src/win32/GBCheatsDlg.h @@ -0,0 +1,217 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_) +#define AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBCheats.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch dialog + +struct WinGbCheatsData { + int bank; + u16 addr; + char address[9]; + char oldValue[12]; + char newValue[12]; +}; + +class GBCheatSearch : public CDialog +{ + // Construction + public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + afx_msg void OnSearchType(UINT id); + afx_msg void OnValueType(UINT id); + void addChanges(bool showMsg); + void addChange(int index, int bank, u16 address, int offset, u32 oldValue, u32 newValue); + int getBank(u16 addr, int j); + GBCheatSearch(CWnd* pParent = NULL); // standard constructor + ~GBCheatSearch(); + + // Dialog Data + //{{AFX_DATA(GBCheatSearch) + enum { IDD = IDD_CHEATS }; + CEdit m_value; + CListCtrl m_list; + int searchType; + int numberType; + int sizeType; + BOOL updateValues; + int valueType; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBCheatSearch) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + WinGbCheatsData *data; + + // Generated message map functions + //{{AFX_MSG(GBCheatSearch) + afx_msg void OnOk(); + afx_msg void OnAddCheat(); + afx_msg void OnSearch(); + afx_msg void OnStart(); + afx_msg void OnUpdate(); + virtual BOOL OnInitDialog(); + afx_msg void OnGetdispinfoCheatList(NMHDR* pNMHDR, LRESULT* pResult); + afx_msg void OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// AddGBCheat dialog + +class AddGBCheat : public CDialog +{ + // Construction + public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + bool addCheat(); + AddGBCheat(u32 addr, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGBCheat) + enum { IDD = IDD_ADD_CHEAT }; + CEdit m_value; + CEdit m_address; + CEdit m_desc; + int sizeType; + int numberType; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGBCheat) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + u32 address; + + // Generated message map functions + //{{AFX_MSG(AddGBCheat) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + ///////////////////////////////////////////////////////////////////////////// +// GBCheatList dialog + +class GBCheatList : public CDialog +{ + // Construction + public: + void refresh(); + bool duringRefresh; + GBCheatList(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBCheatList) + enum { IDD = IDD_GB_CHEAT_LIST }; + CListCtrl m_list; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBCheatList) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBCheatList) + afx_msg void OnOk(); + afx_msg void OnAddGgCheat(); + afx_msg void OnAddGsCheat(); + afx_msg void OnEnable(); + afx_msg void OnRemove(); + afx_msg void OnRemoveAll(); + afx_msg void OnItemchangedCheatList(NMHDR* pNMHDR, LRESULT* pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// +// AddGBCode dialog + +class AddGBCode : public CDialog +{ + // Construction + public: + AddGBCode(bool (*verify)(const char *, const char *),int, const char *, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGBCode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + int addLength; + CString addTitle; + bool (*addVerify)(const char *, const char*); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGBCode) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(AddGBCode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_) diff --git a/src/win32/GBColorDlg.cpp b/src/win32/GBColorDlg.cpp new file mode 100644 index 00000000..a2527aa6 --- /dev/null +++ b/src/win32/GBColorDlg.cpp @@ -0,0 +1,255 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBColorDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GBColorDlg.h" +#include "../System.h" +#include "Reg.h" + +extern int gbPaletteOption; +extern int emulating; +extern int cartridgeType; +extern u16 gbPalette[128]; + +static u16 defaultPalettes[][24] = { + { + 0x7FFF, 0x56B5, 0x318C, 0x0000, 0x7FFF, 0x56B5, 0x318C, 0x0000, + }, + { + 0x6200, 0x7E10, 0x7C10, 0x5000, 0x6200, 0x7E10, 0x7C10, 0x5000, + }, + { + 0x4008, 0x4000, 0x2000, 0x2008, 0x4008, 0x4000, 0x2000, 0x2008, + }, + { + 0x43F0, 0x03E0, 0x4200, 0x2200, 0x43F0, 0x03E0, 0x4200, 0x2200, + }, + { + 0x43FF, 0x03FF, 0x221F, 0x021F, 0x43FF, 0x03FF, 0x221F, 0x021F, + }, + { + 0x621F, 0x7E1F, 0x7C1F, 0x2010, 0x621F, 0x7E1F, 0x7C1F, 0x2010, + }, + { + 0x621F, 0x401F, 0x001F, 0x2010, 0x621F, 0x401F, 0x001F, 0x2010, + }, + { + 0x421F, 0x03E0, 0x7C00, 0x401F, 0x021F, 0x2200, 0x4008, 0x2010, + } +}; + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBColorDlg dialog + + +GBColorDlg::GBColorDlg(CWnd* pParent /*=NULL*/) + : CDialog(GBColorDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBColorDlg) + which = -1; + //}}AFX_DATA_INIT + which = gbPaletteOption; +} + + +void GBColorDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBColorDlg) + DDX_Control(pDX, IDC_PREDEFINED, m_predefined); + DDX_Radio(pDX, IDC_DEFAULT, which); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBColorDlg, CDialog) + //{{AFX_MSG_MAP(GBColorDlg) + ON_BN_CLICKED(IDC_DEFAULT, OnDefault) + ON_BN_CLICKED(IDC_RESET, OnReset) + ON_BN_CLICKED(IDC_USER1, OnUser1) + ON_BN_CLICKED(IDC_USER2, OnUser2) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_CBN_SELCHANGE(IDC_PREDEFINED, OnSelchangePredefined) + //}}AFX_MSG_MAP + ON_CONTROL_RANGE(BN_CLICKED, IDC_COLOR_BG0, IDC_COLOR_OB3, OnColorClicked) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBColorDlg message handlers + +void GBColorDlg::OnDefault() +{ + setWhich(0); +} + +void GBColorDlg::OnReset() +{ + int s = which * 8; + colors[s++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + colors[s++] = (0x15) | (0x15 << 5) | (0x15 << 10); + colors[s++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + colors[s++] = 0; + + colors[s++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + colors[s++] = (0x15) | (0x15 << 5) | (0x15 << 10); + colors[s++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + colors[s] = 0; + setWhich(which); +} + +void GBColorDlg::OnUser1() +{ + setWhich(1); +} + +void GBColorDlg::OnUser2() +{ + setWhich(2); +} + +void GBColorDlg::OnCancel() +{ + EndDialog(FALSE); +} + +void GBColorDlg::OnOk() +{ + EndDialog(TRUE); +} + +BOOL GBColorDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + colorControls[0].SubclassDlgItem(IDC_COLOR_BG0, this); + colorControls[1].SubclassDlgItem(IDC_COLOR_BG1, this); + colorControls[2].SubclassDlgItem(IDC_COLOR_BG2, this); + colorControls[3].SubclassDlgItem(IDC_COLOR_BG3, this); + colorControls[4].SubclassDlgItem(IDC_COLOR_OB0, this); + colorControls[5].SubclassDlgItem(IDC_COLOR_OB1, this); + colorControls[6].SubclassDlgItem(IDC_COLOR_OB2, this); + colorControls[7].SubclassDlgItem(IDC_COLOR_OB3, this); + + for(int i = 0; i < 24; i++) { + colors[i] = systemGbPalette[i]; + } + + const char *names[] = { + "Standard", + "Blue Sea", + "Dark Night", + "Green Forest", + "Hot Desert", + "Pink Dreams", + "Weird Colors" + }; + + for(int j = 0; j < 7; j++) { + int index = m_predefined.AddString(names[j]); + m_predefined.SetItemData(index, j); + } + + RECT cbSize; + int Height; + + m_predefined.GetClientRect(&cbSize); + Height = m_predefined.GetItemHeight(0); + Height += m_predefined.GetItemHeight(0) * (10); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_predefined.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + + setWhich(which); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBColorDlg::setWhich(int w) +{ + which = w; + + for(int i = 0; i < 8; i++) { + colorControls[i].setColor(colors[which*8+i]); + } +} + +u16 * GBColorDlg::getColors() +{ + return colors; +} + +void GBColorDlg::OnColorClicked(UINT id) +{ + id -= IDC_COLOR_BG0; + + u16 color = colors[id]; + + CColorDialog dlg(RGB(color & 0x1f, (color >> 5) & 0x1f, (color >> 10) & 0x1f), + CC_FULLOPEN | CC_ANYCOLOR, this); + if(dlg.DoModal()) { + COLORREF c = dlg.GetColor(); + + colors[which*8+id] = (u16)((c >> 3) & 0x1f | ((c >> 11) & 0x1f) << 5 | + ((c >> 19) & 0x1f) << 10); + colorControls[id].setColor(colors[which*8+id]); + } +} + +int GBColorDlg::getWhich() +{ + return which; +} + + +void GBColorDlg::OnSelchangePredefined() +{ + int sel = m_predefined.GetCurSel(); + + if(sel != -1) { + int data = m_predefined.GetItemData(sel); + for(int i = 0; i < 8; i++) { + colorControls[i].setColor(defaultPalettes[data][i]); + colors[which*8+i] = defaultPalettes[data][i]; + } + } +} diff --git a/src/win32/GBColorDlg.h b/src/win32/GBColorDlg.h new file mode 100644 index 00000000..d19c3082 --- /dev/null +++ b/src/win32/GBColorDlg.h @@ -0,0 +1,81 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_) +#define AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_ + +#include "ColorButton.h" // Added by ClassView +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBColorDlg.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBColorDlg dialog + +class GBColorDlg : public CDialog +{ + // Construction + public: + int getWhich(); + afx_msg void OnColorClicked(UINT id); + u16 * getColors(); + void setWhich(int w); + u16 colors[24]; + ColorButton colorControls[8]; + GBColorDlg(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBColorDlg) + enum { IDD = IDD_GB_COLORS }; + CComboBox m_predefined; + int which; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBColorDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBColorDlg) + afx_msg void OnDefault(); + afx_msg void OnReset(); + afx_msg void OnUser1(); + afx_msg void OnUser2(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + afx_msg void OnSelchangePredefined(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_) diff --git a/src/win32/GBDisassemble.cpp b/src/win32/GBDisassemble.cpp new file mode 100644 index 00000000..6eee4ec7 --- /dev/null +++ b/src/win32/GBDisassemble.cpp @@ -0,0 +1,262 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBDisassemble.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GBDisassemble.h" + +#include "../System.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern gbRegister AF; +extern gbRegister BC; +extern gbRegister DE; +extern gbRegister HL; +extern gbRegister SP; +extern gbRegister PC; +extern u16 IFF; +extern int gbDis(char *, u16); + +///////////////////////////////////////////////////////////////////////////// +// GBDisassemble dialog + + +GBDisassemble::GBDisassemble(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBDisassemble::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBDisassemble) + m_c = FALSE; + m_h = FALSE; + m_n = FALSE; + m_z = FALSE; + //}}AFX_DATA_INIT + address = 0; + autoUpdate = false; + count = 1; + lastAddress = 0; +} + + +void GBDisassemble::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBDisassemble) + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DISASSEMBLE, m_list); + DDX_Check(pDX, IDC_C, m_c); + DDX_Check(pDX, IDC_H, m_h); + DDX_Check(pDX, IDC_N, m_n); + DDX_Check(pDX, IDC_Z, m_z); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBDisassemble, CDialog) + //{{AFX_MSG_MAP(GBDisassemble) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_NEXT, OnNext) + ON_BN_CLICKED(IDC_GO, OnGo) + ON_BN_CLICKED(IDC_GOPC, OnGopc) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_WM_VSCROLL() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBDisassemble message handlers + +void GBDisassemble::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void GBDisassemble::OnRefresh() +{ + refresh(); +} + +void GBDisassemble::OnNext() +{ + gbEmulate(1); + if(PC.W < address || PC.W >= lastAddress) + OnGopc(); + refresh(); +} + +void GBDisassemble::OnGo() +{ + CString buffer; + m_address.GetWindowText(buffer); + sscanf(buffer, "%x", &address); + refresh(); +} + +void GBDisassemble::OnGopc() +{ + address = PC.W; + + refresh(); +} + +void GBDisassemble::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +BOOL GBDisassemble::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_DISASSEMBLE, DS_SizeY) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_NEXT, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_GOPC, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_VSCROLL, DS_SizeY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBDisassembleView", + NULL); + + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; + si.nMin = 0; + si.nMax = 100; + si.nPos = 50; + si.nPage = 0; + GetDlgItem(IDC_VSCROLL)->SetScrollInfo(SB_CTL, &si, TRUE); + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + m_list.SetFont(font); + + for(int i = 0; i < 6; i++) + GetDlgItem(IDC_R0+i)->SetFont(font); + + m_address.LimitText(4); + refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBDisassemble::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + char buffer[80]; + + switch(nSBCode) { + case SB_LINEDOWN: + address += gbDis(buffer, address); + break; + case SB_LINEUP: + address--; + break; + case SB_PAGEDOWN: + address = lastAddress; + break; + case SB_PAGEUP: + address -= count; + break; + } + refresh(); + + CDialog::OnVScroll(nSBCode, nPos, pScrollBar); +} + +void GBDisassemble::refresh() +{ + if(gbRom == NULL) + return; + + int h = m_list.GetItemHeight(0); + RECT r; + m_list.GetClientRect(&r); + count = (r.bottom - r.top+1)/h; + + m_list.ResetContent(); + if(!emulating || theApp.cartridgeType != 1) + return; + + char buffer[80]; + u16 addr = address; + int i; + int sel = -1; + for(i = 0; i < count; i++) { + if(addr == PC.W) + sel = i; + addr += gbDis(buffer, addr); + m_list.InsertString(-1, buffer); + } + lastAddress = addr-1; + if(sel != -1) + m_list.SetCurSel(sel); + + sprintf(buffer, "%04x", AF.W); + GetDlgItem(IDC_R0)->SetWindowText(buffer); + sprintf(buffer, "%04x", BC.W); + GetDlgItem(IDC_R1)->SetWindowText(buffer); + sprintf(buffer, "%04x", DE.W); + GetDlgItem(IDC_R2)->SetWindowText(buffer); + sprintf(buffer, "%04x", HL.W); + GetDlgItem(IDC_R3)->SetWindowText(buffer); + sprintf(buffer, "%04x", SP.W); + GetDlgItem(IDC_R4)->SetWindowText(buffer); + sprintf(buffer, "%04x", PC.W); + GetDlgItem(IDC_R5)->SetWindowText(buffer); + sprintf(buffer, "%04x", IFF); + GetDlgItem(IDC_R6)->SetWindowText(buffer); + + m_z = (AF.B.B0 & 0x80) != 0; + m_n = (AF.B.B0 & 0x40) != 0; + m_h = (AF.B.B0 & 0x20) != 0; + m_c = (AF.B.B0 & 0x10) != 0; + UpdateData(FALSE); +} + +void GBDisassemble::update() +{ + OnGopc(); + refresh(); +} + +void GBDisassemble::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/GBDisassemble.h b/src/win32/GBDisassemble.h new file mode 100644 index 00000000..0a37a6b0 --- /dev/null +++ b/src/win32/GBDisassemble.h @@ -0,0 +1,89 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_) +#define AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBDisassemble.h : header file +// + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// GBDisassemble dialog + +class GBDisassemble : public ResizeDlg, IUpdateListener +{ + // Construction + public: + void refresh(); + u16 lastAddress; + int count; + bool autoUpdate; + u16 address; + GBDisassemble(CWnd* pParent = NULL); // standard constructor + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBDisassemble) + enum { IDD = IDD_GB_DISASSEMBLE }; + CEdit m_address; + CListBox m_list; + BOOL m_c; + BOOL m_h; + BOOL m_n; + BOOL m_z; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBDisassemble) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBDisassemble) + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void OnNext(); + afx_msg void OnGo(); + afx_msg void OnGopc(); + afx_msg void OnAutoUpdate(); + virtual BOOL OnInitDialog(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_) diff --git a/src/win32/GBMapView.cpp b/src/win32/GBMapView.cpp new file mode 100644 index 00000000..c3cd7149 --- /dev/null +++ b/src/win32/GBMapView.cpp @@ -0,0 +1,577 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBMapView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBMapView.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../NLS.h" +#include "../Util.h" +#include "../gb/gbGlobals.h" + +extern "C" { +#include +} + +extern u8 gbInvertTab[256]; + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBMapView dialog + + +GBMapView::GBMapView(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBMapView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBMapView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 1024; + bmpInfo.bmiHeader.biHeight = -1024; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 1024 * 1024); + + mapView.setData(data); + mapView.setBmpInfo(&bmpInfo); + + bg = 0; + bank = 0; +} + + +void GBMapView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBMapView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_MAP_VIEW, mapView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, mapViewZoom); + DDX_Control(pDX, IDC_COLOR, color); +} + + +BEGIN_MESSAGE_MAP(GBMapView, CDialog) + //{{AFX_MSG_MAP(GBMapView) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_BG0, OnBg0) + ON_BN_CLICKED(IDC_BG1, OnBg1) + ON_BN_CLICKED(IDC_BANK_0, OnBank0) + ON_BN_CLICKED(IDC_BANK_1, OnBank1) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBMapView message handlers + +GBMapView::~GBMapView() +{ + free(data); + data = NULL; +} + +void GBMapView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void GBMapView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void GBMapView::OnSave() +{ + CString filename; + + if(theApp.captureFormat == 0) + filename = "map.png"; + else + filename = "map.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + + FileDlg dlg(this, + filename, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + if(dlg.getFilterIndex() == 2) + saveBMP(dlg.GetPathName()); + else + savePNG(dlg.GetPathName()); +} + +void GBMapView::render() +{ + u8 * bank0; + u8 * bank1; + if(gbCgbMode) { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } else { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int tile_map_address = 0x1800; + if(bg == 1) + tile_map_address = 0x1c00; + + int tile_pattern = 0x0000; + if(bank == 1) + tile_pattern = 0x0800; + + w = 256; + h = 256; + + int tile = 0; + for(int y = 0; y < 32; y++) { + for(int x = 0; x < 32; x++) { + u8 *bmp = &data[y * 8 * 32 * 24 + x*24]; + u8 attrs = 0; + if(bank1 != NULL) + attrs = bank1[tile_map_address]; + u8 tile = bank0[tile_map_address]; + tile_map_address++; + + if(bank == 1) { + if(tile < 128) tile += 128; + else tile -= 128; + } + for(int j = 0; j < 8; j++) { + int tile_pattern_address = attrs & 0x40 ? + tile_pattern + tile*16 + (7-j)*2: + tile_pattern + tile*16+j*2; + + u8 tile_a = 0; + u8 tile_b = 0; + + if(attrs & 0x08) { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } else { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if(attrs & 0x20) { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + u8 mask = 0x80; + + while(mask > 0) { + u8 c = (tile_a & mask) ? 1 : 0; + c += (tile_b & mask) ? 2 : 0; + + if(gbCgbMode) + c = c + (attrs & 7)*4; + + u16 color = gbPalette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + mask >>= 1; + } + bmp += 31*24; + } + } + } +} + +void GBMapView::paint() +{ + if(gbRom == NULL) + return; + render(); + + SIZE s; + if(mapView.getStretch()) { + mapView.setSize(w, h); + s.cx = s.cy = 1; + mapView.SetScrollSizes(MM_TEXT, s); + } else { + mapView.setSize(w, h); + s.cx = w; + s.cy = h; + mapView.SetScrollSizes(MM_TEXT, s); + } + + mapView.refresh(); +} + +void GBMapView::OnRefresh() +{ + paint(); +} + +void GBMapView::update() +{ + paint(); +} + +BOOL GBMapView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_MAP_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMapView", + NULL); + + int s = regQueryDwordValue("mapViewStretch", 0); + if(s) + mapView.setStretch(true); + ((CButton *)GetDlgItem(IDC_STRETCH))->SetCheck(s); + + UINT id = IDC_BANK_0; + if(bank == 1) + id = IDC_BANK_1; + CheckRadioButton(IDC_BANK_0, IDC_BANK_1, id); + id = IDC_BG0; + if(bg == 1) + id = IDC_BG1; + CheckRadioButton(IDC_BG0, IDC_BG1, id); + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBMapView::OnBg0() +{ + bg = 0; + paint(); +} + +void GBMapView::OnBg1() +{ + bg = 1; + paint(); +} + +void GBMapView::OnBank0() +{ + bank = 0; + paint(); +} + +void GBMapView::OnBank1() +{ + bank = 1; + paint(); +} + +void GBMapView::OnStretch() +{ + mapView.setStretch(!mapView.getStretch()); + paint(); + regSetDwordValue("mapViewStretch", mapView.getStretch()); +} + +void GBMapView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void GBMapView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +u32 GBMapView::GetClickAddress(int x, int y) +{ + u32 base = 0x9800; + if(bg == 1) + base = 0x9c00; + + return base + (y >> 3)*32 + (x >> 3); +} + +LRESULT GBMapView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + mapViewZoom.setColors(colors); + + int x = wParam & 0xffff; + int y = (wParam >> 16); + + CString buffer; + buffer.Format("(%d,%d)", x, y); + GetDlgItem(IDC_XY)->SetWindowText(buffer); + + u32 address = GetClickAddress(x,y); + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + u8 attrs = 0; + + u8 tile = gbMemoryMap[9][address & 0xfff]; + if(gbCgbMode) { + attrs = gbVram[0x2000 + address - 0x8000]; + tile = gbVram[address & 0x1fff]; + } + + if(bank == 1) { + if(tile > 128) tile -= 128; + else tile += 128; + } + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUM)->SetWindowText(buffer); + + buffer.Empty(); + buffer += attrs & 0x20 ? 'H' : '-'; + buffer += attrs & 0x40 ? 'V' : '-'; + GetDlgItem(IDC_FLIP)->SetWindowText(buffer); + + if(gbCgbMode) { + buffer.Format("%d", (attrs & 7)); + } else + buffer = "---"; + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText(buffer); + + buffer.Empty(); + if(gbCgbMode) + buffer += attrs & 0x80 ? 'P' : '-'; + else + buffer += '-'; + GetDlgItem(IDC_PRIORITY)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT GBMapView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void GBMapView::PostNcDestroy() +{ + delete this; + CDialog::PostNcDestroy(); +} diff --git a/src/win32/GBMapView.h b/src/win32/GBMapView.h new file mode 100644 index 00000000..2b2b006a --- /dev/null +++ b/src/win32/GBMapView.h @@ -0,0 +1,102 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_) +#define AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBMapView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "ZoomControl.h" +#include "ResizeDlg.h" +#include "IUpdate.h" +#include "..\System.h" // Added by ClassView + +///////////////////////////////////////////////////////////////////////////// +// GBMapView dialog + +class GBMapView : public ResizeDlg, IUpdateListener +{ + private: + BITMAPINFO bmpInfo; + u8 *data; + int bank; + int bg; + int w; + int h; + BitmapControl mapView; + ZoomControl mapViewZoom; + ColorControl color; + bool autoUpdate; + // Construction + public: + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + u32 GetClickAddress(int x, int y); + void update(); + void paint(); + void render(); + void savePNG(const char *name); + void saveBMP(const char *name); + ~GBMapView(); + GBMapView(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBMapView) + enum { IDD = IDD_GB_MAP_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBMapView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBMapView) + afx_msg void OnSave(); + afx_msg void OnRefresh(); + virtual BOOL OnInitDialog(); + afx_msg void OnBg0(); + afx_msg void OnBg1(); + afx_msg void OnBank0(); + afx_msg void OnBank1(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_) diff --git a/src/win32/GBMemoryViewerDlg.cpp b/src/win32/GBMemoryViewerDlg.cpp new file mode 100644 index 00000000..a5d35697 --- /dev/null +++ b/src/win32/GBMemoryViewerDlg.cpp @@ -0,0 +1,421 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBMemoryViewerDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBMemoryViewerDlg.h" +#include "MemoryViewerAddressSize.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../gb/gbGlobals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +GBMemoryViewer::GBMemoryViewer() + : MemoryViewer() +{ + setAddressSize(1); +} + +void GBMemoryViewer::readData(u32 address, int len, u8 *data) +{ + u16 addr = address & 0xffff; + if(emulating && gbRom != NULL) { + for(int i = 0; i < len; i++) { + *data++ = gbMemoryMap[addr >> 12][addr & 0xfff]; + addr++; + } + } else { + for(int i = 0; i < len; i++) { + *data++ = 0; + addr++; + } + } +} + +#define GB_READBYTE_QUICK(addr) \ + gbMemoryMap[(addr) >> 12][(addr) & 0xfff] + +#define GB_WRITEBYTE_QUICK(addr,v) \ + gbMemoryMap[(addr) >> 12][(addr) & 0xfff] = (v) + +void GBMemoryViewer::editData(u32 address, int size, int mask, u32 value) +{ + u32 oldValue; + u16 addr = (u16)address & 0xffff; + switch(size) { + case 8: + oldValue = GB_READBYTE_QUICK(addr); + oldValue &= mask; + oldValue |= (u8)value; + GB_WRITEBYTE_QUICK(addr, oldValue); + break; + case 16: + oldValue = GB_READBYTE_QUICK(addr) | + (GB_READBYTE_QUICK(addr + 1) << 8); + oldValue &= mask; + oldValue |= (u16)value; + GB_WRITEBYTE_QUICK(addr, (oldValue & 255)); + GB_WRITEBYTE_QUICK(addr+1, (oldValue >> 8)); + break; + case 32: + oldValue = GB_READBYTE_QUICK(addr) | + (GB_READBYTE_QUICK(addr + 1) << 8) | + (GB_READBYTE_QUICK(addr + 2) << 16) | + (GB_READBYTE_QUICK(addr + 3) << 24); + oldValue &= mask; + oldValue |= (u32)value; + GB_WRITEBYTE_QUICK(addr, (oldValue & 255)); + GB_WRITEBYTE_QUICK(addr+1, (oldValue >> 8)); + GB_WRITEBYTE_QUICK(addr+2, (oldValue >> 16)); + GB_WRITEBYTE_QUICK(addr+3, (oldValue >> 24)); + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg dialog + + +GBMemoryViewerDlg::GBMemoryViewerDlg(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBMemoryViewerDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBMemoryViewerDlg) + m_size = -1; + //}}AFX_DATA_INIT + autoUpdate = false; +} + + +void GBMemoryViewerDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBMemoryViewerDlg) + DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_ADDRESSES, m_addresses); + DDX_Radio(pDX, IDC_8_BIT, m_size); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_VIEWER, m_viewer); +} + + +BEGIN_MESSAGE_MAP(GBMemoryViewerDlg, CDialog) + //{{AFX_MSG_MAP(GBMemoryViewerDlg) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_8_BIT, On8Bit) + ON_BN_CLICKED(IDC_16_BIT, On16Bit) + ON_BN_CLICKED(IDC_32_BIT, On32Bit) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_GO, OnGo) + ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_LOAD, OnLoad) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg message handlers + +BOOL GBMemoryViewerDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_VIEWER, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_LOAD, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMemoryView", + NULL); + + m_viewer.setDialog(this); + m_viewer.ShowScrollBar(SB_VERT, TRUE); + m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH); + + LPCTSTR s[] = { + "0x0000 - ROM", + "0x4000 - ROM", + "0x8000 - VRAM", + "0xA000 - SRAM", + "0xC000 - RAM", + "0xD000 - WRAM", + "0xFF00 - I/O", + "0xFF80 - RAM" + }; + + for(int i = 0; i < 8; i++) + m_addresses.AddString(s[i]); + + m_addresses.SetCurSel(0); + + RECT cbSize; + int Height; + + m_addresses.GetClientRect(&cbSize); + Height = m_addresses.GetItemHeight(-1); + Height += m_addresses.GetItemHeight(0) * (9); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_addresses.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.LimitText(8); + + m_size = regQueryDwordValue("memViewerDataSize", 0); + if(m_size < 0 || m_size > 2) + m_size = 0; + m_viewer.setSize(m_size); + UpdateData(FALSE); + + m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBMemoryViewerDlg::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void GBMemoryViewerDlg::OnRefresh() +{ + m_viewer.Invalidate(); +} + +void GBMemoryViewerDlg::update() +{ + OnRefresh(); +} + +void GBMemoryViewerDlg::On8Bit() +{ + m_viewer.setSize(0); + regSetDwordValue("memViewerDataSize", 0); +} + +void GBMemoryViewerDlg::On16Bit() +{ + m_viewer.setSize(1); + regSetDwordValue("memViewerDataSize", 1); +} + +void GBMemoryViewerDlg::On32Bit() +{ + m_viewer.setSize(2); + regSetDwordValue("memViewerDataSize", 2); +} + +void GBMemoryViewerDlg::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void GBMemoryViewerDlg::OnGo() +{ + CString buffer; + + m_address.GetWindowText(buffer); + + u32 address; + sscanf(buffer, "%x", &address); + if(m_viewer.getSize() == 1) + address &= ~1; + else if(m_viewer.getSize() == 2) + address &= ~3; + m_viewer.setAddress(address); +} + +void GBMemoryViewerDlg::OnSelchangeAddresses() +{ + int cur = m_addresses.GetCurSel(); + + switch(cur) { + case 0: + m_viewer.setAddress(0x0000); + break; + case 1: + m_viewer.setAddress(0x4000); + break; + case 2: + m_viewer.setAddress(0x8000); + break; + case 3: + m_viewer.setAddress(0xa000); + break; + case 4: + m_viewer.setAddress(0xc000); + break; + case 5: + m_viewer.setAddress(0xd000); + break; + case 6: + m_viewer.setAddress(0xff00); + break; + case 7: + m_viewer.setAddress(0xff80); + break; + } +} + +void GBMemoryViewerDlg::setCurrentAddress(u32 address) +{ + CString buffer; + + buffer.Format("0x%08X", address); + m_current.SetWindowText(buffer); +} + +void GBMemoryViewerDlg::OnSave() +{ + MemoryViewerAddressSize dlg; + CString buffer; + + dlg.setAddress(m_viewer.getCurrentAddress()); + + LPCTSTR exts[] = { ".dmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + if(dlg.DoModal() == IDOK) { + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + true); + if(file.DoModal() == IDOK) { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "wb"); + + if(f == NULL) { + systemMessage(IDS_ERROR_CREATING_FILE, buffer); + return; + } + + int size = dlg.getSize(); + u16 addr = dlg.getAddress() & 0xffff; + + for(int i = 0; i < size; i++) { + fputc(gbMemoryMap[addr >> 12][addr & 0xfff], f); + addr++; + } + + fclose(f); + } + } +} + +void GBMemoryViewerDlg::OnLoad() +{ + CString buffer; + LPCTSTR exts[] = { ".dmp" }; + CString filter = theApp.winLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + false); + + if(file.DoModal() == IDOK) { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "rb"); + if(f == NULL) { + systemMessage(IDS_CANNOT_OPEN_FILE, + "Cannot open file %s", + buffer); + return; + } + + MemoryViewerAddressSize dlg; + + fseek(f, 0, SEEK_END); + int size = ftell(f); + + fseek(f, 0, SEEK_SET); + + dlg.setAddress(m_viewer.getCurrentAddress()); + dlg.setSize(size); + + if(dlg.DoModal() == IDOK) { + int size = dlg.getSize(); + u16 addr = dlg.getAddress() & 0xffff; + + for(int i = 0; i < size; i++) { + int c = fgetc(f); + if(c == -1) + break; + gbMemoryMap[addr >> 12][addr & 0xfff] = c; + addr++; + } + OnRefresh(); + } + fclose(f); + } +} + +void GBMemoryViewerDlg::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/GBMemoryViewerDlg.h b/src/win32/GBMemoryViewerDlg.h new file mode 100644 index 00000000..60843858 --- /dev/null +++ b/src/win32/GBMemoryViewerDlg.h @@ -0,0 +1,94 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_) +#define AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBMemoryViewerDlg.h : header file +// +#include "MemoryViewer.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +class GBMemoryViewer : public MemoryViewer { + public: + GBMemoryViewer(); + virtual void readData(u32, int, u8 *); + virtual void editData(u32, int, int, u32); +}; + +///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg dialog + +class GBMemoryViewerDlg : public ResizeDlg, IUpdateListener, IMemoryViewerDlg +{ + GBMemoryViewer m_viewer; + bool autoUpdate; + // Construction + public: + void setCurrentAddress(u32 address); + GBMemoryViewerDlg(CWnd* pParent = NULL); // standard constructor + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBMemoryViewerDlg) + enum { IDD = IDD_MEM_VIEWER }; + CEdit m_current; + CEdit m_address; + CComboBox m_addresses; + int m_size; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBMemoryViewerDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBMemoryViewerDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void On8Bit(); + afx_msg void On16Bit(); + afx_msg void On32Bit(); + afx_msg void OnAutoUpdate(); + afx_msg void OnGo(); + afx_msg void OnSelchangeAddresses(); + afx_msg void OnSave(); + afx_msg void OnLoad(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_) diff --git a/src/win32/GBOamView.cpp b/src/win32/GBOamView.cpp new file mode 100644 index 00000000..7963f0b0 --- /dev/null +++ b/src/win32/GBOamView.cpp @@ -0,0 +1,599 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBOamView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBOamView.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../NLS.h" +#include "../Util.h" +#include "../gb/gbGlobals.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBOamView dialog + + +GBOamView::GBOamView(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBOamView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBOamView) + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 8; + bmpInfo.bmiHeader.biHeight = 16; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 8 * 16); + + oamView.setData(data); + oamView.setBmpInfo(&bmpInfo); + + number = 0; +} + + +void GBOamView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBOamView) + DDX_Control(pDX, IDC_SPRITE, m_sprite); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_COLOR, color); + DDX_Control(pDX, IDC_OAM_VIEW, oamView); + DDX_Control(pDX, IDC_OAM_VIEW_ZOOM, oamZoom); +} + + +BEGIN_MESSAGE_MAP(GBOamView, CDialog) + //{{AFX_MSG_MAP(GBOamView) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_EN_CHANGE(IDC_SPRITE, OnChangeSprite) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_WM_HSCROLL() + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBOamView message handlers + +GBOamView::~GBOamView() +{ + free(data); + data = NULL; +} + + +void GBOamView::paint() +{ + if(gbRom == NULL) + return; + + render(); + oamView.setSize(w,h); + oamView.refresh(); +} + +void GBOamView::update() +{ + paint(); +} + + +void GBOamView::setAttributes(int y, int x, int tile, int flags) +{ + CString buffer; + + int flipH = flags & 0x20; + int flipV = flags & 0x40; + int prio = (flags & 0x80) >> 7; + int pal = flags & 0x7; + int oap = (flags & 0x08) >> 3; + int bank = (flags & 0x10) >> 4; + + buffer.Format("%d,%d", x,y); + GetDlgItem(IDC_POS)->SetWindowText(buffer); + + buffer.Format("%d", pal); + GetDlgItem(IDC_PALETTE)->SetWindowText(buffer); + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE)->SetWindowText(buffer); + + buffer.Format("%d", prio); + GetDlgItem(IDC_PRIO)->SetWindowText(buffer); + + buffer.Format("%d", bank); + GetDlgItem(IDC_BANK)->SetWindowText(buffer); + + buffer.Empty(); + if(flipH) + buffer += 'H'; + else + buffer += ' '; + if(flipV) + buffer += 'V'; + else + buffer += ' '; + GetDlgItem(IDC_FLAGS)->SetWindowText(buffer); + + buffer.Format("%d", oap); + GetDlgItem(IDC_OAP)->SetWindowText(buffer); +} + +void GBOamView::render() +{ + int m=0; + if(gbRom == NULL) + return; + + u16 addr = number * 4 + 0xfe00; + + int size = register_LCDC & 4; + + u8 y = gbMemory[addr++]; + u8 x = gbMemory[addr++]; + u8 tile = gbMemory[addr++]; + if(size) + tile &= 254; + u8 flags = gbMemory[addr++]; + + u8 *bmp = data; + + w = 8; + h = size ? 16 : 8; + + setAttributes(y, x, tile, flags); + + u8 * bank0; + u8 * bank1; + if(gbCgbMode) { + if(register_VBK & 1) { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } else { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + } else { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int init = 0x0000; + + u8 *pal = gbObp0; + + if((flags & 0x10)) + pal = gbObp1; + + for(int yy = 0; yy < h; yy++) { + int address = init + tile * 16 + 2*yy; + int a = 0; + int b = 0; + + if(gbCgbMode && flags & 0x08) { + a = bank1[address++]; + b = bank1[address++]; + } else { + a = bank0[address++]; + b = bank0[address++]; + } + + for(int xx = 0; xx < 8; xx++) { + u8 mask = 1 << (7-xx); + u8 c = 0; + if( (a & mask)) + c++; + if( (b & mask)) + c+=2; + + // make sure that sprites will work even in CGB mode + if(gbCgbMode) { + c = c + (flags & 0x07)*4 + 32; + } else { + c = pal[c]; + } + + u16 color = gbPalette[c]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } +} + +void GBOamView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + +void GBOamView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + + +void GBOamView::save() +{ + CString captureBuffer; + + if(theApp.captureFormat == 0) + captureBuffer = "oam.png"; + else + captureBuffer = "oam.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + captureBuffer = dlg.GetPathName(); + + if(dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +BOOL GBOamView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_OAM_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_OAM_VIEW_ZOOM, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBOamView", + NULL); + m_sprite.SetWindowText("0"); + + updateScrollInfo(); + + m_stretch = regQueryDwordValue("GBOamViewStretch", 0); + if(m_stretch) + oamView.setStretch(true); + UpdateData(FALSE); + + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBOamView::OnStretch() +{ + oamView.setStretch(!oamView.getStretch()); + paint(); + regSetDwordValue("GBOamViewStretch", oamView.getStretch()); +} + +void GBOamView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + + +void GBOamView::OnChangeSprite() +{ + CString buffer; + m_sprite.GetWindowText(buffer); + int n = atoi(buffer); + if(n < 0 || n > 39) { + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + return; + } + number = n; + paint(); + updateScrollInfo(); +} + +void GBOamView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT GBOamView::OnMapInfo(WPARAM, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + oamZoom.setColors(colors); + + return TRUE; +} + +LRESULT GBOamView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + + +void GBOamView::updateScrollInfo() +{ + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + si.nMax = 39; + si.nPage = 1; + si.nPos = number; + GetDlgItem(IDC_SCROLLBAR)->SetScrollInfo(SB_CTL, + &si, + TRUE); +} + + +void GBOamView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + switch(nSBCode) { + case SB_BOTTOM: + number = 39; + break; + case SB_LINEDOWN: + number++; + if(number > 39) + number = 39; + break; + case SB_LINEUP: + number--; + if(number < 0) + number = 0; + break; + case SB_PAGEDOWN: + number += 16; + if(number > 39) + number = 39; + break; + case SB_PAGEUP: + number -= 16; + if(number < 0) + number = 0; + break; + case SB_TOP: + number = 0; + break; + case SB_THUMBTRACK: + number = nPos; + if(number < 0) + number = 0; + if(number > 39) + number = 39; + break; + } + + updateScrollInfo(); + + CString buffer; + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + paint(); +} + +void GBOamView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/GBOamView.h b/src/win32/GBOamView.h new file mode 100644 index 00000000..663ade01 --- /dev/null +++ b/src/win32/GBOamView.h @@ -0,0 +1,103 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_) +#define AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBOamView.h : header file +// + +#include "BitmapControl.h" +#include "ZoomControl.h" +#include "ColorControl.h" + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// GBOamView dialog + +class GBOamView : public ResizeDlg, IUpdateListener +{ + private: + BITMAPINFO bmpInfo; + u8 *data; + int w; + int h; + int number; + bool autoUpdate; + BitmapControl oamView; + ZoomControl oamZoom; + ColorControl color; + + + // Construction + public: + void updateScrollInfo(); + void save(); + void savePNG(const char *name); + void saveBMP(const char *name); + void render(); + void setAttributes(int y, int x, int tile, int flags); + void paint(); + ~GBOamView(); + GBOamView(CWnd* pParent = NULL); // standard constructor + + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBOamView) + enum { IDD = IDD_GB_OAM_VIEW }; + CEdit m_sprite; + BOOL m_stretch; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBOamView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBOamView) + virtual BOOL OnInitDialog(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnChangeSprite(); + afx_msg void OnClose(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_) diff --git a/src/win32/GBPaletteView.cpp b/src/win32/GBPaletteView.cpp new file mode 100644 index 00000000..fa9e57f2 --- /dev/null +++ b/src/win32/GBPaletteView.cpp @@ -0,0 +1,245 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBPaletteView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBPaletteView.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../gb/gbGlobals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +void GBPaletteViewControl::updatePalette() +{ + if(gbRom) { + memcpy(palette, &gbPalette[paletteAddress], 64); + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBPaletteView dialog + + +GBPaletteView::GBPaletteView(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBPaletteView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBPaletteView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; +} + +GBPaletteView::~GBPaletteView() +{ +} + +void GBPaletteView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBPaletteView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_PALETTE_VIEW, paletteView); + DDX_Control(pDX, IDC_PALETTE_VIEW_OBJ, paletteViewOBJ); + DDX_Control(pDX, IDC_COLOR, colorControl); +} + + +BEGIN_MESSAGE_MAP(GBPaletteView, CDialog) + //{{AFX_MSG_MAP(GBPaletteView) + ON_BN_CLICKED(IDC_SAVE_BG, OnSaveBg) + ON_BN_CLICKED(IDC_SAVE_OBJ, OnSaveObj) + ON_BN_CLICKED(IDC_REFRESH2, OnRefresh2) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + //}}AFX_MSG_MAP + ON_MESSAGE(WM_PALINFO, OnPalInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBPaletteView message handlers + +BOOL GBPaletteView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_END() + SetData(sz, + FALSE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBPaletteView", + NULL); + + paletteView.init(32, 64, 128); + paletteViewOBJ.init(32, 64, 128); + + paletteView.setPaletteAddress(0); + paletteView.refresh(); + + paletteViewOBJ.setPaletteAddress(32); + paletteViewOBJ.refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBPaletteView::save(int which) +{ + CString captureBuffer; + + if(which == 0) + captureBuffer = "bg.pal"; + else + captureBuffer = "obj.pal"; + + LPCTSTR exts[] = {".pal", ".pal", ".act" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PAL); + CString title = winResLoadString(IDS_SELECT_PALETTE_NAME); + FileDlg dlg(this, + captureBuffer, + filter, + 1, + "PAL", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = dlg.GetPathName(); + + PaletteViewControl *p = NULL; + + if(which == 0) + p = &paletteView; + else + p = &paletteViewOBJ; + + switch(dlg.getFilterIndex()) { + case 0: + case 1: + p->saveMSPAL(captureBuffer); + break; + case 2: + p->saveJASCPAL(captureBuffer); + break; + case 3: + p->saveAdobe(captureBuffer); + break; + } +} + + +void GBPaletteView::OnSaveBg() +{ + save(0); +} + +void GBPaletteView::OnSaveObj() +{ + save(1); +} + +void GBPaletteView::OnRefresh2() +{ + paletteView.refresh(); + paletteViewOBJ.refresh(); +} + +void GBPaletteView::update() +{ + OnRefresh2(); +} + + +void GBPaletteView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + + +void GBPaletteView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT GBPaletteView::OnPalInfo(WPARAM wParam, LPARAM lParam) +{ + u16 color = (u16)wParam; + u32 address = (u32)lParam; + CString buffer; + + bool isOBJ = address >= 32; + address &= 31; + + buffer.Format("%d", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int r = (color & 0x1f); + int g = (color & 0x3e0) >> 5; + int b = (color & 0x7c00) >> 10; + + buffer.Format("%d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("%d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("%d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + + buffer.Format("0x%04X", color); + GetDlgItem(IDC_VALUE)->SetWindowText(buffer); + + colorControl.setColor(color); + + if(isOBJ) + paletteView.setSelected(-1); + else + paletteViewOBJ.setSelected(-1); + + return TRUE; +} + +void GBPaletteView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/GBPaletteView.h b/src/win32/GBPaletteView.h new file mode 100644 index 00000000..0d2d71f0 --- /dev/null +++ b/src/win32/GBPaletteView.h @@ -0,0 +1,90 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_) +#define AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBPaletteView.h : header file +// + +#include "ColorControl.h" +#include "IUpdate.h" +#include "PaletteViewControl.h" +#include "ResizeDlg.h" + +class GBPaletteViewControl : public PaletteViewControl { + public: + virtual void updatePalette(); +}; + +///////////////////////////////////////////////////////////////////////////// +// GBPaletteView dialog + +class GBPaletteView : public ResizeDlg, IUpdateListener +{ + private: + GBPaletteViewControl paletteView; + GBPaletteViewControl paletteViewOBJ; + ColorControl colorControl; + bool autoUpdate; + // Construction + public: + void save(int which); + GBPaletteView(CWnd* pParent = NULL); // standard constructor + virtual ~GBPaletteView(); + + // Dialog Data + //{{AFX_DATA(GBPaletteView) + enum { IDD = IDD_GB_PALETTE_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBPaletteView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + virtual void update(); + + // Implementation + protected: + afx_msg virtual LRESULT OnPalInfo(WPARAM wParam, LPARAM lParam); + // Generated message map functions + //{{AFX_MSG(GBPaletteView) + virtual BOOL OnInitDialog(); + afx_msg void OnSaveBg(); + afx_msg void OnSaveObj(); + afx_msg void OnRefresh2(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_) diff --git a/src/win32/GBPrinterDlg.cpp b/src/win32/GBPrinterDlg.cpp new file mode 100644 index 00000000..9f28fdcc --- /dev/null +++ b/src/win32/GBPrinterDlg.cpp @@ -0,0 +1,494 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBPrinter.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBPrinterDlg.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../NLS.h" +#include "../Util.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBPrinter dialog + + +GBPrinterDlg::GBPrinterDlg(CWnd* pParent /*=NULL*/) + : CDialog(GBPrinterDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBPrinterDlg) + m_scale = -1; + //}}AFX_DATA_INIT + bitmap = (BITMAPINFO *)bitmapHeader; + + bitmap->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bitmap->bmiHeader.biWidth = 160; + bitmap->bmiHeader.biHeight = -144; + bitmap->bmiHeader.biPlanes = 1; + bitmap->bmiHeader.biBitCount = 8; + bitmap->bmiHeader.biCompression = BI_RGB; + bitmap->bmiHeader.biSizeImage = 160*144; + bitmap->bmiHeader.biXPelsPerMeter = 0; + bitmap->bmiHeader.biYPelsPerMeter = 0; + bitmap->bmiHeader.biClrUsed = 4; + bitmap->bmiHeader.biClrImportant = 4; + bitmap->bmiColors[0].rgbBlue = + bitmap->bmiColors[0].rgbGreen = + bitmap->bmiColors[0].rgbRed = + 255; + bitmap->bmiColors[0].rgbReserved = 0; + bitmap->bmiColors[1].rgbBlue = + bitmap->bmiColors[1].rgbGreen = + bitmap->bmiColors[1].rgbRed = + 168; + bitmap->bmiColors[1].rgbReserved = 0; + bitmap->bmiColors[2].rgbBlue = + bitmap->bmiColors[2].rgbGreen = + bitmap->bmiColors[2].rgbRed = + 96; + bitmap->bmiColors[2].rgbReserved = 0; + bitmap->bmiColors[3].rgbBlue = + bitmap->bmiColors[3].rgbGreen = + bitmap->bmiColors[3].rgbRed = + 0; + bitmap->bmiColors[3].rgbReserved = 0; +} + + +void GBPrinterDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBPrinterDlg) + DDX_Radio(pDX, IDC_1X, m_scale); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GBPrinterDlg, CDialog) + //{{AFX_MSG_MAP(GBPrinterDlg) + ON_BN_CLICKED(ID_SAVE, OnSave) + ON_BN_CLICKED(ID_PRINT, OnPrint) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(IDC_1X, On1x) + ON_BN_CLICKED(IDC_2X, On2x) + ON_BN_CLICKED(IDC_3X, On3x) + ON_BN_CLICKED(IDC_4X, On4x) + ON_WM_PAINT() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBPrinter message handlers + +void GBPrinterDlg::saveAsBMP(const char *name) +{ + u8 writeBuffer[512 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + 160*144*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, 160); + utilPutDword(bmpheader.height, 144); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 160*144); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + u8 *data = (u8 *)bitmapData; + u8 *p = data + (160*143); + for(int y = 0; y < 144; y++) { + for(int x = 0; x < 160; x++) { + u8 c = *p++; + + *b++ = bitmap->bmiColors[c].rgbBlue; + *b++ = bitmap->bmiColors[c].rgbGreen; + *b++ = bitmap->bmiColors[c].rgbRed; + } + p -= 2*(160); + fwrite(writeBuffer, 1, 3*160, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + +void GBPrinterDlg::saveAsPNG(const char *name) +{ + u8 writeBuffer[160 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + 160, + 144, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = 160; + int sizeY = 144; + + u8 *pixU8 = (u8 *)bitmapData; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u8 c = *pixU8++; + *b++ = bitmap->bmiColors[c].rgbRed; + *b++ = bitmap->bmiColors[c].rgbGreen; + *b++ = bitmap->bmiColors[c].rgbBlue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + + +void GBPrinterDlg::OnSave() +{ + CString captureBuffer; + + if(theApp.captureFormat == 0) + captureBuffer = "printer.png"; + else + captureBuffer = "printer.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = dlg.GetPathName(); + + if(dlg.getFilterIndex() == 2) + saveAsBMP(captureBuffer); + else + saveAsPNG(captureBuffer); +} + +void GBPrinterDlg::OnPrint() +{ + CPrintDialog dlg(FALSE); + + dlg.m_pd.nFromPage = 1; + dlg.m_pd.nToPage = 1; + dlg.m_pd.nMinPage = 1; + dlg.m_pd.nMaxPage = 1; + dlg.m_pd.nCopies = 1; + + if(dlg.DoModal() == IDOK) { + DOCINFO di; + float fLogPelsX1 = 0; + float fLogPelsX2 = 0; + float fLogPelsY1 = 0; + float fLogPelsY2 = 0; + float fScaleX = 0, fScaleY = 0; + CDC *winDC = NULL; + memset(&di, 0, sizeof(di)); + di.cbSize = sizeof(DOCINFO); + CString docName = winResLoadString(IDS_POCKET_PRINTER); + di.lpszDocName = docName; + CDC dc; + dc.Attach(dlg.GetPrinterDC()); + int nError = dc.StartDoc(&di); + + if(nError == SP_ERROR) { + systemMessage(IDS_ERROR_ON_STARTDOC,"Error on StartDoc"); + goto error; + } + nError = dc.StartPage(); + if(nError <= 0) { + systemMessage(IDS_ERROR_ON_STARTPAGE, "Error on StartPage"); + goto error; + } + + winDC = GetDC(); + fLogPelsX1 = (float)winDC->GetDeviceCaps(LOGPIXELSX); + fLogPelsY1 = (float)winDC->GetDeviceCaps(LOGPIXELSY); + ReleaseDC(winDC); + + fLogPelsX2 = (float)dc.GetDeviceCaps(LOGPIXELSX); + fLogPelsY2 = (float)dc.GetDeviceCaps(LOGPIXELSY); + + if(fLogPelsX1 > fLogPelsX2) + fScaleX = fLogPelsX1 / fLogPelsX2; + else + fScaleX = fLogPelsX2 / fLogPelsX1; + + if(fLogPelsY1 > fLogPelsY2) + fScaleY = fLogPelsY1 / fLogPelsY2; + else + fScaleY = fLogPelsY2 / fLogPelsY1; + + fScaleX *= (scale+1); + fScaleY *= (scale+1); + + if(StretchDIBits(dc, + 0, + 0, + (int)((float)160*fScaleX), + (int)((float)144*fScaleY), + 0, + 0, + 160, + 144, + bitmapData, + bitmap, + DIB_RGB_COLORS, + SRCCOPY) == GDI_ERROR) { + systemMessage(IDS_ERROR_PRINTING_ON_STRETCH, + "Error printing on StretchDIBits"); + } + + nError = dc.EndPage(); + + if(nError <= 0) { + systemMessage(IDS_ERROR_ON_ENDPAGE, "Error on EndPage"); + goto error; + } + + nError = dc.EndDoc(); + + if(nError <= 0) + systemMessage(IDS_ERROR_ON_ENDDOC, "Error on EndDoc"); + error: + dc.DeleteDC(); + } +} + +void GBPrinterDlg::processData(u8 *data) +{ + for(int y = 0; y < 0x12; y++) { + for(int x = 0; x < 0x14; x++) { + for(int k = 0; k < 8; k++) { + int a = *data++; + int b = *data++; + for(int j = 0; j < 8; j++) { + int mask = 1 << (7-j); + int c = 0; + if(a & mask) + c++; + if(b & mask) + c+=2; + bitmapData[x*8+j + 160*(y*8+k)] = c; + } + } + } + } +} + + +BOOL GBPrinterDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + scale = regQueryDwordValue("printerScale", 0); + if(scale < 0 || scale > 3) + scale = 0; + m_scale = scale; + UpdateData(FALSE); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBPrinterDlg::OnOk() +{ + EndDialog(TRUE); +} + +void GBPrinterDlg::On1x() +{ + regSetDwordValue("printerScale", 0); + scale = 0; +} + +void GBPrinterDlg::On2x() +{ + regSetDwordValue("printerScale", 1); + scale = 1; +} + +void GBPrinterDlg::On3x() +{ + regSetDwordValue("printerScale", 2); + scale = 2; +} + +void GBPrinterDlg::On4x() +{ + regSetDwordValue("printerScale", 3); + scale = 3; +} + +void GBPrinterDlg::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + CWnd *h = GetDlgItem(IDC_GB_PRINTER); + h->GetWindowRect(&rect); + POINT p; + p.x = rect.left; + p.y = rect.top; + ScreenToClient((POINT *)&p); + rect.left = p.x+1; + rect.top = p.y+1; + p.x = rect.right; + p.y = rect.bottom; + ScreenToClient((POINT *)&p); + rect.right = p.x-1; + rect.bottom = p.y-1; + + StretchDIBits(dc, + rect.left, + rect.top, + rect.right - rect.left, + rect.bottom - rect.top, + 0, + 0, + 160, + 144, + bitmapData, + bitmap, + DIB_RGB_COLORS, + SRCCOPY); +} + +void systemGbPrint(u8 *data, + int pages, + int feed, + int palette, + int contrast) +{ + theApp.winCheckFullscreen(); + GBPrinterDlg printer; + printer.processData(data); + printer.DoModal(); +} + diff --git a/src/win32/GBPrinterDlg.h b/src/win32/GBPrinterDlg.h new file mode 100644 index 00000000..506de51c --- /dev/null +++ b/src/win32/GBPrinterDlg.h @@ -0,0 +1,81 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_) +#define AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBPrinter.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBPrinter dialog + +class GBPrinterDlg : public CDialog +{ + private: + u8 bitmapHeader[sizeof(BITMAPINFO)+4*sizeof(RGBQUAD)]; + BITMAPINFO *bitmap; + u8 bitmapData[160*144]; + int scale; + // Construction + public: + void processData(u8 *data); + void saveAsPNG(const char *name); + void saveAsBMP(const char *name); + GBPrinterDlg(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBPrinterDlg) + enum { IDD = IDD_GB_PRINTER }; + int m_scale; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBPrinterDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GBPrinterDlg) + afx_msg void OnSave(); + afx_msg void OnPrint(); + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void On1x(); + afx_msg void On2x(); + afx_msg void On3x(); + afx_msg void On4x(); + afx_msg void OnPaint(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_) diff --git a/src/win32/GBTileView.cpp b/src/win32/GBTileView.cpp new file mode 100644 index 00000000..69bbe326 --- /dev/null +++ b/src/win32/GBTileView.cpp @@ -0,0 +1,524 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GBTileView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "GBTileView.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../NLS.h" +#include "../Util.h" +#include "../gb/gbGlobals.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GBTileView dialog + + +GBTileView::GBTileView(CWnd* pParent /*=NULL*/) + : ResizeDlg(GBTileView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBTileView) + m_charBase = -1; + m_bank = -1; + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo, 0, sizeof(bmpInfo)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32*8; + bmpInfo.bmiHeader.biHeight = 32*8; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 32*32 * 64); + + tileView.setData(data); + tileView.setBmpInfo(&bmpInfo); + + charBase = 0; + palette = 0; + bank = 0; + w = h = 0; +} + +GBTileView::~GBTileView() +{ + free(data); + data = NULL; +} + +void GBTileView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBTileView) + DDX_Control(pDX, IDC_PALETTE_SLIDER, m_slider); + DDX_Radio(pDX, IDC_CHARBASE_0, m_charBase); + DDX_Radio(pDX, IDC_BANK_0, m_bank); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_TILE_VIEW, tileView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, zoom); + DDX_Control(pDX, IDC_COLOR, color); +} + + +BEGIN_MESSAGE_MAP(GBTileView, CDialog) + //{{AFX_MSG_MAP(GBTileView) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_CHARBASE_0, OnCharbase0) + ON_BN_CLICKED(IDC_CHARBASE_1, OnCharbase1) + ON_BN_CLICKED(IDC_BANK_0, OnBank0) + ON_BN_CLICKED(IDC_BANK_1, OnBank1) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_WM_HSCROLL() + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GBTileView message handlers + +void GBTileView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + +void GBTileView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + + +void GBTileView::OnSave() +{ + CString captureBuffer; + + if(theApp.captureFormat == 0) + captureBuffer = "tiles.png"; + else + captureBuffer = "tiles.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = dlg.GetPathName(); + + if(theApp.captureFormat) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +void GBTileView::renderTile(int tile, int x, int y, u8 *charBase) +{ + u8 *bmp = &data[24*x + 8*16*24*y]; + + for(int j = 0; j < 8; j++) { + u8 mask = 0x80; + u8 tile_a = charBase[tile*16+j*2]; + u8 tile_b = charBase[tile*16+j*2+1]; + + for(int i = 0; i < 8; i++) { + u8 c = (tile_a & mask) ? 1 : 0; + c += ((tile_b & mask) ? 2 : 0); + + if(gbCgbMode) { + c = c + palette*4; + } else { + c = gbBgp[c]; + } + + u16 color = gbPalette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + mask >>= 1; + } + bmp += 15*24; // advance line + } +} + + +void GBTileView::render() +{ + int tiles = 0x0000; + if(charBase) + tiles = 0x0800; + u8 *charBase = (gbVram != NULL) ? + (bank ? &gbVram[0x2000+tiles] : &gbVram[tiles]) : + &gbMemory[0x8000+tiles]; + + int tile = 0; + for(int y = 0; y < 16; y++) { + for(int x = 0; x < 16; x++) { + renderTile(tile, x, y, charBase); + tile++; + } + } + tileView.setSize(16*8, 16*8); + w = 16*8; + h = 16*8; + SIZE s; + s.cx = s.cy = 16*8; + if(tileView.getStretch()) { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT, s); +} + +void GBTileView::update() +{ + paint(); +} + + +BOOL GBTileView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_TILE_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBTileView", + NULL); + + m_charBase = charBase; + m_bank = bank; + + m_slider.SetRange(0, 7); + m_slider.SetPageSize(2); + m_slider.SetTicFreq(1); + paint(); + + m_stretch = regQueryDwordValue("tileViewStretch", 0); + if(m_stretch) + tileView.setStretch(true); + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBTileView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + + +void GBTileView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void GBTileView::paint() +{ + if(gbRom != NULL) { + render(); + tileView.refresh(); + } +} + +void GBTileView::OnCharbase0() +{ + charBase = 0; + paint(); +} + +void GBTileView::OnCharbase1() +{ + charBase = 1; + paint(); +} + +void GBTileView::OnBank0() +{ + bank = 0; + paint(); +} + +void GBTileView::OnBank1() +{ + bank = 1; + paint(); +} + + +void GBTileView::OnStretch() +{ + tileView.setStretch(!tileView.getStretch()); + paint(); + regSetDwordValue("tileViewStretch", tileView.getStretch()); +} + +LRESULT GBTileView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + zoom.setColors(colors); + + int x = (wParam & 0xFFFF)/8; + int y = ((wParam >> 16) & 0xFFFF)/8; + + int tiles = 0x0000; + if(charBase) + tiles = 0x0800; + u32 address = 0x8000 + tiles; + int tile = 16 * y + x; + + address += 16 * tile; + + CString buffer; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUMBER)->SetWindowText(buffer); + + buffer.Format("%04x", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT GBTileView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void GBTileView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + switch(nSBCode) { + case TB_THUMBPOSITION: + palette = nPos; + break; + default: + palette = m_slider.GetPos(); + break; + } + paint(); +} + + +void GBTileView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/GBTileView.h b/src/win32/GBTileView.h new file mode 100644 index 00000000..7e72c5ec --- /dev/null +++ b/src/win32/GBTileView.h @@ -0,0 +1,104 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_) +#define AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBTileView.h : header file +// +#include "BitmapControl.h" +#include "ColorControl.h" +#include "IUpdate.h" +#include "ResizeDlg.h" +#include "ZoomControl.h" + +///////////////////////////////////////////////////////////////////////////// +// GBTileView dialog + +class GBTileView : public ResizeDlg, IUpdateListener +{ + int charBase; + int palette; + int bank; + BitmapControl tileView; + BITMAPINFO bmpInfo; + u8 *data; + ZoomControl zoom; + ColorControl color; + int w; + int h; + bool autoUpdate; + // Construction + public: + void paint(); + void render(); + void renderTile(int tile, int x, int y, u8 *charBase); + void savePNG(const char *name); + void saveBMP(const char *name); + GBTileView(CWnd* pParent = NULL); // standard constructor + virtual ~GBTileView(); + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBTileView) + enum { IDD = IDD_GB_TILE_VIEWER }; + CSliderCtrl m_slider; + int m_charBase; + int m_bank; + BOOL m_stretch; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBTileView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + virtual afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + + // Generated message map functions + //{{AFX_MSG(GBTileView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnAutoUpdate(); + afx_msg void OnCharbase0(); + afx_msg void OnCharbase1(); + afx_msg void OnBank0(); + afx_msg void OnBank1(); + afx_msg void OnStretch(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_) diff --git a/src/win32/GDBConnection.cpp b/src/win32/GDBConnection.cpp new file mode 100644 index 00000000..af038695 --- /dev/null +++ b/src/win32/GDBConnection.cpp @@ -0,0 +1,261 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GDBConnection.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GDBConnection.h" + +#include + +#define SOCKET_MESSAGE WM_APP+1 + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +static bool initialized = false; + +///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg dialog + + +GDBPortDlg::GDBPortDlg(CWnd* pParent /*=NULL*/) + : CDialog(GDBPortDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GDBPortDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + port = 55555; + sock = INVALID_SOCKET; + + if(!initialized) { + WSADATA wsaData; + + int error = WSAStartup(MAKEWORD(1,1), &wsaData); + + initialized = true; + } +} + + +void GDBPortDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GDBPortDlg) + DDX_Control(pDX, IDC_PORT, m_port); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog) + //{{AFX_MSG_MAP(GDBPortDlg) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_WM_CLOSE() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg message handlers + +int GDBPortDlg::getPort() +{ + return port; +} + + +SOCKET GDBPortDlg::getSocket() +{ + return sock; +} + + +BOOL GDBPortDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + + buffer.Format("%d", port); + + m_port.SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GDBPortDlg::OnOk() +{ + CString buffer; + + m_port.GetWindowText(buffer); + + sockaddr_in address; + address.sin_family = AF_INET; + address.sin_addr.s_addr = inet_addr("0.0.0.0"); + address.sin_port = htons(atoi(buffer)); + port = ntohs(address.sin_port); + + SOCKET s = socket(AF_INET, SOCK_STREAM, 0); + + if(s != INVALID_SOCKET) { + int error = bind(s, (sockaddr *)&address, sizeof(address)); + + if(error) { + systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use."); + error = closesocket(s); + EndDialog(FALSE); + } else { + error = listen(s, 1); + if(!error) { + sock = s; + EndDialog(TRUE); + } else { + systemMessage(IDS_ERROR_LISTENING, "Error listening on socket."); + closesocket(s); + EndDialog(FALSE); + } + } + } else { + systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket."); + EndDialog(FALSE); + } +} + +void GDBPortDlg::OnCancel() +{ + OnClose(); +} + +void GDBPortDlg::OnClose() +{ + EndDialog(FALSE); +} +///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg dialog + + +GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd* pParent /*=NULL*/) + : CDialog(GDBWaitingDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GDBWaitingDlg) + //}}AFX_DATA_INIT + port = p & 65535; + listenSocket = s; +} + + +void GDBWaitingDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GDBWaitingDlg) + DDX_Control(pDX, IDC_PORT, m_port); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog) + //{{AFX_MSG_MAP(GDBWaitingDlg) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_WM_CLOSE() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg message handlers + +BOOL GDBWaitingDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + + buffer.Format("%d", port); + + m_port.SetWindowText(buffer); + + CenterWindow(); + + int error = WSAAsyncSelect(listenSocket, + (HWND )*this, + SOCKET_MESSAGE, + FD_ACCEPT); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam) +{ + if(LOWORD(lParam) == FD_ACCEPT) { + WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0); + + int flag = 0; + ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag); + + SOCKET s = accept(listenSocket, NULL, NULL); + if(s != INVALID_SOCKET) { + char dummy; + recv(s, &dummy, 1, 0); + if(dummy != '+') { + systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB."); + OnClose(); // calls EndDialog + } else { + sock = s; + EndDialog(TRUE); + } + } + } + + return TRUE; +} + +void GDBWaitingDlg::OnCancel() +{ + OnClose(); +} + +void GDBWaitingDlg::OnClose() +{ + if(sock != INVALID_SOCKET) { + closesocket(sock); + sock = INVALID_SOCKET; + } + if(listenSocket != INVALID_SOCKET) { + closesocket(listenSocket); + listenSocket = INVALID_SOCKET; + } + EndDialog(FALSE); +} + +SOCKET GDBWaitingDlg::getListenSocket() +{ + return listenSocket; +} + +SOCKET GDBWaitingDlg::getSocket() +{ + return sock; +} diff --git a/src/win32/GDBConnection.h b/src/win32/GDBConnection.h new file mode 100644 index 00000000..f82ac0ea --- /dev/null +++ b/src/win32/GDBConnection.h @@ -0,0 +1,114 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_) +#define AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GDBConnection.h : header file +// + +#include + +///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg dialog + +class GDBPortDlg : public CDialog +{ + int port; + SOCKET sock; + // Construction + public: + SOCKET getSocket(); + int getPort(); + GDBPortDlg(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GDBPortDlg) + enum { IDD = IDD_GDB_PORT }; + CEdit m_port; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GDBPortDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GDBPortDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg dialog + +class GDBWaitingDlg : public CDialog +{ + int port; + SOCKET listenSocket; + SOCKET sock; + // Construction + public: + SOCKET getSocket(); + SOCKET getListenSocket(); + afx_msg LRESULT OnSocketAccept(WPARAM wParam, LPARAM lParam); + GDBWaitingDlg(SOCKET s,int p, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GDBWaitingDlg) + enum { IDD = IDD_GDB_WAITING }; + CStatic m_port; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GDBWaitingDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GDBWaitingDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_) diff --git a/src/win32/GDIDisplay.cpp b/src/win32/GDIDisplay.cpp new file mode 100644 index 00000000..dc179804 --- /dev/null +++ b/src/win32/GDIDisplay.cpp @@ -0,0 +1,479 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "..\gb\gbGlobals.h" +#include "../Text.h" +#include "../Util.h" +#include "UniVideoModeDlg.h" + +#include "VBA.h" +#include "MainWnd.h" +#include "Reg.h" +#include "..\..\res\resource.h" + +#include "../gbafilter.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern void winlog(const char *,...); +extern int Init_2xSaI(u32); +extern int systemSpeed; +extern int winVideoModeSelect(CWnd *, GUID **); + + +class GDIDisplay : public IDisplay +{ +private: + u8 *filterData; + u8 info[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)]; + int SelectedFreq, SelectedAdapter; +public: + GDIDisplay(); + virtual ~GDIDisplay(); + + virtual bool changeRenderSize(int w, int h); + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual DISPLAY_TYPE getType() { return GDI; }; + virtual void setOption(const char *, int) {} + virtual int selectFullScreenMode(GUID **); + virtual int selectFullScreenMode2(); +}; + + +static int calculateShift(u32 mask) +{ + int m = 0; + + while(mask) { + m++; + mask >>= 1; + } + + return m-5; +} + +GDIDisplay::GDIDisplay() +{ + filterData = NULL; +} + +GDIDisplay::~GDIDisplay() +{ + cleanup(); +} + +void GDIDisplay::cleanup() +{ + if(filterData) + { + delete [] filterData; + filterData = NULL; + } +} + +bool GDIDisplay::initialize() +{ + switch(theApp.cartridgeType) + { + case 0: + theApp.sizeX = 240; + theApp.sizeY = 160; + break; + case 1: + if(gbBorderOn) + { + theApp.sizeX = 256; + theApp.sizeY = 224; + } + else + { + theApp.sizeX = 160; + theApp.sizeY = 144; + } + break; + } + + switch(theApp.videoOption) + { + case VIDEO_1X: + theApp.surfaceSizeX = theApp.sizeX; + theApp.surfaceSizeY = theApp.sizeY; + break; + case VIDEO_2X: + theApp.surfaceSizeX = theApp.sizeX * 2; + theApp.surfaceSizeY = theApp.sizeY * 2; + break; + case VIDEO_3X: + theApp.surfaceSizeX = theApp.sizeX * 3; + theApp.surfaceSizeY = theApp.sizeY * 3; + break; + case VIDEO_4X: + theApp.surfaceSizeX = theApp.sizeX * 4; + theApp.surfaceSizeY = theApp.sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + float scaleX = ((float)theApp.fsWidth / theApp.sizeX); + float scaleY = ((float)theApp.fsHeight / theApp.sizeY); + float min = scaleX < scaleY ? scaleX : scaleY; + if(theApp.fsMaxScale) + min = min > theApp.fsMaxScale ? theApp.fsMaxScale : min; + if(theApp.fullScreenStretch) + { + theApp.surfaceSizeX = theApp.fsWidth; + theApp.surfaceSizeY = theApp.fsHeight; + } + else + { + theApp.surfaceSizeX = (int)(theApp.sizeX * min); + theApp.surfaceSizeY = (int)(theApp.sizeY * min); + } + break; + } + + theApp.rect.left = 0; + theApp.rect.top = 0; + theApp.rect.right = theApp.sizeX; + theApp.rect.bottom = theApp.sizeY; + + theApp.dest.left = 0; + theApp.dest.top = 0; + theApp.dest.right = theApp.surfaceSizeX; + theApp.dest.bottom = theApp.surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + style |= WS_OVERLAPPEDWINDOW; + else + styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + AdjustWindowRectEx(&theApp.dest, style, TRUE, styleEx); + else + AdjustWindowRectEx(&theApp.dest, style, FALSE, styleEx); + + int winSizeX = theApp.dest.right-theApp.dest.left; + int winSizeY = theApp.dest.bottom-theApp.dest.top; + + if(theApp.videoOption > VIDEO_4X) { + winSizeX = theApp.fsWidth; + winSizeY = theApp.fsHeight; + } + + int x = 0; + int y = 0; + + if(theApp.videoOption <= VIDEO_4X) { + x = theApp.windowPositionX; + y = theApp.windowPositionY; + } + + // Create a window + MainWnd *pWnd = new MainWnd; + theApp.m_pMainWnd = pWnd; + + pWnd->CreateEx(styleEx, + theApp.wndClass, + "VisualBoyAdvance", + style, + x,y,winSizeX,winSizeY, + NULL, + 0); + + if (!(HWND)*pWnd) { + winlog("Error creating Window %08x\n", GetLastError()); + return FALSE; + } + + theApp.updateMenuBar(); + + theApp.adjustDestRect(); + + // Enumerate available display modes + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + theApp.mode1024Available = false; + theApp.mode1280Available = false; + DISPLAY_DEVICE dev; + dev.cb = sizeof(DISPLAY_DEVICE); + EnumDisplayDevices(NULL, 0, &dev, 0); + DEVMODE mode; + for (DWORD iMode = 0; + TRUE == EnumDisplaySettings(dev.DeviceName, iMode, &mode); + iMode++) + { + if ( (mode.dmBitsPerPel == 16) && + (mode.dmPelsWidth==320) && (mode.dmPelsHeight==240)) + theApp.mode320Available = true; + + if ( (mode.dmBitsPerPel == 16) && + (mode.dmPelsWidth==640) && (mode.dmPelsHeight==480)) + theApp.mode640Available = true; + + if ( (mode.dmBitsPerPel == 16) && + (mode.dmPelsWidth==800) && (mode.dmPelsHeight==600)) + theApp.mode800Available = true; + + if ( (mode.dmBitsPerPel == 32) && + (mode.dmPelsWidth==1024) && (mode.dmPelsHeight==768)) + theApp.mode1024Available = true; + + if ( (mode.dmBitsPerPel == 32) && + (mode.dmPelsWidth==1280) && (mode.dmPelsHeight==1024)) + theApp.mode1280Available = true; + } + + // Go into fullscreen + if(theApp.videoOption >= VIDEO_320x240) + { + mode.dmBitsPerPel = theApp.fsColorDepth; + mode.dmPelsWidth = theApp.fsWidth; + mode.dmPelsHeight = theApp.fsHeight; + mode.dmDisplayFrequency = theApp.fsFrequency; + mode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY; + DISPLAY_DEVICE dd; + dd.cb = sizeof(DISPLAY_DEVICE); + EnumDisplayDevices(NULL, theApp.fsAdapter, &dd, 0); + ChangeDisplaySettingsEx(dd.DeviceName, &mode, NULL, CDS_FULLSCREEN, NULL); + } + else // Reset from fullscreen + { + ChangeDisplaySettings(NULL, 0); + } + + + // Initialize 2xSaI + HDC dc = GetDC(NULL); + HBITMAP hbm = CreateCompatibleBitmap(dc, 1, 1); + BITMAPINFO *bi = (BITMAPINFO *)info; + ZeroMemory(bi, sizeof(info)); + bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + GetDIBits(dc, hbm, 0, 1, NULL, (LPBITMAPINFO)info, DIB_RGB_COLORS); + GetDIBits(dc, hbm, 0, 1, NULL, (LPBITMAPINFO)info, DIB_RGB_COLORS); + DeleteObject(hbm); + ReleaseDC(NULL, dc); + + if(bi->bmiHeader.biCompression == BI_BITFIELDS) { + systemColorDepth = bi->bmiHeader.biBitCount; + if(systemColorDepth == 15) + systemColorDepth = 16; + systemRedShift = calculateShift(*((DWORD *)&bi->bmiColors[0])); + systemGreenShift = calculateShift(*((DWORD *)&bi->bmiColors[1])); + systemBlueShift = calculateShift(*((DWORD *)&bi->bmiColors[2])); + if(systemColorDepth == 16) { + if(systemGreenShift == 6) { + Init_2xSaI(565); + } else { + Init_2xSaI(555); + } + } else if(systemColorDepth == 32) + Init_2xSaI(32); + } else { + systemColorDepth = 32; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + + Init_2xSaI(32); + } + + + // Setup system color depth + theApp.fsColorDepth = systemColorDepth; + if(systemColorDepth == 24) + theApp.filterFunction = NULL; +#ifdef MMX + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + utilUpdateSystemColorMaps(theApp.filterLCD ); + theApp.updateFilter(); + theApp.updateIFB(); + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + + +void GDIDisplay::clear() +{ + CDC *dc = theApp.m_pMainWnd->GetDC(); + CBrush brush(RGB(0x00, 0x00, 0x00)); + dc->FillRect(CRect(0, 0, theApp.fsWidth, theApp.fsHeight), &brush); + theApp.m_pMainWnd->ReleaseDC(dc); +} + +void GDIDisplay::renderMenu() +{ + checkFullScreen(); + theApp.m_pMainWnd->DrawMenuBar(); +} + +void GDIDisplay::checkFullScreen() +{ +} + +void GDIDisplay::render() +{ + unsigned int pitch = theApp.filterWidth * (systemColorDepth / 8) + 4; + + BITMAPINFO *bi = (BITMAPINFO *)info; + bi->bmiHeader.biWidth = theApp.filterWidth + 1; + bi->bmiHeader.biHeight = -theApp.filterHeight; + + if(theApp.filterFunction) + { + bi->bmiHeader.biWidth = theApp.rect.right; + bi->bmiHeader.biHeight = -(int)theApp.rect.bottom; + + (*theApp.filterFunction)( + pix + pitch, + pitch, + (u8*)theApp.delta, + (u8*)filterData, + theApp.rect.right * (systemColorDepth / 8), + theApp.filterWidth, + theApp.filterHeight); + } + + POINT p1, p2; + p1.x = theApp.dest.left; + p1.y = theApp.dest.top; + p2.x = theApp.dest.right; + p2.y = theApp.dest.bottom; + theApp.m_pMainWnd->ScreenToClient(&p1); + theApp.m_pMainWnd->ScreenToClient(&p2); + + CDC *dc = theApp.m_pMainWnd->GetDC(); + + // Draw bitmap to device + StretchDIBits( + dc->GetSafeHdc(), + p1.x, p1.y, + p2.x - p1.x, + p2.y - p1.y, + theApp.rect.left, theApp.rect.top, + theApp.rect.right - theApp.rect.left, + theApp.rect.bottom - theApp.rect.top, + theApp.filterFunction ? filterData : pix + pitch, + bi, + DIB_RGB_COLORS, + SRCCOPY); + + // Draw frame counter + if (theApp.showSpeed && (theApp.videoOption >= VIDEO_320x240)) + { + CString speedText; + if (theApp.showSpeed == 1) + speedText.AppendFormat("%3d%%", systemSpeed); + else + speedText.AppendFormat("%3d%%(%d, %d fps)", + systemSpeed, systemFrameSkip, theApp.showRenderedFrames); + + dc->SetTextColor(RGB(0xFF, 0x3F, 0x3F)); + if (theApp.showSpeedTransparent) + dc->SetBkMode(TRANSPARENT); + else + dc->SetBkMode(OPAQUE); + dc->SetBkColor(RGB(0xFF, 0xFF, 0xFF)); + dc->TextOut(p1.x + 16, p1.y + 16, speedText); + } + + // Draw screen message + if (theApp.screenMessage) + { + if ( ((GetTickCount() - theApp.screenMessageTime) < 3000) && !theApp.disableStatusMessage ) + { + dc->SetTextColor(RGB(0x3F, 0x3F, 0xFF)); + if (theApp.showSpeedTransparent) + dc->SetBkMode(TRANSPARENT); + else + dc->SetBkMode(OPAQUE); + dc->SetBkColor(RGB(0xFF, 0xFF, 0xFF)); + dc->TextOut(p1.x + 16, p2.y - 16, theApp.screenMessageBuffer); + } + else + theApp.screenMessage = false; + } + + theApp.m_pMainWnd->ReleaseDC(dc); +} + +int GDIDisplay::selectFullScreenMode(GUID **pGUID) +{ + int w, h, b; + UniVideoModeDlg dlg(0, &w, &h, &b, &SelectedFreq, &SelectedAdapter); + + if (0 == dlg.DoModal()) + { + return (b<<24) + (w<<12) + h; + // Bits<<24 | Width<<12 | Height + } + else + { + return -1; + } +} + +int GDIDisplay::selectFullScreenMode2() +{ + return (SelectedAdapter<<16) + SelectedFreq; +} + +bool GDIDisplay::changeRenderSize(int w, int h) +{ + if (filterData) + { + delete [] filterData; + filterData = NULL; + } + filterData = new u8[w*h*(systemColorDepth>>3)]; + return true; +} + + +IDisplay *newGDIDisplay() +{ + return new GDIDisplay(); +} \ No newline at end of file diff --git a/src/win32/GSACodeSelect.cpp b/src/win32/GSACodeSelect.cpp new file mode 100644 index 00000000..78e3b60e --- /dev/null +++ b/src/win32/GSACodeSelect.cpp @@ -0,0 +1,120 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// GSACodeSelect.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "GSACodeSelect.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect dialog + + +GSACodeSelect::GSACodeSelect(FILE *file, CWnd* pParent /*=NULL*/) + : CDialog(GSACodeSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(GSACodeSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + m_file = file; +} + + +void GSACodeSelect::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GSACodeSelect) + DDX_Control(pDX, IDC_GAME_LIST, m_games); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(GSACodeSelect, CDialog) + //{{AFX_MSG_MAP(GSACodeSelect) + ON_BN_CLICKED(ID_OK, OnOk) + ON_LBN_SELCHANGE(IDC_GAME_LIST, OnSelchangeGameList) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect message handlers + +void GSACodeSelect::OnCancel() +{ + EndDialog(-1); +} + +void GSACodeSelect::OnOk() +{ + EndDialog(m_games.GetCurSel()); +} + +void GSACodeSelect::OnSelchangeGameList() +{ + int item = m_games.GetCurSel(); + CWnd *ok = GetDlgItem(ID_OK); + + ok->EnableWindow(item != -1); +} + +BOOL GSACodeSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[1024]; + + FILE *f = m_file; + int games = 0; + int len = 0; + fseek(f, -4, SEEK_CUR); + fread(&games, 1, 4, f); + while(games > 0) { + fread(&len, 1, 4, f); + fread(buffer, 1, len, f); + buffer[len] = 0; + m_games.AddString(buffer); + int codes = 0; + fread(&codes, 1, 4, f); + + while(codes > 0) { + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 4, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len*12, SEEK_CUR); + codes--; + } + games--; + } + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/GSACodeSelect.h b/src/win32/GSACodeSelect.h new file mode 100644 index 00000000..8eeafa00 --- /dev/null +++ b/src/win32/GSACodeSelect.h @@ -0,0 +1,69 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_) +#define AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GSACodeSelect.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect dialog + +class GSACodeSelect : public CDialog +{ + // Construction + public: + GSACodeSelect(FILE *file, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GSACodeSelect) + enum { IDD = IDD_CODE_SELECT }; + CListBox m_games; + //}}AFX_DATA + FILE *m_file; + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GSACodeSelect) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(GSACodeSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnSelchangeGameList(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_) diff --git a/src/win32/Hyperlink.cpp b/src/win32/Hyperlink.cpp new file mode 100644 index 00000000..05770d66 --- /dev/null +++ b/src/win32/Hyperlink.cpp @@ -0,0 +1,118 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Hyperlink.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Hyperlink.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// Hyperlink + +Hyperlink::Hyperlink() +{ + m_over = false; +} + +Hyperlink::~Hyperlink() +{ + m_underlineFont.DeleteObject(); +} + + +BEGIN_MESSAGE_MAP(Hyperlink, CStatic) + //{{AFX_MSG_MAP(Hyperlink) + ON_WM_CTLCOLOR_REFLECT() + ON_WM_ERASEBKGND() + ON_WM_MOUSEMOVE() + //}}AFX_MSG_MAP + ON_CONTROL_REFLECT(STN_CLICKED, OnClicked) +END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Hyperlink message handlers + +void Hyperlink::PreSubclassWindow() +{ + DWORD dwStyle = GetStyle(); + ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY); + + // 32649 is the hand cursor + m_cursor = LoadCursor(NULL, MAKEINTRESOURCE(32649)); + + CFont *font = GetFont(); + + LOGFONT lg; + font->GetLogFont(&lg); + + lg.lfUnderline = TRUE; + + m_underlineFont.CreateFontIndirect(&lg); + SetFont(&m_underlineFont); + + CStatic::PreSubclassWindow(); +} + +void Hyperlink::OnClicked() +{ + CString url; + GetWindowText(url); + ::ShellExecute(0, _T("open"), url, + 0, 0, SW_SHOWNORMAL); +} + +HBRUSH Hyperlink::CtlColor(CDC* pDC, UINT nCtlColor) +{ + pDC->SetTextColor(RGB(0,0,240)); + + return (HBRUSH)GetStockObject(NULL_BRUSH); +} + +BOOL Hyperlink::OnEraseBkgnd(CDC* pDC) +{ + CRect rect; + GetClientRect(rect); + pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE)); + + return TRUE; +} + +void Hyperlink::OnMouseMove(UINT nFlags, CPoint point) +{ + if(!m_over) { + m_over = true; + SetCapture(); + ::SetCursor(m_cursor); + } else { + CRect r; + GetClientRect(&r); + + if(!r.PtInRect(point)) { + m_over = false; + ReleaseCapture(); + } + } +} diff --git a/src/win32/Hyperlink.h b/src/win32/Hyperlink.h new file mode 100644 index 00000000..4afe28e4 --- /dev/null +++ b/src/win32/Hyperlink.h @@ -0,0 +1,75 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_) +#define AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Hyperlink.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Hyperlink window + +class Hyperlink : public CStatic +{ +// Construction +public: + Hyperlink(); + +// Attributes +public: + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Hyperlink) + protected: + virtual void PreSubclassWindow(); + //}}AFX_VIRTUAL + +// Implementation +public: + bool m_over; + HCURSOR m_cursor; + afx_msg void OnClicked(); + CFont m_underlineFont; + virtual ~Hyperlink(); + + // Generated message map functions +protected: + //{{AFX_MSG(Hyperlink) + afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + afx_msg void OnMouseMove(UINT nFlags, CPoint point); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_) diff --git a/src/win32/IOViewer.cpp b/src/win32/IOViewer.cpp new file mode 100644 index 00000000..491000e9 --- /dev/null +++ b/src/win32/IOViewer.cpp @@ -0,0 +1,202 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// IOViewer.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "IOViewer.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Sound.h" + +#include "IOViewerRegs.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// IOViewer dialog + + +IOViewer::IOViewer(CWnd* pParent /*=NULL*/) + : ResizeDlg(IOViewer::IDD, pParent) +{ + //{{AFX_DATA_INIT(IOViewer) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + selected = 0; + autoUpdate = false; +} + + +void IOViewer::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(IOViewer) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_ADDRESSES, m_address); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(IOViewer, CDialog) + //{{AFX_MSG_MAP(IOViewer) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) + ON_BN_CLICKED(IDC_APPLY, OnApply) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// IOViewer message handlers + +void IOViewer::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void IOViewer::OnRefresh() +{ + // TODO: Add your control notification handler code here + +} + +void IOViewer::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void IOViewer::OnSelchangeAddresses() +{ + selected = m_address.GetCurSel(); + + update(); +} + +void IOViewer::PostNcDestroy() +{ + delete this; +} + +BOOL IOViewer::OnInitDialog() +{ + CDialog::OnInitDialog(); + + // winCenterWindow(getHandle()); + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\IOView", + NULL); + + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + int i; + for(i = 0; i < sizeof(ioViewRegisters)/sizeof(IOData); i++) { + m_address.AddString(ioViewRegisters[i].name); + } + m_address.SetFont(font); + for(i = 0; i < 16; i++) { + GetDlgItem(IDC_BIT_0+i)->SetFont(font); + } + + RECT cbSize; + int Height; + + m_address.GetClientRect(&cbSize); + Height = m_address.GetItemHeight(0); + Height += m_address.GetItemHeight(0) * (10); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_address.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.SetCurSel(0); + update(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void IOViewer::update() +{ + CString buffer; + + const IOData *sel = &ioViewRegisters[selected]; + u16 data = sel->address ? *sel->address : + (ioMem ? soundRead16(sel->offset) : 0); + + for(int i = 0; i < 16; i++) { + CButton *pWnd = (CButton *)GetDlgItem(IDC_BIT_0 + i); + + if(pWnd) { + if(!(sel->write & (1 << i))) + pWnd->EnableWindow(FALSE); + else + pWnd->EnableWindow(TRUE); + pWnd->SetCheck(((data & (1 << i)) >> i)); + buffer.Format("%2d %s", i, sel->bits[i]); + pWnd->SetWindowText(buffer); + } + } + + buffer.Format("%04X", data); + m_value.SetWindowText(buffer); +} + +void IOViewer::OnApply() +{ + const IOData *sel = &ioViewRegisters[selected]; + u16 res = 0; + for(int i = 0; i < 16; i++) { + CButton *pWnd = (CButton *)GetDlgItem(IDC_BIT_0 + i); + + if(pWnd) { + if(pWnd->GetCheck()) + res |= (1 << i); + } + } + CPUWriteHalfWord(0x4000000+sel->offset, res); + update(); +} diff --git a/src/win32/IOViewer.h b/src/win32/IOViewer.h new file mode 100644 index 00000000..d768b018 --- /dev/null +++ b/src/win32/IOViewer.h @@ -0,0 +1,78 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_) +#define AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// IOViewer.h : header file +// + +#include "ResizeDlg.h" +#include "IUpdate.h" + +///////////////////////////////////////////////////////////////////////////// +// IOViewer dialog + +class IOViewer : public ResizeDlg, IUpdateListener +{ + // Construction + public: + void update(); + bool autoUpdate; + int selected; + IOViewer(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(IOViewer) + enum { IDD = IDD_IO_VIEWER }; + CStatic m_value; + CComboBox m_address; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(IOViewer) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(IOViewer) + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void OnAutoUpdate(); + afx_msg void OnSelchangeAddresses(); + virtual BOOL OnInitDialog(); + afx_msg void OnApply(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_) diff --git a/src/win32/IOViewerRegs.h b/src/win32/IOViewerRegs.h new file mode 100644 index 00000000..9d28a6e2 --- /dev/null +++ b/src/win32/IOViewerRegs.h @@ -0,0 +1,2087 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +struct IOData { + u16 *address; + u16 offset; + char *name; + u16 write; + char *bits[16]; +}; + +const IOData ioViewRegisters[] = { + { + &DISPCNT, 0, "0x4000000-DISPCNT", 0xFFF7, + { + "", + "", + "BG Mode (3 bits)", + "CGB Mode", + "Display Frame", + "H-Blank Interval OBJ processing", + "OBJ Character mapping", + "Forced blank", + "BG0", + "BG1", + "BG2", + "BG3", + "OBJ", + "WIN0", + "WIN1", + "OBJWIN" + } + }, + { + &DISPSTAT, 4, "0x4000004-DISPSTAT", 0xFF38, + { + "V-Blank Status", + "H-Blank Status", + "VCOUNT Evaluation", + "V-Blank Interrupt Enable", + "H-Blank Interrupt Enable", + "VCOUNT Match Interrupt Enable", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "VCOUNT setting (8 bits)" + } + }, + { + &VCOUNT, 6, "0x4000006-VCOUNT", 0x0000, + { + "", + "", + "", + "", + "", + "", + "", + "VCOUNT (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG0CNT, 8, "0x4000008-BG0CNT", 0xDFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "", + "", + "Size (2 bits)" + } + }, + { + &BG1CNT, 0xA, "0x400000A-BG1CNT", 0xDFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "", + "", + "Size (2 bits)" + } + }, + { + &BG2CNT, 0xC, "0x400000C-BG2CNT", 0xFFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "Area Overflow", + "", + "Size (2 bits)" + } + }, + { + &BG3CNT, 0xE, "0x400000E-BG3CNT", 0xFFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "Area Overflow", + "", + "Size (2 bits)" + } + }, + { + &BG0HOFS, 0x10, "0x4000010-BG0HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG0VOFS, 0x12, "0x4000012-BG0VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG1HOFS, 0x14, "0x4000014-BG1HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG1VOFS, 0x16, "0x4000016-BG1VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2HOFS, 0x18, "0x4000018-BG8HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2VOFS, 0x1A, "0x400001A-BG2VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG3HOFS, 0x1C, "0x400001C-BG3HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits,W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG3VOFS, 0x1E, "0x400001E-BG3VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits,W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2PA, 0x20, "0x4000020-BG2PA", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dx (16 bits,W)" + } + }, + { + &BG2PB, 0x22, "0x4000022-BG2PB", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmx (16 bits,W)" + } + }, + { + &BG2PC, 0x24, "0x4000024-BG2PC", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dy (16 bits,W)" + } + }, + { + &BG2PD, 0x26, "0x4000026-BG2PD", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmy (16 bits,W)" + } + }, + { + &BG2X_L, 0x28, "0x4000028-BG2X_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X low bits (16 bits,W)" + } + }, + { + &BG2X_H, 0x2A, "0x400002A-BG2X_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X high bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG2Y_L, 0x2C, "0x400002C-BG2Y_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y low bits (16 bits,W)" + } + }, + { + &BG2Y_H, 0x2E, "0x400002E-BG2Y_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG3PA, 0x30, "0x4000030-BG3PA", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dx (16 bits,W)" + } + }, + { + &BG3PB, 0x32, "0x4000032-BG3PB", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmx (16 bits,W)" + } + }, + { + &BG3PC, 0x34, "0x4000034-BG3PC", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dy (16 bits,W)" + } + }, + { + &BG3PD, 0x36, "0x4000036-BG3PD", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmy (16 bits,W)" + } + }, + { + &BG3X_L, 0x38, "0x4000038-BG3X_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X low bits (16 bits,W)" + } + }, + { + &BG3X_H, 0x3A, "0x400003A-BG3X_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG3Y_L, 0x3C, "0x400003C-BG3Y_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y low bits (16 bits,W)" + } + }, + { + &BG3Y_H, 0x3E, "0x400003E-BG3Y_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &WIN0H, 0x40, "0x4000040-WIN0H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 0 lower-right X (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 0 upper-left X (8 bits,W)", + } + }, + { + &WIN1H, 0x42, "0x4000042-WIN1H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 1 lower-right X (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 1 upper-left (8 bits,W)", + } + }, + { + &WIN0V, 0x44, "0x4000044-WIN0V", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 0 lower-right Y (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 0 upper-left Y (8 bits,W)", + } + }, + { + &WIN1V, 0x46, "0x4000046-WIN1V", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 1 lower-right Y (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 1 upper-left Y (8 bits,W)", + } + }, + { + &WININ, 0x48, "0x4000048-WININ", 0x3F3F, + { + "WIN0 BG0", + "WIN0 BG1", + "WIN0 BG2", + "WIN0 BG3", + "WIN0 OBJ", + "WIN0 Special FX", + "", + "", + "WIN1 BG0", + "WIN1 BG1", + "WIN1 BG2", + "WIN1 BG3", + "WIN1 OBJ", + "WIN1 Special FX", + "", + "", + } + }, + { + &WINOUT, 0x4A, "0x400004A-WINOUT", 0x3F3F, + { + "WIN0/1 BG0", + "WIN0/1 BG1", + "WIN0/1 BG2", + "WIN0/1 BG3", + "WIN0/1 OBJ", + "WIN0/1 Special FX", + "", + "", + "OBJWIN BG0", + "OBJWIN BG1", + "OBJWIN BG2", + "OBJWIN BG3", + "OBJWIN OBJ", + "OBJWIN Special FX", + "", + "", + } + }, + { + &MOSAIC, 0x4C, "0x400004C-MOSAIC", 0xFFFF, + { + "", + "", + "", + "BG H Size (4 bits,W)", + "", + "", + "", + "BG V Size (4 bits,W)", + "", + "", + "", + "OBJ H Size (4 bits,W)", + "", + "", + "", + "OBJ V Size (4 bits,W)", + } + }, + { + &BLDMOD, 0x50, "0x4000050-BLDMOD", 0x3FFF, + { + "1st BG0", + "1st BG1", + "1st BG2", + "1st BG3", + "1st OBJ", + "1st BD", + "", + "FX Type (2 bits)", + "2nd BG0", + "2nd BG1", + "2nd BG2", + "2nd BG3", + "2nd OBJ", + "2nd BD", + "", + "", + } + }, + { + &COLEV, 0x52, "0x4000052-COLEV", 0x1F1F, + { + "", + "", + "", + "", + "Coefficient EVA (5 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Coefficient EVB (5 bits,W)", + "", + "", + "", + } + }, + { + &COLY, 0x54, "0x4000054-COLEY", 0x001F, + { + "", + "", + "", + "", + "Coefficient EVY (5 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x60, "0x4000060-SG10_L", 0x007F, + { + "", + "", + "Sweep Shifts (3 bits)", + "Sweep addition/decrease", + "", + "", + "Sweep Time (3 bits)", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x62, "0x4000062-SG10_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "Waveform Type (2 bits)", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x64, "0x4000064-SG11", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x68, "0x4000068-SG20", 0xFFFF, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "Waveform Type (2 bits)", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x6C, "0x400006C-SG21", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x70, "0x4000070-SG30_L", 0x00E0, + { + "", + "", + "", + "", + "", + "Waveform 32/64 Steps", + "Waveform Bank 0/1", + "Sound Output", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x72, "0x4000072-SG30_H", 0xE0FF, + { + "", + "", + "", + "", + "", + "", + "", + "Sound Length (8 bits,W)", + "", + "", + "", + "", + "", + "", + "Output Level (2 bits)", + "Forced 3/4 Output Level", + } + }, + { + NULL, 0x74, "0x4000074-SG31", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x78, "0x4000078-SG40", 0xFF3F, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x7C, "0x400007C-SG41", 0xC0FF, + { + "", + "", + "Dividing Ratio Freq. (3 bits)", + "Counter 15/7 Steps", + "", + "", + "", + "Counter Shift Clock (4 bits)", + "", + "", + "", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x80, "0x4000080-SGCNT0_L", 0xFF77, + { + "", + "", + "Right Volume (3 bits)", + "", + "", + "", + "Left Volume (3 bits)", + "", + "Channel 1->Right", + "Channel 2->Right", + "Channel 3->Right", + "Channel 4->Right", + "Channel 1->Left", + "Channel 2->Left", + "Channel 3->Left", + "Channel 4->Left", + } + }, + { + NULL, 0x82, "0x4000082-SGCNT0_H", 0xFF1F, + { + "", + "Sound 1-4 Volume (2 bits)", + "DMA Sound A Volume", + "DMA Sound B Volume", + "", + "", + "", + "", + "DMA Sound A->Right", + "DMA Sound A->Left", + "DMA Sound A Timer", + "DMA Sound A Reset FIFO", + "DMA Sound B->Right", + "DMA Sound B->Left", + "DMA Sound B Timer", + "DMA Sound B Reset FIFO", + } + }, + { + NULL, 0x84, "0x4000084-SGCNT1", 0x0080, + { + "Sound 1 On", + "Sound 2 On", + "Sound 3 On", + "Sound 4 On", + "", + "", + "", + "Master Sound Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x88, "0x4000088-SGBIAS", 0xC3FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Bias Level (10 bits)", + "", + "", + "", + "", + "", + "Sampling Rate (2 bits)", + } + }, + { + NULL, 0xA0, "0x40000A0-SIGFIFOA_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 0 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 1 (8 bits)", + } + }, + { + NULL, 0xA2, "0x40000A2-SIGFIFOA_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 2 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 3 (8 bits)", + } + }, + { + NULL, 0xA4, "0x40000A4-SIGFIFOB_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 0 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 1 (8 bits)", + } + }, + { + NULL, 0xA6, "0x40000A6-SIGFIFOB_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 2 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 3 (8 bits)", + } + }, + { + &DM0SAD_L, 0xB0, "0x40000B0-DM0SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM0SAD_H, 0xB2, "0x40000B2-DM0SAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM0DAD_L, 0xB4, "0x40000B4-DM0DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM0DAD_H, 0xB6, "0x40000B6-DM0DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM0CNT_L, 0xB8, "0x40000B8-DM0CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM0CNT_H, 0xBA, "0x40000BA-DM0CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM1SAD_L, 0xBC, "0x40000BC-DM1SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM1SAD_H, 0xBE, "0x40000BE-DM1SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM1DAD_L, 0xC0, "0x40000C0-DM1DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM1DAD_H, 0xC2, "0x40000C2-DM1DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM1CNT_L, 0xC4, "0x40000C4-DM1CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM1CNT_H, 0xC6, "0x40000C6-DM1CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM2SAD_L, 0xC8, "0x40000C8-DM2SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM2SAD_H, 0xCA, "0x40000CA-DM2SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM2DAD_L, 0xCC, "0x40000CC-DM2DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM2DAD_H, 0xCE, "0x40000CE-DM2DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM2CNT_L, 0xD0, "0x40000D0-DM2CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM2CNT_H, 0xD2, "0x40000D2-DM2CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM3SAD_L, 0xD4, "0x40000D4-DM3SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM3SAD_H, 0xD6, "0x40000D6-DM3SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM3DAD_L, 0xD8, "0x40000D8-DM3DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM3DAD_H, 0xDA, "0x40000DA-DM3DAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM3CNT_L, 0xDC, "0x40000DC-DM3CNT_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (16 bits)", + } + }, + { + &DM3CNT_H, 0xDE, "0x40000DE-DM3CNT_H", 0xFFE0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "Game Pak Data Request", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &TM0D, 0x100, "0x4000100-TM0D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM0CNT, 0x102, "0x4000102-TM0CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM1D, 0x104, "0x4000104-TM1D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM1CNT, 0x106, "0x4000106-TM1CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM2D, 0x108, "0x4000108-TM2D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM2CNT, 0x10A, "0x400010A-TM2CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM3D, 0x10C, "0x400010C-TM3D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM3CNT, 0x10E, "0x400010E-TM3CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &P1, 0x130, "0x4000130-P1", 0x03FF, + { + "A", + "B", + "Select", + "Start", + "Right", + "Left", + "Up", + "Down", + "Shoulder Right", + "Shoulder Left", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x132, "0x4000132-P1CNT", 0xC3FF, + { + "A", + "B", + "Select", + "Start", + "Right", + "Left", + "Up", + "Down", + "Shoulder Right", + "Shoulder Left", + "", + "", + "", + "", + "Interrupt Request", + "Interrupt Condition", + } + }, + { + &IE, 0x200, "0x4000200-IE", 0x3FFF, + { + "VBlank", + "HBlank", + "VCount", + "Timer 0", + "Timer 1", + "Timer 2", + "Timer 3", + "Serial", + "DMA 0", + "DMA 1", + "DMA 2", + "DMA 3", + "Keypad", + "Game Pak", + "", + "", + } + }, + { + &IF, 0x202, "0x4000202-IF", 0x0000, + { + "VBlank", + "HBlank", + "VCount", + "Timer 0", + "Timer 1", + "Timer 2", + "Timer 3", + "Serial", + "DMA 0", + "DMA 1", + "DMA 2", + "DMA 3", + "Keypad", + "Game Pak", + "", + "", + } + }, + { + NULL, 0x204, "0x4000204-WAITCNT", 0x5FFF, + { + "", + "SRAM Wait Control (2 bits)", + "", + "Wait State 0 First Access (2 bits)", + "Wait State 0 Second Access", + "", + "Wait State 1 First Access (2 bits)", + "Wait State 1 Second Access", + "", + "Wait State 2 First Access (2 bits)", + "Wait State 2 Second Access", + "", + "PHI Terminal Output (2 bits)", + "", + "Game Pak Prefetch Buffer", + "Game Pak Type Flag", + } + }, + { + &IME, 0x208, "0x4000208-IME", 0x0001, + { + "Master Interrupt Enable", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x300, "0x4000300-HALTCNT", 0x8001, + { + "First Boot", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Power Down", + } + }, +}; diff --git a/src/win32/IUpdate.h b/src/win32/IUpdate.h new file mode 100644 index 00000000..a409cb43 --- /dev/null +++ b/src/win32/IUpdate.h @@ -0,0 +1,27 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_WIN32_IUPDATE_H +#define VBA_WIN32_IUPDATE_H + +class IUpdateListener { + public: + virtual void update()=0; +}; +#endif diff --git a/src/win32/Input.h b/src/win32/Input.h new file mode 100644 index 00000000..bc48c451 --- /dev/null +++ b/src/win32/Input.h @@ -0,0 +1,55 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_WIN32_INPUT_H +#define VBA_WIN32_INPUT_H +#include "../System.h" + +#define JOYCONFIG_MESSAGE (WM_USER + 1000) + +enum { + KEY_LEFT, KEY_RIGHT, + KEY_UP, KEY_DOWN, + KEY_BUTTON_A, KEY_BUTTON_B, + KEY_BUTTON_START, KEY_BUTTON_SELECT, + KEY_BUTTON_L, KEY_BUTTON_R, + KEY_BUTTON_SPEED, KEY_BUTTON_CAPTURE, + KEY_BUTTON_GS +}; + +class Input { + + public: + Input() {}; + virtual ~Input() {}; + + virtual bool initialize() = 0; + + virtual bool readDevices() = 0; + virtual u32 readDevice(int which) = 0; + virtual CString getKeyName(int key) = 0; + virtual void checkKeys() = 0; + virtual void checkMotionKeys() = 0; + virtual void checkDevices() = 0; + virtual void activate() = 0; + virtual void loadSettings() = 0; + virtual void saveSettings() = 0; +}; + +#endif diff --git a/src/win32/Joypad.cpp b/src/win32/Joypad.cpp new file mode 100644 index 00000000..193313cc --- /dev/null +++ b/src/win32/Joypad.cpp @@ -0,0 +1,435 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Joypad.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Joypad.h" +#include "Input.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern USHORT joypad[4][13]; +extern USHORT motion[4]; + +///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl + +JoypadEditControl::JoypadEditControl() +{ +} + +JoypadEditControl::~JoypadEditControl() +{ +} + + +BEGIN_MESSAGE_MAP(JoypadEditControl, CEdit) + //{{AFX_MSG_MAP(JoypadEditControl) + ON_WM_CHAR() + //}}AFX_MSG_MAP + ON_MESSAGE(JOYCONFIG_MESSAGE, OnJoyConfig) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl message handlers + +void JoypadEditControl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{ +} + +LRESULT JoypadEditControl::OnJoyConfig(WPARAM wParam, LPARAM lParam) +{ + SetWindowLong(GetSafeHwnd(), GWL_USERDATA,((wParam<<8)|lParam)); + SetWindowText(theApp.input->getKeyName((wParam<<8)|lParam)); + GetParent()->GetNextDlgTabItem(this, FALSE)->SetFocus(); + return TRUE; +} + +BOOL JoypadEditControl::PreTranslateMessage(MSG *pMsg) +{ + if(pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN)) + return TRUE; + + return CEdit::PreTranslateMessage(pMsg); +} + +///////////////////////////////////////////////////////////////////////////// +// JoypadConfig dialog + + +JoypadConfig::JoypadConfig(int w, CWnd* pParent /*=NULL*/) + : CDialog(JoypadConfig::IDD, pParent) +{ + //{{AFX_DATA_INIT(JoypadConfig) + //}}AFX_DATA_INIT + timerId = 0; + which = w; + if(which < 0 || which > 3) + which = 0; +} + +void JoypadConfig::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(JoypadConfig) + DDX_Control(pDX, IDC_EDIT_UP, up); + DDX_Control(pDX, IDC_EDIT_SPEED, speed); + DDX_Control(pDX, IDC_EDIT_RIGHT, right); + DDX_Control(pDX, IDC_EDIT_LEFT, left); + DDX_Control(pDX, IDC_EDIT_DOWN, down); + DDX_Control(pDX, IDC_EDIT_CAPTURE, capture); + DDX_Control(pDX, IDC_EDIT_BUTTON_START, buttonStart); + DDX_Control(pDX, IDC_EDIT_BUTTON_SELECT, buttonSelect); + DDX_Control(pDX, IDC_EDIT_BUTTON_R, buttonR); + DDX_Control(pDX, IDC_EDIT_BUTTON_L, buttonL); + DDX_Control(pDX, IDC_EDIT_BUTTON_GS, buttonGS); + DDX_Control(pDX, IDC_EDIT_BUTTON_B, buttonB); + DDX_Control(pDX, IDC_EDIT_BUTTON_A, buttonA); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(JoypadConfig, CDialog) + //{{AFX_MSG_MAP(JoypadConfig) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + ON_WM_CHAR() + ON_WM_DESTROY() + ON_WM_TIMER() + ON_WM_KEYDOWN() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// JoypadConfig message handlers + +void JoypadConfig::OnCancel() +{ + EndDialog(FALSE); +} + +void JoypadConfig::OnOk() +{ + assignKeys(); + theApp.input->checkKeys(); + EndDialog(TRUE); +} + +void JoypadConfig::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{ +} + +void JoypadConfig::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timerId); +} + +void JoypadConfig::OnTimer(UINT nIDEvent) +{ + theApp.input->checkDevices(); + + CDialog::OnTimer(nIDEvent); +} + +void JoypadConfig::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{ +} + +BOOL JoypadConfig::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timerId = SetTimer(0,200,NULL); + + SetWindowLong(up, GWL_USERDATA,joypad[which][KEY_UP]); + up.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_UP])); + + SetWindowLong(down, GWL_USERDATA,joypad[which][KEY_DOWN]); + down.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_DOWN])); + + SetWindowLong(left, GWL_USERDATA,joypad[which][KEY_LEFT]); + left.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_LEFT])); + + SetWindowLong(right, GWL_USERDATA,joypad[which][KEY_RIGHT]); + right.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_RIGHT])); + + SetWindowLong(buttonA, GWL_USERDATA,joypad[which][KEY_BUTTON_A]); + buttonA.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_A])); + + SetWindowLong(buttonB, GWL_USERDATA,joypad[which][KEY_BUTTON_B]); + buttonB.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_B])); + + SetWindowLong(buttonL, GWL_USERDATA,joypad[which][KEY_BUTTON_L]); + buttonL.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_L])); + + SetWindowLong(buttonR, GWL_USERDATA,joypad[which][KEY_BUTTON_R]); + buttonR.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_R])); + + SetWindowLong(buttonSelect, GWL_USERDATA,joypad[which][KEY_BUTTON_SELECT]); + buttonSelect.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_SELECT])); + + SetWindowLong(buttonStart, GWL_USERDATA,joypad[which][KEY_BUTTON_START]); + buttonStart.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_START])); + + SetWindowLong(speed, GWL_USERDATA,joypad[which][KEY_BUTTON_SPEED]); + speed.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_SPEED])); + + SetWindowLong(capture, GWL_USERDATA,joypad[which][KEY_BUTTON_CAPTURE]); + capture.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_CAPTURE])); + + SetWindowLong(buttonGS, GWL_USERDATA,joypad[which][KEY_BUTTON_GS]); + buttonGS.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_GS])); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void JoypadConfig::assignKey(int id, int key) +{ + switch(id) { + case IDC_EDIT_LEFT: + joypad[which][KEY_LEFT] = key; + break; + case IDC_EDIT_RIGHT: + joypad[which][KEY_RIGHT] = key; + break; + case IDC_EDIT_UP: + joypad[which][KEY_UP] = key; + break; + case IDC_EDIT_SPEED: + joypad[which][KEY_BUTTON_SPEED] = key; + break; + case IDC_EDIT_CAPTURE: + joypad[which][KEY_BUTTON_CAPTURE] = key; + break; + case IDC_EDIT_DOWN: + joypad[which][KEY_DOWN] = key; + break; + case IDC_EDIT_BUTTON_A: + joypad[which][KEY_BUTTON_A] = key; + break; + case IDC_EDIT_BUTTON_B: + joypad[which][KEY_BUTTON_B] = key; + break; + case IDC_EDIT_BUTTON_L: + joypad[which][KEY_BUTTON_L] = key; + break; + case IDC_EDIT_BUTTON_R: + joypad[which][KEY_BUTTON_R] = key; + break; + case IDC_EDIT_BUTTON_START: + joypad[which][KEY_BUTTON_START] = key; + break; + case IDC_EDIT_BUTTON_SELECT: + joypad[which][KEY_BUTTON_SELECT] = key; + break; + case IDC_EDIT_BUTTON_GS: + joypad[which][KEY_BUTTON_GS] = key; + break; + } +} + +void JoypadConfig::assignKeys() +{ + int id; + + id = IDC_EDIT_UP; + assignKey(id, GetWindowLong(up, GWL_USERDATA)); + + id = IDC_EDIT_DOWN; + assignKey(id, GetWindowLong(down, GWL_USERDATA)); + + id = IDC_EDIT_LEFT; + assignKey(id, GetWindowLong(left, GWL_USERDATA)); + + id = IDC_EDIT_RIGHT; + assignKey(id, GetWindowLong(right, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_A; + assignKey(id, GetWindowLong(buttonA, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_B; + assignKey(id, GetWindowLong(buttonB, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_L; + assignKey(id, GetWindowLong(buttonL, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_R; + assignKey(id, GetWindowLong(buttonR, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_SELECT; + assignKey(id, GetWindowLong(buttonSelect, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_START; + assignKey(id, GetWindowLong(buttonStart, GWL_USERDATA)); + + id = IDC_EDIT_SPEED; + assignKey(id, GetWindowLong(speed, GWL_USERDATA)); + + id = IDC_EDIT_CAPTURE; + assignKey(id, GetWindowLong(capture, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_GS; + assignKey(id, GetWindowLong(buttonGS, GWL_USERDATA)); + + // winSaveKeys(); +} + +///////////////////////////////////////////////////////////////////////////// +// MotionConfig dialog + + +MotionConfig::MotionConfig(CWnd* pParent /*=NULL*/) + : CDialog(MotionConfig::IDD, pParent) +{ + //{{AFX_DATA_INIT(MotionConfig) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + timerId = 0; +} + + +void MotionConfig::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MotionConfig) + DDX_Control(pDX, IDC_EDIT_UP, up); + DDX_Control(pDX, IDC_EDIT_RIGHT, right); + DDX_Control(pDX, IDC_EDIT_LEFT, left); + DDX_Control(pDX, IDC_EDIT_DOWN, down); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(MotionConfig, CDialog) + //{{AFX_MSG_MAP(MotionConfig) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + ON_WM_CHAR() + ON_WM_DESTROY() + ON_WM_KEYDOWN() + ON_WM_TIMER() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// MotionConfig message handlers + +void MotionConfig::OnCancel() +{ + EndDialog(FALSE); +} + +void MotionConfig::OnOk() +{ + assignKeys(); + theApp.input->checkKeys(); + EndDialog( TRUE); +} + +void MotionConfig::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{ +} + +void MotionConfig::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timerId); +} + +BOOL MotionConfig::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timerId = SetTimer(0,200,NULL); + + SetWindowLong(up, GWL_USERDATA,motion[KEY_UP]); + up.SetWindowText(theApp.input->getKeyName(motion[KEY_UP])); + + SetWindowLong(down, GWL_USERDATA,motion[KEY_DOWN]); + down.SetWindowText(theApp.input->getKeyName(motion[KEY_DOWN])); + + SetWindowLong(left, GWL_USERDATA,motion[KEY_LEFT]); + left.SetWindowText(theApp.input->getKeyName(motion[KEY_LEFT])); + + SetWindowLong(right, GWL_USERDATA,motion[KEY_RIGHT]); + right.SetWindowText(theApp.input->getKeyName(motion[KEY_RIGHT])); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MotionConfig::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{ +} + +void MotionConfig::OnTimer(UINT nIDEvent) +{ + theApp.input->checkDevices(); + + CDialog::OnTimer(nIDEvent); +} + +void MotionConfig::assignKey(int id, int key) +{ + switch(id) { + case IDC_EDIT_LEFT: + motion[KEY_LEFT] = key; + break; + case IDC_EDIT_RIGHT: + motion[KEY_RIGHT] = key; + break; + case IDC_EDIT_UP: + motion[KEY_UP] = key; + break; + case IDC_EDIT_DOWN: + motion[KEY_DOWN] = key; + break; + } +} + +void MotionConfig::assignKeys() +{ + int id; + + id = IDC_EDIT_UP; + assignKey(id, GetWindowLong(up, GWL_USERDATA)); + + id = IDC_EDIT_DOWN; + assignKey(id, GetWindowLong(down, GWL_USERDATA)); + + id = IDC_EDIT_LEFT; + assignKey(id, GetWindowLong(left, GWL_USERDATA)); + + id = IDC_EDIT_RIGHT; + assignKey(id, GetWindowLong(right, GWL_USERDATA)); +} diff --git a/src/win32/Joypad.h b/src/win32/Joypad.h new file mode 100644 index 00000000..4e39f091 --- /dev/null +++ b/src/win32/Joypad.h @@ -0,0 +1,168 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_) +#define AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Joypad.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl window + +class JoypadEditControl : public CEdit +{ + // Construction + public: + JoypadEditControl(); + + // Attributes + public: + + // Operations + public: + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(JoypadEditControl) + //}}AFX_VIRTUAL + + // Implementation + public: + virtual BOOL PreTranslateMessage(MSG *pMsg); + afx_msg LRESULT OnJoyConfig(WPARAM wParam, LPARAM lParam); + virtual ~JoypadEditControl(); + + // Generated message map functions + protected: + //{{AFX_MSG(JoypadEditControl) + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////////////////////// +// JoypadConfig dialog + +class JoypadConfig : public CDialog +{ + // Construction + public: + void assignKeys(); + void assignKey(int id, int key); + JoypadConfig(int w, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(JoypadConfig) + enum { IDD = IDD_CONFIG }; + JoypadEditControl up; + JoypadEditControl speed; + JoypadEditControl right; + JoypadEditControl left; + JoypadEditControl down; + JoypadEditControl capture; + JoypadEditControl buttonStart; + JoypadEditControl buttonSelect; + JoypadEditControl buttonR; + JoypadEditControl buttonL; + JoypadEditControl buttonGS; + JoypadEditControl buttonB; + JoypadEditControl buttonA; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(JoypadConfig) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + UINT timerId; + int which; + + // Generated message map functions + //{{AFX_MSG(JoypadConfig) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnDestroy(); + afx_msg void OnTimer(UINT nIDEvent); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + ///////////////////////////////////////////////////////////////////////////// +// MotionConfig dialog + +class MotionConfig : public CDialog +{ + // Construction + public: + void assignKeys(); + void assignKey(int id, int key); + MotionConfig(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MotionConfig) + enum { IDD = IDD_MOTION_CONFIG }; + JoypadEditControl up; + JoypadEditControl right; + JoypadEditControl left; + JoypadEditControl down; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MotionConfig) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(MotionConfig) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnDestroy(); + virtual BOOL OnInitDialog(); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnTimer(UINT nIDEvent); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + UINT timerId; +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_) diff --git a/src/win32/KeyboardEdit.cpp b/src/win32/KeyboardEdit.cpp new file mode 100644 index 00000000..65678091 --- /dev/null +++ b/src/win32/KeyboardEdit.cpp @@ -0,0 +1,148 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : KeyboardEdit.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Authors : A.Lebatard + T.Maurel +// Date : 17.08.98 +// +// Remarks : implementation file +// +//////////////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "KeyboardEdit.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern TCHAR* mapVirtKeysStringFromWORD(WORD wKey); + +///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit + +CKeyboardEdit::CKeyboardEdit() +{ + m_bKeyDefined = false; + ResetKey (); +} + +CKeyboardEdit::~CKeyboardEdit() +{ +} + + +BEGIN_MESSAGE_MAP(CKeyboardEdit, CEdit) + //{{AFX_MSG_MAP(CKeyboardEdit) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + +#pragma warning( disable : 4706 ) + ///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit message handlers +BOOL CKeyboardEdit::PreTranslateMessage (MSG* pMsg) +{ + bool bPressed; + if ((bPressed = (pMsg->message == WM_KEYDOWN)) || pMsg->message == WM_KEYUP || (bPressed = (pMsg->message == WM_SYSKEYDOWN)) || pMsg->message == WM_SYSKEYUP) { + if (bPressed && m_bKeyDefined && !((1 << 30) & pMsg->lParam)) + ResetKey (); + if (pMsg->wParam == VK_SHIFT && !m_bKeyDefined) + m_bShiftPressed = bPressed; + else if (pMsg->wParam == VK_CONTROL &&!m_bKeyDefined) { + m_bCtrlPressed = bPressed; + } + else if (pMsg->wParam == VK_MENU && !m_bKeyDefined) + m_bAltPressed = bPressed; + else { + if (!m_bKeyDefined) { + m_wVirtKey = (WORD)pMsg->wParam; + if (bPressed) + m_bKeyDefined = true; + } + } + DisplayKeyboardString (); + return TRUE; + } + + return CEdit::PreTranslateMessage(pMsg); +} +#pragma warning( default : 4706 ) + +//////////////////////////////////////////////////////////////////////// +// +void CKeyboardEdit::DisplayKeyboardString() +{ + CString strKbd; + + // modifiers + if (m_bCtrlPressed) + strKbd = "Ctrl"; + if (m_bAltPressed) { + if (strKbd.GetLength () > 0) + strKbd += '+'; + strKbd += "Alt"; + } + if (m_bShiftPressed) { + if (strKbd.GetLength () > 0) + strKbd += '+'; + strKbd += "Shift"; + } + // virtual key + LPCTSTR szVirtKey = mapVirtKeysStringFromWORD(m_wVirtKey); + if (szVirtKey != NULL) { + if (strKbd.GetLength () > 0) + strKbd += '+'; + strKbd += szVirtKey; + } + SetWindowText (strKbd); +} + + +//////////////////////////////////////////////////////////////////////// +// +void CKeyboardEdit::ResetKey () +{ + m_wVirtKey = 0; + m_bCtrlPressed = false; + m_bAltPressed = false; + m_bShiftPressed = false; + + m_bKeyDefined = false; + if(m_hWnd != NULL) + SetWindowText(_T("")); +} + + +//////////////////////////////////////////////////////////////////////// +// +bool CKeyboardEdit::GetAccelKey(WORD& wVirtKey, bool& bCtrl, bool& bAlt, bool& bShift) +{ + if (!m_bKeyDefined) + return false; + + wVirtKey = m_wVirtKey; + bAlt = m_bAltPressed; + bCtrl = m_bCtrlPressed; + bShift = m_bShiftPressed; + return true; +} + diff --git a/src/win32/KeyboardEdit.h b/src/win32/KeyboardEdit.h new file mode 100644 index 00000000..c72da1d3 --- /dev/null +++ b/src/win32/KeyboardEdit.h @@ -0,0 +1,87 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : KeyboardEdit.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Authors : A.Lebatard + T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// +#if !defined(AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_) +#define AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// KeyboardEdit.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit window + +class CKeyboardEdit : public CEdit +{ + // Construction + public: + CKeyboardEdit(); + + // Attributes + public: + bool m_bKeyDefined; + + WORD m_wVirtKey; + bool m_bCtrlPressed; + bool m_bAltPressed; + bool m_bShiftPressed; + + // Operations + public: + bool GetAccelKey(WORD& wVirtKey, bool& bCtrl, bool& bAlt, bool& bShift); + void ResetKey (); + + protected: + void DisplayKeyboardString (); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CKeyboardEdit) + public: + virtual BOOL PreTranslateMessage(MSG* pMsg); + //}}AFX_VIRTUAL + + // Implementation + public: + virtual ~CKeyboardEdit(); + + // Generated message map functions + protected: + //{{AFX_MSG(CKeyboardEdit) + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_) diff --git a/src/win32/LangSelect.cpp b/src/win32/LangSelect.cpp new file mode 100644 index 00000000..4dcabe6b --- /dev/null +++ b/src/win32/LangSelect.cpp @@ -0,0 +1,97 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// LangSelect.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "LangSelect.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// LangSelect dialog + + +LangSelect::LangSelect(CWnd* pParent /*=NULL*/) + : CDialog(LangSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(LangSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void LangSelect::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LangSelect) + DDX_Control(pDX, IDC_LANG_STRING, m_langString); + DDX_Control(pDX, IDC_LANG_NAME, m_langName); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(LangSelect, CDialog) + //{{AFX_MSG_MAP(LangSelect) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// LangSelect message handlers + +void LangSelect::OnCancel() +{ + EndDialog(FALSE); +} + +void LangSelect::OnOk() +{ + m_langString.GetWindowText(theApp.languageName); + EndDialog(TRUE); +} + +BOOL LangSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char lbuffer[10]; + if(GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) { + m_langName.SetWindowText(lbuffer); + } else { + m_langName.SetWindowText("???"); + } + + if(!theApp.languageName.IsEmpty()) + m_langString.SetWindowText(theApp.languageName); + + m_langString.LimitText(3); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/LangSelect.h b/src/win32/LangSelect.h new file mode 100644 index 00000000..d8b22394 --- /dev/null +++ b/src/win32/LangSelect.h @@ -0,0 +1,68 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_) +#define AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// LangSelect.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// LangSelect dialog + +class LangSelect : public CDialog +{ + // Construction + public: + LangSelect(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(LangSelect) + enum { IDD = IDD_LANG_SELECT }; + CEdit m_langString; + CStatic m_langName; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LangSelect) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(LangSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_) diff --git a/src/win32/LinkOptions.cpp b/src/win32/LinkOptions.cpp new file mode 100644 index 00000000..690f6608 --- /dev/null +++ b/src/win32/LinkOptions.cpp @@ -0,0 +1,423 @@ +// LinkOptions.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "LinkOptions.h" +#include "../Link.h" + +extern int lspeed; +extern lserver ls; + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// LinkOptions dialog + +CMyTabCtrl::CMyTabCtrl(){ + m_tabdialog[0] = new LinkGeneral; + m_tabdialog[1] = new LinkServer; + m_tabdialog[2] = new LinkClient; +} + +CMyTabCtrl::~CMyTabCtrl() +{ + m_tabdialog[0]->DestroyWindow(); + m_tabdialog[1]->DestroyWindow(); + m_tabdialog[2]->DestroyWindow(); + + delete m_tabdialog[0]; + delete m_tabdialog[1]; + delete m_tabdialog[2]; +} + +LinkOptions::LinkOptions(CWnd* pParent /*=NULL*/) + : CDialog(LinkOptions::IDD, pParent) +{ + //{{AFX_DATA_INIT(LinkOptions) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void LinkOptions::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LinkOptions) + DDX_Control(pDX, IDC_TAB1, m_tabctrl); + //}}AFX_DATA_MAP +} + +BOOL LinkOptions::OnInitDialog(){ + TCITEM tabitem; + char tabtext[3][8] = {"General", "Server", "Client"}; + int i; + + CDialog::OnInitDialog(); + + tabitem.mask = TCIF_TEXT; + + for(i=0;i<3;i++){ + tabitem.pszText = tabtext[i]; + m_tabctrl.InsertItem(i, &tabitem); + } + + m_tabctrl.m_tabdialog[0]->Create(IDD_LINKTAB1, this); + m_tabctrl.m_tabdialog[1]->Create(IDD_LINKTAB2, this); + m_tabctrl.m_tabdialog[2]->Create(IDD_LINKTAB3, this); + + m_tabctrl.m_tabdialog[0]->ShowWindow(SW_SHOW); + m_tabctrl.m_tabdialog[1]->ShowWindow(SW_HIDE); + m_tabctrl.m_tabdialog[2]->ShowWindow(SW_HIDE); + + m_tabctrl.SetCurSel(0); + m_tabctrl.OnSwitchTabs(); + + return TRUE; +} + + +BEGIN_MESSAGE_MAP(LinkOptions, CDialog) + //{{AFX_MSG_MAP(LinkOptions) + ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnSelchangeTab1) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// LinkOptions message handlers +///////////////////////////////////////////////////////////////////////////// +// LinkGeneral dialog + + +LinkGeneral::LinkGeneral(CWnd* pParent /*=NULL*/) + : CDialog(LinkGeneral::IDD, pParent) +{ + //{{AFX_DATA_INIT(LinkGeneral) + //}}AFX_DATA_INIT +} + +void LinkGeneral::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LinkGeneral) + DDX_Radio(pDX, IDC_LINK_SINGLE, m_type); + DDX_Control(pDX, IDC_LINKTIMEOUT, m_timeout); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(LinkGeneral, CDialog) + //{{AFX_MSG_MAP(LinkGeneral) + ON_BN_CLICKED(IDC_LINK_SINGLE, OnRadio1) + ON_BN_CLICKED(IDC_LINK_LAN, OnRadio2) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// LinkGeneral message handlers +///////////////////////////////////////////////////////////////////////////// +// LinkServer dialog + + +LinkServer::LinkServer(CWnd* pParent /*=NULL*/) + : CDialog(LinkServer::IDD, pParent) +{ + //{{AFX_DATA_INIT(LinkServer) + m_numplayers = -1; + m_prottype = -1; + m_speed = FALSE; + //}}AFX_DATA_INIT +} + + +void LinkServer::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LinkServer) + DDX_Radio(pDX, IDC_LINK2P, m_numplayers); + DDX_Radio(pDX, IDC_LINKTCP, m_prottype); + DDX_Check(pDX, IDC_SSPEED, m_speed); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(LinkServer, CDialog) + //{{AFX_MSG_MAP(LinkServer) + ON_BN_CLICKED(IDC_SERVERSTART, OnServerStart) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// LinkServer message handlers + +LinkClient::LinkClient(CWnd* pParent /*=NULL*/) + : CDialog(LinkClient::IDD, pParent) +{ + //{{AFX_DATA_INIT(LinkClient) + m_prottype = -1; + m_hacks = -1; + //}}AFX_DATA_INIT +} + + +void LinkClient::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LinkClient) + DDX_Control(pDX, IDC_SERVERIP, m_serverip); + DDX_Radio(pDX, IDC_CLINKTCP, m_prottype); + DDX_Radio(pDX, IDC_SPEEDOFF, m_hacks); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(LinkClient, CDialog) + //{{AFX_MSG_MAP(LinkClient) + ON_BN_CLICKED(IDC_LINKCONNECT, OnLinkConnect) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// LinkClient message handlers + +BOOL LinkServer::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_numplayers = lanlink.numgbas; + m_prottype = lanlink.type; + m_speed = lanlink.speed; + + UpdateData(FALSE); + + return TRUE; +} + +void LinkOptions::OnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult) +{ + m_tabctrl.OnSwitchTabs(); + *pResult = 0; +} + +void CMyTabCtrl::OnSwitchTabs(void) +{ + CRect tabRect, itemRect; + int nX, nY, nXc, nYc, i; + + GetClientRect(&tabRect); + GetItemRect(0, &itemRect); + + nX=itemRect.left+10; + nY=itemRect.bottom+12; + nXc=tabRect.right-itemRect.left-2; + nYc=tabRect.bottom-nY+8; + + if(lanlink.active==0) SetCurSel(0); + + for(i=0;i<3;i++){ + if(i==GetCurSel()){ + m_tabdialog[i]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW); + } else { + m_tabdialog[i]->ShowWindow(SW_HIDE); + } + } + return; +} + + +void LinkOptions::OnOk() +{ + GetAllData((LinkGeneral*)m_tabctrl.m_tabdialog[0]); + CDialog::OnOK(); + return; +} + +void LinkGeneral::OnRadio1() +{ + m_type = 0; + lanlink.active = 0; +} + +void LinkGeneral::OnRadio2() +{ + m_type = 1; + lanlink.active = 1; +} + +BOOL LinkGeneral::OnInitDialog(){ + + char timeout[6]; + + CDialog::OnInitDialog(); + + m_timeout.LimitText(5); + sprintf(timeout, "%d", linktimeout); + m_timeout.SetWindowText(timeout); + + m_type = lanlink.active; + + UpdateData(FALSE); + + return TRUE; +} + + +void LinkOptions::OnCancel() +{ + CDialog::OnCancel(); + return; +} + +void LinkServer::OnServerStart() +{ + int errorcode; + ServerWait dlg; + + UpdateData(TRUE); + + lanlink.numgbas = m_numplayers+1; + lanlink.type = m_prottype; + lanlink.server = 1; + lanlink.speed = m_speed==1 ? true : false; + lspeed = lanlink.speed; + + if((errorcode=ls.Init(&dlg))!=0){ + char message[50]; + sprintf(message, "Error %d occured.\nPlease try again.", errorcode); + MessageBox(message, "Error", MB_OK); + return; + } + + dlg.DoModal(); + + return; +} + +BOOL LinkClient::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_prottype = lanlink.type; + m_hacks = lanlink.speed; + + UpdateData(FALSE); + + return TRUE; +} + +void LinkClient::OnLinkConnect() +{ + char ipaddress[31]; + int errorcode; + ServerWait dlg; + + UpdateData(TRUE); + + lanlink.type = m_prottype; + lanlink.server = 0; + lanlink.speed = m_hacks==1 ? true : false; + lspeed = lanlink.speed; + + m_serverip.GetWindowText(ipaddress, 30); + + if((errorcode=lc.Init(gethostbyname(ipaddress), &dlg))!=0){ + char message[50]; + sprintf(message, "Error %d occured.\nPlease try again.", errorcode); + MessageBox(message, "Error", MB_OK); + return; + } + dlg.DoModal(); + return; +} + +void LinkOptions::GetAllData(LinkGeneral *src) +{ + char timeout[6]; + + src->UpdateData(true); + + src->m_timeout.GetWindowText(timeout, 5); + sscanf(timeout, "%d", &linktimeout); + + if(src->m_type==0){ + lanlink.speed = 0; + lspeed = 0; + } + + return; +} +///////////////////////////////////////////////////////////////////////////// +// ServerWait dialog + + +ServerWait::ServerWait(CWnd* pParent /*=NULL*/) + : CDialog(ServerWait::IDD, pParent) +{ + //{{AFX_DATA_INIT(ServerWait) + m_serveraddress = _T(""); + m_plconn[0] = _T(""); + m_plconn[1] = _T(""); + m_plconn[2] = _T(""); + //}}AFX_DATA_INIT +} + + +void ServerWait::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(ServerWait) + DDX_Control(pDX, IDC_SERVERWAIT, m_prgctrl); + DDX_Text(pDX, IDC_STATIC1, m_serveraddress); + DDX_Text(pDX, IDC_STATIC2, m_plconn[0]); + DDX_Text(pDX, IDC_STATIC3, m_plconn[1]); + DDX_Text(pDX, IDC_STATIC4, m_plconn[2]); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(ServerWait, CDialog) + //{{AFX_MSG_MAP(ServerWait) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// ServerWait message handlers + +void ServerWait::OnCancel() +{ + lanlink.terminate = true; + CDialog::OnCancel(); +} + +BOOL LinkGeneral::PreTranslateMessage(MSG* pMsg) +{ + if(pMsg->message==WM_KEYDOWN) + if(pMsg->wParam==VK_RETURN||pMsg->wParam==VK_ESCAPE) + pMsg->wParam = NULL; + + return CDialog::PreTranslateMessage(pMsg); +} + +BOOL LinkClient::PreTranslateMessage(MSG* pMsg) +{ + if(pMsg->message==WM_KEYDOWN) + if(pMsg->wParam==VK_RETURN||pMsg->wParam==VK_ESCAPE) + pMsg->wParam = NULL; + + return CDialog::PreTranslateMessage(pMsg); +} + +BOOL LinkServer::PreTranslateMessage(MSG* pMsg) +{ + if(pMsg->message==WM_KEYDOWN) + if(pMsg->wParam==VK_RETURN||pMsg->wParam==VK_ESCAPE) + pMsg->wParam = NULL; + + return CDialog::PreTranslateMessage(pMsg); +} diff --git a/src/win32/LinkOptions.h b/src/win32/LinkOptions.h new file mode 100644 index 00000000..2bb4b6c2 --- /dev/null +++ b/src/win32/LinkOptions.h @@ -0,0 +1,207 @@ +#if !defined(AFX_LINKOPTIONS_H__55E28DAC_10CC_4AB4_9FAC_B036AA8017D3__INCLUDED_) +#define AFX_LINKOPTIONS_H__55E28DAC_10CC_4AB4_9FAC_B036AA8017D3__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// LinkOptions.h : header file +// + +class CMyTabCtrl : public CTabCtrl +{ +public: + CMyTabCtrl(void); + ~CMyTabCtrl(void); + CDialog *m_tabdialog[3]; + void OnSwitchTabs(void); +}; + +///////////////////////////////////////////////////////////////////////////// +// LinkGeneral dialog + +class LinkGeneral : public CDialog +{ +// Construction +public: + LinkGeneral(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(LinkGeneral) + enum { IDD = IDD_LINKTAB1 }; + int m_type; + CEdit m_timeout; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LinkGeneral) + public: + virtual BOOL PreTranslateMessage(MSG* pMsg); + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(LinkGeneral) + virtual BOOL OnInitDialog(); + afx_msg void OnRadio1(); + afx_msg void OnRadio2(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// LinkOptions dialog + +class LinkOptions : public CDialog +{ +// Construction +public: + LinkOptions(CWnd* pParent = NULL); // standard constructor + void GetAllData(LinkGeneral*); +// Dialog Data + //{{AFX_DATA(LinkOptions) + enum { IDD = IDD_LINKTAB }; + CMyTabCtrl m_tabctrl; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LinkOptions) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(LinkOptions) + afx_msg void OnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult); + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// LinkServer dialog + +class LinkServer : public CDialog +{ +// Construction +public: + LinkServer(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(LinkServer) + enum { IDD = IDD_LINKTAB2 }; + int m_numplayers; + int m_prottype; + BOOL m_speed; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LinkServer) + public: + virtual BOOL PreTranslateMessage(MSG* pMsg); + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(LinkServer) + virtual BOOL OnInitDialog(); + afx_msg void OnServerStart(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +class LinkClient : public CDialog +{ +// Construction +public: + LinkClient(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(LinkClient) + enum { IDD = IDD_LINKTAB3 }; + CEdit m_serverip; + int m_prottype; + int m_hacks; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LinkClient) + public: + virtual BOOL PreTranslateMessage(MSG* pMsg); + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(LinkClient) + virtual BOOL OnInitDialog(); + afx_msg void OnLinkConnect(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////// +// ServerWait dialog + +class ServerWait : public CDialog +{ +// Construction +public: + ServerWait(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(ServerWait) + enum { IDD = IDD_SERVERWAIT }; + CProgressCtrl m_prgctrl; + CString m_serveraddress; + CString m_plconn[3]; + //CString m_p2conn; + //CString m_p3conn; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ServerWait) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + afx_msg void OnCancel(); + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(ServerWait) + // NOTE: the ClassWizard will add member functions here + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_LINKOPTIONS_H__55E28DAC_10CC_4AB4_9FAC_B036AA8017D3__INCLUDED_) diff --git a/src/win32/Logging.cpp b/src/win32/Logging.cpp new file mode 100644 index 00000000..697b6d42 --- /dev/null +++ b/src/win32/Logging.cpp @@ -0,0 +1,287 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Logging.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" + +#include "FileDlg.h" +#include "Logging.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// Logging dialog + +Logging *Logging::instance = NULL; +CString Logging::text; + +Logging::Logging(CWnd* pParent /*=NULL*/) + : ResizeDlg(Logging::IDD, pParent) +{ + //{{AFX_DATA_INIT(Logging) + m_swi = FALSE; + m_unaligned_access = FALSE; + m_illegal_write = FALSE; + m_illegal_read = FALSE; + m_dma0 = FALSE; + m_dma1 = FALSE; + m_dma2 = FALSE; + m_dma3 = FALSE; + m_agbprint = FALSE; + m_undefined = FALSE; + //}}AFX_DATA_INIT +} + + +void Logging::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Logging) + DDX_Control(pDX, IDC_LOG, m_log); + DDX_Check(pDX, IDC_VERBOSE_SWI, m_swi); + DDX_Check(pDX, IDC_VERBOSE_UNALIGNED_ACCESS, m_unaligned_access); + DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_WRITE, m_illegal_write); + DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_READ, m_illegal_read); + DDX_Check(pDX, IDC_VERBOSE_DMA0, m_dma0); + DDX_Check(pDX, IDC_VERBOSE_DMA1, m_dma1); + DDX_Check(pDX, IDC_VERBOSE_DMA2, m_dma2); + DDX_Check(pDX, IDC_VERBOSE_DMA3, m_dma3); + DDX_Check(pDX, IDC_VERBOSE_AGBPRINT, m_agbprint); + DDX_Check(pDX, IDC_VERBOSE_UNDEFINED, m_undefined); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(Logging, CDialog) + //{{AFX_MSG_MAP(Logging) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(IDC_CLEAR, OnClear) + ON_BN_CLICKED(IDC_VERBOSE_AGBPRINT, OnVerboseAgbprint) + ON_BN_CLICKED(IDC_VERBOSE_DMA0, OnVerboseDma0) + ON_BN_CLICKED(IDC_VERBOSE_DMA1, OnVerboseDma1) + ON_BN_CLICKED(IDC_VERBOSE_DMA2, OnVerboseDma2) + ON_BN_CLICKED(IDC_VERBOSE_DMA3, OnVerboseDma3) + ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_READ, OnVerboseIllegalRead) + ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_WRITE, OnVerboseIllegalWrite) + ON_BN_CLICKED(IDC_VERBOSE_SWI, OnVerboseSwi) + ON_BN_CLICKED(IDC_VERBOSE_UNALIGNED_ACCESS, OnVerboseUnalignedAccess) + ON_BN_CLICKED(IDC_VERBOSE_UNDEFINED, OnVerboseUndefined) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_EN_ERRSPACE(IDC_LOG, OnErrspaceLog) + ON_EN_MAXTEXT(IDC_LOG, OnMaxtextLog) + ON_WM_CLOSE() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Logging message handlers + +void Logging::OnOk() +{ + EndDialog(TRUE); + + instance = NULL; +} + +void Logging::OnClear() +{ + text = ""; + m_log.SetWindowText(""); +} + +void Logging::OnVerboseAgbprint() +{ + systemVerbose ^= 512; +} + +void Logging::OnVerboseDma0() +{ + systemVerbose ^= 16; +} + +void Logging::OnVerboseDma1() +{ + systemVerbose ^= 32; +} + +void Logging::OnVerboseDma2() +{ + systemVerbose ^= 64; +} + +void Logging::OnVerboseDma3() +{ + systemVerbose ^= 128; +} + +void Logging::OnVerboseIllegalRead() +{ + systemVerbose ^= 8; +} + +void Logging::OnVerboseIllegalWrite() +{ + systemVerbose ^= 4; +} + +void Logging::OnVerboseSwi() +{ + systemVerbose ^= 1; +} + +void Logging::OnVerboseUnalignedAccess() +{ + systemVerbose ^= 2; +} + +void Logging::OnVerboseUndefined() +{ + systemVerbose ^= 256; +} + +void Logging::OnSave() +{ + int len = m_log.GetWindowTextLength(); + + char *mem = (char *)malloc(len); + + if(mem) { + LPCTSTR exts[] = { ".txt" }; + m_log.GetWindowText(mem, len); + CString filter = "All Files|*.*||"; + FileDlg dlg(this, "", filter, 0, + NULL, exts, NULL, "Save output", true); + + if(dlg.DoModal() == IDOK) { + FILE *f = fopen(dlg.GetPathName(), "w"); + if(f) { + fwrite(mem, 1, len, f); + fclose(f); + } + } + } + + free(mem); +} + +void Logging::OnErrspaceLog() +{ + systemMessage(0, "Error allocating space"); +} + +void Logging::OnMaxtextLog() +{ + systemMessage(0, "Max text length reached %d", m_log.GetLimitText()); +} + +void Logging::PostNcDestroy() +{ + delete this; +} + +BOOL Logging::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_LOG, DS_SizeY|DS_SizeX) + DIALOG_SIZER_ENTRY( ID_OK, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLEAR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\LogView", + NULL); + m_swi = (systemVerbose & 1) != 0; + m_unaligned_access = (systemVerbose & 2) != 0; + m_illegal_write = (systemVerbose & 4) != 0; + m_illegal_read = (systemVerbose & 8) != 0; + m_dma0 = (systemVerbose & 16) != 0; + m_dma1 = (systemVerbose & 32) != 0; + m_dma2 = (systemVerbose & 64) != 0; + m_dma3 = (systemVerbose & 128) != 0; + m_undefined = (systemVerbose & 256) != 0; + m_agbprint = (systemVerbose & 256) != 0; + UpdateData(FALSE); + + m_log.LimitText(-1); + m_log.SetWindowText(text); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Logging::log(const char *s) +{ + int size = ::SendMessage(m_log, WM_GETTEXTLENGTH, 0, 0); + m_log.SetSel(size, size); + m_log.ReplaceSel(s); +} + +void Logging::OnClose() +{ + EndDialog(FALSE); + + instance = NULL; + + CDialog::OnClose(); +} + +void toolsLogging() +{ + if(Logging::instance == NULL) { + Logging::instance = new Logging(); + Logging::instance->Create(IDD_LOGGING, AfxGetApp()->m_pMainWnd); + Logging::instance->ShowWindow(SW_SHOW); + } else { + Logging::instance->SetForegroundWindow(); + } +} + +void toolsLog(const char *s) +{ + CString str; + int state = 0; + if(s) { + char c = *s++; + while(c) { + if(c == '\n' && state == 0) + str += '\r'; + else if(c == '\r') + state = 1; + else + state = 0; + str += c; + c = *s++; + } + } + + Logging::text += str; + if(Logging::instance != NULL) { + Logging::instance->log(str); + } +} diff --git a/src/win32/Logging.h b/src/win32/Logging.h new file mode 100644 index 00000000..01514f61 --- /dev/null +++ b/src/win32/Logging.h @@ -0,0 +1,98 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_) +#define AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Logging.h : header file +// + +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// Logging dialog + +class Logging : public ResizeDlg +{ + // Construction + public: + void log(const char *); + Logging(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Logging) + enum { IDD = IDD_LOGGING }; + CEdit m_log; + BOOL m_swi; + BOOL m_unaligned_access; + BOOL m_illegal_write; + BOOL m_illegal_read; + BOOL m_dma0; + BOOL m_dma1; + BOOL m_dma2; + BOOL m_dma3; + BOOL m_agbprint; + BOOL m_undefined; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Logging) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(Logging) + afx_msg void OnOk(); + afx_msg void OnClear(); + afx_msg void OnVerboseAgbprint(); + afx_msg void OnVerboseDma0(); + afx_msg void OnVerboseDma1(); + afx_msg void OnVerboseDma2(); + afx_msg void OnVerboseDma3(); + afx_msg void OnVerboseIllegalRead(); + afx_msg void OnVerboseIllegalWrite(); + afx_msg void OnVerboseSwi(); + afx_msg void OnVerboseUnalignedAccess(); + afx_msg void OnVerboseUndefined(); + afx_msg void OnSave(); + afx_msg void OnErrspaceLog(); + afx_msg void OnMaxtextLog(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + public: + static Logging *instance; + static CString text; +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_) diff --git a/src/win32/MainWnd.cpp b/src/win32/MainWnd.cpp new file mode 100644 index 00000000..227576f9 --- /dev/null +++ b/src/win32/MainWnd.cpp @@ -0,0 +1,1137 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MainWnd.cpp : implementation file +// + +#include "stdafx.h" +#include "VBA.h" +#include "MainWnd.h" + +#include + +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../cheatSearch.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Flash.h" +#include "../Globals.h" +#include "../gb/GB.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" +#include "../RTC.h" +#include "../Sound.h" +#include "../Util.h" +#include ".\mainwnd.h" + +#include "../Link.h" // Link + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +#define VBA_CONFIRM_MODE WM_APP + 100 + +extern void remoteCleanUp(); + +///////////////////////////////////////////////////////////////////////////// +// MainWnd + +MainWnd::MainWnd() +{ + m_hAccelTable = NULL; + arrow = LoadCursor(NULL, IDC_ARROW); +} + +MainWnd::~MainWnd() +{ +} + + +BEGIN_MESSAGE_MAP(MainWnd, CWnd) + //{{AFX_MSG_MAP(MainWnd) + ON_WM_CLOSE() + ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout) + ON_COMMAND(ID_HELP_FAQ, OnHelpFaq) + ON_COMMAND(ID_FILE_OPEN, OnFileOpen) + ON_WM_INITMENUPOPUP() + ON_COMMAND(ID_FILE_PAUSE, OnFilePause) + ON_UPDATE_COMMAND_UI(ID_FILE_PAUSE, OnUpdateFilePause) + ON_COMMAND(ID_FILE_RESET, OnFileReset) + ON_UPDATE_COMMAND_UI(ID_FILE_RESET, OnUpdateFileReset) + ON_UPDATE_COMMAND_UI(ID_FILE_RECENT_FREEZE, OnUpdateFileRecentFreeze) + ON_COMMAND(ID_FILE_RECENT_RESET, OnFileRecentReset) + ON_COMMAND(ID_FILE_RECENT_FREEZE, OnFileRecentFreeze) + ON_COMMAND(ID_FILE_EXIT, OnFileExit) + ON_COMMAND(ID_FILE_CLOSE, OnFileClose) + ON_UPDATE_COMMAND_UI(ID_FILE_CLOSE, OnUpdateFileClose) + ON_COMMAND(ID_FILE_OPENGAMEBOY, OnFileOpengameboy) + ON_COMMAND(ID_FILE_LOAD, OnFileLoad) + ON_UPDATE_COMMAND_UI(ID_FILE_LOAD, OnUpdateFileLoad) + ON_COMMAND(ID_FILE_SAVE, OnFileSave) + ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave) + ON_COMMAND(ID_FILE_IMPORT_BATTERYFILE, OnFileImportBatteryfile) + ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_BATTERYFILE, OnUpdateFileImportBatteryfile) + ON_COMMAND(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnFileImportGamesharkcodefile) + ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnUpdateFileImportGamesharkcodefile) + ON_COMMAND(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnFileImportGamesharksnapshot) + ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnUpdateFileImportGamesharksnapshot) + ON_COMMAND(ID_FILE_EXPORT_BATTERYFILE, OnFileExportBatteryfile) + ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_BATTERYFILE, OnUpdateFileExportBatteryfile) + ON_COMMAND(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnFileExportGamesharksnapshot) + ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnUpdateFileExportGamesharksnapshot) + ON_COMMAND(ID_FILE_SCREENCAPTURE, OnFileScreencapture) + ON_UPDATE_COMMAND_UI(ID_FILE_SCREENCAPTURE, OnUpdateFileScreencapture) + ON_COMMAND(ID_FILE_ROMINFORMATION, OnFileRominformation) + ON_UPDATE_COMMAND_UI(ID_FILE_ROMINFORMATION, OnUpdateFileRominformation) + ON_COMMAND(ID_FILE_TOGGLEMENU, OnFileTogglemenu) + ON_UPDATE_COMMAND_UI(ID_FILE_TOGGLEMENU, OnUpdateFileTogglemenu) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE, OnUpdateOptionsFrameskipThrottleNothrottle) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnUpdateOptionsFrameskipThrottle25) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnUpdateOptionsFrameskipThrottle50) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnUpdateOptionsFrameskipThrottle100) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnUpdateOptionsFrameskipThrottle150) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnUpdateOptionsFrameskipThrottle200) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnUpdateOptionsFrameskipThrottleOther) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE, OnOptionsFrameskipThrottleNothrottle) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnOptionsFrameskipThrottle25) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnOptionsFrameskipThrottle50) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnOptionsFrameskipThrottle100) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnOptionsFrameskipThrottle150) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnOptionsFrameskipThrottle200) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnOptionsFrameskipThrottleOther) + ON_COMMAND(ID_OPTIONS_FRAMESKIP_AUTOMATIC, OnOptionsFrameskipAutomatic) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_AUTOMATIC, OnUpdateOptionsFrameskipAutomatic) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_0, OnUpdateOptionsVideoFrameskip0) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_1, OnUpdateOptionsVideoFrameskip1) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_2, OnUpdateOptionsVideoFrameskip2) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_3, OnUpdateOptionsVideoFrameskip3) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_4, OnUpdateOptionsVideoFrameskip4) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_5, OnUpdateOptionsVideoFrameskip5) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_6, OnUpdateOptionsVideoFrameskip6) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_7, OnUpdateOptionsVideoFrameskip7) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_8, OnUpdateOptionsVideoFrameskip8) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_9, OnUpdateOptionsVideoFrameskip9) + ON_COMMAND(ID_OPTIONS_VIDEO_VSYNC, OnOptionsVideoVsync) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_VSYNC, OnUpdateOptionsVideoVsync) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X1, OnUpdateOptionsVideoX1) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X2, OnUpdateOptionsVideoX2) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X3, OnUpdateOptionsVideoX3) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X4, OnUpdateOptionsVideoX4) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnUpdateOptionsVideoFullscreen320x240) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnUpdateOptionsVideoFullscreen640x480) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnUpdateOptionsVideoFullscreen800x600) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnOptionsVideoFullscreen320x240) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnOptionsVideoFullscreen640x480) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnOptionsVideoFullscreen800x600) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN, OnOptionsVideoFullscreen) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN, OnUpdateOptionsVideoFullscreen) + ON_WM_MOVE() + ON_WM_SIZE() + ON_COMMAND(ID_OPTIONS_VIDEO_DISABLESFX, OnOptionsVideoDisablesfx) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DISABLESFX, OnUpdateOptionsVideoDisablesfx) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnOptionsVideoFullscreenstretchtofit) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnUpdateOptionsVideoFullscreenstretchtofit) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnOptionsVideoRendermethodGdi) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnUpdateOptionsVideoRendermethodGdi) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnOptionsVideoRendermethodDirectdraw) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnUpdateOptionsVideoRendermethodDirectdraw) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnOptionsVideoRendermethodDirect3d) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnUpdateOptionsVideoRendermethodDirect3d) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnOptionsVideoRendermethodOpengl) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnUpdateOptionsVideoRendermethodOpengl) + ON_COMMAND(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnOptionsVideoTriplebuffering) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnUpdateOptionsVideoTriplebuffering) + ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnOptionsVideoDdrawemulationonly) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnUpdateOptionsVideoDdrawemulationonly) + ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnOptionsVideoDdrawusevideomemory) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnUpdateOptionsVideoDdrawusevideomemory) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnOptionsVideoRenderoptionsD3dnofilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnUpdateOptionsVideoRenderoptionsD3dnofilter) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnOptionsVideoRenderoptionsD3dbilinear) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnUpdateOptionsVideoRenderoptionsD3dbilinear) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnOptionsVideoRenderoptionsGlnearest) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnUpdateOptionsVideoRenderoptionsGlnearest) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnOptionsVideoRenderoptionsGlbilinear) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnUpdateOptionsVideoRenderoptionsGlbilinear) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnOptionsVideoRenderoptionsGltriangle) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnUpdateOptionsVideoRenderoptionsGltriangle) + ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnOptionsVideoRenderoptionsGlquads) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnUpdateOptionsVideoRenderoptionsGlquads) + ON_WM_CONTEXTMENU() + ON_COMMAND(ID_OPTIONS_EMULATOR_ASSOCIATE, OnOptionsEmulatorAssociate) + ON_COMMAND(ID_OPTIONS_EMULATOR_DIRECTORIES, OnOptionsEmulatorDirectories) + ON_COMMAND(ID_OPTIONS_EMULATOR_DISABLESTATUSMESSAGES, OnOptionsEmulatorDisablestatusmessages) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_DISABLESTATUSMESSAGES, OnUpdateOptionsEmulatorDisablestatusmessages) + ON_COMMAND(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnOptionsEmulatorSynchronize) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnUpdateOptionsEmulatorSynchronize) + ON_COMMAND(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnOptionsEmulatorPausewheninactive) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnUpdateOptionsEmulatorPausewheninactive) + ON_COMMAND(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnOptionsEmulatorSpeeduptoggle) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnUpdateOptionsEmulatorSpeeduptoggle) + ON_COMMAND(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnOptionsEmulatorRemoveintrosgba) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnUpdateOptionsEmulatorRemoveintrosgba) + ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnOptionsEmulatorAutomaticallyipspatch) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnUpdateOptionsEmulatorAutomaticallyipspatch) + ON_COMMAND(ID_OPTIONS_EMULATOR_AGBPRINT, OnOptionsEmulatorAgbprint) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AGBPRINT, OnUpdateOptionsEmulatorAgbprint) + ON_COMMAND(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnOptionsEmulatorRealtimeclock) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnUpdateOptionsEmulatorRealtimeclock) + ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnOptionsEmulatorAutohidemenu) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnUpdateOptionsEmulatorAutohidemenu) + ON_COMMAND(ID_OPTIONS_EMULATOR_REWINDINTERVAL, OnOptionsEmulatorRewindinterval) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnOptionsEmulatorSavetypeAutomatic) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnUpdateOptionsEmulatorSavetypeAutomatic) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnOptionsEmulatorSavetypeEeprom) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnUpdateOptionsEmulatorSavetypeEeprom) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnOptionsEmulatorSavetypeSram) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnUpdateOptionsEmulatorSavetypeSram) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnOptionsEmulatorSavetypeFlash) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnUpdateOptionsEmulatorSavetypeFlash) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnOptionsEmulatorSavetypeEepromsensor) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnUpdateOptionsEmulatorSavetypeEepromsensor) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnOptionsEmulatorSavetypeNone) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnUpdateOptionsEmulatorSavetypeNone) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnOptionsEmulatorSavetypeFlash512k) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnUpdateOptionsEmulatorSavetypeFlash512k) + ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnOptionsEmulatorSavetypeFlash1m) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnUpdateOptionsEmulatorSavetypeFlash1m) + ON_COMMAND(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnOptionsEmulatorUsebiosfile) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnUpdateOptionsEmulatorUsebiosfile) + ON_COMMAND(ID_OPTIONS_EMULATOR_SKIPBIOS, OnOptionsEmulatorSkipbios) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SKIPBIOS, OnUpdateOptionsEmulatorSkipbios) + ON_COMMAND(ID_OPTIONS_EMULATOR_SELECTBIOSFILE, OnOptionsEmulatorSelectbiosfile) + ON_COMMAND(ID_OPTIONS_EMULATOR_PNGFORMAT, OnOptionsEmulatorPngformat) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PNGFORMAT, OnUpdateOptionsEmulatorPngformat) + ON_COMMAND(ID_OPTIONS_EMULATOR_BMPFORMAT, OnOptionsEmulatorBmpformat) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_BMPFORMAT, OnUpdateOptionsEmulatorBmpformat) +// ON_COMMAND(ID_OPTIONS_SOUND_OFF, OnOptionsSoundOff) /* mute hax */ +// ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_OFF, OnUpdateOptionsSoundOff) /* mute hax */ + ON_COMMAND(ID_OPTIONS_SOUND_OFF, OnOptionsSoundMute) /* mute hax */ + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_OFF, OnUpdateOptionsSoundMute) /* mute hax */ + ON_COMMAND(ID_OPTIONS_SOUND_ON, OnOptionsSoundOn) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ON, OnUpdateOptionsSoundOn) + ON_COMMAND(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnOptionsSoundUseoldsynchronization) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnUpdateOptionsSoundUseoldsynchronization) + ON_COMMAND(ID_OPTIONS_SOUND_ECHO, OnOptionsSoundEcho) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ECHO, OnUpdateOptionsSoundEcho) + ON_COMMAND(ID_OPTIONS_SOUND_LOWPASSFILTER, OnOptionsSoundLowpassfilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_LOWPASSFILTER, OnUpdateOptionsSoundLowpassfilter) + ON_COMMAND(ID_OPTIONS_SOUND_REVERSESTEREO, OnOptionsSoundReversestereo) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_REVERSESTEREO, OnUpdateOptionsSoundReversestereo) + ON_COMMAND(ID_OPTIONS_SOUND_11KHZ, OnOptionsSound11khz) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_11KHZ, OnUpdateOptionsSound11khz) + ON_COMMAND(ID_OPTIONS_SOUND_22KHZ, OnOptionsSound22khz) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_22KHZ, OnUpdateOptionsSound22khz) + ON_COMMAND(ID_OPTIONS_SOUND_44KHZ, OnOptionsSound44khz) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_44KHZ, OnUpdateOptionsSound44khz) + ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL1, OnOptionsSoundChannel1) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL1, OnUpdateOptionsSoundChannel1) + ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL2, OnOptionsSoundChannel2) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL2, OnUpdateOptionsSoundChannel2) + ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL3, OnOptionsSoundChannel3) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL3, OnUpdateOptionsSoundChannel3) + ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL4, OnOptionsSoundChannel4) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL4, OnUpdateOptionsSoundChannel4) + ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnOptionsSoundDirectsounda) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnUpdateOptionsSoundDirectsounda) + ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnOptionsSoundDirectsoundb) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnUpdateOptionsSoundDirectsoundb) + ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDER, OnOptionsGameboyBorder) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDER, OnUpdateOptionsGameboyBorder) + ON_COMMAND(ID_OPTIONS_GAMEBOY_PRINTER, OnOptionsGameboyPrinter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_PRINTER, OnUpdateOptionsGameboyPrinter) + ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnOptionsGameboyBorderAutomatic) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnUpdateOptionsGameboyBorderAutomatic) + ON_COMMAND(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnOptionsGameboyAutomatic) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnUpdateOptionsGameboyAutomatic) + ON_COMMAND(ID_OPTIONS_GAMEBOY_GBA, OnOptionsGameboyGba) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GBA, OnUpdateOptionsGameboyGba) + ON_COMMAND(ID_OPTIONS_GAMEBOY_CGB, OnOptionsGameboyCgb) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_CGB, OnUpdateOptionsGameboyCgb) + ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB, OnOptionsGameboySgb) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB, OnUpdateOptionsGameboySgb) + ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB2, OnOptionsGameboySgb2) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB2, OnUpdateOptionsGameboySgb2) + ON_COMMAND(ID_OPTIONS_GAMEBOY_GB, OnOptionsGameboyGb) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GB, OnUpdateOptionsGameboyGb) + ON_COMMAND(ID_OPTIONS_GAMEBOY_REALCOLORS, OnOptionsGameboyRealcolors) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_REALCOLORS, OnUpdateOptionsGameboyRealcolors) + ON_COMMAND(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnOptionsGameboyGameboycolors) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnUpdateOptionsGameboyGameboycolors) + ON_COMMAND(ID_OPTIONS_GAMEBOY_COLORS, OnOptionsGameboyColors) + ON_COMMAND(ID_OPTIONS_FILTER_DISABLEMMX, OnOptionsFilterDisablemmx) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_DISABLEMMX, OnUpdateOptionsFilterDisablemmx) + ON_COMMAND(ID_OPTIONS_LANGUAGE_SYSTEM, OnOptionsLanguageSystem) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_SYSTEM, OnUpdateOptionsLanguageSystem) + ON_COMMAND(ID_OPTIONS_LANGUAGE_ENGLISH, OnOptionsLanguageEnglish) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_ENGLISH, OnUpdateOptionsLanguageEnglish) + ON_COMMAND(ID_OPTIONS_LANGUAGE_OTHER, OnOptionsLanguageOther) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_OTHER, OnUpdateOptionsLanguageOther) + ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnOptionsJoypadConfigure1) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnUpdateOptionsJoypadConfigure1) + ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnOptionsJoypadConfigure2) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnUpdateOptionsJoypadConfigure2) + ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnOptionsJoypadConfigure3) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnUpdateOptionsJoypadConfigure3) + ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnOptionsJoypadConfigure4) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnUpdateOptionsJoypadConfigure4) + ON_COMMAND(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnOptionsJoypadMotionconfigure) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnUpdateOptionsJoypadMotionconfigure) + ON_COMMAND(ID_CHEATS_SEARCHFORCHEATS, OnCheatsSearchforcheats) + ON_UPDATE_COMMAND_UI(ID_CHEATS_SEARCHFORCHEATS, OnUpdateCheatsSearchforcheats) + ON_COMMAND(ID_CHEATS_CHEATLIST, OnCheatsCheatlist) + ON_UPDATE_COMMAND_UI(ID_CHEATS_CHEATLIST, OnUpdateCheatsCheatlist) + ON_COMMAND(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnCheatsAutomaticsaveloadcheats) + ON_COMMAND(ID_CHEATS_LOADCHEATLIST, OnCheatsLoadcheatlist) + ON_UPDATE_COMMAND_UI(ID_CHEATS_LOADCHEATLIST, OnUpdateCheatsLoadcheatlist) + ON_COMMAND(ID_CHEATS_SAVECHEATLIST, OnCheatsSavecheatlist) + ON_UPDATE_COMMAND_UI(ID_CHEATS_SAVECHEATLIST, OnUpdateCheatsSavecheatlist) + ON_COMMAND(ID_TOOLS_DISASSEMBLE, OnToolsDisassemble) + ON_UPDATE_COMMAND_UI(ID_TOOLS_DISASSEMBLE, OnUpdateToolsDisassemble) + ON_COMMAND(ID_TOOLS_LOGGING, OnToolsLogging) + ON_UPDATE_COMMAND_UI(ID_TOOLS_LOGGING, OnUpdateToolsLogging) + ON_COMMAND(ID_TOOLS_IOVIEWER, OnToolsIoviewer) + ON_UPDATE_COMMAND_UI(ID_TOOLS_IOVIEWER, OnUpdateToolsIoviewer) + ON_COMMAND(ID_TOOLS_MAPVIEW, OnToolsMapview) + ON_UPDATE_COMMAND_UI(ID_TOOLS_MAPVIEW, OnUpdateToolsMapview) + ON_COMMAND(ID_TOOLS_MEMORYVIEWER, OnToolsMemoryviewer) + ON_UPDATE_COMMAND_UI(ID_TOOLS_MEMORYVIEWER, OnUpdateToolsMemoryviewer) + ON_COMMAND(ID_TOOLS_OAMVIEWER, OnToolsOamviewer) + ON_UPDATE_COMMAND_UI(ID_TOOLS_OAMVIEWER, OnUpdateToolsOamviewer) + ON_COMMAND(ID_TOOLS_PALETTEVIEW, OnToolsPaletteview) + ON_UPDATE_COMMAND_UI(ID_TOOLS_PALETTEVIEW, OnUpdateToolsPaletteview) + ON_COMMAND(ID_TOOLS_TILEVIEWER, OnToolsTileviewer) + ON_UPDATE_COMMAND_UI(ID_TOOLS_TILEVIEWER, OnUpdateToolsTileviewer) + ON_COMMAND(ID_DEBUG_NEXTFRAME, OnDebugNextframe) + ON_UPDATE_COMMAND_UI(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnUpdateCheatsAutomaticsaveloadcheats) + ON_COMMAND(ID_TOOLS_DEBUG_GDB, OnToolsDebugGdb) + ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_GDB, OnUpdateToolsDebugGdb) + ON_COMMAND(ID_TOOLS_DEBUG_LOADANDWAIT, OnToolsDebugLoadandwait) + ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_LOADANDWAIT, OnUpdateToolsDebugLoadandwait) + ON_COMMAND(ID_TOOLS_DEBUG_BREAK, OnToolsDebugBreak) + ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_BREAK, OnUpdateToolsDebugBreak) + ON_COMMAND(ID_TOOLS_DEBUG_DISCONNECT, OnToolsDebugDisconnect) + ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_DISCONNECT, OnUpdateToolsDebugDisconnect) + ON_COMMAND(ID_OPTIONS_SOUND_STARTRECORDING, OnOptionsSoundStartrecording) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_STARTRECORDING, OnUpdateOptionsSoundStartrecording) + ON_COMMAND(ID_OPTIONS_SOUND_STOPRECORDING, OnOptionsSoundStoprecording) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_STOPRECORDING, OnUpdateOptionsSoundStoprecording) + ON_COMMAND(ID_TOOLS_RECORD_STARTAVIRECORDING, OnToolsRecordStartavirecording) + ON_UPDATE_COMMAND_UI(ID_TOOLS_RECORD_STARTAVIRECORDING, OnUpdateToolsRecordStartavirecording) + ON_COMMAND(ID_TOOLS_RECORD_STOPAVIRECORDING, OnToolsRecordStopavirecording) + ON_UPDATE_COMMAND_UI(ID_TOOLS_RECORD_STOPAVIRECORDING, OnUpdateToolsRecordStopavirecording) + ON_WM_PAINT() + ON_COMMAND(ID_TOOLS_RECORD_STARTMOVIERECORDING, OnToolsRecordStartmovierecording) + ON_UPDATE_COMMAND_UI(ID_TOOLS_RECORD_STARTMOVIERECORDING, OnUpdateToolsRecordStartmovierecording) + ON_COMMAND(ID_TOOLS_RECORD_STOPMOVIERECORDING, OnToolsRecordStopmovierecording) + ON_UPDATE_COMMAND_UI(ID_TOOLS_RECORD_STOPMOVIERECORDING, OnUpdateToolsRecordStopmovierecording) + ON_COMMAND(ID_TOOLS_PLAY_STARTMOVIEPLAYING, OnToolsPlayStartmovieplaying) + ON_UPDATE_COMMAND_UI(ID_TOOLS_PLAY_STARTMOVIEPLAYING, OnUpdateToolsPlayStartmovieplaying) + ON_COMMAND(ID_TOOLS_PLAY_STOPMOVIEPLAYING, OnToolsPlayStopmovieplaying) + ON_UPDATE_COMMAND_UI(ID_TOOLS_PLAY_STOPMOVIEPLAYING, OnUpdateToolsPlayStopmovieplaying) + ON_COMMAND(ID_TOOLS_REWIND, OnToolsRewind) + ON_UPDATE_COMMAND_UI(ID_TOOLS_REWIND, OnUpdateToolsRewind) + ON_COMMAND(ID_TOOLS_CUSTOMIZE, OnToolsCustomize) + ON_UPDATE_COMMAND_UI(ID_TOOLS_CUSTOMIZE, OnUpdateToolsCustomize) + ON_COMMAND(ID_HELP_BUGREPORT, OnHelpBugreport) + ON_WM_MOUSEMOVE() + ON_WM_INITMENU() + ON_WM_ACTIVATE() + ON_WM_ACTIVATEAPP() + ON_WM_DROPFILES() + ON_COMMAND(ID_FILE_SAVEGAME_OLDESTSLOT, OnFileSavegameOldestslot) + ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_OLDESTSLOT, OnUpdateFileSavegameOldestslot) + ON_COMMAND(ID_FILE_LOADGAME_MOSTRECENT, OnFileLoadgameMostrecent) + ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MOSTRECENT, OnUpdateFileLoadgameMostrecent) + ON_COMMAND(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnFileLoadgameAutoloadmostrecent) + ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnUpdateFileLoadgameAutoloadmostrecent) + ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_25X, OnOptionsSoundVolume25x) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_25X, OnUpdateOptionsSoundVolume25x) + ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_5X, OnOptionsSoundVolume5x) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_5X, OnUpdateOptionsSoundVolume5x) + ON_COMMAND(ID_CHEATS_DISABLECHEATS, OnCheatsDisablecheats) + ON_UPDATE_COMMAND_UI(ID_CHEATS_DISABLECHEATS, OnUpdateCheatsDisablecheats) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE, OnOptionsVideoFullscreenmaxscale) + + ON_COMMAND(ID_OPTIONS_LINK_OPTIONS, OnLinkOptions) + ON_COMMAND(ID_OPTIONS_LINK_LOG, OnOptionsLinkLog) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_LINK_LOG, OnUpdateOptionsLinkLog) + ON_COMMAND(ID_OPTIONS_LINK_WIRELESSADAPTER, OnOptionsLinkRFU) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_LINK_WIRELESSADAPTER, OnUpdateOptionsLinkRFU) + //}}AFX_MSG_MAP + ON_COMMAND_EX_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE10, OnFileRecentFile) + ON_COMMAND_EX_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnFileLoadSlot) + ON_COMMAND_EX_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnFileSaveSlot) + ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnUpdateFileLoadGameSlot) + ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnUpdateFileSaveGameSlot) + ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_FRAMESKIP_0, ID_OPTIONS_VIDEO_FRAMESKIP_5, OnOptionsFrameskip) + ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_FRAMESKIP_6, ID_OPTIONS_VIDEO_FRAMESKIP_9, OnOptionsFrameskip) + ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_X1, ID_OPTIONS_VIDEO_X4, OnOptionVideoSize) + ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnVideoLayer) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnUpdateVideoLayer) + ON_COMMAND_EX_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE, ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT, OnOptionsEmulatorShowSpeed) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE, ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT, OnUpdateOptionsEmulatorShowSpeed) + ON_COMMAND_EX_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnOptionsSoundVolume) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnUpdateOptionsSoundVolume) + ON_COMMAND_EX_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnOptionsPriority) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnUpdateOptionsPriority) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnOptionsFilter) + ON_COMMAND_EX(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, OnOptionsFilter) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnOptionsFilter) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnOptionsFilter) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnOptionsFilter) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_HQ2X, ID_OPTIONS_FILTER_LQ2X, OnOptionsFilter) + ON_COMMAND_EX(ID_OPTIONS_FILTER_HQ3X, OnOptionsFilter) + ON_COMMAND_EX(ID_OPTIONS_FILTER_HQ4X, OnOptionsFilter) + ON_COMMAND_EX(ID_OPTIONS_FILTER_SIMPLE3X, OnOptionsFilter) + ON_COMMAND_EX(ID_OPTIONS_FILTER_SIMPLE4X, OnOptionsFilter) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_HQ2X, ID_OPTIONS_FILTER_LQ2X, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_SIMPLE3X, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_SIMPLE4X, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_HQ3X, OnUpdateOptionsFilter) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_HQ4X, OnUpdateOptionsFilter) + ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE, ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART, OnOptionsFilterIFB) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE, ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART, OnUpdateOptionsFilterIFB) + ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnOptionsJoypadDefault) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnUpdateOptionsJoypadDefault) + ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_R, OnOptionsJoypadAutofire) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_R, OnUpdateOptionsJoypadAutofire) + ON_MESSAGE(VBA_CONFIRM_MODE, OnConfirmMode) + ON_MESSAGE(WM_SYSCOMMAND, OnMySysCommand) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN1280X1024, OnOptionsVideoFullscreen1280x1024) + ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN1024X768, OnOptionsVideoFullscreen1024x768) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN1024X768, OnUpdateOptionsVideoFullscreen1024x768) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN1280X1024, OnUpdateOptionsVideoFullscreen1280x1024) + ON_COMMAND(ID_OPTIONS_FILTER_LCDCOLORS, OnOptionsFilterLcdcolors) + ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_LCDCOLORS, OnUpdateOptionsFilterLcdcolors) + ON_COMMAND_EX_RANGE(ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE, ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE, OnOptionsSoundPcminterpolation) + ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE, ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE, OnUpdateOptionsSoundPcminterpolation) + ON_WM_SETFOCUS() + ON_WM_KILLFOCUS() + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// MainWnd message handlers + +void MainWnd::OnClose() +{ + CWnd::OnClose(); + + delete this; +} + +bool MainWnd::FileRun() +{ + // save battery file before we change the filename... + if(rom != NULL || gbRom != NULL) { + if(theApp.autoSaveLoadCheatList) + winSaveCheatListDefault(); + writeBatteryFile(); + cheatSearchCleanup(&cheatSearchData); + theApp.emulator.emuCleanUp(); + remoteCleanUp(); + emulating = false; +#ifdef APU_LOGGER_H + end_apu_log(); +#endif + } + char tempName[2048]; + char file[2048]; + + utilGetBaseName(theApp.szFile, tempName); + + _fullpath(file, tempName, 1024); + theApp.filename = file; + + int index = theApp.filename.ReverseFind('.'); + if(index != -1) + theApp.filename = theApp.filename.Left(index); + + CString ipsname; + ipsname.Format("%s.ips", theApp.filename); + + if(!theApp.dir.GetLength()) { + int index = theApp.filename.ReverseFind('\\'); + if(index != -1) { + theApp.dir = theApp.filename.Left(index-1); + } + } + + IMAGE_TYPE type = utilFindType(theApp.szFile); + + if(type == IMAGE_UNKNOWN) { + systemMessage(IDS_UNSUPPORTED_FILE_TYPE, + "Unsupported file type: %s", theApp.szFile); + return false; + } + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + theApp.cartridgeType = (int)type; + if(type == IMAGE_GB) { + if(!gbLoadRom(theApp.szFile)) + return false; + theApp.emulator = GBSystem; + gbBorderOn = theApp.winGbBorderOn; + theApp.romSize = gbRomSize; + if(theApp.autoIPS) { + int size = gbRomSize; + utilApplyIPS(ipsname, &gbRom, &size); + if(size != gbRomSize) { + extern bool gbUpdateSizes(); + gbUpdateSizes(); + gbReset(); + theApp.romSize = size; + } + } + } else { + int size = CPULoadRom(theApp.szFile); + if(!size) + return false; + + theApp.romSize = size; + + flashSetSize(theApp.winFlashSize); + rtcEnable(theApp.winRtcEnable); + cpuSaveType = theApp.winSaveType; + + // if(cpuEnhancedDetection && winSaveType == 0) { + // utilGBAFindSave(rom, size); + // } + GetModuleFileName(NULL, tempName, 2048); + + char *p = strrchr(tempName, '\\'); + if(p) + *p = 0; + + char buffer[5]; + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + + strcat(tempName, "\\vba-over.ini"); + + UINT i = GetPrivateProfileInt(buffer, + "rtcEnabled", + -1, + tempName); + if(i != (UINT)-1) + rtcEnable(i == 0 ? false : true); + + i = GetPrivateProfileInt(buffer, + "flashSize", + -1, + tempName); + if(i != (UINT)-1 && (i == 0x10000 || i == 0x20000)) + flashSetSize((int)i); + + i = GetPrivateProfileInt(buffer, + "saveType", + -1, + tempName); + if(i != (UINT)-1 && (i <= 5)) + cpuSaveType = (int)i; + + theApp.emulator = GBASystem; + /* disabled due to problems + if(theApp.removeIntros && rom != NULL) { + *((u32 *)rom)= 0xea00002e; + } + */ + + if(theApp.autoIPS) { + int size = 0x2000000; + utilApplyIPS(ipsname, &rom, &size); + if(size != 0x2000000) { + CPUReset(); + } + } + } + + if(theApp.soundInitialized) { + if(theApp.cartridgeType == 1) + gbSoundReset(); + else + soundReset(); + } else { + //if(!soundOffFlag) /* mute hax */ + soundInit(!theApp.cartridgeType); + if ( soundOffFlag ) + soundDisable(0x30f); + theApp.soundInitialized = true; + } + +#ifdef APU_LOGGER_H + begin_apu_log("apu_log.txt"); +#endif + + if(type == IMAGE_GBA) { + skipBios = theApp.skipBiosFile ? true : false; + CPUInit((char *)(LPCTSTR)theApp.biosFileName, theApp.useBiosFile ? true : false); + CPUReset(); + } + + readBatteryFile(); + + if(theApp.autoSaveLoadCheatList) + winLoadCheatListDefault(); + + theApp.addRecentFile(theApp.szFile); + + theApp.updateWindowSize(theApp.videoOption); + + theApp.updateFrameSkip(); + + if(theApp.autoHideMenu && theApp.videoOption > VIDEO_4X && theApp.menuToggle) + OnFileTogglemenu(); + + emulating = true; + + if(theApp.autoLoadMostRecent) + OnFileLoadgameMostrecent(); + + theApp.frameskipadjust = 0; + theApp.renderedFrames = 0; + theApp.autoFrameSkipLastTime = theApp.throttleLastTime = systemGetClock(); + + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + + return true; +} + +void MainWnd::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu) +{ + ASSERT(pMenu != NULL); + + CCmdUI state; + state.m_pMenu = pMenu; + ASSERT(state.m_pOther == NULL); + ASSERT(state.m_pParentMenu == NULL); + + // determine if menu is popup in top-level menu and set m_pOther to + // it if so (m_pParentMenu == NULL indicates that it is secondary popup) + HMENU hParentMenu; + if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu) + state.m_pParentMenu = pMenu; // parent == child for tracking popup + else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL) { + CWnd* pParent = GetTopLevelParent(); + // child windows don't have menus -- need to go to the top! + if (pParent != NULL && + (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL) { + int nIndexMax = ::GetMenuItemCount(hParentMenu); + for (int nIndex = 0; nIndex < nIndexMax; nIndex++) { + if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu) { + // when popup is found, m_pParentMenu is containing menu + state.m_pParentMenu = CMenu::FromHandle(hParentMenu); + break; + } + } + } + } + + state.m_nIndexMax = pMenu->GetMenuItemCount(); + for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; + state.m_nIndex++) { + state.m_nID = pMenu->GetMenuItemID(state.m_nIndex); + if (state.m_nID == 0) + continue; // menu separator or invalid cmd - ignore it + + ASSERT(state.m_pOther == NULL); + ASSERT(state.m_pMenu != NULL); + if (state.m_nID == (UINT)-1) { + // possibly a popup menu, route to first item of that popup + state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex); + if (state.m_pSubMenu == NULL || + (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 || + state.m_nID == (UINT)-1) { + continue; // first item of popup can't be routed to + } + state.DoUpdate(this, FALSE); // popups are never auto disabled + } else { + // normal menu item + // Auto enable/disable if frame window has 'm_bAutoMenuEnable' + // set and command is _not_ a system command. + state.m_pSubMenu = NULL; + state.DoUpdate(this, state.m_nID < 0xF000); + } + + // adjust for menu deletions and additions + UINT nCount = pMenu->GetMenuItemCount(); + if (nCount < state.m_nIndexMax) { + state.m_nIndex -= (state.m_nIndexMax - nCount); + while (state.m_nIndex < nCount && + pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) { + state.m_nIndex++; + } + } + state.m_nIndexMax = nCount; + } +} + +void MainWnd::OnMove(int x, int y) +{ + CWnd::OnMove(x, y); + + if(!theApp.changingVideoSize) { + if(this) { + if(!IsIconic()) { + RECT r; + + GetWindowRect(&r); + theApp.windowPositionX = r.left; + theApp.windowPositionY = r.top; + theApp.adjustDestRect(); + regSetDwordValue("windowX", theApp.windowPositionX); + regSetDwordValue("windowY", theApp.windowPositionY); + } + } + } +} + +void MainWnd::OnSize(UINT nType, int cx, int cy) +{ + CWnd::OnSize(nType, cx, cy); + + if(!theApp.changingVideoSize) { + if(this) { + if(!IsIconic()) { + if(theApp.iconic) { + if(emulating) { + soundResume(); + theApp.paused = false; + } + } + if(theApp.videoOption <= VIDEO_4X) { + theApp.surfaceSizeX = cx; + theApp.surfaceSizeY = cy; + theApp.adjustDestRect(); + if(theApp.display) + theApp.display->resize(theApp.dest.right-theApp.dest.left, theApp.dest.bottom-theApp.dest.top); + } + } else { + if(emulating) { + if(!theApp.paused) { + theApp.paused = true; + soundPause(); + } + } + theApp.iconic = true; + } + } + } +} + +void MainWnd::winSaveCheatListDefault() +{ + CString name; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + CString dir = regQueryStringValue("saveDir", NULL); + + if(!dir.GetLength()) + dir = getDirFromFile(filename); + + if(isDriveRoot(dir)) + filename.Format("%s%s.clt", dir, name); + else + filename.Format("%s\\%s.clt", dir, name); + + winSaveCheatList(filename); +} + +void MainWnd::winSaveCheatList(const char *name) +{ + if(theApp.cartridgeType == 0) + cheatsSaveCheatList(name); + else + gbCheatsSaveCheatList(name); +} + +void MainWnd::winLoadCheatListDefault() +{ + CString name; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + CString dir = regQueryStringValue("saveDir", NULL); + + if(!dir.GetLength()) + dir = getDirFromFile(filename); + + if(isDriveRoot(dir)) + filename.Format("%s%s.clt", dir, name); + else + filename.Format("%s\\%s.clt", dir, name); + + winLoadCheatList(filename); +} + +void MainWnd::winLoadCheatList(const char *name) +{ + bool res = false; + + if(theApp.cartridgeType == 0) + res = cheatsLoadCheatList(name); + else + res = gbCheatsLoadCheatList(name); + + if(res) + systemScreenMessage(winResLoadString(IDS_LOADED_CHEATS)); +} + +CString MainWnd::getDirFromFile(CString& file) +{ + CString temp = file; + int index = temp.ReverseFind('\\'); + + if(index != -1) { + temp = temp.Left(index); + if(temp.GetLength() == 2 && temp[1] == ':') + temp += "\\"; + } else { + temp = ""; + } + return temp; +} + +bool MainWnd::isDriveRoot(CString& file) +{ + if(file.GetLength() == 3) { + if(file[1] == ':' && file[2] == '\\') + return true; + } + return false; +} + +void MainWnd::writeBatteryFile() +{ + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("batteryDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.sav", saveDir, buffer); + else + filename.Format("%s\\%s.sav", saveDir, buffer); + + if(theApp.emulator.emuWriteBattery) + theApp.emulator.emuWriteBattery(filename); +} + + +void MainWnd::readBatteryFile() +{ + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("batteryDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.sav", saveDir, buffer); + else + filename.Format("%s\\%s.sav", saveDir, buffer); + + bool res = false; + + if(theApp.emulator.emuReadBattery) + res = theApp.emulator.emuReadBattery(filename); + + if(res) + systemScreenMessage(winResLoadString(IDS_LOADED_BATTERY)); +} + +CString MainWnd::winLoadFilter(UINT id) +{ + CString res = winResLoadString(id); + res.Replace('_','|'); + + return res; +} + +bool MainWnd::loadSaveGame(const char *name) +{ + if(theApp.emulator.emuReadState) + return theApp.emulator.emuReadState(name); + return false; +} + +bool MainWnd::writeSaveGame(const char *name) +{ + if(theApp.emulator.emuWriteState) + return theApp.emulator.emuWriteState(name); + return false; +} + +void MainWnd::OnContextMenu(CWnd* pWnd, CPoint point) +{ + winMouseOn(); +} + +bool MainWnd::fileOpenSelect() +{ + theApp.dir = ""; + CString initialDir = regQueryStringValue("romdir","."); + if(!initialDir.IsEmpty()) + theApp.dir = initialDir; + + int selectedFilter = regQueryDwordValue("selectedFilter", 0); + if(selectedFilter < 0 || selectedFilter > 2) + selectedFilter = 0; + + theApp.szFile = ""; + + LPCTSTR exts[] = { "" }; + CString filter = winLoadFilter(IDS_FILTER_ROM); + CString title = winResLoadString(IDS_SELECT_ROM); + + FileDlg dlg(this, "", filter, selectedFilter, "", exts, theApp.dir, title, false); + + if(dlg.DoModal() == IDOK) { + regSetDwordValue("selectedFilter", dlg.m_ofn.nFilterIndex); + theApp.szFile = dlg.GetPathName(); + theApp.dir = theApp.szFile.Left(dlg.m_ofn.nFileOffset); + if(theApp.dir.GetLength() > 3 && theApp.dir[theApp.dir.GetLength()-1] == '\\') + theApp.dir = theApp.dir.Left(theApp.dir.GetLength()-1); + regSetStringValue("romdir", theApp.dir); + return true; + } + return false; +} + +void MainWnd::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + if(emulating) { + theApp.painting = true; + systemDrawScreen(); + theApp.painting = false; + theApp.renderedFrames--; + } +} + +BOOL MainWnd::PreTranslateMessage(MSG* pMsg) +{ + if (CWnd::PreTranslateMessage(pMsg)) + return TRUE; + + if(pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST) { + return theApp.hAccel != NULL && ::TranslateAccelerator(m_hWnd, theApp.hAccel, pMsg); + } + + return FALSE; +} + +void MainWnd::screenCapture(int captureNumber) +{ + CString buffer; + + CString captureDir = regQueryStringValue("captureDir", ""); + int index = theApp.filename.ReverseFind('\\'); + + CString name; + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + + if(captureDir.IsEmpty()) + captureDir = getDirFromFile(theApp.filename); + + LPCTSTR ext = "png"; + if(theApp.captureFormat != 0) + ext = "bmp"; + + if(isDriveRoot(captureDir)) + buffer.Format("%s%s_%02d.%s", + captureDir, + name, + captureNumber, + ext); + else + buffer.Format("%s\\%s_%02d.%s", + captureDir, + name, + captureNumber, + ext); + + if(theApp.captureFormat == 0) + theApp.emulator.emuWritePNG(buffer); + else + theApp.emulator.emuWriteBMP(buffer); + + CString msg = winResLoadString(IDS_SCREEN_CAPTURE); + systemScreenMessage(msg); +} + +void MainWnd::winMouseOn() +{ + SetCursor(arrow); + if(theApp.videoOption > VIDEO_4X) { + theApp.mouseCounter = 10; + } else + theApp.mouseCounter = 0; +} + +void MainWnd::OnMouseMove(UINT nFlags, CPoint point) +{ + winMouseOn(); + + CWnd::OnMouseMove(nFlags, point); +} + +void MainWnd::OnInitMenu(CMenu* pMenu) +{ + CWnd::OnInitMenu(pMenu); + + soundPause(); +} + +void MainWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) +{ + CWnd::OnActivate(nState, pWndOther, bMinimized); + + bool a = (nState == WA_ACTIVE) || (nState == WA_CLICKACTIVE); + + if(a && theApp.input) { + theApp.active = a; + theApp.input->activate(); + if(!theApp.paused) { + if(emulating) { + soundResume(); + } + } + } else { + theApp.wasPaused = true; + if(theApp.pauseWhenInactive) { + if(emulating) { + soundPause(); + } + theApp.active = a; + } + + memset(theApp.delta,255,sizeof(theApp.delta)); + } + + if(theApp.paused && emulating) + { + theApp.painting = true; + systemDrawScreen(); + theApp.painting = false; + theApp.renderedFrames--; + } +} + +#if _MSC_VER <= 1200 +void MainWnd::OnActivateApp(BOOL bActive, HTASK hTask) +#else +void MainWnd::OnActivateApp(BOOL bActive, DWORD hTask) +#endif +{ + CWnd::OnActivateApp(bActive, hTask); + + if(theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) { + if(bActive) { + if(theApp.display) + theApp.display->clear(); + } + } +} + +void MainWnd::OnDropFiles(HDROP hDropInfo) +{ + char szFile[1024]; + + if(DragQueryFile(hDropInfo, + 0, + szFile, + 1024)) { + theApp.szFile = szFile; + if(FileRun()) { + SetForegroundWindow(); + emulating = TRUE; + } else { + emulating = FALSE; + soundPause(); + } + } + DragFinish(hDropInfo); +} + +LRESULT MainWnd::OnMySysCommand(WPARAM wParam, LPARAM lParam) +{ + if(emulating && !theApp.paused) { + if((wParam&0xFFF0) == SC_SCREENSAVE || (wParam&0xFFF0) == SC_MONITORPOWER) + return 0; + } + return Default(); +} + +void MainWnd::OnSetFocus(CWnd * pOldWnd) +{ + theApp.dinputKeyFocus = true; +} + +void MainWnd::OnKillFocus(CWnd * pNewWnd) +{ + theApp.dinputKeyFocus = false; +} diff --git a/src/win32/MainWnd.h b/src/win32/MainWnd.h new file mode 100644 index 00000000..6407b415 --- /dev/null +++ b/src/win32/MainWnd.h @@ -0,0 +1,442 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_) +#define AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MainWnd.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MainWnd window + +class MainWnd : public CWnd +{ + // Construction + public: + MainWnd(); + + // Attributes + public: + + // Operations + public: + bool FileRun(); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MainWnd) + public: + virtual BOOL PreTranslateMessage(MSG* pMsg); + //}}AFX_VIRTUAL + + // Implementation + public: + HCURSOR arrow; + void winMouseOn(); + void screenCapture(int captureNumber); + HACCEL m_hAccelTable; + bool fileOpenSelect(); + afx_msg LRESULT OnConfirmMode(WPARAM, LPARAM); + afx_msg LRESULT OnMySysCommand(WPARAM, LPARAM); + afx_msg void OnUpdateFileLoadGameSlot(CCmdUI *pCmdUI); + afx_msg void OnUpdateFileSaveGameSlot(CCmdUI *pCmdUI); + afx_msg void OnUpdateOptionsJoypadAutofire(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsJoypadAutofire(UINT nID); + afx_msg void OnUpdateOptionsJoypadDefault(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsJoypadDefault(UINT nID); + afx_msg void OnUpdateOptionsFilterIFB(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsFilterIFB(UINT nID); + afx_msg void OnUpdateOptionsFilter(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsFilter(UINT nID); + afx_msg void OnUpdateOptionsPriority(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsPriority(UINT nID); + afx_msg void OnSetFocus(CWnd * pOldWnd); + afx_msg void OnKillFocus(CWnd * pNewWnd); + + void updateSoundChannels(UINT nID); + afx_msg void OnUpdateOptionsSoundVolume(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsSoundVolume(UINT nID); + afx_msg void OnUpdateOptionsEmulatorShowSpeed(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsEmulatorShowSpeed(UINT nID); + afx_msg void OnSystemMinimize(); + afx_msg void OnUpdateVideoLayer(CCmdUI* pCmdUI); + afx_msg BOOL OnVideoLayer(UINT nID); + void winConfirmMode(); + afx_msg BOOL OnOptionVideoSize(UINT nID); + afx_msg BOOL OnOptionsFrameskip(UINT nID); + bool fileImportGSACodeFile(CString& fileName); + bool writeSaveGame(const char *name); + bool loadSaveGame(const char *name); + CString winLoadFilter(UINT id); + void winLoadCheatList(const char *name); + void winLoadCheatListDefault(); + void readBatteryFile(); + void writeBatteryFile(); + bool isDriveRoot(CString& file); + CString getDirFromFile(CString& file); + void winSaveCheatList(const char *name); + void winSaveCheatListDefault(); + virtual ~MainWnd(); + + // Generated message map functions + protected: + //{{AFX_MSG(MainWnd) + afx_msg void OnClose(); + afx_msg void OnHelpAbout(); + afx_msg void OnHelpFaq(); + afx_msg void OnFileOpen(); + afx_msg void OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu); + afx_msg void OnFilePause(); + afx_msg void OnUpdateFilePause(CCmdUI* pCmdUI); + afx_msg void OnFileReset(); + afx_msg void OnUpdateFileReset(CCmdUI* pCmdUI); + afx_msg void OnUpdateFileRecentFreeze(CCmdUI* pCmdUI); + afx_msg void OnFileRecentReset(); + afx_msg void OnFileRecentFreeze(); + afx_msg void OnFileExit(); + afx_msg void OnFileClose(); + afx_msg void OnUpdateFileClose(CCmdUI* pCmdUI); + afx_msg void OnFileOpengameboy(); + afx_msg void OnFileLoad(); + afx_msg void OnUpdateFileLoad(CCmdUI* pCmdUI); + afx_msg void OnFileSave(); + afx_msg void OnUpdateFileSave(CCmdUI* pCmdUI); + afx_msg void OnFileImportBatteryfile(); + afx_msg void OnUpdateFileImportBatteryfile(CCmdUI* pCmdUI); + afx_msg void OnFileImportGamesharkcodefile(); + afx_msg void OnUpdateFileImportGamesharkcodefile(CCmdUI* pCmdUI); + afx_msg void OnFileImportGamesharksnapshot(); + afx_msg void OnUpdateFileImportGamesharksnapshot(CCmdUI* pCmdUI); + afx_msg void OnFileExportBatteryfile(); + afx_msg void OnUpdateFileExportBatteryfile(CCmdUI* pCmdUI); + afx_msg void OnFileExportGamesharksnapshot(); + afx_msg void OnUpdateFileExportGamesharksnapshot(CCmdUI* pCmdUI); + afx_msg void OnFileScreencapture(); + afx_msg void OnUpdateFileScreencapture(CCmdUI* pCmdUI); + afx_msg void OnFileRominformation(); + afx_msg void OnUpdateFileRominformation(CCmdUI* pCmdUI); + afx_msg void OnFileTogglemenu(); + afx_msg void OnUpdateFileTogglemenu(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleNothrottle(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle25(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle50(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle100(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle150(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle200(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleOther(CCmdUI* pCmdUI); + afx_msg void OnOptionsFrameskipThrottleNothrottle(); + afx_msg void OnOptionsFrameskipThrottle25(); + afx_msg void OnOptionsFrameskipThrottle50(); + afx_msg void OnOptionsFrameskipThrottle100(); + afx_msg void OnOptionsFrameskipThrottle150(); + afx_msg void OnOptionsFrameskipThrottle200(); + afx_msg void OnOptionsFrameskipThrottleOther(); + afx_msg void OnOptionsFrameskipAutomatic(); + afx_msg void OnUpdateOptionsFrameskipAutomatic(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip0(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip1(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip2(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip3(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip4(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip5(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip6(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip7(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip8(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip9(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoVsync(); + afx_msg void OnUpdateOptionsVideoVsync(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoX1(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoX2(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoX3(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoX4(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen320x240(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen640x480(CCmdUI* pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen800x600(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoFullscreen320x240(); + afx_msg void OnOptionsVideoFullscreen640x480(); + afx_msg void OnOptionsVideoFullscreen800x600(); + afx_msg void OnOptionsVideoFullscreen(); + afx_msg void OnUpdateOptionsVideoFullscreen(CCmdUI* pCmdUI); + afx_msg void OnMove(int x, int y); + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg void OnOptionsVideoDisablesfx(); + afx_msg void OnUpdateOptionsVideoDisablesfx(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoFullscreenstretchtofit(); + afx_msg void OnUpdateOptionsVideoFullscreenstretchtofit(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRendermethodGdi(); + afx_msg void OnUpdateOptionsVideoRendermethodGdi(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRendermethodDirectdraw(); + afx_msg void OnUpdateOptionsVideoRendermethodDirectdraw(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRendermethodDirect3d(); + afx_msg void OnUpdateOptionsVideoRendermethodDirect3d(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRendermethodOpengl(); + afx_msg void OnUpdateOptionsVideoRendermethodOpengl(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoTriplebuffering(); + afx_msg void OnUpdateOptionsVideoTriplebuffering(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoDdrawemulationonly(); + afx_msg void OnUpdateOptionsVideoDdrawemulationonly(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoDdrawusevideomemory(); + afx_msg void OnUpdateOptionsVideoDdrawusevideomemory(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsD3dnofilter(); + afx_msg void OnUpdateOptionsVideoRenderoptionsD3dnofilter(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsD3dbilinear(); + afx_msg void OnUpdateOptionsVideoRenderoptionsD3dbilinear(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlnearest(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlnearest(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlbilinear(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlbilinear(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGltriangle(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGltriangle(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlquads(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlquads(CCmdUI* pCmdUI); + afx_msg void OnContextMenu(CWnd* pWnd, CPoint point); + afx_msg void OnOptionsEmulatorAssociate(); + afx_msg void OnOptionsEmulatorDirectories(); + afx_msg void OnOptionsEmulatorDisablestatusmessages(); + afx_msg void OnUpdateOptionsEmulatorDisablestatusmessages(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSynchronize(); + afx_msg void OnUpdateOptionsEmulatorSynchronize(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorPausewheninactive(); + afx_msg void OnUpdateOptionsEmulatorPausewheninactive(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSpeeduptoggle(); + afx_msg void OnUpdateOptionsEmulatorSpeeduptoggle(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorRemoveintrosgba(); + afx_msg void OnUpdateOptionsEmulatorRemoveintrosgba(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorAutomaticallyipspatch(); + afx_msg void OnUpdateOptionsEmulatorAutomaticallyipspatch(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorAgbprint(); + afx_msg void OnUpdateOptionsEmulatorAgbprint(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorRealtimeclock(); + afx_msg void OnUpdateOptionsEmulatorRealtimeclock(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorAutohidemenu(); + afx_msg void OnUpdateOptionsEmulatorAutohidemenu(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorRewindinterval(); + afx_msg void OnOptionsEmulatorSavetypeAutomatic(); + afx_msg void OnUpdateOptionsEmulatorSavetypeAutomatic(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeEeprom(); + afx_msg void OnUpdateOptionsEmulatorSavetypeEeprom(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeSram(); + afx_msg void OnUpdateOptionsEmulatorSavetypeSram(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeEepromsensor(); + afx_msg void OnUpdateOptionsEmulatorSavetypeEepromsensor(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeNone(); + afx_msg void OnUpdateOptionsEmulatorSavetypeNone(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash512k(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash512k(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash1m(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash1m(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorUsebiosfile(); + afx_msg void OnUpdateOptionsEmulatorUsebiosfile(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSkipbios(); + afx_msg void OnUpdateOptionsEmulatorSkipbios(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorSelectbiosfile(); + afx_msg void OnOptionsEmulatorPngformat(); + afx_msg void OnUpdateOptionsEmulatorPngformat(CCmdUI* pCmdUI); + afx_msg void OnOptionsEmulatorBmpformat(); + afx_msg void OnUpdateOptionsEmulatorBmpformat(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundOff(); /* mute hax */ + afx_msg void OnUpdateOptionsSoundOff(CCmdUI* pCmdUI); /* mute hax */ + afx_msg void OnOptionsSoundMute(); + afx_msg void OnUpdateOptionsSoundMute(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundOn(); + afx_msg void OnUpdateOptionsSoundOn(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundUseoldsynchronization(); + afx_msg void OnUpdateOptionsSoundUseoldsynchronization(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundEcho(); + afx_msg void OnUpdateOptionsSoundEcho(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundLowpassfilter(); + afx_msg void OnUpdateOptionsSoundLowpassfilter(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundReversestereo(); + afx_msg void OnUpdateOptionsSoundReversestereo(CCmdUI* pCmdUI); + afx_msg void OnOptionsSound11khz(); + afx_msg void OnUpdateOptionsSound11khz(CCmdUI* pCmdUI); + afx_msg void OnOptionsSound22khz(); + afx_msg void OnUpdateOptionsSound22khz(CCmdUI* pCmdUI); + afx_msg void OnOptionsSound44khz(); + afx_msg void OnUpdateOptionsSound44khz(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundChannel1(); + afx_msg void OnUpdateOptionsSoundChannel1(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundChannel2(); + afx_msg void OnUpdateOptionsSoundChannel2(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundChannel3(); + afx_msg void OnUpdateOptionsSoundChannel3(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundChannel4(); + afx_msg void OnUpdateOptionsSoundChannel4(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundDirectsounda(); + afx_msg void OnUpdateOptionsSoundDirectsounda(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundDirectsoundb(); + afx_msg void OnUpdateOptionsSoundDirectsoundb(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyBorder(); + afx_msg void OnUpdateOptionsGameboyBorder(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyPrinter(); + afx_msg void OnUpdateOptionsGameboyPrinter(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyBorderAutomatic(); + afx_msg void OnUpdateOptionsGameboyBorderAutomatic(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyAutomatic(); + afx_msg void OnUpdateOptionsGameboyAutomatic(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyGba(); + afx_msg void OnUpdateOptionsGameboyGba(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyCgb(); + afx_msg void OnUpdateOptionsGameboyCgb(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboySgb(); + afx_msg void OnUpdateOptionsGameboySgb(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboySgb2(); + afx_msg void OnUpdateOptionsGameboySgb2(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyGb(); + afx_msg void OnUpdateOptionsGameboyGb(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyRealcolors(); + afx_msg void OnUpdateOptionsGameboyRealcolors(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyGameboycolors(); + afx_msg void OnUpdateOptionsGameboyGameboycolors(CCmdUI* pCmdUI); + afx_msg void OnOptionsGameboyColors(); + afx_msg void OnOptionsFilterDisablemmx(); + afx_msg void OnUpdateOptionsFilterDisablemmx(CCmdUI* pCmdUI); + afx_msg void OnOptionsLanguageSystem(); + afx_msg void OnUpdateOptionsLanguageSystem(CCmdUI* pCmdUI); + afx_msg void OnOptionsLanguageEnglish(); + afx_msg void OnUpdateOptionsLanguageEnglish(CCmdUI* pCmdUI); + afx_msg void OnOptionsLanguageOther(); + afx_msg void OnUpdateOptionsLanguageOther(CCmdUI* pCmdUI); + afx_msg void OnOptionsJoypadConfigure1(); + afx_msg void OnUpdateOptionsJoypadConfigure1(CCmdUI* pCmdUI); + afx_msg void OnOptionsJoypadConfigure2(); + afx_msg void OnUpdateOptionsJoypadConfigure2(CCmdUI* pCmdUI); + afx_msg void OnOptionsJoypadConfigure3(); + afx_msg void OnUpdateOptionsJoypadConfigure3(CCmdUI* pCmdUI); + afx_msg void OnOptionsJoypadConfigure4(); + afx_msg void OnUpdateOptionsJoypadConfigure4(CCmdUI* pCmdUI); + afx_msg void OnOptionsJoypadMotionconfigure(); + afx_msg void OnUpdateOptionsJoypadMotionconfigure(CCmdUI* pCmdUI); + afx_msg void OnCheatsSearchforcheats(); + afx_msg void OnUpdateCheatsSearchforcheats(CCmdUI* pCmdUI); + afx_msg void OnCheatsCheatlist(); + afx_msg void OnUpdateCheatsCheatlist(CCmdUI* pCmdUI); + afx_msg void OnCheatsAutomaticsaveloadcheats(); + afx_msg void OnCheatsLoadcheatlist(); + afx_msg void OnUpdateCheatsLoadcheatlist(CCmdUI* pCmdUI); + afx_msg void OnCheatsSavecheatlist(); + afx_msg void OnUpdateCheatsSavecheatlist(CCmdUI* pCmdUI); + afx_msg void OnToolsDisassemble(); + afx_msg void OnUpdateToolsDisassemble(CCmdUI* pCmdUI); + afx_msg void OnToolsLogging(); + afx_msg void OnUpdateToolsLogging(CCmdUI* pCmdUI); + afx_msg void OnToolsIoviewer(); + afx_msg void OnUpdateToolsIoviewer(CCmdUI* pCmdUI); + afx_msg void OnToolsMapview(); + afx_msg void OnUpdateToolsMapview(CCmdUI* pCmdUI); + afx_msg void OnToolsMemoryviewer(); + afx_msg void OnUpdateToolsMemoryviewer(CCmdUI* pCmdUI); + afx_msg void OnToolsOamviewer(); + afx_msg void OnUpdateToolsOamviewer(CCmdUI* pCmdUI); + afx_msg void OnToolsPaletteview(); + afx_msg void OnUpdateToolsPaletteview(CCmdUI* pCmdUI); + afx_msg void OnToolsTileviewer(); + afx_msg void OnUpdateToolsTileviewer(CCmdUI* pCmdUI); + afx_msg void OnDebugNextframe(); + afx_msg void OnUpdateCheatsAutomaticsaveloadcheats(CCmdUI* pCmdUI); + afx_msg void OnToolsDebugGdb(); + afx_msg void OnUpdateToolsDebugGdb(CCmdUI* pCmdUI); + afx_msg void OnToolsDebugLoadandwait(); + afx_msg void OnUpdateToolsDebugLoadandwait(CCmdUI* pCmdUI); + afx_msg void OnToolsDebugBreak(); + afx_msg void OnUpdateToolsDebugBreak(CCmdUI* pCmdUI); + afx_msg void OnToolsDebugDisconnect(); + afx_msg void OnUpdateToolsDebugDisconnect(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundStartrecording(); + afx_msg void OnUpdateOptionsSoundStartrecording(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundStoprecording(); + afx_msg void OnUpdateOptionsSoundStoprecording(CCmdUI* pCmdUI); + afx_msg void OnToolsRecordStartavirecording(); + afx_msg void OnUpdateToolsRecordStartavirecording(CCmdUI* pCmdUI); + afx_msg void OnToolsRecordStopavirecording(); + afx_msg void OnUpdateToolsRecordStopavirecording(CCmdUI* pCmdUI); + afx_msg void OnPaint(); + afx_msg void OnToolsRecordStartmovierecording(); + afx_msg void OnUpdateToolsRecordStartmovierecording(CCmdUI* pCmdUI); + afx_msg void OnToolsRecordStopmovierecording(); + afx_msg void OnUpdateToolsRecordStopmovierecording(CCmdUI* pCmdUI); + afx_msg void OnToolsPlayStartmovieplaying(); + afx_msg void OnUpdateToolsPlayStartmovieplaying(CCmdUI* pCmdUI); + afx_msg void OnToolsPlayStopmovieplaying(); + afx_msg void OnUpdateToolsPlayStopmovieplaying(CCmdUI* pCmdUI); + afx_msg void OnToolsRewind(); + afx_msg void OnUpdateToolsRewind(CCmdUI* pCmdUI); + afx_msg void OnToolsCustomize(); + afx_msg void OnUpdateToolsCustomize(CCmdUI* pCmdUI); + afx_msg void OnHelpBugreport(); + afx_msg void OnMouseMove(UINT nFlags, CPoint point); + afx_msg void OnInitMenu(CMenu* pMenu); + afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized); +#if _MSC_VER <= 1200 + afx_msg void OnActivateApp(BOOL bActive, HTASK hTask); +#else + afx_msg void OnActivateApp(BOOL bActive, DWORD hTask); +#endif + afx_msg void OnDropFiles(HDROP hDropInfo); + afx_msg void OnFileSavegameOldestslot(); + afx_msg void OnUpdateFileSavegameOldestslot(CCmdUI* pCmdUI); + afx_msg void OnFileLoadgameMostrecent(); + afx_msg void OnUpdateFileLoadgameMostrecent(CCmdUI* pCmdUI); + afx_msg void OnFileLoadgameAutoloadmostrecent(); + afx_msg void OnUpdateFileLoadgameAutoloadmostrecent(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundVolume25x(); + afx_msg void OnUpdateOptionsSoundVolume25x(CCmdUI* pCmdUI); + afx_msg void OnOptionsSoundVolume5x(); + afx_msg void OnUpdateOptionsSoundVolume5x(CCmdUI* pCmdUI); + afx_msg void OnCheatsDisablecheats(); + afx_msg void OnUpdateCheatsDisablecheats(CCmdUI* pCmdUI); + afx_msg void OnOptionsVideoFullscreenmaxscale(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + + afx_msg BOOL OnFileRecentFile(UINT nID); + afx_msg BOOL OnFileLoadSlot(UINT nID); + afx_msg BOOL OnFileSaveSlot(UINT nID); + afx_msg void OnOptionsFilterLcdcolors(); + afx_msg void OnUpdateOptionsFilterLcdcolors(CCmdUI *pCmdUI); + + afx_msg BOOL OnOptionsSoundPcminterpolation(UINT nID); + afx_msg void OnUpdateOptionsSoundPcminterpolation(CCmdUI *pCmdUI); +public: + afx_msg void OnOptionsVideoFullscreen1280x1024(); + afx_msg void OnOptionsVideoFullscreen1024x768(); + afx_msg void OnUpdateOptionsVideoFullscreen1024x768(CCmdUI *pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen1280x1024(CCmdUI *pCmdUI); + void OnLinkOptions(); + void OnOptionsLinkLog() ; + void OnUpdateOptionsLinkLog(CCmdUI* pCmdUI) ; + void OnOptionsLinkRFU() ; + void OnUpdateOptionsLinkRFU(CCmdUI* pCmdUI) ; + + + +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_) diff --git a/src/win32/MainWndCheats.cpp b/src/win32/MainWndCheats.cpp new file mode 100644 index 00000000..90df14bf --- /dev/null +++ b/src/win32/MainWndCheats.cpp @@ -0,0 +1,165 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" + +#include "FileDlg.h" +#include "GBACheats.h" +#include "GBCheatsDlg.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../GBA.h" +#include "../Globals.h" +#include "../gb/gbCheats.h" + +extern int emulating; + +void MainWnd::OnCheatsSearchforcheats() +{ + theApp.winCheckFullscreen(); + if(theApp.cartridgeType == 0) { + GBACheatSearch dlg; + dlg.DoModal(); + } else { + GBCheatSearch dlg; + dlg.DoModal(); + } +} + +void MainWnd::OnUpdateCheatsSearchforcheats(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsCheatlist() +{ + theApp.winCheckFullscreen(); + if(theApp.cartridgeType == 0) { + GBACheatList dlg; + dlg.DoModal(); + } else { + GBCheatList dlg; + dlg.DoModal(); + } +} + +void MainWnd::OnUpdateCheatsCheatlist(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsAutomaticsaveloadcheats() +{ + theApp.autoSaveLoadCheatList = !theApp.autoSaveLoadCheatList; +} + +void MainWnd::OnUpdateCheatsAutomaticsaveloadcheats(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoSaveLoadCheatList); +} + +void MainWnd::OnCheatsLoadcheatlist() +{ + theApp.winCheckFullscreen(); + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.clt", saveDir, buffer); + else + filename.Format("%s\\%s.clt", saveDir, buffer); + + LPCTSTR exts[] = { ".clt" }; + CString filter = winLoadFilter(IDS_FILTER_CHEAT_LIST); + CString title = winResLoadString(IDS_SELECT_CHEAT_LIST_NAME); + + FileDlg dlg(this, filename, filter, 0, "CLT", exts, saveDir, title, false); + + if(dlg.DoModal() == IDOK) { + winLoadCheatList(dlg.GetPathName()); + } +} + +void MainWnd::OnUpdateCheatsLoadcheatlist(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsSavecheatlist() +{ + theApp.winCheckFullscreen(); + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.clt", saveDir, buffer); + else + filename.Format("%s\\%s.clt", saveDir, buffer); + + LPCTSTR exts[] = { ".clt" }; + CString filter = winLoadFilter(IDS_FILTER_CHEAT_LIST); + CString title = winResLoadString(IDS_SELECT_CHEAT_LIST_NAME); + + FileDlg dlg(this, filename, filter, 0, "CLT", exts, saveDir, title, true); + + if(dlg.DoModal() == IDOK) { + winSaveCheatList(dlg.GetPathName()); + } +} + +void MainWnd::OnUpdateCheatsSavecheatlist(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsDisablecheats() +{ + cheatsEnabled = !cheatsEnabled; +} + +void MainWnd::OnUpdateCheatsDisablecheats(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(!cheatsEnabled); +} + diff --git a/src/win32/MainWndFile.cpp b/src/win32/MainWndFile.cpp new file mode 100644 index 00000000..f6e55c4e --- /dev/null +++ b/src/win32/MainWndFile.cpp @@ -0,0 +1,897 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" + +#include "ExportGSASnapshot.h" +#include "FileDlg.h" +#include "GSACodeSelect.h" +#include "RomInfo.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../GBA.h" +#include "../Globals.h" +#include "../NLS.h" +#include "../Sound.h" +#include "../gb/GB.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" +#include "../Link.h" + +extern int emulating; + +extern void remoteCleanUp(); + +void MainWnd::OnFileOpen() +{ + theApp.winCheckFullscreen(); + if(fileOpenSelect()) { + FileRun(); + } +} + +void MainWnd::OnFilePause() +{ + theApp.paused = !theApp.paused; + if(emulating) { + if(theApp.paused) { + theApp.wasPaused = true; + soundPause(); + } else { + soundResume(); + } + } +} + +void MainWnd::OnUpdateFilePause(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.paused); +} + +void MainWnd::OnFileReset() +{ + if(emulating) { + theApp.emulator.emuReset(); + systemScreenMessage(winResLoadString(IDS_RESET)); + } +} + +void MainWnd::OnUpdateFileReset(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnUpdateFileRecentFreeze(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.recentFreeze); + + if(pCmdUI->m_pMenu == NULL) + return; + + CMenu *pMenu = pCmdUI->m_pMenu; + + int i; + for(i = 0; i < 10; i++) { + if(!pMenu->RemoveMenu(ID_FILE_MRU_FILE1+i, MF_BYCOMMAND)) + break; + } + + for(i = 0; i < 10; i++) { + CString p = theApp.recentFiles[i]; + if(p.GetLength() == 0) + break; + int index = p.ReverseFind('\\'); + + if(index != -1) + p = p.Right(p.GetLength()-index-1); + + pMenu->AppendMenu(MF_STRING, ID_FILE_MRU_FILE1+i, p); + } + theApp.winAccelMgr.UpdateMenu((HMENU)*pMenu); +} + +BOOL MainWnd::OnFileRecentFile(UINT nID) +{ + if(theApp.recentFiles[(nID&0xFFFF)-ID_FILE_MRU_FILE1].GetLength()) { + theApp.szFile = theApp.recentFiles[(nID&0xFFFF)-ID_FILE_MRU_FILE1]; + if(FileRun()) + emulating = true; + else { + emulating = false; + soundPause(); + } + } + return TRUE; +} + +void MainWnd::OnFileRecentReset() +{ + int i = 0; + for(i = 0; i < 10; i++) + theApp.recentFiles[i] = ""; +} + +void MainWnd::OnFileRecentFreeze() +{ + theApp.recentFreeze = !theApp.recentFreeze; +} + +void MainWnd::OnFileExit() +{ + SendMessage(WM_CLOSE); +} + +void MainWnd::OnFileClose() +{ + // save battery file before we change the filename... + if(rom != NULL || gbRom != NULL) { + if(theApp.autoSaveLoadCheatList) + winSaveCheatListDefault(); + writeBatteryFile(); + soundPause(); + theApp.emulator.emuCleanUp(); + remoteCleanUp(); + } + emulating = 0; + RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN); + systemSetTitle("VisualBoyAdvance"); +} + +void MainWnd::OnUpdateFileClose(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileOpengameboy() +{ + theApp.winCheckFullscreen(); + theApp.dir = ""; + CString initialDir = regQueryStringValue("gbromdir","."); + if(!initialDir.IsEmpty()) + theApp.dir = initialDir; + + theApp.szFile = ""; + + LPCTSTR exts[] = { "" }; + CString filter = winLoadFilter(IDS_FILTER_GBROM); + CString title = winResLoadString(IDS_SELECT_ROM); + + FileDlg dlg(this, "", filter, 0, "", exts, initialDir, title, false); + + if(dlg.DoModal() == IDOK) { + theApp.szFile = dlg.GetPathName(); + theApp.dir = theApp.szFile.Left(dlg.m_ofn.nFileOffset); + if(theApp.dir.GetLength() > 3 && theApp.dir[theApp.dir.GetLength()-1] == '\\') + theApp.dir = theApp.dir.Left(theApp.dir.GetLength()-1); + regSetStringValue("gbromdir", theApp.dir); + FileRun(); + } +} + +void MainWnd::OnFileLoad() +{ + theApp.winCheckFullscreen(); + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.sgm", saveDir, buffer); + else + filename.Format("%s\\%s.sgm", saveDir, buffer); + + LPCTSTR exts[] = { ".sgm" }; + CString filter = winLoadFilter(IDS_FILTER_SGM); + CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME); + + FileDlg dlg(this, filename, filter, 0, "", exts, saveDir, title, false); + + if(dlg.DoModal() == IDOK) { + bool res = loadSaveGame(dlg.GetPathName()); + + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + + if(res) + systemScreenMessage(winResLoadString(IDS_LOADED_STATE)); + } +} + +void MainWnd::OnUpdateFileLoad(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +BOOL MainWnd::OnFileLoadSlot(UINT nID) +{ + nID = nID + 1 - ID_FILE_LOADGAME_SLOT1; + + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s%d.sgm", saveDir, buffer, nID); + else + filename.Format("%s\\%s%d.sgm", saveDir, buffer, nID); + + bool res = loadSaveGame(filename); + + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + + CString format = winResLoadString(IDS_LOADED_STATE_N); + buffer.Format(format, nID); + + systemScreenMessage(buffer); + + return res; +} + +void MainWnd::OnFileSave() +{ + theApp.winCheckFullscreen(); + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s.sgm", saveDir, buffer); + else + filename.Format("%s\\%s.sgm", saveDir, buffer); + + LPCTSTR exts[] = { ".sgm" }; + CString filter = winLoadFilter(IDS_FILTER_SGM); + CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME); + + FileDlg dlg(this, filename, filter, 0, "", exts, saveDir, title, true); + + if(dlg.DoModal() == IDOK) { + bool res = writeSaveGame(dlg.GetPathName()); + if(res) + systemScreenMessage(winResLoadString(IDS_WROTE_STATE)); + } +} + +void MainWnd::OnUpdateFileSave(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +BOOL MainWnd::OnFileSaveSlot(UINT nID) +{ + nID = nID + 1 - ID_FILE_SAVEGAME_SLOT1; + + CString buffer; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + buffer = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(isDriveRoot(saveDir)) + filename.Format("%s%s%d.sgm", saveDir, buffer, nID); + else + filename.Format("%s\\%s%d.sgm", saveDir, buffer, nID); + + bool res = writeSaveGame(filename); + + CString format = winResLoadString(IDS_WROTE_STATE_N); + buffer.Format(format, nID); + + systemScreenMessage(buffer); + + return res; +} + +void MainWnd::OnFileImportBatteryfile() +{ + theApp.winCheckFullscreen(); + LPCTSTR exts[] = { ".sav" }; + CString filter = winLoadFilter(IDS_FILTER_SAV); + CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); + + CString saveDir = regQueryStringValue("batteryDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + FileDlg dlg(this, "", filter, 0, "", exts, saveDir, title, false); + + if(dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + if(MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + bool res = false; + + res = theApp.emulator.emuReadBattery(dlg.GetPathName()); + + if(!res) + systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", dlg.GetPathName()); + else { + theApp.emulator.emuReset(); + } +} + +void MainWnd::OnUpdateFileImportBatteryfile(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileImportGamesharkcodefile() +{ + theApp.winCheckFullscreen(); + LPCTSTR exts[] = { "" }; + CString filter = theApp.cartridgeType == 0 ? winLoadFilter(IDS_FILTER_SPC) : winLoadFilter(IDS_FILTER_GCF); + CString title = winResLoadString(IDS_SELECT_CODE_FILE); + + FileDlg dlg(this, "", filter, 0, "", exts, "", title, false); + + if(dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_CODES_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + if(MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + bool res = false; + CString file = dlg.GetPathName(); + if(theApp.cartridgeType == 1) + res = gbCheatReadGSCodeFile(file); + else { + res = fileImportGSACodeFile(file); + } +} + +void MainWnd::OnUpdateFileImportGamesharkcodefile(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileImportGamesharksnapshot() +{ + theApp.winCheckFullscreen(); + LPCTSTR exts[] = { "" }; + CString filter = theApp.cartridgeType == 1 ? winLoadFilter(IDS_FILTER_GBS) : winLoadFilter(IDS_FILTER_SPS); + CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE); + + FileDlg dlg(this, "", filter, 0, "", exts, "", title, false); + + if(dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + if(MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + if(theApp.cartridgeType == 1) + gbReadGSASnapshot(dlg.GetPathName()); + else + CPUReadGSASnapshot(dlg.GetPathName()); +} + +void MainWnd::OnUpdateFileImportGamesharksnapshot(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileExportBatteryfile() +{ + theApp.winCheckFullscreen(); + CString name; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + + LPCTSTR exts[] = {".sav", ".dat" }; + + CString filter = winLoadFilter(IDS_FILTER_SAV); + CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); + + CString saveDir = regQueryStringValue("batteryDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + FileDlg dlg(this, + name, + filter, + 1, + "SAV", + exts, + saveDir, + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + bool result = false; + + if(theApp.cartridgeType == 1) { + result = gbWriteBatteryFile(dlg.GetPathName(), false); + } else + result = theApp.emulator.emuWriteBattery(dlg.GetPathName()); + + if(!result) + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + dlg.GetPathName()); +} + +void MainWnd::OnUpdateFileExportBatteryfile(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileExportGamesharksnapshot() +{ + theApp.winCheckFullscreen(); + if(eepromInUse) { + systemMessage(IDS_EEPROM_NOT_SUPPORTED, "EEPROM saves cannot be exported"); + return; + } + + CString name; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + + LPCTSTR exts[] = {".sps" }; + + CString filter = winLoadFilter(IDS_FILTER_SPS); + CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE); + + FileDlg dlg(this, + name, + filter, + 1, + "SPS", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) + return; + + char buffer[16]; + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + + ExportGSASnapshot dlg2(dlg.GetPathName(), buffer, this); + dlg2.DoModal(); +} + +void MainWnd::OnUpdateFileExportGamesharksnapshot(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating && theApp.cartridgeType == 0); +} + +void MainWnd::OnFileScreencapture() +{ + theApp.winCheckFullscreen(); + CString name; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + name = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + name = theApp.filename; + + CString capdir = regQueryStringValue("captureDir", ""); + if(capdir.IsEmpty()) + capdir = getDirFromFile(name); + + CString ext = "png"; + + if(theApp.captureFormat != 0) + ext = "bmp"; + + if(isDriveRoot(capdir)) + filename.Format("%s%s.%s", capdir, name, ext); + else + filename.Format("%s\\%s.%s", capdir, name, ext); + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + filename, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + capdir, + title, + true); + + if(dlg.DoModal() == IDCANCEL) + return; + + if(dlg.getFilterIndex() == 2) + theApp.emulator.emuWriteBMP(dlg.GetPathName()); + else + theApp.emulator.emuWritePNG(dlg.GetPathName()); + + systemScreenMessage(winResLoadString(IDS_SCREEN_CAPTURE)); +} + +void MainWnd::OnUpdateFileScreencapture(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileRominformation() +{ + theApp.winCheckFullscreen(); + if(theApp.cartridgeType == 0) { + RomInfoGBA dlg(rom); + dlg.DoModal(); + } else { + RomInfoGB dlg(gbRom); + dlg.DoModal(); + } +} + +void MainWnd::OnUpdateFileRominformation(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileTogglemenu() +{ + if(theApp.videoOption <= VIDEO_4X) + return; + + theApp.menuToggle = !theApp.menuToggle; + + if(theApp.menuToggle) { + theApp.updateMenuBar(); + if(theApp.tripleBuffering) { + if(theApp.display) + theApp.display->checkFullScreen(); + DrawMenuBar(); + } + } else { + SetMenu(NULL); + DestroyMenu(theApp.menu); + } + + theApp.adjustDestRect(); +} + +void MainWnd::OnUpdateFileTogglemenu(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption > VIDEO_4X); +} + +bool MainWnd::fileImportGSACodeFile(CString& fileName) +{ + FILE *f = fopen(fileName, "rb"); + + if(f == NULL) { + systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", fileName); + return false; + } + + u32 len; + fread(&len, 1, 4, f); + if(len != 14) { + fclose(f); + systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s", + fileName); + return false; + } + char buffer[16]; + fread(buffer, 1, 14, f); + buffer[14] = 0; + if(memcmp(buffer, "SharkPortCODES", 14)) { + fclose(f); + systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s", + fileName); + return false; + } + fseek(f, 0x1e, SEEK_SET); + fread(&len, 1, 4, f); + int game = 0; + if(len > 1) { + GSACodeSelect dlg(f); + game = dlg.DoModal(); + } + fclose(f); + + bool v3 = false; + + int index = fileName.ReverseFind('.'); + + if(index != -1) { + if(fileName.Right(3).CompareNoCase("XPC") == 0) + v3 = true; + } + + if(game != -1) { + return cheatsImportGSACodeFile(fileName, game, v3); + } + + return true; +} + +void MainWnd::OnFileSavegameOldestslot() +{ + if(!emulating) + return; + + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + filename = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + filename = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(!isDriveRoot(saveDir)) + saveDir += "\\"; + + CString name; + CFileStatus status; + CString str; + unsigned long time = (unsigned long)-1; + int found = 0; + + for(int i = 0; i < 10; i++) { + name.Format("%s%s%d.sgm", saveDir, filename, i+1); + + if(emulating && CFile::GetStatus(name, status)) { + if((unsigned long)status.m_mtime.GetTime() < time) { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } else { + found = i; + break; + } + } + OnFileSaveSlot(ID_FILE_SAVEGAME_SLOT1+found); +} + +void MainWnd::OnUpdateFileSavegameOldestslot(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); + if(pCmdUI->m_pSubMenu != NULL) { + CMenu *pMenu = pCmdUI->m_pSubMenu; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + filename = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + filename = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(!isDriveRoot(saveDir)) + saveDir += "\\"; + + CString name; + CFileStatus status; + CString str; + + for(int i = 0; i < 10; i++) { + name.Format("%s%s%d.sgm", saveDir, filename, i+1); + + if(emulating && CFile::GetStatus(name, status)) { + CString timestamp = status.m_mtime.Format("%Y/%m/%d %H:%M:%S"); + str.Format("%d %s", i+1, timestamp); + } else { + str.Format("%d ----/--/-- --:--:--", i+1); + } + pMenu->ModifyMenu(ID_FILE_SAVEGAME_SLOT1+i, MF_STRING|MF_BYCOMMAND, ID_FILE_SAVEGAME_SLOT1+i, str); + } + + theApp.winAccelMgr.UpdateMenu(pMenu->GetSafeHmenu()); + } +} + +void MainWnd::OnFileLoadgameMostrecent() +{ + if(!emulating) + return; + + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + filename = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + filename = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(!isDriveRoot(saveDir)) + saveDir += "\\"; + + CString name; + CFileStatus status; + CString str; + unsigned long time = 0; + int found = -1; + + for(int i = 0; i < 10; i++) { + name.Format("%s%s%d.sgm", saveDir, filename, i+1); + + if(emulating && CFile::GetStatus(name, status)) { + if((unsigned long)status.m_mtime.GetTime() > time) { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } + } + if(found != -1) { + OnFileLoadSlot(ID_FILE_LOADGAME_SLOT1+found); + } +} + +void MainWnd::OnUpdateFileLoadgameMostrecent(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(emulating); + + if(pCmdUI->m_pSubMenu != NULL) { + CMenu *pMenu = pCmdUI->m_pSubMenu; + CString filename; + + int index = theApp.filename.ReverseFind('\\'); + + if(index != -1) + filename = theApp.filename.Right(theApp.filename.GetLength()-index-1); + else + filename = theApp.filename; + + CString saveDir = regQueryStringValue("saveDir", NULL); + + if(saveDir.IsEmpty()) + saveDir = getDirFromFile(theApp.filename); + + if(!isDriveRoot(saveDir)) + saveDir += "\\"; + + CString name; + CFileStatus status; + CString str; + + for(int i = 0; i < 10; i++) { + name.Format("%s%s%d.sgm", saveDir, filename, i+1); + + if(emulating && CFile::GetStatus(name, status)) { + CString timestamp = status.m_mtime.Format("%Y/%m/%d %H:%M:%S"); + str.Format("%d %s", i+1, timestamp); + } else { + str.Format("%d ----/--/-- --:--:--", i+1); + } + pMenu->ModifyMenu(ID_FILE_LOADGAME_SLOT1+i, MF_STRING|MF_BYCOMMAND, ID_FILE_LOADGAME_SLOT1+i, str); + } + + theApp.winAccelMgr.UpdateMenu(pMenu->GetSafeHmenu()); + } +} + +void MainWnd::OnUpdateFileLoadGameSlot(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnUpdateFileSaveGameSlot(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileLoadgameAutoloadmostrecent() +{ + theApp.autoLoadMostRecent = !theApp.autoLoadMostRecent; +} + +void MainWnd::OnUpdateFileLoadgameAutoloadmostrecent(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoLoadMostRecent); +} diff --git a/src/win32/MainWndHelp.cpp b/src/win32/MainWndHelp.cpp new file mode 100644 index 00000000..bbecc0ba --- /dev/null +++ b/src/win32/MainWndHelp.cpp @@ -0,0 +1,46 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" + +#include "AboutDialog.h" +#include "BugReport.h" + +extern int emulating; + +void MainWnd::OnHelpAbout() +{ + theApp.winCheckFullscreen(); + AboutDialog dlg; + + dlg.DoModal(); +} + +void MainWnd::OnHelpFaq() +{ + ::ShellExecute(0, _T("open"), "http://vba.ngemu.com/faq.shtml", + 0, 0, SW_SHOWNORMAL); +} + +void MainWnd::OnHelpBugreport() +{ + BugReport dlg(theApp.m_pMainWnd); + + dlg.DoModal(); +} diff --git a/src/win32/MainWndOptions.cpp b/src/win32/MainWndOptions.cpp new file mode 100644 index 00000000..693372a9 --- /dev/null +++ b/src/win32/MainWndOptions.cpp @@ -0,0 +1,1770 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" + +#include "Associate.h" +#include "Directories.h" +#include "FileDlg.h" +#include "GBColorDlg.h" +#include "Joypad.h" +#include "MaxScale.h" +#include "ModeConfirm.h" +#include "Reg.h" +#include "RewindInterval.h" +#include "Throttle.h" +#include "WinResUtil.h" +#include "LinkOptions.h" + +#include "../System.h" +#include "../agbprint.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Sound.h" +#include "../Util.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" +#include "../Link.h" + +extern int emulating; + +#define VBA_CONFIRM_MODE WM_APP + 100 + +extern void CPUUpdateRenderBuffers(bool force); + +void MainWnd::OnOptionsFrameskipThrottleNothrottle() +{ + theApp.throttle = 0; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleNothrottle(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 0); +} + +void MainWnd::OnOptionsFrameskipThrottle25() +{ + theApp.throttle = 25; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle25(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 25); +} + +void MainWnd::OnOptionsFrameskipThrottle50() +{ + theApp.throttle = 50; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle50(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 50); +} + +void MainWnd::OnOptionsFrameskipThrottle100() +{ + theApp.throttle = 100; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle100(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 100); +} + +void MainWnd::OnOptionsFrameskipThrottle150() +{ + theApp.throttle = 150; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle150(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 150); +} + +void MainWnd::OnOptionsFrameskipThrottle200() +{ + theApp.throttle = 200; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle200(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 200); +} + +void MainWnd::OnOptionsFrameskipThrottleOther() +{ + Throttle dlg; + int v = dlg.DoModal(); + if(v) + theApp.throttle = v; +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleOther(CCmdUI* pCmdUI) +{ + int throttle = theApp.throttle; + pCmdUI->SetCheck(throttle != 0 && throttle != 25 && + throttle != 50 && throttle != 100 && + throttle != 150 && throttle != 200); +} + +void MainWnd::OnOptionsFrameskipAutomatic() +{ + theApp.autoFrameSkip = !theApp.autoFrameSkip; + if(!theApp.autoFrameSkip && emulating) + theApp.updateFrameSkip(); +} + +void MainWnd::OnUpdateOptionsFrameskipAutomatic(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoFrameSkip); +} + +BOOL MainWnd::OnOptionsFrameskip(UINT nID) +{ + switch(nID) { + case ID_OPTIONS_VIDEO_FRAMESKIP_0: + case ID_OPTIONS_VIDEO_FRAMESKIP_1: + case ID_OPTIONS_VIDEO_FRAMESKIP_2: + case ID_OPTIONS_VIDEO_FRAMESKIP_3: + case ID_OPTIONS_VIDEO_FRAMESKIP_4: + case ID_OPTIONS_VIDEO_FRAMESKIP_5: + if(theApp.cartridgeType == 0) { + frameSkip = nID - ID_OPTIONS_VIDEO_FRAMESKIP_0; + } else { + gbFrameSkip = nID - ID_OPTIONS_VIDEO_FRAMESKIP_0; + } + if(emulating) + theApp.updateFrameSkip(); + return TRUE; + break; + case ID_OPTIONS_VIDEO_FRAMESKIP_6: + case ID_OPTIONS_VIDEO_FRAMESKIP_7: + case ID_OPTIONS_VIDEO_FRAMESKIP_8: + case ID_OPTIONS_VIDEO_FRAMESKIP_9: + if(theApp.cartridgeType == 0) { + frameSkip = 6 + nID - ID_OPTIONS_VIDEO_FRAMESKIP_6; + } else { + gbFrameSkip = 6 + nID - ID_OPTIONS_VIDEO_FRAMESKIP_6; + } + if(emulating) + theApp.updateFrameSkip(); + return TRUE; + break; + } + return FALSE; +} + +void MainWnd::OnUpdateOptionsVideoFrameskip0(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 0 : gbFrameSkip == 0); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip1(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 1 : gbFrameSkip == 1); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip2(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 2 : gbFrameSkip == 2); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip3(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 3 : gbFrameSkip == 3); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip4(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 4 : gbFrameSkip == 4); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip5(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 5 : gbFrameSkip == 5); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip6(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 6 : gbFrameSkip == 6); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip7(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 7 : gbFrameSkip == 7); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip8(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 8 : gbFrameSkip == 8); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip9(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.cartridgeType == 0 ? frameSkip == 9 : gbFrameSkip == 9); +} + +void MainWnd::OnOptionsVideoVsync() +{ + theApp.vsync = !theApp.vsync; +} + +void MainWnd::OnUpdateOptionsVideoVsync(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.vsync); +} + +void MainWnd::OnUpdateOptionsVideoX1(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_1X); +} + +void MainWnd::OnUpdateOptionsVideoX2(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_2X); +} + +void MainWnd::OnUpdateOptionsVideoX3(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_3X); +} + +void MainWnd::OnUpdateOptionsVideoX4(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_4X); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen320x240(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.mode320Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_320x240); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen640x480(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.mode640Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_640x480); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen800x600(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.mode800Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_800x600); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen1024x768(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.mode1024Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_1024x768); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen1280x1024(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.mode1280Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_1280x1024); +} + +BOOL MainWnd::OnOptionVideoSize(UINT nID) +{ + theApp.updateVideoSize(nID); + theApp.m_pMainWnd->PostMessage(VBA_CONFIRM_MODE); + return TRUE; +} + +void MainWnd::OnOptionsVideoFullscreen320x240() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN320X240); +} + +void MainWnd::OnOptionsVideoFullscreen640x480() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN640X480); +} + +void MainWnd::OnOptionsVideoFullscreen800x600() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN800X600); +} + +void MainWnd::OnOptionsVideoFullscreen1024x768() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN1024X768); +} + +void MainWnd::OnOptionsVideoFullscreen1280x1024() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN1280X1024); +} + +void MainWnd::OnOptionsVideoFullscreen() +{ + theApp.winCheckFullscreen(); + GUID *pGUID = NULL; + int size = theApp.display->selectFullScreenMode(&pGUID); + if(size != -1) { + int width = (size >> 12) & 4095; + int height = (size & 4095); + int colorDepth = (size >> 24); + if(width != theApp.fsWidth || + height != theApp.fsHeight || + colorDepth != theApp.fsColorDepth || + pGUID != theApp.pVideoDriverGUID || + theApp.videoOption != VIDEO_OTHER) { + theApp.fsForceChange = true; + theApp.fsWidth = width; + theApp.fsHeight = height; + theApp.fsFrequency = (theApp.display->selectFullScreenMode2() & 0xFFFF); + theApp.fsAdapter = theApp.display->selectFullScreenMode2(); + theApp.fsAdapter = (theApp.fsAdapter & 0xFFFF0000) >> 16; + theApp.fsColorDepth = colorDepth; + theApp.pVideoDriverGUID = pGUID; + if(pGUID) { + theApp.videoDriverGUID = *pGUID; + regSetDwordValue("defaultVideoDriver", FALSE); + regSetBinaryValue("videoDriverGUID", + (char *)pGUID, sizeof(GUID)); + } else { + regSetDwordValue("defaultVideoDriver", TRUE); + } + theApp.updateVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN); + theApp.m_pMainWnd->PostMessage(VBA_CONFIRM_MODE); + } + } + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_OTHER); +} + +void MainWnd::OnOptionsVideoDisablesfx() +{ + cpuDisableSfx = !cpuDisableSfx; + if(emulating && theApp.cartridgeType == 0) + CPUUpdateRender(); +} + +void MainWnd::OnUpdateOptionsVideoDisablesfx(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(!cpuDisableSfx); +} + +void MainWnd::OnOptionsVideoFullscreenstretchtofit() +{ + theApp.fullScreenStretch = !theApp.fullScreenStretch; + theApp.updateWindowSize(theApp.videoOption); + if(theApp.display) + theApp.display->clear(); +} + +void MainWnd::OnUpdateOptionsVideoFullscreenstretchtofit(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.fullScreenStretch); +} + +BOOL MainWnd::OnVideoLayer(UINT nID) +{ + layerSettings ^= 0x0100 << + ((nID & 0xFFFF) - ID_OPTIONS_VIDEO_LAYERS_BG0); + layerEnable = DISPCNT & layerSettings; + CPUUpdateRenderBuffers(false); + return TRUE; +} + +void MainWnd::OnUpdateVideoLayer(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck((layerSettings >> (8 + pCmdUI->m_nID - ID_OPTIONS_VIDEO_LAYERS_BG0)) & 1); + switch(pCmdUI->m_nID) { + case ID_OPTIONS_VIDEO_LAYERS_BG1: + case ID_OPTIONS_VIDEO_LAYERS_BG2: + case ID_OPTIONS_VIDEO_LAYERS_BG3: + case ID_OPTIONS_VIDEO_LAYERS_WIN1: + case ID_OPTIONS_VIDEO_LAYERS_OBJWIN: + pCmdUI->Enable(theApp.cartridgeType == 0); + break; + } +} + +void MainWnd::OnOptionsVideoRendermethodGdi() +{ + theApp.renderMethod = GDI; + theApp.updateRenderMethod(false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodGdi(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == GDI); +} + +void MainWnd::OnOptionsVideoRendermethodDirectdraw() +{ + theApp.renderMethod = DIRECT_DRAW; + theApp.updateRenderMethod(false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodDirectdraw(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == DIRECT_DRAW); +} + +void MainWnd::OnOptionsVideoRendermethodDirect3d() +{ + theApp.renderMethod = DIRECT_3D; + theApp.updateRenderMethod(false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodDirect3d(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == DIRECT_3D); +} + +void MainWnd::OnOptionsVideoRendermethodOpengl() +{ + theApp.renderMethod = OPENGL; + theApp.updateRenderMethod(false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodOpengl(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == OPENGL); +} + +void MainWnd::OnOptionsVideoTriplebuffering() +{ + theApp.tripleBuffering = !theApp.tripleBuffering; +} + +void MainWnd::OnUpdateOptionsVideoTriplebuffering(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.tripleBuffering); +} + +void MainWnd::OnOptionsVideoDdrawemulationonly() +{ + theApp.ddrawEmulationOnly = !theApp.ddrawEmulationOnly; +} + +void MainWnd::OnUpdateOptionsVideoDdrawemulationonly(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.ddrawEmulationOnly); +} + +void MainWnd::OnOptionsVideoDdrawusevideomemory() +{ + theApp.ddrawUseVideoMemory = !theApp.ddrawUseVideoMemory; +} + +void MainWnd::OnUpdateOptionsVideoDdrawusevideomemory(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.ddrawUseVideoMemory); +} + +void MainWnd::OnOptionsVideoRenderoptionsD3dnofilter() +{ + theApp.d3dFilter = 0; + if(theApp.display) + theApp.display->setOption("d3dFilter", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsD3dnofilter(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.d3dFilter == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsD3dbilinear() +{ + theApp.d3dFilter = 1; + if(theApp.display) + theApp.display->setOption("d3dFilter", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsD3dbilinear(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.d3dFilter == 1); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlnearest() +{ + theApp.glFilter = 0; + if(theApp.display) + theApp.display->setOption("glFilter", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlnearest(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.glFilter == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlbilinear() +{ + theApp.glFilter = 1; + if(theApp.display) + theApp.display->setOption("glFilter", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlbilinear(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.glFilter == 1); +} + +void MainWnd::OnOptionsVideoRenderoptionsGltriangle() +{ + theApp.glType = 0; + if(theApp.display) + theApp.display->setOption("glType", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGltriangle(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.glType == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlquads() +{ + theApp.glType = 1; + if(theApp.display) + theApp.display->setOption("glType", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlquads(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.glType == 1); +} + +void MainWnd::OnOptionsEmulatorAssociate() +{ + theApp.winCheckFullscreen(); + Associate dlg; + dlg.DoModal(); +} + +void MainWnd::OnOptionsEmulatorDirectories() +{ + theApp.winCheckFullscreen(); + Directories dlg; + dlg.DoModal(); +} + +void MainWnd::OnOptionsEmulatorDisablestatusmessages() +{ + theApp.disableStatusMessage = !theApp.disableStatusMessage; +} + +void MainWnd::OnUpdateOptionsEmulatorDisablestatusmessages(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.disableStatusMessage); +} + +void MainWnd::OnOptionsEmulatorSynchronize() +{ + synchronize = !synchronize; +} + +void MainWnd::OnUpdateOptionsEmulatorSynchronize(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(synchronize); +} + +void MainWnd::OnOptionsEmulatorPausewheninactive() +{ + theApp.pauseWhenInactive = !theApp.pauseWhenInactive; +} + +void MainWnd::OnUpdateOptionsEmulatorPausewheninactive(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.pauseWhenInactive); +} + +void MainWnd::OnOptionsEmulatorSpeeduptoggle() +{ + theApp.speedupToggle = !theApp.speedupToggle; +} + +void MainWnd::OnUpdateOptionsEmulatorSpeeduptoggle(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.speedupToggle); +} + +void MainWnd::OnOptionsEmulatorRemoveintrosgba() +{ + // theApp.removeIntros = !theApp.removeIntros; +} + +void MainWnd::OnUpdateOptionsEmulatorRemoveintrosgba(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(false); + // pCmdUI->SetCheck(theApp.removeIntros); +} + +void MainWnd::OnOptionsEmulatorAutomaticallyipspatch() +{ + theApp.autoIPS = !theApp.autoIPS; +} + +void MainWnd::OnUpdateOptionsEmulatorAutomaticallyipspatch(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoIPS); +} + +void MainWnd::OnOptionsEmulatorAgbprint() +{ + agbPrintEnable(!agbPrintIsEnabled()); +} + +void MainWnd::OnUpdateOptionsEmulatorAgbprint(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(agbPrintIsEnabled()); +} + +void MainWnd::OnOptionsEmulatorRealtimeclock() +{ + theApp.winRtcEnable = !theApp.winRtcEnable; +} + +void MainWnd::OnUpdateOptionsEmulatorRealtimeclock(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winRtcEnable); +} + +void MainWnd::OnOptionsEmulatorAutohidemenu() +{ + theApp.autoHideMenu = !theApp.autoHideMenu; +} + +void MainWnd::OnUpdateOptionsEmulatorAutohidemenu(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoHideMenu); +} + +void MainWnd::OnOptionsEmulatorRewindinterval() +{ + RewindInterval dlg(theApp.rewindTimer/6); + int v = dlg.DoModal(); + + if(v >= 0) { + theApp.rewindTimer = v*6; // convert to a multiple of 10 frames + regSetDwordValue("rewindTimer", v); + if(v == 0) { + if(theApp.rewindMemory) + free(theApp.rewindMemory); + theApp.rewindMemory = NULL; + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + } else { + if(theApp.rewindMemory == NULL) + theApp.rewindMemory = (char *)malloc(8*REWIND_SIZE); + } + } +} + +BOOL MainWnd::OnOptionsEmulatorShowSpeed(UINT nID) +{ + switch(nID) { + case ID_OPTIONS_EMULATOR_SHOWSPEED_NONE: + theApp.showSpeed = 0; + systemSetTitle("VisualBoyAdvance"); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE: + theApp.showSpeed = 1; + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED: + theApp.showSpeed = 2; + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT: + theApp.showSpeedTransparent = !theApp.showSpeedTransparent; + break; + default: + return FALSE; + } + return TRUE; +} + +void MainWnd::OnUpdateOptionsEmulatorShowSpeed(CCmdUI *pCmdUI) +{ + switch(pCmdUI->m_nID) { + case ID_OPTIONS_EMULATOR_SHOWSPEED_NONE: + pCmdUI->SetCheck(theApp.showSpeed == 0); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE: + pCmdUI->SetCheck(theApp.showSpeed == 1); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED: + pCmdUI->SetCheck(theApp.showSpeed == 2); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT: + pCmdUI->SetCheck(theApp.showSpeedTransparent); + break; + } +} + +void MainWnd::OnOptionsEmulatorSavetypeAutomatic() +{ + theApp.winSaveType = 0; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeAutomatic(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 0); +} + +void MainWnd::OnOptionsEmulatorSavetypeEeprom() +{ + theApp.winSaveType = 1; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeEeprom(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 1); +} + +void MainWnd::OnOptionsEmulatorSavetypeSram() +{ + theApp.winSaveType = 2; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeSram(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 2); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash() +{ + theApp.winSaveType = 3; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 3); +} + +void MainWnd::OnOptionsEmulatorSavetypeEepromsensor() +{ + theApp.winSaveType = 4; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeEepromsensor(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 4); +} + +void MainWnd::OnOptionsEmulatorSavetypeNone() +{ + theApp.winSaveType = 5; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeNone(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 5); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash512k() +{ + flashSetSize(0x10000); + theApp.winFlashSize = 0x10000; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash512k(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winFlashSize == 0x10000); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash1m() +{ + flashSetSize(0x20000); + theApp.winFlashSize = 0x20000; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash1m(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winFlashSize == 0x20000); +} + +void MainWnd::OnOptionsEmulatorUsebiosfile() +{ + if(!theApp.biosFileName.IsEmpty()) + theApp.useBiosFile = !theApp.useBiosFile; +} + +void MainWnd::OnUpdateOptionsEmulatorUsebiosfile(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.useBiosFile); + pCmdUI->Enable(!theApp.biosFileName.IsEmpty()); +} + +void MainWnd::OnOptionsEmulatorSkipbios() +{ + theApp.skipBiosFile = !theApp.skipBiosFile; +} + +void MainWnd::OnUpdateOptionsEmulatorSkipbios(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.skipBiosFile); +} + +void MainWnd::OnOptionsEmulatorSelectbiosfile() +{ + theApp.winCheckFullscreen(); + LPCTSTR exts[] = { "" }; + CString filter = winLoadFilter(IDS_FILTER_BIOS); + CString title = winResLoadString(IDS_SELECT_BIOS_FILE); + + FileDlg dlg(this, + theApp.biosFileName, + filter, + 0, + "BIOS", + exts, + "", + title, + false); + + if(dlg.DoModal() == IDOK) { + theApp.biosFileName = dlg.GetPathName(); + } +} + +void MainWnd::OnOptionsEmulatorPngformat() +{ + theApp.captureFormat = 0; +} + +void MainWnd::OnUpdateOptionsEmulatorPngformat(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.captureFormat == 0); +} + +void MainWnd::OnOptionsEmulatorBmpformat() +{ + theApp.captureFormat = 1; +} + +void MainWnd::OnUpdateOptionsEmulatorBmpformat(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.captureFormat == 1); +} + +void MainWnd::OnOptionsSoundOff() +{ + soundOffFlag = true; + soundShutdown(); +} + +void MainWnd::OnUpdateOptionsSoundOff(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundOffFlag); +} + +void MainWnd::OnOptionsSoundMute() +{ + soundDisable(0x30f); +} + +void MainWnd::OnUpdateOptionsSoundMute(CCmdUI* pCmdUI) +{ + int active = soundGetEnable() & 0x30f; + pCmdUI->SetCheck(active == 0); +} + +void MainWnd::OnOptionsSoundOn() +{ + //if(soundOffFlag) { + soundOffFlag = false; + //soundInit(!theApp.cartridgeType); + //} + soundEnable(0x30f); +} + +void MainWnd::OnUpdateOptionsSoundOn(CCmdUI* pCmdUI) +{ + //int active = soundGetEnable() & 0x30f; + pCmdUI->SetCheck(/*active != 0 &&*/ !soundOffFlag); +} + +void MainWnd::OnOptionsSoundUseoldsynchronization() +{ + theApp.useOldSync = !theApp.useOldSync; + systemMessage(IDS_SETTING_WILL_BE_EFFECTIVE, + "Setting will be effective the next time you start the emulator"); +} + +void MainWnd::OnUpdateOptionsSoundUseoldsynchronization(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.useOldSync); +} + +void MainWnd::OnOptionsSoundEcho() +{ + soundEcho = !soundEcho; +} + +void MainWnd::OnUpdateOptionsSoundEcho(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundEcho); +} + +void MainWnd::OnOptionsSoundLowpassfilter() +{ + soundLowPass = !soundLowPass; +} + +void MainWnd::OnUpdateOptionsSoundLowpassfilter(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundLowPass); +} + +void MainWnd::OnOptionsSoundReversestereo() +{ + soundReverse = !soundReverse; +} + +void MainWnd::OnUpdateOptionsSoundReversestereo(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundReverse); +} + +void MainWnd::OnOptionsSound11khz() +{ + if(theApp.cartridgeType == 0) + soundSetQuality(4); + else + gbSoundSetQuality(4); +} + +void MainWnd::OnUpdateOptionsSound11khz(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 4); +} + +void MainWnd::OnOptionsSound22khz() +{ + if(theApp.cartridgeType == 0) + soundSetQuality(2); + else + gbSoundSetQuality(2); +} + +void MainWnd::OnUpdateOptionsSound22khz(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 2); +} + +void MainWnd::OnOptionsSound44khz() +{ + if(theApp.cartridgeType == 0) + soundSetQuality(1); + else + gbSoundSetQuality(1); +} + +void MainWnd::OnUpdateOptionsSound44khz(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 1); +} + +BOOL MainWnd::OnOptionsSoundVolume(UINT nID) +{ + soundVolume = nID - ID_OPTIONS_SOUND_VOLUME_1X; + return TRUE; +} + +void MainWnd::OnUpdateOptionsSoundVolume(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == (int)(pCmdUI->m_nID - ID_OPTIONS_SOUND_VOLUME_1X)); +} + + +void MainWnd::OnOptionsSoundVolume25x() +{ + soundVolume = 4; +} + +void MainWnd::OnUpdateOptionsSoundVolume25x(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == 4); +} + +void MainWnd::OnOptionsSoundVolume5x() +{ + soundVolume = 5; +} + +void MainWnd::OnUpdateOptionsSoundVolume5x(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == 5); +} + +void MainWnd::updateSoundChannels(UINT id) +{ + int flag = 0; + + if ( soundOffFlag ) return; // mute hax + + if(id == ID_OPTIONS_SOUND_CHANNEL1) + flag = 1; + + if(id == ID_OPTIONS_SOUND_CHANNEL2) + flag = 2; + + if(id == ID_OPTIONS_SOUND_CHANNEL3) + flag = 4; + + if(id == ID_OPTIONS_SOUND_CHANNEL4) + flag = 8; + + if(id == ID_OPTIONS_SOUND_DIRECTSOUNDA) + flag = 256; + + if(id == ID_OPTIONS_SOUND_DIRECTSOUNDB) + flag = 512; + + int active = soundGetEnable() & 0x30f; + + if(active & flag) + active &= (~flag); + else + active |= flag; + + soundEnable(active); + soundDisable((~active)&0x30f); +} + +void MainWnd::OnOptionsSoundChannel1() +{ + updateSoundChannels(ID_OPTIONS_SOUND_CHANNEL1); +} + +void MainWnd::OnUpdateOptionsSoundChannel1(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 1); +} + +void MainWnd::OnOptionsSoundChannel2() +{ + updateSoundChannels(ID_OPTIONS_SOUND_CHANNEL2); +} + +void MainWnd::OnUpdateOptionsSoundChannel2(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 2); +} + +void MainWnd::OnOptionsSoundChannel3() +{ + updateSoundChannels(ID_OPTIONS_SOUND_CHANNEL3); +} + +void MainWnd::OnUpdateOptionsSoundChannel3(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 4); +} + +void MainWnd::OnOptionsSoundChannel4() +{ + updateSoundChannels(ID_OPTIONS_SOUND_CHANNEL4); +} + +void MainWnd::OnUpdateOptionsSoundChannel4(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 8); +} + +void MainWnd::OnOptionsSoundDirectsounda() +{ + updateSoundChannels(ID_OPTIONS_SOUND_DIRECTSOUNDA); +} + +void MainWnd::OnUpdateOptionsSoundDirectsounda(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 256); + pCmdUI->Enable(theApp.cartridgeType == 0); +} + +void MainWnd::OnOptionsSoundDirectsoundb() +{ + updateSoundChannels(ID_OPTIONS_SOUND_DIRECTSOUNDB); +} + +void MainWnd::OnUpdateOptionsSoundDirectsoundb(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnable() & 512); + pCmdUI->Enable(theApp.cartridgeType == 0); +} + +BOOL MainWnd::OnOptionsSoundPcminterpolation(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE: + soundInterpolation = 0; + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_LINEAR: + soundInterpolation = 1; + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_CUBIC: + soundInterpolation = 2; + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_FIR: + soundInterpolation = 3; + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE: + soundInterpolation = 4; + break; + + default: + return FALSE; + } + + return TRUE; +} + +void MainWnd::OnUpdateOptionsSoundPcminterpolation(CCmdUI *pCmdUI) +{ + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_SOUND_PCMINTERPOLATION_NONE: + pCmdUI->SetCheck(soundInterpolation == 0); + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_LINEAR: + pCmdUI->SetCheck(soundInterpolation == 1); + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_CUBIC: + pCmdUI->SetCheck(soundInterpolation == 2); + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_FIR: + pCmdUI->SetCheck(soundInterpolation == 3); + break; + case ID_OPTIONS_SOUND_PCMINTERPOLATION_LIBRESAMPLE: + pCmdUI->SetCheck(soundInterpolation == 4); + break; + + default: + return; + } + pCmdUI->Enable(theApp.cartridgeType == 0); +} + +void MainWnd::OnOptionsGameboyBorder() +{ + theApp.winGbBorderOn = !theApp.winGbBorderOn; + gbBorderOn = theApp.winGbBorderOn; + if(emulating && theApp.cartridgeType == 1 && gbBorderOn) { + gbSgbRenderBorder(); + } + theApp.updateWindowSize(theApp.videoOption); +} + +void MainWnd::OnUpdateOptionsGameboyBorder(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.winGbBorderOn); +} + +void MainWnd::OnOptionsGameboyPrinter() +{ + theApp.winGbPrinterEnabled = !theApp.winGbPrinterEnabled; + if(theApp.winGbPrinterEnabled) + gbSerialFunction = gbPrinterSend; + else + gbSerialFunction = NULL; +} + +void MainWnd::OnUpdateOptionsGameboyPrinter(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbSerialFunction == gbPrinterSend); +} + +void MainWnd::OnOptionsGameboyBorderAutomatic() +{ + gbBorderAutomatic = !gbBorderAutomatic; + if(emulating && theApp.cartridgeType == 1 && gbBorderOn) { + gbSgbRenderBorder(); + theApp.updateWindowSize(theApp.videoOption); + } +} + +void MainWnd::OnUpdateOptionsGameboyBorderAutomatic(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbBorderAutomatic); +} + +void MainWnd::OnOptionsGameboyAutomatic() +{ + gbEmulatorType = 0; +} + +void MainWnd::OnUpdateOptionsGameboyAutomatic(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 0); +} + +void MainWnd::OnOptionsGameboyGba() +{ + gbEmulatorType = 4; +} + +void MainWnd::OnUpdateOptionsGameboyGba(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 4); +} + +void MainWnd::OnOptionsGameboyCgb() +{ + gbEmulatorType = 1; +} + +void MainWnd::OnUpdateOptionsGameboyCgb(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 1); +} + +void MainWnd::OnOptionsGameboySgb() +{ + gbEmulatorType = 2; +} + +void MainWnd::OnUpdateOptionsGameboySgb(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 2); +} + +void MainWnd::OnOptionsGameboySgb2() +{ + gbEmulatorType = 5; +} + +void MainWnd::OnUpdateOptionsGameboySgb2(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 5); +} + +void MainWnd::OnOptionsGameboyGb() +{ + gbEmulatorType = 3; +} + +void MainWnd::OnUpdateOptionsGameboyGb(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 3); +} + +void MainWnd::OnOptionsGameboyRealcolors() +{ + gbColorOption = 0; +} + +void MainWnd::OnUpdateOptionsGameboyRealcolors(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbColorOption == 0); +} + +void MainWnd::OnOptionsGameboyGameboycolors() +{ + gbColorOption = 1; +} + +void MainWnd::OnUpdateOptionsGameboyGameboycolors(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(gbColorOption == 1); +} + + +void MainWnd::OnOptionsGameboyColors() +{ + theApp.winCheckFullscreen(); + GBColorDlg dlg; + if(dlg.DoModal()) { + gbPaletteOption = dlg.getWhich(); + memcpy(systemGbPalette, dlg.getColors(), 24*sizeof(u16)); + if(emulating && theApp.cartridgeType == 1) { + memcpy(gbPalette, &systemGbPalette[dlg.getWhich()*8], 8*sizeof(u16)); + } + } +} + +BOOL MainWnd::OnOptionsPriority(UINT nID) +{ + switch(nID) { + case ID_OPTIONS_PRIORITY_HIGHEST: + theApp.threadPriority = 0; + break; + case ID_OPTIONS_PRIORITY_ABOVENORMAL: + theApp.threadPriority = 1; + break; + case ID_OPTIONS_PRIORITY_NORMAL: + theApp.threadPriority = 2; + break; + case ID_OPTIONS_PRIORITY_BELOWNORMAL: + theApp.threadPriority = 3; + break; + default: + return FALSE; + } + theApp.updatePriority(); + + return TRUE; +} + +void MainWnd::OnUpdateOptionsPriority(CCmdUI *pCmdUI) +{ + switch(pCmdUI->m_nID) { + case ID_OPTIONS_PRIORITY_HIGHEST: + pCmdUI->SetCheck(theApp.threadPriority == 0); + break; + case ID_OPTIONS_PRIORITY_ABOVENORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 1); + break; + case ID_OPTIONS_PRIORITY_NORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 2); + break; + case ID_OPTIONS_PRIORITY_BELOWNORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 3); + break; + } +} + +BOOL MainWnd::OnOptionsFilter(UINT nID) +{ + switch(nID) + { + case ID_OPTIONS_FILTER_NORMAL: + theApp.filterType = FILTER_NONE; + break; + case ID_OPTIONS_FILTER_TVMODE: + theApp.filterType = FILTER_TVMODE; + break; + case ID_OPTIONS_FILTER_2XSAI: + theApp.filterType = FILTER_2XSAI; + break; + case ID_OPTIONS_FILTER_SUPER2XSAI: + theApp.filterType = FILTER_SUPER2XSAI; + break; + case ID_OPTIONS_FILTER_SUPEREAGLE: + theApp.filterType = FILTER_SUPEREAGLE; + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL: + theApp.filterType = FILTER_PIXELATE; + break; + case ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X: + theApp.filterType = FILTER_MAMESCALE2X; + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE2X: + theApp.filterType = FILTER_SIMPLE2X; + break; + case ID_OPTIONS_FILTER_BILINEAR: + theApp.filterType = FILTER_BILINEAR; + break; + case ID_OPTIONS_FILTER_BILINEARPLUS: + theApp.filterType = FILTER_BILINEARPLUS; + break; + case ID_OPTIONS_FILTER_SCANLINES: + theApp.filterType = FILTER_SCANLINES; + break; + case ID_OPTIONS_FILTER_HQ2X: + theApp.filterType = FILTER_HQ2X; + break; + case ID_OPTIONS_FILTER_LQ2X: + theApp.filterType = FILTER_LQ2X; + break; + case ID_OPTIONS_FILTER_SIMPLE3X: + theApp.filterType = FILTER_SIMPLE3X; + break; + case ID_OPTIONS_FILTER_SIMPLE4X: + theApp.filterType = FILTER_SIMPLE4X; + break; + case ID_OPTIONS_FILTER_HQ3X: + theApp.filterType = FILTER_HQ3X; + break; + case ID_OPTIONS_FILTER_HQ4X: + theApp.filterType = FILTER_HQ4X; + break; + default: + return FALSE; + } + theApp.updateFilter(); + return TRUE; +} + +void MainWnd::OnUpdateOptionsFilter(CCmdUI *pCmdUI) +{ + pCmdUI->Enable( systemColorDepth == 16 || systemColorDepth == 32 ); + + switch(pCmdUI->m_nID) { + case ID_OPTIONS_FILTER_NORMAL: + pCmdUI->SetCheck(theApp.filterType == FILTER_NONE); + break; + case ID_OPTIONS_FILTER_TVMODE: + pCmdUI->SetCheck(theApp.filterType == FILTER_TVMODE); + break; + case ID_OPTIONS_FILTER_2XSAI: + pCmdUI->SetCheck(theApp.filterType == FILTER_2XSAI); + break; + case ID_OPTIONS_FILTER_SUPER2XSAI: + pCmdUI->SetCheck(theApp.filterType == FILTER_SUPER2XSAI); + break; + case ID_OPTIONS_FILTER_SUPEREAGLE: + pCmdUI->SetCheck(theApp.filterType == FILTER_SUPEREAGLE); + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL: + pCmdUI->SetCheck(theApp.filterType == FILTER_PIXELATE); + break; + case ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X: + pCmdUI->SetCheck(theApp.filterType == FILTER_MAMESCALE2X); + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE2X: + pCmdUI->SetCheck(theApp.filterType == FILTER_SIMPLE2X); + break; + case ID_OPTIONS_FILTER_BILINEAR: + pCmdUI->SetCheck(theApp.filterType == FILTER_BILINEAR); + break; + case ID_OPTIONS_FILTER_BILINEARPLUS: + pCmdUI->SetCheck(theApp.filterType == FILTER_BILINEARPLUS); + break; + case ID_OPTIONS_FILTER_SCANLINES: + pCmdUI->SetCheck(theApp.filterType == FILTER_SCANLINES); + break; + case ID_OPTIONS_FILTER_HQ2X: + pCmdUI->SetCheck(theApp.filterType == FILTER_HQ2X); + break; + case ID_OPTIONS_FILTER_LQ2X: + pCmdUI->SetCheck(theApp.filterType == FILTER_LQ2X); + break; + case ID_OPTIONS_FILTER_SIMPLE3X: + pCmdUI->SetCheck(theApp.filterType == FILTER_SIMPLE3X); + break; + case ID_OPTIONS_FILTER_SIMPLE4X: + pCmdUI->SetCheck(theApp.filterType == FILTER_SIMPLE4X); + break; + case ID_OPTIONS_FILTER_HQ3X: + pCmdUI->SetCheck(theApp.filterType == FILTER_HQ3X); + break; + case ID_OPTIONS_FILTER_HQ4X: + pCmdUI->SetCheck(theApp.filterType == FILTER_HQ4X); + break; + } +} + +BOOL MainWnd::OnOptionsFilterIFB(UINT nID) +{ + switch(nID) { + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE: + theApp.ifbType = 0; + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR: + theApp.ifbType = 1; + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART: + theApp.ifbType = 2; + break; + default: + return FALSE; + } + theApp.updateIFB(); + return TRUE; +} + +void MainWnd::OnUpdateOptionsFilterIFB(CCmdUI *pCmdUI) +{ + switch(pCmdUI->m_nID) { + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE: + pCmdUI->SetCheck(theApp.ifbType == 0); + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR: + pCmdUI->SetCheck(theApp.ifbType == 1); + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART: + pCmdUI->SetCheck(theApp.ifbType == 2); + break; + } +} + +void MainWnd::OnOptionsFilterDisablemmx() +{ +#ifdef MMX + theApp.disableMMX = !theApp.disableMMX; + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif +} + +void MainWnd::OnUpdateOptionsFilterDisablemmx(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.disableMMX); +} + +void MainWnd::OnOptionsFilterLcdcolors() +{ + theApp.filterLCD = !theApp.filterLCD; + utilUpdateSystemColorMaps(theApp.filterLCD); +} + +void MainWnd::OnUpdateOptionsFilterLcdcolors(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.filterLCD); +} + +void MainWnd::OnOptionsLanguageSystem() +{ + theApp.winSetLanguageOption(0, false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsLanguageSystem(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 0); +} + +void MainWnd::OnOptionsLanguageEnglish() +{ + theApp.winSetLanguageOption(1, false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsLanguageEnglish(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 1); +} + +void MainWnd::OnOptionsLanguageOther() +{ + theApp.winCheckFullscreen(); + theApp.winSetLanguageOption(2, false); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnUpdateOptionsLanguageOther(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 2); +} + + +void MainWnd::OnOptionsJoypadConfigure1() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(0); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure1(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure2() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(1); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure2(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure3() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(2); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure3(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure4() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(3); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure4(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +BOOL MainWnd::OnOptionsJoypadDefault(UINT nID) +{ + theApp.joypadDefault = nID - ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1; + return TRUE; +} + +void MainWnd::OnUpdateOptionsJoypadDefault(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.joypadDefault == (int)(pCmdUI->m_nID - ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1)); +} + +void MainWnd::OnOptionsJoypadMotionconfigure() +{ + theApp.winCheckFullscreen(); + MotionConfig dlg; + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadMotionconfigure(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +BOOL MainWnd::OnOptionsJoypadAutofire(UINT nID) +{ + switch(nID) { + case ID_OPTIONS_JOYPAD_AUTOFIRE_A: + if(theApp.autoFire & 1) { + theApp.autoFire &= ~1; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_A_DISABLED)); + } else { + theApp.autoFire |= 1; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_A)); + } + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_B: + if(theApp.autoFire & 2) { + theApp.autoFire &= ~2; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_B_DISABLED)); + } else { + theApp.autoFire |= 2; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_B)); + } + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_L: + if(theApp.autoFire & 512) { + theApp.autoFire &= ~512; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_L_DISABLED)); + } else { + theApp.autoFire |= 512; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_L)); + } + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_R: + if(theApp.autoFire & 256) { + theApp.autoFire &= ~256; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_R_DISABLED)); + } else { + theApp.autoFire |= 256; + systemScreenMessage(winResLoadString(IDS_AUTOFIRE_R)); + } + break; + default: + return FALSE; + } + return TRUE; +} + +void MainWnd::OnUpdateOptionsJoypadAutofire(CCmdUI *pCmdUI) +{ + bool check = true; + switch(pCmdUI->m_nID) { + case ID_OPTIONS_JOYPAD_AUTOFIRE_A: + check = (theApp.autoFire & 1) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_B: + check = (theApp.autoFire & 2) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_L: + check = (theApp.autoFire & 512) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_R: + check = (theApp.autoFire & 256) != 0; + break; + } + pCmdUI->SetCheck(check); +} + +LRESULT MainWnd::OnConfirmMode(WPARAM, LPARAM) +{ + // we need to do this separately or the window will not have the right + // parent. must be related to the way MFC does modal dialogs + winConfirmMode(); + return 0; +} + +void MainWnd::winConfirmMode() +{ + if(theApp.renderMethod == DIRECT_DRAW && theApp.videoOption > VIDEO_4X) { + theApp.winCheckFullscreen(); + ModeConfirm dlg(theApp.m_pMainWnd); + + if(!dlg.DoModal()) { + theApp.updateVideoSize(ID_OPTIONS_VIDEO_X2); + } + } + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +void MainWnd::OnOptionsVideoFullscreenmaxscale() +{ + MaxScale dlg; + + theApp.winCheckFullscreen(); + + dlg.DoModal(); +} + + +void MainWnd::OnLinkOptions() +{ + LinkOptions dlg; + + dlg.DoModal(); +} + +void MainWnd::OnOptionsLinkLog() +{ + if(linklog){ + if(jjj!=NULL) fclose(jjj); + linklog = 0; + jjj = NULL; + } else { + char filename[20]; + sprintf(filename, "vbalog%1d.txt", vbaid+1); + jjj = fopen(filename, "wt"); + linklog = 1; + } +} + +void MainWnd::OnUpdateOptionsLinkLog(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(linklog); +} + +void MainWnd::OnOptionsLinkRFU() +{ + if(adapter) adapter = false; + else { + adapter = true; + MessageBox("Please note this is the first version\nof RFU emulation code and it's not 100% bug free.\nAlso only 2 players single computer are supported at this time.", "Warning", MB_OK); + } +} + +void MainWnd::OnUpdateOptionsLinkRFU(CCmdUI* pCmdUI) +{ + pCmdUI->SetCheck(adapter); +} \ No newline at end of file diff --git a/src/win32/MainWndTools.cpp b/src/win32/MainWndTools.cpp new file mode 100644 index 00000000..1441a148 --- /dev/null +++ b/src/win32/MainWndTools.cpp @@ -0,0 +1,603 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" + +#include "AccelEditor.h" +#include "AVIWrite.h" +#include "Disassemble.h" +#include "FileDlg.h" +#include "GBDisassemble.h" +#include "GBMapView.h" +#include "GBMemoryViewerDlg.h" +#include "GBOamView.h" +#include "GBPaletteView.h" +#include "GBTileView.h" +#include "GDBConnection.h" +#include "IOViewer.h" +#include "MapView.h" +#include "MemoryViewerDlg.h" +#include "OamView.h" +#include "PaletteView.h" +#include "Reg.h" +#include "TileView.h" +#include "WavWriter.h" +#include "WinResUtil.h" + +#include "../GBA.h" +#include "../Globals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern bool debugger; +extern int emulating; +extern int remoteSocket; + +extern void remoteCleanUp(); +extern void remoteSetSockets(SOCKET, SOCKET); +extern void toolsLogging(); + +void MainWnd::OnToolsDisassemble() +{ + if(theApp.cartridgeType == 0) { + Disassemble *dlg = new Disassemble(); + dlg->Create(IDD_DISASSEMBLE, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBDisassemble *dlg = new GBDisassemble(); + dlg->Create(IDD_GB_DISASSEMBLE, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsDisassemble(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsLogging() +{ + toolsLogging(); +} + +void MainWnd::OnUpdateToolsLogging(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsIoviewer() +{ + IOViewer *dlg = new IOViewer; + dlg->Create(IDD_IO_VIEWER,this); + dlg->ShowWindow(SW_SHOW); +} + +void MainWnd::OnUpdateToolsIoviewer(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && theApp.cartridgeType == 0); +} + +void MainWnd::OnToolsMapview() +{ + if(theApp.cartridgeType == 0) { + MapView *dlg = new MapView; + dlg->Create(IDD_MAP_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBMapView *dlg = new GBMapView; + dlg->Create(IDD_GB_MAP_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsMapview(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsMemoryviewer() +{ + if(theApp.cartridgeType == 0) { + MemoryViewerDlg *dlg = new MemoryViewerDlg; + dlg->Create(IDD_MEM_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBMemoryViewerDlg *dlg = new GBMemoryViewerDlg; + dlg->Create(IDD_MEM_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsMemoryviewer(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsOamviewer() +{ + if(theApp.cartridgeType == 0) { + OamView *dlg = new OamView; + dlg->Create(IDD_OAM_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBOamView *dlg = new GBOamView; + dlg->Create(IDD_GB_OAM_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsOamviewer(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsPaletteview() +{ + if(theApp.cartridgeType == 0) { + PaletteView *dlg = new PaletteView; + dlg->Create(IDD_PALETTE_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBPaletteView *dlg = new GBPaletteView; + dlg->Create(IDD_GB_PALETTE_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsPaletteview(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsTileviewer() +{ + if(theApp.cartridgeType == 0) { + TileView *dlg = new TileView; + dlg->Create(IDD_TILE_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } else { + GBTileView *dlg = new GBTileView; + dlg->Create(IDD_GB_TILE_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsTileviewer(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnDebugNextframe() +{ + if(theApp.paused) + theApp.paused = false; + theApp.winPauseNextFrame = true; +} + +void MainWnd::OnToolsDebugGdb() +{ + theApp.winCheckFullscreen(); + GDBPortDlg dlg; + + if(dlg.DoModal()) { + GDBWaitingDlg wait(dlg.getSocket(), dlg.getPort()); + if(wait.DoModal()) { + remoteSetSockets(wait.getListenSocket(), wait.getSocket()); + debugger = true; + emulating = 1; + theApp.cartridgeType = 0; + theApp.filename = "\\gnu_stub"; + rom = (u8 *)malloc(0x2000000); + workRAM = (u8 *)calloc(1, 0x40000); + bios = (u8 *)calloc(1,0x4000); + internalRAM = (u8 *)calloc(1,0x8000); + paletteRAM = (u8 *)calloc(1,0x400); + vram = (u8 *)calloc(1, 0x20000); + oam = (u8 *)calloc(1, 0x400); + pix = (u8 *)calloc(1, 4 * 240 * 160); + ioMem = (u8 *)calloc(1, 0x400); + + theApp.emulator = GBASystem; + + CPUInit(theApp.biosFileName, theApp.useBiosFile ? true : false); + CPUReset(); + } + } +} + +void MainWnd::OnUpdateToolsDebugGdb(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket == -1); +} + +void MainWnd::OnToolsDebugLoadandwait() +{ + theApp.winCheckFullscreen(); + if(fileOpenSelect()) { + if(FileRun()) { + if(theApp.cartridgeType != 0) { + systemMessage(IDS_ERROR_NOT_GBA_IMAGE, "Error: not a GBA image"); + OnFileClose(); + return; + } + GDBPortDlg dlg; + + if(dlg.DoModal()) { + GDBWaitingDlg wait(dlg.getSocket(), dlg.getPort()); + if(wait.DoModal()) { + remoteSetSockets(wait.getListenSocket(), wait.getSocket()); + debugger = true; + emulating = 1; + } + } + } + } +} + +void MainWnd::OnUpdateToolsDebugLoadandwait(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket == -1); +} + +void MainWnd::OnToolsDebugBreak() +{ + if(armState) { + armNextPC -= 4; + reg[15].I -= 4; + } else { + armNextPC -= 2; + reg[15].I -= 2; + } + debugger = true; +} + +void MainWnd::OnUpdateToolsDebugBreak(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket != -1); +} + +void MainWnd::OnToolsDebugDisconnect() +{ + remoteCleanUp(); + debugger = false; +} + +void MainWnd::OnUpdateToolsDebugDisconnect(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket != -1); +} + +void MainWnd::OnOptionsSoundStartrecording() +{ + theApp.winCheckFullscreen(); + CString captureBuffer; + + CString capdir = regQueryStringValue("soundRecordDir", NULL); + + if(capdir.IsEmpty()) + capdir = getDirFromFile(theApp.filename); + + CString filter = theApp.winLoadFilter(IDS_FILTER_WAV); + CString title = winResLoadString(IDS_SELECT_WAV_NAME); + + LPCTSTR exts[] = { ".WAV" }; + + FileDlg dlg(this, "", filter, 1, "WAV", exts, capdir, title, true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = theApp.soundRecordName = dlg.GetPathName(); + theApp.soundRecording = true; + + if(dlg.m_ofn.nFileOffset > 0) { + captureBuffer = captureBuffer.Left(dlg.m_ofn.nFileOffset); + } + + int len = captureBuffer.GetLength(); + + if(len > 3 && captureBuffer[len-1] == '\\') + captureBuffer = captureBuffer.Left(len-1); + regSetStringValue("soundRecordDir", captureBuffer); +} + +void MainWnd::OnUpdateOptionsSoundStartrecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(!theApp.soundRecording); +} + +void MainWnd::OnOptionsSoundStoprecording() +{ + if(theApp.soundRecorder) { + delete theApp.soundRecorder; + theApp.soundRecorder = NULL; + } + theApp.soundRecording = false; +} + +void MainWnd::OnUpdateOptionsSoundStoprecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.soundRecording); +} + +void MainWnd::OnToolsRecordStartavirecording() +{ + theApp.winCheckFullscreen(); + CString captureBuffer; + + CString capdir = regQueryStringValue("aviRecordDir", NULL); + + if(capdir.IsEmpty()) + capdir = getDirFromFile(theApp.filename); + + CString filter = theApp.winLoadFilter(IDS_FILTER_AVI); + CString title = winResLoadString(IDS_SELECT_AVI_NAME); + + LPCTSTR exts[] = { ".AVI" }; + + FileDlg dlg(this, "", filter, 1, "AVI", exts, capdir, title, true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = theApp.soundRecordName = dlg.GetPathName(); + theApp.aviRecordName = captureBuffer; + theApp.aviRecording = true; + + if(dlg.m_ofn.nFileOffset > 0) { + captureBuffer = captureBuffer.Left(dlg.m_ofn.nFileOffset); + } + + int len = captureBuffer.GetLength(); + + if(len > 3 && captureBuffer[len-1] == '\\') + captureBuffer = captureBuffer.Left(len-1); + + regSetStringValue("aviRecordDir", captureBuffer); +} + +void MainWnd::OnUpdateToolsRecordStartavirecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(!theApp.aviRecording); +} + +void MainWnd::OnToolsRecordStopavirecording() +{ + if(theApp.aviRecorder != NULL) { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + theApp.aviFrameNumber = 0; + } + theApp.aviRecording = false; +} + +void MainWnd::OnUpdateToolsRecordStopavirecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.aviRecording); +} + +void MainWnd::OnToolsRecordStartmovierecording() +{ + theApp.winCheckFullscreen(); + CString captureBuffer; + CString capdir = regQueryStringValue("movieRecordDir", ""); + + if(capdir.IsEmpty()) + capdir = getDirFromFile(theApp.filename); + + CString filter = theApp.winLoadFilter(IDS_FILTER_VMV); + CString title = winResLoadString(IDS_SELECT_MOVIE_NAME); + + LPCTSTR exts[] = { ".VMV" }; + + FileDlg dlg(this, "", filter, 1, "VMV", exts, capdir, title, true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + CString movieName = dlg.GetPathName(); + captureBuffer = movieName; + + if(dlg.m_ofn.nFileOffset > 0) { + captureBuffer = captureBuffer.Left(dlg.m_ofn.nFileOffset); + } + + int len = captureBuffer.GetLength(); + + if(len > 3 && captureBuffer[len-1] == '\\') + captureBuffer = captureBuffer.Left(len-1); + + regSetStringValue("movieRecordDir", captureBuffer); + + theApp.movieFile = fopen(movieName, "wb"); + + if(!theApp.movieFile) { + systemMessage(IDS_CANNOT_OPEN_FILE, "Cannot open file %s", + (const char *)movieName); + return; + } + + int version = 1; + + fwrite(&version, 1, sizeof(int), theApp.movieFile); + + movieName = movieName.Left(movieName.GetLength()-3) + "VM0"; + + if(writeSaveGame(movieName)) { + theApp.movieFrame = 0; + theApp.movieLastJoypad = 0; + theApp.movieRecording = true; + theApp.moviePlaying = false; + } else { + systemMessage(IDS_CANNOT_OPEN_FILE, "Cannot open file %s", + (const char *)movieName); + } +} + +void MainWnd::OnUpdateToolsRecordStartmovierecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(!theApp.movieRecording); +} + +void MainWnd::OnToolsRecordStopmovierecording() +{ + if(theApp.movieRecording) { + if(theApp.movieFile != NULL) { + // record the last joypad change so that the correct time can be + // recorded + fwrite(&theApp.movieFrame, 1, sizeof(int), theApp.movieFile); + fwrite(&theApp.movieLastJoypad, 1, sizeof(u32), theApp.movieFile); + fclose(theApp.movieFile); + theApp.movieFile = NULL; + } + theApp.movieRecording = false; + theApp.moviePlaying = false; + theApp.movieLastJoypad = 0; + } +} + +void MainWnd::OnUpdateToolsRecordStopmovierecording(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.movieRecording); +} + +void MainWnd::OnToolsPlayStartmovieplaying() +{ + static bool moviePlayMessage = false; + + if(!moviePlayMessage) { + moviePlayMessage = true; + CString msg = winResLoadString(IDS_MOVIE_PLAY); + CString title = winResLoadString(IDS_CONFIRM_ACTION); + if(MessageBox(msg, + title, + MB_OKCANCEL) == IDCANCEL) + return; + } + + CString captureBuffer; + CString capdir = regQueryStringValue("movieRecordDir", ""); + + if(capdir.IsEmpty()) + capdir = getDirFromFile(theApp.filename); + + CString filter = theApp.winLoadFilter(IDS_FILTER_VMV); + CString title = winResLoadString(IDS_SELECT_MOVIE_NAME); + + LPCTSTR exts[] = { ".VMV" }; + + FileDlg dlg(this, "", filter, 1, "VMV", exts, capdir, title, false); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + CString movieName = dlg.GetPathName(); + captureBuffer = movieName; + + theApp.movieFile = fopen(movieName, "rb"); + + if(!theApp.movieFile) { + systemMessage(IDS_CANNOT_OPEN_FILE, "Cannot open file %s", + (const char *)movieName); + return; + } + int version = 0; + fread(&version, 1, sizeof(int), theApp.movieFile); + if(version != 1) { + systemMessage(IDS_UNSUPPORTED_MOVIE_VERSION, + "Unsupported movie version %d.", + version); + fclose(theApp.movieFile); + theApp.movieFile = NULL; + return; + } + movieName = movieName.Left(movieName.GetLength()-3)+"VM0"; + if(loadSaveGame(movieName)) { + theApp.moviePlaying = true; + theApp.movieFrame = 0; + theApp.moviePlayFrame = 0; + theApp.movieLastJoypad = 0; + theApp.movieReadNext(); + } else { + systemMessage(IDS_CANNOT_OPEN_FILE, "Cannot open file %s", + (const char *)movieName); + } +} + +void MainWnd::OnUpdateToolsPlayStartmovieplaying(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(!theApp.moviePlaying); +} + +void MainWnd::OnToolsPlayStopmovieplaying() +{ + if(theApp.moviePlaying) { + if(theApp.movieFile != NULL) { + fclose(theApp.movieFile); + theApp.movieFile = NULL; + } + theApp.moviePlaying = false; + theApp.movieLastJoypad = 0; + } +} + +void MainWnd::OnUpdateToolsPlayStopmovieplaying(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.moviePlaying); +} + +void MainWnd::OnToolsRewind() +{ + if(emulating && theApp.emulator.emuReadMemState && theApp.rewindMemory && theApp.rewindCount) { + theApp.rewindPos = --theApp.rewindPos & 7; + theApp.emulator.emuReadMemState(&theApp.rewindMemory[REWIND_SIZE*theApp.rewindPos], REWIND_SIZE); + theApp.rewindCount--; + theApp.rewindCounter = 0; + } +} + +void MainWnd::OnUpdateToolsRewind(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.rewindMemory != NULL && emulating && theApp.rewindCount); +} + +void MainWnd::OnToolsCustomize() +{ + AccelEditor dlg; + + if(dlg.DoModal()) { + theApp.winAccelMgr = dlg.mgr; + theApp.winAccelMgr.UpdateWndTable(); + theApp.winAccelMgr.Write(); + theApp.winAccelMgr.UpdateMenu(theApp.menu); + } +} + +void MainWnd::OnUpdateToolsCustomize(CCmdUI* pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} diff --git a/src/win32/MapView.cpp b/src/win32/MapView.cpp new file mode 100644 index 00000000..92e7c725 --- /dev/null +++ b/src/win32/MapView.cpp @@ -0,0 +1,1026 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MapView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "MapView.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../NLS.h" +#include "../Util.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// MapView dialog + + +MapView::MapView(CWnd* pParent /*=NULL*/) + : ResizeDlg(MapView::IDD, pParent) +{ + //{{AFX_DATA_INIT(MapView) + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 1024; + bmpInfo.bmiHeader.biHeight = -1024; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 1024 * 1024); + + mapView.setData(data); + mapView.setBmpInfo(&bmpInfo); + + control = BG0CNT; + + bg = 0; + frame = 0; +} + +MapView::~MapView() +{ + free(data); + data = NULL; +} + +void MapView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MapView) + DDX_Control(pDX, IDC_NUMCOLORS, m_numcolors); + DDX_Control(pDX, IDC_MODE, m_mode); + DDX_Control(pDX, IDC_OVERFLOW, m_overflow); + DDX_Control(pDX, IDC_MOSAIC, m_mosaic); + DDX_Control(pDX, IDC_PRIORITY, m_priority); + DDX_Control(pDX, IDC_DIM, m_dim); + DDX_Control(pDX, IDC_CHARBASE, m_charbase); + DDX_Control(pDX, IDC_MAPBASE, m_mapbase); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_MAP_VIEW, mapView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, mapViewZoom); + DDX_Control(pDX, IDC_COLOR, color); +} + + +BEGIN_MESSAGE_MAP(MapView, CDialog) + //{{AFX_MSG_MAP(MapView) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_FRAME_0, OnFrame0) + ON_BN_CLICKED(IDC_FRAME_1, OnFrame1) + ON_BN_CLICKED(IDC_BG0, OnBg0) + ON_BN_CLICKED(IDC_BG1, OnBg1) + ON_BN_CLICKED(IDC_BG2, OnBg2) + ON_BN_CLICKED(IDC_BG3, OnBg3) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_SAVE, OnSave) + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// MapView message handlers + +void MapView::renderTextScreen(u16 control) +{ + u16 *palette = (u16 *)paletteRAM; + u8 *charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u16 *screenBase = (u16 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u8 *bmp = data; + + int sizeX = 256; + int sizeY = 256; + switch((control >> 14) & 3) { + case 0: + break; + case 1: + sizeX = 512; + break; + case 2: + sizeY = 512; + break; + case 3: + sizeX = 512; + sizeY = 512; + break; + } + + w = sizeX; + h = sizeY; + + if(control & 0x80) { + for(int y = 0; y < sizeY; y++) { + int yy = y & 255; + + if(y == 256 && sizeY > 256) { + screenBase += 0x400; + if(sizeX > 256) + screenBase += 0x400; + } + u16 *screenSource = screenBase + ((yy>>3)*32); + + for(int x = 0; x < sizeX; x++) { + u16 data = *screenSource; + + int tile = data & 0x3FF; + int tileX = (x & 7); + int tileY = y & 7; + + if(data & 0x0400) + tileX = 7 - tileX; + if(data & 0x0800) + tileY = 7 - tileY; + + u8 c = charBase[tile * 64 + tileY * 8 + tileX]; + + u16 color = palette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + if(data & 0x0400) { + if(tileX == 0) + screenSource++; + } else if(tileX == 7) + screenSource++; + if(x == 255 && sizeX > 256) { + screenSource = screenBase + 0x400 + ((yy>>3)*32); + } + } + } + } else { + for(int y = 0; y < sizeY; y++) { + int yy = y & 255; + + if(y == 256 && sizeY > 256) { + screenBase += 0x400; + if(sizeX > 256) + screenBase += 0x400; + } + u16 *screenSource = screenBase + ((yy>>3)*32); + + for(int x = 0; x < sizeX; x++) { + u16 data = *screenSource; + + int tile = data & 0x3FF; + int tileX = (x & 7); + int tileY = y & 7; + + if(data & 0x0400) + tileX = 7 - tileX; + if(data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[tile * 32 + tileY * 4 + (tileX>>1)]; + + if(tileX & 1) { + color = (color >> 4); + } else { + color &= 0x0F; + } + + int pal = (*screenSource>>8) & 0xF0; + u16 color2 = palette[pal + color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + + if(data & 0x0400) { + if(tileX == 0) + screenSource++; + } else if(tileX == 7) + screenSource++; + + if(x == 255 && sizeX > 256) { + screenSource = screenBase + 0x400 + ((yy>>3)*32); + } + } + } + } + /* + switch(bg) { + case 0: + renderView(BG0HOFS<<8, BG0VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 1: + renderView(BG1HOFS<<8, BG1VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 2: + renderView(BG2HOFS<<8, BG2VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 3: + renderView(BG3HOFS<<8, BG3VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + } + */ +} + +void MapView::renderRotScreen(u16 control) +{ + u16 *palette = (u16 *)paletteRAM; + u8 *charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u8 *screenBase = (u8 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u8 *bmp = data; + + int sizeX = 128; + int sizeY = 128; + switch((control >> 14) & 3) { + case 0: + break; + case 1: + sizeX = sizeY = 256; + break; + case 2: + sizeX = sizeY = 512; + break; + case 3: + sizeX = sizeY = 1024; + break; + } + + w = sizeX; + h = sizeY; + + if(control & 0x80) { + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int tile = screenBase[(x>>3) + (y>>3)*(w>>3)]; + + int tileX = (x & 7); + int tileY = y & 7; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + u16 color2 = palette[color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + } + } + } else { + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int tile = screenBase[(x>>3) + (y>>3)*(w>>3)]; + + int tileX = (x & 7); + int tileY = y & 7; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + u16 color2 = palette[color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + } + } + } + + u32 xx; + u32 yy; + + switch(bg) { + case 2: + xx = BG2X_L | BG2X_H << 16; + yy = BG2Y_L | BG2Y_H << 16; + + /* + renderView(xx, yy, + BG2PA, BG2PC, + BG2PB, BG2PD, + (sizeX -1) <<8, + (sizeY -1) << 8, + (control & 0x2000) != 0); + */ + break; + case 3: + xx = BG3X_L | BG3X_H << 16; + yy = BG3Y_L | BG3Y_H << 16; + /* + renderView(xx, yy, + BG3PA, BG3PC, + BG3PB, BG3PD, + (sizeX -1) <<8, + (sizeY -1) << 8, + (control & 0x2000) != 0); + */ + break; + } +} + +void MapView::renderMode0() +{ + renderTextScreen(control); +} + +void MapView::renderMode1() +{ + switch(bg) { + case 0: + case 1: + renderTextScreen(control); + break; + case 2: + renderRotScreen(control); + break; + default: + bg = 0; + control = BG0CNT; + renderTextScreen(control); + break; + } +} + +void MapView::renderMode2() +{ + switch(bg) { + case 2: + case 3: + renderRotScreen(control); + break; + default: + bg = 2; + control = BG2CNT; + renderRotScreen(control); + break; + } +} + +void MapView::renderMode3() +{ + u8 *bmp = data; + u16 *src = (u16 *)&vram[0]; + + w = 240; + h = 160; + + for(int y = 0; y < 160; y++) { + for(int x = 0; x < 240; x++) { + u16 data = *src++; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + + +void MapView::renderMode4() +{ + u8 *bmp = data; + u8 *src = frame ? &vram[0xa000] : &vram[0]; + u16 *pal = (u16 *)&paletteRAM[0]; + + w = 240; + h = 160; + + for(int y = 0; y < 160; y++) { + for(int x = 0; x < 240; x++) { + u8 c = *src++; + u16 data = pal[c]; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + + +void MapView::renderMode5() +{ + u8 *bmp = data; + u16 *src = (u16 *)(frame ? &vram[0xa000] : &vram[0]); + + w = 160; + h = 128; + + for(int y = 0; y < 128; y++) { + for(int x = 0; x < 160; x++) { + u16 data = *src++; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + +void MapView::OnRefresh() +{ + paint(); +} + +void MapView::paint() +{ + if(vram == NULL) + return; + int mode = DISPCNT & 7; + + switch(bg) { + default: + case 0: + control = BG0CNT; + break; + case 1: + control = BG1CNT; + break; + case 2: + control = BG2CNT; + break; + case 3: + control = BG3CNT; + break; + } + + switch(mode) { + case 0: + renderMode0(); + break; + case 1: + renderMode1(); + break; + case 2: + renderMode2(); + break; + case 3: + renderMode3(); + break; + case 4: + renderMode4(); + break; + case 5: + renderMode5(); + break; + } + enableButtons(mode); + SIZE s; + + if(mapView.getStretch()) { + mapView.setSize(w, h); + s.cx = s.cy = 1; + mapView.SetScrollSizes(MM_TEXT, s); + } else { + mapView.setSize(w, h); + s.cx = w; + s.cy = h; + mapView.SetScrollSizes(MM_TEXT, s); + } + + mapView.refresh(); + + CString buffer; + + u32 charBase = ((control >> 2) & 0x03) * 0x4000 + 0x6000000; + u32 screenBase = ((control >> 8) & 0x1f) * 0x800 + 0x6000000; + + buffer.Format("%d", mode); + m_mode.SetWindowText(buffer); + + if(mode >= 3) { + m_mapbase.SetWindowText(""); + m_charbase.SetWindowText(""); + } else { + buffer.Format("0x%08X", screenBase); + m_mapbase.SetWindowText(buffer); + + buffer.Format("0x%08X", charBase); + m_charbase.SetWindowText(buffer); + } + + buffer.Format("%dx%d", w, h); + m_dim.SetWindowText(buffer); + + m_numcolors.SetWindowText(control & 0x80 ? "256" : "16"); + + buffer.Format("%d", control & 3); + m_priority.SetWindowText(buffer); + + m_mosaic.SetWindowText(control & 0x40 ? "1" : "0"); + + m_overflow.SetWindowText(bg <= 1 ? "" : + control & 0x2000 ? "1" : "0"); +} + +BOOL MapView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_MAP_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\MapView", + NULL); + SIZE size; + size.cx = 1; + size.cy = 1; + mapView.SetScrollSizes(MM_TEXT,size); + int s = regQueryDwordValue("mapViewStretch", 0); + if(s) + mapView.setStretch(true); + ((CButton *)GetDlgItem(IDC_STRETCH))->SetCheck(s); + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MapView::PostNcDestroy() +{ + delete this; +} + +void MapView::enableButtons(int mode) +{ + bool enable[6] = { true, true, true, true, true, true }; + + switch(mode) { + case 0: + enable[4] = false; + enable[5] = false; + break; + case 1: + enable[3] = false; + enable[4] = false; + enable[5] = false; + break; + case 2: + enable[0] = false; + enable[1] = false; + enable[4] = false; + enable[5] = false; + break; + case 3: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + enable[4] = false; + enable[5] = false; + break; + case 4: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + break; + case 5: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + break; + } + GetDlgItem(IDC_BG0)->EnableWindow(enable[0]); + GetDlgItem(IDC_BG1)->EnableWindow(enable[1]); + GetDlgItem(IDC_BG2)->EnableWindow(enable[2]); + GetDlgItem(IDC_BG3)->EnableWindow(enable[3]); + GetDlgItem(IDC_FRAME_0)->EnableWindow(enable[4]); + GetDlgItem(IDC_FRAME_1)->EnableWindow(enable[5]); + int id = IDC_BG0; + switch(bg) { + case 1: + id = IDC_BG1; + break; + case 2: + id = IDC_BG2; + break; + case 3: + id = IDC_BG3; + break; + } + CheckRadioButton(IDC_BG0, IDC_BG3, id); + id = IDC_FRAME_0; + if(frame != 0) + id = IDC_FRAME_1; + CheckRadioButton(IDC_FRAME_0, IDC_FRAME_1, id); +} + +void MapView::OnFrame0() +{ + frame = 0; + paint(); +} + +void MapView::OnFrame1() +{ + frame = 1; + paint(); +} + +void MapView::OnBg0() +{ + bg = 0; + control = BG0CNT; + paint(); +} + +void MapView::OnBg1() +{ + bg = 1; + control = BG1CNT; + paint(); +} + +void MapView::OnBg2() +{ + bg = 2; + control = BG2CNT; + paint(); +} + +void MapView::OnBg3() +{ + bg = 3; + control = BG3CNT; + paint(); +} + +void MapView::OnStretch() +{ + mapView.setStretch(!mapView.getStretch()); + paint(); + regSetDwordValue("mapViewStretch", mapView.getStretch()); +} + +void MapView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void MapView::update() +{ + paint(); +} + +void MapView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +u32 MapView::GetTextClickAddress(u32 base, int x, int y) +{ + if(y > 255 && h > 256) { + base += 0x800; + if(w > 256) + base += 0x800; + } + if(x >= 256) + base += 0x800; + x &= 255; + y &= 255; + base += (x>>3)*2 + 64*(y>>3); + + return base; +} + + + +u32 MapView::GetClickAddress(int x, int y) +{ + int mode = DISPCNT & 7; + + u32 base = ((control >> 8) & 0x1f) * 0x800 + 0x6000000; + + // all text bgs (16 bits) + if(mode == 0 ||(mode < 3 && bg < 2)) { + return GetTextClickAddress(base, x, y); + } + // rot bgs (8 bits) + if(mode < 3) { + return base + (x>>3) + (w>>3)*(y>>3); + } + // mode 3/5 (16 bits) + if(mode != 4) { + return 0x6000000 + 0xa000*frame + 2*x + w*y*2; + } + // mode 4 (8 bits) + return 0x6000000 + 0xa000*frame + x + w*y; +} + +LRESULT MapView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + mapViewZoom.setColors(colors); + + int x = wParam & 0xffff; + int y = (wParam >> 16); + + CString buffer; + buffer.Format("(%d,%d)", x, y); + GetDlgItem(IDC_XY)->SetWindowText(buffer); + + u32 address = GetClickAddress(x,y); + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int mode = DISPCNT & 7; + if(mode >= 3) { + // bitmap modes + GetDlgItem(IDC_TILE_NUM)->SetWindowText("---"); + GetDlgItem(IDC_FLIP)->SetWindowText("--"); + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText("---"); + } else if(mode == 0 || bg < 2) { + // text bgs + u16 value = *((u16 *)&vram[address - 0x6000000]); + + int tile = value & 1023; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUM)->SetWindowText(buffer); + buffer.Empty(); + buffer += value & 1024 ? 'H' : '-'; + buffer += value & 2048 ? 'V' : '-'; + GetDlgItem(IDC_FLIP)->SetWindowText(buffer); + + if(!(control & 0x80)) { + buffer.Format("%d", (value >> 12) & 15); + } else + buffer = "---"; + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText(buffer); + } else { + // rot bgs + GetDlgItem(IDC_TILE_NUM)->SetWindowText("---"); + GetDlgItem(IDC_FLIP)->SetWindowText("--"); + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText("---"); + } + + return TRUE; +} + +LRESULT MapView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void MapView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + + +void MapView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void MapView::OnSave() +{ + CString filename; + + if(theApp.captureFormat == 0) + filename = "map.png"; + else + filename = "map.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + filename, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + if(dlg.getFilterIndex() == 2) + saveBMP(dlg.GetPathName()); + else + savePNG(dlg.GetPathName()); +} diff --git a/src/win32/MapView.h b/src/win32/MapView.h new file mode 100644 index 00000000..db0a575d --- /dev/null +++ b/src/win32/MapView.h @@ -0,0 +1,122 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_) +#define AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MapView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "ZoomControl.h" +#include "ResizeDlg.h" +#include "IUpdate.h" +#include "..\System.h" // Added by ClassView + +///////////////////////////////////////////////////////////////////////////// +// MapView dialog + +class MapView : public ResizeDlg, IUpdateListener +{ + private: + BITMAPINFO bmpInfo; + u8 *data; + int frame; + u16 control; + int bg; + int w; + int h; + BitmapControl mapView; + ZoomControl mapViewZoom; + ColorControl color; + bool autoUpdate; + + // Construction + public: + void savePNG(const char *name); + void saveBMP(const char *name); + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + u32 GetClickAddress(int x, int y); + u32 GetTextClickAddress(u32 base, int x, int y); + void update(); + void enableButtons(int mode); + void paint(); + void renderMode5(); + void renderMode4(); + void renderMode3(); + void renderMode2(); + void renderMode1(); + void renderMode0(); + void renderRotScreen(u16 control); + void renderTextScreen(u16 control); + MapView(CWnd* pParent = NULL); // standard constructor + ~MapView(); + + // Dialog Data + //{{AFX_DATA(MapView) + enum { IDD = IDD_MAP_VIEW }; + CStatic m_numcolors; + CStatic m_mode; + CStatic m_overflow; + CStatic m_mosaic; + CStatic m_priority; + CStatic m_dim; + CStatic m_charbase; + CStatic m_mapbase; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MapView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(MapView) + afx_msg void OnRefresh(); + virtual BOOL OnInitDialog(); + afx_msg void OnFrame0(); + afx_msg void OnFrame1(); + afx_msg void OnBg0(); + afx_msg void OnBg1(); + afx_msg void OnBg2(); + afx_msg void OnBg3(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + afx_msg void OnSave(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_) diff --git a/src/win32/MaxScale.cpp b/src/win32/MaxScale.cpp new file mode 100644 index 00000000..1972f887 --- /dev/null +++ b/src/win32/MaxScale.cpp @@ -0,0 +1,93 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MaxScale.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "MaxScale.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// MaxScale dialog + + +MaxScale::MaxScale(CWnd* pParent /*=NULL*/) + : CDialog(MaxScale::IDD, pParent) +{ + //{{AFX_DATA_INIT(MaxScale) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void MaxScale::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MaxScale) + DDX_Control(pDX, IDC_VALUE, m_value); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(MaxScale, CDialog) + //{{AFX_MSG_MAP(MaxScale) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MaxScale message handlers + +void MaxScale::OnCancel() +{ + EndDialog(FALSE); +} + +void MaxScale::OnOk() +{ + CString tmp; + m_value.GetWindowText(tmp); + theApp.fsMaxScale = atoi(tmp); + theApp.updateWindowSize(theApp.videoOption); + if(theApp.display) + theApp.display->clear(); + this->SetFocus(); + EndDialog(TRUE); +} + +BOOL MaxScale::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp; + + temp.Format("%d", theApp.fsMaxScale); + + m_value.SetWindowText(temp); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/MaxScale.h b/src/win32/MaxScale.h new file mode 100644 index 00000000..2ad3d455 --- /dev/null +++ b/src/win32/MaxScale.h @@ -0,0 +1,67 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_) +#define AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MaxScale.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MaxScale dialog + +class MaxScale : public CDialog +{ +// Construction +public: + MaxScale(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(MaxScale) + enum { IDD = IDD_MAX_SCALE }; + CEdit m_value; + //}}AFX_DATA + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MaxScale) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(MaxScale) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_) diff --git a/src/win32/MemoryViewer.cpp b/src/win32/MemoryViewer.cpp new file mode 100644 index 00000000..ff9c8796 --- /dev/null +++ b/src/win32/MemoryViewer.cpp @@ -0,0 +1,622 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MemoryViewer.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "MemoryViewer.h" + +#include "../System.h" +extern int emulating; + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewer + +bool MemoryViewer::isRegistered = false; + +MemoryViewer::MemoryViewer() +{ + address = 0; + addressSize = 0; + dataSize = 0; + editAddress = 0; + editNibble = 0; + displayedLines = 0; + hasCaret = false; + maxNibble = 0; + font = (HFONT)GetStockObject(SYSTEM_FIXED_FONT); + fontSize.cx = fontSize.cy = 0; + beginAscii = 0; + beginHex = 0; + dlg = NULL; + registerClass(); +} + +MemoryViewer::~MemoryViewer() +{ +} + + +BEGIN_MESSAGE_MAP(MemoryViewer, CWnd) + //{{AFX_MSG_MAP(MemoryViewer) + ON_WM_ERASEBKGND() + ON_WM_PAINT() + ON_WM_VSCROLL() + ON_WM_GETDLGCODE() + ON_WM_LBUTTONDOWN() + ON_WM_SETFOCUS() + ON_WM_KILLFOCUS() + ON_WM_KEYDOWN() + //}}AFX_MSG_MAP + ON_MESSAGE(WM_CHAR, OnWMChar) + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// MemoryViewer message handlers + +void MemoryViewer::setDialog(IMemoryViewerDlg *d) +{ + dlg = d; +} + + +void MemoryViewer::setAddress(u32 a) +{ + address = a; + if(displayedLines) { + if(addressSize) { + u16 addr = address; + if((u16)(addr+(displayedLines<<4)) < addr) { + address = 0xffff - (displayedLines<<4) + 1; + } + } else { + if((address+(displayedLines<<4)) < address) { + address = 0xffffffff - (displayedLines<<4) + 1; + } + } + } + if(addressSize) + address &= 0xffff; + setCaretPos(); + InvalidateRect(NULL, TRUE); +} + + +void MemoryViewer::setSize(int s) +{ + dataSize = s; + if(s == 0) + maxNibble = 1; + else if(s == 1) + maxNibble = 3; + else + maxNibble = 7; + + InvalidateRect(NULL, TRUE); +} + +BOOL MemoryViewer::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + +void MemoryViewer::updateScrollInfo(int lines) +{ + int page = lines * 16; + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + if(addressSize) { + si.nMax = 0x10000/page; + si.nPage = 1; + } else { + si.nMax = 0xa000000 / page; + si.nPage = page; + } + + si.nPos = address / page; + SetScrollInfo(SB_VERT, + &si, + TRUE); +} + +void MemoryViewer::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + int w = rect.right - rect.left; + int h = rect.bottom - rect.top - 6; + + CDC memDC; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, rect.bottom - rect.top); + pOldBitmap = memDC.SelectObject(&bitmap); + + memDC.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); + memDC.DrawEdge(&rect, EDGE_ETCHED, BF_RECT); + + CFont *oldFont = memDC.SelectObject(CFont::FromHandle(font)); + + fontSize = memDC.GetTextExtent("0", 1); + + int lines = h / fontSize.cy; + + displayedLines = lines; + + updateScrollInfo(lines); + + u32 addr = address; + + memDC.SetTextColor(RGB(0,0,0)); + + u8 data[32]; + + RECT r; + r.top = 3; + r.left = 3; + r.bottom = r.top+fontSize.cy; + r.right = rect.right-3; + + int line = 0; + + for(int i = 0; i < lines; i++) { + CString buffer; + if(addressSize) + buffer.Format("%04X", addr); + else + buffer.Format("%08X", addr); + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 10*fontSize.cx; + beginHex = r.left; + readData(addr, 16, data); + + int j; + + if(dataSize == 0) { + for(j = 0; j < 16; j++) { + buffer.Format("%02X", data[j]); + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 3*fontSize.cx; + } + } + if(dataSize == 1) { + for(j = 0; j < 16; j += 2) { + buffer.Format("%04X", data[j] | data[j+1]<<8); + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 5*fontSize.cx; + } + } + if(dataSize == 2) { + for(j = 0; j < 16; j += 4) { + buffer.Format("%08X", data[j] | data[j+1]<<8 | + data[j+2] << 16 | data[j+3] << 24); + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 9*fontSize.cx; + } + } + + line = r.left; + + r.left += fontSize.cx; + beginAscii = r.left; + buffer.Empty(); + for(j = 0; j < 16; j++) { + char c = data[j]; + if(c >= 32 && c <= 127) { + buffer += c; + } else + buffer += '.'; + } + + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + addr += 16; + if(addressSize) + addr &= 0xffff; + r.top += fontSize.cy; + r.bottom += fontSize.cy; + r.left = 3; + } + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(0,0,0)); + CPen *old = memDC.SelectObject(&pen); + + memDC.MoveTo(3+fontSize.cx*9, 3); + memDC.LineTo(3+fontSize.cx*9, 3+displayedLines*fontSize.cy); + + memDC.MoveTo(line, 3); + memDC.LineTo(line, 3+displayedLines*fontSize.cy); + + memDC.SelectObject(old); + pen.DeleteObject(); + + memDC.SelectObject(oldFont); + + dc.BitBlt(0, 0, w, rect.bottom - rect.top, &memDC, 0, 0, SRCCOPY); + + memDC.SelectObject(pOldBitmap); + memDC.DeleteDC(); + bitmap.DeleteObject(); +} + +void MemoryViewer::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + int address = this->address; + switch(nSBCode) { + case SB_BOTTOM: + address = 0xffffff00; + break; + case SB_LINEDOWN: + address += 0x10; + break; + case SB_LINEUP: + address -= 0x10; + break; + case SB_PAGEDOWN: + address += (displayedLines<<4); + break; + case SB_PAGEUP: + address -= (displayedLines<<4); + break; + case SB_TOP: + address = 0; + break; + case SB_THUMBTRACK: + { + int page = displayedLines * 16; + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_TRACKPOS; + GetScrollInfo(SB_VERT, &si); + address = page * si.nTrackPos; + } + break; + } + setAddress(address); +} + +UINT MemoryViewer::OnGetDlgCode() +{ + return DLGC_WANTALLKEYS; +} + +void MemoryViewer::createEditCaret(int w, int h) +{ + if(!hasCaret || caretWidth != w || caretHeight != h) { + hasCaret = true; + caretWidth = w; + caretHeight = h; + ::CreateCaret(m_hWnd, (HBITMAP)0, w, h); + } +} + + +void MemoryViewer::destroyEditCaret() +{ + hasCaret = false; + DestroyCaret(); +} + +void MemoryViewer::setCaretPos() +{ + if(GetFocus() != this) { + destroyEditCaret(); + return; + } + + if(dlg) + dlg->setCurrentAddress(editAddress); + + if(editAddress < address || editAddress > (address -1 + (displayedLines<<4))) { + destroyEditCaret(); + return; + } + + int subAddress = (editAddress - address); + + int x = 3+10*fontSize.cx+editNibble*fontSize.cx; + int y = 3+fontSize.cy*((editAddress-address)>>4); + + if(editAscii) { + x = beginAscii + fontSize.cx*(subAddress&15); + } else { + switch(dataSize) { + case 0: + x += 3*fontSize.cx*(subAddress & 15); + break; + case 1: + x += 5*fontSize.cx*((subAddress>>1) & 7); + break; + case 2: + x += 9*fontSize.cx*((subAddress>>2) & 3); + break; + } + } + + RECT r; + GetClientRect(&r); + r.right -= 3; + if(x >= r.right) { + destroyEditCaret(); + return; + } + int w = fontSize.cx; + if((x+fontSize.cx)>=r.right) + w = r.right - x; + createEditCaret(w, fontSize.cy); + ::SetCaretPos(x, y); + ShowCaret(); +} + +void MemoryViewer::OnLButtonDown(UINT nFlags, CPoint point) +{ + int x = point.x; + int y = point.y; + int line = (y-3)/fontSize.cy; + int beforeAscii = beginHex; + int inc = 1; + int sub = 3*fontSize.cx; + switch(dataSize) { + case 0: + beforeAscii += 47*fontSize.cx; + break; + case 1: + beforeAscii += 39*fontSize.cx; + inc = 2; + sub = 5*fontSize.cx; + break; + case 2: + beforeAscii += 35*fontSize.cx; + inc = 4; + sub = 9*fontSize.cx; + break; + } + + editAddress = address + (line<<4); + if(x >= beginHex && x < beforeAscii) { + x -= beginHex; + editNibble = 0; + while(x > 0) { + x -= sub; + if(x >= 0) + editAddress += inc; + else { + editNibble = (x + sub)/fontSize.cx; + } + } + editAscii = false; + } else if(x >= beginAscii) { + int afterAscii = beginAscii+16*fontSize.cx; + if(x >= afterAscii) + x = afterAscii-1; + editAddress += (x-beginAscii)/fontSize.cx; + editNibble = 0; + editAscii = true; + } else { + return; + } + + if(editNibble > maxNibble) + editNibble = maxNibble; + SetFocus(); + setCaretPos(); +} + +void MemoryViewer::OnSetFocus(CWnd* pOldWnd) +{ + setCaretPos(); + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::OnKillFocus(CWnd* pNewWnd) +{ + destroyEditCaret(); + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{ + bool isShift = (GetKeyState(VK_SHIFT) & 0x80000000) == 0x80000000; + + switch(nChar) { + case VK_RIGHT: + if(editAscii) + moveAddress(1,0); + else if(isShift) + moveAddress((maxNibble+1)>>1,0); + else + moveAddress(0, 1); + break; + case VK_LEFT: + if(editAscii) + moveAddress(-1, 0); + else if(isShift) + moveAddress(-((maxNibble+1)>>1),0); + else + moveAddress(0, -1); + break; + case VK_DOWN: + moveAddress(16, 0); + break; + case VK_UP: + moveAddress(-16, 0); + break; + case VK_TAB: + GetNextDlgTabItem(GetParent(), isShift)->SetFocus(); + break; + } +} + +void MemoryViewer::moveAddress(s32 offset, int nibbleOff) +{ + if(offset == 0) { + if(nibbleOff == -1) { + editNibble--; + if(editNibble == -1) { + editAddress -= (maxNibble + 1) >> 1; + editNibble = maxNibble; + } + if(address == 0 && (editAddress >= (u32)(displayedLines<<4))) { + editAddress = 0; + editNibble = 0; + beep(); + } + if(editAddress < address) + setAddress(address - 16); + } else { + editNibble++; + if(editNibble > maxNibble) { + editNibble = 0; + editAddress += (maxNibble + 1) >> 1; + } + if(editAddress < address) { + editAddress -= (maxNibble + 1) >> 1; + editNibble = maxNibble; + beep(); + } + if(editAddress >= (address+(displayedLines<<4))) + setAddress(address+16); + } + } else { + editAddress += offset; + if(offset < 0 && editAddress > (address-1+(displayedLines<<4))) { + editAddress -= offset; + beep(); + return; + } + if(offset > 0 && (editAddress < address)) { + editAddress -= offset; + beep(); + return; + } + if(editAddress < address) { + if(offset & 15) + setAddress((address+offset-16) & ~15); + else + setAddress(address+offset); + } else if(editAddress > (address - 1 + (displayedLines<<4))) { + if(offset & 15) + setAddress((address+offset+16) & ~15); + else + setAddress(address+offset); + } + } + + setCaretPos(); +} + +LRESULT MemoryViewer::OnWMChar(WPARAM wParam, LPARAM LPARAM) +{ + if(OnEditInput(wParam)) + return 0; + return 1; +} + +bool MemoryViewer::OnEditInput(UINT c) +{ + if(c > 255 || !emulating) { + beep(); + return false; + } + + if(!editAscii) + c = tolower(c); + + u32 value = 256; + + if(c >= 'a' && c <= 'f') + value = 10 + (c - 'a'); + else if(c >= '0' && c <= '9') + value = (c - '0'); + if(editAscii) { + editData(editAddress, 8, 0, c); + moveAddress(1, 0); + InvalidateRect(NULL, TRUE); + } else { + if(value != 256) { + value <<= 4*(maxNibble-editNibble); + u32 mask = ~(15 << 4*(maxNibble - editNibble)); + switch(dataSize) { + case 0: + editData(editAddress, 8, mask, value); + break; + case 1: + editData(editAddress, 16, mask, value); + break; + case 2: + editData(editAddress, 32, mask, value); + break; + } + moveAddress(0, 1); + InvalidateRect(NULL, TRUE); + } + } + return true; +} + +void MemoryViewer::beep() +{ + MessageBeep((UINT)-1); +} + +void MemoryViewer::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(WHITE_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaMemoryViewer"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void MemoryViewer::setAddressSize(int s) +{ + addressSize = s; +} + + +u32 MemoryViewer::getCurrentAddress() +{ + return editAddress; +} + +int MemoryViewer::getSize() +{ + return dataSize; +} diff --git a/src/win32/MemoryViewer.h b/src/win32/MemoryViewer.h new file mode 100644 index 00000000..2db7189e --- /dev/null +++ b/src/win32/MemoryViewer.h @@ -0,0 +1,114 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_) +#define AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MemoryViewer.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewer window + +class IMemoryViewerDlg { + public: + virtual void setCurrentAddress(u32 address)=0; +}; + +class MemoryViewer : public CWnd +{ + u32 address; + int addressSize; + int dataSize; + bool hasCaret; + int caretWidth; + int caretHeight; + HFONT font; + CSize fontSize; + u32 editAddress; + int editNibble; + int maxNibble; + int displayedLines; + int beginAscii; + int beginHex; + bool editAscii; + IMemoryViewerDlg *dlg; + + static bool isRegistered; + // Construction + public: + MemoryViewer(); + + // Attributes + public: + + // Operations + public: + virtual void readData(u32,int,u8 *) = 0; + virtual void editData(u32,int,int,u32)=0; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewer) + //}}AFX_VIRTUAL + + // Implementation + public: + int getSize(); + u32 getCurrentAddress(); + void setAddressSize(int s); + void registerClass(); + void beep(); + bool OnEditInput(UINT c); + void moveAddress(s32 offset, int nibbleOff); + void setCaretPos(); + void destroyEditCaret(); + void createEditCaret(int w, int h); + void updateScrollInfo(int lines); + void setSize(int s); + void setAddress(u32 a); + void setDialog(IMemoryViewerDlg *d); + virtual ~MemoryViewer(); + + // Generated message map functions + protected: + //{{AFX_MSG(MemoryViewer) + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + afx_msg void OnPaint(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + afx_msg UINT OnGetDlgCode(); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg void OnSetFocus(CWnd* pOldWnd); + afx_msg void OnKillFocus(CWnd* pNewWnd); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + afx_msg LRESULT OnWMChar(WPARAM wParam, LPARAM lParam); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_) diff --git a/src/win32/MemoryViewerAddressSize.cpp b/src/win32/MemoryViewerAddressSize.cpp new file mode 100644 index 00000000..78b00560 --- /dev/null +++ b/src/win32/MemoryViewerAddressSize.cpp @@ -0,0 +1,137 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MemoryViewerAddressSize.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "MemoryViewerAddressSize.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize dialog + + +MemoryViewerAddressSize::MemoryViewerAddressSize(u32 a, int s, CWnd* pParent /*=NULL*/) + : CDialog(MemoryViewerAddressSize::IDD, pParent) +{ + //{{AFX_DATA_INIT(MemoryViewerAddressSize) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + address = a; + size = s; +} + + +void MemoryViewerAddressSize::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MemoryViewerAddressSize) + DDX_Control(pDX, IDC_SIZE_CONTROL, m_size); + DDX_Control(pDX, IDC_ADDRESS, m_address); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(MemoryViewerAddressSize, CDialog) + //{{AFX_MSG_MAP(MemoryViewerAddressSize) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize message handlers + +BOOL MemoryViewerAddressSize::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + if(address != 0xFFFFFFFF) { + buffer.Format("%08X", address); + m_address.SetWindowText(buffer); + } + if(size != -1) { + buffer.Format("%08X", size); + m_size.SetWindowText(buffer); + m_size.EnableWindow(FALSE); + } + + if(size == -1 && address != 0xFFFFFFFF) + m_size.SetFocus(); + + m_address.LimitText(9); + m_size.LimitText(9); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MemoryViewerAddressSize::OnOk() +{ + CString buffer; + + m_address.GetWindowText(buffer); + if(buffer.IsEmpty()) { + m_address.SetFocus(); + return; + } + sscanf(buffer, "%x", &address); + + m_size.GetWindowText(buffer); + if(buffer.IsEmpty()) { + m_size.SetFocus(); + return; + } + sscanf(buffer, "%x", &size); + EndDialog(TRUE); +} + +void MemoryViewerAddressSize::OnCancel() +{ + EndDialog(FALSE); +} + +void MemoryViewerAddressSize::setAddress(u32 a) +{ + address = a; +} + +void MemoryViewerAddressSize::setSize(int s) +{ + size = s; +} + + +u32 MemoryViewerAddressSize::getAddress() +{ + return address; +} +\ + +int MemoryViewerAddressSize::getSize() +{ + return size; +} diff --git a/src/win32/MemoryViewerAddressSize.h b/src/win32/MemoryViewerAddressSize.h new file mode 100644 index 00000000..71d3b5ee --- /dev/null +++ b/src/win32/MemoryViewerAddressSize.h @@ -0,0 +1,75 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_) +#define AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MemoryViewerAddressSize.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize dialog + +class MemoryViewerAddressSize : public CDialog +{ + u32 address; + int size; + // Construction + public: + int getSize(); + u32 getAddress(); + void setSize(int s); + void setAddress(u32 a); + MemoryViewerAddressSize(u32 a=0xffffff, int s=-1, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MemoryViewerAddressSize) + enum { IDD = IDD_ADDR_SIZE }; + CEdit m_size; + CEdit m_address; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewerAddressSize) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(MemoryViewerAddressSize) + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_) diff --git a/src/win32/MemoryViewerDlg.cpp b/src/win32/MemoryViewerDlg.cpp new file mode 100644 index 00000000..09449de4 --- /dev/null +++ b/src/win32/MemoryViewerDlg.cpp @@ -0,0 +1,424 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// MemoryViewerDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "MemoryViewerAddressSize.h" +#include "MemoryViewerDlg.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern int emulating; + +#define CPUReadByteQuick(addr) \ + ::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask] +#define CPUWriteByteQuick(addr, b) \ + ::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask] = (b) +#define CPUReadHalfWordQuick(addr) \ + *((u16 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) +#define CPUWriteHalfWordQuick(addr, b) \ + *((u16 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) = (b) +#define CPUReadMemoryQuick(addr) \ + *((u32 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) +#define CPUWriteMemoryQuick(addr, b) \ + *((u32 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) = (b) + +///////////////////////////////////////////////////////////////////////////// +// GBAMemoryViewer control + + +GBAMemoryViewer::GBAMemoryViewer() + : MemoryViewer() +{ + setAddressSize(0); +} + +void GBAMemoryViewer::readData(u32 address, int len, u8 *data) +{ + if(emulating && rom != NULL) { + for(int i = 0; i < len; i++) { + *data++ = CPUReadByteQuick(address); + address++; + } + } else { + for(int i = 0; i < len; i++) { + *data++ = 0; + address++; + } + } +} + +void GBAMemoryViewer::editData(u32 address, int size, int mask, u32 value) +{ + u32 oldValue; + + switch(size) { + case 8: + oldValue = (CPUReadByteQuick(address) & mask) | value; + CPUWriteByteQuick(address, oldValue); + break; + case 16: + oldValue = (CPUReadHalfWordQuick(address) & mask) | value; + CPUWriteHalfWordQuick(address, oldValue); + break; + case 32: + oldValue = (CPUReadMemoryQuick(address) & mask) | value; + CPUWriteMemoryQuick(address, oldValue); + break; + } +} + + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg dialog + + +MemoryViewerDlg::MemoryViewerDlg(CWnd* pParent /*=NULL*/) + : ResizeDlg(MemoryViewerDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(MemoryViewerDlg) + m_size = -1; + //}}AFX_DATA_INIT + autoUpdate = false; +} + + +void MemoryViewerDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MemoryViewerDlg) + DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_ADDRESSES, m_addresses); + DDX_Radio(pDX, IDC_8_BIT, m_size); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_VIEWER, m_viewer); +} + + +BEGIN_MESSAGE_MAP(MemoryViewerDlg, CDialog) + //{{AFX_MSG_MAP(MemoryViewerDlg) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_REFRESH, OnRefresh) + ON_BN_CLICKED(IDC_8_BIT, On8Bit) + ON_BN_CLICKED(IDC_16_BIT, On16Bit) + ON_BN_CLICKED(IDC_32_BIT, On32Bit) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_GO, OnGo) + ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_LOAD, OnLoad) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg message handlers + +BOOL MemoryViewerDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_VIEWER, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_LOAD, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\MemoryView", + NULL); + + m_viewer.setDialog(this); + m_viewer.ShowScrollBar(SB_VERT, TRUE); + m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH); + + LPCTSTR s[] = { + "0x00000000 - BIOS", + "0x02000000 - WRAM", + "0x03000000 - IRAM", + "0x04000000 - I/O", + "0x05000000 - PALETTE", + "0x06000000 - VRAM", + "0x07000000 - OAM", + "0x08000000 - ROM" + }; + + for(int i = 0; i < 8; i++) + m_addresses.AddString(s[i]); + + m_addresses.SetCurSel(0); + + RECT cbSize; + int Height; + + m_addresses.GetClientRect(&cbSize); + Height = m_addresses.GetItemHeight(-1); + Height += m_addresses.GetItemHeight(0) * (9); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_addresses.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.LimitText(8); + + m_size = regQueryDwordValue("memViewerDataSize", 0); + if(m_size < 0 || m_size > 2) + m_size = 0; + m_viewer.setSize(m_size); + UpdateData(FALSE); + + m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MemoryViewerDlg::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void MemoryViewerDlg::OnRefresh() +{ + m_viewer.Invalidate(); +} + +void MemoryViewerDlg::update() +{ + OnRefresh(); +} + + +void MemoryViewerDlg::On8Bit() +{ + m_viewer.setSize(0); + regSetDwordValue("memViewerDataSize", 0); +} + +void MemoryViewerDlg::On16Bit() +{ + m_viewer.setSize(1); + regSetDwordValue("memViewerDataSize", 1); +} + +void MemoryViewerDlg::On32Bit() +{ + m_viewer.setSize(2); + regSetDwordValue("memViewerDataSize", 2); +} + +void MemoryViewerDlg::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void MemoryViewerDlg::OnGo() +{ + CString buffer; + + m_address.GetWindowText(buffer); + + u32 address; + sscanf(buffer, "%x", &address); + if(m_viewer.getSize() == 1) + address &= ~1; + else if(m_viewer.getSize() == 2) + address &= ~3; + m_viewer.setAddress(address); +} + +void MemoryViewerDlg::OnSelchangeAddresses() +{ + int cur = m_addresses.GetCurSel(); + + switch(cur) { + case 0: + m_viewer.setAddress(0); + break; + case 1: + m_viewer.setAddress(0x2000000); + break; + case 2: + m_viewer.setAddress(0x3000000); + break; + case 3: + m_viewer.setAddress(0x4000000); + break; + case 4: + m_viewer.setAddress(0x5000000); + break; + case 5: + m_viewer.setAddress(0x6000000); + break; + case 6: + m_viewer.setAddress(0x7000000); + break; + case 7: + m_viewer.setAddress(0x8000000); + break; + } +} + +void MemoryViewerDlg::setCurrentAddress(u32 address) +{ + CString buffer; + + buffer.Format("0x%08X", address); + m_current.SetWindowText(buffer); +} + +void MemoryViewerDlg::OnSave() +{ + MemoryViewerAddressSize dlg; + CString buffer; + + dlg.setAddress(m_viewer.getCurrentAddress()); + + LPCTSTR exts[] = { ".dmp" }; + + if(dlg.DoModal() == IDOK) { + CString filter = theApp.winLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + true); + if(file.DoModal() == IDOK) { + buffer = file.GetPathName(); + + FILE *f = fopen(buffer, "wb"); + + if(f == NULL) { + systemMessage(IDS_ERROR_CREATING_FILE, buffer); + return; + } + + int size = dlg.getSize(); + u32 addr = dlg.getAddress(); + + for(int i = 0; i < size; i++) { + fputc(CPUReadByteQuick(addr), f); + addr++; + } + + fclose(f); + } + } +} + +void MemoryViewerDlg::OnLoad() +{ + CString buffer; + LPCTSTR exts[] = { ".dmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + false); + + if(file.DoModal() == IDOK) { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "rb"); + if(f == NULL) { + systemMessage(IDS_CANNOT_OPEN_FILE, + "Cannot open file %s", + buffer); + return; + } + + MemoryViewerAddressSize dlg; + + fseek(f, 0, SEEK_END); + int size = ftell(f); + + fseek(f, 0, SEEK_SET); + + dlg.setAddress(m_viewer.getCurrentAddress()); + dlg.setSize(size); + + if(dlg.DoModal() == IDOK) { + int size = dlg.getSize(); + u32 addr = dlg.getAddress(); + + for(int i = 0; i < size; i++) { + int c = fgetc(f); + if(c == -1) + break; + CPUWriteByteQuick(addr, c); + addr++; + } + OnRefresh(); + } + fclose(f); + } +} + +void MemoryViewerDlg::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/MemoryViewerDlg.h b/src/win32/MemoryViewerDlg.h new file mode 100644 index 00000000..90d53f7f --- /dev/null +++ b/src/win32/MemoryViewerDlg.h @@ -0,0 +1,94 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_) +#define AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MemoryViewerDlg.h : header file +// + +#include "MemoryViewer.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +class GBAMemoryViewer : public MemoryViewer { + public: + GBAMemoryViewer(); + virtual void readData(u32, int, u8 *); + virtual void editData(u32,int,int,u32); +}; + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg dialog + +class MemoryViewerDlg : public ResizeDlg, IUpdateListener, IMemoryViewerDlg +{ + GBAMemoryViewer m_viewer; + // Construction + public: + void setCurrentAddress(u32 address); + bool autoUpdate; + void update(); + MemoryViewerDlg(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MemoryViewerDlg) + enum { IDD = IDD_MEM_VIEWER }; + CEdit m_current; + CEdit m_address; + CComboBox m_addresses; + int m_size; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewerDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(MemoryViewerDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void On8Bit(); + afx_msg void On16Bit(); + afx_msg void On32Bit(); + afx_msg void OnAutoUpdate(); + afx_msg void OnGo(); + afx_msg void OnSelchangeAddresses(); + afx_msg void OnSave(); + afx_msg void OnLoad(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_) diff --git a/src/win32/ModeConfirm.cpp b/src/win32/ModeConfirm.cpp new file mode 100644 index 00000000..6ac88032 --- /dev/null +++ b/src/win32/ModeConfirm.cpp @@ -0,0 +1,113 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// ModeConfirm.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "ModeConfirm.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// ModeConfirm dialog + + +ModeConfirm::ModeConfirm(CWnd* pParent /*=NULL*/) + : CDialog(ModeConfirm::IDD, pParent) +{ + //{{AFX_DATA_INIT(ModeConfirm) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void ModeConfirm::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(ModeConfirm) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(ModeConfirm, CDialog) + //{{AFX_MSG_MAP(ModeConfirm) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + ON_WM_DESTROY() + ON_WM_TIMER() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// ModeConfirm message handlers + +void ModeConfirm::OnCancel() +{ + EndDialog(FALSE); +} + +void ModeConfirm::OnOk() +{ + EndDialog(TRUE); +} + +void ModeConfirm::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timer); + timer = 0; +} + +BOOL ModeConfirm::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timer = SetTimer(0, 1000, NULL); + + count = 10; + + CString buffer; + buffer.Format("%d", count); + + GetDlgItem(IDC_TIMER)->SetWindowText(buffer); + + CenterWindow(theApp.m_pMainWnd); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void ModeConfirm::OnTimer(UINT nIDEvent) +{ + CString buffer; + count--; + if(count == 0) + EndDialog(FALSE); + buffer.Format("%d", count); + GetDlgItem(IDC_TIMER)->SetWindowText(buffer); + + CDialog::OnTimer(nIDEvent); +} diff --git a/src/win32/ModeConfirm.h b/src/win32/ModeConfirm.h new file mode 100644 index 00000000..0e5a1838 --- /dev/null +++ b/src/win32/ModeConfirm.h @@ -0,0 +1,71 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_) +#define AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ModeConfirm.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ModeConfirm dialog + +class ModeConfirm : public CDialog +{ + // Construction + public: + int count; + UINT timer; + ModeConfirm(CWnd* pParent); // standard constructor + + // Dialog Data + //{{AFX_DATA(ModeConfirm) + enum { IDD = IDD_MODE_CONFIRM }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ModeConfirm) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(ModeConfirm) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnDestroy(); + virtual BOOL OnInitDialog(); + afx_msg void OnTimer(UINT nIDEvent); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_) diff --git a/src/win32/OamView.cpp b/src/win32/OamView.cpp new file mode 100644 index 00000000..13c233ac --- /dev/null +++ b/src/win32/OamView.cpp @@ -0,0 +1,669 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// OamView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "OamView.h" +#include "Reg.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../NLS.h" +#include "../Util.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// OamView dialog + + +OamView::OamView(CWnd* pParent /*=NULL*/) + : ResizeDlg(OamView::IDD, pParent) +{ + //{{AFX_DATA_INIT(OamView) + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32; + bmpInfo.bmiHeader.biHeight = 32; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 64 * 64); + + oamView.setData(data); + oamView.setBmpInfo(&bmpInfo); + + number = 0; +} + + +void OamView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(OamView) + DDX_Control(pDX, IDC_SPRITE, m_sprite); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_COLOR, color); + DDX_Control(pDX, IDC_OAM_VIEW, oamView); + DDX_Control(pDX, IDC_OAM_VIEW_ZOOM, oamZoom); +} + + +BEGIN_MESSAGE_MAP(OamView, CDialog) + //{{AFX_MSG_MAP(OamView) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_EN_CHANGE(IDC_SPRITE, OnChangeSprite) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_WM_HSCROLL() + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// OamView message handlers + +OamView::~OamView() +{ + free(data); + data = NULL; +} + +void OamView::paint() +{ + if(oam == NULL || paletteRAM == NULL || vram == NULL) + return; + + render(); + oamView.setSize(w,h); + oamView.refresh(); +} + +void OamView::update() +{ + paint(); +} + + + +void OamView::setAttributes(u16 a0, u16 a1, u16 a2) +{ + CString buffer; + + int y = a0 & 255; + int rot = a0 & 512; + int mode = (a0 >> 10) & 3; + int mosaic = a0 & 4096; + int color = a0 & 8192; + int duple = a0 & 1024; + int shape = (a0 >> 14) & 3; + int x = a1 & 511; + int rotParam = (a1 >> 9) & 31; + int flipH = a1 & 4096; + int flipV = a1 & 8192; + int size = (a1 >> 14) & 3; + int tile = a2 & 1023; + int prio = (a2 >> 10) & 3; + int pal = (a2 >> 12) & 15; + + buffer.Format("%d,%d", x,y); + GetDlgItem(IDC_POS)->SetWindowText(buffer); + + buffer.Format("%d", mode); + GetDlgItem(IDC_MODE)->SetWindowText(buffer); + + GetDlgItem(IDC_COLORS)->SetWindowText(color ? "256" : "16"); + + buffer.Format("%d", pal); + GetDlgItem(IDC_PALETTE)->SetWindowText(buffer); + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE)->SetWindowText(buffer); + + buffer.Format("%d", prio); + GetDlgItem(IDC_PRIO)->SetWindowText(buffer); + + buffer.Format("%d,%d", w,h); + GetDlgItem(IDC_SIZE2)->SetWindowText(buffer); + + if(rot) { + buffer.Format("%d", rotParam); + } else + buffer.Empty(); + GetDlgItem(IDC_ROT)->SetWindowText(buffer); + + buffer.Empty(); + + if(rot) + buffer += 'R'; + else buffer += ' '; + if(!rot) { + if(flipH) + buffer += 'H'; + else + buffer += ' '; + if(flipV) + buffer += 'V'; + else + buffer += ' '; + } else { + buffer += ' '; + buffer += ' '; + } + if(mosaic) + buffer += 'M'; + else + buffer += ' '; + if(duple) + buffer += 'D'; + else + buffer += ' '; + + GetDlgItem(IDC_FLAGS)->SetWindowText(buffer); +} + +void OamView::render() +{ + int m=0; + if(oam == NULL || paletteRAM == NULL || vram == NULL) + return; + + u16 *sprites = &((u16 *)oam)[4*number]; + u16 *spritePalette = &((u16 *)paletteRAM)[0x100]; + u8 *bmp = data; + + u16 a0 = *sprites++; + u16 a1 = *sprites++; + u16 a2 = *sprites++; + + int sizeY = 8; + int sizeX = 8; + + switch(((a0 >>12) & 0x0c)|(a1>>14)) { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + return; + } + + w = sizeX; + h = sizeY; + + setAttributes(a0,a1,a2); + + int sy = (a0 & 255); + + if(a0 & 0x2000) { + int c = (a2 & 0x3FF); + // if((DISPCNT & 7) > 2 && (c < 512)) + // return; + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u32 color = vram[0x10000 + (((c + (y>>3) * inc)* + 32 + (y & 7) * 8 + (x >> 3) * 64 + + (x & 7))&0x7FFF)]; + color = spritePalette[color]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } + } else { + int c = (a2 & 0x3FF); + // if((DISPCNT & 7) > 2 && (c < 512)) + // continue; + + int inc = 32; + if(DISPCNT & 0x40) + inc = sizeX >> 3; + int palette = (a2 >> 8) & 0xF0; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + u32 color = vram[0x10000 + (((c + (y>>3) * inc)* + 32 + (y & 7) * 4 + (x >> 3) * 32 + + ((x & 7)>>1))&0x7FFF)]; + if(x & 1) + color >>= 4; + else + color &= 0x0F; + + color = spritePalette[palette+color]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } + } +} + +void OamView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + + +void OamView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void OamView::OnSave() +{ + CString captureBuffer; + + if(theApp.captureFormat == 0) + captureBuffer = "oam.png"; + else + captureBuffer = "oam.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + captureBuffer = dlg.GetPathName(); + + if(dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +BOOL OamView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_OAM_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_OAM_VIEW_ZOOM, DS_MoveX) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\OamView", + NULL); + m_sprite.SetWindowText("0"); + + updateScrollInfo(); + + m_stretch = regQueryDwordValue("oamViewStretch", 0); + if(m_stretch) + oamView.setStretch(true); + UpdateData(FALSE); + + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void OamView::OnStretch() +{ + oamView.setStretch(!oamView.getStretch()); + paint(); + regSetDwordValue("oamViewStretch", oamView.getStretch()); +} + + +void OamView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void OamView::OnChangeSprite() +{ + CString buffer; + m_sprite.GetWindowText(buffer); + int n = atoi(buffer); + if(n < 0 || n > 127) { + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + return; + } + number = n; + paint(); + updateScrollInfo(); +} + +void OamView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT OamView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + oamZoom.setColors(colors); + + return TRUE; +} + +LRESULT OamView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void OamView::updateScrollInfo() +{ + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + si.nMax = 127; + si.nPage = 1; + si.nPos = number; + GetDlgItem(IDC_SCROLLBAR)->SetScrollInfo(SB_CTL, + &si, + TRUE); +} + +void OamView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + switch(nSBCode) { + case SB_BOTTOM: + number = 127; + break; + case SB_LINEDOWN: + number++; + if(number > 127) + number = 127; + break; + case SB_LINEUP: + number--; + if(number < 0) + number = 0; + break; + case SB_PAGEDOWN: + number += 16; + if(number > 127) + number = 127; + break; + case SB_PAGEUP: + number -= 16; + if(number < 0) + number = 0; + break; + case SB_TOP: + number = 0; + break; + case SB_THUMBTRACK: + number = nPos; + if(number < 0) + number = 0; + if(number > 127) + number = 127; + break; + } + + updateScrollInfo(); + + CString buffer; + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + paint(); +} + +void OamView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/OamView.h b/src/win32/OamView.h new file mode 100644 index 00000000..dbe770be --- /dev/null +++ b/src/win32/OamView.h @@ -0,0 +1,100 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_) +#define AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// OamView.h : header file +// +#include "BitmapControl.h" +#include "ZoomControl.h" +#include "ColorControl.h" + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// OamView dialog + +class OamView : public ResizeDlg, IUpdateListener +{ + private: + BITMAPINFO bmpInfo; + u8 *data; + int w; + int h; + int number; + bool autoUpdate; + BitmapControl oamView; + ZoomControl oamZoom; + ColorControl color; + + // Construction + public: + void updateScrollInfo(); + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + void savePNG(const char *name); + void saveBMP(const char *name); + void render(); + void setAttributes(u16 a0, u16 a1, u16 a2); + void paint(); + ~OamView(); + OamView(CWnd* pParent = NULL); // standard constructor + + virtual void update(); + // Dialog Data + //{{AFX_DATA(OamView) + enum { IDD = IDD_OAM_VIEW }; + CEdit m_sprite; + BOOL m_stretch; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(OamView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(OamView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnChangeSprite(); + afx_msg void OnClose(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_) diff --git a/src/win32/OpenGL.cpp b/src/win32/OpenGL.cpp new file mode 100644 index 00000000..e64b302d --- /dev/null +++ b/src/win32/OpenGL.cpp @@ -0,0 +1,552 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" +#include "MainWnd.h" +#include + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../Text.h" +#include "../Util.h" + +#include "Reg.h" +#include "..\..\res\resource.h" + +#include "../gbafilter.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +#ifdef MMX +extern "C" bool cpu_mmx; + +extern bool detectMMX(); +#endif + +extern int Init_2xSaI(u32); +extern void winlog(const char *,...); +extern int systemSpeed; + +class OpenGLDisplay : public IDisplay { +private: + HDC hDC; + HGLRC hglrc; + GLuint texture; + int width; + int height; + float size; + u8 *filterData; + bool failed; + + bool initializeTexture(int w, int h); + void updateFiltering(int); +public: + OpenGLDisplay(); + virtual ~OpenGLDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual void resize(int w, int h); + virtual DISPLAY_TYPE getType() { return OPENGL; }; + virtual void setOption(const char *, int); + virtual int selectFullScreenMode(GUID **); +}; + +OpenGLDisplay::OpenGLDisplay() +{ + hDC = NULL; + hglrc = NULL; + texture = 0; + width = 0; + height = 0; + size = 0.0f; + filterData = (u8 *)malloc(4*4*4*256*240); + failed = false; +} + +OpenGLDisplay::~OpenGLDisplay() +{ + cleanup(); +} + +void OpenGLDisplay::cleanup() +{ + if(texture != 0) { + glDeleteTextures(1, &texture); + texture = 0; + } + if(hglrc != NULL) { + wglDeleteContext(hglrc); + wglMakeCurrent(NULL, NULL); + hglrc = NULL; + } + if(hDC != NULL) { + ReleaseDC(*theApp.m_pMainWnd, hDC); + hDC = NULL; + } + if(filterData) { + free(filterData); + filterData = NULL; + } + width = 0; + height = 0; + size = 0.0f; +} + +bool OpenGLDisplay::initialize() +{ + theApp.sizeX = 240; + theApp.sizeY = 160; + + switch(theApp.videoOption) { + case VIDEO_1X: + theApp.surfaceSizeX = theApp.sizeX; + theApp.surfaceSizeY = theApp.sizeY; + break; + case VIDEO_2X: + theApp.surfaceSizeX = theApp.sizeX * 2; + theApp.surfaceSizeY = theApp.sizeY * 2; + break; + case VIDEO_3X: + theApp.surfaceSizeX = theApp.sizeX * 3; + theApp.surfaceSizeY = theApp.sizeY * 3; + break; + case VIDEO_4X: + theApp.surfaceSizeX = theApp.sizeX * 4; + theApp.surfaceSizeY = theApp.sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + { + RECT r; + ::GetWindowRect(GetDesktopWindow(), &r); + theApp.fsWidth = r.right - r.left; + theApp.fsHeight = r.bottom - r.top; + + /* Need to fix this code later. For now, Fullscreen takes the whole + screen. + int scaleX = (fsWidth / sizeX); + int scaleY = (fsHeight / sizeY); + int min = scaleX < scaleY ? scaleX : scaleY; + surfaceSizeX = sizeX * min; + surfaceSizeY = sizeY * min; + if(fullScreenStretch) { + */ + theApp.surfaceSizeX = theApp.fsWidth; + theApp.surfaceSizeY = theApp.fsHeight; + // } + } + break; + } + + theApp.rect.left = 0; + theApp.rect.top = 0; + theApp.rect.right = theApp.sizeX; + theApp.rect.bottom = theApp.sizeY; + + theApp.dest.left = 0; + theApp.dest.top = 0; + theApp.dest.right = theApp.surfaceSizeX; + theApp.dest.bottom = theApp.surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + style |= WS_OVERLAPPEDWINDOW; + else + styleEx = 0; + + if(theApp.videoOption <= VIDEO_4X) + AdjustWindowRectEx(&theApp.dest, style, TRUE, styleEx); + else + AdjustWindowRectEx(&theApp.dest, style, FALSE, styleEx); + + int winSizeX = theApp.dest.right-theApp.dest.left; + int winSizeY = theApp.dest.bottom-theApp.dest.top; + + if(theApp.videoOption > VIDEO_4X) { + winSizeX = theApp.fsWidth; + winSizeY = theApp.fsHeight; + } + + int x = 0; + int y = 0; + + if(theApp.videoOption <= VIDEO_4X) { + x = theApp.windowPositionX; + y = theApp.windowPositionY; + } + + // Create a window + MainWnd *pWnd = new MainWnd; + theApp.m_pMainWnd = pWnd; + + pWnd->CreateEx(styleEx, + theApp.wndClass, + "VisualBoyAdvance", + style, + x,y,winSizeX,winSizeY, + NULL, + 0); + + if (!(HWND)*pWnd) { + winlog("Error creating Window %08x\n", GetLastError()); + return FALSE; + } + + theApp.updateMenuBar(); + + theApp.adjustDestRect(); + + theApp.mode320Available = FALSE; + theApp.mode640Available = FALSE; + theApp.mode800Available = FALSE; + + CDC *dc = pWnd->GetDC(); + HDC hDC = dc->GetSafeHdc(); + + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd + 1, // version number + PFD_DRAW_TO_WINDOW | // support window + PFD_SUPPORT_OPENGL | // support OpenGL + PFD_DOUBLEBUFFER, // double buffered + PFD_TYPE_RGBA, // RGBA type + 16, // 16-bit color depth + 0, 0, 0, 0, 0, 0, // color bits ignored + 0, // no alpha buffer + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + 32, // 32-bit z-buffer + 0, // no stencil buffer + 0, // no auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + int iPixelFormat; + + if(!(iPixelFormat = ChoosePixelFormat(hDC, &pfd))) { + winlog("Failed ChoosePixelFormat\n"); + return false; + } + + // obtain detailed information about + // the device context's first pixel format + if(!(DescribePixelFormat(hDC, iPixelFormat, + sizeof(PIXELFORMATDESCRIPTOR), &pfd))) { + winlog("Failed DescribePixelFormat\n"); + return false; + } + + if(!SetPixelFormat(hDC, iPixelFormat, &pfd)) { + winlog("Failed SetPixelFormat\n"); + return false; + } + + if(!(hglrc = wglCreateContext(hDC))) { + winlog("Failed wglCreateContext\n"); + return false; + } + + if(!wglMakeCurrent(hDC, hglrc)) { + winlog("Failed wglMakeCurrent\n"); + return false; + } + pWnd->ReleaseDC(dc); + + // setup 2D gl environment + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_TEXTURE_2D); + + glViewport(0, 0, theApp.surfaceSizeX, theApp.surfaceSizeY); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, (GLdouble)(theApp.surfaceSizeX), (GLdouble)(theApp.surfaceSizeY), + 0.0, 0.0,1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + systemRedShift = 3; + systemGreenShift = 11; + systemBlueShift = 19; + systemColorDepth = 32; + theApp.fsColorDepth = 32; + + Init_2xSaI(32); +#ifdef MMX + if(!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + if(theApp.ddrawDebug) { + winlog("R shift: %d\n", systemRedShift); + winlog("G shift: %d\n", systemGreenShift); + winlog("B shift: %d\n", systemBlueShift); + } + + utilUpdateSystemColorMaps(theApp.filterLCD); + theApp.updateFilter(); + theApp.updateIFB(); + + if(failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + +void OpenGLDisplay::clear() +{ +} + +void OpenGLDisplay::renderMenu() +{ + checkFullScreen(); + if(theApp.m_pMainWnd) + theApp.m_pMainWnd->DrawMenuBar(); +} + +void OpenGLDisplay::checkFullScreen() +{ + // if(tripleBuffering) + // pOpenGL->FlipToGDISurface(); +} + +void OpenGLDisplay::render() +{ + int pitch = theApp.filterWidth * 4 + 4; + u8 *data = pix + (theApp.sizeX+1)*4; + + if(theApp.filterFunction) { + data = filterData; + theApp.filterFunction(pix+pitch, + pitch, + (u8*)theApp.delta, + (u8*)filterData, + theApp.rect.right * (systemColorDepth / 8), + theApp.filterWidth, + theApp.filterHeight); + } + + if(theApp.videoOption > VIDEO_4X && theApp.showSpeed) { + char buffer[30]; + if(theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + + if(theApp.filterFunction) { + int p = theApp.rect.right * 2; + if(systemColorDepth == 24) + p = theApp.rect.right * 3; + else if(systemColorDepth == 32) + p = theApp.rect.right * 4; + if(theApp.showSpeedTransparent) + drawTextTransp((u8*)filterData, + p, + 10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8*)filterData, + p, + 10, + theApp.rect.bottom-10, + buffer); + } else { + if(theApp.showSpeedTransparent) + drawTextTransp((u8*)pix, + pitch, + 10, + theApp.filterHeight-10, + buffer); + else + drawText((u8*)pix, + pitch, + 10, + theApp.filterHeight-10, + buffer); + } + } + + // Texturemap complete texture to surface so we have free scaling + // and antialiasing + if(theApp.filterFunction) { + glPixelStorei(GL_UNPACK_ROW_LENGTH, theApp.rect.right); + } else { + glPixelStorei(GL_UNPACK_ROW_LENGTH, theApp.sizeX+1); + } + + glTexSubImage2D( GL_TEXTURE_2D,0, + 0,0, theApp.rect.right,theApp.rect.bottom, + GL_RGBA,GL_UNSIGNED_BYTE,data); + + if(theApp.glType == 0) { + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.0, 0.0); glVertex3i(0, 0, 0); + glTexCoord2f(theApp.rect.right/size, 0.0); glVertex3i(theApp.surfaceSizeX, 0, 0); + glTexCoord2f(0.0, theApp.rect.bottom/size); glVertex3i(0, theApp.surfaceSizeY, 0); + glTexCoord2f(theApp.rect.right/size, theApp.rect.bottom/size); glVertex3i(theApp.surfaceSizeX, theApp.surfaceSizeY, 0); + glEnd(); + } else { + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); glVertex3i(0, 0, 0); + glTexCoord2f(theApp.rect.right/size, 0.0); glVertex3i(theApp.surfaceSizeX, 0, 0); + glTexCoord2f(theApp.rect.right/size, theApp.rect.bottom/size); glVertex3i(theApp.surfaceSizeX, theApp.surfaceSizeY, 0); + glTexCoord2f(0.0, theApp.rect.bottom/size); glVertex3i(0, theApp.surfaceSizeY, 0); + glEnd(); + } + + CDC *dc = theApp.m_pMainWnd->GetDC(); + + if(theApp.screenMessage) { + if(((GetTickCount() - theApp.screenMessageTime) < 3000) && + !theApp.disableStatusMessage) { + dc->SetTextColor(RGB(255,0,0)); + dc->SetBkMode(TRANSPARENT); + dc->TextOut(10, theApp.surfaceSizeY - 20, theApp.screenMessageBuffer); + } else { + theApp.screenMessage = false; + } + } + + SwapBuffers(dc->GetSafeHdc()); + + theApp.m_pMainWnd->ReleaseDC(dc); +} + +void OpenGLDisplay::resize(int w, int h) +{ + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, (GLdouble)(w), (GLdouble)(h), + 0.0, 0.0,1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void OpenGLDisplay::updateFiltering(int value) +{ + switch(value) { + case 0: + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + break; + case 1: + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + break; + } +} + +bool OpenGLDisplay::initializeTexture(int w, int h) +{ + int mySize = 256; + size = 256.0f; + if(w > 255 || h > 255) { + size = 512.0f; + mySize = 512; + } + if(w > 512 || h > 512) { + size = 1024.0f; + mySize = 1024; + } + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + int filter = regQueryDwordValue("glFilter", 0); + if(filter < 0 || filter > 1) + filter = 0; + updateFiltering(filter); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mySize, mySize, 0, GL_RGBA, + GL_UNSIGNED_BYTE, NULL ); + width = w; + height = h; + + return true; +} + +bool OpenGLDisplay::changeRenderSize(int w, int h) +{ + if(width != w || height != h) { + if(texture != 0) { + glDeleteTextures(1, &texture); + texture = 0; + } + if(!initializeTexture(w, h)) { + failed = true; + return false; + } + } + return true; +} + +void OpenGLDisplay::setOption(const char *option, int value) +{ + if(!strcmp(option, "glFilter")) + updateFiltering(value); +} + +int OpenGLDisplay::selectFullScreenMode(GUID **) +{ + HWND wnd = GetDesktopWindow(); + RECT r; + GetWindowRect(wnd, &r); + int w = (r.right - r.left) & 4095; + int h = (r.bottom - r.top) & 4095; + HDC dc = GetDC(wnd); + int c = GetDeviceCaps(dc, BITSPIXEL); + ReleaseDC(wnd, dc); + + return (c << 24) | (w << 12) | h; +} + +IDisplay *newOpenGLDisplay() +{ + return new OpenGLDisplay(); +} + diff --git a/src/win32/PaletteView.cpp b/src/win32/PaletteView.cpp new file mode 100644 index 00000000..bd4e72f0 --- /dev/null +++ b/src/win32/PaletteView.cpp @@ -0,0 +1,242 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// PaletteView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "PaletteView.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +void GBAPaletteViewControl::updatePalette() +{ + if(paletteRAM != NULL) + memcpy(palette, &paletteRAM[paletteAddress], 512); +} + +///////////////////////////////////////////////////////////////////////////// +// PaletteView dialog + + +PaletteView::PaletteView(CWnd* pParent /*=NULL*/) + : ResizeDlg(PaletteView::IDD, pParent) +{ + //{{AFX_DATA_INIT(PaletteView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; +} + +PaletteView::~PaletteView() +{ +} + + +void PaletteView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(PaletteView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_PALETTE_VIEW, paletteView); + DDX_Control(pDX, IDC_PALETTE_VIEW_OBJ, paletteViewOBJ); + DDX_Control(pDX, IDC_COLOR, colorControl); +} + + +BEGIN_MESSAGE_MAP(PaletteView, CDialog) + //{{AFX_MSG_MAP(PaletteView) + ON_BN_CLICKED(IDC_SAVE_BG, OnSaveBg) + ON_BN_CLICKED(IDC_SAVE_OBJ, OnSaveObj) + ON_BN_CLICKED(IDC_REFRESH2, OnRefresh2) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + //}}AFX_MSG_MAP + ON_MESSAGE(WM_PALINFO, OnPalInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// PaletteView message handlers + +BOOL PaletteView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_END() + SetData(sz, + FALSE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\PaletteView", + NULL); + + paletteView.setPaletteAddress(0); + paletteView.refresh(); + + paletteViewOBJ.setPaletteAddress(0x200); + paletteViewOBJ.refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void PaletteView::save(int which) +{ + CString captureBuffer; + + if(which == 0) + captureBuffer = "bg.pal"; + else + captureBuffer = "obj.pal"; + + LPCTSTR exts[] = {".pal", ".pal", ".act" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PAL); + CString title = winResLoadString(IDS_SELECT_PALETTE_NAME); + FileDlg dlg(this, + captureBuffer, + filter, + 1, + "PAL", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = dlg.GetPathName(); + + PaletteViewControl *p = NULL; + + if(which == 0) + p = &paletteView; + else + p = &paletteViewOBJ; + + switch(dlg.getFilterIndex()) { + case 0: + case 1: + p->saveMSPAL(captureBuffer); + break; + case 2: + p->saveJASCPAL(captureBuffer); + break; + case 3: + p->saveAdobe(captureBuffer); + break; + } +} + +void PaletteView::OnSaveBg() +{ + save(0); +} + +void PaletteView::OnSaveObj() +{ + save(1); +} + +void PaletteView::OnRefresh2() +{ + paletteView.refresh(); + paletteViewOBJ.refresh(); +} + +void PaletteView::update() +{ + OnRefresh2(); +} + + +void PaletteView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + +void PaletteView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT PaletteView::OnPalInfo(WPARAM wParam, LPARAM lParam) +{ + u16 color = (u16)wParam; + u32 address = (u32)lParam; + CString buffer; + + if(address >= 0x200) + address = 0x5000200 + 2*(address & 255); + else + address = 0x5000000 + 2*(address & 255); + + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int r = (color & 0x1f); + int g = (color & 0x3e0) >> 5; + int b = (color & 0x7c00) >> 10; + + buffer.Format("%d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("%d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("%d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + buffer.Format("0x%04X", color); + GetDlgItem(IDC_VALUE)->SetWindowText(buffer); + + colorControl.setColor(color); + + if(address >= 0x5000200) { + paletteView.setSelected(-1); + } else + paletteViewOBJ.setSelected(-1); + + return TRUE; +} + +void PaletteView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/PaletteView.h b/src/win32/PaletteView.h new file mode 100644 index 00000000..aee3a4cb --- /dev/null +++ b/src/win32/PaletteView.h @@ -0,0 +1,90 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_) +#define AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// PaletteView.h : header file +// + +#include "ColorControl.h" +#include "PaletteViewControl.h" +#include "ResizeDlg.h" + +class GBAPaletteViewControl : public PaletteViewControl { + public: + virtual void updatePalette(); +}; + +///////////////////////////////////////////////////////////////////////////// +// PaletteView dialog + +class PaletteView : public ResizeDlg, IUpdateListener +{ + private: + GBAPaletteViewControl paletteView; + GBAPaletteViewControl paletteViewOBJ; + ColorControl colorControl; + bool autoUpdate; + // Construction + public: + void save(int which); + PaletteView(CWnd* pParent = NULL); // standard constructor + ~PaletteView(); + afx_msg LRESULT OnPalInfo(WPARAM wParam, LPARAM lParam); + + // Dialog Data + //{{AFX_DATA(PaletteView) + enum { IDD = IDD_PALETTE_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(PaletteView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + virtual void update(); + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(PaletteView) + virtual BOOL OnInitDialog(); + afx_msg void OnSaveBg(); + afx_msg void OnSaveObj(); + afx_msg void OnRefresh2(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_) diff --git a/src/win32/PaletteViewControl.cpp b/src/win32/PaletteViewControl.cpp new file mode 100644 index 00000000..a769d565 --- /dev/null +++ b/src/win32/PaletteViewControl.cpp @@ -0,0 +1,409 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// PaletteViewControl.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "PaletteViewControl.h" + +#include "../Util.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +bool PaletteViewControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl + +PaletteViewControl::PaletteViewControl() +{ + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 256; + bmpInfo.bmiHeader.biHeight = -256; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)malloc(3 * 256 * 256); + + w = 256; + h = 256; + + colors = 256; + + paletteAddress = 0; + + ZeroMemory(palette, 512); + + selected = -1; + registerClass(); +} + +PaletteViewControl::~PaletteViewControl() +{ + if(data) + free(data); +} + + +BEGIN_MESSAGE_MAP(PaletteViewControl, CWnd) + //{{AFX_MSG_MAP(PaletteViewControl) + ON_WM_LBUTTONDOWN() + ON_WM_ERASEBKGND() + ON_WM_PAINT() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl message handlers + +void PaletteViewControl::init(int c, int w, int h) +{ + this->w = w; + this->h = h; + this->colors = c; + + bmpInfo.bmiHeader.biWidth = w; + bmpInfo.bmiHeader.biHeight = -h; +} + + +bool PaletteViewControl::saveAdobe(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + u8 data[3] = { r, g, b }; + fwrite(data, 1, 3, f); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) { + u8 data[3] = { 0, 0, 0 }; + fwrite(data, 1, 3, f); + } + } + fclose(f); + + return true; +} + + +bool PaletteViewControl::saveMSPAL(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + u8 data[4] = { 'R', 'I', 'F', 'F' }; + + fwrite(data, 1, 4, f); + utilPutDword(data, 256 * 4 + 16); + fwrite(data, 1, 4, f); + u8 data3[4] = { 'P', 'A', 'L', ' ' }; + fwrite(data3, 1, 4, f); + u8 data4[4] = { 'd', 'a', 't', 'a' }; + fwrite(data4, 1, 4, f); + utilPutDword(data, 256*4+4); + fwrite(data, 1, 4, f); + utilPutWord(&data[0], 0x0300); + utilPutWord(&data[2], 256); // causes problems if not 16 or 256 + fwrite(data, 1, 4, f); + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + u8 data7[4] = { r, g, b, 0 }; + fwrite(data7, 1, 4, f); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) { + u8 data7[4] = { 0, 0, 0, 0 }; + fwrite(data7, 1, 4, f); + } + } + fclose(f); + + return true; +} + + +bool PaletteViewControl::saveJASCPAL(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + fprintf(f, "JASC-PAL\r\n0100\r\n256\r\n"); + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + fprintf(f, "%d %d %d\r\n", r, g, b); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) + fprintf(f, "0 0 0\r\n"); + } + fclose(f); + + return true; +} + +void PaletteViewControl::setPaletteAddress(int address) +{ + paletteAddress = address; +} + + +void PaletteViewControl::setSelected(int s) +{ + selected = s; + InvalidateRect(NULL, FALSE); +} + + +void PaletteViewControl::render(u16 color, int x, int y) +{ + u8 *start = data + y*16*w*3 + x*16*3; + int skip = w*3-16*3; + + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + for(int i = 0; i < 16; i++) { + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + start += skip; + } +} + +void PaletteViewControl::refresh() +{ + updatePalette(); + int sw = w/16; + int sh = h/16; + for(int i = 0; i < colors; i++) { + render(palette[i], i & (sw-1), i/sw); + } + InvalidateRect(NULL, FALSE); +} + +void PaletteViewControl::OnLButtonDown(UINT nFlags, CPoint point) +{ + int x = point.x; + int y = point.y; + RECT rect; + GetClientRect(&rect); + int h = rect.bottom - rect.top; + int w = rect.right - rect.left; + int sw = (this->w/16); + int sh = (this->h/16); + int mult = w / sw; + int multY = h / sh; + + setSelected(x/mult + (y/multY)*sw); + + GetParent()->SendMessage(WM_PALINFO, + palette[x/mult+(y/multY)*sw], + paletteAddress+(x/mult+(y/multY)*sw)); +} + +BOOL PaletteViewControl::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + + +void PaletteViewControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + int w = rect.right - rect.left; + int h = rect.bottom - rect.top; + + CDC memDC; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, h); + pOldBitmap = memDC.SelectObject(&bitmap); + + StretchDIBits(memDC.GetSafeHdc(), + 0, + 0, + w, + h, + 0, + 0, + this->w, + this->h, + data, + &bmpInfo, + DIB_RGB_COLORS, + SRCCOPY); + int sw = this->w / 16; + int sh = this->h / 16; + int mult = w / sw; + int multY = h / sh; + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(192,192,192)); + CPen *old = memDC.SelectObject(&pen); + int i; + for(i = 1; i < sh; i++) { + memDC.MoveTo(0, i * multY); + memDC.LineTo(w, i * multY); + } + for(i = 1; i < sw; i++) { + memDC.MoveTo(i * mult, 0); + memDC.LineTo(i * mult, h); + } + memDC.DrawEdge(&rect, EDGE_SUNKEN, BF_RECT); + memDC.SelectObject(old); + pen.DeleteObject(); + + if(selected != -1) { + pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); + old = memDC.SelectObject(&pen); + + int startX = (selected & (sw-1))*mult+1; + int startY = (selected / sw)*multY+1; + int endX = startX + mult-2; + int endY = startY + multY-2; + + memDC.MoveTo(startX, startY); + memDC.LineTo(endX, startY); + memDC.LineTo(endX, endY); + memDC.LineTo(startX, endY); + memDC.LineTo(startX, startY-1); + + memDC.SelectObject(old); + pen.DeleteObject(); + } + + dc.BitBlt(0,0,w,h, + &memDC,0,0,SRCCOPY); + + memDC.SelectObject(pOldBitmap); + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +void PaletteViewControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaPaletteViewControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff --git a/src/win32/PaletteViewControl.h b/src/win32/PaletteViewControl.h new file mode 100644 index 00000000..a7c89228 --- /dev/null +++ b/src/win32/PaletteViewControl.h @@ -0,0 +1,90 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_) +#define AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// PaletteViewControl.h : header file +// +#define WM_PALINFO WM_APP+1 + +///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl window + +class PaletteViewControl : public CWnd +{ + int w; + int h; + int colors; + u8 *data; + BITMAPINFO bmpInfo; + static bool isRegistered; + int selected; + protected: + u16 palette[256]; + int paletteAddress; + // Construction + public: + PaletteViewControl(); + + virtual void updatePalette()=0; + + // Attributes + public: + + // Operations + public: + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(PaletteViewControl) + //}}AFX_VIRTUAL + + // Implementation + public: + void registerClass(); + void refresh(); + void render(u16 color, int x, int y); + void setSelected(int s); + void setPaletteAddress(int address); + bool saveJASCPAL(const char *name); + bool saveMSPAL(const char *name); + bool saveAdobe(const char *name); + void init(int c, int w, int h); + virtual ~PaletteViewControl(); + + // Generated message map functions + protected: + //{{AFX_MSG(PaletteViewControl) + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + afx_msg void OnPaint(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_) diff --git a/src/win32/Reg.cpp b/src/win32/Reg.cpp new file mode 100644 index 00000000..ac931c0a --- /dev/null +++ b/src/win32/Reg.cpp @@ -0,0 +1,369 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" + +static char buffer[2048]; +static HKEY vbKey = NULL; +static CString regVbaPath; + +#define VBA_PREF "preferences" + +bool regEnabled = true; + +void regInit(const char *path) +{ + DWORD disp = 0; + LONG res = RegCreateKeyEx(HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance", + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &vbKey, + &disp); + regVbaPath.Format("%s\\vba.ini", path); +} + +void regShutdown() +{ + LONG res = RegCloseKey(vbKey); +} + +const char *regGetINIPath() +{ + return regVbaPath; +} + +char *regQueryStringValue(const char * key, char *def) +{ + if(regEnabled) { + DWORD type = 0; + DWORD size = 2048; + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)buffer, + &size); + + if(res == ERROR_SUCCESS && type == REG_SZ) + return buffer; + + return def; + } + + DWORD res = GetPrivateProfileString(VBA_PREF, + key, + def, + (LPTSTR)buffer, + 2048, + regVbaPath); + + if(res) + return buffer; + + return def; +} + +DWORD regQueryDwordValue(const char * key, DWORD def, bool force) +{ + if(regEnabled || force) { + DWORD type = 0; + DWORD size = sizeof(DWORD); + DWORD result = 0; + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)&result, + &size); + + if(res == ERROR_SUCCESS && type == REG_DWORD) + return result; + + return def; + } + + return GetPrivateProfileInt(VBA_PREF, + key, + def, + regVbaPath); +} + +BOOL regQueryBinaryValue(const char * key, char *value, int count) +{ + if(regEnabled) { + DWORD type = 0; + DWORD size = count; + DWORD result = 0; + + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)value, + &size); + + if(res == ERROR_SUCCESS && type == REG_BINARY) + return TRUE; + + return FALSE; + } + CString k = key; + k += "Count"; + int size = GetPrivateProfileInt(VBA_PREF, + k, + -1, + regVbaPath); + if(size >= 0 && size < count) + count = size; + return GetPrivateProfileStruct(VBA_PREF, + key, + value, + count, + regVbaPath); +} + +void regSetStringValue(const char * key, const char * value) +{ + if(regEnabled) { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_SZ, + (const UCHAR *)value, + strlen(value)+1); + } else { + WritePrivateProfileString(VBA_PREF, + key, + value, + regVbaPath); + } +} + +void regSetDwordValue(const char * key, DWORD value, bool force) +{ + if(regEnabled || force) { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_DWORD, + (const UCHAR *)&value, + sizeof(DWORD)); + } else { + wsprintf(buffer, "%u", value); + WritePrivateProfileString(VBA_PREF, + key, + buffer, + regVbaPath); + } +} + +void regSetBinaryValue(const char *key, char *value, int count) +{ + if(regEnabled) { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_BINARY, + (const UCHAR *)value, + count); + } else { + CString k = key; + k += "Count"; + wsprintf(buffer, "%u", count); + + WritePrivateProfileString(VBA_PREF, + k, + buffer, + regVbaPath); + + WritePrivateProfileStruct(VBA_PREF, + key, + value, + count, + regVbaPath); + } +} + +void regDeleteValue(char *key) +{ + if(regEnabled) { + LONG res = RegDeleteValue(vbKey, + key); + } else { + WritePrivateProfileString(VBA_PREF, + key, + NULL, + regVbaPath); + } +} + +bool regCreateFileType(const char *ext, const char *type) +{ + DWORD disp = 0; + HKEY key; + LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, + ext, + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key, + &disp); + if(res == ERROR_SUCCESS) { + res = RegSetValueEx(key, + "", + 0, + REG_SZ, + (const UCHAR *)type, + strlen(type)+1); + RegCloseKey(key); + return true; + } + return false; +} + +bool regAssociateType(const char *type, const char *desc, const char *application) +{ + DWORD disp = 0; + HKEY key; + LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, + type, + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key, + &disp); + if(res == ERROR_SUCCESS) { + res = RegSetValueEx(key, + "", + 0, + REG_SZ, + (const UCHAR *)desc, + strlen(desc)+1); + HKEY key2; + res = RegCreateKeyEx(key, + "Shell\\Open\\Command", + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key2, + &disp); + if(res == ERROR_SUCCESS) { + res = RegSetValueEx(key2, + "", + 0, + REG_SZ, + (const UCHAR *)application, + strlen(application)+1); + RegCloseKey(key2); + RegCloseKey(key); + return true; + } + + RegCloseKey(key); + } + return false; +} + +static void regExportSettingsToINI(HKEY key, const char *section) +{ + char valueName[256]; + int index = 0; + while(1) { + DWORD nameSize = 256; + DWORD size = 2048; + DWORD type; + LONG res = RegEnumValue(key, + index, + valueName, + &nameSize, + NULL, + &type, + (LPBYTE)buffer, + &size); + + if(res == ERROR_SUCCESS) { + switch(type) { + case REG_DWORD: + { + char temp[256]; + wsprintf(temp, "%u", *((DWORD *)buffer)); + WritePrivateProfileString(section, + valueName, + temp, + regVbaPath); + } + break; + case REG_SZ: + WritePrivateProfileString(section, + valueName, + buffer, + regVbaPath); + break; + case REG_BINARY: + { + char temp[256]; + + wsprintf(temp, "%u", size); + CString k = valueName; + k += "Count"; + WritePrivateProfileString(section, + k, + temp, + regVbaPath); + WritePrivateProfileStruct(section, + valueName, + buffer, + size, + regVbaPath); + } + break; + } + index++; + } else + break; + } +} + +void regExportSettingsToINI() +{ + if(vbKey != NULL) { + regExportSettingsToINI(vbKey, VBA_PREF); + } + + HKEY key; + + if(RegOpenKey(HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) == + ERROR_SUCCESS) { + regExportSettingsToINI(key, "Viewer"); + RegCloseKey(key); + } +} diff --git a/src/win32/Reg.h b/src/win32/Reg.h new file mode 100644 index 00000000..1e658711 --- /dev/null +++ b/src/win32/Reg.h @@ -0,0 +1,37 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_REG_H +#define VBA_REG_H + +extern bool regEnabled; + +char *regQueryStringValue(const char *key, char *def); +DWORD regQueryDwordValue(const char *key, DWORD def, bool force=false); +BOOL regQueryBinaryValue(const char *key, char *value, int count); +void regSetStringValue(const char *key,const char *value); +void regSetDwordValue(const char *key,DWORD value,bool force=false); +void regSetBinaryValue(const char *key, char *value, int count); +void regDeleteValue(char *key); +void regInit(const char *); +void regShutdown(); +bool regCreateFileType(const char *ext, const char *type); +bool regAssociateType(const char *type, const char *desc, const char *application); +void regExportSettingsToINI(); +#endif // VBA_REG_H diff --git a/src/win32/ResizeDlg.cpp b/src/win32/ResizeDlg.cpp new file mode 100644 index 00000000..af843dbe --- /dev/null +++ b/src/win32/ResizeDlg.cpp @@ -0,0 +1,561 @@ +/*---------------------------------------------------------------------- + Copyright (c) Gipsysoft. All Rights Reserved. + File: DialogSizer_Set.cpp + Web site: http://gipsysoft.com + + This software is provided 'as-is', without any express or implied warranty. + + In no event will the author be held liable for any damages arising from the + use of this software. + + Permission is granted to anyone to use this software for any purpose, including + commercial applications, and to alter it and redistribute it freely, subject + to the following restrictions: + + 1) The origin of this software must not be misrepresented; you must not claim + that you wrote the original software. If you use this software in a product, + an acknowledgment in the product documentation is requested but not required. + 2) Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. Altered source is encouraged + to be submitted back to the original author so it can be shared with the + community. Please share your changes. + 3) This notice may not be removed or altered from any source distribution. + + Owner: russf@gipsysoft.com + Purpose: Main functionality for sizeable dialogs + + Store a local copy of the user settings + Subclass the window + Respond to various messages withinn the subclassed window. + + ----------------------------------------------------------------------*/ +#include "stdafx.h" +#include "VBA.h" +#include "ResizeDlg.h" +#undef ASSERT +#include "WinHelper.h" + +// moved functions to this file to reduce number of files + +struct RegistryData +{ + WINDOWPLACEMENT m_wpl; +}; + + +struct DialogData // dd +{ + HKEY hkRootSave; + LPCTSTR pcszName; + + // + // The number of items contained in the psd member. + // Used in the DeferWindowPos structure and in allocating memory + int nItemCount; + DialogSizerSizingItem *psd; + + // + // We need the smallest to respond to the WM_GETMINMAXINFO message + POINT m_ptSmallest; + + // + // We don't strictly speaking need to say how big the biggest can be but + POINT m_ptLargest; + bool m_bLargestSet; + + // + // we need this to decide how much the window has changed size when we get a WM_SIZE message + SIZE m_sizeClient; + + // + // Draw the sizing grip...or not + bool m_bMaximised; + BOOL m_bShowSizingGrip; + + WinHelper::CRect m_rcGrip; +}; + +//extern bool regEnabled; +extern const char *regGetINIPath(); + +void AssertFailed(char *file, int line, char *exp) +{ + char buffer[1024]; + + sprintf(buffer, "File %s\nLine %d\nExpression %s\nPress Retry to debug", + file, line, exp); + int res = MessageBox(*theApp.m_pMainWnd, buffer, "Assertion failed!", + MB_ICONHAND | MB_SETFOREGROUND | MB_TASKMODAL | + MB_ABORTRETRYIGNORE); + + if(res == IDRETRY) { + __asm int 3; + } else if(res == IDABORT) + SendMessage(*theApp.m_pMainWnd, WM_QUIT, 0, 0); +} + +void ApiFailure(char *pcszFilename, int nLine, char *pcszExpression ) +{ + const DWORD dwLastError = ::GetLastError(); + LPCTSTR lpMsgBuf; + (void)::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, dwLastError, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, 0, NULL ); + + char szExeName[ MAX_PATH ]; + + if( !GetModuleFileName( NULL, szExeName, countof( szExeName ) ) ) + strcpy( szExeName, "" ); + + + char szMessage[ 1024 ]; + _snprintf( szMessage, countof( szMessage ) + , "API VERIFY Failure!" + "\nProgram: %s" + "\n" + "\nFile %s" + "\nLine %d" + "\n" + "\nExpression %s" + "\n" + "\nLast Error %d" + "\n %s" + "\n\nPress Retry to debug the application" + , szExeName + , pcszFilename + , nLine + , pcszExpression + , dwLastError + , lpMsgBuf + ); + + (void)LocalFree( (LPVOID)lpMsgBuf ); + HWND hwndParent = ::GetActiveWindow(); + hwndParent = ::GetLastActivePopup( hwndParent ); + int nCode = ::MessageBoxA( hwndParent, + szMessage, + "Debug Helper", + MB_TASKMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE | + MB_SETFOREGROUND ); + if(nCode == IDABORT) { + ::SendMessage(*theApp.m_pMainWnd, WM_QUIT, 0, 0); + } else if(nCode == IDRETRY) + __asm int 3; +} + +long FASTCALL RegQueryValueExRecursive( HKEY hKey, LPCTSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData ) +{ + TCHAR szBuffer[ 256 ]; + R_ASSERT( lstrlen( lpValueName ) < countof( szBuffer ) ); + (void)lstrcpy( szBuffer, lpValueName ); + + LPTSTR pszBuffer = szBuffer; + LPTSTR pszLast = szBuffer; + while( *pszBuffer ) + { + if( *pszBuffer == _T('\\') || *pszBuffer == _T('/') ) + { + pszLast = pszBuffer; + lpValueName = pszLast + 1; + } + pszBuffer++; + } + + /*if(!regEnabled) {*/ + if(GetPrivateProfileStruct("Viewer", + lpValueName, + lpData, + *lpcbData, + regGetINIPath())) { + *lpType = REG_BINARY; + return ERROR_SUCCESS; + } + return -1; + /*} + + bool m_bNeedToCloseKey = false; + if( pszLast != szBuffer ) + { + *pszLast = _T('\000'); + HKEY hkeyTemp; + long lRet = RegOpenKey( hKey, szBuffer, &hkeyTemp ); + if( lRet != ERROR_SUCCESS ) + { + return lRet; + } + hKey = hkeyTemp; + m_bNeedToCloseKey = true; + } + + long lRet = RegQueryValueEx( hKey, lpValueName, lpReserved, lpType, lpData, lpcbData ); + if( m_bNeedToCloseKey ) + { + R_VERIFY( RegCloseKey( hKey ) == ERROR_SUCCESS ); + } + return lRet;*/ +} + +long FASTCALL RegSetValueExRecursive( HKEY hKey, LPCTSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE* lpData, DWORD cbData ) +{ + TCHAR szBuffer[ 256 ]; + R_ASSERT( lstrlen( lpValueName ) < countof( szBuffer ) ); + (void)lstrcpy( szBuffer, lpValueName ); + + LPTSTR pszBuffer = szBuffer; + LPTSTR pszLast = szBuffer; + while( *pszBuffer ) + { + if( *pszBuffer == _T('\\') || *pszBuffer == _T('/') ) + { + pszLast = pszBuffer; + lpValueName = pszLast + 1; + } + pszBuffer++; + } + + /*if(!regEnabled) {*/ + if(WritePrivateProfileStruct("Viewer", + lpValueName, + (LPVOID)lpData, + cbData, + regGetINIPath())) { + return ERROR_SUCCESS; + } + return -1; + /*} + + bool m_bNeedToCloseKey = false; + if( pszLast != szBuffer ) + { + *pszLast = _T('\000'); + HKEY hkeyTemp; + long lRet = RegOpenKey( hKey, szBuffer, &hkeyTemp ); + if( lRet != ERROR_SUCCESS ) + { + lRet = RegCreateKey( hKey, szBuffer, &hkeyTemp ); + if( lRet != ERROR_SUCCESS ) + return lRet; + } + hKey = hkeyTemp; + m_bNeedToCloseKey = true; + } + + long lRet = RegSetValueEx( hKey, lpValueName, Reserved, dwType, lpData, cbData ); + if( m_bNeedToCloseKey ) + { + R_VERIFY( RegCloseKey( hKey ) == ERROR_SUCCESS ); + } + return lRet;*/ +} + + +int ResizeDlgGetItemCount(const DialogSizerSizingItem *psd) +{ + R_ASSERT( psd ); + int nCount = 0; + while( psd->uSizeInfo != 0xFFFFFFFF ) + { + nCount++; + psd++; + } + return nCount; +} + +void ResizeDlgUpdateGripperRect( const int cx, const int cy, WinHelper::CRect &rcGrip ) +{ + const int nGripWidth = GetSystemMetrics( SM_CYVSCROLL ); + const int nGripHeight = GetSystemMetrics( SM_CXVSCROLL ); + rcGrip.left = cx - nGripWidth; + rcGrip.top = cy - nGripHeight; + rcGrip.right = cx; + rcGrip.bottom = cy; +} + +void ResizeDlgUpdateGripper( HWND hwnd, DialogData *pdd ) +{ + if( pdd->m_bShowSizingGrip ) + { + WinHelper::CRect rcOld( pdd->m_rcGrip ); + + ResizeDlgUpdateGripperRect( pdd->m_sizeClient.cx, pdd->m_sizeClient.cy, pdd->m_rcGrip ); + + // + // We also need to invalidate the combined area of the old and new rectangles + // otherwise we would have trail of grippers when we sized the dialog larger + // in any axis + (void)UnionRect( &rcOld, &rcOld, &pdd->m_rcGrip ); + (void)InvalidateRect( hwnd, &rcOld, TRUE ); + } +} + +void ResizeDlgCopyItems( DialogSizerSizingItem *psdDest, const DialogSizerSizingItem *psdSource ) + // + // Will copy all of the items in psdSource into psdDest. +{ + // + // Loop til we reach the end + while( psdSource->uSizeInfo != 0xFFFFFFFF ) + { + *psdDest = *psdSource; + psdDest++; + psdSource++; + } + // And when we do copy the last item + *psdDest = *psdSource; +} + + +ResizeDlg::ResizeDlg(UINT id, CWnd *parent) + : CDialog(id, parent) +{ + dd = NULL; +} + +void *ResizeDlg::AddDialogData() + // + // Firstly determine if the data already exists, if it does then return that, if not then we will + // create and initialise a brand new structure. +{ + DialogData *pdd = (DialogData*)dd; + if( !pdd ) { + pdd = (DialogData *)calloc(1, sizeof(DialogData)); + } + + if( pdd ) { + // + // Store some sizes etc. for later. + CRect rc; + GetWindowRect( rc ); + pdd->m_ptSmallest.x = rc.Width(); + pdd->m_ptSmallest.y = rc.Height(); + + + GetClientRect( rc ); + pdd->m_sizeClient = rc.Size(); + dd = pdd; + ResizeDlgUpdateGripperRect( pdd->m_sizeClient.cx, pdd->m_sizeClient.cy, pdd->m_rcGrip ); + } + return pdd; +} + +BOOL ResizeDlg::SetData(const DialogSizerSizingItem *psd, + BOOL bShowSizingGrip, + HKEY hkRootSave, + LPCTSTR pcszName, + SIZE *psizeMax) + // + // Setting a dialog sizeable involves subclassing the window and handling it's + // WM_SIZE messages, if we have a hkRootSave and pcszName then we will also be loading/saving + // the size and position of the window from the registry. We load from the registry when we + // subclass the window and we save to the registry when we get a WM_DESTROY. + // + // It will return non-zero for success and zero if it fails +{ + R_ASSERT( psd ); + R_ASSERT( ( hkRootSave != NULL && pcszName != NULL ) + || ( hkRootSave == NULL && pcszName == NULL ) ); + // + // Make sure all of the parameters are valid. + if( ::IsWindow( *this ) + && psd + && ( ( hkRootSave != NULL && pcszName != NULL && + !IsBadStringPtr( pcszName, 0xFFFF ) ) || + ( hkRootSave == NULL && pcszName == NULL ) ) + && ( psizeMax == NULL || !IsBadReadPtr( psizeMax, sizeof( SIZE ) ) ) + ) { + DialogData *pdd = (DialogData *)AddDialogData(); + if( pdd ) { + pdd->hkRootSave = hkRootSave; + pdd->pcszName = pcszName; + pdd->m_bShowSizingGrip = bShowSizingGrip; + pdd->nItemCount = ResizeDlgGetItemCount( psd ) + 1; + pdd->psd = (DialogSizerSizingItem *) + calloc(pdd->nItemCount, + sizeof(DialogSizerSizingItem )); + if( pdd->psd ) { + // + // Copy all of the user controls etc. for later, this way the user can quite happily + // let the structure go out of scope. + ResizeDlgCopyItems( pdd->psd, psd ); + if( psizeMax ) { + pdd->m_ptLargest.x = psizeMax->cx; + pdd->m_ptLargest.y = psizeMax->cy; + pdd->m_bLargestSet = true; + } + + // + // If the there was save info passed in then we need to make damn good use of it + // by attempting to load the RegistryData structure + if( hkRootSave && pcszName ) { + RegistryData rd; + DWORD dwSize = sizeof( RegistryData ); + DWORD dwType = REG_BINARY; + if( RegQueryValueExRecursive( hkRootSave, pcszName, NULL, &dwType, reinterpret_cast( &rd ), &dwSize ) == ERROR_SUCCESS && dwSize == sizeof( rd ) ) { + if( !(GetWindowLong( *this, GWL_STYLE ) & WS_VISIBLE) ) + rd.m_wpl.showCmd = SW_HIDE; + + VAPI( SetWindowPlacement( &rd.m_wpl ) ); + } + } + return TRUE; + } else { + free(pdd); + } + } + } + return FALSE; +} + +void ResizeDlg::UpdateWindowSize(const int cx, const int cy, HWND hwnd) +{ + DialogData *pdd = (DialogData*)dd; + if( pdd ) { + const int nDeltaX = cx - pdd->m_sizeClient.cx; + const int nDeltaY = cy - pdd->m_sizeClient.cy; + WinHelper::CDeferWindowPos def( pdd->nItemCount ); + WinHelper::CRect rc; + const DialogSizerSizingItem *psd = pdd->psd; + while( psd->uSizeInfo != 0xFFFFFFFF ) { + HWND hwndChild = ::GetDlgItem( *this, psd->uControlID ); + if( ::IsWindow( hwndChild ) ) { + VAPI( ::GetWindowRect( hwndChild, rc ) ); + (void)::MapWindowPoints( ::GetDesktopWindow(), hwnd, + (LPPOINT)&rc, 2 ); + + // + // Adjust the window horizontally + if( psd->uSizeInfo & DS_MoveX ) { + rc.left += nDeltaX; + rc.right += nDeltaX; + } + + // + // Adjust the window vertically + if( psd->uSizeInfo & DS_MoveY ) { + rc.top += nDeltaY; + rc.bottom += nDeltaY; + } + + // + // Size the window horizontally + if( psd->uSizeInfo & DS_SizeX ) { + rc.right += nDeltaX; + } + + // + // Size the window vertically + if( psd->uSizeInfo & DS_SizeY ) { + rc.bottom += nDeltaY; + } + + (void)def.DeferWindowPos( hwndChild, NULL, rc, + SWP_NOACTIVATE | SWP_NOZORDER ); + } + psd++; + } + + pdd->m_sizeClient.cx = cx; + pdd->m_sizeClient.cy = cy; + + // + // If we have a sizing grip enabled then adjust it's position + ResizeDlgUpdateGripper( hwnd, pdd ); + } +} + +BOOL ResizeDlg::OnWndMsg(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *res ) + // Actual window procedure that will handle saving window size/position and moving + // the controls whilst the window sizes. +{ + if(dd == NULL) { + return CDialog::OnWndMsg(msg, wParam, lParam, res); + } + switch( msg ) { + case WM_ERASEBKGND: + { + BOOL r = CDialog::OnWndMsg(msg, wParam, lParam, res); + DialogData *pdd = (DialogData*)dd; + if( pdd && pdd->m_bShowSizingGrip && !pdd->m_bMaximised ) { + VAPI( ::DrawFrameControl( reinterpret_cast( wParam ), + pdd->m_rcGrip, + DFC_SCROLL, DFCS_SCROLLSIZEGRIP ) ); + } + return r; + } + case WM_SIZE: + { + DialogData *pdd = (DialogData*)dd; + if( pdd && wParam != SIZE_MINIMIZED ) { + pdd->m_bMaximised = ( wParam == SIZE_MAXIMIZED ? true : false ); + UpdateWindowSize( LOWORD( lParam ), HIWORD( lParam ), *this); + } + } + break; + case WM_NCHITTEST: + { + // + // If the gripper is enabled then perform a simple hit test on our gripper area. + DialogData *pdd = (DialogData*)dd; + if( pdd && pdd->m_bShowSizingGrip ) { + POINT pt = { LOWORD(lParam), HIWORD(lParam) }; + (void)ScreenToClient( &pt ); + if( PtInRect( pdd->m_rcGrip, pt ) ) + return (BOOL)HTBOTTOMRIGHT; + } + } + break; + case WM_GETMINMAXINFO: + { + // + // Our opportunity to say that we do not want the dialog to grow or shrink any more. + DialogData *pdd = (DialogData*)dd; + LPMINMAXINFO lpmmi = reinterpret_cast( lParam ); + lpmmi->ptMinTrackSize = pdd->m_ptSmallest; + if( pdd->m_bLargestSet ) { + lpmmi->ptMaxTrackSize = pdd->m_ptLargest; + } + } + return (BOOL)0; + case WM_NOTIFY: + { + if( reinterpret_cast(lParam)->code == PSN_SETACTIVE ) { + CRect rc; + VAPI( ::GetClientRect( *GetParent( ), &rc ) ); + UpdateWindowSize( rc.Width(), rc.Height(), *GetParent( ) ); + } + } + break; + case WM_DESTROY: + { + // + // Our opportunty for cleanup. + // Simply acquire all of our objects, free the appropriate memory and remove the + // properties from the window. If we do not remove the properties then they will constitute + // a resource leak. + DialogData *pdd = (DialogData*)dd; + if( pdd ) { + RegistryData rd; + rd.m_wpl.length = sizeof( rd.m_wpl ); + VAPI( GetWindowPlacement( &rd.m_wpl ) ); + + if( pdd->hkRootSave && pdd->pcszName ) { + (void)RegSetValueExRecursive( pdd->hkRootSave, pdd->pcszName, + NULL, REG_BINARY, + reinterpret_cast( &rd ), + sizeof( rd ) ); + } + + if( pdd->psd ) { + free(pdd->psd); + } + free(pdd); + } + + } + break; + } + return CDialog::OnWndMsg(msg, wParam, lParam, res); +} diff --git a/src/win32/ResizeDlg.h b/src/win32/ResizeDlg.h new file mode 100644 index 00000000..e09d5893 --- /dev/null +++ b/src/win32/ResizeDlg.h @@ -0,0 +1,59 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_WIN32_RESIZEDLG_H +#define VBA_WIN32_RESIZEDLG_H + +#ifndef _INC_TCHAR +#include +#endif // _INC_TCHAR + +// +// Predefined sizing information +#define DS_MoveX 1 +#define DS_MoveY 2 +#define DS_SizeX 4 +#define DS_SizeY 8 + +typedef struct DialogSizerSizingItem // sdi +{ + UINT uControlID; + UINT uSizeInfo; +} DialogSizerSizingItem; + +#define DIALOG_SIZER_START( name ) DialogSizerSizingItem name[] = { +#define DIALOG_SIZER_ENTRY( controlID, flags ) { controlID, flags }, +#define DIALOG_SIZER_END() { 0xFFFFFFFF, 0xFFFFFFFF } }; + +class ResizeDlg : public CDialog { + void *dd; + public: + ResizeDlg(UINT id, CWnd *parent = NULL); + + void *AddDialogData(); + BOOL SetData(const DialogSizerSizingItem *psd, + BOOL bShowSizingGrip, + HKEY hkRootSave, + LPCTSTR pcszName, + SIZE *psizeMax); + void UpdateWindowSize(const int cx, const int cy, HWND); + + virtual BOOL OnWndMsg(UINT, WPARAM, LPARAM, LRESULT *); +}; +#endif diff --git a/src/win32/RewindInterval.cpp b/src/win32/RewindInterval.cpp new file mode 100644 index 00000000..6b7c1bd2 --- /dev/null +++ b/src/win32/RewindInterval.cpp @@ -0,0 +1,100 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// RewindInterval.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "RewindInterval.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// RewindInterval dialog + + +RewindInterval::RewindInterval(int interval, CWnd* pParent /*=NULL*/) + : CDialog(RewindInterval::IDD, pParent) +{ + //{{AFX_DATA_INIT(RewindInterval) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->interval = interval; +} + + +void RewindInterval::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RewindInterval) + DDX_Control(pDX, IDC_INTERVAL, m_interval); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(RewindInterval, CDialog) + //{{AFX_MSG_MAP(RewindInterval) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// RewindInterval message handlers + +void RewindInterval::OnCancel() +{ + EndDialog(-1); +} + +void RewindInterval::OnOk() +{ + CString buffer; + + m_interval.GetWindowText(buffer); + + int v = atoi(buffer); + + if(v >= 0 && v <= 600) { + EndDialog(v); + } else + systemMessage(IDS_INVALID_INTERVAL_VALUE, + "Invalid rewind interval value. Please enter a number " + "between 0 and 600 seconds"); +} + +BOOL RewindInterval::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_interval.LimitText(3); + + CString buffer; + buffer.Format("%d", interval); + m_interval.SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/RewindInterval.h b/src/win32/RewindInterval.h new file mode 100644 index 00000000..2410c25b --- /dev/null +++ b/src/win32/RewindInterval.h @@ -0,0 +1,68 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_) +#define AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// RewindInterval.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// RewindInterval dialog + +class RewindInterval : public CDialog +{ + // Construction + public: + int interval; + RewindInterval(int interval, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RewindInterval) + enum { IDD = IDD_REWIND_INTERVAL }; + CEdit m_interval; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RewindInterval) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(RewindInterval) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_) diff --git a/src/win32/RomInfo.cpp b/src/win32/RomInfo.cpp new file mode 100644 index 00000000..9892038d --- /dev/null +++ b/src/win32/RomInfo.cpp @@ -0,0 +1,585 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004-2005 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// RomInfo.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "RomInfo.h" +#include "WinResUtil.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +extern int gbRomSize; + +struct WinGBACompanyName { + LPCTSTR code; + LPCTSTR name; +}; + +static WinGBACompanyName winGBARomInfoCompanies[] = { + { "01", "Nintendo" }, + { "02", "Rocket Games" }, + { "08", "Capcom" }, + { "09", "Hot B Co." }, + { "0A", "Jaleco" }, + { "0B", "Coconuts Japan" }, + { "0C", "Coconuts Japan/G.X.Media" }, + { "0H", "Starfish" }, + { "0L", "Warashi Inc." }, + { "0N", "Nowpro" }, + { "0P", "Game Village" }, + { "13", "Electronic Arts Japan" }, + { "18", "Hudson Soft Japan" }, + { "19", "S.C.P." }, + { "1A", "Yonoman" }, + { "1G", "SMDE" }, + { "1P", "Creatures Inc." }, + { "1Q", "TDK Deep Impresion" }, + { "20", "Destination Software" }, + { "22", "VR 1 Japan" }, + { "25", "San-X" }, + { "28", "Kemco Japan" }, + { "29", "Seta" }, + { "2H", "Ubisoft Japan" }, + { "2K", "NEC InterChannel" }, + { "2L", "Tam" }, + { "2M", "Jordan" }, + { "2N", "Smilesoft" }, + { "2Q", "Mediakite" }, + { "36", "Codemasters" }, + { "37", "GAGA Communications" }, + { "38", "Laguna" }, + { "39", "Telstar Fun and Games" }, + { "41", "Ubi Soft Entertainment" }, + { "42", "Sunsoft" }, + { "47", "Spectrum Holobyte" }, + { "49", "IREM" }, + { "4D", "Malibu Games" }, + { "4F", "Eidos/U.S. Gold" }, + { "4J", "Fox Interactive" }, + { "4K", "Time Warner Interactive" }, + { "4Q", "Disney" }, + { "4S", "Black Pearl" }, + { "4X", "GT Interactive" }, + { "4Y", "RARE" }, + { "4Z", "Crave Entertainment" }, + { "50", "Absolute Entertainment" }, + { "51", "Acclaim" }, + { "52", "Activision" }, + { "53", "American Sammy Corp." }, + { "54", "Take 2 Interactive" }, + { "55", "Hi Tech" }, + { "56", "LJN LTD." }, + { "58", "Mattel" }, + { "5A", "Mindscape/Red Orb Ent." }, + { "5C", "Taxan" }, + { "5D", "Midway" }, + { "5F", "American Softworks" }, + { "5G", "Majesco Sales Inc" }, + { "5H", "3DO" }, + { "5K", "Hasbro" }, + { "5L", "NewKidCo" }, + { "5M", "Telegames" }, + { "5N", "Metro3D" }, + { "5P", "Vatical Entertainment" }, + { "5Q", "LEGO Media" }, + { "5S", "Xicat Interactive" }, + { "5T", "Cryo Interactive" }, + { "5W", "Red Storm Ent./BKN Ent." }, + { "5X", "Microids" }, + { "5Z", "Conspiracy Entertainment Corp." }, + { "60", "Titus Interactive Studios" }, + { "61", "Virgin Interactive" }, + { "62", "Maxis" }, + { "64", "LucasArts Entertainment" }, + { "67", "Ocean" }, + { "69", "Electronic Arts" }, + { "6E", "Elite Systems Ltd." }, + { "6F", "Electro Brain" }, + { "6G", "The Learning Company" }, + { "6H", "BBC" }, + { "6J", "Software 2000" }, + { "6L", "BAM! Entertainment" }, + { "6M", "Studio 3" }, + { "6Q", "Classified Games" }, + { "6S", "TDK Mediactive" }, + { "6U", "DreamCatcher" }, + { "6V", "JoWood Productions" }, + { "6W", "SEGA" }, + { "6X", "Wannado Edition" }, + { "6Y", "LSP" }, + { "6Z", "ITE Media" }, + { "70", "Infogrames" }, + { "71", "Interplay" }, + { "72", "JVC Musical Industries Inc" }, + { "73", "Parker Brothers" }, + { "75", "SCI" }, + { "78", "THQ" }, + { "79", "Accolade" }, + { "7A", "Triffix Ent. Inc." }, + { "7C", "Microprose Software" }, + { "7D", "Universal Interactive Studios" }, + { "7F", "Kemco" }, + { "7G", "Rage Software" }, + { "7H", "Encore" }, + { "7J", "Zoo" }, + { "7K", "BVM" }, + { "7L", "Simon & Schuster Interactive" }, + { "7M", "Asmik Ace Entertainment Inc./AIA" }, + { "7N", "Empire Interactive" }, + { "7Q", "Jester Interactive" }, + { "7T", "Scholastic" }, + { "7U", "Ignition Entertainment" }, + { "7W", "Stadlbauer" }, + { "80", "Misawa" }, + { "83", "LOZC" }, + { "8B", "Bulletproof Software" }, + { "8C", "Vic Tokai Inc." }, + { "8J", "General Entertainment" }, + { "8N", "Success" }, + { "8P", "SEGA Japan" }, + { "91", "Chun Soft" }, + { "92", "Video System" }, + { "93", "BEC" }, + { "96", "Yonezawa/S'pal" }, + { "97", "Kaneko" }, + { "99", "Victor Interactive Software" }, + { "9A", "Nichibutsu/Nihon Bussan" }, + { "9B", "Tecmo" }, + { "9C", "Imagineer" }, + { "9F", "Nova" }, + { "9H", "Bottom Up" }, + { "9L", "Hasbro Japan" }, + { "9N", "Marvelous Entertainment" }, + { "9P", "Keynet Inc." }, + { "9Q", "Hands-On Entertainment" }, + { "A0", "Telenet" }, + { "A1", "Hori" }, + { "A4", "Konami" }, + { "A6", "Kawada" }, + { "A7", "Takara" }, + { "A9", "Technos Japan Corp." }, + { "AA", "JVC" }, + { "AC", "Toei Animation" }, + { "AD", "Toho" }, + { "AF", "Namco" }, + { "AG", "Media Rings Corporation" }, + { "AH", "J-Wing" }, + { "AK", "KID" }, + { "AL", "MediaFactory" }, + { "AP", "Infogrames Hudson" }, + { "AQ", "Kiratto. Ludic Inc" }, + { "B0", "Acclaim Japan" }, + { "B1", "ASCII" }, + { "B2", "Bandai" }, + { "B4", "Enix" }, + { "B6", "HAL Laboratory" }, + { "B7", "SNK" }, + { "B9", "Pony Canyon Hanbai" }, + { "BA", "Culture Brain" }, + { "BB", "Sunsoft" }, + { "BD", "Sony Imagesoft" }, + { "BF", "Sammy" }, + { "BG", "Magical" }, + { "BJ", "Compile" }, + { "BL", "MTO Inc." }, + { "BN", "Sunrise Interactive" }, + { "BP", "Global A Entertainment" }, + { "BQ", "Fuuki" }, + { "C0", "Taito" }, + { "C2", "Kemco" }, + { "C3", "Square Soft" }, + { "C5", "Data East" }, + { "C6", "Tonkin House" }, + { "C8", "Koei" }, + { "CA", "Konami/Palcom/Ultra" }, + { "CB", "Vapinc/NTVIC" }, + { "CC", "Use Co.,Ltd." }, + { "CD", "Meldac" }, + { "CE", "FCI/Pony Canyon" }, + { "CF", "Angel" }, + { "CM", "Konami Computer Entertainment Osaka" }, + { "CP", "Enterbrain" }, + { "D1", "Sofel" }, + { "D2", "Quest" }, + { "D3", "Sigma Enterprises" }, + { "D4", "Ask Kodansa" }, + { "D6", "Naxat" }, + { "D7", "Copya System" }, + { "D9", "Banpresto" }, + { "DA", "TOMY" }, + { "DB", "LJN Japan" }, + { "DD", "NCS" }, + { "DF", "Altron Corporation" }, + { "DH", "Gaps Inc." }, + { "DN", "ELF" }, + { "E2", "Yutaka" }, + { "E3", "Varie" }, + { "E5", "Epoch" }, + { "E7", "Athena" }, + { "E8", "Asmik Ace Entertainment Inc." }, + { "E9", "Natsume" }, + { "EA", "King Records" }, + { "EB", "Atlus" }, + { "EC", "Epic/Sony Records" }, + { "EE", "IGS" }, + { "EL", "Spike" }, + { "EM", "Konami Computer Entertainment Tokyo" }, + { "EN", "Alphadream Corporation" }, + { "F0", "A Wave" }, + { "G1", "PCCW" }, + { "G4", "KiKi Co Ltd" }, + { "G5", "Open Sesame Inc." }, + { "G6", "Sims" }, + { "G7", "Broccoli" }, + { "G8", "Avex" }, + { "G9", "D3 Publisher" }, + { "GB", "Konami Computer Entertainment Japan" }, + { "GD", "Square-Enix" }, + { "HY", "Sachen" }, + { NULL, NULL } +}; + +static LPCTSTR winGBARomInfoFindMakerCode(LPCTSTR code) +{ + int i = 0; + while(winGBARomInfoCompanies[i].code) { + if(!strcmp(winGBARomInfoCompanies[i].code, code)) + return winGBARomInfoCompanies[i].name; + i++; + } + return (LPCTSTR)winResLoadString(IDS_UNKNOWN); +} + + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGB dialog + + +RomInfoGB::RomInfoGB(u8 *rom, CWnd* pParent /*=NULL*/) + : CDialog(RomInfoGB::IDD, pParent) +{ + //{{AFX_DATA_INIT(RomInfoGB) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->rom = rom; +} + + +void RomInfoGB::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RomInfoGB) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(RomInfoGB, CDialog) + //{{AFX_MSG_MAP(RomInfoGB) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// RomInfoGB message handlers + +void RomInfoGB::OnOk() +{ + EndDialog(TRUE); +} + +BOOL RomInfoGB::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[128]; + + strncpy(buffer, (const char *)&rom[0x134], 15); + buffer[15] = 0; + GetDlgItem(IDC_ROM_TITLE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x143]); + GetDlgItem(IDC_ROM_COLOR)->SetWindowText(buffer); + + strncpy(buffer, (const char *)&rom[0x144],2); + buffer[2] = 0; + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + + if(rom[0x14b] != 0x33) { + sprintf(buffer, "%02X", rom[0x14b]); + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + } + GetDlgItem(IDC_ROM_MAKER_NAME2)->SetWindowText(winGBARomInfoFindMakerCode(buffer)); + + sprintf(buffer, "%02x", rom[0x146]); + GetDlgItem(IDC_ROM_UNIT_CODE)->SetWindowText(buffer); + + CString type = winResLoadString(IDS_UNKNOWN); + switch(rom[0x147]) { + case 0x00: + type = "ROM"; + break; + case 0x01: + type = "ROM+MBC1"; + break; + case 0x02: + type = "ROM+MBC1+RAM"; + break; + case 0x03: + type = "ROM+MBC1+RAM+BATT"; + break; + case 0x05: + type = "ROM+MBC2"; + break; + case 0x06: + type = "ROM+MBC2+BATT"; + break; + case 0x0f: + type = "ROM+MBC3+TIMER+BATT"; + break; + case 0x10: + type = "ROM+MBC3+TIMER+RAM+BATT"; + break; + case 0x11: + type = "ROM+MBC3"; + break; + case 0x12: + type = "ROM+MBC3+RAM"; + break; + case 0x13: + type = "ROM+MBC3+RAM+BATT"; + break; + case 0x19: + type = "ROM+MBC5"; + break; + case 0x1a: + type = "ROM+MBC5+RAM"; + break; + case 0x1b: + type = "ROM+MBC5+RAM+BATT"; + break; + case 0x1c: + type = "ROM+MBC5+RUMBLE"; + break; + case 0x1d: + type = "ROM+MBC5+RUMBLE+RAM"; + break; + case 0x1e: + type = "ROM+MBC5+RUMBLE+RAM+BATT"; + break; + case 0x22: + type = "ROM+MBC7+BATT"; + break; + case 0xfe: + type = "ROM+HuC-3"; + break; + case 0xff: + type = "ROM+HuC-1"; + break; + } + sprintf(buffer, "%02x (%s)", rom[0x147], (const char *)type); + GetDlgItem(IDC_ROM_DEVICE_TYPE)->SetWindowText(buffer); + + type = winResLoadString(IDS_UNKNOWN); + switch(rom[0x148]) { + case 0: + type = "32K"; + break; + case 1: + type = "64K"; + break; + case 2: + type = "128K"; + break; + case 3: + type = "256K"; + break; + case 4: + type = "512K"; + break; + case 5: + type = "1M"; + break; + case 6: + type = "2M"; + break; + case 7: + type = "4M"; + break; + } + + sprintf(buffer, "%02x (%s)", rom[0x148], (const char *)type); + GetDlgItem(IDC_ROM_SIZE)->SetWindowText(buffer); + + type = winResLoadString(IDS_UNKNOWN); + switch(rom[0x149]) { + case 0: + type = winResLoadString(IDS_NONE); + break; + case 1: + type = "2K"; + break; + case 2: + type = "8K"; + break; + case 3: + type = "32K"; + break; + case 4: + type = "128K"; + break; + case 5: + type = "64K"; + break; + } + + sprintf(buffer, "%02x (%s)", rom[0x149], (const char *)type); + GetDlgItem(IDC_ROM_RAM_SIZE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14a]); + GetDlgItem(IDC_ROM_DEST_CODE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14b]); + GetDlgItem(IDC_ROM_LIC_CODE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14c]); + GetDlgItem(IDC_ROM_VERSION)->SetWindowText(buffer); + + u8 crc = 25; + int i; + for(i = 0x134; i < 0x14d; i++) { + crc += rom[i]; + } + + crc = 256 - crc; + + sprintf(buffer, "%02x (%02x)", crc, rom[0x14d]); + GetDlgItem(IDC_ROM_CRC)->SetWindowText(buffer); + + u16 crc16 = 0; + for(i = 0; i < gbRomSize; i++) { + crc16 += rom[i]; + } + + crc16 -= rom[0x14e]; + crc16 -= rom[0x14f]; + sprintf(buffer, "%04x (%04x)", crc16, (rom[0x14e]<<8)|rom[0x14f]); + GetDlgItem(IDC_ROM_CHECKSUM)->SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} +///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA dialog + + +RomInfoGBA::RomInfoGBA(u8 *rom, CWnd* pParent /*=NULL*/) + : CDialog(RomInfoGBA::IDD, pParent) +{ + //{{AFX_DATA_INIT(RomInfoGBA) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->rom = rom; +} + + +void RomInfoGBA::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RomInfoGBA) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(RomInfoGBA, CDialog) + //{{AFX_MSG_MAP(RomInfoGBA) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA message handlers + +void RomInfoGBA::OnOk() +{ + EndDialog(TRUE); +} + +BOOL RomInfoGBA::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[32]; + + // Game Title + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + GetDlgItem(IDC_ROM_TITLE)->SetWindowText(buffer); + + // Game Code + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + GetDlgItem(IDC_ROM_GAME_CODE)->SetWindowText(buffer); + + // Maker Code + strncpy(buffer, (const char *)&rom[0xb0],2); + buffer[2] = 0; + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + + // Maker Code -> Maker Name + GetDlgItem(IDC_ROM_MAKER_NAME)->SetWindowText(winGBARomInfoFindMakerCode(buffer)); + + // Main Unit Code + sprintf(buffer, "%02x", rom[0xb3]); + GetDlgItem(IDC_ROM_UNIT_CODE)->SetWindowText(buffer); + + // Device Type + sprintf(buffer, "%02x", rom[0xb4]); + GetDlgItem(IDC_ROM_DEVICE_TYPE)->SetWindowText(buffer); + + // ROM Version Number + sprintf(buffer, "%02x", rom[0xbc]); + GetDlgItem(IDC_ROM_VERSION)->SetWindowText(buffer); + + // Complement Check + int x, crc = 0x19; + for(int i = 0xa0; i < 0xbd; i++) + { + x = rom[i]; + crc += x; + } + crc = (-crc) & 0xFF; + char same = (rom[0xbd] == crc) ? '=' : '/'; // Compare + sprintf(buffer, "ROM: %02X %c EMU: %02X", rom[0xbd], same, crc); + GetDlgItem(IDC_ROM_CRC)->SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/win32/RomInfo.h b/src/win32/RomInfo.h new file mode 100644 index 00000000..e7005f47 --- /dev/null +++ b/src/win32/RomInfo.h @@ -0,0 +1,101 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_) +#define AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// RomInfo.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGB dialog + +class RomInfoGB : public CDialog +{ + // Construction + public: + RomInfoGB(u8 *rom, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RomInfoGB) + enum { IDD = IDD_GB_ROM_INFO }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RomInfoGB) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + u8 *rom; + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(RomInfoGB) + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA dialog + +class RomInfoGBA : public CDialog +{ + // Construction + public: + RomInfoGBA(u8 *rom, CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RomInfoGBA) + enum { IDD = IDD_GBA_ROM_INFO }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + u8 *rom; + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RomInfoGBA) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(RomInfoGBA) + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_) diff --git a/src/win32/Sound.h b/src/win32/Sound.h new file mode 100644 index 00000000..3f4cc2ad --- /dev/null +++ b/src/win32/Sound.h @@ -0,0 +1,35 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_WIN32_SOUND_H +#define VBA_WIN32_SOUND_H + +class ISound +{ + public: + virtual ~ISound() {}; + + virtual bool init() = 0; + virtual void pause() = 0; + virtual void reset() = 0; + virtual void resume() = 0; + virtual void write() = 0; +}; + +#endif diff --git a/src/win32/StringTokenizer.cpp b/src/win32/StringTokenizer.cpp new file mode 100644 index 00000000..b7ea3136 --- /dev/null +++ b/src/win32/StringTokenizer.cpp @@ -0,0 +1,69 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// StringTokenizer.cpp: implementation of the StringTokenizer class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "vba.h" +#include "StringTokenizer.h" + +#ifdef _DEBUG +#undef THIS_FILE +static char THIS_FILE[]=__FILE__; +#define new DEBUG_NEW +#endif + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +StringTokenizer::StringTokenizer(CString str, CString del) +{ + m_right = str; + m_delim = del; +} + +StringTokenizer::~StringTokenizer() +{ + +} + + +const char *StringTokenizer::next() +{ + int index = m_right.FindOneOf(m_delim); + + while(index == 0) { + m_right = m_right.Right(m_right.GetLength()-1); + index = m_right.FindOneOf(m_delim); + } + if(index == -1) { + if(m_right.IsEmpty()) + return NULL; + m_token = m_right; + m_right.Empty(); + return m_token; + } + + m_token = m_right.Left(index); + m_right = m_right.Right(m_right.GetLength()-(1+index)); + + return m_token; +} diff --git a/src/win32/StringTokenizer.h b/src/win32/StringTokenizer.h new file mode 100644 index 00000000..7deae2da --- /dev/null +++ b/src/win32/StringTokenizer.h @@ -0,0 +1,44 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// StringTokenizer.h: interface for the StringTokenizer class. +// +////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_) +#define AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +class StringTokenizer +{ + public: + const char * next(); + StringTokenizer(CString str, CString token); + virtual ~StringTokenizer(); + + private: + CString m_token; + CString m_delim; + CString m_right; +}; + +#endif // !defined(AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_) diff --git a/src/win32/Throttle.cpp b/src/win32/Throttle.cpp new file mode 100644 index 00000000..b143d0cc --- /dev/null +++ b/src/win32/Throttle.cpp @@ -0,0 +1,87 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Throttle.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "Throttle.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// Throttle dialog + + +Throttle::Throttle(CWnd* pParent /*=NULL*/) + : CDialog(Throttle::IDD, pParent) +{ + //{{AFX_DATA_INIT(Throttle) + m_throttle = 0; + //}}AFX_DATA_INIT +} + + +void Throttle::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Throttle) + DDX_Text(pDX, IDC_THROTTLE, m_throttle); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(Throttle, CDialog) + //{{AFX_MSG_MAP(Throttle) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Throttle message handlers + +BOOL Throttle::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Throttle::OnCancel() +{ + EndDialog(false); +} + +void Throttle::OnOk() +{ + UpdateData(); + + if(m_throttle < 5 || m_throttle > 1000) + systemMessage(IDS_INVALID_THROTTLE_VALUE, "Invalid throttle value. Please enter a number between 5 and 1000"); + else + EndDialog(m_throttle); +} diff --git a/src/win32/Throttle.h b/src/win32/Throttle.h new file mode 100644 index 00000000..e4064178 --- /dev/null +++ b/src/win32/Throttle.h @@ -0,0 +1,67 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_) +#define AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Throttle.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Throttle dialog + +class Throttle : public CDialog +{ + // Construction + public: + Throttle(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Throttle) + enum { IDD = IDD_THROTTLE }; + int m_throttle; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Throttle) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(Throttle) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_) diff --git a/src/win32/TileView.cpp b/src/win32/TileView.cpp new file mode 100644 index 00000000..24b3a4c7 --- /dev/null +++ b/src/win32/TileView.cpp @@ -0,0 +1,587 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// TileView.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "FileDlg.h" +#include "Reg.h" +#include "TileView.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../NLS.h" +#include "../Util.h" + +extern "C" { +#include +} + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// TileView dialog + + +TileView::TileView(CWnd* pParent /*=NULL*/) + : ResizeDlg(TileView::IDD, pParent) +{ + //{{AFX_DATA_INIT(TileView) + m_colors = -1; + m_charBase = -1; + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo, 0, sizeof(bmpInfo)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32*8; + bmpInfo.bmiHeader.biHeight = 32*8; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 32*32 * 64); + + tileView.setData(data); + tileView.setBmpInfo(&bmpInfo); + + charBase = 0; + is256Colors = 0; + palette = 0; + w = h = 0; +} + +TileView::~TileView() +{ + free(data); + data = NULL; +} + +void TileView::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(TileView) + DDX_Control(pDX, IDC_PALETTE_SLIDER, m_slider); + DDX_Radio(pDX, IDC_16_COLORS, m_colors); + DDX_Radio(pDX, IDC_CHARBASE_0, m_charBase); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_TILE_VIEW, tileView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, zoom); + DDX_Control(pDX, IDC_COLOR, color); +} + + +BEGIN_MESSAGE_MAP(TileView, CDialog) + //{{AFX_MSG_MAP(TileView) + ON_BN_CLICKED(IDC_SAVE, OnSave) + ON_BN_CLICKED(IDC_CLOSE, OnClose) + ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) + ON_BN_CLICKED(IDC_16_COLORS, On16Colors) + ON_BN_CLICKED(IDC_256_COLORS, On256Colors) + ON_BN_CLICKED(IDC_CHARBASE_0, OnCharbase0) + ON_BN_CLICKED(IDC_CHARBASE_1, OnCharbase1) + ON_BN_CLICKED(IDC_CHARBASE_2, OnCharbase2) + ON_BN_CLICKED(IDC_CHARBASE_3, OnCharbase3) + ON_BN_CLICKED(IDC_CHARBASE_4, OnCharbase4) + ON_BN_CLICKED(IDC_STRETCH, OnStretch) + ON_WM_HSCROLL() + //}}AFX_MSG_MAP + ON_MESSAGE(WM_MAPINFO, OnMapInfo) + ON_MESSAGE(WM_COLINFO, OnColInfo) + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// TileView message handlers + +void TileView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + + +void TileView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name,"wb"); + + if(!fp) { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if(!png_ptr) { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if(!info_ptr) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + if(setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr,NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr,fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr,info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for(int y = 0; y < sizeY; y++) { + for(int x = 0; x < sizeX; x++) { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr,writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + + +void TileView::OnSave() +{ + CString captureBuffer; + + if(theApp.captureFormat == 0) + captureBuffer = "tiles.png"; + else + captureBuffer = "tiles.bmp"; + + LPCTSTR exts[] = {".png", ".bmp" }; + + CString filter = theApp.winLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if(dlg.DoModal() == IDCANCEL) { + return; + } + + captureBuffer = dlg.GetPathName(); + + if(dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +void TileView::renderTile256(int tile, int x, int y, u8 *charBase, u16 *palette) +{ + u8 *bmp = &data[24*x + 8*32*24*y]; + + for(int j = 0; j < 8; j++) { + for(int i = 0; i < 8; i++) { + u8 c = charBase[tile*64 + j * 8 + i]; + + u16 color = palette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + } + bmp += 31*24; // advance line + } +} + +void TileView::renderTile16(int tile, int x, int y, u8 *charBase, u16 *palette) +{ + u8 *bmp = &data[24*x + 8*32*24*y]; + + int pal = this->palette; + + if(this->charBase == 4) + pal += 16; + + for(int j = 0; j < 8; j++) { + for(int i = 0; i < 8; i++) { + u8 c = charBase[tile*32 + j * 4 + (i>>1)]; + + if(i & 1) + c = c>>4; + else + c = c & 15; + + u16 color = palette[pal*16+c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + bmp += 31*24; // advance line + } +} + + +void TileView::render() +{ + u16 *palette = (u16 *)paletteRAM; + u8 *charBase = &vram[this->charBase * 0x4000]; + + int maxY; + + if(is256Colors) { + int tile = 0; + maxY = 16; + for(int y = 0; y < maxY; y++) { + for(int x = 0; x < 32; x++) { + if(this->charBase == 4) + renderTile256(tile, x, y, charBase, &palette[256]); + else + renderTile256(tile, x, y, charBase, palette); + tile++; + } + } + tileView.setSize(32*8, maxY*8); + w = 32*8; + h = maxY*8; + SIZE s; + s.cx = 32*8; + s.cy = maxY*8; + if(tileView.getStretch()) { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT,s); + } else { + int tile = 0; + maxY = 32; + if(this->charBase == 3) + maxY = 16; + for(int y = 0; y < maxY; y++) { + for(int x = 0; x < 32; x++) { + renderTile16(tile, x, y, charBase, palette); + tile++; + } + } + tileView.setSize(32*8, maxY*8); + w = 32*8; + h = maxY*8; + SIZE s; + s.cx = 32*8; + s.cy = maxY*8; + if(tileView.getStretch()) { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT, s); + } +} + +void TileView::update() +{ + paint(); +} + + +BOOL TileView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START( sz ) + DIALOG_SIZER_ENTRY( IDC_TILE_VIEW, DS_SizeX | DS_SizeY ) + DIALOG_SIZER_ENTRY( IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_B, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\TileView", + NULL); + + m_colors = is256Colors; + m_charBase = charBase; + + m_slider.SetRange(0, 15); + m_slider.SetPageSize(4); + m_slider.SetTicFreq(1); + + paint(); + + m_stretch = regQueryDwordValue("tileViewStretch", 0); + if(m_stretch) + tileView.setStretch(true); + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void TileView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + + +void TileView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if(autoUpdate) { + theApp.winAddUpdateListener(this); + } else { + theApp.winRemoveUpdateListener(this); + } +} + + +void TileView::paint() +{ + if(vram != NULL && paletteRAM != NULL) { + render(); + tileView.refresh(); + } +} + + +void TileView::On16Colors() +{ + is256Colors = 0; + paint(); +} + +void TileView::On256Colors() +{ + is256Colors = 1; + paint(); +} + +void TileView::OnCharbase0() +{ + charBase = 0; + paint(); +} + +void TileView::OnCharbase1() +{ + charBase = 1; + paint(); +} + +void TileView::OnCharbase2() +{ + charBase = 2; + paint(); +} + +void TileView::OnCharbase3() +{ + charBase = 3; + paint(); +} + +void TileView::OnCharbase4() +{ + charBase = 4; + paint(); +} + +void TileView::OnStretch() +{ + tileView.setStretch(!tileView.getStretch()); + paint(); + regSetDwordValue("tileViewStretch", tileView.getStretch()); +} + +LRESULT TileView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + zoom.setColors(colors); + + int x = (wParam & 0xFFFF)/8; + int y = ((wParam >> 16) & 0xFFFF)/8; + + u32 address = 0x6000000 + 0x4000 * charBase; + int tile = 32 * y + x; + if(is256Colors) + tile *= 2; + address += 32 * tile; + + CString buffer; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUMBER)->SetWindowText(buffer); + + buffer.Format("%08x", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT TileView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void TileView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) +{ + switch(nSBCode) { + case TB_THUMBPOSITION: + palette = nPos; + break; + default: + palette = m_slider.GetPos(); + break; + } + paint(); +} + +void TileView::PostNcDestroy() +{ + delete this; +} diff --git a/src/win32/TileView.h b/src/win32/TileView.h new file mode 100644 index 00000000..888d67b2 --- /dev/null +++ b/src/win32/TileView.h @@ -0,0 +1,109 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_) +#define AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// TileView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "IUpdate.h" +#include "ResizeDlg.h" +#include "ZoomControl.h" + +///////////////////////////////////////////////////////////////////////////// +// TileView dialog + +class TileView : public ResizeDlg, IUpdateListener +{ + int charBase; + int is256Colors; + int palette; + BitmapControl tileView; + BITMAPINFO bmpInfo; + u8 *data; + ZoomControl zoom; + ColorControl color; + int w; + int h; + bool autoUpdate; + // Construction + public: + void paint(); + void render(); + void renderTile16(int tile, int x, int y, u8 *charBase, u16 *palette); + void renderTile256(int tile, int x, int y, u8 *charBase, u16 *palette); + void savePNG(const char *name); + void saveBMP(const char *name); + TileView(CWnd* pParent = NULL); // standard constructor + virtual ~TileView(); + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(TileView) + enum { IDD = IDD_TILE_VIEWER }; + CSliderCtrl m_slider; + int m_colors; + int m_charBase; + BOOL m_stretch; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(TileView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + virtual afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + + // Generated message map functions + //{{AFX_MSG(TileView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnAutoUpdate(); + afx_msg void On16Colors(); + afx_msg void On256Colors(); + afx_msg void OnCharbase0(); + afx_msg void OnCharbase1(); + afx_msg void OnCharbase2(); + afx_msg void OnCharbase3(); + afx_msg void OnCharbase4(); + afx_msg void OnStretch(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_) diff --git a/src/win32/UniVideoModeDlg.cpp b/src/win32/UniVideoModeDlg.cpp new file mode 100644 index 00000000..a7ba3569 --- /dev/null +++ b/src/win32/UniVideoModeDlg.cpp @@ -0,0 +1,278 @@ +// source\win32\UniVideoModeDlg.cpp : Implementierungsdatei +// + +#include ".\univideomodedlg.h" + +// UniVideoModeDlg-Dialogfeld + +IMPLEMENT_DYNAMIC(UniVideoModeDlg, CDialog) +UniVideoModeDlg::UniVideoModeDlg(CWnd* pParent /*=NULL*/, int *width, int *height, int *BPP, int *freq, int *adapt) + : CDialog(UniVideoModeDlg::IDD, pParent) +{ + WidthList = 0; + HeightList = 0; + BPPList = 0; + FreqList = 0; + iDisplayDevice = 0; + SelectedWidth = width; + SelectedHeight = height; + SelectedBPP = BPP; + SelectedFreq = freq; + SelectedAdapter = adapt; + pD3D = NULL; + d3dDLL = NULL; + nAdapters = 1; +} + +UniVideoModeDlg::~UniVideoModeDlg() +{ + if (WidthList) delete [] WidthList; + if (HeightList) delete [] HeightList; + if (BPPList) delete [] BPPList; + if (FreqList) delete [] FreqList; +} + +void UniVideoModeDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); +} + + +BEGIN_MESSAGE_MAP(UniVideoModeDlg, CDialog) + ON_WM_CREATE() + ON_BN_CLICKED(IDOK, OnBnClickedOk) + ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) +// ON_STN_CLICKED(IDC_DISPLAYDEVICE, OnStnClickedDisplaydevice) +//ON_STN_CLICKED(IDC_DISPLAYDEVICE, OnStnClickedDisplaydevice) +ON_STN_CLICKED(IDC_DISPLAYDEVICE, OnStnClickedDisplaydevice) +ON_BN_CLICKED(IDC_BUTTON_MAXSCALE, OnBnClickedButtonMaxscale) +ON_BN_CLICKED(IDC_CHECK_STRETCHTOFIT, OnBnClickedCheckStretchtofit) +END_MESSAGE_MAP() + + +// UniVideoModeDlg-Meldungshandler + +int UniVideoModeDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) +{ + if (CDialog::OnCreate(lpCreateStruct) == -1) + return -1; + + // TODO: Fügen Sie Ihren spezialisierten Erstellcode hier ein. + + return 0; +} + + +BOOL UniVideoModeDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CButton* check_stretchtofit = (CButton*)GetDlgItem(IDC_CHECK_STRETCHTOFIT); + check_stretchtofit->SetCheck(theApp.fullScreenStretch ? BST_CHECKED : BST_UNCHECKED); + + CStatic* apiname = (CStatic*)GetDlgItem(IDC_APINAME); + CStatic* devicename = (CStatic*)GetDlgItem(IDC_DISPLAYDEVICE); + CListBox* listmodes = (CListBox*)GetDlgItem(IDC_LISTMODES); + CString temp; + DWORD w, h, b, f; + UINT nModes16, nModes32; + + switch (theApp.display->getType()) + { + case GDI: + apiname->SetWindowText("Windows GDI"); + DISPLAY_DEVICE dd; + dd.cb = sizeof(dd); + EnumDisplayDevices(NULL, iDisplayDevice, &dd, 0); + devicename->SetWindowText(dd.DeviceString); + DEVMODE dm; + dm.dmSize = sizeof(DEVMODE); + dm.dmDriverExtra = 0; + DWORD maxMode; + for (DWORD i=0; 0 != EnumDisplaySettings(dd.DeviceName, i, &dm); i++) {} + maxMode = i-1; + listmodes->InitStorage(i, 25); + if (WidthList!=0) delete [] WidthList; + if (HeightList!=0) delete [] HeightList; + if (BPPList!=0) delete [] BPPList; + if (FreqList!=0) delete [] FreqList; + WidthList = new DWORD[i]; + HeightList = new DWORD[i]; + BPPList = new DWORD[i]; + FreqList = new DWORD[i]; + listmodes->ResetContent(); + for (i=0; iAddString(temp); + WidthList[i] = w; + HeightList[i] = h; + BPPList[i] = b; + FreqList[i] = f; + } + break; + + + case DIRECT_DRAW: + apiname->SetWindowText("DirectDraw 7"); + break; + + + case DIRECT_3D: + apiname->SetWindowText("Direct3D 9"); + // Load DirectX DLL + d3dDLL = LoadLibrary("D3D9.DLL"); + LPDIRECT3D9 (WINAPI *D3DCreate)(UINT); + D3DCreate = (LPDIRECT3D9 (WINAPI *)(UINT)) + GetProcAddress(d3dDLL, "Direct3DCreate9"); + pD3D = D3DCreate(D3D_SDK_VERSION); + nAdapters = pD3D->GetAdapterCount(); + D3DADAPTER_IDENTIFIER9 id; + pD3D->GetAdapterIdentifier(iDisplayDevice, 0, &id); + devicename->SetWindowText(id.Description); + + D3DDISPLAYMODE d3ddm; + + nModes16 = pD3D->GetAdapterModeCount(iDisplayDevice, D3DFMT_R5G6B5); + nModes32 = pD3D->GetAdapterModeCount(iDisplayDevice, D3DFMT_X8R8G8B8); + + listmodes->InitStorage(nModes16+nModes32, 25); + listmodes->ResetContent(); + + if (WidthList!=0) delete [] WidthList; + if (HeightList!=0) delete [] HeightList; + if (BPPList!=0) delete [] BPPList; + if (FreqList!=0) delete [] FreqList; + WidthList = new DWORD[nModes16+nModes32]; + HeightList = new DWORD[nModes16+nModes32]; + BPPList = new DWORD[nModes16+nModes32]; + FreqList = new DWORD[nModes16+nModes32]; + + b = 16; + for (UINT i = 0; i < nModes16; i++) + { + pD3D->EnumAdapterModes(iDisplayDevice, D3DFMT_R5G6B5, i, &d3ddm); + w = d3ddm.Width; + h = d3ddm.Height; + f = d3ddm.RefreshRate; + + temp.Format("%dx%dx%d @%dHz", w, h, b, f); + listmodes->AddString(temp); + + WidthList[i] = w; + HeightList[i] = h; + BPPList[i] = b; + FreqList[i] = f; + } + b = 32; + for (UINT i = 0; i < nModes32; i++) + { + pD3D->EnumAdapterModes(iDisplayDevice, D3DFMT_X8R8G8B8, i, &d3ddm); + w = d3ddm.Width; + h = d3ddm.Height; + f = d3ddm.RefreshRate; + + temp.Format("%dx%dx%d @%dHz", w, h, b, f); + listmodes->AddString(temp); + + WidthList[i+nModes16] = w; + HeightList[i+nModes16] = h; + BPPList[i+nModes16] = b; + FreqList[i+nModes16] = f; + } + + // Clean up + pD3D->Release(); + pD3D = NULL; + if(d3dDLL != NULL) + { + FreeLibrary(d3dDLL); + d3dDLL = NULL; + } + break; + + + case OPENGL: + apiname->SetWindowText("OpenGL"); + break; + } + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + + +void UniVideoModeDlg::OnBnClickedOk() +{ + CListBox* listmodes = (CListBox*)GetDlgItem(IDC_LISTMODES); + int selection = listmodes->GetCurSel(); + if (selection == LB_ERR) + { + MessageBox("No mode selected!", "Error", MB_OK | MB_ICONWARNING); + return; + } + *SelectedWidth = WidthList[selection]; + *SelectedHeight = HeightList[selection]; + *SelectedBPP = BPPList[selection]; + *SelectedFreq = FreqList[selection]; + *SelectedAdapter = iDisplayDevice; + + EndDialog(0); +} + +void UniVideoModeDlg::OnBnClickedCancel() +{ + EndDialog(-1); +} +void UniVideoModeDlg::OnStnClickedDisplaydevice() +{ + DWORD old = iDisplayDevice; + switch (theApp.display->getType()) + { + case GDI: + DISPLAY_DEVICE dd; + dd.cb = sizeof(dd); + if (0 == EnumDisplayDevices(NULL, ++iDisplayDevice, &dd, 0)) + iDisplayDevice = 0; + break; + + + case DIRECT_DRAW: + break; + + + case DIRECT_3D: + iDisplayDevice++; + if (iDisplayDevice == nAdapters) iDisplayDevice = 0; + break; + + + case OPENGL: + break; + } + + if (iDisplayDevice != old) + OnInitDialog(); +} + +void UniVideoModeDlg::OnBnClickedButtonMaxscale() +{ + MaxScale dlg; + theApp.winCheckFullscreen(); + dlg.DoModal(); +} + +void UniVideoModeDlg::OnBnClickedCheckStretchtofit() +{ + theApp.fullScreenStretch = !theApp.fullScreenStretch; + theApp.updateWindowSize(theApp.videoOption); + if(theApp.display) + theApp.display->clear(); + this->SetFocus(); +} diff --git a/src/win32/UniVideoModeDlg.h b/src/win32/UniVideoModeDlg.h new file mode 100644 index 00000000..f19b675e --- /dev/null +++ b/src/win32/UniVideoModeDlg.h @@ -0,0 +1,47 @@ +#pragma once + +#include "stdafx.h" +#include +#include "VBA.h" +#include "MaxScale.h" + +// UniVideoModeDlg-Dialogfeld + +class UniVideoModeDlg : public CDialog +{ + DECLARE_DYNAMIC(UniVideoModeDlg) + +public: + UniVideoModeDlg(CWnd* pParent = NULL, int *width=0, int *height=0, int *BPP=0, int *freq=0, int *adapt=0); // Standardkonstruktor + virtual ~UniVideoModeDlg(); + +// Dialogfelddaten + enum { IDD = IDD_UNIVIDMODE }; + +protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV-Unterstützung + + DECLARE_MESSAGE_MAP() +public: + afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); + afx_msg void OnBnClickedOk(); + BOOL OnInitDialog(); + afx_msg void OnBnClickedCancel(); + +private: + DWORD *WidthList, *HeightList, *BPPList, *FreqList; + DWORD iDisplayDevice; + HINSTANCE d3dDLL; + LPDIRECT3D9 pD3D; + UINT nAdapters; +public: + int + *SelectedWidth, + *SelectedHeight, + *SelectedBPP, + *SelectedFreq, + *SelectedAdapter; + afx_msg void OnStnClickedDisplaydevice(); + afx_msg void OnBnClickedButtonMaxscale(); + afx_msg void OnBnClickedCheckStretchtofit(); +}; diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp new file mode 100644 index 00000000..109e4aea --- /dev/null +++ b/src/win32/VBA.cpp @@ -0,0 +1,2294 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// VBA.cpp : Defines the class behaviors for the application. +// +#include "stdafx.h" +#include + +#include "AVIWrite.h" +#include "LangSelect.h" +#include "MainWnd.h" +#include "Reg.h" +#include "..\..\res\resource.h" +#include "WavWriter.h" +#include "WinResUtil.h" + +#include "../System.h" +#include "../agbprint.h" +#include "../cheatSearch.h" +#include "../GBA.h" +#include "../Globals.h" +#include "../RTC.h" +#include "../Sound.h" +#include "../Util.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" + +/* Link +---------------------*/ +#include "../Link.h" +/* ---------------- */ + +#include "../gbafilter.h" + +#ifdef SDL +#pragma comment( lib, "SDL" ) +#pragma comment( lib, "SDLmain" ) +#endif + +extern void Pixelate(u8*,u32,u8*,u8*,u32,int,int); +extern void Pixelate32(u8*,u32,u8*,u8*,u32,int,int); +extern void MotionBlur(u8*,u32,u8*,u8*,u32,int,int); +extern void MotionBlur32(u8*,u32,u8*,u8*,u32,int,int); +extern void _2xSaI(u8*,u32,u8*,u8*,u32,int,int); +extern void _2xSaI32(u8*,u32,u8*,u8*,u32,int,int); +extern void Super2xSaI(u8*,u32,u8*,u8*,u32,int,int); +extern void Super2xSaI32(u8*,u32,u8*,u8*,u32,int,int); +extern void SuperEagle(u8*,u32,u8*,u8*,u32,int,int); +extern void SuperEagle32(u8*,u32,u8*,u8*,u32,int,int); +extern void AdMame2x(u8*,u32,u8*,u8*,u32,int,int); +extern void AdMame2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Bilinear(u8*,u32,u8*,u8*,u32,int,int); +extern void Bilinear32(u8*,u32,u8*,u8*,u32,int,int); +extern void BilinearPlus(u8*,u32,u8*,u8*,u32,int,int); +extern void BilinearPlus32(u8*,u32,u8*,u8*,u32,int,int); +extern void Scanlines(u8*,u32,u8*,u8*,u32,int,int); +extern void Scanlines32(u8*,u32,u8*,u8*,u32,int,int); +extern void ScanlinesTV(u8*,u32,u8*,u8*,u32,int,int); +extern void ScanlinesTV32(u8*,u32,u8*,u8*,u32,int,int); +extern void hq2x(u8*,u32,u8*,u8*,u32,int,int); +extern void hq2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void lq2x(u8*,u32,u8*,u8*,u32,int,int); +extern void lq2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple2x16(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple3x16(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple3x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple4x16(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple4x32(u8*,u32,u8*,u8*,u32,int,int); + +extern void hq3x32(u8*,u32,u8*,u8*,u32,int,int); +extern void hq3x16(u8*,u32,u8*,u8*,u32,int,int); + +extern void SmartIB(u8*,u32,int,int); +extern void SmartIB32(u8*,u32,int,int); +extern void MotionBlurIB(u8*,u32,int,int); +extern void InterlaceIB(u8*,u32,int,int); +extern void MotionBlurIB32(u8*,u32,int,int); + +extern void toolsLog(const char *); + +extern IDisplay *newGDIDisplay(); +extern IDisplay *newDirectDrawDisplay(); +extern IDisplay *newDirect3DDisplay(); +extern IDisplay *newOpenGLDisplay(); + +extern Input *newDirectInput(); + +extern ISound *newDirectSound(); + +extern void remoteStubSignal(int, int); +extern void remoteOutput(char *, u32); +extern void remoteStubMain(); +extern void remoteSetProtocol(int); +extern void remoteCleanUp(); +extern int remoteSocket; + +extern void InterframeCleanup(); + +void winlog(const char *msg, ...); + +/* Link +---------------------*/ +extern int InitLink(void); +extern void CloseLink(void); +//extern int linkid; +extern char inifile[]; +extern FILE *jjj; +/* ------------------- */ +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +int emulating = 0; +bool debugger = false; +int RGB_LOW_BITS_MASK = 0; +bool b16to32Video = false; +int systemFrameSkip = 0; +int systemSpeed = 0; +bool systemSoundOn = false; +u32 systemColorMap32[0x10000]; +u16 systemColorMap16[0x10000]; +u16 systemGbPalette[24]; +int systemRedShift = 0; +int systemBlueShift = 0; +int systemGreenShift = 0; +int systemColorDepth = 16; +int systemVerbose = 0; +int systemDebug = 0; +int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + +void winSignal(int,int); +void winOutput(char *, u32); + +void (*dbgSignal)(int,int) = winSignal; +void (*dbgOutput)(char *, u32) = winOutput; + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +void directXMessage(const char *msg) +{ + systemMessage(IDS_DIRECTX_7_REQUIRED, + "DirectX 7.0 or greater is required to run.\nDownload at http://www.microsoft.com/directx.\n\nError found at: %s", + msg); +} + +///////////////////////////////////////////////////////////////////////////// +// VBA + +BEGIN_MESSAGE_MAP(VBA, CWinApp) + //{{AFX_MSG_MAP(VBA) + // NOTE - the ClassWizard will add and remove mapping macros here. + // DO NOT EDIT what you see in these blocks of generated code! + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// VBA construction + +VBA::VBA() +{ + mode320Available = false; + mode640Available = false; + mode800Available = false; + mode1024Available = false; + mode1280Available = false; + windowPositionX = 0; + windowPositionY = 0; + filterFunction = NULL; + ifbFunction = NULL; + ifbType = 0; + filterType = FILTER_NONE; + filterWidth = 0; + filterHeight = 0; + fsWidth = 0; + fsHeight = 0; + fsColorDepth = 0; + fsForceChange = false; + surfaceSizeX = 0; + surfaceSizeY = 0; + sizeX = 0; + sizeY = 0; + videoOption = VIDEO_3X; + fullScreenStretch = false; + disableStatusMessage = false; + showSpeed = 0; + showSpeedTransparent = true; + showRenderedFrames = 0; + screenMessage = false; + screenMessageTime = 0; + menuToggle = true; + display = NULL; + menu = NULL; + popup = NULL; + cartridgeType = 0; + soundInitialized = false; + useBiosFile = false; + skipBiosFile = false; + active = true; + paused = false; + recentFreeze = false; + autoSaveLoadCheatList = false; + winout = NULL; + removeIntros = false; + autoIPS = false; + winGbBorderOn = 0; + winFlashSize = 0x20000; + winRtcEnable = false; + winSaveType = 0; + rewindMemory = NULL; + rewindPos = 0; + rewindTopPos = 0; + rewindCounter = 0; + rewindCount = 0; + rewindSaveNeeded = false; + rewindTimer = 0; + captureFormat = 0; + tripleBuffering = true; + autoHideMenu = false; + throttle = 0; + throttleLastTime = 0; + autoFrameSkipLastTime = 0; + autoFrameSkip = false; + vsync = false; + changingVideoSize = false; + pVideoDriverGUID = NULL; + renderMethod = DIRECT_DRAW; + iconic = false; + ddrawEmulationOnly = false; + ddrawUsingEmulationOnly = false; + ddrawDebug = false; + ddrawUseVideoMemory = false; + d3dFilter = 0; + glFilter = 0; + glType = 0; + regEnabled = false; + pauseWhenInactive = true; + speedupToggle = false; + useOldSync = false; + winGbPrinterEnabled = false; + threadPriority = 2; + disableMMX = false; + languageOption = 0; + languageModule = NULL; + languageName = ""; + renderedFrames = 0; + input = NULL; + joypadDefault = 0; + autoFire = 0; + autoFireToggle = false; + winPauseNextFrame = false; + soundRecording = false; + soundRecorder = NULL; + sound = NULL; + aviRecording = false; + aviRecorder = NULL; + aviFrameNumber = 0; + painting = false; + movieRecording = false; + moviePlaying = false; + movieFrame = 0; + moviePlayFrame = 0; + movieFile = NULL; + movieLastJoypad = 0; + movieNextJoypad = 0; + sensorX = 2047; + sensorY = 2047; + mouseCounter = 0; + wasPaused = false; + frameskipadjust = 0; + autoLoadMostRecent = false; + fsMaxScale = 0; + romSize = 0; + + updateCount = 0; + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + ZeroMemory(&emulator, sizeof(emulator)); + + hAccel = NULL; + + for(int i = 0; i < 24;) { + systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); + systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + systemGbPalette[i++] = 0; + } +} + +VBA::~VBA() +{ + InterframeCleanup(); + + saveSettings(); + + if(moviePlaying) { + if(movieFile != NULL) { + fclose(movieFile); + movieFile = NULL; + } + moviePlaying = false; + movieLastJoypad = 0; + } + + if(movieRecording) { + if(movieFile != NULL) { + // record the last joypad change so that the correct time can be + // recorded + fwrite(&movieFrame, 1, sizeof(int), movieFile); + fwrite(&movieLastJoypad, 1, sizeof(u32), movieFile); + fclose(movieFile); + movieFile = NULL; + } + movieRecording = false; + moviePlaying = false; + movieLastJoypad = 0; + } + + if(aviRecorder) { + delete aviRecorder; + aviRecording = false; + } + + if(soundRecorder) { + delete soundRecorder; + soundRecorder = NULL; + } + soundRecording = false; + soundPause(); + soundShutdown(); + + if(gbRom != NULL || rom != NULL) { + if(autoSaveLoadCheatList) + ((MainWnd *)m_pMainWnd)->winSaveCheatListDefault(); + ((MainWnd *)m_pMainWnd)->writeBatteryFile(); + cheatSearchCleanup(&cheatSearchData); + emulator.emuCleanUp(); + } + + if(input) + delete input; + + shutdownDisplay(); + + if(rewindMemory) + free(rewindMemory); +} + +///////////////////////////////////////////////////////////////////////////// +// The one and only VBA object + +VBA theApp; +//#include +///////////////////////////////////////////////////////////////////////////// +// VBA initialization + +// code from SDL_main.c for Windows +/* Parse a command line buffer into arguments */ +static int parseCommandLine(char *cmdline, char **argv) +{ + char *bufp; + int argc; + + argc = 0; + for ( bufp = cmdline; *bufp; ) { + /* Skip leading whitespace */ + while ( isspace(*bufp) ) { + ++bufp; + } + /* Skip over argument */ + if ( *bufp == '"' ) { + ++bufp; + if ( *bufp ) { + if ( argv ) { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while ( *bufp && (*bufp != '"') ) { + ++bufp; + } + } else { + if ( *bufp ) { + if ( argv ) { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while ( *bufp && ! isspace(*bufp) ) { + ++bufp; + } + } + if ( *bufp ) { + if ( argv ) { + *bufp = '\0'; + } + ++bufp; + } + } + if ( argv ) { + argv[argc] = NULL; + } + return(argc); +} + +BOOL VBA::InitInstance() +{ + AfxEnableControlContainer(); + // Standard initialization + // If you are not using these features and wish to reduce the size + // of your final executable, you should remove from the following + // the specific initialization routines you do not need. + + SetRegistryKey(_T("VBA")); + + remoteSetProtocol(0); + + systemVerbose = GetPrivateProfileInt("config", + "verbose", + 0, + "VBA.ini"); + + systemDebug = GetPrivateProfileInt("config", + "debug", + 0, + "VBA.ini"); + ddrawDebug = GetPrivateProfileInt("config", + "ddrawDebug", + 0, + "VBA.ini") ? true : false; + + wndClass = AfxRegisterWndClass(0, LoadCursor(IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), LoadIcon(IDI_ICON)); + + char winBuffer[2048]; + + GetModuleFileName(NULL, winBuffer, 2048); + char *p = strrchr(winBuffer, '\\'); + if(p) + *p = 0; + + regInit(winBuffer); + + loadSettings(); + + if(!initInput()) + return FALSE; + + if(!initDisplay()) { + if(videoOption >= VIDEO_320x240) { + regSetDwordValue("video", VIDEO_1X); + if(pVideoDriverGUID) + regSetDwordValue("defaultVideoDriver", TRUE); + } + return FALSE; + } + + hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR)); + + winAccelMgr.Connect((MainWnd *)m_pMainWnd); + + winAccelMgr.SetRegKey(HKEY_CURRENT_USER, "Software\\Emulators\\VisualBoyAdvance"); + + extern void winAccelAddCommands(CAcceleratorManager&); + + winAccelAddCommands(winAccelMgr); + + winAccelMgr.CreateDefaultTable(); + + winAccelMgr.Load(); + + winAccelMgr.UpdateWndTable(); + + winAccelMgr.UpdateMenu(menu); + + if (m_lpCmdLine[0]) + { + int argc = parseCommandLine(m_lpCmdLine, NULL); + char **argv = (char **)malloc((argc+1)*sizeof(char *)); + parseCommandLine(m_lpCmdLine, argv); + if(argc > 0) { + szFile = argv[0]; + filename = szFile; + } + int index = filename.ReverseFind('.'); + + if(index != -1) + filename = filename.Left(index); + + if(((MainWnd*)m_pMainWnd)->FileRun()) + emulating = true; + else + emulating = false; + free(argv); + } + + return TRUE; +} + +void VBA::adjustDestRect() +{ + POINT point; + point.x = 0; + point.y = 0; + + m_pMainWnd->ClientToScreen(&point); + dest.top = point.y; + dest.left = point.x; + + point.x = surfaceSizeX; + point.y = surfaceSizeY; + + m_pMainWnd->ClientToScreen(&point); + dest.bottom = point.y; + dest.right = point.x; + + // make sure that dest rect lies in the monitor + if(videoOption >= VIDEO_320x240) { + dest.top -= windowPositionY; + dest.left -= windowPositionX; + dest.bottom-= windowPositionY; + dest.right -= windowPositionX; + } + + int menuSkip = 0; + + if(videoOption >= VIDEO_320x240 && menuToggle) { + int m = GetSystemMetrics(SM_CYMENU); + menuSkip = m; + dest.bottom -=m; + } + + if(videoOption > VIDEO_4X) { + int top = (fsHeight - surfaceSizeY) / 2; + int left = (fsWidth - surfaceSizeX) / 2; + dest.top += top; + dest.bottom += top; + dest.left += left; + dest.right += left; + if(fullScreenStretch) { + dest.top = 0+menuSkip; + dest.left = 0; + dest.right = fsWidth; + dest.bottom = fsHeight; + } + } +} + +void VBA::updateIFB() +{ + if(systemColorDepth == 16) { + switch(ifbType) { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB; + break; + case 2: + ifbFunction = SmartIB; + break; + } + } else if(systemColorDepth == 32) { + switch(ifbType) { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB32; + break; + case 2: + ifbFunction = SmartIB32; + break; + } + } else + ifbFunction = NULL; +} + +void VBA::updateFilter() +{ + // BEGIN hacky ugly code + + // HQ3X asm wants 16 bit input. When we switch + // away from 16 bits we need to restore the driver values + + if ( b16to32Video ) + { + b16to32Video = false; + systemColorDepth = 32; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + utilUpdateSystemColorMaps(theApp.filterLCD); + } + // END hacky ugly code + + filterWidth = sizeX; + filterHeight = sizeY; + filterMagnification = 1; + + + if ( videoOption == VIDEO_1X || videoOption == VIDEO_320x240 ) + { + filterFunction = NULL; + filterMagnification = 1; + } + else + { + if ( systemColorDepth == 16 ) + { + switch(filterType) + { + default: + case FILTER_NONE: + filterFunction = NULL; + filterMagnification = 1; + break; + case FILTER_TVMODE: + filterFunction = ScanlinesTV; + filterMagnification = 2; + break; + case FILTER_2XSAI: + filterFunction = _2xSaI; + filterMagnification = 2; + break; + case FILTER_SUPER2XSAI: + filterFunction = Super2xSaI; + filterMagnification = 2; + break; + case FILTER_SUPEREAGLE: + filterFunction = SuperEagle; + filterMagnification = 2; + break; + case FILTER_PIXELATE: + filterFunction = Pixelate; + filterMagnification = 2; + break; + case FILTER_MAMESCALE2X: + filterFunction = AdMame2x; + filterMagnification = 2; + break; + case FILTER_SIMPLE2X: + filterFunction = Simple2x16; + filterMagnification = 2; + break; + case FILTER_BILINEAR: + filterFunction = Bilinear; + filterMagnification = 2; + break; + case FILTER_BILINEARPLUS: + filterFunction = BilinearPlus; + filterMagnification = 2; + break; + case FILTER_SCANLINES: + filterFunction = Scanlines; + filterMagnification = 2; + break; + case FILTER_HQ2X: + filterFunction = hq2x; + filterMagnification = 2; + break; + case FILTER_LQ2X: + filterFunction = lq2x; + filterMagnification = 2; + break; + case FILTER_SIMPLE3X: + filterFunction = Simple3x16; + filterMagnification = 3; + break; + case FILTER_SIMPLE4X: + filterFunction = Simple4x16; + filterMagnification = 4; + break; + case FILTER_HQ3X: + filterFunction = hq3x16; + filterMagnification = 3; + break; + case FILTER_HQ4X: + //filterFunction = lq2x; + filterMagnification = 4; + break; + } + } + + if ( systemColorDepth == 32 ) + { + switch(filterType) + { + default: + case FILTER_NONE: + filterFunction = NULL; + filterMagnification = 1; + break; + case FILTER_TVMODE: + filterFunction = ScanlinesTV32; + filterMagnification = 2; + break; + case FILTER_2XSAI: + filterFunction = _2xSaI32; + filterMagnification = 2; + break; + case FILTER_SUPER2XSAI: + filterFunction = Super2xSaI32; + filterMagnification = 2; + break; + case FILTER_SUPEREAGLE: + filterFunction = SuperEagle32; + filterMagnification = 2; + break; + case FILTER_PIXELATE: + filterFunction = Pixelate32; + filterMagnification = 2; + break; + case FILTER_MAMESCALE2X: + filterFunction = AdMame2x32; + filterMagnification = 2; + break; + case FILTER_SIMPLE2X: + filterFunction = Simple2x32; + filterMagnification = 2; + break; + case FILTER_BILINEAR: + filterFunction = Bilinear32; + filterMagnification = 2; + break; + case FILTER_BILINEARPLUS: + filterFunction = BilinearPlus32; + filterMagnification = 2; + break; + case FILTER_SCANLINES: + filterFunction = Scanlines32; + filterMagnification = 2; + break; + case FILTER_HQ2X: + filterFunction = hq2x32; + filterMagnification = 2; + break; + case FILTER_LQ2X: + filterFunction = lq2x32; + filterMagnification = 2; + break; + case FILTER_SIMPLE3X: + filterFunction = Simple3x32; + filterMagnification = 3; + break; + case FILTER_SIMPLE4X: + filterFunction = Simple4x32; + filterMagnification = 4; + break; + case FILTER_HQ3X: + filterFunction = hq3x32; + filterMagnification = 3; + b16to32Video=true; + break; + case FILTER_HQ4X: + //filterFunction = lq2x; + filterMagnification = 4; + break; + } + } + } + + rect.right = sizeX * filterMagnification; + rect.bottom = sizeY * filterMagnification; + + if( filterType != FILTER_NONE ) + memset(delta, 0xFF, sizeof(delta)); + + if( display ) + display->changeRenderSize(rect.right, rect.bottom); + + if (b16to32Video) + { + systemColorDepth = 16; + systemRedShift = 11; + systemGreenShift = 6; + systemBlueShift = 0; + utilUpdateSystemColorMaps(theApp.filterLCD); + } +} + + +void VBA::updateMenuBar() +{ + if(menu != NULL) { + if(m_pMainWnd) + m_pMainWnd->SetMenu(NULL); + m_menu.Detach(); + DestroyMenu(menu); + } + + if(popup != NULL) { + // force popup recreation if language changed + DestroyMenu(popup); + popup = NULL; + } + + m_menu.Attach(winResLoadMenu(MAKEINTRESOURCE(IDR_MENU))); + menu = (HMENU)m_menu; + + if(m_pMainWnd) + m_pMainWnd->SetMenu(&m_menu); + + if (display) + display->clear(); +} + +void winlog(const char *msg, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, msg); + buffer.FormatV(msg, valist); + + if(theApp.winout == NULL) { + theApp.winout = fopen("vba-trace.log","w"); + } + + fputs(buffer, theApp.winout); + + va_end(valist); +} + +void log(const char *msg, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, msg); + buffer.FormatV(msg, valist); + + toolsLog(buffer); + + va_end(valist); +} + +bool systemReadJoypads() +{ + if(theApp.input) + return theApp.input->readDevices(); + return false; +} + +u32 systemReadJoypad(int which) +{ + if(theApp.input) + return theApp.input->readDevice(which); + return 0; +} + +void systemDrawScreen() +{ + if(theApp.display == NULL) + return; + + theApp.renderedFrames++; + + if(theApp.updateCount) { + POSITION pos = theApp.updateList.GetHeadPosition(); + while(pos) { + IUpdateListener *up = theApp.updateList.GetNext(pos); + up->update(); + } + } + + if(theApp.aviRecording && !theApp.painting) { + int width = 240; + int height = 160; + switch(theApp.cartridgeType) { + case 0: + width = 240; + height = 160; + break; + case 1: + if(gbBorderOn) { + width = 256; + height = 224; + } else { + width = 160; + height = 144; + } + break; + } + + if(theApp.aviRecorder == NULL) { + theApp.aviRecorder = new AVIWrite(); + theApp.aviFrameNumber = 0; + + theApp.aviRecorder->SetFPS(60); + + BITMAPINFOHEADER bi; + memset(&bi, 0, sizeof(bi)); + bi.biSize = 0x28; + bi.biPlanes = 1; + bi.biBitCount = 24; + bi.biWidth = width; + bi.biHeight = height; + bi.biSizeImage = 3*width*height; + theApp.aviRecorder->SetVideoFormat(&bi); + theApp.aviRecorder->Open(theApp.aviRecordName); + } + + char *bmp = new char[width*height*3]; + + utilWriteBMP(bmp, width, height, pix); + theApp.aviRecorder->AddFrame(theApp.aviFrameNumber, bmp); + + delete bmp; + } + + if(theApp.ifbFunction) { + if(systemColorDepth == 16) + theApp.ifbFunction(pix+theApp.filterWidth*2+4, theApp.filterWidth*2+4, + theApp.filterWidth, theApp.filterHeight); + else + theApp.ifbFunction(pix+theApp.filterWidth*4+4, theApp.filterWidth*4+4, + theApp.filterWidth, theApp.filterHeight); + } + + theApp.display->render(); +} + +void systemScreenCapture(int captureNumber) +{ + if(theApp.m_pMainWnd) + ((MainWnd *)theApp.m_pMainWnd)->screenCapture(captureNumber); +} + +u32 systemGetClock() +{ + return GetTickCount(); +} + +void systemMessage(int number, const char *defaultMsg, ...) +{ + CString buffer; + va_list valist; + CString msg = defaultMsg; + if(number) + msg = winResLoadString(number); + + va_start(valist, defaultMsg); + buffer.FormatV(msg, valist); + + theApp.winCheckFullscreen(); + + AfxGetApp()->m_pMainWnd->MessageBox(buffer, winResLoadString(IDS_ERROR), MB_OK|MB_ICONERROR); + + va_end(valist); +} + +void systemSetTitle(const char *title) +{ + if(theApp.m_pMainWnd != NULL) { + AfxGetApp()->m_pMainWnd->SetWindowText(title); + } +} + +void systemShowSpeed(int speed) +{ + systemSpeed = speed; + theApp.showRenderedFrames = theApp.renderedFrames; + theApp.renderedFrames = 0; + if(theApp.videoOption <= VIDEO_4X && theApp.showSpeed) { + CString buffer; + if(theApp.showSpeed == 1) + buffer.Format("VisualBoyAdvance-%3d%%", systemSpeed); + else + buffer.Format("VisualBoyAdvance-%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + + systemSetTitle(buffer); + } +} + +void systemFrame() +{ + if(theApp.aviRecording) + theApp.aviFrameNumber++; + if(theApp.movieRecording || theApp.moviePlaying) + theApp.movieFrame++; +} + +void system10Frames(int rate) +{ + u32 time = systemGetClock(); + if(!theApp.wasPaused && theApp.autoFrameSkip && !theApp.throttle) { + u32 diff = time - theApp.autoFrameSkipLastTime; + int speed = 100; + + if(diff) + speed = (1000000/rate)/diff; + + if(speed >= 98) { + theApp.frameskipadjust++; + + if(theApp.frameskipadjust >= 3) { + theApp.frameskipadjust=0; + if(systemFrameSkip > 0) + systemFrameSkip--; + } + } else { + if(speed < 80) + theApp.frameskipadjust -= (90 - speed)/5; + else if(systemFrameSkip < 9) + theApp.frameskipadjust--; + + if(theApp.frameskipadjust <= -2) { + theApp.frameskipadjust += 2; + if(systemFrameSkip < 9) + systemFrameSkip++; + } + } + } + if(!theApp.wasPaused && theApp.throttle) { + if(!speedup) { + u32 diff = time - theApp.throttleLastTime; + + int target = (1000000/(rate*theApp.throttle)); + int d = (target - diff); + + if(d > 0) { + Sleep(d); + } + } + theApp.throttleLastTime = systemGetClock(); + } + if(theApp.rewindMemory) { + if(++theApp.rewindCounter >= (theApp.rewindTimer)) { + theApp.rewindSaveNeeded = true; + theApp.rewindCounter = 0; + } + } + if(systemSaveUpdateCounter) { + if(--systemSaveUpdateCounter <= SYSTEM_SAVE_NOT_UPDATED) { + ((MainWnd *)theApp.m_pMainWnd)->writeBatteryFile(); + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + } + } + + theApp.wasPaused = false; + theApp.autoFrameSkipLastTime = time; +} + +void systemScreenMessage(const char *msg) +{ + theApp.screenMessage = true; + theApp.screenMessageTime = GetTickCount(); + theApp.screenMessageBuffer = msg; + + if(theApp.screenMessageBuffer.GetLength() > 40) + theApp.screenMessageBuffer = theApp.screenMessageBuffer.Left(40); +} + +void systemUpdateMotionSensor() +{ + if(theApp.input) + theApp.input->checkMotionKeys(); +} + +int systemGetSensorX() +{ + return theApp.sensorX; +} + +int systemGetSensorY() +{ + return theApp.sensorY; +} + +bool systemSoundInit() +{ + if(theApp.sound) + delete theApp.sound; + + theApp.sound = newDirectSound(); + return theApp.sound->init(); +} + + +void systemSoundShutdown() +{ + if(theApp.sound) + delete theApp.sound; + theApp.sound = NULL; +} + +void systemSoundPause() +{ + if(theApp.sound) + theApp.sound->pause(); +} + +void systemSoundReset() +{ + if(theApp.sound) + theApp.sound->reset(); +} + +void systemSoundResume() +{ + if(theApp.sound) + theApp.sound->resume(); +} + +void systemWriteDataToSoundBuffer() +{ + if(theApp.sound) + theApp.sound->write(); +} + +bool systemCanChangeSoundQuality() +{ + return true; +} + +bool systemPauseOnFrame() +{ + if(theApp.winPauseNextFrame) { + theApp.paused = true; + theApp.winPauseNextFrame = false; + return true; + } + return false; +} + +void systemGbBorderOn() +{ + if(emulating && theApp.cartridgeType == 1 && gbBorderOn) { + theApp.updateWindowSize(theApp.videoOption); + } +} + +BOOL VBA::OnIdle(LONG lCount) +{ + if(emulating && debugger) { + MSG msg; + remoteStubMain(); + if(debugger) + return TRUE; // continue loop + return !::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE); + } else if(emulating && active && !paused) { + for(int i = 0; i < 2; i++) { + emulator.emuMain(emulator.emuCount); + if(lanlink.connected&&linkid&&lc.numtransfers==0) lc.CheckConn(); + + if(rewindSaveNeeded && rewindMemory && emulator.emuWriteMemState) { + rewindCount++; + if(rewindCount > 8) + rewindCount = 8; + if(emulator.emuWriteMemState(&rewindMemory[rewindPos*REWIND_SIZE], + REWIND_SIZE)) { + rewindPos = ++rewindPos & 7; + if(rewindCount == 8) + rewindTopPos = ++rewindTopPos & 7; + } + } + + rewindSaveNeeded = false; + } + + if(mouseCounter) { + if(--mouseCounter == 0) { + SetCursor(NULL); + } + } + return TRUE; + } + return FALSE; + + // return CWinApp::OnIdle(lCount); +} + +void VBA::addRecentFile(CString file) +{ + // Do not change recent list if frozen + if(recentFreeze) + return; + int i = 0; + for(i = 0; i < 10; i++) { + if(recentFiles[i].GetLength() == 0) + break; + + if(recentFiles[i].Compare(file) == 0) { + if(i == 0) + return; + CString p = recentFiles[i]; + for(int j = i; j > 0; j--) { + recentFiles[j] = recentFiles[j-1]; + } + recentFiles[0] = p; + return; + } + } + int num = 0; + for(i = 0; i < 10; i++) { + if(recentFiles[i].GetLength() != 0) + num++; + } + if(num == 10) { + num--; + } + + for(i = num; i >= 1; i--) { + recentFiles[i] = recentFiles[i-1]; + } + recentFiles[0] = file; +} + +void VBA::loadSettings() +{ + CString buffer; + + languageOption = regQueryDwordValue("language", 1); + if(languageOption < 0 || languageOption > 2) + languageOption = 1; + + buffer = regQueryStringValue("languageName", ""); + if(!buffer.IsEmpty()) { + languageName = buffer.Left(3); + } else + languageName = ""; + + winSetLanguageOption(languageOption, true); + + frameSkip = regQueryDwordValue("frameSkip", 0); + if(frameSkip < 0 || frameSkip > 9) + frameSkip = 0; + + gbFrameSkip = regQueryDwordValue("gbFrameSkip", 0); + if(gbFrameSkip < 0 || gbFrameSkip > 9) + gbFrameSkip = 0; + + autoFrameSkip = regQueryDwordValue("autoFrameSkip", FALSE) ? TRUE : FALSE; + + vsync = regQueryDwordValue("vsync", false) ? true : false ; + synchronize = regQueryDwordValue("synchronize", TRUE) ? true : false; + fullScreenStretch = regQueryDwordValue("stretch", FALSE) ? true : false; + + videoOption = regQueryDwordValue("video", VIDEO_3X); + + if(videoOption < VIDEO_1X || videoOption > VIDEO_OTHER) + videoOption = VIDEO_3X; + + bool defaultVideoDriver = regQueryDwordValue("defaultVideoDriver", true) ? + true : false; + + if(!regQueryBinaryValue("videoDriverGUID", (char *)&videoDriverGUID, + sizeof(GUID))) { + defaultVideoDriver = TRUE; + } + + if(defaultVideoDriver) + pVideoDriverGUID = NULL; + else + pVideoDriverGUID = &videoDriverGUID; + + fsWidth = regQueryDwordValue("fsWidth", 0); + fsHeight = regQueryDwordValue("fsHeight", 0); + fsColorDepth = regQueryDwordValue("fsColorDepth", 0); + fsFrequency = regQueryDwordValue("fsFrequency", 0); + fsAdapter = regQueryDwordValue("fsAdapter", 0); + + if(videoOption == VIDEO_OTHER) + { + if(fsWidth < 0 || fsWidth > 4095 || fsHeight < 0 || fsHeight > 4095) + videoOption = 0; + if(fsColorDepth != 16 && fsColorDepth != 24 && fsColorDepth != 32) + videoOption = 0; + } + + renderMethod = (DISPLAY_TYPE)regQueryDwordValue("renderMethod", DIRECT_3D); + + if(renderMethod < GDI || renderMethod > OPENGL) + renderMethod = DIRECT_3D; + + windowPositionX = regQueryDwordValue("windowX", 0); + if(windowPositionX < 0) + windowPositionX = 0; + windowPositionY = regQueryDwordValue("windowY", 0); + if(windowPositionY < 0) + windowPositionY = 0; + + useBiosFile = regQueryDwordValue("useBios", 0) ? true: false; + + skipBiosFile = regQueryDwordValue("skipBios", 0) ? true : false; + + buffer = regQueryStringValue("biosFile", ""); + + if(!buffer.IsEmpty()) { + biosFileName = buffer; + } + + int res = regQueryDwordValue("soundEnable", 0x30f); + + soundEnable(res); + soundDisable(~res); + + soundOffFlag = (regQueryDwordValue("soundOff", 0)) ? true : false; + + soundQuality = regQueryDwordValue("soundQuality", 1); + + soundEcho = regQueryDwordValue("soundEcho", 0) ? true : false; + + soundLowPass = regQueryDwordValue("soundLowPass", 0) ? true : false; + + soundReverse = regQueryDwordValue("soundReverse", 0) ? true : false; + + soundVolume = regQueryDwordValue("soundVolume", 0); + if(soundVolume < 0 || soundVolume > 5) + soundVolume = 0; + + soundInterpolation = regQueryDwordValue("soundInterpolation", 0); + if(soundInterpolation < 0 || soundInterpolation > 4) + soundInterpolation = 0; + + ddrawEmulationOnly = regQueryDwordValue("ddrawEmulationOnly", false) ? true : false; + ddrawUseVideoMemory = regQueryDwordValue("ddrawUseVideoMemory", false) ? true : false; + tripleBuffering = regQueryDwordValue("tripleBuffering", true) ? true : false; + + d3dFilter = regQueryDwordValue("d3dFilter", 0); + if(d3dFilter < 0 || d3dFilter > 1) + d3dFilter = 0; + glFilter = regQueryDwordValue("glFilter", 0); + if(glFilter < 0 || glFilter > 1) + glFilter = 0; + glType = regQueryDwordValue("glType", 0); + if(glType < 0 || glType > 1) + glType = 0; + + filterType = regQueryDwordValue("filter", 0); + if(filterType < 0 || filterType > 16) + filterType = 0; + + disableMMX = regQueryDwordValue("disableMMX", 0) ? true: false; + + disableStatusMessage = regQueryDwordValue("disableStatus", 0) ? true : false; + + showSpeed = regQueryDwordValue("showSpeed", 0); + if(showSpeed < 0 || showSpeed > 2) + showSpeed = 0; + + showSpeedTransparent = regQueryDwordValue("showSpeedTransparent", TRUE) ? + TRUE : FALSE; + + winGbPrinterEnabled = regQueryDwordValue("gbPrinter", false) ? true : false; + + if(winGbPrinterEnabled) + gbSerialFunction = gbPrinterSend; + else + gbSerialFunction = NULL; + + pauseWhenInactive = regQueryDwordValue("pauseWhenInactive", 1) ? + true : false; + + useOldSync = regQueryDwordValue("useOldSync", 0) ? + TRUE : FALSE; + + captureFormat = regQueryDwordValue("captureFormat", 0); + + removeIntros = regQueryDwordValue("removeIntros", false) ? true : false; + + recentFreeze = regQueryDwordValue("recentFreeze", false) ? true : false; + + autoIPS = regQueryDwordValue("autoIPS", false) ? true : false; + + cpuDisableSfx = regQueryDwordValue("disableSfx", 0) ? true : false; + + winSaveType = regQueryDwordValue("saveType", 0); + if(winSaveType < 0 || winSaveType > 5) + winSaveType = 0; + + cpuEnhancedDetection = regQueryDwordValue("enhancedDetection", 1) ? true : + false; + + ifbType = regQueryDwordValue("ifbType", 0); + if(ifbType < 0 || ifbType > 2) + ifbType = 0; + + winFlashSize = regQueryDwordValue("flashSize", 0x20000); + if(winFlashSize != 0x10000 && winFlashSize != 0x20000) + winFlashSize = 0x20000; + + agbPrintEnable(regQueryDwordValue("agbPrint", 0) ? true : false); + + winRtcEnable = regQueryDwordValue("rtcEnabled", 1) ? true : false; + rtcEnable(winRtcEnable); + + autoHideMenu = regQueryDwordValue("autoHideMenu", 0) ? true : false; + + switch(videoOption) { + case VIDEO_320x240: + fsWidth = 320; + fsHeight = 240; + fsColorDepth = 16; + break; + case VIDEO_640x480: + fsWidth = 640; + fsHeight = 480; + fsColorDepth = 16; + break; + case VIDEO_800x600: + fsWidth = 800; + fsHeight = 600; + fsColorDepth = 16; + break; + case VIDEO_1024x768: + fsWidth = 1024; + fsHeight = 768; + fsColorDepth = 32; + break; + case VIDEO_1280x1024: + fsWidth = 1280; + fsHeight = 1024; + fsColorDepth = 32; + break; + } + + winGbBorderOn = regQueryDwordValue("borderOn", 0); + gbBorderAutomatic = regQueryDwordValue("borderAutomatic", 0); + gbEmulatorType = regQueryDwordValue("emulatorType", 4); + if(gbEmulatorType < 0 || gbEmulatorType > 5) + gbEmulatorType = 4; + gbColorOption = regQueryDwordValue("colorOption", 0); + + threadPriority = regQueryDwordValue("priority", 2); + + if(threadPriority < 0 || threadPriority >3) + threadPriority = 2; + updatePriority(); + + autoSaveLoadCheatList = regQueryDwordValue("autoSaveCheatList", 0) ? + true : false; + + gbPaletteOption = regQueryDwordValue("gbPaletteOption", 0); + if(gbPaletteOption < 0) + gbPaletteOption = 0; + if(gbPaletteOption > 2) + gbPaletteOption = 2; + + regQueryBinaryValue("gbPalette", (char *)systemGbPalette, + 24*sizeof(u16)); + + rewindTimer = regQueryDwordValue("rewindTimer", 0); + + if(rewindTimer < 0 || rewindTimer > 600) + rewindTimer = 0; + + rewindTimer *= 6; // convert to 10 frames multiple + + if(rewindTimer != 0) + rewindMemory = (char *)malloc(8*REWIND_SIZE); + + for(int i = 0; i < 10; i++) { + buffer.Format("recent%d", i); + char *s = regQueryStringValue(buffer, NULL); + if(s == NULL) + break; + recentFiles[i] = s; + } + + joypadDefault = regQueryDwordValue("joypadDefault", 0); + if(joypadDefault < 0 || joypadDefault > 3) + joypadDefault = 0; + + autoLoadMostRecent = regQueryDwordValue("autoLoadMostRecent", false) ? true : + false; + + cheatsEnabled = regQueryDwordValue("cheatsEnabled", true) ? true : false; + + fsMaxScale = regQueryDwordValue("fsMaxScale", 0); + + throttle = regQueryDwordValue("throttle", 0); + if(throttle < 5 || throttle > 1000) + throttle = 0; + + linktimeout = regQueryDwordValue("LinkTimeout", 1000); + + linklog = regQueryDwordValue("Linklog", false) ? true : false; + + adapter = regQueryDwordValue("RFU", false) ? true : false; + + lanlink.active = regQueryDwordValue("LAN", 0) ? true : false; +} + +void VBA::updateFrameSkip() +{ + switch(cartridgeType) { + case 0: + systemFrameSkip = frameSkip; + break; + case 1: + systemFrameSkip = gbFrameSkip; + break; + } +} + +void VBA::updateVideoSize(UINT id) +{ + int value = 0; + + switch(id) { + case ID_OPTIONS_VIDEO_X1: + value = VIDEO_1X; + break; + case ID_OPTIONS_VIDEO_X2: + value = VIDEO_2X; + break; + case ID_OPTIONS_VIDEO_X3: + value = VIDEO_3X; + break; + case ID_OPTIONS_VIDEO_X4: + value = VIDEO_4X; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN320X240: + value = VIDEO_320x240; + fsWidth = 320; + fsHeight = 240; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN640X480: + value = VIDEO_640x480; + fsWidth = 640; + fsHeight = 480; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN800X600: + value = VIDEO_800x600; + fsWidth = 800; + fsHeight = 600; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN1024X768: + value = VIDEO_1024x768; + fsWidth = 1024; + fsHeight = 768; + fsColorDepth = 32; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN1280X1024: + value = VIDEO_1280x1024; + fsWidth = 1280; + fsHeight = 1024; + fsColorDepth = 32; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN: + value = VIDEO_OTHER; + break; + } + + if(videoOption == value && value != VIDEO_OTHER) + return; + + updateWindowSize(value); +} + +typedef BOOL (WINAPI *GETMENUBARINFO)(HWND, LONG, LONG, PMENUBARINFO); + +static void winCheckMenuBarInfo(int& winSizeX, int& winSizeY) +{ + HINSTANCE hinstDll; + DWORD dwVersion = 0; + + hinstDll = LoadLibrary("USER32.DLL"); + + if(hinstDll) { + GETMENUBARINFO func = (GETMENUBARINFO)GetProcAddress(hinstDll, + "GetMenuBarInfo"); + + if(func) { + MENUBARINFO info; + info.cbSize = sizeof(info); + + func(AfxGetMainWnd()->GetSafeHwnd(), OBJID_MENU, 0, &info); + + int menuHeight = GetSystemMetrics(SM_CYMENU); + + if((info.rcBar.bottom - info.rcBar.top) > menuHeight) { + winSizeY += (info.rcBar.bottom - info.rcBar.top) - menuHeight + 1; + theApp.m_pMainWnd->SetWindowPos( + 0, //HWND_TOPMOST, + theApp.windowPositionX, + theApp.windowPositionY, + winSizeX, + winSizeY, + SWP_NOMOVE | SWP_SHOWWINDOW); + } + } + FreeLibrary(hinstDll); + } +} + +void VBA::updateWindowSize(int value) +{ + regSetDwordValue("video", value); + + if(value == VIDEO_OTHER) { + regSetDwordValue("fsWidth", fsWidth); + regSetDwordValue("fsHeight", fsHeight); + regSetDwordValue("fsColorDepth", fsColorDepth); + } + + if(((value >= VIDEO_320x240) && + (videoOption != value)) || + (videoOption >= VIDEO_320x240 && + value <= VIDEO_4X) || + fsForceChange) { + fsForceChange = false; + changingVideoSize = true; + shutdownDisplay(); + if(input) { + delete input; + input = NULL; + } + m_pMainWnd->DragAcceptFiles(FALSE); + CWnd *pWnd = m_pMainWnd; + m_pMainWnd = NULL; + pWnd->DestroyWindow(); + delete pWnd; + videoOption = value; + if(!initDisplay()) { + if(videoOption == VIDEO_320x240 || + videoOption == VIDEO_640x480 || + videoOption == VIDEO_800x600 || + videoOption == VIDEO_1024x768 || + videoOption == VIDEO_1280x1024 || + videoOption == VIDEO_OTHER) { + regSetDwordValue("video", VIDEO_1X); + if(pVideoDriverGUID) + regSetDwordValue("defaultVideoDriver", TRUE); + } + changingVideoSize = false; + AfxPostQuitMessage(0); + return; + } + if(!initInput()) { + changingVideoSize = false; + AfxPostQuitMessage(0); + return; + } + input->checkKeys(); + updateMenuBar(); + changingVideoSize = FALSE; + updateWindowSize(videoOption); + return; + } + + sizeX = 240; + sizeY = 160; + + videoOption = value; + + if(cartridgeType == 1) { + if(gbBorderOn) { + sizeX = 256; + sizeY = 224; + gbBorderLineSkip = 256; + gbBorderColumnSkip = 48; + gbBorderRowSkip = 40; + } else { + sizeX = 160; + sizeY = 144; + gbBorderLineSkip = 160; + gbBorderColumnSkip = 0; + gbBorderRowSkip = 0; + } + } + + surfaceSizeX = sizeX; + surfaceSizeY = sizeY; + + switch(videoOption) { + case VIDEO_1X: + surfaceSizeX = sizeX; + surfaceSizeY = sizeY; + break; + case VIDEO_2X: + surfaceSizeX = sizeX * 2; + surfaceSizeY = sizeY * 2; + break; + case VIDEO_3X: + surfaceSizeX = sizeX * 3; + surfaceSizeY = sizeY * 3; + break; + case VIDEO_4X: + surfaceSizeX = sizeX * 4; + surfaceSizeY = sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_1024x768: + case VIDEO_1280x1024: + case VIDEO_OTHER: + { + float scaleX = 1; + float scaleY = 1; + scaleX = ((float)fsWidth / sizeX); + scaleY = ((float)fsHeight / sizeY); + float min = scaleX < scaleY ? scaleX : scaleY; + if(fsMaxScale) + min = min > fsMaxScale ? fsMaxScale : min; + surfaceSizeX = (int)(min * sizeX); + surfaceSizeY = (int)(min * sizeY); + + if(fullScreenStretch) + { + surfaceSizeX = fsWidth; + surfaceSizeY = fsHeight; + } + } + break; + } + + rect.right = sizeX; + rect.bottom = sizeY; + + int winSizeX = sizeX; + int winSizeY = sizeY; + + if(videoOption <= VIDEO_4X) { + dest.left = 0; + dest.top = 0; + dest.right = surfaceSizeX; + dest.bottom = surfaceSizeY; + + DWORD style = WS_POPUP | WS_VISIBLE; + + style |= WS_OVERLAPPEDWINDOW; + + menuToggle = TRUE; + AdjustWindowRectEx(&dest, style, TRUE, 0); //WS_EX_TOPMOST); + + winSizeX = dest.right-dest.left; + winSizeY = dest.bottom-dest.top; + + m_pMainWnd->SetWindowPos(0, //HWND_TOPMOST, + windowPositionX, + windowPositionY, + winSizeX, + winSizeY, + SWP_NOMOVE | SWP_SHOWWINDOW); + + winCheckMenuBarInfo(winSizeX, winSizeY); + } + + adjustDestRect(); + + updateIFB(); + updateFilter(); + + m_pMainWnd->RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN); +} + +bool VBA::initDisplay() +{ + return updateRenderMethod(false); +} + +bool VBA::updateRenderMethod(bool force) +{ + bool res = updateRenderMethod0(force); + + while(!res && renderMethod > 0) { + if(renderMethod == OPENGL) + renderMethod = DIRECT_3D; + else if(renderMethod == DIRECT_3D) + renderMethod = DIRECT_DRAW; + else if(renderMethod == DIRECT_DRAW) { + if(videoOption > VIDEO_4X) { + videoOption = VIDEO_2X; + force = true; + } else + renderMethod = GDI; + } + + res = updateRenderMethod(force); + } + return res; +} + +bool VBA::updateRenderMethod0(bool force) +{ + bool initInput = false; + b16to32Video = false; + + if(display) { + if(display->getType() != renderMethod || force) { + initInput = true; + changingVideoSize = true; + shutdownDisplay(); + if(input) { + delete input; + input = NULL; + } + CWnd *pWnd = m_pMainWnd; + + m_pMainWnd = NULL; + pWnd->DragAcceptFiles(FALSE); + pWnd->DestroyWindow(); + delete pWnd; + + display = NULL; + regSetDwordValue("renderMethod", renderMethod); + } + } + if(display == NULL) { + switch(renderMethod) { + case GDI: + display = newGDIDisplay(); + break; + case DIRECT_DRAW: + display = newDirectDrawDisplay(); + break; + case DIRECT_3D: + display = newDirect3DDisplay(); + break; + case OPENGL: + display = newOpenGLDisplay(); + break; + } + + if(display->initialize()) { + if(initInput) { + if(!this->initInput()) { + changingVideoSize = false; + AfxPostQuitMessage(0); + return false; + } + input->checkKeys(); + updateMenuBar(); + changingVideoSize = false; + updateWindowSize(videoOption); + + m_pMainWnd->ShowWindow(SW_SHOW); + m_pMainWnd->UpdateWindow(); + m_pMainWnd->SetFocus(); + + return true; + } else { + changingVideoSize = false; + return true; + } + } + changingVideoSize = false; + } + return true; +} + +void VBA::winCheckFullscreen() +{ + if(videoOption > VIDEO_4X && tripleBuffering) { + if(display) + display->checkFullScreen(); + } +} + +void VBA::shutdownDisplay() +{ + if(display != NULL) { + display->cleanup(); + delete display; + display = NULL; + } +} + +void VBA::directXMessage(const char *msg) +{ + systemMessage(IDS_DIRECTX_7_REQUIRED, + "DirectX 7.0 or greater is required to run.\nDownload at http://www.microsoft.com/directx.\n\nError found at: %s", + msg); +} + +void VBA::updatePriority() +{ + switch(threadPriority) { + case 0: + SetThreadPriority(THREAD_PRIORITY_HIGHEST); + break; + case 1: + SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL); + break; + case 3: + SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL); + break; + default: + SetThreadPriority(THREAD_PRIORITY_NORMAL); + } +} + +#ifdef MMX +bool VBA::detectMMX() +{ + bool support = false; + char brand[13]; + + // check for Intel chip + __try { + __asm { + mov eax, 0; + cpuid; + mov [dword ptr brand+0], ebx; + mov [dword ptr brand+4], edx; + mov [dword ptr brand+8], ecx; + } + } + __except(EXCEPTION_EXECUTE_HANDLER) { + if(_exception_code() == STATUS_ILLEGAL_INSTRUCTION) { + return false; + } + return false; + } + // Check for Intel or AMD CPUs + if(strncmp(brand, "GenuineIntel", 12)) { + if(strncmp(brand, "AuthenticAMD", 12)) { + return false; + } + } + + __asm { + mov eax, 1; + cpuid; + test edx, 00800000h; + jz NotFound; + mov [support], 1; + NotFound: + } + return support; +} +#endif + +void VBA::winSetLanguageOption(int option, bool force) +{ + if(((option == languageOption) && option != 2) && !force) + return; + switch(option) { + case 0: + { + char lbuffer[10]; + + if(GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) { + HINSTANCE l = winLoadLanguage(lbuffer); + if(l == NULL) { + LCID locIdBase = MAKELCID( MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANG_NEUTRAL ), SORT_DEFAULT ); + if(GetLocaleInfo(locIdBase, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) { + l = winLoadLanguage(lbuffer); + if(l == NULL) { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + lbuffer); + return; + } + } + } + AfxSetResourceHandle(l); + if(languageModule != NULL) + FreeLibrary(languageModule); + languageModule = l; + } else { + systemMessage(IDS_FAILED_TO_GET_LOCINFO, + "Failed to get locale information"); + return; + } + } + break; + case 1: + if(languageModule != NULL) + FreeLibrary(languageModule); + languageModule = NULL; + AfxSetResourceHandle(AfxGetInstanceHandle()); + break; + case 2: + { + if(!force) { + LangSelect dlg; + if(dlg.DoModal()) { + HINSTANCE l = winLoadLanguage(languageName); + if(l == NULL) { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + languageName); + return; + } + AfxSetResourceHandle(l); + if(languageModule != NULL) + FreeLibrary(languageModule); + languageModule = l; + } + } else { + if(languageName.IsEmpty()) + return; + HINSTANCE l = winLoadLanguage(languageName); + if(l == NULL) { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + languageName); + return; + } + AfxSetResourceHandle(l); + if(languageModule != NULL) + FreeLibrary(languageModule); + languageModule = l; + } + } + break; + } + languageOption = option; + updateMenuBar(); +} + +HINSTANCE VBA::winLoadLanguage(const char *name) +{ + CString buffer; + + buffer.Format("vba_%s.dll", name); + + HINSTANCE l = LoadLibrary(buffer); + + if(l == NULL) { + if(strlen(name) == 3) { + char buffer2[3]; + buffer2[0] = name[0]; + buffer2[1] = name[1]; + buffer2[2] = 0; + buffer.Format("vba_%s.dll", buffer2); + + return LoadLibrary(buffer); + } + } + return l; +} + + +bool VBA::initInput() +{ + if(input) + delete input; + input = newDirectInput(); + if(input->initialize()) { + input->loadSettings(); + input->checkKeys(); + return true; + } + delete input; + return false; +} + +void VBA::winAddUpdateListener(IUpdateListener *l) +{ + updateList.AddTail(l); + updateCount++; +} + +void VBA::winRemoveUpdateListener(IUpdateListener *l) +{ + POSITION pos = updateList.Find(l); + if(pos) { + updateList.RemoveAt(pos); + updateCount--; + if(updateCount < 0) + updateCount = 0; + } +} + +CString VBA::winLoadFilter(UINT id) +{ + CString res = winResLoadString(id); + res.Replace('_','|'); + + return res; +} + +void VBA::movieReadNext() +{ + if(movieFile) { + bool movieEnd = false; + + if(fread(&moviePlayFrame, 1, sizeof(int), movieFile) == sizeof(int)) { + if(fread(&movieNextJoypad, 1, sizeof(u32), movieFile) == sizeof(int)) { + // make sure we don't have spurious entries on the movie that can + // cause us to play it forever + if(moviePlayFrame <= movieFrame) + movieEnd = true; + } else + movieEnd = true; + } else + movieEnd = true; + if(movieEnd) { + CString string = winResLoadString(IDS_END_OF_MOVIE); + systemScreenMessage(string); + moviePlaying = false; + fclose(movieFile); + movieFile = NULL; + return; + } + } else + moviePlaying = false; +} + +void VBA::saveSettings() +{ + regSetDwordValue("language", languageOption); + + regSetStringValue("languageName", languageName); + + regSetDwordValue("frameSkip", frameSkip); + + regSetDwordValue("gbFrameSkip", gbFrameSkip); + + regSetDwordValue("autoFrameSkip", autoFrameSkip); + + regSetDwordValue("vsync", vsync); + regSetDwordValue("synchronize", synchronize); + regSetDwordValue("stretch", fullScreenStretch); + + regSetDwordValue("video", videoOption); + + regSetDwordValue("defaultVideoDriver", pVideoDriverGUID == NULL); + + if(pVideoDriverGUID) { + regSetBinaryValue("videoDriverGUID", (char *)&videoDriverGUID, + sizeof(GUID)); + } + + + regSetDwordValue("fsWidth", fsWidth); + regSetDwordValue("fsHeight", fsHeight); + regSetDwordValue("fsColorDepth", fsColorDepth); + regSetDwordValue("fsFrequency", fsFrequency); + regSetDwordValue("fsAdapter", fsAdapter); + + regSetDwordValue("renderMethod", renderMethod); + + regSetDwordValue("windowX", windowPositionX); + regSetDwordValue("windowY", windowPositionY); + + regSetDwordValue("useBios", useBiosFile); + + regSetDwordValue("skipBios", skipBiosFile); + + if(!biosFileName.IsEmpty()) + regSetStringValue("biosFile", biosFileName); + + regSetDwordValue("soundEnable", soundGetEnable() & 0x30f); + + regSetDwordValue("soundOff", soundOffFlag); + + regSetDwordValue("soundQuality", soundQuality); + + regSetDwordValue("soundEcho", soundEcho); + + regSetDwordValue("soundLowPass", soundLowPass); + + regSetDwordValue("soundReverse", soundReverse); + + regSetDwordValue("soundVolume", soundVolume); + + regSetDwordValue("soundInterpolation", soundInterpolation); + + regSetDwordValue("ddrawEmulationOnly", ddrawEmulationOnly); + regSetDwordValue("ddrawUseVideoMemory", ddrawUseVideoMemory); + regSetDwordValue("tripleBuffering", tripleBuffering); + + regSetDwordValue("d3dFilter", d3dFilter); + regSetDwordValue("glFilter", glFilter); + regSetDwordValue("glType", glType); + + regSetDwordValue("filter", filterType); + + regSetDwordValue("LCDFilter", filterLCD); + + regSetDwordValue("disableMMX", disableMMX); + + regSetDwordValue("disableStatus", disableStatusMessage); + + regSetDwordValue("showSpeed", showSpeed); + + regSetDwordValue("showSpeedTransparent", showSpeedTransparent); + + regSetDwordValue("gbPrinter", winGbPrinterEnabled); + + regSetDwordValue("pauseWhenInactive", pauseWhenInactive); + + regSetDwordValue("useOldSync", useOldSync); + + regSetDwordValue("captureFormat", captureFormat); + + regSetDwordValue("removeIntros", removeIntros); + + regSetDwordValue("recentFreeze", recentFreeze); + + regSetDwordValue("autoIPS", autoIPS); + + regSetDwordValue("disableSfx", cpuDisableSfx); + + regSetDwordValue("saveType", winSaveType); + + regSetDwordValue("enhancedDetection", cpuEnhancedDetection); + + regSetDwordValue("ifbType", ifbType); + + regSetDwordValue("flashSize", winFlashSize); + + regSetDwordValue("agbPrint", agbPrintIsEnabled()); + + regSetDwordValue("rtcEnabled", winRtcEnable); + + regSetDwordValue("autoHideMenu", autoHideMenu); + + regSetDwordValue("borderOn", winGbBorderOn); + regSetDwordValue("borderAutomatic", gbBorderAutomatic); + regSetDwordValue("emulatorType", gbEmulatorType); + regSetDwordValue("colorOption", gbColorOption); + + regSetDwordValue("priority", threadPriority); + + regSetDwordValue("autoSaveCheatList", autoSaveLoadCheatList); + + regSetDwordValue("gbPaletteOption", gbPaletteOption); + + regSetBinaryValue("gbPalette", (char *)systemGbPalette, + 24*sizeof(u16)); + + regSetDwordValue("rewindTimer", rewindTimer/6); + + CString buffer; + for(int i = 0; i < 10; i++) { + buffer.Format("recent%d", i); + regSetStringValue(buffer, recentFiles[i]); + } + + regSetDwordValue("joypadDefault", joypadDefault); + regSetDwordValue("autoLoadMostRecent", autoLoadMostRecent); + regSetDwordValue("cheatsEnabled", cheatsEnabled); + regSetDwordValue("fsMaxScale", fsMaxScale); + regSetDwordValue("throttle", throttle); + + + regSetDwordValue("LinkTimeout", linktimeout); + regSetDwordValue("Linklog", linklog); + regSetDwordValue("RFU", adapter); +} + +void winSignal(int, int) +{ +} + +#define CPUReadByteQuick(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +void winOutput(char *s, u32 addr) +{ + if(s) { + toolsLog(s); + } else { + CString str; + char c; + + c = CPUReadByteQuick(addr); + addr++; + while(c) { + str += c; + c = CPUReadByteQuick(addr); + addr++; + } + toolsLog(str); + } +} diff --git a/src/win32/VBA.h b/src/win32/VBA.h new file mode 100644 index 00000000..ee1916ca --- /dev/null +++ b/src/win32/VBA.h @@ -0,0 +1,280 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// VBA.h : main header file for the VBA application +// + +#if !defined(AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_) +#define AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#ifndef __AFXWIN_H__ +#error include 'stdafx.h' before including this file for PCH +#endif + +#include + +#include "AcceleratorManager.h" +#include "..\..\res\resource.h" // main symbols +#include "Display.h" +#include "Input.h" +#include "IUpdate.h" +#include "Sound.h" +#include "../System.h" + +///////////////////////////////////////////////////////////////////////////// +// VBA: +// See VBA.cpp for the implementation of this class +// + +enum { + VIDEO_1X, VIDEO_2X, VIDEO_3X, VIDEO_4X, + VIDEO_320x240, VIDEO_640x480, VIDEO_800x600, VIDEO_1024x768, VIDEO_1280x1024, + VIDEO_OTHER +}; + +enum pixelFilterType +{ + FILTER_NONE, + + FILTER_SIMPLE2X, FILTER_PIXELATE, FILTER_TVMODE, FILTER_SCANLINES, + FILTER_BILINEAR, FILTER_BILINEARPLUS, FILTER_MAMESCALE2X, + FILTER_2XSAI, FILTER_SUPER2XSAI, FILTER_SUPEREAGLE, FILTER_LQ2X, FILTER_HQ2X, + + FILTER_SIMPLE3X, FILTER_HQ3X, + + FILTER_SIMPLE4X, FILTER_HQ4X +}; + +#define REWIND_SIZE 400000 + +class AVIWrite; +class WavWriter; + +class VBA : public CWinApp +{ + public: + CMenu m_menu; + HMENU menu; + HMENU popup; + bool mode320Available; + bool mode640Available; + bool mode800Available; + bool mode1024Available; + bool mode1280Available; + int windowPositionX; + int windowPositionY; + void (*filterFunction)(u8*,u32,u8*,u8*,u32,int,int); + void (*ifbFunction)(u8*,u32,int,int); + int ifbType; + int filterType; + int filterWidth; + int filterHeight; + int filterMagnification; + int filterLCD; + int fsWidth; + int fsHeight; + int fsColorDepth; + int fsFrequency; + int fsAdapter; + bool fsForceChange; + int sizeX; + int sizeY; + int surfaceSizeX; + int surfaceSizeY; + int videoOption; + bool fullScreenStretch; + bool disableStatusMessage; + int showSpeed; + BOOL showSpeedTransparent; + int showRenderedFrames; + bool screenMessage; + CString screenMessageBuffer; + DWORD screenMessageTime; + u8 *delta[257*244*4]; + bool menuToggle; + IDisplay *display; + int cartridgeType; + bool soundInitialized; + bool useBiosFile; + bool skipBiosFile; + CString biosFileName; + bool active; + bool paused; + CString recentFiles[10]; + bool recentFreeze; + bool autoSaveLoadCheatList; + FILE *winout; + bool removeIntros; + bool autoIPS; + int winGbBorderOn; + int winFlashSize; + bool winRtcEnable; + int winSaveType; + char *rewindMemory; + int rewindPos; + int rewindTopPos; + int rewindCounter; + int rewindCount; + bool rewindSaveNeeded; + int rewindTimer; + int captureFormat; + bool tripleBuffering; + bool autoHideMenu; + int throttle; + u32 throttleLastTime; + u32 autoFrameSkipLastTime; + bool autoFrameSkip; + bool vsync; + bool changingVideoSize; + GUID videoDriverGUID; + GUID *pVideoDriverGUID; + DISPLAY_TYPE renderMethod; + bool iconic; + bool ddrawEmulationOnly; + bool ddrawUsingEmulationOnly; + bool ddrawDebug; + bool ddrawUseVideoMemory; + int d3dFilter; + int glFilter; + int glType; + bool dinputKeyFocus; + bool pauseWhenInactive; + bool speedupToggle; + bool useOldSync; + bool winGbPrinterEnabled; + int threadPriority; + bool disableMMX; + int languageOption; + CString languageName; + HINSTANCE languageModule; + int renderedFrames; + Input *input; + int joypadDefault; + int autoFire; + bool autoFireToggle; + bool winPauseNextFrame; + bool soundRecording; + WavWriter *soundRecorder; + CString soundRecordName; + ISound *sound; + bool aviRecording; + AVIWrite *aviRecorder; + CString aviRecordName; + int aviFrameNumber; + bool painting; + bool movieRecording; + bool moviePlaying; + int movieFrame; + int moviePlayFrame; + FILE *movieFile; + u32 movieLastJoypad; + u32 movieNextJoypad; + int sensorX; + int sensorY; + int mouseCounter; + bool wasPaused; + int frameskipadjust; + bool autoLoadMostRecent; + int fsMaxScale; + int romSize; + + CList updateList; + int updateCount; + + CAcceleratorManager winAccelMgr; + HACCEL hAccel; + + RECT rect; + RECT dest; + + struct EmulatedSystem emulator; + + CString szFile; + CString filename; + CString dir; + + CString wndClass; + + public: + VBA(); + ~VBA(); + + void adjustDestRect(); + void updateIFB(); + void updateFilter(); + void updateMenuBar(); + void winAddUpdateListener(IUpdateListener *l); + void winRemoveUpdateListener(IUpdateListener *l); + CString winLoadFilter(UINT id); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VBA) + public: + virtual BOOL InitInstance(); + virtual BOOL OnIdle(LONG lCount); + //}}AFX_VIRTUAL + + // Implementation + + public: + void saveSettings(); + void movieReadNext(); + bool initInput(); + HINSTANCE winLoadLanguage(const char *name); + void winSetLanguageOption(int option, bool force); + bool detectMMX(); +#ifdef MMX +#endif + void updatePriority(); + void directXMessage(const char *msg); + void shutdownDisplay(); + void winCheckFullscreen(); + bool updateRenderMethod0(bool force); + bool updateRenderMethod(bool force); + bool initDisplay(); + void updateWindowSize(int value); + void updateVideoSize(UINT id); + void updateFrameSkip(); + void loadSettings(); + void addRecentFile(CString file); + //{{AFX_MSG(VBA) + afx_msg void OnAppAbout(); + // NOTE - the ClassWizard will add and remove member functions here. + // DO NOT EDIT what you see in these blocks of generated code ! + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + extern VBA theApp; + +#ifdef MMX + extern "C" bool cpu_mmx; +#endif + + ///////////////////////////////////////////////////////////////////////////// + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_) diff --git a/src/win32/VideoMode.cpp b/src/win32/VideoMode.cpp new file mode 100644 index 00000000..3e084ddc --- /dev/null +++ b/src/win32/VideoMode.cpp @@ -0,0 +1,365 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// VideoMode.cpp : implementation file +// + +#include "stdafx.h" +#include "VBA.h" + +#define DIRECTDRAW_VERSION 0x0700 +#include + +#include "VideoMode.h" + +#include "../System.h" +#include "..\..\res\resource.h" + + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +#define MAX_DRIVERS 32 // 32 drivers maximum + +//----------------------------------------------------------------------------- +// Local structures +//----------------------------------------------------------------------------- +// Keeps data on the available DDraw drivers +struct +{ + char szDescription[128]; + char szName[128]; + GUID *pGUID; + GUID GUIDcopy; + HMONITOR hm; +} Drivers[MAX_DRIVERS]; + +//----------------------------------------------------------------------------- +// Local data +//----------------------------------------------------------------------------- +static int gDriverCnt = 0; // Total number of drivers +static GUID *gpSelectedDriverGUID; + +//----------------------------------------------------------------------------- +// Name: DDEnumCallbackEx() +// Desc: This call back is used to determine the existing available DDraw +// devices, so the user can pick which one to run on. +//----------------------------------------------------------------------------- +BOOL WINAPI +DDEnumCallbackEx(GUID *pGUID, LPSTR pDescription, LPSTR pName, LPVOID pContext, HMONITOR hm) +{ + if (pGUID) + { + Drivers[gDriverCnt].GUIDcopy = *pGUID; + Drivers[gDriverCnt].pGUID = &Drivers[gDriverCnt].GUIDcopy; + } + else + Drivers[gDriverCnt].pGUID = NULL; + Drivers[gDriverCnt].szDescription[127] = '\0'; + Drivers[gDriverCnt].szName[127] = '\0'; + strncpy(Drivers[gDriverCnt].szDescription,pDescription,127); + strncpy(Drivers[gDriverCnt].szName,pName,127); + Drivers[gDriverCnt].hm = hm; + if (gDriverCnt < MAX_DRIVERS) + gDriverCnt++; + else + return DDENUMRET_CANCEL; + return DDENUMRET_OK; +} + + + + +//----------------------------------------------------------------------------- +// Name: DDEnumCallback() +// Desc: This callback is used only with old versions of DDraw. +//----------------------------------------------------------------------------- +BOOL WINAPI +DDEnumCallback(GUID *pGUID, LPSTR pDescription, LPSTR pName, LPVOID context) +{ + return (DDEnumCallbackEx(pGUID, pDescription, pName, context, NULL)); +} + +static HRESULT WINAPI addVideoMode(LPDDSURFACEDESC2 surf, LPVOID lpContext) +{ + HWND h = (HWND)lpContext; + char buffer[50]; + + switch(surf->ddpfPixelFormat.dwRGBBitCount) { + case 16: + case 24: + case 32: + if(surf->dwWidth >= 640 && surf->dwHeight >= 480) { + sprintf(buffer, "%4dx%4dx%2d", surf->dwWidth, surf->dwHeight, + surf->ddpfPixelFormat.dwRGBBitCount); + int pos = ::SendMessage(h, LB_ADDSTRING, 0, (LPARAM)buffer); + ::SendMessage(h, LB_SETITEMDATA, pos, + (surf->ddpfPixelFormat.dwRGBBitCount << 24) | + ((surf->dwWidth & 4095) << 12) | + (surf->dwHeight & 4095)); + } + } + + return DDENUMRET_OK; +} + +int winVideoModeSelect(CWnd *pWnd, GUID **guid) +{ + HINSTANCE h = LoadLibrary("ddraw.dll"); + + // If ddraw.dll doesn't exist in the search path, + // then DirectX probably isn't installed, so fail. + if (!h) + return -1; + + gDriverCnt = 0; + + // Note that you must know which version of the + // function to retrieve (see the following text). + // For this example, we use the ANSI version. + LPDIRECTDRAWENUMERATEEX lpDDEnumEx; + lpDDEnumEx = (LPDIRECTDRAWENUMERATEEX) + GetProcAddress(h,"DirectDrawEnumerateExA"); + + // If the function is there, call it to enumerate all display + // devices attached to the desktop, and any non-display DirectDraw + // devices. + if (lpDDEnumEx) + lpDDEnumEx(DDEnumCallbackEx, NULL, + DDENUM_ATTACHEDSECONDARYDEVICES | + DDENUM_NONDISPLAYDEVICES + ); + else { + /* + * We must be running on an old version of DirectDraw. + * Therefore MultiMon isn't supported. Fall back on + * DirectDrawEnumerate to enumerate standard devices on a + * single-monitor system. + */ + BOOL (WINAPI *lpDDEnum)(LPDDENUMCALLBACK, LPVOID); + + lpDDEnum = (BOOL (WINAPI *)(LPDDENUMCALLBACK, LPVOID)) + GetProcAddress(h, "DirectDrawEnumerateA"); + if(lpDDEnum) + lpDDEnum(DDEnumCallback,NULL); + + /* Note that it could be handy to let the OldCallback function + * be a wrapper for a DDEnumCallbackEx. + * + * Such a function would look like: + * BOOL FAR PASCAL OldCallback(GUID FAR *lpGUID, + * LPSTR pDesc, + * LPSTR pName, + * LPVOID pContext) + * { + * return Callback(lpGUID,pDesc,pName,pContext,NULL); + * } + */ + } + + int selected = 0; + + if(gDriverCnt > 1) { + VideoDriverSelect d(pWnd); + + selected = d.DoModal(); + + if(selected == -1) { + // If the library was loaded by calling LoadLibrary(), + // then you must use FreeLibrary() to let go of it. + FreeLibrary(h); + + return -1; + } + } + + HRESULT (WINAPI *DDrawCreateEx)(GUID *,LPVOID *,REFIID,IUnknown *); + DDrawCreateEx = (HRESULT (WINAPI *)(GUID *,LPVOID *,REFIID,IUnknown *)) + GetProcAddress(h, "DirectDrawCreateEx"); + + LPDIRECTDRAW7 ddraw = NULL; + if(DDrawCreateEx) { + HRESULT hret = DDrawCreateEx(Drivers[selected].pGUID, + (void **)&ddraw, + IID_IDirectDraw7, + NULL); + if(hret != DD_OK) { + systemMessage(0, "Error during DirectDrawCreateEx: %08x", hret); + FreeLibrary(h); + return -1; + } + } else { + // should not happen.... + systemMessage(0, "Error getting DirectDrawCreateEx"); + FreeLibrary(h); + return -1; + } + + VideoMode dlg(ddraw, pWnd); + + int res = dlg.DoModal(); + + if(res != -1) { + *guid = Drivers[selected].pGUID; + } + ddraw->Release(); + ddraw = NULL; + + // If the library was loaded by calling LoadLibrary(), + // then you must use FreeLibrary() to let go of it. + FreeLibrary(h); + + return res; +} + +///////////////////////////////////////////////////////////////////////////// +// VideoMode dialog + + +VideoMode::VideoMode(LPDIRECTDRAW7 pDraw, CWnd* pParent /*=NULL*/) + : CDialog(VideoMode::IDD, pParent) +{ + //{{AFX_DATA_INIT(VideoMode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + pDirectDraw = pDraw; +} + + +void VideoMode::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(VideoMode) + DDX_Control(pDX, IDC_MODES, m_modes); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(VideoMode, CDialog) + //{{AFX_MSG_MAP(VideoMode) + ON_LBN_SELCHANGE(IDC_MODES, OnSelchangeModes) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// VideoMode message handlers + +void VideoMode::OnSelchangeModes() +{ + int item = m_modes.GetCurSel(); + + GetDlgItem(ID_OK)->EnableWindow(item != -1); +} + +void VideoMode::OnCancel() +{ + EndDialog(-1); +} + +void VideoMode::OnOk() +{ + int cur = m_modes.GetCurSel(); + + if(cur != -1) { + cur = m_modes.GetItemData(cur); + } + EndDialog(cur); +} + +BOOL VideoMode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + // check for available fullscreen modes + pDirectDraw->EnumDisplayModes(DDEDM_STANDARDVGAMODES, NULL, m_modes.m_hWnd, + addVideoMode); + + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect dialog + + +VideoDriverSelect::VideoDriverSelect(CWnd* pParent /*=NULL*/) + : CDialog(VideoDriverSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(VideoDriverSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void VideoDriverSelect::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(VideoDriverSelect) + DDX_Control(pDX, IDC_DRIVERS, m_drivers); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(VideoDriverSelect, CDialog) + //{{AFX_MSG_MAP(VideoDriverSelect) + ON_BN_CLICKED(ID_OK, OnOk) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_LBN_SELCHANGE(IDC_DRIVERS, OnSelchangeDrivers) + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect message handlers + +void VideoDriverSelect::OnCancel() +{ + EndDialog(-1); +} + +void VideoDriverSelect::OnOk() +{ + EndDialog(m_drivers.GetCurSel()); +} + +BOOL VideoDriverSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + for(int i = 0; i < gDriverCnt; i++) { + m_drivers.AddString(Drivers[i].szDescription); + } + + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void VideoDriverSelect::OnSelchangeDrivers() +{ + GetDlgItem(ID_OK)->EnableWindow(m_drivers.GetCurSel() != -1); +} diff --git a/src/win32/VideoMode.h b/src/win32/VideoMode.h new file mode 100644 index 00000000..6b359f51 --- /dev/null +++ b/src/win32/VideoMode.h @@ -0,0 +1,102 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_) +#define AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// VideoMode.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// VideoMode dialog + +class VideoMode : public CDialog +{ + // Construction + public: + VideoMode(LPDIRECTDRAW7 pDraw,CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(VideoMode) + enum { IDD = IDD_MODES }; + CListBox m_modes; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VideoMode) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(VideoMode) + afx_msg void OnSelchangeModes(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + LPDIRECTDRAW7 pDirectDraw; +}; + +///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect dialog + +class VideoDriverSelect : public CDialog +{ + // Construction + public: + VideoDriverSelect(CWnd* pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(VideoDriverSelect) + enum { IDD = IDD_DRIVERS }; + CListBox m_drivers; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VideoDriverSelect) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation + protected: + + // Generated message map functions + //{{AFX_MSG(VideoDriverSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + afx_msg void OnSelchangeDrivers(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; +#endif // !defined(AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_) diff --git a/src/win32/WavWriter.cpp b/src/win32/WavWriter.cpp new file mode 100644 index 00000000..3759bfd3 --- /dev/null +++ b/src/win32/WavWriter.cpp @@ -0,0 +1,117 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// WavWriter.cpp: implementation of the WavWriter class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "vba.h" +#include "WavWriter.h" + +#include "../System.h" +#include "../Util.h" + +#ifdef _DEBUG +#undef THIS_FILE +static char THIS_FILE[]=__FILE__; +#define new DEBUG_NEW +#endif + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +WavWriter::WavWriter() +{ + m_file = NULL; + m_len = 0; + m_posSize = 0; +} + +WavWriter::~WavWriter() +{ + if(m_file) + Close(); +} + +void WavWriter::Close() +{ + // calculate the total file length + u32 len = ftell(m_file)-8; + fseek(m_file, 4, SEEK_SET); + u8 data[4]; + utilPutDword(data, len); + fwrite(data, 1, 4, m_file); + // write out the size of the data section + fseek(m_file, m_posSize, SEEK_SET); + utilPutDword(data, m_len); + fwrite(data, 1, 4, m_file); + fclose(m_file); + m_file = NULL; +} + +bool WavWriter::Open(const char *name) +{ + if(m_file) + Close(); + m_file = fopen(name, "wb"); + + if(!m_file) + return false; + // RIFF header + u8 data[4] = { 'R', 'I', 'F', 'F' }; + fwrite(data, 1, 4, m_file); + utilPutDword(data, 0); + // write 0 for now. Will get filled during close + fwrite(data, 1, 4, m_file); + // write WAVE header + u8 data2[4] = { 'W', 'A', 'V', 'E' }; + fwrite(data2, 1, 4, m_file); + return true; +} + +void WavWriter::SetFormat(const WAVEFORMATEX *format) +{ + if(m_file == NULL) + return; + // write fmt header + u8 data[4] = { 'f', 'm', 't', ' ' }; + fwrite(data, 1, 4, m_file); + u32 value = sizeof(WAVEFORMATEX); + utilPutDword(data, value); + fwrite(data, 1, 4, m_file); + fwrite(format, 1, sizeof(WAVEFORMATEX), m_file); + // start data header + u8 data2[4] = { 'd', 'a', 't', 'a' }; + fwrite(data2, 1, 4, m_file); + + m_posSize = ftell(m_file); + // write 0 for data chunk size. Filled out during Close() + utilPutDword(data, 0); + fwrite(data, 1, 4, m_file); +} + +void WavWriter::AddSound(const u8 *data, int len) +{ + if(m_file == NULL) + return; + // write a block of sound data + fwrite(data, 1, len, m_file); + m_len += len; +} diff --git a/src/win32/WavWriter.h b/src/win32/WavWriter.h new file mode 100644 index 00000000..8e3c31bc --- /dev/null +++ b/src/win32/WavWriter.h @@ -0,0 +1,52 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// WavWriter.h: interface for the WavWriter class. +// +////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_) +#define AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include + +class WavWriter +{ + private: + FILE *m_file; + int m_len; + long m_posSize; + + public: + WavWriter(); + ~WavWriter(); + + bool Open(const char *name); + void SetFormat(const WAVEFORMATEX *format); + void AddSound(const u8 *data, int len); + + private: + void Close(); +}; + +#endif // !defined(AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_) diff --git a/src/win32/WinHelper.h b/src/win32/WinHelper.h new file mode 100644 index 00000000..66fb6b70 --- /dev/null +++ b/src/win32/WinHelper.h @@ -0,0 +1,232 @@ +/*---------------------------------------------------------------------- + Copyright (c) 1998 Gipsysoft. All Rights Reserved. + Please see the file "licence.txt" for licencing details. + File: WinHelper.h + Owner: russf@gipsysoft.com + Purpose: Windows helper functions, classes, structures and macros + that make life a little easier + These should all be zero impact classes etc. that is they + should *not* have a cpp file associated with them. + ----------------------------------------------------------------------*/ +#ifndef WINHELPER_H +#define WINHELPER_H + +//#ifndef DEBUGHLP_H +// #include +//#endif // DEBUGHLP_H + +#ifndef FASTCALL +#define FASTCALL +#endif // FASTCALL + +extern void AssertFailed(char *, int, char *); +extern void ApiFailure(char *, int, char *); + +#define R_VERIFY(a) R_ASSERT(a) +#define R_ASSERT(a) \ + do {\ + if(!(a)) {\ + AssertFailed(__FILE__, __LINE__, #a);\ + }\ + } while(0); + +#define VAPI(a) \ + do { \ + if(!(a)) {\ + ApiFailure(__FILE__, __LINE__, #a); \ + }\ + } while (0); + +#define ASSERT_VALID_HWND(a) ASSERT( ::IsWindow(a) ) + +namespace WinHelper +{ + + class CSize : public tagSIZE + // + // Wrapper for the SIZE structure + { + public: + inline CSize() {}; + inline explicit CSize( const SIZE &size ) { cx = size.cx; cy = size.cy; } + inline explicit CSize( long nSizeX, long nSizeY ) { cx = nSizeX; cy = nSizeY; } + inline void Set( long nSizeX, long nSizeY ) { cx = nSizeX; cy = nSizeY; } + inline operator LPSIZE() { return this; }; + + inline bool operator !=( const SIZE &size ) const { return cx != size.cx || cy != size.cy;} + inline CSize & operator =( const SIZE &size ) { cx = size.cx; cy = size.cy; return *this; } + inline void Empty() { cx = cy = 0; } + }; + + + class CRect : public tagRECT + // + // Wrapper for a RECT structure + { + public: + inline CRect() {} + // Initialisation constructor + inline explicit CRect( const RECT& rhs ) { Set( rhs.left, rhs.top, rhs.right, rhs.bottom );} + inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set( xLeft, yTop, xRight, yBottom ); } + // Get the width of the rectangle + inline int Width() const { return right - left; } + // Get the height of the rectangle + inline int Height() const { return bottom - top; } + // overloaded operator so you don't have to do &rc anymore + inline operator LPCRECT() const { return this; }; + inline operator LPRECT() { return this; }; + // Return the SIZE of the rectangle; + inline CSize Size() const { CSize s( Width(), Height() ); return s; } + // Return the top left of the rectangle + inline POINT TopLeft() const { POINT pt = { left, top }; return pt; } + // Return the bottom right of the rectangle + inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; } + // Set the rectangles left, top, right and bottom + inline void Set( int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left = xLeft; } + // Return true if the rectangle contains all zeros + inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; } + // Zero out our rectangle + inline void Empty() { left = right = top = bottom = 0; } + // Set the size of the rect but leave the top left position untouched. + inline void SetSize( const CSize &size ) { bottom = top + size.cy; right = left + size.cx; } + inline void SetSize( const SIZE &size ) { bottom = top + size.cy; right = left + size.cx; } + inline void SetSize( int cx, int cy ) { bottom = top + cy; right = left + cx; } + // Move the rectangle by an offset + inline void Offset( int cx, int cy ) + { + top+=cy; + bottom+=cy; + right+=cx; + left+=cx; + } + // Inflate the rectangle by the cx and cy, use negative to shrink the rectangle + inline void Inflate( int cx, int cy ) + { + top-=cy; + bottom+=cy; + right+=cx; + left-=cx; + } + // Assignment from a RECT + inline CRect &operator = ( const RECT&rhs ) + { + left = rhs.left; top = rhs.top; + right = rhs.right; bottom = rhs.bottom; + return *this; + } + + // Return true if the point passed is within the rectangle + inline bool PtInRect( const POINT &pt ) const { return ( pt.x >= left && pt.x < right && pt.y >=top && pt.y < bottom ); } + // Return true if the rectangle passed overlaps this rectangle + inline bool Intersect( const RECT &rc ) const { return ( rc.left < right && rc.right > left && rc.top < bottom && rc.bottom > top ); } + }; + + + class CPoint : public tagPOINT + // + // Wrapper for the POINT structure + { + public: + inline CPoint() {}; + inline CPoint( LPARAM lParam ) { x = LOWORD( lParam ); y = HIWORD(lParam); } + inline CPoint( int nX, int nY ) { x = nX; y = nY; } + inline CPoint( const POINT &pt ) { x = pt.x; y = pt.y; } + inline bool operator == ( const CPoint &rhs ) const { return x == rhs.x && y == rhs.y; } + inline bool operator != ( const CPoint &rhs ) const { return x != rhs.x || y != rhs.y; } + inline operator LPPOINT () { return this; } + }; + + + class CScrollInfo : public tagSCROLLINFO + { + public: + CScrollInfo( UINT fPassedMask ) { cbSize = sizeof( tagSCROLLINFO ); fMask = fPassedMask; } + }; + + + class CCriticalSection + // + // Simple crtical section handler/wrapper + { + public: + inline CCriticalSection() { ::InitializeCriticalSection(&m_sect); } + inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); } + + // Blocking lock. + inline void Lock() { ::EnterCriticalSection(&m_sect); } + // Unlock + inline void Unlock() { ::LeaveCriticalSection(&m_sect); } + + class CLock + // + // Simple lock class for the critcal section + { + public: + inline CLock( CCriticalSection § ) : m_sect( sect ) { m_sect.Lock(); } + inline ~CLock() { m_sect.Unlock(); } + private: + CCriticalSection &m_sect; + + CLock(); + CLock( const CLock &); + CLock& operator =( const CLock &); + }; + + private: + CRITICAL_SECTION m_sect; + + CCriticalSection( const CCriticalSection & ); + CCriticalSection& operator =( const CCriticalSection & ); + }; + + +#define ZeroStructure( t ) ZeroMemory( &t, sizeof( t ) ) +#define countof( t ) (sizeof( (t) ) / sizeof( (t)[0] ) ) +#define UNREF(P) UNREFERENCED_PARAMETER(P) + + inline bool IsShiftPressed() + { + return GetKeyState(VK_SHIFT) & 0x8000 ? true : false; + } + + inline bool IsAltPressed() + { + return GetKeyState(VK_MENU) & 0x8000 ? true : false; + } + + inline bool IsControlPressed() + { + return GetKeyState(VK_CONTROL) & 0x8000 ? true : false; + } + + inline HICON LoadIcon16x16( HINSTANCE hInst, UINT uID ) + // + // Load a 16x16 icon from the same resource as the other size icons. + { + return reinterpret_cast( ::LoadImage( hInst, MAKEINTRESOURCE( uID ), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR ) ); + } + + + class CDeferWindowPos + // + // Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous. + { + public: + inline CDeferWindowPos( const int nWindows = 1 ) : m_hdlDef( ::BeginDeferWindowPos( nWindows ) ) {} + inline ~CDeferWindowPos() { R_VERIFY( ::EndDeferWindowPos( m_hdlDef ) ); } + inline HDWP DeferWindowPos( HWND hWnd, HWND hWndInsertAfter , int x, int y, int cx, int cy, UINT uFlags ) + { + return ::DeferWindowPos( m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags ); + } + inline HDWP DeferWindowPos( HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags ) + { + return ::DeferWindowPos( m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags ); + } + + private: + HDWP m_hdlDef; + }; + +} // WinHelper + +#endif //WINHELPER_H diff --git a/src/win32/WinResUtil.cpp b/src/win32/WinResUtil.cpp new file mode 100644 index 00000000..968df8a3 --- /dev/null +++ b/src/win32/WinResUtil.cpp @@ -0,0 +1,109 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "stdafx.h" + +static HINSTANCE winResGetInstance(LPCTSTR resType, LPCTSTR resName) +{ + // TODO: make language DLL first + return AfxFindResourceHandle(resName, resType); +} + + +UCHAR *winResGetResource(LPCTSTR resType, LPCTSTR resName) +{ + HINSTANCE winResInstance = winResGetInstance(resType, resName); + + HRSRC hRsrc = FindResourceEx(winResInstance, resType, resName, 0); + + if(hRsrc != NULL) { + HGLOBAL hGlobal = LoadResource(winResInstance, hRsrc); + + if(hGlobal != NULL) { + UCHAR * b = (UCHAR *)LockResource(hGlobal); + + return b; + } + } + return NULL; +} + +HMENU winResLoadMenu(LPCTSTR menuName) +{ + UCHAR * b = winResGetResource(RT_MENU, menuName); + + if(b != NULL) { + HMENU menu = LoadMenuIndirect((CONST MENUTEMPLATE *)b); + + if(menu != NULL) + return menu; + } + + return LoadMenu(NULL, menuName); +} + +int winResDialogBox(LPCTSTR boxName, + HWND parent, + DLGPROC dlgProc, + LPARAM lParam) +{ + /* + UCHAR * b = winResGetResource(RT_DIALOG, boxName); + + if(b != NULL) { + + return DialogBoxIndirectParam(hInstance, + (LPCDLGTEMPLATE)b, + parent, + dlgProc, + lParam); + } + + return DialogBoxParam(hInstance, + boxName, + parent, + dlgProc, + lParam); + */ + return 0; +} + +int winResDialogBox(LPCTSTR boxName, + HWND parent, + DLGPROC dlgProc) +{ + return winResDialogBox(boxName, + parent, + dlgProc, + 0); +} + +CString winResLoadString(UINT id) +{ + int stId = id / 16 + 1; + HINSTANCE inst = winResGetInstance(RT_STRING, MAKEINTRESOURCE(stId)); + + CString res; + if(res.LoadString(id)) + return res; + + // TODO: handle case where string is only in the default English + res = ""; + + return res; +} diff --git a/src/win32/WinResUtil.h b/src/win32/WinResUtil.h new file mode 100644 index 00000000..8184c952 --- /dev/null +++ b/src/win32/WinResUtil.h @@ -0,0 +1,31 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern HMENU winResLoadMenu(LPCTSTR menuName); + +extern int winResDialogBox(LPCTSTR boxName, + HWND parent, + DLGPROC dlgProc); + +extern int winResDialogBox(LPCTSTR boxName, + HWND parent, + DLGPROC dlgProc, + LPARAM lParam); + +extern CString winResLoadString(UINT id); diff --git a/src/win32/ZoomControl.cpp b/src/win32/ZoomControl.cpp new file mode 100644 index 00000000..67509fc0 --- /dev/null +++ b/src/win32/ZoomControl.cpp @@ -0,0 +1,190 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// ZoomControl.cpp : implementation file +// + +#include "stdafx.h" +#include "vba.h" +#include "ZoomControl.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +bool ZoomControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ZoomControl + +ZoomControl::ZoomControl() +{ + ZeroMemory(colors, 3*64); + selected = -1; + registerClass(); +} + +ZoomControl::~ZoomControl() +{ +} + + +BEGIN_MESSAGE_MAP(ZoomControl, CWnd) + //{{AFX_MSG_MAP(ZoomControl) + ON_WM_PAINT() + ON_WM_LBUTTONDOWN() + ON_WM_ERASEBKGND() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// ZoomControl message handlers + +void ZoomControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaZoomControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void ZoomControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + + int w = rect.right - rect.left; + int h = rect.bottom - rect.top; + + CDC memDC ; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, h); + + pOldBitmap = memDC.SelectObject(&bitmap); + + int multX = w / 8; + int multY = h / 8; + + int i; + for(i = 0; i < 64; i++) { + CBrush b; + b.CreateSolidBrush(RGB(colors[i*3+2], colors[i*3+1], colors[i*3])); + + RECT r; + int x = i & 7; + int y = i / 8; + r.top = y*multY; + r.left = x*multX; + r.bottom = r.top + multY; + r.right = r.left + multX; + memDC.FillRect(&r, &b); + b.DeleteObject(); + } + + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(192,192,192)); + CPen *old = (CPen *)memDC.SelectObject(&pen); + + for(i = 0; i < 8; i++) { + memDC.MoveTo(0, i * multY); + memDC.LineTo(w, i * multY); + memDC.MoveTo(i * multX, 0); + memDC.LineTo(i * multX, h); + } + + if(selected != -1) { + CPen pen2; + pen2.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); + CPen *old2 = (CPen*)memDC.SelectObject(&pen2); + + int startX = (selected & 7)*multX+1; + int startY = (selected / 8)*multY+1; + int endX = startX + multX-2; + int endY = startY + multY-2; + + memDC.MoveTo(startX, startY); + memDC.LineTo(endX, startY); + memDC.LineTo(endX, endY); + memDC.LineTo(startX, endY); + memDC.LineTo(startX, startY-1); + memDC.SelectObject(old2); + pen2.DeleteObject(); + } + memDC.SelectObject(old); + pen.DeleteObject(); + + dc.BitBlt(0,0,w,h, + &memDC,0,0, SRCCOPY); + + memDC.SelectObject(pOldBitmap); + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +void ZoomControl::OnLButtonDown(UINT nFlags, CPoint point) +{ + RECT rect; + GetClientRect(&rect); + + int height = rect.bottom - rect.top; + int width = rect.right - rect.left; + + int multX = width / 8; + int multY = height / 8; + + selected = point.x / multX + 8 * (point.y / multY); + + int c = point.x / multX + 8 * (point.y/multY); + u16 color = colors[c*3] << 7 | + colors[c*3+1] << 2 | + (colors[c*3+2] >> 3); + + GetParent()->PostMessage(WM_COLINFO, + color, + 0); + + Invalidate(); +} + +BOOL ZoomControl::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + +void ZoomControl::setColors(const u8 *c) +{ + memcpy(colors, c, 3*64); + selected = -1; + Invalidate(); +} diff --git a/src/win32/ZoomControl.h b/src/win32/ZoomControl.h new file mode 100644 index 00000000..d70b4c98 --- /dev/null +++ b/src/win32/ZoomControl.h @@ -0,0 +1,78 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#if !defined(AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_) +#define AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_ + +#include "..\System.h" // Added by ClassView +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ZoomControl.h : header file +// +#ifndef WM_COLINFO +#define WM_COLINFO WM_APP+100 +#endif + +///////////////////////////////////////////////////////////////////////////// +// ZoomControl window + +class ZoomControl : public CWnd +{ + // Construction + public: + ZoomControl(); + + // Attributes + public: + + // Operations + public: + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ZoomControl) + //}}AFX_VIRTUAL + + // Implementation + public: + void setColors(const u8 *c); + static bool isRegistered; + virtual ~ZoomControl(); + + // Generated message map functions + protected: + //{{AFX_MSG(ZoomControl) + afx_msg void OnPaint(); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + private: + int selected; + u8 colors[3*64]; + void registerClass(); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_) diff --git a/src/win32/stdafx.cpp b/src/win32/stdafx.cpp new file mode 100644 index 00000000..1577c4e3 --- /dev/null +++ b/src/win32/stdafx.cpp @@ -0,0 +1 @@ +#include "stdafx.h" \ No newline at end of file diff --git a/src/win32/stdafx.h b/src/win32/stdafx.h new file mode 100644 index 00000000..78b29ad5 --- /dev/null +++ b/src/win32/stdafx.h @@ -0,0 +1,37 @@ +#pragma once + +#define VERSION "S1.7.6" + +#ifndef DIRECT3D_VERSION +#define DIRECT3D_VERSION 0x0900 +#endif + +#ifndef WINVER +#define WINVER 0x0500 +#endif + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 +#endif + +#ifndef _WIN32_WINDOWS +#define _WIN32_WINDOWS 0x0500 +#endif + +#ifndef VC_EXTRALEAN +#define VC_EXTRALEAN +#endif + +#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS + +#define _AFX_ALL_WARNINGS + +#include +#include + +#include +#ifndef _AFX_NO_AFXCMN_SUPPORT +#include +#endif + +#include "VBA.h" \ No newline at end of file diff --git a/zlib/adler32.c b/zlib/adler32.c new file mode 100644 index 00000000..007ba262 --- /dev/null +++ b/zlib/adler32.c @@ -0,0 +1,149 @@ +/* adler32.c -- compute the Adler-32 checksum of a data stream + * Copyright (C) 1995-2004 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#define ZLIB_INTERNAL +#include "zlib.h" + +#define BASE 65521UL /* largest prime smaller than 65536 */ +#define NMAX 5552 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ + +#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); +#define DO16(buf) DO8(buf,0); DO8(buf,8); + +/* use NO_DIVIDE if your processor does not do division in hardware */ +#ifdef NO_DIVIDE +# define MOD(a) \ + do { \ + if (a >= (BASE << 16)) a -= (BASE << 16); \ + if (a >= (BASE << 15)) a -= (BASE << 15); \ + if (a >= (BASE << 14)) a -= (BASE << 14); \ + if (a >= (BASE << 13)) a -= (BASE << 13); \ + if (a >= (BASE << 12)) a -= (BASE << 12); \ + if (a >= (BASE << 11)) a -= (BASE << 11); \ + if (a >= (BASE << 10)) a -= (BASE << 10); \ + if (a >= (BASE << 9)) a -= (BASE << 9); \ + if (a >= (BASE << 8)) a -= (BASE << 8); \ + if (a >= (BASE << 7)) a -= (BASE << 7); \ + if (a >= (BASE << 6)) a -= (BASE << 6); \ + if (a >= (BASE << 5)) a -= (BASE << 5); \ + if (a >= (BASE << 4)) a -= (BASE << 4); \ + if (a >= (BASE << 3)) a -= (BASE << 3); \ + if (a >= (BASE << 2)) a -= (BASE << 2); \ + if (a >= (BASE << 1)) a -= (BASE << 1); \ + if (a >= BASE) a -= BASE; \ + } while (0) +# define MOD4(a) \ + do { \ + if (a >= (BASE << 4)) a -= (BASE << 4); \ + if (a >= (BASE << 3)) a -= (BASE << 3); \ + if (a >= (BASE << 2)) a -= (BASE << 2); \ + if (a >= (BASE << 1)) a -= (BASE << 1); \ + if (a >= BASE) a -= BASE; \ + } while (0) +#else +# define MOD(a) a %= BASE +# define MOD4(a) a %= BASE +#endif + +/* ========================================================================= */ +uLong ZEXPORT adler32(adler, buf, len) + uLong adler; + const Bytef *buf; + uInt len; +{ + unsigned long sum2; + unsigned n; + + /* split Adler-32 into component sums */ + sum2 = (adler >> 16) & 0xffff; + adler &= 0xffff; + + /* in case user likes doing a byte at a time, keep it fast */ + if (len == 1) { + adler += buf[0]; + if (adler >= BASE) + adler -= BASE; + sum2 += adler; + if (sum2 >= BASE) + sum2 -= BASE; + return adler | (sum2 << 16); + } + + /* initial Adler-32 value (deferred check for len == 1 speed) */ + if (buf == Z_NULL) + return 1L; + + /* in case short lengths are provided, keep it somewhat fast */ + if (len < 16) { + while (len--) { + adler += *buf++; + sum2 += adler; + } + if (adler >= BASE) + adler -= BASE; + MOD4(sum2); /* only added so many BASE's */ + return adler | (sum2 << 16); + } + + /* do length NMAX blocks -- requires just one modulo operation */ + while (len >= NMAX) { + len -= NMAX; + n = NMAX / 16; /* NMAX is divisible by 16 */ + do { + DO16(buf); /* 16 sums unrolled */ + buf += 16; + } while (--n); + MOD(adler); + MOD(sum2); + } + + /* do remaining bytes (less than NMAX, still just one modulo) */ + if (len) { /* avoid modulos if none remaining */ + while (len >= 16) { + len -= 16; + DO16(buf); + buf += 16; + } + while (len--) { + adler += *buf++; + sum2 += adler; + } + MOD(adler); + MOD(sum2); + } + + /* return recombined sums */ + return adler | (sum2 << 16); +} + +/* ========================================================================= */ +uLong ZEXPORT adler32_combine(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off_t len2; +{ + unsigned long sum1; + unsigned long sum2; + unsigned rem; + + /* the derivation of this formula is left as an exercise for the reader */ + rem = (unsigned)(len2 % BASE); + sum1 = adler1 & 0xffff; + sum2 = rem * sum1; + MOD(sum2); + sum1 += (adler2 & 0xffff) + BASE - 1; + sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; + if (sum1 > BASE) sum1 -= BASE; + if (sum1 > BASE) sum1 -= BASE; + if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); + if (sum2 > BASE) sum2 -= BASE; + return sum1 | (sum2 << 16); +} diff --git a/zlib/compress.c b/zlib/compress.c new file mode 100644 index 00000000..df04f014 --- /dev/null +++ b/zlib/compress.c @@ -0,0 +1,79 @@ +/* compress.c -- compress a memory buffer + * Copyright (C) 1995-2003 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#define ZLIB_INTERNAL +#include "zlib.h" + +/* =========================================================================== + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least 0.1% larger than sourceLen plus + 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ +int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; + int level; +{ + z_stream stream; + int err; + + stream.next_in = (Bytef*)source; + stream.avail_in = (uInt)sourceLen; +#ifdef MAXSEG_64K + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; +#endif + stream.next_out = dest; + stream.avail_out = (uInt)*destLen; + if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; + + stream.zalloc = (alloc_func)0; + stream.zfree = (free_func)0; + stream.opaque = (voidpf)0; + + err = deflateInit(&stream, level); + if (err != Z_OK) return err; + + err = deflate(&stream, Z_FINISH); + if (err != Z_STREAM_END) { + deflateEnd(&stream); + return err == Z_OK ? Z_BUF_ERROR : err; + } + *destLen = stream.total_out; + + err = deflateEnd(&stream); + return err; +} + +/* =========================================================================== + */ +int ZEXPORT compress (dest, destLen, source, sourceLen) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; +{ + return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); +} + +/* =========================================================================== + If the default memLevel or windowBits for deflateInit() is changed, then + this function needs to be updated. + */ +uLong ZEXPORT compressBound (sourceLen) + uLong sourceLen; +{ + return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; +} diff --git a/zlib/crc32.c b/zlib/crc32.c new file mode 100644 index 00000000..f658a9ef --- /dev/null +++ b/zlib/crc32.c @@ -0,0 +1,423 @@ +/* crc32.c -- compute the CRC-32 of a data stream + * Copyright (C) 1995-2005 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Thanks to Rodney Brown for his contribution of faster + * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing + * tables for updating the shift register in one step with three exclusive-ors + * instead of four steps with four exclusive-ors. This results in about a + * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. + */ + +/* @(#) $Id$ */ + +/* + Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore + protection on the static variables used to control the first-use generation + of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should + first call get_crc_table() to initialize the tables before allowing more than + one thread to use crc32(). + */ + +#ifdef MAKECRCH +# include +# ifndef DYNAMIC_CRC_TABLE +# define DYNAMIC_CRC_TABLE +# endif /* !DYNAMIC_CRC_TABLE */ +#endif /* MAKECRCH */ + +#include "zutil.h" /* for STDC and FAR definitions */ + +#define local static + +/* Find a four-byte integer type for crc32_little() and crc32_big(). */ +#ifndef NOBYFOUR +# ifdef STDC /* need ANSI C limits.h to determine sizes */ +# include +# define BYFOUR +# if (UINT_MAX == 0xffffffffUL) + typedef unsigned int u4; +# else +# if (ULONG_MAX == 0xffffffffUL) + typedef unsigned long u4; +# else +# if (USHRT_MAX == 0xffffffffUL) + typedef unsigned short u4; +# else +# undef BYFOUR /* can't find a four-byte integer type! */ +# endif +# endif +# endif +# endif /* STDC */ +#endif /* !NOBYFOUR */ + +/* Definitions for doing the crc four data bytes at a time. */ +#ifdef BYFOUR +# define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ + (((w)&0xff00)<<8)+(((w)&0xff)<<24)) + local unsigned long crc32_little OF((unsigned long, + const unsigned char FAR *, unsigned)); + local unsigned long crc32_big OF((unsigned long, + const unsigned char FAR *, unsigned)); +# define TBLS 8 +#else +# define TBLS 1 +#endif /* BYFOUR */ + +/* Local functions for crc concatenation */ +local unsigned long gf2_matrix_times OF((unsigned long *mat, + unsigned long vec)); +local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); + +#ifdef DYNAMIC_CRC_TABLE + +local volatile int crc_table_empty = 1; +local unsigned long FAR crc_table[TBLS][256]; +local void make_crc_table OF((void)); +#ifdef MAKECRCH + local void write_table OF((FILE *, const unsigned long FAR *)); +#endif /* MAKECRCH */ +/* + Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: + x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. + + Polynomials over GF(2) are represented in binary, one bit per coefficient, + with the lowest powers in the most significant bit. Then adding polynomials + is just exclusive-or, and multiplying a polynomial by x is a right shift by + one. If we call the above polynomial p, and represent a byte as the + polynomial q, also with the lowest power in the most significant bit (so the + byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, + where a mod b means the remainder after dividing a by b. + + This calculation is done using the shift-register method of multiplying and + taking the remainder. The register is initialized to zero, and for each + incoming bit, x^32 is added mod p to the register if the bit is a one (where + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by + x (which is shifting right by one and adding x^32 mod p if the bit shifted + out is a one). We start with the highest power (least significant bit) of + q and repeat for all eight bits of q. + + The first table is simply the CRC of all possible eight bit values. This is + all the information needed to generate CRCs on data a byte at a time for all + combinations of CRC register values and incoming bytes. The remaining tables + allow for word-at-a-time CRC calculation for both big-endian and little- + endian machines, where a word is four bytes. +*/ +local void make_crc_table() +{ + unsigned long c; + int n, k; + unsigned long poly; /* polynomial exclusive-or pattern */ + /* terms of polynomial defining this crc (except x^32): */ + static volatile int first = 1; /* flag to limit concurrent making */ + static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* See if another task is already doing this (not thread-safe, but better + than nothing -- significantly reduces duration of vulnerability in + case the advice about DYNAMIC_CRC_TABLE is ignored) */ + if (first) { + first = 0; + + /* make exclusive-or pattern from polynomial (0xedb88320UL) */ + poly = 0UL; + for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) + poly |= 1UL << (31 - p[n]); + + /* generate a crc for every 8-bit value */ + for (n = 0; n < 256; n++) { + c = (unsigned long)n; + for (k = 0; k < 8; k++) + c = c & 1 ? poly ^ (c >> 1) : c >> 1; + crc_table[0][n] = c; + } + +#ifdef BYFOUR + /* generate crc for each value followed by one, two, and three zeros, + and then the byte reversal of those as well as the first table */ + for (n = 0; n < 256; n++) { + c = crc_table[0][n]; + crc_table[4][n] = REV(c); + for (k = 1; k < 4; k++) { + c = crc_table[0][c & 0xff] ^ (c >> 8); + crc_table[k][n] = c; + crc_table[k + 4][n] = REV(c); + } + } +#endif /* BYFOUR */ + + crc_table_empty = 0; + } + else { /* not first */ + /* wait for the other guy to finish (not efficient, but rare) */ + while (crc_table_empty) + ; + } + +#ifdef MAKECRCH + /* write out CRC tables to crc32.h */ + { + FILE *out; + + out = fopen("crc32.h", "w"); + if (out == NULL) return; + fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); + fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); + fprintf(out, "local const unsigned long FAR "); + fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); + write_table(out, crc_table[0]); +# ifdef BYFOUR + fprintf(out, "#ifdef BYFOUR\n"); + for (k = 1; k < 8; k++) { + fprintf(out, " },\n {\n"); + write_table(out, crc_table[k]); + } + fprintf(out, "#endif\n"); +# endif /* BYFOUR */ + fprintf(out, " }\n};\n"); + fclose(out); + } +#endif /* MAKECRCH */ +} + +#ifdef MAKECRCH +local void write_table(out, table) + FILE *out; + const unsigned long FAR *table; +{ + int n; + + for (n = 0; n < 256; n++) + fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], + n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); +} +#endif /* MAKECRCH */ + +#else /* !DYNAMIC_CRC_TABLE */ +/* ======================================================================== + * Tables of CRC-32s of all single-byte values, made by make_crc_table(). + */ +#include "crc32.h" +#endif /* DYNAMIC_CRC_TABLE */ + +/* ========================================================================= + * This function can be used by asm versions of crc32() + */ +const unsigned long FAR * ZEXPORT get_crc_table() +{ +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + return (const unsigned long FAR *)crc_table; +} + +/* ========================================================================= */ +#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) +#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 + +/* ========================================================================= */ +unsigned long ZEXPORT crc32(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + if (buf == Z_NULL) return 0UL; + +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + +#ifdef BYFOUR + if (sizeof(void *) == sizeof(ptrdiff_t)) { + u4 endian; + + endian = 1; + if (*((unsigned char *)(&endian))) + return crc32_little(crc, buf, len); + else + return crc32_big(crc, buf, len); + } +#endif /* BYFOUR */ + crc = crc ^ 0xffffffffUL; + while (len >= 8) { + DO8; + len -= 8; + } + if (len) do { + DO1; + } while (--len); + return crc ^ 0xffffffffUL; +} + +#ifdef BYFOUR + +/* ========================================================================= */ +#define DOLIT4 c ^= *buf4++; \ + c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ + crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] +#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 + +/* ========================================================================= */ +local unsigned long crc32_little(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register u4 c; + register const u4 FAR *buf4; + + c = (u4)crc; + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + len--; + } + + buf4 = (const u4 FAR *)(const void FAR *)buf; + while (len >= 32) { + DOLIT32; + len -= 32; + } + while (len >= 4) { + DOLIT4; + len -= 4; + } + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + } while (--len); + c = ~c; + return (unsigned long)c; +} + +/* ========================================================================= */ +#define DOBIG4 c ^= *++buf4; \ + c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ + crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] +#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 + +/* ========================================================================= */ +local unsigned long crc32_big(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register u4 c; + register const u4 FAR *buf4; + + c = REV((u4)crc); + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + len--; + } + + buf4 = (const u4 FAR *)(const void FAR *)buf; + buf4--; + while (len >= 32) { + DOBIG32; + len -= 32; + } + while (len >= 4) { + DOBIG4; + len -= 4; + } + buf4++; + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + } while (--len); + c = ~c; + return (unsigned long)(REV(c)); +} + +#endif /* BYFOUR */ + +#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ + +/* ========================================================================= */ +local unsigned long gf2_matrix_times(mat, vec) + unsigned long *mat; + unsigned long vec; +{ + unsigned long sum; + + sum = 0; + while (vec) { + if (vec & 1) + sum ^= *mat; + vec >>= 1; + mat++; + } + return sum; +} + +/* ========================================================================= */ +local void gf2_matrix_square(square, mat) + unsigned long *square; + unsigned long *mat; +{ + int n; + + for (n = 0; n < GF2_DIM; n++) + square[n] = gf2_matrix_times(mat, mat[n]); +} + +/* ========================================================================= */ +uLong ZEXPORT crc32_combine(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off_t len2; +{ + int n; + unsigned long row; + unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ + unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ + + /* degenerate case */ + if (len2 == 0) + return crc1; + + /* put operator for one zero bit in odd */ + odd[0] = 0xedb88320L; /* CRC-32 polynomial */ + row = 1; + for (n = 1; n < GF2_DIM; n++) { + odd[n] = row; + row <<= 1; + } + + /* put operator for two zero bits in even */ + gf2_matrix_square(even, odd); + + /* put operator for four zero bits in odd */ + gf2_matrix_square(odd, even); + + /* apply len2 zeros to crc1 (first square will put the operator for one + zero byte, eight zero bits, in even) */ + do { + /* apply zeros operator for this bit of len2 */ + gf2_matrix_square(even, odd); + if (len2 & 1) + crc1 = gf2_matrix_times(even, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + if (len2 == 0) + break; + + /* another iteration of the loop with odd and even swapped */ + gf2_matrix_square(odd, even); + if (len2 & 1) + crc1 = gf2_matrix_times(odd, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + } while (len2 != 0); + + /* return combined crc */ + crc1 ^= crc2; + return crc1; +} diff --git a/zlib/crc32.h b/zlib/crc32.h new file mode 100644 index 00000000..8053b611 --- /dev/null +++ b/zlib/crc32.h @@ -0,0 +1,441 @@ +/* crc32.h -- tables for rapid CRC calculation + * Generated automatically by crc32.c + */ + +local const unsigned long FAR crc_table[TBLS][256] = +{ + { + 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, + 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, + 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, + 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, + 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, + 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, + 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, + 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, + 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, + 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, + 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, + 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, + 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, + 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, + 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, + 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, + 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, + 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, + 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, + 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, + 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, + 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, + 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, + 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, + 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, + 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, + 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, + 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, + 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, + 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, + 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, + 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, + 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, + 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, + 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, + 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, + 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, + 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, + 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, + 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, + 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, + 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, + 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, + 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, + 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, + 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, + 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, + 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, + 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, + 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, + 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, + 0x2d02ef8dUL +#ifdef BYFOUR + }, + { + 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, + 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, + 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, + 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, + 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, + 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, + 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, + 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, + 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, + 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, + 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, + 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, + 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, + 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, + 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, + 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, + 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, + 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, + 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, + 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, + 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, + 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, + 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, + 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, + 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, + 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, + 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, + 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, + 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, + 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, + 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, + 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, + 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, + 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, + 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, + 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, + 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, + 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, + 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, + 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, + 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, + 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, + 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, + 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, + 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, + 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, + 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, + 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, + 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, + 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, + 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, + 0x9324fd72UL + }, + { + 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, + 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, + 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, + 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, + 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, + 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, + 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, + 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, + 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, + 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, + 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, + 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, + 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, + 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, + 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, + 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, + 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, + 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, + 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, + 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, + 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, + 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, + 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, + 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, + 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, + 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, + 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, + 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, + 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, + 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, + 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, + 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, + 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, + 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, + 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, + 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, + 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, + 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, + 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, + 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, + 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, + 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, + 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, + 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, + 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, + 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, + 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, + 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, + 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, + 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, + 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, + 0xbe9834edUL + }, + { + 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, + 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, + 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, + 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, + 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, + 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, + 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, + 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, + 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, + 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, + 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, + 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, + 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, + 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, + 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, + 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, + 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, + 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, + 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, + 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, + 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, + 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, + 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, + 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, + 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, + 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, + 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, + 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, + 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, + 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, + 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, + 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, + 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, + 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, + 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, + 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, + 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, + 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, + 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, + 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, + 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, + 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, + 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, + 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, + 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, + 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, + 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, + 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, + 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, + 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, + 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, + 0xde0506f1UL + }, + { + 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, + 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, + 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, + 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, + 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, + 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, + 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, + 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, + 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, + 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, + 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, + 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, + 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, + 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, + 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, + 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, + 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, + 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, + 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, + 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, + 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, + 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, + 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, + 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, + 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, + 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, + 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, + 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, + 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, + 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, + 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, + 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, + 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, + 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, + 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, + 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, + 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, + 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, + 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, + 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, + 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, + 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, + 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, + 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, + 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, + 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, + 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, + 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, + 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, + 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, + 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, + 0x8def022dUL + }, + { + 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, + 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, + 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, + 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, + 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, + 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, + 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, + 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, + 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, + 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, + 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, + 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, + 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, + 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, + 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, + 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, + 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, + 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, + 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, + 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, + 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, + 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, + 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, + 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, + 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, + 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, + 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, + 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, + 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, + 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, + 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, + 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, + 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, + 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, + 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, + 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, + 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, + 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, + 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, + 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, + 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, + 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, + 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, + 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, + 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, + 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, + 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, + 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, + 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, + 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, + 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, + 0x72fd2493UL + }, + { + 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, + 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, + 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, + 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, + 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, + 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, + 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, + 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, + 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, + 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, + 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, + 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, + 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, + 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, + 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, + 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, + 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, + 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, + 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, + 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, + 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, + 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, + 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, + 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, + 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, + 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, + 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, + 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, + 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, + 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, + 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, + 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, + 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, + 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, + 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, + 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, + 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, + 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, + 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, + 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, + 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, + 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, + 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, + 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, + 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, + 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, + 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, + 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, + 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, + 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, + 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, + 0xed3498beUL + }, + { + 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, + 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, + 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, + 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, + 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, + 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, + 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, + 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, + 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, + 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, + 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, + 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, + 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, + 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, + 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, + 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, + 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, + 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, + 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, + 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, + 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, + 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, + 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, + 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, + 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, + 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, + 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, + 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, + 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, + 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, + 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, + 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, + 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, + 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, + 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, + 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, + 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, + 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, + 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, + 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, + 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, + 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, + 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, + 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, + 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, + 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, + 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, + 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, + 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, + 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, + 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, + 0xf10605deUL +#endif + } +}; diff --git a/zlib/deflate.c b/zlib/deflate.c new file mode 100644 index 00000000..29ce1f64 --- /dev/null +++ b/zlib/deflate.c @@ -0,0 +1,1736 @@ +/* deflate.c -- compress data using the deflation algorithm + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process depends on being able to identify portions + * of the input text which are identical to earlier input (within a + * sliding window trailing behind the input currently being processed). + * + * The most straightforward technique turns out to be the fastest for + * most input files: try all possible matches and select the longest. + * The key feature of this algorithm is that insertions into the string + * dictionary are very simple and thus fast, and deletions are avoided + * completely. Insertions are performed at each input character, whereas + * string matches are performed only when the previous match ends. So it + * is preferable to spend more time in matches to allow very fast string + * insertions and avoid deletions. The matching algorithm for small + * strings is inspired from that of Rabin & Karp. A brute force approach + * is used to find longer strings when a small match has been found. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze + * (by Leonid Broukhis). + * A previous version of this file used a more sophisticated algorithm + * (by Fiala and Greene) which is guaranteed to run in linear amortized + * time, but has a larger average cost, uses more memory and is patented. + * However the F&G algorithm may be faster for some highly redundant + * files if the parameter max_chain_length (described below) is too large. + * + * ACKNOWLEDGEMENTS + * + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and + * I found it in 'freeze' written by Leonid Broukhis. + * Thanks to many people for bug reports and testing. + * + * REFERENCES + * + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". + * Available in http://www.ietf.org/rfc/rfc1951.txt + * + * A description of the Rabin and Karp algorithm is given in the book + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. + * + * Fiala,E.R., and Greene,D.H. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 + * + */ + +/* @(#) $Id$ */ + +#include "deflate.h" + +const char deflate_copyright[] = + " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* =========================================================================== + * Function prototypes. + */ +typedef enum { + need_more, /* block not completed, need more input or more output */ + block_done, /* block flush performed */ + finish_started, /* finish started, need only more output at next deflate */ + finish_done /* finish done, accept no more input or output */ +} block_state; + +typedef block_state (*compress_func) OF((deflate_state *s, int flush)); +/* Compression function. Returns the block state after the call. */ + +local void fill_window OF((deflate_state *s)); +local block_state deflate_stored OF((deflate_state *s, int flush)); +local block_state deflate_fast OF((deflate_state *s, int flush)); +#ifndef FASTEST +local block_state deflate_slow OF((deflate_state *s, int flush)); +#endif +local void lm_init OF((deflate_state *s)); +local void putShortMSB OF((deflate_state *s, uInt b)); +local void flush_pending OF((z_streamp strm)); +local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); +#ifndef FASTEST +#ifdef ASMV + void match_init OF((void)); /* asm code initialization */ + uInt longest_match OF((deflate_state *s, IPos cur_match)); +#else +local uInt longest_match OF((deflate_state *s, IPos cur_match)); +#endif +#endif +local uInt longest_match_fast OF((deflate_state *s, IPos cur_match)); + +#ifdef DEBUG +local void check_match OF((deflate_state *s, IPos start, IPos match, + int length)); +#endif + +/* =========================================================================== + * Local data + */ + +#define NIL 0 +/* Tail of hash chains */ + +#ifndef TOO_FAR +# define TOO_FAR 4096 +#endif +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ + +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) +/* Minimum amount of lookahead, except at the end of the input file. + * See deflate.c for comments about the MIN_MATCH+1. + */ + +/* Values for max_lazy_match, good_match and max_chain_length, depending on + * the desired pack level (0..9). The values given below have been tuned to + * exclude worst case performance for pathological files. Better values may be + * found for specific files. + */ +typedef struct config_s { + ush good_length; /* reduce lazy search above this match length */ + ush max_lazy; /* do not perform lazy search above this match length */ + ush nice_length; /* quit search above this match length */ + ush max_chain; + compress_func func; +} config; + +#ifdef FASTEST +local const config configuration_table[2] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ +#else +local const config configuration_table[10] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ +/* 2 */ {4, 5, 16, 8, deflate_fast}, +/* 3 */ {4, 6, 32, 32, deflate_fast}, + +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ +/* 5 */ {8, 16, 32, 32, deflate_slow}, +/* 6 */ {8, 16, 128, 128, deflate_slow}, +/* 7 */ {8, 32, 128, 256, deflate_slow}, +/* 8 */ {32, 128, 258, 1024, deflate_slow}, +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ +#endif + +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different + * meaning. + */ + +#define EQUAL 0 +/* result of memcmp for equal strings */ + +#ifndef NO_DUMMY_DECL +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ +#endif + +/* =========================================================================== + * Update a hash value with the given input byte + * IN assertion: all calls to to UPDATE_HASH are made with consecutive + * input characters, so that a running hash key can be computed from the + * previous key instead of complete recalculation each time. + */ +#define UPDATE_HASH(s,h,c) (h = (((h)<hash_shift) ^ (c)) & s->hash_mask) + + +/* =========================================================================== + * Insert string str in the dictionary and set match_head to the previous head + * of the hash chain (the most recent string with same hash key). Return + * the previous length of the hash chain. + * If this file is compiled with -DFASTEST, the compression level is forced + * to 1, and no hash chains are maintained. + * IN assertion: all calls to to INSERT_STRING are made with consecutive + * input characters and the first MIN_MATCH bytes of str are valid + * (except for the last MIN_MATCH-1 bytes of the input file). + */ +#ifdef FASTEST +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#else +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#endif + +/* =========================================================================== + * Initialize the hash table (avoiding 64K overflow for 16 bit systems). + * prev[] will be initialized on the fly. + */ +#define CLEAR_HASH(s) \ + s->head[s->hash_size-1] = NIL; \ + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); + +/* ========================================================================= */ +int ZEXPORT deflateInit_(strm, level, version, stream_size) + z_streamp strm; + int level; + const char *version; + int stream_size; +{ + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, version, stream_size); + /* To do: ignore strm->next_in if we use it as window */ +} + +/* ========================================================================= */ +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + version, stream_size) + z_streamp strm; + int level; + int method; + int windowBits; + int memLevel; + int strategy; + const char *version; + int stream_size; +{ + deflate_state *s; + int wrap = 1; + static const char my_version[] = ZLIB_VERSION; + + ushf *overlay; + /* We overlay pending_buf and d_buf+l_buf. This works since the average + * output size for (length,distance) codes is <= 24 bits. + */ + + if (version == Z_NULL || version[0] != my_version[0] || + stream_size != sizeof(z_stream)) { + return Z_VERSION_ERROR; + } + if (strm == Z_NULL) return Z_STREAM_ERROR; + + strm->msg = Z_NULL; + if (strm->zalloc == (alloc_func)0) { + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; + } + if (strm->zfree == (free_func)0) strm->zfree = zcfree; + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + + if (windowBits < 0) { /* suppress zlib wrapper */ + wrap = 0; + windowBits = -windowBits; + } +#ifdef GZIP + else if (windowBits > 15) { + wrap = 2; /* write gzip wrapper instead */ + windowBits -= 16; + } +#endif + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || + windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + strategy < 0 || strategy > Z_FIXED) { + return Z_STREAM_ERROR; + } + if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); + if (s == Z_NULL) return Z_MEM_ERROR; + strm->state = (struct internal_state FAR *)s; + s->strm = strm; + + s->wrap = wrap; + s->gzhead = Z_NULL; + s->w_bits = windowBits; + s->w_size = 1 << s->w_bits; + s->w_mask = s->w_size - 1; + + s->hash_bits = memLevel + 7; + s->hash_size = 1 << s->hash_bits; + s->hash_mask = s->hash_size - 1; + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); + + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); + + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ + + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); + s->pending_buf = (uchf *) overlay; + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); + + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || + s->pending_buf == Z_NULL) { + s->status = FINISH_STATE; + strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); + deflateEnd (strm); + return Z_MEM_ERROR; + } + s->d_buf = overlay + s->lit_bufsize/sizeof(ush); + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; + + s->level = level; + s->strategy = strategy; + s->method = (Byte)method; + + return deflateReset(strm); +} + +/* ========================================================================= */ +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) + z_streamp strm; + const Bytef *dictionary; + uInt dictLength; +{ + deflate_state *s; + uInt length = dictLength; + uInt n; + IPos hash_head = 0; + + if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || + strm->state->wrap == 2 || + (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) + return Z_STREAM_ERROR; + + s = strm->state; + if (s->wrap) + strm->adler = adler32(strm->adler, dictionary, dictLength); + + if (length < MIN_MATCH) return Z_OK; + if (length > MAX_DIST(s)) { + length = MAX_DIST(s); + dictionary += dictLength - length; /* use the tail of the dictionary */ + } + zmemcpy(s->window, dictionary, length); + s->strstart = length; + s->block_start = (long)length; + + /* Insert all strings in the hash table (except for the last two bytes). + * s->lookahead stays null, so s->ins_h will be recomputed at the next + * call of fill_window. + */ + s->ins_h = s->window[0]; + UPDATE_HASH(s, s->ins_h, s->window[1]); + for (n = 0; n <= length - MIN_MATCH; n++) { + INSERT_STRING(s, n, hash_head); + } + if (hash_head) hash_head = 0; /* to make compiler happy */ + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateReset (strm) + z_streamp strm; +{ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { + return Z_STREAM_ERROR; + } + + strm->total_in = strm->total_out = 0; + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ + strm->data_type = Z_UNKNOWN; + + s = (deflate_state *)strm->state; + s->pending = 0; + s->pending_out = s->pending_buf; + + if (s->wrap < 0) { + s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ + } + s->status = s->wrap ? INIT_STATE : BUSY_STATE; + strm->adler = +#ifdef GZIP + s->wrap == 2 ? crc32(0L, Z_NULL, 0) : +#endif + adler32(0L, Z_NULL, 0); + s->last_flush = Z_NO_FLUSH; + + _tr_init(s); + lm_init(s); + + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateSetHeader (strm, head) + z_streamp strm; + gz_headerp head; +{ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm->state->wrap != 2) return Z_STREAM_ERROR; + strm->state->gzhead = head; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflatePrime (strm, bits, value) + z_streamp strm; + int bits; + int value; +{ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + strm->state->bi_valid = bits; + strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateParams(strm, level, strategy) + z_streamp strm; + int level; + int strategy; +{ + deflate_state *s; + compress_func func; + int err = Z_OK; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { + return Z_STREAM_ERROR; + } + func = configuration_table[s->level].func; + + if (func != configuration_table[level].func && strm->total_in != 0) { + /* Flush the last buffer: */ + err = deflate(strm, Z_PARTIAL_FLUSH); + } + if (s->level != level) { + s->level = level; + s->max_lazy_match = configuration_table[level].max_lazy; + s->good_match = configuration_table[level].good_length; + s->nice_match = configuration_table[level].nice_length; + s->max_chain_length = configuration_table[level].max_chain; + } + s->strategy = strategy; + return err; +} + +/* ========================================================================= */ +int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) + z_streamp strm; + int good_length; + int max_lazy; + int nice_length; + int max_chain; +{ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; + s->good_match = good_length; + s->max_lazy_match = max_lazy; + s->nice_match = nice_length; + s->max_chain_length = max_chain; + return Z_OK; +} + +/* ========================================================================= + * For the default windowBits of 15 and memLevel of 8, this function returns + * a close to exact, as well as small, upper bound on the compressed size. + * They are coded as constants here for a reason--if the #define's are + * changed, then this function needs to be changed as well. The return + * value for 15 and 8 only works for those exact settings. + * + * For any setting other than those defaults for windowBits and memLevel, + * the value returned is a conservative worst case for the maximum expansion + * resulting from using fixed blocks instead of stored blocks, which deflate + * can emit on compressed data for some combinations of the parameters. + * + * This function could be more sophisticated to provide closer upper bounds + * for every combination of windowBits and memLevel, as well as wrap. + * But even the conservative upper bound of about 14% expansion does not + * seem onerous for output buffer allocation. + */ +uLong ZEXPORT deflateBound(strm, sourceLen) + z_streamp strm; + uLong sourceLen; +{ + deflate_state *s; + uLong destLen; + + /* conservative upper bound */ + destLen = sourceLen + + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11; + + /* if can't get parameters, return conservative bound */ + if (strm == Z_NULL || strm->state == Z_NULL) + return destLen; + + /* if not default parameters, return conservative bound */ + s = strm->state; + if (s->w_bits != 15 || s->hash_bits != 8 + 7) + return destLen; + + /* default settings: return tight bound for that case */ + return compressBound(sourceLen); +} + +/* ========================================================================= + * Put a short in the pending buffer. The 16-bit value is put in MSB order. + * IN assertion: the stream state is correct and there is enough room in + * pending_buf. + */ +local void putShortMSB (s, b) + deflate_state *s; + uInt b; +{ + put_byte(s, (Byte)(b >> 8)); + put_byte(s, (Byte)(b & 0xff)); +} + +/* ========================================================================= + * Flush as much pending output as possible. All deflate() output goes + * through this function so some applications may wish to modify it + * to avoid allocating a large strm->next_out buffer and copying into it. + * (See also read_buf()). + */ +local void flush_pending(strm) + z_streamp strm; +{ + unsigned len = strm->state->pending; + + if (len > strm->avail_out) len = strm->avail_out; + if (len == 0) return; + + zmemcpy(strm->next_out, strm->state->pending_out, len); + strm->next_out += len; + strm->state->pending_out += len; + strm->total_out += len; + strm->avail_out -= len; + strm->state->pending -= len; + if (strm->state->pending == 0) { + strm->state->pending_out = strm->state->pending_buf; + } +} + +/* ========================================================================= */ +int ZEXPORT deflate (strm, flush) + z_streamp strm; + int flush; +{ + int old_flush; /* value of flush param for previous deflate call */ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL || + flush > Z_FINISH || flush < 0) { + return Z_STREAM_ERROR; + } + s = strm->state; + + if (strm->next_out == Z_NULL || + (strm->next_in == Z_NULL && strm->avail_in != 0) || + (s->status == FINISH_STATE && flush != Z_FINISH)) { + ERR_RETURN(strm, Z_STREAM_ERROR); + } + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); + + s->strm = strm; /* just in case */ + old_flush = s->last_flush; + s->last_flush = flush; + + /* Write the header */ + if (s->status == INIT_STATE) { +#ifdef GZIP + if (s->wrap == 2) { + strm->adler = crc32(0L, Z_NULL, 0); + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + if (s->gzhead == NULL) { + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, OS_CODE); + s->status = BUSY_STATE; + } + else { + put_byte(s, (s->gzhead->text ? 1 : 0) + + (s->gzhead->hcrc ? 2 : 0) + + (s->gzhead->extra == Z_NULL ? 0 : 4) + + (s->gzhead->name == Z_NULL ? 0 : 8) + + (s->gzhead->comment == Z_NULL ? 0 : 16) + ); + put_byte(s, (Byte)(s->gzhead->time & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, s->gzhead->os & 0xff); + if (s->gzhead->extra != NULL) { + put_byte(s, s->gzhead->extra_len & 0xff); + put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); + } + if (s->gzhead->hcrc) + strm->adler = crc32(strm->adler, s->pending_buf, + s->pending); + s->gzindex = 0; + s->status = EXTRA_STATE; + } + } + else +#endif + { + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; + uInt level_flags; + + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) + level_flags = 0; + else if (s->level < 6) + level_flags = 1; + else if (s->level == 6) + level_flags = 2; + else + level_flags = 3; + header |= (level_flags << 6); + if (s->strstart != 0) header |= PRESET_DICT; + header += 31 - (header % 31); + + s->status = BUSY_STATE; + putShortMSB(s, header); + + /* Save the adler32 of the preset dictionary: */ + if (s->strstart != 0) { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + strm->adler = adler32(0L, Z_NULL, 0); + } + } +#ifdef GZIP + if (s->status == EXTRA_STATE) { + if (s->gzhead->extra != NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + + while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) + break; + } + put_byte(s, s->gzhead->extra[s->gzindex]); + s->gzindex++; + } + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (s->gzindex == s->gzhead->extra_len) { + s->gzindex = 0; + s->status = NAME_STATE; + } + } + else + s->status = NAME_STATE; + } + if (s->status == NAME_STATE) { + if (s->gzhead->name != NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + int val; + + do { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) { + val = 1; + break; + } + } + val = s->gzhead->name[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (val == 0) { + s->gzindex = 0; + s->status = COMMENT_STATE; + } + } + else + s->status = COMMENT_STATE; + } + if (s->status == COMMENT_STATE) { + if (s->gzhead->comment != NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + int val; + + do { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) { + val = 1; + break; + } + } + val = s->gzhead->comment[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (val == 0) + s->status = HCRC_STATE; + } + else + s->status = HCRC_STATE; + } + if (s->status == HCRC_STATE) { + if (s->gzhead->hcrc) { + if (s->pending + 2 > s->pending_buf_size) + flush_pending(strm); + if (s->pending + 2 <= s->pending_buf_size) { + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + strm->adler = crc32(0L, Z_NULL, 0); + s->status = BUSY_STATE; + } + } + else + s->status = BUSY_STATE; + } +#endif + + /* Flush as much pending output as possible */ + if (s->pending != 0) { + flush_pending(strm); + if (strm->avail_out == 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s->last_flush = -1; + return Z_OK; + } + + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } else if (strm->avail_in == 0 && flush <= old_flush && + flush != Z_FINISH) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* User must not provide more input after the first FINISH: */ + if (s->status == FINISH_STATE && strm->avail_in != 0) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* Start a new block or continue the current one. + */ + if (strm->avail_in != 0 || s->lookahead != 0 || + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { + block_state bstate; + + bstate = (*(configuration_table[s->level].func))(s, flush); + + if (bstate == finish_started || bstate == finish_done) { + s->status = FINISH_STATE; + } + if (bstate == need_more || bstate == finish_started) { + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ + } + return Z_OK; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } + if (bstate == block_done) { + if (flush == Z_PARTIAL_FLUSH) { + _tr_align(s); + } else { /* FULL_FLUSH or SYNC_FLUSH */ + _tr_stored_block(s, (char*)0, 0L, 0); + /* For a full flush, this empty block will be recognized + * as a special marker by inflate_sync(). + */ + if (flush == Z_FULL_FLUSH) { + CLEAR_HASH(s); /* forget history */ + } + } + flush_pending(strm); + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; + } + } + } + Assert(strm->avail_out > 0, "bug2"); + + if (flush != Z_FINISH) return Z_OK; + if (s->wrap <= 0) return Z_STREAM_END; + + /* Write the trailer */ +#ifdef GZIP + if (s->wrap == 2) { + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); + put_byte(s, (Byte)(strm->total_in & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); + } + else +#endif + { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + flush_pending(strm); + /* If avail_out is zero, the application will call deflate again + * to flush the rest. + */ + if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ + return s->pending != 0 ? Z_OK : Z_STREAM_END; +} + +/* ========================================================================= */ +int ZEXPORT deflateEnd (strm) + z_streamp strm; +{ + int status; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + + status = strm->state->status; + if (status != INIT_STATE && + status != EXTRA_STATE && + status != NAME_STATE && + status != COMMENT_STATE && + status != HCRC_STATE && + status != BUSY_STATE && + status != FINISH_STATE) { + return Z_STREAM_ERROR; + } + + /* Deallocate in reverse order of allocations: */ + TRY_FREE(strm, strm->state->pending_buf); + TRY_FREE(strm, strm->state->head); + TRY_FREE(strm, strm->state->prev); + TRY_FREE(strm, strm->state->window); + + ZFREE(strm, strm->state); + strm->state = Z_NULL; + + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; +} + +/* ========================================================================= + * Copy the source state to the destination state. + * To simplify the source, this is not supported for 16-bit MSDOS (which + * doesn't have enough memory anyway to duplicate compression states). + */ +int ZEXPORT deflateCopy (dest, source) + z_streamp dest; + z_streamp source; +{ +#ifdef MAXSEG_64K + return Z_STREAM_ERROR; +#else + deflate_state *ds; + deflate_state *ss; + ushf *overlay; + + + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { + return Z_STREAM_ERROR; + } + + ss = source->state; + + zmemcpy(dest, source, sizeof(z_stream)); + + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); + if (ds == Z_NULL) return Z_MEM_ERROR; + dest->state = (struct internal_state FAR *) ds; + zmemcpy(ds, ss, sizeof(deflate_state)); + ds->strm = dest; + + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); + ds->pending_buf = (uchf *) overlay; + + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || + ds->pending_buf == Z_NULL) { + deflateEnd (dest); + return Z_MEM_ERROR; + } + /* following zmemcpy do not work for 16-bit MSDOS */ + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); + zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); + zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); + + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; + + ds->l_desc.dyn_tree = ds->dyn_ltree; + ds->d_desc.dyn_tree = ds->dyn_dtree; + ds->bl_desc.dyn_tree = ds->bl_tree; + + return Z_OK; +#endif /* MAXSEG_64K */ +} + +/* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->next_in buffer and copying from it. + * (See also flush_pending()). + */ +local int read_buf(strm, buf, size) + z_streamp strm; + Bytef *buf; + unsigned size; +{ + unsigned len = strm->avail_in; + + if (len > size) len = size; + if (len == 0) return 0; + + strm->avail_in -= len; + + if (strm->state->wrap == 1) { + strm->adler = adler32(strm->adler, strm->next_in, len); + } +#ifdef GZIP + else if (strm->state->wrap == 2) { + strm->adler = crc32(strm->adler, strm->next_in, len); + } +#endif + zmemcpy(buf, strm->next_in, len); + strm->next_in += len; + strm->total_in += len; + + return (int)len; +} + +/* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ +local void lm_init (s) + deflate_state *s; +{ + s->window_size = (ulg)2L*s->w_size; + + CLEAR_HASH(s); + + /* Set the default configuration parameters: + */ + s->max_lazy_match = configuration_table[s->level].max_lazy; + s->good_match = configuration_table[s->level].good_length; + s->nice_match = configuration_table[s->level].nice_length; + s->max_chain_length = configuration_table[s->level].max_chain; + + s->strstart = 0; + s->block_start = 0L; + s->lookahead = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + s->ins_h = 0; +#ifndef FASTEST +#ifdef ASMV + match_init(); /* initialize the asm code */ +#endif +#endif +} + +#ifndef FASTEST +/* =========================================================================== + * Set match_start to the longest match starting at the given string and + * return its length. Matches shorter or equal to prev_length are discarded, + * in which case the result is equal to prev_length and match_start is + * garbage. + * IN assertions: cur_match is the head of the hash chain for the current + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 + * OUT assertion: the match length is not greater than s->lookahead. + */ +#ifndef ASMV +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or + * match.S. The code will be functionally equivalent. + */ +local uInt longest_match(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + unsigned chain_length = s->max_chain_length;/* max hash chain length */ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + int best_len = s->prev_length; /* best match length so far */ + int nice_match = s->nice_match; /* stop if match long enough */ + IPos limit = s->strstart > (IPos)MAX_DIST(s) ? + s->strstart - (IPos)MAX_DIST(s) : NIL; + /* Stop when cur_match becomes <= limit. To simplify the code, + * we prevent matches with the string of window index 0. + */ + Posf *prev = s->prev; + uInt wmask = s->w_mask; + +#ifdef UNALIGNED_OK + /* Compare two bytes at a time. Note: this is not always beneficial. + * Try with and without -DUNALIGNED_OK to check. + */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; + register ush scan_start = *(ushf*)scan; + register ush scan_end = *(ushf*)(scan+best_len-1); +#else + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + register Byte scan_end1 = scan[best_len-1]; + register Byte scan_end = scan[best_len]; +#endif + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + /* Do not waste too much time if we already have a good match: */ + if (s->prev_length >= s->good_match) { + chain_length >>= 2; + } + /* Do not look for matches beyond the end of the input. This is necessary + * to make deflate deterministic. + */ + if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + do { + Assert(cur_match < s->strstart, "no future"); + match = s->window + cur_match; + + /* Skip to next match if the match length cannot increase + * or if the match length is less than 2. Note that the checks below + * for insufficient lookahead only occur occasionally for performance + * reasons. Therefore uninitialized memory will be accessed, and + * conditional jumps will be made that depend on those values. + * However the length of the match is limited to the lookahead, so + * the output of deflate is not affected by the uninitialized values. + */ +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258) + /* This code assumes sizeof(unsigned short) == 2. Do not use + * UNALIGNED_OK if your compiler uses a different size. + */ + if (*(ushf*)(match+best_len-1) != scan_end || + *(ushf*)match != scan_start) continue; + + /* It is not necessary to compare scan[2] and match[2] since they are + * always equal when the other bytes match, given that the hash keys + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at + * strstart+3, +5, ... up to strstart+257. We check for insufficient + * lookahead only every 4th comparison; the 128th check will be made + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is + * necessary to put more guard bytes at the end of the window, or + * to check more often for insufficient lookahead. + */ + Assert(scan[2] == match[2], "scan[2]?"); + scan++, match++; + do { + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + scan < strend); + /* The funny "do {}" generates better code on most compilers */ + + /* Here, scan <= window+strstart+257 */ + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + if (*scan == *match) scan++; + + len = (MAX_MATCH - 1) - (int)(strend-scan); + scan = strend - (MAX_MATCH-1); + +#else /* UNALIGNED_OK */ + + if (match[best_len] != scan_end || + match[best_len-1] != scan_end1 || + *match != *scan || + *++match != scan[1]) continue; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match++; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + scan = strend - MAX_MATCH; + +#endif /* UNALIGNED_OK */ + + if (len > best_len) { + s->match_start = cur_match; + best_len = len; + if (len >= nice_match) break; +#ifdef UNALIGNED_OK + scan_end = *(ushf*)(scan+best_len-1); +#else + scan_end1 = scan[best_len-1]; + scan_end = scan[best_len]; +#endif + } + } while ((cur_match = prev[cur_match & wmask]) > limit + && --chain_length != 0); + + if ((uInt)best_len <= s->lookahead) return (uInt)best_len; + return s->lookahead; +} +#endif /* ASMV */ +#endif /* FASTEST */ + +/* --------------------------------------------------------------------------- + * Optimized version for level == 1 or strategy == Z_RLE only + */ +local uInt longest_match_fast(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + Assert(cur_match < s->strstart, "no future"); + + match = s->window + cur_match; + + /* Return failure if the match length is less than 2: + */ + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match += 2; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + + if (len < MIN_MATCH) return MIN_MATCH - 1; + + s->match_start = cur_match; + return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; +} + +#ifdef DEBUG +/* =========================================================================== + * Check that the match at match_start is indeed a match. + */ +local void check_match(s, start, match, length) + deflate_state *s; + IPos start, match; + int length; +{ + /* check that the match is indeed a match */ + if (zmemcmp(s->window + match, + s->window + start, length) != EQUAL) { + fprintf(stderr, " start %u, match %u, length %d\n", + start, match, length); + do { + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); + } while (--length != 0); + z_error("invalid match"); + } + if (z_verbose > 1) { + fprintf(stderr,"\\[%d,%d]", start-match, length); + do { putc(s->window[start++], stderr); } while (--length != 0); + } +} +#else +# define check_match(s, start, match, length) +#endif /* DEBUG */ + +/* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ +local void fill_window(s) + deflate_state *s; +{ + register unsigned n, m; + register Posf *p; + unsigned more; /* Amount of free space at the end of the window. */ + uInt wsize = s->w_size; + + do { + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); + + /* Deal with !@#$% 64K limit: */ + if (sizeof(int) <= 2) { + if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + more = wsize; + + } else if (more == (unsigned)(-1)) { + /* Very unlikely, but possible on 16 bit machine if + * strstart == 0 && lookahead == 1 (input done a byte at time) + */ + more--; + } + } + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + if (s->strstart >= wsize+MAX_DIST(s)) { + + zmemcpy(s->window, s->window+wsize, (unsigned)wsize); + s->match_start -= wsize; + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ + s->block_start -= (long) wsize; + + /* Slide the hash table (could be avoided with 32 bit values + at the expense of memory usage). We slide even when level == 0 + to keep the hash table consistent if we switch back to level > 0 + later. (Using level 0 permanently is not an optimal usage of + zlib, so we don't care about this pathological case.) + */ + /* %%% avoid this when Z_RLE */ + n = s->hash_size; + p = &s->head[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m-wsize : NIL); + } while (--n); + + n = wsize; +#ifndef FASTEST + p = &s->prev[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m-wsize : NIL); + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); +#endif + more += wsize; + } + if (s->strm->avail_in == 0) return; + + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + Assert(more >= 2, "more < 2"); + + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); + s->lookahead += n; + + /* Initialize the hash value now that we have some input: */ + if (s->lookahead >= MIN_MATCH) { + s->ins_h = s->window[s->strstart]; + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); +} + +/* =========================================================================== + * Flush the current block, with given end-of-file flag. + * IN assertion: strstart is set to the end of the current match. + */ +#define FLUSH_BLOCK_ONLY(s, eof) { \ + _tr_flush_block(s, (s->block_start >= 0L ? \ + (charf *)&s->window[(unsigned)s->block_start] : \ + (charf *)Z_NULL), \ + (ulg)((long)s->strstart - s->block_start), \ + (eof)); \ + s->block_start = s->strstart; \ + flush_pending(s->strm); \ + Tracev((stderr,"[FLUSH]")); \ +} + +/* Same but force premature exit if necessary. */ +#define FLUSH_BLOCK(s, eof) { \ + FLUSH_BLOCK_ONLY(s, eof); \ + if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \ +} + +/* =========================================================================== + * Copy without compression as much as possible from the input stream, return + * the current block state. + * This function does not insert new strings in the dictionary since + * uncompressible data is probably not useful. This function is used + * only for the level=0 compression option. + * NOTE: this function should be optimized to avoid extra copying from + * window to pending_buf. + */ +local block_state deflate_stored(s, flush) + deflate_state *s; + int flush; +{ + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited + * to pending_buf_size, and each stored block has a 5 byte header: + */ + ulg max_block_size = 0xffff; + ulg max_start; + + if (max_block_size > s->pending_buf_size - 5) { + max_block_size = s->pending_buf_size - 5; + } + + /* Copy as much as possible from input to output: */ + for (;;) { + /* Fill the window as much as possible: */ + if (s->lookahead <= 1) { + + Assert(s->strstart < s->w_size+MAX_DIST(s) || + s->block_start >= (long)s->w_size, "slide too late"); + + fill_window(s); + if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; + + if (s->lookahead == 0) break; /* flush the current block */ + } + Assert(s->block_start >= 0L, "block gone"); + + s->strstart += s->lookahead; + s->lookahead = 0; + + /* Emit a stored block if pending_buf will be full: */ + max_start = s->block_start + max_block_size; + if (s->strstart == 0 || (ulg)s->strstart >= max_start) { + /* strstart == 0 is possible when wraparound on 16-bit machine */ + s->lookahead = (uInt)(s->strstart - max_start); + s->strstart = (uInt)max_start; + FLUSH_BLOCK(s, 0); + } + /* Flush if we may have to slide, otherwise block_start may become + * negative and the data will be gone: + */ + if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { + FLUSH_BLOCK(s, 0); + } + } + FLUSH_BLOCK(s, flush == Z_FINISH); + return flush == Z_FINISH ? finish_done : block_done; +} + +/* =========================================================================== + * Compress as much as possible from the input stream, return the current + * block state. + * This function does not perform lazy evaluation of matches and inserts + * new strings in the dictionary only for unmatched strings or for short + * matches. It is used only for the fast compression options. + */ +local block_state deflate_fast(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head = NIL; /* head of the hash chain */ + int bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + * At this point we have always match_length < MIN_MATCH + */ + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ +#ifdef FASTEST + if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) || + (s->strategy == Z_RLE && s->strstart - hash_head == 1)) { + s->match_length = longest_match_fast (s, hash_head); + } +#else + if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { + s->match_length = longest_match (s, hash_head); + } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + s->match_length = longest_match_fast (s, hash_head); + } +#endif + /* longest_match() or longest_match_fast() sets match_start */ + } + if (s->match_length >= MIN_MATCH) { + check_match(s, s->strstart, s->match_start, s->match_length); + + _tr_tally_dist(s, s->strstart - s->match_start, + s->match_length - MIN_MATCH, bflush); + + s->lookahead -= s->match_length; + + /* Insert new strings in the hash table only if the match length + * is not too large. This saves time but degrades compression. + */ +#ifndef FASTEST + if (s->match_length <= s->max_insert_length && + s->lookahead >= MIN_MATCH) { + s->match_length--; /* string at strstart already in table */ + do { + s->strstart++; + INSERT_STRING(s, s->strstart, hash_head); + /* strstart never exceeds WSIZE-MAX_MATCH, so there are + * always MIN_MATCH bytes ahead. + */ + } while (--s->match_length != 0); + s->strstart++; + } else +#endif + { + s->strstart += s->match_length; + s->match_length = 0; + s->ins_h = s->window[s->strstart]; + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not + * matter since it will be recomputed at next deflate call. + */ + } + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + FLUSH_BLOCK(s, flush == Z_FINISH); + return flush == Z_FINISH ? finish_done : block_done; +} + +#ifndef FASTEST +/* =========================================================================== + * Same as above, but achieves better compression. We use a lazy + * evaluation for matches: a match is finally adopted only if there is + * no better match at the next window position. + */ +local block_state deflate_slow(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head = NIL; /* head of hash chain */ + int bflush; /* set if current block must be flushed */ + + /* Process the input block. */ + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + */ + s->prev_length = s->match_length, s->prev_match = s->match_start; + s->match_length = MIN_MATCH-1; + + if (hash_head != NIL && s->prev_length < s->max_lazy_match && + s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { + s->match_length = longest_match (s, hash_head); + } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + s->match_length = longest_match_fast (s, hash_head); + } + /* longest_match() or longest_match_fast() sets match_start */ + + if (s->match_length <= 5 && (s->strategy == Z_FILTERED +#if TOO_FAR <= 32767 + || (s->match_length == MIN_MATCH && + s->strstart - s->match_start > TOO_FAR) +#endif + )) { + + /* If prev_match is also MIN_MATCH, match_start is garbage + * but we will ignore the current match anyway. + */ + s->match_length = MIN_MATCH-1; + } + } + /* If there was a match at the previous step and the current + * match is not better, output the previous match: + */ + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; + /* Do not insert strings in hash table beyond this. */ + + check_match(s, s->strstart-1, s->prev_match, s->prev_length); + + _tr_tally_dist(s, s->strstart -1 - s->prev_match, + s->prev_length - MIN_MATCH, bflush); + + /* Insert in hash table all strings up to the end of the match. + * strstart-1 and strstart are already inserted. If there is not + * enough lookahead, the last two strings are not inserted in + * the hash table. + */ + s->lookahead -= s->prev_length-1; + s->prev_length -= 2; + do { + if (++s->strstart <= max_insert) { + INSERT_STRING(s, s->strstart, hash_head); + } + } while (--s->prev_length != 0); + s->match_available = 0; + s->match_length = MIN_MATCH-1; + s->strstart++; + + if (bflush) FLUSH_BLOCK(s, 0); + + } else if (s->match_available) { + /* If there was no match at the previous position, output a + * single literal. If there was a match but the current match + * is longer, truncate the previous match to a single literal. + */ + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + if (bflush) { + FLUSH_BLOCK_ONLY(s, 0); + } + s->strstart++; + s->lookahead--; + if (s->strm->avail_out == 0) return need_more; + } else { + /* There is no previous match to compare with, wait for + * the next step to decide. + */ + s->match_available = 1; + s->strstart++; + s->lookahead--; + } + } + Assert (flush != Z_NO_FLUSH, "no flush?"); + if (s->match_available) { + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + s->match_available = 0; + } + FLUSH_BLOCK(s, flush == Z_FINISH); + return flush == Z_FINISH ? finish_done : block_done; +} +#endif /* FASTEST */ + +#if 0 +/* =========================================================================== + * For Z_RLE, simply look for runs of bytes, generate matches only of distance + * one. Do not maintain a hash table. (It will be regenerated if this run of + * deflate switches away from Z_RLE.) + */ +local block_state deflate_rle(s, flush) + deflate_state *s; + int flush; +{ + int bflush; /* set if current block must be flushed */ + uInt run; /* length of run */ + uInt max; /* maximum length of run */ + uInt prev; /* byte at distance one to match */ + Bytef *scan; /* scan for end of run */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the longest encodable run. + */ + if (s->lookahead < MAX_MATCH) { + fill_window(s); + if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* See how many times the previous byte repeats */ + run = 0; + if (s->strstart > 0) { /* if there is a previous byte, that is */ + max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH; + scan = s->window + s->strstart - 1; + prev = *scan++; + do { + if (*scan++ != prev) + break; + } while (++run < max); + } + + /* Emit match if have run of MIN_MATCH or longer, else emit literal */ + if (run >= MIN_MATCH) { + check_match(s, s->strstart, s->strstart - 1, run); + _tr_tally_dist(s, 1, run - MIN_MATCH, bflush); + s->lookahead -= run; + s->strstart += run; + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + FLUSH_BLOCK(s, flush == Z_FINISH); + return flush == Z_FINISH ? finish_done : block_done; +} +#endif diff --git a/zlib/deflate.h b/zlib/deflate.h new file mode 100644 index 00000000..05a5ab3a --- /dev/null +++ b/zlib/deflate.h @@ -0,0 +1,331 @@ +/* deflate.h -- internal compression state + * Copyright (C) 1995-2004 Jean-loup Gailly + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef DEFLATE_H +#define DEFLATE_H + +#include "zutil.h" + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer creation by deflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip encoding + should be left enabled. */ +#ifndef NO_GZIP +# define GZIP +#endif + +/* =========================================================================== + * Internal compression state. + */ + +#define LENGTH_CODES 29 +/* number of length codes, not counting the special END_BLOCK code */ + +#define LITERALS 256 +/* number of literal bytes 0..255 */ + +#define L_CODES (LITERALS+1+LENGTH_CODES) +/* number of Literal or Length codes, including the END_BLOCK code */ + +#define D_CODES 30 +/* number of distance codes */ + +#define BL_CODES 19 +/* number of codes used to transfer the bit lengths */ + +#define HEAP_SIZE (2*L_CODES+1) +/* maximum heap size */ + +#define MAX_BITS 15 +/* All codes must not exceed MAX_BITS bits */ + +#define INIT_STATE 42 +#define EXTRA_STATE 69 +#define NAME_STATE 73 +#define COMMENT_STATE 91 +#define HCRC_STATE 103 +#define BUSY_STATE 113 +#define FINISH_STATE 666 +/* Stream status */ + + +/* Data structure describing a single value and its code string. */ +typedef struct ct_data_s { + union { + ush freq; /* frequency count */ + ush code; /* bit string */ + } fc; + union { + ush dad; /* father node in Huffman tree */ + ush len; /* length of bit string */ + } dl; +} FAR ct_data; + +#define Freq fc.freq +#define Code fc.code +#define Dad dl.dad +#define Len dl.len + +typedef struct static_tree_desc_s static_tree_desc; + +typedef struct tree_desc_s { + ct_data *dyn_tree; /* the dynamic tree */ + int max_code; /* largest code with non zero frequency */ + static_tree_desc *stat_desc; /* the corresponding static tree */ +} FAR tree_desc; + +typedef ush Pos; +typedef Pos FAR Posf; +typedef unsigned IPos; + +/* A Pos is an index in the character window. We use short instead of int to + * save space in the various tables. IPos is used only for parameter passing. + */ + +typedef struct internal_state { + z_streamp strm; /* pointer back to this zlib stream */ + int status; /* as the name implies */ + Bytef *pending_buf; /* output still pending */ + ulg pending_buf_size; /* size of pending_buf */ + Bytef *pending_out; /* next pending byte to output to the stream */ + uInt pending; /* nb of bytes in the pending buffer */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + gz_headerp gzhead; /* gzip header information to write */ + uInt gzindex; /* where in extra, name, or comment */ + Byte method; /* STORED (for zip only) or DEFLATED */ + int last_flush; /* value of flush param for previous deflate call */ + + /* used by deflate.c: */ + + uInt w_size; /* LZ77 window size (32K by default) */ + uInt w_bits; /* log2(w_size) (8..16) */ + uInt w_mask; /* w_size - 1 */ + + Bytef *window; + /* Sliding window. Input bytes are read into the second half of the window, + * and move to the first half later to keep a dictionary of at least wSize + * bytes. With this organization, matches are limited to a distance of + * wSize-MAX_MATCH bytes, but this ensures that IO is always + * performed with a length multiple of the block size. Also, it limits + * the window size to 64K, which is quite useful on MSDOS. + * To do: use the user input buffer as sliding window. + */ + + ulg window_size; + /* Actual size of window: 2*wSize, except when the user input buffer + * is directly used as sliding window. + */ + + Posf *prev; + /* Link to older string with same hash index. To limit the size of this + * array to 64K, this link is maintained only for the last 32K strings. + * An index in this array is thus a window index modulo 32K. + */ + + Posf *head; /* Heads of the hash chains or NIL. */ + + uInt ins_h; /* hash index of string to be inserted */ + uInt hash_size; /* number of elements in hash table */ + uInt hash_bits; /* log2(hash_size) */ + uInt hash_mask; /* hash_size-1 */ + + uInt hash_shift; + /* Number of bits by which ins_h must be shifted at each input + * step. It must be such that after MIN_MATCH steps, the oldest + * byte no longer takes part in the hash key, that is: + * hash_shift * MIN_MATCH >= hash_bits + */ + + long block_start; + /* Window position at the beginning of the current output block. Gets + * negative when the window is moved backwards. + */ + + uInt match_length; /* length of best match */ + IPos prev_match; /* previous match */ + int match_available; /* set if previous match exists */ + uInt strstart; /* start of string to insert */ + uInt match_start; /* start of matching string */ + uInt lookahead; /* number of valid bytes ahead in window */ + + uInt prev_length; + /* Length of the best match at previous step. Matches not greater than this + * are discarded. This is used in the lazy match evaluation. + */ + + uInt max_chain_length; + /* To speed up deflation, hash chains are never searched beyond this + * length. A higher limit improves compression ratio but degrades the + * speed. + */ + + uInt max_lazy_match; + /* Attempt to find a better match only when the current match is strictly + * smaller than this value. This mechanism is used only for compression + * levels >= 4. + */ +# define max_insert_length max_lazy_match + /* Insert new strings in the hash table only if the match length is not + * greater than this length. This saves time but degrades compression. + * max_insert_length is used only for compression levels <= 3. + */ + + int level; /* compression level (1..9) */ + int strategy; /* favor or force Huffman coding*/ + + uInt good_match; + /* Use a faster search when the previous match is longer than this */ + + int nice_match; /* Stop searching when current match exceeds this */ + + /* used by trees.c: */ + /* Didn't use ct_data typedef below to supress compiler warning */ + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ + + struct tree_desc_s l_desc; /* desc. for literal tree */ + struct tree_desc_s d_desc; /* desc. for distance tree */ + struct tree_desc_s bl_desc; /* desc. for bit length tree */ + + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ + int heap_len; /* number of elements in the heap */ + int heap_max; /* element of largest frequency */ + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. + * The same heap array is used to build all trees. + */ + + uch depth[2*L_CODES+1]; + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + + uchf *l_buf; /* buffer for literals or lengths */ + + uInt lit_bufsize; + /* Size of match buffer for literals/lengths. There are 4 reasons for + * limiting lit_bufsize to 64K: + * - frequencies can be kept in 16 bit counters + * - if compression is not successful for the first block, all input + * data is still in the window so we can still emit a stored block even + * when input comes from standard input. (This can also be done for + * all blocks if lit_bufsize is not greater than 32K.) + * - if compression is not successful for a file smaller than 64K, we can + * even emit a stored file instead of a stored block (saving 5 bytes). + * This is applicable only for zip (not gzip or zlib). + * - creating new Huffman trees less frequently may not provide fast + * adaptation to changes in the input data statistics. (Take for + * example a binary file with poorly compressible code followed by + * a highly compressible string table.) Smaller buffer sizes give + * fast adaptation but have of course the overhead of transmitting + * trees more frequently. + * - I can't count above 4 + */ + + uInt last_lit; /* running index in l_buf */ + + ushf *d_buf; + /* Buffer for distances. To simplify the code, d_buf and l_buf have + * the same number of elements. To use different lengths, an extra flag + * array would be necessary. + */ + + ulg opt_len; /* bit length of current block with optimal trees */ + ulg static_len; /* bit length of current block with static trees */ + uInt matches; /* number of string matches in current block */ + int last_eob_len; /* bit length of EOB code for last block */ + +#ifdef DEBUG + ulg compressed_len; /* total bit length of compressed file mod 2^32 */ + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ +#endif + + ush bi_buf; + /* Output buffer. bits are inserted starting at the bottom (least + * significant bits). + */ + int bi_valid; + /* Number of valid bits in bi_buf. All bits above the last valid bit + * are always zero. + */ + +} FAR deflate_state; + +/* Output a byte on the stream. + * IN assertion: there is enough room in pending_buf. + */ +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} + + +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) +/* Minimum amount of lookahead, except at the end of the input file. + * See deflate.c for comments about the MIN_MATCH+1. + */ + +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) +/* In order to simplify the code, particularly on 16 bit machines, match + * distances are limited to MAX_DIST instead of WSIZE. + */ + + /* in trees.c */ +void _tr_init OF((deflate_state *s)); +int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); +void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, + int eof)); +void _tr_align OF((deflate_state *s)); +void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, + int eof)); + +#define d_code(dist) \ + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) +/* Mapping from a distance to a distance code. dist is the distance - 1 and + * must not have side effects. _dist_code[256] and _dist_code[257] are never + * used. + */ + +#ifndef DEBUG +/* Inline versions of _tr_tally for speed: */ + +#if defined(GEN_TREES_H) || !defined(STDC) + extern uch _length_code[]; + extern uch _dist_code[]; +#else + extern const uch _length_code[]; + extern const uch _dist_code[]; +#endif + +# define _tr_tally_lit(s, c, flush) \ + { uch cc = (c); \ + s->d_buf[s->last_lit] = 0; \ + s->l_buf[s->last_lit++] = cc; \ + s->dyn_ltree[cc].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +# define _tr_tally_dist(s, distance, length, flush) \ + { uch len = (length); \ + ush dist = (distance); \ + s->d_buf[s->last_lit] = dist; \ + s->l_buf[s->last_lit++] = len; \ + dist--; \ + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ + s->dyn_dtree[d_code(dist)].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +#else +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) +# define _tr_tally_dist(s, distance, length, flush) \ + flush = _tr_tally(s, distance, length) +#endif + +#endif /* DEFLATE_H */ diff --git a/zlib/gzio.c b/zlib/gzio.c new file mode 100644 index 00000000..7e90f492 --- /dev/null +++ b/zlib/gzio.c @@ -0,0 +1,1026 @@ +/* gzio.c -- IO on .gz files + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Compile this file with -DNO_GZCOMPRESS to avoid the compression code. + */ + +/* @(#) $Id$ */ + +#include + +#include "zutil.h" + +#ifdef NO_DEFLATE /* for compatibility with old definition */ +# define NO_GZCOMPRESS +#endif + +#ifndef NO_DUMMY_DECL +struct internal_state {int dummy;}; /* for buggy compilers */ +#endif + +#ifndef Z_BUFSIZE +# ifdef MAXSEG_64K +# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */ +# else +# define Z_BUFSIZE 16384 +# endif +#endif +#ifndef Z_PRINTF_BUFSIZE +# define Z_PRINTF_BUFSIZE 4096 +#endif + +#ifdef __MVS__ +# pragma map (fdopen , "\174\174FDOPEN") + FILE *fdopen(int, const char *); +#endif + +#ifndef STDC +extern voidp malloc OF((uInt size)); +extern void free OF((voidpf ptr)); +#endif + +#define ALLOC(size) malloc(size) +#define TRYFREE(p) {if (p) free(p);} + +static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ + +/* gzip flag byte */ +#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ +#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ +#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ +#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ +#define COMMENT 0x10 /* bit 4 set: file comment present */ +#define RESERVED 0xE0 /* bits 5..7: reserved */ + +typedef struct gz_stream { + z_stream stream; + int z_err; /* error code for last stream operation */ + int z_eof; /* set if end of input file */ + FILE *file; /* .gz file */ + Byte *inbuf; /* input buffer */ + Byte *outbuf; /* output buffer */ + uLong crc; /* crc32 of uncompressed data */ + char *msg; /* error message */ + char *path; /* path name for debugging only */ + int transparent; /* 1 if input file is not a .gz file */ + char mode; /* 'w' or 'r' */ + z_off_t start; /* start of compressed data in file (header skipped) */ + z_off_t in; /* bytes into deflate or inflate */ + z_off_t out; /* bytes out of deflate or inflate */ + int back; /* one character push-back */ + int last; /* true if push-back is last character */ +} gz_stream; + + +local gzFile gz_open OF((const char *path, const char *mode, int fd)); +local int do_flush OF((gzFile file, int flush)); +local int get_byte OF((gz_stream *s)); +local void check_header OF((gz_stream *s)); +local int destroy OF((gz_stream *s)); +local void putLong OF((FILE *file, uLong x)); +local uLong getLong OF((gz_stream *s)); + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. The mode parameter + is as in fopen ("rb" or "wb"). The file is given either by file descriptor + or path name (if fd == -1). + gz_open returns NULL if the file could not be opened or if there was + insufficient memory to allocate the (de)compression state; errno + can be checked to distinguish the two cases (if errno is zero, the + zlib error is Z_MEM_ERROR). +*/ +local gzFile gz_open (path, mode, fd) + const char *path; + const char *mode; + int fd; +{ + int err; + int level = Z_DEFAULT_COMPRESSION; /* compression level */ + int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */ + char *p = (char*)mode; + gz_stream *s; + char fmode[80]; /* copy of mode, without the compression level */ + char *m = fmode; + + if (!path || !mode) return Z_NULL; + + s = (gz_stream *)ALLOC(sizeof(gz_stream)); + if (!s) return Z_NULL; + + s->stream.zalloc = (alloc_func)0; + s->stream.zfree = (free_func)0; + s->stream.opaque = (voidpf)0; + s->stream.next_in = s->inbuf = Z_NULL; + s->stream.next_out = s->outbuf = Z_NULL; + s->stream.avail_in = s->stream.avail_out = 0; + s->file = NULL; + s->z_err = Z_OK; + s->z_eof = 0; + s->in = 0; + s->out = 0; + s->back = EOF; + s->crc = crc32(0L, Z_NULL, 0); + s->msg = NULL; + s->transparent = 0; + + s->path = (char*)ALLOC(strlen(path)+1); + if (s->path == NULL) { + return destroy(s), (gzFile)Z_NULL; + } + strcpy(s->path, path); /* do this early for debugging */ + + s->mode = '\0'; + do { + if (*p == 'r') s->mode = 'r'; + if (*p == 'w' || *p == 'a') s->mode = 'w'; + if (*p >= '0' && *p <= '9') { + level = *p - '0'; + } else if (*p == 'f') { + strategy = Z_FILTERED; + } else if (*p == 'h') { + strategy = Z_HUFFMAN_ONLY; + } else if (*p == 'R') { + strategy = Z_RLE; + } else { + *m++ = *p; /* copy the mode */ + } + } while (*p++ && m != fmode + sizeof(fmode)); + if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; + + if (s->mode == 'w') { +#ifdef NO_GZCOMPRESS + err = Z_STREAM_ERROR; +#else + err = deflateInit2(&(s->stream), level, + Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy); + /* windowBits is passed < 0 to suppress zlib header */ + + s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); +#endif + if (err != Z_OK || s->outbuf == Z_NULL) { + return destroy(s), (gzFile)Z_NULL; + } + } else { + s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); + + err = inflateInit2(&(s->stream), -MAX_WBITS); + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are + * present after the compressed stream. + */ + if (err != Z_OK || s->inbuf == Z_NULL) { + return destroy(s), (gzFile)Z_NULL; + } + } + s->stream.avail_out = Z_BUFSIZE; + + errno = 0; + s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode); + + if (s->file == NULL) { + return destroy(s), (gzFile)Z_NULL; + } + if (s->mode == 'w') { + /* Write a very simple .gz header: + */ + fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], + Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); + s->start = 10L; + /* We use 10L instead of ftell(s->file) to because ftell causes an + * fflush on some systems. This version of the library doesn't use + * start anyway in write mode, so this initialization is not + * necessary. + */ + } else { + check_header(s); /* skip the .gz header */ + s->start = ftell(s->file) - s->stream.avail_in; + } + + return (gzFile)s; +} + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. +*/ +gzFile ZEXPORT gzopen (path, mode) + const char *path; + const char *mode; +{ + return gz_open (path, mode, -1); +} + +/* =========================================================================== + Associate a gzFile with the file descriptor fd. fd is not dup'ed here + to mimic the behavio(u)r of fdopen. +*/ +gzFile ZEXPORT gzdopen (fd, mode) + int fd; + const char *mode; +{ + char name[46]; /* allow for up to 128-bit integers */ + + if (fd < 0) return (gzFile)Z_NULL; + sprintf(name, "", fd); /* for debugging */ + + return gz_open (name, mode, fd); +} + +/* =========================================================================== + * Update the compression level and strategy + */ +int ZEXPORT gzsetparams (file, level, strategy) + gzFile file; + int level; + int strategy; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; + + /* Make room to allow flushing */ + if (s->stream.avail_out == 0) { + + s->stream.next_out = s->outbuf; + if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { + s->z_err = Z_ERRNO; + } + s->stream.avail_out = Z_BUFSIZE; + } + + return deflateParams (&(s->stream), level, strategy); +} + +/* =========================================================================== + Read a byte from a gz_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. +*/ +local int get_byte(s) + gz_stream *s; +{ + if (s->z_eof) return EOF; + if (s->stream.avail_in == 0) { + errno = 0; + s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) { + s->z_eof = 1; + if (ferror(s->file)) s->z_err = Z_ERRNO; + return EOF; + } + s->stream.next_in = s->inbuf; + } + s->stream.avail_in--; + return *(s->stream.next_in)++; +} + +/* =========================================================================== + Check the gzip header of a gz_stream opened for reading. Set the stream + mode to transparent if the gzip magic header is not present; set s->err + to Z_DATA_ERROR if the magic header is present but the rest of the header + is incorrect. + IN assertion: the stream s has already been created sucessfully; + s->stream.avail_in is zero for the first time, but may be non-zero + for concatenated .gz files. +*/ +local void check_header(s) + gz_stream *s; +{ + int method; /* method byte */ + int flags; /* flags byte */ + uInt len; + int c; + + /* Assure two bytes in the buffer so we can peek ahead -- handle case + where first byte of header is at the end of the buffer after the last + gzip segment */ + len = s->stream.avail_in; + if (len < 2) { + if (len) s->inbuf[0] = s->stream.next_in[0]; + errno = 0; + len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file); + if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO; + s->stream.avail_in += len; + s->stream.next_in = s->inbuf; + if (s->stream.avail_in < 2) { + s->transparent = s->stream.avail_in; + return; + } + } + + /* Peek ahead to check the gzip magic header */ + if (s->stream.next_in[0] != gz_magic[0] || + s->stream.next_in[1] != gz_magic[1]) { + s->transparent = 1; + return; + } + s->stream.avail_in -= 2; + s->stream.next_in += 2; + + /* Check the rest of the gzip header */ + method = get_byte(s); + flags = get_byte(s); + if (method != Z_DEFLATED || (flags & RESERVED) != 0) { + s->z_err = Z_DATA_ERROR; + return; + } + + /* Discard time, xflags and OS code: */ + for (len = 0; len < 6; len++) (void)get_byte(s); + + if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ + len = (uInt)get_byte(s); + len += ((uInt)get_byte(s))<<8; + /* len is garbage if EOF but the loop below will quit anyway */ + while (len-- != 0 && get_byte(s) != EOF) ; + } + if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ + while ((c = get_byte(s)) != 0 && c != EOF) ; + } + if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ + while ((c = get_byte(s)) != 0 && c != EOF) ; + } + if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ + for (len = 0; len < 2; len++) (void)get_byte(s); + } + s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK; +} + + /* =========================================================================== + * Cleanup then free the given gz_stream. Return a zlib error code. + Try freeing in the reverse order of allocations. + */ +local int destroy (s) + gz_stream *s; +{ + int err = Z_OK; + + if (!s) return Z_STREAM_ERROR; + + TRYFREE(s->msg); + + if (s->stream.state != NULL) { + if (s->mode == 'w') { +#ifdef NO_GZCOMPRESS + err = Z_STREAM_ERROR; +#else + err = deflateEnd(&(s->stream)); +#endif + } else if (s->mode == 'r') { + err = inflateEnd(&(s->stream)); + } + } + if (s->file != NULL && fclose(s->file)) { +#ifdef ESPIPE + if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ +#endif + err = Z_ERRNO; + } + if (s->z_err < 0) err = s->z_err; + + TRYFREE(s->inbuf); + TRYFREE(s->outbuf); + TRYFREE(s->path); + TRYFREE(s); + return err; +} + +/* =========================================================================== + Reads the given number of uncompressed bytes from the compressed file. + gzread returns the number of bytes actually read (0 for end of file). +*/ +int ZEXPORT gzread (file, buf, len) + gzFile file; + voidp buf; + unsigned len; +{ + gz_stream *s = (gz_stream*)file; + Bytef *start = (Bytef*)buf; /* starting point for crc computation */ + Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */ + + if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; + + if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1; + if (s->z_err == Z_STREAM_END) return 0; /* EOF */ + + next_out = (Byte*)buf; + s->stream.next_out = (Bytef*)buf; + s->stream.avail_out = len; + + if (s->stream.avail_out && s->back != EOF) { + *next_out++ = s->back; + s->stream.next_out++; + s->stream.avail_out--; + s->back = EOF; + s->out++; + start++; + if (s->last) { + s->z_err = Z_STREAM_END; + return 1; + } + } + + while (s->stream.avail_out != 0) { + + if (s->transparent) { + /* Copy first the lookahead bytes: */ + uInt n = s->stream.avail_in; + if (n > s->stream.avail_out) n = s->stream.avail_out; + if (n > 0) { + zmemcpy(s->stream.next_out, s->stream.next_in, n); + next_out += n; + s->stream.next_out = next_out; + s->stream.next_in += n; + s->stream.avail_out -= n; + s->stream.avail_in -= n; + } + if (s->stream.avail_out > 0) { + s->stream.avail_out -= + (uInt)fread(next_out, 1, s->stream.avail_out, s->file); + } + len -= s->stream.avail_out; + s->in += len; + s->out += len; + if (len == 0) s->z_eof = 1; + return (int)len; + } + if (s->stream.avail_in == 0 && !s->z_eof) { + + errno = 0; + s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) { + s->z_eof = 1; + if (ferror(s->file)) { + s->z_err = Z_ERRNO; + break; + } + } + s->stream.next_in = s->inbuf; + } + s->in += s->stream.avail_in; + s->out += s->stream.avail_out; + s->z_err = inflate(&(s->stream), Z_NO_FLUSH); + s->in -= s->stream.avail_in; + s->out -= s->stream.avail_out; + + if (s->z_err == Z_STREAM_END) { + /* Check CRC and original size */ + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + start = s->stream.next_out; + + if (getLong(s) != s->crc) { + s->z_err = Z_DATA_ERROR; + } else { + (void)getLong(s); + /* The uncompressed length returned by above getlong() may be + * different from s->out in case of concatenated .gz files. + * Check for such files: + */ + check_header(s); + if (s->z_err == Z_OK) { + inflateReset(&(s->stream)); + s->crc = crc32(0L, Z_NULL, 0); + } + } + } + if (s->z_err != Z_OK || s->z_eof) break; + } + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + + if (len == s->stream.avail_out && + (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)) + return -1; + return (int)(len - s->stream.avail_out); +} + + +/* =========================================================================== + Reads one byte from the compressed file. gzgetc returns this byte + or -1 in case of end of file or error. +*/ +int ZEXPORT gzgetc(file) + gzFile file; +{ + unsigned char c; + + return gzread(file, &c, 1) == 1 ? c : -1; +} + + +/* =========================================================================== + Push one byte back onto the stream. +*/ +int ZEXPORT gzungetc(c, file) + int c; + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF; + s->back = c; + s->out--; + s->last = (s->z_err == Z_STREAM_END); + if (s->last) s->z_err = Z_OK; + s->z_eof = 0; + return c; +} + + +/* =========================================================================== + Reads bytes from the compressed file until len-1 characters are + read, or a newline character is read and transferred to buf, or an + end-of-file condition is encountered. The string is then terminated + with a null character. + gzgets returns buf, or Z_NULL in case of error. + + The current implementation is not optimized at all. +*/ +char * ZEXPORT gzgets(file, buf, len) + gzFile file; + char *buf; + int len; +{ + char *b = buf; + if (buf == Z_NULL || len <= 0) return Z_NULL; + + while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ; + *buf = '\0'; + return b == buf && len > 0 ? Z_NULL : b; +} + + +#ifndef NO_GZCOMPRESS +/* =========================================================================== + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of bytes actually written (0 in case of error). +*/ +int ZEXPORT gzwrite (file, buf, len) + gzFile file; + voidpc buf; + unsigned len; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; + + s->stream.next_in = (Bytef*)buf; + s->stream.avail_in = len; + + while (s->stream.avail_in != 0) { + + if (s->stream.avail_out == 0) { + + s->stream.next_out = s->outbuf; + if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { + s->z_err = Z_ERRNO; + break; + } + s->stream.avail_out = Z_BUFSIZE; + } + s->in += s->stream.avail_in; + s->out += s->stream.avail_out; + s->z_err = deflate(&(s->stream), Z_NO_FLUSH); + s->in -= s->stream.avail_in; + s->out -= s->stream.avail_out; + if (s->z_err != Z_OK) break; + } + s->crc = crc32(s->crc, (const Bytef *)buf, len); + + return (int)(len - s->stream.avail_in); +} + + +/* =========================================================================== + Converts, formats, and writes the args to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written (0 in case of error). +*/ +#ifdef STDC +#include + +int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) +{ + char buf[Z_PRINTF_BUFSIZE]; + va_list va; + int len; + + buf[sizeof(buf) - 1] = 0; + va_start(va, format); +#ifdef NO_vsnprintf +# ifdef HAS_vsprintf_void + (void)vsprintf(buf, format, va); + va_end(va); + for (len = 0; len < sizeof(buf); len++) + if (buf[len] == 0) break; +# else + len = vsprintf(buf, format, va); + va_end(va); +# endif +#else +# ifdef HAS_vsnprintf_void + (void)vsnprintf(buf, sizeof(buf), format, va); + va_end(va); + len = strlen(buf); +# else + len = vsnprintf(buf, sizeof(buf), format, va); + va_end(va); +# endif +#endif + if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0) + return 0; + return gzwrite(file, buf, (unsigned)len); +} +#else /* not ANSI C */ + +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) + gzFile file; + const char *format; + int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; +{ + char buf[Z_PRINTF_BUFSIZE]; + int len; + + buf[sizeof(buf) - 1] = 0; +#ifdef NO_snprintf +# ifdef HAS_sprintf_void + sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); + for (len = 0; len < sizeof(buf); len++) + if (buf[len] == 0) break; +# else + len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); +# endif +#else +# ifdef HAS_snprintf_void + snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); + len = strlen(buf); +# else + len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); +# endif +#endif + if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0) + return 0; + return gzwrite(file, buf, len); +} +#endif + +/* =========================================================================== + Writes c, converted to an unsigned char, into the compressed file. + gzputc returns the value that was written, or -1 in case of error. +*/ +int ZEXPORT gzputc(file, c) + gzFile file; + int c; +{ + unsigned char cc = (unsigned char) c; /* required for big endian systems */ + + return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1; +} + + +/* =========================================================================== + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + gzputs returns the number of characters written, or -1 in case of error. +*/ +int ZEXPORT gzputs(file, s) + gzFile file; + const char *s; +{ + return gzwrite(file, (char*)s, (unsigned)strlen(s)); +} + + +/* =========================================================================== + Flushes all pending output into the compressed file. The parameter + flush is as in the deflate() function. +*/ +local int do_flush (file, flush) + gzFile file; + int flush; +{ + uInt len; + int done = 0; + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR; + + s->stream.avail_in = 0; /* should be zero already anyway */ + + for (;;) { + len = Z_BUFSIZE - s->stream.avail_out; + + if (len != 0) { + if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) { + s->z_err = Z_ERRNO; + return Z_ERRNO; + } + s->stream.next_out = s->outbuf; + s->stream.avail_out = Z_BUFSIZE; + } + if (done) break; + s->out += s->stream.avail_out; + s->z_err = deflate(&(s->stream), flush); + s->out -= s->stream.avail_out; + + /* Ignore the second of two consecutive flushes: */ + if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK; + + /* deflate has finished flushing only when it hasn't used up + * all the available space in the output buffer: + */ + done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); + + if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; + } + return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; +} + +int ZEXPORT gzflush (file, flush) + gzFile file; + int flush; +{ + gz_stream *s = (gz_stream*)file; + int err = do_flush (file, flush); + + if (err) return err; + fflush(s->file); + return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; +} +#endif /* NO_GZCOMPRESS */ + +/* =========================================================================== + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error. + SEEK_END is not implemented, returns error. + In this version of the library, gzseek can be extremely slow. +*/ +z_off_t ZEXPORT gzseek (file, offset, whence) + gzFile file; + z_off_t offset; + int whence; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || whence == SEEK_END || + s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) { + return -1L; + } + + if (s->mode == 'w') { +#ifdef NO_GZCOMPRESS + return -1L; +#else + if (whence == SEEK_SET) { + offset -= s->in; + } + if (offset < 0) return -1L; + + /* At this point, offset is the number of zero bytes to write. */ + if (s->inbuf == Z_NULL) { + s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */ + if (s->inbuf == Z_NULL) return -1L; + zmemzero(s->inbuf, Z_BUFSIZE); + } + while (offset > 0) { + uInt size = Z_BUFSIZE; + if (offset < Z_BUFSIZE) size = (uInt)offset; + + size = gzwrite(file, s->inbuf, size); + if (size == 0) return -1L; + + offset -= size; + } + return s->in; +#endif + } + /* Rest of function is for reading only */ + + /* compute absolute position */ + if (whence == SEEK_CUR) { + offset += s->out; + } + if (offset < 0) return -1L; + + if (s->transparent) { + /* map to fseek */ + s->back = EOF; + s->stream.avail_in = 0; + s->stream.next_in = s->inbuf; + if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; + + s->in = s->out = offset; + return offset; + } + + /* For a negative seek, rewind and use positive seek */ + if (offset >= s->out) { + offset -= s->out; + } else if (gzrewind(file) < 0) { + return -1L; + } + /* offset is now the number of bytes to skip. */ + + if (offset != 0 && s->outbuf == Z_NULL) { + s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); + if (s->outbuf == Z_NULL) return -1L; + } + if (offset && s->back != EOF) { + s->back = EOF; + s->out++; + offset--; + if (s->last) s->z_err = Z_STREAM_END; + } + while (offset > 0) { + int size = Z_BUFSIZE; + if (offset < Z_BUFSIZE) size = (int)offset; + + size = gzread(file, s->outbuf, (uInt)size); + if (size <= 0) return -1L; + offset -= size; + } + return s->out; +} + +/* =========================================================================== + Rewinds input file. +*/ +int ZEXPORT gzrewind (file) + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'r') return -1; + + s->z_err = Z_OK; + s->z_eof = 0; + s->back = EOF; + s->stream.avail_in = 0; + s->stream.next_in = s->inbuf; + s->crc = crc32(0L, Z_NULL, 0); + if (!s->transparent) (void)inflateReset(&s->stream); + s->in = 0; + s->out = 0; + return fseek(s->file, s->start, SEEK_SET); +} + +/* =========================================================================== + Returns the starting position for the next gzread or gzwrite on the + given compressed file. This position represents a number of bytes in the + uncompressed data stream. +*/ +z_off_t ZEXPORT gztell (file) + gzFile file; +{ + return gzseek(file, 0L, SEEK_CUR); +} + +/* =========================================================================== + Returns 1 when EOF has previously been detected reading the given + input stream, otherwise zero. +*/ +int ZEXPORT gzeof (file) + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + /* With concatenated compressed files that can have embedded + * crc trailers, z_eof is no longer the only/best indicator of EOF + * on a gz_stream. Handle end-of-stream error explicitly here. + */ + if (s == NULL || s->mode != 'r') return 0; + if (s->z_eof) return 1; + return s->z_err == Z_STREAM_END; +} + +/* =========================================================================== + Returns 1 if reading and doing so transparently, otherwise zero. +*/ +int ZEXPORT gzdirect (file) + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL || s->mode != 'r') return 0; + return s->transparent; +} + +/* =========================================================================== + Outputs a long in LSB order to the given file +*/ +local void putLong (file, x) + FILE *file; + uLong x; +{ + int n; + for (n = 0; n < 4; n++) { + fputc((int)(x & 0xff), file); + x >>= 8; + } +} + +/* =========================================================================== + Reads a long in LSB order from the given gz_stream. Sets z_err in case + of error. +*/ +local uLong getLong (s) + gz_stream *s; +{ + uLong x = (uLong)get_byte(s); + int c; + + x += ((uLong)get_byte(s))<<8; + x += ((uLong)get_byte(s))<<16; + c = get_byte(s); + if (c == EOF) s->z_err = Z_DATA_ERROR; + x += ((uLong)c)<<24; + return x; +} + +/* =========================================================================== + Flushes all pending output if necessary, closes the compressed file + and deallocates all the (de)compression state. +*/ +int ZEXPORT gzclose (file) + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL) return Z_STREAM_ERROR; + + if (s->mode == 'w') { +#ifdef NO_GZCOMPRESS + return Z_STREAM_ERROR; +#else + if (do_flush (file, Z_FINISH) != Z_OK) + return destroy((gz_stream*)file); + + putLong (s->file, s->crc); + putLong (s->file, (uLong)(s->in & 0xffffffff)); +#endif + } + return destroy((gz_stream*)file); +} + +#ifdef STDC +# define zstrerror(errnum) strerror(errnum) +#else +# define zstrerror(errnum) "" +#endif + +/* =========================================================================== + Returns the error message for the last error which occurred on the + given compressed file. errnum is set to zlib error number. If an + error occurred in the file system and not in the compression library, + errnum is set to Z_ERRNO and the application may consult errno + to get the exact error code. +*/ +const char * ZEXPORT gzerror (file, errnum) + gzFile file; + int *errnum; +{ + char *m; + gz_stream *s = (gz_stream*)file; + + if (s == NULL) { + *errnum = Z_STREAM_ERROR; + return (const char*)ERR_MSG(Z_STREAM_ERROR); + } + *errnum = s->z_err; + if (*errnum == Z_OK) return (const char*)""; + + m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg); + + if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err); + + TRYFREE(s->msg); + s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); + if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR); + strcpy(s->msg, s->path); + strcat(s->msg, ": "); + strcat(s->msg, m); + return (const char*)s->msg; +} + +/* =========================================================================== + Clear the error and end-of-file flags, and do the same for the real file. +*/ +void ZEXPORT gzclearerr (file) + gzFile file; +{ + gz_stream *s = (gz_stream*)file; + + if (s == NULL) return; + if (s->z_err != Z_STREAM_END) s->z_err = Z_OK; + s->z_eof = 0; + clearerr(s->file); +} diff --git a/zlib/infback.c b/zlib/infback.c new file mode 100644 index 00000000..455dbc9e --- /dev/null +++ b/zlib/infback.c @@ -0,0 +1,623 @@ +/* infback.c -- inflate using a call-back interface + * Copyright (C) 1995-2005 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + This code is largely copied from inflate.c. Normally either infback.o or + inflate.o would be linked into an application--not both. The interface + with inffast.c is retained so that optimized assembler-coded versions of + inflate_fast() can be used with either inflate.c or infback.c. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +/* function prototypes */ +local void fixedtables OF((struct inflate_state FAR *state)); + +/* + strm provides memory allocation functions in zalloc and zfree, or + Z_NULL to use the library memory allocation functions. + + windowBits is in the range 8..15, and window is a user-supplied + window and output buffer that is 2**windowBits bytes. + */ +int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) +z_streamp strm; +int windowBits; +unsigned char FAR *window; +const char *version; +int stream_size; +{ + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL || window == Z_NULL || + windowBits < 8 || windowBits > 15) + return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; + } + if (strm->zfree == (free_func)0) strm->zfree = zcfree; + state = (struct inflate_state FAR *)ZALLOC(strm, 1, + sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->dmax = 32768U; + state->wbits = windowBits; + state->wsize = 1U << windowBits; + state->window = window; + state->write = 0; + state->whave = 0; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +/* Macros for inflateBack(): */ + +/* Load returned state from inflate_fast() */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Set state from registers for inflate_fast() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Assure that some input is available. If input is requested, but denied, + then return a Z_BUF_ERROR from inflateBack(). */ +#define PULL() \ + do { \ + if (have == 0) { \ + have = in(in_desc, &next); \ + if (have == 0) { \ + next = Z_NULL; \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflateBack() + with an error if there is no input available. */ +#define PULLBYTE() \ + do { \ + PULL(); \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflateBack() with + an error. */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* Assure that some output space is available, by writing out the window + if it's full. If the write fails, return from inflateBack() with a + Z_BUF_ERROR. */ +#define ROOM() \ + do { \ + if (left == 0) { \ + put = state->window; \ + left = state->wsize; \ + state->whave = left; \ + if (out(out_desc, put, left)) { \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* + strm provides the memory allocation functions and window buffer on input, + and provides information on the unused input on return. For Z_DATA_ERROR + returns, strm will also provide an error message. + + in() and out() are the call-back input and output functions. When + inflateBack() needs more input, it calls in(). When inflateBack() has + filled the window with output, or when it completes with data in the + window, it calls out() to write out the data. The application must not + change the provided input until in() is called again or inflateBack() + returns. The application must not change the window/output buffer until + inflateBack() returns. + + in() and out() are called with a descriptor parameter provided in the + inflateBack() call. This parameter can be a structure that provides the + information required to do the read or write, as well as accumulated + information on the input and output such as totals and check values. + + in() should return zero on failure. out() should return non-zero on + failure. If either in() or out() fails, than inflateBack() returns a + Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it + was in() or out() that caused in the error. Otherwise, inflateBack() + returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format + error, or Z_MEM_ERROR if it could not allocate memory for the state. + inflateBack() can also return Z_STREAM_ERROR if the input parameters + are not correct, i.e. strm is Z_NULL or the state was not initialized. + */ +int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) +z_streamp strm; +in_func in; +void FAR *in_desc; +out_func out; +void FAR *out_desc; +{ + struct inflate_state FAR *state; + unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code this; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + /* Check that the strm exists and that the state was initialized */ + if (strm == Z_NULL || strm->state == Z_NULL) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* Reset the state */ + strm->msg = Z_NULL; + state->mode = TYPE; + state->last = 0; + state->whave = 0; + next = strm->next_in; + have = next != Z_NULL ? strm->avail_in : 0; + hold = 0; + bits = 0; + put = state->window; + left = state->wsize; + + /* Inflate until end of block marked as last */ + for (;;) + switch (state->mode) { + case TYPE: + /* determine and dispatch block type */ + if (state->last) { + BYTEBITS(); + state->mode = DONE; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN; /* decode codes */ + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + + case STORED: + /* get and verify stored block length */ + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + + /* copy stored block from input to output */ + while (state->length != 0) { + copy = state->length; + PULL(); + ROOM(); + if (copy > have) copy = have; + if (copy > left) copy = left; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + + case TABLE: + /* get dynamic table entries descriptor */ + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + + /* get code length code lengths (not a typo) */ + state->have = 0; + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + + /* get length and distance code code lengths */ + state->have = 0; + while (state->have < state->nlen + state->ndist) { + for (;;) { + this = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if (this.val < 16) { + NEEDBITS(this.bits); + DROPBITS(this.bits); + state->lens[state->have++] = this.val; + } + else { + if (this.val == 16) { + NEEDBITS(this.bits + 2); + DROPBITS(this.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = (unsigned)(state->lens[state->have - 1]); + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (this.val == 17) { + NEEDBITS(this.bits + 3); + DROPBITS(this.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(this.bits + 7); + DROPBITS(this.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* build code tables */ + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (code const FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN; + + case LEN: + /* use inflate_fast() if we have enough input and output */ + if (have >= 6 && left >= 258) { + RESTORE(); + if (state->whave < state->wsize) + state->whave = state->wsize - left; + inflate_fast(strm, state->wsize); + LOAD(); + break; + } + + /* get a literal, length, or end-of-block code */ + for (;;) { + this = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if (this.op && (this.op & 0xf0) == 0) { + last = this; + for (;;) { + this = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + this.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(this.bits); + state->length = (unsigned)this.val; + + /* process literal */ + if (this.op == 0) { + Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", this.val)); + ROOM(); + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + } + + /* process end of block */ + if (this.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + + /* invalid code */ + if (this.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + + /* length code -- get extra bits, if any */ + state->extra = (unsigned)(this.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + + /* get distance code */ + for (;;) { + this = state->distcode[BITS(state->distbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if ((this.op & 0xf0) == 0) { + last = this; + for (;;) { + this = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + this.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(this.bits); + if (this.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)this.val; + + /* get distance extra bits, if any */ + state->extra = (unsigned)(this.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + } + if (state->offset > state->wsize - (state->whave < state->wsize ? + left : 0)) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + + /* copy match from window to output */ + do { + ROOM(); + copy = state->wsize - state->offset; + if (copy < left) { + from = put + copy; + copy = left - copy; + } + else { + from = put - state->offset; + copy = left; + } + if (copy > state->length) copy = state->length; + state->length -= copy; + left -= copy; + do { + *put++ = *from++; + } while (--copy); + } while (state->length != 0); + break; + + case DONE: + /* inflate stream terminated properly -- write leftover output */ + ret = Z_STREAM_END; + if (left < state->wsize) { + if (out(out_desc, state->window, state->wsize - left)) + ret = Z_BUF_ERROR; + } + goto inf_leave; + + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + + default: /* can't happen, but makes compilers happy */ + ret = Z_STREAM_ERROR; + goto inf_leave; + } + + /* Return unused input */ + inf_leave: + strm->next_in = next; + strm->avail_in = have; + return ret; +} + +int ZEXPORT inflateBackEnd(strm) +z_streamp strm; +{ + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} diff --git a/zlib/inffast.c b/zlib/inffast.c new file mode 100644 index 00000000..bbee92ed --- /dev/null +++ b/zlib/inffast.c @@ -0,0 +1,318 @@ +/* inffast.c -- fast decoding + * Copyright (C) 1995-2004 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifndef ASMINF + +/* Allow machine dependent optimization for post-increment or pre-increment. + Based on testing to date, + Pre-increment preferred for: + - PowerPC G3 (Adler) + - MIPS R5000 (Randers-Pehrson) + Post-increment preferred for: + - none + No measurable difference: + - Pentium III (Anderson) + - M68060 (Nikl) + */ +#ifdef POSTINC +# define OFF 0 +# define PUP(a) *(a)++ +#else +# define OFF 1 +# define PUP(a) *++(a) +#endif + +/* + Decode literal, length, and distance codes and write out the resulting + literal and match bytes until either not enough input or output is + available, an end-of-block is encountered, or a data error is encountered. + When large enough input and output buffers are supplied to inflate(), for + example, a 16K input buffer and a 64K output buffer, more than 95% of the + inflate execution time is spent in this routine. + + Entry assumptions: + + state->mode == LEN + strm->avail_in >= 6 + strm->avail_out >= 258 + start >= strm->avail_out + state->bits < 8 + + On return, state->mode is one of: + + LEN -- ran out of enough output space or enough available input + TYPE -- reached end of block code, inflate() to interpret next block + BAD -- error in block data + + Notes: + + - The maximum input bits used by a length/distance pair is 15 bits for the + length code, 5 bits for the length extra, 15 bits for the distance code, + and 13 bits for the distance extra. This totals 48 bits, or six bytes. + Therefore if strm->avail_in >= 6, then there is enough input to avoid + checking for available input while decoding. + + - The maximum bytes that a single length/distance pair can output is 258 + bytes, which is the maximum length that can be coded. inflate_fast() + requires strm->avail_out >= 258 for each loop to avoid checking for + output space. + */ +void inflate_fast(strm, start) +z_streamp strm; +unsigned start; /* inflate()'s starting value for strm->avail_out */ +{ + struct inflate_state FAR *state; + unsigned char FAR *in; /* local strm->next_in */ + unsigned char FAR *last; /* while in < last, enough input available */ + unsigned char FAR *out; /* local strm->next_out */ + unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ + unsigned char FAR *end; /* while out < end, enough space available */ +#ifdef INFLATE_STRICT + unsigned dmax; /* maximum distance from zlib header */ +#endif + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned write; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ + unsigned long hold; /* local strm->hold */ + unsigned bits; /* local strm->bits */ + code const FAR *lcode; /* local strm->lencode */ + code const FAR *dcode; /* local strm->distcode */ + unsigned lmask; /* mask for first level of length codes */ + unsigned dmask; /* mask for first level of distance codes */ + code this; /* retrieved table entry */ + unsigned op; /* code bits, operation, extra bits, or */ + /* window position, window bytes to copy */ + unsigned len; /* match length, unused bytes */ + unsigned dist; /* match distance */ + unsigned char FAR *from; /* where to copy match from */ + + /* copy state to local variables */ + state = (struct inflate_state FAR *)strm->state; + in = strm->next_in - OFF; + last = in + (strm->avail_in - 5); + out = strm->next_out - OFF; + beg = out - (start - strm->avail_out); + end = out + (strm->avail_out - 257); +#ifdef INFLATE_STRICT + dmax = state->dmax; +#endif + wsize = state->wsize; + whave = state->whave; + write = state->write; + window = state->window; + hold = state->hold; + bits = state->bits; + lcode = state->lencode; + dcode = state->distcode; + lmask = (1U << state->lenbits) - 1; + dmask = (1U << state->distbits) - 1; + + /* decode literals and length/distances until end-of-block or not enough + input data or output space */ + do { + if (bits < 15) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + this = lcode[hold & lmask]; + dolen: + op = (unsigned)(this.bits); + hold >>= op; + bits -= op; + op = (unsigned)(this.op); + if (op == 0) { /* literal */ + Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", this.val)); + PUP(out) = (unsigned char)(this.val); + } + else if (op & 16) { /* length base */ + len = (unsigned)(this.val); + op &= 15; /* number of extra bits */ + if (op) { + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + len += (unsigned)hold & ((1U << op) - 1); + hold >>= op; + bits -= op; + } + Tracevv((stderr, "inflate: length %u\n", len)); + if (bits < 15) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + this = dcode[hold & dmask]; + dodist: + op = (unsigned)(this.bits); + hold >>= op; + bits -= op; + op = (unsigned)(this.op); + if (op & 16) { /* distance base */ + dist = (unsigned)(this.val); + op &= 15; /* number of extra bits */ + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + } + dist += (unsigned)hold & ((1U << op) - 1); +#ifdef INFLATE_STRICT + if (dist > dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + hold >>= op; + bits -= op; + Tracevv((stderr, "inflate: distance %u\n", dist)); + op = (unsigned)(out - beg); /* max distance in output */ + if (dist > op) { /* see if copy from window */ + op = dist - op; /* distance back in window */ + if (op > whave) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + from = window - OFF; + if (write == 0) { /* very common case */ + from += wsize - op; + if (op < len) { /* some from window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + else if (write < op) { /* wrap around window */ + from += wsize + write - op; + op -= write; + if (op < len) { /* some from end of window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = window - OFF; + if (write < len) { /* some from start of window */ + op = write; + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + } + else { /* contiguous in window */ + from += write - op; + if (op < len) { /* some from window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + while (len > 2) { + PUP(out) = PUP(from); + PUP(out) = PUP(from); + PUP(out) = PUP(from); + len -= 3; + } + if (len) { + PUP(out) = PUP(from); + if (len > 1) + PUP(out) = PUP(from); + } + } + else { + from = out - dist; /* copy direct from output */ + do { /* minimum length is three */ + PUP(out) = PUP(from); + PUP(out) = PUP(from); + PUP(out) = PUP(from); + len -= 3; + } while (len > 2); + if (len) { + PUP(out) = PUP(from); + if (len > 1) + PUP(out) = PUP(from); + } + } + } + else if ((op & 64) == 0) { /* 2nd level distance code */ + this = dcode[this.val + (hold & ((1U << op) - 1))]; + goto dodist; + } + else { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + } + else if ((op & 64) == 0) { /* 2nd level length code */ + this = lcode[this.val + (hold & ((1U << op) - 1))]; + goto dolen; + } + else if (op & 32) { /* end-of-block */ + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + else { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + } while (in < last && out < end); + + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ + len = bits >> 3; + in -= len; + bits -= len << 3; + hold &= (1U << bits) - 1; + + /* update state and return */ + strm->next_in = in + OFF; + strm->next_out = out + OFF; + strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); + strm->avail_out = (unsigned)(out < end ? + 257 + (end - out) : 257 - (out - end)); + state->hold = hold; + state->bits = bits; + return; +} + +/* + inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): + - Using bit fields for code structure + - Different op definition to avoid & for extra bits (do & for table bits) + - Three separate decoding do-loops for direct, window, and write == 0 + - Special case for distance > 1 copies to do overlapped load and store copy + - Explicit branch predictions (based on measured branch probabilities) + - Deferring match copy and interspersed it with decoding subsequent codes + - Swapping literal/length else + - Swapping window/direct else + - Larger unrolled copy loops (three is about right) + - Moving len -= 3 statement into middle of loop + */ + +#endif /* !ASMINF */ diff --git a/zlib/inffast.h b/zlib/inffast.h new file mode 100644 index 00000000..1e88d2d9 --- /dev/null +++ b/zlib/inffast.h @@ -0,0 +1,11 @@ +/* inffast.h -- header to use inffast.c + * Copyright (C) 1995-2003 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +void inflate_fast OF((z_streamp strm, unsigned start)); diff --git a/zlib/inffixed.h b/zlib/inffixed.h new file mode 100644 index 00000000..75ed4b59 --- /dev/null +++ b/zlib/inffixed.h @@ -0,0 +1,94 @@ + /* inffixed.h -- table for decoding fixed codes + * Generated automatically by makefixed(). + */ + + /* WARNING: this file should *not* be used by applications. It + is part of the implementation of the compression library and + is subject to change. Applications should only use zlib.h. + */ + + static const code lenfix[512] = { + {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, + {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, + {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, + {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, + {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, + {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, + {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, + {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, + {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, + {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, + {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, + {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, + {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, + {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, + {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, + {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, + {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, + {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, + {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, + {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, + {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, + {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, + {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, + {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, + {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, + {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, + {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, + {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, + {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, + {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, + {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, + {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, + {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, + {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, + {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, + {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, + {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, + {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, + {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, + {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, + {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, + {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, + {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, + {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, + {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, + {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, + {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, + {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, + {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, + {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, + {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, + {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, + {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, + {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, + {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, + {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, + {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, + {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, + {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, + {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, + {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, + {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, + {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, + {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, + {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, + {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, + {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, + {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, + {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, + {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, + {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, + {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, + {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, + {0,9,255} + }; + + static const code distfix[32] = { + {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, + {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, + {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, + {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, + {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, + {22,5,193},{64,5,0} + }; diff --git a/zlib/inflate.c b/zlib/inflate.c new file mode 100644 index 00000000..792fdee8 --- /dev/null +++ b/zlib/inflate.c @@ -0,0 +1,1368 @@ +/* inflate.c -- zlib decompression + * Copyright (C) 1995-2005 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * Change history: + * + * 1.2.beta0 24 Nov 2002 + * - First version -- complete rewrite of inflate to simplify code, avoid + * creation of window when not needed, minimize use of window when it is + * needed, make inffast.c even faster, implement gzip decoding, and to + * improve code readability and style over the previous zlib inflate code + * + * 1.2.beta1 25 Nov 2002 + * - Use pointers for available input and output checking in inffast.c + * - Remove input and output counters in inffast.c + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 + * - Remove unnecessary second byte pull from length extra in inffast.c + * - Unroll direct copy to three copies per loop in inffast.c + * + * 1.2.beta2 4 Dec 2002 + * - Change external routine names to reduce potential conflicts + * - Correct filename to inffixed.h for fixed tables in inflate.c + * - Make hbuf[] unsigned char to match parameter type in inflate.c + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) + * to avoid negation problem on Alphas (64 bit) in inflate.c + * + * 1.2.beta3 22 Dec 2002 + * - Add comments on state->bits assertion in inffast.c + * - Add comments on op field in inftrees.h + * - Fix bug in reuse of allocated window after inflateReset() + * - Remove bit fields--back to byte structure for speed + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths + * - Change post-increments to pre-increments in inflate_fast(), PPC biased? + * - Add compile time option, POSTINC, to use post-increments instead (Intel?) + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used + * - Use local copies of stream next and avail values, as well as local bit + * buffer and bit count in inflate()--for speed when inflate_fast() not used + * + * 1.2.beta4 1 Jan 2003 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings + * - Move a comment on output buffer sizes from inffast.c to inflate.c + * - Add comments in inffast.c to introduce the inflate_fast() routine + * - Rearrange window copies in inflate_fast() for speed and simplification + * - Unroll last copy for window match in inflate_fast() + * - Use local copies of window variables in inflate_fast() for speed + * - Pull out common write == 0 case for speed in inflate_fast() + * - Make op and len in inflate_fast() unsigned for consistency + * - Add FAR to lcode and dcode declarations in inflate_fast() + * - Simplified bad distance check in inflate_fast() + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new + * source file infback.c to provide a call-back interface to inflate for + * programs like gzip and unzip -- uses window as output buffer to avoid + * window copying + * + * 1.2.beta5 1 Jan 2003 + * - Improved inflateBack() interface to allow the caller to provide initial + * input in strm. + * - Fixed stored blocks bug in inflateBack() + * + * 1.2.beta6 4 Jan 2003 + * - Added comments in inffast.c on effectiveness of POSTINC + * - Typecasting all around to reduce compiler warnings + * - Changed loops from while (1) or do {} while (1) to for (;;), again to + * make compilers happy + * - Changed type of window in inflateBackInit() to unsigned char * + * + * 1.2.beta7 27 Jan 2003 + * - Changed many types to unsigned or unsigned short to avoid warnings + * - Added inflateCopy() function + * + * 1.2.0 9 Mar 2003 + * - Changed inflateBack() interface to provide separate opaque descriptors + * for the in() and out() functions + * - Changed inflateBack() argument and in_func typedef to swap the length + * and buffer address return values for the input function + * - Check next_in and next_out for Z_NULL on entry to inflate() + * + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifdef MAKEFIXED +# ifndef BUILDFIXED +# define BUILDFIXED +# endif +#endif + +/* function prototypes */ +local void fixedtables OF((struct inflate_state FAR *state)); +local int updatewindow OF((z_streamp strm, unsigned out)); +#ifdef BUILDFIXED + void makefixed OF((void)); +#endif +local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, + unsigned len)); + +int ZEXPORT inflateReset(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + strm->total_in = strm->total_out = state->total = 0; + strm->msg = Z_NULL; + strm->adler = 1; /* to support ill-conceived Java test suite */ + state->mode = HEAD; + state->last = 0; + state->havedict = 0; + state->dmax = 32768U; + state->head = Z_NULL; + state->wsize = 0; + state->whave = 0; + state->write = 0; + state->hold = 0; + state->bits = 0; + state->lencode = state->distcode = state->next = state->codes; + Tracev((stderr, "inflate: reset\n")); + return Z_OK; +} + +int ZEXPORT inflatePrime(strm, bits, value) +z_streamp strm; +int bits; +int value; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; + value &= (1L << bits) - 1; + state->hold += value << state->bits; + state->bits += bits; + return Z_OK; +} + +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) +z_streamp strm; +int windowBits; +const char *version; +int stream_size; +{ + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL) return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; + } + if (strm->zfree == (free_func)0) strm->zfree = zcfree; + state = (struct inflate_state FAR *) + ZALLOC(strm, 1, sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + if (windowBits < 0) { + state->wrap = 0; + windowBits = -windowBits; + } + else { + state->wrap = (windowBits >> 4) + 1; +#ifdef GUNZIP + if (windowBits < 48) windowBits &= 15; +#endif + } + if (windowBits < 8 || windowBits > 15) { + ZFREE(strm, state); + strm->state = Z_NULL; + return Z_STREAM_ERROR; + } + state->wbits = (unsigned)windowBits; + state->window = Z_NULL; + return inflateReset(strm); +} + +int ZEXPORT inflateInit_(strm, version, stream_size) +z_streamp strm; +const char *version; +int stream_size; +{ + return inflateInit2_(strm, DEF_WBITS, version, stream_size); +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +#ifdef MAKEFIXED +#include + +/* + Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also + defines BUILDFIXED, so the tables are built on the fly. makefixed() writes + those tables to stdout, which would be piped to inffixed.h. A small program + can simply call makefixed to do this: + + void makefixed(void); + + int main(void) + { + makefixed(); + return 0; + } + + Then that can be linked with zlib built with MAKEFIXED defined and run: + + a.out > inffixed.h + */ +void makefixed() +{ + unsigned low, size; + struct inflate_state state; + + fixedtables(&state); + puts(" /* inffixed.h -- table for decoding fixed codes"); + puts(" * Generated automatically by makefixed()."); + puts(" */"); + puts(""); + puts(" /* WARNING: this file should *not* be used by applications."); + puts(" It is part of the implementation of this library and is"); + puts(" subject to change. Applications should only use zlib.h."); + puts(" */"); + puts(""); + size = 1U << 9; + printf(" static const code lenfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 7) == 0) printf("\n "); + printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, + state.lencode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); + size = 1U << 5; + printf("\n static const code distfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 6) == 0) printf("\n "); + printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, + state.distcode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); +} +#endif /* MAKEFIXED */ + +/* + Update the window with the last wsize (normally 32K) bytes written before + returning. If window does not exist yet, create it. This is only called + when a window is already in use, or when output has been written during this + inflate call, but the end of the deflate stream has not been reached yet. + It is also called to create a window for dictionary data when a dictionary + is loaded. + + Providing output buffers larger than 32K to inflate() should provide a speed + advantage, since only the last 32K of output is copied to the sliding window + upon return from inflate(), and since all distances after the first 32K of + output will fall in the output data, making match copies simpler and faster. + The advantage may be dependent on the size of the processor's data caches. + */ +local int updatewindow(strm, out) +z_streamp strm; +unsigned out; +{ + struct inflate_state FAR *state; + unsigned copy, dist; + + state = (struct inflate_state FAR *)strm->state; + + /* if it hasn't been done already, allocate space for the window */ + if (state->window == Z_NULL) { + state->window = (unsigned char FAR *) + ZALLOC(strm, 1U << state->wbits, + sizeof(unsigned char)); + if (state->window == Z_NULL) return 1; + } + + /* if window not in use yet, initialize */ + if (state->wsize == 0) { + state->wsize = 1U << state->wbits; + state->write = 0; + state->whave = 0; + } + + /* copy state->wsize or less output bytes into the circular window */ + copy = out - strm->avail_out; + if (copy >= state->wsize) { + zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); + state->write = 0; + state->whave = state->wsize; + } + else { + dist = state->wsize - state->write; + if (dist > copy) dist = copy; + zmemcpy(state->window + state->write, strm->next_out - copy, dist); + copy -= dist; + if (copy) { + zmemcpy(state->window, strm->next_out - copy, copy); + state->write = copy; + state->whave = state->wsize; + } + else { + state->write += dist; + if (state->write == state->wsize) state->write = 0; + if (state->whave < state->wsize) state->whave += dist; + } + } + return 0; +} + +/* Macros for inflate(): */ + +/* check function to use adler32() for zlib or crc32() for gzip */ +#ifdef GUNZIP +# define UPDATE(check, buf, len) \ + (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) +#else +# define UPDATE(check, buf, len) adler32(check, buf, len) +#endif + +/* check macros for header crc */ +#ifdef GUNZIP +# define CRC2(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + check = crc32(check, hbuf, 2); \ + } while (0) + +# define CRC4(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + hbuf[2] = (unsigned char)((word) >> 16); \ + hbuf[3] = (unsigned char)((word) >> 24); \ + check = crc32(check, hbuf, 4); \ + } while (0) +#endif + +/* Load registers with state in inflate() for speed */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Restore state from registers in inflate() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflate() + if there is no input available. */ +#define PULLBYTE() \ + do { \ + if (have == 0) goto inf_leave; \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflate(). */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* Reverse the bytes in a 32-bit value */ +#define REVERSE(q) \ + ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ + (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) + +/* + inflate() uses a state machine to process as much input data and generate as + much output data as possible before returning. The state machine is + structured roughly as follows: + + for (;;) switch (state) { + ... + case STATEn: + if (not enough input data or output space to make progress) + return; + ... make progress ... + state = STATEm; + break; + ... + } + + so when inflate() is called again, the same case is attempted again, and + if the appropriate resources are provided, the machine proceeds to the + next state. The NEEDBITS() macro is usually the way the state evaluates + whether it can proceed or should return. NEEDBITS() does the return if + the requested bits are not available. The typical use of the BITS macros + is: + + NEEDBITS(n); + ... do something with BITS(n) ... + DROPBITS(n); + + where NEEDBITS(n) either returns from inflate() if there isn't enough + input left to load n bits into the accumulator, or it continues. BITS(n) + gives the low n bits in the accumulator. When done, DROPBITS(n) drops + the low n bits off the accumulator. INITBITS() clears the accumulator + and sets the number of available bits to zero. BYTEBITS() discards just + enough bits to put the accumulator on a byte boundary. After BYTEBITS() + and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. + + NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return + if there is no input available. The decoding of variable length codes uses + PULLBYTE() directly in order to pull just enough bytes to decode the next + code, and no more. + + Some states loop until they get enough input, making sure that enough + state information is maintained to continue the loop where it left off + if NEEDBITS() returns in the loop. For example, want, need, and keep + would all have to actually be part of the saved state in case NEEDBITS() + returns: + + case STATEw: + while (want < need) { + NEEDBITS(n); + keep[want++] = BITS(n); + DROPBITS(n); + } + state = STATEx; + case STATEx: + + As shown above, if the next state is also the next case, then the break + is omitted. + + A state may also return if there is not enough output space available to + complete that state. Those states are copying stored data, writing a + literal byte, and copying a matching string. + + When returning, a "goto inf_leave" is used to update the total counters, + update the check value, and determine whether any progress has been made + during that inflate() call in order to return the proper return code. + Progress is defined as a change in either strm->avail_in or strm->avail_out. + When there is a window, goto inf_leave will update the window with the last + output written. If a goto inf_leave occurs in the middle of decompression + and there is no window currently, goto inf_leave will create one and copy + output to the window for the next call of inflate(). + + In this implementation, the flush parameter of inflate() only affects the + return code (per zlib.h). inflate() always writes as much as possible to + strm->next_out, given the space available and the provided input--the effect + documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers + the allocation of and copying into a sliding window until necessary, which + provides the effect documented in zlib.h for Z_FINISH when the entire input + stream available. So the only thing the flush parameter actually does is: + when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it + will return Z_BUF_ERROR if it has not reached the end of the stream. + */ + +int ZEXPORT inflate(strm, flush) +z_streamp strm; +int flush; +{ + struct inflate_state FAR *state; + unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned in, out; /* save starting available input and output */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code this; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ +#ifdef GUNZIP + unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ +#endif + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || + (strm->next_in == Z_NULL && strm->avail_in != 0)) + return Z_STREAM_ERROR; + + state = (struct inflate_state FAR *)strm->state; + if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ + LOAD(); + in = have; + out = left; + ret = Z_OK; + for (;;) + switch (state->mode) { + case HEAD: + if (state->wrap == 0) { + state->mode = TYPEDO; + break; + } + NEEDBITS(16); +#ifdef GUNZIP + if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ + state->check = crc32(0L, Z_NULL, 0); + CRC2(state->check, hold); + INITBITS(); + state->mode = FLAGS; + break; + } + state->flags = 0; /* expect zlib header */ + if (state->head != Z_NULL) + state->head->done = -1; + if (!(state->wrap & 1) || /* check if zlib header allowed */ +#else + if ( +#endif + ((BITS(8) << 8) + (hold >> 8)) % 31) { + strm->msg = (char *)"incorrect header check"; + state->mode = BAD; + break; + } + if (BITS(4) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + DROPBITS(4); + len = BITS(4) + 8; + if (len > state->wbits) { + strm->msg = (char *)"invalid window size"; + state->mode = BAD; + break; + } + state->dmax = 1U << len; + Tracev((stderr, "inflate: zlib header ok\n")); + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = hold & 0x200 ? DICTID : TYPE; + INITBITS(); + break; +#ifdef GUNZIP + case FLAGS: + NEEDBITS(16); + state->flags = (int)(hold); + if ((state->flags & 0xff) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + if (state->flags & 0xe000) { + strm->msg = (char *)"unknown header flags set"; + state->mode = BAD; + break; + } + if (state->head != Z_NULL) + state->head->text = (int)((hold >> 8) & 1); + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + state->mode = TIME; + case TIME: + NEEDBITS(32); + if (state->head != Z_NULL) + state->head->time = hold; + if (state->flags & 0x0200) CRC4(state->check, hold); + INITBITS(); + state->mode = OS; + case OS: + NEEDBITS(16); + if (state->head != Z_NULL) { + state->head->xflags = (int)(hold & 0xff); + state->head->os = (int)(hold >> 8); + } + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + state->mode = EXLEN; + case EXLEN: + if (state->flags & 0x0400) { + NEEDBITS(16); + state->length = (unsigned)(hold); + if (state->head != Z_NULL) + state->head->extra_len = (unsigned)hold; + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + } + else if (state->head != Z_NULL) + state->head->extra = Z_NULL; + state->mode = EXTRA; + case EXTRA: + if (state->flags & 0x0400) { + copy = state->length; + if (copy > have) copy = have; + if (copy) { + if (state->head != Z_NULL && + state->head->extra != Z_NULL) { + len = state->head->extra_len - state->length; + zmemcpy(state->head->extra + len, next, + len + copy > state->head->extra_max ? + state->head->extra_max - len : copy); + } + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + state->length -= copy; + } + if (state->length) goto inf_leave; + } + state->length = 0; + state->mode = NAME; + case NAME: + if (state->flags & 0x0800) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->name != Z_NULL && + state->length < state->head->name_max) + state->head->name[state->length++] = len; + } while (len && copy < have); + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->name = Z_NULL; + state->length = 0; + state->mode = COMMENT; + case COMMENT: + if (state->flags & 0x1000) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->comment != Z_NULL && + state->length < state->head->comm_max) + state->head->comment[state->length++] = len; + } while (len && copy < have); + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->comment = Z_NULL; + state->mode = HCRC; + case HCRC: + if (state->flags & 0x0200) { + NEEDBITS(16); + if (hold != (state->check & 0xffff)) { + strm->msg = (char *)"header crc mismatch"; + state->mode = BAD; + break; + } + INITBITS(); + } + if (state->head != Z_NULL) { + state->head->hcrc = (int)((state->flags >> 9) & 1); + state->head->done = 1; + } + strm->adler = state->check = crc32(0L, Z_NULL, 0); + state->mode = TYPE; + break; +#endif + case DICTID: + NEEDBITS(32); + strm->adler = state->check = REVERSE(hold); + INITBITS(); + state->mode = DICT; + case DICT: + if (state->havedict == 0) { + RESTORE(); + return Z_NEED_DICT; + } + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = TYPE; + case TYPE: + if (flush == Z_BLOCK) goto inf_leave; + case TYPEDO: + if (state->last) { + BYTEBITS(); + state->mode = CHECK; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN; /* decode codes */ + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + case STORED: + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + state->mode = COPY; + case COPY: + copy = state->length; + if (copy) { + if (copy > have) copy = have; + if (copy > left) copy = left; + if (copy == 0) goto inf_leave; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + break; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + case TABLE: + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + state->have = 0; + state->mode = LENLENS; + case LENLENS: + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + state->have = 0; + state->mode = CODELENS; + case CODELENS: + while (state->have < state->nlen + state->ndist) { + for (;;) { + this = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if (this.val < 16) { + NEEDBITS(this.bits); + DROPBITS(this.bits); + state->lens[state->have++] = this.val; + } + else { + if (this.val == 16) { + NEEDBITS(this.bits + 2); + DROPBITS(this.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = state->lens[state->have - 1]; + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (this.val == 17) { + NEEDBITS(this.bits + 3); + DROPBITS(this.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(this.bits + 7); + DROPBITS(this.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* build code tables */ + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (code const FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN; + case LEN: + if (have >= 6 && left >= 258) { + RESTORE(); + inflate_fast(strm, out); + LOAD(); + break; + } + for (;;) { + this = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if (this.op && (this.op & 0xf0) == 0) { + last = this; + for (;;) { + this = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + this.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(this.bits); + state->length = (unsigned)this.val; + if ((int)(this.op) == 0) { + Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", this.val)); + state->mode = LIT; + break; + } + if (this.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + if (this.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + state->extra = (unsigned)(this.op) & 15; + state->mode = LENEXT; + case LENEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + state->mode = DIST; + case DIST: + for (;;) { + this = state->distcode[BITS(state->distbits)]; + if ((unsigned)(this.bits) <= bits) break; + PULLBYTE(); + } + if ((this.op & 0xf0) == 0) { + last = this; + for (;;) { + this = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + this.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(this.bits); + if (this.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)this.val; + state->extra = (unsigned)(this.op) & 15; + state->mode = DISTEXT; + case DISTEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + } +#ifdef INFLATE_STRICT + if (state->offset > state->dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + if (state->offset > state->whave + out - left) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + state->mode = MATCH; + case MATCH: + if (left == 0) goto inf_leave; + copy = out - left; + if (state->offset > copy) { /* copy from window */ + copy = state->offset - copy; + if (copy > state->write) { + copy -= state->write; + from = state->window + (state->wsize - copy); + } + else + from = state->window + (state->write - copy); + if (copy > state->length) copy = state->length; + } + else { /* copy from output */ + from = put - state->offset; + copy = state->length; + } + if (copy > left) copy = left; + left -= copy; + state->length -= copy; + do { + *put++ = *from++; + } while (--copy); + if (state->length == 0) state->mode = LEN; + break; + case LIT: + if (left == 0) goto inf_leave; + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + case CHECK: + if (state->wrap) { + NEEDBITS(32); + out -= left; + strm->total_out += out; + state->total += out; + if (out) + strm->adler = state->check = + UPDATE(state->check, put - out, out); + out = left; + if (( +#ifdef GUNZIP + state->flags ? hold : +#endif + REVERSE(hold)) != state->check) { + strm->msg = (char *)"incorrect data check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: check matches trailer\n")); + } +#ifdef GUNZIP + state->mode = LENGTH; + case LENGTH: + if (state->wrap && state->flags) { + NEEDBITS(32); + if (hold != (state->total & 0xffffffffUL)) { + strm->msg = (char *)"incorrect length check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: length matches trailer\n")); + } +#endif + state->mode = DONE; + case DONE: + ret = Z_STREAM_END; + goto inf_leave; + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + case MEM: + return Z_MEM_ERROR; + case SYNC: + default: + return Z_STREAM_ERROR; + } + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + inf_leave: + RESTORE(); + if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) + if (updatewindow(strm, out)) { + state->mode = MEM; + return Z_MEM_ERROR; + } + in -= strm->avail_in; + out -= strm->avail_out; + strm->total_in += in; + strm->total_out += out; + state->total += out; + if (state->wrap && out) + strm->adler = state->check = + UPDATE(state->check, strm->next_out - out, out); + strm->data_type = state->bits + (state->last ? 64 : 0) + + (state->mode == TYPE ? 128 : 0); + if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) + ret = Z_BUF_ERROR; + return ret; +} + +int ZEXPORT inflateEnd(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->window != Z_NULL) ZFREE(strm, state->window); + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} + +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) +z_streamp strm; +const Bytef *dictionary; +uInt dictLength; +{ + struct inflate_state FAR *state; + unsigned long id; + + /* check state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->wrap != 0 && state->mode != DICT) + return Z_STREAM_ERROR; + + /* check for correct dictionary id */ + if (state->mode == DICT) { + id = adler32(0L, Z_NULL, 0); + id = adler32(id, dictionary, dictLength); + if (id != state->check) + return Z_DATA_ERROR; + } + + /* copy dictionary to window */ + if (updatewindow(strm, strm->avail_out)) { + state->mode = MEM; + return Z_MEM_ERROR; + } + if (dictLength > state->wsize) { + zmemcpy(state->window, dictionary + dictLength - state->wsize, + state->wsize); + state->whave = state->wsize; + } + else { + zmemcpy(state->window + state->wsize - dictLength, dictionary, + dictLength); + state->whave = dictLength; + } + state->havedict = 1; + Tracev((stderr, "inflate: dictionary set\n")); + return Z_OK; +} + +int ZEXPORT inflateGetHeader(strm, head) +z_streamp strm; +gz_headerp head; +{ + struct inflate_state FAR *state; + + /* check state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; + + /* save header structure */ + state->head = head; + head->done = 0; + return Z_OK; +} + +/* + Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found + or when out of input. When called, *have is the number of pattern bytes + found in order so far, in 0..3. On return *have is updated to the new + state. If on return *have equals four, then the pattern was found and the + return value is how many bytes were read including the last byte of the + pattern. If *have is less than four, then the pattern has not been found + yet and the return value is len. In the latter case, syncsearch() can be + called again with more data and the *have state. *have is initialized to + zero for the first call. + */ +local unsigned syncsearch(have, buf, len) +unsigned FAR *have; +unsigned char FAR *buf; +unsigned len; +{ + unsigned got; + unsigned next; + + got = *have; + next = 0; + while (next < len && got < 4) { + if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) + got++; + else if (buf[next]) + got = 0; + else + got = 4 - got; + next++; + } + *have = got; + return next; +} + +int ZEXPORT inflateSync(strm) +z_streamp strm; +{ + unsigned len; /* number of bytes to look at or looked at */ + unsigned long in, out; /* temporary to save total_in and total_out */ + unsigned char buf[4]; /* to restore bit buffer to byte string */ + struct inflate_state FAR *state; + + /* check parameters */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; + + /* if first time, start search in bit buffer */ + if (state->mode != SYNC) { + state->mode = SYNC; + state->hold <<= state->bits & 7; + state->bits -= state->bits & 7; + len = 0; + while (state->bits >= 8) { + buf[len++] = (unsigned char)(state->hold); + state->hold >>= 8; + state->bits -= 8; + } + state->have = 0; + syncsearch(&(state->have), buf, len); + } + + /* search available input */ + len = syncsearch(&(state->have), strm->next_in, strm->avail_in); + strm->avail_in -= len; + strm->next_in += len; + strm->total_in += len; + + /* return no joy or set up to restart inflate() on a new block */ + if (state->have != 4) return Z_DATA_ERROR; + in = strm->total_in; out = strm->total_out; + inflateReset(strm); + strm->total_in = in; strm->total_out = out; + state->mode = TYPE; + return Z_OK; +} + +/* + Returns true if inflate is currently at the end of a block generated by + Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP + implementation to provide an additional safety check. PPP uses + Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored + block. When decompressing, PPP checks that at the end of input packet, + inflate is waiting for these length bytes. + */ +int ZEXPORT inflateSyncPoint(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + return state->mode == STORED && state->bits == 0; +} + +int ZEXPORT inflateCopy(dest, source) +z_streamp dest; +z_streamp source; +{ + struct inflate_state FAR *state; + struct inflate_state FAR *copy; + unsigned char FAR *window; + unsigned wsize; + + /* check input */ + if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || + source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)source->state; + + /* allocate space */ + copy = (struct inflate_state FAR *) + ZALLOC(source, 1, sizeof(struct inflate_state)); + if (copy == Z_NULL) return Z_MEM_ERROR; + window = Z_NULL; + if (state->window != Z_NULL) { + window = (unsigned char FAR *) + ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); + if (window == Z_NULL) { + ZFREE(source, copy); + return Z_MEM_ERROR; + } + } + + /* copy state */ + zmemcpy(dest, source, sizeof(z_stream)); + zmemcpy(copy, state, sizeof(struct inflate_state)); + if (state->lencode >= state->codes && + state->lencode <= state->codes + ENOUGH - 1) { + copy->lencode = copy->codes + (state->lencode - state->codes); + copy->distcode = copy->codes + (state->distcode - state->codes); + } + copy->next = copy->codes + (state->next - state->codes); + if (window != Z_NULL) { + wsize = 1U << state->wbits; + zmemcpy(window, state->window, wsize); + } + copy->window = window; + dest->state = (struct internal_state FAR *)copy; + return Z_OK; +} diff --git a/zlib/inflate.h b/zlib/inflate.h new file mode 100644 index 00000000..07bd3e78 --- /dev/null +++ b/zlib/inflate.h @@ -0,0 +1,115 @@ +/* inflate.h -- internal inflate state definition + * Copyright (C) 1995-2004 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer decoding by inflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip decoding + should be left enabled. */ +#ifndef NO_GZIP +# define GUNZIP +#endif + +/* Possible inflate modes between inflate() calls */ +typedef enum { + HEAD, /* i: waiting for magic header */ + FLAGS, /* i: waiting for method and flags (gzip) */ + TIME, /* i: waiting for modification time (gzip) */ + OS, /* i: waiting for extra flags and operating system (gzip) */ + EXLEN, /* i: waiting for extra length (gzip) */ + EXTRA, /* i: waiting for extra bytes (gzip) */ + NAME, /* i: waiting for end of file name (gzip) */ + COMMENT, /* i: waiting for end of comment (gzip) */ + HCRC, /* i: waiting for header crc (gzip) */ + DICTID, /* i: waiting for dictionary check value */ + DICT, /* waiting for inflateSetDictionary() call */ + TYPE, /* i: waiting for type bits, including last-flag bit */ + TYPEDO, /* i: same, but skip check to exit inflate on new block */ + STORED, /* i: waiting for stored size (length and complement) */ + COPY, /* i/o: waiting for input or output to copy stored block */ + TABLE, /* i: waiting for dynamic block table lengths */ + LENLENS, /* i: waiting for code length code lengths */ + CODELENS, /* i: waiting for length/lit and distance code lengths */ + LEN, /* i: waiting for length/lit code */ + LENEXT, /* i: waiting for length extra bits */ + DIST, /* i: waiting for distance code */ + DISTEXT, /* i: waiting for distance extra bits */ + MATCH, /* o: waiting for output space to copy string */ + LIT, /* o: waiting for output space to write literal */ + CHECK, /* i: waiting for 32-bit check value */ + LENGTH, /* i: waiting for 32-bit length (gzip) */ + DONE, /* finished check, done -- remain here until reset */ + BAD, /* got a data error -- remain here until reset */ + MEM, /* got an inflate() memory error -- remain here until reset */ + SYNC /* looking for synchronization bytes to restart inflate() */ +} inflate_mode; + +/* + State transitions between above modes - + + (most modes can go to the BAD or MEM mode -- not shown for clarity) + + Process header: + HEAD -> (gzip) or (zlib) + (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME + NAME -> COMMENT -> HCRC -> TYPE + (zlib) -> DICTID or TYPE + DICTID -> DICT -> TYPE + Read deflate blocks: + TYPE -> STORED or TABLE or LEN or CHECK + STORED -> COPY -> TYPE + TABLE -> LENLENS -> CODELENS -> LEN + Read deflate codes: + LEN -> LENEXT or LIT or TYPE + LENEXT -> DIST -> DISTEXT -> MATCH -> LEN + LIT -> LEN + Process trailer: + CHECK -> LENGTH -> DONE + */ + +/* state maintained between inflate() calls. Approximately 7K bytes. */ +struct inflate_state { + inflate_mode mode; /* current inflate mode */ + int last; /* true if processing last block */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + int havedict; /* true if dictionary provided */ + int flags; /* gzip header method and flags (0 if zlib) */ + unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ + unsigned long check; /* protected copy of check value */ + unsigned long total; /* protected copy of output count */ + gz_headerp head; /* where to save gzip header information */ + /* sliding window */ + unsigned wbits; /* log base 2 of requested window size */ + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned write; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if needed */ + /* bit accumulator */ + unsigned long hold; /* input bit accumulator */ + unsigned bits; /* number of bits in "in" */ + /* for string and stored block copying */ + unsigned length; /* literal or length of data to copy */ + unsigned offset; /* distance back to copy string from */ + /* for table and code decoding */ + unsigned extra; /* extra bits needed */ + /* fixed and dynamic code tables */ + code const FAR *lencode; /* starting table for length/literal codes */ + code const FAR *distcode; /* starting table for distance codes */ + unsigned lenbits; /* index bits for lencode */ + unsigned distbits; /* index bits for distcode */ + /* dynamic table building */ + unsigned ncode; /* number of code length code lengths */ + unsigned nlen; /* number of length code lengths */ + unsigned ndist; /* number of distance code lengths */ + unsigned have; /* number of code lengths in lens[] */ + code FAR *next; /* next available space in codes[] */ + unsigned short lens[320]; /* temporary storage for code lengths */ + unsigned short work[288]; /* work area for code table building */ + code codes[ENOUGH]; /* space for code tables */ +}; diff --git a/zlib/inftrees.c b/zlib/inftrees.c new file mode 100644 index 00000000..8a9c13ff --- /dev/null +++ b/zlib/inftrees.c @@ -0,0 +1,329 @@ +/* inftrees.c -- generate Huffman trees for efficient decoding + * Copyright (C) 1995-2005 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" + +#define MAXBITS 15 + +const char inflate_copyright[] = + " inflate 1.2.3 Copyright 1995-2005 Mark Adler "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* + Build a set of tables to decode the provided canonical Huffman code. + The code lengths are lens[0..codes-1]. The result starts at *table, + whose indices are 0..2^bits-1. work is a writable array of at least + lens shorts, which is used as a work area. type is the type of code + to be generated, CODES, LENS, or DISTS. On return, zero is success, + -1 is an invalid code, and +1 means that ENOUGH isn't enough. table + on return points to the next available entry's address. bits is the + requested root table index bits, and on return it is the actual root + table index bits. It will differ if the request is greater than the + longest code or if it is less than the shortest code. + */ +int inflate_table(type, lens, codes, table, bits, work) +codetype type; +unsigned short FAR *lens; +unsigned codes; +code FAR * FAR *table; +unsigned FAR *bits; +unsigned short FAR *work; +{ + unsigned len; /* a code's length in bits */ + unsigned sym; /* index of code symbols */ + unsigned min, max; /* minimum and maximum code lengths */ + unsigned root; /* number of index bits for root table */ + unsigned curr; /* number of index bits for current table */ + unsigned drop; /* code bits to drop for sub-table */ + int left; /* number of prefix codes available */ + unsigned used; /* code entries in table used */ + unsigned huff; /* Huffman code */ + unsigned incr; /* for incrementing code, index */ + unsigned fill; /* index for replicating entries */ + unsigned low; /* low bits for current root entry */ + unsigned mask; /* mask for low root bits */ + code this; /* table entry for duplication */ + code FAR *next; /* next available space in table */ + const unsigned short FAR *base; /* base value table to use */ + const unsigned short FAR *extra; /* extra bits table to use */ + int end; /* use base and extra for symbol > end */ + unsigned short count[MAXBITS+1]; /* number of codes of each length */ + unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ + static const unsigned short lbase[31] = { /* Length codes 257..285 base */ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; + static const unsigned short lext[31] = { /* Length codes 257..285 extra */ + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196}; + static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577, 0, 0}; + static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ + 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, + 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, + 28, 28, 29, 29, 64, 64}; + + /* + Process a set of code lengths to create a canonical Huffman code. The + code lengths are lens[0..codes-1]. Each length corresponds to the + symbols 0..codes-1. The Huffman code is generated by first sorting the + symbols by length from short to long, and retaining the symbol order + for codes with equal lengths. Then the code starts with all zero bits + for the first code of the shortest length, and the codes are integer + increments for the same length, and zeros are appended as the length + increases. For the deflate format, these bits are stored backwards + from their more natural integer increment ordering, and so when the + decoding tables are built in the large loop below, the integer codes + are incremented backwards. + + This routine assumes, but does not check, that all of the entries in + lens[] are in the range 0..MAXBITS. The caller must assure this. + 1..MAXBITS is interpreted as that code length. zero means that that + symbol does not occur in this code. + + The codes are sorted by computing a count of codes for each length, + creating from that a table of starting indices for each length in the + sorted table, and then entering the symbols in order in the sorted + table. The sorted table is work[], with that space being provided by + the caller. + + The length counts are used for other purposes as well, i.e. finding + the minimum and maximum length codes, determining if there are any + codes at all, checking for a valid set of lengths, and looking ahead + at length counts to determine sub-table sizes when building the + decoding tables. + */ + + /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ + for (len = 0; len <= MAXBITS; len++) + count[len] = 0; + for (sym = 0; sym < codes; sym++) + count[lens[sym]]++; + + /* bound code lengths, force root to be within code lengths */ + root = *bits; + for (max = MAXBITS; max >= 1; max--) + if (count[max] != 0) break; + if (root > max) root = max; + if (max == 0) { /* no symbols to code at all */ + this.op = (unsigned char)64; /* invalid code marker */ + this.bits = (unsigned char)1; + this.val = (unsigned short)0; + *(*table)++ = this; /* make a table to force an error */ + *(*table)++ = this; + *bits = 1; + return 0; /* no symbols, but wait for decoding to report error */ + } + for (min = 1; min <= MAXBITS; min++) + if (count[min] != 0) break; + if (root < min) root = min; + + /* check for an over-subscribed or incomplete set of lengths */ + left = 1; + for (len = 1; len <= MAXBITS; len++) { + left <<= 1; + left -= count[len]; + if (left < 0) return -1; /* over-subscribed */ + } + if (left > 0 && (type == CODES || max != 1)) + return -1; /* incomplete set */ + + /* generate offsets into symbol table for each length for sorting */ + offs[1] = 0; + for (len = 1; len < MAXBITS; len++) + offs[len + 1] = offs[len] + count[len]; + + /* sort symbols by length, by symbol order within each length */ + for (sym = 0; sym < codes; sym++) + if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; + + /* + Create and fill in decoding tables. In this loop, the table being + filled is at next and has curr index bits. The code being used is huff + with length len. That code is converted to an index by dropping drop + bits off of the bottom. For codes where len is less than drop + curr, + those top drop + curr - len bits are incremented through all values to + fill the table with replicated entries. + + root is the number of index bits for the root table. When len exceeds + root, sub-tables are created pointed to by the root entry with an index + of the low root bits of huff. This is saved in low to check for when a + new sub-table should be started. drop is zero when the root table is + being filled, and drop is root when sub-tables are being filled. + + When a new sub-table is needed, it is necessary to look ahead in the + code lengths to determine what size sub-table is needed. The length + counts are used for this, and so count[] is decremented as codes are + entered in the tables. + + used keeps track of how many table entries have been allocated from the + provided *table space. It is checked when a LENS table is being made + against the space in *table, ENOUGH, minus the maximum space needed by + the worst case distance code, MAXD. This should never happen, but the + sufficiency of ENOUGH has not been proven exhaustively, hence the check. + This assumes that when type == LENS, bits == 9. + + sym increments through all symbols, and the loop terminates when + all codes of length max, i.e. all codes, have been processed. This + routine permits incomplete codes, so another loop after this one fills + in the rest of the decoding tables with invalid code markers. + */ + + /* set up for code type */ + switch (type) { + case CODES: + base = extra = work; /* dummy value--not used */ + end = 19; + break; + case LENS: + base = lbase; + base -= 257; + extra = lext; + extra -= 257; + end = 256; + break; + default: /* DISTS */ + base = dbase; + extra = dext; + end = -1; + } + + /* initialize state for loop */ + huff = 0; /* starting code */ + sym = 0; /* starting code symbol */ + len = min; /* starting code length */ + next = *table; /* current table to fill in */ + curr = root; /* current table index bits */ + drop = 0; /* current bits to drop from code for index */ + low = (unsigned)(-1); /* trigger new sub-table when len > root */ + used = 1U << root; /* use root table entries */ + mask = used - 1; /* mask for comparing low */ + + /* check available table space */ + if (type == LENS && used >= ENOUGH - MAXD) + return 1; + + /* process all codes and make table entries */ + for (;;) { + /* create table entry */ + this.bits = (unsigned char)(len - drop); + if ((int)(work[sym]) < end) { + this.op = (unsigned char)0; + this.val = work[sym]; + } + else if ((int)(work[sym]) > end) { + this.op = (unsigned char)(extra[work[sym]]); + this.val = base[work[sym]]; + } + else { + this.op = (unsigned char)(32 + 64); /* end of block */ + this.val = 0; + } + + /* replicate for those indices with low len bits equal to huff */ + incr = 1U << (len - drop); + fill = 1U << curr; + min = fill; /* save offset to next table */ + do { + fill -= incr; + next[(huff >> drop) + fill] = this; + } while (fill != 0); + + /* backwards increment the len-bit code huff */ + incr = 1U << (len - 1); + while (huff & incr) + incr >>= 1; + if (incr != 0) { + huff &= incr - 1; + huff += incr; + } + else + huff = 0; + + /* go to next symbol, update count, len */ + sym++; + if (--(count[len]) == 0) { + if (len == max) break; + len = lens[work[sym]]; + } + + /* create new sub-table if needed */ + if (len > root && (huff & mask) != low) { + /* if first time, transition to sub-tables */ + if (drop == 0) + drop = root; + + /* increment past last table */ + next += min; /* here min is 1 << curr */ + + /* determine length of next table */ + curr = len - drop; + left = (int)(1 << curr); + while (curr + drop < max) { + left -= count[curr + drop]; + if (left <= 0) break; + curr++; + left <<= 1; + } + + /* check for enough space */ + used += 1U << curr; + if (type == LENS && used >= ENOUGH - MAXD) + return 1; + + /* point entry in root table to sub-table */ + low = huff & mask; + (*table)[low].op = (unsigned char)curr; + (*table)[low].bits = (unsigned char)root; + (*table)[low].val = (unsigned short)(next - *table); + } + } + + /* + Fill in rest of table for incomplete codes. This loop is similar to the + loop above in incrementing huff for table indices. It is assumed that + len is equal to curr + drop, so there is no loop needed to increment + through high index bits. When the current sub-table is filled, the loop + drops back to the root table to fill in any remaining entries there. + */ + this.op = (unsigned char)64; /* invalid code marker */ + this.bits = (unsigned char)(len - drop); + this.val = (unsigned short)0; + while (huff != 0) { + /* when done with sub-table, drop back to root table */ + if (drop != 0 && (huff & mask) != low) { + drop = 0; + len = root; + next = *table; + this.bits = (unsigned char)len; + } + + /* put invalid code marker in table */ + next[huff >> drop] = this; + + /* backwards increment the len-bit code huff */ + incr = 1U << (len - 1); + while (huff & incr) + incr >>= 1; + if (incr != 0) { + huff &= incr - 1; + huff += incr; + } + else + huff = 0; + } + + /* set return parameters */ + *table += used; + *bits = root; + return 0; +} diff --git a/zlib/inftrees.h b/zlib/inftrees.h new file mode 100644 index 00000000..b1104c87 --- /dev/null +++ b/zlib/inftrees.h @@ -0,0 +1,55 @@ +/* inftrees.h -- header to use inftrees.c + * Copyright (C) 1995-2005 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* Structure for decoding tables. Each entry provides either the + information needed to do the operation requested by the code that + indexed that table entry, or it provides a pointer to another + table that indexes more bits of the code. op indicates whether + the entry is a pointer to another table, a literal, a length or + distance, an end-of-block, or an invalid code. For a table + pointer, the low four bits of op is the number of index bits of + that table. For a length or distance, the low four bits of op + is the number of extra bits to get after the code. bits is + the number of bits in this code or part of the code to drop off + of the bit buffer. val is the actual byte to output in the case + of a literal, the base length or distance, or the offset from + the current table to the next table. Each entry is four bytes. */ +typedef struct { + unsigned char op; /* operation, extra bits, table bits */ + unsigned char bits; /* bits in this part of the code */ + unsigned short val; /* offset in table or code value */ +} code; + +/* op values as set by inflate_table(): + 00000000 - literal + 0000tttt - table link, tttt != 0 is the number of table index bits + 0001eeee - length or distance, eeee is the number of extra bits + 01100000 - end of block + 01000000 - invalid code + */ + +/* Maximum size of dynamic tree. The maximum found in a long but non- + exhaustive search was 1444 code structures (852 for length/literals + and 592 for distances, the latter actually the result of an + exhaustive search). The true maximum is not known, but the value + below is more than safe. */ +#define ENOUGH 2048 +#define MAXD 592 + +/* Type of code to build for inftable() */ +typedef enum { + CODES, + LENS, + DISTS +} codetype; + +extern int inflate_table OF((codetype type, unsigned short FAR *lens, + unsigned codes, code FAR * FAR *table, + unsigned FAR *bits, unsigned short FAR *work)); diff --git a/zlib/trees.c b/zlib/trees.c new file mode 100644 index 00000000..395e4e16 --- /dev/null +++ b/zlib/trees.c @@ -0,0 +1,1219 @@ +/* trees.c -- output deflated data using Huffman coding + * Copyright (C) 1995-2005 Jean-loup Gailly + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process uses several Huffman trees. The more + * common source values are represented by shorter bit sequences. + * + * Each code tree is stored in a compressed form which is itself + * a Huffman encoding of the lengths of all the code strings (in + * ascending order by source values). The actual code strings are + * reconstructed from the lengths in the inflate process, as described + * in the deflate specification. + * + * REFERENCES + * + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc + * + * Storer, James A. + * Data Compression: Methods and Theory, pp. 49-50. + * Computer Science Press, 1988. ISBN 0-7167-8156-5. + * + * Sedgewick, R. + * Algorithms, p290. + * Addison-Wesley, 1983. ISBN 0-201-06672-6. + */ + +/* @(#) $Id$ */ + +/* #define GEN_TREES_H */ + +#include "deflate.h" + +#ifdef DEBUG +# include +#endif + +/* =========================================================================== + * Constants + */ + +#define MAX_BL_BITS 7 +/* Bit length codes must not exceed MAX_BL_BITS bits */ + +#define END_BLOCK 256 +/* end of block literal code */ + +#define REP_3_6 16 +/* repeat previous bit length 3-6 times (2 bits of repeat count) */ + +#define REPZ_3_10 17 +/* repeat a zero length 3-10 times (3 bits of repeat count) */ + +#define REPZ_11_138 18 +/* repeat a zero length 11-138 times (7 bits of repeat count) */ + +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ + = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; + +local const int extra_dbits[D_CODES] /* extra bits for each distance code */ + = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; + +local const uch bl_order[BL_CODES] + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; +/* The lengths of the bit length codes are sent in order of decreasing + * probability, to avoid transmitting the lengths for unused bit length codes. + */ + +#define Buf_size (8 * 2*sizeof(char)) +/* Number of bits used within bi_buf. (bi_buf might be implemented on + * more than 16 bits on some systems.) + */ + +/* =========================================================================== + * Local data. These are initialized only once. + */ + +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ + +#if defined(GEN_TREES_H) || !defined(STDC) +/* non ANSI compilers may not accept trees.h */ + +local ct_data static_ltree[L_CODES+2]; +/* The static literal tree. Since the bit lengths are imposed, there is no + * need for the L_CODES extra codes used during heap construction. However + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init + * below). + */ + +local ct_data static_dtree[D_CODES]; +/* The static distance tree. (Actually a trivial tree since all codes use + * 5 bits.) + */ + +uch _dist_code[DIST_CODE_LEN]; +/* Distance codes. The first 256 values correspond to the distances + * 3 .. 258, the last 256 values correspond to the top 8 bits of + * the 15 bit distances. + */ + +uch _length_code[MAX_MATCH-MIN_MATCH+1]; +/* length code for each normalized match length (0 == MIN_MATCH) */ + +local int base_length[LENGTH_CODES]; +/* First normalized length for each code (0 = MIN_MATCH) */ + +local int base_dist[D_CODES]; +/* First normalized distance for each code (0 = distance of 1) */ + +#else +# include "trees.h" +#endif /* GEN_TREES_H */ + +struct static_tree_desc_s { + const ct_data *static_tree; /* static tree or NULL */ + const intf *extra_bits; /* extra bits for each code or NULL */ + int extra_base; /* base index for extra_bits */ + int elems; /* max number of elements in the tree */ + int max_length; /* max bit length for the codes */ +}; + +local static_tree_desc static_l_desc = +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; + +local static_tree_desc static_d_desc = +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; + +local static_tree_desc static_bl_desc = +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; + +/* =========================================================================== + * Local (static) routines in this file. + */ + +local void tr_static_init OF((void)); +local void init_block OF((deflate_state *s)); +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); +local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); +local void build_tree OF((deflate_state *s, tree_desc *desc)); +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local int build_bl_tree OF((deflate_state *s)); +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, + int blcodes)); +local void compress_block OF((deflate_state *s, ct_data *ltree, + ct_data *dtree)); +local void set_data_type OF((deflate_state *s)); +local unsigned bi_reverse OF((unsigned value, int length)); +local void bi_windup OF((deflate_state *s)); +local void bi_flush OF((deflate_state *s)); +local void copy_block OF((deflate_state *s, charf *buf, unsigned len, + int header)); + +#ifdef GEN_TREES_H +local void gen_trees_header OF((void)); +#endif + +#ifndef DEBUG +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) + /* Send a code of the given tree. c and tree must not have side effects */ + +#else /* DEBUG */ +# define send_code(s, c, tree) \ + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ + send_bits(s, tree[c].Code, tree[c].Len); } +#endif + +/* =========================================================================== + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. + */ +#define put_short(s, w) { \ + put_byte(s, (uch)((w) & 0xff)); \ + put_byte(s, (uch)((ush)(w) >> 8)); \ +} + +/* =========================================================================== + * Send a value on a given number of bits. + * IN assertion: length <= 16 and value fits in length bits. + */ +#ifdef DEBUG +local void send_bits OF((deflate_state *s, int value, int length)); + +local void send_bits(s, value, length) + deflate_state *s; + int value; /* value to send */ + int length; /* number of bits */ +{ + Tracevv((stderr," l %2d v %4x ", length, value)); + Assert(length > 0 && length <= 15, "invalid length"); + s->bits_sent += (ulg)length; + + /* If not enough room in bi_buf, use (valid) bits from bi_buf and + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) + * unused bits in value. + */ + if (s->bi_valid > (int)Buf_size - length) { + s->bi_buf |= (value << s->bi_valid); + put_short(s, s->bi_buf); + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); + s->bi_valid += length - Buf_size; + } else { + s->bi_buf |= value << s->bi_valid; + s->bi_valid += length; + } +} +#else /* !DEBUG */ + +#define send_bits(s, value, length) \ +{ int len = length;\ + if (s->bi_valid > (int)Buf_size - len) {\ + int val = value;\ + s->bi_buf |= (val << s->bi_valid);\ + put_short(s, s->bi_buf);\ + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ + s->bi_valid += len - Buf_size;\ + } else {\ + s->bi_buf |= (value) << s->bi_valid;\ + s->bi_valid += len;\ + }\ +} +#endif /* DEBUG */ + + +/* the arguments must not have side effects */ + +/* =========================================================================== + * Initialize the various 'constant' tables. + */ +local void tr_static_init() +{ +#if defined(GEN_TREES_H) || !defined(STDC) + static int static_init_done = 0; + int n; /* iterates over tree elements */ + int bits; /* bit counter */ + int length; /* length value */ + int code; /* code value */ + int dist; /* distance index */ + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + if (static_init_done) return; + + /* For some embedded targets, global variables are not initialized: */ + static_l_desc.static_tree = static_ltree; + static_l_desc.extra_bits = extra_lbits; + static_d_desc.static_tree = static_dtree; + static_d_desc.extra_bits = extra_dbits; + static_bl_desc.extra_bits = extra_blbits; + + /* Initialize the mapping length (0..255) -> length code (0..28) */ + length = 0; + for (code = 0; code < LENGTH_CODES-1; code++) { + base_length[code] = length; + for (n = 0; n < (1< dist code (0..29) */ + dist = 0; + for (code = 0 ; code < 16; code++) { + base_dist[code] = dist; + for (n = 0; n < (1<>= 7; /* from now on, all distances are divided by 128 */ + for ( ; code < D_CODES; code++) { + base_dist[code] = dist << 7; + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { + _dist_code[256 + dist++] = (uch)code; + } + } + Assert (dist == 256, "tr_static_init: 256+dist != 512"); + + /* Construct the codes of the static literal tree */ + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; + n = 0; + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; + /* Codes 286 and 287 do not exist, but we must include them in the + * tree construction to get a canonical Huffman tree (longest code + * all ones) + */ + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); + + /* The static distance tree is trivial: */ + for (n = 0; n < D_CODES; n++) { + static_dtree[n].Len = 5; + static_dtree[n].Code = bi_reverse((unsigned)n, 5); + } + static_init_done = 1; + +# ifdef GEN_TREES_H + gen_trees_header(); +# endif +#endif /* defined(GEN_TREES_H) || !defined(STDC) */ +} + +/* =========================================================================== + * Genererate the file trees.h describing the static trees. + */ +#ifdef GEN_TREES_H +# ifndef DEBUG +# include +# endif + +# define SEPARATOR(i, last, width) \ + ((i) == (last)? "\n};\n\n" : \ + ((i) % (width) == (width)-1 ? ",\n" : ", ")) + +void gen_trees_header() +{ + FILE *header = fopen("trees.h", "w"); + int i; + + Assert (header != NULL, "Can't open trees.h"); + fprintf(header, + "/* header created automatically with -DGEN_TREES_H */\n\n"); + + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); + for (i = 0; i < L_CODES+2; i++) { + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); + } + + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); + } + + fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n"); + for (i = 0; i < DIST_CODE_LEN; i++) { + fprintf(header, "%2u%s", _dist_code[i], + SEPARATOR(i, DIST_CODE_LEN-1, 20)); + } + + fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { + fprintf(header, "%2u%s", _length_code[i], + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); + } + + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); + for (i = 0; i < LENGTH_CODES; i++) { + fprintf(header, "%1u%s", base_length[i], + SEPARATOR(i, LENGTH_CODES-1, 20)); + } + + fprintf(header, "local const int base_dist[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "%5u%s", base_dist[i], + SEPARATOR(i, D_CODES-1, 10)); + } + + fclose(header); +} +#endif /* GEN_TREES_H */ + +/* =========================================================================== + * Initialize the tree data structures for a new zlib stream. + */ +void _tr_init(s) + deflate_state *s; +{ + tr_static_init(); + + s->l_desc.dyn_tree = s->dyn_ltree; + s->l_desc.stat_desc = &static_l_desc; + + s->d_desc.dyn_tree = s->dyn_dtree; + s->d_desc.stat_desc = &static_d_desc; + + s->bl_desc.dyn_tree = s->bl_tree; + s->bl_desc.stat_desc = &static_bl_desc; + + s->bi_buf = 0; + s->bi_valid = 0; + s->last_eob_len = 8; /* enough lookahead for inflate */ +#ifdef DEBUG + s->compressed_len = 0L; + s->bits_sent = 0L; +#endif + + /* Initialize the first block of the first file: */ + init_block(s); +} + +/* =========================================================================== + * Initialize a new block. + */ +local void init_block(s) + deflate_state *s; +{ + int n; /* iterates over tree elements */ + + /* Initialize the trees. */ + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; + + s->dyn_ltree[END_BLOCK].Freq = 1; + s->opt_len = s->static_len = 0L; + s->last_lit = s->matches = 0; +} + +#define SMALLEST 1 +/* Index within the heap array of least frequent node in the Huffman tree */ + + +/* =========================================================================== + * Remove the smallest element from the heap and recreate the heap with + * one less element. Updates heap and heap_len. + */ +#define pqremove(s, tree, top) \ +{\ + top = s->heap[SMALLEST]; \ + s->heap[SMALLEST] = s->heap[s->heap_len--]; \ + pqdownheap(s, tree, SMALLEST); \ +} + +/* =========================================================================== + * Compares to subtrees, using the tree depth as tie breaker when + * the subtrees have equal frequency. This minimizes the worst case length. + */ +#define smaller(tree, n, m, depth) \ + (tree[n].Freq < tree[m].Freq || \ + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) + +/* =========================================================================== + * Restore the heap property by moving down the tree starting at node k, + * exchanging a node with the smallest of its two sons if necessary, stopping + * when the heap property is re-established (each father smaller than its + * two sons). + */ +local void pqdownheap(s, tree, k) + deflate_state *s; + ct_data *tree; /* the tree to restore */ + int k; /* node to move down */ +{ + int v = s->heap[k]; + int j = k << 1; /* left son of k */ + while (j <= s->heap_len) { + /* Set j to the smallest of the two sons: */ + if (j < s->heap_len && + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { + j++; + } + /* Exit if v is smaller than both sons */ + if (smaller(tree, v, s->heap[j], s->depth)) break; + + /* Exchange v with the smallest son */ + s->heap[k] = s->heap[j]; k = j; + + /* And continue down the tree, setting j to the left son of k */ + j <<= 1; + } + s->heap[k] = v; +} + +/* =========================================================================== + * Compute the optimal bit lengths for a tree and update the total bit length + * for the current block. + * IN assertion: the fields freq and dad are set, heap[heap_max] and + * above are the tree nodes sorted by increasing frequency. + * OUT assertions: the field len is set to the optimal bit length, the + * array bl_count contains the frequencies for each bit length. + * The length opt_len is updated; static_len is also updated if stree is + * not null. + */ +local void gen_bitlen(s, desc) + deflate_state *s; + tree_desc *desc; /* the tree descriptor */ +{ + ct_data *tree = desc->dyn_tree; + int max_code = desc->max_code; + const ct_data *stree = desc->stat_desc->static_tree; + const intf *extra = desc->stat_desc->extra_bits; + int base = desc->stat_desc->extra_base; + int max_length = desc->stat_desc->max_length; + int h; /* heap index */ + int n, m; /* iterate over the tree elements */ + int bits; /* bit length */ + int xbits; /* extra bits */ + ush f; /* frequency */ + int overflow = 0; /* number of elements with bit length too large */ + + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; + + /* In a first pass, compute the optimal bit lengths (which may + * overflow in the case of the bit length tree). + */ + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ + + for (h = s->heap_max+1; h < HEAP_SIZE; h++) { + n = s->heap[h]; + bits = tree[tree[n].Dad].Len + 1; + if (bits > max_length) bits = max_length, overflow++; + tree[n].Len = (ush)bits; + /* We overwrite tree[n].Dad which is no longer needed */ + + if (n > max_code) continue; /* not a leaf node */ + + s->bl_count[bits]++; + xbits = 0; + if (n >= base) xbits = extra[n-base]; + f = tree[n].Freq; + s->opt_len += (ulg)f * (bits + xbits); + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); + } + if (overflow == 0) return; + + Trace((stderr,"\nbit length overflow\n")); + /* This happens for example on obj2 and pic of the Calgary corpus */ + + /* Find the first bit length which could increase: */ + do { + bits = max_length-1; + while (s->bl_count[bits] == 0) bits--; + s->bl_count[bits]--; /* move one leaf down the tree */ + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ + s->bl_count[max_length]--; + /* The brother of the overflow item also moves one step up, + * but this does not affect bl_count[max_length] + */ + overflow -= 2; + } while (overflow > 0); + + /* Now recompute all bit lengths, scanning in increasing frequency. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all + * lengths instead of fixing only the wrong ones. This idea is taken + * from 'ar' written by Haruhiko Okumura.) + */ + for (bits = max_length; bits != 0; bits--) { + n = s->bl_count[bits]; + while (n != 0) { + m = s->heap[--h]; + if (m > max_code) continue; + if ((unsigned) tree[m].Len != (unsigned) bits) { + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s->opt_len += ((long)bits - (long)tree[m].Len) + *(long)tree[m].Freq; + tree[m].Len = (ush)bits; + } + n--; + } + } +} + +/* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ +local void gen_codes (tree, max_code, bl_count) + ct_data *tree; /* the tree to decorate */ + int max_code; /* largest code with non zero frequency */ + ushf *bl_count; /* number of codes at each bit length */ +{ + ush next_code[MAX_BITS+1]; /* next code value for each bit length */ + ush code = 0; /* running code value */ + int bits; /* bit index */ + int n; /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + for (bits = 1; bits <= MAX_BITS; bits++) { + next_code[bits] = code = (code + bl_count[bits-1]) << 1; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + Assert (code + bl_count[MAX_BITS]-1 == (1<dyn_tree; + const ct_data *stree = desc->stat_desc->static_tree; + int elems = desc->stat_desc->elems; + int n, m; /* iterate over heap elements */ + int max_code = -1; /* largest code with non zero frequency */ + int node; /* new node being created */ + + /* Construct the initial heap, with least frequent element in + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[0] is not used. + */ + s->heap_len = 0, s->heap_max = HEAP_SIZE; + + for (n = 0; n < elems; n++) { + if (tree[n].Freq != 0) { + s->heap[++(s->heap_len)] = max_code = n; + s->depth[n] = 0; + } else { + tree[n].Len = 0; + } + } + + /* The pkzip format requires that at least one distance code exists, + * and that at least one bit should be sent even if there is only one + * possible code. So to avoid special checks later on we force at least + * two codes of non zero frequency. + */ + while (s->heap_len < 2) { + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); + tree[node].Freq = 1; + s->depth[node] = 0; + s->opt_len--; if (stree) s->static_len -= stree[node].Len; + /* node is 0 or 1 so it does not have extra bits */ + } + desc->max_code = max_code; + + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + * establish sub-heaps of increasing lengths: + */ + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); + + /* Construct the Huffman tree by repeatedly combining the least two + * frequent nodes. + */ + node = elems; /* next internal node of the tree */ + do { + pqremove(s, tree, n); /* n = node of least frequency */ + m = s->heap[SMALLEST]; /* m = node of next least frequency */ + + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ + s->heap[--(s->heap_max)] = m; + + /* Create a new node father of n and m */ + tree[node].Freq = tree[n].Freq + tree[m].Freq; + s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? + s->depth[n] : s->depth[m]) + 1); + tree[n].Dad = tree[m].Dad = (ush)node; +#ifdef DUMP_BL_TREE + if (tree == s->bl_tree) { + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); + } +#endif + /* and insert the new node in the heap */ + s->heap[SMALLEST] = node++; + pqdownheap(s, tree, SMALLEST); + + } while (s->heap_len >= 2); + + s->heap[--(s->heap_max)] = s->heap[SMALLEST]; + + /* At this point, the fields freq and dad are set. We can now + * generate the bit lengths. + */ + gen_bitlen(s, (tree_desc *)desc); + + /* The field len is now set, we can generate the bit codes */ + gen_codes ((ct_data *)tree, max_code, s->bl_count); +} + +/* =========================================================================== + * Scan a literal or distance tree to determine the frequencies of the codes + * in the bit length tree. + */ +local void scan_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + if (nextlen == 0) max_count = 138, min_count = 3; + tree[max_code+1].Len = (ush)0xffff; /* guard */ + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + s->bl_tree[curlen].Freq += count; + } else if (curlen != 0) { + if (curlen != prevlen) s->bl_tree[curlen].Freq++; + s->bl_tree[REP_3_6].Freq++; + } else if (count <= 10) { + s->bl_tree[REPZ_3_10].Freq++; + } else { + s->bl_tree[REPZ_11_138].Freq++; + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Send a literal or distance tree in compressed form, using the codes in + * bl_tree. + */ +local void send_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + /* tree[max_code+1].Len = -1; */ /* guard already set */ + if (nextlen == 0) max_count = 138, min_count = 3; + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + do { send_code(s, curlen, s->bl_tree); } while (--count != 0); + + } else if (curlen != 0) { + if (curlen != prevlen) { + send_code(s, curlen, s->bl_tree); count--; + } + Assert(count >= 3 && count <= 6, " 3_6?"); + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); + + } else if (count <= 10) { + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); + + } else { + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Construct the Huffman tree for the bit lengths and return the index in + * bl_order of the last bit length code to send. + */ +local int build_bl_tree(s) + deflate_state *s; +{ + int max_blindex; /* index of last bit length code of non zero freq */ + + /* Determine the bit length frequencies for literal and distance trees */ + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); + + /* Build the bit length tree: */ + build_tree(s, (tree_desc *)(&(s->bl_desc))); + /* opt_len now includes the length of the tree representations, except + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + */ + + /* Determine the number of bit length codes to send. The pkzip format + * requires that at least 4 bit length codes be sent. (appnote.txt says + * 3 but the actual value used is 4.) + */ + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; + } + /* Update opt_len to include the bit length tree and counts */ + s->opt_len += 3*(max_blindex+1) + 5+5+4; + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", + s->opt_len, s->static_len)); + + return max_blindex; +} + +/* =========================================================================== + * Send the header for a block using dynamic Huffman trees: the counts, the + * lengths of the bit length codes, the literal tree and the distance tree. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. + */ +local void send_all_trees(s, lcodes, dcodes, blcodes) + deflate_state *s; + int lcodes, dcodes, blcodes; /* number of codes for each tree */ +{ + int rank; /* index in bl_order */ + + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, + "too many codes"); + Tracev((stderr, "\nbl counts: ")); + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ + send_bits(s, dcodes-1, 5); + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ + for (rank = 0; rank < blcodes; rank++) { + Tracev((stderr, "\nbl code %2d ", bl_order[rank])); + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); + } + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); +} + +/* =========================================================================== + * Send a stored block + */ +void _tr_stored_block(s, buf, stored_len, eof) + deflate_state *s; + charf *buf; /* input block */ + ulg stored_len; /* length of input block */ + int eof; /* true if this is the last block for a file */ +{ + send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ +#ifdef DEBUG + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; + s->compressed_len += (stored_len + 4) << 3; +#endif + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ +} + +/* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + * The current inflate code requires 9 bits of lookahead. If the + * last two codes for the previous block (real code plus EOB) were coded + * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode + * the last real code. In this case we send two empty static blocks instead + * of one. (There are no problems if the previous block is stored or fixed.) + * To simplify the code, we assume the worst case of last real code encoded + * on one bit only. + */ +void _tr_align(s) + deflate_state *s; +{ + send_bits(s, STATIC_TREES<<1, 3); + send_code(s, END_BLOCK, static_ltree); +#ifdef DEBUG + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ +#endif + bi_flush(s); + /* Of the 10 bits for the empty block, we have already sent + * (10 - bi_valid) bits. The lookahead for the last real code (before + * the EOB of the previous block) was thus at least one plus the length + * of the EOB plus what we have just sent of the empty static block. + */ + if (1 + s->last_eob_len + 10 - s->bi_valid < 9) { + send_bits(s, STATIC_TREES<<1, 3); + send_code(s, END_BLOCK, static_ltree); +#ifdef DEBUG + s->compressed_len += 10L; +#endif + bi_flush(s); + } + s->last_eob_len = 7; +} + +/* =========================================================================== + * Determine the best encoding for the current block: dynamic trees, static + * trees or store, and output the encoded block to the zip file. + */ +void _tr_flush_block(s, buf, stored_len, eof) + deflate_state *s; + charf *buf; /* input block, or NULL if too old */ + ulg stored_len; /* length of input block */ + int eof; /* true if this is the last block for a file */ +{ + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ + int max_blindex = 0; /* index of last bit length code of non zero freq */ + + /* Build the Huffman trees unless a stored block is forced */ + if (s->level > 0) { + + /* Check if the file is binary or text */ + if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN) + set_data_type(s); + + /* Construct the literal and distance trees */ + build_tree(s, (tree_desc *)(&(s->l_desc))); + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + + build_tree(s, (tree_desc *)(&(s->d_desc))); + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ + + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); + + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s->opt_len+3+7)>>3; + static_lenb = (s->static_len+3+7)>>3; + + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + s->last_lit)); + + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + + } else { + Assert(buf != (char*)0, "lost buf"); + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + } + +#ifdef FORCE_STORED + if (buf != (char*)0) { /* force stored block */ +#else + if (stored_len+4 <= opt_lenb && buf != (char*)0) { + /* 4: two words for the lengths */ +#endif + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. + * Otherwise we can't have processed more than WSIZE input bytes since + * the last block flush, because compression would have been + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to + * transform a block into a stored block. + */ + _tr_stored_block(s, buf, stored_len, eof); + +#ifdef FORCE_STATIC + } else if (static_lenb >= 0) { /* force static trees */ +#else + } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { +#endif + send_bits(s, (STATIC_TREES<<1)+eof, 3); + compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); +#ifdef DEBUG + s->compressed_len += 3 + s->static_len; +#endif + } else { + send_bits(s, (DYN_TREES<<1)+eof, 3); + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, + max_blindex+1); + compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); +#ifdef DEBUG + s->compressed_len += 3 + s->opt_len; +#endif + } + Assert (s->compressed_len == s->bits_sent, "bad compressed size"); + /* The above check is made mod 2^32, for files larger than 512 MB + * and uLong implemented on 32 bits. + */ + init_block(s); + + if (eof) { + bi_windup(s); +#ifdef DEBUG + s->compressed_len += 7; /* align on byte boundary */ +#endif + } + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, + s->compressed_len-7*eof)); +} + +/* =========================================================================== + * Save the match info and tally the frequency counts. Return true if + * the current block must be flushed. + */ +int _tr_tally (s, dist, lc) + deflate_state *s; + unsigned dist; /* distance of matched string */ + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ +{ + s->d_buf[s->last_lit] = (ush)dist; + s->l_buf[s->last_lit++] = (uch)lc; + if (dist == 0) { + /* lc is the unmatched char */ + s->dyn_ltree[lc].Freq++; + } else { + s->matches++; + /* Here, lc is the match length - MIN_MATCH */ + dist--; /* dist = match distance - 1 */ + Assert((ush)dist < (ush)MAX_DIST(s) && + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); + + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; + s->dyn_dtree[d_code(dist)].Freq++; + } + +#ifdef TRUNCATE_BLOCK + /* Try to guess if it is profitable to stop the current block here */ + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { + /* Compute an upper bound for the compressed length */ + ulg out_length = (ulg)s->last_lit*8L; + ulg in_length = (ulg)((long)s->strstart - s->block_start); + int dcode; + for (dcode = 0; dcode < D_CODES; dcode++) { + out_length += (ulg)s->dyn_dtree[dcode].Freq * + (5L+extra_dbits[dcode]); + } + out_length >>= 3; + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", + s->last_lit, in_length, out_length, + 100L - out_length*100L/in_length)); + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; + } +#endif + return (s->last_lit == s->lit_bufsize-1); + /* We avoid equality with lit_bufsize because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ +} + +/* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ +local void compress_block(s, ltree, dtree) + deflate_state *s; + ct_data *ltree; /* literal tree */ + ct_data *dtree; /* distance tree */ +{ + unsigned dist; /* distance of matched string */ + int lc; /* match length or unmatched char (if dist == 0) */ + unsigned lx = 0; /* running index in l_buf */ + unsigned code; /* the code to send */ + int extra; /* number of extra bits to send */ + + if (s->last_lit != 0) do { + dist = s->d_buf[lx]; + lc = s->l_buf[lx++]; + if (dist == 0) { + send_code(s, lc, ltree); /* send a literal byte */ + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code+LITERALS+1, ltree); /* send the length code */ + extra = extra_lbits[code]; + if (extra != 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); /* send the extra length bits */ + } + dist--; /* dist is now the match distance - 1 */ + code = d_code(dist); + Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); /* send the distance code */ + extra = extra_dbits[code]; + if (extra != 0) { + dist -= base_dist[code]; + send_bits(s, dist, extra); /* send the extra distance bits */ + } + } /* literal or match pair ? */ + + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ + Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + "pendingBuf overflow"); + + } while (lx < s->last_lit); + + send_code(s, END_BLOCK, ltree); + s->last_eob_len = ltree[END_BLOCK].Len; +} + +/* =========================================================================== + * Set the data type to BINARY or TEXT, using a crude approximation: + * set it to Z_TEXT if all symbols are either printable characters (33 to 255) + * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise. + * IN assertion: the fields Freq of dyn_ltree are set. + */ +local void set_data_type(s) + deflate_state *s; +{ + int n; + + for (n = 0; n < 9; n++) + if (s->dyn_ltree[n].Freq != 0) + break; + if (n == 9) + for (n = 14; n < 32; n++) + if (s->dyn_ltree[n].Freq != 0) + break; + s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY; +} + +/* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ +local unsigned bi_reverse(code, len) + unsigned code; /* the value to invert */ + int len; /* its bit length */ +{ + register unsigned res = 0; + do { + res |= code & 1; + code >>= 1, res <<= 1; + } while (--len > 0); + return res >> 1; +} + +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +local void bi_flush(s) + deflate_state *s; +{ + if (s->bi_valid == 16) { + put_short(s, s->bi_buf); + s->bi_buf = 0; + s->bi_valid = 0; + } else if (s->bi_valid >= 8) { + put_byte(s, (Byte)s->bi_buf); + s->bi_buf >>= 8; + s->bi_valid -= 8; + } +} + +/* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ +local void bi_windup(s) + deflate_state *s; +{ + if (s->bi_valid > 8) { + put_short(s, s->bi_buf); + } else if (s->bi_valid > 0) { + put_byte(s, (Byte)s->bi_buf); + } + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef DEBUG + s->bits_sent = (s->bits_sent+7) & ~7; +#endif +} + +/* =========================================================================== + * Copy a stored block, storing first the length and its + * one's complement if requested. + */ +local void copy_block(s, buf, len, header) + deflate_state *s; + charf *buf; /* the input data */ + unsigned len; /* its length */ + int header; /* true if block header must be written */ +{ + bi_windup(s); /* align on byte boundary */ + s->last_eob_len = 8; /* enough lookahead for inflate */ + + if (header) { + put_short(s, (ush)len); + put_short(s, (ush)~len); +#ifdef DEBUG + s->bits_sent += 2*16; +#endif + } +#ifdef DEBUG + s->bits_sent += (ulg)len<<3; +#endif + while (len--) { + put_byte(s, *buf++); + } +} diff --git a/zlib/trees.h b/zlib/trees.h new file mode 100644 index 00000000..72facf90 --- /dev/null +++ b/zlib/trees.h @@ -0,0 +1,128 @@ +/* header created automatically with -DGEN_TREES_H */ + +local const ct_data static_ltree[L_CODES+2] = { +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} +}; + +local const ct_data static_dtree[D_CODES] = { +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} +}; + +const uch _dist_code[DIST_CODE_LEN] = { + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 +}; + +const uch _length_code[MAX_MATCH-MIN_MATCH+1]= { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 +}; + +local const int base_length[LENGTH_CODES] = { +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, +64, 80, 96, 112, 128, 160, 192, 224, 0 +}; + +local const int base_dist[D_CODES] = { + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 +}; + diff --git a/zlib/uncompr.c b/zlib/uncompr.c new file mode 100644 index 00000000..b59e3d0d --- /dev/null +++ b/zlib/uncompr.c @@ -0,0 +1,61 @@ +/* uncompr.c -- decompress a memory buffer + * Copyright (C) 1995-2003 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#define ZLIB_INTERNAL +#include "zlib.h" + +/* =========================================================================== + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total + size of the destination buffer, which must be large enough to hold the + entire uncompressed data. (The size of the uncompressed data must have + been saved previously by the compressor and transmitted to the decompressor + by some mechanism outside the scope of this compression library.) + Upon exit, destLen is the actual size of the compressed buffer. + This function can be used to decompress a whole file at once if the + input file is mmap'ed. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted. +*/ +int ZEXPORT uncompress (dest, destLen, source, sourceLen) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; +{ + z_stream stream; + int err; + + stream.next_in = (Bytef*)source; + stream.avail_in = (uInt)sourceLen; + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; + + stream.next_out = dest; + stream.avail_out = (uInt)*destLen; + if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; + + stream.zalloc = (alloc_func)0; + stream.zfree = (free_func)0; + + err = inflateInit(&stream); + if (err != Z_OK) return err; + + err = inflate(&stream, Z_FINISH); + if (err != Z_STREAM_END) { + inflateEnd(&stream); + if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) + return Z_DATA_ERROR; + return err; + } + *destLen = stream.total_out; + + err = inflateEnd(&stream); + return err; +} diff --git a/zlib/zconf.h b/zlib/zconf.h new file mode 100644 index 00000000..03a9431c --- /dev/null +++ b/zlib/zconf.h @@ -0,0 +1,332 @@ +/* zconf.h -- configuration of the zlib compression library + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#ifndef ZCONF_H +#define ZCONF_H + +/* + * If you *really* need a unique prefix for all types and library functions, + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. + */ +#ifdef Z_PREFIX +# define deflateInit_ z_deflateInit_ +# define deflate z_deflate +# define deflateEnd z_deflateEnd +# define inflateInit_ z_inflateInit_ +# define inflate z_inflate +# define inflateEnd z_inflateEnd +# define deflateInit2_ z_deflateInit2_ +# define deflateSetDictionary z_deflateSetDictionary +# define deflateCopy z_deflateCopy +# define deflateReset z_deflateReset +# define deflateParams z_deflateParams +# define deflateBound z_deflateBound +# define deflatePrime z_deflatePrime +# define inflateInit2_ z_inflateInit2_ +# define inflateSetDictionary z_inflateSetDictionary +# define inflateSync z_inflateSync +# define inflateSyncPoint z_inflateSyncPoint +# define inflateCopy z_inflateCopy +# define inflateReset z_inflateReset +# define inflateBack z_inflateBack +# define inflateBackEnd z_inflateBackEnd +# define compress z_compress +# define compress2 z_compress2 +# define compressBound z_compressBound +# define uncompress z_uncompress +# define adler32 z_adler32 +# define crc32 z_crc32 +# define get_crc_table z_get_crc_table +# define zError z_zError + +# define alloc_func z_alloc_func +# define free_func z_free_func +# define in_func z_in_func +# define out_func z_out_func +# define Byte z_Byte +# define uInt z_uInt +# define uLong z_uLong +# define Bytef z_Bytef +# define charf z_charf +# define intf z_intf +# define uIntf z_uIntf +# define uLongf z_uLongf +# define voidpf z_voidpf +# define voidp z_voidp +#endif + +#if defined(__MSDOS__) && !defined(MSDOS) +# define MSDOS +#endif +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) +# define OS2 +#endif +#if defined(_WINDOWS) && !defined(WINDOWS) +# define WINDOWS +#endif +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) +# ifndef WIN32 +# define WIN32 +# endif +#endif +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) +# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) +# ifndef SYS16BIT +# define SYS16BIT +# endif +# endif +#endif + +/* + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more + * than 64k bytes at a time (needed on systems with 16-bit int). + */ +#ifdef SYS16BIT +# define MAXSEG_64K +#endif +#ifdef MSDOS +# define UNALIGNED_OK +#endif + +#ifdef __STDC_VERSION__ +# ifndef STDC +# define STDC +# endif +# if __STDC_VERSION__ >= 199901L +# ifndef STDC99 +# define STDC99 +# endif +# endif +#endif +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) +# define STDC +#endif +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) +# define STDC +#endif +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) +# define STDC +#endif +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) +# define STDC +#endif + +#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ +# define STDC +#endif + +#ifndef STDC +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ +# define const /* note: need a more gentle solution here */ +# endif +#endif + +/* Some Mac compilers merge all .h files incorrectly: */ +#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) +# define NO_DUMMY_DECL +#endif + +/* Maximum value for memLevel in deflateInit2 */ +#ifndef MAX_MEM_LEVEL +# ifdef MAXSEG_64K +# define MAX_MEM_LEVEL 8 +# else +# define MAX_MEM_LEVEL 9 +# endif +#endif + +/* Maximum value for windowBits in deflateInit2 and inflateInit2. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files + * created by gzip. (Files created by minigzip can still be extracted by + * gzip.) + */ +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +/* The memory requirements for deflate are (in bytes): + (1 << (windowBits+2)) + (1 << (memLevel+9)) + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) + plus a few kilobytes for small objects. For example, if you want to reduce + the default memory requirements from 256K to 128K, compile with + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" + Of course this will generally degrade compression (there's no free lunch). + + The memory requirements for inflate are (in bytes) 1 << windowBits + that is, 32K for windowBits=15 (default value) plus a few kilobytes + for small objects. +*/ + + /* Type declarations */ + +#ifndef OF /* function prototypes */ +# ifdef STDC +# define OF(args) args +# else +# define OF(args) () +# endif +#endif + +/* The following definitions for FAR are needed only for MSDOS mixed + * model programming (small or medium model with some far allocations). + * This was tested only with MSC; for other MSDOS compilers you may have + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, + * just define FAR to be empty. + */ +#ifdef SYS16BIT +# if defined(M_I86SM) || defined(M_I86MM) + /* MSC small or medium model */ +# define SMALL_MEDIUM +# ifdef _MSC_VER +# define FAR _far +# else +# define FAR far +# endif +# endif +# if (defined(__SMALL__) || defined(__MEDIUM__)) + /* Turbo C small or medium model */ +# define SMALL_MEDIUM +# ifdef __BORLANDC__ +# define FAR _far +# else +# define FAR far +# endif +# endif +#endif + +#if defined(WINDOWS) || defined(WIN32) + /* If building or using zlib as a DLL, define ZLIB_DLL. + * This is not mandatory, but it offers a little performance increase. + */ +# ifdef ZLIB_DLL +# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) +# ifdef ZLIB_INTERNAL +# define ZEXTERN extern __declspec(dllexport) +# else +# define ZEXTERN extern __declspec(dllimport) +# endif +# endif +# endif /* ZLIB_DLL */ + /* If building or using zlib with the WINAPI/WINAPIV calling convention, + * define ZLIB_WINAPI. + * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. + */ +# ifdef ZLIB_WINAPI +# ifdef FAR +# undef FAR +# endif +# include + /* No need for _export, use ZLIB.DEF instead. */ + /* For complete Windows compatibility, use WINAPI, not __stdcall. */ +# define ZEXPORT WINAPI +# ifdef WIN32 +# define ZEXPORTVA WINAPIV +# else +# define ZEXPORTVA FAR CDECL +# endif +# endif +#endif + +#if defined (__BEOS__) +# ifdef ZLIB_DLL +# ifdef ZLIB_INTERNAL +# define ZEXPORT __declspec(dllexport) +# define ZEXPORTVA __declspec(dllexport) +# else +# define ZEXPORT __declspec(dllimport) +# define ZEXPORTVA __declspec(dllimport) +# endif +# endif +#endif + +#ifndef ZEXTERN +# define ZEXTERN extern +#endif +#ifndef ZEXPORT +# define ZEXPORT +#endif +#ifndef ZEXPORTVA +# define ZEXPORTVA +#endif + +#ifndef FAR +# define FAR +#endif + +#if !defined(__MACTYPES__) +typedef unsigned char Byte; /* 8 bits */ +#endif +typedef unsigned int uInt; /* 16 bits or more */ +typedef unsigned long uLong; /* 32 bits or more */ + +#ifdef SMALL_MEDIUM + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ +# define Bytef Byte FAR +#else + typedef Byte FAR Bytef; +#endif +typedef char FAR charf; +typedef int FAR intf; +typedef uInt FAR uIntf; +typedef uLong FAR uLongf; + +#ifdef STDC + typedef void const *voidpc; + typedef void FAR *voidpf; + typedef void *voidp; +#else + typedef Byte const *voidpc; + typedef Byte FAR *voidpf; + typedef Byte *voidp; +#endif + +#if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ +# include /* for off_t */ +# include /* for SEEK_* and off_t */ +# ifdef VMS +# include /* for off_t */ +# endif +# define z_off_t off_t +#endif +#ifndef SEEK_SET +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ +#endif +#ifndef z_off_t +# define z_off_t long +#endif + +#if defined(__OS400__) +# define NO_vsnprintf +#endif + +#if defined(__MVS__) +# define NO_vsnprintf +# ifdef FAR +# undef FAR +# endif +#endif + +/* MVS linker does not support external names larger than 8 bytes */ +#if defined(__MVS__) +# pragma map(deflateInit_,"DEIN") +# pragma map(deflateInit2_,"DEIN2") +# pragma map(deflateEnd,"DEEND") +# pragma map(deflateBound,"DEBND") +# pragma map(inflateInit_,"ININ") +# pragma map(inflateInit2_,"ININ2") +# pragma map(inflateEnd,"INEND") +# pragma map(inflateSync,"INSY") +# pragma map(inflateSetDictionary,"INSEDI") +# pragma map(compressBound,"CMBND") +# pragma map(inflate_table,"INTABL") +# pragma map(inflate_fast,"INFA") +# pragma map(inflate_copyright,"INCOPY") +#endif + +#endif /* ZCONF_H */ diff --git a/zlib/zlib.h b/zlib/zlib.h new file mode 100644 index 00000000..02281792 --- /dev/null +++ b/zlib/zlib.h @@ -0,0 +1,1357 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.3, July 18th, 2005 + + Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt + (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.3" +#define ZLIB_VERNUM 0x1230 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed + data. This version of the library supports only one compression method + (deflation) but other algorithms will be added later and will have the same + stream interface. + + Compression can be done in a single step if the buffers are large + enough (for example if an input file is mmap'ed), or can be done by + repeated calls of the compression function. In the latter case, the + application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip streams in memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never + crash even in case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total nb of input bytes read so far */ + + Bytef *next_out; /* next output byte should be put there */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total nb of bytes output so far */ + + char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text */ + uLong adler; /* adler32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has + dropped to zero. It must update next_out and avail_out when avail_out + has dropped to zero. The application must initialize zalloc, zfree and + opaque before calling the init function. All other fields are set by the + compression library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this + if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, + pointers returned by zalloc for objects of exactly 65536 bytes *must* + have their offset normalized to zero. The default allocation function + provided by this library ensures this (see zutil.c). To reduce memory + requirements and avoid any allocation of 64K objects, at the expense of + compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or + progress reports. After compression, total_in holds the total size of + the uncompressed data and may be saved for use in the decompressor + (particularly if the decompressor wants to decompress everything in + a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative + * values are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field (though see inflate()) */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is + not compatible with the zlib.h header file used by the application. + This check is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. + If zalloc and zfree are set to Z_NULL, deflateInit updates them to + use default allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at + all (the input data is simply copied a block at a time). + Z_DEFAULT_COMPRESSION requests a default compromise between speed and + compression (currently equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if level is not a valid compression level, + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). + msg is set to null if there is no error message. deflateInit does not + perform any compression: this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce some + output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary (in interactive applications). + Some output may be provided even if flush is not set. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming + more output, and updating avail_in or avail_out accordingly; avail_out + should never be zero before the call. The application can consume the + compressed output when it wants, for example when the output buffer is full + (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK + and with zero avail_out, it must be called again after making room in the + output buffer because there might be more output pending. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumualte before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In particular + avail_in is zero after the call if enough output space has been provided + before the call.) Flushing may degrade compression for some compression + algorithms and so it should be used only when necessary. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there + was enough output space; if deflate returns with Z_OK, this function must be + called again with Z_FINISH and more output space (updated avail_out) but no + more input data, until it returns with Z_STREAM_END or an error. After + deflate has returned Z_STREAM_END, the only possible operations on the + stream are deflateReset or deflateEnd. + + Z_FINISH can be used immediately after deflateInit if all the compression + is to be done in a single step. In this case, avail_out must be at least + the value returned by deflateBound (see below). If deflate does not return + Z_STREAM_END, then it must be called again as described above. + + deflate() sets strm->adler to the adler32 checksum of all input read + so far (that is, total_in bytes). + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered + binary. This field is only for information purposes and does not affect + the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible + (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not + fatal, and deflate() can be called again with more input and more output + space to continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any + pending output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, + msg may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. If next_in is not Z_NULL and avail_in is large enough (the exact + value depends on the compression method), inflateInit determines the + compression method from the zlib header and allocates all data structures + accordingly; otherwise the allocation will be deferred to the first call of + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to + use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller. msg is set to null if there is no error + message. inflateInit does not perform any decompression apart from reading + the zlib header if present: this will be done by inflate(). (So next_in and + avail_in may be modified, but next_out and avail_out are unchanged.) +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in is updated and processing + will resume at this point for the next call of inflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there + is no more input data or no more space in the output buffer (see below + about the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming + more output, and updating the next_* and avail_* values accordingly. + The application can consume the uncompressed output when it wants, for + example when the output buffer is full (avail_out == 0), or after each + call of inflate(). If inflate returns Z_OK and with zero avail_out, it + must be called again after making room in the output buffer because there + might be more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, + Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() stop + if and when it gets to the next deflate block boundary. When decoding the + zlib or gzip format, this will cause inflate() to return immediately after + the header and before the first block. When doing a raw inflate, inflate() + will go ahead and process the first block, and will return when it gets to + the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + Also to assist in this, on return inflate() will set strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 + if inflate() is currently decoding the last block in the deflate stream, + plus 128 if inflate() returned immediately after decoding an end-of-block + code or decoding the complete header up to just before the first byte of the + deflate stream. The end-of-block will not be indicated until all of the + uncompressed data from that block has been written to strm->next_out. The + number of unused bits may in general be greater than seven, except when + bit 7 of data_type is set, in which case the number of unused bits will be + less than eight. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step + (a single call of inflate), the parameter flush should be set to + Z_FINISH. In this case all pending input is processed and all pending + output is flushed; avail_out must be large enough to hold all the + uncompressed data. (The size of the uncompressed data may have been saved + by the compressor for this purpose.) The next operation on this stream must + be inflateEnd to deallocate the decompression state. The use of Z_FINISH + is never required, but can be used to inform inflate that a faster approach + may be used for the single inflate() call. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the only effect of the flush parameter in this implementation + is on the return value of inflate(), as noted below, or when it returns early + because Z_BLOCK is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the adler32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the adler32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed adler32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() will decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically. Any information + contained in the gzip header is not retained, so applications that need that + information should instead use raw inflate, see inflateInit2() below, or + inflateBack() and perform their own processing of the gzip header and + trailer. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value), Z_STREAM_ERROR if the stream structure was inconsistent (for example + if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, + Z_BUF_ERROR if no progress is possible or if there was not enough room in the + output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may then + call inflateSync() to look for a good compression block if a partial recovery + of the data is desired. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any + pending output. + + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state + was inconsistent. In the error case, msg may be set but then points to a + static string (which must not be deallocated). +*/ + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by + the caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute an adler32 check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), + no header crc, and the operating system will be set to 255 (unknown). If a + gzip stream is being written, strm->adler is a crc32 instead of an adler32. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but + is slow and reduces compression ratio; memLevel=9 uses maximum memory + for optimal speed. The default value is 8. See zconf.h for total memory + usage as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as + Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy + parameter only affects the compression ratio but not the correctness of the + compressed output even if it is not set appropriately. Z_FIXED prevents the + use of dynamic Huffman codes, allowing for a simpler decoder for special + applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid + method). msg is set to null if there is no error message. deflateInit2 does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. This function must be called + immediately after deflateInit, deflateInit2 or deflateReset, before any + call of deflate. The compressor and decompressor must use exactly the same + dictionary (see inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size in + deflate or deflate2. Thus the strings most likely to be useful should be + put at the end of the dictionary, not at the front. In addition, the + current implementation of deflate will use at most the window size minus + 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the adler32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The adler32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + adler32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (such as NULL dictionary) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if the compression method is bsort). deflateSetDictionary does not + perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and + can consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, + but does not free and reallocate all the internal compression state. + The stream will keep the same compression level and any other attributes + that may have been set by deflateInit2. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2. This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different + strategy. If the compression level is changed, the input available so far + is compressed with the old level (and may be flushed); the new level will + take effect only at the next call of deflate(). + + Before the call of deflateParams, the stream state must be set as for + a call of deflate(), since the currently available input may have to + be compressed and flushed. In particular, strm->avail_out must be non-zero. + + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR + if strm->avail_out was zero. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() + or deflateInit2(). This would be used to allocate an output buffer + for deflation in a single pass, and so would be called before deflate(). +*/ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the + bits leftover from a previous deflate stream when appending to it. As such, + this function can only be used for raw deflate, and must be used before the + first deflate() call after a deflateInit2() or deflateReset(). bits must be + less than or equal to 16, and that many of the least significant bits of + value will be inserted in the output. + + deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an adler32 or a crc32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is + a crc32 instead of an adler32. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg + is set to null if there is no error message. inflateInit2 does not perform + any decompression apart from reading the zlib header if present: this will + be done by inflate(). (So next_in and avail_in may be modified, but next_out + and avail_out are unchanged.) +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the adler32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called + immediately after inflateInit2() or inflateReset() and before any call of + inflate() to set the dictionary. The application must insure that the + dictionary that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (such as NULL dictionary) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect adler32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a full flush point (see above the + description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR + if no more input was provided, Z_DATA_ERROR if no flush point has been found, + or Z_STREAM_ERROR if the stream structure was inconsistent. In the success + case, the application may save the current current value of total_in which + indicates where valid compressed data was found. In the error case, the + application may repeatedly call inflateSync, providing more input each time, + until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate all the internal decompression state. + The stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being NULL). +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK can be used to + force inflate() to return immediately after header processing is complete + and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When + any of extra, name, or comment are not Z_NULL and the respective field is + not present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the paramaters are invalid, Z_MEM_ERROR if the internal state could not + be allocated, or Z_VERSION_ERROR if the version of the library does not + match the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is more efficient than inflate() for + file i/o applications in that it avoids copying between the output and the + sliding window by simply making the window itself the output buffer. This + function trusts the application to not change the output buffer passed by + the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free + the allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects + only the raw deflate stream to decompress. This is different from the + normal behavior of inflate(), which expects either a zlib or gzip header and + trailer around the deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero--buf is ignored in that + case--and inflateBack() will return a buffer error. inflateBack() will call + out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() + should return zero on success, or non-zero on failure. If out() returns + non-zero, inflateBack() will return with an error. Neither in() nor out() + are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format + error in the deflate stream (in which case strm->msg is set to indicate the + nature of the error), or Z_STREAM_ERROR if the stream was not properly + initialized. In the case of Z_BUF_ERROR, an input or output error can be + distinguished using strm->next_in which will be Z_NULL only if in() returned + an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to + out() returning non-zero. (in() will always be called before out(), so + strm->next_in is assured to be defined if out() returns non-zero.) Note + that inflateBack() cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + + + /* utility functions */ + +/* + The following utility functions are implemented on top of the + basic stream-oriented functions. To simplify the interface, some + default options are assumed (compression level and memory usage, + standard memory allocation functions). The source code of these + utility functions can easily be modified if you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total + size of the destination buffer, which must be at least the value returned + by compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + This function can be used to compress a whole file at once if the + input file is mmap'ed. + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before + a compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total + size of the destination buffer, which must be large enough to hold the + entire uncompressed data. (The size of the uncompressed data must have + been saved previously by the compressor and transmitted to the decompressor + by some mechanism outside the scope of this compression library.) + Upon exit, destLen is the actual size of the compressed buffer. + This function can be used to decompress a whole file at once if the + input file is mmap'ed. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. +*/ + + +typedef voidp gzFile; + +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); +/* + Opens a gzip (.gz) file for reading or writing. The mode parameter + is as in fopen ("rb" or "wb") but can also include a compression level + ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for + Huffman only compression as in "wb1h", or 'R' for run-length encoding + as in "wb1R". (See the description of deflateInit2 for more information + about the strategy parameter.) + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. + + gzopen returns NULL if the file could not be opened or if there was + insufficient memory to allocate the (de)compression state; errno + can be checked to distinguish the two cases (if errno is zero, the + zlib error is Z_MEM_ERROR). */ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen() associates a gzFile with the file descriptor fd. File + descriptors are obtained from calls like open, dup, creat, pipe or + fileno (in the file has been previously opened with fopen). + The mode parameter is as in gzopen. + The next call of gzclose on the returned gzFile will also close the + file descriptor fd, just like fclose(fdopen(fd), mode) closes the file + descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). + gzdopen returns NULL if there was insufficient memory to allocate + the (de)compression state. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not + opened for writing. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. + If the input file was not in gzip format, gzread copies the given number + of bytes into the buffer. + gzread returns the number of uncompressed bytes actually read (0 for + end of file, -1 for error). */ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes actually written + (0 in case of error). +*/ + +ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the args to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written (0 in case of error). The number of + uncompressed bytes written is limited to 4095. The caller should assure that + this limit is not exceeded. If it is exceeded, then gzprintf() will return + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or + a newline character is read and transferred to buf, or an end-of-file + condition is encountered. The string is then terminated with a null + character. + gzgets returns buf, or Z_NULL in case of error. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. + gzputc returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte + or -1 in case of end of file or error. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read again later. + Only one character of push-back is allowed. gzungetc() returns the + character pushed, or -1 on failure. gzungetc() will fail if a + character has been pushed but not read yet, or if c is -1. The pushed + character will be discarded if the stream is repositioned with gzseek() + or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter + flush is as in the deflate() function. The return value is the zlib + error number (see function gzerror below). gzflush returns Z_OK if + the flush parameter is Z_FINISH and all output could be flushed. + gzflush should be called only when strictly necessary because it can + degrade compression. +*/ + +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); +/* + Sets the starting position for the next gzread or gzwrite on the + given compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); +/* + Returns the starting position for the next gzread or gzwrite on the + given compressed file. This position represents a number of bytes in the + uncompressed data stream. + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns 1 when EOF has previously been detected reading the given + input stream, otherwise zero. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns 1 if file is being read directly without decompression, otherwise + zero. +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file + and deallocates all the (de)compression state. The return value is the zlib + error number (see function gzerror below). +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the + given compressed file. errnum is set to zlib error number. If an + error occurred in the file system and not in the compression library, + errnum is set to Z_ERRNO and the application may consult errno + to get the exact error code. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the + compression library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is NULL, this function returns + the required initial value for the checksum. + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed + much faster. Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); +/* + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is NULL, this function returns the required initial + value for the for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + +/* + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) +#define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, sizeof(z_stream)) +#define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) +#define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, sizeof(z_stream)) + + +#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) + struct internal_state {int dummy;}; /* hack for buggy compilers */ +#endif + +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); +ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/zlib/zlib.vcproj b/zlib/zlib.vcproj new file mode 100644 index 00000000..3a4d3e04 --- /dev/null +++ b/zlib/zlib.vcproj @@ -0,0 +1,239 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zlib/zlib.vcproj.7.10.old b/zlib/zlib.vcproj.7.10.old new file mode 100644 index 00000000..9ae9e0c9 --- /dev/null +++ b/zlib/zlib.vcproj.7.10.old @@ -0,0 +1,167 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zlib/zutil.c b/zlib/zutil.c new file mode 100644 index 00000000..d55f5948 --- /dev/null +++ b/zlib/zutil.c @@ -0,0 +1,318 @@ +/* zutil.c -- target dependent utility functions for the compression library + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#include "zutil.h" + +#ifndef NO_DUMMY_DECL +struct internal_state {int dummy;}; /* for buggy compilers */ +#endif + +const char * const z_errmsg[10] = { +"need dictionary", /* Z_NEED_DICT 2 */ +"stream end", /* Z_STREAM_END 1 */ +"", /* Z_OK 0 */ +"file error", /* Z_ERRNO (-1) */ +"stream error", /* Z_STREAM_ERROR (-2) */ +"data error", /* Z_DATA_ERROR (-3) */ +"insufficient memory", /* Z_MEM_ERROR (-4) */ +"buffer error", /* Z_BUF_ERROR (-5) */ +"incompatible version",/* Z_VERSION_ERROR (-6) */ +""}; + + +const char * ZEXPORT zlibVersion() +{ + return ZLIB_VERSION; +} + +uLong ZEXPORT zlibCompileFlags() +{ + uLong flags; + + flags = 0; + switch (sizeof(uInt)) { + case 2: break; + case 4: flags += 1; break; + case 8: flags += 2; break; + default: flags += 3; + } + switch (sizeof(uLong)) { + case 2: break; + case 4: flags += 1 << 2; break; + case 8: flags += 2 << 2; break; + default: flags += 3 << 2; + } + switch (sizeof(voidpf)) { + case 2: break; + case 4: flags += 1 << 4; break; + case 8: flags += 2 << 4; break; + default: flags += 3 << 4; + } + switch (sizeof(z_off_t)) { + case 2: break; + case 4: flags += 1 << 6; break; + case 8: flags += 2 << 6; break; + default: flags += 3 << 6; + } +#ifdef DEBUG + flags += 1 << 8; +#endif +#if defined(ASMV) || defined(ASMINF) + flags += 1 << 9; +#endif +#ifdef ZLIB_WINAPI + flags += 1 << 10; +#endif +#ifdef BUILDFIXED + flags += 1 << 12; +#endif +#ifdef DYNAMIC_CRC_TABLE + flags += 1 << 13; +#endif +#ifdef NO_GZCOMPRESS + flags += 1L << 16; +#endif +#ifdef NO_GZIP + flags += 1L << 17; +#endif +#ifdef PKZIP_BUG_WORKAROUND + flags += 1L << 20; +#endif +#ifdef FASTEST + flags += 1L << 21; +#endif +#ifdef STDC +# ifdef NO_vsnprintf + flags += 1L << 25; +# ifdef HAS_vsprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_vsnprintf_void + flags += 1L << 26; +# endif +# endif +#else + flags += 1L << 24; +# ifdef NO_snprintf + flags += 1L << 25; +# ifdef HAS_sprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_snprintf_void + flags += 1L << 26; +# endif +# endif +#endif + return flags; +} + +#ifdef DEBUG + +# ifndef verbose +# define verbose 0 +# endif +int z_verbose = verbose; + +void z_error (m) + char *m; +{ + fprintf(stderr, "%s\n", m); + exit(1); +} +#endif + +/* exported to allow conversion of error code to string for compress() and + * uncompress() + */ +const char * ZEXPORT zError(err) + int err; +{ + return ERR_MSG(err); +} + +#if defined(_WIN32_WCE) + /* The Microsoft C Run-Time Library for Windows CE doesn't have + * errno. We define it as a global variable to simplify porting. + * Its value is always 0 and should not be used. + */ + int errno = 0; +#endif + +#ifndef HAVE_MEMCPY + +void zmemcpy(dest, source, len) + Bytef* dest; + const Bytef* source; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = *source++; /* ??? to be unrolled */ + } while (--len != 0); +} + +int zmemcmp(s1, s2, len) + const Bytef* s1; + const Bytef* s2; + uInt len; +{ + uInt j; + + for (j = 0; j < len; j++) { + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; + } + return 0; +} + +void zmemzero(dest, len) + Bytef* dest; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = 0; /* ??? to be unrolled */ + } while (--len != 0); +} +#endif + + +#ifdef SYS16BIT + +#ifdef __TURBOC__ +/* Turbo C in 16-bit mode */ + +# define MY_ZCALLOC + +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes + * and farmalloc(64K) returns a pointer with an offset of 8, so we + * must fix the pointer. Warning: the pointer must be put back to its + * original form in order to free it, use zcfree(). + */ + +#define MAX_PTR 10 +/* 10*64K = 640K */ + +local int next_ptr = 0; + +typedef struct ptr_table_s { + voidpf org_ptr; + voidpf new_ptr; +} ptr_table; + +local ptr_table table[MAX_PTR]; +/* This table is used to remember the original form of pointers + * to large buffers (64K). Such pointers are normalized with a zero offset. + * Since MSDOS is not a preemptive multitasking OS, this table is not + * protected from concurrent access. This hack doesn't work anyway on + * a protected system like OS/2. Use Microsoft C instead. + */ + +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) +{ + voidpf buf = opaque; /* just to make some compilers happy */ + ulg bsize = (ulg)items*size; + + /* If we allocate less than 65520 bytes, we assume that farmalloc + * will return a usable pointer which doesn't have to be normalized. + */ + if (bsize < 65520L) { + buf = farmalloc(bsize); + if (*(ush*)&buf != 0) return buf; + } else { + buf = farmalloc(bsize + 16L); + } + if (buf == NULL || next_ptr >= MAX_PTR) return NULL; + table[next_ptr].org_ptr = buf; + + /* Normalize the pointer to seg:0 */ + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; + *(ush*)&buf = 0; + table[next_ptr++].new_ptr = buf; + return buf; +} + +void zcfree (voidpf opaque, voidpf ptr) +{ + int n; + if (*(ush*)&ptr != 0) { /* object < 64K */ + farfree(ptr); + return; + } + /* Find the original pointer */ + for (n = 0; n < next_ptr; n++) { + if (ptr != table[n].new_ptr) continue; + + farfree(table[n].org_ptr); + while (++n < next_ptr) { + table[n-1] = table[n]; + } + next_ptr--; + return; + } + ptr = opaque; /* just to make some compilers happy */ + Assert(0, "zcfree: ptr not found"); +} + +#endif /* __TURBOC__ */ + + +#ifdef M_I86 +/* Microsoft C in 16-bit mode */ + +# define MY_ZCALLOC + +#if (!defined(_MSC_VER) || (_MSC_VER <= 600)) +# define _halloc halloc +# define _hfree hfree +#endif + +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) +{ + if (opaque) opaque = 0; /* to make compiler happy */ + return _halloc((long)items, size); +} + +void zcfree (voidpf opaque, voidpf ptr) +{ + if (opaque) opaque = 0; /* to make compiler happy */ + _hfree(ptr); +} + +#endif /* M_I86 */ + +#endif /* SYS16BIT */ + + +#ifndef MY_ZCALLOC /* Any system without a special alloc function */ + +#ifndef STDC +extern voidp malloc OF((uInt size)); +extern voidp calloc OF((uInt items, uInt size)); +extern void free OF((voidpf ptr)); +#endif + +voidpf zcalloc (opaque, items, size) + voidpf opaque; + unsigned items; + unsigned size; +{ + if (opaque) items += size - size; /* make compiler happy */ + return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : + (voidpf)calloc(items, size); +} + +void zcfree (opaque, ptr) + voidpf opaque; + voidpf ptr; +{ + free(ptr); + if (opaque) return; /* make compiler happy */ +} + +#endif /* MY_ZCALLOC */ diff --git a/zlib/zutil.h b/zlib/zutil.h new file mode 100644 index 00000000..b7d5eff8 --- /dev/null +++ b/zlib/zutil.h @@ -0,0 +1,269 @@ +/* zutil.h -- internal interface and configuration of the compression library + * Copyright (C) 1995-2005 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef ZUTIL_H +#define ZUTIL_H + +#define ZLIB_INTERNAL +#include "zlib.h" + +#ifdef STDC +# ifndef _WIN32_WCE +# include +# endif +# include +# include +#endif +#ifdef NO_ERRNO_H +# ifdef _WIN32_WCE + /* The Microsoft C Run-Time Library for Windows CE doesn't have + * errno. We define it as a global variable to simplify porting. + * Its value is always 0 and should not be used. We rename it to + * avoid conflict with other libraries that use the same workaround. + */ +# define errno z_errno +# endif + extern int errno; +#else +# ifndef _WIN32_WCE +# include +# endif +#endif + +#ifndef local +# define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + +typedef unsigned char uch; +typedef uch FAR uchf; +typedef unsigned short ush; +typedef ush FAR ushf; +typedef unsigned long ulg; + +extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ +/* (size given to avoid silly warnings with Visual C++) */ + +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] + +#define ERR_RETURN(strm,err) \ + return (strm->msg = (char*)ERR_MSG(err), (err)) +/* To be used only when the state is known to be valid */ + + /* common constants */ + +#ifndef DEF_WBITS +# define DEF_WBITS MAX_WBITS +#endif +/* default windowBits for decompression. MAX_WBITS is for compression only */ + +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif +/* default memLevel */ + +#define STORED_BLOCK 0 +#define STATIC_TREES 1 +#define DYN_TREES 2 +/* The three kinds of block type */ + +#define MIN_MATCH 3 +#define MAX_MATCH 258 +/* The minimum and maximum match lengths */ + +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ + + /* target dependencies */ + +#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) +# define OS_CODE 0x00 +# if defined(__TURBOC__) || defined(__BORLANDC__) +# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) + /* Allow compilation with ANSI keywords only enabled */ + void _Cdecl farfree( void *block ); + void *_Cdecl farmalloc( unsigned long nbytes ); +# else +# include +# endif +# else /* MSC or DJGPP */ +# include +# endif +#endif + +#ifdef AMIGA +# define OS_CODE 0x01 +#endif + +#if defined(VAXC) || defined(VMS) +# define OS_CODE 0x02 +# define F_OPEN(name, mode) \ + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") +#endif + +#if defined(ATARI) || defined(atarist) +# define OS_CODE 0x05 +#endif + +#ifdef OS2 +# define OS_CODE 0x06 +# ifdef M_I86 + #include +# endif +#endif + +#if defined(MACOS) || defined(TARGET_OS_MAC) +# define OS_CODE 0x07 +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os +# include /* for fdopen */ +# else +# ifndef fdopen +# define fdopen(fd,mode) NULL /* No fdopen() */ +# endif +# endif +#endif + +#ifdef TOPS20 +# define OS_CODE 0x0a +#endif + +#ifdef WIN32 +# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ +# define OS_CODE 0x0b +# endif +#endif + +#ifdef __50SERIES /* Prime/PRIMOS */ +# define OS_CODE 0x0f +#endif + +#if defined(_BEOS_) || defined(RISCOS) +# define fdopen(fd,mode) NULL /* No fdopen() */ +#endif + +#if (defined(_MSC_VER) && (_MSC_VER > 600)) +# if defined(_WIN32_WCE) +# define fdopen(fd,mode) NULL /* No fdopen() */ +# ifndef _PTRDIFF_T_DEFINED + typedef int ptrdiff_t; +# define _PTRDIFF_T_DEFINED +# endif +# else +# define fdopen(fd,type) _fdopen(fd,type) +# endif +#endif + + /* common defaults */ + +#ifndef OS_CODE +# define OS_CODE 0x03 /* assume Unix */ +#endif + +#ifndef F_OPEN +# define F_OPEN(name, mode) fopen((name), (mode)) +#endif + + /* functions */ + +#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif +#if defined(__CYGWIN__) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif +#ifndef HAVE_VSNPRINTF +# ifdef MSDOS + /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), + but for now we just assume it doesn't. */ +# define NO_vsnprintf +# endif +# ifdef __TURBOC__ +# define NO_vsnprintf +# endif +# ifdef WIN32 + /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ +# if !defined(vsnprintf) && !defined(NO_vsnprintf) +# define vsnprintf _vsnprintf +# endif +# endif +# ifdef __SASC +# define NO_vsnprintf +# endif +#endif +#ifdef VMS +# define NO_vsnprintf +#endif + +#if defined(pyr) +# define NO_MEMCPY +#endif +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) + /* Use our own functions for small and medium model with MSC <= 5.0. + * You may have to use the same strategy for Borland C (untested). + * The __SC__ check is for Symantec. + */ +# define NO_MEMCPY +#endif +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) +# define HAVE_MEMCPY +#endif +#ifdef HAVE_MEMCPY +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */ +# define zmemcpy _fmemcpy +# define zmemcmp _fmemcmp +# define zmemzero(dest, len) _fmemset(dest, 0, len) +# else +# define zmemcpy memcpy +# define zmemcmp memcmp +# define zmemzero(dest, len) memset(dest, 0, len) +# endif +#else + extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); + extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); + extern void zmemzero OF((Bytef* dest, uInt len)); +#endif + +/* Diagnostic functions */ +#ifdef DEBUG +# include + extern int z_verbose; + extern void z_error OF((char *m)); +# define Assert(cond,msg) {if(!(cond)) z_error(msg);} +# define Trace(x) {if (z_verbose>=0) fprintf x ;} +# define Tracev(x) {if (z_verbose>0) fprintf x ;} +# define Tracevv(x) {if (z_verbose>1) fprintf x ;} +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} +#else +# define Assert(cond,msg) +# define Trace(x) +# define Tracev(x) +# define Tracevv(x) +# define Tracec(c,x) +# define Tracecv(c,x) +#endif + + +voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); +void zcfree OF((voidpf opaque, voidpf ptr)); + +#define ZALLOC(strm, items, size) \ + (*((strm)->zalloc))((strm)->opaque, (items), (size)) +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} + +#endif /* ZUTIL_H */