aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2023-04-08 17:20:32 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2023-04-08 17:20:32 +0300
commit5ab9865fa6ed343ae0cffefb81b20af2e533e36f (patch)
tree57949a3b8335cdf58234e366e50701a19bd6c21d /src
parent10e0c53890701abe53c80f1da4a0b64bbd05914c (diff)
Move from logisim to digital
Diffstat (limited to 'src')
-rwxr-xr-xsrc/assembler.py57
-rw-r--r--src/test.atk162
-rw-r--r--src/test_sum.atk163
-rwxr-xr-xsrc/ucode.py38
4 files changed, 61 insertions, 39 deletions
diff --git a/src/assembler.py b/src/assembler.py
index 9e2d92f..5802983 100755
--- a/src/assembler.py
+++ b/src/assembler.py
@@ -45,7 +45,7 @@ def parse(line: str) -> list[str]:
def eval_expr(expr: str) -> int:
expr = eval_symbol(expr)
- return eval(expr, labels) # eval as Python expr
+ return eval(expr, labels.copy()) # eval as Python expr
def eval_symbol(c: str):
if c in labels:
@@ -85,7 +85,7 @@ labels: dict[str, int] = {}
address = 0
for (lineNo, line) in enumerate(src_lines):
- line = line.strip()
+ line = line.split(";")[0].strip()
if line == "": continue
keyword, *args = line.lower().split()
match keyword:
@@ -98,8 +98,6 @@ for (lineNo, line) in enumerate(src_lines):
address += 1
-print("labels:", labels)
-
# 2nd pass
address = 0
@@ -122,33 +120,50 @@ for (lineNo, line) in enumerate(src_lines):
# Instructions
case "alu":
+ # S (alu op)
+ # L (lhs)
+ # R (rhs)
+ # T (target)
word = (0b0000 << 12) + \
- (eval_expr(args[0]) << 9) + \
- (eval_expr(args[1]) << 6) + \
- (eval_expr(args[2]) << 3) + \
+ (eval_expr(args[0]) << 3) + \
+ (eval_expr(args[1]) << 9) + \
+ (eval_expr(args[2]) << 6) + \
(eval_expr(args[3]) << 0)
case "als":
+ # S (alu op)
+ # L (lhs)
+ # R (rhs)
+ # T (target)
word = (0b0001 << 12) + \
- (eval_expr(args[0]) << 9) + \
- (eval_expr(args[1]) << 6) + \
- (eval_expr(args[2]) << 3) + \
+ (eval_expr(args[0]) << 3) + \
+ (eval_expr(args[1]) << 9) + \
+ (eval_expr(args[2]) << 6) + \
(eval_expr(args[3]) << 0)
case "ldr":
+ # R (address)
+ # T (target)
word = (0b0010 << 12) + \
(eval_expr(args[0]) << 9) + \
- (eval_expr(args[1]) << 6)
+ (eval_expr(args[1]) << 0)
case "str":
+ # R (address)
+ # T (value to store)
word = (0b0011 << 12) + \
(eval_expr(args[0]) << 9) + \
(eval_expr(args[1]) << 6)
case "ldi":
+ # T (target)
+ # I (immediate)
word = (0b0100 << 12) + \
- (eval_expr(args[0]) << 9) + \
- (eval_expr(args[1]) << 0)
+ (eval_expr(args[0]) << 0) + \
+ (eval_expr(args[1]) << 3)
case "jmp":
+ # R (reg holding address)
word = (0b0101 << 12) + \
(eval_expr(args[0]) << 9)
case "br":
+ # F (flag selector)
+ # R (reg holding address)
word = (0b0110 << 12) + \
(eval_expr(args[0]) << 9) + \
(eval_expr(args[1]) << 6)
@@ -160,20 +175,20 @@ for (lineNo, line) in enumerate(src_lines):
word = (0b0000 << 12) + \
(eval_expr(args[0]) << 9) + \
(eval_expr(args[1]) << 6) + \
- (eval_expr(args[2]) << 3) + \
- (eval_expr("al_a_plus_b") << 0)
+ (eval_expr("al_a_plus_b") << 3) + \
+ (eval_expr(args[2]) << 0)
case "sub":
word = (0b0000 << 12) + \
(eval_expr(args[0]) << 9) + \
(eval_expr(args[1]) << 6) + \
- (eval_expr(args[2]) << 3) + \
- (eval_expr("al_a_minus_b") << 0)
+ (eval_expr("al_a_minus_b") << 3) + \
+ (eval_expr(args[2]) << 0)
case "mov":
word = (0b0000 << 12) + \
(eval_expr("rz") << 9) + \
(eval_expr(args[0]) << 6) + \
- (eval_expr(args[1]) << 3) + \
- (eval_expr("al_a_plus_b") << 0)
+ (eval_expr("al_a_plus_b") << 3) + \
+ (eval_expr(args[1]) << 0)
# Default case: evaluate as is (e.g. data word)
case _:
@@ -185,6 +200,10 @@ for (lineNo, line) in enumerate(src_lines):
if len(result) < 2 * address + 1:
result.extend((2 * address + 1 - len(result)) * nop)
+ for (label, label_addr) in labels.items():
+ if address == label_addr:
+ print(f"{label}:")
+
print(f"{address:>08x} 0x{word:>04x} {line}")
result[2 * address + 0] = ((word >> 8) & 0xff)
result[2 * address + 1] = ((word >> 0) & 0xff)
diff --git a/src/test.atk16 b/src/test.atk16
index 399bd7e..364480b 100644
--- a/src/test.atk16
+++ b/src/test.atk16
@@ -10,7 +10,7 @@
@label program
ldi RA 10 ; RA := 10
- ldi RB 20 ; RB := 10
+ ldi RB 20 ; RB := 20
add RA RB RC ; RC := RA + RB
ldi RD ram_offset ; store address of ram_offset in RD
ldr RD RD ; dereference ram_offset address
diff --git a/src/test_sum.atk16 b/src/test_sum.atk16
new file mode 100644
index 0000000..3f69a55
--- /dev/null
+++ b/src/test_sum.atk16
@@ -0,0 +1,3 @@
+ ldi RA 10 ; RA := 10
+ ldi RB 20 ; RB := 20
+ add RA RB RC ; RC := RA + RB
diff --git a/src/ucode.py b/src/ucode.py
index ea5c040..9d7c55c 100755
--- a/src/ucode.py
+++ b/src/ucode.py
@@ -16,16 +16,16 @@ PC_CO = 0b0000_0000_0000_0001
PC_IE = 0b0000_0000_0000_0010
PC_OE = 0b0000_0000_0000_0100
MAR_IE = 0b0000_0000_0000_1000
-RAM_IE = 0b0000_0000_0001_0000
-RAM_OE = 0b0000_0000_0010_0000
-R1_IE = 0b0000_0000_0100_0000
+MEM_IE = 0b0000_0000_0001_0000
+MEM_OE = 0b0000_0000_0010_0000
+RW_IE = 0b0000_0000_0100_0000
R1_OE = 0b0000_0000_1000_0000
-R2_IE = 0b0000_0001_0000_0000
-R2_OE = 0b0000_0010_0000_0000
-IR_IE = 0b0000_0100_0000_0000
-L9_OE = 0b0000_1000_0000_0000
-L12_OE = 0b0001_0000_0000_0000
-ALU_OE = 0b0010_0000_0000_0000
+R2_OE = 0b0000_0001_0000_0000
+IR_IE = 0b0000_0010_0000_0000
+L9_OE = 0b0000_0100_0000_0000
+unused1 = 0b0000_1000_0000_0000
+ALU_OE = 0b0001_0000_0000_0000
+FR_IE = 0b0010_0000_0000_0000
HALT = 0b0100_0000_0000_0000
US_RS = 0b1000_0000_0000_0000
@@ -39,22 +39,22 @@ def not_branch(bs):
def branch(false_branch, true_branch):
return [false_branch, true_branch]
-fetch = [PC_OE|MAR_IE, RAM_OE|IR_IE|PC_CO]
+fetch = [PC_OE|MAR_IE, MEM_OE|IR_IE|PC_CO]
def nop():
return not_branch([*fetch, US_RS, 0, 0, 0, 0, 0])
ucode = [
- # ALU 0000 LLLR RRTT TSSS
- not_branch([*fetch, ALU_OE, US_RS, 0, 0, 0, 0]),
- # ALS 0001 LLLR RRTT TSSS
- not_branch([*fetch, ALU_OE, US_RS, 0, 0, 0, 0]),
- # LDR 0010 RRRT TTXX XXXX
- not_branch([*fetch, R1_OE|MAR_IE, RAM_OE|R2_IE, US_RS, 0, 0, 0]),
+ # ALU 0000 LLLR RRSS STTT
+ not_branch([*fetch, ALU_OE|FR_IE|RW_IE, US_RS, 0, 0, 0, 0]),
+ # ALS 0001 LLLR RRSS STTT
+ not_branch([*fetch, ALU_OE|FR_IE|RW_IE, US_RS, 0, 0, 0, 0]),
+ # LDR 0010 RRRX XXXX XTTT
+ not_branch([*fetch, R1_OE|MAR_IE, MEM_OE|RW_IE, US_RS, 0, 0, 0]),
# STR 0011 RRRT TTXX XXXX
- not_branch([*fetch, R1_OE|MAR_IE, R2_OE|RAM_IE, US_RS, 0, 0, 0]),
- # LDI 0100 RRRI IIII IIII
- not_branch([*fetch, L9_OE|R1_IE, US_RS, 0, 0, 0, 0]),
+ not_branch([*fetch, R1_OE|MAR_IE, R2_OE|MEM_IE, US_RS, 0, 0, 0]),
+ # LDI 0100 IIII IIII ITTT
+ not_branch([*fetch, H9_OE|RW_IE, US_RS, 0, 0, 0, 0]),
# JMP 0101 RRRX XXXX XXXX
not_branch([*fetch, R1_OE|PC_IE, US_RS, 0, 0, 0, 0]),
# BR 0110 XFFR RRXX XXXX