This commit is contained in:
xamidev
2024-10-18 22:21:42 +02:00
parent 730bc7858d
commit 02f05f1302
2 changed files with 22 additions and 4 deletions

21
cpu.c
View File

@@ -85,6 +85,10 @@ void cpu_exec(uint8_t opcode)
case HLT: case HLT:
cpu.halted = true; cpu.halted = true;
break; break;
case JMP:
addr = cpu.memory[cpu.pc++];
cpu.pc = addr;
break;
// TODO: complete instruction set // TODO: complete instruction set
default: default:
printf("Unknown instruction: 0x%02X\n", opcode); printf("Unknown instruction: 0x%02X\n", opcode);
@@ -147,6 +151,11 @@ void assemble(const char* filename)
} }
} else if (sscanf(line, "%s %d", instruction, &addr) == 2) } else if (sscanf(line, "%s %d", instruction, &addr) == 2)
{ {
if (strncmp(instruction, "JMP", 3) == 0)
{
cpu.memory[mem_index++] = JMP;
cpu.memory[mem_index++] = addr;
}
// TODO for jmp... // TODO for jmp...
} else if (strncmp(line, "HLT", 3) == 0) } else if (strncmp(line, "HLT", 3) == 0)
{ {
@@ -202,9 +211,13 @@ void mem_dump()
puts(""); puts("");
} }
bool prefix(const char* pre, const char* str) /*
* Write to register: useful for debugging and testing
*/
void reg_write(int index, uint8_t val)
{ {
return strncmp(pre, str, strlen(pre)) == 0; cpu.reg[index] = val;
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@@ -221,8 +234,10 @@ int main(int argc, char* argv[])
// Dumping our program // Dumping our program
mem_dump(); mem_dump();
reg_write(2, 0x10);
cpu_run(); cpu_run();
// Post-mortem analysis // Post-mortem analysis
cpu_dump(); cpu_dump();

View File

@@ -1,3 +1,6 @@
;this is a comment ;this is a comment
MOV 1 2 JMP 5 ; this is one too
MOV 1 2 ; hellooo?
MOV 3 2 ; should jump here, R1 should be untouched
HLT HLT