OSDN Git Service

6f95d8488aa101b87d3765bbb7f6c748187cf085
[csp-qt/common_source_project-fm7.git] / source / src / emu.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ win32 emulation i/f ]
8 */
9
10 #ifndef _EMU_H_
11 #define _EMU_H_
12
13 // DirectX
14 #define DIRECTSOUND_VERSION     0x900
15 #define DIRECT3D_VERSION        0x900
16 //#define DIRECTINPUT_VERSION   0x500
17 #define DIRECTINPUT_VERSION     0x800
18
19 // for debug
20 //#define _DEBUG_LOG
21 #ifdef _DEBUG_LOG
22         // output fdc debug log
23 //      #define _FDC_DEBUG_LOG
24         // output i/o debug log
25 //      #define _IO_DEBUG_LOG
26 #endif
27
28 #if defined(_USE_AGAR)
29 # include <SDL/SDL.h>
30 # include <agar/core.h>
31 # include <agar/gui.h>
32 # include "simd_types.h"
33 // Wrapper of WIN32->*nix
34
35 #ifndef _MAX_PATH
36  #define _MAX_PATH AG_PATHNAME_MAX
37 #endif
38
39 #elif defined(_USE_QT)
40 # include <SDL2/SDL.h>
41 //# include "menuclasses.h"
42 //# include "mainwidget.h"
43 //# include "qt_gldraw.h"
44 //# include "emu_utils.h"
45 //# include "qt_main.h"
46 # include "simd_types.h"
47 // Wrapper of WIN32->*nix
48
49 #ifndef _MAX_PATH
50  #ifdef MAX_PATH
51    #define _MAX_PATH MAX_PATH
52  #else
53    #define MAX_PATH 2048
54    #define _MAX_PATH 2048
55  #endif
56 #endif
57
58 #else // _USE_WIN32
59
60 #include <windows.h>
61 #include <windowsx.h>
62 #include <mmsystem.h>
63 #include <process.h>
64
65 #endif // _USE_WIN32
66
67 #include <stdio.h>
68 #include <assert.h>
69 #include "common.h"
70 #include "config.h"
71 #include "vm/vm.h"
72 #if defined(USE_FD1)
73 #include "vm/disk.h"
74 #endif
75
76 #if defined(_USE_AGAR)
77 #include "agar_input.h"
78 #elif defined(_USE_QT)
79 #include <QSemaphore>
80 #include "qt_input.h"
81 #endif
82
83 #if defined(_USE_AGAR) || defined(_USE_QT)
84 # define WM_RESIZE  1
85 #define WM_SOCKET0 2
86 #define WM_SOCKET1 3
87 #define WM_SOCKET2 4
88 #define WM_SOCKET3 5
89 #else // WIN32
90 #define WM_RESIZE  (WM_USER + 1)
91 #define WM_SOCKET0 (WM_USER + 2)
92 #define WM_SOCKET1 (WM_USER + 3)
93 #define WM_SOCKET2 (WM_USER + 4)
94 #define WM_SOCKET3 (WM_USER + 5)
95 #endif
96
97 #if defined(USE_LASER_DISC) || defined(USE_VIDEO_CAPTURE)
98 #define USE_DIRECT_SHOW
99 #endif
100
101 #ifdef USE_VIDEO_CAPTURE
102 #define MAX_CAPTURE_DEVS 8
103 #endif
104
105 #ifndef SCREEN_WIDTH_ASPECT
106 #define SCREEN_WIDTH_ASPECT SCREEN_WIDTH
107 #endif
108 #ifndef SCREEN_HEIGHT_ASPECT
109 #define SCREEN_HEIGHT_ASPECT SCREEN_HEIGHT
110 #endif
111 #ifndef WINDOW_WIDTH_ASPECT
112 #define WINDOW_WIDTH_ASPECT SCREEN_WIDTH
113 #endif
114 #ifndef WINDOW_HEIGHT_ASPECT
115 #define WINDOW_HEIGHT_ASPECT SCREEN_HEIGHT
116 #endif
117 #ifndef WINDOW_WIDTH
118 #define WINDOW_WIDTH SCREEN_WIDTH_ASPECT
119 #endif
120 #ifndef WINDOW_HEIGHT
121 #define WINDOW_HEIGHT SCREEN_HEIGHT_ASPECT
122 #endif
123
124 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
125
126 typedef struct {
127    Sint16 **pSoundBuf;
128    int *uBufSize;
129    int *nSndWritePos;
130    int *nSndDataLen;
131    SDL_sem **pSndApplySem;
132    Uint8 *iTotalVolume;
133    bool *bSndExit;
134    bool *bSoundDebug;
135 } sdl_snddata_t;
136
137 #else // WIN32
138 #pragma comment(lib, "d3d9.lib")
139 #pragma comment(lib, "d3dx9.lib")
140 #include <d3d9.h>
141 #include <d3dx9.h>
142 #include <d3d9types.h>
143
144 #include <dsound.h>
145 #include <vfw.h>
146
147 //#pragma comment(lib, "dinput.lib")
148 #pragma comment(lib, "dinput8.lib")
149 #pragma comment(lib, "dxguid.lib")
150 #include <dinput.h>
151
152 #ifdef USE_DIRECT_SHOW
153 #pragma comment(lib, "strmiids.lib")
154 #include <dshow.h>
155 //#include <qedit.h>
156 EXTERN_C const CLSID CLSID_SampleGrabber;
157 EXTERN_C const CLSID CLSID_NullRenderer;
158 EXTERN_C const IID IID_ISampleGrabberCB;
159 MIDL_INTERFACE("0579154A-2B53-4994-B0D0-E773148EFF85")
160 ISampleGrabberCB : public IUnknown {
161 public:
162         virtual HRESULT STDMETHODCALLTYPE SampleCB( double SampleTime,IMediaSample *pSample) = 0;
163         virtual HRESULT STDMETHODCALLTYPE BufferCB( double SampleTime,BYTE *pBuffer,long BufferLen) = 0;
164 };
165 EXTERN_C const IID IID_ISampleGrabber;
166 MIDL_INTERFACE("6B652FFF-11FE-4fce-92AD-0266B5D7C78F")
167 ISampleGrabber : public IUnknown {
168 public:
169         virtual HRESULT STDMETHODCALLTYPE SetOneShot( BOOL OneShot) = 0;
170         virtual HRESULT STDMETHODCALLTYPE SetMediaType( const AM_MEDIA_TYPE *pType) = 0;
171         virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType( AM_MEDIA_TYPE *pType) = 0;
172         virtual HRESULT STDMETHODCALLTYPE SetBufferSamples( BOOL BufferThem) = 0;
173         virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer( /* [out][in] */ long *pBufferSize,/* [out] */ long *pBuffer) = 0;
174         virtual HRESULT STDMETHODCALLTYPE GetCurrentSample( /* [retval][out] */ IMediaSample **ppSample) = 0;
175         virtual HRESULT STDMETHODCALLTYPE SetCallback( ISampleGrabberCB *pCallback,long WhichMethodToCallback) = 0;
176 };
177 #endif
178
179 #ifdef __cplusplus
180 #ifdef USE_LASER_DISC
181 class CMySampleGrabberCB : public ISampleGrabberCB {
182 private:
183         VM *vm;
184 public:
185         CMySampleGrabberCB(VM *vm_ptr)
186         {
187                 vm = vm_ptr;
188         }
189         STDMETHODIMP_(ULONG) AddRef()
190         {
191                 return 2;
192         }
193         STDMETHODIMP_(ULONG) Release()
194         {
195                 return 1;
196         }
197         STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
198         {
199                 if(riid == IID_ISampleGrabberCB || riid == IID_IUnknown) {
200                         *ppv = (void *) static_cast<ISampleGrabberCB*>(this);
201                         return NOERROR;
202                 }
203                 return E_NOINTERFACE;
204         }
205         STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample)
206         {
207                 return S_OK;
208         }
209         STDMETHODIMP BufferCB(double dblSampleTime, BYTE *pBuffer, long lBufferSize)
210         {
211                 vm->movie_sound_callback(pBuffer, lBufferSize);
212                 return S_OK;
213         }
214 };
215 #endif
216 #endif
217 #endif
218
219 #ifdef USE_SOCKET
220 # if defined(_USE_AGAR) || defined(_USE_SDL)
221 # elif defined(_USE_QT)
222 # else // _WIN32
223 #  include <winsock.h>
224 # endif
225 #endif
226
227 // check memory leaks
228 #ifdef _DEBUG
229 #define _CRTDBG_MAP_ALLOC
230 #include <crtdbg.h>
231 #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
232 #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
233 #endif
234
235 #ifdef USE_FD1
236 #define MAX_D88_BANKS 64
237 #endif
238
239 #ifdef USE_SOCKET
240 #define SOCKET_MAX 4
241 #define SOCKET_BUFFER_MAX 0x100000
242 #endif
243
244 #ifdef __cplusplus
245 class EMU;
246 class FIFO;
247 class FILEIO;
248 #endif
249 #if defined(_USE_QT)
250 class GLDrawClass;
251 #endif
252
253 #if defined(_USE_AGAR) || defined(_USE_QT)
254         typedef Uint32 scrntype;
255 #else
256 typedef struct {
257         PAVISTREAM pAVICompressed;
258         scrntype* lpBmpSource;
259         LPBITMAPINFOHEADER pbmInfoHeader;
260         DWORD dwAVIFileSize;
261         LONG lAVIFrames;
262         int frames;
263         int result;
264 } video_thread_t;
265 #endif
266
267 #ifdef USE_DEBUGGER
268 typedef struct {
269         EMU *emu;
270         VM *vm;
271         int cpu_index;
272         bool running;
273         bool request_terminate;
274 } debugger_thread_t;
275 class CSP_Debugger;
276 #endif
277
278 #ifdef __cplusplus
279 class EMU
280 {
281 protected:
282         VM* vm;
283 #if defined(_USE_AGAR)
284         SDL_sem *pVMSemaphore; // To be thread safed.
285 #elif defined(_USE_QT)
286         QSemaphore *VMSemaphore;
287 #endif
288 private:
289         // ----------------------------------------
290         // input
291         // ----------------------------------------
292         void initialize_input();
293         void release_input();
294         void update_input();
295         void key_down_sub(int code, bool repeat);
296         void key_up_sub(int code);
297         
298 #if !defined(_USE_AGAR) && !defined(_USE_SDL) && !defined(_USE_QT)
299         LPDIRECTINPUT lpdi;
300         LPDIRECTINPUTDEVICE lpdikey;
301         LPDIRECTINPUTDEVICE lpdijoy;
302         bool dinput_key_ok;
303         bool dinput_joy_ok;
304 #endif
305    
306         uint8 keycode_conv[256];
307         uint8 key_status[256];  // windows key code mapping
308         uint32 modkey_status;
309         uint8 key_dik_prev[256];
310 #ifdef USE_SHIFT_NUMPAD_KEY
311         uint8 key_converted[256];
312         bool key_shift_pressed, key_shift_released;
313 #endif
314         bool lost_focus;
315         
316         uint32 joy_status[2];   // joystick #1, #2 (b0 = up, b1 = down, b2 = left, b3 = right, b4- = buttons
317         int joy_num;
318         uint32 joy_mask[2];
319         
320         int mouse_status[3];    // x, y, button (b0 = left, b1 = right)
321         bool mouse_enabled;
322         
323 #ifdef USE_AUTO_KEY
324         FIFO* autokey_buffer;
325         int autokey_phase, autokey_shift;
326 #endif
327         
328         // ----------------------------------------
329         // screen
330         // ----------------------------------------
331         void initialize_screen();
332         void release_screen();
333 #if !defined(_USE_AGAR) && !defined(_USE_SDL) && !defined(_USE_QT)
334         void create_dib_section(HDC hdc, int width, int height, HDC *hdcDib, HBITMAP *hBmp, HBITMAP *hOldBmp, LPBYTE *lpBuf, scrntype **lpBmp, LPBITMAPINFO *lpDib);
335 #endif  
336         // screen settings
337         int screen_width, screen_height;
338         int screen_width_aspect, screen_height_aspect;
339         int window_width, window_height;
340         int display_width, display_height;
341         bool screen_size_changed;
342         
343 #if defined(_USE_AGAR)
344
345 #elif defined(_USE_QT)
346
347 #else
348         HDC hdcDibSource;
349         scrntype* lpBmpSource;
350         LPBITMAPINFO lpDibSource;
351         LPBITMAPINFOHEADER pbmInfoHeader;
352 #endif
353    
354         int source_width, source_height;
355         int source_width_aspect, source_height_aspect;
356         int stretched_width, stretched_height;
357         int stretch_pow_x, stretch_pow_y;
358         int screen_dest_x, screen_dest_y;
359         bool stretch_screen;
360         
361         // update flags
362         bool first_draw_screen;
363         bool first_invalidate;
364         bool self_invalidate;
365         
366         // screen buffer
367 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
368         
369 #ifdef USE_SCREEN_ROTATE
370         // rotate buffer
371 #endif
372         
373         // stretch buffer
374         bool render_to_GL;
375         bool render_to_SDLFB;
376         bool use_GL;
377         bool use_SDLFB;
378         bool render_with_OpenCL;
379         bool single_window;
380         bool wait_vsync;
381 #ifdef _USE_QT
382         QImage *pPseudoVram;
383 #else
384         Uint32 *pPseudoVram;
385 #endif
386         // record video
387 #ifdef _USE_QT
388         _TCHAR video_file_name[_MAX_PATH];
389 #else
390         _TCHAR video_file_name[AG_PATHNAME_MAX];
391 #endif
392         int rec_video_fps;
393         double rec_video_run_frames;
394         double rec_video_frames;
395         
396 //      LPBITMAPINFO lpDibRec;
397 //      PAVIFILE pAVIFile;
398 //      PAVISTREAM pAVIStream;
399 //      PAVISTREAM pAVICompressed;
400 //      AVICOMPRESSOPTIONS opts;
401 //      DWORD dwAVIFileSize;
402 //      LONG lAVIFrames;
403         
404 //      HDC hdcDibRec;
405 //      HBITMAP hBmpRec, hOldBmpRec;
406 //      LPBYTE lpBufRec;
407 //      scrntype* lpBmpRec;
408         
409         bool use_video_thread;
410 #if defined(_USE_QT)
411 #else
412         AG_Thread hVideoThread;
413 #endif
414         //video_thread_t video_thread_param;
415
416         // ----------------------------------------
417         // sound
418         // ----------------------------------------
419         void initialize_sound();
420         void release_sound();
421         void update_sound(int* extra_frames);
422         //void AudioCallbackSDL(void *udata, Uint8 *stream, int len);
423
424         sdl_snddata_t snddata;
425         int sound_rate, sound_samples;
426         bool sound_ok, sound_started, now_mute;
427         SDL_AudioSpec SndSpecReq, SndSpecPresented;
428         //Uint8 iTotalVolume;
429         // direct sound
430         bool first_half;
431         
432         // record sound
433 #if defined(_USE_QT)
434         _TCHAR sound_file_name[_MAX_PATH];
435 #else
436         _TCHAR sound_file_name[AG_PATHNAME_MAX];
437 #endif
438         FILEIO* rec;
439         int rec_bytes;
440         int rec_buffer_ptr;
441
442 #else // _WIN32
443         HDC hdcDib;
444         HBITMAP hBmp, hOldBmp;
445         LPBYTE lpBuf;
446         scrntype* lpBmp;
447         LPBITMAPINFO lpDib;
448         
449 #ifdef USE_SCREEN_ROTATE
450         // rotate buffer
451         HDC hdcDibRotate;
452         HBITMAP hBmpRotate, hOldBmpRotate;
453         LPBYTE lpBufRotate;
454         scrntype* lpBmpRotate;
455         LPBITMAPINFO lpDibRotate;
456 #endif
457         
458         // stretch buffer
459         HDC hdcDibStretch1;
460         HBITMAP hBmpStretch1, hOldBmpStretch1;
461         LPBYTE lpBufStretch1;
462         scrntype* lpBmpStretch1;
463         LPBITMAPINFO lpDibStretch1;
464         
465         HDC hdcDibStretch2;
466         HBITMAP hBmpStretch2, hOldBmpStretch2;
467         LPBYTE lpBufStretch2;
468         scrntype* lpBmpStretch2;
469         LPBITMAPINFO lpDibStretch2;
470         
471         // for direct3d9
472         LPDIRECT3D9 lpd3d9;
473         LPDIRECT3DDEVICE9 lpd3d9Device;
474         LPDIRECT3DSURFACE9 lpd3d9Surface;
475         LPDIRECT3DSURFACE9 lpd3d9OffscreenSurface;
476         scrntype *lpd3d9Buffer;
477         bool render_to_d3d9Buffer;
478         bool use_d3d9;
479         bool wait_vsync;
480         // record video
481         _TCHAR video_file_name[_MAX_PATH];
482         int rec_video_fps;
483         double rec_video_run_frames;
484         double rec_video_frames;
485 #endif  
486         
487 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
488 #else
489         LPBITMAPINFO lpDibRec;
490         PAVIFILE pAVIFile;
491         PAVISTREAM pAVIStream;
492         PAVISTREAM pAVICompressed;
493         AVICOMPRESSOPTIONS opts;
494         DWORD dwAVIFileSize;
495         LONG lAVIFrames;
496         
497         HDC hdcDibRec;
498         HBITMAP hBmpRec, hOldBmpRec;
499         LPBYTE lpBufRec;
500         scrntype* lpBmpRec;
501
502         bool use_video_thread;
503         HANDLE hVideoThread;
504         video_thread_t video_thread_param;
505
506         void initialize_sound();
507         void release_sound();
508         void update_sound(int* extra_frames);
509         
510         int sound_rate, sound_samples;
511         bool sound_ok, sound_started, now_mute;
512
513 #endif
514    
515         
516         // ----------------------------------------
517         // sound
518         // ----------------------------------------
519 #if defined(_USE_AGAR) || defined(_USE_QT)
520 #else
521         // direct sound
522         LPDIRECTSOUND lpds;
523         LPDIRECTSOUNDBUFFER lpdsb, lpdsp;
524         bool first_half;
525         
526         // record sound
527         _TCHAR sound_file_name[_MAX_PATH];
528         FILEIO* rec;
529         int rec_bytes;
530         int rec_buffer_ptr;
531 #endif
532 #endif
533    
534 #if defined(_USE_AGAR)
535         // ----------------------------------------
536         // direct show
537         // ----------------------------------------
538         void initialize_display_agar();
539         void release_display_agar();
540 #elif defined(_USE_QT)
541 #ifdef USE_LASER_DISC
542         double movie_frame_rate;
543         int movie_sound_rate;
544         bool now_movie_play, now_movie_pause;
545 #endif
546 #else
547 #ifdef USE_DIRECT_SHOW
548         // ----------------------------------------
549         // direct show
550         // ----------------------------------------
551         void initialize_direct_show();
552         void release_direct_show();
553         void create_direct_show_dib_section();
554         void release_direct_show_dib_section();
555         
556         IGraphBuilder *pGraphBuilder;
557         IBaseFilter *pVideoBaseFilter;
558         IBaseFilter *pCaptureBaseFilter;
559         ICaptureGraphBuilder2 *pCaptureGraphBuilder2;
560         ISampleGrabber *pVideoSampleGrabber;
561         IBaseFilter *pSoundBaseFilter;
562         ISampleGrabber *pSoundSampleGrabber;
563 #ifdef USE_LASER_DISC
564         CMySampleGrabberCB *pSoundCallBack;
565 #endif
566         IMediaControl *pMediaControl;
567         IMediaSeeking *pMediaSeeking;
568         IMediaPosition *pMediaPosition;
569         IVideoWindow *pVideoWindow;
570         IBasicVideo *pBasicVideo;
571         IBasicAudio *pBasicAudio;
572         bool bTimeFormatFrame;
573         bool bVerticalReversed;
574         
575         HDC hdcDibDShow;
576         HBITMAP hBmpDShow, hOldBmpDShow;
577         LPBYTE lpBufDShow;
578         scrntype* lpBmpDShow;
579         LPBITMAPINFO lpDibDShow;
580         
581         int direct_show_width, direct_show_height;
582         bool direct_show_mute[2];
583 #ifdef USE_LASER_DISC
584         double movie_frame_rate;
585         int movie_sound_rate;
586         bool now_movie_play, now_movie_pause;
587 #endif
588 #ifdef USE_VIDEO_CAPTURE
589         void enum_capture_devs();
590         bool connect_capture_dev(int index, bool pin);
591         int cur_capture_dev_index;
592         int num_capture_devs;
593         _TCHAR capture_dev_name[MAX_CAPTURE_DEVS][256];
594 #endif
595 #endif
596 #endif // _WIN32
597    
598         // ----------------------------------------
599         // media
600         // ----------------------------------------
601         typedef struct {
602                 _TCHAR path[_MAX_PATH];
603                 bool play;
604                 int bank;
605                 int wait_count;
606         } media_status_t;
607         
608 #ifdef USE_CART1
609         media_status_t cart_status[MAX_CART];
610 #endif
611 #ifdef USE_FD1
612         media_status_t disk_status[MAX_FD];
613 #endif
614 #ifdef USE_QD1
615         media_status_t quickdisk_status[MAX_QD];
616 #endif
617 #ifdef USE_TAPE
618         media_status_t tape_status;
619 #endif
620 #ifdef USE_LASER_DISC
621         media_status_t laser_disc_status;
622 #endif
623         
624         void initialize_media();
625         void update_media();
626         void restore_media();
627         
628         void clear_media_status(media_status_t *status)
629         {
630                 status->path[0] = _T('\0');
631                 status->wait_count = 0;
632         }
633         
634         // ----------------------------------------
635         // printer
636         // ----------------------------------------
637         void initialize_printer();
638         void release_printer();
639         void reset_printer();
640         void update_printer();
641         void open_printer_file();
642         void close_printer_file();
643 #ifdef _USE_QT
644         _TCHAR prn_file_name[_MAX_PATH];
645 #else
646         _TCHAR prn_file_name[_MAX_PATH];
647 #endif
648         FILEIO *prn_fio;
649         int prn_data, prn_wait_frames;
650         bool prn_strobe;
651         
652 #ifdef USE_SOCKET
653         // ----------------------------------------
654         // socket
655         // ----------------------------------------
656         void initialize_socket();
657         void release_socket();
658         void update_socket();
659         
660         int soc[SOCKET_MAX];
661         bool is_tcp[SOCKET_MAX];
662 #if !defined(_USE_QT) 
663         struct sockaddr_in udpaddr[SOCKET_MAX];
664 #endif
665         int socket_delay[SOCKET_MAX];
666         char recv_buffer[SOCKET_MAX][SOCKET_BUFFER_MAX];
667         int recv_r_ptr[SOCKET_MAX], recv_w_ptr[SOCKET_MAX];
668 #endif
669         
670 #ifdef USE_DEBUGGER
671         // ----------------------------------------
672         // debugger
673         // ----------------------------------------
674         void initialize_debugger();
675         void release_debugger();
676 #if defined(_USE_AGAR)
677         AG_Thread hDebuggerThread;
678 #elif defined(_USE_QT)
679         CSP_Debugger *hDebugger;
680         QThread *hDebuggerThread;
681 #else
682         HANDLE hDebuggerThread;
683 #endif
684         debugger_thread_t debugger_thread_param;
685 #endif
686         
687 #ifdef _DEBUG_LOG
688         // ----------------------------------------
689         // debug log
690         // ----------------------------------------
691         void initialize_debug_log();
692         void release_debug_log();
693         FILE* debug_log;
694 #endif
695         
696 #ifdef USE_STATE
697         // ----------------------------------------
698         // state
699         // ----------------------------------------
700         void save_state_tmp(_TCHAR* file_path);
701         bool load_state_tmp(_TCHAR* file_path);
702 #endif
703         
704         // ----------------------------------------
705         // misc
706         // ----------------------------------------
707 #ifdef USE_CPU_TYPE
708         int cpu_type;
709 #endif
710 #ifdef USE_SOUND_DEVICE_TYPE
711         int sound_device_type;
712 #endif
713         _TCHAR app_path[_MAX_PATH];
714         bool now_suspended;
715         
716 public:
717         bool bDrawLine[SCREEN_HEIGHT];
718         uint32 *getJoyStatPtr(void) {
719                 return joy_status;
720         }
721    
722         // ----------------------------------------
723         // initialize
724         // ----------------------------------------
725 #if defined(_USE_AGAR)
726         EMU(AG_Window *hwnd, AG_Widget *hinst);
727 #elif defined(_USE_QT)
728         EMU(class Ui_MainWindow*, GLDrawClass*);
729 #else
730         EMU(HWND hwnd, HINSTANCE hinst);
731 #endif
732         ~EMU();
733
734         _TCHAR* application_path()
735         {
736                 return app_path;
737         }
738         _TCHAR* bios_path(_TCHAR* file_name);
739 #if defined(_USE_AGAR)
740         // To be thread safety.
741         void LockVM(void) {
742            if(pVMSemaphore) SDL_SemWait(pVMSemaphore);
743         }
744         void UnlockVM(void) {
745            if(pVMSemaphore) SDL_SemPost(pVMSemaphore);
746         }
747 #elif defined(_USE_QT)
748         void LockVM(void) {
749                 VMSemaphore->acquire();
750         }
751         void UnlockVM(void) {
752                 VMSemaphore->release();
753         }
754 #else // M$ VC
755         void LockVM(void) {
756         }
757         void UnlockVM(void) {
758         }
759 #endif
760 #ifdef _USE_QT
761         QImage *getPseudoVramClass(void) { return pPseudoVram;}
762 #endif
763         // ----------------------------------------
764         // for windows
765         // ----------------------------------------
766 #if defined(_USE_AGAR)
767         AG_Window *main_window_handle;
768         AG_Widget *instance_handle;
769         bool use_opengl;
770         bool use_opencl;
771 #elif defined(_USE_QT)
772         class Ui_MainWindow *main_window_handle;
773         GLDrawClass *instance_handle;
774         bool use_opengl;
775         bool use_opencl;
776 #else
777         HWND main_window_handle;
778         HINSTANCE instance_handle;
779         bool vista_or_later;
780 #endif  
781         // drive virtual machine
782         int frame_interval();
783         int run();
784         void reset();
785 #ifdef USE_SPECIAL_RESET
786         void special_reset();
787 #endif
788 #ifdef USE_POWER_OFF
789         void notify_power_off();
790 #endif
791         void suspend();
792         
793         // media
794 #ifdef USE_FD1
795         struct {
796                 _TCHAR path[_MAX_PATH];
797 //              struct {
798 //                      _TCHAR name[128]; // Convert to UTF8
799 //              //      int offset;
800 //              } bank[MAX_D88_BANKS];
801                 _TCHAR disk_name[MAX_D88_BANKS][128];  // Convert to UTF8
802                 int bank_num;
803                 int cur_bank;
804         } d88_file[MAX_FD];
805 #endif
806        int get_access_lamp(void);
807         
808         // user interface
809 #ifdef USE_CART1
810         void open_cart(int drv, _TCHAR* file_path);
811         void close_cart(int drv);
812         bool cart_inserted(int drv);
813 #endif
814 #ifdef USE_FD1
815         void open_disk(int drv, _TCHAR* file_path, int bank);
816         void close_disk(int drv);
817         bool disk_inserted(int drv);
818         bool is_write_protected_fd(int drv);
819         void write_protect_fd(int drv, bool flag);
820 #endif
821 #ifdef USE_QD1
822         void open_quickdisk(int drv, _TCHAR* file_path);
823         void close_quickdisk(int drv);
824         bool quickdisk_inserted(int drv);
825 #endif
826 #ifdef USE_TAPE
827         void play_tape(_TCHAR* file_path);
828         void rec_tape(_TCHAR* file_path);
829         void close_tape();
830         bool tape_inserted();
831 # ifdef USE_TAPE_PTR
832         int get_tape_ptr() {
833            return vm->get_tape_ptr();
834         }
835 # endif
836 #endif
837 #ifdef USE_TAPE_BUTTON
838         void push_play();
839         void push_stop();
840         void push_fast_forward();
841         void push_fast_rewind();
842         void push_apss_forward();
843         void push_apss_rewind();
844         bool get_tape_play(void)
845         {
846                 return vm->get_tape_play();
847         }
848 #endif
849 #ifdef USE_LASER_DISC
850         void open_laser_disc(_TCHAR* file_path);
851         void close_laser_disc();
852         bool laser_disc_inserted();
853 #endif
854 #ifdef USE_BINARY_FILE1
855         void load_binary(int drv, _TCHAR* file_path);
856         void save_binary(int drv, _TCHAR* file_path);
857 #endif
858         bool now_skip();
859         
860         void start_rec_sound();
861         void stop_rec_sound();
862         void restart_rec_sound();
863         bool now_rec_sound;
864         
865         void capture_screen();
866         bool start_rec_video(int fps);
867         void stop_rec_video();
868         void restart_rec_video();
869         bool now_rec_video;
870         
871         void update_config();
872         
873 #ifdef USE_STATE
874         void save_state();
875         void load_state();
876 #endif
877         
878         // input device
879 #if defined(_USE_QT)
880        void key_mod(uint32 mod) {
881             modkey_status = mod;
882        }
883 #endif
884         void key_down(int code, bool repeat);
885         void key_up(int code);
886         void key_lost_focus()
887         {
888                 lost_focus = true;
889         }
890         uint32 recent_key_sym;
891         uint32 recent_key_mod;
892         uint32 recent_key_unicode;
893 #ifdef USE_BUTTON
894         void press_button(int num);
895 #endif
896         
897         void enable_mouse();
898         void disenable_mouse();
899         void toggle_mouse();
900         bool get_mouse_enabled()
901         {
902                 return mouse_enabled;
903         }
904         
905 #ifdef USE_AUTO_KEY
906         void start_auto_key();
907         void stop_auto_key();
908         bool now_auto_key()
909         {
910                 return (autokey_phase != 0);
911         }
912 #endif
913         
914         // screen
915         int get_window_width(int mode);
916         int get_window_height(int mode);
917         void set_display_size(int width, int height, bool window_mode);
918         int draw_screen();
919 #if defined(_USE_AGAR)
920         void update_screen(AG_Widget *target);
921 #elif defined(_USE_QT)
922         void update_screen(GLDrawClass *glv);
923 #else
924         void update_screen(HDC hdc);
925 #endif
926 #ifdef USE_BITMAP
927         void reload_bitmap()
928         {
929                 first_invalidate = true;
930         }
931 #endif
932         
933         // sound
934         void mute_sound();
935         
936 #ifdef USE_VIDEO_CAPTURE
937         // video capture
938         int get_cur_capture_dev_index()
939         {
940                 return cur_capture_dev_index;
941         }
942         int get_num_capture_devs()
943         {
944                 return num_capture_devs;
945         }
946         _TCHAR* get_capture_dev_name(int index)
947         {
948                 return capture_dev_name[index];
949         }
950         void open_capture_dev(int index, bool pin);
951         void close_capture_dev();
952         void show_capture_dev_filter();
953         void show_capture_dev_pin();
954         void show_capture_dev_source();
955 #endif
956         
957 #ifdef USE_SOCKET
958         // socket
959         int get_socket(int ch)
960         {
961                 return soc[ch];
962         }
963         void socket_connected(int ch);
964         void socket_disconnected(int ch);
965         void send_data(int ch);
966         void recv_data(int ch);
967 #endif
968         
969 #ifdef USE_DEBUGGER
970         // debugger
971         void open_debugger(int cpu_index);
972         void close_debugger();
973         bool debugger_enabled(int cpu_index);
974         bool now_debugging;
975 #endif
976         
977         // ----------------------------------------
978         // for virtual machine
979         // ----------------------------------------
980         
981         // power off
982         void power_off()
983         {
984 #if defined(_USE_AGAR)
985
986 #elif defined(_USE_QT)
987            
988 #else
989                 PostMessage(main_window_handle, WM_CLOSE, 0, 0L);
990 #endif
991         }
992         
993         // input device
994         uint8* key_buffer()
995         {
996                 return key_status;
997         }
998         uint32* joy_buffer()
999         {
1000                 return joy_status;
1001         }
1002         int* mouse_buffer()
1003         {
1004                 return mouse_status;
1005         }
1006         
1007         // screen
1008         void change_screen_size(int sw, int sh, int swa, int sha, int ww, int wh);
1009         int get_screen_width_aspect(void) {return screen_width_aspect;}
1010         int get_screen_height_aspect(void) {return screen_height_aspect;}
1011         scrntype* screen_buffer(int y);
1012 #ifdef USE_CRT_FILTER
1013         bool screen_skip_line;
1014 #endif
1015         
1016         // timer
1017         void get_host_time(cur_time_t* time);
1018         
1019         // printer
1020         void printer_out(uint8 value);
1021         void printer_strobe(bool value);
1022         
1023 #ifdef USE_DIRECT_SHOW
1024         // direct show
1025         void get_direct_show_buffer();
1026         void mute_direct_show_dev(bool l, bool r);
1027         
1028 #ifdef USE_LASER_DISC
1029         bool open_movie_file(_TCHAR* file_path);
1030         void close_movie_file();
1031         
1032         void play_movie();
1033         void stop_movie();
1034         void pause_movie();
1035         
1036         double get_movie_frame_rate()
1037         {
1038                 return movie_frame_rate;
1039         }
1040         int get_movie_sound_rate()
1041         {
1042                 return movie_sound_rate;
1043         }
1044         void set_cur_movie_frame(int frame, bool relative);
1045         uint32 get_cur_movie_frame();
1046 #endif
1047 #ifdef USE_VIDEO_CAPTURE
1048         void set_capture_dev_channel(int ch);
1049 #endif
1050 #endif
1051         
1052 #ifdef USE_SOCKET
1053         // socket
1054         bool init_socket_tcp(int ch);
1055         bool init_socket_udp(int ch);
1056         bool connect_socket(int ch, uint32 ipaddr, int port);
1057         void disconnect_socket(int ch);
1058         bool listen_socket(int ch);
1059         void send_data_tcp(int ch);
1060         void send_data_udp(int ch, uint32 ipaddr, int port);
1061 #endif
1062         
1063         // debug log
1064         void out_debug_log(const _TCHAR* format, ...);
1065         
1066         void out_message(const _TCHAR* format, ...);
1067         int message_count;
1068         _TCHAR message[_MAX_PATH];
1069 };
1070 #endif
1071
1072