1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - tools - loadrun
3   File:     isd_api.c
4 
5   Copyright 2005-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include <windows.h>
18 #include "ISNITRODLL.h"
19 #include "isd_api.h"
20 
21 //---- IS-dll functions
22 NITROTOOLAPI_FINDOPEN ISNTD_FindOpen;
23 NITROTOOLAPI_FINDNEXT ISNTD_FindNext;
24 NITROTOOLAPI_FINDCLOSE ISNTD_FindClose;
25 NITROTOOLAPI_DEVSELECTOR ISNTD_Selector;
26 NITROTOOLAPI_OPEN ISNTD_DeviceOpen;
27 NITROTOOLAPI_CHECKCONNECT ISNTD_CheckConnect;
28 NITROTOOLAPI_CLOSE ISNTD_DeviceClose;
29 NITROTOOLAPI_WRITEROM ISNTD_WriteROM;
30 NITROTOOLAPI_READROM ISNTD_ReadROM;
31 NITROTOOLAPI_RESET ISNTD_Reset;
32 NITROTOOLAPI_SYNCWRITE ISNTD_SyncWriteROM;
33 NITROTOOLAPI_SYNCREAD ISNTD_SyncReadROM;
34 NITROTOOLAPI_STREAMWRITE ISNTD_StreamWrite;
35 NITROTOOLAPI_STREAMGETWRITABLELEN ISNTD_StreamGetWritableLength;
36 NITROTOOLAPI_STREAMSETCB ISNTD_SetReceiveStreamCallBackFunction;
37 NITROTOOLAPI_STREAMCHKRECV ISNTD_StreamCheckStreamReceive;
38 NITROTOOLAPI_SETSYNCTIMEOUT ISNTD_SetSyncTimeOut;
39 NITROTOOLAPI_GETDEBUGPRINT ISNTD_GetDebugPrint;
40 NITROTOOLAPI_ROMSIZE ISNTD_GetROMSize;
41 NITROTOOLAPI_CHECKPOWER ISNTD_GetPowerStatus;
42 NITROTOOLAPI_CARDSLOTPOWER ISNTD_CardSlotPower;
43 NITROTOOLAPI_CARTRIDGESLOTPOWER ISNTD_CartridgeSlotPower;
44 NITROTOOLAPI_GETLASTERROR ISNTD_GetLastError;
45 NITROTOOLAPI_GETERRORMESSAGE ISNTD_GetLastErrorMessage;
46 
47 
48 //---- flag of initialized already
49 static BOOL ISNTDi_IsInitialized = FALSE;
50 
51 //---- dll instance
52 HINSTANCE ISNTD_DllInstance;
53 
54 
55 #ifdef _declDllFunc
56 #undef _declDllFunc
57 #endif
58 #define _declDllFunc(type,name) (NITROTOOLAPI_##type)GetProcAddress(ISNTD_DllInstance, NITRO_FUNC_##name)
59 
60 /*---------------------------------------------------------------------------*
61   Name:         ISNTD_InitDll
62 
63   Description:  initialize IS-dll functions
64 
65   Arguments:    None
66 
67   Returns:      TRUE if success. FALSE if called already.
68  *---------------------------------------------------------------------------*/
ISNTD_InitDll(void)69 BOOL ISNTD_InitDll(void)
70 {
71     //---- check initialized already
72     if (ISNTDi_IsInitialized)
73     {
74         return FALSE;
75     }
76     ISNTDi_IsInitialized = TRUE;
77 
78     //---- load dll
79     ISNTD_DllInstance = LoadLibrary(NITROEMUDLL_NAME);
80 
81     ISNTD_FindOpen = _declDllFunc(FINDOPEN, FINDOPEN);
82     ISNTD_FindNext = _declDllFunc(FINDNEXT, FINDNEXT);
83     ISNTD_FindClose = _declDllFunc(FINDCLOSE, FINDCLOSE);
84     ISNTD_Selector = _declDllFunc(DEVSELECTOR, SELECTOR);
85     ISNTD_DeviceOpen = _declDllFunc(OPEN, OPEN);
86     ISNTD_CheckConnect = _declDllFunc(CHECKCONNECT, CHECKCONNECT);
87     ISNTD_DeviceClose = _declDllFunc(CLOSE, CLOSE);
88     ISNTD_WriteROM = _declDllFunc(WRITEROM, WRITEROM);
89     ISNTD_ReadROM = _declDllFunc(READROM, READROM);
90     ISNTD_Reset = _declDllFunc(RESET, RESET);
91     ISNTD_SyncWriteROM = _declDllFunc(SYNCWRITE, SYNC_WRITEROM);
92     ISNTD_SyncReadROM = _declDllFunc(SYNCREAD, SYNC_READROM);
93     ISNTD_StreamWrite = _declDllFunc(STREAMWRITE, STREAM_WRITE);
94     ISNTD_StreamGetWritableLength = _declDllFunc(STREAMGETWRITABLELEN, STREAM_GETWL);
95     ISNTD_SetReceiveStreamCallBackFunction = _declDllFunc(STREAMSETCB, STREAM_SETCB);
96     ISNTD_StreamCheckStreamReceive = _declDllFunc(STREAMCHKRECV, STREAM_CHECK);
97     ISNTD_SetSyncTimeOut = _declDllFunc(SETSYNCTIMEOUT, SET_SYNCTIMEOUT);
98     ISNTD_GetDebugPrint = _declDllFunc(GETDEBUGPRINT, DEBUGPRINT);
99     ISNTD_GetROMSize = _declDllFunc(ROMSIZE, ROMSIZE);
100     ISNTD_GetPowerStatus = _declDllFunc(CHECKPOWER, CHECKPOWER);
101     ISNTD_CardSlotPower = _declDllFunc(CARDSLOTPOWER, CARDSLOTPOWER);
102     ISNTD_CartridgeSlotPower = _declDllFunc(CARTRIDGESLOTPOWER, CARTSLOTPOWER);
103     ISNTD_GetLastError = _declDllFunc(GETLASTERROR, GETLASTERROR);
104     ISNTD_GetLastErrorMessage = _declDllFunc(GETERRORMESSAGE, GETERRORMESSAGE);
105 
106     return TRUE;
107 }
108 
109 /*---------------------------------------------------------------------------*
110   Name:         ISNTD_FreeDll
111 
112   Description:  free dll module
113 
114   Arguments:    None
115 
116   Returns:      TRUE if success
117  *---------------------------------------------------------------------------*/
ISNTD_FreeDll(void)118 BOOL ISNTD_FreeDll(void)
119 {
120     //---- check initialized already
121     if (!ISNTDi_IsInitialized)
122     {
123         return FALSE;
124     }
125 
126     FreeLibrary(ISNTD_DllInstance);
127     return TRUE;
128 }
129 
130 /*---------------------------------------------------------------------------*
131   Name:         ISNTD_GetDeviceList
132 
133   Description:  get connecting device list
134 
135   Arguments:    deviceList : pointer of list
136                 deviceMax  : max number of list
137 
138   Returns:      -1 if FAIL
139                 >=0 value is num of device which is connected
140  *---------------------------------------------------------------------------*/
ISNTD_GetDeviceList(ISNTDDevice * deviceList,int deviceMax)141 int ISNTD_GetDeviceList(ISNTDDevice * deviceList, int deviceMax)
142 {
143     NITROFINDHANDLE finder;
144     int     count = 0;
145     NITRODEVICEID id;
146     ISNTDDevice *p = deviceList;
147 
148 
149     //---- check device list
150     if (!deviceList)
151     {
152         return -1;
153     }
154 
155     //---- search devices
156     finder = ISNTD_FindOpen();
157     if (!finder)
158     {
159         return -1;
160     }
161 
162     //---- clear
163     {
164         int     n;
165         for (n = 0; n < deviceMax; n++)
166         {
167             deviceList[n].type = ISNTD_DEVICE_NONE;
168         }
169     }
170 
171     //---- store devices id to list
172     while (ISNTD_FindNext(finder, &id) && count < deviceMax)
173     {
174         p->ntdId = id;
175 
176         if (IS_DEVICE_CGBUSB(id))
177         {
178             p->type = ISNTD_DEVICE_CGB_EMULATOR_USB;
179             p->serial = GET_CGBUSB_SERIAL(id);
180         }
181         else if (IS_DEVICE_CGBSCSI(id))
182         {
183             p->type = ISNTD_DEVICE_CGB_EMULATOR_SCSI;
184             p->host = (int)GET_CGBSCSI_HOST(id);
185             p->serial = (int)GET_NITROUSB_SERIAL(id);
186         }
187         else if (IS_DEVICE_NITROUSB(id))
188         {
189             p->type = ISNTD_DEVICE_IS_NITRO_EMULATOR;
190             p->serial = (int)GET_NITROUSB_SERIAL(id);
191         }
192         else if (IS_DEVICE_NITROUIC(id))
193         {
194             p->type = ISNTD_DEVICE_IS_NITRO_UIC;
195             p->serial = (int)GET_NITROUIC_SERIAL(id);
196         }
197         else
198         {
199             p->type = ISNTD_DEVICE_UNKNOWN;
200             p->serial = 0;
201         }
202 
203         p++;
204         count++;
205     }
206 
207     return count;
208 }
209