mem dump coloring, + SUB

This commit is contained in:
xamidev
2024-10-19 19:40:56 +02:00
parent 14e8802497
commit 2067cb5cde
3 changed files with 69 additions and 21 deletions

BIN
.cpu.c.swo Normal file

Binary file not shown.

73
cpu.c
View File

@@ -23,16 +23,26 @@
typedef enum typedef enum
{ {
// TODO: Bitwise operations (OR, AND, XOR, NOT, ...) // 0x00 -> No operation
NOP = 0, NOP = 0,
MOV = 1,
ADD, // 0xA? -> Memory operations
SUB, MOV = 0xA0,
LOAD, LOAD = 0xA1,
STORE, STORE = 0xA2,
JMP,
JEQ, // 0xB? -> Arithmetic operations
CMP, ADD = 0xB0,
SUB = 0xB1,
// 0xC? -> Bitwise operations
// 0xE? -> Jump and comparisons
JMP = 0xE0,
JEQ = 0xE1,
CMP = 0xE2,
// 0xF? -> Misc operations
HLT = 0xFF HLT = 0xFF
} instruction_set_t; } instruction_set_t;
@@ -100,6 +110,11 @@ void cpu_exec(uint8_t opcode)
reg2 = cpu.memory[cpu.pc++]; reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] += cpu.reg[reg2]; cpu.reg[reg1] += cpu.reg[reg2];
break; break;
case SUB:
reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] -= cpu.reg[reg2];
break;
case HLT: case HLT:
cpu.halted = true; cpu.halted = true;
break; break;
@@ -189,6 +204,11 @@ void assemble(const char* filename)
cpu.memory[mem_index++] = CMP; cpu.memory[mem_index++] = CMP;
cpu.memory[mem_index++] = reg1_n; cpu.memory[mem_index++] = reg1_n;
cpu.memory[mem_index++] = reg2_n; cpu.memory[mem_index++] = reg2_n;
} else if (strncmp(instruction, "SUB", 3) == 0)
{
cpu.memory[mem_index++] = SUB;
cpu.memory[mem_index++] = reg1_n;
cpu.memory[mem_index++] = reg2_n;
} }
} else if (sscanf(line, "%s %[^,], %d", instruction, reg1, &addr) == 2) } else if (sscanf(line, "%s %[^,], %d", instruction, reg1, &addr) == 2)
{ {
@@ -263,7 +283,42 @@ void mem_dump()
{ {
puts(""); puts("");
} }
switch (cpu.memory[i])
{
// Instructions (colored background)
case 0xa0:
case 0xa1:
case 0xa2:
printf("\e[42m%02x\e[0m ", cpu.memory[i]);
break;
case 0xb0:
case 0xb1:
printf("\e[43m%02x\e[0m ", cpu.memory[i]);
break;
case 0xe0:
case 0xe1:
case 0xe2:
printf("\e[44m%02x\e[0m ", cpu.memory[i]);
break;
case 0xff:
printf("\e[41m%02x\e[0m ", cpu.memory[i]);
break;
// General purpose registers (colored foreground)
case 0x01:
case 0x02:
case 0x03:
case 0x04:
printf("\e[36m%02x\e[0m ", cpu.memory[i]);
break;
default:
printf("%02x ", cpu.memory[i]); printf("%02x ", cpu.memory[i]);
break;
}
} }
puts(""); puts("");
} }

View File

@@ -1,13 +1,6 @@
;this is a comment ;this is a comment
JMP 4
HLT
NOP
MOV R1, R2 ; hellooo?
;MOV R3, R2 ; should jump here, R1 should be untouched
CMP R1, R2
JEQ R1, 2
ADD R1, R2 ADD R1, R2
SUB R1, R2
SUB R1, R2
HLT HLT