-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngDataLinker.cpp
234 lines (192 loc) · 5.5 KB
/
ngDataLinker.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
/**********************************************************************************************************************************************************
*
* \ | __| _ \ \ __ __| \ | _ _| \ | | / __| _ \
* . | (_ | | | _ \ | _ \ | | . | . < _| /
* _|\_| \___| ___/ _/ _\ _| _/ _\ ____| ___| _|\_| _|\_\ ___| _|_\
*
**********************************************************************************************************************************************************
*
* Last Revision : Rev 1.0 11/02/2021
*
*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*
* Rev 1.0 11/02/2021
* Started.
* OzzyOuzo.
*
*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*
* NGDataLinker is a tool which generates consistent addresses needed during link for data accessed through bank switching mecanism on P roms.
*
* Contact:
* Author:[email protected]
*
***********************************************************************************************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <vector>
#include <string>
#define MAX_LINE_LENGTH 1024
char* cutAndPut(char* filename, const char* extension)
{
uint32_t i = 0, j = 0;
int32_t len;
char buf[MAX_LINE_LENGTH], *pbuf = buf, *pname;
if (filename == NULL || extension == NULL || (!strlen(filename))) return filename;
if (strlen(filename) + strlen(extension) >= MAX_LINE_LENGTH) return filename;
len = strlen(filename) - 1;
pname = filename + len;
while (len-- && *pname != '.' && *pname != '/' && *pname != '\\')
pname--;
if (len < 0) {
len = strlen(filename);
}
else {
len++;
if (*pname == '/' || *pname == '\\')
len = strlen(filename);
}
strncpy(buf, filename, len);
pbuf += len;
while (*extension == '.') extension++;
*pbuf++ = '.';
while (*extension != 0) *pbuf++ = *extension++;
*pbuf++ = 0;
sprintf_s(filename, MAX_LINE_LENGTH, "%s", buf);
return (filename);
}
const char *cleanString(const char* string_)
{
int32_t i, j, len;
static char cleaned[MAX_LINE_LENGTH];
if (string_ == nullptr) {
return nullptr;
}
memset(cleaned, 0, MAX_LINE_LENGTH);
len = strlen(string_);
j = 0;
for (i = 0; i < len; i++) {
if (string_[i] != 0x20 && string_[i] != '\t' && string_[i] != '\n') {
cleaned[j++] = string_[i];
}
}
return cleaned;
}
char* kickExtension(char* filename)
{
char* pName;
int32_t len;
if (filename == NULL) return filename;
if (strchr(filename, '.') == NULL) return filename;
len = strlen(filename) - 1;
pName = filename;
while (pName[len] != '.') {
len--;
}
filename[len] = 0;
return filename;
}
char* kickPath(char* filename)
{
char* pName;
if (filename == NULL) return filename;
pName = strrchr(filename, '/');
if (pName == NULL) {
pName = strrchr(filename, '\\');
if (pName != NULL) {
return pName + 1;
}
}
else {
return pName + 1;
}
return filename;
}
char *hasNotDoubleOffset(char *string_)
{
char *ptr;
if ((ptr = strstr(string_,"0x")) == nullptr){
return nullptr;
}
if (strstr(ptr+2,"0x")){
return nullptr;
}
return ptr;
}
char *getDep(char *string_)
{
char *ptr;
ptr = strchr(string_,0x20);
return ptr;
}
int main(int argc, char *argv[])
{
uint32_t i,topAddress;
FILE *fp, *fp2;
bool error = false;
char outDir[MAX_LINE_LENGTH];
char filename[MAX_LINE_LENGTH];
char nameUp[MAX_LINE_LENGTH];
char filemap[MAX_LINE_LENGTH];
char string[MAX_LINE_LENGTH];
char *ptr,*pString;
std::vector<std::string> _modules;
std::string module;
if (argc <= 1) {
fprintf(stderr, "\nUsage: ngDataLinker <GCC map file> <out directory>\n");
return 1;
}
strcpy(filename, argv[1]);
strcpy(filemap, filename);
strcpy(nameUp, kickPath(kickExtension(filename)));
strcat(nameUp,"_h");
cutAndPut(filename,".h");
strcpy(outDir, argv[2]);
_strupr(nameUp);
if (strstr(nameUp, "CD")) { //neoCD detection like 'foocd.h'
topAddress = 0x100000;
}
else {
topAddress = 0x200000;
}
strcat(outDir,kickPath(filename));
fp2 = fopen(outDir,"wt");
if (fp2 == NULL){
printf("\ncan't create output file : %s\n", filename);
return 1;
}
fprintf(fp2, "#ifndef _%s_\n", nameUp);
fprintf(fp2, "#define _%s_\n", nameUp);
fprintf(fp2, "\n//\n//Generated by ngDataLinker 1.1 coded by Ozzy \n//\n\n\n");
fp = fopen(filemap, "rt");
if (fp == NULL) {
printf("\nfile not found %s\n", argv[1]);
return 1;
}
while ((!feof(fp)) && (!error)) {
fgets(string, MAX_LINE_LENGTH, fp);
if ((ptr=strstr(string,"LOAD ")) != nullptr){
module = cleanString(ptr+5);
_modules.push_back(module);
}
else{
for (i=0; i < _modules.size();i++){
if (strstr(string,_modules[i].c_str()) != nullptr){
fgets(string, MAX_LINE_LENGTH, fp);
while ((ptr = hasNotDoubleOffset(string)) != nullptr) {
pString = getDep(ptr);
fprintf(fp2, "\n#define %s \t\t0x%08x", cleanString(pString + 1), (strtoul(ptr, NULL, 0) + topAddress));
fgets(string, MAX_LINE_LENGTH, fp);
}
}
}
}
}
fclose(fp);
fprintf(fp2, "\n\n#endif //_%s_\n\n", nameUp);
fclose(fp2);
return 0;
}