Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ This project contains:


## How to use it
This project build in python3.x

Build an exemple:

```
./asm/asm.py tests/multiplication_test.asm > memory.list
```

or
```
bash script.sh
```

Run the computer:

```
Expand Down
17 changes: 13 additions & 4 deletions asm/asm.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -36,6 +38,7 @@
"mov": 0b10000000,
}

# Register mapping to 3-bit codes
reg = {
"A": 0b000,
"B": 0b001,
Expand All @@ -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)]
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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]))
19 changes: 11 additions & 8 deletions gtkwave/gen-filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3

instructions = {
"0000 0000": "NOP",
Expand Down Expand Up @@ -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)
6 changes: 6 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

for file in tests/*.asm; do
echo "Running $file"
python3 asm/asm.py "$file"
done