-
Notifications
You must be signed in to change notification settings - Fork 0
/
Machine.h
154 lines (126 loc) · 3.72 KB
/
Machine.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <vector>
#ifndef MACHINE_H_
#define MACHINE_H_
class Machine {
private:
// index which points to current operation
unsigned short pc;
unsigned short opcode;
// 16 8-bit registers named V0 - VF
unsigned char registers [16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
// one 12-bit address/index register named I
unsigned short address_register;
// 4096 bytes of memory
unsigned char memory [4096];
std::vector <unsigned short> stack;
// number font
unsigned char font [80] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
// CPU functions:
void fetchDecodeAndExecute();
// OPCODES: 0xABCD
// 0x00E0
void clearDisplay();
// 0x00EE
void returnFromSubroutine();
// 0x1NNN
void jump();
// 0x2NNN
void call();
// 0x3XNN
void skipEquals(unsigned char b, unsigned char c, unsigned char d);
// 0x4XNN
void skipNotEquals(unsigned char b, unsigned char c, unsigned char d);
// 0x5XY0
void skipEqualsXY(unsigned char b, unsigned char c);
// 0x6XNN
void setXNN(unsigned char b, unsigned char c, unsigned char d);
// 0x7XNN
void addXNN(unsigned char b, unsigned char c, unsigned char d);
// 0x8XY0
void setXY(unsigned char b, unsigned char c);
// 0x8XY1
void setOR(unsigned char b, unsigned char c);
// 0x8XY2
void setAND(unsigned char b, unsigned char c);
// 0x8XY3
void setXOR(unsigned char b, unsigned char c);
// 0x8XY4
void addOverflow(unsigned char b, unsigned char c);
// 0x8XY5
void subtractUnderflow(unsigned char b, unsigned char c);
// 0x8XY6
void rightShift(unsigned char b);
// 0x8XY7
void reverseSubtractUnderflow(unsigned char b, unsigned char c);
// 0x8XYE
void leftShift(unsigned char b);
// 0x9XY0
void skipNotEqualsXY(unsigned char b, unsigned char c);
// 0xANNN
void setAddress(unsigned char b, unsigned char c, unsigned char d);
// 0xBNNN
void jumpV0(unsigned char b, unsigned char c, unsigned char d);
// 0xCXNN
void setRandomAND(unsigned char b, unsigned char c, unsigned char d);
// 0xDXYN
void draw(unsigned char b, unsigned char c, unsigned char d);
// 0xEX9E
void skipKeyPressed(unsigned char b);
// 0xEXA1
void skipKeyNotPressed(unsigned char b);
// 0xFX07
void setXDelay(unsigned char b);
// 0xFX0A
void awaitKeyPress(unsigned char b);
// 0xFX15
void setDelayTimer(unsigned char b);
// 0xFX18
void setSoundTimer(unsigned char b);
// 0xFX1E
void addAddressRegister(unsigned char b);
// 0xFX29
void fontCharacter(unsigned char b);
// 0xFX33
void binaryCodedDecimal(unsigned char b);
// 0xFX55
void registerDump(unsigned char b);
// 0xFX65
void registerLoad(unsigned char b);
public:
// execute 1 CPU cycle
// about 700 instructions per second
void tick();
// constructor
Machine();
// INIT functions:
void readFileIntoMemory(const char* path);
// Key function
void setKeysToFalse();
// width x height = 64 x 32 pixels; value 1 = white; value 0 = blue;
bool pixels [32] [64]; // (x,y) at pixels[y][x]
const int PIXEL_WIDTH = 64;
const int PIXEL_HEIGHT = 32;
// 16 keys 0 through F pressed
bool keys [16] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
// decrements at 60 Hz while not zero
unsigned char delay_timer;
unsigned char sound_timer;
};
#endif /* MACHINE_H_ */