From 986c62cce7f430a645fafce70c0e5d0478e9da6a Mon Sep 17 00:00:00 2001 From: Anurag Indora <98074145+AnuragIndora@users.noreply.github.com> Date: Thu, 22 Jan 2026 18:31:03 +0530 Subject: [PATCH] Port get-filters.py and asm.py to Python 3 - Converted get-filters.py and asm.py to Python 3 syntax (print, map, etc.) - Fixed dictionary ordering in asm.py to preserve .data memory layout and prevent byte flipping between Python 2 and 3 - Verified all 5 test cases pass and produce identical byte output --- Dockerfile | 30 ++++++++++++++++++++++++++++++ README.md | 6 ++++++ asm/asm.py | 17 +++++++++++++---- gtkwave/gen-filters.py | 19 +++++++++++-------- script.sh | 6 ++++++ 5 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 Dockerfile create mode 100644 script.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..914a68d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM ubuntu:20.04 + +ENV DEBIAN_FRONTEND=noninteractive + +# Install required packages +RUN apt-get update && \ + apt-get install -y \ + python3 \ + make \ + iverilog \ + gtkwave \ + build-essential \ + git \ + bash \ + && rm -rf /var/lib/apt/lists/* + +# Ensure `python` points to python3 +RUN ln -sf /usr/bin/python3 /usr/bin/python + +# Working directory +WORKDIR /opt/8bit-computer + +# Copy project files +COPY . . + +# Make assembler executable +RUN chmod +x asm/asm.py + +# Start interactive shell +CMD ["/bin/bash"] diff --git a/README.md b/README.md index 4af1670..4b2f0ce 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This project contains: ## How to use it +This project build in python3.x Build an exemple: @@ -15,6 +16,11 @@ Build an exemple: ./asm/asm.py tests/multiplication_test.asm > memory.list ``` +or +``` +bash script.sh +``` + Run the computer: ``` diff --git a/asm/asm.py b/asm/asm.py index d4b7156..1e984f0 100755 --- a/asm/asm.py +++ b/asm/asm.py @@ -1,10 +1,12 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import re import sys +# Get the input assembly program filename from command line arguments progf = sys.argv[1] +# Dictionary mapping assembly instructions to their opcodes inst = { "nop": 0x00, "call": 0b00000001, @@ -36,6 +38,7 @@ "mov": 0b10000000, } +# Register mapping to 3-bit codes reg = { "A": 0b000, "B": 0b001, @@ -47,7 +50,10 @@ "M": 0b111, } +# Section constants TEXT, DATA = 0, 1 + +# Memory size (256 bytes) MEM_SIZE = 256 mem = [0 for _ in range(MEM_SIZE)] @@ -57,6 +63,7 @@ data = {} data_addr = {} +# Helper function to parse integers in decimal, hex (0x), or binary (0b) def rich_int(v): if v.startswith("0x"): return int(v, 16) @@ -79,7 +86,7 @@ def rich_int(v): section = DATA else: if section == DATA: - n, v = map(str.strip, l.split("=", 2)) + n, v = list(map(str.strip, l.split("=", 2))) data[str(n)] = int(v) elif section == TEXT: kw = l.split() @@ -111,8 +118,10 @@ def rich_int(v): mem[cnt] = a cnt += 1 +data = dict(sorted(data.items())) + # Write data into memory -for k, v in data.items(): +for k, v in list(data.items()): data_addr[k] = cnt mem[cnt] = v cnt += 1 @@ -124,4 +133,4 @@ def rich_int(v): if str(b).startswith("%"): mem[i] = data_addr[b.lstrip("%")] -print ' '.join(['%02x' % int(b) for b in mem]) +print(' '.join(['%02x' % int(b) for b in mem])) diff --git a/gtkwave/gen-filters.py b/gtkwave/gen-filters.py index 23f8565..d1730c3 100755 --- a/gtkwave/gen-filters.py +++ b/gtkwave/gen-filters.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 instructions = { "0000 0000": "NOP", @@ -35,24 +35,27 @@ "110": "G" } +# LDI, PUSH, POP instructions for k, v in regs.items(): instructions["0001 0" + k] = "LDI " + v instructions["0010 0" + k] = "PUSH " + v instructions["0010 1" + k] = "POP " + v +# MOV between registers for k1, v1 in regs.items(): for k2, v2 in regs.items(): if k1 == k2: txt = "invalid" else: - txt = " ".join((v1, v2)) - - instructions[" ".join(("10", k1, k2))] = "MOV " + txt + txt = f"{v1} {v2}" + instructions["10" + k1 + k2] = "MOV " + txt +# MOV to/from memory for k, v in regs.items(): - instructions["10 111 %s" % k] = "MOV M %s" % v - instructions["10 %s 111" % k] = "MOV %s M" % v + instructions[f"10 111 {k}"] = f"MOV M {v}" + instructions[f"10 {k} 111"] = f"MOV {v} M" -for k, v in instructions.items(): - print k.replace(" ", ""), v +# Print sorted instructions +for k, v in sorted(instructions.items()): + print(k.replace(" ", ""), v) diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..21dbdaf --- /dev/null +++ b/script.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for file in tests/*.asm; do + echo "Running $file" + python3 asm/asm.py "$file" +done