Minor changes
This commit is contained in:
parent
c6ddcbe2bd
commit
9550924042
5 changed files with 42 additions and 22 deletions
|
@ -17,9 +17,9 @@ int main(int argc, char *argv[])
|
|||
|
||||
ScreenInit(800,400);
|
||||
VCPUInit();
|
||||
// MemDump();
|
||||
//MemDump();
|
||||
int i=0;
|
||||
|
||||
|
||||
while (!WindowShouldClose()){
|
||||
VCPUFetch();
|
||||
VCPUDecode();
|
||||
|
|
14
src/mem.c
14
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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
18
src/screen.c
18
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){
|
||||
|
|
23
src/vcpu.c
23
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]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue