-
Notifications
You must be signed in to change notification settings - Fork 10
/
Mapping.cpp
304 lines (255 loc) · 7.81 KB
/
Mapping.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Project 64 - A Nintendo 64 emulator.
*
* (c) Copyright 2001 zilmar ([email protected]) and
* Jabo ([email protected]).
*
* pj64 homepage: www.pj64.net
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <windows.h>
#include <stdio.h>
#if (!defined(EXTERNAL_RELEASE))
#include "main.h"
#include "R4300iCommands.h"
#include "BreakPoints.h"
#include "mapping.h"
#include "unzip.h"
MAP_ENTRY * MapTable = NULL;
DWORD NoOfMapEntries;
char strLabelName[100];
void GetMapDirectory ( char * Directory );
int ProcessMapFile ( BYTE * File, DWORD FileLen );
void SetMapDirectory ( char * Directory );
void AddMapEntry ( DWORD Address, char * Label) {
if (Label == NULL) { return; }
if (NoOfMapEntries == 0) {
NoOfMapEntries = 1;
MapTable = malloc(sizeof(MAP_ENTRY));
} else {
NoOfMapEntries += 1;
MapTable = realloc(MapTable, sizeof(MAP_ENTRY) * NoOfMapEntries);
}
MapTable[NoOfMapEntries - 1].VAddr = Address;
strcpy(MapTable[NoOfMapEntries - 1].Label, Label);
}
void ChooseMapFile ( HWND hWnd ) {
OPENFILENAME OpenFileName;
char Directory[255], MapFileName[255];
memset(&MapFileName, 0, sizeof(MapFileName));
memset(&OpenFileName, 0, sizeof(OpenFileName));
GetCurrentDirectory( 255, Directory );
GetMapDirectory( Directory );
OpenFileName.lStructSize = sizeof( OpenFileName );
OpenFileName.hwndOwner = hWnd;
OpenFileName.lpstrFilter = "Map file(*.map;*.cod)\0*.map;*.cod\0All files (*.*)\0*.*\0";
OpenFileName.lpstrFile = MapFileName;
OpenFileName.lpstrInitialDir = Directory;
OpenFileName.nMaxFile = MAX_PATH;
OpenFileName.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName (&OpenFileName)) {
GetCurrentDirectory( 255, Directory );
SetMapDirectory( Directory );
ResetMappings ();
if (!OpenMapFile(MapFileName)) {
DisplayError("Failed to process %s",MapFileName);
ResetMappings ();
}
}
}
void GetMapDirectory ( char * Directory ) {
char Dir[255], Group[200];
long lResult;
HKEY hKeyResults = 0;
sprintf(Group,"Software\\N64 Emulation\\%s\\Main",AppName);
lResult = RegOpenKeyEx( HKEY_CURRENT_USER,Group,0,KEY_ALL_ACCESS,
&hKeyResults);
if (lResult == ERROR_SUCCESS) {
DWORD Type, Bytes = sizeof(Dir);
lResult = RegQueryValueEx(hKeyResults,"Map Directory",0,&Type,(LPBYTE)Dir,&Bytes);
if (lResult == ERROR_SUCCESS) { strcpy(Directory,Dir); }
}
RegCloseKey(hKeyResults);
}
char * LabelName (DWORD Address) {
DWORD count;
for (count = 0; count < NoOfMapEntries; count ++ ) {
if (MapTable[count].VAddr == Address) {
sprintf(strLabelName,"%s",MapTable[count].Label);
return strLabelName;
}
}
sprintf(strLabelName,"0x%08X",Address);
return strLabelName;
}
void OpenZipMapFile(char * FileName) {
int port = 0, FoundMap;
unz_file_info info;
char zname[_MAX_PATH];
unzFile file;
file = unzOpen(FileName);
if (file == NULL) {
return;
}
port = unzGoToFirstFile(file);
FoundMap = FALSE;
while(port == UNZ_OK && FoundMap == FALSE) {
unzGetCurrentFileInfo(file, &info, zname, 128, NULL,0, NULL,0);
if (unzLocateFile(file, zname, 1) != UNZ_OK ) {
unzClose(file);
return;
}
if( unzOpenCurrentFile(file) != UNZ_OK ) {
unzClose(file);
return;
}
if (strrchr(zname,'.') != NULL) {
char *p = strrchr(zname,'.');
if (strcmp(p,".cod") == 0 || strcmp(p,".map") == 0) {
BYTE * MapFileContents;
DWORD dwRead;
HGLOBAL hMem;
MapFileContents = GlobalAlloc(GPTR,info.uncompressed_size + 1);
if (MapFileContents == NULL) {
unzCloseCurrentFile(file);
unzClose(file);
DisplayError("Not enough memory for Map File Contents");
return;
}
dwRead = unzReadCurrentFile(file,MapFileContents,info.uncompressed_size);
unzCloseCurrentFile(file);
unzClose(file);
if (info.uncompressed_size != dwRead) {
hMem = GlobalHandle (MapFileContents);
GlobalFree(hMem);
DisplayError("Failed to copy Map File to memory");
return;
}
ProcessMapFile(MapFileContents, info.uncompressed_size);
hMem = GlobalHandle (MapFileContents);
GlobalFree(hMem);
UpdateBP_FunctionList();
Update_r4300iCommandList();
return;
}
}
if (FoundMap == FALSE) {
unzCloseCurrentFile(file);
port = unzGoToNextFile(file);
}
}
unzClose(file);
}
int OpenMapFile(char * FileName) {
DWORD RomFileSize, dwRead;
BYTE * MapFileContents;
HGLOBAL hMem;
HANDLE hFile;
hFile = CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL);
if (hFile == INVALID_HANDLE_VALUE) { return FALSE; }
RomFileSize = GetFileSize(hFile,NULL);
MapFileContents = GlobalAlloc(GPTR,RomFileSize + 1);
if (MapFileContents == NULL) {
DisplayError("Not enough memory for Map File Contents");
CloseHandle( hFile );
return FALSE;
}
SetFilePointer(hFile,0,0,FILE_BEGIN);
if (!ReadFile(hFile,MapFileContents,RomFileSize,&dwRead,NULL)) {
DisplayError("Failed to copy Map File to memory");
CloseHandle( hFile );
return FALSE;
}
CloseHandle( hFile );
if (RomFileSize != dwRead) {
DisplayError("Failed to copy Map File to memory");
return FALSE;
}
if (!ProcessMapFile(MapFileContents, RomFileSize)) {
hMem = GlobalHandle (MapFileContents);
GlobalFree(hMem);
return FALSE;
}
hMem = GlobalHandle (MapFileContents);
GlobalFree(hMem);
UpdateBP_FunctionList();
Update_r4300iCommandList();
return TRUE;
}
int ProcessCODFile(BYTE * File, DWORD FileLen) {
BYTE * CurrentPos = File;
char Label[40];
DWORD Address;
int Length;
while ( CurrentPos < File + FileLen ) {
if (*CurrentPos != '0') { return FALSE; }
CurrentPos += 1;
if (*CurrentPos != 'x') { return FALSE; }
CurrentPos += 1;
if (strchr(CurrentPos,',') - CurrentPos != 8) { return FALSE; }
Address = AsciiToHex (CurrentPos);
CurrentPos += 9;
if (strchr(CurrentPos,'\r') == NULL) {
Length = strchr(CurrentPos,'\n') - CurrentPos;
} else {
Length = strchr(CurrentPos,'\r') - CurrentPos;
if (Length > (strchr(CurrentPos,'\n') - CurrentPos)) {
Length = strchr(CurrentPos,'\n') - CurrentPos;
}
}
if (Length > 40) { Length = 40; }
memcpy(Label,CurrentPos,Length);
Label[Length] = '\0';
AddMapEntry (Address, Label);
CurrentPos = strchr(CurrentPos,'\n') + 1;
}
return TRUE;
}
int ProcessMapFile(BYTE * File, DWORD FileLen) {
if (ProcessCODFile(File,FileLen)) { return TRUE; }
return FALSE;
}
void ResetMappings (void) {
NoOfMapEntries = 0;
if (MapTable != NULL) { free(MapTable); }
MapTable = NULL;
UpdateBP_FunctionList ();
Update_r4300iCommandList();
}
void SetMapDirectory ( char * Directory ) {
long lResult;
HKEY hKeyResults = 0;
DWORD Disposition = 0;
char Group[200];
sprintf(Group,"Software\\N64 Emulation\\%s\\Main",AppName);
lResult = RegCreateKeyEx( HKEY_CURRENT_USER, Group,0,"",REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,NULL,&hKeyResults,&Disposition);
if (lResult == ERROR_SUCCESS) {
RegSetValueEx(hKeyResults,"Map Directory",0,REG_SZ,(LPBYTE)Directory,strlen(Directory));
}
}
#else
char strLabelName[100];
char * LabelName (DWORD Address) {
sprintf(strLabelName,"0x%08X",Address);
return strLabelName;
}
#endif