-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.h
136 lines (105 loc) · 4.02 KB
/
chip8.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
//
// Created by pazuzu on 14/04/19.
//
#ifndef CHIP8_CHIP8_H
#define CHIP8_CHIP8_H
#include <zconf.h>
#include <cstring>
#include <SFML/Audio.hpp>
class Chip8 {
private:
// registers V0 to VF
u_int8_t V[16];
// 4K RAM
u_int8_t RAM[0x1000];
// stack pointer
int SP;
// program counter
int PC;
// memory addr register
u_int16_t I;
// stack
u_int16_t stack[16];
// keys
bool keys[16];
int** display;
// timers
u_int8_t DT;
u_int8_t ST;
u_int8_t chip8font[0x50] = {
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
};
sf::Sound buzzer;
sf::SoundBuffer soundBuffer;
short getNNN(u_int16_t opcode);
short getByte(u_int16_t opcode);
short getX(u_int16_t opcode);
short getY(u_int16_t opcode);
short getN(u_int16_t opcode);
void clearDisplay();
public:
Chip8()
{
// initialize registers and stack
memset(V, 0, sizeof(V));
memset(stack, 0, sizeof(stack));
memset(RAM, 0, sizeof(RAM));
SP = 0;
PC = 0x200;
I = 0;
DT = 0;
ST = 0;
// initialize display
display = new int*[32];
for (int i = 0; i < 32; i ++)
{
display[i] = new int[64];
for (int j = 0; j < 64; j ++)
{
display[i][j] = 0;
}
}
// put font set into RAM
for (int i = 0; i < 0x50; i ++)
{
RAM[i] = chip8font[i];
}
// initialize sound
soundBuffer.loadFromFile("../sound/tone.wav");
buzzer.setBuffer(soundBuffer);
buzzer.setLoop(true);
// seed randomness
srand(time(NULL));
}
u_int8_t* getRAM() { return RAM; }
int* getPC() { return &PC; }
int* getSP() { return &SP; }
u_int8_t* getDT() { return &DT; }
u_int8_t* getST() { return &ST; }
void decrementDT() { DT --; }
void decrementST() { ST --; }
int** getDisplay() { return display; }
u_int8_t* getRegisterV(int index) { return &this->V[index]; }
u_int16_t* getRegisterI() { return &this->I; }
sf::Sound* getBuzzer() { return &buzzer; }
void setKeyState(bool state, int key) { keys[key] = state; }
void increasePC() { PC += 1; }
void resetPC() { PC = 0x200; }
void emulateCycle();
};
#endif //CHIP8_CHIP8_H