1 // -----------------------------------------------------------------------
2 // file:        msxgr.cpp
3 // version:     0.5d (October 22, 2005)
4 //
5 // description: This is an unofficial API to the MSX Game Reader. Most
6 //              of the interface is based on assumptions and tests.
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 2, or (at your option)
11 //  any later version. See COPYING for more details.
12 //
13 // Copyright 2005 Vincent van Dam (vincentd@erg.verweg.com)
14 // -----------------------------------------------------------------------
15 
16 #include "msxgr.h"
17 
18 #ifdef WII
19 // Not supported on WII
20 #else
Init()21 int CMSXGr::Init()
22 {
23 	// Load DLL.
24 	hLib = LoadLibrary("MSXGr.dll");
25 
26 	if (!hLib)	{
27 		// DLL could not be found.
28 		return -1;
29 	}
30 
31 	_MSXGR_Init MSXGR_Init =
32 		(_MSXGR_Init)GetProcAddress(hLib, "MSXGR_Init");
33 
34 	if (MSXGR_Init == NULL) {
35 		// MSXGR_Init method not found, assume corrupt DLL.
36 		return -1;
37 	}
38 
39 	nLastError = MSXGR_Init();
40 
41 	if (nLastError) {
42 		// Error during initialisation
43 		FreeLibrary(hLib);
44 		return nLastError;
45 	}
46 
47 	// initialiase funtion pointers
48 	MSXGR_Err2Str = (_MSXGR_Err2Str)GetProcAddress(hLib, "MSXGR_Err2Str");
49 	MSXGR_GetVersion = (_MSXGR_GetVersion)GetProcAddress(hLib, "MSXGR_GetVersion");
50 	MSXGR_SetDebugMode = (_MSXGR_SetDebugMode)GetProcAddress(hLib, "MSXGR_SetDebugMode");
51 	MSXGR_IsSlotEnable = (_MSXGR_IsSlotEnable)GetProcAddress(hLib, "MSXGR_IsSlotEnable");
52 	MSXGR_GetSlotStatus = (_MSXGR_GetSlotStatus)GetProcAddress(hLib, "MSXGR_GetSlotStatus");
53 	MSXGR_ReadMemory = (_MSXGR_ReadMemory)GetProcAddress(hLib, "MSXGR_ReadMemory");
54 	MSXGR_WriteMemory =	(_MSXGR_WriteMemory)GetProcAddress(hLib, "MSXGR_WriteMemory");
55 	MSXGR_WriteIO =	(_MSXGR_WriteIO)GetProcAddress(hLib, "MSXGR_WriteIO");
56 	MSXGR_ReadIO = (_MSXGR_ReadIO)GetProcAddress(hLib, "MSXGR_ReadIO");
57 
58 	// Wait for the driver to attach the game reader(s)
59 	int nSlot;
60 	int nTry=0;
61 	while (nTry<5) {
62 		Sleep(300);
63 		for (nSlot=0;nSlot<16;nSlot++)
64 			if (IsSlotEnable(nSlot)) nTry = 5;
65 	}
66 
67 	return 0;
68 }
69 
70 
Uninit()71 void CMSXGr::Uninit()
72 {
73 	if (!hLib)	{
74 		// DLL not loaded
75 		return;
76 	}
77 
78 	_MSXGR_Uninit MSXGR_Uninit =
79 		(_MSXGR_Uninit)GetProcAddress(hLib, "MSXGR_Uninit");
80 
81 	// uninit msxgr
82 	if (MSXGR_Uninit) {
83 		MSXGR_Uninit();
84 	}
85 
86 	// unload dll
87 	FreeLibrary(hLib);
88 	hLib = NULL;
89 
90 	return;
91 }
92 
Err2Str(int nError)93 char* CMSXGr::Err2Str(int nError)
94 {
95 	return MSXGR_Err2Str(nError);
96 }
97 
98 
GetLastErrorStr()99 char* CMSXGr::GetLastErrorStr()
100 {
101 	return Err2Str(nLastError);
102 }
103 
GetVersion()104 int CMSXGr::GetVersion()
105 {
106 	return MSXGR_GetVersion();
107 }
108 
SetDebugMode(int nLevel)109 void CMSXGr::SetDebugMode(int nLevel)
110 {
111 	MSXGR_SetDebugMode(nLevel);
112 	return;
113 }
114 
IsSlotEnable(int nSlot)115 bool CMSXGr::IsSlotEnable(int nSlot)
116 {
117 	return MSXGR_IsSlotEnable(nSlot);
118 }
119 
GetSlotStatus(int nSlot,int * pBuffer)120 int CMSXGr::GetSlotStatus(int nSlot,int *pBuffer)
121 {
122 	// pBuffer[0] = 01 slot enable
123 	// pBuffer[1] = ff cartridge inserted
124 	// pBuffer[2] = ?
125 	nLastError = MSXGR_GetSlotStatus(nSlot,pBuffer);
126 	return nLastError;
127 }
128 
IsCartridgeInserted(int nSlot)129 bool CMSXGr::IsCartridgeInserted(int nSlot)
130 {
131 	int Status[3];
132 	int nError = GetSlotStatus(nSlot,Status);
133 	return !nError && Status[1];
134 }
135 
ReadMemory(int nSlot,char * pBuffer,int nAddress,int nLength)136 int CMSXGr::ReadMemory(int nSlot,char* pBuffer,int nAddress,int nLength)
137 {
138 	nLastError = MSXGR_ReadMemory(nSlot,pBuffer,nAddress,nLength);
139 	return nLastError;
140 }
141 
WriteMemory(int nSlot,char * pBuffer,int nAddress,int nLength)142 int CMSXGr::WriteMemory(int nSlot,char* pBuffer,int nAddress,int nLength)
143 {
144 	nLastError = MSXGR_WriteMemory(nSlot,pBuffer,nAddress,nLength);
145 	return nLastError;
146 }
147 
WriteIO(int nSlot,char * pBuffer,int nAddress,int nLength)148 int CMSXGr::WriteIO(int nSlot,char* pBuffer,int nAddress,int nLength)
149 {
150 	nLastError = MSXGR_WriteIO(nSlot,pBuffer,nAddress,nLength);
151 	return nLastError;
152 }
153 
154 
ReadIO(int nSlot,char * pBuffer,int nAddress,int nLength)155 int CMSXGr::ReadIO(int nSlot,char* pBuffer,int nAddress,int nLength)
156 {
157 	nLastError = MSXGR_ReadIO(nSlot,pBuffer,nAddress,nLength);
158 	return nLastError;
159 }
160 #endif
161 
162