diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2022-01-26 21:36:44 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2022-01-26 21:36:44 +0100 |
| commit | 2ea7eec53978ff743e73b83c4adab942914b0438 (patch) | |
| tree | 11eee27d811fe5df177e30ba696014aa4f4e0a0d | |
| download | tsea83-2ea7eec53978ff743e73b83c4adab942914b0438.tar.gz | |
initial commit
| -rw-r--r-- | lab1asm.py | 79 | ||||
| -rw-r--r-- | lab1ucode.in | 71 | ||||
| -rw-r--r-- | lab1ucode.out | 71 | ||||
| -rw-r--r-- | lab1ucode.py | 159 | ||||
| -rw-r--r-- | lab1upg2.in | 30 | ||||
| -rw-r--r-- | lab1upg2.out | 29 | ||||
| -rw-r--r-- | lab1upg3.in | 62 | ||||
| -rw-r--r-- | lab1upg3.out | 51 |
8 files changed, 552 insertions, 0 deletions
diff --git a/lab1asm.py b/lab1asm.py new file mode 100644 index 0000000..6f09bff --- /dev/null +++ b/lab1asm.py @@ -0,0 +1,79 @@ +import sys + +OPS = { + "load": 0, + "store": 1, + "add": 2, + "sub": 3, + "and": 4, + "lsr": 5, + "bra": 6, + "bne": 7, + "halt": 8, + "cmp": 9, + "bge": 10, + "beq": 11, +} + +def compile(lines): + addr = 0 + labels = {} + compiled = [] + for line in lines: + if not line: + # empty line, ignore + continue + if line.startswith(";"): + # comment + continue + if line.endswith(":"): + # label + labels[line.split(":")[0]] = addr + continue + match line.split(" "): + case [inst, gr, m, *adr]: + if int(m) == 1: + assert adr == [] + compiled.append("{:02x}: {:01x}{:01x}{:02x}".format( + addr, + OPS[inst], + int(gr) * 4 + int(m), + int(adr[0], 16) if int(m) != 1 else 0, + )) + case [inst, label]: + assert inst in ("bra", "bne", "bge", "beq") + compiled.append("{:02x}: {:01x}000".format( + addr, + OPS[inst], + )) + addr += 1 + compiled.append("{:02x}: <{}>".format( + addr, + label + )) + case ["halt"]: + compiled.append("{:02x}: {:01x}000".format(addr, OPS["halt"])) + case [oper]: + compiled.append("{:02x}: {:04x}".format(addr, int(oper, 16))) + case _: + compiled.append(" !!!", line) + continue + addr += 1 + + linked = [] + for line_nr, line in enumerate(compiled): + for label, label_nr in labels.items(): + line = line.replace(f"<{label}>", f"{label_nr - line_nr:04x}") + linked.append(line) + + return linked, labels + + +if __name__ == "__main__": + prog, labels = compile([line.strip() for line in sys.stdin]) + print("prog:") + print("\n".join(prog)) + print() + print("labels:") + for label, label_nr in labels.items(): + print(label, hex(label_nr)) diff --git a/lab1ucode.in b/lab1ucode.in new file mode 100644 index 0000000..6dc2b3f --- /dev/null +++ b/lab1ucode.in @@ -0,0 +1,71 @@ +pc->asr +pm->ir, pc++ +k2->upc + +direct: +ir->asr, k1->upc + +immediate: +pc->asr, pc++, k1->upc + +indirect: +ir->asr +pm->asr, k1->upc + +indexed: +ir->ar +; ar+gr3->ar +!1184000 +ar->asr, k1->upc + +load: +pm->grx, 0->upc + +store: +grx->pm, 0->upc + +add: +pm->ar +ar+grx->ar +ar->grx, 0->upc + +sub: +pm->ar +ar-grx->ar +ar->grx, 0->upc + +and: +pm->ar +ar&grx->ar +ar->grx, 0->upc + +halt: +halt + +cmp: +pm->ar +ar-grx->ar, 0->upc + +lsr: +grx->ar +pm->lc +lsr_loop: +l=1? lsr_exit +lc--, lsr, b lsr_loop +lsr_exit: +ar->grx, 0->upc + +bra: +0->upc + +bne: +0->upc + +bge: +0->upc + +beq: +0->upc + +end: +b end diff --git a/lab1ucode.out b/lab1ucode.out new file mode 100644 index 0000000..20e9804 --- /dev/null +++ b/lab1ucode.out @@ -0,0 +1,71 @@ +prog: +00: 00f8000 +01: 0088000 +02: 0002000 +03: 0000100 +04: 0078000 +05: 0000080 +06: 00f8000 +07: 0002000 +08: 0000080 +09: 0078000 +0a: 00b8000 +0b: 0000080 +0c: 0240000 +0d: 1184000 +0e: 0138000 +0f: 0000080 +10: 00b0000 +11: 0000180 +12: 0190000 +13: 0000180 +14: 0280000 +15: 0900000 +16: 0130000 +17: 0000180 +18: 0280000 +19: 0b00000 +1a: 0130000 +1b: 0000180 +1c: 0280000 +1d: 0200000 +1e: 0130000 +1f: 0000180 +20: 0000780 +21: 0280000 +22: 0b00000 +23: 0000180 +24: 0380000 +25: 0081000 +26: 000062a +27: 0000800 +28: 1a00000 +29: 0000326 +2a: 0130000 +2b: 0000180 +2c: 0000180 +2d: 0000180 +2e: 0000180 +2f: 0000180 +30: 0000330 + +labels: +direct 0x4 +immediate 0x6 +indirect 0x9 +indexed 0xc +load 0x10 +store 0x12 +add 0x14 +sub 0x18 +and 0x1c +halt 0x20 +cmp 0x21 +lsr 0x24 +lsr_loop 0x26 +lsr_exit 0x2a +bra 0x2c +bne 0x2d +bge 0x2e +beq 0x2f +end 0x30 diff --git a/lab1ucode.py b/lab1ucode.py new file mode 100644 index 0000000..6c74130 --- /dev/null +++ b/lab1ucode.py @@ -0,0 +1,159 @@ +import sys + +TWOWAY = { + "ir": "001", + "pm": "010", + "pc": "011", + "hr": "101", + "grx": "110", +} + +# to bus: source +TB = TWOWAY | { + "ar": "100", + # "styrord": "111", +} + +# from bus: destination +FB = TWOWAY | { + "asr": "111" +} + +def compile(lines): + addr = 0 + labels = {} + compiled = [] + todo = [] + for line in lines: + if not line: + # empty line, ignore + continue + if line.startswith(";"): + # comment + continue + if line.endswith(":"): + # label + labels[line.split(":")[0]] = addr + continue + if line.startswith("!"): + # inline + compiled.append(line[1:]) + addr += 1 + continue + + for inst in line.split(", "): + alu = "0000" + tb = "000" + fb = "000" + s = "0" + p = "0" + lc = "00" + seq = "0000" + myadr = "0000000" + + if "->" in inst: + # move a value + fr, to = inst.split("->") + # via bus + if fr in TB: + tb = TB[fr] + if to in FB: + fb = FB[to] + + if "gr" in fr and len(fr) == 3: + tb = TB["grx"] + if "gr" in to and len(to) == 3: + fb = FB["grx"] + + if to == "lc": + lc = "10" + + # load alu + if to == "ar": + alu = "0001" + + # alu addition + if "+" in fr: + if "+'" in fr: + # ignore flags + other = fr.split("+'")[0] + alu = "1000" + else: + other = fr.split("+")[0] + alu = "0100" + tb = TB[other] + + # alu subtraction + if "-" in fr: + other = fr.split("-")[0] + tb = TB[other] + alu = "0101" + + if inst == "pc++": + p = "1" + + if inst == "k1->upc": + seq = "0001" + if inst == "k2->upc": + seq = "0010" + if inst == "0->upc": + seq = "0011" + if inst == "halt": + seq = "1111" + if inst == "lc--": + lc = "01" + if inst == "lsr": + alu = "1101" + + if "?" in inst: + cond, to = inst.split("? ") + if cond == "l=1": + seq = "1100" + else: + assert False + myadr = f"<{to}>" + + if inst.startswith("b "): + seq = "0110" #TODO 0101? + myadr = "<{}>".format(inst.split("b ")[1]) + + compiled.append((inst, (alu, tb, fb, s, p, lc, seq, myadr))) + + addr += 1 + + linked = [] + for addr, line in enumerate(compiled): + if type(line) is str: + linked.append((addr, line)) + continue + inst, (alu, tb, fb, s, p, lc, seq, myadr) = line + if myadr != "0000000": + for label, label_line in labels.items(): + myadr = myadr.replace(f"<{label}>", str(bin(label_line)[2:]).zfill(7)) + first = alu[0] + rest = alu[1:] + tb + fb + s + p + lc + seq + myadr + hs = "".join([f"{int(first):01x}"] + [f"{int(rest[i:i+4], 2):01x}" for i in range(0, len(rest), 4)]) + linked.append((addr, hs)) + if hs == "0000000": + todo.append(inst) + + return ["{:02x}: {}".format(addr, line) for addr, line in linked], labels + """ + todo + l=1? lsr_exit + b lsr_loop + end->upc + """ + + print("todo") + print("\n".join(todo)) + + +if __name__ == "__main__": + prog, labels = compile([line.strip() for line in sys.stdin]) + print("prog:") + print("\n".join(prog)) + print() + print("labels:") + for label, label_nr in labels.items(): + print(label, hex(label_nr)) diff --git a/lab1upg2.in b/lab1upg2.in new file mode 100644 index 0000000..10d9586 --- /dev/null +++ b/lab1upg2.in @@ -0,0 +1,30 @@ +load 0 0 fe +and 0 1 +000f +store 0 0 ff + +load 0 0 fe +lsr 0 1 +0004 +and 0 1 +000f +add 0 0 ff +store 0 0 ff + +load 0 0 fe +lsr 0 1 +0008 +and 0 1 +000f +add 0 0 ff +store 0 0 ff + +load 0 0 fe +lsr 0 1 +000c +and 0 1 +000f +add 0 0 ff +store 0 0 ff + +halt diff --git a/lab1upg2.out b/lab1upg2.out new file mode 100644 index 0000000..a155f52 --- /dev/null +++ b/lab1upg2.out @@ -0,0 +1,29 @@ +prog: +00: 00fe +01: 4100 +02: 000f +03: 10ff +04: 00fe +05: 5100 +06: 0004 +07: 4100 +08: 000f +09: 20ff +0a: 10ff +0b: 00fe +0c: 5100 +0d: 0008 +0e: 4100 +0f: 000f +10: 20ff +11: 10ff +12: 00fe +13: 5100 +14: 000c +15: 4100 +16: 000f +17: 20ff +18: 10ff +19: 8000 + +labels: diff --git a/lab1upg3.in b/lab1upg3.in new file mode 100644 index 0000000..1f0a437 --- /dev/null +++ b/lab1upg3.in @@ -0,0 +1,62 @@ +label1: +; lista_sorterad = 1 +load 0 1 +0001 +store 0 0 d0 + +; addr = e0 +load 0 1 +00e0 +store 0 0 d1 + +label2: +; tmp1 = pm(addr) +load 0 2 d1 +store 0 0 d3 + +; tmp2 = pm(addr + 1) +load 0 0 d1 +add 0 1 +0001 +store 0 0 d2 +load 0 2 d2 +store 0 0 d4 + +; tmp1 > tmp2? +load 0 0 d3 +cmp 0 0 d4 +beq no_swap +bge swap +bra no_swap + +swap: +load 0 2 d1 +load 1 2 d2 +store 0 2 d2 +store 1 2 d1 + +; lista_sorterad = 0 +load 0 1 +0000 +store 0 0 d0 + +no_swap: +; räkna upp addr +load 0 0 d1 +add 0 1 +0001 +store 0 0 d1 + +; addr = 0xff? +load 0 0 d1 +cmp 0 1 +00ff +bne label2 + +; lista_sorterad = 1? +load 0 0 d0 +cmp 0 1 +0001 +bne label1 + +halt diff --git a/lab1upg3.out b/lab1upg3.out new file mode 100644 index 0000000..5dfe4fb --- /dev/null +++ b/lab1upg3.out @@ -0,0 +1,51 @@ +prog: +00: 0100 +01: 0001 +02: 10d0 +03: 0100 +04: 00e0 +05: 10d1 +06: 02d1 +07: 10d3 +08: 00d1 +09: 2100 +0a: 0001 +0b: 10d2 +0c: 02d2 +0d: 10d4 +0e: 00d3 +0f: 90d4 +10: b000 +11: 000c +12: a000 +13: 0003 +14: 6000 +15: 0008 +16: 02d1 +17: 06d2 +18: 12d2 +19: 16d1 +1a: 0100 +1b: 0000 +1c: 10d0 +1d: 00d1 +1e: 2100 +1f: 0001 +20: 10d1 +21: 00d1 +22: 9100 +23: 00ff +24: 7000 +25: -01f +26: 00d0 +27: 9100 +28: 0001 +29: 7000 +2a: -02a +2b: 8000 + +labels: +label1 0x0 +label2 0x6 +swap 0x16 +no_swap 0x1d |
