aboutsummaryrefslogtreecommitdiffstats
path: root/resources/asm/ext_std.py
blob: 247e605ee832994e2ad10024cda823950fc23687 (plain)
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
from asm_ops import *

def expand_add(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_plus", left, right, target]]

def expand_sub(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_minus", left, right, target]]

def expand_addi(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_plus", left, imm, target]]

def expand_subi(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_minus", left, imm, target]]

def expand_not(reg: str, target: str) -> ExpandResult:
  return [["alr", "al_xor", reg, "0xFFFF", target]]

def expand_noti(imm: str, target: str) -> ExpandResult:
  return [["ali", "al_xor", imm, "0xFFFF", target]]

def expand_and(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_and", left, right, target]]

def expand_andi(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_and", left, imm, target]]

def expand_or(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_or", left, right, target]]

def expand_ori(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_or", left, imm, target]]

def expand_xor(left: str, imm: str, target: str) -> ExpandResult:
  return [["alr", "al_xor", left, imm, target]]

def expand_xori(left: str, right: str, target: str) -> ExpandResult:
  return [["ali", "al_xor", left, right, target]]

def expand_sll(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_sll", left, right, target]]

def expand_slr(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_slr", left, right, target]]

def expand_sar(left: str, right: str, target: str) -> ExpandResult:
  return [["alr", "al_sar", left, right, target]]

def expand_slli(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_sll", left, imm, target]]

def expand_slri(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_slr", left, imm, target]]

def expand_sari(left: str, imm: str, target: str) -> ExpandResult:
  return [["ali", "al_sar", left, imm, target]]

def expand_inc(reg: str) -> ExpandResult:
  return [["ali", "al_plus", reg, "1", reg]]

def expand_dec(reg: str) -> ExpandResult:
  return [["ali", "al_minus", reg, "1", reg]]

def expand_mov(from_reg: str, to_reg: str) -> ExpandResult:
  return [["ali", "al_plus", from_reg, "0", to_reg]]

def expand_nop() -> ExpandResult:
  return [["ali", "al_plus", "RA", "0", "RA"]]

def expand_spu(reg: str) -> ExpandResult:
  return [["str", reg, "SP"]] + expand_inc("SP")

def expand_spo(reg: str) -> ExpandResult:
  return expand_dec("SP") + [["ldr", "SP", reg]]

def expand_sinc(imm: str) -> ExpandResult:
  return expand_addi("SP", imm, "SP")

def expand_sdec(imm: str) -> ExpandResult:
  return expand_subi("SP", imm, "SP")

def expand_csr(addr_reg: str, stratch_reg: str):
  return [
    ["lpc", stratch_reg],
    *expand_addi(stratch_reg, "4", stratch_reg),
    *expand_spu(stratch_reg),
    ["jpr", addr_reg]
  ]

def expand_csi(addr_imm: str, stratch_reg: str):
  return [
    ["lpc", stratch_reg],
    *expand_addi(stratch_reg, "4", stratch_reg),
    *expand_spu(stratch_reg),
    ["jpi", addr_imm]
  ]

def expand_rsr(stratch_reg: str):
  return expand_spo(stratch_reg) + [["jpr", stratch_reg]]


def stack_stash(*rs: str):
  result: ExpandResult = []
  for r in rs:
    result += expand_spu(r)

  return result

def stack_restore(*rs: str):
  result: ExpandResult = []
  for r in rs:
    prefix = expand_spu(r)
    result = prefix + result

  return result

def set_graphics_mode(mode: str):
  return [
    ["ldi", "vt_gr_mode_addr", "RA"],
    ["ldr", "RA", "RA"],
    ["ldi", mode, "RB"],
    ["str", "RB", "RA"],
  ]

expansions: OpExpansionDict = {
  "add": expand_add,
  "sub": expand_sub,
  "addi": expand_addi,
  "subi": expand_subi,
  "not": expand_not,
  "noti": expand_noti,
  "and": expand_and,
  "andi": expand_andi,
  "or": expand_or,
  "ori": expand_ori,
  "xor": expand_xor,
  "xori": expand_xori,
  "sll": expand_sll,
  "slr": expand_slr,
  "sar": expand_sar,
  "slli": expand_slli,
  "slri": expand_slri,
  "sari": expand_sari,
  "inc": expand_inc,
  "dec": expand_dec,
  "mov": expand_mov,
  "nop": expand_nop,
  "spu": expand_spu,
  "spo": expand_spo,
  "sinc": expand_sinc,
  "sdec": expand_sdec,
  "csr": expand_csr,
  "csi": expand_csi,
  "rsr": expand_rsr,
  "stack_stash": stack_stash,
  "stack_restore": stack_restore,
  "set_graphics_mode": set_graphics_mode,
}