summaryrefslogtreecommitdiff
path: root/src/vcpu.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vcpu.c')
-rw-r--r--src/vcpu.c45
1 files changed, 30 insertions, 15 deletions
diff --git a/src/vcpu.c b/src/vcpu.c
index 4bfd86b..fb73eeb 100644
--- a/src/vcpu.c
+++ b/src/vcpu.c
@@ -12,7 +12,11 @@
// Current VCPU state
VCPU_State State;
-void VCPUInit(){
+void VCPUInit(int width, int height){
+ MemInit();
+ ScreenInit(width,height);
+ SpeakerInit();
+
State.PC=ADDR_ROM;
State.S=0;
State.dt_ticks=0;
@@ -22,6 +26,11 @@ void VCPUInit(){
srand(time(NULL));
}
+void VCPUFinish(){
+ SpeakerFinish();
+ ScreenClose();
+}
+
void VCPUFetch(){
unsigned char byte[2];
MemLoad(byte,2,State.PC); // Little indian to -1 no +1
@@ -64,7 +73,7 @@ void VCPUDoubleDabble(unsigned char x, unsigned char *u, unsigned char *t, unsig
}
void VCPUExecute(){
- // VCPUDump();
+ // Very big switch case... Thinking about it, ifelse would have been cleaner for the reader
switch(State.opcode >> 12){
case 0x0: // Clear screen or return from subroutine
if(State.N == 0x0){ // Clear screen
@@ -259,7 +268,7 @@ void VCPUExecute(){
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]);
+ // printf("hundreds:%d tens:%d units:%d byte:%d\n",hundreds,tens,units,State.V[State.X]);
break;
case 0x55:
@@ -279,32 +288,29 @@ void VCPUTick(){
struct timespec start, stop;
double duration, delay;
+ // Start CPU pipeline instrumentation
+ clock_gettime(CLOCK_REALTIME, &start);
+
// Update keypressed
int key=KeypadGetPressed();
if(key>=0){
State.keypressed=1;
State.key=key&0xF;
- //printf("Keypressed: %x\n",State.key);
+ // printf("Keypressed: %x\n",State.key);
}
- else
+ else{
State.keypressed=0;
+ }
- // Run and benchmark CPU pipeline
- clock_gettime(CLOCK_REALTIME, &start);
+ // Execute next instruction
VCPUFetch();
VCPUDecode();
VCPUExecute();
+
+ // Update ticks
State.dt_ticks++;
State.st_ticks++;
State.screen_ticks++;
- clock_gettime(CLOCK_REALTIME, &stop);
-
- // Adjust pipeline duration
- duration=(stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec)*1e-9;
- delay=1.0/VCPU_FREQ-duration;
- if(delay>0){
- usleep(delay*1e6);
- }
// Update Delay Timer
if(State.dt_ticks>=(1.0*VCPU_FREQ/DTST_FREQ)){
@@ -330,6 +336,15 @@ void VCPUTick(){
ScreenUpdate();
}
+ // End instrumentation
+ clock_gettime(CLOCK_REALTIME, &stop);
+
+ // Adjust pipeline duration
+ duration=(stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec)*1e-9;
+ delay=1.0/VCPU_FREQ-duration;
+ if(delay>0){
+ usleep(delay*1e6);
+ }
}
void VCPUDump(){