Minor changes
This commit is contained in:
parent
8d56e4a953
commit
5a6aecc0f5
1 changed files with 119 additions and 1 deletions
120
src/vcpu.c
120
src/vcpu.c
|
@ -2,6 +2,8 @@
|
|||
#include "mem.h"
|
||||
#include "screen.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
// Current VCPU state
|
||||
VCPU_State State;
|
||||
|
@ -9,6 +11,7 @@ VCPU_State State;
|
|||
void VCPUInit(){
|
||||
State.PC=ADDR_ROM;
|
||||
State.S=0;
|
||||
srand(time(NULL));
|
||||
}
|
||||
|
||||
void VCPUFetch(){
|
||||
|
@ -65,7 +68,7 @@ void VCPUExecute(){
|
|||
State.PC+=2;
|
||||
break;
|
||||
;;
|
||||
case 0x5: // SNE: VX, VY
|
||||
case 0x5: // SE: VX, VY
|
||||
if(State.V[State.X]==State.V[State.Y])
|
||||
State.PC+=2;
|
||||
break;
|
||||
|
@ -78,10 +81,81 @@ void VCPUExecute(){
|
|||
State.V[State.X]+=State.NN;
|
||||
break
|
||||
;;
|
||||
case 0x8: // Register operations
|
||||
switch(State.N){
|
||||
case 0x1: // VX = VX OR VY
|
||||
State.V[State.X]=State.V[State.X] | State.V[State.Y];
|
||||
break;
|
||||
;;
|
||||
case 0x2: // VX = VX AND VY
|
||||
State.V[State.X]=State.V[State.X] & State.V[State.Y];
|
||||
break;
|
||||
;;
|
||||
case 0x3: // VX = VX XOR VY
|
||||
State.V[State.X]=State.V[State.X] ^ State.V[State.Y];
|
||||
break;
|
||||
;;
|
||||
case 0x4: // VX = VX + VY
|
||||
if(State.V[State.X] + State.V[State.Y] > 255)
|
||||
State.V[REG_FLAG]=1;
|
||||
else
|
||||
State.V[REG_FLAG]=0;
|
||||
State.V[State.X]=(State.V[State.X] + State.V[State.Y]) & 0xFF;
|
||||
break;
|
||||
;;
|
||||
case 0x5: // VX = VX - VY
|
||||
if(State.V[State.X] > State.V[State.Y])
|
||||
State.V[REG_FLAG]=1;
|
||||
else
|
||||
State.V[REG_FLAG]=0;
|
||||
State.V[State.X]=State.V[State.X] - State.V[State.Y];
|
||||
break;
|
||||
;;
|
||||
case 0x6: // VX = VX SHR 1
|
||||
if(State.V[State.X] & 0x1 == 1)
|
||||
State.V[REG_FLAG]=1;
|
||||
else
|
||||
State.V[REG_FLAG]=0;
|
||||
State.V[State.X]=State.V[State.X] >> 1;
|
||||
break;
|
||||
;;
|
||||
case 0x7: // VX = VY - VX
|
||||
if(State.V[State.X] < State.V[State.Y])
|
||||
State.V[REG_FLAG]=1;
|
||||
else
|
||||
State.V[REG_FLAG]=0;
|
||||
State.V[State.X]=State.V[State.Y] - State.V[State.X];
|
||||
break;
|
||||
;;
|
||||
case 0xE: // VX = VX SHL 1
|
||||
if(State.V[State.X] >> 15 == 1)
|
||||
State.V[REG_FLAG]=1;
|
||||
else
|
||||
State.V[REG_FLAG]=0;
|
||||
State.V[State.X]=State.V[State.X] << 1;
|
||||
break;
|
||||
;;
|
||||
}
|
||||
break
|
||||
;;
|
||||
case 0x9: // SNE: VX, VY
|
||||
if(State.V[State.X]!=State.V[State.Y])
|
||||
State.PC+=2;
|
||||
break;
|
||||
;;
|
||||
case 0xA:
|
||||
State.I=State.NNN;
|
||||
break
|
||||
;;
|
||||
case 0xB:
|
||||
State.PC=State.V[0]+State.NNN;
|
||||
break
|
||||
;;
|
||||
case 0xC:
|
||||
unsigned short n = rand() % 255 + 1;
|
||||
State.V[State.X]=n & State.NN;
|
||||
break
|
||||
;;
|
||||
case 0xD: // Draw a sprite
|
||||
int X=State.V[State.X]%63;
|
||||
int Y=State.V[State.Y]%31;
|
||||
|
@ -105,6 +179,50 @@ void VCPUExecute(){
|
|||
}
|
||||
break;
|
||||
;;
|
||||
case 0xE:
|
||||
// TODO
|
||||
break;;
|
||||
case 0xF:
|
||||
switch(State.NN){
|
||||
case 0x07: // Get timer
|
||||
State.V[State.X]=State.DT;
|
||||
break;
|
||||
;;
|
||||
case 0x0A:
|
||||
// TODO
|
||||
break;
|
||||
;;
|
||||
case 0x15: // Set timer
|
||||
State.DT=State.V[State.X];
|
||||
break;
|
||||
;;
|
||||
case 0x18: // Set sound timer
|
||||
State.ST=State.V[State.X];
|
||||
break;
|
||||
;;
|
||||
case 0x1E: // I = I + VX
|
||||
State.I=State.I+State.V[State.X];
|
||||
break;
|
||||
;;
|
||||
case 0x29:
|
||||
// TODO
|
||||
break;
|
||||
;;
|
||||
case 0x33:
|
||||
// TODO
|
||||
break;
|
||||
;;
|
||||
case 0x55:
|
||||
MemCopy(State.V,0xF,State.I);
|
||||
break;
|
||||
;;
|
||||
case 0x65:
|
||||
MemRead(State.V,0xF,State.I);
|
||||
break;
|
||||
;;
|
||||
}
|
||||
break;
|
||||
;;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue