diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2024-02-24 20:30:17 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2024-02-24 20:30:17 +0200 |
| commit | 6dd1e346d330e662e42dd8bc75d17317223908fd (patch) | |
| tree | c081b1de90cfc5ca7007a8935e6892aaee03aa56 | |
| parent | 0a2cf66672364ed555c85682e948cff0b54c8f35 (diff) | |
Add setup.py, fix bugs
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 9 | ||||
| -rw-r--r-- | atk16.egg-info/PKG-INFO | 4 | ||||
| -rw-r--r-- | atk16.egg-info/SOURCES.txt | 30 | ||||
| -rw-r--r-- | atk16.egg-info/dependency_links.txt | 1 | ||||
| -rw-r--r-- | atk16.egg-info/entry_points.txt | 3 | ||||
| -rw-r--r-- | atk16.egg-info/requires.txt | 1 | ||||
| -rw-r--r-- | atk16.egg-info/top_level.txt | 3 | ||||
| -rwxr-xr-x | atk16_asm/assembler.py | 11 | ||||
| -rw-r--r-- | atk16_asm/pad_bin.py | 42 | ||||
| -rw-r--r-- | atk16_emu/cli.py | 62 | ||||
| -rw-r--r-- | atk16_emu/debugger.py | 8 | ||||
| -rw-r--r-- | setup.py | 16 |
13 files changed, 160 insertions, 32 deletions
@@ -7,3 +7,5 @@ out/ venv/ __pycache__ .pytest_cache + +build/ @@ -1,7 +1,8 @@ -.PHONY: all test +.PHONY: all test install py = /usr/bin/env python3 digital_path = $(HOME)/.local/share/Digital/digital.jar +PREFIX=/usr/local start-digital: java -jar $(digital_path) & @@ -46,5 +47,11 @@ test-digital-alu: test: pytest --ignore resources +install: + mkdir -p "$(PREFIX)/share/atk16/asm" + cp atk16_asm/*.py "$(PREFIX)/share/atk16/asm/" + printf "#!/bin/sh\ncd \"$(PREFIX)/share/atk16\"\n/usr/bin/env python -m asm.assembler \$$@\n" > "$(PREFIX)/bin/atk16c" + chmod +x "$(PREFIX)/bin/atk16c" + run-single-test: java -cp $(digital_path) CLI test -verbose -circ $(circ) -tests $(tests) diff --git a/atk16.egg-info/PKG-INFO b/atk16.egg-info/PKG-INFO new file mode 100644 index 0000000..193ecdf --- /dev/null +++ b/atk16.egg-info/PKG-INFO @@ -0,0 +1,4 @@ +Metadata-Version: 2.1 +Name: atk16 +Version: 0.1 +Requires-Dist: getch>=1.0 diff --git a/atk16.egg-info/SOURCES.txt b/atk16.egg-info/SOURCES.txt new file mode 100644 index 0000000..069ea3e --- /dev/null +++ b/atk16.egg-info/SOURCES.txt @@ -0,0 +1,30 @@ +setup.py +atk16.egg-info/PKG-INFO +atk16.egg-info/SOURCES.txt +atk16.egg-info/dependency_links.txt +atk16.egg-info/entry_points.txt +atk16.egg-info/requires.txt +atk16.egg-info/top_level.txt +atk16_asm/__init__.py +atk16_asm/asm_eval.py +atk16_asm/asm_ops.py +atk16_asm/asm_pass0.py +atk16_asm/asm_pass1.py +atk16_asm/asm_pass2.py +atk16_asm/asm_pass3.py +atk16_asm/asm_pass4.py +atk16_asm/assembler.py +atk16_asm/optimizer.py +atk16_asm/pad_bin.py +atk16_asm/parser.py +atk16_asm/tokenizer.py +atk16_emu/__init__.py +atk16_emu/cli.py +atk16_emu/debugger.py +atk16_emu/emu.py +atk16_emu/opcodes.py +atk16_emu/test/__init__.py +atk16_emu/test/test_opcode_alr.py +atk16_emu/test/test_opcode_hlt.py +test/__init__.py +test/utils.py
\ No newline at end of file diff --git a/atk16.egg-info/dependency_links.txt b/atk16.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/atk16.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/atk16.egg-info/entry_points.txt b/atk16.egg-info/entry_points.txt new file mode 100644 index 0000000..543f53b --- /dev/null +++ b/atk16.egg-info/entry_points.txt @@ -0,0 +1,3 @@ +[console_scripts] +atk16c = atk16_asm.assembler:main +atk16emu = atk16_emu.cli:main diff --git a/atk16.egg-info/requires.txt b/atk16.egg-info/requires.txt new file mode 100644 index 0000000..36f298c --- /dev/null +++ b/atk16.egg-info/requires.txt @@ -0,0 +1 @@ +getch>=1.0 diff --git a/atk16.egg-info/top_level.txt b/atk16.egg-info/top_level.txt new file mode 100644 index 0000000..b7ba0e4 --- /dev/null +++ b/atk16.egg-info/top_level.txt @@ -0,0 +1,3 @@ +atk16_asm +atk16_emu +test diff --git a/atk16_asm/assembler.py b/atk16_asm/assembler.py index 1451d47..993472e 100755 --- a/atk16_asm/assembler.py +++ b/atk16_asm/assembler.py @@ -9,6 +9,7 @@ from .asm_pass1 import pass_1 from .asm_pass2 import pass_2 from .asm_pass3 import pass_3 from .asm_pass4 import pass_4 +from .pad_bin import pad_binary def assemble(source: str, file_name: str) -> bytearray: src_lines = source.splitlines() @@ -41,7 +42,7 @@ def assemble(source: str, file_name: str) -> bytearray: return result -if __name__ == "__main__": +def main(): if len(sys.argv) != 3: print("usage: assembler.py <infile> <outfile> # read from file") print(" assembler.py - <outfile> # read from stdin") @@ -59,7 +60,13 @@ if __name__ == "__main__": src = f.read() result = assemble(src, infile_path) + with open(outfile_path, "wb") as f: f.write(result) - print(f"Wrote {len(result)} bytes to {outfile_path}") + pad_binary(outfile_path, 64 * 1024) + + print(f"Wrote 64 KB to {outfile_path} ({len(result)} B without padding)") + +if __name__ == "__main__": + main() diff --git a/atk16_asm/pad_bin.py b/atk16_asm/pad_bin.py new file mode 100644 index 0000000..a9fde17 --- /dev/null +++ b/atk16_asm/pad_bin.py @@ -0,0 +1,42 @@ +import os + +def pad_binary(file_path: str, target_size: int) -> int: + """ + Pads a binary file with zeros until it reaches a specified size in KiB. + + :param file_path: Path to the binary file to be padded. + :param target_size: Target size in bytes. + """ + + # Check current file size + current_size = os.path.getsize(file_path) + + # Calculate needed padding + padding_size = target_size - current_size + + if padding_size < 0: + raise ValueError(f"File is larger than the target size ({current_size} > {target_size}).") + + # Append zeros if needed + if padding_size > 0: + with open(file_path, 'ab') as file: + file.write(b'\x00' * padding_size) + + return padding_size + +def convert_size_to_bytes(size_str: str) -> int: + """ + Converts a size string with K, M, or G suffix to bytes. + + :param size_str: Size string (e.g., "64K", "1M", "2G"). + :return: Size in bytes. + """ + size_str = size_str.upper() + if size_str.endswith('K'): + return int(size_str[:-1]) * 1024 + elif size_str.endswith('M'): + return int(size_str[:-1]) * 1024 ** 2 + elif size_str.endswith('G'): + return int(size_str[:-1]) * 1024 ** 3 + else: + return int(size_str) diff --git a/atk16_emu/cli.py b/atk16_emu/cli.py index d6bd46b..824fbbf 100644 --- a/atk16_emu/cli.py +++ b/atk16_emu/cli.py @@ -21,41 +21,45 @@ def print_help(): print(" -h: print help") print(" -d: enable debugger") -for arg in sys.argv[1:]: - if arg == "-d": - options.debugger_enabled = True - elif arg == "-h" or arg == "--help" or arg == "-?": - print_help() - sys.exit(0) - elif arg[0] == "-": - print(f"Unknown option: {arg}") +def main(): + for arg in sys.argv[1:]: + if arg == "-d": + options.debugger_enabled = True + elif arg == "-h" or arg == "--help" or arg == "-?": + print_help() + sys.exit(0) + elif arg[0] == "-": + print(f"Unknown option: {arg}") + print_help() + sys.exit(1) + else: + options.rom_image_path = arg + + if options.rom_image_path is None: print_help() sys.exit(1) - else: - options.rom_image_path = arg -if options.rom_image_path is None: - print_help() - sys.exit(1) + def load_rom_image_from_path(path: str) -> bytearray: + with open(path, "rb") as f: + return bytearray(f.read()) -def load_rom_image_from_path(path: str) -> bytearray: - with open(path, "rb") as f: - return bytearray(f.read()) + rom_image = load_rom_image_from_path(options.rom_image_path) -rom_image = load_rom_image_from_path(options.rom_image_path) + if not options.debugger_enabled: + machine = Machine() + machine.load_rom_image(rom_image) + machine.reset() + machine.run_until_halted() -if not options.debugger_enabled: - machine = Machine() - machine.load_rom_image(rom_image) - machine.reset() - machine.run_until_halted() + print("Machine halted.") + print("===============") - print("Machine halted.") - print("===============") + machine.print_state_summary() - machine.print_state_summary() + else: + debugger = Debugger() + debugger.load_rom_image(rom_image) + debugger.activate() -else: - debugger = Debugger() - debugger.load_rom_image(rom_image) - debugger.activate() +if __name__ == "__main__": + main() diff --git a/atk16_emu/debugger.py b/atk16_emu/debugger.py index f887ed0..3d8f131 100644 --- a/atk16_emu/debugger.py +++ b/atk16_emu/debugger.py @@ -76,11 +76,19 @@ class Debugger: else: self.breakpoints.add(addr) elif cmd == "n": + if not self.machine.running: + print("Machine halted.") + continue + self.machine.step() + + if not self.machine.running: + print("Machine halted.") elif cmd == "s": self.machine.print_state_summary() elif cmd == "0": self.machine.reset() + self.machine.run() print("Machine reset.") else: print(f"Unknown command: {cmd}") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a5fedd0 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup, find_packages + +setup( + name='atk16', + version='0.1', + packages=find_packages(), + entry_points={ + 'console_scripts': [ + 'atk16c=atk16_asm.assembler:main', + 'atk16emu=atk16_emu.cli:main', + ], + }, + install_requires=[ + "getch>=1.0", + ] +) |
