summaryrefslogtreecommitdiff
path: root/src/vcpu.c
blob: 45b9acaeb31963fda491fb1479aa5f9fa4ccc1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "vcpu.h"
#include "mem.h"
#include "screen.h"
#include <stdio.h>

// Current VCPU state
VCPU_State State;

void VCPUInit(){
  State.PC=ADDR_ROM;
}

void VCPUFetch(){
  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];
  State.PC+=2;
}

void VCPUDecode(){
  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;
}

void VCPUExecute(){
  //  VCPUDump();
  switch(State.opcode >> 12){
  case 0x0:
    ScreenClear();
    break
      ;;
  case 0x1:
    State.PC=State.NNN;
    break
      ;;
  case 0x6:
    State.V[State.X]=State.NN;
    break
      ;;
  case 0x7:
    State.V[State.X]+=State.NN;
    break
      ;;
  case 0xA:
    State.I=State.NNN;
    break
      ;;
  case 0xD:
    int X=State.V[State.X]%63;
    int Y=State.V[State.Y]%31;
    State.V[REG_FLAG]=0; // Set flag to 0
    for(char row=0;row<State.N;row++){
      
    }
    break;
    ;;
  }
}

void VCPUDump(){
  printf("opcode: 0x%04x\n",State.opcode&0xFFFF);
  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);
}