19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <nds.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <nds/registers_alt.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ndsvideo.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ndsevents_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ndsmouse_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NDSVID_DRIVER_NAME "nds"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_VideoInit(_THIS, SDL_PixelFormat *vformat);
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_VideoQuit(_THIS);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_AllocHWSurface(_THIS, SDL_Surface *surface);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_LockHWSurface(_THIS, SDL_Surface *surface);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_FlipHWSurface(_THIS, SDL_Surface *surface);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_FreeHWSurface(_THIS, SDL_Surface *surface);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* etc. */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* NDS driver bootstrap functions */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_Available(void)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_DeleteDevice(SDL_VideoDevice *device)
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid on_irq_vblank()
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Disable interrupts
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  //REG_IME = 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  scanKeys();
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  //  VBLANK_INTR_WAIT_FLAGS |= IRQ_VBLANK;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  //  REG_IF |= IRQ_VBLANK;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  //REG_IF = REG_IF;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Enable interrupts
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  //REG_IME = 1;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_Surface *dst, SDL_Rect *dstrect)
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (src->flags & SDL_SRCALPHA) return false;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (src->flags & SDL_SRCCOLORKEY) return false;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (src->flags & SDL_HWPALETTE ) return false;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (dst->flags & SDL_SRCALPHA) return false;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (dst->flags & SDL_SRCCOLORKEY) return false;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (dst->flags & SDL_HWPALETTE ) return false;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (src->format->BitsPerPixel != dst->format->BitsPerPixel) return false;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	if (src->format->BytesPerPixel != dst->format->BytesPerPixel) return false;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        src->map->hw_blit = HWAccelBlit;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return true;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *NDS_CreateDevice(int devindex)
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device=0;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->hidden));
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ) {
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device ) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = NDS_VideoInit;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = NDS_ListModes;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = NDS_SetVideoMode;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateYUVOverlay = NULL;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = NDS_SetColors;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = NDS_UpdateRects;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = NDS_VideoQuit;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = NDS_AllocHWSurface;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = CheckHWBlit;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = NDS_LockHWSurface;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = NDS_UnlockHWSurface;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = NDS_FlipHWSurface;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = NDS_FreeHWSurface;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = NULL;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = NULL;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->IconifyWindow = NULL;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GrabInput = NULL;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = NULL;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = NDS_InitOSKeymap;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = NDS_PumpEvents;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->info.blit_hw=1;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = NDS_DeleteDevice;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap NDS_bootstrap = {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NDSVID_DRIVER_NAME, "SDL NDS video driver",
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NDS_Available, NDS_CreateDevice
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	u16* frontBuffer;// = (u16*)(0x06000000);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	u16* backBuffer;// =  (u16*)(0x06000000 + 256 * 256 * 2);
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint NDS_VideoInit(_THIS, SDL_PixelFormat *vformat)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("WARNING: You are using the SDL NDS video driver!\n");
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the screen depth (use default 8-bit depth) */
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* we change this during the SDL_SetVideoMode implementation... */
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = 16;	// mode 3
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BytesPerPixel = 2;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Rmask = 0x0000f800;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Gmask = 0x000007e0;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Bmask = 0x0000001f;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    powerON(POWER_ALL);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	irqInit();
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	irqSet(IRQ_VBLANK, on_irq_vblank);
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	irqEnable(IRQ_VBLANK);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //set the mode for 2 text layers and two extended background layers
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	videoSetMode(MODE_6_2D| DISPLAY_BG2_ACTIVE);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//set the sub background up for text display (we could just print to one
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//of the main display text backgrounds just as easily
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //set the first two banks as background memory and the third as sub background memory
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //D is not used..if you need a bigger background then you will need to map
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //more vram banks consecutivly (VRAM A-D are all 0x20000 bytes in size)
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //vramSetMainBanks(VRAM_A_MAIN_BG_0x6000000, VRAM_B_MAIN_BG_0x6020000,VRAM_C_SUB_BG , VRAM_D_LCD);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG);
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankA(VRAM_A_MAIN_BG);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankB(VRAM_B_MAIN_BG);
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankC(VRAM_C_MAIN_BG);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankD(VRAM_D_MAIN_BG);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankE(VRAM_E_MAIN_BG);
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankF(VRAM_F_MAIN_BG);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//vramSetBankG(VRAM_G_MAIN_BG);
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vramSetBankH(VRAM_H_SUB_BG);
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vramSetBankI(VRAM_I_LCD);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	////////////////set up text background for text/////////////////////
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SUB_BG0_CR = BG_MAP_BASE(8);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BG_PALETTE_SUB[255] = RGB15(31,31,31);//by default font will be rendered with color 255
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	///////////////set up our bitmap background///////////////////////
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//BG3_CR = BG_BMP16_512x512;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//these are rotation backgrounds so you must set the rotation attributes:
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //these are fixed point numbers with the low 8 bits the fractional part
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /*  BG3_XDX = 1<<8;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_XDY = 0;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_YDX = 0;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_YDY = 1<<8;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_CX = 0;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_CY = 0;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//consoleInit() is a lot more flexible but this gets you up and running quick
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(8), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	frontBuffer =(u16*)(0x06000000);
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//backBuffer  =(u16*)(0x06000000 + 1024 * 512*2);
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//lcdSwap();
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **NDS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (SDL_Rect **) -1;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *NDS_SetVideoMode(_THIS, SDL_Surface *current,
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Rmask, Gmask, Bmask, Amask;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//if(width > 1024 || height > 512 || bpp > 16)
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//	return(NULL);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp >8) {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp=16;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		Rmask = 0x0000001F;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Gmask = 0x000003E0;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Bmask = 0x00007C00;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Amask = 0x00008000;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		videoSetMode(MODE_5_2D| DISPLAY_BG2_ACTIVE);
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vramSetMainBanks(VRAM_A_MAIN_BG,VRAM_B_MAIN_BG,VRAM_C_MAIN_BG,VRAM_D_MAIN_BG);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BG2_CR = BG_BMP16_512x512;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    BG2_XDX = ((width / 256) << 8) | (width % 256) ;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_XDY = 0;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_YDX = 0;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_CX = 0;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_CY = 0;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//        for (i=0;i<256*192;i++)
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//	        frontBuffer[i] = RGB15(31,0,0)|BIT(15);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp <= 8) {
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp=8;
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Rmask = 0x00000000;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Gmask = 0x00000000;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Bmask = 0x00000000;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BG2_CR = BG_BMP8_1024x512;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_XDX = ((width / 256) << 8) | (width % 256) ;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_XDY = 0;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_YDX = 0;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_YDY = ((height / 192) << 8) | ((height % 192) + (height % 192) / 3) ;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(bpp < 15) bpp=15;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(width<=256) width=256;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(width<256) width=256;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(height<=192) height=192;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(height<192) height=192;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp==8)
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(width<256) width=256;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(height<192) height=192;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->ndsmode=4;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp==15)
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(width<256) this->hidden->ndsmode=5;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else this->hidden->ndsmode=3;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->buffer= frontBuffer;//NDS_VRAM_BASE;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode)|NDS_DISP_BG2;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	//fprintf(stderr,"Setting mode %dx%d (ndsmode %d)\n", width, height,this->hidden->ndsmode);
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// FIXME: How do I tell that 15 bits mode is 555?
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden->buffer, 0, 1024 * 512* ((this->hidden->ndsmode==4 || this->hidden->ndsmode==5) ? 2 : 1 ) * ((bpp+7) / 8));
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new pixel format for the screen */
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, Amask) ) {
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->buffer = NULL;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't allocate new pixel format for requested mode");
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new mode framebuffer */
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->flags = flags | SDL_FULLSCREEN | SDL_HWSURFACE | (this->hidden->ndsmode > 0 ? SDL_DOUBLEBUF : 0);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->w = current->w = width;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->h = current->h = height;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pixels = frontBuffer;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (flags & SDL_DOUBLEBUF) {
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->secondbufferallocd=1;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		backBuffer=(u16*)SDL_malloc(1024*512*2);
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pixels = backBuffer;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp==8)
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pitch =1024;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pitch =512*2;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(current);
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_AllocHWSurface(_THIS, SDL_Surface *surface)
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(this->hidden->secondbufferallocd) {
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//printf("double double buffer alloc\n");
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//if(this->hidden->ndsmode==3)
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//{
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//	printf("no 2nd buffer in mode3\n");
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//	return -1;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("second buffer\n");
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//this->hidden->secondbufferallocd=1;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//backBuffer=(u16*)malloc(1024*512*2);
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//surface->pixels = backBuffer;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_FreeHWSurface(_THIS, SDL_Surface *surface)
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//free(backBuffer);
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->secondbufferallocd=0;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint z=0;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We need to wait for vertical retrace on page flipped displays */
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_LockHWSurface(_THIS, SDL_Surface *surface)
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint8* a = surface->pixels;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int i,j;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  a += 5 * SCREEN_WIDTH + 5;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for( i = 0; i < 195; ++i) {
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    uint16* line = a + (SCREEN_WIDTH * i);
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for( j = 0; j < 158; ++j) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      *line++ = RGB15(155,155,25);
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//if (z <256)
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// BG_PALETTE[z++]=RGB15(255-z,z,255-z);
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_UnlockHWSurface(_THIS, SDL_Surface *surface)
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_FlipHWSurface(_THIS, SDL_Surface *surface)
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(this->hidden->secondbufferallocd){
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(DISP_Y!=192);
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    while(DISP_Y==192);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//printf("flip");
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dmaCopyAsynch(backBuffer,frontBuffer,1024*512);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//printf("flip\n");
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //u16* temp = surface->pixels;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //surface->pixels = frontBuffer;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //frontBuffer = temp;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*	u8* vram=BG_GFX;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y = 0; y < 512; y++)
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512);
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//unsigned char buf;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("NDS_FlipHWSurface\n");
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("ptr now: 0x%x\n",surface->pixels);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    while(DISP_Y!=192);
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    while(DISP_Y==192);
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //swap
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        u16* temp = frontBuffer;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        frontBuffer = backBuffer;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        backBuffer = temp;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //flip
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //base is 16KB and screen size is 256x256x2 (128KB)
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG2_CR ^= BG_BMP_BASE( 512 / 16 ); */
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(surface->pixels == frontBuffer)//NDS_VRAM_BASE)
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while(DISP_Y!=192);
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(DISP_Y==192);
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //swap
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        u16* temp = backBuffer;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        backBuffer = frontBuffer;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        frontBuffer = temp;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //flip
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //base is 16KB and screen size is 256x256x2 (128KB)
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_CR ^= BG_BMP_BASE( 128 / 16 );
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(DISP_Y!=192);
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(DISP_Y==192);
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //swap
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        u16* temp = frontBuffer;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        frontBuffer = backBuffer;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        backBuffer = temp;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //flip
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        //base is 16KB and screen size is 256x256x2 (128KB)
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        BG3_CR ^= BG_BMP_BASE( 128 / 16 );
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("ptr then: 0x%x\n",surface->pixels);
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("setting dispcnt to 0x%x\n",NDS_DISPCNT = NDS_DISP_MODE(this->hidden->ndsmode)|NDS_DISP_BG2| buf);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//fprintf(stderr,"update\n");
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* do nothing. */
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//dmaCopy(frontBuffer,BG_GFX,512*512);
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 /*
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	u8* vram=(u8*)BG_GFX;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y = 0; y < 512; y++)
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dmaCopy(&frontBuffer[y*rects->w], &vram[y*512],512);
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint NDS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("SetColors\n");
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short r,g,b;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(this->hidden->ndsmode != 4)
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf("This is not a palettized mode\n");
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i,j=firstcolor+ncolors;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i=firstcolor;i<j;i++)
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		r=colors[i].r>>3;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g=colors[i].g>>3;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		b=colors[i].b>>3;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BG_PALETTE[i]=RGB15(r, g, b);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note:  If we are terminated, this could be called in the middle of
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   another SDL video routine -- notably UpdateRects.
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid NDS_VideoQuit(_THIS)
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
501