Assembler sino a ADDI
This commit is contained in:
parent
c50dd9718f
commit
8ad7634be1
14
cpp/vm.cpp
14
cpp/vm.cpp
@ -55,8 +55,8 @@ uint8_t *VM::reg_name(uint8_t regvalue) {
|
||||
void VM::status(void) {
|
||||
#ifdef DBG
|
||||
uint8_t i;
|
||||
DBG_INFO(("VM Status:\n"));
|
||||
DBG_INFO(("~~~~~~~~~~\n"));
|
||||
DBG_SUCC(("VM Status:\n"));
|
||||
DBG_SUCC(("~~~~~~~~~~\n"));
|
||||
for (i = R0; i <= SP; i++) {
|
||||
switch (i) {
|
||||
case R0:
|
||||
@ -94,7 +94,7 @@ void VM::status(void) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
DBG_INFO(("~~~~~~~~~~\n"));
|
||||
DBG_SUCC(("~~~~~~~~~~\n"));
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
@ -102,13 +102,13 @@ void VM::status(void) {
|
||||
CONSTRUCTORS
|
||||
*/
|
||||
VM::VM() {
|
||||
DBG_INFO(("Creating VM without code.\n"));
|
||||
DBG_SUCC(("Creating VM without code.\n"));
|
||||
as.allocate();
|
||||
init_regs();
|
||||
}
|
||||
|
||||
VM::VM(uint8_t *code, uint32_t codesize) {
|
||||
DBG_INFO(("Creating VM with code.\n"));
|
||||
DBG_SUCC(("Creating VM with code.\n"));
|
||||
if (as.allocate()) {
|
||||
as.insCode(code, codesize);
|
||||
}
|
||||
@ -184,7 +184,7 @@ bool VM::exec_putm(void) {
|
||||
uint8_t src;
|
||||
dst = *((uint16_t *)&as.code[regs[IP] + 1]);
|
||||
src = as.code[regs[IP] + 3];
|
||||
DBG_INFO(("PUTM 0x%x 0x%x\n", dst, src));
|
||||
DBG_INFO(("PUTM 0x%x, %s\n", dst, reg_name(src)));
|
||||
*((uint16_t *)&as.data[dst]) = regs[src];
|
||||
return true;
|
||||
}
|
||||
@ -198,7 +198,7 @@ bool VM::exec_addi(void) {
|
||||
|
||||
dst = as.code[regs[IP] + 1];
|
||||
src = *((uint16_t *)&as.code[regs[IP] + 2]);
|
||||
DBG_INFO(("ADDI 0x%x 0x%x\n", dst, src));
|
||||
DBG_INFO(("ADDI %s, 0x%x\n", reg_name(dst), src));
|
||||
regs[dst] += src;
|
||||
return true;
|
||||
}
|
||||
|
@ -41,8 +41,7 @@ def to_uint8(data):
|
||||
return struct.pack("<B", int(data, 16))
|
||||
elif alphanum.match(data): # only numbers
|
||||
return struct.pack("<B", int(data))
|
||||
else:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def to_uint16(data):
|
||||
@ -53,8 +52,7 @@ def to_uint16(data):
|
||||
return struct.pack("<H", int(data, 16))
|
||||
elif alphanum.match(data): # only numbers
|
||||
return struct.pack("<H", int(data))
|
||||
else:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def is_reg(data):
|
||||
@ -63,21 +61,121 @@ def is_reg(data):
|
||||
return True
|
||||
|
||||
|
||||
def movi(op, dst, src):
|
||||
def movi(op_str, dst_str, src_str):
|
||||
global assembled
|
||||
if (is_reg(dst) and dst is not "ip"):
|
||||
op_val = to_uint8(ops[op])
|
||||
dst_val = to_uint8(regs[dst])
|
||||
src_val = to_uint16(src)
|
||||
if op_val and dst_val and src_val:
|
||||
assembled += op_val + dst_val + src_val
|
||||
if is_reg(dst_str):
|
||||
if dst_str != "ip":
|
||||
op_val = to_uint8(ops[op_str])
|
||||
dst_val = to_uint8(regs[dst_str])
|
||||
src_val = to_uint16(src_str)
|
||||
if op_val and dst_val and src_val:
|
||||
assembled += op_val + dst_val + src_val
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
sys.stderr.write("CAN'T MOVI TO IP!\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst))
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def movr(op_str, dst_str, src_str):
|
||||
global assembled
|
||||
|
||||
if is_reg(dst_str) and is_reg(src_str):
|
||||
if dst_str != "ip" and src_str != "ip":
|
||||
op_val = to_uint8(ops[op_str])
|
||||
dstsrc_val = (to_uint8(regs[dst_str])[0]
|
||||
<< 4) ^ to_uint8(regs[src_str])[0]
|
||||
if op_val and dstsrc_val:
|
||||
assembled += op_val + to_uint8(dstsrc_val)
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write("CAN'T MOVR IP!\n")
|
||||
return False
|
||||
else:
|
||||
if not is_reg(dst_str):
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(src_str))
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def getm(op_str, dst_str, src_str):
|
||||
global assembled
|
||||
if is_reg(dst_str):
|
||||
if dst_str != "ip":
|
||||
op_val = to_uint8(ops[op_str])
|
||||
dst_val = to_uint8(regs[dst_str])
|
||||
src_val = to_uint16(src_str)
|
||||
if op_val and dst_val and src_val:
|
||||
assembled += op_val + dst_val + src_val
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write("CAN'T MOVI TO IP!\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def putm(op_str, dst_str, src_str):
|
||||
global assembled
|
||||
if is_reg(src_str):
|
||||
if src_str != "ip":
|
||||
op_val = to_uint8(ops[op_str])
|
||||
src_val = to_uint8(regs[src_str])
|
||||
dst_val = to_uint16(dst_str)
|
||||
if op_val and dst_val and src_val:
|
||||
assembled += op_val + dst_val + src_val
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write("CAN'T MOVI TO IP!\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
|
||||
return False
|
||||
return True
|
||||
|
||||
def addi(op_str, dst_str, src_str):
|
||||
global assembled
|
||||
if is_reg(dst_str):
|
||||
if src_str != "ip":
|
||||
op_val = to_uint8(ops[op_str])
|
||||
src_val = to_uint16(src_str)
|
||||
dst_val = to_uint8(regs[dst_str])
|
||||
if op_val and dst_val and src_val:
|
||||
assembled += op_val + dst_val + src_val
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write("CAN'T MOVI TO IP!\n")
|
||||
return False
|
||||
else:
|
||||
sys.stderr.write(
|
||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst_str))
|
||||
return False
|
||||
return True
|
||||
|
||||
@ -95,6 +193,14 @@ def assemble_code(line):
|
||||
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::])))
|
||||
if op_name == "movi":
|
||||
movi(op_name, instruction[1], instruction[2])
|
||||
elif op_name == "movr":
|
||||
movr(op_name, instruction[1], instruction[2])
|
||||
elif op_name == "getm":
|
||||
getm(op_name, instruction[1], instruction[2])
|
||||
elif op_name == "putm":
|
||||
putm(op_name, instruction[1], instruction[2])
|
||||
elif op_name == "addi":
|
||||
addi(op_name, instruction[1], instruction[2])
|
||||
return True
|
||||
|
||||
|
||||
@ -105,8 +211,8 @@ def assemble_data(line):
|
||||
|
||||
def main():
|
||||
global assembled
|
||||
if (len(sys.argv) < 2):
|
||||
print("Usage: {} <file_to_assemble>".format(sys.argv[0]))
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: {} file_to_assemble output".format(sys.argv[0]))
|
||||
return
|
||||
|
||||
with open(sys.argv[1], 'r') as f:
|
||||
@ -122,8 +228,9 @@ def main():
|
||||
elif flag == section_flags["code:"]:
|
||||
assemble_code(line)
|
||||
if not flag:
|
||||
sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n")
|
||||
with open("./out.gipu", 'wb') as f:
|
||||
sys.stderr.write(
|
||||
"Nothing was assembled! Did you use the section delimiters?\n")
|
||||
with open(sys.argv[2], 'wb') as f:
|
||||
f.write(assembled)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user