-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.test.py
More file actions
325 lines (225 loc) · 13.5 KB
/
Copy pathlayout.test.py
File metadata and controls
325 lines (225 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from mapper import errors, layout
def offset_of(resolution: "layout.Resolution") -> int:
"""Where a resolved address sits in the file, insisting that it sits anywhere.
A resolution that landed on open bus or on save memory has no file offset at
all, so the attribute is optional. Every use below is about a region that does
have one, and saying so here means a test that accidentally resolves into open
bus fails on that rather than on arithmetic with None.
"""
assert resolution.offset is not None, f"{resolution.region} has no file offset"
return resolution.offset
class RegionTest(unittest.TestCase):
def test_work_ram_banks_are_named_as_work_ram(self) -> None:
for bank in (0x7E, 0x7F):
self.assertEqual(layout.resolve(layout.LOROM, bank << 16).region, layout.WORK_RAM)
def test_the_bottom_of_a_low_bank_is_the_mirror_of_work_ram(self) -> None:
found = layout.resolve(layout.LOROM, 0x000100)
self.assertEqual(found.region, layout.WORK_RAM)
def test_the_register_window_is_named_as_registers(self) -> None:
for address in (0x002100, 0x004200, 0x004300, 0x00420B):
self.assertEqual(layout.resolve(layout.LOROM, address).region, layout.REGISTERS)
def test_the_upper_half_of_a_low_bank_is_cartridge(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x008000).region, layout.ROM)
def test_a_high_bank_is_cartridge_throughout_in_high_rom(self) -> None:
self.assertEqual(layout.resolve(layout.HIROM, 0xC00000).region, layout.ROM)
def test_save_memory_has_its_own_window_in_low_rom(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x700000).region, layout.SAVE_RAM)
class SpeedTest(unittest.TestCase):
def test_the_low_half_of_the_space_is_always_slow(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x008000, fast=True).cycles, layout.SLOW)
def test_the_upper_half_runs_fast_when_the_cartridge_asks(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x808000, fast=True).cycles, layout.FAST)
def test_the_upper_half_stays_slow_when_it_does_not(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x808000, fast=False).cycles, layout.SLOW)
def test_work_ram_never_runs_fast(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x7E0000, fast=True).cycles, layout.SLOW)
def test_the_register_window_runs_at_its_own_speed(self) -> None:
self.assertEqual(layout.resolve(layout.LOROM, 0x004200, fast=True).cycles, layout.XSLOW)
class OffsetTest(unittest.TestCase):
def test_a_low_rom_bank_maps_its_upper_half_to_a_thirty_two_kilobyte_page(self) -> None:
self.assertEqual(offset_of(layout.resolve(layout.LOROM, 0x018000)), 0x008000)
def test_a_low_rom_offset_grows_by_a_page_per_bank(self) -> None:
first = offset_of(layout.resolve(layout.LOROM, 0x008000))
second = offset_of(layout.resolve(layout.LOROM, 0x028000))
self.assertEqual(second - first, 0x010000)
def test_a_high_rom_bank_maps_a_whole_bank(self) -> None:
self.assertEqual(offset_of(layout.resolve(layout.HIROM, 0xC10000)), 0x010000)
def test_a_mirror_bank_reaches_the_same_place(self) -> None:
self.assertEqual(
offset_of(layout.resolve(layout.LOROM, 0x808000)),
offset_of(layout.resolve(layout.LOROM, 0x008000)),
)
def test_a_region_that_is_not_cartridge_has_no_offset(self) -> None:
self.assertIsNone(layout.resolve(layout.LOROM, 0x7E0000).offset)
class ReachTest(unittest.TestCase):
def test_a_cartridge_address_is_reported_as_reachable(self) -> None:
self.assertTrue(layout.resolve(layout.LOROM, 0x008000).is_rom)
def test_work_ram_is_not_cartridge(self) -> None:
self.assertFalse(layout.resolve(layout.LOROM, 0x7E0000).is_rom)
def test_a_resolution_prints_as_its_region_and_address(self) -> None:
printed = repr(layout.resolve(layout.LOROM, 0x008000))
self.assertIn("rom", printed)
self.assertIn("008000", printed)
def test_an_address_wraps_into_the_space(self) -> None:
self.assertEqual(
layout.resolve(layout.LOROM, 0x1008000).address,
layout.resolve(layout.LOROM, 0x008000).address,
)
def test_every_address_in_the_space_resolves_to_something(self) -> None:
for bank in range(0, 0x100, 7):
for page in range(0, 0x10000, 0x1000):
found = layout.resolve(layout.LOROM, (bank << 16) | page)
self.assertIn(found.region, layout.REGIONS)
class ExtendedHighTest(unittest.TestCase):
"""The extended layout reaches past four megabytes; the plain high one cannot.
Its two halves are swapped relative to where a reader expects them. The banks
the console boots from carry the second half of the image, which is the whole
reason an extended cartridge keeps its header at 0x40FFC0.
"""
def test_a_low_bank_reaches_the_far_half_of_the_image(self) -> None:
found = layout.resolve(layout.EXHIROM, 0x00FFC0)
self.assertEqual(found.offset, 0x40FFC0)
def test_a_high_bank_reaches_the_near_half(self) -> None:
found = layout.resolve(layout.EXHIROM, 0xC0FFC0)
self.assertEqual(found.offset, 0x00FFC0)
def test_the_far_half_runs_to_the_top_of_the_low_banks(self) -> None:
found = layout.resolve(layout.EXHIROM, 0x7DFFFF)
self.assertEqual(found.offset, 0x7DFFFF)
def test_the_near_half_runs_to_the_top_of_the_space(self) -> None:
found = layout.resolve(layout.EXHIROM, 0xFFFFFF)
self.assertEqual(found.offset, 0x3FFFFF)
def test_the_plain_high_layout_never_reaches_past_four_megabytes(self) -> None:
for address in (0x00FFC0, 0x7DFFFF, 0xC0FFC0, 0xFFFFFF):
self.assertLess(offset_of(layout.resolve(layout.HIROM, address)), 0x400000)
def test_the_two_layouts_disagree_wherever_the_extended_one_is_the_point(self) -> None:
plain = offset_of(layout.resolve(layout.HIROM, 0x00FFC0))
extended = offset_of(layout.resolve(layout.EXHIROM, 0x00FFC0))
self.assertNotEqual(plain, extended)
def test_a_mirrored_low_bank_reaches_the_far_half_as_well(self) -> None:
found = layout.resolve(layout.EXHIROM, 0x30FFC0)
self.assertEqual(found.offset, 0x70FFC0)
def test_the_extended_layout_keeps_the_save_window_the_high_one_has(self) -> None:
found = layout.resolve(layout.EXHIROM, 0x206000)
self.assertEqual(found.region, layout.SAVE_RAM)
def test_work_ram_is_still_decided_before_any_cartridge_half(self) -> None:
found = layout.resolve(layout.EXHIROM, 0x7E0000)
self.assertEqual(found.region, layout.WORK_RAM)
def test_every_address_in_the_space_resolves_under_the_extended_layout(self) -> None:
for bank in range(0, 0x100, 7):
for page in range(0, 0x10000, 0x1000):
found = layout.resolve(layout.EXHIROM, (bank << 16) | page)
self.assertIn(found.region, layout.REGIONS)
def test_no_cartridge_offset_it_produces_falls_outside_the_largest_image(self) -> None:
for bank in range(0, 0x100):
found = layout.resolve(layout.EXHIROM, (bank << 16) | 0xFFFF)
if found.is_rom:
self.assertLess(offset_of(found), layout.EXHIROM_BYTES)
class WholeBankTest(unittest.TestCase):
"""The map that gives every bank below the window a whole sixty four kilobytes.
Named for its shape, not for a chip. The S-DD1 boards were where it was first
measured, and a cartridge that had its coprocessor removed lands here too,
declaring no chipset at all. Every offset asserted below is one a reference
implementation asserts, and that reference was validated against a cartridge
that boots on hardware.
"""
BANKS = 192
def test_the_upper_half_of_a_bank_sits_where_plain_low_rom_puts_it(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0x008000, banks=self.BANKS)
self.assertEqual((found.region, found.offset), (layout.ROM, 0x000000))
def test_and_the_next_bank_a_half_bank_further_in(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0x018000, banks=self.BANKS)
self.assertEqual(found.offset, 0x008000)
def test_the_lower_half_of_a_bank_sits_a_whole_image_away(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0x400000, banks=self.BANKS)
self.assertEqual((found.region, found.offset), (layout.ROM, 0x800000))
def test_the_window_banks_take_the_other_route(self) -> None:
low = layout.resolve(layout.WHOLEBANK, 0xC00000, banks=self.BANKS)
high = layout.resolve(layout.WHOLEBANK, 0xC08000, banks=self.BANKS)
self.assertEqual(low.offset, 0xA00000)
self.assertEqual(high.offset, 0x600000)
def test_the_window_advances_one_half_bank_per_bank(self) -> None:
for step in range(4):
found = layout.resolve(layout.WHOLEBANK, ((0xC0 + step) << 16), banks=self.BANKS)
self.assertEqual(found.offset, 0xA00000 + step * 0x8000)
def test_the_address_the_reference_pins_resolves_where_it_says(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0xC04D6A, banks=self.BANKS)
self.assertEqual(found.offset, 0xA04D6A)
def test_the_system_area_is_still_the_system_area(self) -> None:
self.assertEqual(
layout.resolve(layout.WHOLEBANK, 0x000100, banks=self.BANKS).region, layout.WORK_RAM
)
self.assertEqual(
layout.resolve(layout.WHOLEBANK, 0x002100, banks=self.BANKS).region, layout.REGISTERS
)
self.assertEqual(
layout.resolve(layout.WHOLEBANK, 0x7E0000, banks=self.BANKS).region, layout.WORK_RAM
)
def test_a_cartridge_with_save_memory_keeps_a_window_for_it(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0x700000, banks=self.BANKS, save=True)
self.assertEqual(found.region, layout.SAVE_RAM)
def test_and_one_without_carries_cartridge_there_instead(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0x700000, banks=self.BANKS)
self.assertEqual(found.region, layout.ROM)
def test_no_two_addresses_below_the_window_reach_the_same_byte(self) -> None:
seen: set[int] = set()
for bank in range(0x40, 0x7E):
for page in (0x0000, 0x8000):
found = offset_of(
layout.resolve(layout.WHOLEBANK, (bank << 16) | page, banks=self.BANKS)
)
self.assertNotIn(found, seen)
seen.add(found)
def test_and_no_window_bank_collides_with_one_below_it(self) -> None:
below = {
offset_of(layout.resolve(layout.WHOLEBANK, (bank << 16) | page, banks=self.BANKS))
for bank in range(0x40, 0x7E)
for page in (0x0000, 0x8000)
}
for bank in range(0xC0, 0x100):
for page in (0x0000, 0x8000):
found = layout.resolve(layout.WHOLEBANK, (bank << 16) | page, banks=self.BANKS)
self.assertNotIn(found.offset, below)
def test_every_cartridge_byte_it_names_is_inside_the_image(self) -> None:
size = self.BANKS * layout.BANK_BYTES
for bank in range(0x100):
for page in (0x0000, 0x8000, 0xFFFF):
found = layout.resolve(layout.WHOLEBANK, (bank << 16) | page, banks=self.BANKS)
if found.is_rom:
self.assertLess(offset_of(found), size, hex((bank << 16) | page))
def test_an_image_too_small_for_the_window_is_refused(self) -> None:
with self.assertRaises(errors.NeedsBankCount) as raised:
layout.resolve(layout.WHOLEBANK, 0x400000, banks=64)
self.assertIn("64", str(raised.exception))
def test_the_refusal_says_how_many_banks_the_map_wants(self) -> None:
with self.assertRaises(errors.NeedsBankCount) as raised:
layout.resolve(layout.WHOLEBANK, 0x400000, banks=191)
self.assertIn(str(layout.WHOLEBANK_BANKS), str(raised.exception))
def test_the_smallest_image_it_accepts_is_the_one_the_window_fits_in(self) -> None:
found = layout.resolve(layout.WHOLEBANK, 0xC00000, banks=layout.WHOLEBANK_BANKS)
self.assertEqual(found.offset, 0xA00000)
def test_every_window_byte_lands_inside_an_image_of_that_size(self) -> None:
size = layout.WHOLEBANK_BANKS * layout.BANK_BYTES
for bank in range(0xC0, 0x100):
for page in (0x0000, 0x7FFF, 0x8000, 0xFFFF):
found = layout.resolve(
layout.WHOLEBANK, (bank << 16) | page, banks=layout.WHOLEBANK_BANKS
)
self.assertLess(offset_of(found), size, hex((bank << 16) | page))
def test_resolving_it_without_a_bank_count_is_refused(self) -> None:
with self.assertRaises(errors.NeedsBankCount):
layout.resolve(layout.WHOLEBANK, 0x008000)
def test_the_refusal_says_what_is_missing(self) -> None:
with self.assertRaises(errors.NeedsBankCount) as raised:
layout.resolve(layout.WHOLEBANK, 0x008000)
self.assertIn("bank count", str(raised.exception))
def test_every_address_in_the_space_resolves_under_this_board(self) -> None:
for bank in range(0, 0x100, 7):
for page in range(0, 0x10000, 0x1000):
found = layout.resolve(layout.WHOLEBANK, (bank << 16) | page, banks=self.BANKS)
self.assertIn(found.region, layout.REGIONS)
if __name__ == "__main__":
unittest.main()