prog8/tinyvm/vm.py

764 lines
30 KiB
Python
Raw Normal View History

2018-02-25 15:43:00 +00:00
# 8/16 bit virtual machine
# machine specs:
# MEMORY: 64K bytes, treated as one single array, indexed per byte, ONLY DATA - NO CODE
# elements addressable as one of three elementary data types:
# 8-bit byte (singed and unsigned),
# 16-bit words (two 8-bit bytes, signed and unsigned) (stored in LSB order),
# 5-byte MFLPT floating point
# addressing is possible via byte index (for the $0000-$00ff range) or via an unsigned word.
2018-02-27 19:39:46 +00:00
# there is NO memory management at all; all of the mem is globally shared and always available in full.
# certain blocks of memory can be marked as read-only (write attempts will then crash the vm)
2018-02-25 15:43:00 +00:00
#
# MEMORY ACCESS: via explicit load and store instructions,
# to put a value onto the stack or store the value on the top of the stack,
# or in one of the dynamic variables.
#
# I/O: either via programmed I/O routines:
2018-03-02 23:07:44 +00:00
# write [byte/bytearray to text output/screen],
# read [byte/bytearray from keyboard],
# wait [till any input comes available], @todo
# check [if input is available) @todo
2018-03-03 11:13:25 +00:00
# or via memory-mapped I/O (text screen matrix, keyboard scan register @todo)
2018-02-25 15:43:00 +00:00
#
# CPU: stack based execution, no registers.
# unlimited dynamic variables (v0, v1, ...) that have a value and a type.
# types:
2018-02-27 19:39:46 +00:00
# 1-bit boolean,
# 8-bit byte (singed and unsigned),
# 16-bit words (two 8-bit bytes, signed and unsigned),
# floating point,
2018-02-25 15:43:00 +00:00
# array of bytes (signed and unsigned),
# array of words (signed and unsigned),
# matrix (2-dimensional array) of bytes (signed and unsigned).
2018-02-27 19:39:46 +00:00
# all of these can have the flag CONST as well which means they cannot be modified.
2018-02-25 15:43:00 +00:00
#
# push (constant,
# mark, unwind to previous mark.
#
# CPU INSTRUCTIONS:
# stack manipulation mainly:
# nop
# push var / push2 var1, var2
# pop var / pop2 var1, var2
2018-02-27 19:39:46 +00:00
# various arithmetic operations, logical operations, boolean test and comparison operations
2018-02-25 15:43:00 +00:00
# jump label
# jump_if_true label, jump_if_false label
2018-02-27 19:39:46 +00:00
# @todo jump_if_status_XX label special system dependent status register conditional check such as carry bit or overflow bit)
2018-02-25 15:43:00 +00:00
# return (return values on stack)
# syscall function (special system dependent implementation)
# call function (arguments are on stack)
2018-02-27 19:39:46 +00:00
# enter / exit (function call frame)
2018-02-25 15:43:00 +00:00
#
2018-02-27 21:16:45 +00:00
# TIMER INTERRUPT: triggered around each 1/30th of a second.
2018-02-25 15:43:00 +00:00
# executes on a DIFFERENT stack and with a different PROGRAM LIST,
2018-02-27 19:39:46 +00:00
# but with access to ALL THE SAME DYNAMIC VARIABLES.
2018-02-27 21:16:45 +00:00
# This suspends the main program until the timer program RETURNs!
2018-02-25 15:43:00 +00:00
#
import time
import itertools
2018-02-27 19:39:46 +00:00
import collections
import array
2018-02-27 21:16:45 +00:00
import threading
2018-02-27 19:39:46 +00:00
import pprint
2018-03-03 11:13:25 +00:00
import tkinter
import tkinter.font
2018-02-25 15:43:00 +00:00
from typing import Dict, List, Tuple, Union
from il65.emit import mflpt5_to_float, to_mflpt5
2018-03-04 14:11:45 +00:00
from .program import Instruction, Variable, Block, Program, Opcode, Value, DataType
2018-02-25 15:43:00 +00:00
class ExecutionError(Exception):
pass
2018-02-27 19:39:46 +00:00
class TerminateExecution(SystemExit):
pass
class MemoryAccessError(Exception):
pass
2018-02-25 15:43:00 +00:00
class Memory:
def __init__(self):
self.mem = bytearray(65536)
2018-02-27 19:39:46 +00:00
self.readonly = bytearray(65536)
def mark_readonly(self, start: int, end: int) -> None:
self.readonly[start:end+1] = [1] * (end-start+1)
2018-02-25 15:43:00 +00:00
def get_byte(self, index: int) -> int:
return self.mem[index]
2018-03-03 11:13:25 +00:00
def get_bytes(self, startindex: int, amount: int) -> int:
return self.mem[startindex: startindex+amount]
2018-02-25 15:43:00 +00:00
def get_sbyte(self, index: int) -> int:
return 256 - self.mem[index]
def get_word(self, index: int) -> int:
return self.mem[index] + 256 * self.mem[index+1]
def get_sword(self, index: int) -> int:
return 65536 - (self.mem[index] + 256 * self.mem[index+1])
def get_float(self, index: int) -> float:
return mflpt5_to_float(self.mem[index: index+5])
def set_byte(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index] = value
def set_sbyte(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index] = value + 256
def set_word(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index] or self.readonly[index+1]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
hi, lo = divmod(value, 256)
self.mem[index] = lo
self.mem[index+1] = hi
def set_sword(self, index: int, value: int) -> None:
2018-02-27 19:39:46 +00:00
if self.readonly[index] or self.readonly[index+1]:
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
hi, lo = divmod(value + 65536, 256)
self.mem[index] = lo
self.mem[index+1] = hi
def set_float(self, index: int, value: float) -> None:
2018-02-27 19:39:46 +00:00
if any(self.readonly[index:index+5]):
raise MemoryAccessError("read-only", index)
2018-02-25 15:43:00 +00:00
self.mem[index: index+5] = to_mflpt5(value)
2018-02-27 21:16:45 +00:00
class CallFrameMarker:
2018-03-02 01:49:43 +00:00
__slots__ = ["returninstruction"]
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __init__(self, instruction: Instruction) -> None:
self.returninstruction = instruction
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __str__(self) -> str:
return repr(self)
2018-02-27 21:16:45 +00:00
2018-03-02 01:49:43 +00:00
def __repr__(self) -> str:
return "<CallFrameMarker returninstruction={:s}>".format(str(self.returninstruction))
2018-02-27 21:16:45 +00:00
2018-03-04 14:11:45 +00:00
StackValueType = Union[Value, CallFrameMarker]
2018-02-25 15:43:00 +00:00
class Stack:
def __init__(self):
self.stack = []
2018-02-27 19:39:46 +00:00
self.pop_history = collections.deque(maxlen=10)
2018-02-25 15:43:00 +00:00
def debug_peek(self, size: int) -> List[StackValueType]:
return self.stack[-size:]
def size(self) -> int:
return len(self.stack)
def pop(self) -> StackValueType:
2018-02-27 19:39:46 +00:00
x = self.stack.pop()
self.pop_history.append(x)
return x
2018-02-25 15:43:00 +00:00
def pop2(self) -> Tuple[StackValueType, StackValueType]:
2018-02-27 19:39:46 +00:00
x, y = self.stack.pop(), self.stack.pop()
self.pop_history.append(x)
self.pop_history.append(y)
return x, y
2018-02-25 15:43:00 +00:00
def pop3(self) -> Tuple[StackValueType, StackValueType, StackValueType]:
2018-02-27 19:39:46 +00:00
x, y, z = self.stack.pop(), self.stack.pop(), self.stack.pop()
self.pop_history.append(x)
self.pop_history.append(y)
self.pop_history.append(z)
return x, y, z
2018-02-25 15:43:00 +00:00
2018-03-02 01:49:43 +00:00
def pop_under(self, number: int) -> StackValueType:
return self.stack.pop(-1-number)
2018-02-25 15:43:00 +00:00
def push(self, item: StackValueType) -> None:
self._typecheck(item)
self.stack.append(item)
def push2(self, first: StackValueType, second: StackValueType) -> None:
self._typecheck(first)
self._typecheck(second)
self.stack.append(first)
self.stack.append(second)
def push3(self, first: StackValueType, second: StackValueType, third: StackValueType) -> None:
self._typecheck(first)
self._typecheck(second)
self._typecheck(third)
self.stack.extend([first, second, third])
2018-03-02 01:49:43 +00:00
def push_under(self, number: int, value: StackValueType) -> None:
self.stack.insert(-number, value)
2018-02-25 15:43:00 +00:00
def peek(self) -> StackValueType:
return self.stack[-1] if self.stack else None
def swap(self) -> None:
x = self.stack[-1]
self.stack[-1] = self.stack[-2]
self.stack[-2] = x
def _typecheck(self, value: StackValueType):
2018-03-04 14:11:45 +00:00
if not isinstance(value, (Value, CallFrameMarker)):
raise TypeError("invalid item type pushed", value)
2018-02-25 15:43:00 +00:00
# noinspection PyPep8Naming,PyUnusedLocal,PyMethodMayBeStatic
class VM:
2018-02-27 19:39:46 +00:00
str_encoding = "iso-8859-15"
str_alt_encoding = "iso-8859-15"
readonly_mem_ranges = [] # type: List[Tuple[int, int]]
2018-02-27 21:16:45 +00:00
timer_irq_resolution = 1/30
timer_irq_interlock = threading.Lock()
timer_irq_event = threading.Event()
2018-02-25 15:43:00 +00:00
2018-03-03 11:13:25 +00:00
def __init__(self, program: Program, timerprogram: Program=None) -> None:
2018-02-25 15:43:00 +00:00
opcode_names = [oc.name for oc in Opcode]
2018-03-03 11:13:25 +00:00
timerprogram = timerprogram or Program([])
2018-02-25 15:43:00 +00:00
for ocname in opcode_names:
if not hasattr(self, "opcode_" + ocname):
raise NotImplementedError("missing opcode method for " + ocname)
for method in dir(self):
if method.startswith("opcode_"):
if not method[7:] in opcode_names:
raise RuntimeError("opcode method for undefined opcode " + method)
2018-03-02 01:49:43 +00:00
for oc in Opcode:
if oc not in self.dispatch_table:
raise NotImplementedError("no dispatch entry in table for " + oc.name)
2018-02-25 15:43:00 +00:00
self.memory = Memory()
2018-02-27 19:39:46 +00:00
for start, end in self.readonly_mem_ranges:
self.memory.mark_readonly(start, end)
2018-02-25 15:43:00 +00:00
self.main_stack = Stack()
self.timer_stack = Stack()
2018-02-27 21:16:45 +00:00
self.main_program, self.timer_program, self.variables, self.labels = self.flatten_programs(program, timerprogram)
2018-02-25 15:43:00 +00:00
self.connect_instruction_pointers(self.main_program)
self.connect_instruction_pointers(self.timer_program)
self.program = self.main_program
self.stack = self.main_stack
self.pc = None # type: Instruction
2018-03-04 14:11:45 +00:00
self.charscreen_address = 0
self.charscreen_width = 0
self.charscreen_height = 0
2018-02-27 19:39:46 +00:00
self.system = System(self)
assert all(i.next for i in self.main_program
if i.opcode != Opcode.TERMINATE), "main: all instrs next must be set"
assert all(i.next for i in self.timer_program
2018-02-27 21:16:45 +00:00
if i.opcode not in (Opcode.TERMINATE, Opcode.RETURN)), "timer: all instrs next must be set"
assert all(i.alt_next for i in self.main_program
if i.opcode in (Opcode.CALL, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE)), "main: alt_nexts must be set"
assert all(i.alt_next for i in self.timer_program
if i.opcode in (Opcode.CALL, Opcode.JUMP_IF_FALSE, Opcode.JUMP_IF_TRUE)), "timer: alt_nexts must be set"
threading.Thread(target=self.timer_irq, name="timer_irq", daemon=True).start()
2018-02-27 19:39:46 +00:00
print("[TinyVM starting up.]")
2018-02-25 15:43:00 +00:00
2018-03-03 11:13:25 +00:00
def enable_charscreen(self, screen_address: int, width: int, height: int) -> None:
2018-03-04 14:11:45 +00:00
self.charscreen_address = screen_address
self.charscreen_width, self.charscreen_height = width, height
2018-03-03 11:13:25 +00:00
2018-02-27 21:16:45 +00:00
def flatten_programs(self, main: Program, timer: Program) \
-> Tuple[List[Instruction], List[Instruction], Dict[str, Variable], Dict[str, Instruction]]:
variables = {} # type: Dict[str, Variable]
labels = {} # type: Dict[str, Instruction]
instructions_main = [] # type: List[Instruction]
instructions_timer = [] # type: List[Instruction]
for block in main.blocks:
flat = self.flatten(block, variables, labels)
instructions_main.extend(flat)
instructions_main.append(Instruction(Opcode.TERMINATE, [], None, None))
for block in timer.blocks:
flat = self.flatten(block, variables, labels)
instructions_timer.extend(flat)
return instructions_main, instructions_timer, variables, labels
2018-02-25 15:43:00 +00:00
def flatten(self, block: Block, variables: Dict[str, Variable], labels: Dict[str, Instruction]) -> List[Instruction]:
def block_prefix(b: Block) -> str:
if b.parent:
return block_prefix(b.parent) + "." + b.name
else:
return b.name
prefix = block_prefix(block)
instructions = block.instructions
for ins in instructions:
if ins.opcode == Opcode.SYSCALL:
continue
if ins.args:
2018-03-04 14:11:45 +00:00
newargs = [] # type: List[Union[str, int, Value]]
2018-02-25 15:43:00 +00:00
for a in ins.args:
2018-03-04 14:11:45 +00:00
if isinstance(a, str):
2018-02-25 15:43:00 +00:00
newargs.append(prefix + "." + a)
else:
2018-03-04 14:11:45 +00:00
newargs.append(a) # type: ignore
2018-02-25 15:43:00 +00:00
ins.args = newargs
for vardef in block.variables:
vname = prefix + "." + vardef.name
assert vname not in variables
variables[vname] = vardef
for name, instr in block.labels.items():
name = prefix + "." + name
assert name not in labels
labels[name] = instr
for subblock in block.blocks:
instructions.extend(self.flatten(subblock, variables, labels))
del block.instructions
del block.variables
del block.labels
return instructions
def connect_instruction_pointers(self, instructions: List[Instruction]) -> None:
i1, i2 = itertools.tee(instructions)
next(i2, None)
for i, nexti in itertools.zip_longest(i1, i2):
2018-02-27 21:16:45 +00:00
if i.opcode in (Opcode.JUMP_IF_TRUE, Opcode.JUMP_IF_FALSE):
i.next = nexti # normal flow target
i.alt_next = self.labels[i.args[0]] # conditional jump target
elif i.opcode == Opcode.JUMP:
i.next = self.labels[i.args[0]] # jump target
elif i.opcode == Opcode.CALL:
2018-03-02 01:49:43 +00:00
i.next = self.labels[i.args[1]] # call target
2018-02-27 21:16:45 +00:00
i.alt_next = nexti # return instruction
else:
i.next = nexti
2018-02-25 15:43:00 +00:00
def run(self) -> None:
2018-03-04 14:11:45 +00:00
if self.charscreen_address:
threading.Thread(target=ScreenViewer.create,
args=(self.memory, self.system, self.charscreen_address, self.charscreen_width, self.charscreen_height),
name="screenviewer", daemon=True).start()
2018-03-03 11:13:25 +00:00
2018-02-25 15:43:00 +00:00
self.pc = self.program[0] # first instruction of the main program
2018-03-02 01:49:43 +00:00
self.stack.push(CallFrameMarker(None)) # enter the call frame so the timer program can end with a RETURN
2018-02-25 15:43:00 +00:00
try:
2018-02-27 21:16:45 +00:00
while self.pc is not None:
with self.timer_irq_interlock:
2018-02-28 01:40:16 +00:00
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
2018-02-27 21:16:45 +00:00
if next_pc:
self.pc = self.pc.next
2018-02-27 19:39:46 +00:00
except TerminateExecution as x:
why = str(x)
2018-02-27 21:16:45 +00:00
print("[TinyVM execution terminated{:s}]\n".format(": "+why if why else "."))
2018-02-27 19:39:46 +00:00
return
2018-02-25 15:43:00 +00:00
except Exception as x:
print("EXECUTION ERROR")
self.debug_stack(5)
raise
2018-02-27 21:16:45 +00:00
else:
print("[TinyVM execution ended.]")
def timer_irq(self) -> None:
2018-03-03 11:13:25 +00:00
# This is the timer 'irq' handler. It runs the timer program at a certain interval.
# NOTE: executing the timer program will LOCK the main program and vice versa!
# (because the VM is a strictly single threaded machine)
2018-02-27 21:16:45 +00:00
resolution = 1/30
wait_time = resolution
while True:
self.timer_irq_event.wait(wait_time)
self.timer_irq_event.clear()
start = time.perf_counter()
if self.timer_program:
with self.timer_irq_interlock:
previous_pc = self.pc
previous_program = self.program
previous_stack = self.stack
self.stack = self.timer_stack
self.program = self.timer_program
self.pc = self.program[0]
2018-03-02 01:49:43 +00:00
self.stack.push(CallFrameMarker(None)) # enter the call frame so the timer program can end with a RETURN
2018-02-27 21:16:45 +00:00
while self.pc is not None:
2018-02-28 01:40:16 +00:00
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
2018-02-27 21:16:45 +00:00
if next_pc:
self.pc = self.pc.next
self.pc = previous_pc
self.program = previous_program
self.stack = previous_stack
current = time.perf_counter()
duration, previously = current - start, current
wait_time = max(0, resolution - duration)
2018-02-25 15:43:00 +00:00
def debug_stack(self, size: int=5) -> None:
stack = self.stack.debug_peek(size)
if len(stack) > 0:
2018-03-02 01:49:43 +00:00
print("** stack (top {:d}):".format(size))
2018-02-25 15:43:00 +00:00
for i, value in enumerate(reversed(stack), start=1):
2018-03-02 01:49:43 +00:00
print(" {:d}. {:s} {:s}".format(i, type(value).__name__, str(value)))
2018-02-25 15:43:00 +00:00
else:
2018-03-02 01:49:43 +00:00
print("** stack is empty.")
2018-02-27 19:39:46 +00:00
if self.stack.pop_history:
2018-03-02 01:49:43 +00:00
print("** last {:d} values popped from stack (most recent on top):".format(self.stack.pop_history.maxlen))
pprint.pprint(list(reversed(self.stack.pop_history)), indent=2, compact=True, width=20) # type: ignore
2018-02-25 15:43:00 +00:00
if self.pc is not None:
2018-02-27 19:39:46 +00:00
print("* instruction:", self.pc)
2018-02-25 15:43:00 +00:00
2018-03-04 14:11:45 +00:00
def assign_variable(self, variable: Variable, value: Value) -> None:
2018-02-27 19:39:46 +00:00
assert not variable.const, "cannot modify a const"
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value)
2018-02-27 19:39:46 +00:00
variable.value = value
2018-02-25 15:43:00 +00:00
def opcode_NOP(self, instruction: Instruction) -> bool:
# do nothing
return True
def opcode_TERMINATE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
raise TerminateExecution()
2018-02-25 15:43:00 +00:00
def opcode_PUSH(self, instruction: Instruction) -> bool:
value = self.variables[instruction.args[0]].value
self.stack.push(value)
return True
2018-03-02 01:49:43 +00:00
def opcode_DUP(self, instruction: Instruction) -> bool:
self.stack.push(self.stack.peek())
return True
def opcode_DUP2(self, instruction: Instruction) -> bool:
x = self.stack.peek()
self.stack.push(x)
self.stack.push(x)
return True
2018-03-03 11:13:25 +00:00
def opcode_SWAP(self, instruction: Instruction) -> bool:
value2, value1 = self.stack.pop2()
self.stack.push2(value2, value1)
return True
2018-02-25 15:43:00 +00:00
def opcode_PUSH2(self, instruction: Instruction) -> bool:
value1 = self.variables[instruction.args[0]].value
value2 = self.variables[instruction.args[1]].value
self.stack.push2(value1, value2)
return True
def opcode_PUSH3(self, instruction: Instruction) -> bool:
value1 = self.variables[instruction.args[0]].value
value2 = self.variables[instruction.args[1]].value
value3 = self.variables[instruction.args[2]].value
self.stack.push3(value1, value2, value3)
return True
def opcode_POP(self, instruction: Instruction) -> bool:
value = self.stack.pop()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value)
2018-02-25 15:43:00 +00:00
return True
def opcode_POP2(self, instruction: Instruction) -> bool:
value1, value2 = self.stack.pop2()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value1)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[1]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value2)
2018-02-25 15:43:00 +00:00
return True
def opcode_POP3(self, instruction: Instruction) -> bool:
value1, value2, value3 = self.stack.pop3()
variable = self.variables[instruction.args[0]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value1)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[1]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value2)
2018-02-25 15:43:00 +00:00
variable = self.variables[instruction.args[2]]
2018-02-27 19:39:46 +00:00
self.assign_variable(variable, value3)
2018-02-25 15:43:00 +00:00
return True
def opcode_ADD(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first + second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_SUB(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first - second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_MUL(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first * second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_DIV(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first / second) # type: ignore
2018-02-25 15:43:00 +00:00
return True
def opcode_AND(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first and second)
2018-02-25 15:43:00 +00:00
return True
def opcode_OR(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
self.stack.push(first or second)
2018-02-25 15:43:00 +00:00
return True
def opcode_XOR(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
ifirst = 1 if first else 0
isecond = 1 if second else 0
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, bool(ifirst ^ isecond)))
2018-02-25 15:43:00 +00:00
return True
def opcode_NOT(self, instruction: Instruction) -> bool:
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, not self.stack.pop()))
2018-02-25 15:43:00 +00:00
return True
2018-02-27 21:16:45 +00:00
def opcode_TEST(self, instruction: Instruction) -> bool:
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, bool(self.stack.pop())))
2018-02-27 21:16:45 +00:00
return True
2018-02-25 15:43:00 +00:00
def opcode_CMP_EQ(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, first == second))
2018-02-27 19:39:46 +00:00
return True
2018-02-25 15:43:00 +00:00
def opcode_CMP_LT(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, first < second))
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_GT(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, first > second))
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_LTE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, first <= second))
2018-02-25 15:43:00 +00:00
return True
def opcode_CMP_GTE(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 14:11:45 +00:00
self.stack.push(Value(DataType.BOOL, first >= second))
2018-02-25 15:43:00 +00:00
return True
2018-02-27 21:16:45 +00:00
def opcode_CALL(self, instruction: Instruction) -> bool:
2018-03-02 01:49:43 +00:00
# arguments are already on the stack
2018-03-04 14:11:45 +00:00
num_args = instruction.args[0]
assert isinstance(num_args, int)
self.stack.push_under(num_args, CallFrameMarker(instruction.alt_next))
2018-02-27 21:16:45 +00:00
return True
2018-02-25 15:43:00 +00:00
def opcode_RETURN(self, instruction: Instruction) -> bool:
2018-03-04 14:11:45 +00:00
num_returnvalues = instruction.args[0]
assert isinstance(num_returnvalues, int)
callframe = self.stack.pop_under(num_returnvalues)
2018-03-02 23:07:44 +00:00
assert isinstance(callframe, CallFrameMarker), callframe
2018-03-02 01:49:43 +00:00
self.pc = callframe.returninstruction
2018-02-27 21:16:45 +00:00
return False
2018-02-25 15:43:00 +00:00
def opcode_JUMP(self, instruction: Instruction) -> bool:
2018-02-27 21:16:45 +00:00
return True # jump simply points to the next instruction elsewhere
2018-02-25 15:43:00 +00:00
def opcode_JUMP_IF_TRUE(self, instruction: Instruction) -> bool:
result = self.stack.pop()
if result:
2018-02-27 21:16:45 +00:00
self.pc = self.pc.alt_next # alternative next instruction
2018-02-25 15:43:00 +00:00
return False
return True
def opcode_JUMP_IF_FALSE(self, instruction: Instruction) -> bool:
result = self.stack.pop()
if result:
return True
2018-02-27 21:16:45 +00:00
self.pc = self.pc.alt_next # alternative next instruction
2018-02-25 15:43:00 +00:00
return False
def opcode_SYSCALL(self, instruction: Instruction) -> bool:
2018-03-04 14:11:45 +00:00
syscall = instruction.args[0]
assert isinstance(syscall, str)
call = getattr(self.system, "syscall_" + syscall, None)
2018-02-25 15:43:00 +00:00
if call:
return call()
else:
2018-03-04 14:11:45 +00:00
raise RuntimeError("no syscall method for " + syscall)
2018-02-25 15:43:00 +00:00
2018-02-28 01:40:16 +00:00
dispatch_table = {
Opcode.TERMINATE: opcode_TERMINATE,
Opcode.NOP: opcode_NOP,
Opcode.PUSH: opcode_PUSH,
Opcode.PUSH2: opcode_PUSH2,
Opcode.PUSH3: opcode_PUSH3,
Opcode.POP: opcode_POP,
Opcode.POP2: opcode_POP2,
Opcode.POP3: opcode_POP3,
2018-03-02 01:49:43 +00:00
Opcode.DUP: opcode_DUP,
Opcode.DUP2: opcode_DUP2,
2018-03-03 11:13:25 +00:00
Opcode.SWAP: opcode_SWAP,
2018-02-28 01:40:16 +00:00
Opcode.ADD: opcode_ADD,
Opcode.SUB: opcode_SUB,
Opcode.MUL: opcode_MUL,
Opcode.DIV: opcode_DIV,
Opcode.AND: opcode_AND,
Opcode.OR: opcode_OR,
Opcode.XOR: opcode_XOR,
Opcode.NOT: opcode_NOT,
Opcode.TEST: opcode_TEST,
Opcode.CMP_EQ: opcode_CMP_EQ,
Opcode.CMP_LT: opcode_CMP_LT,
Opcode.CMP_GT: opcode_CMP_GT,
Opcode.CMP_LTE: opcode_CMP_LTE,
Opcode.CMP_GTE: opcode_CMP_GTE,
Opcode.CALL: opcode_CALL,
Opcode.RETURN: opcode_RETURN,
Opcode.JUMP: opcode_JUMP,
Opcode.JUMP_IF_TRUE: opcode_JUMP_IF_TRUE,
Opcode.JUMP_IF_FALSE: opcode_JUMP_IF_FALSE,
Opcode.SYSCALL: opcode_SYSCALL,
}
2018-02-27 19:39:46 +00:00
class System:
def __init__(self, vm: VM) -> None:
self.vm = vm
2018-03-03 11:13:25 +00:00
def encodestr(self, string: str, alt: bool=False) -> bytearray:
2018-02-27 19:39:46 +00:00
return bytearray(string, self.vm.str_alt_encoding if alt else self.vm.str_encoding)
2018-03-03 11:13:25 +00:00
def decodestr(self, bb: Union[bytearray, array.array], alt: bool=False) -> str:
2018-03-02 23:07:44 +00:00
return str(bb, self.vm.str_alt_encoding if alt else self.vm.str_encoding) # type: ignore
2018-02-27 19:39:46 +00:00
2018-02-25 15:43:00 +00:00
def syscall_printstr(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value)
if value.dtype == DataType.ARRAY_BYTE:
print(self.decodestr(value.value), end="@") # type: ignore
2018-02-25 15:43:00 +00:00
return True
else:
2018-02-27 19:39:46 +00:00
raise TypeError("printstr expects bytearray", value)
2018-02-25 15:43:00 +00:00
2018-03-02 23:07:44 +00:00
def syscall_printchr(self) -> bool:
2018-03-04 14:11:45 +00:00
charactervalue = self.vm.stack.pop()
assert isinstance(charactervalue, Value)
if charactervalue.dtype == DataType.BYTE:
print(self.decodestr(bytearray([charactervalue.value])), end="") # type: ignore
2018-03-02 23:07:44 +00:00
return True
else:
2018-03-04 14:11:45 +00:00
raise TypeError("printchr expects BYTE", charactervalue)
2018-03-02 23:07:44 +00:00
def syscall_input(self) -> bool:
2018-03-04 14:11:45 +00:00
self.vm.stack.push(Value(DataType.ARRAY_BYTE, self.encodestr(input())))
2018-03-02 23:07:44 +00:00
return True
def syscall_getchr(self) -> bool:
2018-03-04 14:11:45 +00:00
self.vm.stack.push(Value(DataType.BYTE, self.encodestr(input() + '\n')[0]))
2018-03-02 23:07:44 +00:00
return True
2018-02-25 15:43:00 +00:00
def syscall_decimalstr_signed(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-03-04 14:11:45 +00:00
if value.dtype in (DataType.SBYTE, DataType.SWORD):
self.vm.stack.push(Value(DataType.ARRAY_BYTE, self.encodestr(str(value.value))))
return True
else:
raise TypeError("decimalstr_signed expects signed int", value)
def syscall_decimalstr_unsigned(self) -> bool:
value = self.vm.stack.pop()
if value.dtype in (DataType.BYTE, DataType.WORD):
self.vm.stack.push(Value(DataType.ARRAY_BYTE, self.encodestr(str(value.value))))
2018-02-25 15:43:00 +00:00
return True
else:
2018-03-04 14:11:45 +00:00
raise TypeError("decimalstr_signed expects unsigned int", value)
2018-02-25 15:43:00 +00:00
def syscall_hexstr_signed(self) -> bool:
2018-02-27 19:39:46 +00:00
value = self.vm.stack.pop()
2018-02-25 15:43:00 +00:00
if type(value) is int:
if value >= 0: # type: ignore
strvalue = "${:x}".format(value)
else:
strvalue = "-${:x}".format(-value) # type: ignore
2018-03-04 14:11:45 +00:00
self.vm.stack.push(Value(DataType.ARRAY_BYTE, self.encodestr(strvalue)))
2018-02-25 15:43:00 +00:00
return True
else:
2018-02-27 19:39:46 +00:00
raise TypeError("hexstr expects int", value)
def syscall_memwrite_byte(self) -> bool:
value, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value) and isinstance(address, Value)
assert value.dtype == DataType.BYTE and address.dtype == DataType.WORD
self.vm.memory.set_byte(address.value, value.value) # type: ignore
2018-02-27 19:39:46 +00:00
return True
def syscall_memwrite_sbyte(self) -> bool:
value, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value) and isinstance(address, Value)
assert value.dtype == DataType.SBYTE and address.dtype == DataType.WORD
self.vm.memory.set_sbyte(address.value, value.value) # type: ignore
2018-02-27 19:39:46 +00:00
return True
2018-03-02 23:07:44 +00:00
def syscall_memwrite_word(self) -> bool:
value, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value) and isinstance(address, Value)
assert value.dtype in (DataType.WORD, DataType.BYTE) and address.dtype == DataType.WORD
self.vm.memory.set_word(address.value, value.value) # type: ignore
2018-03-02 23:07:44 +00:00
return True
def syscall_memwrite_sword(self) -> bool:
value, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value) and isinstance(address, Value)
assert value.dtype in (DataType.SWORD, DataType.SBYTE, DataType.BYTE) and address.dtype == DataType.WORD
self.vm.memory.set_sword(address.value, value.value) # type: ignore
2018-03-02 23:07:44 +00:00
return True
def syscall_memwrite_float(self) -> bool:
value, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(value, Value) and isinstance(address, Value)
assert value.dtype == DataType.FLOAT and address.dtype == DataType.WORD
self.vm.memory.set_float(address.value, value.value) # type: ignore
2018-03-02 23:07:44 +00:00
return True
def syscall_memwrite_str(self) -> bool:
strbytes, address = self.vm.stack.pop2()
2018-03-04 14:11:45 +00:00
assert isinstance(strbytes, Value) and isinstance(address, Value)
assert strbytes.dtype == DataType.ARRAY_BYTE and address.dtype == DataType.WORD
for i, b in enumerate(strbytes.value): # type: ignore
self.vm.memory.set_byte(address+i, b) # type: ignore
2018-03-02 23:07:44 +00:00
return True
2018-03-03 11:13:25 +00:00
def syscall_smalldelay(self) -> bool:
time.sleep(1/100)
return True
def syscall_delay(self) -> bool:
2018-03-04 14:11:45 +00:00
time.sleep(0.1)
2018-03-03 11:13:25 +00:00
return True
class ScreenViewer(tkinter.Tk):
2018-03-04 14:11:45 +00:00
def __init__(self, memory: Memory, system: System, screen_addr: int, screen_width: int, screen_height: int) -> None:
2018-03-03 11:13:25 +00:00
super().__init__()
self.fontsize = 14
self.memory = memory
self.system = system
2018-03-04 14:11:45 +00:00
self.address = screen_addr
self.width = screen_width
self.height = screen_height
self.monospace = tkinter.font.Font(self, family="Courier", weight="bold", size=self.fontsize) # type: ignore
2018-03-03 11:13:25 +00:00
cw = self.monospace.measure("x")*self.width+8
self.canvas = tkinter.Canvas(self, width=cw, height=self.fontsize*self.height+8, bg="blue")
self.canvas.pack()
self.after(10, self.update_screen)
def update_screen(self):
self.canvas.delete(tkinter.ALL)
for y in range(self.height):
line = self.memory.get_bytes(self.address+y*self.width, self.width)
text = self.system.decodestr(line)
self.canvas.create_text(4, self.fontsize*y, text=text, fill="white", font=self.monospace, anchor=tkinter.NW)
self.after(10, self.update_screen)
@classmethod
2018-03-04 14:11:45 +00:00
def create(cls, memory: Memory, system: System, screen_addr: int, screen_width: int, screen_height: int) -> None:
viewer = cls(memory, system, screen_addr, screen_width, screen_height)
2018-03-03 11:13:25 +00:00
viewer.mainloop()