-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvm.py
More file actions
127 lines (89 loc) · 3.07 KB
/
Copy pathvm.py
File metadata and controls
127 lines (89 loc) · 3.07 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python2
#
# VM.PY
# This is the Virtual Machine itself.
# Quite Simply, it consists of memory, and a CPU.
# The loader lives in a different module, loader.py
from instructions import instruction_set, registers
from loader import loader
import operator
class Processor:
def __init__(self, filename):
self.memory = loader(filename)
self.instruction_set = {v['opcode']: k
for k,v in instruction_set.iteritems()}
self.registers = {v: k.lower() for k, v in registers.iteritems()}
[setattr(self, k, 0) for k in self.registers.values()]
while (self.do_instruction()):
pass
from pprint import pprint
pprint({reg: getattr(self, reg) for reg in self.registers.values()})
def do_instruction(self):
""" Performs a single instruction """
instruction = self.memory.get(self.ip, None)
if not instruction:
return False
instruction = self.instruction_set.get(instruction)
nbytes = instruction_set[instruction]['nbytes']
operation = getattr(self, "op_%s" % instruction.lower())
self.ip = operation(nbytes) or self.ip + nbytes
return True
def get_ops(self, nbytes):
return [self.memory[self.ip + i] for i in xrange(1, nbytes)]
def do_operation(self, nbytes, operation):
ops = self.get_ops(nbytes)
r0 = self.registers[ops[0]]
r1 = self.registers[ops[1]]
setattr(self, r0, operation(getattr(self, r0), getattr(self, r1)))
def op_add(self, nbytes):
self.do_operation(nbytes, operator.add)
def op_sub(self, nbytes):
self.do_operation(nbytes, operator.sub)
def op_and(self, nbytes):
self.do_operation(nbytes, operator.and_)
def op_or(self, nbytes):
self.do_operation(nbytes, operator.or_)
def op_xor(self, nbytes):
self.do_operation(nbytes, operator.xor)
def op_shl(self, nbytes):
self.do_operation(nbytes, operator.lshift)
def op_shr(self, nbytes):
self.do_operation(nbytes, operator.rshift)
def op_mov(self, nbytes):
ops = self.get_ops(nbytes)
r0 = self.registers[ops[0]]
r1 = self.registers[ops[1]]
setattr(self, r0, getattr(self, r1))
def op_ldm(self, nbytes):
pass
def op_ldi(self, nbytes):
ops = self.get_ops(nbytes)
r = self.registers[ops[0]]
setattr(self, r, ops[1])
def op_str(self, nbytes):
pass
def op_push(self, nbytes):
pass
def op_pop(self, nbytes):
pass
def op_inc(self, nbytes):
pass
def op_dec(self, nbytes):
pass
def op_bra(self, nbytes):
new_ip = [hex(v) for v in self.get_ops(nbytes)]
new_ip = ''.join(new_ip).replace('0x', '')
return int(new_ip)
def op_bne(self, nbytes):
pass
def op_beq(self, nbytes):
pass
def op_blt(self, nbytes):
pass
def op_ble(self, nbytes):
pass
def op_bgt(self, nbytes):
pass
def op_bge(self, nbytes):
pass
Processor('out')