1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - tools - loadrun
3   File:     isd_api.c
4 
5   Copyright 2007-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-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include <windows.h>
18 #include "isd_api.h"
19 
20 //---- IS-dll functions
21 ISD_STREAMRECVCB ISTD_StreamRecvCB;
22 ISD_PRINTFRECVCB ISTD_PrintfRecvCB;
23 ISD_ENUMDEVICECB ISTD_EnumDeviceCB;
24 ISD_GETVERSION   ISTD_GetVersion;
25 //NITROTOOLAPI_FINDOPEN ISNTD_FindOpen;
26 ISD_FINDOPEN     ISTD_FindOpen;
27 //NITROTOOLAPI_FINDNEXT ISNTD_FindNext;
28 ISD_FINDNEXT     ISTD_FindNext;
29 //NITROTOOLAPI_FINDCLOSE ISNTD_FindClose;
30 ISD_FINDCLOSE    ISTD_FindClose;
31 //NITROTOOLAPI_DEVSELECTOR ISNTD_Selector;
32 ISD_DEVSELECTOR  ISTD_DevSelector;
33 //NITROTOOLAPI_OPEN ISNTD_DeviceOpen;
34 ISD_OPEN         ISTD_Open;
35 ISD_OPENEX       ISTD_OpenEx;
36 //NITROTOOLAPI_CHECKCONNECT ISNTD_CheckConnect;
37 //NITROTOOLAPI_CLOSE ISNTD_DeviceClose;
38 ISD_CLOSE        ISTD_Close;
39 ISD_GETDEVICEID  ISTD_GetDeviceID;
40 ISD_CHECKCONNECT ISTD_CheckConnect;
41 ISD_SETHYBRIDMODE ISTD_SetHybridMode;
42 ISD_DOWNLOADGO   ISTD_DownloadGo;
43 //NITROTOOLAPI_RESET ISNTD_Reset;
44 ISD_RESET        ISTD_Reset;
45 //NITROTOOLAPI_WRITEROM ISNTD_WriteROM;
46 ISD_WRITEROM     ISTD_WriteROM;
47 //NITROTOOLAPI_READROM ISNTD_ReadROM;
48 ISD_READROM      ISTD_ReadROM;
49 //NITROTOOLAPI_ROMSIZE ISNTD_GetROMSize;
50 ISD_ROMSIZE      ISTD_RomSize;
51 //NITROTOOLAPI_STREAMSETCB ISNTD_SetReceiveStreamCallBackFunction;
52 //ISD_STREAMSETCB  ISTD_StreamSetCB;
53 ISD_PRINTFSETCB  ISTD_PrintfSetCB;
54 //NITROTOOLAPI_GETLASTERROR ISNTD_GetLastError;
55 ISD_GETLASTERROR ISTD_GetLastError;
56 //NITROTOOLAPI_GETERRORMESSAGE ISNTD_GetLastErrorMessage;
57 ISD_GETERRORMESSAGE ISTD_GetErrorMessage;
58 //NITROTOOLAPI_CHECKPOWER ISNTD_GetPowerStatus;
59 //ISD_CHECKPOWER not implemented
60 //NITROTOOLAPI_STREAMWRITE ISNTD_StreamWrite;
61 //ISD_STREAMWRITE Not implemented
62 //NITROTOOLAPI_STREAMGETWRITABLELEN ISNTD_StreamGetWritableLength;
63 //IISD_STREAMGETWRITABLELEN Not implemented
64 //NITROTOOLAPI_CARDSLOTPOWER ISNTD_CardSlotPower;
65 //ISD_CARDSLOTPOWER Not implemented
66 
67 //No corresponding functions below
68 //NITROTOOLAPI_SYNCWRITE ISNTD_SyncWriteROM;
69 //NITROTOOLAPI_SYNCREAD ISNTD_SyncReadROM;
70 //NITROTOOLAPI_STREAMCHKRECV ISNTD_StreamCheckStreamReceive;
71 //NITROTOOLAPI_SETSYNCTIMEOUT ISNTD_SetSyncTimeOut;
72 //NITROTOOLAPI_GETDEBUGPRINT ISNTD_GetDebugPrint;
73 //NITROTOOLAPI_CARTRIDGESLOTPOWER ISNTD_CartridgeSlotPower;
74 
75 //---- flag of initialized already
76 static BOOL ISTDi_IsInitialized = FALSE;
77 
78 //---- dll instance
79 HINSTANCE ISTD_DllInstance;
80 
81 
82 #ifdef _declDllFunc
83 #undef _declDllFunc
84 #endif
85 #define _declDllFunc(type,name) (ISD_##type)GetProcAddress(ISTD_DllInstance, ISD_FUNC_##name)
86 
87 /*---------------------------------------------------------------------------*
88   Name:         ISTD_InitDll
89 
90   Description:  initialize IS-dll functions
91 
92   Arguments:    None.
93 
94   Returns:      TRUE if success. FALSE if called already.
95  *---------------------------------------------------------------------------*/
ISTD_InitDll(void)96 BOOL ISTD_InitDll(void)
97 {
98     //---- check initialized already
99     if (ISTDi_IsInitialized)
100     {
101         return FALSE;
102     }
103     ISTDi_IsInitialized = TRUE;
104 
105     //---- load dll
106     ISTD_DllInstance = LoadLibrary(ISDDLL_NAME);
107 
108 //	ISTD_StreamRecvCB    = _declDllFunc(STREAMRECVCB, STREAMRECVCB);
109 //	ISTD_PrintfRecvCB    = _declDllFunc(PRINTFRECVCB, PRINTFRECVCB);
110 //	ISTD_EnumDeviceCB    = _declDllFunc(ENUMDEVIVECV, ENUMDEVIVECV);
111 	ISTD_GetVersion      = _declDllFunc(GETVERSION, GETVERSION);
112     ISTD_FindOpen        = _declDllFunc(FINDOPEN, FINDOPEN);
113     ISTD_FindNext        = _declDllFunc(FINDNEXT, FINDNEXT);
114 	ISTD_FindClose       = _declDllFunc(FINDCLOSE, FINDCLOSE);
115 //	ISTD_DevSelector     = _declDllFunc(DEVSELECTOR, DEVSELECTOR);
116 	ISTD_Open            = _declDllFunc(OPEN, OPEN);
117 	ISTD_OpenEx          = _declDllFunc(OPENEX, OPENEX);
118 	ISTD_Close           = _declDllFunc(CLOSE, CLOSE);
119 	ISTD_GetDeviceID     = _declDllFunc(GETDEVICEID, GETDEVICEID);
120 	ISTD_CheckConnect    = _declDllFunc(CHECKCONNECT, CHECKCONNECT);
121 	ISTD_DownloadGo      = _declDllFunc(DOWNLOADGO, DOWNLOADGO);
122 	ISTD_SetHybridMode   = _declDllFunc(SETHYBRIDMODE, MODEHYBRID);
123 	ISTD_Reset           = _declDllFunc(RESET, RESET);
124     ISTD_WriteROM        = _declDllFunc(WRITEROM, WRITEROM);
125 	ISTD_ReadROM         = _declDllFunc(READROM, READROM);
126 	ISTD_RomSize         = _declDllFunc(ROMSIZE, ROMSIZE);
127 //	ISTD_StreamSetCB     = _declDllFunc(STREAMSETCB, STREAMSETCB);
128 	ISTD_PrintfSetCB     = _declDllFunc(PRINTFSETCB, PRINTFSETCB);
129 	ISTD_GetLastError    = _declDllFunc(GETLASTERROR, GETLASTERROR);
130 	ISTD_GetErrorMessage = _declDllFunc(GETERRORMESSAGE, GETERRORMESSAGE);
131 
132     return TRUE;
133 }
134 
135 /*---------------------------------------------------------------------------*
136   Name:         ISTD_FreeDll
137 
138   Description:  free dll module
139 
140   Arguments:    None.
141 
142   Returns:      TRUE   if success
143  *---------------------------------------------------------------------------*/
ISTD_FreeDll(void)144 BOOL ISTD_FreeDll(void)
145 {
146     //---- check initialized already
147     if (!ISTDi_IsInitialized)
148     {
149         return FALSE;
150     }
151 
152     FreeLibrary(ISTD_DllInstance);
153     return TRUE;
154 }
155 
156 /*---------------------------------------------------------------------------*
157   Name:         ISTD_GetDeviceList
158 
159   Description:  get connecting device list
160 
161   Arguments:    deviceList : pointer of list
162                 deviceMax  : max number of list
163 
164   Returns:      -1 if FAIL
165                 >=0 value is num of device which is connected
166  *---------------------------------------------------------------------------*/
ISTD_GetDeviceList(ISTDDevice * deviceList,int deviceMax)167 int ISTD_GetDeviceList(ISTDDevice * deviceList, int deviceMax)
168 {
169     ISDFINDHANDLE finder;
170     int     count = 0;
171     ISDDEVICEID id;
172     ISTDDevice *p = deviceList;
173 
174     //---- check device list
175     if (!deviceList)
176     {
177         return -1;
178     }
179 
180     //---- search devices
181     finder = ISTD_FindOpen();
182     if (!finder)
183     {
184         return -1;
185     }
186 
187     //---- clear
188     {
189         int     n;
190         for (n = 0; n < deviceMax; n++)
191         {
192             deviceList[n].type = ISTD_DEVICE_NONE;
193         }
194     }
195 
196     //---- store devices id to list
197     while (ISTD_FindNext(finder, &id) && count < deviceMax)
198     {
199         p->id = id;
200 		switch( p->id.eType )
201 		{
202 			case ISDDEV_ISTWLEMU:
203 				p->type = ISTD_DEVICE_IS_TWL_EMULATOR;
204 				p->serial = id.nSerial;
205 				break;
206 			case ISDDEV_ISTWLDBG:
207 				p->type = ISTD_DEVICE_IS_TWL_DEBUGGER;
208 				p->serial = id.nSerial;
209 				break;
210 			default:
211 				p->type = ISTD_DEVICE_UNKNOWN;
212 				p->serial = 0;
213 				break;
214 		}
215 
216         p++;
217         count++;
218     }
219 
220     return count;
221 }
222