-
Notifications
You must be signed in to change notification settings - Fork 1
/
knightinstructions16.py
76 lines (58 loc) · 2.56 KB
/
knightinstructions16.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
# A derivitive port of:
# https://github.com/oriansj/stage0/blob/master/vm_instructions.c
#
# Copyright (C) 2019 Mark Jenkins <[email protected]>
# This file is part of knightpies
#
# knightpies is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# knightpies is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with knightpies. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division # prevent use of "/" in the old way
from knightinstructions import *
from knightinstructions_bit_optimized import make_nbit_optimized_functions
nbit_optimized_dict = make_nbit_optimized_functions(16)
# 1 OP
# We don't need to mess around with sign extension for 16 bit LOADI,
# when copying a signed 16 bit value stored as a 16 bit unsigned into a
# 16 bit unsigned register, we just copy the bits like generic LOADUI does
LOADI = LOADUI
TRUE = nbit_optimized_dict['TRUE_16']
def RET(vm, c):
mem, register_file, reg0, next_ip_discard = get_args_for_1OP(vm, c)
reg_size = register_file.itemsize
# Update our index
address_of_pc_on_stack = register_file[reg0] - reg_size
register_file[reg0] = address_of_pc_on_stack
# Read in the new PC
# big endian, least significant byte is most significant bits
next_ip = ( (mem[address_of_pc_on_stack]<<8) |
mem[address_of_pc_on_stack+1] )
# Clear Stack Values
mem[address_of_pc_on_stack], mem[address_of_pc_on_stack+1] = 0,0
return next_ip
# 3 OP
CMP = nbit_optimized_dict['CMP_16']
# 1 OP immediate
JUMP_P = nbit_optimized_dict['JUMP_P_16']
JUMP_NP = nbit_optimized_dict['JUMP_NP_16']
def CALLI(vm, c):
mem, register_file, reg0, raw_immediate, next_ip = get_args_for_1OPI(vm, c)
mem_address = register_file[reg0]
# big endian
mem[mem_address] = next_ip>>8 # most significant byte
mem[mem_address+1] = next_ip & 0xFF # least significant byte
register_file[reg0] += register_file.itemsize # Update our index
return next_ip + raw_immediate # Update PC
def SL0I(vm, c):
mem, register_file, reg0, raw_immediate, next_ip = get_args_for_1OPI(vm, c)
register_file[reg0] = (register_file[reg0]<<raw_immediate) & 0xFFFF
return next_ip