Skip to content

Commit fdf93cf

Browse files
committed
Scramble banks from the end of the ROM
This is more likely to test edge cases, such as having content in banks with their highest bit set.
1 parent 495d701 commit fdf93cf

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

src/link/assign.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,25 @@ static bool isLocationSuitable(struct Section const *section,
114114
static struct FreeSpace *getPlacement(struct Section const *section,
115115
struct MemoryLocation *location)
116116
{
117-
static uint16_t curScrambleROM = 1;
118-
static uint8_t curScrambleWRAM = 1;
119-
static uint8_t curScrambleSRAM = 1;
117+
static uint16_t curScrambleROM = 0;
118+
static uint8_t curScrambleWRAM = 0;
119+
static int8_t curScrambleSRAM = 0;
120120

121121
// Determine which bank we should start searching in
122122
if (section->isBankFixed) {
123123
location->bank = section->bank;
124124
} else if (scrambleROMX && section->type == SECTTYPE_ROMX) {
125-
location->bank = curScrambleROM++;
126-
if (curScrambleROM > scrambleROMX)
127-
curScrambleROM = 1;
125+
if (curScrambleROM < 1)
126+
curScrambleROM = scrambleROMX;
127+
location->bank = curScrambleROM--;
128128
} else if (scrambleWRAMX && section->type == SECTTYPE_WRAMX) {
129-
location->bank = curScrambleWRAM++;
130-
if (curScrambleWRAM > scrambleWRAMX)
131-
curScrambleWRAM = 1;
129+
if (curScrambleWRAM < 1)
130+
curScrambleWRAM = scrambleWRAMX;
131+
location->bank = curScrambleWRAM--;
132132
} else if (scrambleSRAM && section->type == SECTTYPE_SRAM) {
133-
location->bank = curScrambleSRAM++;
134-
if (curScrambleSRAM > scrambleSRAM)
135-
curScrambleSRAM = 0;
133+
if (curScrambleSRAM < 0)
134+
curScrambleSRAM = scrambleSRAM;
135+
location->bank = curScrambleSRAM--;
136136
} else {
137137
location->bank = sectionTypeInfo[section->type].firstBank;
138138
}
@@ -168,8 +168,7 @@ static struct FreeSpace *getPlacement(struct Section const *section,
168168
// Ensure we're there (e.g. on first check)
169169
location->address &= ~section->alignMask;
170170
// Go to next align boundary and add offset
171-
location->address += section->alignMask + 1
172-
+ section->alignOfs;
171+
location->address += section->alignMask + 1 + section->alignOfs;
173172
} else {
174173
// Any location is fine, so, next free block
175174
space = space->next;
@@ -179,20 +178,43 @@ static struct FreeSpace *getPlacement(struct Section const *section,
179178

180179
// If that location is past the current block's end,
181180
// go forwards until that is no longer the case.
182-
while (space && location->address >=
183-
space->address + space->size)
181+
while (space && location->address >= space->address + space->size)
184182
space = space->next;
185183

186184
// Try again with the new location/free space combo
187185
}
188186

189-
if (section->isBankFixed)
187+
// Try again in the next bank, if one is available.
188+
// Try scrambled banks in descending order until no bank in the scrambled range is available.
189+
// Otherwise, try in ascending order.
190+
if (section->isBankFixed) {
190191
return NULL;
191-
192-
// Try again in the next bank
193-
location->bank++;
194-
if (location->bank > sectionTypeInfo[section->type].lastBank)
192+
} else if (scrambleROMX && section->type == SECTTYPE_ROMX && location->bank <= scrambleROMX) {
193+
if (location->bank > sectionTypeInfo[section->type].firstBank)
194+
location->bank--;
195+
else if (scrambleROMX < sectionTypeInfo[section->type].lastBank)
196+
location->bank = scrambleROMX + 1;
197+
else
198+
return NULL;
199+
} else if (scrambleWRAMX && section->type == SECTTYPE_WRAMX && location->bank <= scrambleWRAMX) {
200+
if (location->bank > sectionTypeInfo[section->type].firstBank)
201+
location->bank--;
202+
else if (scrambleWRAMX < sectionTypeInfo[section->type].lastBank)
203+
location->bank = scrambleWRAMX + 1;
204+
else
205+
return NULL;
206+
} else if (scrambleSRAM && section->type == SECTTYPE_SRAM && location->bank <= scrambleSRAM) {
207+
if (location->bank > sectionTypeInfo[section->type].firstBank)
208+
location->bank--;
209+
else if (scrambleSRAM < sectionTypeInfo[section->type].lastBank)
210+
location->bank = scrambleSRAM + 1;
211+
else
212+
return NULL;
213+
} else if (location->bank < sectionTypeInfo[section->type].lastBank) {
214+
location->bank++;
215+
} else {
195216
return NULL;
217+
}
196218
#undef BANK_INDEX
197219
}
198220
}

test/link/scramble-romx/a.asm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SECTION "fixed", ROMX, BANK[3]
2+
ds $4000, 1
3+
4+
SECTION "floating", ROMX
5+
db 2

test/link/scramble-romx/out.err

Whitespace-only changes.

test/link/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ tryDiff overlay/out.err "$outtemp"
146146
tryCmp overlay/out.gb "$gbtemp"
147147
(( rc = rc || $? ))
148148

149+
i="scramble-romx.asm"
150+
startTest
151+
"$RGBASM" -o "$otemp" scramble-romx/a.asm
152+
rgblinkQuiet -o "$gbtemp" -S romx=3 "$otemp" >"$outtemp" 2>&1
153+
tryDiff scramble-romx/out.err "$outtemp"
154+
(( rc = rc || $? ))
155+
# This test does not compare its exact output with 'tryCmpRom' because no scrambling order is guaranteed
156+
test $(wc -c <"$gbtemp") = 65536
157+
(( rc = rc || $? ))
158+
149159
i="section-fragment/jr-offset.asm"
150160
startTest
151161
"$RGBASM" -o "$otemp" section-fragment/jr-offset/a.asm

0 commit comments

Comments
 (0)