def show(self): print(' '+' '.join([Drip.tohex(x).zfill(4).upper() for x in range(256)])) for y, row in enumerate(self.image): print(Drip.tohex(y*256), end=' ') for item in row: print(item, end=' ') print('')
def pop(self, stack): if stack == 'call': self.CSP += 1 return self.ram.get(Drip.tohex(self.CSP)) elif stack == 'global': self.GSP += 1 return self.ram.get(Drip.tohex(self.GSP)) elif stack == 'private': self.PSP += 1 return self.ram.get(Drip.tohex(self.PSP))
def push(self, stack, arg): if stack == 'call': self.ram.put(Drip.tohex(self.CSP), arg) self.CSP -= 1 elif stack == 'global': self.ram.put(Drip.tohex(self.GSP), arg) self.GSP -= 1 elif stack == 'private': self.ram.put(Drip.tohex(self.PSP), arg) self.PSP -= 1
def run(self): self.registers = dict.fromkeys(Drip.registers.values(), '0000') self.IP = 0 + self.init print('init', self.init) self.CSP = int('0xFFFF', 16) self.GSP = int('0xFEFF', 16) self.MSP = int('0xFDFF', 16) self.SR = 0 self.debug = '' self.load_in() while True: self.jumped = 0 func, args, forward, op = self.fetch(hex(self.IP)) if func == 'HALT': break elif op[2] == 'A': self.registers[args[0]] = Drip.tohex( self.math(func, [int(self.registers[register], 16) for register in args])) elif op[2] == 'B': self.registers[args[0]] = Drip.tohex( self.math(func, [int(self.registers[args[0]], 16), int(args[1], 16)])) elif op[2] == 'D' or op[2] == 'F': func(op, args) elif op[1] == 'A': self.push('call', Drip.tohex(self.IP)) print('call branch') self.IP = int(args[0], 16) self.MSP -= 256 self.PSP = self.MSP continue elif op[1] == 'B': print('call return') self.IP = int(self.pop('call'), 16) self.MSP += 265 self.PSP = self.MSP elif op == '00E0': self.push('private', self.registers[args[0]]) elif op == '00E1': self.registers[args[0]] = self.pop('private') elif op == '00EA': self.push('global', self.registers[args[0]]) elif op == '00EB': self.registers[args[0]] = self.pop('global') else: func(args) if not self.jumped: self.IP += forward + 1 print('out', self.stdout) print(''.join(map(chr, map(partial(int, base=16), self.stdout)))) print('Finished')
def load_in(self): self.stdin = [Drip.tohex(ord(c)) for c in input()]