chip-8/src/vcpu.c

119 lines
2.4 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 10:01:40 +01:00
State.S=0;
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 09:22:19 +01:00
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;
2023-12-25 07:24:17 +01:00
}
void VCPUExecute(){
2023-12-25 09:22:19 +01:00
// VCPUDump();
2023-12-25 09:11:45 +01:00
switch(State.opcode >> 12){
2023-12-25 10:24:29 +01:00
case 0x0: // Clear screen or return from subroutine
if(State.N == 0){ // Clear screen
ScreenClear();
}
else { // Return from subroutine
State.PC=State.stack[State.S];
State.S--;
}
2023-12-25 07:24:17 +01:00
break
;;
2023-12-25 10:07:12 +01:00
case 0x1: // Jump
2023-12-25 07:32:11 +01:00
State.PC=State.NNN;
2023-12-25 07:30:17 +01:00
break
;;
2023-12-25 10:24:29 +01:00
case 0x2: // Call
State.S++;
State.stack[State.S]=State.PC;
State.PC=State.NNN;
break
;;
case 0x3: // SE: VX, byte
if(State.V[State.X]==State.NN)
State.PC+=2;
break;
;;
case 0x4: // SNE: VX, byte
if(State.V[State.X]!=State.NN)
State.PC+=2;
break;
;;
case 0x5: // SNE: VX, VY
if(State.V[State.X]==State.V[State.Y])
State.PC+=2;
break;
;;
2023-12-25 07:30:17 +01:00
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 10:01:40 +01:00
case 0xD: // Draw a sprite
2023-12-25 09:11:45 +01:00
int X=State.V[State.X]%63;
int Y=State.V[State.Y]%31;
2023-12-25 09:25:14 +01:00
State.V[REG_FLAG]=0; // Set flag to 0
2023-12-25 10:01:40 +01:00
int width,height;
ScreenWH(&width,&height);
2023-12-25 09:11:45 +01:00
for(char row=0;row<State.N;row++){
2023-12-25 10:01:40 +01:00
// Stop if row out of screen
if(Y+row>=height)
break;
char sprite;
MemRead(&sprite,1,State.I+row); // Load sprite
// Draw sprite
for(int shift=0;shift<8;shift++){
// Stop if column is out of screen
if(X+shift >= width)
break;
if(ScreenPixelApply(X+shift,Y+row,(sprite>>(7-shift))&0x1))
State.V[REG_FLAG]=1;
}
2023-12-25 09:11:45 +01:00
}
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);
2023-12-25 09:22:19 +01:00
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);
2023-12-25 09:11:45 +01:00
}