NO IP in MOVI, MOVR, casefold

This commit is contained in:
Giulio De Pasquale 2017-05-15 14:39:40 +02:00
parent 34d7d3c85f
commit c50dd9718f
3 changed files with 36 additions and 28 deletions

View File

@ -128,7 +128,7 @@ void VM::init_regs(void) {
INSTRUCTIONS IMPLEMENTATIONS INSTRUCTIONS IMPLEMENTATIONS
*/ */
void VM::exec_movi(void) { bool VM::exec_movi(void) {
/* /*
MOVI R0, 0x2400 | R0 = 0x2400 MOVI R0, 0x2400 | R0 = 0x2400
*/ */
@ -137,11 +137,15 @@ void VM::exec_movi(void) {
dst = as.code[regs[IP] + 1]; dst = as.code[regs[IP] + 1];
imm = *((uint16_t *)&as.code[regs[IP] + 2]); imm = *((uint16_t *)&as.code[regs[IP] + 2]);
DBG_INFO(("MOVI %s, 0x%x\n", reg_name(dst), imm)); 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; regs[dst] = imm;
return; return true;
} }
void VM::exec_movr(void) { bool VM::exec_movr(void) {
/* /*
MOVR R1, R0 | R1 = R0 MOVR R1, R0 | R1 = R0
--------------------- ---------------------
@ -151,11 +155,15 @@ void VM::exec_movr(void) {
dst = as.code[regs[IP] + 1] >> 4; dst = as.code[regs[IP] + 1] >> 4;
src = as.code[regs[IP] + 1] & 0b00001111; src = as.code[regs[IP] + 1] & 0b00001111;
DBG_INFO(("MOVR %s, %s\n", reg_name(dst), reg_name(src))); 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]; regs[dst] = regs[src];
return; return true;
} }
void VM::exec_getm(void) { bool VM::exec_getm(void) {
/* /*
GETM R0, 0x1000 | R0 = data[0x1000] GETM R0, 0x1000 | R0 = data[0x1000]
*/ */
@ -165,10 +173,10 @@ void VM::exec_getm(void) {
src = *((uint16_t *)&as.code[regs[IP] + 2]); src = *((uint16_t *)&as.code[regs[IP] + 2]);
DBG_INFO(("GETM %s, 0x%x\n", reg_name(dst), src)); DBG_INFO(("GETM %s, 0x%x\n", reg_name(dst), src));
regs[dst] = *((uint16_t *)&as.data[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 PUTM 0x1000, R0 | data[0x1000] = R0
*/ */
@ -178,10 +186,10 @@ void VM::exec_putm(void) {
src = as.code[regs[IP] + 3]; src = as.code[regs[IP] + 3];
DBG_INFO(("PUTM 0x%x 0x%x\n", dst, src)); DBG_INFO(("PUTM 0x%x 0x%x\n", dst, src));
*((uint16_t *)&as.data[dst]) = regs[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 ADDI R0, 0x2 | R0 += 2
*/ */
@ -192,7 +200,7 @@ void VM::exec_addi(void) {
src = *((uint16_t *)&as.code[regs[IP] + 2]); src = *((uint16_t *)&as.code[regs[IP] + 2]);
DBG_INFO(("ADDI 0x%x 0x%x\n", dst, src)); DBG_INFO(("ADDI 0x%x 0x%x\n", dst, src));
regs[dst] += src; regs[dst] += src;
return; return true;
} }
void VM::run(void) { void VM::run(void) {

View File

@ -53,12 +53,12 @@ private:
/* /*
IMPLEMENTATIONS IMPLEMENTATIONS
*/ */
void exec_movi(void); bool exec_movi(void);
void exec_movr(void); bool exec_movr(void);
void exec_movm(void); bool exec_movm(void);
void exec_getm(void); bool exec_getm(void);
void exec_putm(void); bool exec_putm(void);
void exec_addi(void); bool exec_addi(void);
public: public:
VM(); VM();

View File

@ -26,9 +26,9 @@ op_names = ["MOVI",
reg_names = ["R0", "R1", "R2", "R3", "S0", "S1", "S2", "S3", "IP", "BP", "SP"] reg_names = ["R0", "R1", "R2", "R3", "S0", "S1", "S2", "S3", "IP", "BP", "SP"]
section_names = ["DATA:", "CODE:", "STACK:"] section_names = ["DATA:", "CODE:", "STACK:"]
section_flags = {s: i + 1 for i, s in enumerate(section_names)} section_flags = {s.casefold(): i + 1 for i, s in enumerate(section_names)}
ops = {s: i for i, s in enumerate(op_names)} ops = {s.casefold(): i for i, s in enumerate(op_names)}
regs = {s: i for i, s in enumerate(reg_names)} regs = {s.casefold(): i for i, s in enumerate(reg_names)}
assembled = bytearray() assembled = bytearray()
@ -58,14 +58,14 @@ def to_uint16(data):
def is_reg(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 False
return True return True
def movi(op, dst, src): def movi(op, dst, src):
global assembled global assembled
if (is_reg(dst)): if (is_reg(dst) and dst is not "ip"):
op_val = to_uint8(ops[op]) op_val = to_uint8(ops[op])
dst_val = to_uint8(regs[dst]) dst_val = to_uint8(regs[dst])
src_val = to_uint16(src) src_val = to_uint16(src)
@ -86,14 +86,14 @@ def assemble_code(line):
global assembled global assembled
sys.stdout.write("CODE: ") sys.stdout.write("CODE: ")
instruction = [x for x in re.split('\W', line) if x] 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( sys.stderr.write(
"ERROR WHILE ASSEMBLING UNKNOWN OPERATION: {}\n".format(op_name)) "ERROR WHILE ASSEMBLING UNKNOWN OPERATION: {}\n".format(op_name))
return False return False
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::]))) 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]) movi(op_name, instruction[1], instruction[2])
return True return True
@ -110,16 +110,16 @@ def main():
return return
with open(sys.argv[1], 'r') as f: 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 flag = None
for line in gen: 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] flag = section_flags[line]
continue continue
if flag == section_flags["DATA:"]: if flag == section_flags["data:"]:
assemble_data(line) assemble_data(line)
elif flag == section_flags["CODE:"]: elif flag == section_flags["code:"]:
assemble_code(line) assemble_code(line)
if not flag: if not flag:
sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n") sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n")