Crusader_Decomp/tools/tests/test_ghidra_to_dosbox_memory.py
Marco 1225a3e737 Refactor and enhance documentation and tooling for Crusader USECODE
- Updated retail-debugger-entry-options.md to clarify the status of the debugger in No Remorse and No Regret, including findings from recent investigations.
- Revised usecode-format-specification.md to improve clarity on opcode behavior and historical naming conventions, with added references to Japanese executable analysis.
- Introduced usecode-jp-dispatch-analysis.md to document findings from the Japanese No Remorse executable, detailing opcode dispatch and search object behavior.
- Added ghidra_to_dosbox_memory.py, a new tool for translating Ghidra segment:offset addresses to DOSBox physical memory offsets, including comprehensive address validation.
- Created test_ghidra_to_dosbox_memory.py to ensure the correctness of address translation logic and selector base loading, with unit tests covering various scenarios.
2026-10-08 17:40:59 +02:00

68 lines
No EOL
2.4 KiB
Python

from __future__ import annotations
import json
import tempfile
import unittest
from pathlib import Path
from tools.ghidra_to_dosbox_memory import (
load_selector_bases,
ne_segment_to_selector,
selector_to_ne_segment,
translate_address,
)
class GhidraToDosboxMemoryTests(unittest.TestCase):
def setUp(self) -> None:
self.selector_bases = {
0x1030: 0x00300000,
0x1480: 0x00410000,
}
def test_ne_segment_address_translates_through_its_selector(self) -> None:
result = translate_address("0007:28ce", self.selector_bases)
self.assertEqual(result.selector, 0x1030)
self.assertEqual(result.ne_segment, 7)
self.assertEqual(result.physical_address, 0x003028CE)
def test_selector_address_translates_to_physical_memory(self) -> None:
result = translate_address("1480:712c", self.selector_bases)
self.assertEqual(result.ne_segment, 0x91)
self.assertEqual(result.physical_address, 0x0041712C)
def test_selector_and_ne_segment_conversions_are_inverse(self) -> None:
self.assertEqual(selector_to_ne_segment(0x1480), 0x91)
self.assertEqual(ne_segment_to_selector(0x91), 0x1480)
def test_unknown_runtime_base_is_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "no verified physical base"):
translate_address("0008:1000", self.selector_bases)
def test_non_ne_selector_is_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "invalid Crusader NE selector"):
selector_to_ne_segment(0x1481)
def test_selector_outside_ne_address_space_is_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "outside the NE segment range"):
selector_to_ne_segment(0x10000)
def test_selector_base_map_loads_hex_values(self) -> None:
document = {
"program": "REGRET.EXE",
"selector_bases": {"0x1480": "0x410000"},
}
with tempfile.TemporaryDirectory() as temporary_directory:
map_path = Path(temporary_directory) / "selector-bases.json"
map_path.write_text(json.dumps(document), encoding="utf-8")
program, selector_bases = load_selector_bases(map_path)
self.assertEqual(program, "REGRET.EXE")
self.assertEqual(selector_bases, {0x1480: 0x410000})
if __name__ == "__main__":
unittest.main()