|
| 1 | +# -------------------------------------------------------------------- |
| 2 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | +# © Copyright 2008-2024 José Manuel Rodríguez de la Rosa and contributors. |
| 4 | +# See the file CONTRIBUTORS.md for copyright details. |
| 5 | +# See https://www.gnu.org/licenses/agpl-3.0.html for details. |
| 6 | +# -------------------------------------------------------------------- |
| 7 | + |
| 8 | +from src.outfmt.tzx import TZX |
| 9 | + |
| 10 | + |
| 11 | +def test_tzx_init(): |
| 12 | + tzx = TZX() |
| 13 | + assert tzx.output == b"ZXTape!\x1a\x01\x15" |
| 14 | + |
| 15 | + |
| 16 | +def test_tzx_lh(): |
| 17 | + tzx = TZX() |
| 18 | + assert tzx.LH(0x1234) == [0x34, 0x12] |
| 19 | + assert tzx.LH(0x00FF) == [0xFF, 0x00] |
| 20 | + |
| 21 | + |
| 22 | +def test_tzx_standard_block(): |
| 23 | + tzx = TZX() |
| 24 | + tzx.output = bytearray() |
| 25 | + tzx.standard_block(b"\x01\x02") |
| 26 | + # BLOCK_STANDARD (0x10) |
| 27 | + # Pause (1000ms -> [0xE8, 0x03]) |
| 28 | + # Length (3 -> [0x03, 0x00]) - length of data + 1 for checksum |
| 29 | + # Data: \x01\x02 |
| 30 | + # Checksum: 0x01 ^ 0x02 = 0x03 |
| 31 | + assert tzx.output == b"\x10\xe8\x03\x03\x00\x01\x02\x03" |
| 32 | + |
| 33 | + |
| 34 | +def test_tzx_emit(tmp_path): |
| 35 | + output_file = tmp_path / "test.tzx" |
| 36 | + tzx = TZX() |
| 37 | + program_bytes = b"\x00\x01" |
| 38 | + tzx.emit( |
| 39 | + output_filename=str(output_file), |
| 40 | + program_name="test", |
| 41 | + loader_bytes=None, |
| 42 | + entry_point=16384, |
| 43 | + program_bytes=program_bytes, |
| 44 | + aux_bin_blocks=[], |
| 45 | + aux_headless_bin_blocks=[], |
| 46 | + ) |
| 47 | + assert output_file.exists() |
| 48 | + content = output_file.read_bytes() |
| 49 | + assert content.startswith(b"ZXTape!\x1a\x01\x15") |
| 50 | + |
| 51 | + # Header block for "test" |
| 52 | + # 0x10 (Standard block ID) |
| 53 | + # 0xE8 0x03 (1000ms pause) |
| 54 | + # 0x13 0x00 (Length of header: 1 + 1 + 10 + 2 + 2 + 2 + 1 = 19 -> 0x13) |
| 55 | + # 0x00 (BLOCK_TYPE_HEADER) |
| 56 | + # 0x03 (HEADER_TYPE_CODE) |
| 57 | + # "test " (10 bytes) |
| 58 | + # 0x02 0x00 (Length 2) |
| 59 | + # 0x00 0x40 (Address 16384) |
| 60 | + # 0x00 0x80 (32768) |
| 61 | + # Checksum (XOR of all bytes in block) |
| 62 | + expected_header_content = b"\x00\x03test \x02\x00\x00\x40\x00\x80" |
| 63 | + checksum = 0 |
| 64 | + for b in expected_header_content: |
| 65 | + checksum ^= b |
| 66 | + expected_header_block = b"\x10\xe8\x03\x13\x00" + expected_header_content + bytes([checksum]) |
| 67 | + assert expected_header_block in content |
| 68 | + |
| 69 | + # Data block |
| 70 | + # 0x10 |
| 71 | + # 0xE8 0x03 |
| 72 | + # 0x04 0x00 (Length of data: 1 (type) + 2 (bytes) + 1 (checksum) = 4) |
| 73 | + # 0xFF (BLOCK_TYPE_DATA) |
| 74 | + # 0x00 0x01 (data) |
| 75 | + # Checksum: 0xFF ^ 0x00 ^ 0x01 = 0xFE |
| 76 | + expected_data_block = b"\x10\xe8\x03\x04\x00\xff\x00\x01\xfe" |
| 77 | + assert expected_data_block in content |
0 commit comments