-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinystack_emu.py
executable file
·184 lines (154 loc) · 5.02 KB
/
tinystack_emu.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
#!/usr/bin/python2
# Emulator for Tinystack
import sys
from argparse import ArgumentParser, FileType
from itertools import chain
by_opcode = {}
by_name = {}
def instruction(opcode):
def instruction_(f):
def call_instr(cpu):
return f(cpu)
call_instr.opcode = opcode
call_instr.__name__ = f.__name__.split('_')[0]
by_opcode[opcode] = call_instr
by_name[call_instr.__name__] = call_instr
return call_instr
return instruction_
class Tinystack(object):
@instruction(0x0)
def nop_instr(cpu):
"do nothing"
@instruction(0x1)
def nand_instr(cpu):
"bitwise NAND x and y, store result in x"
cpu.stack.append((~(cpu.stack.pop() & cpu.stack.pop())) & 0xffff)
@instruction(0x2)
def neg_instr(cpu):
"x = -x"
cpu.stack.append((~cpu.stack.pop() + 1) & 0xffff)
@instruction(0x3)
def add_instr(cpu):
"add x and y, unsigned"
cpu.stack.append((cpu.stack.pop() + cpu.stack.pop()) & 0xffff)
@instruction(0x4)
def rol_instr(cpu):
"x = y << x"
x = cpu.stack.pop() & 0xf
y = cpu.stack.pop()
cpu.stack.append(((y << x) | (y >> (16 - x))) & 0xffff)
@instruction(0x5)
def sign_instr(cpu):
"fill x with x's high bit. That is, 0xa99f -> 0xffff, 0x4485 -> 0x0000"
cpu.stack.append(0xFFFF if cpu.stack.pop() & 0x8000 else 0)
@instruction(0x6)
def swap_instr(cpu):
"swap x and y"
x, y = cpu.stack.pop(), cpu.stack.pop()
cpu.stack.append(x), cpu.stack.append(y)
@instruction(0x7)
def save_instr(cpu):
"pop a value from the stack and push it onto the stash"
cpu.stash.append(cpu.stack.pop())
@instruction(0x8)
def rstor_instr(cpu):
"pop a value from the stash and push it onto the stack"
cpu.stack.append(cpu.stash.pop())
@instruction(0x9)
def dup_instr(cpu):
"duplicate the top of the stack"
cpu.stack.append(cpu.stack[-1])
@instruction(0xa)
def disc_instr(cpu):
"pop x and drop it on the floor"
cpu.stack.pop()
@instruction(0xb)
def lit_instr(cpu):
"push a literal nibble"
if cpu.last_lit != cpu.cycle_count - 1 or cpu.lit_shift == 16:
cpu.lit_shift = 0
if not cpu.lit_shift:
cpu.stack.append(0)
cpu.lit_next = True
cpu.last_lit = cpu.cycle_count
@instruction(0xc)
def skip_instr(cpu):
"IP += x; push old IP+1 onto the stack"
offset = cpu.stack.pop()
cpu.stack.append(cpu.ip + 1)
if not offset: return
cpu.new_ip = (cpu.ip + 1 + offset) & 0xFFFF
@instruction(0xd)
def call_instr(cpu):
"IP = x"
addr = cpu.stack.pop()
cpu.stack.append(cpu.ip + 1)
cpu.new_ip = addr
@instruction(0xe)
def ld_instr(cpu):
"x = *x"
x = cpu.stack.pop()
value = cpu.mem[x]
if not x & 1:
value = (value << 8) | cpu.mem[x + 1]
cpu.stack.append(value)
@instruction(0xf)
def st_instr(cpu):
"*x = y; x = (x+2)"
x = cpu.stack.pop()
y = cpu.stack.pop()
if y & 1:
cpu.mem[x] = y & 0xff
else:
cpu.mem[x] = (y & 0xff00) >> 8
cpu.mem[x + 1] = y & 0x00ff
cpu.stack.append(y)
def __init__(self, memory):
self.ip = 0
self.cycle_count = 0
self.mem = memory
self.stack = []
self.stash = []
self.last_lit = -2
self.lit_shift = 16
self.lit_next = False
self.new_ip = None
self.half = 0
def execute_instruction(self, instr):
h = lambda x: hex(x)[2:].rjust(4, '0')
print h(self.ip),
if self.lit_next:
self.stack[-1] |= (instr << self.lit_shift)
self.lit_next = False
self.lit_shift += 4
print '\t',
else:
instr = by_opcode[instr]
print '\t', instr.__name__,
instr(self)
self.cycle_count += 1
print '\t', ' '.join(chain(map(h, self.stack), '|', map(h, reversed(self.stash))))
def step_once(self):
if self.half:
self.execute_instruction(self.mem[self.ip] & 0x0F)
else:
self.execute_instruction(self.mem[self.ip] >> 4)
if self.half:
self.ip += 1
self.half ^= 1
def step_until(self, end_addr):
while self.ip < end_addr:
self.step_once()
if self.new_ip is not None:
# We cannot skip in the last quarter of a word
assert self.half or (self.ip & 1)
self.step_once()
self.ip = self.new_ip
self.half = 0
self.new_ip = None
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('infile', nargs='?', type=FileType('r'), default=sys.stdin)
args = parser.parse_args()
memory = map(ord, args.infile.read())
Tinystack(memory + [0] * (65536 - len(memory))).step_until(len(memory))