diff --git a/CMakeLists.txt b/CMakeLists.txt index fa841c8..99b8025 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,16 @@ # $HeadURL$ # $Revision$ +cmake_minimum_required (VERSION 3.6) + +set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) +set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +add_subdirectory("external/glfw") +find_package(OpenGL REQUIRED) + +include_directories(${OPENGL_INCLUDE_DIR}) -cmake_minimum_required (VERSION 2.6) project (TINES) add_subdirectory (src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 23662c1..d929cd7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,15 +2,13 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ # $HeadURL$ # $Revision$ -include_directories($(TINES_SOURCE_DIR)/include) - ########################## # Configurations variables ########################## @@ -22,17 +20,7 @@ set(DETECT_BUS_CONFLICT OFF CACHE BOOL "Activate the bus conflit detector? (Coul set(USE_EFENCE OFF CACHE BOOL "Use electricfence memory debugger?") set(USE_PROFILING OFF CACHE BOOL "Use profiling tools? (Will slow down a lot.)") -set(USE_ALLEGRO ON CACHE BOOL "Use Allegro backend" FORCE) - -IF(NOT CMAKE_BUILD_TYPE) - SET(CMAKE_BUILD_TYPE Debug CACHE STRING - "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." - FORCE) -ENDIF(NOT CMAKE_BUILD_TYPE) - -if (APPLE) - SET (CMAKE_FIND_FRAMEWORK LAST) -endif (APPLE) +set(USE_ALLEGRO ON CACHE BOOL "Use Allegro backend") ########################## # Link & Compile flags @@ -41,9 +29,7 @@ endif (APPLE) set (CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused-parameter -Werror ${PLATFORM_FLAGS}") set (CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-unused-parameter -Werror ${PLATFORM_FLAGS}") -add_definitions (-DNO_DECIMAL -DFAST_RDOP) - -SET ( CMAKE_EXE_LINKER_FLAGS "-mmacosx-version-min=10.4") +add_definitions (-DNO_DECIMAL) if (PPU_ISPAL) add_definitions (-DISPAL) @@ -63,15 +49,6 @@ if (DETECT_BUS_CONFLICT) add_definitions (-DDETECT_BUS_CONFLICT) endif (DETECT_BUS_CONFLICT) -if (USE_EFENCE) - if (CMAKE_BUILD_TYPE MATCHES Release) - SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Debug info is forced" FORCE) - else(CMAKE_BUILD_TYPE) - SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Debug info is forced" FORCE) - endif(CMAKE_BUILD_TYPE) - -endif (USE_EFENCE) - if (USE_PROFILING) if (CMAKE_BUILD_TYPE MATCHES Rel) SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Debug info is forced" FORCE) @@ -82,18 +59,7 @@ if (USE_PROFILING) set(CMAKE_C_FLAGS -pg) endif (USE_PROFILING) -if (APPLE) - include_directories(BEFORE /usr/include) -endif (APPLE) - -#if the CPU is LSB set the define -if (CMAKE_SYSTEM_PROCESSOR MATCHES i386 OR CMAKE_SYSTEM_PROCESSOR MATCHES [aA][rR][mM]) - add_definitions (-DLSB_FIRST) -endif (CMAKE_SYSTEM_PROCESSOR MATCHES i386 OR CMAKE_SYSTEM_PROCESSOR MATCHES [aA][rR][mM]) - -#Add release mode extra C Flags -set (CMAKE_C_FLAGS_RELEASE "-fomit-frame-pointer -funroll-loops -Wall ${CMAKE_C_FLAGS_RELEASE}") -set (CMAKE_C_FLAGS_RELWITHDEBINFO "-fomit-frame-pointer -funroll-loops -Wall ${CMAKE_C_FLAGS_RELWITHDEBINFO}") +include_directories(include) add_subdirectory(apu) add_subdirectory(corecpu) @@ -105,36 +71,13 @@ add_subdirectory(ppu) if (TARGET_TI68k) add_subdirectory(os/ti68k) -elseif (APPLE AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - add_subdirectory(os/macos) -elseif (UNIX) - add_subdirectory(os/unix) -else (TARGET_TI68k) - #So we target UNIX like OS +elseif (WIN32) add_subdirectory(os/win32) +else (TARGET_TI68k) + add_subdirectory(os/unix) endif (TARGET_TI68k) - -add_library (main main.c paddle.c NESCarts.c) - -add_executable(tines main.c) -set(CMAKE_FIND_FRAMEWORK LAST) -find_library(ALLEGROLIB allegro) find_library(PTHREADLIB pthread) - -if (USE_EFENCE) - find_library(EFENCELIB efence) - target_link_libraries(tines ${EFENCELIB}) -endif (USE_EFENCE) -if (USE_ALLEGRO) - target_link_libraries(tines debug alld-main) - # target_link_libraries(tines) - if (APPLE) - find_library(COCOALIB Cocoa) - target_link_libraries(tines ${COCOALIB}) - endif (APPLE) - -endif (USE_ALLEGRO) - -target_link_libraries(tines main apu corecpu mappermanager memorymanager pluginsmanager ppu oslib ${ALLEGROLIB} ${PTHREADLIB}) +add_executable(tines main.c paddle.c NESCarts.c) +target_link_libraries(tines apu corecpu mappermanager memorymanager pluginsmanager ppu oslib ${PTHREADLIB}) \ No newline at end of file diff --git a/src/NESCarts.c b/src/NESCarts.c index ddccc81..90ca8c2 100755 --- a/src/NESCarts.c +++ b/src/NESCarts.c @@ -3,7 +3,7 @@ * NESCart.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/apu/CMakeLists.txt b/src/apu/CMakeLists.txt index 7cc1994..412d4cd 100644 --- a/src/apu/CMakeLists.txt +++ b/src/apu/CMakeLists.txt @@ -2,7 +2,7 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ diff --git a/src/apu/SndUnixT.c b/src/apu/SndUnixT.c index 7a26af0..e9280d0 100755 --- a/src/apu/SndUnixT.c +++ b/src/apu/SndUnixT.c @@ -10,7 +10,7 @@ /** commercially. Please, notify me, if you make any **/ /** changes to this file. **/ /*************************************************************/ -#include "Sound.h" +//#include "Sound.h" #include #include @@ -20,7 +20,7 @@ #include #include - +#if 0 #ifdef SUN_AUDIO #include @@ -553,3 +553,4 @@ void UnixDrum(int Type,int Force) { /* This function is currently empty */ } +#endif \ No newline at end of file diff --git a/src/include/NESCarts.h b/src/include/NESCarts.h index cff32a7..afee5d2 100755 --- a/src/include/NESCarts.h +++ b/src/include/NESCarts.h @@ -3,7 +3,7 @@ * NESCart.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/include/color.h b/src/include/color.h index 264d05c..bcbabad 100644 --- a/src/include/color.h +++ b/src/include/color.h @@ -1,9 +1,9 @@ /** - * ANSI Color definitiont - The Quick6502 Project + * ANSI Color definition - The Quick6502 Project * include/color.h * * Created by Manoel Trapier on 25/06/10 - * Copyright 2010 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate:$ * $Author:$ diff --git a/src/include/corecpu.h b/src/include/corecpu.h index bf90c9d..a220ccb 100644 --- a/src/include/corecpu.h +++ b/src/include/corecpu.h @@ -3,7 +3,7 @@ * corecpu.h * * Created by Manoel Trapier on 24/02/08 - * Copyright 2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/include/log.h b/src/include/log.h index c966744..cb018ad 100644 --- a/src/include/log.h +++ b/src/include/log.h @@ -3,7 +3,7 @@ * include/log.h * * Created by Manoel Trapier on 19/05/10 - * Copyright 2010 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate:$ * $Author:$ diff --git a/src/include/os_dependent.h b/src/include/os_dependent.h index d1ba00b..10df2af 100644 --- a/src/include/os_dependent.h +++ b/src/include/os_dependent.h @@ -3,7 +3,7 @@ * os_dependent.h * * Created by Manoel TRAPIER on 08/05/08. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -15,11 +15,21 @@ #ifndef OS_DEPENDENT_H #define OS_DEPENDENT_H +#include + /* File related functions */ /* Graphics related functions */ int graphics_init(); int graphics_drawpixel(long x, long y, long color); int graphics_blit(long x, long y, long w, long h); +int graphics_drawline(long x, long y, long x1, long y1, long color); + +typedef struct Palette_t +{ + uint8_t r,g,b,a; +} Palette; + +int getKeyStatus(int key); /* Sound related functions */ @@ -37,7 +47,6 @@ typedef enum ConsoleLevel_t Console_Debug, } ConsoleLevel; - int console_init(ConsoleLevel DefaultLevel); int console_printf(const ConsoleLevel level, const char *format, ...); int console_printf_d(const char *format, ...); diff --git a/src/include/paddle.h b/src/include/paddle.h index 3974b04..09b6e24 100755 --- a/src/include/paddle.h +++ b/src/include/paddle.h @@ -3,7 +3,7 @@ * paddle.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/include/palette.h b/src/include/palette.h index 53e4661..9dea0ef 100644 --- a/src/include/palette.h +++ b/src/include/palette.h @@ -6,7 +6,7 @@ * $Revision$ */ -PALETTE basicPalette = { +Palette basicPalette[] = { { 0x1E, 0x1E, 0x1E, 0x07 }, { 0x03, 0x09, 0x28, 0xB7 }, { 0x0A, 0x04, 0x2B, 0x0D }, diff --git a/src/include/ppu/ppu.h b/src/include/ppu/ppu.h index b41844f..675da5c 100755 --- a/src/include/ppu/ppu.h +++ b/src/include/ppu/ppu.h @@ -5,7 +5,7 @@ * Define and emulate the PPU (Picture Processing Unit) of the real NES * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -21,10 +21,10 @@ typedef struct PPU_Sprite_ { - byte y; - byte tileid; - byte flags; - byte x; + uint8_t y; + uint8_t tileid; + uint8_t flags; + uint8_t x; } PPU_Sprite; /* @@ -32,13 +32,13 @@ PPU must be initialized after memory initialisation.. */ int ppu_init(); -int ppu_hblank(int scanline); +int ppu_hblank(uint16_t scanline); -byte ppu_readReg(byte id); +uint8_t ppu_readReg(uint8_t id); -void ppu_writeReg(byte id, byte val); +void ppu_writeReg(uint8_t id, uint8_t val); -void ppu_fillSprRamDMA(byte value); +void ppu_fillSprRamDMA(uint8_t value); #define PPU_MIRROR_HORIZTAL 0 #define PPU_MIRROR_VERTICAL 1 @@ -52,15 +52,15 @@ void ppu_fillSprRamDMA(byte value); #define PPU_SCMODE_NORMAL 1 #define PPU_SCMODE_FOURSC 2 -void ppu_setMirroring(byte direction); -void ppu_setSingleScreen(byte screen); -void ppu_setScreenMode(byte mode); +void ppu_setMirroring(uint8_t direction); +void ppu_setSingleScreen(uint8_t screen); +void ppu_setScreenMode(uint8_t mode); -PPU_Sprite ppu_getSprite(unsigned short i); +PPU_Sprite ppu_getSprite(uint16_t i); -unsigned char ppu_memoryRead(byte page, byte addr); -void ppu_memoryWrite(byte page, byte addr, byte value); +unsigned char ppu_memoryRead(uint8_t page, uint8_t addr); +void ppu_memoryWrite(uint8_t page, uint8_t addr, uint8_t value); void ppu_debugSprites(); void ppu_debugColor(); diff --git a/src/include/types.h b/src/include/types.h index 24aa8fe..8dfa7b5 100755 --- a/src/include/types.h +++ b/src/include/types.h @@ -3,7 +3,7 @@ * types.h - Taken from the Quick6502 project * * Created by Manoel Trapier on 18/09/06. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -15,12 +15,14 @@ #ifndef TYPES_H #define TYPES_H +#include + #ifndef BYTE_TYPE_DEFINED #define BYTE_TYPE_DEFINED -typedef unsigned char byte; +typedef uint8_t byte; #endif -typedef unsigned char bool; +typedef uint8_t bool; #define true (0) #define false (!true) diff --git a/src/main.c b/src/main.c index 277b754..3cb0e30 100755 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ * main.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2012 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -20,21 +20,15 @@ #include #include #include +#include +#include #include #include #include #include -/* Allegro includes */ -#ifdef __APPLE__ -#define USE_CONSOLE -#include -#else -#define USE_CONSOLE -#include -#endif - +#include #else @@ -61,23 +55,11 @@ #include #endif -#include - -/* PAL support is broken, so force NTSC mode */ -#if ISPAL || ISNTSC -#undef ISPAL -#undef ISNTSC -#endif -#define ISNTSC 1 - #if ISPAL && !ISNTSC - int VBLANK_TIME = 70; -int HBLANK_TIME = 140; +int HBLANK_TIME = 103; double APU_BASEFREQ = 1.7734474; - #elif !ISPAL && ISNTSC - int VBLANK_TIME = 20; int HBLANK_TIME = 113; double APU_BASEFREQ = 1.7897725; @@ -91,13 +73,16 @@ double APU_BASEFREQ = 1.7897725; /* TI-NESulator Version */ #define V_MAJOR 0 -#define V_MINOR 40 +#define V_MINOR 70 + +#ifdef USE_SOUND +#undef USE_SOUND +#endif /* SVN specific values */ -#define VS_ID "$Id$" + #define VS_REVISION "$Revision$" #define VS_LASTCHANGEDDATE "$LastChangedDate$" -#define VS_HEADURL "$HeadURL$" #define VS_AUTHOR "$Author$" /* @@ -113,10 +98,6 @@ NesCart *Cart; byte *FDSRom; byte *FDSRam; -/* Allegro main screen */ -BITMAP *Buffer; - - /* Command line options */ byte START_DEBUG = 0; byte START_WITH_FDS = 0; @@ -139,8 +120,6 @@ struct timeval timeEnd; volatile unsigned long FPS, IPS; -PALETTE pal; - short IRQScanHit = -1; short SZHit = -1; @@ -166,15 +145,6 @@ void CloseHook(void) WantClosing = 1; } -void ips_fps_counter(void) -{ - FPS = frame; - IPS = ccount; - frame = 0; - ccount = 0; -} -END_OF_FUNCTION(ips_fps_counter); - void SaveSaveRam(char *name) { FILE *fp; @@ -216,7 +186,7 @@ void LoadSaveRam(char *name) } -void LoadPalette(char *filename, PALETTE pal) +void LoadPalette(char *filename, Palette *pal) { FILE *fp; int ret; @@ -380,6 +350,7 @@ byte Page40[256]; void WrHook4000Multiplexer(byte addr, byte value) { +#ifdef USE_SOUND static byte SQ1V = 0; static byte SQ2V = 0; static byte NOIV = 0; @@ -400,6 +371,8 @@ void WrHook4000Multiplexer(byte addr, byte value) static byte Sq2_reg3 = 0; double SQ = 0.0; +#endif + switch(addr) { #ifdef USE_SOUND @@ -655,14 +628,9 @@ int main(int argc, char *argv[]) /* Print the banner */ console_printf(Console_Default, "--------------------------------------------------------------------------------\n" "Welcome to TI-NESulator v%d.%d - by Godzil\n" - "Copyright 2003-2012 TRAPIER Manoel (godzil@godzil.net)\n" - "%s\n%s\n%s\n" + "Copyright 2003-2016 TRAPIER Manoel (godzil@godzil.net)\n" "--------------------------------------------------------------------------------\n\n", - V_MAJOR, - V_MINOR, - VS_REVISION, - VS_LASTCHANGEDDATE, - VS_AUTHOR); + V_MAJOR, V_MINOR); console_printf(Console_Default, "Install signal handlers...\t["); signal(SIGABRT, signalhandler); @@ -928,23 +896,7 @@ int main(int argc, char *argv[]) unmap_sram(); InitPaddle(&P1); - - console_printf(Console_Default, "Initializing Allegro...\t\t"); - allegro_init(); - install_timer(); - install_keyboard(); - console_printf(Console_Default, "[ OK ]\n"); - console_printf(Console_Default, "Set graphic mode...\t\t"); - set_color_depth(8); - set_gfx_mode(GFX_AUTODETECT_WINDOWED, 512 + 256, 480, 512 + 256, 480); - Buffer = create_bitmap(512 + 256, 480); - clear_to_color(Buffer, 0x0D); - - //set_close_button_callback(CloseHook); - //set_window_title("TI-NESulator"); - - console_printf(Console_Default, "[ OK ]\n"); - + console_printf(Console_Default, "Init PPU...\n"); if (ppu_init() != 0) @@ -970,16 +922,8 @@ int main(int argc, char *argv[]) return -1; console_printf(Console_Default, "[ OK ]\n"); - if (PALETTE_FILENAME == NULL) - { - set_palette(basicPalette); - } - else - { - LoadPalette(PALETTE_FILENAME, pal); - set_palette(pal); - } - +// set_palette(basicPalette); + #ifdef USE_SOUND InitSound(44400,!0); @@ -990,7 +934,7 @@ int main(int argc, char *argv[]) #endif // Actually no real debugguer... //console_printf(Console_Default, "Press ESC to pause emulation and jump to debugguer\n"); - install_int(ips_fps_counter, 1000); + ScanLine = 0; /* Initialize the CPU */ @@ -1027,7 +971,6 @@ int main(int argc, char *argv[]) } return 0; } -END_OF_MAIN() /* Access directly to Memory pages *HACKISH* */ extern byte *memory_pages[0xFF]; @@ -1079,7 +1022,7 @@ void MemoryPageZeroWrite (unsigned short Addr, byte Value) void Loop6502(quick6502_cpu *R) { byte ret; - short skey; +// short skey; long WaitTime; static long delta=0; @@ -1122,7 +1065,7 @@ void Loop6502(quick6502_cpu *R) #endif /* If we press Page Up, we want to accelerate "time" */ - if ((!key[KEY_PGUP]) && (!key[KEY_Y])) + if (!getKeyStatus('Y')) if ((WaitTime >= 0) && (WaitTime < 100000)) usleep(WaitTime); @@ -1148,13 +1091,12 @@ void Loop6502(quick6502_cpu *R) ScanLine++; //console_printf(Console_Default, "SL:%d HBT:%d VbT:%d\n", ScanLine, HBLANK_TIME, VBLANK_TIME); - if (keypressed()) - { - skey = (readkey() & 0xFF); + // TODO: NO DEBUGER -/* if (skey == 27) - R->Trace = 1;*/ - + if (getKeyStatus(GLFW_KEY_ESCAPE)) + exit(0); + +#if 0 if (skey == '9') { VBLANK_TIME += 2; @@ -1178,17 +1120,16 @@ void Loop6502(quick6502_cpu *R) HBLANK_TIME -= 1; console_printf(Console_Default, "HBLT: %d\n", HBLANK_TIME); } - - if ((skey == 'r') || (skey == 'R')) +#endif + + if (getKeyStatus('r') || getKeyStatus('R')) { /* Force the PPU to stop NMIs */ MemoryWrite(0x2000, 0x00); quick6502_reset(R); } - plugin_keypress(skey); - - } +// plugin_keypress(skey); if (ret != 0) quick6502_int(R, ret); diff --git a/src/mappersmanager/CMakeLists.txt b/src/mappersmanager/CMakeLists.txt index cb0a6df..da1e1b5 100644 --- a/src/mappersmanager/CMakeLists.txt +++ b/src/mappersmanager/CMakeLists.txt @@ -2,7 +2,7 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ diff --git a/src/mappersmanager/manager.c b/src/mappersmanager/manager.c index 7b006ce..ea2e840 100644 --- a/src/mappersmanager/manager.c +++ b/src/mappersmanager/manager.c @@ -3,7 +3,7 @@ * manager.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/aorom.c b/src/mappersmanager/mappers/aorom.c index 761b324..5da449a 100755 --- a/src/mappersmanager/mappers/aorom.c +++ b/src/mappersmanager/mappers/aorom.c @@ -3,7 +3,7 @@ * aorom.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -55,7 +55,7 @@ void aorom_MapperWriteHook(register byte Addr, register byte Value) aorom_load_bank = BankNb; - console_printf(Console_Default, "aorom: Asking bank %d - NT is 0x%04X\n",BankNb,(Value&0x10)?0x2400:0x2000); + //console_printf(Console_Default, "aorom: Asking bank %d - NT is 0x%04X\n",BankNb,(Value&0x10)?0x2400:0x2000); set_prom_bank_32k(0x8000,BankNb); } diff --git a/src/mappersmanager/mappers/aorom.h b/src/mappersmanager/mappers/aorom.h index 272c46b..5e7d35f 100755 --- a/src/mappersmanager/mappers/aorom.h +++ b/src/mappersmanager/mappers/aorom.h @@ -3,7 +3,7 @@ * aorom.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/cnrom.c b/src/mappersmanager/mappers/cnrom.c index a92a2a1..8ed1cf2 100755 --- a/src/mappersmanager/mappers/cnrom.c +++ b/src/mappersmanager/mappers/cnrom.c @@ -3,7 +3,7 @@ * cnrom.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/cnrom.h b/src/mappersmanager/mappers/cnrom.h index baa5e50..04ae052 100755 --- a/src/mappersmanager/mappers/cnrom.h +++ b/src/mappersmanager/mappers/cnrom.h @@ -3,7 +3,7 @@ * cnrom.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/iremh3001.c b/src/mappersmanager/mappers/iremh3001.c index df7f4f2..6a5ee95 100755 --- a/src/mappersmanager/mappers/iremh3001.c +++ b/src/mappersmanager/mappers/iremh3001.c @@ -3,7 +3,7 @@ * iremh3001.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/iremh3001.h b/src/mappersmanager/mappers/iremh3001.h index 887ee4d..56cf4cd 100755 --- a/src/mappersmanager/mappers/iremh3001.h +++ b/src/mappersmanager/mappers/iremh3001.h @@ -3,7 +3,7 @@ * iremh3001.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc1.c b/src/mappersmanager/mappers/mmc1.c index 7ca0dbb..a8ec282 100755 --- a/src/mappersmanager/mappers/mmc1.c +++ b/src/mappersmanager/mappers/mmc1.c @@ -3,7 +3,7 @@ * mmc1.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc1.h b/src/mappersmanager/mappers/mmc1.h index e83a15b..e7a7112 100644 --- a/src/mappersmanager/mappers/mmc1.h +++ b/src/mappersmanager/mappers/mmc1.h @@ -3,7 +3,7 @@ * mmc1.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc3.c b/src/mappersmanager/mappers/mmc3.c index 80a6ecf..c108794 100755 --- a/src/mappersmanager/mappers/mmc3.c +++ b/src/mappersmanager/mappers/mmc3.c @@ -3,7 +3,7 @@ * mmc3.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc3.h b/src/mappersmanager/mappers/mmc3.h index 69409bb..fb64116 100755 --- a/src/mappersmanager/mappers/mmc3.h +++ b/src/mappersmanager/mappers/mmc3.h @@ -3,7 +3,7 @@ * mmc3.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc4.c b/src/mappersmanager/mappers/mmc4.c index 1170aa6..3fcfaf6 100644 --- a/src/mappersmanager/mappers/mmc4.c +++ b/src/mappersmanager/mappers/mmc4.c @@ -3,7 +3,7 @@ * mmc4.h * * Created by Manoel TRAPIER. - * Copyright (c) 2007-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/mmc4.h b/src/mappersmanager/mappers/mmc4.h index 4cfec2e..48257e9 100644 --- a/src/mappersmanager/mappers/mmc4.h +++ b/src/mappersmanager/mappers/mmc4.h @@ -3,7 +3,7 @@ * mmc4.h * * Created by Manoel TRAPIER. - * Copyright (c) 2007-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/norom.c b/src/mappersmanager/mappers/norom.c index 290c149..0d362cd 100644 --- a/src/mappersmanager/mappers/norom.c +++ b/src/mappersmanager/mappers/norom.c @@ -3,7 +3,7 @@ * norom.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/norom.h b/src/mappersmanager/mappers/norom.h index dfa4f95..21f279b 100644 --- a/src/mappersmanager/mappers/norom.h +++ b/src/mappersmanager/mappers/norom.h @@ -3,7 +3,7 @@ * norom.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/unrom.c b/src/mappersmanager/mappers/unrom.c index 0cc80e2..ee19925 100755 --- a/src/mappersmanager/mappers/unrom.c +++ b/src/mappersmanager/mappers/unrom.c @@ -3,7 +3,7 @@ * unrom.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers/unrom.h b/src/mappersmanager/mappers/unrom.h index 4ba4a96..ff717c1 100755 --- a/src/mappersmanager/mappers/unrom.h +++ b/src/mappersmanager/mappers/unrom.h @@ -3,7 +3,7 @@ * unrom.h * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/mappers_list.h b/src/mappersmanager/mappers_list.h index 0875845..fef9218 100644 --- a/src/mappersmanager/mappers_list.h +++ b/src/mappersmanager/mappers_list.h @@ -3,7 +3,7 @@ * mappers_list.h * * Created by Manoel TRAPIER on 25/10/07. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/mappersmanager/utils.c b/src/mappersmanager/utils.c index 6d91101..38f70d4 100755 --- a/src/mappersmanager/utils.c +++ b/src/mappersmanager/utils.c @@ -3,7 +3,7 @@ * mappers.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/memorymanager/CMakeLists.txt b/src/memorymanager/CMakeLists.txt index 7a865e3..d1a2c87 100644 --- a/src/memorymanager/CMakeLists.txt +++ b/src/memorymanager/CMakeLists.txt @@ -2,7 +2,7 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ diff --git a/src/memorymanager/memory.c b/src/memorymanager/memory.c index 6984619..c3f26a7 100755 --- a/src/memorymanager/memory.c +++ b/src/memorymanager/memory.c @@ -3,7 +3,7 @@ * memory.c - Taken from the Quick6502 project * * Created by Manoel Trapier on 18/09/06. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/os/unix/CMakeLists.txt b/src/os/unix/CMakeLists.txt index 915b2d3..b14fa40 100644 --- a/src/os/unix/CMakeLists.txt +++ b/src/os/unix/CMakeLists.txt @@ -2,7 +2,7 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ @@ -10,3 +10,5 @@ # $Revision$ add_library(oslib loadfile.c graphics.c sound.c io.c) + +target_link_libraries(oslib glfw ${OPENGL_glu_LIBRARY}) \ No newline at end of file diff --git a/src/os/unix/graphics.c b/src/os/unix/graphics.c index 97a69fb..9dd714c 100644 --- a/src/os/unix/graphics.c +++ b/src/os/unix/graphics.c @@ -3,7 +3,7 @@ * os/macos/graphics.c * * Created by Manoel TRAPIER on 08/05/08. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -11,19 +11,426 @@ * $Revision$ * */ +#include +#include +#include +#include + #include +#include +#include + +#include + +typedef struct GLWindow_t GLWindow; + +struct KeyArray +{ + unsigned char lastState; + unsigned char curState; + unsigned char debounced; + GLFWwindow* window; +}; + +struct GLWindow_t +{ + struct KeyArray keyArray[512]; + GLFWwindow* windows; + unsigned char *videoMemory; + GLint videoTexture; + int WIDTH; + int HEIGHT; +}; + +#ifndef GL_TEXTURE_RECTANGLE_EXT +#define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV +#endif + +static int window_num = 0; + +void GLWindowInitEx(GLWindow *g, int w, int h) +{ + g->WIDTH = w; + g->HEIGHT = h; + g->videoTexture = window_num++; +} + +void GLWindowInit(GLWindow *g) +{ + GLWindowInitEx(g, 100, 100); +} + +void ShowScreen(GLWindow *g, int w, int h) +{ + glBindTexture(GL_TEXTURE_RECTANGLE_EXT, g->videoTexture); + + // glTexSubImage2D is faster when not using a texture range + glTexSubImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, g->videoMemory); + glBegin(GL_QUADS); + + glTexCoord2f(0.0f, 0.0f); + glVertex2f(-1.0f,1.0f); + + glTexCoord2f(0.0f, h); + glVertex2f(-1.0f, -1.0f); + + glTexCoord2f(w, h); + glVertex2f(1.0f, -1.0f); + + glTexCoord2f(w, 0.0f); + glVertex2f(1.0f, 1.0f); + glEnd(); + + glFlush(); +} + +void setupGL(GLWindow *g, int w, int h) +{ + g->videoMemory = (unsigned char*)malloc(w*h*sizeof(unsigned int)); + memset(g->videoMemory, 0,w*h*sizeof(unsigned int)); + //Tell OpenGL how to convert from coordinates to pixel values + glViewport(0, 0, w, h); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glClearColor(1.0f, 0.f, 1.0f, 1.0f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glDisable(GL_TEXTURE_2D); + glEnable(GL_TEXTURE_RECTANGLE_EXT); + glBindTexture(GL_TEXTURE_RECTANGLE_EXT, g->videoTexture); + + // glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_NV_EXT, 0, NULL); + + // glTexParameteri(GL_TEXTURE_RECTANGLE_NV_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_CACHED_APPLE); + // glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + + glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, w, + h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, g->videoMemory); + + glDisable(GL_DEPTH_TEST); +} + +void restoreGL(GLWindow *g, int w, int h) +{ + //Tell OpenGL how to convert from coordinates to pixel values + glViewport(0, 0, w, h); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glClearColor(1.0f, 0.f, 1.0f, 1.0f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glDisable(GL_TEXTURE_2D); + glEnable(GL_TEXTURE_RECTANGLE_EXT); + glDisable(GL_DEPTH_TEST); +} + +void kbHandler(GLFWwindow* window, int key, int scan, int action, int mod ) +{ + struct KeyArray *keyArray; + + keyArray = (struct KeyArray*) glfwGetWindowUserPointer(window); + + keyArray[key].lastState=keyArray[key].curState; + if (action==GLFW_RELEASE) + { + keyArray[key].curState=GLFW_RELEASE; + } + else + { + keyArray[key].curState=GLFW_PRESS; + } + keyArray[key].debounced |= (keyArray[key].lastState==GLFW_RELEASE)&&(keyArray[key].curState==GLFW_PRESS); + keyArray[key].window = window; +} + +void sizeHandler(GLFWwindow* window,int xs,int ys) +{ + glfwMakeContextCurrent(window); + glViewport(0, 0, xs, ys); +} + + +void initDisplay(GLWindow *g) +{ + int h = g->HEIGHT; + int w = g->WIDTH; + + /// Initialize GLFW + glfwInit(); + + // Open screen OpenGL window + if( !(g->windows=glfwCreateWindow( g->WIDTH, g->HEIGHT, "Main", NULL, NULL)) ) + { + glfwTerminate(); + fprintf(stderr, "Window creation error...\n"); + return; + } + + glfwMakeContextCurrent(g->windows); + setupGL(g, g->WIDTH, g->HEIGHT); + + glfwSwapInterval(0); // Disable VSYNC + + glfwGetWindowSize(g->windows, &w, &h); + + glfwSetWindowUserPointer(g->windows, g->keyArray); + + glfwSetKeyCallback(g->windows, kbHandler); + glfwSetWindowSizeCallback(g->windows, sizeHandler); +} + +void drawPixel(GLWindow *gw, int x, int y, uint32_t colour) +{ + uint8_t r,g,b,a; + + uint32_t offset = (y*gw->WIDTH*4)+4*x; + + if ((x < 0) || (x > gw->WIDTH) || (y < 0) || (y > gw->HEIGHT)) + return; + + b = colour & 0xFF; + g = (colour >> 8) & 0xFF; + r = (colour >> 16) & 0xFF; + a = (colour >> 24) & 0xFF; + + gw->videoMemory[offset + 0] = a; + gw->videoMemory[offset + 1] = r; + gw->videoMemory[offset + 2] = g; + gw->videoMemory[offset + 3] = b; +} + +void drawLine(GLWindow *g, int x0, int y0, int x1, int y1, uint32_t colour) +{ + int d, dx, dy, aincr, bincr, xincr, yincr, x, y; + if (abs(x1 - x0) < abs(y1 - y0)) + { + /* parcours par l'axe vertical */ + if (y0 > y1) + { + drawLine(g, x1, y1, x0, y0, colour); + goto exit; + } + + xincr = x1 > x0 ? 1 : -1; + dy = y1 - y0; + dx = abs(x1 - x0); + d = 2 * dx - dy; + aincr = 2 * (dx - dy); + bincr = 2 * dx; + x = x0; + y = y0; + + drawPixel(g, x, y, colour); + + for (y = y0+1; y <= y1; y++) + { + if (d >= 0) + { + x += xincr; + d += aincr; + } + else + { + d += bincr; + } + + drawPixel(g, x, y, colour); + } + + } + else + { + /* parcours par l'axe horizontal */ + if (x0 > x1) + { + drawLine(g, x1, y1, x0, y0, colour); + goto exit; + } + yincr = y1 > y0 ? 1 : -1; + dx = x1 - x0; + dy = abs(y1 - y0); + d = 2 * dy - dx; + aincr = 2 * (dy - dx); + bincr = 2 * dy; + x = x0; + y = y0; + + drawPixel(g, x, y, colour); + + for (x = x0+1; x <= x1; ++x) + { + if (d >= 0) + { + y += yincr; + d += aincr; + } + else + { + d += bincr; + } + + drawPixel(g, x, y, colour); + } + } + + exit: + return; +} + +void drawCircle(GLWindow *g, int xc, int yc, int radius, uint32_t colour) +{ + int f = 1 - radius; + int ddF_x = 0; + int ddF_y = -2 * radius; + int x = 0; + int y = radius; + int pX, pY; + + pX = xc; pY = yc + radius; + drawPixel(g, pX, pY, colour); + pY -= (2*radius); + drawPixel(g, pX, pY, colour); + pY += radius; pX += radius; + drawPixel(g, pX, pY, colour); + pX -= (2*radius); + drawPixel(g, pX, pY, colour); + + while(x < y) + { + if(f >= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x + 1; + pX = xc+x ; pY = yc+y; + drawPixel(g, pX, pY, colour); + pX = xc-x ; pY = yc+y; + drawPixel(g, pX, pY, colour); + pX = xc+x ; pY = yc-y; + drawPixel(g, pX, pY, colour); + pX = xc-x ; pY = yc-y; + drawPixel(g, pX, pY, colour); + pX = xc+y ; pY = yc+x; + drawPixel(g, pX, pY, colour); + pX = xc-y ; pY = yc+x; + drawPixel(g, pX, pY, colour); + pX = xc+y ; pY = yc-x; + drawPixel(g, pX, pY, colour); + pX = xc-y ; pY = yc-x; + drawPixel(g, pX, pY, colour); + } + + return; +} + +void drawRect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour) +{ + drawLine(g, x0 , y0 , x0 + w, y0 , colour); + drawLine(g, x0 + w, y0 , x0 + w, y0 + h, colour); + drawLine(g, x0 + w, y0 + h, x0 , y0 + h, colour); + drawLine(g, x0 , y0 + h, x0 , y0 , colour); +} + +void drawFillrect(GLWindow *g, int x0, int y0, int w, int h, uint32_t colour) +{ + int i; + for(i = 0; i < h; i++) + drawLine(g, x0, y0+i, x0+w, y0+i, colour); +} + +void clearScreen(GLWindow *g) +{ + memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4); +} + +void updateScreen(GLWindow *g) +{ + /*Update windows code */ + glfwMakeContextCurrent(g->windows); + ShowScreen(g, g->WIDTH, g->HEIGHT); + glfwSwapBuffers(g->windows); + glfwPollEvents(); +} + +void updateScreenAndWait(GLWindow *g) +{ + while (glfwGetKey(g->windows,GLFW_KEY_ESCAPE) != GLFW_PRESS) + { + updateScreen(g); + + glfwPollEvents(); + } + while(glfwGetKey(g->windows,GLFW_KEY_ESCAPE) != GLFW_RELEASE) + { + glfwPollEvents(); + } +} + +GLWindow mainWindow; + int graphics_init() { + GLWindowInitEx(&mainWindow, 256, 240); + initDisplay(&mainWindow); + clearScreen(&mainWindow); + updateScreen(&mainWindow); return 0; } +static unsigned long getColour(long color) +{ + Palette *pal = &basicPalette[color]; + uint8_t r, g, b, a; + r = pal->r; + b = pal->b; + g = pal->g; + a = 255;//pal->a; + return (b << 24) | (g << 16) | (r << 8) | a; +} + int graphics_drawpixel(long x, long y, long color) { + drawPixel(&mainWindow, x, y, getColour(color)); + return 0; +} + +int graphics_drawline(long x, long y, long x1, long y1, long color) +{ + drawLine(&mainWindow, x, y, x1, y1, getColour(color)); return 0; } int graphics_blit(long x, long y, long w, long h) { + updateScreen(&mainWindow); return 0; } + +int getKeyStatus(int key) +{ + return mainWindow.keyArray[key].curState; +} \ No newline at end of file diff --git a/src/os/unix/io.c b/src/os/unix/io.c index 84e999c..171a611 100644 --- a/src/os/unix/io.c +++ b/src/os/unix/io.c @@ -3,7 +3,7 @@ * os/macos/graphics.c * * Created by Manoƫl Trapier on 04/01/09. - * Copyright (c) 2003-2009 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/os/unix/loadfile.c b/src/os/unix/loadfile.c index 8dce40f..4d79168 100644 --- a/src/os/unix/loadfile.c +++ b/src/os/unix/loadfile.c @@ -1,3 +1,6 @@ +/* + * Copyright (c) 2003-2016 986-Studio. All rights reserved. + */ #include #include #include diff --git a/src/paddle.c b/src/paddle.c index c35c517..ccdfaf6 100755 --- a/src/paddle.c +++ b/src/paddle.c @@ -3,7 +3,7 @@ * paddle.c * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -12,117 +12,83 @@ * */ -/* Allegro includes */ -#ifdef __APPLE__ -#define USE_CONSOLE -#include -#else -#define USE_CONSOLE -#include -#endif +#include + #include "paddle.h" -void InitPaddle(Paddle * pdl) +void InitPaddle(Paddle *pdl) { - pdl->Bit = 1; - pdl->LastWrite = 0; -} + pdl->Bit = 1; + pdl->LastWrite = 0; +} -void WritePaddle(Paddle *pdl, unsigned char val) +void WritePaddle(Paddle *pdl, unsigned char val) { - if ((pdl->LastWrite == 1) && (val == 0)) - InitPaddle(pdl); - - pdl->LastWrite = val; -} -unsigned char ReadPaddle(Paddle * pdl) + if ( ( pdl->LastWrite == 1 ) && ( val == 0 ) ) + InitPaddle(pdl); + + pdl->LastWrite = val; +} + +unsigned char ReadPaddle(Paddle *pdl) { - switch (pdl->Bit++) - { - - case 1: - if (key[KEY_Z]) - - return 0x41; - - break; - - case 2: - - if (key[KEY_X]) - - return 0x41; - - break; - - case 3: - - if (key[KEY_P]) - - return 0x41; - - break; - - case 4: - - if (key[KEY_ENTER]) - - return 0x41; - - break; - - case 5: - - if (key[KEY_UP]) - - return 0x41; - - break; - - case 6: - - if (key[KEY_DOWN]) - - return 0x41; - - break; - - case 7: - - if (key[KEY_LEFT]) - - return 0x41; - - break; - - case 8: - - if (key[KEY_RIGHT]) - - return 0x41; - - break; - - case 20: - - return 0x40; - - break; - - case 24: - - pdl->Bit = 1; - - return 0x40; - - default: - - return 0x40; - - break; - - } + switch(pdl->Bit++) + { + + case 1: + if ( getKeyStatus('O') ) + return 0x41; + break; + + case 2: + if ( getKeyStatus('P') ) + return 0x41; + break; + + case 3: + if ( getKeyStatus('I') ) + return 0x41; + break; + + case 4: + if ( getKeyStatus('U') ) + return 0x41; + break; + + case 5: + if ( getKeyStatus('W') ) + return 0x41; + break; + + case 6: + if ( getKeyStatus('S') ) + return 0x41; + break; + + case 7: + if ( getKeyStatus('A') ) + return 0x41; + break; + + case 8: + if ( getKeyStatus('D') ) + return 0x41; + break; + + case 20: + return 0x40; + break; + + case 24: + pdl->Bit = 1; + return 0x40; + + default: + return 0x40; + break; + + } + return 0x40; - } diff --git a/src/pluginsmanager/CMakeLists.txt b/src/pluginsmanager/CMakeLists.txt index 15a00b5..a0b0965 100644 --- a/src/pluginsmanager/CMakeLists.txt +++ b/src/pluginsmanager/CMakeLists.txt @@ -2,7 +2,7 @@ # TI-NES CMake # # Created by Manoel TRAPIER. -# Copyright (c) 2003-2008 986Corp. All rights reserved. +# Copyright (c) 2003-2016 986-Studio. All rights reserved. # # $LastChangedDate$ # $Author$ diff --git a/src/pluginsmanager/manager.c b/src/pluginsmanager/manager.c index 88b1bd3..f7d94a3 100644 --- a/src/pluginsmanager/manager.c +++ b/src/pluginsmanager/manager.c @@ -3,7 +3,7 @@ * plugins.c * * Created by Manoel TRAPIER on 02/04/07. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/pluginsmanager/plugins/gamegenie.c b/src/pluginsmanager/plugins/gamegenie.c index cec5e42..efef749 100644 --- a/src/pluginsmanager/plugins/gamegenie.c +++ b/src/pluginsmanager/plugins/gamegenie.c @@ -3,7 +3,7 @@ * gamegenie.c: Hack your games with unlimited lives of add new powers! * * Created by Manoel Trapier. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -24,6 +24,7 @@ #include #include +#if 0 /* Allegro includes */ #ifdef __APPLE__ #define USE_CONSOLE @@ -824,3 +825,4 @@ int gg_Deinit() { return 0; } +#endif \ No newline at end of file diff --git a/src/pluginsmanager/plugins/gamegenie.h b/src/pluginsmanager/plugins/gamegenie.h index 732abc6..4c6b769 100644 --- a/src/pluginsmanager/plugins/gamegenie.h +++ b/src/pluginsmanager/plugins/gamegenie.h @@ -3,7 +3,7 @@ * gamegenie.h * * Created by Manoel Trapier. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ diff --git a/src/pluginsmanager/plugins_list.h b/src/pluginsmanager/plugins_list.h index 95b09e1..d723f01 100644 --- a/src/pluginsmanager/plugins_list.h +++ b/src/pluginsmanager/plugins_list.h @@ -3,7 +3,7 @@ * plugins_list.h * * Created by Manoel Trapier. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -17,7 +17,7 @@ #include "plugins/gamegenie.h" Plugin Plugins[] = { - { "Game Genie", gg_Init, gg_Deinit }, + // { "Game Genie", gg_Init, gg_Deinit }, /* EOL tag */ { NULL, NULL, NULL } diff --git a/src/ppu/ppu.c b/src/ppu/ppu.c index 451ea12..df3c09b 100755 --- a/src/ppu/ppu.c +++ b/src/ppu/ppu.c @@ -5,7 +5,7 @@ * Define and emulate the PPU (Picture Processing Unit) of the real NES * * Created by Manoel TRAPIER. - * Copyright (c) 2003-2008 986Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$ @@ -14,20 +14,14 @@ * */ -/* Allegro includes */ -#ifdef __APPLE__ -#define USE_CONSOLE -#include -#else -#define USE_CONSOLE -#include -#endif - #include #include #include +#include + #define __TINES_PPU_INTERNAL__ + #include #include #include @@ -37,128 +31,105 @@ #include #define __TINES_PLUGINS__ + #include extern int VBLANK_TIME; -extern BITMAP *Buffer; - volatile extern int frame; volatile extern unsigned long IPS, FPS; -extern unsigned long ColorPalette[ 9 * 63 ]; +extern unsigned long ColorPalette[9 * 63]; extern short IRQScanHit; extern short SZHit; -BITMAP *VideoBuffer; /* The ppu will only write pixel to this, and then bliting - this on the screen "surface" */ +typedef struct spriteData +{ + uint8_t palette; + uint8_t flip_h; + uint8_t flip_v; + uint8_t priority; + uint8_t tile; + uint8_t bank; + uint8_t y; + uint8_t x; + uint8_t rel_y; + uint8_t inUse; +} spriteData; -/* PPU sprite sorted by scanline */ - -/* Work as follow: - -3322 2222 2222 1111 1111 1100 0000 0000 -1098 7654 3210 9876 5432 1098 7654 3210 ---------------------------------------- -AAAA AAAA TTTT TTTT xxxx XXXX YYYY YYYY ---------------------------------------- -8421 8421 8421 8421 8421 8421 8421 8421 - -A = Sprite Attributes -x = reserved -T = Tile ID -X = X relative position -Y = Y absolute position - -x = for future use - - */ -unsigned long PPU_SpriteByScanLine[241][9]; /* There is 240 scanline and 8 sprite per scanline */ -unsigned long PPU_NbSpriteByScanLine[241]; /* There is 240 scanline and 8 sprite per scanline */ -unsigned long PPU_NbSpriteByScanLineOverFlow[241]; /* There is 240 scanline and 8 sprite per scanline */ - -#define PPU_SCANLINESPRITE_GET_ATTRS(sprt) (((sprt)&0xFF000000) >> 24) -#define PPU_SCANLINESPRITE_GET_TILIDX(sprt) (((sprt)&0x00FF0000) >> 16) -#define PPU_SCANLINESPRITE_GET_RELY(sprt) (((sprt)&0x00000F00) >> 8) -#define PPU_SCANLINESPRITE_GET_X(sprt) ((sprt)&0x000000FF) - -#define PPU_SCANLINESPRITE_SET_ATTRS(sprt, v) sprt = (((sprt)&0x00FFFFFF) | (( (v) & 0xFF) << 24)) -#define PPU_SCANLINESPRITE_SET_TILIDX(sprt, v) sprt = (((sprt)&0xFF00FFFF) | (( (v) & 0xFF) << 16)) -#define PPU_SCANLINESPRITE_SET_RELY(sprt, v) sprt = (((sprt)&0xFFFFF0FF) | (( (v) & 0x0F) << 8)) -#define PPU_SCANLINESPRITE_SET_X(sprt, v) sprt = (((sprt)&0xFFFFFF00) | ( (v) & 0xFF )) /* PPU registers */ /* NT: Name Table */ -byte PPU_Reg_NT; +uint8_t PPU_Reg_NT; /* AT: Attribute/Color Table */ -byte PPU_Reg_AT; +uint8_t PPU_Reg_AT; /* FV: Fine Vertical Scroll latch/counter */ -byte PPU_Reg_FV; +uint8_t PPU_Reg_FV; /* HV: Fine Horizontal Scroll latch/counter */ -byte PPU_Reg_FH; +uint8_t PPU_Reg_FH; /* VT: Vertical Tile indev latch/counter */ -byte PPU_Reg_VT; +uint8_t PPU_Reg_VT; /* HT: Horizontal Tile indev latch/counter */ -byte PPU_Reg_HT; +uint8_t PPU_Reg_HT; /* V: Vertical Name Table Selection latch/counter */ -byte PPU_Reg_V; +uint8_t PPU_Reg_V; /* H: Horizontal Name Table Selection latch/counter */ -byte PPU_Reg_H; +uint8_t PPU_Reg_H; /* S: Playfield pattern table selection latch */ unsigned short PPU_Reg_S; /* PAR: Picture Address Register */ -byte PPU_Reg_PAR; +uint8_t PPU_Reg_PAR; /* AR: Tile Attribute (palette select) value latch */ -byte PPU_Reg_AR; +uint8_t PPU_Reg_AR; unsigned short PPU_Reg_Counter; /* PPU Memory Areas */ -byte *ppu_mem_nameTables; -byte *ppu_mem_patternTables; -byte *ppu_mem_paletteValues; +uint8_t *ppu_mem_nameTables; +uint8_t *ppu_mem_patternTables; +uint8_t *ppu_mem_paletteValues; -byte ppu_mem_spritesTable[0x100]; -byte ppu_mem_sptrTablePtr; +uint8_t ppu_mem_spritesTable[0x100]; +uint8_t ppu_mem_sptrTablePtr; /* Some other PPU "registers" */ -byte ppu_VramAccessFlipFlop; +uint8_t ppu_VramAccessFlipFlop; -byte ppu_inVBlankTime; -byte ppu_spriteZeroHit; -byte ppu_scanlineSpriteOverflow; +uint8_t ppu_inVBlankTime; +uint8_t ppu_spriteZeroHit; +uint8_t ppu_scanlineSpriteOverflow; -byte ppu_bgColor; +uint8_t ppu_bgColor; /* CR #1 variables */ -unsigned short ppu_spritePatternTable; -byte ppu_spriteSize; -byte ppu_addrIncrement; -byte ppu_execNMIonVBlank; +uint16_t ppu_spritePatternTable; +uint8_t ppu_spriteSize; +uint8_t ppu_addrIncrement; +uint8_t ppu_execNMIonVBlank; /* CR #2 variables */ -byte ppu_spriteVisibility; -byte ppu_backgroundVisibility; -byte ppu_spriteClipping; -byte ppu_backgroundClipping; -byte ppu_displayType; +uint8_t ppu_spriteVisibility; +uint8_t ppu_backgroundVisibility; +uint8_t ppu_spriteClipping; +uint8_t ppu_backgroundClipping; +uint8_t ppu_displayType; -byte ppu_mirrorMode; -byte ppu_singleScreenMode; -byte ppu_screenMode; +uint8_t ppu_mirrorMode; +uint8_t ppu_singleScreenMode; +uint8_t ppu_screenMode; #define PPU_MEM_PATTERNTABLES_SIZE 0x2000 #define PPU_MEM_NAMETABLE_SIZE 0x1000 @@ -171,8 +142,8 @@ byte ppu_screenMode; #define PPU_FLAG_SR_VBLANK ( 1 << 7 ) #define PPU_FLAG_SR_SPRT0 ( 1 << 6 ) -#define PPU_FLAG_SR_8SPRT ( 1 << 5 ) -#define PPU_FLAG_SR_RDWRALLOW ( 1 << 4 ) +#define PPU_FLAG_SR_8SPRT ( 1 << 5 ) +#define PPU_FLAG_SR_RDWRALLOW ( 1 << 4 ) #define PPU_CR1_SPRTSIZE ( 1 << 5 ) #define PPU_CR1_EXECNMI ( 1 << 7 ) @@ -182,273 +153,199 @@ byte ppu_screenMode; int ppu_init() { - int i; + int i; - /*byte defaultColors[] = { 0x09,0x01,0x00,0x01,0x00,0x02,0x02,0x0D,0x08,0x10,0x08,0x24,0x00,0x00,0x04,0x2C, - 0x09,0x01,0x34,0x03,0x00,0x04,0x00,0x14,0x08,0x3A,0x00,0x02,0x00,0x20,0x2C,0x08 };*/ + /*uint8_t defaultColors[] = { 0x09,0x01,0x00,0x01,0x00,0x02,0x02,0x0D,0x08,0x10,0x08,0x24,0x00,0x00,0x04,0x2C, + 0x09,0x01,0x34,0x03,0x00,0x04,0x00,0x14,0x08,0x3A,0x00,0x02,0x00,0x20,0x2C,0x08 };*/ - if (ppu_initMemory()) - return -1; + if ( ppu_initMemory() ) + return -1; - /* Set ppu memory parameters */ + /* Set ppu memory parameters */ - /* First: Allocate each memory zone */ - ppu_mem_patternTables = (byte*) malloc(PPU_MEM_PATTERNTABLES_SIZE); - if (!ppu_mem_patternTables) - return -1; + /* First: Allocate each memory zone */ + ppu_mem_patternTables = (uint8_t *) malloc(PPU_MEM_PATTERNTABLES_SIZE); + if ( !ppu_mem_patternTables ) + return -1; - ppu_mem_nameTables = (byte*) malloc(PPU_MEM_NAMETABLE_SIZE); - if (!ppu_mem_nameTables) - return -1; + ppu_mem_nameTables = (uint8_t *) malloc(PPU_MEM_NAMETABLE_SIZE); + if ( !ppu_mem_nameTables ) + return -1; - ppu_mem_paletteValues = (byte*) malloc(PPU_MEM_PALETTEVALUES_SIZE); - if (!ppu_mem_paletteValues) - return -1; + ppu_mem_paletteValues = (uint8_t *) malloc(PPU_MEM_PALETTEVALUES_SIZE); + if ( !ppu_mem_paletteValues ) + return -1; - console_printf(Console_Default, "ppu_mem_nameTables :%p\n" - "ppu_mem_patternTables:%p\n" - "ppu_mem_paletteValues:%p\n", - ppu_mem_nameTables, - ppu_mem_patternTables, - ppu_mem_paletteValues); + console_printf(Console_Default, "ppu_mem_nameTables :%p\n" + "ppu_mem_patternTables:%p\n" + "ppu_mem_paletteValues:%p\n", + ppu_mem_nameTables, + ppu_mem_patternTables, + ppu_mem_paletteValues); - /* Second: make the ppu memory manager point on the memory zones */ - ppu_setPagePtr8k(0x00, ppu_mem_patternTables); - ppu_setPagePtr4k(0x20, ppu_mem_nameTables); - ppu_setPagePtr (0x3F, ppu_mem_paletteValues); + /* Second: make the ppu memory manager point on the memory zones */ + ppu_setPagePtr8k(0x00, ppu_mem_patternTables); + ppu_setPagePtr4k(0x20, ppu_mem_nameTables); + ppu_setPagePtr(0x3F, ppu_mem_paletteValues); - for ( i = 0x00; i < 0x0F; i++ ) - ppu_setPageGhost(0x30 + i, true, 0x20 + i); + for ( i = 0x00 ; i < 0x0F ; i++ ) + ppu_setPageGhost(0x30 + i, true, 0x20 + i); - /* Third: set registers to defaults */ + /* Third: set registers to defaults */ - /* Now test the memory ! */ + /* Now test the memory ! */ - /* Fille PPU memory with garbage */ - for (i = 0x0000; i < 0x2000 ; i++) - ppu_mem_patternTables[i] = rand()%0xFF; - for (i = 0x0000; i < 0x1000 ; i++) - ppu_mem_nameTables[i] = rand()%0xFF; - for (i = 0x0000; i < 0x001F ; i++) - ppu_mem_paletteValues[i] = rand()%0xFF; + /* Fille PPU memory with garbage */ + for ( i = 0x0000 ; i < 0x2000 ; i++ ) + ppu_mem_patternTables[i] = rand() % 0xFF; + for ( i = 0x0000 ; i < 0x1000 ; i++ ) + ppu_mem_nameTables[i] = rand() % 0xFF; + for ( i = 0x0000 ; i < 0x001F ; i++ ) + ppu_mem_paletteValues[i] = rand() % 0xFF; - //memcpy(ppu_mem_paletteValues, defaultColors, 32); + //memcpy(ppu_mem_paletteValues, defaultColors, 32); - /* Set some other variables */ - ppu_VramAccessFlipFlop = 0; + /* Set some other variables */ + ppu_VramAccessFlipFlop = 0; - ppu_addrIncrement = 1; - ppu_spritePatternTable = 0; - ppu_spriteSize = 8; - ppu_execNMIonVBlank = 0; + ppu_addrIncrement = 1; + ppu_spritePatternTable = 0; + ppu_spriteSize = 8; + ppu_execNMIonVBlank = 0; - ppu_spriteVisibility = 0; - ppu_backgroundVisibility = 0; - ppu_spriteClipping = 0; - ppu_backgroundClipping = 0; - ppu_displayType = 0; + ppu_spriteVisibility = 0; + ppu_backgroundVisibility = 0; + ppu_spriteClipping = 0; + ppu_backgroundClipping = 0; + ppu_displayType = 0; - ppu_inVBlankTime = 0; - ppu_bgColor = 0; + ppu_inVBlankTime = 0; + ppu_bgColor = 0; - /* Set PPU registers on CPU side */ - set_page_rd_hook(0x20, ppu_readReg); - set_page_wr_hook(0x20, ppu_writeReg); + /* Set PPU registers on CPU side */ + set_page_rd_hook(0x20, ppu_readReg); + set_page_wr_hook(0x20, ppu_writeReg); - set_page_readable(0x20, true); - set_page_writeable(0x20, true); + set_page_readable(0x20, true); + set_page_writeable(0x20, true); - /* Set PPU Ghost Registers */ - for(i = 0x21; i < 0x40; i++) - set_page_ghost(i, true, 0x20); + /* Set PPU Ghost Registers */ + for ( i = 0x21 ; i < 0x40 ; i++ ) + set_page_ghost(i, true, 0x20); - /* allocate the PPU Video memory */ - VideoBuffer = create_bitmap(256, 240); + /* allocate the PPU Video memory */ + graphics_init(); - if (VideoBuffer == NULL) - return -1; - - return 0; + return 0; } -void ppu_updateSpriteScanlineTable() +void ppu_setMirroring(uint8_t direction) { - int32_t i,j,k; - int line; - volatile int32_t sprite_x, sprite_y, sprite_idx, sprite_attr; + if ( ppu_screenMode != PPU_SCMODE_NORMAL ) + return; - int curline; + if ( ppu_mirrorMode == direction ) + return; /* Same value, no need to change! */ - for (line = 0; line < 241; line ++) - { - PPU_NbSpriteByScanLine[line] = 0; - PPU_NbSpriteByScanLineOverFlow[line] = 0; + switch(direction) + { + default: + direction = PPU_MIRROR_HORIZTAL; + ppu_mirrorMode = direction; - for (i = 0; i < 9; i++) - PPU_SpriteByScanLine[line][i] = 0xFFFFFFFF; - } - - for ( i = 0; i < 64; i ++) - { - /* Fill sprite_zzz variables */ - sprite_y = ppu_mem_spritesTable[(i*4) + 0] + 1; - sprite_idx = ppu_mem_spritesTable[(i*4) + 1]; - sprite_attr = ppu_mem_spritesTable[(i*4) + 2] | ((i==0)?0x04:0); /* Add a flag for the sprite #0 */ - sprite_x = ppu_mem_spritesTable[(i*4) + 3]; - - /* For each line covered by the sprite */ - for (line = 0; line < ppu_spriteSize; line ++) - { - curline = line + sprite_y; - - if ((curline < 0) || (curline > 240)) - continue; /* Don't go beyond, this sprite go beyond the borders */ - - if (PPU_NbSpriteByScanLine[curline] < 8) - PPU_NbSpriteByScanLine[curline] ++; - else - { - PPU_NbSpriteByScanLineOverFlow[curline] = 1; - continue; /* We have 8 sprite in this line, don't continue */ - } - if (((sprite_x+8) < 0) && ((sprite_x-8) > 256)) - continue; /* this sprite isn't either displayable */ - /* Now test if this sprite can be put in the sprite list */ - for (j = 0; j <= (int32_t)PPU_NbSpriteByScanLine[curline]; j++) - { - /* sprite are ordered by their y value, so, the first time that - we have lower y value is where we need to put the sprite */ - if (sprite_x < (int32_t)PPU_SCANLINESPRITE_GET_X(PPU_SpriteByScanLine[curline][j])) - { - /* move the j eme item and next to the right in the list, trashing - if needed the rightest item. */ - for (k = 7; k >= j; k--) - PPU_SpriteByScanLine[curline][k] = PPU_SpriteByScanLine[curline][k-1]; - - PPU_SpriteByScanLine[curline][j] = 0; - - PPU_SCANLINESPRITE_SET_ATTRS (PPU_SpriteByScanLine[curline][j], sprite_attr); - - PPU_SCANLINESPRITE_SET_TILIDX(PPU_SpriteByScanLine[curline][j], sprite_idx); - - PPU_SCANLINESPRITE_SET_RELY (PPU_SpriteByScanLine[curline][j], curline - sprite_y); - - PPU_SCANLINESPRITE_SET_X (PPU_SpriteByScanLine[curline][j], sprite_x); - - break; /* Stop the for, we don't need to go further in the line list */ - } - } - } - } + case PPU_MIRROR_HORIZTAL: /* Horizontal */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); + break; + case PPU_MIRROR_VERTICAL: /* Vertical */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); + break; + } + ppu_mirrorMode = direction; } -void ppu_setMirroring(byte direction) +void ppu_setSingleScreen(uint8_t screen) { - if (ppu_screenMode != PPU_SCMODE_NORMAL) - return; + if ( ppu_screenMode != PPU_SCMODE_SINGLE ) + return; + if ( ppu_singleScreenMode == screen ) + return; /* Same value, no need to change! */ - if (ppu_mirrorMode == direction) - return; /* Same value, no need to change! */ + switch(screen) + { + default: + screen = PPU_SCREEN_000; + ppu_singleScreenMode = screen; - switch(direction) - { - default: - direction = PPU_MIRROR_HORIZTAL; - ppu_mirrorMode = direction; + case PPU_SCREEN_000: /* 0x2000 */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x000); + break; - case PPU_MIRROR_HORIZTAL: /* Horizontal */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); - break; - case PPU_MIRROR_VERTICAL: /* Vertical */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); - break; - } - ppu_mirrorMode = direction; -} + case PPU_SCREEN_400: /* 0x2400 */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); + break; -void ppu_setSingleScreen(byte screen) -{ - if (ppu_screenMode != PPU_SCMODE_SINGLE) - return; - if (ppu_singleScreenMode == screen) - return; /* Same value, no need to change! */ + case PPU_SCREEN_800: /* 0x2800 */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x800); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x800); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x800); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x800); + break; - switch(screen) - { - default: - screen = PPU_SCREEN_000; - ppu_singleScreenMode = screen; - - case PPU_SCREEN_000: /* 0x2000 */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x000); - break; - - case PPU_SCREEN_400: /* 0x2400 */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x400); - break; - - case PPU_SCREEN_800: /* 0x2800 */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x800); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x800); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x800); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0x800); - break; - - case PPU_SCREEN_C00: /* 0x2C00 */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0xC00); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0xC00); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0xC00); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0xC00); - break; - } - ppu_singleScreenMode = screen; + case PPU_SCREEN_C00: /* 0x2C00 */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0xC00); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0xC00); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0xC00); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0xC00); + break; + } + ppu_singleScreenMode = screen; } /* Let set display to Single screen (1 NT with mirroring) Normal screen (2 NT with mirroring) Four screen (4 NT without mirroring) */ -void ppu_setScreenMode(byte mode) +void ppu_setScreenMode(uint8_t mode) { - if (ppu_screenMode == mode) - return; /* Same value, no need to change! */ + if ( ppu_screenMode == mode ) + return; /* Same value, no need to change! */ - ppu_screenMode = mode; + ppu_screenMode = mode; - switch(mode) - { - case PPU_SCMODE_SINGLE: /* Single screen (1 NT with mirroring) */ - ppu_setSingleScreen(~ppu_singleScreenMode); - break; + switch(mode) + { + case PPU_SCMODE_SINGLE: /* Single screen (1 NT with mirroring) */ + ppu_setSingleScreen(~ppu_singleScreenMode); + break; - default: - mode = PPU_SCMODE_NORMAL; - ppu_screenMode = mode; + default: + mode = PPU_SCMODE_NORMAL; + ppu_screenMode = mode; - case PPU_SCMODE_NORMAL: /* Normal screen (2 NT with mirroring) */ - ppu_setMirroring(~ppu_mirrorMode); - break; + case PPU_SCMODE_NORMAL: /* Normal screen (2 NT with mirroring) */ + ppu_setMirroring(~ppu_mirrorMode); + break; - case PPU_SCMODE_FOURSC: /* Four screen (4 NT withou mirroring) */ - ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); - ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); - ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x800); - ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0xC00); - break; - } + case PPU_SCMODE_FOURSC: /* Four screen (4 NT withou mirroring) */ + ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000); + ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400); + ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x800); + ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0xC00); + break; + } } /* update whole counters */ @@ -471,216 +368,277 @@ void ppu_updateCounters() _AAA BCDD DDDE EEEE */ - PPU_Reg_Counter = (PPU_Reg_FV & 0x07) << 12; - PPU_Reg_Counter |= PPU_Reg_V << 11; - PPU_Reg_Counter |= PPU_Reg_H << 10; - PPU_Reg_Counter |= PPU_Reg_VT << 5; - PPU_Reg_Counter |= PPU_Reg_HT; + PPU_Reg_Counter = ( PPU_Reg_FV & 0x07 ) << 12; + PPU_Reg_Counter |= PPU_Reg_V << 11; + PPU_Reg_Counter |= PPU_Reg_H << 10; + PPU_Reg_Counter |= PPU_Reg_VT << 5; + PPU_Reg_Counter |= PPU_Reg_HT; } -int ppu_hblank(int scanline) +int ppu_hblank(uint16_t scanline) { - uint32_t i, j; - byte pixelColor = 0x42; - byte BgColor = 0x42; - byte SpriteColor = 0x42; - uint16_t addr; - byte value; - uint16_t tmp_HHT = 0; - uint16_t tmp_VVTFV = 0; - uint32_t CurrentSprite; - byte SpriteVFlip; - - if (scanline == 0) - { - ppu_bgColor = ppu_readMemory(0x3F,00); + uint32_t i, j; + uint8_t pixelColor = 0; + uint8_t BgColor = 0; + uint8_t SpriteColor = 0; - rectfill(Buffer, 256, 0, 277, 260, ppu_bgColor); + /* Sprite to display on current scanline */ + spriteData scanSprites[8]; - if ((ppu_spriteVisibility != 0) || (ppu_backgroundVisibility != 0)) - ppu_updateCounters(); - } + uint16_t addr; + uint8_t value; + uint16_t tmp_HHT = 0; + uint16_t tmp_VVTFV = 0; + uint8_t spriteCount = 0; + ppu_scanlineSpriteOverflow = 0; - if (scanline < 240) - { + if ( scanline == 0 ) + { + if ( ( ppu_spriteVisibility != 0 ) || ( ppu_backgroundVisibility != 0 ) ) + ppu_updateCounters(); + } - /* For each PPU pixel of this scanline */ - for (i = 0; i < 256; i ++) - { - /* Set the current pixel color to the bg color */ - pixelColor = ppu_readMemory(0x3F,00); + if ( scanline < 240 ) + { + /* Search for sprites on current scanline */ + for (i = 0 ; i < 8 ; i++) + { + scanSprites[i].inUse = 0; + } - /* Compute current pixel bg color if bg is visible */ - if (ppu_backgroundVisibility == 1) + for (i = 0 ; i < 64 && spriteCount < 8; i++) + { + uint8_t spriteY = ppu_mem_spritesTable[i*4] + 1; + if ((scanline >= spriteY) && (scanline < (spriteY + ppu_spriteSize))) + { + /* This sprite is on the scanline */ + scanSprites[spriteCount].inUse = 1; + + scanSprites[spriteCount].x = ppu_mem_spritesTable[i*4 + 3]; + scanSprites[spriteCount].y = spriteY; + scanSprites[spriteCount].rel_y = scanline - scanSprites[spriteCount].y; + + scanSprites[spriteCount].tile = ppu_mem_spritesTable[i * 4 + 1]; + scanSprites[spriteCount].bank = ppu_mem_spritesTable[i * 4 + 1] & 0x01; + scanSprites[spriteCount].flip_h = ( ppu_mem_spritesTable[i * 4 + 2] & PPU_SPRITE_FLAGS_HFLIP )?1:0; + scanSprites[spriteCount].flip_v = ( ppu_mem_spritesTable[i * 4 + 2] & PPU_SPRITE_FLAGS_VFLIP )?1:0; + scanSprites[spriteCount].palette = ( ppu_mem_spritesTable[i * 4 + 2] & PPU_SPRITE_FLAGS_UPPERCOLOR ) & 0x0F; + scanSprites[spriteCount].priority = ( ppu_mem_spritesTable[i * 4 + 2] & PPU_SPRITE_FLAGS_BGPRIO )?1:0; + + spriteCount++; + } + } + + + ppu_bgColor = ppu_readMemory(0x3F, 00); + /* For each PPU pixel of this scanline */ + for ( i = 0 ; i < 256 ; i++ ) + { + /* Set the current pixel color to the bg color */ + pixelColor = ppu_bgColor; + BgColor = 0; + + /* Compute current pixel bg color if bg is visible */ + if (( ppu_backgroundVisibility == 1 ) && (!getKeyStatus('B'))) + { + if ( ((i < 8) && (!ppu_backgroundClipping)) || (i >= 8)) { - addr = (PPU_Reg_Counter & 0x0C00); - addr = addr | 0x03C0; - addr |= (PPU_Reg_Counter >> 4 ) & 0x0038; - addr |= (PPU_Reg_Counter >> 2 ) & 0x0007; + addr = ( PPU_Reg_Counter & 0x0C00 ); + addr = addr | 0x03C0; + addr |= ( PPU_Reg_Counter >> 4 ) & 0x0038; + addr |= ( PPU_Reg_Counter >> 2 ) & 0x0007; - PPU_Reg_AR = ppu_readMemory(0x20 | ((addr>>8) & 0x0F), addr& 0xFF); + PPU_Reg_AR = ppu_readMemory(0x20 | ( ( addr >> 8 ) & 0x0F ), addr & 0xFF); - PPU_Reg_AR = PPU_Reg_AR >> (((PPU_Reg_Counter >> 4 ) & 0x04)|((PPU_Reg_Counter ) & 0x02)); - PPU_Reg_AR = (PPU_Reg_AR<<2) & 0x0C; + PPU_Reg_AR = PPU_Reg_AR >> ( ( ( PPU_Reg_Counter >> 4 ) & 0x04 ) | ( ( PPU_Reg_Counter ) & 0x02 ) ); + PPU_Reg_AR = ( PPU_Reg_AR << 2 ) & 0x0C; - PPU_Reg_PAR = ppu_readMemory(0x20 | ((PPU_Reg_Counter>>8) & 0x0F), PPU_Reg_Counter& 0xFF); + PPU_Reg_PAR = ppu_readMemory(0x20 | ( ( PPU_Reg_Counter >> 8 ) & 0x0F ), PPU_Reg_Counter & 0xFF); - addr = PPU_Reg_S; - addr |= ((PPU_Reg_PAR & 0xFF) << 4); - addr |= ((PPU_Reg_Counter >> 12) & 0x07); - - value = ppu_readMemory((addr >> 8) , addr ); - BgColor = (value & (1 << (7-(i + PPU_Reg_FH) % 8)))?0x01:0; + addr = PPU_Reg_S; + addr |= ( ( PPU_Reg_PAR & 0xFF ) << 4 ); + addr |= ( ( PPU_Reg_Counter >> 12 ) & 0x07 ); - value = ppu_readMemory((addr >> 8) , addr | 0x08 ); - BgColor |= (value & (1 << (7-(i + PPU_Reg_FH) % 8)))?0x02:0; + value = ppu_readMemory(( addr >> 8 ), addr); + BgColor = ( value & ( 1 << ( 7 - ( i + PPU_Reg_FH ) % 8 ) ) )?0x01:0; - if (BgColor > 0x00) - { - BgColor |= PPU_Reg_AR; - BgColor &= 0x0F; + value = ppu_readMemory(( addr >> 8 ), addr | 0x08); + BgColor |= ( value & ( 1 << ( 7 - ( i + PPU_Reg_FH ) % 8 ) ) )?0x02:0; - pixelColor = ppu_readMemory(0x3F, BgColor); - } + if ( BgColor > 0x00 ) + { + BgColor |= PPU_Reg_AR; + BgColor &= 0x0F; - if (((i + PPU_Reg_FH)%8) == 7) - { - tmp_HHT = ((PPU_Reg_Counter >> 5) & 0x0020) | - (PPU_Reg_Counter & 0x001F); - tmp_HHT = (tmp_HHT + 1) & 0x003F; + pixelColor = ppu_readMemory(0x3F, BgColor); + } - /* Reassemble with HT & H */ - PPU_Reg_Counter = (PPU_Reg_Counter & 0xFBE0) | - ((tmp_HHT & 0x0020) << 5) | - (tmp_HHT & 0x001F); - } + if ( ( ( i + PPU_Reg_FH ) % 8 ) == 7 ) + { + tmp_HHT = ( ( PPU_Reg_Counter >> 5 ) & 0x0020 ) | + ( PPU_Reg_Counter & 0x001F ); + tmp_HHT = ( tmp_HHT + 1 ) & 0x003F; + + /* Reassemble with HT & H */ + PPU_Reg_Counter = ( PPU_Reg_Counter & 0xFBE0 ) | + ( ( tmp_HHT & 0x0020 ) << 5 ) | + ( tmp_HHT & 0x001F ); + } } - - /* Now calculate if there is a sprite here and sprite visibility is on */ - if ((ppu_spriteVisibility == 1) && - (PPU_NbSpriteByScanLine[scanline] != 0)) +#if 0 + // ISPAL + else { - /* scan each sprite on this line to find the one (or more) that is on this pixel */ - for (j = 0; j < PPU_NbSpriteByScanLine[scanline]; j++) - { - /* they are orderer by X, so if this one is too far on the right - it's not need to go further */ - CurrentSprite = PPU_SpriteByScanLine[scanline][j]; + pixelColor = 0x1D; + } +#endif + } - if (PPU_SCANLINESPRITE_GET_X(CurrentSprite) > i) - break; /* break the current for */ + /* Now calculate if there is a sprite here and sprite visibility is on */ + if ( ppu_spriteVisibility == 1 ) + { + if (((!ppu_spriteClipping) && (i < 8)) || (i >= 8)) + { + for( j = 0 ; j < 8 ; j++) + { + spriteData *sprite = &scanSprites[j]; + int8_t spriteRelX; - if ((PPU_SCANLINESPRITE_GET_X(CurrentSprite) + 8) < i) - continue; /* Not this one too (too far on the left) try next one*/ + if ( sprite->inUse == 0 ) + break; - /* Ok if we arrive here, the current sprite is on the good position */ - /* Does the sprite is a BG or FG sprite ? */ - - /* Ok we could now get the sprite current pixel color */ - /* Read sprite scanline pattern */ - SpriteVFlip = PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & PPU_SPRITE_FLAGS_VFLIP; - - if (ppu_spriteSize == 8) + spriteRelX = i - sprite->x; + + if ( ( spriteRelX >= 0 ) && ( spriteRelX < 8 ) ) + { + /* Get sprite tile address */ + if ( ppu_spriteSize == 8 ) + { + addr = ( sprite->tile << 4 ) + ppu_spritePatternTable; + } + else + { + if ( sprite->rel_y < 8 ) { - addr = (PPU_SCANLINESPRITE_GET_TILIDX(CurrentSprite) << 4) + ppu_spritePatternTable; + addr = ( ( ( sprite->tile & 0xFE ) + ( sprite->flip_v?1:0 ) ) << 4 ) + + ( ( sprite->bank )?0x1000:0x0000 ); } else { - if (PPU_SCANLINESPRITE_GET_RELY(CurrentSprite) < 8) - addr = (((PPU_SCANLINESPRITE_GET_TILIDX(CurrentSprite)&0xFE) + (SpriteVFlip?1:0)) << 4) + ((PPU_SCANLINESPRITE_GET_TILIDX(CurrentSprite)&0x01)?0x1000:0x0000); - else - addr = (((PPU_SCANLINESPRITE_GET_TILIDX(CurrentSprite)&0xFE) + (SpriteVFlip?0:1)) << 4) + ((PPU_SCANLINESPRITE_GET_TILIDX(CurrentSprite)&0x01)?0x1000:0x0000); + addr = ( ( ( sprite->tile & 0xFE ) + ( sprite->flip_v?0:1 ) ) << 4 ) + + ( ( sprite->bank )?0x1000:0x0000 ); } - if (SpriteVFlip) - { - addr += 7; - addr -= (PPU_SCANLINESPRITE_GET_RELY(CurrentSprite) % 8); - } - else - addr += (PPU_SCANLINESPRITE_GET_RELY(CurrentSprite) % 8); + } + if ( sprite->flip_v ) + { + addr += 7; + addr -= sprite->rel_y % 8; + } + else + { + addr += sprite->rel_y % 8; + } - if (PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & PPU_SPRITE_FLAGS_HFLIP) + if ( sprite->flip_h ) + { + value = ppu_readMemory(( addr >> 8 ), addr); + SpriteColor = ( value & ( 1 << ( spriteRelX ) ) )?0x01:0; + + value = ppu_readMemory(( addr >> 8 ), addr | 0x08); + SpriteColor |= ( value & ( 1 << ( spriteRelX ) ) )?0x02:0; + } + else + { + value = ppu_readMemory(( addr >> 8 ), addr); + SpriteColor = ( value & ( 1 << ( 7 - ( spriteRelX ) ) ) )?0x01:0; + value = ppu_readMemory(( addr >> 8 ), addr | 0x08); + SpriteColor |= ( value & ( 1 << ( 7 - ( spriteRelX ) ) ) )?0x02:0; + } + + /* If we get a color different from 0, the pixel is not transparent */ + if ( SpriteColor > 0 ) + { + /* Add second part of the colour */ + SpriteColor |= ( ( sprite->palette ) << 2 ); + SpriteColor &= 0x0F; + + if ( j == 0 ) { - value = ppu_readMemory((addr >> 8) , addr ); - SpriteColor = (value & (1 << (i-PPU_SCANLINESPRITE_GET_X(CurrentSprite))))?0x01:0; - - value = ppu_readMemory((addr >> 8) , addr | 0x08 ); - SpriteColor |= (value & (1 << (i-PPU_SCANLINESPRITE_GET_X(CurrentSprite))))?0x02:0; - } - else - { - value = ppu_readMemory((addr >> 8) , addr ); - SpriteColor = (value & (1 << (7-(i-PPU_SCANLINESPRITE_GET_X(CurrentSprite)))))?0x01:0; - - value = ppu_readMemory((addr >> 8) , addr | 0x08 ); - SpriteColor |= (value & (1 << (7-(i-PPU_SCANLINESPRITE_GET_X(CurrentSprite)))))?0x02:0; - } - - if (SpriteColor > 0x00) - { - SpriteColor |= ((PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & PPU_SPRITE_FLAGS_UPPERCOLOR) << 2); - SpriteColor &= 0x0F; - } - - if ((PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & 0x04) && - (SpriteColor != 0x00) && (BgColor != 0x00)) - { - if (!ppu_spriteZeroHit) + /* Sprite 0 */ + if ( ( BgColor != 0 ) && ( !ppu_spriteZeroHit ) ) { - ppu_spriteZeroHit = (ppu_backgroundVisibility)?1:0; - if (ppu_spriteZeroHit) + ppu_spriteZeroHit = ( ppu_backgroundVisibility )?1:0; + if ( ppu_spriteZeroHit ) SZHit = scanline; } } - if ( ( (PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & PPU_SPRITE_FLAGS_BGPRIO) && (BgColor == 0x0000)) || - (!(PPU_SCANLINESPRITE_GET_ATTRS(CurrentSprite) & PPU_SPRITE_FLAGS_BGPRIO)) ) - { - if (SpriteColor != 0x00) pixelColor = ppu_readMemory(0x3F, (0x10 + SpriteColor)); - } + + if ( sprite->priority ) + { + if ( SpriteColor > 0x00 ) + { + if ( BgColor == 0 ) + { + pixelColor = ppu_readMemory(0x3F, ( 0x10 + SpriteColor )); + } + break; + } + } + else + { + if ( SpriteColor != 0x00 ) + { + pixelColor = ppu_readMemory(0x3F, ( 0x10 + SpriteColor )); + break; + } + } + } + } } + } - - /* Set to monochrome if needed */ - if (ppu_displayType) + } + + /* Set to monochrome if needed */ + if ( ppu_displayType ) pixelColor &= 0x30; - - /* draw the pixel */ - _putpixel(VideoBuffer, i, scanline, pixelColor); - } - - if (ppu_backgroundVisibility || ppu_spriteVisibility) - if (PPU_NbSpriteByScanLineOverFlow[scanline] == 1) - ppu_scanlineSpriteOverflow = 1; - - //blit(VideoBuffer, screen, 0, scanline, 0, scanline, 256, 1); - if (ppu_backgroundVisibility == 1) - { + /* draw the pixel */ + graphics_drawpixel(i, scanline, pixelColor); + } - tmp_VVTFV = ((PPU_Reg_Counter >> 3 ) & 0x0100) | /* V */ - ((PPU_Reg_Counter >> 2 ) & 0x00F8) | /* VT */ - ((PPU_Reg_Counter >> 12) & 0x0007); /* FV */ + if (spriteCount > 8) + { + ppu_scanlineSpriteOverflow = 1; + } - tmp_VVTFV++; - if ((tmp_VVTFV&0x0F8) == 0xF0) - { + if ( ppu_backgroundVisibility == 1 ) + { + + tmp_VVTFV = ( ( PPU_Reg_Counter >> 3 ) & 0x0100 ) | /* V */ + ( ( PPU_Reg_Counter >> 2 ) & 0x00F8 ) | /* VT */ + ( ( PPU_Reg_Counter >> 12 ) & 0x0007 ); /* FV */ + + tmp_VVTFV++; + if ( ( tmp_VVTFV & 0x0F8 ) == 0xF0 ) + { tmp_VVTFV &= ~0x0F8; tmp_VVTFV ^= 0x100; - } + } - PPU_Reg_Counter = ( PPU_Reg_Counter & 0x041F) | - ((tmp_VVTFV & 0x0100 ) << 3 ) | /* V */ - ((tmp_VVTFV & 0x00F8 ) << 2 ) | /* VT */ - ((tmp_VVTFV & 0x0007 ) << 12); /* FV */ + PPU_Reg_Counter = ( PPU_Reg_Counter & 0x041F ) | + ( ( tmp_VVTFV & 0x0100 ) << 3 ) | /* V */ + ( ( tmp_VVTFV & 0x00F8 ) << 2 ) | /* VT */ + ( ( tmp_VVTFV & 0x0007 ) << 12 ); /* FV */ - /* Update H & HT */ - PPU_Reg_Counter = (PPU_Reg_Counter & ~0x041F) | - (PPU_Reg_H << 10) | + /* Update H & HT */ + PPU_Reg_Counter = ( PPU_Reg_Counter & ~0x041F ) | + ( PPU_Reg_H << 10 ) | PPU_Reg_HT; } - } - /* Increment only V & VT & FV*/ + } + /* Increment only V, VT & FV*/ /* 8421 8421 8421 8421 @@ -703,109 +661,97 @@ E = HT */ - if (scanline == 239) - { - ppu_inVBlankTime = 1; - textprintf_ex(Buffer, font, 260, 3, 4, 0, "FPS : %ld (CPU@~%2.2fMhz : %d%%)", FPS, (float) (((float) IPS) / 1000000.0), (int) ((((float) IPS) / 1770000.0) * 100.0)); - - blit(VideoBuffer, Buffer, 0, 0, 0, 0, 256, 240); - blit(Buffer, screen, 0, 0, 0, 0, 512+256, 512); + if ( scanline == 239 ) + { + ppu_inVBlankTime = 1; + // textprintf_ex(Buffer, font, 260, 3, 4, 0, "FPS : %ld (CPU@~%2.2fMhz : %d%%)", FPS, (float) (((float) IPS) / 1000000.0), (int) ((((float) IPS) / 1770000.0) * 100.0)); - return ppu_execNMIonVBlank; - } - - if (scanline == SZHit) - { - line(Buffer, 257, scanline, 267, scanline, 0x12); - line(Buffer, 257, scanline, 260, scanline-2, 0x12); - line(Buffer, 257, scanline, 260, scanline+2, 0x12); - } + graphics_blit(0, 0, 256, 240); + return ppu_execNMIonVBlank; + } - if (scanline == IRQScanHit) - { - line(Buffer, 267, scanline, 277, scanline, 0x13); - line(Buffer, 267, scanline, 270, scanline-2, 0x13); - line(Buffer, 267, scanline, 270, scanline+2, 0x13); - } + /* Debug tools */ + /*if ( scanline == SZHit ) + { + graphics_drawline(0, scanline, 256, scanline, 0x12); + } + if ( scanline == IRQScanHit ) + { + graphics_drawline(0, scanline, 256, scanline, 0x13); + }*/ + /* */ - if (key[KEY_B]) - { - blit(VideoBuffer, Buffer, 0, 0, 0, 0, 256, 240); - blit(Buffer, screen, 0, 0, 0, 0, 512 + 256, 480); - } - - if (scanline == (239 + VBLANK_TIME)+0) - { - ppu_inVBlankTime = 0; - ppu_spriteZeroHit = 0; - ppu_scanlineSpriteOverflow = 0; - } - return 0; + if ( scanline == ( 239 + VBLANK_TIME ) + 0 ) + { + ppu_inVBlankTime = 0; + ppu_spriteZeroHit = 0; + ppu_scanlineSpriteOverflow = 0; + } + return 0; } -byte PPU_RegValues[8]; +uint8_t PPU_RegValues[8]; -byte ppu_readReg(byte id) +uint8_t ppu_readReg(uint8_t id) { - id &= 0x07; - static byte garbage; - static byte lastValue; - switch(id) - { - default: - garbage = PPU_RegValues[id]; - break; - case 0x02: /* PPU Status Register */ + id &= 0x07; + static uint8_t garbage; + static uint8_t lastValue; + switch(id) + { + default: + garbage = PPU_RegValues[id]; + break; + case 0x02: /* PPU Status Register */ - /* Reset VRam 2005/2006 flipflop */ - ppu_VramAccessFlipFlop = 0; - garbage = 0; + /* Reset VRam 2005/2006 flipflop */ + ppu_VramAccessFlipFlop = 0; + garbage = 0; - garbage |= (ppu_inVBlankTime!=0) ?PPU_FLAG_SR_VBLANK:0; - garbage |= (ppu_spriteZeroHit!=0) ?PPU_FLAG_SR_SPRT0:0; - garbage |= (ppu_scanlineSpriteOverflow!=0)?PPU_FLAG_SR_8SPRT:0; + garbage |= ( ppu_inVBlankTime != 0 )?PPU_FLAG_SR_VBLANK:0; + garbage |= ( ppu_spriteZeroHit != 0 )?PPU_FLAG_SR_SPRT0:0; + garbage |= ( ppu_scanlineSpriteOverflow != 0 )?PPU_FLAG_SR_8SPRT:0; - ppu_inVBlankTime = 0; - break; + ppu_inVBlankTime = 0; + break; - case 0x04: /* SPR-RAM I/O */ - garbage = ppu_mem_spritesTable[ppu_mem_sptrTablePtr]; - break; + case 0x04: /* SPR-RAM I/O */ + garbage = ppu_mem_spritesTable[ppu_mem_sptrTablePtr]; + break; - case 0x07: /* VRAM I/O */ - if (PPU_Reg_Counter < 0x3F00) - { - garbage = lastValue; - lastValue = ppu_readMemory((PPU_Reg_Counter>>8) & 0x3F, - PPU_Reg_Counter & 0xFF); - } - else - { - lastValue = ppu_readMemory( 0x2F, - PPU_Reg_Counter & 0xFF); - garbage = ppu_readMemory( 0x3F, - PPU_Reg_Counter & 0xFF); - } + case 0x07: /* VRAM I/O */ + if ( PPU_Reg_Counter < 0x3F00 ) + { + garbage = lastValue; + lastValue = ppu_readMemory(( PPU_Reg_Counter >> 8 ) & 0x3F, + PPU_Reg_Counter & 0xFF); + } + else + { + lastValue = ppu_readMemory(0x2F, + PPU_Reg_Counter & 0xFF); + garbage = ppu_readMemory(0x3F, + PPU_Reg_Counter & 0xFF); + } - PPU_Reg_Counter += ppu_addrIncrement; + PPU_Reg_Counter += ppu_addrIncrement; - break; - } - return garbage; + break; + } + return garbage; } -void ppu_writeReg(byte id, byte val) +void ppu_writeReg(uint8_t id, uint8_t val) { - id &= 0x07; - PPU_RegValues[id] = val; - switch(id) - { - default: - break; - - case 0x00: /* PPU Control Register #1 */ + id &= 0x07; + PPU_RegValues[id] = val; + switch(id) + { + default: + break; + case 0x00: /* PPU Control Register #1 */ /* +===============+===============================================+ |2000 | 1 0 4 | @@ -816,39 +762,35 @@ void ppu_writeReg(byte id, byte val) | |+===++=++=++=====++=====+ | +---------------+-----------------------------------------------+ */ - /* Set PPU internal registers */ - PPU_Reg_V = (val & 0x02)?1:0; - PPU_Reg_H = (val & 0x01)?1:0; - PPU_Reg_S = (val & 0x10)?0x1000:0x0000; + /* Set PPU internal registers */ + PPU_Reg_V = ( val & 0x02 )?1:0; + PPU_Reg_H = ( val & 0x01 )?1:0; + PPU_Reg_S = ( val & 0x10 )?0x1000:0x0000; - /* Set Other parameters */ - ppu_addrIncrement = (val & 0x04)?0x20:0x01; - ppu_spritePatternTable = (val & 0x08)?0x1000:0; - ppu_spriteSize = (val & 0x20)?16:8; - ppu_execNMIonVBlank = (val & 0x80)?1:0; - break; + /* Set Other parameters */ + ppu_addrIncrement = ( val & 0x04 )?0x20:0x01; + ppu_spritePatternTable = ( val & 0x08 )?0x1000:0; + ppu_spriteSize = ( val & 0x20 )?16:8; + ppu_execNMIonVBlank = ( val & 0x80 )?1:0; + break; - case 0x01: /* PPU Control Register #2 */ + case 0x01: /* PPU Control Register #2 */ + ppu_spriteVisibility = ( val & 0x10 )?1:0; + ppu_backgroundVisibility = ( val & 0x08 )?1:0; + ppu_spriteClipping = ( val & 0x04 )?0:1; + ppu_backgroundClipping = ( val & 0x02 )?0:1; + ppu_displayType = ( val & 0x01 )?1:0; + break; - ppu_spriteVisibility = (val & 0x10)?1:0; - ppu_backgroundVisibility = (val & 0x08)?1:0; - ppu_spriteClipping = (val & 0x04)?1:0; - ppu_backgroundClipping = (val & 0x02)?1:0; - ppu_displayType = (val & 0x01)?1:0; + case 0x03: /* SPR-RAM Address Register */ + ppu_mem_sptrTablePtr = val; + break; - ppu_updateSpriteScanlineTable(); - break; + case 0x04: /* SPR-RAM I/O */ + ppu_mem_spritesTable[ppu_mem_sptrTablePtr++] = val; + break; - case 0x03: /* SPR-RAM Address Register */ - ppu_mem_sptrTablePtr = val; - break; - - case 0x04: /* SPR-RAM I/O */ - ppu_mem_spritesTable[ppu_mem_sptrTablePtr++] = val; - ppu_updateSpriteScanlineTable(); - break; - - case 0x05: /* 2005 VRAM Register */ + case 0x05: /* 2005 VRAM Register */ /* +===============+===============================================+ |2005/1 | 76543 210 | @@ -860,24 +802,24 @@ void ppu_writeReg(byte id, byte val) | |+===++=++=++=====++=====+ | +---------------+-----------------------------------------------+ */ - if (ppu_VramAccessFlipFlop == 0) - { - ppu_VramAccessFlipFlop = ~0; + if ( ppu_VramAccessFlipFlop == 0 ) + { + ppu_VramAccessFlipFlop = ~0; - PPU_Reg_FH = val & 0x07; - PPU_Reg_HT = (val & 0xF8) >> 3; - } - else - { - ppu_VramAccessFlipFlop = 0; + PPU_Reg_FH = val & 0x07; + PPU_Reg_HT = ( val & 0xF8 ) >> 3; + } + else + { + ppu_VramAccessFlipFlop = 0; - PPU_Reg_FV = val & 0x07; - PPU_Reg_VT = (val & 0xF8) >> 3; - } + PPU_Reg_FV = val & 0x07; + PPU_Reg_VT = ( val & 0xF8 ) >> 3; + } - break; + break; - case 0x06: /* 2006 VRAM Register */ + case 0x06: /* 2006 VRAM Register */ /* +===============+===============================================+ |2006/1 | -54 3 2 10 | @@ -889,27 +831,27 @@ void ppu_writeReg(byte id, byte val) | |+===++=++=++=====++=====+ | +---------------+-----------------------------------------------+ */ - if (ppu_VramAccessFlipFlop == 0) - { - ppu_VramAccessFlipFlop = ~0; + if ( ppu_VramAccessFlipFlop == 0 ) + { + ppu_VramAccessFlipFlop = ~0; - PPU_Reg_FV = (val >> 4) & 0x03; - PPU_Reg_V = (val >> 3) & 0x01; - PPU_Reg_H = (val >> 2) & 0x01; - PPU_Reg_VT = (PPU_Reg_VT & 0x07) | ((val & 0x03) << 3); - } - else - { - ppu_VramAccessFlipFlop = 0; - PPU_Reg_VT = (PPU_Reg_VT & 0x18) | ((val >> 5) & 0x07); - PPU_Reg_HT = val & 0x1F; + PPU_Reg_FV = ( val >> 4 ) & 0x03; + PPU_Reg_V = ( val >> 3 ) & 0x01; + PPU_Reg_H = ( val >> 2 ) & 0x01; + PPU_Reg_VT = ( PPU_Reg_VT & 0x07 ) | ( ( val & 0x03 ) << 3 ); + } + else + { + ppu_VramAccessFlipFlop = 0; + PPU_Reg_VT = ( PPU_Reg_VT & 0x18 ) | ( ( val >> 5 ) & 0x07 ); + PPU_Reg_HT = val & 0x1F; - ppu_updateCounters(); - } + ppu_updateCounters(); + } - break; + break; - case 0x07: /* VRAM I/O */ + case 0x07: /* VRAM I/O */ /* +---------------+-----------------------------------------------+ | |+===++=++=++=====++=====++===++=++========++==+| @@ -921,20 +863,19 @@ void ppu_writeReg(byte id, byte val) +===============+===============================================+ */ - ppu_writeMemory((PPU_Reg_Counter>>8) & 0x3F, PPU_Reg_Counter & 0xFF, val); - PPU_Reg_Counter += ppu_addrIncrement; + ppu_writeMemory(( PPU_Reg_Counter >> 8 ) & 0x3F, PPU_Reg_Counter & 0xFF, val); + PPU_Reg_Counter += ppu_addrIncrement; - break; - } + break; + } } -void ppu_fillSprRamDMA(byte value) +void ppu_fillSprRamDMA(uint8_t value) { - short i; - byte *ptr = get_page_ptr(value); - for (i = 0; i < 0x100; i++) - { - ppu_mem_spritesTable[(ppu_mem_sptrTablePtr + i)&0xFF] = *(ptr+i); - } - ppu_updateSpriteScanlineTable(); + short i; + uint8_t *ptr = get_page_ptr(value); + for ( i = 0 ; i < 0x100 ; i++ ) + { + ppu_mem_spritesTable[( ppu_mem_sptrTablePtr + i ) & 0xFF] = *( ptr + i ); + } } diff --git a/src/ppu/ppu.memory.c b/src/ppu/ppu.memory.c index fdb2f61..1ef7981 100644 --- a/src/ppu/ppu.memory.c +++ b/src/ppu/ppu.memory.c @@ -3,7 +3,7 @@ * ppu.memory.c - Inspired from the memory manager of the Quick6502 Project. * * Created by Manoel Trapier on 12/04/07. - * Copyright 2003-2008 986 Corp. All rights reserved. + * Copyright (c) 2003-2016 986-Studio. All rights reserved. * * $LastChangedDate$ * $Author$