2023-12-25 07:24:17 +01:00
|
|
|
#include "vcpu.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "screen.h"
|
2023-12-25 09:11:45 +01:00
|
|
|
#include <stdio.h>
|
2023-12-25 07:24:17 +01:00
|
|
|
|
|
|
|
// Current VCPU state
|
|
|
|
VCPU_State State;
|
|
|
|
|
|
|
|
void VCPUInit(){
|
2023-12-25 07:32:11 +01:00
|
|
|
State.PC=ADDR_ROM;
|
2023-12-25 07:24:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VCPUFetch(){
|
2023-12-25 09:11:45 +01:00
|
|
|
unsigned char byte[2];
|
|
|
|
MemRead(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];
|
2023-12-25 07:32:11 +01:00
|
|
|
State.PC+=2;
|
2023-12-25 07:24:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VCPUDecode(){
|
2023-12-25 09:22:19 +01:00
|
|
|
State.X=(State.opcode>>8) & 0xF;
|
|
|
|
State.Y=(State.opcode>>4) & 0xF;
|
|
|
|
State.N=State.opcode & 0xF;
|
|
|
|
|
|
|
|
State.NN=State.Y;
|
|
|
|
State.NN=State.NN<<4;
|
|
|
|
State.NN=State.NN | State.N;
|
|
|
|
|
|
|
|
State.NNN=State.opcode&0x0FFF;
|
2023-12-25 07:24:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VCPUExecute(){
|
2023-12-25 09:22:19 +01:00
|
|
|
// VCPUDump();
|
2023-12-25 09:11:45 +01:00
|
|
|
switch(State.opcode >> 12){
|
2023-12-25 07:30:17 +01:00
|
|
|
case 0x0:
|
2023-12-25 07:24:17 +01:00
|
|
|
ScreenClear();
|
|
|
|
break
|
|
|
|
;;
|
2023-12-25 07:30:17 +01:00
|
|
|
case 0x1:
|
2023-12-25 07:32:11 +01:00
|
|
|
State.PC=State.NNN;
|
2023-12-25 07:30:17 +01:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
case 0x6:
|
2023-12-25 07:32:11 +01:00
|
|
|
State.V[State.X]=State.NN;
|
2023-12-25 07:30:17 +01:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
case 0x7:
|
2023-12-25 07:32:11 +01:00
|
|
|
State.V[State.X]+=State.NN;
|
2023-12-25 07:30:17 +01:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
case 0xA:
|
2023-12-25 07:32:11 +01:00
|
|
|
State.I=State.NNN;
|
2023-12-25 07:30:17 +01:00
|
|
|
break
|
|
|
|
;;
|
2023-12-25 09:11:45 +01:00
|
|
|
case 0xD:
|
|
|
|
int X=State.V[State.X]%63;
|
|
|
|
int Y=State.V[State.Y]%31;
|
|
|
|
State.V[0xF]=0; // Set flag to 0
|
|
|
|
for(char row=0;row<State.N;row++){
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
;;
|
2023-12-25 07:24:17 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-25 09:11:45 +01:00
|
|
|
|
|
|
|
void VCPUDump(){
|
|
|
|
printf("opcode: 0x%04x\n",State.opcode&0xFFFF);
|
2023-12-25 09:22:19 +01:00
|
|
|
printf("X: 0x%01x\n",State.X);
|
|
|
|
printf("Y: 0x%01x\n",State.Y);
|
|
|
|
printf("N: 0x%01x\n",State.N);
|
|
|
|
printf("NN: 0x%02x\n",State.NN);
|
|
|
|
printf("NNN: 0x%03x\n",State.NNN);
|
2023-12-25 09:11:45 +01:00
|
|
|
}
|