Skip to content

Commit

Permalink
Make get_snapshot() return just the 128K RAM when 'page' is -1
Browse files Browse the repository at this point in the history
  • Loading branch information
skoolkid committed Sep 2, 2023
1 parent 23417ba commit 4ac29a7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
23 changes: 14 additions & 9 deletions skoolkit/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ def can_read(fname):
# Component API
def get_snapshot(fname, page=None):
"""
Read a snapshot file and produce a 65536-element list of byte values.
Read a snapshot file and produce a list of byte values. For a 48K snapshot,
or a 128K snapshot with a page number (0-7) specified, the list contains
65536 (64K) elements: a blank 16K ROM followed by 48K RAM. For a 128K
snapshot with `page` equal to -1, the list contains 131072 (128K) elements:
RAM banks 0-7 (16K each) in order.
:param fname: The snapshot filename.
:param page: The page number to map to addresses 49152-65535 (C000-FFFF).
This is relevant only when reading a 128K snapshot file.
:return: A 65536-element list of byte values.
:param page: The page number (0-7) to map to addresses 49152-65535
(C000-FFFF), or -1 to return all RAM banks. This is relevant
only when reading a 128K snapshot file.
:return: A list of byte values.
"""
if not can_read(fname):
raise SnapshotError("{}: Unknown file type".format(fname))
Expand All @@ -113,11 +118,11 @@ def get_snapshot(fname, page=None):
ram = _read_z80(data, page)
elif ext == '.szx':
ram = _read_szx(data, page)
if len(ram) not in (49152, 131072):
raise SnapshotError("RAM size is {0}".format(len(ram)))
mem = [0] * 16384
mem.extend(ram)
return mem
if len(ram) == 49152:
return [0] * 16384 + list(ram)
if len(ram) == 131072:
return list(ram)
raise SnapshotError(f'RAM size is {len(ram)}')

def make_snapshot(fname, org, start=None, end=65536, page=None):
snapshot_reader = get_snapshot_reader()
Expand Down
4 changes: 2 additions & 2 deletions skoolkit/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def run(snafile, options, config):
if snafile == '48':
memory = [0] * 0x10000
else:
memory = [0] * 0x28000
memory = [0] * 0x20000
reg = None
org = 0
else:
Expand Down Expand Up @@ -241,7 +241,7 @@ def run(snafile, options, config):
elif len(memory) == 65536:
memory[:0x4000] = read_bin_file(ROM48)
else:
banks = [memory[a:a + 0x4000] for a in range(0x4000, 0x24000, 0x4000)]
banks = [memory[a:a + 0x4000] for a in range(0, 0x20000, 0x4000)]
memory = Memory(banks, out7ffd)
for spec in options.pokes:
poke(memory, spec)
Expand Down
6 changes: 3 additions & 3 deletions sphinx/source/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ skoolkit.disassembler.OperandFormatter:

Snapshot reader
---------------
This object is responsible for producing a 65536-element list of byte values
from a snapshot file. It must supply the following API functions, in common
with skoolkit.snapshot:
This object is responsible for producing a list of byte values from a snapshot
file. It must supply the following API functions, in common with
skoolkit.snapshot:

.. automodule:: skoolkit.snapshot
:members: can_read, get_snapshot
Expand Down
9 changes: 5 additions & 4 deletions tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ def test_sna_128k_all_pages(self):
config.append(0) # TR-DOS ROM not paged
sna = header + pages[5] + pages[2] + pages[3] + config + pages[0] + pages[1] + pages[4] + pages[6] + pages[7]
tmp_sna = self.write_bin_file(sna, suffix='.sna')
snapshot = get_snapshot(tmp_sna, -1)
ram = snapshot[16384:]
ram = get_snapshot(tmp_sna, -1)
self.assertEqual(len(ram), 131072)
self.assertTrue(set(ram[0x00000:0x04000]), {5})
self.assertTrue(set(ram[0x04000:0x08000]), {2})
Expand All @@ -133,7 +132,8 @@ class Z80Test(SnapshotTest):
def _test_z80(self, exp_ram, version, compress, machine_id=0, modify=False, out_7ffd=0, pages={}, page=None):
model, tmp_z80 = self.write_z80(exp_ram, version, compress, machine_id, modify, out_7ffd, pages)
snapshot = get_snapshot(tmp_z80, page)
self._check_ram(snapshot[16384:], exp_ram, model, out_7ffd, pages, page)
ram = snapshot[16384:] if len(snapshot) == 65536 else snapshot
self._check_ram(ram, exp_ram, model, out_7ffd, pages, page)

def test_z80v1(self):
exp_ram = [n & 255 for n in range(49152)]
Expand Down Expand Up @@ -343,7 +343,8 @@ class SZXTest(SnapshotTest):
def _test_szx(self, exp_ram, compress, machine_id=1, ch7ffd=0, pages={}, page=None):
tmp_szx = self.write_szx(exp_ram, compress, machine_id, ch7ffd, pages)
snapshot = get_snapshot(tmp_szx, page)
self._check_ram(snapshot[16384:], exp_ram, machine_id, ch7ffd, pages, page)
ram = snapshot[16384:] if len(snapshot) == 65536 else snapshot
self._check_ram(ram, exp_ram, machine_id, ch7ffd, pages, page)

def test_szx_16k(self):
exp_ram = [(n + 13) & 255 for n in range(16384)]
Expand Down

0 comments on commit 4ac29a7

Please sign in to comment.