chip-8/src/vcpu.c

361 lines
8 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 21:21:20 +01:00
#include "keypad.h"
2023-12-26 13:17:52 +01:00
#include "speaker.h"
2023-12-25 21:21:20 +01:00
2023-12-25 09:11:45 +01:00
#include <stdio.h>
2023-12-25 11:00:27 +01:00
#include <stdlib.h>
2023-12-26 09:06:40 +01:00
#include <unistd.h>
2023-12-25 11:00:27 +01:00
#include <time.h>
2023-12-25 07:24:17 +01:00
// Current VCPU state
VCPU_State State;
2023-12-26 19:27:23 +01:00
void VCPUInit(int width, int height){
MemInit();
ScreenInit(width,height);
SpeakerInit();
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-26 13:17:52 +01:00
State.dt_ticks=0;
State.st_ticks=0;
2023-12-26 09:06:40 +01:00
State.screen_ticks=0;
2023-12-26 13:17:52 +01:00
State.keypressed=0;
2023-12-25 11:00:27 +01:00
srand(time(NULL));
2023-12-25 07:24:17 +01:00
}
2023-12-26 19:27:23 +01:00
void VCPUFinish(){
SpeakerFinish();
ScreenClose();
}
2023-12-25 07:24:17 +01:00
void VCPUFetch(){
2023-12-25 09:11:45 +01:00
unsigned char byte[2];
2023-12-25 15:59:36 +01:00
MemLoad(byte,2,State.PC); // Little indian to -1 no +1
2023-12-25 09:11:45 +01:00
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;
2023-12-25 15:03:22 +01:00
State.N=State.opcode & 0x0F;
State.NN=State.opcode & 0xFF;
State.NNN=State.opcode & 0x0FFF;
}
2023-12-25 09:22:19 +01:00
2023-12-25 15:03:22 +01:00
void VCPUDoubleDabble(unsigned char x, unsigned char *u, unsigned char *t, unsigned char *h){
unsigned int bcd=x;
for(int i=0;i<8;i++){
bcd=bcd<<1;
unsigned char byte=bcd & 0xFF;
unsigned char units=(bcd>>8) & 0xF;
unsigned char tens=(bcd>>12) & 0xF;
unsigned char hundreds=(bcd>>16) & 0xF;
//printf("hundreds:%04b tens:%04b units:%04b byte:%08b\n",hundreds,tens,units,byte);
if(i<7){
if(units>4)
units+=3;
if(tens>4)
tens+=3;
if(hundreds>4)
hundreds+=3;
}
bcd = (hundreds<<16) | (tens << 12) | (units << 8) | byte;
}
*u=bcd>>8 & 0xF;
*t=bcd>>12 & 0xF;
*h=bcd>>16 & 0xF;
2023-12-25 07:24:17 +01:00
}
void VCPUExecute(){
2023-12-26 19:27:23 +01:00
// Very big switch case... Thinking about it, ifelse would have been cleaner for the reader
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
2023-12-25 15:03:22 +01:00
if(State.N == 0x0){ // Clear screen
2023-12-25 10:24:29 +01:00
ScreenClear();
}
2023-12-25 15:03:22 +01:00
else if(State.N == 0xE) { // Return from subroutine
2023-12-25 10:24:29 +01:00
State.PC=State.stack[State.S];
State.S--;
}
2023-12-25 15:03:22 +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 15:03:22 +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;
2023-12-25 15:03:22 +01:00
break;
2023-12-25 10:24:29 +01:00
case 0x3: // SE: VX, byte
if(State.V[State.X]==State.NN)
State.PC+=2;
break;
2023-12-25 15:03:22 +01:00
2023-12-25 10:24:29 +01:00
case 0x4: // SNE: VX, byte
if(State.V[State.X]!=State.NN)
State.PC+=2;
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x5: // SE: VX, VY
2023-12-25 15:03:22 +01:00
if(State.N == 0){
if(State.V[State.X]==State.V[State.Y])
State.PC+=2;
}
2023-12-25 10:24:29 +01:00
break;
2023-12-25 15:03:22 +01:00
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 15:03:22 +01:00
break;
2023-12-25 07:30:17 +01:00
case 0x7:
2023-12-25 15:03:22 +01:00
State.V[State.X]=State.V[State.X] + State.NN;
break;
2023-12-25 11:00:27 +01:00
case 0x8: // Register operations
switch(State.N){
2023-12-25 15:03:22 +01:00
case 0x0: // VX = VY
State.V[State.X]=State.V[State.Y];
break;
2023-12-25 11:00:27 +01:00
case 0x1: // VX = VX OR VY
State.V[State.X]=State.V[State.X] | State.V[State.Y];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x2: // VX = VX AND VY
State.V[State.X]=State.V[State.X] & State.V[State.Y];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x3: // VX = VX XOR VY
State.V[State.X]=State.V[State.X] ^ State.V[State.Y];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x4: // VX = VX + VY
2023-12-25 18:36:53 +01:00
unsigned char x=State.V[State.X];
unsigned char y=State.V[State.Y];
State.V[State.X]=x+y;
State.V[REG_FLAG]=((x+y) > 255);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x5: // VX = VX - VY
2023-12-25 18:36:53 +01:00
unsigned char x2=State.V[State.X];
unsigned char y2=State.V[State.Y];
State.V[State.X]=x2-y2;
State.V[REG_FLAG]=(x2>=y2);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x6: // VX = VX SHR 1
2023-12-25 18:36:53 +01:00
char flag=State.V[State.X] & 0x1 == 1;
2023-12-25 11:00:27 +01:00
State.V[State.X]=State.V[State.X] >> 1;
2023-12-25 18:36:53 +01:00
State.V[REG_FLAG]=flag;
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x7: // VX = VY - VX
2023-12-25 18:36:53 +01:00
unsigned char x3=State.V[State.X];
unsigned char y3=State.V[State.Y];
State.V[State.X]=y3-x3;
State.V[REG_FLAG]=(y3>=x3);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0xE: // VX = VX SHL 1
2023-12-25 18:36:53 +01:00
char flag2=State.V[State.X] >> 7 == 1;
2023-12-25 11:00:27 +01:00
State.V[State.X]=State.V[State.X] << 1;
2023-12-25 18:36:53 +01:00
State.V[REG_FLAG]=flag2;
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
}
2023-12-25 15:03:22 +01:00
break;
2023-12-25 11:00:27 +01:00
case 0x9: // SNE: VX, VY
2023-12-25 15:03:22 +01:00
if(State.N==0){
if(State.V[State.X]!=State.V[State.Y])
State.PC+=2;
}
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 07:30:17 +01:00
case 0xA:
2023-12-25 07:32:11 +01:00
State.I=State.NNN;
2023-12-25 15:03:22 +01:00
break;
2023-12-25 11:00:27 +01:00
case 0xB:
State.PC=State.V[0]+State.NNN;
2023-12-25 15:03:22 +01:00
break;
2023-12-25 11:00:27 +01:00
case 0xC:
unsigned short n = rand() % 255 + 1;
State.V[State.X]=n & State.NN;
2023-12-25 15:03:22 +01:00
break;
2023-12-25 10:01:40 +01:00
case 0xD: // Draw a sprite
2023-12-25 09:25:14 +01:00
State.V[REG_FLAG]=0; // Set flag to 0
2023-12-25 15:03:22 +01:00
int width, height;
2023-12-25 10:01:40 +01:00
ScreenWH(&width,&height);
2023-12-25 15:59:36 +01:00
int X=State.V[State.X]%width;
int Y=State.V[State.Y]%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;
2023-12-25 15:03:22 +01:00
unsigned char sprite;
2023-12-25 15:59:36 +01:00
MemLoad(&sprite,1,State.I+row); // Load sprite
2023-12-25 10:01:40 +01:00
// 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 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0xE:
2023-12-25 21:21:20 +01:00
if(State.NN==0x9E){ // Skip if keypress in VX
2023-12-26 13:17:52 +01:00
if(State.keypressed){
if(State.V[State.X] == State.key)
2023-12-26 09:06:40 +01:00
State.PC+=2;
2023-12-25 21:21:20 +01:00
}
}else if(State.NN==0xA1){ // Skip if not keypress in VX
2023-12-26 13:17:52 +01:00
State.PC+=2;
if(State.keypressed){
if(State.V[State.X] == State.key)
2023-12-26 20:16:51 +01:00
State.PC-=2;
2023-12-26 09:06:40 +01:00
}
2023-12-25 21:21:20 +01:00
}
2023-12-25 15:03:22 +01:00
break;
2023-12-25 11:00:27 +01:00
case 0xF:
switch(State.NN){
case 0x07: // Get timer
State.V[State.X]=State.DT;
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x0A:
2023-12-26 13:17:52 +01:00
if(State.keypressed){
State.V[State.X]=State.key;
2023-12-25 21:29:52 +01:00
}
else
2023-12-26 09:06:40 +01:00
State.PC-=2; // Go back to last instruction (loop until key is pressed)
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x15: // Set timer
State.DT=State.V[State.X];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x18: // Set sound timer
State.ST=State.V[State.X];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x1E: // I = I + VX
State.I=State.I+State.V[State.X];
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x29:
2023-12-25 19:34:14 +01:00
State.I=ADDR_FONT+5*(State.V[State.X]&0x0F);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x33:
2023-12-25 15:03:22 +01:00
unsigned char units, tens, hundreds;
VCPUDoubleDabble(State.V[State.X],&units,&tens,&hundreds);
2023-12-25 15:59:36 +01:00
MemStore(&hundreds,1,State.I);
MemStore(&tens,1,State.I+1);
MemStore(&units,1,State.I+2);
2023-12-26 19:27:23 +01:00
// printf("hundreds:%d tens:%d units:%d byte:%d\n",hundreds,tens,units,State.V[State.X]);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x55:
2023-12-25 17:59:22 +01:00
MemStore(State.V,State.X+1,State.I);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
case 0x65:
2023-12-25 17:59:22 +01:00
MemLoad(State.V,State.X+1,State.I);
2023-12-25 11:00:27 +01:00
break;
2023-12-25 15:03:22 +01:00
2023-12-25 11:00:27 +01:00
}
break;
2023-12-25 07:24:17 +01:00
}
}
2023-12-25 09:11:45 +01:00
2023-12-26 09:06:40 +01:00
void VCPUTick(){
struct timespec start, stop;
double duration, delay;
2023-12-26 19:27:23 +01:00
// Start CPU pipeline instrumentation
clock_gettime(CLOCK_REALTIME, &start);
2023-12-26 13:17:52 +01:00
// Update keypressed
int key=KeypadGetPressed();
if(key>=0){
State.keypressed=1;
State.key=key&0xF;
2023-12-26 19:27:23 +01:00
// printf("Keypressed: %x\n",State.key);
2023-12-26 13:17:52 +01:00
}
2023-12-26 19:27:23 +01:00
else{
2023-12-26 13:17:52 +01:00
State.keypressed=0;
2023-12-26 19:27:23 +01:00
}
2023-12-26 13:17:52 +01:00
2023-12-26 19:27:23 +01:00
// Execute next instruction
2023-12-26 09:06:40 +01:00
VCPUFetch();
VCPUDecode();
VCPUExecute();
2023-12-26 19:27:23 +01:00
// Update ticks
2023-12-26 13:17:52 +01:00
State.dt_ticks++;
State.st_ticks++;
2023-12-26 09:06:40 +01:00
State.screen_ticks++;
2023-12-26 13:17:52 +01:00
// Update Delay Timer
if(State.dt_ticks>=(1.0*VCPU_FREQ/DTST_FREQ)){
State.dt_ticks=0;
2023-12-26 09:06:40 +01:00
if(State.DT>0)
State.DT--;
}
2023-12-26 13:17:52 +01:00
// Update Sound Timer
if(State.st_ticks>=(1.0*VCPU_FREQ/DTST_FREQ)){
State.st_ticks=0;
if(State.ST>0)
State.ST--;
}
// Play sound
if(State.ST>0)
2023-12-26 17:51:39 +01:00
SpeakerPlay();
2023-12-26 13:17:52 +01:00
2023-12-26 09:06:40 +01:00
// Refresh screen
if(State.screen_ticks>=(1.0*VCPU_FREQ/SCREEN_FREQ)){
State.screen_ticks=0;
ScreenUpdate();
}
2023-12-26 19:27:23 +01:00
// End instrumentation
clock_gettime(CLOCK_REALTIME, &stop);
// Adjust pipeline duration
duration=(stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec)*1e-9;
delay=1.0/VCPU_FREQ-duration;
if(delay>0){
usleep(delay*1e6);
}
2023-12-26 09:06:40 +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 15:59:36 +01:00
for(int i=0;i<16;i++){
printf("V%d: 0x%02x\n",i,State.V[i]);
}
2023-12-25 09:11:45 +01:00
}