1 |
2 |
hmanske |
from pyparsing import *
|
2 |
|
|
|
3 |
|
|
#------------------------------------------------------------------------------------------------------
|
4 |
|
|
# CLASS ASSEMBLER
|
5 |
|
|
#------------------------------------------------------------------------------------------------------
|
6 |
|
|
class Assembler(object):
|
7 |
|
|
def __init__(self, args):
|
8 |
|
|
|
9 |
|
|
formaterList = FormaterList()
|
10 |
|
|
formaterList.registerComponent("bin", Binary_Formater)
|
11 |
|
|
formaterList.registerComponent("vhdl", VHDL_Formater)
|
12 |
|
|
formaterList.registerComponent("coe", COE_Formater)
|
13 |
|
|
formaterList.registerComponent("symboltable", SYMBOLTABLE_Formater)
|
14 |
|
|
|
15 |
|
|
if len(args) != 4:
|
16 |
|
|
print
|
17 |
|
|
print "usage: python clvpasm.py <format> <infilename> <outfilename>"
|
18 |
|
|
print " supported formats are: " + formaterList.getList()
|
19 |
|
|
sys.exit(1)
|
20 |
|
|
|
21 |
|
|
format = args[1]
|
22 |
|
|
fileNameIn = args[2]
|
23 |
|
|
fileNameOut = args[3]
|
24 |
|
|
|
25 |
|
|
formater = formaterList.getFormater(format)
|
26 |
|
|
|
27 |
|
|
if formater == None:
|
28 |
|
|
print
|
29 |
|
|
print "error: unsupported format, use one of the following: " + formaterList.getList()
|
30 |
|
|
sys.exit(1)
|
31 |
|
|
|
32 |
|
|
try:
|
33 |
|
|
fileIn = open (fileNameIn,"r")
|
34 |
|
|
programcode, symbols = Parser().parse(fileIn.readlines())
|
35 |
|
|
fileIn.close()
|
36 |
|
|
except IOError:
|
37 |
|
|
print
|
38 |
|
|
print "error: unable to open input file: " + fileNameIn
|
39 |
|
|
sys.exit(1)
|
40 |
|
|
|
41 |
|
|
try:
|
42 |
|
|
formaterList.getFormater(format).write(programcode, symbols, fileNameOut)
|
43 |
|
|
print "done"
|
44 |
|
|
except IOError:
|
45 |
|
|
print
|
46 |
|
|
print "error: unable to write output file: " + fileNameOut
|
47 |
|
|
sys.exit(1)
|
48 |
|
|
|
49 |
|
|
#------------------------------------------------------------------------------------------------------
|
50 |
|
|
# CLASS FORMATERLIST
|
51 |
|
|
#------------------------------------------------------------------------------------------------------
|
52 |
|
|
class FormaterList(object):
|
53 |
|
|
def __init__(self):
|
54 |
|
|
self.components = []
|
55 |
|
|
|
56 |
|
|
def getFormater(self, format):
|
57 |
|
|
for a in self.components:
|
58 |
|
|
if format.lower().lstrip().startswith(a[0].lower()):
|
59 |
|
|
return a[1]()
|
60 |
|
|
|
61 |
|
|
return None;
|
62 |
|
|
|
63 |
|
|
def getList(self):
|
64 |
|
|
cmdlist = []
|
65 |
|
|
for a in self.components:
|
66 |
|
|
cmdlist.append(a[0])
|
67 |
|
|
|
68 |
|
|
return str(cmdlist)
|
69 |
|
|
|
70 |
|
|
def registerComponent(self,format,component):
|
71 |
|
|
self.components.append([format,component])
|
72 |
|
|
|
73 |
|
|
#------------------------------------------------------------------------------------------------------
|
74 |
|
|
# CLASS VHDL_FORMATER
|
75 |
|
|
#------------------------------------------------------------------------------------------------------
|
76 |
|
|
class VHDL_Formater(object):
|
77 |
|
|
def write(self, code, symbols, filename):
|
78 |
|
|
|
79 |
|
|
#get last adress
|
80 |
|
|
list = sorted(code.iteritems())
|
81 |
|
|
list.reverse()
|
82 |
|
|
end = list[0][0]
|
83 |
|
|
|
84 |
|
|
f=open(filename,"w")
|
85 |
|
|
|
86 |
|
|
f.write("-- *****************************************\n")
|
87 |
|
|
f.write("-- * SRAM FILE GENERATED BY CLVP ASSEMBLER *\n")
|
88 |
|
|
f.write("-- * do not make modifications here *\n")
|
89 |
|
|
f.write("-- *****************************************\n\n")
|
90 |
|
|
f.write("library ieee;\nuse ieee.std_logic_1164.all;\nuse ieee.std_logic_unsigned.all;\n")
|
91 |
|
|
f.write("entity sram is\n\tport (\n\t\tclk : in std_logic;\n\t\twe : in std_logic;\n\t\ten : in std_logic;\n")
|
92 |
|
|
f.write("\t\taddr : in std_logic_vector(31 downto 0);\n\t\tdi : in std_logic_vector(31 downto 0);\n")
|
93 |
|
|
f.write("\t\tdo : out std_logic_vector(31 downto 0)\n\t);\nend sram;\n")
|
94 |
|
|
f.write("architecture rtl of sram is\n")
|
95 |
|
|
f.write("\ttype memory_type is array(0 to " + str(end+1) + ") of std_logic_vector(31 downto 0);\n")
|
96 |
|
|
f.write("\tsignal memory : memory_type := (\n")
|
97 |
|
|
|
98 |
|
|
for i in range(0,end+1):
|
99 |
|
|
if code.has_key(i):
|
100 |
|
|
f.write('\t\t"' + code[i][0] + '", -- ' + hex(i) + ' : ' +code[i][1] +'\n')
|
101 |
|
|
else:
|
102 |
|
|
f.write('\t\t"00000000000000000000000000000000", -- ' + hex(i)+'\n')
|
103 |
|
|
|
104 |
|
|
f.write('\t\t"00000000000000000000000000000000"'+'\n')
|
105 |
|
|
f.write('\t);'+'\n')
|
106 |
|
|
|
107 |
|
|
f.write("begin\n\tprocess (clk)\n\tbegin\n\t\tif clk'event and clk = '1' then\n\t\t\tif en = '1' then\n")
|
108 |
|
|
f.write("\t\t\t\tif we = '1' then\n\t\t\t\t\tmemory(conv_integer(addr)) <= di;\n\t\t\t\t\tdo <= di;\n")
|
109 |
|
|
f.write("\t\t\t\telse\n\t\t\t\t\tdo <= memory(conv_integer(addr));\n\t\t\t\tend if;\n\t\t\tend if;\n\t\tend if;\n")
|
110 |
|
|
f.write("\tend process;\nend;\n")
|
111 |
|
|
|
112 |
|
|
f.close()
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
#------------------------------------------------------------------------------------------------------
|
116 |
|
|
# CLASS BINARY_FORMATER
|
117 |
|
|
#------------------------------------------------------------------------------------------------------
|
118 |
|
|
class Binary_Formater(object):
|
119 |
|
|
def write(self, code, symbols, filename):
|
120 |
|
|
|
121 |
|
|
#get last adress
|
122 |
|
|
list = sorted(code.iteritems())
|
123 |
|
|
list.reverse()
|
124 |
|
|
end = list[0][0]
|
125 |
|
|
|
126 |
|
|
f=open(filename,"wb")
|
127 |
|
|
|
128 |
|
|
for i in range(0,end+1):
|
129 |
|
|
if code.has_key(i):
|
130 |
|
|
value = string.atoi(code[i][0],2)
|
131 |
|
|
m = string.atoi("11111111000000000000000000000000",2)
|
132 |
|
|
for j in range(4):
|
133 |
|
|
a = (value & m) >> 8 * (3-j)
|
134 |
|
|
m = m >> 8
|
135 |
|
|
f.write(chr(a))
|
136 |
|
|
else:
|
137 |
|
|
f.write(chr(0) + chr(0) + chr(0) + chr(0))
|
138 |
|
|
f.close()
|
139 |
|
|
|
140 |
|
|
#------------------------------------------------------------------------------------------------------
|
141 |
|
|
# CLASS COE_FORMATER
|
142 |
|
|
#------------------------------------------------------------------------------------------------------
|
143 |
|
|
class COE_Formater(object):
|
144 |
|
|
def write(self, code, symbols, filename):
|
145 |
|
|
|
146 |
|
|
#get last adress
|
147 |
|
|
list = sorted(code.iteritems())
|
148 |
|
|
list.reverse()
|
149 |
|
|
end = list[0][0]
|
150 |
|
|
|
151 |
|
|
f=open(filename,"w")
|
152 |
|
|
f.write("; COE FILE FOR XILINX CORE GENERATOR\n")
|
153 |
|
|
f.write("; depth=" + str(end + 1) + ", and width=32\n")
|
154 |
|
|
f.write(";\n")
|
155 |
|
|
f.write("memory_initialization_radix=2;\n")
|
156 |
|
|
f.write("memory_initialization_vector=\n")
|
157 |
|
|
|
158 |
|
|
for i in range(0,end+1):
|
159 |
|
|
if code.has_key(i):
|
160 |
|
|
f.write(code[i][0] + ",\n")
|
161 |
|
|
else:
|
162 |
|
|
f.write("00000000000000000000000000000000,\n")
|
163 |
|
|
|
164 |
|
|
f.write("00000000000000000000000000000000;\n")
|
165 |
|
|
|
166 |
|
|
f.close()
|
167 |
|
|
|
168 |
|
|
#------------------------------------------------------------------------------------------------------
|
169 |
|
|
# CLASS SYMBOLTABLE_FORMATER
|
170 |
|
|
#------------------------------------------------------------------------------------------------------
|
171 |
|
|
class SYMBOLTABLE_Formater(object):
|
172 |
|
|
def write(self, code, symbols, filename):
|
173 |
|
|
|
174 |
|
|
f=open(filename,"w")
|
175 |
|
|
|
176 |
|
|
for sym, addr in sorted(symbols.iteritems()):
|
177 |
|
|
f.write(str(addr) + ":" + str(sym) + "\n")
|
178 |
|
|
|
179 |
|
|
f.close()
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
#------------------------------------------------------------------------------------------------------
|
183 |
|
|
# CLASS INSTRUCTION
|
184 |
|
|
#------------------------------------------------------------------------------------------------------
|
185 |
|
|
class Instruction(object):
|
186 |
|
|
def __init__(self):
|
187 |
|
|
self.bincode = []
|
188 |
|
|
for i in range(32):
|
189 |
|
|
self.bincode.append("0")
|
190 |
|
|
|
191 |
|
|
def revert(self,value):
|
192 |
|
|
result = ""
|
193 |
|
|
for i in range(len(value),0,-1):
|
194 |
|
|
result = result + value[i-1]
|
195 |
|
|
return result
|
196 |
|
|
|
197 |
|
|
def insert(self, start, end, value):
|
198 |
|
|
for i in range (start+1, end, -1):
|
199 |
|
|
self.bincode[i-1] = self.revert(value)[i-1-end]
|
200 |
|
|
|
201 |
|
|
def tostring(self):
|
202 |
|
|
result = ""
|
203 |
|
|
for i in range(32,0, -1):
|
204 |
|
|
result = result + self.bincode[i-1]
|
205 |
|
|
return result
|
206 |
|
|
|
207 |
|
|
def tobinary(self,n):
|
208 |
|
|
b = ""
|
209 |
|
|
if n == 0:
|
210 |
|
|
b= "0"
|
211 |
|
|
else:
|
212 |
|
|
while n > 0:
|
213 |
|
|
b = str(n % 2) + b
|
214 |
|
|
n = n >> 1
|
215 |
|
|
return b
|
216 |
|
|
|
217 |
|
|
def insertImmediate(self, n):
|
218 |
|
|
self.insert(15,0,self.tobinary(n).rjust(16,"0"))
|
219 |
|
|
|
220 |
|
|
def insertConstant(self, n):
|
221 |
|
|
self.insert(31,0,self.tobinary(n).rjust(32,"0"))
|
222 |
|
|
|
223 |
|
|
def insertR(self,r):
|
224 |
|
|
self.insert(11,8,self.tobinary(r).rjust(4,"0"))
|
225 |
|
|
|
226 |
|
|
def insertV(self,v):
|
227 |
|
|
self.insert(7,4,self.tobinary(v).rjust(4,"0"))
|
228 |
|
|
|
229 |
|
|
def insertW(self,w):
|
230 |
|
|
self.insert(3,0,self.tobinary(w).rjust(4,"0"))
|
231 |
|
|
|
232 |
|
|
def insertVectorImmediate(self,n):
|
233 |
|
|
self.insert(27,20,self.tobinary(n).rjust(8,"0"))
|
234 |
|
|
|
235 |
|
|
#------------------------------------------------------------------------------------------------------
|
236 |
|
|
# CLASS PARSER
|
237 |
|
|
#------------------------------------------------------------------------------------------------------
|
238 |
|
|
class Parser:
|
239 |
|
|
def tointeger(self,s):
|
240 |
|
|
if s[0] == "$":
|
241 |
|
|
return string.atoi(s[1:], base = 16)
|
242 |
|
|
|
243 |
|
|
if s[0] == "%":
|
244 |
|
|
return string.atoi(s[1:], base = 2)
|
245 |
|
|
|
246 |
|
|
if s[0] == "R":
|
247 |
|
|
return string.atoi(s[1:])
|
248 |
|
|
|
249 |
|
|
return string.atoi(s)
|
250 |
|
|
|
251 |
|
|
def parse(self, program):
|
252 |
|
|
|
253 |
|
|
#create parsed commandlist
|
254 |
|
|
errorcounter = 0
|
255 |
|
|
commands = []
|
256 |
|
|
linenr = 0
|
257 |
|
|
|
258 |
|
|
for line in program:
|
259 |
|
|
linenr = linenr + 1
|
260 |
|
|
line = line.strip().upper()
|
261 |
|
|
|
262 |
|
|
try:
|
263 |
|
|
result = LineParser().parseLine(line)
|
264 |
|
|
if result.dcommand or result.vcommand or result.command or result.ccommand:
|
265 |
|
|
commands.append((result, linenr))
|
266 |
|
|
|
267 |
|
|
except ParseException, err:
|
268 |
|
|
print
|
269 |
|
|
print "conflict in line " + str(linenr) + ":"
|
270 |
|
|
print line
|
271 |
|
|
print " "*err.loc + "^ " + "parse error"
|
272 |
|
|
errorcounter = errorcounter + 1
|
273 |
|
|
|
274 |
|
|
#create symbol table, assign adresses, detect conflicts
|
275 |
|
|
symbols = {}
|
276 |
|
|
program = {}
|
277 |
|
|
|
278 |
|
|
address = 0
|
279 |
|
|
|
280 |
|
|
for cmd, linenr in commands:
|
281 |
|
|
if cmd.dcommand == "ORG":
|
282 |
|
|
address = self.tointeger(cmd.address)
|
283 |
|
|
|
284 |
|
|
if cmd.dcommand == "EQU":
|
285 |
|
|
if symbols.has_key(cmd.symbol):
|
286 |
|
|
print
|
287 |
|
|
print "conflict in line " + str(linenr) + ":"
|
288 |
|
|
print "=> symbol " + cmd.symbol + " has already been defined"
|
289 |
|
|
errorcounter = errorcounter + 1
|
290 |
|
|
else:
|
291 |
|
|
symbols[cmd.symbol] = self.tointeger(cmd.value)
|
292 |
|
|
|
293 |
|
|
if cmd.label:
|
294 |
|
|
if symbols.has_key(cmd.label[0]):
|
295 |
|
|
print
|
296 |
|
|
print "conflict in line " + str(linenr) + ":"
|
297 |
|
|
print "=> symbol " + cmd.label[0] + " has already been defined"
|
298 |
|
|
errorcounter = errorcounter + 1
|
299 |
|
|
else:
|
300 |
|
|
symbols[cmd.label[0]] = address
|
301 |
|
|
|
302 |
|
|
if cmd.dcommand != "ORG" and cmd.dcommand != "EQU":
|
303 |
|
|
if program.has_key(address):
|
304 |
|
|
print
|
305 |
|
|
print "conflict in line " + str(linenr) + ":"
|
306 |
|
|
print "=> memory address " + hex(address) + " is already in use"
|
307 |
|
|
errorcounter = errorcounter + 1
|
308 |
|
|
|
309 |
|
|
program [address] = (cmd, linenr)
|
310 |
|
|
address = address + 1
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
#translate into machine program
|
314 |
|
|
code = {}
|
315 |
|
|
|
316 |
|
|
for addr, prg in sorted(program.iteritems()):
|
317 |
|
|
cmd = prg[0]
|
318 |
|
|
linenr = prg[1]
|
319 |
|
|
|
320 |
|
|
if cmd.command and cmd.vcommand and cmd.immediate:
|
321 |
|
|
print
|
322 |
|
|
print "conflict in line " + str(linenr) + ":"
|
323 |
|
|
print "=> you can't combine " + cmd.command + " (with immediate value) and " + cmd.vcommand
|
324 |
|
|
errorcounter = errorcounter + 1
|
325 |
|
|
|
326 |
|
|
immediate = 0
|
327 |
|
|
|
328 |
|
|
if cmd.immediate:
|
329 |
|
|
try:
|
330 |
|
|
if cmd.value:
|
331 |
|
|
immediate = self.tointeger(cmd.value)
|
332 |
|
|
else:
|
333 |
|
|
immediate = symbols[cmd.symbol]
|
334 |
|
|
except KeyError:
|
335 |
|
|
print
|
336 |
|
|
print "conflict in line " + str(linenr) + ":"
|
337 |
|
|
print "=> symbol " + cmd.symbol + " not declared"
|
338 |
|
|
errorcounter = errorcounter + 1
|
339 |
|
|
|
340 |
|
|
if immediate > 65535:
|
341 |
|
|
print
|
342 |
|
|
print "conflict in line " + str(linenr) + ":"
|
343 |
|
|
print "=> immediate value " + cmd.immediate +" (" + hex(immediate) + ") is out of range (max = 0xFFFF)"
|
344 |
|
|
errorcounter = errorcounter + 1
|
345 |
|
|
|
346 |
|
|
if cmd.rvector_register:
|
347 |
|
|
if self.tointeger(cmd.rvector_register) > Settings().getN()-1:
|
348 |
|
|
print
|
349 |
|
|
print "conflict in line " + str(linenr) + ":"
|
350 |
|
|
print "=> vector register (" + cmd.rvector_register + ") is out of range (R0..R"+ str(Settings().getN()-1) + ")"
|
351 |
|
|
errorcounter = errorcounter + 1
|
352 |
|
|
|
353 |
|
|
if cmd.vvector_register:
|
354 |
|
|
if self.tointeger(cmd.vvector_register) > Settings().getN()-1:
|
355 |
|
|
print
|
356 |
|
|
print "conflict in line " + str(linenr) + ":"
|
357 |
|
|
print "=> vector register (" + cmd.vvector_register + ") is out of range (R0..R"+ str(Settings().getN()-1) + ")"
|
358 |
|
|
errorcounter = errorcounter + 1
|
359 |
|
|
|
360 |
|
|
if cmd.wvector_register:
|
361 |
|
|
if self.tointeger(cmd.wvector_register) > Settings().getN()-1:
|
362 |
|
|
print
|
363 |
|
|
print "conflict in line " + str(linenr) + ":"
|
364 |
|
|
print "=> vector register (" + cmd.wvector_register + ") is out of range (R0..R"+ str(Settings().getN()-1) + ")"
|
365 |
|
|
errorcounter = errorcounter + 1
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
i = Instruction()
|
369 |
|
|
register = {"0":"00","A":"01","X":"10","Y":"11"}
|
370 |
|
|
wordlength = {"B":"00","W":"01","DW":"10","QW":"11"}
|
371 |
|
|
|
372 |
|
|
alucommand = False
|
373 |
|
|
jumpcommand = False
|
374 |
|
|
valucommand = False
|
375 |
|
|
immediate_possible = True
|
376 |
|
|
|
377 |
|
|
#directive commands
|
378 |
|
|
if cmd.dcommand == "DC":
|
379 |
|
|
i.insertConstant(self.tointeger(cmd.address))
|
380 |
|
|
|
381 |
|
|
#scalar commands
|
382 |
|
|
if cmd.command == "ADD":
|
383 |
|
|
alucommand = True
|
384 |
|
|
i.insert(29,26,"0000")
|
385 |
|
|
|
386 |
|
|
if cmd.command == "ADC":
|
387 |
|
|
alucommand = True
|
388 |
|
|
i.insert(29,26,"0001")
|
389 |
|
|
|
390 |
|
|
if cmd.command == "SUB":
|
391 |
|
|
alucommand = True
|
392 |
|
|
i.insert(29,26,"0010")
|
393 |
|
|
|
394 |
|
|
if cmd.command == "SBC":
|
395 |
|
|
alucommand = True
|
396 |
|
|
i.insert(29,26,"0011")
|
397 |
|
|
|
398 |
|
|
if cmd.command == "AND":
|
399 |
|
|
alucommand = True
|
400 |
|
|
i.insert(29,26,"1000")
|
401 |
|
|
|
402 |
|
|
if cmd.command == "OR":
|
403 |
|
|
alucommand = True
|
404 |
|
|
i.insert(29,26,"1001")
|
405 |
|
|
|
406 |
|
|
if cmd.command == "XOR":
|
407 |
|
|
alucommand = True
|
408 |
|
|
i.insert(29,26,"1010")
|
409 |
|
|
|
410 |
|
|
if cmd.command == "MUL":
|
411 |
|
|
alucommand = True
|
412 |
|
|
i.insert(29,26,"1011")
|
413 |
|
|
|
414 |
|
|
if cmd.command == "INC":
|
415 |
|
|
alucommand = True
|
416 |
|
|
i.insert(29,26,"0100")
|
417 |
|
|
|
418 |
|
|
if cmd.command == "DEC":
|
419 |
|
|
alucommand = True
|
420 |
|
|
i.insert(29,26,"0110")
|
421 |
|
|
|
422 |
|
|
if cmd.command == "LSL":
|
423 |
|
|
alucommand = True
|
424 |
|
|
i.insert(29,26,"1100")
|
425 |
|
|
|
426 |
|
|
if cmd.command == "LSR":
|
427 |
|
|
alucommand = True
|
428 |
|
|
i.insert(29,26,"1110")
|
429 |
|
|
|
430 |
|
|
if cmd.command == "ROL":
|
431 |
|
|
alucommand = True
|
432 |
|
|
i.insert(29,26,"1101")
|
433 |
|
|
|
434 |
|
|
if cmd.command == "ROR":
|
435 |
|
|
alucommand = True
|
436 |
|
|
i.insert(29,26,"1111")
|
437 |
|
|
|
438 |
|
|
if cmd.command == "LD":
|
439 |
|
|
i.insert(31,28,"1000")
|
440 |
|
|
i.insert(25,24,register[cmd.dregister])
|
441 |
|
|
i.insert(23,22,register[cmd.sregister])
|
442 |
|
|
immediate_possible = True
|
443 |
|
|
|
444 |
|
|
if cmd.command == "ST":
|
445 |
|
|
i.insert(31,28,"1010")
|
446 |
|
|
i.insert(23,22,register[cmd.sregister])
|
447 |
|
|
immediate_possible = True
|
448 |
|
|
|
449 |
|
|
if cmd.command == "HALT":
|
450 |
|
|
i.insert(31,27,"00101")
|
451 |
|
|
|
452 |
|
|
if cmd.command == "NOP":
|
453 |
|
|
i.insert(31,29,"000")
|
454 |
|
|
|
455 |
|
|
if cmd.command == "JMP":
|
456 |
|
|
i.insert(31,24,"00100000")
|
457 |
|
|
jumpcommand = True
|
458 |
|
|
|
459 |
|
|
if cmd.command == "JAL":
|
460 |
|
|
i.insert(31,26,"001000")
|
461 |
|
|
i.insert(25,24,register[cmd.dregister])
|
462 |
|
|
jumpcommand = True
|
463 |
|
|
|
464 |
|
|
if cmd.command == "JNC":
|
465 |
|
|
i.insert(31,24,"00110000")
|
466 |
|
|
jumpcommand = True
|
467 |
|
|
|
468 |
|
|
if cmd.command == "JC":
|
469 |
|
|
i.insert(31,24,"00110100")
|
470 |
|
|
jumpcommand = True
|
471 |
|
|
|
472 |
|
|
if cmd.command == "JNZ":
|
473 |
|
|
i.insert(31,24,"00111000")
|
474 |
|
|
jumpcommand = True
|
475 |
|
|
|
476 |
|
|
if cmd.command == "JZ":
|
477 |
|
|
i.insert(31,24,"00111100")
|
478 |
|
|
jumpcommand = True
|
479 |
|
|
|
480 |
|
|
if cmd.command == "CLC":
|
481 |
|
|
i.insert(31,26,"110000")
|
482 |
|
|
i.insert(23,20,"0100")
|
483 |
|
|
|
484 |
|
|
if cmd.command == "CLZ":
|
485 |
|
|
i.insert(31,26,"110000")
|
486 |
|
|
i.insert(23,20,"1000")
|
487 |
|
|
|
488 |
|
|
if cmd.command == "SEC":
|
489 |
|
|
i.insert(31,26,"110000")
|
490 |
|
|
i.insert(23,20,"0101")
|
491 |
|
|
|
492 |
|
|
if cmd.command == "SEZ":
|
493 |
|
|
i.insert(31,26,"110000")
|
494 |
|
|
i.insert(23,20,"1010")
|
495 |
|
|
|
496 |
|
|
#common parts of scalar commands
|
497 |
|
|
if alucommand == True:
|
498 |
|
|
i.insert(31,30,"01");
|
499 |
|
|
i.insert(25,24,register[cmd.dregister])
|
500 |
|
|
i.insert(23,22,register[cmd.sregister])
|
501 |
|
|
immediate_possible = True
|
502 |
|
|
|
503 |
|
|
if jumpcommand == True:
|
504 |
|
|
i.insert(23,22,register[cmd.sregister])
|
505 |
|
|
immediate_possible = True
|
506 |
|
|
|
507 |
|
|
if immediate_possible == True:
|
508 |
|
|
if(cmd.immediate):
|
509 |
|
|
i.insert(21,20,"00")
|
510 |
|
|
i.insert(19,17,"000")
|
511 |
|
|
i.insertImmediate(immediate)
|
512 |
|
|
else:
|
513 |
|
|
if cmd.tregister:
|
514 |
|
|
i.insert(21,20,register[cmd.tregister])
|
515 |
|
|
|
516 |
|
|
#vector commands
|
517 |
|
|
if cmd.vcommand == "VADD":
|
518 |
|
|
i.insert(15,12,"0000")
|
519 |
|
|
valucommand = True
|
520 |
|
|
|
521 |
|
|
if cmd.vcommand == "VSUB":
|
522 |
|
|
i.insert(15,12,"0010")
|
523 |
|
|
valucommand = True
|
524 |
|
|
|
525 |
|
|
if cmd.vcommand == "VAND":
|
526 |
|
|
i.insert(15,12,"1000")
|
527 |
|
|
valucommand = True
|
528 |
|
|
|
529 |
|
|
if cmd.vcommand == "VOR":
|
530 |
|
|
i.insert(15,12,"1001")
|
531 |
|
|
valucommand = True
|
532 |
|
|
|
533 |
|
|
if cmd.vcommand == "VMUL":
|
534 |
|
|
i.insert(15,12,"1011")
|
535 |
|
|
valucommand = True
|
536 |
|
|
|
537 |
|
|
if cmd.vcommand == "VXOR":
|
538 |
|
|
i.insert(15,12,"1010")
|
539 |
|
|
valucommand = True
|
540 |
|
|
|
541 |
|
|
if cmd.vcommand == "VLSL":
|
542 |
|
|
i.insert(15,12,"1100")
|
543 |
|
|
valucommand = True
|
544 |
|
|
|
545 |
|
|
if cmd.vcommand == "VLSR":
|
546 |
|
|
i.insert(15,12,"1110")
|
547 |
|
|
valucommand = True
|
548 |
|
|
|
549 |
|
|
if cmd.vcommand == "VNOP":
|
550 |
|
|
i.insert(19,17,"000")
|
551 |
|
|
|
552 |
|
|
if cmd.vcommand == "VMOL":
|
553 |
|
|
i.insert(19,17,"001")
|
554 |
|
|
i.insert(15,12,"1000")
|
555 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
556 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
557 |
|
|
|
558 |
|
|
if cmd.vcommand == "VMOR":
|
559 |
|
|
i.insert(19,17,"001")
|
560 |
|
|
i.insert(15,12,"1100")
|
561 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
562 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
563 |
|
|
|
564 |
|
|
if cmd.vcommand == "VMOV":
|
565 |
|
|
i.insert(19,17,"001")
|
566 |
|
|
|
567 |
|
|
if cmd.rvector_register and cmd.vvector_register: #vmov r,v
|
568 |
|
|
i.insert(15,12,"0001")
|
569 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
570 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
571 |
|
|
|
572 |
|
|
if cmd.immediate:
|
573 |
|
|
if immediate > 255:
|
574 |
|
|
print
|
575 |
|
|
print "conflict in line " + str(linenr) + ":"
|
576 |
|
|
print "=> immediate value " + cmd.immediate +" (" + hex(immediate) + ") is out of range (max = 0xFF)"
|
577 |
|
|
errorcounter = errorcounter + 1
|
578 |
|
|
else:
|
579 |
|
|
if cmd.rvector_register: #vmov r, r<n>
|
580 |
|
|
i.insertVectorImmediate(immediate)
|
581 |
|
|
i.insert(15,12,"0010")
|
582 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
583 |
|
|
|
584 |
|
|
if cmd.vvector_register: #vmov r<n>,r
|
585 |
|
|
i.insertVectorImmediate(immediate)
|
586 |
|
|
i.insert(15,12,"0011")
|
587 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
588 |
|
|
|
589 |
|
|
#common parts of vector commands
|
590 |
|
|
if valucommand == True:
|
591 |
|
|
i.insert(19,18,"01")
|
592 |
|
|
i.insert(17,16,wordlength[cmd.width])
|
593 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
594 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
595 |
|
|
|
596 |
|
|
if cmd.wvector_register:
|
597 |
|
|
i.insertW(self.tointeger(cmd.wvector_register))
|
598 |
|
|
|
599 |
|
|
#cooperative commands
|
600 |
|
|
if cmd.ccommand == "VLD":
|
601 |
|
|
i.insert(31,28,"1001")
|
602 |
|
|
i.insert(23,22,register[cmd.sregister])
|
603 |
|
|
i.insert(21,20,register[cmd.tregister])
|
604 |
|
|
i.insert(19,18,"10")
|
605 |
|
|
i.insert(15,12,"0010")
|
606 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
607 |
|
|
|
608 |
|
|
if cmd.ccommand == "VST":
|
609 |
|
|
i.insert(31,26,"101100")
|
610 |
|
|
i.insert(23,22,register[cmd.sregister])
|
611 |
|
|
i.insert(21,20,register[cmd.tregister])
|
612 |
|
|
i.insert(19,18,"10")
|
613 |
|
|
i.insert(15,12,"0011")
|
614 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
615 |
|
|
|
616 |
|
|
if cmd.ccommand == "MOV":
|
617 |
|
|
if cmd.rvector_register and cmd.tregister and cmd.sregister: #mov r(t),s
|
618 |
|
|
i.insert(31,26,"101110")
|
619 |
|
|
i.insert(23,22,register[cmd.sregister])
|
620 |
|
|
i.insert(21,20,register[cmd.tregister])
|
621 |
|
|
i.insert(19,18,"10")
|
622 |
|
|
i.insert(15,12,"0100")
|
623 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
624 |
|
|
|
625 |
|
|
if cmd.vvector_register and cmd.tregister and cmd.dregister: #mov d,v(t)
|
626 |
|
|
i.insert(31,26,"101111")
|
627 |
|
|
i.insert(25,24,register[cmd.dregister])
|
628 |
|
|
i.insert(21,20,register[cmd.tregister])
|
629 |
|
|
i.insert(19,18,"10")
|
630 |
|
|
i.insert(15,12,"0101")
|
631 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
632 |
|
|
|
633 |
|
|
if cmd.ccommand == "MOVA":
|
634 |
|
|
i.insert(31,26,"101101")
|
635 |
|
|
i.insert(23,22,register[cmd.sregister])
|
636 |
|
|
i.insert(19,18,"10")
|
637 |
|
|
i.insert(15,12,"0110")
|
638 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
639 |
|
|
|
640 |
|
|
if cmd.ccommand == "VSHUF":
|
641 |
|
|
i.insert(31,29,"000")
|
642 |
|
|
i.insert(19,18,"11")
|
643 |
|
|
i.insert(17,16,cmd.perm[0:2])
|
644 |
|
|
i.insert(15,12,cmd.perm[2:6])
|
645 |
|
|
i.insert(27,20,cmd.perm[6:14])
|
646 |
|
|
i.insertR(self.tointeger(cmd.rvector_register))
|
647 |
|
|
i.insertV(self.tointeger(cmd.vvector_register))
|
648 |
|
|
i.insertW(self.tointeger(cmd.wvector_register))
|
649 |
|
|
|
650 |
|
|
code [addr] = (i.tostring(), string.join(cmd))
|
651 |
|
|
|
652 |
|
|
if errorcounter > 0:
|
653 |
|
|
print
|
654 |
|
|
print str(errorcounter) + " error(s) found => output has not been written"
|
655 |
|
|
sys.exit(1)
|
656 |
|
|
else:
|
657 |
|
|
return code, symbols
|
658 |
|
|
|
659 |
|
|
#------------------------------------------------------------------------------------------------------
|
660 |
|
|
# CLASS LINEPARSER
|
661 |
|
|
#------------------------------------------------------------------------------------------------------
|
662 |
|
|
class LineParser:
|
663 |
|
|
def parseLine(self,line):
|
664 |
|
|
|
665 |
|
|
expression = Forward()
|
666 |
|
|
|
667 |
|
|
dec_value = Word("0123456789", min=1)
|
668 |
|
|
hex_value = Word("$","ABCDEF0123456789", min=2)
|
669 |
|
|
bin_value = Word("%","10", min=2)
|
670 |
|
|
|
671 |
|
|
value = (dec_value ^ hex_value ^ bin_value).setResultsName("value")
|
672 |
|
|
ident = Word( alphas, alphanums + "_" ).setResultsName("symbol")
|
673 |
|
|
|
674 |
|
|
immediate = (value ^ ident).setResultsName("immediate")
|
675 |
|
|
perm = Word("10", min=14, max=14).setResultsName("perm")
|
676 |
|
|
|
677 |
|
|
length = (Literal("B") ^ Literal("W") ^ Literal("DW") ^ Literal("QW")).setResultsName("width")
|
678 |
|
|
mult_length = (Literal("B") ^ Literal("W")).setResultsName("width")
|
679 |
|
|
|
680 |
|
|
width = ("." + length)
|
681 |
|
|
mult_width = ("." + mult_length)
|
682 |
|
|
|
683 |
|
|
label = (ident + ":").setResultsName("label")
|
684 |
|
|
|
685 |
|
|
comment = ( ";" + restOfLine).setResultsName("comment")
|
686 |
|
|
|
687 |
|
|
dregister = (Literal("A") ^ Literal("X") ^ Literal("Y") ^ Literal("0")).setResultsName("dregister")
|
688 |
|
|
sregister = (Literal("A") ^ Literal("X") ^ Literal("Y") ^ Literal("0")).setResultsName("sregister")
|
689 |
|
|
tregister = (Literal("A") ^ Literal("X") ^ Literal("Y")).setResultsName("tregister")
|
690 |
|
|
akku = Literal("A")
|
691 |
|
|
|
692 |
|
|
rvector_register = Word("R","0123456789",min=2).setResultsName("rvector_register")
|
693 |
|
|
vvector_register = Word("R","0123456789",min=2).setResultsName("vvector_register")
|
694 |
|
|
wvector_register = Word("R","0123456789",min=2).setResultsName("wvector_register")
|
695 |
|
|
|
696 |
|
|
|
697 |
|
|
directive_cmd_keyword_1 = (Keyword("ORG") ^ Keyword("DC")).setResultsName("dcommand")
|
698 |
|
|
directive_cmd_keyword_2 = (Keyword("EQU")).setResultsName("dcommand")
|
699 |
|
|
|
700 |
|
|
directive_cmd_1 = directive_cmd_keyword_1 + value.setResultsName("address")
|
701 |
|
|
directive_cmd_2 = directive_cmd_keyword_2 + ident + value
|
702 |
|
|
|
703 |
|
|
directive_cmd = (directive_cmd_1 ^ directive_cmd_2)
|
704 |
|
|
|
705 |
|
|
alu_cmd_keyword_1 = (Keyword("INC") ^ Keyword("DEC") ^ Keyword("LSL") ^ Keyword("LSR") ^ Keyword("ROL") \
|
706 |
|
|
^ Keyword("ROR")).setResultsName("command")
|
707 |
|
|
|
708 |
|
|
alu_cmd_keyword_2 = (Keyword("ADD") ^ Keyword("ADC") ^ Keyword("SUB") ^ Keyword("SBC") ^ Keyword("AND") \
|
709 |
|
|
^ Keyword("OR") ^ Keyword("XOR") ^ Keyword("MUL")).setResultsName("command")
|
710 |
|
|
|
711 |
|
|
|
712 |
|
|
alu_cmd_1 = alu_cmd_keyword_1 + dregister + "," + sregister
|
713 |
|
|
alu_cmd_2 = alu_cmd_keyword_2 + dregister + "," + sregister + "," + (tregister ^ immediate)
|
714 |
|
|
alu_cmd = (alu_cmd_1 ^ alu_cmd_2)
|
715 |
|
|
|
716 |
|
|
jmp_cmd_keyword_1 = (Keyword("JMP") ^ Keyword("JNC") ^ Keyword("JC") ^ Keyword("JNZ") ^ Keyword("JZ")) \
|
717 |
|
|
.setResultsName("command")
|
718 |
|
|
jmp_cmd_keyword_2 = Keyword("JAL").setResultsName("command")
|
719 |
|
|
|
720 |
|
|
jmp_cmd_1 = jmp_cmd_keyword_1 + Optional("[") + sregister + "+" + (tregister ^ immediate) + Optional("]")
|
721 |
|
|
jmp_cmd_2 = jmp_cmd_keyword_2 + dregister + "," + Optional("[") + sregister + "+" + (tregister ^ immediate) + Optional("]")
|
722 |
|
|
jmp_cmd = (jmp_cmd_1 ^ jmp_cmd_2)
|
723 |
|
|
|
724 |
|
|
inherent_cmd_keyword = (Keyword ("HALT") ^ Keyword ("NOP") ^ Keyword ("CLC") ^ Keyword ("CLZ") ^ Keyword ("SEC") \
|
725 |
|
|
^ Keyword ("SEZ")).setResultsName("command")
|
726 |
|
|
|
727 |
|
|
inherent_cmd = inherent_cmd_keyword
|
728 |
|
|
|
729 |
|
|
load_cmd_keyword = Keyword("LD").setResultsName("command")
|
730 |
|
|
load_cmd = load_cmd_keyword + dregister + "," + Optional("[") + sregister + "+" + (tregister ^ immediate) + Optional("]")
|
731 |
|
|
|
732 |
|
|
store_cmd_keyword = Keyword("ST").setResultsName("command")
|
733 |
|
|
store_cmd = store_cmd_keyword + Optional("[") + sregister + "+" + (tregister ^ immediate) + Optional("]") +"," + akku
|
734 |
|
|
|
735 |
|
|
scalar_command = (alu_cmd ^ jmp_cmd ^ inherent_cmd ^ load_cmd ^ store_cmd)
|
736 |
|
|
|
737 |
|
|
mov_cmd_keyword = Keyword("MOV").setResultsName("ccommand")
|
738 |
|
|
mov_cmd_1 = mov_cmd_keyword + rvector_register + "(" + tregister + ")" + "," + sregister
|
739 |
|
|
mov_cmd_2 = mov_cmd_keyword + dregister + "," + vvector_register + "(" + tregister + ")"
|
740 |
|
|
mov_cmd = (mov_cmd_1 ^ mov_cmd_2)
|
741 |
|
|
|
742 |
|
|
vld_cmd_keyword = Keyword("VLD").setResultsName("ccommand")
|
743 |
|
|
vld_cmd = vld_cmd_keyword + rvector_register + "," + Optional("[") + sregister + "+" + tregister + Optional("]")
|
744 |
|
|
|
745 |
|
|
vst_cmd_keyword = Keyword("VST").setResultsName("ccommand")
|
746 |
|
|
vst_cmd = vst_cmd_keyword + Optional("[") + sregister + "+" + tregister + Optional("]") + "," + vvector_register
|
747 |
|
|
|
748 |
|
|
shuffle_cmd_keyword = Keyword("VSHUF").setResultsName("ccommand")
|
749 |
|
|
shuffle_cmd = shuffle_cmd_keyword + rvector_register + "," + vvector_register + "," + wvector_register + "," + perm
|
750 |
|
|
|
751 |
|
|
mova_cmd_keyword = Keyword("MOVA").setResultsName("ccommand")
|
752 |
|
|
mova_cmd = mova_cmd_keyword + rvector_register + "," + sregister
|
753 |
|
|
|
754 |
|
|
coop_command = (mov_cmd ^ vld_cmd ^ vst_cmd ^ shuffle_cmd ^ mova_cmd)
|
755 |
|
|
|
756 |
|
|
valu_cmd_keyword_1 = (Keyword("VADD") ^ Keyword("VSUB") ^ Keyword("VAND") ^ Keyword("VOR") ^ Keyword("VXOR")) \
|
757 |
|
|
.setResultsName("vcommand")
|
758 |
|
|
|
759 |
|
|
valu_cmd_keyword_2 = (Keyword("VLSL") ^ Keyword("VLSR")).setResultsName("vcommand")
|
760 |
|
|
|
761 |
|
|
valu_cmd_keyword_3 = Keyword("VMUL").setResultsName("vcommand")
|
762 |
|
|
|
763 |
|
|
valu_cmd_1 = valu_cmd_keyword_1 + width + rvector_register + "," + vvector_register + "," + wvector_register
|
764 |
|
|
valu_cmd_2 = valu_cmd_keyword_2 + width + rvector_register + "," + vvector_register
|
765 |
|
|
valu_cmd_3 = valu_cmd_keyword_3 + mult_width + rvector_register + "," + vvector_register + "," + wvector_register
|
766 |
|
|
valu_cmd = (valu_cmd_1 ^ valu_cmd_2 ^ valu_cmd_3)
|
767 |
|
|
|
768 |
|
|
vnop_cmd_keyword = Keyword("VNOP").setResultsName("vcommand")
|
769 |
|
|
vnop_cmd = vnop_cmd_keyword
|
770 |
|
|
|
771 |
|
|
vmov_cmd_keyword = Keyword("VMOV").setResultsName("vcommand")
|
772 |
|
|
vmov_cmd_1 = vmov_cmd_keyword + rvector_register + "," + vvector_register
|
773 |
|
|
vmov_cmd_2 = vmov_cmd_keyword + rvector_register + "," + "R" + "<" + immediate + ">"
|
774 |
|
|
vmov_cmd_3 = vmov_cmd_keyword + "R" + "<" + immediate + ">" + "," + vvector_register
|
775 |
|
|
vmov_cmd = (vmov_cmd_1 ^ vmov_cmd_2 ^ vmov_cmd_3)
|
776 |
|
|
|
777 |
|
|
vmolr_cmd_keyword = (Keyword("VMOL") ^ Keyword("VMOR")).setResultsName("vcommand")
|
778 |
|
|
vmolr_cmd = vmolr_cmd_keyword + rvector_register + "," + vvector_register
|
779 |
|
|
|
780 |
|
|
vector_command = (valu_cmd ^ vnop_cmd ^ vmov_cmd ^ vmolr_cmd)
|
781 |
|
|
|
782 |
|
|
command = ((directive_cmd ^ scalar_command ^ coop_command ^ vector_command) \
|
783 |
|
|
^ (scalar_command + "|" + vector_command )\
|
784 |
|
|
^ (vector_command + "|" + scalar_command ))
|
785 |
|
|
|
786 |
|
|
|
787 |
|
|
expression << (Optional((Optional(label) + command)) + Optional(comment) + lineEnd)
|
788 |
|
|
result = expression.parseString(line)
|
789 |
|
|
return result
|
790 |
|
|
|
791 |
|
|
#------------------------------------------------------------------------------------------------------
|
792 |
|
|
# CLASS SETTINGS
|
793 |
|
|
#------------------------------------------------------------------------------------------------------
|
794 |
|
|
class Settings(object):
|
795 |
|
|
def getN(self):
|
796 |
|
|
return 16
|
797 |
|
|
|
798 |
|
|
def getK(self):
|
799 |
|
|
return 16
|
800 |
|
|
|
801 |
|
|
#------------------------------------------------------------------------------------------------------
|
802 |
|
|
# MAIN PROGRAM
|
803 |
|
|
#------------------------------------------------------------------------------------------------------
|
804 |
|
|
if __name__ == '__main__':
|
805 |
|
|
Assembler(sys.argv)
|
806 |
|
|
|
807 |
|
|
|