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