@@ -114,25 +114,25 @@ static bool isLocationSuitable(struct Section const *section,
114
114
static struct FreeSpace *getPlacement (struct Section const *section,
115
115
struct MemoryLocation *location)
116
116
{
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 ;
120
120
121
121
// Determine which bank we should start searching in
122
122
if (section->isBankFixed ) {
123
123
location->bank = section->bank ;
124
124
} 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-- ;
128
128
} 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-- ;
132
132
} 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-- ;
136
136
} else {
137
137
location->bank = sectionTypeInfo[section->type ].firstBank ;
138
138
}
@@ -168,8 +168,7 @@ static struct FreeSpace *getPlacement(struct Section const *section,
168
168
// Ensure we're there (e.g. on first check)
169
169
location->address &= ~section->alignMask ;
170
170
// 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 ;
173
172
} else {
174
173
// Any location is fine, so, next free block
175
174
space = space->next ;
@@ -179,20 +178,43 @@ static struct FreeSpace *getPlacement(struct Section const *section,
179
178
180
179
// If that location is past the current block's end,
181
180
// 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 )
184
182
space = space->next ;
185
183
186
184
// Try again with the new location/free space combo
187
185
}
188
186
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 ) {
190
191
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 {
195
216
return NULL ;
217
+ }
196
218
#undef BANK_INDEX
197
219
}
198
220
}
0 commit comments