Bitwise operations OR, XOR, AND

This commit is contained in:
xamidev
2024-10-19 20:28:35 +02:00
parent 2067cb5cde
commit b46bfc38e1
2 changed files with 48 additions and 9 deletions

48
cpu.c
View File

@@ -28,14 +28,15 @@ typedef enum
// 0xA? -> Memory operations // 0xA? -> Memory operations
MOV = 0xA0, MOV = 0xA0,
LOAD = 0xA1,
STORE = 0xA2,
// 0xB? -> Arithmetic operations // 0xB? -> Arithmetic operations
ADD = 0xB0, ADD = 0xB0,
SUB = 0xB1, SUB = 0xB1,
// 0xC? -> Bitwise operations // 0xC? -> Bitwise operations
OR = 0xC0,
AND = 0xC1,
XOR = 0xC2,
// 0xE? -> Jump and comparisons // 0xE? -> Jump and comparisons
JMP = 0xE0, JMP = 0xE0,
@@ -98,6 +99,7 @@ void cpu_exec(uint8_t opcode)
switch (opcode) switch (opcode)
{ {
// Some lines are repeating.. Should make this a better way..
case NOP: case NOP:
break; break;
case MOV: case MOV:
@@ -115,6 +117,21 @@ 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 OR:
reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] |= cpu.reg[reg2];
break;
case AND:
reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] &= cpu.reg[reg2];
break;
case XOR:
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;
@@ -210,6 +227,24 @@ void assemble(const char* filename)
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, "OR", 2) == 0)
{
cpu.memory[mem_index++] = OR;
cpu.memory[mem_index++] = reg1_n;
cpu.memory[mem_index++] = reg2_n;
}
else if (strncmp(instruction, "AND", 3) == 0)
{
cpu.memory[mem_index++] = AND;
cpu.memory[mem_index++] = reg1_n;
cpu.memory[mem_index++] = reg2_n;
}
else if (strncmp(instruction, "XOR", 3) == 0)
{
cpu.memory[mem_index++] = XOR;
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)
{ {
int reg1_n = reg1[1] - '0'; int reg1_n = reg1[1] - '0';
@@ -288,8 +323,6 @@ void mem_dump()
{ {
// Instructions (colored background) // Instructions (colored background)
case 0xa0: case 0xa0:
case 0xa1:
case 0xa2:
printf("\e[42m%02x\e[0m ", cpu.memory[i]); printf("\e[42m%02x\e[0m ", cpu.memory[i]);
break; break;
@@ -298,6 +331,12 @@ void mem_dump()
printf("\e[43m%02x\e[0m ", cpu.memory[i]); printf("\e[43m%02x\e[0m ", cpu.memory[i]);
break; break;
case 0xc0:
case 0xc1:
case 0xc2:
printf("\e[45m%02x\e[0m ", cpu.memory[i]);
break;
case 0xe0: case 0xe0:
case 0xe1: case 0xe1:
case 0xe2: case 0xe2:
@@ -347,6 +386,7 @@ int main(int argc, char* argv[])
// Dumping our program // Dumping our program
mem_dump(); mem_dump();
reg_write(1, 0x12);
reg_write(2, 0x10); reg_write(2, 0x10);
cpu_run(); cpu_run();

View File

@@ -1,6 +1,5 @@
;this is a comment ;this is a comment
ADD R1, R2 OR R1, R2
SUB R1, R2
SUB R1, R2
HLT HLT