summaryrefslogtreecommitdiff
path: root/src/vcpu.h
blob: 09bfd4eb20e42ac59fe8637cd0b641e886cd6826 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once

#define VCPU_FREQ 600
#define DTST_FREQ 60
#define SCREEN_FREQ 60
#define REG_FLAG 0xF

/**
 * @brief Store the entire VCPU state
 * 
 */
typedef struct VCPU_State {
  /// @brief Program Counter (16 bits but only 12 bits used (4096 memory addresses))
  unsigned short PC;

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

  /// @brief Stack register (16 bits)
  unsigned short S;
  unsigned short stack[100]; // Emulated stack

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

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

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

  /// @brief Intruction (opcode + decoded fields)
  unsigned short opcode;
  unsigned char X;
  unsigned char Y;
  unsigned char N;
  unsigned char NN;
  unsigned short NNN;

  /// @brief Keypressed
  int keypressed; // Not 0 if a key was pressed
  unsigned char key;
  
  /// @brief Count VCPU ticks
  int dt_ticks;
  int st_ticks;
  int screen_ticks;
} VCPU_State;

/**
 * @brief Must be called first!
 * 
 */
void VCPUInit(int width, int height);

/**
 * @brief Fetch instruction from memory
 * 
 */
void VCPUFetch();

/**
 * @brief Decode instruction from the last fetch
 * 
 */
void VCPUDecode();

/**
 * @brief Execute decode instruction
 * 
 */
void VCPUExecute();

/**
 * @brief Fetch, decode and execute an instruction
 * 
 */
void VCPUTick();

/**
 * @brief Simple (a bit) binary to BCD convertion
 * 
 * @param x 
 * @param u 
 * @param t 
 * @param h 
 */
void VCPUDoubleDabble(unsigned char x, unsigned char *u, unsigned char *t, unsigned char *h);

/**
 * @brief Dump VCPU state
 * 
 */
void VCPUDump();

/**
 * @brief To call before terminating the application
 *
 */
void VCPUFinish();