| 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 |
|
483469 |
static int findromwritepos(int snesoffset, int searchstartpos, int searchendpos) |
| 26 |
|
|
{ |
| 27 |
2/2
✓ Branch 0 taken 139441 times.
✓ Branch 1 taken 344028 times.
|
483469 |
if (searchendpos == searchstartpos) |
| 28 |
|
|
{ |
| 29 |
|
139441 |
return searchstartpos; |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
344028 |
int centerpos = searchstartpos + ((searchendpos - searchstartpos) / 2); |
| 33 |
|
|
|
| 34 |
4/4
✓ Branch 0 taken 63409 times.
✓ Branch 1 taken 108524 times.
✓ Branch 2 taken 63459 times.
✓ Branch 3 taken 108636 times.
|
344028 |
if (writtenblocks[centerpos].snesoffset >= snesoffset) |
| 35 |
|
|
{ |
| 36 |
|
126868 |
return findromwritepos(snesoffset, searchstartpos, centerpos); |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
217160 |
return findromwritepos(snesoffset, centerpos + 1, searchendpos); |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
150447 |
static void addromwriteforbank(int snesoffset, int numbytes) |
| 44 |
|
|
{ |
| 45 |
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 150289 times.
|
156342 |
if(numbytes == 0) return; |
| 46 |
|
|
|
| 47 |
|
|
// common case: this rom write is immediately after the last one. |
| 48 |
2/2
✓ Branch 0 taken 150073 times.
✓ Branch 1 taken 216 times.
|
150289 |
if(last_writtenblock_ind < writtenblocks.count) { |
| 49 |
1/2
✓ Branch 0 taken 150073 times.
✗ Branch 1 not taken.
|
150073 |
auto& blk = writtenblocks[last_writtenblock_ind]; |
| 50 |
8/8
✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 68821 times.
✓ Branch 2 taken 6134 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 6168 times.
✓ Branch 5 taken 68949 times.
✓ Branch 6 taken 6166 times.
✓ Branch 7 taken 2 times.
|
150073 |
if(blk.snesoffset + blk.numbytes == snesoffset && (blk.snesoffset&0xff0000)==(snesoffset&0xff0000)) { |
| 51 |
|
|
// check if we overlap with next block |
| 52 |
|
24600 |
if(last_writtenblock_ind+1 >= writtenblocks.count |
| 53 |
9/10
✓ Branch 0 taken 3589 times.
✓ Branch 1 taken 8711 times.
✓ Branch 2 taken 3589 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1555 times.
✓ Branch 5 taken 239 times.
✓ Branch 6 taken 7451 times.
✓ Branch 7 taken 478 times.
✓ Branch 8 taken 5927 times.
✓ Branch 9 taken 239 times.
|
12300 |
|| writtenblocks[last_writtenblock_ind+1].snesoffset > snesoffset+numbytes) { |
| 54 |
|
|
// if not, merge with previous written block |
| 55 |
|
11822 |
blk.numbytes += numbytes; |
| 56 |
|
11822 |
return; |
| 57 |
|
|
} |
| 58 |
|
|
} |
| 59 |
|
|
} |
| 60 |
|
138467 |
int currentbank = (snesoffset & 0xFF0000); |
| 61 |
|
|
|
| 62 |
1/2
✓ Branch 0 taken 138467 times.
✗ Branch 1 not taken.
|
138467 |
int insertpos = findromwritepos(snesoffset, 0, writtenblocks.count); |
| 63 |
|
|
|
| 64 |
5/6
✓ Branch 0 taken 138207 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68818 times.
✓ Branch 3 taken 230 times.
✓ Branch 4 taken 68927 times.
✓ Branch 5 taken 232 times.
|
138207 |
if (insertpos > 0 && (writtenblocks[insertpos - 1].snesoffset & 0xFF0000) == currentbank |
| 65 |
12/14
✓ Branch 0 taken 138207 times.
✓ Branch 1 taken 260 times.
✓ Branch 2 taken 137745 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68818 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 137569 times.
✓ Branch 7 taken 176 times.
✓ Branch 8 taken 68642 times.
✓ Branch 9 taken 512 times.
✓ Branch 10 taken 68725 times.
✓ Branch 11 taken 202 times.
✓ Branch 12 taken 68725 times.
✓ Branch 13 taken 588 times.
|
276674 |
&& 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 137367 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68642 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68725 times.
✗ Branch 5 not taken.
|
137367 |
int firstend = writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes; |
| 69 |
|
137367 |
int secondend = snesoffset + numbytes; |
| 70 |
|
|
|
| 71 |
2/2
✓ Branch 0 taken 136473 times.
✓ Branch 1 taken 894 times.
|
137367 |
int newend = (firstend > secondend ? firstend : secondend); |
| 72 |
|
|
|
| 73 |
1/2
✓ Branch 0 taken 137367 times.
✗ Branch 1 not taken.
|
137367 |
numbytes = newend - writtenblocks[insertpos - 1].snesoffset; |
| 74 |
1/2
✓ Branch 0 taken 137367 times.
✗ Branch 1 not taken.
|
137367 |
snesoffset = writtenblocks[insertpos - 1].snesoffset; |
| 75 |
|
|
|
| 76 |
|
137367 |
writtenblocks.remove(insertpos - 1); |
| 77 |
|
137367 |
insertpos -= 1; |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
5/6
✓ Branch 0 taken 126744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30534 times.
✓ Branch 3 taken 32814 times.
✓ Branch 4 taken 30582 times.
✓ Branch 5 taken 32814 times.
|
265211 |
while (insertpos < writtenblocks.count && (writtenblocks[insertpos].snesoffset & 0xFF0000) == currentbank |
| 81 |
9/10
✓ Branch 0 taken 126744 times.
✓ Branch 1 taken 12280 times.
✓ Branch 2 taken 61116 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 265 times.
✓ Branch 5 taken 30269 times.
✓ Branch 6 taken 557 times.
✓ Branch 7 taken 99444 times.
✓ Branch 8 taken 292 times.
✓ Branch 9 taken 69313 times.
|
265768 |
&& snesoffset + numbytes >= writtenblocks[insertpos].snesoffset) |
| 82 |
|
|
{ |
| 83 |
|
|
// Merge if we overlap with a succeeding block |
| 84 |
|
557 |
int firstend = snesoffset + numbytes; |
| 85 |
3/6
✓ Branch 0 taken 557 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 265 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 292 times.
✗ Branch 5 not taken.
|
557 |
int secondend = writtenblocks[insertpos].snesoffset + writtenblocks[insertpos].numbytes; |
| 86 |
|
|
|
| 87 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
|
557 |
int newend = (firstend > secondend ? firstend : secondend); |
| 88 |
|
|
|
| 89 |
|
557 |
numbytes = newend - snesoffset; |
| 90 |
|
|
|
| 91 |
|
557 |
writtenblocks.remove(insertpos); |
| 92 |
|
|
} |
| 93 |
|
|
|
| 94 |
|
|
// Insert ROM write |
| 95 |
|
69313 |
writtenblockdata blockdata; |
| 96 |
|
138467 |
blockdata.snesoffset = snesoffset; |
| 97 |
|
138467 |
blockdata.pcoffset = snestopc(snesoffset); |
| 98 |
|
138467 |
blockdata.numbytes = numbytes; |
| 99 |
|
|
|
| 100 |
1/2
✓ Branch 0 taken 138467 times.
✗ Branch 1 not taken.
|
138467 |
writtenblocks.insert(insertpos, blockdata); |
| 101 |
|
138467 |
last_writtenblock_ind = insertpos; |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
|
|
|
| 105 |
|
150289 |
void addromwrite(int pcoffset, int numbytes) |
| 106 |
|
|
{ |
| 107 |
|
150289 |
int snesaddr = pctosnes(pcoffset); |
| 108 |
|
150289 |
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 158 times.
✓ Branch 1 taken 150289 times.
|
150447 |
while ((snesaddr >> 16) != ((snesaddr + bytesleft) >> 16)) |
| 114 |
|
|
{ |
| 115 |
|
158 |
int bytesinbank = ((snesaddr + 0x10000) & 0xffff0000) - snesaddr; |
| 116 |
|
|
|
| 117 |
|
158 |
addromwriteforbank(snesaddr, bytesinbank); |
| 118 |
|
|
|
| 119 |
|
158 |
pcoffset += bytesinbank; |
| 120 |
|
158 |
snesaddr = pctosnes(pcoffset); |
| 121 |
|
158 |
bytesleft -= bytesinbank; |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
150289 |
addromwriteforbank(snesaddr, bytesleft); |
| 125 |
|
150289 |
} |
| 126 |
|
|
|
| 127 |
|
27 |
void writeromdata(int pcoffset, const void * indata, int numbytes) |
| 128 |
|
|
{ |
| 129 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
|
27 |
memcpy(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes); |
| 130 |
|
27 |
addromwrite(pcoffset, numbytes); |
| 131 |
|
27 |
} |
| 132 |
|
|
|
| 133 |
|
149668 |
void writeromdata_byte(int pcoffset, unsigned char indata, bool add_write) |
| 134 |
|
|
{ |
| 135 |
|
149668 |
memcpy(const_cast<unsigned char*>(romdata) + pcoffset, &indata, 1); |
| 136 |
2/2
✓ Branch 0 taken 140546 times.
✓ Branch 1 taken 9122 times.
|
149668 |
if(add_write) |
| 137 |
|
140546 |
addromwrite(pcoffset, 1); |
| 138 |
|
149668 |
} |
| 139 |
|
|
|
| 140 |
|
127 |
void writeromdata_bytes(int pcoffset, unsigned char indata, int numbytes, bool add_write) |
| 141 |
|
|
{ |
| 142 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
|
127 |
memset(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes); |
| 143 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
|
127 |
if(add_write) |
| 144 |
|
✗ |
addromwrite(pcoffset, numbytes); |
| 145 |
|
127 |
} |
| 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 |
|
275 |
void handle_cleared_rats_tags() |
| 191 |
|
|
{ |
| 192 |
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 275 times.
|
303 |
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 |
|
275 |
cleared_rats_tag_blocks.reset(); |
| 197 |
|
275 |
} |
| 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 |
|
562 |
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 552 times.
|
562 |
if(alt_start >= 0) start = std::max(start, alt_start); |
| 221 |
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 535 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 254 times.
|
562 |
if(!found_rats_tags_initialized) find_rats_tags(); |
| 222 |
2/2
✓ Branch 0 taken 143122 times.
✓ Branch 1 taken 38 times.
|
143160 |
while (start+size<=end) |
| 223 |
|
|
{ |
| 224 |
2/2
✓ Branch 0 taken 142878 times.
✓ Branch 1 taken 244 times.
|
143122 |
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 |
|
72586 |
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 244 times.
✓ Branch 1 taken 140300 times.
|
140544 |
else if(!write_rats && |
| 243 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
|
244 |
(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 140538 times.
|
140544 |
if (minalign) |
| 250 |
|
|
{ |
| 251 |
|
6 |
start&=~minalign&0xFFFFFF; |
| 252 |
|
6 |
start|=minalign&0xFFFFF8; |
| 253 |
|
|
} |
| 254 |
|
140544 |
bool bad=false; |
| 255 |
2/2
✓ Branch 0 taken 8816040 times.
✓ Branch 1 taken 976 times.
|
8817016 |
for (int i=0;i<size;i++) |
| 256 |
|
|
{ |
| 257 |
4/4
✓ Branch 0 taken 69784 times.
✓ Branch 1 taken 4338236 times.
✓ Branch 2 taken 69784 times.
✓ Branch 3 taken 4338236 times.
|
8816040 |
if (romdata[start+i]!=freespacebyte) |
| 258 |
|
|
{ |
| 259 |
|
|
// TheBiob: fix freedata align freezing. |
| 260 |
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 139566 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
139568 |
if ((start & minalign) == 0x7FF8 && i < 8) i = 8; |
| 261 |
|
139568 |
start+=i; |
| 262 |
2/2
✓ Branch 0 taken 103722 times.
✓ Branch 1 taken 35846 times.
|
139568 |
if (!i) start++;//this could check for a rats tag instead, but somehow I think this will give better performance. |
| 263 |
|
139568 |
bad=true; |
| 264 |
|
139568 |
break; |
| 265 |
|
|
} |
| 266 |
|
|
} |
| 267 |
2/2
✓ Branch 0 taken 139568 times.
✓ Branch 1 taken 976 times.
|
140544 |
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 488 times.
✓ Branch 1 taken 488 times.
✗ Branch 2 not taken.
|
976 |
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 486 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 486 times.
|
976 |
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 483 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 483 times.
|
976 |
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 |
|
974 |
int snesstart = pctosnes(start); |
| 297 |
|
|
// this gives the index of the first block whose snespos is >= start. |
| 298 |
1/2
✓ Branch 0 taken 974 times.
✗ Branch 1 not taken.
|
974 |
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 956 times.
✓ Branch 1 taken 18 times.
|
974 |
if(writtenblock_pos > 0) writtenblock_pos--; |
| 302 |
2/2
✓ Branch 0 taken 980 times.
✓ Branch 1 taken 512 times.
|
1492 |
for(; writtenblock_pos < writtenblocks.count; writtenblock_pos++) |
| 303 |
|
|
{ |
| 304 |
1/2
✓ Branch 0 taken 980 times.
✗ Branch 1 not taken.
|
980 |
auto & block = writtenblocks[writtenblock_pos]; |
| 305 |
4/4
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 231 times.
✓ Branch 3 taken 259 times.
|
980 |
if(snesstart < block.snesoffset+block.numbytes |
| 306 |
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 225 times.
✓ Branch 3 taken 6 times.
|
462 |
&& snesstart+size > block.snesoffset) |
| 307 |
|
|
{ |
| 308 |
|
450 |
start = block.pcoffset + block.numbytes; |
| 309 |
|
450 |
bad = true; |
| 310 |
|
450 |
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 259 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 259 times.
|
530 |
if(block.snesoffset > snesstart+size) break; |
| 314 |
|
|
} |
| 315 |
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 524 times.
|
974 |
if (bad) continue; |
| 316 |
2/2
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 38 times.
|
524 |
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 |
|
505 |
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 38 times.
✗ Branch 1 not taken.
|
38 |
addromwrite(start, size); |
| 336 |
|
38 |
return start; |
| 337 |
|
|
} |
| 338 |
|
|
} |
| 339 |
|
38 |
return -1; |
| 340 |
|
|
} |
| 341 |
|
|
|
| 342 |
|
40 |
static void expandrom(int newromlen) { |
| 343 |
|
40 |
writeromdata_bytes(romlen, freespacebyte, newromlen - romlen, false); |
| 344 |
|
40 |
romlen = newromlen; |
| 345 |
|
40 |
int log_len = 0; |
| 346 |
4/4
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 443 times.
✓ Branch 2 taken 403 times.
✓ Branch 3 taken 20 times.
|
846 |
for(; 1<<log_len < romlen; log_len++); |
| 347 |
|
40 |
writeromdata_byte(snestopc(0xffd7), log_len - 10); |
| 348 |
|
40 |
} |
| 349 |
|
|
|
| 350 |
|
|
//This function finds a block of freespace. -1 means "no freespace found", anything else is a PC address. |
| 351 |
|
|
//isforcode=false tells it to favor banks 40+, true tells it to avoid them entirely. |
| 352 |
|
|
//It automatically adds a RATS tag. |
| 353 |
|
|
|
| 354 |
|
524 |
int getpcfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, bool write_rats, int search_start) |
| 355 |
|
|
{ |
| 356 |
|
|
// TODO: probably should error if specifying target_bank and align, or target_bank and respectbankborders |
| 357 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 524 times.
|
524 |
if (!size) { |
| 358 |
|
|
// return a dummy valid address. 0x8000 should work in most mappers |
| 359 |
|
✗ |
if(target_bank >= 0) return snestopc(target_bank << 16 | 0x8000); |
| 360 |
|
✗ |
return 0; |
| 361 |
|
|
} |
| 362 |
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 524 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.
|
524 |
if (size>0x10000 && (write_rats || respectbankborders)) return -1; |
| 363 |
|
524 |
bool isforcode = target_bank == -2; |
| 364 |
4/4
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 281 times.
✓ Branch 2 taken 243 times.
✓ Branch 3 taken 19 times.
|
524 |
if(write_rats) |
| 365 |
|
486 |
size+=8; |
| 366 |
|
|
|
| 367 |
|
270 |
auto find_for_fixed_bank = [&](int banksize, bool force_high_half = false) { |
| 368 |
5/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
|
16 |
if(!force_high_half && (target_bank & 0x40) == 0x40) |
| 369 |
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.
|
2 |
return trypcfreespace(snestopc(target_bank<<16), search_start, min(romlen, snestopc(target_bank<<16)+0x10000), size, banksize, 0, freespacebyte, write_rats); |
| 370 |
|
|
else |
| 371 |
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
|
14 |
return trypcfreespace(snestopc(target_bank<<16 | 0x8000), search_start, min(romlen, snestopc(target_bank<<16 | 0x8000)+0x8000), size, banksize, 0, freespacebyte, write_rats); |
| 372 |
|
524 |
}; |
| 373 |
|
|
|
| 374 |
4/4
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 255 times.
✓ Branch 3 taken 7 times.
|
524 |
if (mapper==lorom) |
| 375 |
|
|
{ |
| 376 |
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 498 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
510 |
if(target_bank >= 0) return find_for_fixed_bank(0x7fff); |
| 377 |
1/4
✓ Branch 0 taken 498 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
498 |
if (size>0x8008 && respectbankborders) return -1; |
| 378 |
|
517 |
rebootlorom: |
| 379 |
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 508 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
536 |
if (romlen>0x200000 && !isforcode) |
| 380 |
|
|
{ |
| 381 |
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, |
| 382 |
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); |
| 383 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
if (pos>=0) return pos; |
| 384 |
|
|
} |
| 385 |
9/11
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 505 times.
✓ Branch 2 taken 257 times.
✓ Branch 3 taken 251 times.
✓ Branch 4 taken 258 times.
✓ Branch 5 taken 250 times.
✓ Branch 6 taken 258 times.
✓ Branch 7 taken 250 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 254 times.
✗ Branch 10 not taken.
|
1010 |
int pos=trypcfreespace(search_start >= 0 ? 0 : 0x80000, search_start, (romlen<0x200000)?romlen:0x200000, size, |
| 386 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 502 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
502 |
respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats); |
| 387 |
2/2
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 38 times.
|
508 |
if (pos>=0) return pos; |
| 388 |
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 |
if (autoexpand) |
| 389 |
|
|
{ |
| 390 |
|
|
if(0); |
| 391 |
3/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
38 |
else if (romlen==0x080000) expandrom(0x10'0000); |
| 392 |
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 |
else if (romlen==0x100000) expandrom(0x20'0000); |
| 393 |
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 |
| 394 |
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) |
| 395 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
expandrom(0x40'0000); |
| 396 |
|
✗ |
else return -1; |
| 397 |
|
38 |
autoexpand=false; |
| 398 |
|
38 |
goto rebootlorom; |
| 399 |
|
|
} |
| 400 |
|
|
} |
| 401 |
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) |
| 402 |
|
|
{ |
| 403 |
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
10 |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
| 404 |
|
|
while(true) { |
| 405 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 |
if (isforcode) { |
| 406 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
for(int i = 0x8000; i < min(romlen, 0x40'0000); i += 0x1'0000){ |
| 407 |
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); |
| 408 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if(space != -1) return space; |
| 409 |
|
|
} |
| 410 |
|
|
} else { |
| 411 |
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 |
int space = trypcfreespace(0, search_start, romlen, size, respectbankborders?0xFFFF:0xFFFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
| 412 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if(space != -1) return space; |
| 413 |
|
|
} |
| 414 |
|
✗ |
if(!autoexpand || romlen >= 0x40'0000) break; |
| 415 |
|
|
// new len = next power of 2 from current romlen (but at least a bank....) |
| 416 |
|
✗ |
int newlen = 0x1'0000; |
| 417 |
|
✗ |
for(; newlen <= romlen; newlen*=2); |
| 418 |
|
✗ |
expandrom(newlen); |
| 419 |
|
✗ |
} |
| 420 |
|
✗ |
return -1; |
| 421 |
|
|
} |
| 422 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
4 |
if (mapper==exlorom) |
| 423 |
|
|
{ |
| 424 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0x7fff); |
| 425 |
|
|
// RPG Hacker: Not really 100% sure what to do here, but I suppose this simplified code will do |
| 426 |
|
|
// and we won't need all the complicated stuff from LoROM above? |
| 427 |
|
✗ |
if (isforcode) |
| 428 |
|
|
{ |
| 429 |
|
✗ |
trypcfreespace(0, search_start, min(romlen, 0x200000), size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
| 430 |
|
|
} |
| 431 |
|
✗ |
return trypcfreespace(0, search_start, romlen, size, 0x7FFF, align ? 0x7FFF : 0, freespacebyte, write_rats); |
| 432 |
|
|
} |
| 433 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
4 |
if (mapper==exhirom) |
| 434 |
|
|
{ |
| 435 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
| 436 |
|
✗ |
if (isforcode) |
| 437 |
|
|
{ |
| 438 |
|
✗ |
for(int i = 0x8000; i < romlen && i < 0x400000; i += 0x10000){ |
| 439 |
|
✗ |
int space = trypcfreespace(i, search_start, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats); |
| 440 |
|
✗ |
if(space != -1) return space; |
| 441 |
|
|
} |
| 442 |
|
✗ |
return -1; |
| 443 |
|
|
} |
| 444 |
|
✗ |
return trypcfreespace(0, search_start, romlen, size, respectbankborders?0xFFFF:0xFFFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
| 445 |
|
|
} |
| 446 |
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) |
| 447 |
|
|
{ |
| 448 |
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); |
| 449 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (!isforcode) return -1; |
| 450 |
|
|
// try not to overwrite smw stuff |
| 451 |
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); |
| 452 |
|
|
} |
| 453 |
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) |
| 454 |
|
|
{ |
| 455 |
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); |
| 456 |
|
4 |
rebootsa1rom: |
| 457 |
|
4 |
int nextbank=-1; |
| 458 |
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 |
for (int i=0;i<8;i++) |
| 459 |
|
|
{ |
| 460 |
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
|
18 |
if (i&2) continue; |
| 461 |
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) |
| 462 |
|
|
{ |
| 463 |
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]; |
| 464 |
|
8 |
continue; |
| 465 |
|
|
} |
| 466 |
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); |
| 467 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (pos>=0) return pos; |
| 468 |
|
|
} |
| 469 |
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) |
| 470 |
|
|
{ |
| 471 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
expandrom(nextbank+0x10'0000); |
| 472 |
|
2 |
autoexpand=false; |
| 473 |
|
2 |
goto rebootsa1rom; |
| 474 |
|
|
} |
| 475 |
|
|
} |
| 476 |
|
✗ |
if (mapper==bigsa1rom) |
| 477 |
|
|
{ |
| 478 |
|
✗ |
if(target_bank >= 0) return find_for_fixed_bank(0xffff); |
| 479 |
|
✗ |
if(!isforcode && romlen > 0x400000) |
| 480 |
|
|
{ |
| 481 |
|
✗ |
int pos=trypcfreespace(0x400000, search_start, romlen, size, 0xFFFF, align?0xFFFF:0, freespacebyte, write_rats); |
| 482 |
|
✗ |
if(pos>=0) return pos; |
| 483 |
|
|
} |
| 484 |
|
✗ |
int pos=trypcfreespace(search_start >= 0 ? 0 : 0x080000, search_start, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats); |
| 485 |
|
✗ |
if(pos>=0) return pos; |
| 486 |
|
|
} |
| 487 |
|
✗ |
return -1; |
| 488 |
|
|
} |
| 489 |
|
|
|
| 490 |
|
28 |
void WalkMetadata(int loc, void(*func)(int loc, char * name, int len, const unsigned char * contents)) |
| 491 |
|
|
{ |
| 492 |
|
28 |
int pcoff=snestopc(loc); |
| 493 |
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; |
| 494 |
|
28 |
const unsigned char * metadata=romdata+pcoff; |
| 495 |
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])) |
| 496 |
|
|
{ |
| 497 |
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)) |
| 498 |
|
|
{ |
| 499 |
|
4 |
metadata=romdata+pcoff; |
| 500 |
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])) |
| 501 |
|
|
{ |
| 502 |
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)) |
| 503 |
|
|
{ |
| 504 |
|
4 |
break; |
| 505 |
|
|
} |
| 506 |
|
6 |
func(pctosnes((int)(metadata-romdata)), (char*)const_cast<unsigned char*>(metadata), metadata[4], metadata+5); |
| 507 |
|
6 |
metadata+=5+metadata[4]; |
| 508 |
|
|
} |
| 509 |
|
4 |
break; |
| 510 |
|
|
} |
| 511 |
|
6 |
metadata+=5+metadata[4]; |
| 512 |
|
|
} |
| 513 |
|
|
} |
| 514 |
|
|
|
| 515 |
|
524 |
int getsnesfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, bool write_rats, int search_start) |
| 516 |
|
|
{ |
| 517 |
|
524 |
int pcfree = getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, write_rats, snestopc(search_start)); |
| 518 |
|
524 |
int snesfree = pctosnes(pcfree); |
| 519 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 524 times.
|
524 |
if(snesfree == -1) return -1; |
| 520 |
|
524 |
bool isforcode = target_bank == -2; |
| 521 |
|
|
// not sure if this is ever a concern for other mappers... |
| 522 |
8/10
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 150 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.
|
524 |
if(isforcode && (mapper == hirom || mapper == exhirom)) { |
| 523 |
|
2 |
snesfree = snesfree & ~0x400000; |
| 524 |
|
|
// maybe this should throw an internal error or something instead lol |
| 525 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if((snesfree&0x8000) != 0x8000) return -1; |
| 526 |
|
|
} |
| 527 |
|
524 |
return snesfree; |
| 528 |
|
|
} |
| 529 |
|
|
|
| 530 |
|
3 |
bool openrom(const char * filename, bool confirm) |
| 531 |
|
|
{ |
| 532 |
|
3 |
closerom(); |
| 533 |
|
3 |
thisfile = open_file(filename, FileOpenMode_ReadWrite); |
| 534 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
if (thisfile == InvalidFileHandle) |
| 535 |
|
|
{ |
| 536 |
|
✗ |
throw_err_null(pass, err_open_rom_failed); |
| 537 |
|
✗ |
return false; |
| 538 |
|
|
} |
| 539 |
|
3 |
header=false; |
| 540 |
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) |
| 541 |
|
|
{ |
| 542 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
const char * fnameend=strchr(filename, '\0')-4; |
| 543 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
header=(!stricmp(fnameend, ".smc")); |
| 544 |
|
|
} |
| 545 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
romlen=(int)get_file_size(thisfile)-(header*512); |
| 546 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
if (romlen<0) romlen=0; |
| 547 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
set_file_pos(thisfile, header*512); |
| 548 |
|
3 |
romdata=(unsigned char*)malloc(sizeof(unsigned char)*16*1024*1024); |
| 549 |
|
3 |
int truelen=(int)read_file(thisfile, (void*)romdata, (uint32_t)romlen); |
| 550 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
if (truelen!=romlen) |
| 551 |
|
|
{ |
| 552 |
|
✗ |
free(const_cast<unsigned char*>(romdata)); |
| 553 |
|
✗ |
throw_err_null(pass, err_open_rom_failed); |
| 554 |
|
✗ |
return false; |
| 555 |
|
|
} |
| 556 |
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)); |
| 557 |
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)) |
| 558 |
|
|
{ |
| 559 |
|
✗ |
closerom(false); |
| 560 |
|
✗ |
if(header) throw_err_null(pass, err_open_rom_not_smw_extension); |
| 561 |
|
✗ |
else throw_err_null(pass, err_open_rom_not_smw_header); |
| 562 |
|
✗ |
return false; |
| 563 |
|
|
} |
| 564 |
|
|
|
| 565 |
|
3 |
romdata_r=(unsigned char*)malloc((size_t)romlen); |
| 566 |
|
3 |
romlen_r=romlen; |
| 567 |
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 |
| 568 |
|
|
|
| 569 |
|
3 |
return true; |
| 570 |
|
|
} |
| 571 |
|
|
|
| 572 |
|
6 |
uint32_t closerom(bool save) |
| 573 |
|
|
{ |
| 574 |
|
6 |
uint32_t romCrc = 0; |
| 575 |
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) |
| 576 |
|
|
{ |
| 577 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
set_file_pos(thisfile, header*512); |
| 578 |
|
3 |
write_file(thisfile, romdata, (uint32_t)romlen); |
| 579 |
|
|
|
| 580 |
|
|
// do a quick re-read of the header, and include that in the crc32 calculation if necessary |
| 581 |
|
|
{ |
| 582 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
uint8_t* filedata = (uint8_t*)malloc(sizeof(uint8_t) * (romlen + header * 512)); |
| 583 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 |
if (header) |
| 584 |
|
|
{ |
| 585 |
|
✗ |
set_file_pos(thisfile, 0u); |
| 586 |
|
✗ |
read_file(thisfile, filedata, 512); |
| 587 |
|
|
} |
| 588 |
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); |
| 589 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
romCrc = crc32(filedata, (unsigned int)(romlen + header * 512)); |
| 590 |
|
3 |
free(filedata); |
| 591 |
|
|
} |
| 592 |
|
|
} |
| 593 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
if (thisfile != InvalidFileHandle) close_file(thisfile); |
| 594 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
if (romdata) free(const_cast<unsigned char*>(romdata)); |
| 595 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
if (romdata_r) free(const_cast<unsigned char*>(romdata_r)); |
| 596 |
|
6 |
thisfile= InvalidFileHandle; |
| 597 |
|
6 |
romdata= nullptr; |
| 598 |
|
6 |
romdata_r = nullptr; |
| 599 |
|
6 |
romlen=0; |
| 600 |
|
6 |
return romCrc; |
| 601 |
|
|
} |
| 602 |
|
|
|
| 603 |
|
27 |
static unsigned int getchecksum() |
| 604 |
|
|
{ |
| 605 |
|
27 |
unsigned int checksum=0; |
| 606 |
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
|
27 |
if((romlen & (romlen-1)) == 0) |
| 607 |
|
|
{ |
| 608 |
|
|
// romlen is a power of 2, just add up all the bytes |
| 609 |
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]; |
| 610 |
|
|
} |
| 611 |
|
|
else |
| 612 |
|
|
{ |
| 613 |
|
|
// assume romlen is the sum of 2 powers of 2 - i haven't seen any real rom that isn't, |
| 614 |
|
|
// and if you make such a rom, fixing its checksum is your problem. |
| 615 |
|
4 |
int firstpart = bitround(romlen) >> 1; |
| 616 |
|
4 |
int secondpart = romlen - firstpart; |
| 617 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 |
int repeatcount = firstpart / secondpart; |
| 618 |
|
4 |
unsigned int secondpart_sum = 0; |
| 619 |
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 32778 times.
✓ Branch 3 taken 4 times.
|
32782 |
for(int i = 0; i < firstpart; i++) checksum += romdata[i]; |
| 620 |
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 4 times.
|
12 |
for(int i = firstpart; i < romlen; i++) secondpart_sum += romdata[i]; |
| 621 |
|
4 |
checksum += secondpart_sum * repeatcount; |
| 622 |
|
|
} |
| 623 |
|
27 |
return checksum&0xFFFF; |
| 624 |
|
|
} |
| 625 |
|
|
|
| 626 |
|
27 |
void fixchecksum() |
| 627 |
|
|
{ |
| 628 |
|
|
// randomdude999: clear out checksum bytes before recalculating checksum, this should make it correct on roms that don't have a checksum yet |
| 629 |
|
27 |
writeromdata(snestopc(0x00FFDC), "\xFF\xFF\0\0", 4); |
| 630 |
|
27 |
int checksum=(int)getchecksum(); |
| 631 |
|
27 |
writeromdata_byte(snestopc(0x00FFDE), (unsigned char)(checksum&255)); |
| 632 |
|
27 |
writeromdata_byte(snestopc(0x00FFDF), (unsigned char)((checksum>>8)&255)); |
| 633 |
|
27 |
writeromdata_byte(snestopc(0x00FFDC), (unsigned char)((checksum&255)^255)); |
| 634 |
|
27 |
writeromdata_byte(snestopc(0x00FFDD), (unsigned char)(((checksum>>8)&255)^255)); |
| 635 |
|
27 |
} |
| 636 |
|
|
|