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