summaryrefslogtreecommitdiff
path: root/src/vcpu.c
blob: f38ba0147366df1ec4bf98bef8fa351e729c6422 (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
#include "vcpu.h"
#include "mem.h"
#include "screen.h"

// Program Counter (16 bits but only 12 bits used (4096 memory addresses))
unsigned short PC;

// Index register (16 bits but only 12 bits used (4096 memory addresses))
unsigned short I;

// Stack register (16 bits)
unsigned short S;

// General purpose registers (8 bits each)
// Note last one often used as a flag register
unsigned char V[16];

// Delay timer (8 bits)
unsigned char DT;

// Sound timer (8 bits)
unsigned char ST;

// Current VCPU state
VCPU_State State;

void VCPUInit(){
  PC=ADDR_ROM;
}

void VCPUFetch(){
  MemRead((char *)&(State.opcode),2,PC);
  PC+=2;
}

void VCPUDecode(){
  State.X=(State.opcode<<4) & 0xF0;
  State.Y=(State.opcode<<8) & 0xF0;
  State.N=(State.opcode<<12) & 0xF0;
  State.NN=(State.opcode<<8) & 0xFF;
  State.NNN=(State.opcode<<4) & 0xFFF0;
}

void VCPUExecute(){
  switch(State.opcode & 0xF){
  case 0x0:
    ScreenClear();
    break
      ;;
  case 0x1:
    PC=State.NNN;
    break
      ;;
  case 0x6:
    V[State.X]=State.NN;
    break
      ;;
  case 0x7:
    V[State.X]+=State.NN;
    break
      ;;
  case 0xA:
    I=State.NNN;
    break
      ;;

  }
}