BCI-dev/test/test.py

141 lines
2.1 KiB
Python
Raw Normal View History

2018-10-27 14:54:33 +00:00
import sys
from io import StringIO
sys.path.append("../assembler")
from assembler.assembler import Assembler
mnemonic_order = [ "ldi" , "ld" , "st" , "inc" , "dec" , "add" , "sub"
, "mul" , "div" , "gt" , "ge" , "lt" , "le" , "eq" , "not"
, "jmp" , "cjmp" , "call" , "ccall" , "ret" , "cl" , "stop" ]
mnemonics = [
{
"mnemonic": "ldi"
, "args": ["register", "direct_input"]
}
, {
"mnemonic": "ld"
, "args": ["register", "register"]
}
, {
"mnemonic": "st"
, "args": ["register", "register"]
}
, {
"mnemonic": "inc"
, "args": ["register"]
}
, {
"mnemonic": "dec"
, "args": ["register"]
}
, {
"mnemonic": "add"
, "args": ["register", "register"]
}
, {
"mnemonic": "sub"
, "args": ["register", "register"]
}
, {
"mnemonic": "mul"
, "args": ["register", "register"]
}
, {
"mnemonic": "div"
, "args": ["register", "register"]
}
, {
"mnemonic": "gt"
, "args": ["register"]
}
, {
"mnemonic": "ge"
, "args": ["register"]
}
, {
"mnemonic": "lt"
, "args": ["register"]
}
, {
"mnemonic": "le"
, "args": ["register"]
}
, {
"mnemonic": "eq"
, "args": ["register"]
}
, {
"mnemonic": "not"
, "args": []
}
, {
"mnemonic": "jmp"
, "args": ["register"]
}
, {
"mnemonic": "call"
, "args": ["register"]
}
, {
"mnemonic": "ret"
, "args": []
}
, {
"mnemonic": "stop"
, "args": []
}
, {
"mnemonic": "cl"
, "args": []
}
, {
"mnemonic": "cjmp"
, "args": ["register"]
}
, {
"mnemonic": "ccall"
, "args": ["register"]
}
]
mnemonics = sorted(mnemonics, key=lambda x: mnemonic_order.index(x["mnemonic"]))
memory_definition = {
"registers": 40
, "memory": 512
, "program_memory": 2048
}
if( __name__ == "__main__"):
data = StringIO(
'''
ldi r1, 12
ldi r0, 2
ldi r2, 0xff
ldi r3, loop
loop:
add r0, r2
dec r1
gt r1
; r3 = loop
cjmp r3
stop
'''
)
assembler = Assembler(data, memory_definition, mnemonics, {})
assembler.parse()
for i in assembler._code_objects:
print(i, i&0b1111111111000000, i&0b0000000000111111)
with open("code.bin", "wb") as f:
assembler.bindump(f)