chip-8/src/vcpu.c
2023-12-25 09:25:14 +01:00

76 lines
1.4 KiB
C

#include "vcpu.h"
#include "mem.h"
#include "screen.h"
#include <stdio.h>
// Current VCPU state
VCPU_State State;
void VCPUInit(){
State.PC=ADDR_ROM;
}
void VCPUFetch(){
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];
State.PC+=2;
}
void VCPUDecode(){
State.X=(State.opcode>>8) & 0xF;
State.Y=(State.opcode>>4) & 0xF;
State.N=State.opcode & 0xF;
State.NN=State.Y;
State.NN=State.NN<<4;
State.NN=State.NN | State.N;
State.NNN=State.opcode&0x0FFF;
}
void VCPUExecute(){
// VCPUDump();
switch(State.opcode >> 12){
case 0x0:
ScreenClear();
break
;;
case 0x1:
State.PC=State.NNN;
break
;;
case 0x6:
State.V[State.X]=State.NN;
break
;;
case 0x7:
State.V[State.X]+=State.NN;
break
;;
case 0xA:
State.I=State.NNN;
break
;;
case 0xD:
int X=State.V[State.X]%63;
int Y=State.V[State.Y]%31;
State.V[REG_FLAG]=0; // Set flag to 0
for(char row=0;row<State.N;row++){
}
break;
;;
}
}
void VCPUDump(){
printf("opcode: 0x%04x\n",State.opcode&0xFFFF);
printf("X: 0x%01x\n",State.X);
printf("Y: 0x%01x\n",State.Y);
printf("N: 0x%01x\n",State.N);
printf("NN: 0x%02x\n",State.NN);
printf("NNN: 0x%03x\n",State.NNN);
}