asar coverage - build #264


src/asar/
File: src/asar/libsmw.cpp
Date: 2025-02-28 06:49:25
Lines:
319/358
89.1%
Functions:
21/21
100.0%
Branches:
360/612
58.8%

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 483264 static int findromwritepos(int snesoffset, int searchstartpos, int searchendpos)
27 {
28
2/2
✓ Branch 0 taken 139379 times.
✓ Branch 1 taken 343885 times.
483264 if (searchendpos == searchstartpos)
29 {
30 139379 return searchstartpos;
31 }
32
33 343885 int centerpos = searchstartpos + ((searchendpos - searchstartpos) / 2);
34
35
2/2
✓ Branch 0 taken 126830 times.
✓ Branch 1 taken 217055 times.
343885 if (writtenblocks[centerpos].snesoffset >= snesoffset)
36 {
37 126830 return findromwritepos(snesoffset, searchstartpos, centerpos);
38 }
39
40 217055 return findromwritepos(snesoffset, centerpos + 1, searchendpos);
41 }
42
43
44 150367 static void addromwriteforbank(int snesoffset, int numbytes)
45 {
46
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 150208 times.
156243 if(numbytes == 0) return;
47
48 // common case: this rom write is immediately after the last one.
49
2/2
✓ Branch 0 taken 149993 times.
✓ Branch 1 taken 215 times.
150208 if(last_writtenblock_ind < writtenblocks.count) {
50
1/2
✓ Branch 0 taken 149993 times.
✗ Branch 1 not taken.
149993 auto& blk = writtenblocks[last_writtenblock_ind];
51
4/4
✓ Branch 0 taken 12266 times.
✓ Branch 1 taken 137727 times.
✓ Branch 2 taken 12263 times.
✓ Branch 3 taken 3 times.
149993 if(blk.snesoffset + blk.numbytes == snesoffset && (blk.snesoffset&0xff0000)==(snesoffset&0xff0000)) {
52 // check if we overlap with next block
53 24526 if(last_writtenblock_ind+1 >= writtenblocks.count
54
7/8
✓ Branch 0 taken 3589 times.
✓ Branch 1 taken 8674 times.
✓ Branch 2 taken 3589 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3111 times.
✓ Branch 5 taken 478 times.
✓ Branch 6 taken 11785 times.
✓ Branch 7 taken 478 times.
12263 || writtenblocks[last_writtenblock_ind+1].snesoffset > snesoffset+numbytes) {
55 // if not, merge with previous written block
56 11785 blk.numbytes += numbytes;
57 11785 return;
58 }
59 }
60 }
61 138423 int currentbank = (snesoffset & 0xFF0000);
62
63
1/2
✓ Branch 0 taken 138423 times.
✗ Branch 1 not taken.
138423 int insertpos = findromwritepos(snesoffset, 0, writtenblocks.count);
64
65
3/4
✓ Branch 0 taken 138164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137705 times.
✓ Branch 3 taken 459 times.
138164 if (insertpos > 0 && (writtenblocks[insertpos - 1].snesoffset & 0xFF0000) == currentbank
66
8/10
✓ Branch 0 taken 138164 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 137705 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 137705 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 137324 times.
✓ Branch 7 taken 381 times.
✓ Branch 8 taken 137324 times.
✓ Branch 9 taken 1099 times.
276587 && 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 137324 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137324 times.
✗ Branch 3 not taken.
137324 int firstend = writtenblocks[insertpos - 1].snesoffset + writtenblocks[insertpos - 1].numbytes;
70 137324 int secondend = snesoffset + numbytes;
71
72
2/2
✓ Branch 0 taken 136431 times.
✓ Branch 1 taken 893 times.
137324 int newend = (firstend > secondend ? firstend : secondend);
73
74
1/2
✓ Branch 0 taken 137324 times.
✗ Branch 1 not taken.
137324 numbytes = newend - writtenblocks[insertpos - 1].snesoffset;
75
1/2
✓ Branch 0 taken 137324 times.
✗ Branch 1 not taken.
137324 snesoffset = writtenblocks[insertpos - 1].snesoffset;
76
77 137324 writtenblocks.remove(insertpos - 1);
78 137324 insertpos -= 1;
79 }
80
81
3/4
✓ Branch 0 taken 126724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61100 times.
✓ Branch 3 taken 65624 times.
265147 while (insertpos < writtenblocks.count && (writtenblocks[insertpos].snesoffset & 0xFF0000) == currentbank
82
7/8
✓ Branch 0 taken 126724 times.
✓ Branch 1 taken 12255 times.
✓ Branch 2 taken 61100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 556 times.
✓ Branch 5 taken 60544 times.
✓ Branch 6 taken 556 times.
✓ Branch 7 taken 138423 times.
265703 && snesoffset + numbytes >= writtenblocks[insertpos].snesoffset)
83 {
84 // Merge if we overlap with a succeeding block
85 556 int firstend = snesoffset + numbytes;
86
2/4
✓ Branch 0 taken 556 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
556 int secondend = writtenblocks[insertpos].snesoffset + writtenblocks[insertpos].numbytes;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 556 times.
556 int newend = (firstend > secondend ? firstend : secondend);
89
90 556 numbytes = newend - snesoffset;
91
92 556 writtenblocks.remove(insertpos);
93 }
94
95 // Insert ROM write
96 69295 writtenblockdata blockdata;
97 138423 blockdata.snesoffset = snesoffset;
98 138423 blockdata.pcoffset = snestopc(snesoffset);
99 138423 blockdata.numbytes = numbytes;
100
101
1/2
✓ Branch 0 taken 138423 times.
✗ Branch 1 not taken.
138423 writtenblocks.insert(insertpos, blockdata);
102 138423 last_writtenblock_ind = insertpos;
103 }
104
105
106 150208 void addromwrite(int pcoffset, int numbytes)
107 {
108 150208 int snesaddr = pctosnes(pcoffset);
109 150208 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 159 times.
✓ Branch 1 taken 150208 times.
150367 while ((snesaddr >> 16) != ((snesaddr + bytesleft) >> 16))
115 {
116 159 int bytesinbank = ((snesaddr + 0x10000) & 0xffff0000) - snesaddr;
117
118 159 addromwriteforbank(snesaddr, bytesinbank);
119
120 159 pcoffset += bytesinbank;
121 159 snesaddr = pctosnes(pcoffset);
122 159 bytesleft -= bytesinbank;
123 }
124
125 150208 addromwriteforbank(snesaddr, bytesleft);
126 150208 }
127
128 28 void writeromdata(int pcoffset, const void * indata, int numbytes)
129 {
130
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);
131 28 addromwrite(pcoffset, numbytes);
132 28 }
133
134 149604 void writeromdata_byte(int pcoffset, unsigned char indata, bool add_write)
135 {
136 149604 memcpy(const_cast<unsigned char*>(romdata) + pcoffset, &indata, 1);
137
2/2
✓ Branch 0 taken 140502 times.
✓ Branch 1 taken 9102 times.
149604 if(add_write)
138 140502 addromwrite(pcoffset, 1);
139 149604 }
140
141 126 void writeromdata_bytes(int pcoffset, unsigned char indata, int numbytes, bool add_write)
142 {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
126 memset(const_cast<unsigned char*>(romdata) + pcoffset, indata, (size_t)numbytes);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if(add_write)
145 addromwrite(pcoffset, numbytes);
146 126 }
147
148 110 int ratsstart(int snesaddr)
149 {
150 110 int pcaddr=snestopc(snesaddr);
151
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 106 times.
110 if (pcaddr<0x7FFF8) return -1;
152 106 const unsigned char * start=romdata+pcaddr-0x10000;
153
2/2
✓ Branch 0 taken 132250 times.
✓ Branch 1 taken 2 times.
132252 for (int i=0x10000;i>=0;i--)
154 {
155
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 132198 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 66073 times.
132250 if (!strncmp((const char*)start+i, "STAR", 4) &&
156
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
104 (start[i+4]^start[i+6])==0xFF && (start[i+5]^start[i+7])==0xFF)
157 {
158
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
104 if ((start[i+4]|(start[i+5]<<8))>0x10000-i-8-1) return pctosnes((int)(start-romdata+i));
159 return -1;
160 }
161 }
162 2 return -1;
163 }
164
165 6 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
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))
170 {
171
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
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (len%3) return;
173 6 len/=3;
174
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 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);
175 }
176 }
177
178 28 void removerats(int snesaddr, unsigned char clean_byte)
179 {
180 28 int addr=ratsstart(snesaddr);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (addr<0) return;
182 // randomdude999: don't forget bank borders
183 28 WalkMetadata(pctosnes(snestopc(addr)+8), handleprot);
184 28 addr=snestopc(addr);
185 // don't use writeromdata() because this must not go to the writtenblocks list.
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
28 int len = (romdata[addr+4]|(romdata[addr+5]<<8))+9;
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
28 memset(const_cast<unsigned char*>(romdata) + addr, clean_byte, len);
188
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};
189 }
190
191 270 void handle_cleared_rats_tags()
192 {
193
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 270 times.
298 for(int i = 0; i < cleared_rats_tag_blocks.count; i++) {
194 28 auto & block = cleared_rats_tag_blocks[i];
195 28 addromwrite(block.pcoffset, block.numbytes);
196 }
197 270 cleared_rats_tag_blocks.reset();
198 270 }
199
200 54 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 54 found_rats_tags.clear();
205
2/2
✓ Branch 0 taken 37748952 times.
✓ Branch 1 taken 54 times.
37749006 for(int pos = 0; pos < romlen;) {
206
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37748950 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 18874474 times.
37748952 if (!strncmp((const char*)romdata+pos, "STAR", 4) &&
207
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (romdata[pos+4]^romdata[pos+6])==0xFF &&
208
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (romdata[pos+5]^romdata[pos+7])==0xFF) {
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 int block_len = (romdata[pos+4]|(romdata[pos+5]<<8))+1+8;
210
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 found_rats_tags.push_back(writtenblockdata{pos, 0, block_len});
211 4 pos += block_len;
212 4 } else {
213 37748948 pos++;
214 }
215 }
216 54 found_rats_tags_initialized = true;
217 54 }
218
219 552 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 10 times.
✓ Branch 1 taken 542 times.
552 if(alt_start >= 0) start = std::max(start, alt_start);
222
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();
223
2/2
✓ Branch 0 taken 143050 times.
✓ Branch 1 taken 38 times.
143088 while (start+size<=end)
224 {
225
2/2
✓ Branch 0 taken 142878 times.
✓ Branch 1 taken 172 times.
143050 if (write_rats &&
226
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...
227 )
228 {
229 // if we are not already at the end of the bank
230
2/2
✓ Branch 0 taken 2574 times.
✓ Branch 1 taken 4 times.
2578 if((start&banksize&0xFFFFF8) != (banksize&0xFFFFF8)) {
231 2574 start&=~banksize&0xFFFFFF;//round it down to the start of the bank,
232 2574 start|=banksize&0xFFFFF8;//then round it up to the end minus the RATS tag...
233 72555 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 4 start = (start+7) & ~7;
240 4 continue;
241 }
242 }
243
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 140300 times.
140472 else if(!write_rats &&
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172 times.
172 (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 6 times.
✓ Branch 1 taken 140466 times.
140472 if (minalign)
251 {
252 6 start&=~minalign&0xFFFFFF;
253 6 start|=minalign&0xFFFFF8;
254 }
255 140472 bool bad=false;
256
2/2
✓ Branch 0 taken 8815886 times.
✓ Branch 1 taken 958 times.
8816844 for (int i=0;i<size;i++)
257 {
258
2/2
✓ Branch 0 taken 139514 times.
✓ Branch 1 taken 8676372 times.
8815886 if (romdata[start+i]!=freespacebyte)
259 {
260 // TheBiob: fix freedata align freezing.
261
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;
262 139514 start+=i;
263
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.
264 139514 bad=true;
265 139514 break;
266 }
267 }
268
2/2
✓ Branch 0 taken 139514 times.
✓ Branch 1 taken 958 times.
140472 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 958 times.
✗ Branch 1 not taken.
958 auto rats = std::lower_bound(found_rats_tags.begin(), found_rats_tags.end(), start, [](writtenblockdata& blk, int start){
273 14 return blk.pcoffset < start;
274 });
275 // if there's a rats tag after us,
276
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 954 times.
958 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 4 times.
4 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 10 times.
✓ Branch 1 taken 948 times.
958 if(rats != found_rats_tags.begin()) {
286 10 rats--;
287 // and it doesn't end before our spot starts,
288
5/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 8 times.
10 if(start >= rats->pcoffset && start < rats->pcoffset+rats->numbytes) {
289 // we're inside this rats tag, skip to its end
290 2 start = rats->pcoffset + rats->numbytes;
291 2 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 956 int snesstart = pctosnes(start);
298 // this gives the index of the first block whose snespos is >= start.
299
1/2
✓ Branch 0 taken 956 times.
✗ Branch 1 not taken.
956 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 938 times.
✓ Branch 1 taken 18 times.
956 if(writtenblock_pos > 0) writtenblock_pos--;
303
2/2
✓ Branch 0 taken 962 times.
✓ Branch 1 taken 502 times.
1464 for(; writtenblock_pos < writtenblocks.count; writtenblock_pos++)
304 {
305
1/2
✓ Branch 0 taken 962 times.
✗ Branch 1 not taken.
962 auto & block = writtenblocks[writtenblock_pos];
306
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 508 times.
962 if(snesstart < block.snesoffset+block.numbytes
307
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 12 times.
454 && snesstart+size > block.snesoffset)
308 {
309 442 start = block.pcoffset + block.numbytes;
310 442 bad = true;
311 442 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 12 times.
✓ Branch 1 taken 508 times.
520 if(block.snesoffset > snesstart+size) break;
315 }
316
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 514 times.
956 if (bad) continue;
317
2/2
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 28 times.
514 if(write_rats)
318 {
319 486 size-=8;
320
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 addromwrite(start+8, size);
321
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
322
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+0, 'S');
323
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+1, 'T');
324
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+2, 'A');
325
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+3, 'R');
326
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+4, (unsigned char)(size&0xFF));
327
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+5, (unsigned char)((size>>8)&0xFF));
328
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+6, (unsigned char)((size&0xFF)^0xFF));
329
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 writeromdata_byte(start+7, (unsigned char)(((size>>8)&0xFF)^0xFF));
330 500 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 28 times.
✗ Branch 1 not taken.
28 addromwrite(start, size);
337 28 return start;
338 }
339 }
340 38 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 514 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 514 times.
514 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/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;
356 514 bool isforcode = target_bank == -2;
357
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)
358 486 size+=8;
359
360 262 auto find_for_fixed_bank = [&](int banksize, bool force_high_half = false) {
361
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
10 if(!force_high_half && (target_bank & 0x40) == 0x40)
362
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);
363 else
364
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);
365 514 };
366
367
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)
368 {
369
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);
370
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;
371 513 rebootlorom:
372
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)
373 {
374
8/10
✗ 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 taken 14 times.
✗ Branch 9 not taken.
56 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 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats);
376
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (pos>=0) return pos;
377 }
378
9/10
✓ 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 taken 252 times.
✗ Branch 9 not taken.
1002 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 498 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
498 respectbankborders?0x7FFF:0xFFFFFF, align?0x7FFF:(respectbankborders || size<32768)?0:0x7FFF, freespacebyte, write_rats);
380
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 38 times.
504 if (pos>=0) return pos;
381
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (autoexpand)
382 {
383 if(0);
384
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 4 times.
38 else if (romlen==0x080000)
385 {
386
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 writeromdata_bytes(romlen, freespacebyte, 0x100000 - romlen, false);
387 34 romlen=0x100000;
388
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 writeromdata_byte(snestopc(0x00FFD7), 0x0A);
389 }
390
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 else if (romlen==0x100000)
391 {
392
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_bytes(romlen, freespacebyte, 0x200000 - romlen, false);
393 2 romlen=0x200000;
394
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_byte(snestopc(0x00FFD7), 0x0B);
395 }
396
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
397
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)
398 {
399
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_bytes(romlen, freespacebyte, 0x400000 - romlen, false);
400 2 romlen=0x400000;
401
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_byte(snestopc(0x00FFD7), 0x0C);
402 }
403 else return -1;
404 38 autoexpand=false;
405 38 goto rebootlorom;
406 }
407 }
408
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)
409 {
410
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);
411
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (isforcode)
412 {
413
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for(int i = 0x8000; i < min(romlen, 0x400000); i += 0x10000){
414
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 int space = trypcfreespace(i, search_start, min(i+0x7FFF, romlen), size, 0x7FFF, align?0xFFFF:0, freespacebyte, write_rats);
415
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if(space != -1) return space;
416 }
417 return -1;
418 }
419
5/8
✗ 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 taken 2 times.
✗ Branch 7 not taken.
4 return trypcfreespace(0, search_start, romlen, size, respectbankborders?0xFFFF:0xFFFFFF, align?0xFFFF:0, freespacebyte, write_rats);
420 }
421
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 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
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 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
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)
446 {
447
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);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!isforcode) return -1;
449 // try not to overwrite smw stuff
450
5/8
✗ 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 taken 1 times.
✗ Branch 7 not taken.
2 return trypcfreespace(search_start >= 0 ? 0 : 0x80000, search_start, romlen, size, 0x7FFF, align?0x7FFF:0, freespacebyte, write_rats);
451 }
452
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)
453 {
454
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);
455 4 rebootsa1rom:
456 4 int nextbank=-1;
457
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int i=0;i<8;i++)
458 {
459
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (i&2) continue;
460
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (sa1banks[i]+0x100000>romlen)
461 {
462
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 if (sa1banks[i]<=romlen && sa1banks[i]+0x100000>romlen) nextbank=sa1banks[i];
463 8 continue;
464 }
465
5/8
✗ 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 taken 1 times.
✗ Branch 7 not taken.
2 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 2 times.
✗ Branch 1 not taken.
2 if (pos>=0) return pos;
467 }
468
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)
469 {
470 2 unsigned char x7FD7[]={0, 0x0A, 0x0B, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D};
471
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_bytes(romlen, freespacebyte, nextbank + 0x100000 - romlen, false);
472 2 romlen=nextbank+0x100000;
473
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 writeromdata_byte(0x7FD7, x7FD7[romlen>>20]);
474 2 autoexpand=false;
475 2 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 28 void WalkMetadata(int loc, void(*func)(int loc, char * name, int len, const unsigned char * contents))
493 {
494 28 int pcoff=snestopc(loc);
495
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
28 if (strncmp((const char*)romdata+pcoff-8, "STAR", 4)) return;
496 28 const unsigned char * metadata=romdata+pcoff;
497
8/10
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 24 times.
34 while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3]))
498 {
499
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))
500 {
501 4 metadata=romdata+pcoff;
502
5/10
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
10 while (is_upper(metadata[0]) && is_upper(metadata[1]) && is_upper(metadata[2]) && is_upper(metadata[3]))
503 {
504
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))
505 {
506 4 break;
507 }
508 6 func(pctosnes((int)(metadata-romdata)), (char*)const_cast<unsigned char*>(metadata), metadata[4], metadata+5);
509 6 metadata+=5+metadata[4];
510 }
511 4 break;
512 }
513 6 metadata+=5+metadata[4];
514 }
515 }
516
517 514 int getsnesfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, bool write_rats, int search_start)
518 {
519 514 int pcfree = getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, write_rats, snestopc(search_start));
520 514 int snesfree = pctosnes(pcfree);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514 times.
514 if(snesfree == -1) return -1;
522 514 bool isforcode = target_bank == -2;
523 // not sure if this is ever a concern for other mappers...
524
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)) {
525 2 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 2 times.
2 if((snesfree&0x8000) != 0x8000) return -1;
528 }
529 514 return snesfree;
530 }
531
532 3 bool openrom(const char * filename, bool confirm)
533 {
534 3 closerom();
535 3 thisfile = open_file(filename, FileOpenMode_ReadWrite);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (thisfile == InvalidFileHandle)
537 {
538 openromerror = error_id_open_rom_failed;
539 return false;
540 }
541 3 header=false;
542
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)
543 {
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 const char * fnameend=strchr(filename, '\0')-4;
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 header=(!stricmp(fnameend, ".smc"));
546 }
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 romlen=(int)get_file_size(thisfile)-(header*512);
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (romlen<0) romlen=0;
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 set_file_pos(thisfile, header*512);
550 3 romdata=(unsigned char*)malloc(sizeof(unsigned char)*16*1024*1024);
551 3 int truelen=(int)read_file(thisfile, (void*)romdata, (uint32_t)romlen);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (truelen!=romlen)
553 {
554 openromerror = error_id_open_rom_failed;
555 free(const_cast<unsigned char*>(romdata));
556 return false;
557 }
558
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));
559
2/10
✗ 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 taken 3 times.
3 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 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/2
✓ Branch 0 taken 131084 times.
✓ Branch 1 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/2
✓ Branch 0 taken 65546 times.
✓ Branch 1 taken 5 times.
65551 for(int i = 0; i < firstpart; i++) checksum += romdata[i];
621
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 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