From 83ed23890c6870bc9dfeaeb4d9d3fdecaeb3fe2d Mon Sep 17 00:00:00 2001 From: Tommy Thorn Date: Sat, 6 Dec 2008 17:54:48 -0800 Subject: [PATCH] SDL: Ported to YARI There appears to be a bug when invoking SDL_Quit() - it tries to free the hardware surface and obviously fails. --- SDL-1.2/Makefile.yari | 112 ++++ SDL-1.2/include/SDL_config.h | 2 + SDL-1.2/include/SDL_config.h.default | 2 + SDL-1.2/include/SDL_config_yari.h | 114 ++++ SDL-1.2/include/SDL_platform.h | 4 + SDL-1.2/src/timer/yari/SDL_systimer.c | 91 +++ SDL-1.2/src/timer/yari/yari_hwr.c | 28 + SDL-1.2/src/video/SDL_sysvideo.h | 3 + SDL-1.2/src/video/SDL_video.c | 3 + SDL-1.2/src/video/nds/SDL_ndsvideo.c | 993 ++++++++++++++++----------------- SDL-1.2/src/video/yari/SDL_yarivideo.c | 265 +++++++++ 11 files changed, 1117 insertions(+), 500 deletions(-) create mode 100644 SDL-1.2/Makefile.yari create mode 100644 SDL-1.2/include/SDL_config_yari.h create mode 100644 SDL-1.2/src/timer/yari/SDL_systimer.c create mode 100644 SDL-1.2/src/timer/yari/yari_hwr.c rewrite SDL-1.2/src/video/nds/SDL_ndsvideo.c (74%) create mode 100644 SDL-1.2/src/video/yari/SDL_yarivideo.c diff --git a/SDL-1.2/Makefile.yari b/SDL-1.2/Makefile.yari new file mode 100644 index 0000000..0dd0d1e --- /dev/null +++ b/SDL-1.2/Makefile.yari @@ -0,0 +1,112 @@ +CC = mips-elf-gcc -msoft-float -g +AR = mips-elf-ar + +TARGET = libSDL.a +SDLMAIN_TARGET = libSDLmain.a + +CFLAGS=$(KOS_CFLAGS) $(DEFS) -Iinclude -O3 + +SRCS = \ + src/audio/dummy/SDL_dummyaudio.c \ + src/audio/SDL_audio.c \ + src/audio/SDL_audiocvt.c \ + src/audio/SDL_audiodev.c \ + src/audio/SDL_mixer.c \ + src/audio/SDL_wave.c \ + src/cdrom/dummy/SDL_syscdrom.c \ + src/cdrom/SDL_cdrom.c \ + src/events/SDL_active.c \ + src/events/SDL_events.c \ + src/events/SDL_expose.c \ + src/events/SDL_keyboard.c \ + src/events/SDL_mouse.c \ + src/events/SDL_quit.c \ + src/events/SDL_resize.c \ + src/file/SDL_rwops.c \ + src/joystick/dummy/SDL_sysjoystick.c \ + src/joystick/SDL_joystick.c \ + src/loadso/dummy/SDL_sysloadso.c \ + src/SDL.c \ + src/SDL_error.c \ + src/SDL_fatal.c \ + src/stdlib/SDL_getenv.c \ + src/stdlib/SDL_iconv.c \ + src/stdlib/SDL_malloc.c \ + src/stdlib/SDL_qsort.c \ + src/stdlib/SDL_stdlib.c \ + src/stdlib/SDL_string.c \ + src/thread/generic/SDL_syscond.c \ + src/thread/generic/SDL_sysmutex.c \ + src/thread/generic/SDL_syssem.c \ + src/thread/generic/SDL_systhread.c \ + src/thread/SDL_thread.c \ + src/timer/yari/yari_hwr.c \ + src/timer/yari/SDL_systimer.c \ + src/timer/SDL_timer.c \ + src/video/yari/SDL_yarivideo.c \ + src/video/dummy/SDL_nullevents.c \ + src/video/dummy/SDL_nullmouse.c \ + src/video/dummy/SDL_nullvideo.c \ + src/video/SDL_blit.c \ + src/video/SDL_blit_0.c \ + src/video/SDL_blit_1.c \ + src/video/SDL_blit_A.c \ + src/video/SDL_blit_N.c \ + src/video/SDL_bmp.c \ + src/video/SDL_cursor.c \ + src/video/SDL_gamma.c \ + src/video/SDL_pixels.c \ + src/video/SDL_RLEaccel.c \ + src/video/SDL_stretch.c \ + src/video/SDL_surface.c \ + src/video/SDL_video.c \ + src/video/SDL_yuv.c \ + src/video/SDL_yuv_sw.c \ + src/cpuinfo/SDL_cpuinfo.c + +OBJS = $(SRCS:.c=.o) + +SDLMAIN_SRC = src/main/dummy/SDL_dummy_main.c +SDLMAIN_OBJ = $(SDLMAIN_SRC:.c=.o) + + +TEST = \ + test/checkkeys.c \ + test/graywin.c \ + test/loopwave.c \ + test/testalpha.c \ + test/testbitmap.c \ + test/testcdrom.c \ + test/testerror.c \ + test/testgamma.c \ + test/testgl.c \ + test/testhread.c \ + test/testjoystick.c \ + test/testkeys.c \ + test/testlock.c \ + test/testoverlay.c \ + test/testpalette.c \ + test/testsem.c \ + test/testsprite.c \ + test/testtimer.c \ + test/testtypes.c \ + test/testver.c \ + test/testvidinfo.c \ + test/testwin.c \ + test/testwm.c \ + test/threadwin.c \ + test/torturethread.c + +all: $(TARGET) $(SDLMAIN_TARGET) + +$(TARGET): copy_config $(OBJS) Makefile.yari + $(AR) rcs $(TARGET) $(OBJS) + +$(SDLMAIN_TARGET): $(SDLMAIN_OBJ) Makefile.yari + $(AR) rcs $(SDLMAIN_TARGET) $(SDLMAIN_OBJ) + +copy_config: + @cp include/SDL_config.h.default include/SDL_config.h + +clean: + rm -f include/SDL_config.h $(OBJS) diff --git a/SDL-1.2/include/SDL_config.h b/SDL-1.2/include/SDL_config.h index c82f42a..21d21c0 100644 --- a/SDL-1.2/include/SDL_config.h +++ b/SDL-1.2/include/SDL_config.h @@ -38,6 +38,8 @@ #include "SDL_config_win32.h" #elif defined(__OS2__) #include "SDL_config_os2.h" +#elif defined(__YARI__) +#include "SDL_config_yari.h" #else #include "SDL_config_minimal.h" #endif /* platform config */ diff --git a/SDL-1.2/include/SDL_config.h.default b/SDL-1.2/include/SDL_config.h.default index c82f42a..21d21c0 100644 --- a/SDL-1.2/include/SDL_config.h.default +++ b/SDL-1.2/include/SDL_config.h.default @@ -38,6 +38,8 @@ #include "SDL_config_win32.h" #elif defined(__OS2__) #include "SDL_config_os2.h" +#elif defined(__YARI__) +#include "SDL_config_yari.h" #else #include "SDL_config_minimal.h" #endif /* platform config */ diff --git a/SDL-1.2/include/SDL_config_yari.h b/SDL-1.2/include/SDL_config_yari.h new file mode 100644 index 0000000..67a0f7b --- /dev/null +++ b/SDL-1.2/include/SDL_config_yari.h @@ -0,0 +1,114 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2006 Sam Lantinga + + 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 + + Sam Lantinga + slouken@libsdl.org +*/ + +#ifndef _SDL_config_yari_h +#define _SDL_config_yari_h + +/* This is a set of defines to configure the SDL features */ + +/* General platform specific identifiers */ +#include "SDL_platform.h" + +/* C datatypes */ +#define SDL_HAS_64BIT_TYPE 1 + +/* Endianness */ +#define SDL_BYTEORDER 4321 + +/* Useful headers */ +#define HAVE_ALLOCA_H 1 +#define HAVE_SYS_TYPES_H 1 +#define HAVE_STDIO_H 1 +#define STDC_HEADERS 1 +#define HAVE_STDLIB_H 1 +#define HAVE_STDARG_H 1 +#define HAVE_MALLOC_H 1 +#define HAVE_STRING_H 1 +#define HAVE_INTTYPES_H 1 +#define HAVE_STDINT_H 1 +#define HAVE_CTYPE_H 1 +#define HAVE_MATH_H 1 +#define HAVE_ICONV_H 1 +#define HAVE_SIGNAL_H 1 + +/* C library functions */ +#define HAVE_MALLOC 1 +#define HAVE_CALLOC 1 +#define HAVE_REALLOC 1 +#define HAVE_FREE 1 +#define HAVE_ALLOCA 1 +#define HAVE_GETENV 1 +#define HAVE_PUTENV 1 +#define HAVE_UNSETENV 1 +#define HAVE_QSORT 1 +#define HAVE_ABS 1 +#define HAVE_BCOPY 1 +#define HAVE_MEMSET 1 +#define HAVE_MEMCPY 1 +#define HAVE_MEMMOVE 1 +#define HAVE_STRLEN 1 +#define HAVE_STRLCPY 1 +#define HAVE_STRLCAT 1 +#define HAVE_STRDUP 1 +#define HAVE_STRCHR 1 +#define HAVE_STRRCHR 1 +#define HAVE_STRSTR 1 +#define HAVE_STRTOL 1 +#define HAVE_STRTOUL 1 +#define HAVE_STRTOLL 1 +#define HAVE_STRTOULL 1 +#define HAVE_ATOI 1 +#define HAVE_ATOF 1 +#define HAVE_STRCMP 1 +#define HAVE_STRNCMP 1 +#define HAVE_STRCASECMP 1 +#define HAVE_STRNCASECMP 1 +#define HAVE_SSCANF 1 +#define HAVE_SNPRINTF 1 +#define HAVE_VSNPRINTF 1 +#define HAVE_SETJMP 1 + +/* Enable various audio drivers */ +#define SDL_AUDIO_DRIVER_YARI 1 +#define SDL_AUDIO_DRIVER_DUMMY 1 + +/* Enable the stub cdrom driver (src/cdrom/dummy/\*.c) */ +#define SDL_CDROM_DISABLED 1 + +/* Enable the stub joystick driver (src/joystick/dummy/\*.c) */ +#define SDL_JOYSTICK_DISABLED 1 + +/* Enable the stub shared object loader (src/loadso/dummy/\*.c) */ +#define SDL_LOADSO_DISABLED 1 + +/* Enable the stub thread support (src/thread/generic/\*.c) */ +#define SDL_THREADS_DISABLED 1 + +/* Enable various timer systems */ +//#define SDL_TIMER_YARI 1 +#define SDL_TIMER_DUMMY 1 + +/* Enable various video drivers */ +#define SDL_VIDEO_DRIVER_YARI 1 +#define SDL_VIDEO_DRIVER_DUMMY 1 + +#endif /* _SDL_config_yari_h */ diff --git a/SDL-1.2/include/SDL_platform.h b/SDL-1.2/include/SDL_platform.h index 1bfee29..9c83fb7 100644 --- a/SDL-1.2/include/SDL_platform.h +++ b/SDL-1.2/include/SDL_platform.h @@ -96,5 +96,9 @@ #undef __WIN32__ #define __WIN32__ 1 #endif +#if defined(R3000) +#undef __YARI__ +#define __YARI__ 1 +#endif #endif /* _SDL_platform_h */ diff --git a/SDL-1.2/src/timer/yari/SDL_systimer.c b/SDL-1.2/src/timer/yari/SDL_systimer.c new file mode 100644 index 0000000..739f3dd --- /dev/null +++ b/SDL-1.2/src/timer/yari/SDL_systimer.c @@ -0,0 +1,91 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2006 Sam Lantinga + + 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 + + Sam Lantinga + slouken@libsdl.org +*/ + +/* YARI's minimal timer implementation is based on that from NDS */ +#include "SDL_config.h" + +#include "SDL_thread.h" +#include "SDL_timer.h" +#include "SDL_error.h" +#include "../SDL_timer_c.h" + +uint64_t yari_rdhwr_TSC(void); + +static uint32_t scale, counter_overflow; +static uint64_t start_TSC, prev_TSC; + +uint32_t mips32_rdhwr_counter(void); +uint32_t mips32_rdhwr_cycles_pr_count(void); + + +void SDL_StartTicks(void) +{ + uint32_t khz_frequency = 50*1000; + uint32_t cycles_pr_count = mips32_rdhwr_cycles_pr_count(); + scale = (1ULL << 32) * cycles_pr_count / khz_frequency; + counter_overflow = 0; + start_TSC = mips32_rdhwr_counter(); +} + +/* + * Get the number of milliseconds since the SDL library + * initialization. Note that this value wraps if the program runs for + * more than ~49 days + */ +Uint32 SDL_GetTicks(void) +{ + uint64_t t = mips32_rdhwr_counter(); + + counter_overflow += t < prev_TSC; // Happens practically never + prev_TSC = t; + t += ((uint64_t) counter_overflow << 32) - start_TSC; + + return t * scale >> 32; +} + +void SDL_Delay(Uint32 delay_ms) +{ + Uint32 t0 = SDL_GetTicks(); + + while (SDL_GetTicks() < t0 + delay_ms) + SDL_PumpEvents(); // Improve responsiveness at the expense of accuracy +} + +/* This is only called if the event thread is not running */ +int SDL_SYS_TimerInit(void) +{ + return 0; +} + +void SDL_SYS_TimerQuit(void) +{ +} + +int SDL_SYS_StartTimer(void) +{ + SDL_SetError("Timers not implemented on YARI"); + return -1; +} + +void SDL_SYS_StopTimer(void) +{ +} diff --git a/SDL-1.2/src/timer/yari/yari_hwr.c b/SDL-1.2/src/timer/yari/yari_hwr.c new file mode 100644 index 0000000..177c41b --- /dev/null +++ b/SDL-1.2/src/timer/yari/yari_hwr.c @@ -0,0 +1,28 @@ +#include + +#define MIPS32_HWR_COUNTER 2 +#define MIPS32_HWR_CYCLES_PR_COUNT 3 + +uint32_t mips32_rdhwr_counter(void) +{ + uint32_t v; + + asm volatile(".set push;" + ".set mips32r2;" + "rdhwr %0,$%1;" + ".set pop" : "=r" (v) : "i" (MIPS32_HWR_COUNTER)); + + return v; +} + +uint32_t mips32_rdhwr_cycles_pr_count(void) +{ + uint32_t v; + + asm volatile(".set push;" + ".set mips32r2;" + "rdhwr %0,$%1;" + ".set pop" : "=r" (v) : "i" (MIPS32_HWR_CYCLES_PR_COUNT)); + + return v; +} diff --git a/SDL-1.2/src/video/SDL_sysvideo.h b/SDL-1.2/src/video/SDL_sysvideo.h index 403fe9b..99d5d39 100644 --- a/SDL-1.2/src/video/SDL_sysvideo.h +++ b/SDL-1.2/src/video/SDL_sysvideo.h @@ -401,6 +401,9 @@ extern VideoBootStrap RISCOS_bootstrap; #if SDL_VIDEO_DRIVER_OS2FS extern VideoBootStrap OS2FSLib_bootstrap; #endif +#if SDL_VIDEO_DRIVER_YARI +extern VideoBootStrap YARI_bootstrap; +#endif #if SDL_VIDEO_DRIVER_AALIB extern VideoBootStrap AALIB_bootstrap; #endif diff --git a/SDL-1.2/src/video/SDL_video.c b/SDL-1.2/src/video/SDL_video.c index 54d31ae..b68c947 100644 --- a/SDL-1.2/src/video/SDL_video.c +++ b/SDL-1.2/src/video/SDL_video.c @@ -111,6 +111,9 @@ static VideoBootStrap *bootstrap[] = { #if SDL_VIDEO_DRIVER_NDS &NDS_bootstrap, #endif +#if SDL_VIDEO_DRIVER_YARI + &YARI_bootstrap, +#endif #if SDL_VIDEO_DRIVER_RISCOS &RISCOS_bootstrap, #endif diff --git a/SDL-1.2/src/video/nds/SDL_ndsvideo.c b/SDL-1.2/src/video/nds/SDL_ndsvideo.c dissimilarity index 74% index 1bdbb1a..ec377bd 100644 --- a/SDL-1.2/src/video/nds/SDL_ndsvideo.c +++ b/SDL-1.2/src/video/nds/SDL_ndsvideo.c @@ -1,500 +1,493 @@ -/* - SDL - Simple DirectMedia Layer - Copyright (C) 1997-2006 Sam Lantinga - - 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 - - Sam Lantinga - slouken@libsdl.org -*/ -#include "SDL_config.h" - -#include -#include -#include "SDL.h" -#include "SDL_error.h" -#include "SDL_video.h" -#include "SDL_mouse.h" -#include "../SDL_sysvideo.h" -#include "../SDL_pixels_c.h" -#include "../../events/SDL_events_c.h" - -#include "SDL_ndsvideo.h" -#include "SDL_ndsevents_c.h" -#include "SDL_ndsmouse_c.h" - -#define NDSVID_DRIVER_NAME "nds" - -/* Initialization/Query functions */ -static int NDS_VideoInit(_THIS, SDL_PixelFormat *vformat); -static SDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); -static SDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); -static int NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); -static void NDS_VideoQuit(_THIS); - -/* Hardware surface functions */ -static int NDS_AllocHWSurface(_THIS, SDL_Surface *surface); -static int NDS_LockHWSurface(_THIS, SDL_Surface *surface); -static int NDS_FlipHWSurface(_THIS, SDL_Surface *surface); -static void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface); -static void NDS_FreeHWSurface(_THIS, SDL_Surface *surface); - -/* etc. */ -static void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects); - -/* NDS driver bootstrap functions */ - -static int NDS_Available(void) -{ - return(1); -} - -static void NDS_DeleteDevice(SDL_VideoDevice *device) -{ - SDL_free(device->hidden); - SDL_free(device); -} - -void on_irq_vblank() -{ - // Disable interrupts - //REG_IME = 0; - scanKeys(); - - // VBLANK_INTR_WAIT_FLAGS |= IRQ_VBLANK; - // REG_IF |= IRQ_VBLANK; - //REG_IF = REG_IF; - - // Enable interrupts - //REG_IME = 1; -} - -static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, - SDL_Surface *dst, SDL_Rect *dstrect) - { - return 0; - } - -static int CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst) -{ - if (src->flags & SDL_SRCALPHA) return false; - if (src->flags & SDL_SRCCOLORKEY) return false; - if (src->flags & SDL_HWPALETTE ) return false; - if (dst->flags & SDL_SRCALPHA) return false; - if (dst->flags & SDL_SRCCOLORKEY) return false; - if (dst->flags & SDL_HWPALETTE ) return false; - - if (src->format->BitsPerPixel != dst->format->BitsPerPixel) return false; - if (src->format->BytesPerPixel != dst->format->BytesPerPixel) return false; - - src->map->hw_blit = HWAccelBlit; - return true; -} - -static SDL_VideoDevice *NDS_CreateDevice(int devindex) -{ - SDL_VideoDevice *device=0; - - - /* Initialize all variables that we clean on shutdown */ - device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); - if ( device ) { - SDL_memset(device, 0, (sizeof *device)); - device->hidden = (struct SDL_PrivateVideoData *) - SDL_malloc((sizeof *device->hidden)); - } - if ( (device == NULL) || (device->hidden == NULL) ) { - SDL_OutOfMemory(); - if ( device ) { - SDL_free(device); - } - return(0); - } - SDL_memset(device->hidden, 0, (sizeof *device->hidden)); - - /* Set the function pointers */ - device->VideoInit = NDS_VideoInit; - device->ListModes = NDS_ListModes; - device->SetVideoMode = NDS_SetVideoMode; - device->CreateYUVOverlay = NULL; - device->SetColors = NDS_SetColors; - device->UpdateRects = NDS_UpdateRects; - device->VideoQuit = NDS_VideoQuit; - device->AllocHWSurface = NDS_AllocHWSurface; - device->CheckHWBlit = CheckHWBlit; - device->FillHWRect = NULL; - device->SetHWColorKey = NULL; - device->SetHWAlpha = NULL; - device->LockHWSurface = NDS_LockHWSurface; - device->UnlockHWSurface = NDS_UnlockHWSurface; - device->FlipHWSurface = NDS_FlipHWSurface; - device->FreeHWSurface = NDS_FreeHWSurface; - device->SetCaption = NULL; - device->SetIcon = NULL; - device->IconifyWindow = NULL; - device->GrabInput = NULL; - device->GetWMInfo = NULL; - device->InitOSKeymap = NDS_InitOSKeymap; - device->PumpEvents = NDS_PumpEvents; - device->info.blit_hw=1; - - device->free = NDS_DeleteDevice; - return device; -} - -VideoBootStrap NDS_bootstrap = { - NDSVID_DRIVER_NAME, "SDL NDS video driver", - NDS_Available, NDS_CreateDevice -}; - - u16* frontBuffer;// = (u16*)(0x06000000); - u16* backBuffer;// = (u16*)(0x06000000 + 256 * 256 * 2); -int NDS_VideoInit(_THIS, SDL_PixelFormat *vformat) -{ - //printf("WARNING: You are using the SDL NDS video driver!\n"); - - /* Determine the screen depth (use default 8-bit depth) */ - /* we change this during the SDL_SetVideoMode implementation... */ - vformat->BitsPerPixel = 16; // mode 3 - vformat->BytesPerPixel = 2; - vformat->Rmask = 0x0000f800; - vformat->Gmask = 0x000007e0; - vformat->Bmask = 0x0000001f; - powerON(POWER_ALL); - irqInit(); - irqSet(IRQ_VBLANK, on_irq_vblank); - irqEnable(IRQ_VBLANK); - - //set the mode for 2 text layers and two extended background layers - //videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE); - videoSetMode(MODE_6_2D| DISPLAY_BG2_ACTIVE); - - //set the sub background up for text display (we could just print to one - //of the main display text backgrounds just as easily - videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text - - //set the first two banks as background memory and the third as sub background memory - //D is not used..if you need a bigger background then you will need to map - //more vram banks consecutivly (VRAM A-D are all 0x20000 bytes in size) - //vramSetMainBanks(VRAM_A_MAIN_BG_0x6000000, VRAM_B_MAIN_BG_0x6020000,VRAM_C_SUB_BG , VRAM_D_LCD); - vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG); - //vramSetBankA(VRAM_A_MAIN_BG); - //vramSetBankB(VRAM_B_MAIN_BG); - //vramSetBankC(VRAM_C_MAIN_BG); - //vramSetBankD(VRAM_D_MAIN_BG); - //vramSetBankE(VRAM_E_MAIN_BG); - //vramSetBankF(VRAM_F_MAIN_BG); - //vramSetBankG(VRAM_G_MAIN_BG); - vramSetBankH(VRAM_H_SUB_BG); - vramSetBankI(VRAM_I_LCD); - - ////////////////set up text background for text///////////////////// - SUB_BG0_CR = BG_MAP_BASE(8); - - BG_PALETTE_SUB[255] = RGB15(31,31,31);//by default font will be rendered with color 255 - ///////////////set up our bitmap background/////////////////////// - - //BG3_CR = BG_BMP16_512x512; - - //these are rotation backgrounds so you must set the rotation attributes: - //these are fixed point numbers with the low 8 bits the fractional part - //this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap - /* BG3_XDX = 1<<8; - BG3_XDY = 0; - BG3_YDX = 0; - BG3_YDY = 1<<8; - //our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2 - BG3_CX = 0; - BG3_CY = 0; - */ - //consoleInit() is a lot more flexible but this gets you up and running quick - consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(8), (u16*)CHAR_BASE_BLOCK_SUB(0), 16); - - - frontBuffer =(u16*)(0x06000000); - //backBuffer =(u16*)(0x06000000 + 1024 * 512*2); - - //lcdSwap(); - /* We're done! */ - return(0); -} - -SDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) -{ - return (SDL_Rect **) -1; -} - -SDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current, - int width, int height, int bpp, Uint32 flags) -{ - Uint32 Rmask, Gmask, Bmask, Amask; - - //if(width > 1024 || height > 512 || bpp > 16) - // return(NULL); - - if(bpp >8) { - bpp=16; - Rmask = 0x0000001F; - Gmask = 0x000003E0; - Bmask = 0x00007C00; - Amask = 0x00008000; - - videoSetMode(MODE_5_2D| DISPLAY_BG2_ACTIVE); - - vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG); - - BG2_CR = BG_BMP16_512x512; - BG2_XDX = ((width / 256) << 8) | (width % 256) ; - BG2_XDY = 0; - BG2_YDX = 0; - BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ; - BG2_CX = 0; - BG2_CY = 0; -// for (i=0;i<256*192;i++) -// frontBuffer[i] = RGB15(31,0,0)|BIT(15); - } - else - if(bpp <= 8) { - bpp=8; - Rmask = 0x00000000; - Gmask = 0x00000000; - Bmask = 0x00000000; - BG2_CR = BG_BMP8_1024x512; - BG2_XDX = ((width / 256) << 8) | (width % 256) ; - BG2_XDY = 0; - BG2_YDX = 0; - BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ; - - } - else - if(bpp < 15) bpp=15; - if(width<=256) width=256; - else - if(width<256) width=256; - if(height<=192) height=192; - else - if(height<192) height=192; - - if(bpp==8) - { - if(width<256) width=256; - if(height<192) height=192; - this->hidden->ndsmode=4; - } - - if(bpp==15) - { - if(width<256) this->hidden->ndsmode=5; - else this->hidden->ndsmode=3; - } - - this->hidden->buffer= frontBuffer;//NDS_VRAM_BASE; - - //NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode)|NDS_DISP_BG2; - - //fprintf(stderr,"Setting mode %dx%d (ndsmode %d)\n", width, height,this->hidden->ndsmode); - - // FIXME: How do I tell that 15 bits mode is 555? - - SDL_memset(this->hidden->buffer, 0, 1024 * 512* ((this->hidden->ndsmode==4 || this->hidden->ndsmode==5) ? 2 : 1 ) * ((bpp+7) / 8)); - - /* Allocate the new pixel format for the screen */ - if ( ! SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, Amask) ) { - this->hidden->buffer = NULL; - SDL_SetError("Couldn't allocate new pixel format for requested mode"); - return(NULL); - } - - /* Set up the new mode framebuffer */ - current->flags = flags | SDL_FULLSCREEN | SDL_HWSURFACE | (this->hidden->ndsmode > 0 ? SDL_DOUBLEBUF : 0); - this->hidden->w = current->w = width; - this->hidden->h = current->h = height; - current->pixels = frontBuffer; - - if (flags & SDL_DOUBLEBUF) { - this->hidden->secondbufferallocd=1; - backBuffer=(u16*)SDL_malloc(1024*512*2); - current->pixels = backBuffer; - } - if(bpp==8) - current->pitch =1024; - else - current->pitch =512*2; - - /* We're done */ - return(current); -} - -static int NDS_AllocHWSurface(_THIS, SDL_Surface *surface) -{ - if(this->hidden->secondbufferallocd) { - //printf("double double buffer alloc\n"); - return -1; - } - //if(this->hidden->ndsmode==3) - //{ - // printf("no 2nd buffer in mode3\n"); - // return -1; - //} - //printf("second buffer\n"); - //this->hidden->secondbufferallocd=1; - //backBuffer=(u16*)malloc(1024*512*2); - //surface->pixels = backBuffer; - - return(0); -} -static void NDS_FreeHWSurface(_THIS, SDL_Surface *surface) -{ - //free(backBuffer); - this->hidden->secondbufferallocd=0; -} -int z=0; -/* We need to wait for vertical retrace on page flipped displays */ -static int NDS_LockHWSurface(_THIS, SDL_Surface *surface) -{ -/* - uint8* a = surface->pixels; - int i,j; - a += 5 * SCREEN_WIDTH + 5; - for( i = 0; i < 195; ++i) { - uint16* line = a + (SCREEN_WIDTH * i); - for( j = 0; j < 158; ++j) { - *line++ = RGB15(155,155,25); - } - } -*/ - //if (z <256) - // BG_PALETTE[z++]=RGB15(255-z,z,255-z); - - - return(0); -} - -static void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface) -{ - return; -} - -static int NDS_FlipHWSurface(_THIS, SDL_Surface *surface) -{ - if(this->hidden->secondbufferallocd){ - while(DISP_Y!=192); - while(DISP_Y==192); - //printf("flip"); - - dmaCopyAsynch(backBuffer,frontBuffer,1024*512); - } - //printf("flip\n"); - //u16* temp = surface->pixels; - //surface->pixels = frontBuffer; - //frontBuffer = temp; - /* u8* vram=BG_GFX; - int x,y; - for(y = 0; y < 512; y++) - dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512); - //unsigned char buf; - - //printf("NDS_FlipHWSurface\n"); - //printf("ptr now: 0x%x\n",surface->pixels); - - while(DISP_Y!=192); - while(DISP_Y==192); - //swap - u16* temp = frontBuffer; - frontBuffer = backBuffer; - backBuffer = temp; - - //flip - //base is 16KB and screen size is 256x256x2 (128KB) - BG2_CR ^= BG_BMP_BASE( 512 / 16 ); */ -/* - if(surface->pixels == frontBuffer)//NDS_VRAM_BASE) - { - while(DISP_Y!=192); - while(DISP_Y==192); - //swap - u16* temp = backBuffer; - backBuffer = frontBuffer; - frontBuffer = temp; - - //flip - //base is 16KB and screen size is 256x256x2 (128KB) - BG3_CR ^= BG_BMP_BASE( 128 / 16 ); - } - else - { - - while(DISP_Y!=192); - while(DISP_Y==192); - //swap - u16* temp = frontBuffer; - frontBuffer = backBuffer; - backBuffer = temp; - - //flip - //base is 16KB and screen size is 256x256x2 (128KB) - BG3_CR ^= BG_BMP_BASE( 128 / 16 ); - - } - */ - //printf("ptr then: 0x%x\n",surface->pixels); - - //printf("setting dispcnt to 0x%x\n",NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode)|NDS_DISP_BG2| buf); - return(0); -} - -static void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects) -{ - //fprintf(stderr,"update\n"); - /* do nothing. */ - //dmaCopy(frontBuffer,BG_GFX,512*512); - /* - u8* vram=(u8*)BG_GFX; - int x,y; - for(y = 0; y < 512; y++) - dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512); - */ - -} - -int NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) -{ - //printf("SetColors\n"); - short r,g,b; - - if(this->hidden->ndsmode != 4) - { - printf("This is not a palettized mode\n"); - return -1; - } - - int i,j=firstcolor+ncolors; - for(i=firstcolor;i>3; - g=colors[i].g>>3; - b=colors[i].b>>3; - BG_PALETTE[i]=RGB15(r, g, b); - } - - return(0); -} - -/* Note: If we are terminated, this could be called in the middle of - another SDL video routine -- notably UpdateRects. -*/ -void NDS_VideoQuit(_THIS) -{ -} +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2006 Sam Lantinga + + 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 + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#include +#include +#include "SDL.h" +#include "SDL_error.h" +#include "SDL_video.h" +#include "SDL_mouse.h" +#include "../SDL_sysvideo.h" +#include "../SDL_pixels_c.h" +#include "../../events/SDL_events_c.h" + +#include "SDL_ndsvideo.h" +#include "SDL_ndsevents_c.h" +#include "SDL_ndsmouse_c.h" + +#define NDSVID_DRIVER_NAME "nds" + +/* Initialization/Query functions */ +static int NDS_VideoInit(_THIS, SDL_PixelFormat *vformat); +static SDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); +static SDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); +static int NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); +static void NDS_VideoQuit(_THIS); + +/* Hardware surface functions */ +static int NDS_AllocHWSurface(_THIS, SDL_Surface *surface); +static int NDS_LockHWSurface(_THIS, SDL_Surface *surface); +static int NDS_FlipHWSurface(_THIS, SDL_Surface *surface); +static void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface); +static void NDS_FreeHWSurface(_THIS, SDL_Surface *surface); + +/* etc. */ +static void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects); + +/* NDS driver bootstrap functions */ + +static int NDS_Available(void) +{ + return 1; +} + +static void NDS_DeleteDevice(SDL_VideoDevice *device) +{ + SDL_free(device->hidden); + SDL_free(device); +} + +void on_irq_vblank() +{ + // Disable interrupts + //REG_IME = 0; + scanKeys(); + + // VBLANK_INTR_WAIT_FLAGS |= IRQ_VBLANK; + // REG_IF |= IRQ_VBLANK; + //REG_IF = REG_IF; + + // Enable interrupts + //REG_IME = 1; +} + +static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, + SDL_Surface *dst, SDL_Rect *dstrect) +{ + return 0; +} + +static int CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst) +{ + if (src->flags & SDL_SRCALPHA) return false; + if (src->flags & SDL_SRCCOLORKEY) return false; + if (src->flags & SDL_HWPALETTE) return false; + if (dst->flags & SDL_SRCALPHA) return false; + if (dst->flags & SDL_SRCCOLORKEY) return false; + if (dst->flags & SDL_HWPALETTE) return false; + + if (src->format->BitsPerPixel != dst->format->BitsPerPixel) return false; + if (src->format->BytesPerPixel != dst->format->BytesPerPixel) return false; + + src->map->hw_blit = HWAccelBlit; + return true; +} + +static SDL_VideoDevice *NDS_CreateDevice(int devindex) +{ + SDL_VideoDevice *device = 0; + + + /* Initialize all variables that we clean on shutdown */ + device = (SDL_VideoDevice *)SDL_malloc(sizeof *device); + if (device) { + SDL_memset(device, 0, sizeof *device); + device->hidden = (struct SDL_PrivateVideoData *) + SDL_malloc(sizeof *device->hidden); + } + + if (!device || !device->hidden) { + SDL_OutOfMemory(); + if (device) + SDL_free(device); + return 0; + } + SDL_memset(device->hidden, 0, sizeof *device->hidden); + + /* Set the function pointers */ + device->VideoInit = NDS_VideoInit; + device->ListModes = NDS_ListModes; + device->SetVideoMode = NDS_SetVideoMode; + device->CreateYUVOverlay = NULL; + device->SetColors = NDS_SetColors; + device->UpdateRects = NDS_UpdateRects; + device->VideoQuit = NDS_VideoQuit; + device->AllocHWSurface = NDS_AllocHWSurface; + device->CheckHWBlit = CheckHWBlit; + device->FillHWRect = NULL; + device->SetHWColorKey = NULL; + device->SetHWAlpha = NULL; + device->LockHWSurface = NDS_LockHWSurface; + device->UnlockHWSurface = NDS_UnlockHWSurface; + device->FlipHWSurface = NDS_FlipHWSurface; + device->FreeHWSurface = NDS_FreeHWSurface; + device->SetCaption = NULL; + device->SetIcon = NULL; + device->IconifyWindow = NULL; + device->GrabInput = NULL; + device->GetWMInfo = NULL; + device->InitOSKeymap = NDS_InitOSKeymap; + device->PumpEvents = NDS_PumpEvents; + device->info.blit_hw=1; + + device->free = NDS_DeleteDevice; + return device; +} + +VideoBootStrap NDS_bootstrap = { + NDSVID_DRIVER_NAME, "SDL NDS video driver", + NDS_Available, NDS_CreateDevice +}; + +u16* frontBuffer;// = (u16*)(0x06000000); +u16* backBuffer;// = (u16*)(0x06000000 + 256 * 256 * 2); +int NDS_VideoInit(_THIS, SDL_PixelFormat *vformat) +{ + //printf("WARNING: You are using the SDL NDS video driver!\n"); + + /* Determine the screen depth (use default 8-bit depth) */ + /* we change this during the SDL_SetVideoMode implementation... */ + vformat->BitsPerPixel = 16; // mode 3 + vformat->BytesPerPixel = 2; + vformat->Rmask = 0x0000f800; + vformat->Gmask = 0x000007e0; + vformat->Bmask = 0x0000001f; + powerON(POWER_ALL); + irqInit(); + irqSet(IRQ_VBLANK, on_irq_vblank); + irqEnable(IRQ_VBLANK); + + //set the mode for 2 text layers and two extended background layers + //videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE); + videoSetMode(MODE_6_2D | DISPLAY_BG2_ACTIVE); + + //set the sub background up for text display (we could just print to one + //of the main display text backgrounds just as easily + videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text + + //set the first two banks as background memory and the third as sub background memory + //D is not used..if you need a bigger background then you will need to map + //more vram banks consecutivly (VRAM A-D are all 0x20000 bytes in size) + //vramSetMainBanks(VRAM_A_MAIN_BG_0x6000000, VRAM_B_MAIN_BG_0x6020000,VRAM_C_SUB_BG , VRAM_D_LCD); + vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG); + //vramSetBankA(VRAM_A_MAIN_BG); + //vramSetBankB(VRAM_B_MAIN_BG); + //vramSetBankC(VRAM_C_MAIN_BG); + //vramSetBankD(VRAM_D_MAIN_BG); + //vramSetBankE(VRAM_E_MAIN_BG); + //vramSetBankF(VRAM_F_MAIN_BG); + //vramSetBankG(VRAM_G_MAIN_BG); + vramSetBankH(VRAM_H_SUB_BG); + vramSetBankI(VRAM_I_LCD); + + ////////////////set up text background for text///////////////////// + SUB_BG0_CR = BG_MAP_BASE(8); + + BG_PALETTE_SUB[255] = RGB15(31,31,31);//by default font will be rendered with color 255 + ///////////////set up our bitmap background/////////////////////// + + //BG3_CR = BG_BMP16_512x512; + + //these are rotation backgrounds so you must set the rotation attributes: + //these are fixed point numbers with the low 8 bits the fractional part + //this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap + /* BG3_XDX = 1<<8; + BG3_XDY = 0; + BG3_YDX = 0; + BG3_YDY = 1<<8; + //our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2 + BG3_CX = 0; + BG3_CY = 0; + */ + //consoleInit() is a lot more flexible but this gets you up and running quick + consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(8), (u16*)CHAR_BASE_BLOCK_SUB(0), 16); + + + frontBuffer =(u16*)(0x06000000); + //backBuffer =(u16*)(0x06000000 + 1024 * 512*2); + + //lcdSwap(); + /* We're done! */ + return 0; +} + +SDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) +{ + return (SDL_Rect **) -1; +} + +SDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current, + int width, int height, int bpp, Uint32 flags) +{ + Uint32 Rmask, Gmask, Bmask, Amask; + + //if (width > 1024 || height > 512 || bpp > 16) + // return NULL; + + if (bpp > 8) { + bpp = 16; + Rmask = 0x0000001F; + Gmask = 0x000003E0; + Bmask = 0x00007C00; + Amask = 0x00008000; + + videoSetMode(MODE_5_2D| DISPLAY_BG2_ACTIVE); + + vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG); + + BG2_CR = BG_BMP16_512x512; + BG2_XDX = ((width / 256) << 8) | (width % 256) ; + BG2_XDY = 0; + BG2_YDX = 0; + BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ; + BG2_CX = 0; + BG2_CY = 0; + // for (i = 0;i<256*192;i++) + // frontBuffer[i] = RGB15(31,0,0)|BIT(15); + } else if (bpp <= 8) { + bpp = 8; + Rmask = 0x00000000; + Gmask = 0x00000000; + Bmask = 0x00000000; + BG2_CR = BG_BMP8_1024x512; + BG2_XDX = ((width / 256) << 8) | (width % 256) ; + BG2_XDY = 0; + BG2_YDX = 0; + BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ; + + } else if (bpp < 15) bpp = 15; + + if (width <= 256) width = 256; + else if (width < 256) width = 256; + + if (height <= 192) height = 192; + else if (height < 192) height = 192; + + if (bpp == 8) { + if (width < 256) width = 256; + if (height < 192) height = 192; + this->hidden->ndsmode = 4; + } + + if (bpp==15) { + if (width < 256) this->hidden->ndsmode = 5; + else this->hidden->ndsmode = 3; + } + + this->hidden->buffer = frontBuffer;//NDS_VRAM_BASE; + + //NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode) | NDS_DISP_BG2; + + //fprintf(stderr,"Setting mode %dx%d (ndsmode %d)\n", width, height,this->hidden->ndsmode); + + // FIXME: How do I tell that 15 bits mode is 555? + + SDL_memset(this->hidden->buffer, 0, + 1024 * 512 * + (this->hidden->ndsmode == 4 || this->hidden->ndsmode == 5 ? 2 : 1) * ((bpp + 7) / 8)); + + /* Allocate the new pixel format for the screen */ + if (!SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, Amask)) { + this->hidden->buffer = NULL; + SDL_SetError("Couldn't allocate new pixel format for requested mode"); + return NULL; + } + + /* Set up the new mode framebuffer */ + current->flags = flags | SDL_FULLSCREEN | SDL_HWSURFACE | (this->hidden->ndsmode > 0 ? SDL_DOUBLEBUF : 0); + this->hidden->w = current->w = width; + this->hidden->h = current->h = height; + current->pixels = frontBuffer; + + if (flags & SDL_DOUBLEBUF) { + this->hidden->secondbufferallocd = 1; + backBuffer = (u16*)SDL_malloc(1024*512*2); + current->pixels = backBuffer; + } + + if (bpp == 8) + current->pitch = 1024; + else + current->pitch = 512*2; + + /* We're done */ + return current; +} + +static int NDS_AllocHWSurface(_THIS, SDL_Surface *surface) +{ + if (this->hidden->secondbufferallocd) { + //printf("double double buffer alloc\n"); + return -1; + } + //if (this->hidden->ndsmode == 3) + //{ + // printf("no 2nd buffer in mode3\n"); + // return -1; + //} + //printf("second buffer\n"); + //this->hidden->secondbufferallocd = 1; + //backBuffer = (u16*)malloc(1024 * 512 * 2); + //surface->pixels = backBuffer; + + return 0; +} + +static void NDS_FreeHWSurface(_THIS, SDL_Surface *surface) +{ + //free(backBuffer); + this->hidden->secondbufferallocd = 0; +} + +int z = 0; +/* We need to wait for vertical retrace on page flipped displays */ +static int NDS_LockHWSurface(_THIS, SDL_Surface *surface) +{ + /* + uint8* a = surface->pixels; + int i,j; + a += 5 * SCREEN_WIDTH + 5; + for (i = 0; i < 195; ++i) { + uint16* line = a + (SCREEN_WIDTH * i); + for (j = 0; j < 158; ++j) { + *line++ = RGB15(155,155,25); + } + } + */ + //if (z <256) + // BG_PALETTE[z++] = RGB15(255 - z, z, 255 - z); + + + return 0; +} + +static void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface) +{ +} + +static int NDS_FlipHWSurface(_THIS, SDL_Surface *surface) +{ + if (this->hidden->secondbufferallocd) { + while (DISP_Y != 192); + while (DISP_Y == 192); + //printf("flip"); + + dmaCopyAsynch(backBuffer, frontBuffer, 1024 * 512); + } + //printf("flip\n"); + //u16* temp = surface->pixels; + //surface->pixels = frontBuffer; + //frontBuffer = temp; + /* u8* vram=BG_GFX; + int x,y; + for (y = 0; y < 512; y++) + dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512); + //unsigned char buf; + + //printf("NDS_FlipHWSurface\n"); + //printf("ptr now: 0x%x\n",surface->pixels); + + while (DISP_Y!=192); + while (DISP_Y==192); + //swap + u16* temp = frontBuffer; + frontBuffer = backBuffer; + backBuffer = temp; + + //flip + //base is 16KB and screen size is 256x256x2 (128KB) + BG2_CR ^= BG_BMP_BASE(512 / 16); */ + /* + if (surface->pixels == frontBuffer)//NDS_VRAM_BASE) { + while (DISP_Y!=192); + while (DISP_Y==192); + //swap + u16* temp = backBuffer; + backBuffer = frontBuffer; + frontBuffer = temp; + + //flip + //base is 16KB and screen size is 256x256x2 (128KB) + BG3_CR ^= BG_BMP_BASE(128 / 16); + } else { + + while (DISP_Y!=192); + while (DISP_Y==192); + //swap + u16* temp = frontBuffer; + frontBuffer = backBuffer; + backBuffer = temp; + + //flip + //base is 16KB and screen size is 256x256x2 (128KB) + BG3_CR ^= BG_BMP_BASE(128 / 16); + + } + */ + //printf("ptr then: 0x%x\n",surface->pixels); + + //printf("setting dispcnt to 0x%x\n",NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode)|NDS_DISP_BG2| buf); + return 0; +} + +static void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects) +{ + //fprintf(stderr,"update\n"); + /* do nothing. */ + //dmaCopy(frontBuffer,BG_GFX,512*512); + /* + u8* vram = (u8*)BG_GFX; + int x,y; + for (y = 0; y < 512; y++) + dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512); + */ + +} + +int NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) +{ + //printf("SetColors\n"); + short r,g,b; + + if (this->hidden->ndsmode != 4) { + printf("This is not a palettized mode\n"); + return -1; + } + + int i,j = firstcolor + ncolors; + for (i = firstcolor; i < j; ++i) { + r = colors[i].r >> 3; + g = colors[i].g >> 3; + b = colors[i].b >> 3; + BG_PALETTE[i] = RGB15(r, g, b); + } + + return 0; +} + +/* Note: If we are terminated, this could be called in the middle of + another SDL video routine -- notably UpdateRects. +*/ +void NDS_VideoQuit(_THIS) +{ +} diff --git a/SDL-1.2/src/video/yari/SDL_yarivideo.c b/SDL-1.2/src/video/yari/SDL_yarivideo.c new file mode 100644 index 0000000..aef680b --- /dev/null +++ b/SDL-1.2/src/video/yari/SDL_yarivideo.c @@ -0,0 +1,265 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2006 Sam Lantinga + + 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 + + Sam Lantinga + slouken@libsdl.org + + + The YARI video implementation is based on SDL_dcvideo.c +*/ +#include "SDL_config.h" + +#include "SDL_video.h" +#include "SDL_mouse.h" +#include "../SDL_sysvideo.h" +#include "../SDL_pixels_c.h" +#include "../../events/SDL_events_c.h" + +/* Hidden "this" pointer for the video functions */ +#define _THIS SDL_VideoDevice *this + +/* Initialization/Query functions */ +static int YARI_VideoInit(_THIS, SDL_PixelFormat *vformat); +static SDL_Rect **YARI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); +static SDL_Surface *YARI_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); +static int YARI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); +static void YARI_VideoQuit(_THIS); + +/* Hardware surface functions */ +static int YARI_AllocHWSurface(_THIS, SDL_Surface *surface); +static int YARI_LockHWSurface(_THIS, SDL_Surface *surface); +static void YARI_UnlockHWSurface(_THIS, SDL_Surface *surface); +static void YARI_FreeHWSurface(_THIS, SDL_Surface *surface); +static int YARI_FlipHWSurface(_THIS, SDL_Surface *surface); + +/* etc. */ +static void YARI_UpdateRects(_THIS, int numrects, SDL_Rect *rects); + +/* YARI driver bootstrap functions */ + +void YARI_InitOSKeymap(_THIS) +{ +} + +static uint32_t keys; + +#define RS232IN_DATA (*(volatile unsigned *) 0xFF000004) +#define RS232IN_TAG (*(volatile unsigned *) 0xFF000008) +#define SER_OUTBUSY() (*(volatile unsigned *)0xFF000000 != 0) +#define SER_OUT(data) (*(volatile unsigned *)0xFF000000 = (data)) +#define READKEYS (*(volatile unsigned *) 0xFF000010) + +void YARI_PumpEvents(_THIS) +{ + int posted; + + do { + uint32_t new_keys = READKEYS; + int i; + SDL_keysym keysym; + SDL_Event ev; + + posted = 0; + + keysym.mod = 0; + keysym.scancode = 0xff; + memset(&ev, 0, sizeof ev); + + for (i = 0; i < 10; ++i) + if ((1 << i) & (keys ^ new_keys)) { + // 0 = ESCAPE, 1 = LEFT, 2 = SPACE, 3 = RIGHT + switch (i) { + case 0: keysym.sym = SDLK_ESCAPE; break; + case 1: keysym.sym = SDLK_LEFT; break; + case 2: keysym.sym = SDLK_RIGHT; break; + case 3: keysym.sym = SDLK_SPACE; break; + default: keysym.sym = '0' + i; break; + } + keysym.unicode = keysym.sym; + ev.type = (1 << i) & new_keys ? SDL_KEYDOWN : SDL_KEYUP; + ev.key.state = 0; + ev.key.keysym = keysym; + + SDL_PushEvent(&ev); + + keys ^= 1 << i; + ++posted; + } + } while (posted); +} + +static int YARI_Available(void) +{ + return 1; +} + +static void YARI_DeleteDevice(SDL_VideoDevice *device) +{ + SDL_free(device); +} + +static SDL_VideoDevice *YARI_CreateDevice(int devindex) +{ + SDL_VideoDevice *device; + + /* Initialize all variables that we clean on shutdown */ + device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); + if (device) + SDL_memset(device, 0, sizeof *device); + + if (!device) { + SDL_OutOfMemory(); + + return 0; + } + + /* Set the function pointers */ + device->VideoInit = YARI_VideoInit; + device->ListModes = YARI_ListModes; + device->SetVideoMode = YARI_SetVideoMode; + device->CreateYUVOverlay = NULL; + device->SetColors = YARI_SetColors; + device->UpdateRects = YARI_UpdateRects; + device->VideoQuit = YARI_VideoQuit; + device->AllocHWSurface = YARI_AllocHWSurface; + device->CheckHWBlit = NULL; + device->FillHWRect = NULL; + device->SetHWColorKey = NULL; + device->SetHWAlpha = NULL; + device->LockHWSurface = YARI_LockHWSurface; + device->UnlockHWSurface = YARI_UnlockHWSurface; + device->FlipHWSurface = YARI_FlipHWSurface; + device->FreeHWSurface = YARI_FreeHWSurface; + device->SetCaption = NULL; + device->SetIcon = NULL; + device->IconifyWindow = NULL; + device->GrabInput = NULL; + device->GetWMInfo = NULL; + device->InitOSKeymap = YARI_InitOSKeymap; + device->PumpEvents = YARI_PumpEvents; + + device->free = YARI_DeleteDevice; + + return device; +} + +VideoBootStrap YARI_bootstrap = { + "yarivideo", "YARI Video", + YARI_Available, YARI_CreateDevice +}; + + +int YARI_VideoInit(_THIS, SDL_PixelFormat *vformat) +{ + /* YARI's video interface only supports RGB332 (= 8-bit) */ + vformat->BitsPerPixel = 8; + vformat->Rmask = 7 << 5; + vformat->Gmask = 7 << 3; + vformat->Bmask = 3; + + return 0; +} + +/* Currently, YARI only supports 1024x768 */ +static SDL_Rect RECT_1024x768 = {0,0,1024,768}; +static SDL_Rect *vid_modes[] = { + &RECT_1024x768, + NULL +}; + +SDL_Rect **YARI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) +{ + if (format->BitsPerPixel != 8) + return NULL; + + return vid_modes; +} + +SDL_Surface *YARI_SetVideoMode(_THIS, SDL_Surface *current, + int width, int height, int bpp, Uint32 flags) +{ + int disp_mode,pixel_mode,pitch; + Uint32 Rmask, Gmask, Bmask; + + if (width > 1024 || height > 768 || bpp != 8 || (flags & SDL_DOUBLEBUF)) { + SDL_SetError("Couldn't find requested mode in list"); + return NULL; + } + + Rmask = 7 << 5; + Gmask = 7 << 3; + Bmask = 3; + + /* Set up the new mode framebuffer */ + current->flags = SDL_FULLSCREEN | SDL_HWSURFACE; + current->w = width; + current->h = height; + current->pitch = 1024; + current->pixels = (void *) 0x40100000; + + if (!SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, 0)) { + return NULL; + } + + return current; +} + +/* We don't actually allow hardware surfaces other than the main one */ +static int YARI_AllocHWSurface(_THIS, SDL_Surface *surface) +{ + return -1; +} +static void YARI_FreeHWSurface(_THIS, SDL_Surface *surface) +{ +} + +/* We need to wait for vertical retrace on page flipped displays */ +static int YARI_LockHWSurface(_THIS, SDL_Surface *surface) +{ + return 0; +} + +static void YARI_UnlockHWSurface(_THIS, SDL_Surface *surface) +{ +} + +static int YARI_FlipHWSurface(_THIS, SDL_Surface *surface) +{ + if (surface->flags & SDL_DOUBLEBUF) + return -1; + + return 0; +} + +static void YARI_UpdateRects(_THIS, int numrects, SDL_Rect *rects) +{ + /* do nothing. */ +} + +static int YARI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) +{ + /* do nothing of note. */ + return 1; +} + +/* Note: If we are terminated, this could be called in the middle of + another SDL video routine -- notably UpdateRects. +*/ +static void YARI_VideoQuit(_THIS) +{ +} -- 2.11.4.GIT