summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c4
-rw-r--r--src/mem.c14
-rw-r--r--src/mem.h5
-rw-r--r--src/screen.c18
-rw-r--r--src/vcpu.c23
5 files changed, 42 insertions, 22 deletions
diff --git a/src/main.c b/src/main.c
index 8903eed..b870fab 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,9 +17,9 @@ int main(int argc, char *argv[])
ScreenInit(800,400);
VCPUInit();
- // MemDump();
+ //MemDump();
int i=0;
-
+
while (!WindowShouldClose()){
VCPUFetch();
VCPUDecode();
diff --git a/src/mem.c b/src/mem.c
index 12b039f..ec34750 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -7,7 +7,7 @@ extern unsigned char DEFAULT_FONT[];
void MemInit(){
MemSet(0,0,4096);
- MemCopy(DEFAULT_FONT,16*5,ADDR_FONT);
+ MemStore(DEFAULT_FONT,16*5,ADDR_FONT);
}
void MemSet(int addr, unsigned char value, int size){
@@ -15,12 +15,12 @@ void MemSet(int addr, unsigned char value, int size){
memory[addr+i]=value;
}
-void MemCopy(unsigned char *data, int size, int addr){
+void MemStore(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){
+void MemLoad(unsigned char *data, int size, int addr){
int location=addr;
for(int i=0;i<size;i++){
data[i]=memory[addr+i];
@@ -33,6 +33,8 @@ void MemLoadROM(char *path){
ptr=fopen(path,"rb");
int location=ADDR_ROM;
while(fread(&byte,1,1,ptr)==1){
+ if(location >= 4096)
+ break;
MemSet(location,byte,1);
location++;
}
@@ -40,7 +42,11 @@ void MemLoadROM(char *path){
}
void MemDump(){
- for(int addr=0;addr<4096;addr+=2){
+ MemDumpRange(0,4096);
+}
+
+void MemDumpRange(int from, int size){
+ for(int addr=from;addr<from+size;addr+=2){
printf("0x%03x: %02x %02x\n",addr,memory[addr], memory[addr+1]);
}
}
diff --git a/src/mem.h b/src/mem.h
index 3f5eb95..8e4136a 100644
--- a/src/mem.h
+++ b/src/mem.h
@@ -5,7 +5,8 @@
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 MemStore(unsigned char *data, int size, int addr);
+void MemLoad(unsigned char *data, int size, int addr);
void MemLoadROM(char *path);
void MemDump();
+void MemDumpRange(int addr, int size);
diff --git a/src/screen.c b/src/screen.c
index f4eb7ae..fa4a9c4 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -41,10 +41,20 @@ void ScreenUpdate(){
}
char ScreenPixelApply(int x, int y, unsigned char state){
- if(Screen.pixels[x+y*64] != 0 && state != 0)
- return 1;
- Screen.pixels[x+y*64]=state;
- return 0;
+ char flag=0;
+
+ // Toggle pixel if state is on
+ if(state){
+ if(Screen.pixels[x+y*64]){
+ Screen.pixels[x+y*64]=0;
+ flag=1;
+ }
+ else{
+ Screen.pixels[x+y*64]=1;
+ }
+ }
+
+ return flag;
}
void ScreenPixelFlip(int x, int y){
diff --git a/src/vcpu.c b/src/vcpu.c
index c3e1e5e..bd255ef 100644
--- a/src/vcpu.c
+++ b/src/vcpu.c
@@ -16,7 +16,7 @@ void VCPUInit(){
void VCPUFetch(){
unsigned char byte[2];
- MemRead(byte,2,State.PC); // Little indian to -1 no +1
+ MemLoad(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];
@@ -56,7 +56,7 @@ void VCPUDoubleDabble(unsigned char x, unsigned char *u, unsigned char *t, unsig
}
void VCPUExecute(){
- VCPUDump();
+ // VCPUDump();
switch(State.opcode >> 12){
case 0x0: // Clear screen or return from subroutine
if(State.N == 0x0){ // Clear screen
@@ -185,17 +185,17 @@ void VCPUExecute(){
break;
case 0xD: // Draw a sprite
- int X=State.V[State.X]&63;
- int Y=State.V[State.Y]&31;
State.V[REG_FLAG]=0; // Set flag to 0
int width, height;
ScreenWH(&width,&height);
+ int X=State.V[State.X]%width;
+ int Y=State.V[State.Y]%height;
for(char row=0;row<State.N;row++){
// Stop if row out of screen
if(Y+row>=height)
break;
unsigned char sprite;
- MemRead(&sprite,1,State.I+row); // Load sprite
+ MemLoad(&sprite,1,State.I+row); // Load sprite
// Draw sprite
for(int shift=0;shift<8;shift++){
// Stop if column is out of screen
@@ -240,18 +240,18 @@ void VCPUExecute(){
case 0x33:
unsigned char units, tens, hundreds;
VCPUDoubleDabble(State.V[State.X],&units,&tens,&hundreds);
- MemCopy(&hundreds,1,State.I);
- MemCopy(&tens,1,State.I+1);
- MemCopy(&units,1,State.I+2);
+ MemStore(&hundreds,1,State.I);
+ MemStore(&tens,1,State.I+1);
+ MemStore(&units,1,State.I+2);
// printf("hundreds:%d tens:%d units:%d byte:%d\n",hundreds,tens,units,State.V[State.X]);
break;
case 0x55:
- MemCopy(State.V,0xF,State.I);
+ MemStore(State.V,16,State.I);
break;
case 0x65:
- MemRead(State.V,0xF,State.I);
+ MemLoad(State.V,16,State.I);
break;
}
@@ -266,4 +266,7 @@ void VCPUDump(){
printf("N: 0x%01x\n",State.N);
printf("NN: 0x%02x\n",State.NN);
printf("NNN: 0x%03x\n",State.NNN);
+ for(int i=0;i<16;i++){
+ printf("V%d: 0x%02x\n",i,State.V[i]);
+ }
}