summaryrefslogtreecommitdiffstats
path: root/lab1ucode.py
blob: 6c7413013eed14a536f7bc3cfb401cc5f9c4a625 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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))