-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
125 lines (98 loc) · 3.88 KB
/
simulator.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
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
from scanner import scan
from utils import get_instruction_format
from memory import memory_read, memory_write, memory_dump
from Tkinter import Tk, Canvas, PhotoImage, mainloop
import argparse
def execute_r_instruction(instruction, registers):
opcode = instruction[0]
if opcode in ['add', 'sub', 'mul', 'and', 'or', 'xor', 'slt', 'seq']:
rd, rs, rt = map(eval, list(instruction)[1:4])
if opcode == 'add':
registers[rd] = registers[rs] + registers[rt]
if opcode == 'sub':
registers[rd] = registers[rs] - registers[rt]
if opcode == 'mul':
registers[rd] = registers[rs] * registers[rt]
if opcode == 'and':
registers[rd] = registers[rs] & registers[rt]
if opcode == 'or':
registers[rd] = registers[rs] | registers[rt]
if opcode == 'xor':
registers[rd] = registers[rs] ^ registers[rt]
if opcode == 'mul':
registers[rd] = registers[rs] * registers[rt]
if opcode == 'slt':
registers[rd] = registers[rs] < registers[rt]
if opcode == 'seq':
registers[rd] = 1 if registers[rs] == registers[rt] else 0
if opcode == 'mv':
rd, rs = map(eval, list(instruction)[1:3])
registers[rd] = registers[rs]
if opcode in ['sll', 'srl', 'sra']:
rd, rt, sh = map(eval, list(instruction)[1:4])
if opcode == 'sll':
registers[rd] = registers[rt] << sh
if opcode == 'srl':
registers[rd] = (registers[rt] % 0x100000000) >> sh
if opcode == 'sra':
registers[rd] = registers[rt] >> sh
def execute_i_instruction(instruction, registers):
opcode = instruction[0]
if opcode == 'sw':
memory_write(registers[3], registers[4], registers[5])
if opcode == 'lw':
registers[5] = memory_read(registers[3], registers[4])
if opcode == 'ldc':
rd, immediate = map(eval, list(instruction)[1:3])
registers[rd] = constant_memory[immediate]
if opcode == 'ldi':
rd, immediate = map(eval, list(instruction)[1:3])
registers[rd] = immediate
if opcode == 'addi':
rd, rs, immediate = map(eval, list(instruction)[1:4])
registers[rd] = registers[rs] + immediate
def start_kernel(instructions, num_threads):
for thread_id in range(num_threads):
registers = [0] * 16
registers[1] = thread_id >> 16
registers[2] = thread_id % 2**16
for instruction in instructions:
if instruction[-2] and (registers[6] & 1): # Mask
# print instruction[-1]
continue
instruction_format = get_instruction_format(instruction[0])
if instruction_format == 'i':
execute_i_instruction(instruction, registers)
if instruction_format == 'r':
execute_r_instruction(instruction, registers)
def convert_color(color):
red = (0b1111100000000000 & color) >> 11
green = (0b0000011111100000 & color) >> 5
blue = (0b0000000000011111 & color)
red = red << 3
green = green << 2
blue = blue << 3
return "#%02x%02x%02x" % (red, green, blue)
def display():
window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="#000000")
canvas.pack()
img = PhotoImage(width=WIDTH, height=HEIGHT)
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")
data = ""
for i in range(HEIGHT):
data += '{' + ' '.join(map(
convert_color,
memory_dump(i*WIDTH, i*WIDTH + WIDTH)
)) + '} '
img.put(data[:-1])
mainloop()
parser = argparse.ArgumentParser(description="Demolicious Simulator")
parser.add_argument('filename')
args = parser.parse_args()
with open(args.filename) as kernel:
WIDTH, HEIGHT = 512, 256
constant_memory = [0]*256
start_kernel(scan(kernel), WIDTH*HEIGHT)
display()
#print memory_dump(0, 100)