-
Notifications
You must be signed in to change notification settings - Fork 1
/
lite.py
216 lines (183 loc) · 5 KB
/
lite.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
from nitrox import instruction
@instruction
class nop: # 1cy
operands = ''
encoding = '0000 0000 0000 0000'
def emulate():
pass
@instruction
class emit_lo:
operands = 'r{reg}'
encoding = '0000 0000 0010 0rrr'
@instruction
class emit_hi:
operands = 'r{reg}'
encoding = '0000 0000 0100 0rrr'
@instruction
class wait_other: # 5cy
operands = ''
encoding = '1000 0010 0000 0000'
@instruction
class jz: # taken: 5cy, not taken: 1cy
operands = '{addr}'
encoding = 'aa11 0aaa aaaa aaaa'
def emulate(state, addr):
if state.result == 0:
state.pc = (state.segment << 13) | addr
@instruction
class jnz: # same latencies as jz
operands = '{addr}'
encoding = 'aa01 0aaa aaaa aaaa'
def emulate(state, addr):
if state.result != 0:
state.pc = (state.segment << 13) | addr
@instruction
class js: # same latencies as jz
operands = '{addr}'
encoding = 'aa01 1aaa aaaa aaaa'
def emulate(state, addr):
if state.result & 0x8000:
state.pc = (state.segment << 13) | addr
@instruction
class call: # call + ret: 6-8cy
operands = '{addr_}'
encoding = 'aa11 1aaa aaaa aaaa'
def emulate(state, addr):
assert len(state.call_stack) < 32
state.call_stack.append(state.pc)
state.pc = (state.segment << 13) | addr
@instruction
class ret:
operands = '{imm1}'
encoding = 'i000 0001 0000 0000'
@instruction
class emit:
operands = '0x{imm8:02x}'
encoding = '^000 0100 iiii iiii'
@instruction
class li: # load immediate (1cy)
operands = 'r{dst}, 0x{imm8:02x} ; {imm8}'
encoding = '0.00 1ddd iiii iiii'
def emulate(state, dst, imm8):
state.main_reg[dst] = imm8
@instruction
class and_: # bitwise and (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '^.10 0ddd 00rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] & state.main_regs[rhs]
@instruction
class or_: # bitwise or (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '^.10 0ddd 01rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] | state.main_regs[rhs]
@instruction
class add: # addition (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '^.10 0ddd 10rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] + state.main_regs[rhs]
@instruction
class sub: # subtraction (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '^.10 0ddd 11rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] - state.main_regs[rhs]
@instruction
class shl3: # shift left by 3 (1cy)
operands = 'r{dst}, r{lhs}'
encoding = '^.10 1ddd 0001 0lll'
def emulate(state, dst, lhs):
state.main_reg[dst] = (state.main_reg[lhs] << 3) & 0xFFFF
@instruction
class shr3: # logical shift right by 3 (1cy)
operands = 'r{dst}, r{lhs}'
encoding = '^.10 1ddd 0010 0lll'
def emulate(state, dst, lhs):
state.main_reg[dst] = state.main_reg[lhs] >> 3
@instruction
class shr1: # logical shift right by 1 (1cy)
operands = 'r{dst}, r{lhs}'
encoding = '^.10 1ddd 0011 0lll'
def emulate(state, dst, lhs):
state.main_reg[dst] = state.main_reg[lhs] >> 1
@instruction
class not_:
operands = 'r{dst}, r{src}'
encoding = '^.10 1ddd 0100 0sss'
def emulate(state, dst, src):
state.main_reg[dst] = ~state.main_reg[src] & 0xFFFF
@instruction
class getdcr:
operands = 'r{dst}'
encoding = '^.10 1ddd 0101 0000'
@instruction
class getcore:
operands = 'r{dst}'
encoding = '^.10 1ddd 0110 0000'
@instruction
class la:
operands = 'a{dst}, r{src}'
encoding = '^x10 1ddd 1000 0sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class lb:
operands = 'b{dst}, r{src}'
encoding = '^x10 1ddd 1000 1sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class la_hi:
operands = 'a{dst}, r{src}'
encoding = '^x10 1ddd 1001 0sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class lb_hi:
operands = 'b{dst}, r{src}'
encoding = '^x10 1ddd 1001 1sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class la_lo:
operands = 'a{dst}, r{src}'
encoding = '^x10 1ddd 1010 0sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF00
state.addr_reg[dst] |= state.main_reg[src] & 0xFF
@instruction
class lb_lo:
operands = 'b{dst}, r{src}'
encoding = '^x10 1ddd 1010 1sss'
def emulate(state, dst, src):
state.tmp_reg[dst+8] &= 0xFF00
state.tmp_reg[dst+8] |= state.main_reg[src] & 0xFF
@instruction
class sa:
operands = 'r{dst}, a{src}'
encoding = '^.10 1ddd 1100 0sss'
@instruction
class sb:
operands = 'r{dst}, b{src}'
encoding = '^.10 1ddd 1100 1sss'
@instruction
class sa_hi:
operands = 'r{dst}, a{src}'
encoding = '^.10 1ddd 1101 0sss'
@instruction
class sb_hi:
operands = 'r{dst}, b{src}'
encoding = '^.10 1ddd 1101 1sss'
@instruction
class sa_lo:
operands = 'r{dst}, a{src}'
encoding = '^.10 1ddd 1110 0sss'
@instruction
class sb_lo:
operands = 'r{dst}, b{src}'
encoding = '^.10 1ddd 1110 1sss'