Line |
Branch |
Exec |
Source |
1 |
|
|
#include <algorithm> |
2 |
|
|
#include "asar.h" |
3 |
|
|
#include "crc32.h" |
4 |
|
|
|
5 |
|
|
#include "platform/file-helpers.h" |
6 |
|
|
|
7 |
|
|
mapper_t mapper=lorom; |
8 |
|
|
int sa1banks[8]={0<<20, 1<<20, -1, -1, 2<<20, 3<<20, -1, -1}; |
9 |
|
|
const unsigned char * romdata= nullptr; // NOTE: Changed into const to prevent direct write access - use writeromdata() functions below |
10 |
|
|
int romlen; |
11 |
|
|
static bool header; |
12 |
|
|
static FileHandleType thisfile = InvalidFileHandle; |
13 |
|
|
|
14 |
|
|
asar_error_id openromerror; |
15 |
|
|
|
16 |
|
|
autoarray<writtenblockdata> writtenblocks; |
17 |
|
|
// not immediately put into writtenblocks to allow the freespace finder to use |
18 |
|
|
// the reclaimed space. |
19 |
|
|
autoarray<writtenblockdata> cleared_rats_tag_blocks; |
20 |
|
|
std::vector<writtenblockdata> found_rats_tags; |
21 |
|
|
bool found_rats_tags_initialized; |
22 |
|
|
|
23 |
|
|
// RPG Hacker: Uses binary search to find the insert position of our ROM write |
24 |
|
1037786 |
static int findromwritepos(int snesoffset, int searchstartpos, int searchendpos) |
25 |
|
|
{ |
26 |
2/2
✓ Branch 0 taken 479674 times.
✓ Branch 1 taken 1116030 times.
|
1595704 |
if (searchendpos == searchstartpos) |
27 |
|
|
{ |
28 |
|
239920 |
return searchstartpos; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
1116030 |
int centerpos = searchstartpos + ((searchendpos - searchstartpos) / 2); |
32 |
|
|
|
33 |
2/2
✓ Branch 0 taken 396425 times.
✓ Branch 1 taken 719605 times.
|
1116030 |
if (writtenblocks[centerpos].snesoffset >= snesoffset) |
34 |
|
|
{ |
35 |
|
198243 |
return findromwritepos(snesoffset, searchstartpos, centerpos); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
719605 |
return findromwritepos(snesoffset, centerpos + 1, searchendpos); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
|
42 |
|
476986 |
static void addromwriteforbank(int snesoffset, int numbytes) |
43 |
|
|
{ |
44 |
|
476986 |
int currentbank = (snesoffset & 0xFF0000); |
45 |
|
|
|
46 |
1/2
✓ Branch 0 taken 238576 times.
✗ Branch 1 not taken.
|
476986 |
int insertpos = findromwritepos(snesoffset, 0, writtenblocks.count); |
47 |
|
|
|
48 |
4/4
✓ Branch 0 taken 474823 times.
✓ Branch 1 taken 823 times.
✓ Branch 2 taken 237060 times.
✓ Branch 3 taken 827 times.
|
475646 |
if (insertpos > 0 && (writtenblocks[insertpos - 1].snesoffset & 0xFF0000) == currentbank |
49 |
9/10
✓ Branch 0 taken 475646 times.
✓ Branch 1 taken 1340 times.
✓ Branch 2 taken 472993 times.
✓ Branch 3 taken 1003 times.
✓ Branch 4 taken 237060 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 236037 times.
✓ Branch 7 taken 1023 times.
✓ Branch 8 taken 236037 times.
✓ Branch 9 taken 2539 times.
|
951809 |
&& writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes >= snesoffset) |
50 |
|
|
{ |
51 |
|
|
// Merge if we overlap with a preceding block |
52 |
2/4
✓ Branch 0 taken 236037 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 236037 times.
✗ Branch 3 not taken.
|
471970 |
int firstend = writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes; |
53 |
|
471970 |
int secondend = snesoffset + numbytes; |
54 |
|
|
|
55 |
2/2
✓ Branch 0 taken 217358 times.
✓ Branch 1 taken 18679 times.
|
236037 |
int newend = (firstend > secondend ? firstend : secondend); |
56 |
|
|
|
57 |
1/2
✓ Branch 0 taken 236037 times.
✗ Branch 1 not taken.
|
471970 |
numbytes = newend - writtenblocks[insertpos - 1].snesoffset; |
58 |
1/2
✓ Branch 0 taken 236037 times.
✗ Branch 1 not taken.
|
471970 |
snesoffset = writtenblocks[insertpos - 1].snesoffset; |
59 |
|
|
|
60 |
|
471970 |
writtenblocks.remove(insertpos - 1); |
61 |
|
236037 |
insertpos -= 1; |
62 |
|
|
} |
63 |
|
|
|
64 |
4/4
✓ Branch 0 taken 293348 times.
✓ Branch 1 taken 99828 times.
✓ Branch 2 taken 96785 times.
✓ Branch 3 taken 99829 times.
|
631752 |
while (insertpos < writtenblocks.count && (writtenblocks[insertpos].snesoffset & 0xFF0000) == currentbank |
65 |
8/8
✓ Branch 0 taken 393176 times.
✓ Branch 1 taken 86940 times.
✓ Branch 2 taken 191970 times.
✓ Branch 3 taken 1549 times.
✓ Branch 4 taken 1581 times.
✓ Branch 5 taken 95204 times.
✓ Branch 6 taken 1581 times.
✓ Branch 7 taken 238576 times.
|
773464 |
&& snesoffset + numbytes >= writtenblocks[insertpos].snesoffset) |
66 |
|
|
{ |
67 |
|
|
// Merge if we overlap with a succeeding block |
68 |
|
1581 |
int firstend = snesoffset + numbytes; |
69 |
2/4
✓ Branch 0 taken 1581 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1581 times.
✗ Branch 3 not taken.
|
3130 |
int secondend = writtenblocks[insertpos].snesoffset + writtenblocks[insertpos].numbytes; |
70 |
|
|
|
71 |
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 1391 times.
|
1581 |
int newend = (firstend > secondend ? firstend : secondend); |
72 |
|
|
|
73 |
|
3130 |
numbytes = newend - snesoffset; |
74 |
|
|
|
75 |
|
3130 |
writtenblocks.remove(insertpos); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
// Insert ROM write |
79 |
|
|
writtenblockdata blockdata; |
80 |
|
476986 |
blockdata.snesoffset = snesoffset; |
81 |
|
476986 |
blockdata.pcoffset = snestopc(snesoffset); |
82 |
|
476986 |
blockdata.numbytes = numbytes; |
83 |
|
|
|
84 |
1/2
✓ Branch 0 taken 238576 times.
✗ Branch 1 not taken.
|
476986 |
writtenblocks.insert(insertpos, blockdata); |
85 |
|
476986 |
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
476498 |
void addromwrite(int pcoffset, int numbytes) |
89 |
|
|
{ |
90 |
|
476498 |
int snesaddr = pctosnes(pcoffset); |
91 |
|
238331 |
int bytesleft = numbytes; |
92 |
|
|
|
93 |
|
|
// RPG Hacker: Some kind of witchcraft which I actually hope works as intended |
94 |
|
|
// Basically, the purpose of this is to sort all ROM writes into banks for the sake of cleanness |
95 |
|
|
|
96 |
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 476498 times.
|
476986 |
while (((snesaddr >> 16) & 0xFF) != (((snesaddr + bytesleft) >> 16) & 0xFF)) |
97 |
|
|
{ |
98 |
|
488 |
int bytesinbank = ((snesaddr + 0x10000) & 0xFF0000) - snesaddr; |
99 |
|
|
|
100 |
|
488 |
addromwriteforbank(snesaddr, bytesinbank); |
101 |
|
|
|
102 |
|
488 |
pcoffset += bytesinbank; |
103 |
|
488 |
snesaddr = pctosnes(pcoffset); |
104 |
|
488 |
bytesleft -= bytesinbank; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
476498 |
addromwriteforbank(snesaddr, bytesleft); |
108 |
|
476498 |
} |
109 |
|
|
|
110 |
|
470 |
void writeromdata(int pcoffset, const void * indata, int numbytes) |
111 |
|
|
{ |
112 |
|
470 |
memcpy(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes); |
113 |
|
470 |
addromwrite(pcoffset, numbytes); |
114 |
|
470 |
} |
115 |
|
|
|
116 |
|
448051 |
void writeromdata_byte(int pcoffset, unsigned char indata) |
117 |
|
|
{ |
118 |
|
448051 |
memcpy(const_cast<unsigned char*>(romdata) + pcoffset, &indata, 1); |
119 |
|
448051 |
addromwrite(pcoffset, 1); |
120 |
|
448051 |
} |
121 |
|
|
|
122 |
|
✗ |
void writeromdata_bytes(int pcoffset, unsigned char indata, int numbytes) |
123 |
|
|
{ |
124 |
|
✗ |
memset(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes); |
125 |
|
✗ |
addromwrite(pcoffset, numbytes); |
126 |
|
✗ |
} |
127 |
|
|
|
128 |
|
204 |
int ratsstart(int snesaddr) |
129 |
|
|
{ |
130 |
|
204 |
int pcaddr=snestopc(snesaddr); |
131 |
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 102 times.
|
204 |
if (pcaddr<0x7FFF8) return -1; |
132 |
|
180 |
const unsigned char * start=romdata+pcaddr-0x10000; |
133 |
1/2
✓ Branch 0 taken 2952 times.
✗ Branch 1 not taken.
|
2952 |
for (int i=0x10000;i>=0;i--) |
134 |
|
|
{ |
135 |
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 2772 times.
|
2952 |
if (!strncmp((const char*)start+i, "STAR", 4) && |
136 |
2/4
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
✗ Branch 3 not taken.
|
180 |
(start[i+4]^start[i+6])==0xFF && (start[i+5]^start[i+7])==0xFF) |
137 |
|
|
{ |
138 |
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 |
if ((start[i+4]|(start[i+5]<<8))>0x10000-i-8-1) return pctosnes((int)(start-romdata+i)); |
139 |
|
✗ |
return -1; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
✗ |
return -1; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
18 |
static void handleprot(int loc, char * name, int len, const unsigned char * contents) |
146 |
|
|
{ |
147 |
|
|
(void)loc; // RPG Hacker: Silence "unused argument" warning. |
148 |
|
|
|
149 |
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 |
if (!strncmp(name, "PROT", 4)) |
150 |
|
|
{ |
151 |
|
18 |
memcpy(name, "NULL", 4);//to block recursion, in case someone is an idiot |
152 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 |
if (len%3) return; |
153 |
|
18 |
len/=3; |
154 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 18 times.
|
42 |
for (int i=0;i<len;i++) removerats((contents[(i*3)+0] )|(contents[(i*3)+1]<<8 )|(contents[(i*3)+2]<<16), 0x00); |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
48 |
void removerats(int snesaddr, unsigned char clean_byte) |
159 |
|
|
{ |
160 |
|
48 |
int addr=ratsstart(snesaddr); |
161 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 |
if (addr<0) return; |
162 |
|
|
// randomdude999: don't forget bank borders |
163 |
|
42 |
WalkMetadata(pctosnes(snestopc(addr)+8), handleprot); |
164 |
|
42 |
addr=snestopc(addr); |
165 |
|
|
// don't use writeromdata() because this must not go to the writtenblocks list. |
166 |
|
42 |
int len = (romdata[addr+4]|(romdata[addr+5]<<8))+9; |
167 |
|
42 |
memset(const_cast<unsigned char*>(romdata) + addr, clean_byte, len); |
168 |
|
42 |
cleared_rats_tag_blocks[cleared_rats_tag_blocks.count] = writtenblockdata{addr, 0, len}; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
692 |
void handle_cleared_rats_tags() |
172 |
|
|
{ |
173 |
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 692 times.
|
734 |
for(int i = 0; i < cleared_rats_tag_blocks.count; i++) { |
174 |
|
21 |
auto & block = cleared_rats_tag_blocks[i]; |
175 |
|
42 |
addromwrite(block.pcoffset, block.numbytes); |
176 |
|
|
} |
177 |
|
692 |
cleared_rats_tag_blocks.reset(); |
178 |
|
692 |
} |
179 |
|
|
|
180 |
|
120 |
void find_rats_tags() |
181 |
|
|
{ |
182 |
|
|
// TODO: should probably look for overlapping rats tags too, just in case. |
183 |
|
|
// note that found_rats_tags must not have overlaps, but we can merge overlapped rats tags into one |
184 |
|
120 |
found_rats_tags.clear(); |
185 |
2/2
✓ Branch 0 taken 88080264 times.
✓ Branch 1 taken 120 times.
|
88080384 |
for(int pos = 0; pos < romlen;) { |
186 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 88080252 times.
|
88080264 |
if (!strncmp((const char*)romdata+pos, "STAR", 4) && |
187 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
(romdata[pos+4]^romdata[pos+6])==0xFF && |
188 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
(romdata[pos+5]^romdata[pos+7])==0xFF) { |
189 |
|
12 |
int block_len = (romdata[pos+4]|(romdata[pos+5]<<8))+1+8; |
190 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
12 |
found_rats_tags.push_back(writtenblockdata{pos, 0, block_len}); |
191 |
|
12 |
pos += block_len; |
192 |
|
12 |
} else { |
193 |
|
88080252 |
pos++; |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
120 |
found_rats_tags_initialized = true; |
197 |
|
120 |
} |
198 |
|
|
|
199 |
|
1518 |
static inline int trypcfreespace(int start, int alt_start, int end, int size, int banksize, int minalign, unsigned char freespacebyte, bool write_rats) |
200 |
|
|
{ |
201 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1506 times.
|
1524 |
if(alt_start >= 0) start = std::max(start, alt_start); |
202 |
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 1398 times.
|
1518 |
if(!found_rats_tags_initialized) find_rats_tags(); |
203 |
2/2
✓ Branch 0 taken 428244 times.
✓ Branch 1 taken 90 times.
|
428334 |
while (start+size<=end) |
204 |
|
|
{ |
205 |
2/2
✓ Branch 0 taken 427740 times.
✓ Branch 1 taken 504 times.
|
428244 |
if (write_rats && |
206 |
2/2
✓ Branch 0 taken 7734 times.
✓ Branch 1 taken 420006 times.
|
427740 |
((start+8)&~banksize)!=((start+size-1)&~banksize&0xFFFFFF)//if the contents won't fit in this bank... |
207 |
|
|
) |
208 |
|
|
{ |
209 |
|
|
// if we are not already at the end of the bank |
210 |
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 12 times.
|
7734 |
if((start&banksize&0xFFFFF8) != (banksize&0xFFFFF8)) { |
211 |
|
7722 |
start&=~banksize&0xFFFFFF;//round it down to the start of the bank, |
212 |
|
7722 |
start|=banksize&0xFFFFF8;//then round it up to the end minus the RATS tag... |
213 |
|
217269 |
continue; |
214 |
|
|
//} else if((start&banksize) == (banksize&0xFFFFF8)) { |
215 |
|
|
// we are exactly 8 bytes off the end, allow this one to continue |
216 |
|
|
} else { |
217 |
|
|
// less than 8 bytes left in this bank |
218 |
|
|
// go to the start of the next bank |
219 |
|
12 |
start = (start+7) & ~7; |
220 |
|
12 |
continue; |
221 |
|
|
} |
222 |
|
|
} |
223 |
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 210003 times.
|
210507 |
else if(!write_rats && |
224 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
|
504 |
(start&~banksize) != ((start+size-1)&~banksize)) |
225 |
|
|
{ |
226 |
|
|
// go to next bank. |
227 |
|
✗ |
start = (start|banksize)+1; |
228 |
|
✗ |
continue; |
229 |
|
|
} |
230 |
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 420492 times.
|
420510 |
if (minalign) |
231 |
|
|
{ |
232 |
|
18 |
start&=~minalign&0xFFFFFF; |
233 |
|
18 |
start|=minalign&0xFFFFF8; |
234 |
|
|
} |
235 |
|
210255 |
bool bad=false; |
236 |
2/2
✓ Branch 0 taken 26444634 times.
✓ Branch 1 taken 2694 times.
|
26447328 |
for (int i=0;i<size;i++) |
237 |
|
|
{ |
238 |
2/2
✓ Branch 0 taken 417816 times.
✓ Branch 1 taken 26026818 times.
|
26444634 |
if (romdata[start+i]!=freespacebyte) |
239 |
|
|
{ |
240 |
|
|
// TheBiob: fix freedata align freezing. |
241 |
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 417810 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
417816 |
if ((start & minalign) == 0x7FF8 && i < 8) i = 8; |
242 |
|
417816 |
start+=i; |
243 |
2/2
✓ Branch 0 taken 310428 times.
✓ Branch 1 taken 107388 times.
|
417816 |
if (!i) start++;//this could check for a rats tag instead, but somehow I think this will give better performance. |
244 |
|
208908 |
bad=true; |
245 |
|
208908 |
break; |
246 |
|
|
} |
247 |
|
|
} |
248 |
2/2
✓ Branch 0 taken 208908 times.
✓ Branch 1 taken 1347 times.
|
419163 |
if (bad) continue; |
249 |
|
|
// check against collisions with rats tags. |
250 |
|
|
// lower_bound returns first element where comp() is false, |
251 |
|
|
// i.e. the first RATS tag that starts after our spot |
252 |
1/2
✓ Branch 0 taken 1347 times.
✗ Branch 1 not taken.
|
2694 |
auto rats = std::lower_bound(found_rats_tags.begin(), found_rats_tags.end(), start, [](writtenblockdata& blk, int start){ |
253 |
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
|
42 |
return blk.pcoffset < start; |
254 |
|
|
}); |
255 |
|
|
// if there's a rats tag after us, |
256 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2682 times.
|
2694 |
if(rats != found_rats_tags.end()) { |
257 |
|
|
// check that it doesn't intersect our spot |
258 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 |
if(rats->pcoffset < start+size) { |
259 |
|
|
// our spot ends inside this rats tag, skip to the end of it |
260 |
|
✗ |
start = rats->pcoffset + rats->numbytes; |
261 |
|
✗ |
continue; |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
// if there's a rats tag before us, |
265 |
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2664 times.
|
2694 |
if(rats != found_rats_tags.begin()) { |
266 |
|
15 |
rats--; |
267 |
|
|
// and it doesn't end before our spot starts, |
268 |
5/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 12 times.
|
30 |
if(start >= rats->pcoffset && start < rats->pcoffset+rats->numbytes) { |
269 |
|
|
// we're inside this rats tag, skip to its end |
270 |
|
6 |
start = rats->pcoffset + rats->numbytes; |
271 |
|
6 |
continue; |
272 |
|
|
} |
273 |
|
|
} |
274 |
|
|
// check against collisions with any written blocks. |
275 |
|
|
// written blocks go through like 3x snes->pc->snes and are then sorted by snes. |
276 |
|
|
// todo: this is janky..... should sort writtenblocks by pc maybe?? |
277 |
|
2688 |
int snesstart = pctosnes(start); |
278 |
|
|
// this gives the index of the first block whose snespos is >= start. |
279 |
1/2
✓ Branch 0 taken 1344 times.
✗ Branch 1 not taken.
|
2688 |
int writtenblock_pos = findromwritepos(snesstart, 0, writtenblocks.count); |
280 |
|
|
// we might need to check the preceding block too. |
281 |
|
|
// (but we don't need to check any older ones, as blocks are merged together) |
282 |
2/2
✓ Branch 0 taken 2652 times.
✓ Branch 1 taken 36 times.
|
2688 |
if(writtenblock_pos > 0) writtenblock_pos--; |
283 |
2/2
✓ Branch 0 taken 3066 times.
✓ Branch 1 taken 1410 times.
|
4476 |
for(; writtenblock_pos < writtenblocks.count; writtenblock_pos++) |
284 |
|
|
{ |
285 |
1/2
✓ Branch 0 taken 1533 times.
✗ Branch 1 not taken.
|
1533 |
auto & block = writtenblocks[writtenblock_pos]; |
286 |
2/2
✓ Branch 0 taken 1278 times.
✓ Branch 1 taken 1788 times.
|
3066 |
if(snesstart < block.snesoffset+block.numbytes |
287 |
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 18 times.
|
1278 |
&& snesstart+size > block.snesoffset) |
288 |
|
|
{ |
289 |
|
1260 |
start = block.pcoffset + block.numbytes; |
290 |
|
630 |
bad = true; |
291 |
|
630 |
break; |
292 |
|
|
} |
293 |
|
|
// blocks are sorted by snesoffset, so if they start after our end we know there are no more that could intersect |
294 |
2/2
✓ Branch 0 taken 903 times.
✓ Branch 1 taken 903 times.
|
1806 |
if(block.snesoffset > snesstart+size) break; |
295 |
|
|
} |
296 |
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 714 times.
|
1974 |
if (bad) continue; |
297 |
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 72 times.
|
1428 |
if(write_rats) |
298 |
|
|
{ |
299 |
|
1356 |
size-=8; |
300 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
addromwrite(start+8, size); |
301 |
1/2
✓ Branch 0 taken 1356 times.
✗ Branch 1 not taken.
|
1356 |
if (size) size--;//rats tags eat one byte more than specified for some reason |
302 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+0, 'S'); |
303 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+1, 'T'); |
304 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+2, 'A'); |
305 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+3, 'R'); |
306 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+4, (unsigned char)(size&0xFF)); |
307 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+5, (unsigned char)((size>>8)&0xFF)); |
308 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+6, (unsigned char)((size&0xFF)^0xFF)); |
309 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1356 |
writeromdata_byte(start+7, (unsigned char)(((size>>8)&0xFF)^0xFF)); |
310 |
|
1392 |
return start+8; |
311 |
|
|
} |
312 |
|
|
else |
313 |
|
|
{ |
314 |
|
|
// not sure if needs to be done here, |
315 |
|
|
// but it needs to be done somewhere |
316 |
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
72 |
addromwrite(start, size); |
317 |
|
72 |
return start; |
318 |
|
|
} |
319 |
|
|
} |
320 |
|
45 |
return -1; |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
//This function finds a block of freespace. -1 means "no freespace found", anything else is a PC address. |
324 |
|
|
//isforcode=false tells it to favor banks 40+, true tells it to avoid them entirely. |
325 |
|
|
//It automatically adds a RATS tag. |
326 |
|
|
|
327 |
|
1434 |
int getpcfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, unsigned char freespacebyte, bool write_rats, int search_start) |
328 |
|
|
{ |
329 |
|
|
// TODO: probably should error if specifying target_bank and align, or target_bank and respectbankborders |
330 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1428 times.
|
1434 |
if (!size) { |
331 |
|
|
// return a dummy valid address. 0x8000 should work in most mappers |
332 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 |
if(target_bank >= 0) return snestopc(target_bank << 16 | 0x8000); |
333 |
|
3 |
return 0; |
334 |
|
|
} |
335 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1428 times.
|
1428 |
if (size>0x10000) return -1; |
336 |
|
1428 |
bool isforcode = target_bank == -2; |
337 |
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 72 times.
|
1428 |
if(write_rats) |
338 |
|
1356 |
size+=8; |
339 |
|
|
|
340 |
|
30 |
auto find_for_fixed_bank = [&](int banksize, bool force_high_half = false) { |
341 |
3/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 24 times.
|
30 |
if(!force_high_half && (target_bank & 0x40) == 0x40) |
342 |
|
6 |
return trypcfreespace(snestopc(target_bank<<16), search_start, min(romlen, snestopc(target_bank<<16)+0x10000), size, banksize, 0, freespacebyte, write_rats); |
343 |
|
|
else |
344 |
|
24 |
return trypcfreespace(snestopc(target_bank<<16 | 0x8000), search_start, min(romlen, snestopc(target_bank<<16 | 0x8000)+0x8000), size, banksize, 0, freespacebyte, write_rats); |
345 |
|
1428 |
}; |
346 |
|
|
|
347 |
2/2
✓ Branch 0 taken 1386 times.
✓ Branch 1 taken 42 times.
|
1428 |
if (mapper==lorom) |
348 |
|
|
{ |
349 |
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1368 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
1386 |
if(target_bank >= 0) return find_for_fixed_bank(0x7fff); |
350 |
1/4
✓ Branch 0 taken 1368 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1368 |
if (size>0x8008 && respectbankborders) return -1; |
351 |
|
1368 |
rebootlorom: |
352 |
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1374 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
|
1458 |
if (romlen>0x200000 && !isforcode) |
353 |
|
|
{ |
354 |
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 42 times.
✓ Branch 6 taken 42 times.
✗ Branch 7 not taken.
|
210 |
int pos=trypcfreespace(search_start >= 0 ? 0 : 0x200000-8, search_start, (romlen<0x400000)?romlen:0x400000, size, |
355 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
84 |
respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats); |
356 |
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 |
if (pos>=0) return pos; |
357 |
|
|
} |
358 |
7/8
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1356 times.
✓ Branch 2 taken 687 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 687 times.
✓ Branch 5 taken 687 times.
✓ Branch 6 taken 687 times.
✗ Branch 7 not taken.
|
3423 |
int pos=trypcfreespace(search_start >= 0 ? 0 : 0x80000, search_start, (romlen<0x200000)?romlen:0x200000, size, |
359 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1356 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1356 |
respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats); |
360 |
2/2
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 90 times.
|
1374 |
if (pos>=0) return pos; |
361 |
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 |
if (autoexpand) |
362 |
|
|
{ |
363 |
|
|
if(0); |
364 |
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 12 times.
|
90 |
else if (romlen==0x080000) |
365 |
|
|
{ |
366 |
|
78 |
romlen=0x100000; |
367 |
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
78 |
writeromdata_byte(snestopc(0x00FFD7), 0x0A); |
368 |
|
|
} |
369 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 |
else if (romlen==0x100000) |
370 |
|
|
{ |
371 |
|
6 |
romlen=0x200000; |
372 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 |
writeromdata_byte(snestopc(0x00FFD7), 0x0B); |
373 |
|
|
} |
374 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 |
else if (isforcode) return -1;//no point creating freespace that can't be used |
375 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 |
else if (romlen==0x200000 || romlen==0x300000) |
376 |
|
|
{ |
377 |
|
6 |
romlen=0x400000; |
378 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 |
writeromdata_byte(snestopc(0x00FFD7), 0x0C); |
379 |
|
|
} |
380 |
|
✗ |
else return -1; |
381 |
|
45 |
autoexpand=false; |
382 |
|
90 |
goto rebootlorom; |
383 |
|
|
} |
384 |
|
|
} |
385 |
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 12 times.
|
42 |
if (mapper==hirom) |
386 |
|
|
{ |
387 |
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
30 |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
388 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
|
18 |
if (isforcode) |
389 |
|
|
{ |
390 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
for(int i = 0x8000; i < min(romlen, 0x400000); i += 0x10000){ |
391 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
9 |
int space = trypcfreespace(i, search_start, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats); |
392 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
if(space != -1) return space; |
393 |
|
|
} |
394 |
|
✗ |
return -1; |
395 |
|
|
} |
396 |
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
18 |
return trypcfreespace(0, search_start, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
397 |
|
|
} |
398 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 |
if (mapper==exlorom) |
399 |
|
|
{ |
400 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0x7fff); |
401 |
|
|
// RPG Hacker: Not really 100% sure what to do here, but I suppose this simplified code will do |
402 |
|
|
// and we won't need all the complicated stuff from LoROM above? |
403 |
|
✗ |
if (isforcode) |
404 |
|
|
{ |
405 |
|
✗ |
trypcfreespace(0, search_start, min(romlen, 0x200000), size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
406 |
|
|
} |
407 |
|
✗ |
return trypcfreespace(0, search_start, romlen, size, 0x7FFF, align ? 0x7FFF : 0, freespacebyte, write_rats); |
408 |
|
|
} |
409 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 |
if (mapper==exhirom) |
410 |
|
|
{ |
411 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
412 |
|
✗ |
if (isforcode) |
413 |
|
|
{ |
414 |
|
✗ |
for(int i = 0x8000; i < romlen && i < 0x400000; i += 0x10000){ |
415 |
|
✗ |
int space = trypcfreespace(i, search_start, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats); |
416 |
|
✗ |
if(space != -1) return space; |
417 |
|
|
} |
418 |
|
✗ |
return -1; |
419 |
|
|
} |
420 |
|
✗ |
return trypcfreespace(0, search_start, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
421 |
|
|
} |
422 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 |
if (mapper==sfxrom) |
423 |
|
|
{ |
424 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
425 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 |
if (!isforcode) return -1; |
426 |
|
|
// try not to overwrite smw stuff |
427 |
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
12 |
return trypcfreespace(search_start >= 0 ? 0 : 0x80000, search_start, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
428 |
|
|
} |
429 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
if (mapper==sa1rom) |
430 |
|
|
{ |
431 |
2/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
432 |
|
6 |
rebootsa1rom: |
433 |
|
6 |
int nextbank=-1; |
434 |
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 6 times.
|
60 |
for (int i=0;i<8;i++) |
435 |
|
|
{ |
436 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 30 times.
|
54 |
if (i&2) continue; |
437 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
|
30 |
if (sa1banks[i]+0x100000>romlen) |
438 |
|
|
{ |
439 |
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
24 |
if (sa1banks[i]<=romlen && sa1banks[i]+0x100000>romlen) nextbank=sa1banks[i]; |
440 |
|
24 |
continue; |
441 |
|
|
} |
442 |
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
9 |
int pos=trypcfreespace(sa1banks[i]?sa1banks[i]:0x80000, search_start, sa1banks[i]+0x100000, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
443 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
if (pos>=0) return pos; |
444 |
|
|
} |
445 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 |
if (autoexpand && nextbank>=0) |
446 |
|
|
{ |
447 |
|
6 |
unsigned char x7FD7[]={0, 0x0A, 0x0B, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D}; |
448 |
|
6 |
romlen=nextbank+0x100000; |
449 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 |
writeromdata_byte(0x7FD7, x7FD7[romlen>>20]); |
450 |
|
3 |
autoexpand=false; |
451 |
|
6 |
goto rebootsa1rom; |
452 |
|
|
} |
453 |
|
|
} |
454 |
|
✗ |
if (mapper==bigsa1rom) |
455 |
|
|
{ |
456 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
457 |
|
✗ |
if(!isforcode && romlen > 0x400000) |
458 |
|
|
{ |
459 |
|
✗ |
int pos=trypcfreespace(0x400000, search_start, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
460 |
|
✗ |
if(pos>=0) return pos; |
461 |
|
|
} |
462 |
|
✗ |
int pos=trypcfreespace(search_start >= 0 ? 0 : 0x080000, search_start, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
463 |
|
✗ |
if(pos>=0) return pos; |
464 |
|
|
} |
465 |
|
✗ |
return -1; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
42 |
void WalkMetadata(int loc, void(*func)(int loc, char * name, int len, const unsigned char * contents)) |
469 |
|
|
{ |
470 |
|
42 |
int pcoff=snestopc(loc); |
471 |
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 |
if (strncmp((const char*)romdata+pcoff-8, "STAR", 4)) return; |
472 |
|
42 |
const unsigned char * metadata=romdata+pcoff; |
473 |
7/10
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✓ Branch 9 taken 15 times.
|
60 |
while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3])) |
474 |
|
|
{ |
475 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
|
30 |
if (!strncmp((const char*)metadata, "STOP", 4)) |
476 |
|
|
{ |
477 |
|
6 |
metadata=romdata+pcoff; |
478 |
5/10
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
|
30 |
while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3])) |
479 |
|
|
{ |
480 |
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 |
if (!strncmp((const char*)metadata, "STOP", 4)) |
481 |
|
|
{ |
482 |
|
6 |
break; |
483 |
|
|
} |
484 |
|
18 |
func(pctosnes((int)(metadata-romdata)), (char*)const_cast<unsigned char*>(metadata), metadata[4], metadata+5); |
485 |
|
18 |
metadata+=5+metadata[4]; |
486 |
|
|
} |
487 |
|
6 |
break; |
488 |
|
|
} |
489 |
|
18 |
metadata+=5+metadata[4]; |
490 |
|
|
} |
491 |
|
|
} |
492 |
|
|
|
493 |
|
1434 |
int getsnesfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, unsigned char freespacebyte, bool write_rats, int search_start) |
494 |
|
|
{ |
495 |
|
1434 |
return pctosnes(getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, freespacebyte, write_rats, snestopc(search_start))); |
496 |
|
|
} |
497 |
|
|
|
498 |
|
236 |
bool openrom(const char * filename, bool confirm) |
499 |
|
|
{ |
500 |
|
236 |
closerom(); |
501 |
|
236 |
thisfile = open_file(filename, FileOpenMode_ReadWrite); |
502 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
|
236 |
if (thisfile == InvalidFileHandle) |
503 |
|
|
{ |
504 |
|
✗ |
openromerror = error_id_open_rom_failed; |
505 |
|
✗ |
return false; |
506 |
|
|
} |
507 |
|
236 |
header=false; |
508 |
1/2
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
|
236 |
if (strlen(filename)>4) |
509 |
|
|
{ |
510 |
|
236 |
const char * fnameend=strchr(filename, '\0')-4; |
511 |
|
236 |
header=(!stricmp(fnameend, ".smc")); |
512 |
|
|
} |
513 |
|
236 |
romlen=(int)get_file_size(thisfile)-(header*512); |
514 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
|
236 |
if (romlen<0) romlen=0; |
515 |
|
236 |
set_file_pos(thisfile, header*512); |
516 |
|
236 |
romdata=(unsigned char*)malloc(sizeof(unsigned char)*16*1024*1024); |
517 |
|
236 |
int truelen=(int)read_file(thisfile, (void*)romdata, (uint32_t)romlen); |
518 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
|
236 |
if (truelen!=romlen) |
519 |
|
|
{ |
520 |
|
✗ |
openromerror = error_id_open_rom_failed; |
521 |
|
✗ |
free(const_cast<unsigned char*>(romdata)); |
522 |
|
✗ |
return false; |
523 |
|
|
} |
524 |
|
236 |
memset(const_cast<unsigned char*>(romdata)+romlen, 0x00, (size_t)(16*1024*1024-romlen)); |
525 |
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 118 times.
|
236 |
if (confirm && snestopc(0x00FFC0)+21<(int)romlen && strncmp((const char*)romdata+snestopc(0x00FFC0), "SUPER MARIOWORLD ", 21)) |
526 |
|
|
{ |
527 |
|
✗ |
closerom(false); |
528 |
|
✗ |
openromerror = header ? error_id_open_rom_not_smw_extension : error_id_open_rom_not_smw_header; |
529 |
|
✗ |
return false; |
530 |
|
|
} |
531 |
|
|
|
532 |
|
236 |
romdata_r=(unsigned char*)malloc((size_t)romlen); |
533 |
|
236 |
romlen_r=romlen; |
534 |
|
236 |
memcpy((void*)romdata_r, romdata, (size_t)romlen);//recently allocated, dead |
535 |
|
|
|
536 |
|
236 |
return true; |
537 |
|
|
} |
538 |
|
|
|
539 |
|
472 |
uint32_t closerom(bool save) |
540 |
|
|
{ |
541 |
|
236 |
uint32_t romCrc = 0; |
542 |
6/6
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 166 times.
✓ Branch 3 taken 70 times.
✓ Branch 4 taken 160 times.
✓ Branch 5 taken 6 times.
|
472 |
if (thisfile != InvalidFileHandle && save && romlen) |
543 |
|
|
{ |
544 |
|
160 |
set_file_pos(thisfile, header*512); |
545 |
|
160 |
write_file(thisfile, romdata, (uint32_t)romlen); |
546 |
|
|
|
547 |
|
|
// do a quick re-read of the header, and include that in the crc32 calculation if necessary |
548 |
|
|
{ |
549 |
|
160 |
uint8_t* filedata = (uint8_t*)malloc(sizeof(uint8_t) * (romlen + header * 512)); |
550 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 |
if (header) |
551 |
|
|
{ |
552 |
|
✗ |
set_file_pos(thisfile, 0u); |
553 |
|
✗ |
read_file(thisfile, filedata, 512); |
554 |
|
|
} |
555 |
|
160 |
memcpy(filedata + (header * 512), romdata, sizeof(uint8_t) * (size_t)romlen); |
556 |
|
160 |
romCrc = crc32(filedata, (unsigned int)(romlen + header * 512)); |
557 |
|
160 |
free(filedata); |
558 |
|
|
} |
559 |
|
|
} |
560 |
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 236 times.
|
472 |
if (thisfile != InvalidFileHandle) close_file(thisfile); |
561 |
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 236 times.
|
472 |
if (romdata) free(const_cast<unsigned char*>(romdata)); |
562 |
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 236 times.
|
472 |
if (romdata_r) free(const_cast<unsigned char*>(romdata_r)); |
563 |
|
472 |
thisfile= InvalidFileHandle; |
564 |
|
472 |
romdata= nullptr; |
565 |
|
472 |
romdata_r = nullptr; |
566 |
|
472 |
romlen=0; |
567 |
|
472 |
return romCrc; |
568 |
|
|
} |
569 |
|
|
|
570 |
|
470 |
static unsigned int getchecksum() |
571 |
|
|
{ |
572 |
|
244 |
unsigned int checksum=0; |
573 |
2/2
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 195 times.
|
470 |
if((romlen & (romlen-1)) == 0) |
574 |
|
|
{ |
575 |
|
|
// romlen is a power of 2, just add up all the bytes |
576 |
2/2
✓ Branch 0 taken 123732110 times.
✓ Branch 1 taken 275 times.
|
123732385 |
for (int i=0;i<romlen;i++) checksum+=romdata[i]; |
577 |
|
|
} |
578 |
|
|
else |
579 |
|
|
{ |
580 |
|
|
// assume romlen is the sum of 2 powers of 2 - i haven't seen any real rom that isn't, |
581 |
|
|
// and if you make such a rom, fixing its checksum is your problem. |
582 |
|
195 |
int firstpart = bitround(romlen) >> 1; |
583 |
|
195 |
int secondpart = romlen - firstpart; |
584 |
|
195 |
int repeatcount = firstpart / secondpart; |
585 |
|
99 |
unsigned int secondpart_sum = 0; |
586 |
2/2
✓ Branch 0 taken 33731886 times.
✓ Branch 1 taken 195 times.
|
33732081 |
for(int i = 0; i < firstpart; i++) checksum += romdata[i]; |
587 |
2/2
✓ Branch 0 taken 29362514 times.
✓ Branch 1 taken 195 times.
|
29362709 |
for(int i = firstpart; i < romlen; i++) secondpart_sum += romdata[i]; |
588 |
|
195 |
checksum += secondpart_sum * repeatcount; |
589 |
|
|
} |
590 |
|
470 |
return checksum&0xFFFF; |
591 |
|
|
} |
592 |
|
|
|
593 |
|
470 |
void fixchecksum() |
594 |
|
|
{ |
595 |
|
|
// randomdude999: clear out checksum bytes before recalculating checksum, this should make it correct on roms that don't have a checksum yet |
596 |
|
470 |
writeromdata(snestopc(0x00FFDC), "\xFF\xFF\0\0", 4); |
597 |
|
470 |
int checksum=(int)getchecksum(); |
598 |
|
470 |
writeromdata_byte(snestopc(0x00FFDE), (unsigned char)(checksum&255)); |
599 |
|
470 |
writeromdata_byte(snestopc(0x00FFDF), (unsigned char)((checksum>>8)&255)); |
600 |
|
470 |
writeromdata_byte(snestopc(0x00FFDC), (unsigned char)((checksum&255)^255)); |
601 |
|
470 |
writeromdata_byte(snestopc(0x00FFDD), (unsigned char)(((checksum>>8)&255)^255)); |
602 |
|
470 |
} |
603 |
|
|
|