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
|
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct Section {
unsigned short end;
bool invalid;
char * name;
bool banked;
};
typedef struct Section section_t;
// These functions are like strspn and strcspn, but from the right of the string rather than the left.
size_t strrspn(const char *s1, const char *s2) {
const char * _s1 = s1;
const char * _s2;
while (*_s1++);
_s1--;
while (_s1 > s1) {
for (_s2 = s2; *_s2; _s2++) {
if (_s1[-1] == *_s2) {
break;
}
}
if (*_s2 == 0)
break;
_s1--;
}
return _s1 - s1;
}
size_t strrcspn(const char *s1, const char *s2) {
const char * _s1 = s1;
const char * _s2;
while (*_s1++);
_s1--;
while (_s1 > s1) {
for (_s2 = s2; *_s2; _s2++) {
if (_s1[-1] == *_s2)
break;
}
if (*_s2)
break;
_s1--;
}
return _s1 - s1;
}
#define RIP(errmsg) { \
fprintf(stderr, "%s:%d:%s: %s\n", fname, lineno, eline, errmsg); \
if (file) fclose(file); \
return 1; \
}
int main(int argc, char * argv[]) {
int ch;
char line[16*1024];
char eline[16*1024];
char *lineptr = NULL;
char *fname;
int lineno;
section_t section_list[] = {
{0x4000, false, "ROM0", false},
{0x8000, false, "ROMX", true},
{0xA000, false, "VRAM", true},
{0xC000, false, "SRAM", true},
{0xD000, false, "WRAM0", false},
{0xE000, false, "WRAMX", true},
{0xFE00, true, "Echo RAM", false},
{0xFEA0, false, "OAM", false},
{0xFF80, true, "FEXX / IO", false},
{0xFFFF, false, "HRAM", false},
{}
};
while ((ch = getopt(argc, argv, "wdt")) != -1) {
switch (ch) {
case 'w':
case 'd':
section_list[4].end = 0xE000;
break;
case 't':
section_list[0].end = 0x8000;
break;
}
}
for (int arg_idx = optind; arg_idx < argc; arg_idx++) {
int last = -1;
int curr;
eline[0] = 0;
lineno = 0;
fname = argv[arg_idx];
FILE * file = fopen(fname, "r");
if (file == NULL)
RIP("Unable to open file");
while ((lineptr = fgets(line, sizeof(line), file)) != NULL) {
// Assume it's already sorted
lineno++;
unsigned short bank = 0;
unsigned short pointer = 0;
char * symbol = NULL;
char * end;
char * addr_p;
strcpy(eline, line); // Unmodified copy for tracebacks
*strrchr(eline, '\n') = 0;
// line = line.split(";")[0].strip()
lineptr += strspn(lineptr, " \t\n");
end = strchr(lineptr, ';');
if (end) *end = 0;
lineptr[strrspn(lineptr, " \t\n")] = 0;
if (!*line)
continue;
// Get the bank, address, and symbol
end = lineptr + strcspn(lineptr, " \t\n");
symbol = end + strspn(end, " \t\n");
if (!*symbol)
RIP("Declaration not in \"bank:addr Name\" format");
*end = 0;
addr_p = strchr(lineptr, ':');
if (!addr_p)
RIP("Pointer not in bank:addr format");
*addr_p++ = 0;
pointer = strtoul(addr_p, &end, 16);
if (pointer == 0 && end == addr_p)
RIP("Unable to parse symbol address");
bank = strtoul(lineptr, &end, 16);
if (bank == 0 && end == lineptr)
RIP("Unable to parse bank number");
curr = (bank << 14) | (pointer & 0x3fff);
// Main loop
const char * section = NULL;
section_t * section_type;
for (section_type = section_list; section_type->end; section_type++) {
if (pointer < section_type->end) {
if (section_type->invalid) {
fprintf(stderr, "Warning: cannot shim '%s' in section type '%s'\n", symbol, section_type->name);
} else {
section = section_type->name;
if (!section_type->banked)
bank = 0;
}
break;
}
}
if (section == NULL)
// Found section, but cannot shim it
continue;
if (curr != last) {
if (last != -1)
fputc('\n', stdout);
printf("SECTION \"Shim for %s\", %s[$%04X]", symbol, section, pointer);
if (bank)
printf(", BANK[$%04X]", bank);
printf("\n%s::\n", symbol);
last = curr;
} else
printf("%s::\n", symbol);
fflush(stdout);
}
fclose(file);
}
return 0;
}
|