diff --git a/cpp/vm.cpp b/cpp/vm.cpp index 0448a25..2f5999f 100644 --- a/cpp/vm.cpp +++ b/cpp/vm.cpp @@ -128,7 +128,7 @@ void VM::init_regs(void) { INSTRUCTIONS IMPLEMENTATIONS */ -void VM::exec_movi(void) { +bool VM::exec_movi(void) { /* MOVI R0, 0x2400 | R0 = 0x2400 */ @@ -137,11 +137,15 @@ void VM::exec_movi(void) { dst = as.code[regs[IP] + 1]; imm = *((uint16_t *)&as.code[regs[IP] + 2]); DBG_INFO(("MOVI %s, 0x%x\n", reg_name(dst), imm)); + if (dst == IP) { + DBG_ERROR(("Can't MOVI to IP!\n")); + return false; + } regs[dst] = imm; - return; + return true; } -void VM::exec_movr(void) { +bool VM::exec_movr(void) { /* MOVR R1, R0 | R1 = R0 --------------------- @@ -151,11 +155,15 @@ void VM::exec_movr(void) { dst = as.code[regs[IP] + 1] >> 4; src = as.code[regs[IP] + 1] & 0b00001111; DBG_INFO(("MOVR %s, %s\n", reg_name(dst), reg_name(src))); + if (dst == IP || src == IP) { + DBG_ERROR(("Can't MOVR IP!\n")); + return false; + } regs[dst] = regs[src]; - return; + return true; } -void VM::exec_getm(void) { +bool VM::exec_getm(void) { /* GETM R0, 0x1000 | R0 = data[0x1000] */ @@ -165,10 +173,10 @@ void VM::exec_getm(void) { src = *((uint16_t *)&as.code[regs[IP] + 2]); DBG_INFO(("GETM %s, 0x%x\n", reg_name(dst), src)); regs[dst] = *((uint16_t *)&as.data[src]); - return; + return true; } -void VM::exec_putm(void) { +bool VM::exec_putm(void) { /* PUTM 0x1000, R0 | data[0x1000] = R0 */ @@ -178,10 +186,10 @@ void VM::exec_putm(void) { src = as.code[regs[IP] + 3]; DBG_INFO(("PUTM 0x%x 0x%x\n", dst, src)); *((uint16_t *)&as.data[dst]) = regs[src]; - return; + return true; } -void VM::exec_addi(void) { +bool VM::exec_addi(void) { /* ADDI R0, 0x2 | R0 += 2 */ @@ -192,7 +200,7 @@ void VM::exec_addi(void) { src = *((uint16_t *)&as.code[regs[IP] + 2]); DBG_INFO(("ADDI 0x%x 0x%x\n", dst, src)); regs[dst] += src; - return; + return true; } void VM::run(void) { diff --git a/cpp/vm.h b/cpp/vm.h index 921a180..d795616 100644 --- a/cpp/vm.h +++ b/cpp/vm.h @@ -53,12 +53,12 @@ private: /* IMPLEMENTATIONS */ - void exec_movi(void); - void exec_movr(void); - void exec_movm(void); - void exec_getm(void); - void exec_putm(void); - void exec_addi(void); + bool exec_movi(void); + bool exec_movr(void); + bool exec_movm(void); + bool exec_getm(void); + bool exec_putm(void); + bool exec_addi(void); public: VM(); diff --git a/python/assembler.py b/python/assembler.py index 1e474e4..f8f8e03 100644 --- a/python/assembler.py +++ b/python/assembler.py @@ -26,9 +26,9 @@ op_names = ["MOVI", reg_names = ["R0", "R1", "R2", "R3", "S0", "S1", "S2", "S3", "IP", "BP", "SP"] section_names = ["DATA:", "CODE:", "STACK:"] -section_flags = {s: i + 1 for i, s in enumerate(section_names)} -ops = {s: i for i, s in enumerate(op_names)} -regs = {s: i for i, s in enumerate(reg_names)} +section_flags = {s.casefold(): i + 1 for i, s in enumerate(section_names)} +ops = {s.casefold(): i for i, s in enumerate(op_names)} +regs = {s.casefold(): i for i, s in enumerate(reg_names)} assembled = bytearray() @@ -58,14 +58,14 @@ def to_uint16(data): def is_reg(data): - if data not in reg_names: + if data not in [rn.casefold() for rn in reg_names]: return False return True def movi(op, dst, src): global assembled - if (is_reg(dst)): + 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) @@ -86,14 +86,14 @@ def assemble_code(line): global assembled sys.stdout.write("CODE: ") instruction = [x for x in re.split('\W', line) if x] - op_name = instruction[0] + op_name = instruction[0].casefold() - if op_name not in op_names: + if op_name not in [on.casefold() for on in op_names]: sys.stderr.write( "ERROR WHILE ASSEMBLING UNKNOWN OPERATION: {}\n".format(op_name)) return False sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::]))) - if op_name == "MOVI": + if op_name == "movi": movi(op_name, instruction[1], instruction[2]) return True @@ -110,16 +110,16 @@ def main(): return with open(sys.argv[1], 'r') as f: - gen = (line.strip("\n") for line in f if line != "\n") + gen = (line.casefold().strip("\n") for line in f if line != "\n") flag = None for line in gen: - if line.startswith(tuple(section_names)): + if line.startswith(tuple([sn.casefold() for sn in section_names])): flag = section_flags[line] continue - if flag == section_flags["DATA:"]: + if flag == section_flags["data:"]: assemble_data(line) - elif flag == section_flags["CODE:"]: + elif flag == section_flags["code:"]: assemble_code(line) if not flag: sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n")