Minor changes
This commit is contained in:
parent
3924115080
commit
475996af26
9 changed files with 146 additions and 4 deletions
BIN
roms/2-ibm-logo.ch8
Normal file
BIN
roms/2-ibm-logo.ch8
Normal file
Binary file not shown.
|
@ -1,7 +1,7 @@
|
|||
|
||||
EXEC=chip-8
|
||||
|
||||
$(EXEC): main.c screen.c
|
||||
$(EXEC): main.c screen.c mem.c vcpu.c
|
||||
gcc -lraylib $^ -o $@
|
||||
|
||||
clean:
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
#include "screen.h"
|
||||
|
||||
#include "mem.h"
|
||||
#include "vcpu.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
// Initialize
|
||||
MemInit();
|
||||
MemLoadROM("../roms/2-ibm-logo.ch8");
|
||||
ScreenInit(800,400);
|
||||
VCPUInit();
|
||||
|
||||
|
||||
ScreenSetPixel(0,1,1);
|
||||
|
||||
while (!WindowShouldClose()){
|
||||
|
|
57
src/mem.c
Normal file
57
src/mem.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include "mem.h"
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned char memory[4096];
|
||||
|
||||
extern unsigned char DEFAULT_FONT[];
|
||||
|
||||
void MemInit(){
|
||||
MemSet(0,0,4096);
|
||||
MemCopy(DEFAULT_FONT,16*5,ADDR_FONT);
|
||||
}
|
||||
|
||||
void MemSet(int addr, unsigned char value, int size){
|
||||
for(int i=0;i<size;i++)
|
||||
memory[addr+i]=value;
|
||||
}
|
||||
|
||||
void MemCopy(unsigned char *data, int size, int addr){
|
||||
for(int i=0;i<size;i++)
|
||||
memory[addr+i]=data[i];
|
||||
}
|
||||
|
||||
void MemRead(unsigned char *data, int size, int addr){
|
||||
int location=addr;
|
||||
for(int i=0;i<size;i++)
|
||||
data[i]=memory[addr+i];
|
||||
}
|
||||
|
||||
void MemLoadROM(char *path){
|
||||
char byte;
|
||||
FILE *ptr;
|
||||
ptr=fopen(path,"rb");
|
||||
int location=ADDR_ROM;
|
||||
while(fread(&byte,1,1,ptr)==1){
|
||||
MemSet(location,byte,1);
|
||||
location++;
|
||||
}
|
||||
fclose(ptr);
|
||||
}
|
||||
|
||||
unsigned char DEFAULT_FONT[]={
|
||||
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
|
||||
0x20, 0x60, 0x20, 0x20, 0x70, // 1
|
||||
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
|
||||
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
|
||||
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
|
||||
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
|
||||
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
|
||||
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
|
||||
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
|
||||
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
|
||||
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
|
||||
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
|
||||
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
|
||||
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
|
||||
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
|
||||
0xF0, 0x80, 0xF0, 0x80, 0x80}; // F
|
10
src/mem.h
Normal file
10
src/mem.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#define ADDR_ROM 0x200
|
||||
#define ADDR_FONT 0x50
|
||||
|
||||
void MemInit();
|
||||
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);
|
|
@ -39,7 +39,7 @@ void ScreenUpdate(){
|
|||
EndDrawing();
|
||||
}
|
||||
|
||||
void ScreenSetPixel(int x, int y, char state){
|
||||
void ScreenSetPixel(int x, int y, unsigned char state){
|
||||
Screen.pixels[x+y*64]=(state==0) ? 0: 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define MODE_CHIP8 0 // Chip-8 64x32
|
||||
|
@ -13,6 +15,6 @@ typedef struct SCREEN_DATA {
|
|||
|
||||
void ScreenInit(int width, int height);
|
||||
void ScreenClear();
|
||||
void ScreenSetPixel(int x, int y, char state);
|
||||
void ScreenSetPixel(int x, int y, unsigned char state);
|
||||
void ScreenUpdate();
|
||||
void ScreenClose();
|
||||
|
|
52
src/vcpu.c
Normal file
52
src/vcpu.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "vcpu.h"
|
||||
#include "mem.h"
|
||||
#include "screen.h"
|
||||
|
||||
// Program Counter (16 bits but only 12 bits used (4096 memory addresses))
|
||||
unsigned short PC;
|
||||
|
||||
// Index register (16 bits but only 12 bits used (4096 memory addresses))
|
||||
unsigned short I;
|
||||
|
||||
// Stack register (16 bits)
|
||||
unsigned short S;
|
||||
|
||||
// General purpose registers (8 bits each)
|
||||
// Note last one often used as a flag register
|
||||
unsigned char R[16];
|
||||
|
||||
// Delay timer (8 bits)
|
||||
unsigned char DT;
|
||||
|
||||
// Sound timer (8 bits)
|
||||
unsigned char ST;
|
||||
|
||||
// Current VCPU state
|
||||
VCPU_State State;
|
||||
|
||||
void VCPUInit(){
|
||||
PC=ADDR_ROM;
|
||||
}
|
||||
|
||||
void VCPUFetch(){
|
||||
MemRead((char *)&(State.opcode),2,PC);
|
||||
PC+=2;
|
||||
}
|
||||
|
||||
void VCPUDecode(){
|
||||
char X=(State.opcode<<4) & 0xF0;
|
||||
char Y=(State.opcode<<8) & 0xF0;
|
||||
char N=(State.opcode<<12) & 0xF0;
|
||||
char NN=(State.opcode<<8) & 0xFF;
|
||||
short NNN=(State.opcode<<4) & 0xFFF0;
|
||||
}
|
||||
|
||||
void VCPUExecute(){
|
||||
switch(State.opcode){
|
||||
case 0x00E0:
|
||||
ScreenClear();
|
||||
break
|
||||
;;
|
||||
|
||||
}
|
||||
}
|
15
src/vcpu.h
Normal file
15
src/vcpu.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
typedef struct VCPU_State {
|
||||
short opcode;
|
||||
char X;
|
||||
char Y;
|
||||
char N;
|
||||
char NN;
|
||||
short NNN;
|
||||
} VCPU_State;
|
||||
|
||||
void VCPUInit();
|
||||
void VCPUFetch();
|
||||
void VCPUDecode();
|
||||
void VCPUExecute();
|
Loading…
Add table
Reference in a new issue