aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_asm
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-02-24 20:30:17 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2024-02-24 20:30:17 +0200
commit6dd1e346d330e662e42dd8bc75d17317223908fd (patch)
treec081b1de90cfc5ca7007a8935e6892aaee03aa56 /atk16_asm
parent0a2cf66672364ed555c85682e948cff0b54c8f35 (diff)
Add setup.py, fix bugs
Diffstat (limited to 'atk16_asm')
-rwxr-xr-xatk16_asm/assembler.py11
-rw-r--r--atk16_asm/pad_bin.py42
2 files changed, 51 insertions, 2 deletions
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)