chip-8/src/vcpu.c

67 lines
1.1 KiB
C
Raw Normal View History

2023-12-25 07:24:17 +01:00
#include "vcpu.h"
#include "mem.h"
#include "screen.h"
2023-12-25 09:11:45 +01:00
#include <stdio.h>
2023-12-25 07:24:17 +01:00
// Current VCPU state
VCPU_State State;
void VCPUInit(){
2023-12-25 07:32:11 +01:00
State.PC=ADDR_ROM;
2023-12-25 07:24:17 +01:00
}
void VCPUFetch(){
2023-12-25 09:11:45 +01:00
unsigned char byte[2];
MemRead(byte,2,State.PC); // Little indian to -1 no +1
State.opcode=byte[0];
State.opcode=State.opcode<<8;
State.opcode=State.opcode | byte[1];
2023-12-25 07:32:11 +01:00
State.PC+=2;
2023-12-25 07:24:17 +01:00
}
void VCPUDecode(){
2023-12-25 07:30:17 +01:00
State.X=(State.opcode<<4) & 0xF0;
State.Y=(State.opcode<<8) & 0xF0;
State.N=(State.opcode<<12) & 0xF0;
State.NN=(State.opcode<<8) & 0xFF;
State.NNN=(State.opcode<<4) & 0xFFF0;
2023-12-25 07:24:17 +01:00
}
void VCPUExecute(){
2023-12-25 09:11:45 +01:00
switch(State.opcode >> 12){
2023-12-25 07:30:17 +01:00
case 0x0:
2023-12-25 07:24:17 +01:00
ScreenClear();
break
;;
2023-12-25 07:30:17 +01:00
case 0x1:
2023-12-25 07:32:11 +01:00
State.PC=State.NNN;
2023-12-25 07:30:17 +01:00
break
;;
case 0x6:
2023-12-25 07:32:11 +01:00
State.V[State.X]=State.NN;
2023-12-25 07:30:17 +01:00
break
;;
case 0x7:
2023-12-25 07:32:11 +01:00
State.V[State.X]+=State.NN;
2023-12-25 07:30:17 +01:00
break
;;
case 0xA:
2023-12-25 07:32:11 +01:00
State.I=State.NNN;
2023-12-25 07:30:17 +01:00
break
;;
2023-12-25 09:11:45 +01:00
case 0xD:
int X=State.V[State.X]%63;
int Y=State.V[State.Y]%31;
State.V[0xF]=0; // Set flag to 0
for(char row=0;row<State.N;row++){
}
break;
;;
2023-12-25 07:24:17 +01:00
}
}
2023-12-25 09:11:45 +01:00
void VCPUDump(){
printf("opcode: 0x%04x\n",State.opcode&0xFFFF);
}