prog8/python/tinyvm/vm.py

762 lines
31 KiB
Python
Raw Normal View History

2018-03-04 16:03:49 +00:00
"""
Simplistic 8/16 bit Virtual Machine to execute a stack based instruction language.
This is the VM itself (execution engine)
Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
"""
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-06 21:25:45 +00:00
# write [byte/bytearray to text output/screen] : syscall_printstr / syscall_printchr,
# read [byte/bytearray from keyboard] : syscall_input / syscall_getchr (both blocking)
2018-03-05 22:13:19 +00:00
# or via memory-mapped I/O (text screen matrix, keyboard scan register)
2018-02-25 15:43:00 +00:00
#
2018-03-06 21:25:45 +00:00
# CPU: single threaded, stack based execution,
# no registers, but unlimited dynamic variables (v0, v1, ...) that have a value and a type.
2018-02-25 15:43:00 +00:00
# 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
#
# 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-03-06 20:36:41 +00:00
# jump_if_status_XX label special system dependent status register conditional check such as carry bit or overflow bit)
2018-03-06 21:25:45 +00:00
# call function (arguments are on stack)
2018-02-25 15:43:00 +00:00
# return (return values on stack)
# syscall function (special system dependent implementation)
#
2018-03-06 21:25:45 +00:00
# TIMER 'INTERRUPT': triggered around each 1/60th 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-03-04 15:36:05 +00:00
from typing import Dict, List, Tuple, Union, no_type_check
2018-03-05 22:13:19 +00:00
from .program import Instruction, Variable, Block, Program, Opcode, Value
2018-03-06 20:36:41 +00:00
from .core import Memory, DataType, TerminateExecution, ExecutionError
2018-02-25 15:43:00 +00:00
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
2018-03-05 22:13:19 +00:00
charout_address = 0xd000
charin_address = 0xd001
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-03-06 20:36:41 +00:00
for opcode in Opcode:
if opcode not in self.dispatch_table:
raise NotImplementedError("missing opcode dispatch for " + opcode.name)
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-03-05 22:13:19 +00:00
self.memory.memmapped_io_charout(self.charout_address, self.memmapped_charout)
self.memory.memmapped_io_charin(self.charin_address, self.memmapped_charin)
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-03-06 20:36:41 +00:00
self.main_program, self.timer_program, self.variables, self.labels = self.flatten_programs(program, timerprogram or Program([]))
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-03-05 22:13:19 +00:00
self.keyboard_scancode = 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"
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,
2018-03-05 22:13:19 +00:00
args=(self, self.charscreen_address, self.charscreen_width, self.charscreen_height),
2018-03-04 14:11:45 +00:00
name="screenviewer", daemon=True).start()
2018-03-04 15:36:05 +00:00
time.sleep(0.05)
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-03-04 15:36:05 +00:00
counter = 0
previous_timer_irq = time.perf_counter()
2018-02-27 21:16:45 +00:00
while self.pc is not None:
2018-03-04 15:36:05 +00:00
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
if next_pc:
self.pc = self.pc.next
counter += 1
if self.charscreen_address and counter % 1000 == 0:
time.sleep(0.001) # allow the tkinter window to update
time_since_irq = time.perf_counter() - previous_timer_irq
if time_since_irq > 1/60:
self.timer_irq()
previous_timer_irq = time.perf_counter()
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-04 15:36:05 +00:00
# This is the timer 'irq' handler. It is called to run the timer program at a certain interval.
# During the execution the main program is halted
if self.timer_program:
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]
self.stack.push(CallFrameMarker(None)) # enter the call frame so the timer program can end with a RETURN
while self.pc is not None:
next_pc = self.dispatch_table[self.pc.opcode](self, self.pc)
if next_pc:
self.pc = self.pc.next
self.pc = previous_pc
self.program = previous_program
self.stack = previous_stack
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-05 22:13:19 +00:00
def memmapped_charout(self, value: int) -> None:
string = self.system.decodestr(bytearray([value]))
print(string, end="")
def memmapped_charin(self) -> int:
return self.keyboard_scancode
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:
2018-03-04 15:36:05 +00:00
value = self.variables[instruction.args[0]].value # type: ignore
2018-02-25 15:43:00 +00:00
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-03-04 15:36:05 +00:00
@no_type_check
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
def opcode_ADD(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 15:36:05 +00:00
self.stack.push(first + second)
2018-02-25 15:43:00 +00:00
return True
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
def opcode_SUB(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 15:36:05 +00:00
self.stack.push(first - second)
2018-02-25 15:43:00 +00:00
return True
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
def opcode_MUL(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 15:36:05 +00:00
self.stack.push(first * second)
2018-02-25 15:43:00 +00:00
return True
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
def opcode_DIV(self, instruction: Instruction) -> bool:
2018-02-27 19:39:46 +00:00
second, first = self.stack.pop2()
2018-03-04 15:36:05 +00:00
self.stack.push(first / second)
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-03-04 15:36:05 +00:00
@no_type_check
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-04 15:36:05 +00:00
@no_type_check
2018-02-25 15:43:00 +00:00
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
2018-03-06 20:36:41 +00:00
def opcode_SYSCALL(self, instruction: Instruction) -> bool:
syscall = instruction.args[0]
assert isinstance(syscall, str)
call = getattr(self.system, "syscall_" + syscall, None)
if call:
return call()
else:
raise RuntimeError("no syscall method for " + syscall)
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()
2018-03-06 20:36:41 +00:00
assert isinstance(result, Value)
if result.value:
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()
2018-03-06 20:36:41 +00:00
if result.value: # type: ignore
2018-02-25 15:43:00 +00:00
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
2018-03-06 20:36:41 +00:00
def opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG(self, instruction: Instruction) -> bool:
raise ExecutionError("unsupported conditional jump", instruction) # @todo implement hardware specific status register flags
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,
2018-03-06 20:36:41 +00:00
Opcode.SYSCALL: opcode_SYSCALL,
2018-02-28 01:40:16 +00:00
Opcode.JUMP: opcode_JUMP,
Opcode.JUMP_IF_TRUE: opcode_JUMP_IF_TRUE,
Opcode.JUMP_IF_FALSE: opcode_JUMP_IF_FALSE,
2018-03-06 20:36:41 +00:00
Opcode.JUMP_IF_STATUS_ZERO: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_NE: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_EQ: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_CC: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_CS: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_VC: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_VS: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_GE: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_LE: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_GT: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_LT: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_POS: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
Opcode.JUMP_IF_STATUS_NEG: opcode_JUMP_IF_STATUS_UNSUPPORTED_FLAG,
2018-02-28 01:40:16 +00:00
}
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:
2018-03-04 15:36:05 +00:00
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 15:36:05 +00:00
assert isinstance(value, Value)
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()
2018-03-04 15:36:05 +00:00
assert isinstance(value, Value)
2018-03-04 14:11:45 +00:00
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
2018-03-05 22:13:19 +00:00
def syscall_memread_byte(self) -> bool:
address = self.vm.stack.pop()
assert isinstance(address, Value)
assert address.dtype == DataType.WORD
self.vm.stack.push(Value(DataType.BYTE, self.vm.memory.get_byte(address.value))) # type: ignore
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-05 22:13:19 +00:00
def __init__(self, vm: VM, screen_addr: int, screen_width: int, screen_height: int) -> None:
2018-03-03 11:13:25 +00:00
super().__init__()
2018-03-04 15:36:05 +00:00
self.title("IL65 tinyvm")
self.fontsize = 16
2018-03-05 22:13:19 +00:00
self.vm = vm
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()
2018-03-05 22:13:19 +00:00
self.bind("<KeyPress>", self.keypress)
self.bind("<KeyRelease>", self.keyrelease)
2018-03-03 11:13:25 +00:00
self.after(10, self.update_screen)
2018-03-05 22:13:19 +00:00
def keypress(self, e) -> None:
key = e.char or e.keysym
if len(key) == 1:
self.vm.keyboard_scancode = self.vm.system.encodestr(key)[0]
elif len(key) > 1:
code = 0
if key == "Up":
code = ord("w")
elif key == "Down":
code = ord("s")
elif key == "Left":
code = ord("a")
elif key == "Right":
code = ord("d")
self.vm.keyboard_scancode = code
else:
self.vm.keyboard_scancode = 0
def keyrelease(self, e) -> None:
self.vm.keyboard_scancode = 0
def update_screen(self) -> None:
2018-03-03 11:13:25 +00:00
self.canvas.delete(tkinter.ALL)
2018-03-04 15:36:05 +00:00
lines = []
2018-03-03 11:13:25 +00:00
for y in range(self.height):
2018-03-05 22:13:19 +00:00
line = self.vm.system.decodestr(self.vm.memory.get_bytes(self.address+y*self.width, self.width))
2018-03-04 15:36:05 +00:00
lines.append("".join(c if c.isprintable() else " " for c in line))
for y, line in enumerate(lines):
self.canvas.create_text(4, self.fontsize*y, text=line, fill="white", font=self.monospace, anchor=tkinter.NW)
self.after(30, self.update_screen)
2018-03-03 11:13:25 +00:00
@classmethod
2018-03-05 22:13:19 +00:00
def create(cls, vm: VM, screen_addr: int, screen_width: int, screen_height: int) -> None:
viewer = cls(vm, screen_addr, screen_width, screen_height)
2018-03-03 11:13:25 +00:00
viewer.mainloop()