diff options
| -rw-r--r-- | src/main.c | 9 | ||||
| -rw-r--r-- | src/mem.c | 9 | ||||
| -rw-r--r-- | src/mem.h | 1 | ||||
| -rw-r--r-- | src/screen.c | 10 | ||||
| -rw-r--r-- | src/screen.h | 1 | ||||
| -rw-r--r-- | src/vcpu.c | 24 | ||||
| -rw-r--r-- | src/vcpu.h | 5 |
7 files changed, 48 insertions, 11 deletions
@@ -8,13 +8,16 @@ int main(int argc, char *argv[]) // Initialize MemInit(); MemLoadROM("../roms/2-ibm-logo.ch8"); + ScreenInit(800,400); VCPUInit(); - - - ScreenSetPixel(0,1,1); + // MemDump(); + int i=0; while (!WindowShouldClose()){ + VCPUFetch(); + VCPUDecode(); + VCPUExecute(); ScreenUpdate(); } @@ -22,8 +22,9 @@ void MemCopy(unsigned char *data, int size, int addr){ void MemRead(unsigned char *data, int size, int addr){ int location=addr; - for(int i=0;i<size;i++) + for(int i=0;i<size;i++){ data[i]=memory[addr+i]; + } } void MemLoadROM(char *path){ @@ -38,6 +39,12 @@ void MemLoadROM(char *path){ fclose(ptr); } +void MemDump(){ + for(int addr=0;addr<4096;addr+=2){ + printf("0x%03x: %02x %02x\n",addr,memory[addr], memory[addr+1]); + } +} + unsigned char DEFAULT_FONT[]={ 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 0x20, 0x60, 0x20, 0x20, 0x70, // 1 @@ -8,3 +8,4 @@ void MemSet(int addr, unsigned char value, int size); void MemCopy(unsigned char *data, int size, int addr); void MemRead(unsigned char *data, int size, int addr); void MemLoadROM(char *path); +void MemDump(); diff --git a/src/screen.c b/src/screen.c index 24a1eb8..d312a8d 100644 --- a/src/screen.c +++ b/src/screen.c @@ -12,7 +12,8 @@ void ScreenInit(int width, int height){ Screen.originX=(width-64*Screen.pixel)/2; Screen.originY=(height-32*Screen.pixel)/2; ScreenClear(); - + + SetTraceLogLevel(LOG_ERROR); // Disable anoying raylib logs InitWindow(width, height, "Chip-8 Emulator"); SetTargetFPS(60); // Set game to run at 60 frames-per-second } @@ -43,6 +44,13 @@ void ScreenSetPixel(int x, int y, unsigned char state){ Screen.pixels[x+y*64]=(state==0) ? 0: 1; } +void ScreenPixelFlip(int x, int y){ + if(Screen.pixels[x+y*64]==0) + Screen.pixels[x+y*64]=1; + else + Screen.pixels[x+y*64]=0; +} + void ScreenClose(){ CloseWindow(); // Close window and OpenGL context } diff --git a/src/screen.h b/src/screen.h index d239c0d..9faf781 100644 --- a/src/screen.h +++ b/src/screen.h @@ -16,5 +16,6 @@ typedef struct SCREEN_DATA { void ScreenInit(int width, int height); void ScreenClear(); void ScreenSetPixel(int x, int y, unsigned char state); +void ScreenPixelFlip(int x, int y); void ScreenUpdate(); void ScreenClose(); @@ -1,7 +1,7 @@ #include "vcpu.h" #include "mem.h" #include "screen.h" - +#include <stdio.h> // Current VCPU state VCPU_State State; @@ -11,7 +11,11 @@ void VCPUInit(){ } void VCPUFetch(){ - MemRead((char *)&(State.opcode),2,State.PC); + 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; } @@ -24,7 +28,7 @@ void VCPUDecode(){ } void VCPUExecute(){ - switch(State.opcode & 0xF){ + switch(State.opcode >> 12){ case 0x0: ScreenClear(); break @@ -45,6 +49,18 @@ void VCPUExecute(){ State.I=State.NNN; break ;; - + 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; + ;; } } + +void VCPUDump(){ + printf("opcode: 0x%04x\n",State.opcode&0xFFFF); +} @@ -21,15 +21,16 @@ typedef struct VCPU_State { unsigned char ST; // Intruction (opcode + decoded fields) - short opcode; + unsigned short opcode; char X; char Y; char N; char NN; - short NNN; + unsigned short NNN; } VCPU_State; void VCPUInit(); void VCPUFetch(); void VCPUDecode(); void VCPUExecute(); +void VCPUDump(); |
