git @ Cat's Eye Technologies ZOWIE / d03834a
Allow implementation to (more or less) run under Skulpt. catseye 8 years ago
1 changed file(s) with 43 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
88
99 import re
1010 import sys
11
12
13 # check if running under Skulpt, and if so, apply appropriate modifications
14 if getattr(sys, 'resetTimeout', None) is not None:
15 __name__ = '__skulpt__'
16 class EOFError:
17 pass
18 class SyntaxError:
19 pass
20 class UnicodeEncodeError:
21 pass
22 def output(code):
23 if code > 126:
24 print "&#%d;" % code
25 else:
26 print chr(code)
27 else:
28 def output(code):
29 try:
30 sys.stdout.write(unichr(code))
31 except UnicodeEncodeError:
32 sys.stdout.write("&#%d;" % code)
33
34
35 def copy_dict(d):
36 e = {}
37 for key in d:
38 e[key] = d[key]
39 return e
1140
1241
1342 class MappedRegister: # abstract
3463 return x
3564
3665 def write(self, payload):
37 try:
38 sys.stdout.write(unichr(payload))
39 except UnicodeEncodeError:
40 sys.stdout.write("&#%d;" % payload)
66 output(payload)
4167
4268
4369 class BeginTransactionRegister(MappedRegister):
147173 def clone(self):
148174 other = MachineState(self.cpu)
149175 other.pc = self.pc
150 other.storage_register = self.storage_register.copy()
176 other.storage_register = copy_dict(self.storage_register)
151177 return other
152178
153179 def __getitem__(self, key):
180206 line, re.IGNORECASE)
181207 if m is not None:
182208 self.source_is_immediate = True
183 (dest_reg, src_imm) = m.group(1, 2)
209 dest_reg = m.group(1)
210 src_imm = m.group(2)
184211 self.destination_register = long(dest_reg)
185212 self.source_register = long(src_imm)
186213 return True
191218 m = re.match(r'^\s*MOV\s+R(\[R)?(\d+)\]?\s*,\s*R(\[R)?(\d+)\]?'
192219 r'\s*(\;.*)?$', line, re.IGNORECASE)
193220 if m is not None:
194 (dest_ind, dest_reg, src_ind, src_reg) = m.group(1, 2, 3, 4)
221 dest_ind = m.group(1)
222 dest_reg = m.group(2)
223 src_ind = m.group(3)
224 src_reg = m.group(4)
195225 if dest_ind == '[R':
196226 self.destination_is_indirect = True
197227 self.destination_register = long(dest_reg)
251281 if i.parse(line):
252282 self.program.append(i)
253283
284 def load_string(self, text):
285 for line in text.split("\n"):
286 i = Instruction()
287 if i.parse(line):
288 self.program.append(i)
289
254290 def step(self):
255291 try:
256292 i = self.program[self.state.pc]