+++ /dev/null
-\r
-// This file is part of the PicoDrive Megadrive Emulator\r
-\r
-// Copyright (c) 2011 FinalDave (emudave (at) gmail.com)\r
-\r
-// This code is licensed under the GNU General Public License version 2.0 and the MAME License.\r
-// You can choose the license that has the most advantages for you.\r
-\r
-// SVN repository can be found at http://code.google.com/p/cyclone68000/\r
-\r
-#include "stdafx.h"\r
-\r
-TCHAR RomName[260]={0};\r
-static unsigned char *RomData=NULL;\r
-static unsigned int RomSize=0;\r
-\r
-static unsigned int LastSecond=0;\r
-static int FramesDone=0;\r
-static int FramesPerSecond=60;\r
-\r
-struct Target Targ;\r
-\r
-static int TargetInit()\r
-{\r
- RECT rect={0,0,0,0};\r
- int height=0;\r
-\r
- memset(&Targ,0,sizeof(Targ));\r
-\r
- height=168;\r
-\r
- ClientToScreen(FrameWnd,&Targ.point);\r
-\r
- GetClientRect(FrameWnd,&rect);\r
- // Find out where the top of the screen should go:\r
- rect.top=(rect.bottom-height)>>1;\r
- if (rect.top<0) rect.top=0;\r
- rect.bottom=rect.top+height;\r
-\r
- Targ.view=rect; // Save the view rectangle (client coordinates)\r
-\r
- Targ.offset=Targ.view.top+Targ.point.y;\r
-\r
- return 0;\r
-}\r
-\r
-static int TargetUpdate()\r
-{\r
- // Need to repaint the view rectangle:\r
- GetUpdateRect(FrameWnd,&Targ.update,0);\r
-\r
- Targ.top =Targ.update.top +Targ.point.y;\r
- Targ.bottom=Targ.update.bottom+Targ.point.y;\r
-\r
- return 0;\r
-}\r
-\r
-int EmulateInit()\r
-{\r
- FILE *f=NULL;\r
-\r
- EmulateExit(); // Make sure exited\r
-\r
- TargetInit(); // Find out where to put the screen\r
-\r
- PicoInit();\r
-\r
- // Load cartridge\r
- f=_wfopen(RomName,L"rb"); if (f==NULL) return 1;\r
- PicoCartLoad(f,&RomData,&RomSize);\r
- fclose(f);\r
-\r
- PicoCartInsert(RomData,RomSize);\r
-\r
- LastSecond=GetTickCount(); FramesDone=0;\r
-\r
- return 0;\r
-}\r
-\r
-void EmulateExit()\r
-{\r
- // Remove cartridge\r
- PicoCartInsert(NULL,0);\r
- if (RomData) free(RomData); RomData=NULL; RomSize=0;\r
- \r
- PicoExit();\r
-}\r
-\r
-// Callback for scanline data:\r
-static int EmulateScan(unsigned int scan,unsigned short *sdata)\r
-{\r
- int len=0;\r
- unsigned short *ps=NULL,*end=NULL;\r
- unsigned char *pd=NULL;\r
- int xpitch=0;\r
-\r
- if ((scan&3)==1) return 0;\r
- scan+=scan<<1; scan>>=2; // Reduce size to 75%\r
-\r
- scan+=Targ.offset;\r
- if ((int)scan< Targ.top) return 0; // Out of range\r
- if ((int)scan>=Targ.bottom) return 0; // Out of range\r
-\r
- pd=Targ.screen+scan*GXDisp.cbyPitch;\r
-\r
- len=240;\r
- xpitch=GXDisp.cbxPitch;\r
- ps=sdata; end=ps+320;\r
-\r
- // Reduce 4 pixels into 3\r
- do\r
- {\r
- *(unsigned short *)pd=ps[0]; pd+=xpitch;\r
- *(unsigned short *)pd=(unsigned short)((ps[1]+ps[2])>>1); pd+=xpitch;\r
- *(unsigned short *)pd=ps[3]; pd+=xpitch;\r
- ps+=4;\r
- }\r
- while (ps<end);\r
-\r
- return 0;\r
-}\r
-\r
-static int DoFrame()\r
-{\r
- int pad=0,i=0,ks=0;\r
- char map[8]={0,1,2,3,5,6,4,7}; // u/d/l/r/b/c/a/start\r
-\r
- for (i=0;i<8;i++)\r
- {\r
- ks=GetAsyncKeyState(Config.key[map[i]]);\r
- if (ks) pad|=1<<i;\r
- }\r
- PicoPad[0]=pad;\r
-\r
- PicoFrame();\r
- return 0;\r
-}\r
-\r
-static int DrawFrame()\r
-{\r
- // Now final frame is drawn:\r
- InvalidateRect(FrameWnd,&Targ.view,0);\r
-\r
- if (Main3800) Targ.screen=(unsigned char *)0xac0755a0; // The real 3800 screen address\r
- else Targ.screen=(unsigned char *)GXBeginDraw();\r
-\r
- if (Targ.screen==NULL) return 1;\r
-\r
- TargetUpdate();\r
-\r
- PicoScan=EmulateScan; // Setup scanline callback\r
- DoFrame();\r
- PicoScan=NULL;\r
-\r
-\r
- \r
- if (Main3800==0) GXEndDraw();\r
-\r
- Targ.screen=NULL;\r
-\r
- ValidateRect(FrameWnd,&Targ.update);\r
-\r
- if (PicoStatus[0])\r
- {\r
- // Print the status of the 68000:\r
- HDC hdc=GetDC(FrameWnd);\r
- RECT rect={0,220, 240,260};\r
- TCHAR status[128]={0};\r
-\r
- wsprintf(status,L"%.120S",PicoStatus);\r
-\r
- FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));\r
- SetBkMode(hdc,TRANSPARENT);\r
-\r
- DrawText(hdc,status,lstrlen(status),&rect,0);\r
- ReleaseDC(FrameWnd,hdc);\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-int EmulateFrame()\r
-{\r
- int i=0,need=0;\r
- int time=0,frame=0;\r
-\r
- if (RomData==NULL) return 1;\r
-\r
- // Speed throttle:\r
- time=GetTickCount()-LastSecond; // This will be about 0-1000 ms\r
- frame=time*FramesPerSecond/1000;\r
- need=frame-FramesDone;\r
- FramesDone=frame;\r
-\r
- if (FramesPerSecond>0)\r
- {\r
- // Carry over any >60 frame count to one second\r
- while (FramesDone>=FramesPerSecond) { FramesDone-=FramesPerSecond; LastSecond+=1000; }\r
- }\r
-\r
- if (need<=0) { Sleep(2); return 1; }\r
- if (need>4) need=4; // Limit frame skipping\r
-\r
- for (i=0;i<need-1;i++) DoFrame(); // Frame skip if needed\r
-\r
- DrawFrame();\r
- return 0;\r
-}\r
-\r
-int SndRender()\r
-{\r
-// int p=0;\r
-\r
- PsndRate=WaveRate;\r
- PsndLen=WaveLen;\r
- PsndOut=WaveDest;\r
-\r
- DrawFrame();\r
- // Convert to signed:\r
-// for (p=0;p<PsndLen<<1;p++) PsndOut[p]+=0x8000;\r
-\r
- PsndRate=PsndLen=0;\r
- PsndOut=NULL;\r
-\r
- return 0;\r
-}\r